2 * Copyright © 2012 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcssimagelinearprivate.h"
26 #include "gtkcssnumbervalueprivate.h"
27 #include "gtkcssrgbavalueprivate.h"
28 #include "gtkcssprovider.h"
29 #include "gtksymboliccolorprivate.h"
31 G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
34 gtk_css_image_linear_get_start_end (GtkCssImageLinear *linear,
39 GtkCssImageLinearColorStop *stop;
43 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
44 if (stop->offset == NULL)
47 *start = _gtk_css_number_value_get (stop->offset, length) / length;
51 for (i = 0; i < linear->stops->len; i++)
53 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
55 if (stop->offset == NULL)
58 pos = _gtk_css_number_value_get (stop->offset, length) / length;
60 *end = MAX (pos, *end);
63 if (stop->offset == NULL)
64 *end = MAX (*end, 1.0);
68 gtk_css_image_linear_compute_start_point (double angle_in_degrees,
74 double angle, c, slope, perpendicular;
76 angle = fmod (angle_in_degrees, 360);
92 else if (angle == 180)
98 else if (angle == 270)
105 /* The tan() is confusing because the angle is clockwise
107 perpendicular = tan (angle * G_PI / 180);
108 slope = -1 / perpendicular;
112 if (angle < 90 || angle > 270)
115 /* Compute c (of y = mx + c) of perpendicular */
116 c = height - perpendicular * width;
118 *x = c / (slope - perpendicular);
119 *y = perpendicular * *x + c;
123 gtk_css_image_linear_draw (GtkCssImage *image,
128 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
129 cairo_pattern_t *pattern;
130 double x, y; /* coordinates of start point */
131 double length; /* distance in pixels for 100% */
132 double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
136 if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
138 guint side = _gtk_css_number_value_get (linear->angle, 100);
140 if (side & (1 << GTK_CSS_RIGHT))
142 else if (side & (1 << GTK_CSS_LEFT))
147 if (side & (1 << GTK_CSS_TOP))
149 else if (side & (1 << GTK_CSS_BOTTOM))
156 gtk_css_image_linear_compute_start_point (_gtk_css_number_value_get (linear->angle, 100),
161 length = sqrt (x * x + y * y);
162 gtk_css_image_linear_get_start_end (linear, length, &start, &end);
163 pattern = cairo_pattern_create_linear (x * (start - 0.5), y * (start - 0.5),
164 x * (end - 0.5), y * (end - 0.5));
165 if (linear->repeating)
166 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
168 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
172 for (i = 0; i < linear->stops->len; i++)
174 GtkCssImageLinearColorStop *stop;
177 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
179 if (stop->offset == NULL)
183 else if (i + 1 == linear->stops->len)
189 pos = _gtk_css_number_value_get (stop->offset, length) / length;
191 pos = MAX (pos, offset);
192 step = (pos - offset) / (i - last);
193 for (last = last + 1; last <= i; last++)
197 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);
199 rgba = _gtk_css_rgba_value_get_rgba (stop->color);
202 cairo_pattern_add_color_stop_rgba (pattern,
203 (offset - start) / (end - start),
214 cairo_rectangle (cr, 0, 0, width, height);
215 cairo_translate (cr, width / 2, height / 2);
216 cairo_set_source (cr, pattern);
219 cairo_pattern_destroy (pattern);
224 gtk_css_image_linear_parse (GtkCssImage *image,
225 GtkCssParser *parser)
227 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
230 if (_gtk_css_parser_try (parser, "repeating-linear-gradient(", TRUE))
231 linear->repeating = TRUE;
232 else if (_gtk_css_parser_try (parser, "linear-gradient(", TRUE))
233 linear->repeating = FALSE;
236 _gtk_css_parser_error (parser, "Not a linear gradient");
240 if (_gtk_css_parser_try (parser, "to", TRUE))
244 for (i = 0; i < 2; i++)
246 if (_gtk_css_parser_try (parser, "left", TRUE))
248 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
250 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
253 side |= (1 << GTK_CSS_LEFT);
255 else if (_gtk_css_parser_try (parser, "right", TRUE))
257 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
259 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
262 side |= (1 << GTK_CSS_RIGHT);
264 else if (_gtk_css_parser_try (parser, "top", TRUE))
266 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
268 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
271 side |= (1 << GTK_CSS_TOP);
273 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
275 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
277 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
280 side |= (1 << GTK_CSS_BOTTOM);
288 _gtk_css_parser_error (parser, "Expected side that gradient should go to");
292 linear->angle = _gtk_css_number_value_new (side, GTK_CSS_NUMBER);
294 if (!_gtk_css_parser_try (parser, ",", TRUE))
296 _gtk_css_parser_error (parser, "Expected a comma");
300 else if (_gtk_css_parser_has_number (parser))
302 linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
303 if (linear->angle == NULL)
306 if (!_gtk_css_parser_try (parser, ",", TRUE))
308 _gtk_css_parser_error (parser, "Expected a comma");
313 linear->angle = _gtk_css_number_value_new (1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
316 GtkCssImageLinearColorStop stop;
318 stop.color = _gtk_css_symbolic_value_new (parser);
319 if (stop.color == NULL)
322 if (_gtk_css_parser_has_number (parser))
324 stop.offset = _gtk_css_number_value_parse (parser,
325 GTK_CSS_PARSE_PERCENT
326 | GTK_CSS_PARSE_LENGTH);
327 if (stop.offset == NULL)
329 _gtk_css_value_unref (stop.color);
338 g_array_append_val (linear->stops, stop);
340 } while (_gtk_css_parser_try (parser, ",", TRUE));
342 if (!_gtk_css_parser_try (parser, ")", TRUE))
344 _gtk_css_parser_error (parser, "Missing closing bracket at end of linear gradient");
352 gtk_css_image_linear_print (GtkCssImage *image,
355 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
358 if (linear->repeating)
359 g_string_append (string, "repeating-linear-gradient(");
361 g_string_append (string, "linear-gradient(");
363 if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
365 guint side = _gtk_css_number_value_get (linear->angle, 100);
367 if (side != (1 << GTK_CSS_BOTTOM))
369 g_string_append (string, "to");
371 if (side & (1 << GTK_CSS_TOP))
372 g_string_append (string, " top");
373 else if (side & (1 << GTK_CSS_BOTTOM))
374 g_string_append (string, " bottom");
376 if (side & (1 << GTK_CSS_LEFT))
377 g_string_append (string, " left");
378 else if (side & (1 << GTK_CSS_RIGHT))
379 g_string_append (string, " right");
381 g_string_append (string, ", ");
386 _gtk_css_value_print (linear->angle, string);
387 g_string_append (string, ", ");
390 for (i = 0; i < linear->stops->len; i++)
392 GtkCssImageLinearColorStop *stop;
395 g_string_append (string, ", ");
397 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
399 _gtk_css_value_print (stop->color, string);
403 g_string_append (string, " ");
404 _gtk_css_value_print (stop->offset, string);
408 g_string_append (string, ")");
412 gtk_css_image_linear_compute (GtkCssImage *image,
414 GtkStyleProviderPrivate *provider,
415 GtkCssComputedValues *values,
416 GtkCssComputedValues *parent_values,
417 GtkCssDependencies *dependencies)
419 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
420 GtkCssImageLinear *copy;
423 copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
424 copy->repeating = linear->repeating;
426 copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, values, parent_values, dependencies);
428 g_array_set_size (copy->stops, linear->stops->len);
429 for (i = 0; i < linear->stops->len; i++)
431 GtkCssImageLinearColorStop *stop, *scopy;
432 GtkCssDependencies child_deps;
434 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
435 scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i);
437 scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, values, parent_values, &child_deps);
438 *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
442 scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, values, parent_values, &child_deps);
443 *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
447 scopy->offset = NULL;
451 return GTK_CSS_IMAGE (copy);
455 gtk_css_image_linear_transition (GtkCssImage *start_image,
456 GtkCssImage *end_image,
460 GtkCssImageLinear *start, *end, *result;
463 start = GTK_CSS_IMAGE_LINEAR (start_image);
465 if (end_image == NULL)
467 if (!GTK_IS_CSS_IMAGE_LINEAR (end_image))
468 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
470 end = GTK_CSS_IMAGE_LINEAR (end_image);
472 if ((start->repeating != end->repeating)
473 || (start->stops->len != end->stops->len))
474 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
476 result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
477 result->repeating = start->repeating;
479 result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
480 if (result->angle == NULL)
483 for (i = 0; i < start->stops->len; i++)
485 GtkCssImageLinearColorStop stop, *start_stop, *end_stop;
487 start_stop = &g_array_index (start->stops, GtkCssImageLinearColorStop, i);
488 end_stop = &g_array_index (end->stops, GtkCssImageLinearColorStop, i);
490 if ((start_stop->offset != NULL) != (end_stop->offset != NULL))
493 if (start_stop->offset == NULL)
499 stop.offset = _gtk_css_value_transition (start_stop->offset,
503 if (stop.offset == NULL)
507 stop.color = _gtk_css_value_transition (start_stop->color,
511 if (stop.color == NULL)
514 _gtk_css_value_unref (stop.offset);
518 g_array_append_val (result->stops, stop);
521 return GTK_CSS_IMAGE (result);
524 g_object_unref (result);
525 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
529 gtk_css_image_linear_dispose (GObject *object)
531 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
535 g_array_free (linear->stops, TRUE);
536 linear->stops = NULL;
541 _gtk_css_value_unref (linear->angle);
542 linear->angle = NULL;
545 G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
549 _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
551 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
552 GObjectClass *object_class = G_OBJECT_CLASS (klass);
554 image_class->draw = gtk_css_image_linear_draw;
555 image_class->parse = gtk_css_image_linear_parse;
556 image_class->print = gtk_css_image_linear_print;
557 image_class->compute = gtk_css_image_linear_compute;
558 image_class->transition = gtk_css_image_linear_transition;
560 object_class->dispose = gtk_css_image_linear_dispose;
564 gtk_css_image_clear_color_stop (gpointer color_stop)
566 GtkCssImageLinearColorStop *stop = color_stop;
568 _gtk_css_value_unref (stop->color);
570 _gtk_css_value_unref (stop->offset);
574 _gtk_css_image_linear_init (GtkCssImageLinear *linear)
576 linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
577 g_array_set_clear_func (linear->stops, gtk_css_image_clear_color_stop);