]> granicus.if.org Git - graphviz/commitdiff
fix for same names used for different values
authorellson <devnull@localhost>
Thu, 25 Sep 2008 15:29:56 +0000 (15:29 +0000)
committerellson <devnull@localhost>
Thu, 25 Sep 2008 15:29:56 +0000 (15:29 +0000)
lib/inkpot/inkpot_scheme.c
lib/inkpot/inkpot_structs.h
lib/inkpot/inkpot_tables.h
lib/inkpot/test.c

index 888c0d0ece031b843c34da2999a13a6afcabb420..67765eae89241e62c14ed3c4a2825b0b9139fa39 100644 (file)
@@ -100,12 +100,6 @@ inkpot_status_t inkpot_activate ( inkpot_t *inkpot, const char *scheme )
         if (! inkpot->scheme_bits) {
             inkpot->default_scheme_name_idx = idx; /* first scheme is default */
             inkpot->default_value_idx = TAB_NAMES[inkpot_scheme_name->default_name_idx].value_idx;
-            inkpot->first_name_idx = inkpot_scheme_name->first_name_idx;
-            inkpot->last_name_idx = inkpot_scheme_name->last_name_idx;
-        }
-        else {
-                inkpot->first_name_idx = MIN(inkpot->first_name_idx, inkpot_scheme_name->first_name_idx);
-                inkpot->last_name_idx = MAX(inkpot->last_name_idx, inkpot_scheme_name->last_name_idx);
         }
        if (! (inkpot->scheme_bits & (1 << idx))) {
                inkpot->scheme_bits |= 1 << idx;
@@ -170,37 +164,45 @@ static inkpot_status_t inkpot_set_name ( inkpot_t *inkpot, const char *color )
 {
     inkpot_name_t *name;
     const char *last_color;
-    IDX_NAMES j;
-    IDX_STRINGS k;
+    IDX_NAME_ALTS i;
+    IDX_NAMES base, top, name_idx = 0;
+    int found;
 
-    if (inkpot == NULL)
+    if (inkpot == NULL || ! inkpot->scheme_bits)
         return INKPOT_SCHEME_UNKNOWN;
     if (color == NULL)
         return INKPOT_COLOR_UNKNOWN;
     if (! inkpot->name  /* if we can't use the last result */
                || ! ((last_color = &TAB_STRINGS[inkpot->name->string_idx]))
                || ( last_color[0] != color[0] )
-               || ( strcmp(last_color, color) != 0)) {
-        name = (inkpot_name_t *) bsearch(  /* then do a fresh search */
-            (void*)color, (void*)(&TAB_NAMES[inkpot->first_name_idx]),
-            inkpot->last_name_idx + 1 - inkpot->first_name_idx,
-            sizeof(inkpot_name_t), inkpot_name_cmpf); 
-       if (name == NULL) 
-            return INKPOT_COLOR_UNKNOWN;
+               || ( strcmp(last_color, color) != 0)) { /* do a fresh search */
+       for (i = 0; i < SZT_NAME_ALTS; ) {
+           base = TAB_NAME_ALTS[i++];
+           if (i == SZT_NAME_ALTS)
+               top = SZT_NAMES;
+           else
+               top = TAB_NAME_ALTS[i];
+            name = (inkpot_name_t *) bsearch(
+                (void*)color, (void*)(&TAB_NAMES[base]),
+               top-base, sizeof(inkpot_name_t),
+               inkpot_name_cmpf); 
+           if (name == NULL) 
+                return INKPOT_COLOR_UNKNOWN;
        
-       j = name - TAB_NAMES;
-       k = TAB_NAMES[j].string_idx;
-       while ( j < SZT_NAMES && k == TAB_NAMES[j].string_idx && ! (inkpot->scheme_bits & TAB_NAMES[j].scheme_bits)) {
-           /* There can be multiple entries for the same
-             * color string with different values. Linearly search
-             * through them for the first one in the requested scheme(s) */
-            j++;
+           name_idx = name - TAB_NAMES;
+           if (inkpot->scheme_bits & TAB_NAMES[name_idx].scheme_bits) {
+               found++;
+               break;
+           }
        }
-       if (k != TAB_NAMES[j].string_idx || j == SZT_NAMES)
+       if (!found)
            return INKPOT_COLOR_UNKNOWN;
-       inkpot->name = &TAB_NAMES[j];  /* cache name resolution */
+       inkpot->name = &TAB_NAMES[name_idx];  /* cache name resolution */
+    }
+    if (inkpot->value_idx != inkpot->name->value_idx) {
+        inkpot->value_idx = inkpot->name->value_idx;
+       inkpot->out_name = NULL;  /* invalidate out cached name */
     }
-    inkpot->value_idx = inkpot->name->value_idx;
     return INKPOT_SUCCESS;
 }
 
index a5b9179dae3f52754112be93577498de3d70a021..2fda491a5c66a198fbec656a798323d01e6a0255 100644 (file)
@@ -22,14 +22,6 @@ typedef struct inkpot_scheme_name_s {
             string_idx;        /* The scheme name in TAB_STRINGS.  (e.g. "x11") */
 
        IDX_NAMES
-           first_name_idx,
-           last_name_idx,      /* The range of inkpot_name_t in TAB_NAMES for
-                                * search optimization.  Might help schemes with a few
-                                * alphabetically close names but otherwise these probably
-                                * doesn't buy much since most schemes will have inkpot_name_t
-                                * covering substantially the whole alphabet. Not all the
-                                * inkpot_name_t in the range belong to this scheme  */
-
            default_name_idx;   /* The index of the name of the default color
                                 * for this scheme. (Each scheme can have
                                 * a different default.  The default must
@@ -120,26 +112,18 @@ struct inkpot_s {         /* The Ink Pot */
                                 *max 32 schemes */
            out_scheme_bit;     /* One scheme only for output. */
 
-       IDX_NAMES
-           first_name_idx, last_name_idx;  /* The aggregate of the ranges
-                                * of inkpot_name_t from all inkpot_scheme_t
-                                * in the union. This range of of inkpot_name_t
-                                * in TAB_NAMES is for search optimization.
-                                * Might help if just an indexed scheme
-                                * is used. */
-
        IDX_SCHEMES_NAME
            default_scheme_name_idx; /* The index of the scheme which provides
                                 * the default color. It was the first
                                 * scheme added. */
 
-       IDX_VALUES              /* FIXME - what about noname values ? */
+       IDX_VALUES
            default_value_idx,  /* The default color */
            value_idx;          /* The current color value. */
 
        inkpot_scheme_index_t
-           *scheme_index,      /* Pointer to the indexed
-                                * color scheme, or NULL. (Only 1 at a time) */
+           *scheme_index,      /* Pointer to the indexed color scheme,
+                                  or NULL. (Only 1 at a time) */
            *out_scheme_index;  /* Indexed output scheme, or NULL */
 
        inkpot_name_t
index 8aac9a85c2a8a48b0ce4733ae1257d423e1f91fa..f61a814eb16e91d14d5d9444005ce4e99552e0a6 100644 (file)
@@ -3,6 +3,7 @@ typedef unsigned char BIT_SCHEMES_NAME;
 typedef unsigned char IDX_SCHEMES_NAME;
 typedef unsigned char IDX_SCHEMES_INDEX;
 typedef unsigned char IDX_NAMES;
+typedef unsigned char IDX_NAME_ALTS;
 typedef unsigned char IDX_VALUES;
 typedef unsigned char IDX_TONAMES;
 typedef unsigned char IDX_IXVALUES;
@@ -29,9 +30,9 @@ const char TAB_STRINGS[] = {
 #define SZT_STRINGS sizeof(TAB_STRINGS)/sizeof(TAB_STRINGS[0])
 
 inkpot_scheme_name_t TAB_SCHEMES_NAME[] = { /* schemes of named colors */
-       {  0, 1,  7, 1, },  /* french */
-       {  7, 0, 10, 0, },  /* svg */
-       { 11, 0, 10, 0, },  /* x11 */
+       {  0, 1, },  /* french */
+       {  7, 0, },  /* svg */
+       { 11, 0, },  /* x11 */
 };
 #define SZT_SCHEMES_NAME (sizeof(TAB_SCHEMES_NAME)/sizeof(TAB_SCHEMES_NAME[0]))
 
@@ -41,24 +42,30 @@ inkpot_scheme_index_t TAB_SCHEMES_INDEX[] = { /* schemes of indexed colors */
 };
 #define SZT_SCHEMES_INDEX (sizeof(TAB_SCHEMES_INDEX)/sizeof(TAB_SCHEMES_INDEX[0]))
 
-inkpot_name_t TAB_NAMES[] = { /* Must be LC_ALL=C sort'ed by name */
-       { 15, 0, 0x6,  0, }, /* black */
-       { 21, 1, 0x1,  1, }, /* bleu */
-       { 26, 1, 0x6,  2, }, /* blue */
-       { 31, 2, 0x6,  3, }, /* green */
-       { 37, 5, 0x1,  7, }, /* jaune */
-       { 43, 3, 0x6,  5, }, /* red */
-       { 47, 3, 0x1,  6, }, /* rouge */
-       { 53, 2, 0x1,  8, }, /* vert */
-       { 58, 4, 0x2,  4, }, /* yellow (svg) */
-       { 58, 5, 0x4,  9, }, /* yellow (x11) */
-       { 65, 6, 0x6, 10, }, /* white */
+inkpot_name_t TAB_NAMES[] = { /* Must be LC_ALL=C sort'ed by name with each alt */
+       { 15, 0, 0x6, /* black */  0,  /* black */         }, /* 0  */
+       { 21, 1, 0x1, /* bleu */   1,  /* bleu, blue */    }, /* 1  */
+       { 26, 1, 0x6, /* blue */   2,                      }, /* 2  */
+       { 31, 2, 0x6, /* green */  3,  /* green, vert */   }, /* 3  */
+       { 37, 5, 0x1, /* jaune */  7,                      }, /* 4  */
+       { 43, 3, 0x6, /* red */    5,  /* red, rouge */    }, /* 5  */
+       { 47, 3, 0x1, /* rouge */  6,                      }, /* 6  */
+       { 53, 2, 0x1, /* vert */   10, /* yellow(alt1) */  }, /* 7  */
+       { 65, 6, 0x6, /* white */  4,  /* jaune, yellow */ }, /* 8  */
+       { 58, 5, 0x4, /* yellow */ 9,                      }, /* 9  */
+/* alt1 */
+       { 58, 4, 0x2, /* yellow */ 8,  /* white */         }, /* 10 */
 };
 #define SZT_NAMES (sizeof(TAB_NAMES)/sizeof(TAB_NAMES[0]))
 
+IDX_NAMES TAB_NAME_ALTS[] = {
+       0, 10,
+};
+#define SZT_NAME_ALTS (sizeof(TAB_NAME_ALTS)/sizeof(TAB_NAME_ALTS[0]))
+
 inkpot_value_t TAB_VALUES[] = { /* Must be LC_ALL=C sort'ed by r,g,b,a */
        {{0,    0,      0,      255},   0 },  /* black */
-       {{0,    0,      255,    255},   1 },  /* blue, bleu */
+       {{0,    0,      255,    255},   1 },  /* bleu, blue */
        {{0,    255,    0,      255},   3 },  /* green, vert */
        {{255,  0,      0,      255},   5 },  /* red, rouge */
        {{255,  192,    0,      255},   7 },  /* yellow (svg) */
index 48dc805f73735b4edad0c2c9eb951ea5d7d44fec..91c22acf9e2b4e3da4d55f86dcd1cca0d578bd0f 100644 (file)
@@ -59,14 +59,16 @@ int main (int argc, char *argv[])
     rc = inkpot_set(inkpot, color);
     if (rc == INKPOT_SUCCESS || rc == INKPOT_COLOR_NONAME) {
        inkpot_get_rgba(inkpot, rgba);
-       inkpot_get(inkpot, &tocolor);
+       rc = inkpot_get(inkpot, &tocolor);
        if (rc == INKPOT_SUCCESS) 
             fprintf(stderr, "%s", tocolor);
        else if (rc == INKPOT_COLOR_NONAME) 
-            fprintf(stderr, "#%2x%2x%2x%2x", rgba[0], rgba[1], rgba[2], rgba[3]);
+            fprintf(stderr, "#%02x%02x%02x%02x",
+                           rgba[0], rgba[1], rgba[2], rgba[3]);
        else
            assert(0);
-        fprintf(stderr, " %d,%d,%d,%d\n", rgba[0], rgba[1], rgba[2], rgba[3]);
+        fprintf(stderr, " %d,%d,%d,%d\n",
+                       rgba[0], rgba[1], rgba[2], rgba[3]);
 
     }
     else if (rc == INKPOT_COLOR_UNKNOWN) {