Code

Check to make sure canvas provided to forced redraw functions is not null.
[inkscape.git] / src / style.cpp
index ee67469ade716ac45dc3e9710748d83d23b53aa3..bd880b857cb4760adaa117b8dd3cf59eb4072643 100644 (file)
@@ -23,6 +23,8 @@
 #include "xml/croco-node-iface.h"
 
 #include "svg/svg.h"
+#include "svg/svg-color.h"
+#include "svg/svg-icc-color.h"
 
 #include "display/canvas-bpath.h"
 #include "attributes.h"
 #include "unit-constants.h"
 #include "isnan.h"
 
+#include <sigc++/functors/ptr_fun.h>
+#include <sigc++/adaptors/bind.h>
+
+using Inkscape::CSSOStringStream;
+using std::vector;
+
 namespace Inkscape {
 
 /**
@@ -110,6 +118,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(SPStyle *style, SPIFilter *child, SPIFilter const *parent);
 static void sp_style_read_dash(SPStyle *style, gchar const *str);
 
 static SPTextStyle *sp_text_style_new(void);
@@ -129,6 +138,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, SPStyle *style, 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);
@@ -144,10 +154,13 @@ static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint
 static gint sp_style_write_ifontsize(gchar *p, gint len, gchar const *key, SPIFontSize const *val, SPIFontSize const *base, guint flags);
 static gint sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const *const key, SPILengthOrNormal const *const val, SPILengthOrNormal const *const base, guint const flags);
 static gint sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key, SPITextDecoration const *const val, SPITextDecoration const *const base, guint const flags);
+static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
 
-void css2_unescape_unquote (SPIString *val);
+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);
+
+static void sp_style_filter_clear(SPStyle *style, SPIFilter *filter);
 
 #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));}
@@ -361,6 +374,12 @@ static SPStyleEnum const enum_text_rendering[] = {
     {NULL, -1}
 };
 
+static SPStyleEnum const enum_enable_background[] = {
+    {"accumulate", SP_CSS_BACKGROUND_ACCUMULATE},
+    {"new", SP_CSS_BACKGROUND_NEW},
+    {NULL, -1}
+};
+
 /**
  * Release callback.
  */
@@ -389,8 +408,18 @@ sp_style_new()
     sp_style_clear(style);
 
     style->cloned = false;
-    style->hreffed = false;
-    style->listening = false;
+
+    style->fill_hreffed = false;
+    style->stroke_hreffed = false;
+    style->filter_hreffed = false;
+
+    new (&style->release_connection) sigc::connection();
+
+    new (&style->fill_release_connection) sigc::connection();
+    new (&style->fill_modified_connection) sigc::connection();
+
+    new (&style->stroke_release_connection) sigc::connection();
+    new (&style->stroke_modified_connection) sigc::connection();
 
     return style;
 }
@@ -407,7 +436,7 @@ sp_style_new_from_object(SPObject *object)
 
     SPStyle *style = sp_style_new();
     style->object = object;
