Code

Add ubuntu palette, closes 1510556.
[inkscape.git] / src / style.cpp
index 54c3a0c0ca38cd05ed5067cdfd1225ed05319135..d97dac316e031bb725be46e508234855817d27b3 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "svg/svg.h"
 #include "svg/svg-color.h"
+#include "svg/svg-icc-color.h"
 
 #include "display/canvas-bpath.h"
 #include "attributes.h"
@@ -40,6 +41,9 @@
 #include "unit-constants.h"
 #include "isnan.h"
 
+using Inkscape::CSSOStringStream;
+using std::vector;
+
 namespace Inkscape {
 
 /**
@@ -111,6 +115,7 @@ static void sp_style_clear(SPStyle *style);
 static void sp_style_merge_property(SPStyle *style, gint id, gchar const *val);
 
 static void sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent);
+static void sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent);
 static void sp_style_read_dash(SPStyle *style, gchar const *str);
 
 static SPTextStyle *sp_text_style_new(void);
@@ -130,6 +135,7 @@ static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *s
 static void sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
 static void sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document);
 static void sp_style_read_ifontsize(SPIFontSize *val, gchar const *str);
+static void sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document);
 
 static void sp_style_read_penum(SPIEnum *val, Inkscape::XML::Node *repr, gchar const *key, SPStyleEnum const *dict, bool can_explicitly_inherit);
 static void sp_style_read_plength(SPILength *val, Inkscape::XML::Node *repr, gchar const *key);
@@ -148,7 +154,7 @@ static gint sp_style_write_itextdecoration(gchar *p, gint const len, gchar const
 
 static void css2_unescape_unquote(SPIString *val);
 
-static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunref, unsigned unset);
+static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
 
 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
 #define SPS_READ_PENUM_IF_UNSET(v,r,k,d,i) if (!(v)->set) {sp_style_read_penum((v), (r), (k), (d), (i));}
@@ -453,8 +459,8 @@ sp_style_unref(SPStyle *style)
             g_signal_handlers_disconnect_matched(G_OBJECT(style->object),
                                                  G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
         if (style->text) sp_text_style_unref(style->text);
-        sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
-        sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
+        sp_style_paint_clear(style, &style->fill);
+        sp_style_paint_clear(style, &style->stroke);
         g_free(style->stroke_dash.dash);
         g_free(style);
     }
@@ -644,6 +650,14 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
         }
     }
 
+    /* filter effects */
+    if (!style->filter.set) {
+        val = repr->attribute("filter");
+        if (val) {
+            sp_style_read_ifilter(&style->filter, val, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+        }
+    }
+            
     /* 3. Merge from parent */
     if (object) {
         if (object->parent) {
@@ -871,12 +885,14 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
                 sp_style_read_iscale24(&style->opacity, val);
             }
             break;
-            /* Filter */
         case SP_PROP_ENABLE_BACKGROUND:
             g_warning("Unimplemented style property SP_PROP_ENABLE_BACKGROUND: value: %s", val);
             break;
+            /* Filter */
         case SP_PROP_FILTER:
-            g_warning("Unimplemented style property SP_PROP_FILTER: value: %s", val);
+            if (style->filter.set && style->filter.inherit) {
+                sp_style_read_ifilter(&style->filter, val, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+            }
             break;
         case SP_PROP_FLOOD_COLOR:
             g_warning("Unimplemented style property SP_PROP_FLOOD_COLOR: value: %s", val);
@@ -1411,6 +1427,11 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
             style->marker[i].value = g_strdup(parent->marker[i].value);
         }
     }
+
+    /* Filter effects */
+    if(style->filter.set && style->filter.inherit) {
+        sp_style_merge_ifilter(&style->filter, &parent->filter);
+    }
 }
 
 template <typename T>
@@ -1934,13 +1955,13 @@ sp_style_paint_server_release(SPPaintServer *server, SPStyle *style)
     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
         && (server == style->fill.value.paint.server))
     {
-        sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
+        sp_style_paint_clear(style, &style->fill);
     }
 
     if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
         && (server == style->stroke.value.paint.server))
     {
-        sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
+        sp_style_paint_clear(style, &style->stroke);
     }
 }
 
