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 "gtkcsscolorvalueprivate.h"
27 #include "gtkcssnumbervalueprivate.h"
28 #include "gtkcssrgbavalueprivate.h"
29 #include "gtkcssprovider.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 if (linear->repeating)
45 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
46 if (stop->offset == NULL)
49 *start = _gtk_css_number_value_get (stop->offset, length) / length;
53 for (i = 0; i < linear->stops->len; i++)
55 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
57 if (stop->offset == NULL)
60 pos = _gtk_css_number_value_get (stop->offset, length) / length;
62 *end = MAX (pos, *end);
65 if (stop->offset == NULL)
66 *end = MAX (*end, 1.0);
76 gtk_css_image_linear_compute_start_point (double angle_in_degrees,
82 double angle, c, slope, perpendicular;
84 angle = fmod (angle_in_degrees, 360);
100 else if (angle == 180)
106 else if (angle == 270)
113 /* The tan() is confusing because the angle is clockwise
115 perpendicular = tan (angle * G_PI / 180);
116 slope = -1 / perpendicular;
120 if (angle < 90 || angle > 270)
123 /* Compute c (of y = mx + c) of perpendicular */
124 c = height - perpendicular * width;
126 *x = c / (slope - perpendicular);
127 *y = perpendicular * *x + c;
131 gtk_css_image_linear_draw (GtkCssImage *image,
136 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
137 cairo_pattern_t *pattern;
138 double x, y; /* coordinates of start point */
139 double length; /* distance in pixels for 100% */
140 double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
144 if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
146 guint side = _gtk_css_number_value_get (linear->angle, 100);
148 if (side & (1 << GTK_CSS_RIGHT))
150 else if (side & (1 << GTK_CSS_LEFT))
155 if (side & (1 << GTK_CSS_TOP))
157 else if (side & (1 << GTK_CSS_BOTTOM))
164 gtk_css_image_linear_compute_start_point (_gtk_css_number_value_get (linear->angle, 100),
169 length = sqrt (x * x + y * y);
170 gtk_css_image_linear_get_start_end (linear, length, &start, &end);
171 pattern = cairo_pattern_create_linear (x * (start - 0.5), y * (start - 0.5),
172 x * (end - 0.5), y * (end - 0.5));
173 if (linear->repeating)
174 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
176 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
180 for (i = 0; i < linear->stops->len; i++)
182 GtkCssImageLinearColorStop *stop;
185 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
187 if (stop->offset == NULL)
191 else if (i + 1 == linear->stops->len)
197 pos = _gtk_css_number_value_get (stop->offset, length) / length;
199 pos = MAX (pos, offset);
200 step = (pos - offset) / (i - last);
201 for (last = last + 1; last <= i; last++)
205 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);
207 rgba = _gtk_css_rgba_value_get_rgba (stop->color);
210 cairo_pattern_add_color_stop_rgba (pattern,
211 (offset - start) / (end - start),
222 cairo_rectangle (cr, 0, 0, width, height);
223 cairo_translate (cr, width / 2, height / 2);
224 cairo_set_source (cr, pattern);
227 cairo_pattern_destroy (pattern);
232 gtk_css_image_linear_parse (GtkCssImage *image,
233 GtkCssParser *parser)
235 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
238 if (_gtk_css_parser_try (parser, "repeating-linear-gradient(", TRUE))
239 linear->repeating = TRUE;
240 else if (_gtk_css_parser_try (parser, "linear-gradient(", TRUE))
241 linear->repeating = FALSE;
244 _gtk_css_parser_error (parser, "Not a linear gradient");
248 if (_gtk_css_parser_try (parser, "to", TRUE))
252 for (i = 0; i < 2; i++)
254 if (_gtk_css_parser_try (parser, "left", TRUE))
256 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
258 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
261 side |= (1 << GTK_CSS_LEFT);
263 else if (_gtk_css_parser_try (parser, "right", TRUE))
265 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
267 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
270 side |= (1 << GTK_CSS_RIGHT);
272 else if (_gtk_css_parser_try (parser, "top", TRUE))
274 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
276 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
279 side |= (1 << GTK_CSS_TOP);
281 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
283 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
285 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
288 side |= (1 << GTK_CSS_BOTTOM);
296 _gtk_css_parser_error (parser, "Expected side that gradient should go to");
300 linear->angle = _gtk_css_number_value_new (side, GTK_CSS_NUMBER);
302 if (!_gtk_css_parser_try (parser, ",", TRUE))
304 _gtk_css_parser_error (parser, "Expected a comma");
308 else if (_gtk_css_parser_has_number (parser))
310 linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
311 if (linear->angle == NULL)
314 if (!_gtk_css_parser_try (parser, ",", TRUE))
316 _gtk_css_parser_error (parser, "Expected a comma");
321 linear->angle = _gtk_css_number_value_new (1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
324 GtkCssImageLinearColorStop stop;
326 stop.color = _gtk_css_color_value_parse (parser);
327 if (stop.color == NULL)
330 if (_gtk_css_parser_has_number (parser))
332 stop.offset = _gtk_css_number_value_parse (parser,
333 GTK_CSS_PARSE_PERCENT
334 | GTK_CSS_PARSE_LENGTH);
335 if (stop.offset == NULL)
337 _gtk_css_value_unref (stop.color);
346 g_array_append_val (linear->stops, stop);
348 } while (_gtk_css_parser_try (parser, ",", TRUE));
350 if (!_gtk_css_parser_try (parser, ")", TRUE))
352 _gtk_css_parser_error (parser, "Missing closing bracket at end of linear gradient");
360 gtk_css_image_linear_print (GtkCssImage *image,
363 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
366 if (linear->repeating)
367 g_string_append (string, "repeating-linear-gradient(");
369 g_string_append (string, "linear-gradient(");
371 if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
373 guint side = _gtk_css_number_value_get (linear->angle, 100);
375 if (side != (1 << GTK_CSS_BOTTOM))
377 g_string_append (string, "to");
379 if (side & (1 << GTK_CSS_TOP))
380 g_string_append (string, " top");
381 else if (side & (1 << GTK_CSS_BOTTOM))
382 g_string_append (string, " bottom");
384 if (side & (1 << GTK_CSS_LEFT))
385 g_string_append (string, " left");
386 else if (side & (1 << GTK_CSS_RIGHT))
387 g_string_append (string, " right");
389 g_string_append (string, ", ");
394 _gtk_css_value_print (linear->angle, string);
395 g_string_append (string, ", ");
398 for (i = 0; i < linear->stops->len; i++)
400 GtkCssImageLinearColorStop *stop;
403 g_string_append (string, ", ");
405 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
407 _gtk_css_value_print (stop->color, string);
411 g_string_append (string, " ");
412 _gtk_css_value_print (stop->offset, string);
416 g_string_append (string, ")");
420 gtk_css_image_linear_compute (GtkCssImage *image,
422 GtkStyleProviderPrivate *provider,
423 GtkCssComputedValues *values,
424 GtkCssComputedValues *parent_values,
425 GtkCssDependencies *dependencies)
427 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
428 GtkCssImageLinear *copy;
431 copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
432 copy->repeating = linear->repeating;
434 copy->angle = _gtk_css_value_compute (linear->angle, property_id, provider, values, parent_values, dependencies);
436 g_array_set_size (copy->stops, linear->stops->len);
437 for (i = 0; i < linear->stops->len; i++)
439 GtkCssImageLinearColorStop *stop, *scopy;
440 GtkCssDependencies child_deps;
442 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
443 scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i);
445 scopy->color = _gtk_css_value_compute (stop->color, property_id, provider, values, parent_values, &child_deps);
446 *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
450 scopy->offset = _gtk_css_value_compute (stop->offset, property_id, provider, values, parent_values, &child_deps);
451 *dependencies = _gtk_css_dependencies_union (*dependencies, child_deps);
455 scopy->offset = NULL;
459 return GTK_CSS_IMAGE (copy);
463 gtk_css_image_linear_transition (GtkCssImage *start_image,
464 GtkCssImage *end_image,
468 GtkCssImageLinear *start, *end, *result;
471 start = GTK_CSS_IMAGE_LINEAR (start_image);
473 if (end_image == NULL)
475 if (!GTK_IS_CSS_IMAGE_LINEAR (end_image))
476 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
478 end = GTK_CSS_IMAGE_LINEAR (end_image);
480 if ((start->repeating != end->repeating)
481 || (start->stops->len != end->stops->len))
482 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
484 result = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
485 result->repeating = start->repeating;
487 result->angle = _gtk_css_value_transition (start->angle, end->angle, property_id, progress);
488 if (result->angle == NULL)
491 for (i = 0; i < start->stops->len; i++)
493 GtkCssImageLinearColorStop stop, *start_stop, *end_stop;
495 start_stop = &g_array_index (start->stops, GtkCssImageLinearColorStop, i);
496 end_stop = &g_array_index (end->stops, GtkCssImageLinearColorStop, i);
498 if ((start_stop->offset != NULL) != (end_stop->offset != NULL))
501 if (start_stop->offset == NULL)
507 stop.offset = _gtk_css_value_transition (start_stop->offset,
511 if (stop.offset == NULL)
515 stop.color = _gtk_css_value_transition (start_stop->color,
519 if (stop.color == NULL)
522 _gtk_css_value_unref (stop.offset);
526 g_array_append_val (result->stops, stop);
529 return GTK_CSS_IMAGE (result);
532 g_object_unref (result);
533 return GTK_CSS_IMAGE_CLASS (_gtk_css_image_linear_parent_class)->transition (start_image, end_image, property_id, progress);
537 gtk_css_image_linear_equal (GtkCssImage *image1,
540 GtkCssImageLinear *linear1 = GTK_CSS_IMAGE_LINEAR (image1);
541 GtkCssImageLinear *linear2 = GTK_CSS_IMAGE_LINEAR (image2);
544 if (linear1->repeating != linear2->repeating ||
545 !_gtk_css_value_equal (linear1->angle, linear2->angle) ||
546 linear1->stops->len != linear2->stops->len)
549 for (i = 0; i < linear1->stops->len; i++)
551 GtkCssImageLinearColorStop *stop1, *stop2;
553 stop1 = &g_array_index (linear1->stops, GtkCssImageLinearColorStop, i);
554 stop2 = &g_array_index (linear2->stops, GtkCssImageLinearColorStop, i);
556 if (!_gtk_css_value_equal0 (stop1->offset, stop2->offset) ||
557 !_gtk_css_value_equal (stop1->color, stop2->color))
565 gtk_css_image_linear_dispose (GObject *object)
567 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
571 g_array_free (linear->stops, TRUE);
572 linear->stops = NULL;
577 _gtk_css_value_unref (linear->angle);
578 linear->angle = NULL;
581 G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
585 _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
587 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
588 GObjectClass *object_class = G_OBJECT_CLASS (klass);
590 image_class->draw = gtk_css_image_linear_draw;
591 image_class->parse = gtk_css_image_linear_parse;
592 image_class->print = gtk_css_image_linear_print;
593 image_class->compute = gtk_css_image_linear_compute;
594 image_class->equal = gtk_css_image_linear_equal;
595 image_class->transition = gtk_css_image_linear_transition;
597 object_class->dispose = gtk_css_image_linear_dispose;
601 gtk_css_image_clear_color_stop (gpointer color_stop)
603 GtkCssImageLinearColorStop *stop = color_stop;
605 _gtk_css_value_unref (stop->color);
607 _gtk_css_value_unref (stop->offset);
611 _gtk_css_image_linear_init (GtkCssImageLinear *linear)
613 linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
614 g_array_set_clear_func (linear->stops, gtk_css_image_clear_color_stop);