-    g_signal_connect(G_OBJECT(object), "release", G_CALLBACK(sp_style_object_release), style);
+    style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
 
     if (object && SP_OBJECT_IS_CLONED(object)) {
         style->cloned = true;
@@ -444,12 +473,20 @@ sp_style_unref(SPStyle *style)
     style->refcount -= 1;
 
     if (style->refcount < 1) {
-        if (style->object)
-            g_signal_handlers_disconnect_matched(G_OBJECT(style->object),
-                                                 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
+        style->release_connection.disconnect();
+        style->release_connection.~connection();
         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);
+        sp_style_filter_clear(style, &style->filter);
+        style->fill_release_connection.disconnect();
+        style->fill_release_connection.~connection();
+        style->fill_modified_connection.disconnect();
+        style->fill_modified_connection.~connection();
+        style->stroke_release_connection.disconnect();
+        style->stroke_release_connection.~connection();
+        style->stroke_modified_connection.disconnect();
+        style->stroke_modified_connection.~connection();
         g_free(style->stroke_dash.dash);
         g_free(style);
     }
@@ -639,6 +676,16 @@ 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, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+        }
+    }
+    SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
+                            "enable-background", enum_enable_background, true);
+            
     /* 3. Merge from parent */
     if (object) {
         if (object->parent) {
@@ -725,7 +772,7 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             if (!style->text_private) sp_style_privatize_text(style);
             if (!style->text->font_family.set) {
                 sp_style_read_istring(&style->text->font_family, val);
-                css2_unescape_unquote (&style->text->font_family);
+                css2_unescape_unquote(&style->text->font_family);
             }
             break;
         case SP_PROP_FONT_SIZE:
@@ -866,12 +913,15 @@ 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);
+            SPS_READ_IENUM_IF_UNSET(&style->enable_background, val,
+                                    enum_enable_background, true);
             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, (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);
@@ -1239,15 +1289,15 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         // expressible in the current font family, but that's difficult to
         // find out, so jumping by 3 seems an appropriate approximation
         style->font_weight.computed = (parent_val <= SP_CSS_FONT_WEIGHT_100 + 3
-                           ? (unsigned)SP_CSS_FONT_WEIGHT_100
-                           : parent_val - 3);
+                                       ? (unsigned)SP_CSS_FONT_WEIGHT_100
+                                       : parent_val - 3);
         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_BOLDER) {
         unsigned const parent_val = parent->font_weight.computed;
         g_assert(parent_val <= SP_CSS_FONT_WEIGHT_900);
         style->font_weight.computed = (parent_val >= SP_CSS_FONT_WEIGHT_900 - 3
-                           ? (unsigned)SP_CSS_FONT_WEIGHT_900
-                           : parent_val + 3);
+                                       ? (unsigned)SP_CSS_FONT_WEIGHT_900
+                                       : parent_val + 3);
         g_assert(style->font_weight.computed <= (unsigned) SP_CSS_FONT_WEIGHT_900);
     }
 
@@ -1257,15 +1307,15 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_NARROWER) {
         unsigned const parent_val = parent->font_stretch.computed;
         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_CONDENSED
-                        ? parent_val
-                        : parent_val - 1);
+                                        ? parent_val
+                                        : parent_val - 1);
         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
     } else if (style->font_stretch.value == SP_CSS_FONT_STRETCH_WIDER) {
         unsigned const parent_val = parent->font_stretch.computed;
         g_assert(parent_val <= SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
         style->font_stretch.computed = (parent_val == SP_CSS_FONT_STRETCH_ULTRA_EXPANDED
-                        ? parent_val
-                        : parent_val + 1);
+                                        ? parent_val
+                                        : parent_val + 1);
         g_assert(style->font_stretch.computed <= (unsigned) SP_CSS_FONT_STRETCH_ULTRA_EXPANDED);
     }
 
@@ -1406,6 +1456,15 @@ 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, &style->filter, &parent->filter);
+    }
+
+    if(style->enable_background.inherit) {
+        style->enable_background.value = parent->enable_background.value;
+    }
 }
 
 template <typename T>
@@ -1847,7 +1906,7 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
 
         /* display is in principle similar to opacity, but implementation is easier. */
         if ( parent->display.set && !parent->display.inherit
-                    && parent->display.value == SP_CSS_DISPLAY_NONE ) {
+             && parent->display.value == SP_CSS_DISPLAY_NONE ) {
             style->display.value = SP_CSS_DISPLAY_NONE;
             style->display.set = true;
             style->display.inherit = false;
@@ -1859,6 +1918,18 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
             /* Leave as is.  (display doesn't inherit by default.) */
         }
 
+        /* enable-background - this is rather complicated, because
+         * it is valid only when applied to container elements.
+         * Let's check a simple case anyhow. */
+        if (parent->enable_background.set
+            && !parent->enable_background.inherit
+            && style->enable_background.inherit)
+        {
+            style->enable_background.set = true;
+            style->enable_background.inherit = false;
+            style->enable_background.value = parent->enable_background.value;
+        }
+
         /** \todo
          * fixme: Check that we correctly handle all properties that don't
          * inherit by default (as shown in
@@ -1916,6 +1987,8 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
          * represent it as a normal SPILength; though will need to do something about existing
          * users of stroke_dash.offset and stroke_dashoffset_set. */
     }
