[dia] svg: support the full set of named colors



commit 3b8f496933652b22cdb96a9898066ec443199a4a
Author: Hans Breuer <hans breuer org>
Date:   Fri Aug 23 17:45:24 2013 +0200

    svg: support the full set of named colors
    
    First simple implementation with bsearch(), if it turns out
    to be slow while profiling it could be easily be converted
    to a static leaked hash table.

 lib/dia_svg.c |  195 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 171 insertions(+), 24 deletions(-)
---
diff --git a/lib/dia_svg.c b/lib/dia_svg.c
index d924806..b16c171 100644
--- a/lib/dia_svg.c
+++ b/lib/dia_svg.c
@@ -97,36 +97,178 @@ dia_svg_style_copy(DiaSvgStyle *dest, DiaSvgStyle *src)
 }
 
 static const struct _SvgNamedColor {
-  const char *name;
   const gint  value;
+  const char *name;
 } _svg_named_colors [] = {
-  { "maroon", 0x800000 },
-  { "red", 0xff0000 },
-  { "orange", 0xffA500 },
-  { "yellow", 0xffff00 },
-  { "olive", 0x808000 },
-  { "purple", 0x800080 },
-  { "fuchsia", 0xff00ff },
-  { "white", 0xffffff },
-  { "lime", 0x00ff00 },
-  { "green", 0x008000 },
-  { "navy", 0x000080 },
-  { "blue", 0x0000ff },
-  { "aqua", 0x00ffff },
-  { "teal", 0x008080 },
-  { "black", 0x000000 },
-  { "silver", 0xc0c0c0 },
-  { "gray", 0x808080 }
-
+  /* complete list copied from sodipodi source */
+       { 0xF0F8FF, "aliceblue" },
+       { 0xFAEBD7, "antiquewhite" },
+       { 0x00FFFF, "aqua" },
+       { 0x7FFFD4, "aquamarine" },
+       { 0xF0FFFF, "azure" },
+       { 0xF5F5DC, "beige" },
+       { 0xFFE4C4, "bisque" },
+       { 0x000000, "black" },
+       { 0xFFEBCD, "blanchedalmond" },
+       { 0x0000FF, "blue" },
+       { 0x8A2BE2, "blueviolet" },
+       { 0xA52A2A, "brown" },
+       { 0xDEB887, "burlywood" },
+       { 0x5F9EA0, "cadetblue" },
+       { 0x7FFF00, "chartreuse" },
+       { 0xD2691E, "chocolate" },
+       { 0xFF7F50, "coral" },
+       { 0x6495ED, "cornflowerblue" },
+       { 0xFFF8DC, "cornsilk" },
+       { 0xDC143C, "crimson" },
+       { 0x00FFFF, "cyan" },
+       { 0x00008B, "darkblue" },
+       { 0x008B8B, "darkcyan" },
+       { 0xB8860B, "darkgoldenrod" },
+       { 0xA9A9A9, "darkgray" },
+       { 0x006400, "darkgreen" },
+       { 0xA9A9A9, "darkgrey" },
+       { 0xBDB76B, "darkkhaki" },
+       { 0x8B008B, "darkmagenta" },
+       { 0x556B2F, "darkolivegreen" },
+       { 0xFF8C00, "darkorange" },
+       { 0x9932CC, "darkorchid" },
+       { 0x8B0000, "darkred" },
+       { 0xE9967A, "darksalmon" },
+       { 0x8FBC8F, "darkseagreen" },
+       { 0x483D8B, "darkslateblue" },
+       { 0x2F4F4F, "darkslategray" },
+       { 0x2F4F4F, "darkslategrey" },
+       { 0x00CED1, "darkturquoise" },
+       { 0x9400D3, "darkviolet" },
+       { 0xFF1493, "deeppink" },
+       { 0x00BFFF, "deepskyblue" },
+       { 0x696969, "dimgray" },
+       { 0x696969, "dimgrey" },
+       { 0x1E90FF, "dodgerblue" },
+       { 0xB22222, "firebrick" },
+       { 0xFFFAF0, "floralwhite" },
+       { 0x228B22, "forestgreen" },
+       { 0xFF00FF, "fuchsia" },
+       { 0xDCDCDC, "gainsboro" },
+       { 0xF8F8FF, "ghostwhite" },
+       { 0xFFD700, "gold" },
+       { 0xDAA520, "goldenrod" },
+       { 0x808080, "gray" },
+       { 0x808080, "grey" },
+       { 0x008000, "green" },
+       { 0xADFF2F, "greenyellow" },
+       { 0xF0FFF0, "honeydew" },
+       { 0xFF69B4, "hotpink" },
+       { 0xCD5C5C, "indianred" },
+       { 0x4B0082, "indigo" },
+       { 0xFFFFF0, "ivory" },
+       { 0xF0E68C, "khaki" },
+       { 0xE6E6FA, "lavender" },
+       { 0xFFF0F5, "lavenderblush" },
+       { 0x7CFC00, "lawngreen" },
+       { 0xFFFACD, "lemonchiffon" },
+       { 0xADD8E6, "lightblue" },
+       { 0xF08080, "lightcoral" },
+       { 0xE0FFFF, "lightcyan" },
+       { 0xFAFAD2, "lightgoldenrodyellow" },
+       { 0xD3D3D3, "lightgray" },
+       { 0x90EE90, "lightgreen" },
+       { 0xD3D3D3, "lightgrey" },
+       { 0xFFB6C1, "lightpink" },
+       { 0xFFA07A, "lightsalmon" },
+       { 0x20B2AA, "lightseagreen" },
+       { 0x87CEFA, "lightskyblue" },
+       { 0x778899, "lightslategray" },
+       { 0x778899, "lightslategrey" },
+       { 0xB0C4DE, "lightsteelblue" },
+       { 0xFFFFE0, "lightyellow" },
+       { 0x00FF00, "lime" },
+       { 0x32CD32, "limegreen" },
+       { 0xFAF0E6, "linen" },
+       { 0xFF00FF, "magenta" },
+       { 0x800000, "maroon" },
+       { 0x66CDAA, "mediumaquamarine" },
+       { 0x0000CD, "mediumblue" },
+       { 0xBA55D3, "mediumorchid" },
+       { 0x9370DB, "mediumpurple" },
+       { 0x3CB371, "mediumseagreen" },
+       { 0x7B68EE, "mediumslateblue" },
+       { 0x00FA9A, "mediumspringgreen" },
+       { 0x48D1CC, "mediumturquoise" },
+       { 0xC71585, "mediumvioletred" },
+       { 0x191970, "midnightblue" },
+       { 0xF5FFFA, "mintcream" },
+       { 0xFFE4E1, "mistyrose" },
+       { 0xFFE4B5, "moccasin" },
+       { 0xFFDEAD, "navajowhite" },
+       { 0x000080, "navy" },
+       { 0xFDF5E6, "oldlace" },
+       { 0x808000, "olive" },
+       { 0x6B8E23, "olivedrab" },
+       { 0xFFA500, "orange" },
+       { 0xFF4500, "orangered" },
+       { 0xDA70D6, "orchid" },
+       { 0xEEE8AA, "palegoldenrod" },
+       { 0x98FB98, "palegreen" },
+       { 0xAFEEEE, "paleturquoise" },
+       { 0xDB7093, "palevioletred" },
+       { 0xFFEFD5, "papayawhip" },
+       { 0xFFDAB9, "peachpuff" },
+       { 0xCD853F, "peru" },
+       { 0xFFC0CB, "pink" },
+       { 0xDDA0DD, "plum" },
+       { 0xB0E0E6, "powderblue" },
+       { 0x800080, "purple" },
+       { 0xFF0000, "red" },
+       { 0xBC8F8F, "rosybrown" },
+       { 0x4169E1, "royalblue" },
+       { 0x8B4513, "saddlebrown" },
+       { 0xFA8072, "salmon" },
+       { 0xF4A460, "sandybrown" },
+       { 0x2E8B57, "seagreen" },
+       { 0xFFF5EE, "seashell" },
+       { 0xA0522D, "sienna" },
+       { 0xC0C0C0, "silver" },
+       { 0x87CEEB, "skyblue" },
+       { 0x6A5ACD, "slateblue" },
+       { 0x708090, "slategray" },
+       { 0x708090, "slategrey" },
+       { 0xFFFAFA, "snow" },
+       { 0x00FF7F, "springgreen" },
+       { 0x4682B4, "steelblue" },
+       { 0xD2B48C, "tan" },
+       { 0x008080, "teal" },
+       { 0xD8BFD8, "thistle" },
+       { 0xFF6347, "tomato" },
+       { 0x40E0D0, "turquoise" },
+       { 0xEE82EE, "violet" },
+       { 0xF5DEB3, "wheat" },
+       { 0xFFFFFF, "white" },
+       { 0xF5F5F5, "whitesmoke" },
+       { 0xFFFF00, "yellow" },
+       { 0x9ACD32, "yellowgreen" }
 };
 
