Code

Moving out icc parser
authorjoncruz <joncruz@users.sourceforge.net>
Wed, 5 Apr 2006 03:09:15 +0000 (03:09 +0000)
committerjoncruz <joncruz@users.sourceforge.net>
Wed, 5 Apr 2006 03:09:15 +0000 (03:09 +0000)
src/style.cpp
src/svg/svg-color-test.h
src/svg/svg-color.cpp
src/svg/svg-color.h

index 230d8f46694f1a9be73efdee78d909279a0a724e..2a0a30bd165b91fb4b12171307aa3bb3463b90da 100644 (file)
@@ -40,7 +40,7 @@
 #include "xml/repr.h"
 #include "unit-constants.h"
 #include "isnan.h"
-#include <errno.h>
+
 using Inkscape::CSSOStringStream;
 using std::vector;
 
@@ -2915,36 +2915,10 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                 ++str;
             }
             if (strneq(str, "icc-color(", 10)) {
-                str += 10;
-
                 SVGICCColor* tmp = new SVGICCColor();
-                while ( *str && *str != ' ' && *str!= ',' && *str != ')' ) {
-                    tmp->colorProfile += *str++;
-                }
-                while ( g_ascii_isspace(*str) || *str == ',' ) {
-                    ++str;
-                }
-
-                while ( *str && *str != ')' ) {
-                    if ( g_ascii_isdigit(*str) || *str == '.' ) {
-                        gchar* endPtr = 0;
-                        gdouble dbl = g_ascii_strtod( str, &endPtr );
-                        if ( !errno ) {
-                            tmp->colors.push_back( dbl );
-                            str = endPtr;
-                        } else {
-                            delete tmp;
-                            tmp = 0;
-                            break;
-                        }
-
-                        while ( tmp && g_ascii_isspace(*str) || *str == ',' ) {
-                            ++str;
-                        }
-                    } else {
-                        break;
-                    }
-
+                if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                    delete tmp;
+                    tmp = 0;
                 }
                 paint->iccColor = tmp;
             }
index 4d9b2e854ccdc098b190fb945685afc2d4fe914a..ac79ac95903a23c10dd4816a612ee099c2c186b7 100644 (file)
@@ -1,46 +1,72 @@
 #include <cxxtest/TestSuite.h>
 #include "svg/svg-color.h"
+#include "svg/svg-icc-color.h"
 
