1 | /* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
---|
2 | /* |
---|
3 | rsvg-css.c: Parse CSS basic data types. |
---|
4 | |
---|
5 | Copyright (C) 2000 Eazel, Inc. |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with this program; if not, write to the |
---|
19 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Raph Levien <raph@artofcode.com> |
---|
23 | */ |
---|
24 | |
---|
25 | #include "config.h" |
---|
26 | #include "rsvg-css.h" |
---|
27 | |
---|
28 | #include <glib.h> |
---|
29 | #include <math.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> |
---|
32 | #include <errno.h> |
---|
33 | #include <stdio.h> |
---|
34 | |
---|
35 | #ifndef M_PI |
---|
36 | #define M_PI 3.14159265358979323846 |
---|
37 | #endif /* M_PI */ |
---|
38 | |
---|
39 | #define POINTS_PER_INCH (72.0) |
---|
40 | #define CM_PER_INCH (2.54) |
---|
41 | #define MM_PER_INCH (25.4) |
---|
42 | #define PICA_PER_INCH (6.0) |
---|
43 | |
---|
44 | /** |
---|
45 | * rsvg_css_parse_vbox |
---|
46 | * @vbox: The CSS viewBox |
---|
47 | * @x : The X output |
---|
48 | * @y: The Y output |
---|
49 | * @w: The Width output |
---|
50 | * @h: The Height output |
---|
51 | * |
---|
52 | * Returns: Success or failure |
---|
53 | */ |
---|
54 | gboolean |
---|
55 | rsvg_css_parse_vbox (const char * vbox, double * x, double * y, |
---|
56 | double * w, double * h) |
---|
57 | { |
---|
58 | /* TODO: make me cleaner and more efficient */ |
---|
59 | char *ptr, *tok; |
---|
60 | char *str = g_strdup (vbox); |
---|
61 | gboolean has_vbox = FALSE; |
---|
62 | |
---|
63 | tok = strtok_r (str, ", \t", &ptr); |
---|
64 | if (tok != NULL) { |
---|
65 | *x = g_ascii_strtod (tok, NULL); |
---|
66 | tok = strtok_r (NULL, ", \t", &ptr); |
---|
67 | if (tok != NULL) { |
---|
68 | *y = g_ascii_strtod (tok, NULL); |
---|
69 | tok = strtok_r (NULL, ", \t", &ptr); |
---|
70 | if (tok != NULL) { |
---|
71 | *w = g_ascii_strtod (tok, NULL); |
---|
72 | tok = strtok_r (NULL, ", \t", &ptr); |
---|
73 | if (tok != NULL) { |
---|
74 | *h = g_ascii_strtod (tok, NULL); |
---|
75 | has_vbox = TRUE; |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | g_free (str); |
---|
81 | |
---|
82 | return has_vbox; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * rsvg_css_parse_length: Parse CSS2 length to a pixel value. |
---|
87 | * @str: Original string. |
---|
88 | * @pixels_per_inch: Pixels per inch |
---|
89 | * @fixed: Where to store boolean value of whether length is fixed. |
---|
90 | * |
---|
91 | * Parses a CSS2 length into a pixel value. |
---|
92 | * |
---|
93 | * Returns: returns the length. |
---|
94 | **/ |
---|
95 | double |
---|
96 | rsvg_css_parse_length (const char *str, gdouble pixels_per_inch, |
---|
97 | gint *percent, gint *em, gint *ex) |
---|
98 | { |
---|
99 | double length = 0.0; |
---|
100 | char *p = NULL; |
---|
101 | |
---|
102 | /* |
---|
103 | * The supported CSS length unit specifiers are: |
---|
104 | * em, ex, px, pt, pc, cm, mm, in, and % |
---|
105 | */ |
---|
106 | *percent = FALSE; |
---|
107 | *em = FALSE; |
---|
108 | *ex = FALSE; |
---|
109 | |
---|
110 | length = g_ascii_strtod (str, &p); |
---|
111 | |
---|
112 | /* todo: error condition - figure out how to best represent it */ |
---|
113 | if ((length == -HUGE_VAL || length == HUGE_VAL) && (ERANGE == errno)) |
---|
114 | return 0.0; |
---|
115 | |
---|
116 | /* test for either pixels or no unit, which is assumed to be pixels */ |
---|
117 | if (p && (strcmp(p, "px") != 0)) |
---|
118 | { |
---|
119 | if (!strcmp(p, "pt")) |
---|
120 | length *= (pixels_per_inch / POINTS_PER_INCH); |
---|
121 | else if (!strcmp(p, "in")) |
---|
122 | length *= pixels_per_inch; |
---|
123 | else if (!strcmp(p, "cm")) |
---|
124 | length *= (pixels_per_inch / CM_PER_INCH); |
---|
125 | else if (!strcmp(p, "mm")) |
---|
126 | length *= (pixels_per_inch / MM_PER_INCH); |
---|
127 | else if (!strcmp(p, "pc")) |
---|
128 | length *= (pixels_per_inch / PICA_PER_INCH); |
---|
129 | else if (!strcmp(p, "em")) |
---|
130 | *em = TRUE; |
---|
131 | else if (!strcmp(p, "ex")) |
---|
132 | *ex = TRUE; |
---|
133 | else if (!strcmp(p, "%")) |
---|
134 | { |
---|
135 | *percent = TRUE; |
---|
136 | length *= 0.01; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return length; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * rsvg_css_parse_normalized_length: Parse CSS2 length to a pixel value. |
---|
145 | * @str: Original string. |
---|
146 | * @pixels_per_inch: Pixels per inch |
---|
147 | * @normalize_to: Bounding box's width or height, as appropriate, or 0 |
---|
148 | * |
---|
149 | * Parses a CSS2 length into a pixel value. |
---|
150 | * |
---|
151 | * Returns: returns the length. |
---|
152 | */ |
---|
153 | double |
---|
154 | rsvg_css_parse_normalized_length(const char *str, gdouble pixels_per_inch, |
---|
155 | gdouble width_or_height, gdouble font_size) |
---|
156 | { |
---|
157 | double length; |
---|
158 | gint percent, em, ex; |
---|
159 | percent = em = ex = FALSE; |
---|
160 | |
---|
161 | length = rsvg_css_parse_length (str, pixels_per_inch, &percent, &em, &ex); |
---|
162 | if (percent) |
---|
163 | return length * width_or_height; |
---|
164 | else if (em) |
---|
165 | return length * font_size; |
---|
166 | else if (ex) |
---|
167 | return length * font_size * 2.0; /* stolen from imagemagick */ |
---|
168 | else |
---|
169 | return length; |
---|
170 | } |
---|
171 | |
---|
172 | gboolean |
---|
173 | rsvg_css_param_match (const char *str, const char *param_name) |
---|
174 | { |
---|
175 | int i; |
---|
176 | |
---|
177 | for (i = 0; str[i] != '\0' && str[i] != ':'; i++) |
---|
178 | if (param_name[i] != str[i]) |
---|
179 | return FALSE; |
---|
180 | return str[i] == ':' && param_name[i] == '\0'; |
---|
181 | } |
---|
182 | |
---|
183 | int |
---|
184 | rsvg_css_param_arg_offset (const char *str) |
---|
185 | { |
---|
186 | int i; |
---|
187 | |
---|
188 | for (i = 0; str[i] != '\0' && str[i] != ':'; i++); |
---|
189 | if (str[i] != '\0') i++; |
---|
190 | for (; str[i] == ' '; i++); |
---|
191 | return i; |
---|
192 | } |
---|
193 | |
---|
194 | static gint |
---|
195 | rsvg_css_clip_rgb_percent (gint in_percent) |
---|
196 | { |
---|
197 | /* spec says to clip these values */ |
---|
198 | if (in_percent > 100) |
---|
199 | in_percent = 100; |
---|
200 | else if (in_percent <= 0) |
---|
201 | return 0; |
---|
202 | |
---|
203 | return (gint)floor(255. * (double)in_percent / 100. + 0.5); |
---|
204 | } |
---|
205 | |
---|
206 | static gint |
---|
207 | rsvg_css_clip_rgb (gint rgb) |
---|
208 | { |
---|
209 | /* spec says to clip these values */ |
---|
210 | if (rgb > 255) |
---|
211 | rgb = 255; |
---|
212 | else if (rgb < 0) |
---|
213 | rgb = 0; |
---|
214 | |
---|
215 | return rgb; |
---|
216 | } |
---|
217 | |
---|
218 | typedef struct |
---|
219 | { |
---|
220 | const char * name; |
---|
221 | guint rgb; |
---|
222 | } ColorPair; |
---|
223 | |
---|
224 | /* compare function callback for bsearch */ |
---|
225 | static int |
---|
226 | rsvg_css_color_compare (const void * a, const void * b) |
---|
227 | { |
---|
228 | const char * needle = (const char *)a; |
---|
229 | const ColorPair * haystack = (const ColorPair *)b; |
---|
230 | |
---|
231 | return g_ascii_strcasecmp (needle, haystack->name); |
---|
232 | } |
---|
233 | |
---|
234 | /* pack 3 [0,255] ints into one 32 bit one */ |
---|
235 | #define PACK_RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b)) |
---|
236 | |
---|
237 | /** |
---|
238 | * Parse a CSS2 color specifier, return RGB value |
---|
239 | */ |
---|
240 | guint32 |
---|
241 | rsvg_css_parse_color (const char *str) |
---|
242 | { |
---|
243 | gint val = 0; |
---|
244 | |
---|
245 | if (str[0] == '#') |
---|
246 | { |
---|
247 | int i; |
---|
248 | for (i = 1; str[i]; i++) |
---|
249 | { |
---|
250 | int hexval; |
---|
251 | if (str[i] >= '0' && str[i] <= '9') |
---|
252 | hexval = str[i] - '0'; |
---|
253 | else if (str[i] >= 'A' && str[i] <= 'F') |
---|
254 | hexval = str[i] - 'A' + 10; |
---|
255 | else if (str[i] >= 'a' && str[i] <= 'f') |
---|
256 | hexval = str[i] - 'a' + 10; |
---|
257 | else |
---|
258 | break; |
---|
259 | val = (val << 4) + hexval; |
---|
260 | } |
---|
261 | /* handle #rgb case */ |
---|
262 | if (i == 4) |
---|
263 | { |
---|
264 | val = ((val & 0xf00) << 8) | |
---|
265 | ((val & 0x0f0) << 4) | |
---|
266 | (val & 0x00f); |
---|
267 | val |= val << 4; |
---|
268 | } |
---|
269 | } |
---|
270 | /* i want to use g_str_has_prefix but it isn't in my gstrfuncs.h?? */ |
---|
271 | else if (strstr (str, "rgb") != NULL) |
---|
272 | { |
---|
273 | gint r, g, b; |
---|
274 | r = g = b = 0; |
---|
275 | |
---|
276 | if (strstr (str, "%") != 0) |
---|
277 | { |
---|
278 | /* assume "rgb (r%, g%, b%)" */ |
---|
279 | if (3 == sscanf (str, " rgb ( %d %% , %d %% , %d %% ) ", &r, &g, &b)) |
---|
280 | { |
---|
281 | r = rsvg_css_clip_rgb_percent (r); |
---|
282 | g = rsvg_css_clip_rgb_percent (g); |
---|
283 | b = rsvg_css_clip_rgb_percent (b); |
---|
284 | } |
---|
285 | else |
---|
286 | r = g = b = 0; |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | /* assume "rgb (r, g, b)" */ |
---|
291 | if (3 == sscanf (str, " rgb ( %d , %d , %d ) ", &r, &g, &b)) |
---|
292 | { |
---|
293 | r = rsvg_css_clip_rgb (r); |
---|
294 | g = rsvg_css_clip_rgb (g); |
---|
295 | b = rsvg_css_clip_rgb (b); |
---|
296 | } |
---|
297 | else |
---|
298 | r = g = b = 0; |
---|
299 | } |
---|
300 | |
---|
301 | val = PACK_RGB (r,g,b); |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | const static ColorPair color_list [] = |
---|
306 | { |
---|
307 | { "aliceblue", PACK_RGB (240,248,255) }, |
---|
308 | { "antiquewhite", PACK_RGB (250,235,215) }, |
---|
309 | { "aqua", PACK_RGB (0,255,255) }, |
---|
310 | { "aquamarine", PACK_RGB (127,255,212) }, |
---|
311 | { "azure", PACK_RGB (240,255,255) }, |
---|
312 | { "beige", PACK_RGB (245,245,220) }, |
---|
313 | { "bisque", PACK_RGB (255,228,196) }, |
---|
314 | { "black", PACK_RGB (0,0,0) }, |
---|
315 | { "blanchedalmond", PACK_RGB (255,235,205) }, |
---|
316 | { "blue", PACK_RGB (0,0,255) }, |
---|
317 | { "blueviolet", PACK_RGB (138,43,226) }, |
---|
318 | { "brown", PACK_RGB (165,42,42) }, |
---|
319 | { "burlywood", PACK_RGB (222,184,135) }, |
---|
320 | { "cadetblue", PACK_RGB (95,158,160) }, |
---|
321 | { "chartreuse", PACK_RGB (127,255,0) }, |
---|
322 | { "chocolate", PACK_RGB (210,105,30) }, |
---|
323 | { "coral", PACK_RGB (255,127,80) }, |
---|
324 | { "cornflowerblue", PACK_RGB (100,149,237) }, |
---|
325 | { "cornsilk", PACK_RGB (255,248,220) }, |
---|
326 | { "crimson", PACK_RGB (220,20,60) }, |
---|
327 | { "cyan", PACK_RGB (0,255,255) }, |
---|
328 | { "darkblue", PACK_RGB (0,0,139) }, |
---|
329 | { "darkcyan", PACK_RGB (0,139,139) }, |
---|
330 | { "darkgoldenrod", PACK_RGB (184,132,11) }, |
---|
331 | { "darkgray", PACK_RGB (169,169,168) }, |
---|
332 | { "darkgreen", PACK_RGB (0,100,0) }, |
---|
333 | { "darkgrey", PACK_RGB (169,169,169) }, |
---|
334 | { "darkkhaki", PACK_RGB (189,183,107) }, |
---|
335 | { "darkmagenta", PACK_RGB (139,0,139) }, |
---|
336 | { "darkolivegreen", PACK_RGB (85,107,47) }, |
---|
337 | { "darkorange", PACK_RGB (255,140,0) }, |
---|
338 | { "darkorchid", PACK_RGB (153,50,204) }, |
---|
339 | { "darkred", PACK_RGB (139,0,0) }, |
---|
340 | { "darksalmon", PACK_RGB (233,150,122) }, |
---|
341 | { "darkseagreen", PACK_RGB (143,188,143) }, |
---|
342 | { "darkslateblue", PACK_RGB (72,61,139) }, |
---|
343 | { "darkslategray", PACK_RGB (47,79,79) }, |
---|
344 | { "darkslategrey", PACK_RGB (47,79,79) }, |
---|
345 | { "darkturquoise", PACK_RGB (0,206,209) }, |
---|
346 | { "darkviolet", PACK_RGB (148,0,211) }, |
---|
347 | { "deeppink", PACK_RGB (255,20,147) }, |
---|
348 | { "deepskyblue", PACK_RGB (0,191,255) }, |
---|
349 | { "dimgray", PACK_RGB (105,105,105) }, |
---|
350 | { "dimgrey", PACK_RGB (105,105,105) }, |
---|
351 | { "dogerblue", PACK_RGB (30,144,255) }, |
---|
352 | { "firebrick", PACK_RGB (178,34,34) }, |
---|
353 | { "floralwhite" , PACK_RGB (255,255,240)}, |
---|
354 | { "forestgreen", PACK_RGB (34,139,34) }, |
---|
355 | { "fuchsia", PACK_RGB (255,0,255) }, |
---|
356 | { "gainsboro", PACK_RGB (220,220,220) }, |
---|
357 | { "ghostwhite", PACK_RGB (248,248,255) }, |
---|
358 | { "gold", PACK_RGB (215,215,0) }, |
---|
359 | { "goldenrod", PACK_RGB (218,165,32) }, |
---|
360 | { "gray", PACK_RGB (128,128,128) }, |
---|
361 | { "grey", PACK_RGB (128,128,128) }, |
---|
362 | { "green", PACK_RGB (0,128,0)}, |
---|
363 | { "greenyellow", PACK_RGB (173,255,47) }, |
---|
364 | { "honeydew", PACK_RGB (240,255,240) }, |
---|
365 | { "hotpink", PACK_RGB (255,105,180) }, |
---|
366 | { "indianred", PACK_RGB (205,92,92) }, |
---|
367 | { "indigo", PACK_RGB (75,0,130) }, |
---|
368 | { "ivory", PACK_RGB (255,255,240) }, |
---|
369 | { "khaki", PACK_RGB (240,230,140) }, |
---|
370 | { "lavender", PACK_RGB (230,230,250) }, |
---|
371 | { "lavenderblush", PACK_RGB (255,240,245) }, |
---|
372 | { "lawngreen", PACK_RGB (124,252,0) }, |
---|
373 | { "lemonchiffon", PACK_RGB (255,250,205) }, |
---|
374 | { "lightblue", PACK_RGB (173,216,230) }, |
---|
375 | { "lightcoral", PACK_RGB (240,128,128) }, |
---|
376 | { "lightcyan", PACK_RGB (224,255,255) }, |
---|
377 | { "lightgoldenrodyellow", PACK_RGB (250,250,210) }, |
---|
378 | { "lightgray", PACK_RGB (211,211,211) }, |
---|
379 | { "lightgreen", PACK_RGB (144,238,144) }, |
---|
380 | { "lightgrey", PACK_RGB (211,211,211) }, |
---|
381 | { "lightpink", PACK_RGB (255,182,193) }, |
---|
382 | { "lightsalmon", PACK_RGB (255,160,122) }, |
---|
383 | { "lightseagreen", PACK_RGB (32,178,170) }, |
---|
384 | { "lightskyblue", PACK_RGB (135,206,250) }, |
---|
385 | { "lightslategray", PACK_RGB (119,136,153) }, |
---|
386 | { "lightslategrey", PACK_RGB (119,136,153) }, |
---|
387 | { "lightsteelblue", PACK_RGB (176,196,222) }, |
---|
388 | { "lightyellow", PACK_RGB (255,255,224) }, |
---|
389 | { "lime", PACK_RGB (0,255,0) }, |
---|
390 | { "limegreen", PACK_RGB (50,205,50) }, |
---|
391 | { "linen", PACK_RGB (250,240,230) }, |
---|
392 | { "magenta", PACK_RGB (255,0,255) }, |
---|
393 | { "maroon", PACK_RGB (128,0,0) }, |
---|
394 | { "mediumaquamarine", PACK_RGB (102,205,170) }, |
---|
395 | { "mediumblue", PACK_RGB (0,0,205) }, |
---|
396 | { "mediumorchid", PACK_RGB (186,85,211) }, |
---|
397 | { "mediumpurple", PACK_RGB (147,112,219) }, |
---|
398 | { "mediumseagreen", PACK_RGB (60,179,113) }, |
---|
399 | { "mediumslateblue", PACK_RGB (123,104,238) }, |
---|
400 | { "mediumspringgreen", PACK_RGB (0,250,154) }, |
---|
401 | { "mediumturquoise", PACK_RGB (72,209,204) }, |
---|
402 | { "mediumvioletred", PACK_RGB (199,21,133) }, |
---|
403 | { "mediumnightblue", PACK_RGB (25,25,112) }, |
---|
404 | { "mintcream", PACK_RGB (245,255,250) }, |
---|
405 | { "mintyrose", PACK_RGB (255,228,225) }, |
---|
406 | { "moccasin", PACK_RGB (255,228,181) }, |
---|
407 | { "navajowhite", PACK_RGB (255,222,173) }, |
---|
408 | { "navy", PACK_RGB (0,0,128) }, |
---|
409 | { "oldlace", PACK_RGB (253,245,230) }, |
---|
410 | { "olive", PACK_RGB (128,128,0) }, |
---|
411 | { "oliverab", PACK_RGB (107,142,35) }, |
---|
412 | { "orange", PACK_RGB (255,165,0) }, |
---|
413 | { "orangered", PACK_RGB (255,69,0) }, |
---|
414 | { "orchid", PACK_RGB (218,112,214) }, |
---|
415 | { "palegoldenrod", PACK_RGB (238,232,170) }, |
---|
416 | { "palegreen", PACK_RGB (152,251,152) }, |
---|
417 | { "paleturquoise", PACK_RGB (175,238,238) }, |
---|
418 | { "palevioletred", PACK_RGB (219,112,147) }, |
---|
419 | { "papayawhip", PACK_RGB (255,239,213) }, |
---|
420 | { "peachpuff", PACK_RGB (255,218,185) }, |
---|
421 | { "peru", PACK_RGB (205,133,63) }, |
---|
422 | { "pink", PACK_RGB (255,192,203) }, |
---|
423 | { "plum", PACK_RGB (221,160,203) }, |
---|
424 | { "powderblue", PACK_RGB (176,224,230) }, |
---|
425 | { "purple", PACK_RGB (128,0,128) }, |
---|
426 | { "red", PACK_RGB (255,0,0) }, |
---|
427 | { "rosybrown", PACK_RGB (188,143,143) }, |
---|
428 | { "royalblue", PACK_RGB (65,105,225) }, |
---|
429 | { "saddlebrown", PACK_RGB (139,69,19) }, |
---|
430 | { "salmon", PACK_RGB (250,128,114) }, |
---|
431 | { "sandybrown", PACK_RGB (244,164,96) }, |
---|
432 | { "seagreen", PACK_RGB (46,139,87) }, |
---|
433 | { "seashell", PACK_RGB (255,245,238) }, |
---|
434 | { "sienna", PACK_RGB (160,82,45) }, |
---|
435 | { "silver", PACK_RGB (192,192,192) }, |
---|
436 | { "skyblue", PACK_RGB (135,206,235) }, |
---|
437 | { "slateblue", PACK_RGB (106,90,205) }, |
---|
438 | { "slategray", PACK_RGB (112,128,144) }, |
---|
439 | { "slategrey", PACK_RGB (112,128,114) }, |
---|
440 | { "snow", PACK_RGB (255,255,250) }, |
---|
441 | { "springgreen", PACK_RGB (0,255,127) }, |
---|
442 | { "steelblue", PACK_RGB (70,130,180) }, |
---|
443 | { "tan", PACK_RGB (210,180,140) }, |
---|
444 | { "teal", PACK_RGB (0,128,128) }, |
---|
445 | { "thistle", PACK_RGB (216,191,216) }, |
---|
446 | { "tomato", PACK_RGB (255,99,71) }, |
---|
447 | { "turquoise", PACK_RGB (64,224,208) }, |
---|
448 | { "violet", PACK_RGB (238,130,238) }, |
---|
449 | { "wheat", PACK_RGB (245,222,179) }, |
---|
450 | { "white", PACK_RGB (255,255,255) }, |
---|
451 | { "whitesmoke", PACK_RGB (245,245,245) }, |
---|
452 | { "yellow", PACK_RGB (255,255,0) }, |
---|
453 | { "yellowgreen", PACK_RGB (154,205,50) } |
---|
454 | }; |
---|
455 | |
---|
456 | ColorPair * result = bsearch (str, color_list, |
---|
457 | sizeof (color_list)/sizeof (color_list[0]), |
---|
458 | sizeof (ColorPair), |
---|
459 | rsvg_css_color_compare); |
---|
460 | |
---|
461 | /* default to black on failed lookup */ |
---|
462 | if (result == NULL) |
---|
463 | val = 0; |
---|
464 | else |
---|
465 | val = result->rgb; |
---|
466 | } |
---|
467 | |
---|
468 | return val; |
---|
469 | } |
---|
470 | |
---|
471 | #undef PACK_RGB |
---|
472 | |
---|
473 | guint |
---|
474 | rsvg_css_parse_opacity (const char *str) |
---|
475 | { |
---|
476 | char *end_ptr; |
---|
477 | double opacity; |
---|
478 | |
---|
479 | opacity = g_ascii_strtod (str, &end_ptr); |
---|
480 | |
---|
481 | if (end_ptr && end_ptr[0] == '%') |
---|
482 | opacity *= 0.01; |
---|
483 | |
---|
484 | return (guint)floor (opacity * 255. + 0.5); |
---|
485 | } |
---|
486 | |
---|
487 | /* |
---|
488 | <angle>: An angle value is a <number> optionally followed immediately with |
---|
489 | an angle unit identifier. Angle unit identifiers are: |
---|
490 | |
---|
491 | * deg: degrees |
---|
492 | * grad: grads |
---|
493 | * rad: radians |
---|
494 | |
---|
495 | For properties defined in [CSS2], an angle unit identifier must be provided. |
---|
496 | For SVG-specific attributes and properties, the angle unit identifier is |
---|
497 | optional. If not provided, the angle value is assumed to be in degrees. |
---|
498 | */ |
---|
499 | double |
---|
500 | rsvg_css_parse_angle (const char * str) |
---|
501 | { |
---|
502 | double degrees; |
---|
503 | char *end_ptr; |
---|
504 | |
---|
505 | degrees = g_ascii_strtod (str, &end_ptr); |
---|
506 | |
---|
507 | /* todo: error condition - figure out how to best represent it */ |
---|
508 | if ((degrees == -HUGE_VAL || degrees == HUGE_VAL) && (ERANGE == errno)) |
---|
509 | return 0.0; |
---|
510 | |
---|
511 | if (end_ptr) |
---|
512 | { |
---|
513 | if (!strcmp(end_ptr, "rad")) |
---|
514 | return degrees * 180. / M_PI; |
---|
515 | else if (!strcmp(end_ptr, "grad")) |
---|
516 | return degrees * 360. / 400.; |
---|
517 | } |
---|
518 | |
---|
519 | return degrees; |
---|
520 | } |
---|
521 | |
---|
522 | /* |
---|
523 | <frequency>: Frequency values are used with aural properties. The normative |
---|
524 | definition of frequency values can be found in [CSS2-AURAL]. A frequency |
---|
525 | value is a <number> immediately followed by a frequency unit identifier. |
---|
526 | Frequency unit identifiers are: |
---|
527 | |
---|
528 | * Hz: Hertz |
---|
529 | * kHz: kilo Hertz |
---|
530 | |
---|
531 | Frequency values may not be negative. |
---|
532 | */ |
---|
533 | double |
---|
534 | rsvg_css_parse_frequency (const char * str) |
---|
535 | { |
---|
536 | double hz; |
---|
537 | char *end_ptr; |
---|
538 | |
---|
539 | hz = g_ascii_strtod (str, &end_ptr); |
---|
540 | |
---|
541 | /* todo: error condition - figure out how to best represent it */ |
---|
542 | if ((hz == -HUGE_VAL || hz == HUGE_VAL) && (ERANGE == errno)) |
---|
543 | return 0.0; |
---|
544 | |
---|
545 | if (end_ptr && !strcmp(end_ptr, "kHz")) |
---|
546 | return hz * 1000.; |
---|
547 | |
---|
548 | return hz; |
---|
549 | } |
---|
550 | |
---|
551 | /* |
---|
552 | <time>: A time value is a <number> immediately followed by a time unit |
---|
553 | identifier. Time unit identifiers are: |
---|
554 | |
---|
555 | * ms: milliseconds |
---|
556 | * s: seconds |
---|
557 | |
---|
558 | Time values are used in CSS properties and may not be negative. |
---|
559 | */ |
---|
560 | double |
---|
561 | rsvg_css_parse_time (const char * str) |
---|
562 | { |
---|
563 | double ms; |
---|
564 | char *end_ptr; |
---|
565 | |
---|
566 | ms = g_ascii_strtod (str, &end_ptr); |
---|
567 | |
---|
568 | /* todo: error condition - figure out how to best represent it */ |
---|
569 | if ((ms == -HUGE_VAL || ms == HUGE_VAL) && (ERANGE == errno)) |
---|
570 | return 0.0; |
---|
571 | |
---|
572 | if (end_ptr && !strcmp (end_ptr, "s")) |
---|
573 | return ms * 1000.; |
---|
574 | |
---|
575 | return ms; |
---|
576 | } |
---|
577 | |
---|
578 | PangoStyle |
---|
579 | rsvg_css_parse_font_style (const char * str, PangoStyle inherit) |
---|
580 | { |
---|
581 | if (str) |
---|
582 | { |
---|
583 | if (!strcmp(str, "oblique")) |
---|
584 | return PANGO_STYLE_OBLIQUE; |
---|
585 | if (!strcmp(str, "italic")) |
---|
586 | return PANGO_STYLE_ITALIC; |
---|
587 | else if (!strcmp(str, "inherit")) |
---|
588 | return inherit; |
---|
589 | } |
---|
590 | return PANGO_STYLE_NORMAL; |
---|
591 | } |
---|
592 | |
---|
593 | PangoVariant |
---|
594 | rsvg_css_parse_font_variant (const char * str, PangoVariant inherit) |
---|
595 | { |
---|
596 | if (str) |
---|
597 | { |
---|
598 | if (!strcmp(str, "small-caps")) |
---|
599 | return PANGO_VARIANT_SMALL_CAPS; |
---|
600 | else if (!strcmp(str, "inherit")) |
---|
601 | return inherit; |
---|
602 | } |
---|
603 | return PANGO_VARIANT_NORMAL; |
---|
604 | } |
---|
605 | |
---|
606 | PangoWeight |
---|
607 | rsvg_css_parse_font_weight (const char * str, PangoWeight inherit) |
---|
608 | { |
---|
609 | if (str) |
---|
610 | { |
---|
611 | if (!strcmp (str, "lighter")) |
---|
612 | return PANGO_WEIGHT_LIGHT; |
---|
613 | else if (!strcmp (str, "bold")) |
---|
614 | return PANGO_WEIGHT_BOLD; |
---|
615 | else if (!strcmp (str, "bolder")) |
---|
616 | return PANGO_WEIGHT_ULTRABOLD; |
---|
617 | else if (!strcmp (str, "100")) |
---|
618 | return (PangoWeight)100; |
---|
619 | else if (!strcmp (str, "200")) |
---|
620 | return (PangoWeight)200; |
---|
621 | else if (!strcmp (str, "300")) |
---|
622 | return (PangoWeight)300; |
---|
623 | else if (!strcmp (str, "400")) |
---|
624 | return (PangoWeight)400; |
---|
625 | else if (!strcmp (str, "500")) |
---|
626 | return (PangoWeight)500; |
---|
627 | else if (!strcmp (str, "600")) |
---|
628 | return (PangoWeight)600; |
---|
629 | else if (!strcmp (str, "700")) |
---|
630 | return (PangoWeight)700; |
---|
631 | else if (!strcmp (str, "800")) |
---|
632 | return (PangoWeight)800; |
---|
633 | else if (!strcmp (str, "900")) |
---|
634 | return (PangoWeight)900; |
---|
635 | else if (!strcmp(str, "inherit")) |
---|
636 | return inherit; |
---|
637 | } |
---|
638 | |
---|
639 | return PANGO_WEIGHT_NORMAL; |
---|
640 | } |
---|
641 | |
---|
642 | PangoStretch |
---|
643 | rsvg_css_parse_font_stretch (const char * str, PangoStretch inherit) |
---|
644 | { |
---|
645 | if (str) |
---|
646 | { |
---|
647 | if (!strcmp (str, "ultra-condensed")) |
---|
648 | return PANGO_STRETCH_ULTRA_CONDENSED; |
---|
649 | else if (!strcmp (str, "extra-condensed")) |
---|
650 | return PANGO_STRETCH_EXTRA_CONDENSED; |
---|
651 | else if (!strcmp (str, "condensed") || !strcmp (str, "narrower")) /* narrower not quite correct */ |
---|
652 | return PANGO_STRETCH_CONDENSED; |
---|
653 | else if (!strcmp (str, "semi-condensed")) |
---|
654 | return PANGO_STRETCH_SEMI_CONDENSED; |
---|
655 | else if (!strcmp (str, "semi-expanded")) |
---|
656 | return PANGO_STRETCH_SEMI_EXPANDED; |
---|
657 | else if (!strcmp (str, "expanded") || !strcmp (str, "wider")) /* wider not quite correct */ |
---|
658 | return PANGO_STRETCH_EXPANDED; |
---|
659 | else if (!strcmp (str, "extra-expanded")) |
---|
660 | return PANGO_STRETCH_EXTRA_EXPANDED; |
---|
661 | else if (!strcmp (str, "ultra-expanded")) |
---|
662 | return PANGO_STRETCH_ULTRA_EXPANDED; |
---|
663 | else if (!strcmp(str, "inherit")) |
---|
664 | return inherit; |
---|
665 | } |
---|
666 | return PANGO_STRETCH_NORMAL; |
---|
667 | } |
---|
668 | |
---|
669 | const char * |
---|
670 | rsvg_css_parse_font_family (const char * str, const char * inherit) |
---|
671 | { |
---|
672 | if (!str) |
---|
673 | return NULL; |
---|
674 | |
---|
675 | if (!strcmp (str, "inherit")) |
---|
676 | return inherit; |
---|
677 | else |
---|
678 | return str; |
---|
679 | } |
---|