+static int
+_cmp_color (const void *key, const void *elem)
+{
+  const char *a = key;
+  const struct _SvgNamedColor *color = elem;
+
+  return strcmp (a, color->name);
+}
+
 /*!
  * \brief Get an SVG color value by name
  *
- * The list of named SVG colors has only 17 entries according to
+ * The list of named SVG tiny colors has only 17 entries according to
  * http://www.w3.org/TR/CSS21/syndata.html#color-units
  * Still pango_color_parse() does not support seven of them including
  * 'white'. This function supports all of them.
+ * Against the SVG full specification (and the SVG Test Suite) pango's
+ * long list is still missing colors, e.g. crimson. So this function is
+ * using a supposed to be complete internal list.
  *
  * \ingroup DiaSvg
  */
@@ -134,15 +276,18 @@ static gboolean
 svg_named_color (const char *name, gint32 *color)
 {
   int i;
+  const struct _SvgNamedColor *elem;
 
   g_return_val_if_fail (name != NULL && color != NULL, FALSE);
 
-  for (i = 0; i < G_N_ELEMENTS(_svg_named_colors); i++) {
-    if (strncmp (name, _svg_named_colors[i].name, strlen(_svg_named_colors[i].name)) == 0) {
-      *color = _svg_named_colors[i].value;
+  elem = bsearch (name, _svg_named_colors,
+                 G_N_ELEMENTS(_svg_named_colors), sizeof(_svg_named_colors[0]),
+                 _cmp_color);
+  if (elem) {
+    *color = elem->value;
       return TRUE;
     }
-  }
+
   return FALSE;
 }
 /*! 
@@ -214,6 +359,8 @@ _parse_color(gint32 *color, const char *str)
     /* Pango needs null terminated strings, so we just use it as a fallback */
     PangoColor pc;
     char* se = strchr (str, ';');
+    if (!se) /* style might have trailign space */
+      se = strchr (str, ' ');
 
     if (!se) {
       if (pango_color_parse (&pc, str))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]