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 "gtkcssprovider.h"
27 #include "gtkstylecontextprivate.h"
29 G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
32 gtk_css_image_linear_get_start_end (GtkCssImageLinear *linear,
37 GtkCssImageLinearColorStop *stop;
41 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
42 if (stop->offset.unit == GTK_CSS_NUMBER)
44 else if (stop->offset.unit == GTK_CSS_PX)
45 *start = stop->offset.value / length;
47 *start = stop->offset.value / 100;
51 for (i = 0; i < linear->stops->len; i++)
53 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
55 if (stop->offset.unit == GTK_CSS_NUMBER)
58 if (stop->offset.unit == GTK_CSS_PX)
59 pos = stop->offset.value / length;
61 pos = stop->offset.value / 100;
63 *end = MAX (pos, *end);
66 if (stop->offset.unit == GTK_CSS_NUMBER)
67 *end = MAX (*end, 1.0);
71 gtk_css_image_linear_compute_start_point (double angle_in_degrees,
77 double angle, c, slope, perpendicular;
79 angle = fmod (angle_in_degrees, 360);
95 else if (angle == 180)
101 else if (angle == 270)
108 /* The tan() is confusing because the angle is clockwise
110 perpendicular = tan (angle * G_PI / 180);
111 slope = -1 / perpendicular;
115 if (angle < 90 || angle > 270)
118 /* Compute c (of y = mx + c) of perpendicular */
119 c = height - perpendicular * width;
121 *x = c / (slope - perpendicular);
122 *y = perpendicular * *x + c;
126 gtk_css_image_linear_draw (GtkCssImage *image,
131 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
132 cairo_pattern_t *pattern;
133 double x, y; /* coordinates of start point */
134 double length; /* distance in pixels for 100% */
135 double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
139 g_return_if_fail (linear->is_computed);
141 if (linear->angle.unit == GTK_CSS_NUMBER)
143 guint side = linear->angle.value;
145 if (side & (1 << GTK_CSS_RIGHT))
147 else if (side & (1 << GTK_CSS_LEFT))
152 if (side & (1 << GTK_CSS_TOP))
154 else if (side & (1 << GTK_CSS_BOTTOM))
161 gtk_css_image_linear_compute_start_point (linear->angle.value,
166 length = sqrt (x * x + y * y);
167 gtk_css_image_linear_get_start_end (linear, length, &start, &end);
168 pattern = cairo_pattern_create_linear (x * (start - 0.5), y * (start - 0.5),
169 x * (end - 0.5), y * (end - 0.5));
170 if (linear->repeating)
171 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
173 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
177 for (i = 0; i < linear->stops->len; i++)
179 GtkCssImageLinearColorStop *stop;
182 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
184 if (stop->offset.unit == GTK_CSS_NUMBER)
188 else if (i + 1 == linear->stops->len)
193 else if (stop->offset.unit == GTK_CSS_PX)
194 pos = stop->offset.value / length;
196 pos = stop->offset.value / 100;
198 pos = MAX (pos, offset);
199 step = (pos - offset) / (i - last);
200 for (last = last + 1; last <= i; last++)
202 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);
206 cairo_pattern_add_color_stop_rgba (pattern,
207 (offset - start) / (end - start),
208 stop->color.rgba.red,
209 stop->color.rgba.green,
210 stop->color.rgba.blue,
211 stop->color.rgba.alpha);
218 cairo_rectangle (cr, 0, 0, width, height);
219 cairo_translate (cr, width / 2, height / 2);
220 cairo_set_source (cr, pattern);
223 cairo_pattern_destroy (pattern);
228 gtk_css_image_linear_parse (GtkCssImage *image,
229 GtkCssParser *parser,
232 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
235 if (_gtk_css_parser_try (parser, "repeating-linear-gradient(", TRUE))
236 linear->repeating = TRUE;
237 else if (_gtk_css_parser_try (parser, "linear-gradient(", TRUE))
238 linear->repeating = FALSE;
241 _gtk_css_parser_error (parser, "Not a linear gradient");
245 if (_gtk_css_parser_try (parser, "to", TRUE))
249 for (i = 0; i < 2; i++)
251 if (_gtk_css_parser_try (parser, "left", TRUE))
253 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
255 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
258 side |= (1 << GTK_CSS_LEFT);
260 else if (_gtk_css_parser_try (parser, "right", TRUE))
262 if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
264 _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
267 side |= (1 << GTK_CSS_RIGHT);
269 else if (_gtk_css_parser_try (parser, "top", TRUE))
271 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
273 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
276 side |= (1 << GTK_CSS_TOP);
278 else if (_gtk_css_parser_try (parser, "bottom", TRUE))
280 if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
282 _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
285 side |= (1 << GTK_CSS_BOTTOM);
293 _gtk_css_parser_error (parser, "Expected side that gradient should go to");
297 _gtk_css_number_init (&linear->angle, side, GTK_CSS_NUMBER);
299 if (!_gtk_css_parser_try (parser, ",", TRUE))
301 _gtk_css_parser_error (parser, "Expected a comma");
305 else if (_gtk_css_parser_has_number (parser))
307 if (!_gtk_css_parser_read_number (parser,
309 GTK_CSS_PARSE_ANGLE))
312 if (!_gtk_css_parser_try (parser, ",", TRUE))
314 _gtk_css_parser_error (parser, "Expected a comma");
319 _gtk_css_number_init (&linear->angle, 1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
322 GtkCssImageLinearColorStop stop;
324 stop.color.symbolic = _gtk_css_parser_read_symbolic_color (parser);
325 if (stop.color.symbolic == NULL)
328 if (_gtk_css_parser_has_number (parser))
330 if (!_gtk_css_parser_read_number (parser,
332 GTK_CSS_PARSE_PERCENT
333 | GTK_CSS_PARSE_LENGTH))
338 /* use NUMBER to mark as unset number */
339 _gtk_css_number_init (&stop.offset, 0, GTK_CSS_NUMBER);
342 g_array_append_val (linear->stops, stop);
344 } while (_gtk_css_parser_try (parser, ",", TRUE));
346 if (!_gtk_css_parser_try (parser, ")", TRUE))
348 _gtk_css_parser_error (parser, "Missing closing bracket at end of linear gradient");
356 gtk_css_image_linear_print (GtkCssImage *image,
359 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
362 /* XXX: Do these need to be prinatable? */
363 g_return_if_fail (!linear->is_computed);
365 if (linear->repeating)
366 g_string_append (string, "repeating-linear-gradient(");
368 g_string_append (string, "linear-gradient(");
370 if (linear->angle.unit == GTK_CSS_NUMBER)
372 guint side = linear->angle.value;
374 if (side != (1 << GTK_CSS_BOTTOM))
376 g_string_append (string, "to");
378 if (side & (1 << GTK_CSS_TOP))
379 g_string_append (string, " top");
380 else if (side & (1 << GTK_CSS_BOTTOM))
381 g_string_append (string, " bottom");
383 if (side & (1 << GTK_CSS_LEFT))
384 g_string_append (string, " left");
385 else if (side & (1 << GTK_CSS_RIGHT))
386 g_string_append (string, " right");
388 g_string_append (string, ", ");
393 _gtk_css_number_print (&linear->angle, string);
394 g_string_append (string, ", ");
397 for (i = 0; i < linear->stops->len; i++)
399 GtkCssImageLinearColorStop *stop;
403 g_string_append (string, ", ");
405 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
407 s = gtk_symbolic_color_to_string (stop->color.symbolic);
408 g_string_append (string, s);
411 if (stop->offset.unit != GTK_CSS_NUMBER)
413 g_string_append (string, " ");
414 _gtk_css_number_print (&stop->offset, string);
418 g_string_append (string, ")");
422 gtk_css_image_linear_compute (GtkCssImage *image,
423 GtkStyleContext *context)
425 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
426 GtkCssImageLinear *copy;
429 if (linear->is_computed)
430 return g_object_ref (linear);
432 copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
433 copy->is_computed = TRUE;
434 copy->repeating = linear->repeating;
436 _gtk_css_number_compute (©->angle, &linear->angle, context);
438 g_array_set_size (copy->stops, linear->stops->len);
439 for (i = 0; i < linear->stops->len; i++)
441 GtkCssImageLinearColorStop *stop, *scopy;
443 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
444 scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i);
446 if (!_gtk_style_context_resolve_color (context,
447 stop->color.symbolic,
450 static const GdkRGBA transparent = { 0, 0, 0, 0 };
451 scopy->color.rgba = transparent;
454 _gtk_css_number_compute (&scopy->offset, &stop->offset, context);
457 return GTK_CSS_IMAGE (copy);
461 gtk_css_image_linear_dispose (GObject *object)
463 GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
467 if (!linear->is_computed)
471 for (i = 0; i < linear->stops->len; i++)
473 GtkCssImageLinearColorStop *stop;
475 stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
477 gtk_symbolic_color_unref (stop->color.symbolic);
480 g_array_free (linear->stops, TRUE);
481 linear->stops = NULL;
484 G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
488 _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
490 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
491 GObjectClass *object_class = G_OBJECT_CLASS (klass);
493 image_class->draw = gtk_css_image_linear_draw;
494 image_class->parse = gtk_css_image_linear_parse;
495 image_class->print = gtk_css_image_linear_print;
496 image_class->compute = gtk_css_image_linear_compute;
498 object_class->dispose = gtk_css_image_linear_dispose;
502 _gtk_css_image_linear_init (GtkCssImageLinear *linear)
504 linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
506 _gtk_css_number_init (&linear->angle, 1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);