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