+
+    /* TODO: deal with filters */
 }
 
 
@@ -1924,18 +1997,19 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
  * Disconnects from possible fill and stroke paint servers.
  */
 static void
-sp_style_paint_server_release(SPPaintServer *server, SPStyle *style)
+sp_style_paint_server_release(SPObject *obj, SPStyle *style)
 {
+    SPPaintServer *server=static_cast<SPPaintServer *>(obj);
     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);
     }
 }
 
@@ -1947,8 +2021,9 @@ sp_style_paint_server_release(SPPaintServer *server, SPStyle *style)
  * or stroke paint server.
  */
 static void
-sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *style)
+sp_style_paint_server_modified(SPObject *obj, guint flags, SPStyle *style)
 {
+    SPPaintServer *server = static_cast<SPPaintServer *>(obj);
     if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
         && (server == style->fill.value.paint.server))
     {
@@ -1983,7 +2058,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;
@@ -2002,15 +2077,28 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
             paint->value.paint.uri = parent->value.paint.uri;
             if (paint->value.paint.server) {
                 if (style->object && !style->cloned) { // href paintserver for style of non-clones only
-                    style->hreffed = true;
                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
+                    if (paint == &style->fill) {
+                        style->fill_hreffed = true;
+                    } else {
+                        assert(paint == &style->stroke);
+                        style->stroke_hreffed = true;
+                    }
                 }
                 if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
-                                     G_CALLBACK(sp_style_paint_server_release), style);
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
-                                     G_CALLBACK(sp_style_paint_server_modified), style);
-                    style->listening = true;
+                    SPObject *server = paint->value.paint.server;
+                    sigc::connection release_connection
+                      = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
+                    sigc::connection modified_connection
+                      = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
+                    if (paint == &style->fill) {
+                        style->fill_release_connection = release_connection;
+                        style->fill_modified_connection = modified_connection;
+                    } else {
+                        assert(paint == &style->stroke);
+                        style->stroke_release_connection = release_connection;
+                        style->stroke_modified_connection = modified_connection;
+                    }
                 }
             }
             break;
@@ -2023,6 +2111,22 @@ 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(SPStyle *style, SPIFilter *child, SPIFilter const *parent)
+{
+    sp_style_filter_clear(style, child);
+    child->set = parent->set;
+    child->inherit = parent->inherit;
+    child->filter = parent->filter;
+    child->uri = parent->uri;
+    sp_object_href(SP_OBJECT(child), style);
+    style->filter_hreffed = true;
+}
+
 /**
  * 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
@@ -2144,6 +2248,11 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &style->display, NULL, flags);
     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &style->overflow, NULL, flags);
 
+    /* filter: */
+    p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &style->filter, NULL, flags);
+
+    p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &style->enable_background, NULL, flags);
+
     /* fixme: */
     p += sp_text_style_write(p, c + BMAX - p, style->text, flags);
 
@@ -2258,6 +2367,11 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
     p += sp_style_write_ienum(p, c + BMAX - p, "display", enum_display, &from->display, &to->display, SP_STYLE_FLAG_IFSET);
     p += sp_style_write_ienum(p, c + BMAX - p, "overflow", enum_overflow, &from->overflow, &to->overflow, SP_STYLE_FLAG_IFSET);
 
+    /* filter: */
+    p += sp_style_write_ifilter(p, c + BMAX - p, "filter", &from->filter, &to->filter, SP_STYLE_FLAG_IFDIFF);
+
+    p += sp_style_write_ienum(p, c + BMAX - p, "enable-background", enum_enable_background, &from->enable_background, &to->enable_background, SP_STYLE_FLAG_IFSET);
+
     p += sp_text_style_write(p, c + BMAX - p, from->text, SP_STYLE_FLAG_IFDIFF);
 
     /** \todo
@@ -2287,8 +2401,9 @@ 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);
+    sp_style_filter_clear(style, &style->filter);
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
@@ -2408,6 +2523,16 @@ sp_style_clear(SPStyle *style)
         g_free(style->marker[i].value);
         style->marker[i].set = FALSE;
     }
+
+    style->filter.set = FALSE;
+    //Are these really needed?
+    style->filter.inherit = FALSE;
+    style->filter.uri = NULL;
+    style->filter.filter = FALSE;
+
+    style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
+    style->enable_background.set = false;
+    style->enable_background.inherit = false;
 }
 
 
@@ -2819,6 +2944,8 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
 
 /**
  * Set SPIPaint object from string.
+ *
+ * \pre paint == \&style.fill || paint == \&style.stroke.
  */
 static void
 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
