summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <matthiasc@src.gnome.org>2002-09-22 22:47:12 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2002-09-22 22:47:12 +0000
commitfb42249a4ecd56b3595fdb83e2a667abfc0aecbd (patch)
treedd33a75917ad454d085eefa6847145d322c7176f
parent2b3cf2de31923483ca0214079c878b00df3cadd4 (diff)
downloadpango-fb42249a4ecd56b3595fdb83e2a667abfc0aecbd.tar.gz
Make color parsing more robust and correct. (#93804)
* pango/pango-color.c (pango_color_parse): Make color parsing more robust and correct. (#93804) * tests/testcolor.c: Tests for pango_color_parse. * tests/Makefile.am: Build testcolor. * tests/runtests.sh: Run testcolor.
-rw-r--r--ChangeLog11
-rw-r--r--ChangeLog.pre-1-1011
-rw-r--r--ChangeLog.pre-1-211
-rw-r--r--ChangeLog.pre-1-411
-rw-r--r--ChangeLog.pre-1-611
-rw-r--r--ChangeLog.pre-1-811
-rw-r--r--pango/pango-color.c84
-rw-r--r--tests/Makefile.am6
-rwxr-xr-xtests/runtests.sh2
-rwxr-xr-xtests/runtests.sh.in2
-rw-r--r--tests/testcolor.c76
11 files changed, 191 insertions, 45 deletions
diff --git a/ChangeLog b/ChangeLog
index 19bbf17b..295d05b6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/ChangeLog.pre-1-10 b/ChangeLog.pre-1-10
index 19bbf17b..295d05b6 100644
--- a/ChangeLog.pre-1-10
+++ b/ChangeLog.pre-1-10
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/ChangeLog.pre-1-2 b/ChangeLog.pre-1-2
index 19bbf17b..295d05b6 100644
--- a/ChangeLog.pre-1-2
+++ b/ChangeLog.pre-1-2
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/ChangeLog.pre-1-4 b/ChangeLog.pre-1-4
index 19bbf17b..295d05b6 100644
--- a/ChangeLog.pre-1-4
+++ b/ChangeLog.pre-1-4
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/ChangeLog.pre-1-6 b/ChangeLog.pre-1-6
index 19bbf17b..295d05b6 100644
--- a/ChangeLog.pre-1-6
+++ b/ChangeLog.pre-1-6
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/ChangeLog.pre-1-8 b/ChangeLog.pre-1-8
index 19bbf17b..295d05b6 100644
--- a/ChangeLog.pre-1-8
+++ b/ChangeLog.pre-1-8
@@ -1,3 +1,14 @@
+2002-09-23 Matthias Clasen <maclas@gmx.de>
+
+ * pango/pango-color.c (pango_color_parse): Make color parsing
+ more robust and correct. (#93804)
+
+ * tests/testcolor.c: Tests for pango_color_parse.
+
+ * tests/Makefile.am: Build testcolor.
+
+ * tests/runtests.sh: Run testcolor.
+
2002-09-23 Tor Lillqvist <tml@iki.fi>
* modules/basic/basic-win32.c: Minor spacing fixes, comment the
diff --git a/pango/pango-color.c b/pango/pango-color.c
index b834140b..90e8d69d 100644
--- a/pango/pango-color.c
+++ b/pango/pango-color.c
@@ -914,6 +914,21 @@ find_color(const char *name,
return TRUE;
}
+static gboolean
+hex (const char *spec,
+ int len,
+ unsigned int *c)
+{
+ const char *end;
+ *c = 0;
+ for (end = spec + len; spec != end; spec++)
+ if (g_ascii_isxdigit (*spec))
+ *c = (*c << 4) | g_ascii_xdigit_value (*spec);
+ else
+ return FALSE;
+ return TRUE;
+}
+
/**
* pango_color_parse:
* @color: a #PangoColor structure in which to store the result
@@ -934,55 +949,41 @@ gboolean
pango_color_parse (PangoColor *color,
const char *spec)
{
+ g_return_val_if_fail (spec != NULL, FALSE);
+
if (spec[0] == '#')
{
- char fmt[16];
- int i, r, g, b;
+ size_t len;
+ unsigned int r, g, b;
- if ((i = strlen (spec+1)) % 3)
+ spec++;
+ len = strlen (spec);
+ if (len % 3 || len < 3 || len > 12)
return FALSE;
+
+ len /= 3;
- i /= 3;
+ if (!hex (spec, len, &r) ||
+ !hex (spec + len, len, &g) ||
+ !hex (spec + len * 2, len, &b))
+ return FALSE;
- sprintf (fmt, "%%%dx%%%dx%%%dx", i, i, i);
- if (sscanf (spec+1, fmt, &r, &g, &b) != 3)
- return FALSE;
-
- if (i == 4)
- {
- if (color)
+ if (color)
+ {
+ int bits = len * 4;
+ r <<= 16 - bits;
+ g <<= 16 - bits;
+ b <<= 16 - bits;
+ while (bits < 16)
{
- color->red = r;
- color->green = g;
- color->blue = b;
- }
- }
- else if (i == 1)
- {
- if (color)
- {
- color->red = (r * 65535) / 15;
- color->green = (g * 65535) / 15;
- color->blue = (b * 65535) / 15;
- }
- }
- else if (i == 2)
- {
- if (color)
- {
- color->red = (r * 65535) / 255;
- color->green = (g * 65535) / 255;
- color->blue = (b * 65535) / 255;
- }
- }
- else /* if (i == 3) */
- {
- if (color)
- {
- color->red = (r * 65535) / 4095;
- color->green = (g * 65535) / 4095;
- color->blue = (b * 65535) / 4095;
+ r |= (r >> bits);
+ g |= (g >> bits);
+ b |= (b >> bits);
+ bits *= 2;
}
+ color->red = r;
+ color->green = g;
+ color->blue = b;
}
}
else
@@ -990,6 +991,5 @@ pango_color_parse (PangoColor *color,
if (!find_color (spec, color))
return FALSE;
}
-
return TRUE;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 27f9331d..ce81a915 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -39,18 +39,22 @@ TESTS=runtests.sh
noinst_PROGRAMS = gen-all-unicode dump-boundaries
-check_PROGRAMS = testboundaries $(CXX_TEST)
+check_PROGRAMS = testboundaries testcolor $(CXX_TEST)
gen_all_unicode_SOURCES = gen-all-unicode.c
testboundaries_SOURCES = testboundaries.c
+testcolor_SOURCES = testcolor.c
+
dump_boundaries_SOURCES = dump-boundaries.c
gen_all_unicode_LDADD = $(GLIB_LIBS)
testboundaries_LDADD = ../pango/libpango-$(PANGO_API_VERSION).la
+testcolor_LDADD = ../pango/libpango-$(PANGO_API_VERSION).la
+
dump_boundaries_LDADD = ../pango/libpango-$(PANGO_API_VERSION).la
if HAVE_CXX
diff --git a/tests/runtests.sh b/tests/runtests.sh
index 3fd0b110..897cd120 100755
--- a/tests/runtests.sh
+++ b/tests/runtests.sh
@@ -1,7 +1,7 @@
#! /bin/sh
LOGFILE=runtests.log
-POTENTIAL_TESTS='testboundaries'
+POTENTIAL_TESTS='testboundaries testcolor'
for I in $POTENTIAL_TESTS
do
diff --git a/tests/runtests.sh.in b/tests/runtests.sh.in
index 3fd0b110..897cd120 100755
--- a/tests/runtests.sh.in
+++ b/tests/runtests.sh.in
@@ -1,7 +1,7 @@
#! /bin/sh
LOGFILE=runtests.log
-POTENTIAL_TESTS='testboundaries'
+POTENTIAL_TESTS='testboundaries testcolor'
for I in $POTENTIAL_TESTS
do
diff --git a/tests/testcolor.c b/tests/testcolor.c
new file mode 100644
index 00000000..e41b053e
--- /dev/null
+++ b/tests/testcolor.c
@@ -0,0 +1,76 @@
+#include <glib.h>
+#include <pango/pango.h>
+
+typedef struct _ColorSpec {
+ const gchar *spec;
+ gboolean valid;
+ guint16 red;
+ guint16 green;
+ guint16 blue;
+} ColorSpec;
+
+gboolean test_color (ColorSpec *spec)
+{
+ PangoColor color;
+ gboolean accepted;
+
+ accepted = pango_color_parse (&color, spec->spec);
+
+ if (accepted == spec->valid &&
+ (!accepted ||
+ (color.red == spec->red &&
+ color.green == spec->green &&
+ color.blue == spec->blue)))
+ return TRUE;
+ else
+ return FALSE;
+}
+
+
+ColorSpec specs [] = {
+ { "#abc", 1, 0xaaaa, 0xbbbb, 0xcccc },
+ { "#aabbcc", 1, 0xaaaa, 0xbbbb, 0xcccc },
+ { "#aaabbbccc", 1, 0xaaaa, 0xbbbb, 0xcccc },
+ { "#100100100", 1, 0x1001, 0x1001, 0x1001 },
+ { "#aaaabbbbcccc", 1, 0xaaaa, 0xbbbb, 0xcccc },
+ { "#fff", 1, 0xffff, 0xffff, 0xffff },
+ { "#ffffff", 1, 0xffff, 0xffff, 0xffff },
+ { "#fffffffff", 1, 0xffff, 0xffff, 0xffff },
+ { "#ffffffffffff", 1, 0xffff, 0xffff, 0xffff },
+ { "#000", 1, 0x0000, 0x0000, 0x0000 },
+ { "#000000", 1, 0x0000, 0x0000, 0x0000 },
+ { "#000000000", 1, 0x0000, 0x0000, 0x0000 },
+ { "#000000000000", 1, 0x0000, 0x0000, 0x0000 },
+ { "#AAAABBBBCCCC", 1, 0xaaaa, 0xbbbb, 0xcccc },
+ { "#aa bb cc ", 0 },
+ { "#aa bb ccc", 0 },
+ { "#ab", 0 },
+ { "#aabb", 0 },
+ { "#aaabb", 0 },
+ { "aaabb", 0 },
+ { "", 0 },
+ { "#", 0 },
+ { "##fff", 0 },
+ { "#0000ff+", 0 },
+ { "#0000f+", 0 },
+ { "#0x00x10x2", 0 },
+ { NULL, 0 }
+};
+
+int
+main (int argc, char *argv[])
+{
+ gboolean success;
+ ColorSpec *spec;
+ gchar *blob;
+
+ success = TRUE;
+ for (spec = specs; spec->spec; spec++)
+ success &= test_color (spec);
+
+ return !success;
+}
+
+
+
+