summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJon A. Cruz <jon@joncruz.org>2006-04-05 03:09:15 +0000
committerjoncruz <joncruz@users.sourceforge.net>2006-04-05 03:09:15 +0000
commit0dc3be291679ef96980e8dbc75d4332419695cba (patch)
treea64571e8313f1c6ae6227b98502792043cb4427c /src
parentUpdated cases for attributes added in <color-profile> support (diff)
downloadinkscape-0dc3be291679ef96980e8dbc75d4332419695cba.tar.gz
inkscape-0dc3be291679ef96980e8dbc75d4332419695cba.zip
Moving out icc parser
(bzr r424)
Diffstat (limited to 'src')
-rw-r--r--src/style.cpp34
-rw-r--r--src/svg/svg-color-test.h90
-rw-r--r--src/svg/svg-color.cpp69
-rw-r--r--src/svg/svg-color.h4
4 files changed, 135 insertions, 62 deletions
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 <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;
}
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 <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() );
}
+
};
/*
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 <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++
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 <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 */