@@ -2831,16 +2958,16 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
         paint->set = TRUE;
         paint->inherit = TRUE;
         paint->currentcolor = FALSE;
-    } else if (streq(str, "currentColor")) {
+    } else if (streq(str, "currentColor") && paint != &style->color) {
         paint->set = TRUE;
         paint->inherit = FALSE;
         paint->currentcolor = TRUE;
-    } else if (streq(str, "none")) {
+    } else if (streq(str, "none") && paint != &style->color) {
         paint->type = SP_PAINT_TYPE_NONE;
         paint->set = TRUE;
         paint->inherit = FALSE;
         paint->currentcolor = FALSE;
-    } else if (strneq(str, "url", 3)) {
+    } else if (strneq(str, "url", 3) && paint != &style->color) {
         // this is alloc'd uri, but seems to be shared with a parent
         // potentially, so it's not any easy g_free case...
         paint->value.paint.uri = extract_uri(str);
@@ -2858,27 +2985,52 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                 paint->value.paint.server = SP_PAINT_SERVER(ps);
                 if (style->object && !style->cloned) {
                     sp_object_href(SP_OBJECT(paint->value.paint.server), style);
-                    style->hreffed = true;
+                    if (paint == &style->fill) {
+                        style->fill_hreffed = true;
+                    } else {
+                        assert(paint == &style->stroke);
+                        style->stroke_hreffed = true;
+                    }
                 }
                 if (style->object || style->cloned) {
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
-                                     G_CALLBACK(sp_style_paint_server_release), style);
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
-                                     G_CALLBACK(sp_style_paint_server_modified), style);
-                    style->listening = true;
+                    SPObject *server = paint->value.paint.server;
+                    sigc::connection release_connection
+                      = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
+                    sigc::connection modified_connection
+                      = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
+                    if (paint == &style->fill) {
+                        style->fill_release_connection = release_connection;
+                        style->fill_modified_connection = modified_connection;
+                    } else {
+                        assert(paint == &style->stroke);
+                        style->stroke_release_connection = release_connection;
+                        style->stroke_modified_connection = modified_connection;
+                    }
                 }
             } else {
                 paint->value.paint.server = NULL;
             }
         }
     } 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;
+            }
         }
     }
 }
@@ -2951,6 +3103,58 @@ 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, SPStyle * style, 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);
+                sp_object_href(SP_OBJECT(f->filter), style);  
+                style->filter_hreffed = true;
+                //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.
  */
@@ -3298,7 +3502,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;
@@ -3324,14 +3534,27 @@ sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
             return g_snprintf(b, len, "%s:currentColor;", key);
         } else {
             switch (paint->type) {
-            case SP_PAINT_TYPE_COLOR:
-                return g_snprintf(b, len, "%s:#%06x;", key, sp_color_get_rgba32_falpha(&paint->value.color, 0.0) >> 8);
-                break;
-            case SP_PAINT_TYPE_PAINTSERVER:
+                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));
+                    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);
-                break;
-            default:
-                break;
+                default:
+                    break;
             }
             return g_snprintf(b, len, "%s:none;", key);
         }
@@ -3395,33 +3618,81 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 
 
 /**
- * Clear paint object; conditionally disconnect style from paintserver.
+ * Write SPIFilter object into string.
  */
-static void
-sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
-                     unsigned hunref, unsigned unset)
+static gint
+sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
+                         SPIFilter const *const val, SPIFilter const *const base,
+                         guint const flags)
 {
-    if (hunref && (paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
-        if (style->hreffed) {
-            sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
-            style->hreffed = false;
+    if ((flags & SP_STYLE_FLAG_ALWAYS)
+        || ((flags & SP_STYLE_FLAG_IFSET) && val->set)
+        || ((flags & SP_STYLE_FLAG_IFDIFF) && val->set))
+    {
+        if (val->inherit) {
+            return g_snprintf(p, len, "%s:inherit;", key);
+        } else if (val->uri) {
+            return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
         }
-        if (style->listening) {
-            g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
-                                                 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
-            style->listening = false;
+    }
+
+
+    return 0;
+}
+
+
+/**
+ * Clear paint object, and disconnect style from paintserver (if present).
+ */
+static void
+sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
+{
+    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);
+                style->fill_hreffed = false;
+            }
+            style->fill_release_connection.disconnect();
+            style->fill_modified_connection.disconnect();
+        } else {
+            assert(paint == &style->stroke);  // Only fill & stroke can have a paint server.
+            if (style->stroke_hreffed) {
+                sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
+                style->stroke_hreffed = false;
+            }
+            style->stroke_release_connection.disconnect();
+            style->stroke_modified_connection.disconnect();
         }
+
         paint->value.paint.server = NULL;
-        paint->value.paint.uri = NULL;
-        paint->type = SP_PAINT_TYPE_NONE;
     }
+    paint->value.paint.uri = NULL;
+    paint->type = SP_PAINT_TYPE_NONE;
+    delete paint->iccColor;
+    paint->iccColor = NULL;
+}
 
-    if (unset) {
-        paint->set = FALSE;
-        paint->inherit = FALSE;
+
+/**
+ * Clear filter object, and disconnect style from paintserver (if present).
+ */
+static void
+sp_style_filter_clear(SPStyle *style, SPIFilter *f)
+{
+
+    if (style->filter_hreffed) {
+        sp_object_hunref(SP_OBJECT(f->filter), style);
+        style->filter_hreffed = false;
     }
+    //style->fill_release_connection.disconnect();
+    //style->fill_modified_connection.disconnect();
+
+    f->filter = NULL;
+    
 }
 
+
 /**
  * Clear all style property attributes in object.
  */
@@ -3493,6 +3764,12 @@ sp_style_unset_property_attrs(SPObject *o)
     if (style->writing_mode.set) {
         repr->setAttribute("writing_mode", NULL);
     }
+    if (style->filter.set) {
+        repr->setAttribute("filter", NULL);
+    }
+    if (style->enable_background.set) {
+        repr->setAttribute("enable-background", NULL);
+    }
 }
 
 /**
@@ -3527,7 +3804,7 @@ sp_css_attr_from_object(SPObject *object, guint const flags)
     SPStyle const *const style = SP_OBJECT_STYLE(object);
     if (style == NULL)
         return NULL;
-    return sp_css_attr_from_style (style, flags);
+    return sp_css_attr_from_style(style, flags);
 }
 
 /**
@@ -3682,18 +3959,18 @@ sp_css_attr_scale(SPCSSAttr *css, double ex)
  * ALL strings (check CSS spec), in which case this should be part of
  * read_istring.
  */
-void
-css2_unescape_unquote (SPIString *val)
+static void
+css2_unescape_unquote(SPIString *val)
 {
     if (val->set && val->value && strlen(val->value) >= 2) {
 
-       /// \todo unescape all \-escaped chars
+        /// \todo unescape all \-escaped chars
 
         int l = strlen(val->value);
-        if ((val->value[0] == '"' && val->value[l - 1] == '"') ||
-            (val->value[0] == '\'' && val->value[l - 1] == '\'')) {
-                memcpy (val->value, val->value+1, l - 2);
-                val->value[l - 2] = '\0';
+        if ( ( val->value[0] == '"' && val->value[l - 1] == '"' )  ||
+             ( val->value[0] == '\'' && val->value[l - 1] == '\'' )  ) {
+            memcpy(val->value, val->value + 1, l - 2);
+            val->value[l - 2] = '\0';
         }
     }
 }