-static void
-test_rgb24(unsigned const rgb24)
-{
-    char css[8];
-    sp_svg_write_color(css, sizeof(css), rgb24 << 8);
-    TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
-                     rgb24 << 8);
-}
-
-static void
-test_sp_svg_write_color()
+class SVGColorTest : public CxxTest::TestSuite
 {
-    unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
-    unsigned const nc = G_N_ELEMENTS(components);
-    for (unsigned i = nc*nc*nc; i--;) {
-        unsigned tmp = i;
-        unsigned rgb24 = 0;
-        for (unsigned c = 0; c < 3; ++c) {
-            unsigned const component = components[tmp % nc];
-            rgb24 = (rgb24 << 8) | component;
-            tmp /= nc;
+public:
+    void check_rgb24(unsigned const rgb24)
+    {
+        char css[8];
+        sp_svg_write_color(css, sizeof(css), rgb24 << 8);
+        TS_ASSERT_EQUALS(sp_svg_read_color(css, 0xff),
+                         rgb24 << 8);
+    }
+
+    void testWrite()
+    {
+        unsigned const components[] = {0, 0x80, 0xff, 0xc0, 0x77};
+        unsigned const nc = G_N_ELEMENTS(components);
+        for (unsigned i = nc*nc*nc; i--;) {
+            unsigned tmp = i;
+            unsigned rgb24 = 0;
+            for (unsigned c = 0; c < 3; ++c) {
+                unsigned const component = components[tmp % nc];
+                rgb24 = (rgb24 << 8) | component;
+                tmp /= nc;
+            }
+            TS_ASSERT_EQUALS( tmp, 0 );
+            check_rgb24(rgb24);
+        }
+
+        /* And a few completely random ones. */
+        for (unsigned i = 500; i--;) {  /* Arbitrary number of iterations. */
+            unsigned const rgb24 = (rand() >> 4) & 0xffffff;
+            check_rgb24(rgb24);
         }
-        assert(tmp == 0);
-        test_rgb24(rgb24);
     }
 
-    /* And a few completely random ones. */
-    for (unsigned i = 500; i--;) {  /* Arbitrary number of iterations. */
-        unsigned const rgb24 = (rand() >> 4) & 0xffffff;
-        test_rgb24(rgb24);
+    void testReadColor()
+    {
+        gchar* val="#f0f";
+        gchar const * end = 0;
+        guint32 result = sp_svg_read_color( val, &end, 0x3 );
+        TS_ASSERT_EQUALS( result, 0xff00ff00 );
+        TS_ASSERT_LESS_THAN( val, end );
     }
-}
 
-class SVGColorTest : public CxxTest::TestSuite
-{
-public:
-    void testWrite()
+    void testIccColor()
     {
-        test_sp_svg_write_color();
+        SVGICCColor tmp;
+        gchar* str = "icc-color(named, 3)";
+        gchar const * result = 0;
+
+        bool parseRet = sp_svg_read_icc_color( str, &result, &tmp );
+        TS_ASSERT( parseRet );
+        TS_ASSERT_DIFFERS( str, result );
+        TS_ASSERT_EQUALS( std::string("named"), tmp.colorProfile );
+        TS_ASSERT_EQUALS( 1, tmp.colors.size() );
+
+
+        gchar* badThing = "foodle";
+        result = 0;
+        parseRet = sp_svg_read_icc_color( badThing, &result, &tmp );
+        TS_ASSERT( !parseRet );
+        TS_ASSERT_EQUALS( badThing, result );
+        TS_ASSERT_DIFFERS( std::string("named"), tmp.colorProfile );
+        TS_ASSERT_EQUALS( 0, tmp.colors.size() );
     }
+
 };
 
 /*
index b25178d56192d4a8287ce628842498c634b24e01..51d68717e96d083f1106ad112a9e0bc1e9ca500b 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 
 #include "svg-color.h"
+#include "svg-icc-color.h"
 #include <cassert>
 #include <math.h>
 #include <glib/gmem.h>
@@ -26,6 +27,7 @@
 #include <glib/ghash.h>
 #include <glib/gutils.h>
 #include <cstdio> // sprintf
+#include <errno.h>
 #include "strneq.h"
 using std::sprintf;
 
@@ -439,6 +441,73 @@ sp_svg_create_color_hash()
     return colors;
 }
 
+
+bool sp_svg_read_icc_color( gchar const *str, gchar const **end_ptr, SVGICCColor* dest )
+{
+    bool good = true;
+
+    if ( end_ptr ) {
+        *end_ptr = str;
+    }
+    if ( dest ) {
+        dest->colorProfile.clear();
+        dest->colors.clear();
+    }
+
+    if ( !str ) {
+        // invalid input
+        good = false;
+    } else {
+        while ( g_ascii_isspace(*str) ) {
+            str++;
+        }
+
+        good = strneq( str, "icc-color(", 10 );
+
+        if ( good ) {
+            str += 10;
+
+            while ( *str && !g_ascii_isspace(*str) && *str!= ',' && *str != ')' ) {
+                if ( dest ) {
+                    dest->colorProfile += *str;
+                }
+                str++;
+            }
+            while ( g_ascii_isspace(*str) || *str == ',' ) {
+                str++;
+            }
+        }
+
+        while ( good && *str && *str != ')' ) {
+            if ( g_ascii_isdigit(*str) || *str == '.' ) {
+                gchar* endPtr = 0;
+                gdouble dbl = g_ascii_strtod( str, &endPtr );
+                if ( !errno ) {
+                    if ( dest ) {
+                        dest->colors.push_back( dbl );
+                    }
+                    str = endPtr;
+                } else {
+                    good = false;
+                    break;
+                }
+
+                while ( good && g_ascii_isspace(*str) || *str == ',' ) {
+                    str++;
+                }
+            } else {
+                break;
+            }
+        }
+    }
+
+    if ( end_ptr ) {
+        *end_ptr = str;
+    }
+
+    return good;
+}
+
 /*
   Local Variables:
   mode:c++
index 1c48b87de187015c3316a4d384c21c7dce63f390..692c1dd009c6c8d7c0bb6bffbdac4f0e8108ae72 100644 (file)
@@ -3,9 +3,13 @@
 
 #include <glib/gtypes.h>
 
+class SVGICCColor;
+
 guint32 sp_svg_read_color(gchar const *str, unsigned int dfl);
 guint32 sp_svg_read_color(gchar const *str, gchar const **end_ptr, guint32 def);
 void sp_svg_write_color(char *buf, unsigned int buflen, unsigned int rgba32);
 
+bool sp_svg_read_icc_color( gchar const *str, gchar const **end_ptr, SVGICCColor* dest );
+
 
 #endif /* !SVG_SVG_COLOR_H_SEEN */