]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimagelinear.c
cssparser: Move symbolic color parser to gtksymboliccolor.c
[~andy/gtk] / gtk / gtkcssimagelinear.c
1 /*
2  * Copyright © 2012 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcssimagelinearprivate.h"
23
24 #include <math.h>
25
26 #include "gtkcssnumbervalueprivate.h"
27 #include "gtkcssrgbavalueprivate.h"
28 #include "gtkcssprovider.h"
29 #include "gtksymboliccolorprivate.h"
30
31 G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
32
33 static void
34 gtk_css_image_linear_get_start_end (GtkCssImageLinear *linear,
35                                     double             length,
36                                     double            *start,
37                                     double            *end)
38 {
39   GtkCssImageLinearColorStop *stop;
40   double pos;
41   guint i;
42       
43   stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, 0);
44   if (stop->offset == NULL)
45     *start = 0;
46   else
47     *start = _gtk_css_number_value_get (stop->offset, length) / length;
48
49   *end = *start;
50
51   for (i = 0; i < linear->stops->len; i++)
52     {
53       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
54       
55       if (stop->offset == NULL)
56         continue;
57
58       pos = _gtk_css_number_value_get (stop->offset, length) / length;
59
60       *end = MAX (pos, *end);
61     }
62   
63   if (stop->offset == NULL)
64     *end = MAX (*end, 1.0);
65 }
66
67 static void
68 gtk_css_image_linear_compute_start_point (double angle_in_degrees,
69                                           double width,
70                                           double height,
71                                           double *x,
72                                           double *y)
73 {
74   double angle, c, slope, perpendicular;
75   
76   angle = fmod (angle_in_degrees, 360);
77   if (angle < 0)
78     angle += 360;
79
80   if (angle == 0)
81     {
82       *x = 0;
83       *y = -height;
84       return;
85     }
86   else if (angle == 90)
87     {
88       *x = width;
89       *y = 0;
90       return;
91     }
92   else if (angle == 180)
93     {
94       *x = 0;
95       *y = height;
96       return;
97     }
98   else if (angle == 270)
99     {
100       *x = -width;
101       *y = 0;
102       return;
103     }
104
105   /* The tan() is confusing because the angle is clockwise
106    * from 'to top' */
107   perpendicular = tan (angle * G_PI / 180);
108   slope = -1 / perpendicular;
109
110   if (angle > 180)
111     width = - width;
112   if (angle < 90 || angle > 270)
113     height = - height;
114   
115   /* Compute c (of y = mx + c) of perpendicular */
116   c = height - perpendicular * width;
117
118   *x = c / (slope - perpendicular);
119   *y = perpendicular * *x + c;
120 }
121                                          
122 static void
123 gtk_css_image_linear_draw (GtkCssImage        *image,
124                            cairo_t            *cr,
125                            double              width,
126                            double              height)
127 {
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] */
133   double offset;
134   int i, last;
135
136   if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
137     {
138       guint side = _gtk_css_number_value_get (linear->angle, 100);
139
140       if (side & (1 << GTK_CSS_RIGHT))
141         x = width;
142       else if (side & (1 << GTK_CSS_LEFT))
143         x = -width;
144       else
145         x = 0;
146
147       if (side & (1 << GTK_CSS_TOP))
148         y = -height;
149       else if (side & (1 << GTK_CSS_BOTTOM))
150         y = height;
151       else
152         y = 0;
153     }
154   else
155     {
156       gtk_css_image_linear_compute_start_point (_gtk_css_number_value_get (linear->angle, 100),
157                                                 width, height,
158                                                 &x, &y);
159     }
160
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);
167   else
168     cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
169
170   offset = start;
171   last = -1;
172   for (i = 0; i < linear->stops->len; i++)
173     {
174       GtkCssImageLinearColorStop *stop;
175       double pos, step;
176       
177       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
178
179       if (stop->offset == NULL)
180         {
181           if (i == 0)
182             pos = 0.0;
183           else if (i + 1 == linear->stops->len)
184             pos = 1.0;
185           else
186             continue;
187         }
188       else
189         pos = _gtk_css_number_value_get (stop->offset, length) / length;
190
191       pos = MAX (pos, offset);
192       step = (pos - offset) / (i - last);
193       for (last = last + 1; last <= i; last++)
194         {
195           const GdkRGBA *rgba;
196
197           stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);
198
199           rgba = _gtk_css_rgba_value_get_rgba (stop->color);
200           offset += step;
201
202           cairo_pattern_add_color_stop_rgba (pattern,
203                                              (offset - start) / (end - start),
204                                              rgba->red,
205                                              rgba->green,
206                                              rgba->blue,
207                                              rgba->alpha);
208         }
209
210       offset = pos;
211       last = i;
212     }
213
214   cairo_rectangle (cr, 0, 0, width, height);
215   cairo_translate (cr, width / 2, height / 2);
216   cairo_set_source (cr, pattern);
217   cairo_fill (cr);
218
219   cairo_pattern_destroy (pattern);
220 }
221
222
223 static gboolean
224 gtk_css_image_linear_parse (GtkCssImage  *image,
225                             GtkCssParser *parser,
226                             GFile        *base)
227 {
228   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
229   guint i;
230
231   if (_gtk_css_parser_try (parser, "repeating-linear-gradient(", TRUE))
232     linear->repeating = TRUE;
233   else if (_gtk_css_parser_try (parser, "linear-gradient(", TRUE))
234     linear->repeating = FALSE;
235   else
236     {
237       _gtk_css_parser_error (parser, "Not a linear gradient");
238       return FALSE;
239     }
240
241   if (_gtk_css_parser_try (parser, "to", TRUE))
242     {
243       guint side = 0;
244
245       for (i = 0; i < 2; i++)
246         {
247           if (_gtk_css_parser_try (parser, "left", TRUE))
248             {
249               if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
250                 {
251                   _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
252                   return FALSE;
253                 }
254               side |= (1 << GTK_CSS_LEFT);
255             }
256           else if (_gtk_css_parser_try (parser, "right", TRUE))
257             {
258               if (side & ((1 << GTK_CSS_LEFT) | (1 << GTK_CSS_RIGHT)))
259                 {
260                   _gtk_css_parser_error (parser, "Expected `top', `bottom' or comma");
261                   return FALSE;
262                 }
263               side |= (1 << GTK_CSS_RIGHT);
264             }
265           else if (_gtk_css_parser_try (parser, "top", TRUE))
266             {
267               if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
268                 {
269                   _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
270                   return FALSE;
271                 }
272               side |= (1 << GTK_CSS_TOP);
273             }
274           else if (_gtk_css_parser_try (parser, "bottom", TRUE))
275             {
276               if (side & ((1 << GTK_CSS_TOP) | (1 << GTK_CSS_BOTTOM)))
277                 {
278                   _gtk_css_parser_error (parser, "Expected `left', `right' or comma");
279                   return FALSE;
280                 }
281               side |= (1 << GTK_CSS_BOTTOM);
282             }
283           else
284             break;
285         }
286
287       if (side == 0)
288         {
289           _gtk_css_parser_error (parser, "Expected side that gradient should go to");
290           return FALSE;
291         }
292
293       linear->angle = _gtk_css_number_value_new (side, GTK_CSS_NUMBER);
294
295       if (!_gtk_css_parser_try (parser, ",", TRUE))
296         {
297           _gtk_css_parser_error (parser, "Expected a comma");
298           return FALSE;
299         }
300     }
301   else if (_gtk_css_parser_has_number (parser))
302     {
303       linear->angle = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_ANGLE);
304       if (linear->angle == NULL)
305         return FALSE;
306
307       if (!_gtk_css_parser_try (parser, ",", TRUE))
308         {
309           _gtk_css_parser_error (parser, "Expected a comma");
310           return FALSE;
311         }
312     }
313   else
314     linear->angle = _gtk_css_number_value_new (1 << GTK_CSS_BOTTOM, GTK_CSS_NUMBER);
315
316   do {
317     GtkCssImageLinearColorStop stop;
318     GtkSymbolicColor *symbolic;
319
320     symbolic = _gtk_css_parser_read_symbolic_color (parser);
321     if (symbolic == NULL)
322       return FALSE;
323
324     if (_gtk_css_parser_has_number (parser))
325       {
326         stop.offset = _gtk_css_number_value_parse (parser,
327                                                    GTK_CSS_PARSE_PERCENT
328                                                    | GTK_CSS_PARSE_LENGTH);
329         if (stop.offset == NULL)
330           {
331             gtk_symbolic_color_unref (symbolic);
332             return FALSE;
333           }
334       }
335     else
336       {
337         stop.offset = NULL;
338       }
339
340     stop.color = _gtk_css_value_new_take_symbolic_color (symbolic);
341     g_array_append_val (linear->stops, stop);
342
343   } while (_gtk_css_parser_try (parser, ",", TRUE));
344
345   if (!_gtk_css_parser_try (parser, ")", TRUE))
346     {
347       _gtk_css_parser_error (parser, "Missing closing bracket at end of linear gradient");
348       return FALSE;
349     }
350
351   return TRUE;
352 }
353
354 static void
355 gtk_css_image_linear_print (GtkCssImage *image,
356                             GString     *string)
357 {
358   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
359   guint i;
360
361   if (linear->repeating)
362     g_string_append (string, "repeating-linear-gradient(");
363   else
364     g_string_append (string, "linear-gradient(");
365
366   if (_gtk_css_number_value_get_unit (linear->angle) == GTK_CSS_NUMBER)
367     {
368       guint side = _gtk_css_number_value_get (linear->angle, 100);
369
370       if (side != (1 << GTK_CSS_BOTTOM))
371         {
372           g_string_append (string, "to");
373
374           if (side & (1 << GTK_CSS_TOP))
375             g_string_append (string, " top");
376           else if (side & (1 << GTK_CSS_BOTTOM))
377             g_string_append (string, " bottom");
378
379           if (side & (1 << GTK_CSS_LEFT))
380             g_string_append (string, " left");
381           else if (side & (1 << GTK_CSS_RIGHT))
382             g_string_append (string, " right");
383
384           g_string_append (string, ", ");
385         }
386     }
387   else
388     {
389       _gtk_css_value_print (linear->angle, string);
390       g_string_append (string, ", ");
391     }
392
393   for (i = 0; i < linear->stops->len; i++)
394     {
395       GtkCssImageLinearColorStop *stop;
396       
397       if (i > 0)
398         g_string_append (string, ", ");
399
400       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
401
402       _gtk_css_value_print (stop->color, string);
403
404       if (stop->offset)
405         {
406           g_string_append (string, " ");
407           _gtk_css_value_print (stop->offset, string);
408         }
409     }
410   
411   g_string_append (string, ")");
412 }
413
414 static GtkCssImage *
415 gtk_css_image_linear_compute (GtkCssImage     *image,
416                               GtkStyleContext *context)
417 {
418   static const GdkRGBA transparent = { 0, 0, 0, 0 };
419   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
420   GtkCssImageLinear *copy;
421   GtkCssValue *fallback;
422   guint i;
423
424   copy = g_object_new (GTK_TYPE_CSS_IMAGE_LINEAR, NULL);
425   copy->repeating = linear->repeating;
426
427   copy->angle = _gtk_css_number_value_compute (linear->angle, context);
428   
429   fallback = _gtk_css_value_new_take_symbolic_color (gtk_symbolic_color_new_literal (&transparent));
430   g_array_set_size (copy->stops, linear->stops->len);
431   for (i = 0; i < linear->stops->len; i++)
432     {
433       GtkCssImageLinearColorStop *stop, *scopy;
434
435       stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);
436       scopy = &g_array_index (copy->stops, GtkCssImageLinearColorStop, i);
437               
438       scopy->color = _gtk_css_rgba_value_compute_from_symbolic (stop->color,
439                                                                 fallback,
440                                                                 context,
441                                                                 FALSE);
442       
443       if (stop->offset)
444         scopy->offset = _gtk_css_number_value_compute (stop->offset, context);
445       else
446         scopy->offset = NULL;
447     }
448
449   _gtk_css_value_unref (fallback);
450
451   return GTK_CSS_IMAGE (copy);
452 }
453
454 static void
455 gtk_css_image_linear_dispose (GObject *object)
456 {
457   GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (object);
458
459   if (linear->stops)
460     {
461       g_array_free (linear->stops, TRUE);
462       linear->stops = NULL;
463     }
464
465   if (linear->angle)
466     {
467       _gtk_css_value_unref (linear->angle);
468       linear->angle = NULL;
469     }
470
471   G_OBJECT_CLASS (_gtk_css_image_linear_parent_class)->dispose (object);
472 }
473
474 static void
475 _gtk_css_image_linear_class_init (GtkCssImageLinearClass *klass)
476 {
477   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
478   GObjectClass *object_class = G_OBJECT_CLASS (klass);
479
480   image_class->draw = gtk_css_image_linear_draw;
481   image_class->parse = gtk_css_image_linear_parse;
482   image_class->print = gtk_css_image_linear_print;
483   image_class->compute = gtk_css_image_linear_compute;
484
485   object_class->dispose = gtk_css_image_linear_dispose;
486 }
487
488 static void
489 gtk_css_image_clear_color_stop (gpointer color_stop)
490 {
491   GtkCssImageLinearColorStop *stop = color_stop;
492
493   _gtk_css_value_unref (stop->color);
494   if (stop->offset)
495     _gtk_css_value_unref (stop->offset);
496 }
497
498 static void
499 _gtk_css_image_linear_init (GtkCssImageLinear *linear)
500 {
501   linear->stops = g_array_new (FALSE, FALSE, sizeof (GtkCssImageLinearColorStop));
502   g_array_set_clear_func (linear->stops, gtk_css_image_clear_color_stop);
503 }
504