f0a2ae79d6f833fee16f5c2e4e7d3dec5ddcf80a
1 #define __SP_SVG_COLOR_C__
3 /**
4 * \file
5 * Reading \& writing of SVG/CSS colors.
6 */
7 /*
8 * Authors:
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 *
11 * Copyright (C) 1999-2002 Lauris Kaplinski
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
20 #include "prefs-utils.h"
21 #include "svg-color.h"
22 #include "svg-icc-color.h"
23 #include <cassert>
24 #include <math.h>
25 #include <glib/gmem.h>
26 #include <glib/gtestutils.h>
27 #include <glib/gmessages.h>
28 #include <glib/gstrfuncs.h>
29 #include <glib/ghash.h>
30 #include <glib/gutils.h>
31 #include <cstdio> // sprintf
32 #include <errno.h>
33 #include "strneq.h"
34 using std::sprintf;
36 struct SPSVGColor {
37 unsigned long rgb;
38 char const *name;
39 };
41 /*
42 * These are the colors defined in the SVG standard
43 */
44 static SPSVGColor const sp_svg_color_named[] = {
45 { 0xF0F8FF, "aliceblue" },
46 { 0xFAEBD7, "antiquewhite" },
47 { 0x00FFFF, "aqua" },
48 { 0x7FFFD4, "aquamarine" },
49 { 0xF0FFFF, "azure" },
50 { 0xF5F5DC, "beige" },
51 { 0xFFE4C4, "bisque" },
52 { 0x000000, "black" },
53 { 0xFFEBCD, "blanchedalmond" },
54 { 0x0000FF, "blue" },
55 { 0x8A2BE2, "blueviolet" },
56 { 0xA52A2A, "brown" },
57 { 0xDEB887, "burlywood" },
58 { 0x5F9EA0, "cadetblue" },
59 { 0x7FFF00, "chartreuse" },
60 { 0xD2691E, "chocolate" },
61 { 0xFF7F50, "coral" },
62 { 0x6495ED, "cornflowerblue" },
63 { 0xFFF8DC, "cornsilk" },
64 { 0xDC143C, "crimson" },
65 { 0x00FFFF, "cyan" },
66 { 0x00008B, "darkblue" },
67 { 0x008B8B, "darkcyan" },
68 { 0xB8860B, "darkgoldenrod" },
69 { 0xA9A9A9, "darkgray" },
70 { 0x006400, "darkgreen" },
71 { 0xA9A9A9, "darkgrey" },
72 { 0xBDB76B, "darkkhaki" },
73 { 0x8B008B, "darkmagenta" },
74 { 0x556B2F, "darkolivegreen" },
75 { 0xFF8C00, "darkorange" },
76 { 0x9932CC, "darkorchid" },
77 { 0x8B0000, "darkred" },
78 { 0xE9967A, "darksalmon" },
79 { 0x8FBC8F, "darkseagreen" },
80 { 0x483D8B, "darkslateblue" },
81 { 0x2F4F4F, "darkslategray" },
82 { 0x2F4F4F, "darkslategrey" },
83 { 0x00CED1, "darkturquoise" },
84 { 0x9400D3, "darkviolet" },
85 { 0xFF1493, "deeppink" },
86 { 0x00BFFF, "deepskyblue" },
87 { 0x696969, "dimgray" },
88 { 0x696969, "dimgrey" },
89 { 0x1E90FF, "dodgerblue" },
90 { 0xB22222, "firebrick" },
91 { 0xFFFAF0, "floralwhite" },
92 { 0x228B22, "forestgreen" },
93 { 0xFF00FF, "fuchsia" },
94 { 0xDCDCDC, "gainsboro" },
95 { 0xF8F8FF, "ghostwhite" },
96 { 0xFFD700, "gold" },
97 { 0xDAA520, "goldenrod" },
98 { 0x808080, "gray" },
99 { 0x808080, "grey" },
100 { 0x008000, "green" },
101 { 0xADFF2F, "greenyellow" },
102 { 0xF0FFF0, "honeydew" },
103 { 0xFF69B4, "hotpink" },
104 { 0xCD5C5C, "indianred" },
105 { 0x4B0082, "indigo" },
106 { 0xFFFFF0, "ivory" },
107 { 0xF0E68C, "khaki" },
108 { 0xE6E6FA, "lavender" },
109 { 0xFFF0F5, "lavenderblush" },
110 { 0x7CFC00, "lawngreen" },
111 { 0xFFFACD, "lemonchiffon" },
112 { 0xADD8E6, "lightblue" },
113 { 0xF08080, "lightcoral" },
114 { 0xE0FFFF, "lightcyan" },
115 { 0xFAFAD2, "lightgoldenrodyellow" },
116 { 0xD3D3D3, "lightgray" },
117 { 0x90EE90, "lightgreen" },
118 { 0xD3D3D3, "lightgrey" },
119 { 0xFFB6C1, "lightpink" },
120 { 0xFFA07A, "lightsalmon" },
121 { 0x20B2AA, "lightseagreen" },
122 { 0x87CEFA, "lightskyblue" },
123 { 0x778899, "lightslategray" },
124 { 0x778899, "lightslategrey" },
125 { 0xB0C4DE, "lightsteelblue" },
126 { 0xFFFFE0, "lightyellow" },
127 { 0x00FF00, "lime" },
128 { 0x32CD32, "limegreen" },
129 { 0xFAF0E6, "linen" },
130 { 0xFF00FF, "magenta" },
131 { 0x800000, "maroon" },
132 { 0x66CDAA, "mediumaquamarine" },
133 { 0x0000CD, "mediumblue" },
134 { 0xBA55D3, "mediumorchid" },
135 { 0x9370DB, "mediumpurple" },
136 { 0x3CB371, "mediumseagreen" },
137 { 0x7B68EE, "mediumslateblue" },
138 { 0x00FA9A, "mediumspringgreen" },
139 { 0x48D1CC, "mediumturquoise" },
140 { 0xC71585, "mediumvioletred" },
141 { 0x191970, "midnightblue" },
142 { 0xF5FFFA, "mintcream" },
143 { 0xFFE4E1, "mistyrose" },
144 { 0xFFE4B5, "moccasin" },
145 { 0xFFDEAD, "navajowhite" },
146 { 0x000080, "navy" },
147 { 0xFDF5E6, "oldlace" },
148 { 0x808000, "olive" },
149 { 0x6B8E23, "olivedrab" },
150 { 0xFFA500, "orange" },
151 { 0xFF4500, "orangered" },
152 { 0xDA70D6, "orchid" },
153 { 0xEEE8AA, "palegoldenrod" },
154 { 0x98FB98, "palegreen" },
155 { 0xAFEEEE, "paleturquoise" },
156 { 0xDB7093, "palevioletred" },
157 { 0xFFEFD5, "papayawhip" },
158 { 0xFFDAB9, "peachpuff" },
159 { 0xCD853F, "peru" },
160 { 0xFFC0CB, "pink" },
161 { 0xDDA0DD, "plum" },
162 { 0xB0E0E6, "powderblue" },
163 { 0x800080, "purple" },
164 { 0xFF0000, "red" },
165 { 0xBC8F8F, "rosybrown" },
166 { 0x4169E1, "royalblue" },
167 { 0x8B4513, "saddlebrown" },
168 { 0xFA8072, "salmon" },
169 { 0xF4A460, "sandybrown" },
170 { 0x2E8B57, "seagreen" },
171 { 0xFFF5EE, "seashell" },
172 { 0xA0522D, "sienna" },
173 { 0xC0C0C0, "silver" },
174 { 0x87CEEB, "skyblue" },
175 { 0x6A5ACD, "slateblue" },
176 { 0x708090, "slategray" },
177 { 0x708090, "slategrey" },
178 { 0xFFFAFA, "snow" },
179 { 0x00FF7F, "springgreen" },
180 { 0x4682B4, "steelblue" },
181 { 0xD2B48C, "tan" },
182 { 0x008080, "teal" },
183 { 0xD8BFD8, "thistle" },
184 { 0xFF6347, "tomato" },
185 { 0x40E0D0, "turquoise" },
186 { 0xEE82EE, "violet" },
187 { 0xF5DEB3, "wheat" },
188 { 0xFFFFFF, "white" },
189 { 0xF5F5F5, "whitesmoke" },
190 { 0xFFFF00, "yellow" },
191 { 0x9ACD32, "yellowgreen" }
192 };
194 static GHashTable *sp_svg_create_color_hash();
196 guint32
197 sp_svg_read_color(gchar const *str, guint32 const dfl)
198 {
199 return sp_svg_read_color(str, NULL, dfl);
200 }
202 static guint32
203 internal_sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 def)
204 {
205 static GHashTable *colors = NULL;
206 guint32 val = 0;
208 if (str == NULL) return def;
209 while ((*str <= ' ') && *str) str++;
210 if (!*str) return def;
212 if (str[0] == '#') {
213 gint i;
214 for (i = 1; str[i]; i++) {
215 int hexval;
216 if (str[i] >= '0' && str[i] <= '9')
217 hexval = str[i] - '0';
218 else if (str[i] >= 'A' && str[i] <= 'F')
219 hexval = str[i] - 'A' + 10;
220 else if (str[i] >= 'a' && str[i] <= 'f')
221 hexval = str[i] - 'a' + 10;
222 else
223 break;
224 val = (val << 4) + hexval;
225 }
226 /* handle #rgb case */
227 if (i == 1 + 3) {
228 val = ((val & 0xf00) << 8) |
229 ((val & 0x0f0) << 4) |
230 (val & 0x00f);
231 val |= val << 4;
232 } else if (i != 1 + 6) {
233 /* must be either 3 or 6 digits. */
234 return def;
235 }
236 if (end_ptr) {
237 *end_ptr = str + i;
238 }
239 } else if (strneq(str, "rgb(", 4)) {
240 bool hasp, hasd;
241 gchar *s, *e;
242 gdouble r, g, b;
244 s = (gchar *) str + 4;
245 hasp = false;
246 hasd = false;
248 r = g_ascii_strtod(s, &e);
249 if (s == e) return def;
250 s = e;
251 if (*s == '%') {
252 hasp = true;
253 s += 1;
254 } else {
255 hasd = true;
256 }
257 while (*s && g_ascii_isspace(*s)) s += 1;
258 if (*s != ',') return def;
259 s += 1;
260 while (*s && g_ascii_isspace(*s)) s += 1;
261 g = g_ascii_strtod(s, &e);
262 if (s == e) return def;
263 s = e;
264 if (*s == '%') {
265 hasp = true;
266 s += 1;
267 } else {
268 hasd = true;
269 }
270 while (*s && g_ascii_isspace(*s)) s += 1;
271 if (*s != ',') return def;
272 s += 1;
273 while (*s && g_ascii_isspace(*s)) s += 1;
274 b = g_ascii_strtod(s, &e);
275 if (s == e) return def;
276 s = e;
277 if (*s == '%') {
278 hasp = true;
279 s += 1;
280 } else {
281 hasd = true;
282 }
283 while(*s && g_ascii_isspace(*s)) s += 1;
284 if (*s != ')') {
285 return def;
286 }
287 ++s;
288 if (hasp && hasd) return def;
289 if (hasp) {
290 val = (guint) floor(CLAMP(r, 0.0, 100.0) * 2.559999) << 24;
291 val |= ((guint) floor(CLAMP(g, 0.0, 100.0) * 2.559999) << 16);
292 val |= ((guint) floor(CLAMP(b, 0.0, 100.0) * 2.559999) << 8);
293 } else {
294 val = (guint) CLAMP(r, 0, 255) << 24;
295 val |= ((guint) CLAMP(g, 0, 255) << 16);
296 val |= ((guint) CLAMP(b, 0, 255) << 8);
297 }
298 if (end_ptr) {
299 *end_ptr = s;
300 }
301 return val;
302 } else {
303 gint i;
304 if (!colors) {
305 colors = sp_svg_create_color_hash();
306 }
307 gchar c[32];
308 for (i = 0; i < 31; i++) {
309 if (str[i] == ';' || g_ascii_isspace(str[i])) {
310 c[i] = '\0';
311 break;
312 }
313 c[i] = g_ascii_tolower(str[i]);
314 if (!str[i]) break;
315 }
316 c[31] = '\0';
318 gpointer const rgb_ptr = g_hash_table_lookup(colors, c);
319 if (rgb_ptr) {
320 val = *(static_cast<unsigned long *>(rgb_ptr));
321 } else {
322 return def;
323 }
324 if (end_ptr) {
325 *end_ptr = str + i;
326 }
327 }
329 return (val << 8);
330 }
332 guint32
333 sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 dfl)
334 {
335 /* I've been rather hurried in editing the above to add support for end_ptr, so I'm adding
336 * this check wrapper. */
337 gchar const *end = str;
338 guint32 const ret = internal_sp_svg_read_color(str, &end, dfl);
339 assert(ret == dfl && end == str
340 || (((ret & 0xff) == 0)
341 && str < end));
342 if (str < end) {
343 gchar *buf = (gchar *) g_malloc(end + 1 - str);
344 memcpy(buf, str, end - str);
345 buf[end - str] = '\0';
346 gchar const *buf_end = buf;
347 guint32 const check = internal_sp_svg_read_color(buf, &buf_end, 1);
348 assert(check == ret
349 && buf_end - buf == end - str);
350 g_free(buf);
352 if ( end_ptr ) {
353 *end_ptr = end;
354 }
355 }
356 return ret;
357 }
360 /**
361 * Converts an RGB colour expressed in form 0x00rrggbb to a CSS/SVG representation of that colour.
362 * The result is valid even in SVG Tiny or non-SVG CSS.
363 */
364 static void
365 rgb24_to_css(char *const buf, unsigned const rgb24)
366 {
367 assert(rgb24 < (1u << 24));
369 /* SVG 1.1 Full allows additional colour names not supported by SVG Tiny, but we don't bother
370 * with them: it's good for these colours to be copyable to non-SVG CSS stylesheets and for
371 * documents to be more viewable in SVG Tiny/Basic viewers; and some of the SVG Full names are
372 * less meaningful than hex equivalents anyway. And it's easier for a person to map from the
373 * restricted set because the only component values are {00,80,ff}, other than silver 0xc0c0c0.
374 */
376 char const *src = NULL;
377 switch (rgb24) {
378 /* Extracted mechanically from the table at
379 * http://www.w3.org/TR/REC-html40/types.html#h-6.5 .*/
380 case 0x000000: src = "black"; break;
381 case 0xc0c0c0: src = "silver"; break;
382 case 0x808080: src = "gray"; break;
383 case 0xffffff: src = "white"; break;
384 case 0x800000: src = "maroon"; break;
385 case 0xff0000: src = "red"; break;
386 case 0x800080: src = "purple"; break;
387 case 0xff00ff: src = "fuchsia"; break;
388 case 0x008000: src = "green"; break;
389 case 0x00ff00: src = "lime"; break;
390 case 0x808000: src = "olive"; break;
391 case 0xffff00: src = "yellow"; break;
392 case 0x000080: src = "navy"; break;
393 case 0x0000ff: src = "blue"; break;
394 case 0x008080: src = "teal"; break;
395 case 0x00ffff: src = "aqua"; break;
397 default: {
398 if ((rgb24 & 0xf0f0f) * 0x11 == rgb24) {
399 /* Can use the shorter three-digit form #rgb instead of #rrggbb. */
400 sprintf(buf, "#%x%x%x",
401 (rgb24 >> 16) & 0xf,
402 (rgb24 >> 8) & 0xf,
403 rgb24 & 0xf);
404 } else {
405 sprintf(buf, "#%06x", rgb24);
406 }
407 break;
408 }
409 }
410 if (src) {
411 strcpy(buf, src);
412 }
414 // assert(sp_svg_read_color(buf, 0xff) == (rgb24 << 8));
415 }
417 /**
418 * Converts an RGBA32 colour to a CSS/SVG representation of the RGB portion of that colour. The
419 * result is valid even in SVG Tiny or non-SVG CSS.
420 *
421 * \param rgba32 Colour expressed in form 0xrrggbbaa.
422 * \pre buflen \>= 8.
423 */
424 void
425 sp_svg_write_color(gchar *buf, unsigned const buflen, guint32 const rgba32)
426 {
427 g_assert(8 <= buflen);
429 unsigned const rgb24 = rgba32 >> 8;
430 if (prefs_get_int_attribute("options.svgoutput", "usenamedcolors", 0)) {
431 rgb24_to_css(buf, rgb24);
432 } else {
433 g_snprintf(buf, buflen, "#%06x", rgb24);
434 }
435 }
437 static GHashTable *
438 sp_svg_create_color_hash()
439 {
440 GHashTable *colors = g_hash_table_new(g_str_hash, g_str_equal);
442 for (unsigned i = 0 ; i < G_N_ELEMENTS(sp_svg_color_named) ; i++) {
443 g_hash_table_insert(colors,
444 (gpointer)(sp_svg_color_named[i].name),
445 (gpointer)(&sp_svg_color_named[i].rgb));
446 }
448 return colors;
449 }
452 bool sp_svg_read_icc_color( gchar const *str, gchar const **end_ptr, SVGICCColor* dest )
453 {
454 bool good = true;
456 if ( end_ptr ) {
457 *end_ptr = str;
458 }
459 if ( dest ) {
460 dest->colorProfile.clear();
461 dest->colors.clear();
462 }
464 if ( !str ) {
465 // invalid input
466 good = false;
467 } else {
468 while ( g_ascii_isspace(*str) ) {
469 str++;
470 }
472 good = strneq( str, "icc-color(", 10 );
474 if ( good ) {
475 str += 10;
476 while ( g_ascii_isspace(*str) ) {
477 str++;
478 }
480 if ( !g_ascii_isalpha(*str)
481 && ( !(0x080 & *str) ) ) {
482 // Name must start with a certain type of character
483 good = false;
484 } else {
485 while ( g_ascii_isdigit(*str) || g_ascii_isalpha(*str) || (*str == '-') ) {
486 if ( dest ) {
487 dest->colorProfile += *str;
488 }
489 str++;
490 }
491 while ( g_ascii_isspace(*str) || *str == ',' ) {
492 str++;
493 }
494 }
495 }
497 if ( good ) {
498 while ( *str && *str != ')' ) {
499 if ( g_ascii_isdigit(*str) || *str == '.' || *str == '-' || *str == '+') {
500 gchar* endPtr = 0;
501 gdouble dbl = g_ascii_strtod( str, &endPtr );
502 if ( !errno ) {
503 if ( dest ) {
504 dest->colors.push_back( dbl );
505 }
506 str = endPtr;
507 } else {
508 good = false;
509 break;
510 }
512 while ( g_ascii_isspace(*str) || *str == ',' ) {
513 str++;
514 }
515 } else {
516 break;
517 }
518 }
519 }
521 // We need to have ended on a closing parenthesis
522 if ( good ) {
523 while ( g_ascii_isspace(*str) ) {
524 str++;
525 }
526 good &= *str == ')';
527 }
528 }
530 if ( good ) {
531 if ( end_ptr ) {
532 *end_ptr = str;
533 }
534 } else {
535 if ( dest ) {
536 dest->colorProfile.clear();
537 dest->colors.clear();
538 }
539 }
541 return good;
542 }
544 /*
545 Local Variables:
546 mode:c++
547 c-file-style:"stroustrup"
548 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
549 indent-tabs-mode:nil
550 fill-column:99
551 End:
552 */
553 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :