From: joncruz Date: Wed, 5 Apr 2006 03:09:15 +0000 (+0000) Subject: Moving out icc parser X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ad365935db012f6bcb43bb897dc95903dc0c455f;p=inkscape.git Moving out icc parser --- diff --git a/src/style.cpp b/src/style.cpp index 230d8f466..2a0a30bd1 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -40,7 +40,7 @@ #include "xml/repr.h" #include "unit-constants.h" #include "isnan.h" -#include + 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; } diff --git a/src/svg/svg-color-test.h b/src/svg/svg-color-test.h index 4d9b2e854..ac79ac959 100644 --- a/src/svg/svg-color-test.h +++ b/src/svg/svg-color-test.h @@ -1,46 +1,72 @@ #include #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() ); } + }; /* diff --git a/src/svg/svg-color.cpp b/src/svg/svg-color.cpp index b25178d56..51d68717e 100644 --- a/src/svg/svg-color.cpp +++ b/src/svg/svg-color.cpp @@ -18,6 +18,7 @@ #endif #include "svg-color.h" +#include "svg-icc-color.h" #include #include #include @@ -26,6 +27,7 @@ #include #include #include // sprintf +#include #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++ diff --git a/src/svg/svg-color.h b/src/svg/svg-color.h index 1c48b87de..692c1dd00 100644 --- a/src/svg/svg-color.h +++ b/src/svg/svg-color.h @@ -3,9 +3,13 @@ #include +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 */