@@ -1988,7 +2009,7 @@ sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *styl
 static void
 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 {
-    sp_style_paint_clear(style, paint, TRUE, FALSE);
+    sp_style_paint_clear(style, paint);
 
     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
         paint->currentcolor = TRUE;
@@ -2038,6 +2059,19 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 }
 
 
+/**
+ * Merge filter style from parent.
+ * Filter effects do not inherit by default
+ */
+static void
+sp_style_merge_ifilter(SPIFilter *child, SPIFilter const *parent)
+{
+    child->set = parent->set;
+    child->inherit = parent->inherit;
+    child->filter = parent->filter;
+    child->uri = parent->uri;
+}
+
 /**
  * Dumps the style to a CSS string, with either SP_STYLE_FLAG_IFSET or
  * SP_STYLE_FLAG_ALWAYS flags. Used with Always for copying an object's
@@ -2302,8 +2336,8 @@ sp_style_clear(SPStyle *style)
 {
     g_return_if_fail(style != NULL);
 
-    sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
-    sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
+    sp_style_paint_clear(style, &style->fill);
+    sp_style_paint_clear(style, &style->stroke);
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
@@ -2899,13 +2933,25 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
             }
         }
     } else {
-        guint32 const rgb0 = sp_svg_read_color(str, 0xff);
+        guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
         if (rgb0 != 0xff) {
             paint->type = SP_PAINT_TYPE_COLOR;
             sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
             paint->set = TRUE;
             paint->inherit = FALSE;
             paint->currentcolor = FALSE;
+
+            while (g_ascii_isspace(*str)) {
+                ++str;
+            }
+            if (strneq(str, "icc-color(", 10)) {
+                SVGICCColor* tmp = new SVGICCColor();
+                if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                    delete tmp;
+                    tmp = 0;
+                }
+                paint->iccColor = tmp;
+            }
         }
     }
 }
@@ -2978,6 +3024,56 @@ sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
 
 
 
+/**
+ * Set SPIFilter object from string.
+ */
+static void
+sp_style_read_ifilter(SPIFilter *f, gchar const *str, SPDocument *document)
+{
+    /* Try all possible values: inherit, none, uri */
+    if (streq(str, "inherit")) {
+        f->set = TRUE;
+        f->inherit = TRUE;
+        f->filter = NULL;
+    } else if(streq(str, "none")) {
+        f->set = TRUE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+    } else if (strneq(str, "url", 3)) {
+        f->uri = extract_uri(str);
+        if(f->uri == NULL || f->uri[0] == '\0') {
+            g_warning("Specified filter url is empty");
+            f->set = TRUE;
+            f->inherit = FALSE;
+            f->filter = NULL;
+            return;
+        }
+        f->set = TRUE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+        if (document) {
+            SPObject *obj;
+            obj = sp_uri_reference_resolve(document, str);
+            if (SP_IS_FILTER(obj)) {
+                f->filter = SP_FILTER(obj);
+                //g_signal_connect(G_OBJECT(f->filter), "release",
+                //                 G_CALLBACK(sp_style_filter_release), style);
+                //g_signal_connect(G_OBJECT(f->filter), "modified",
+                //                 G_CALLBACK(sp_style_filter_modified), style);
+            } else {
+                g_warning("Element '%s' not found or is not a filter", f->uri);
+            }
+        }
+
+    } else {
+        /* We shouldn't reach this if SVG input is well-formed */
+        f->set = FALSE;
+        f->inherit = FALSE;
+        f->filter = NULL;
+        f->uri = NULL;
+    }
+}
+
 /**
  * Set SPIEnum object from repr attribute.
  */
@@ -3325,7 +3421,13 @@ sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
     if (a->type != b->type)
         return true;
     if (a->type == SP_PAINT_TYPE_COLOR)
-        return !sp_color_is_equal(&a->value.color, &b->value.color);
+        return !(sp_color_is_equal(&a->value.color, &b->value.color)
+                 && ((a->iccColor == b->iccColor)
+                     || (a->iccColor && b->iccColor
+                         && (a->iccColor->colorProfile == b->iccColor->colorProfile)
+                         && (a->iccColor->colors == b->iccColor->colors))));
+    /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
+     * in the string representation. */
     if (a->type == SP_PAINT_TYPE_PAINTSERVER)
         return (a->value.paint.server != b->value.paint.server);
     return false;
@@ -3354,7 +3456,19 @@ sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
                 case SP_PAINT_TYPE_COLOR: {
                     char color_buf[8];
                     sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
-                    return g_snprintf(b, len, "%s:%s;", key, color_buf);
+                    if (paint->iccColor) {
+                        CSSOStringStream css;
+                        css << color_buf << " icc-color(" << paint->iccColor->colorProfile;
+                        for (vector<double>::const_iterator i(paint->iccColor->colors.begin()),
+                                 iEnd(paint->iccColor->colors.end());
+                             i != iEnd; ++i) {
+                            css << ", " << *i;
+                        }
+                        css << ')';
+                        return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
+                    } else {
+                        return g_snprintf(b, len, "%s:%s;", key, color_buf);
+                    }
                 }
                 case SP_PAINT_TYPE_PAINTSERVER:
                     return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
@@ -3423,13 +3537,12 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 
 
 /**
- * Clear paint object; conditionally disconnect style from paintserver.
+ * Clear paint object, and disconnect style from paintserver (if present).
  */
 static void
-sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
-                     unsigned hunref, unsigned unset)
+sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
 {
-    if (hunref && (paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
+    if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
         if (paint == &style->fill) {
             if (style->fill_hreffed) {
                 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
@@ -3454,14 +3567,11 @@ sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
         }
 
         paint->value.paint.server = NULL;
-        paint->value.paint.uri = NULL;
-        paint->type = SP_PAINT_TYPE_NONE;
-    }
-
-    if (unset) {
-        paint->set = FALSE;
-        paint->inherit = FALSE;
     }
+    paint->value.paint.uri = NULL;
+    paint->type = SP_PAINT_TYPE_NONE;
+    delete paint->iccColor;
+    paint->iccColor = NULL;
 }
 
 /**