Code

Filter effects dialog:
[inkscape.git] / src / style.cpp
index 9dd9503614f663e7172c7e41ec63459673ee3c0c..87151c5a847feb38dd29251140c03aa88e37f460 100644 (file)
 #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 "document.h"
 #include "extract-uri.h"
-#include "marker-status.h"
 #include "uri-references.h"
+#include "uri.h"
 #include "sp-paint-server.h"
 #include "streq.h"
 #include "strneq.h"
 #include "xml/repr.h"
 #include "unit-constants.h"
 #include "isnan.h"
+#include "macros.h"
 
-namespace Inkscape {
+#include "sp-filter-reference.h"
 
-/**
- * Parses a CSS url() specification; temporary hack until
- * style stuff is redone.
- * \param string the CSS string to parse
- * \return a newly-allocated URL string (or NULL); free with g_free()
- */
-gchar *parse_css_url(gchar const *string) {
-    if (!string)
-        return NULL;
-
-    gchar const *iter = string;
-    for ( ; g_ascii_isspace(*iter) ; iter = g_utf8_next_char(iter) );
-    if (strncmp(iter, "url(", 4))
-        return NULL;
-    iter += 4;
-
-    gchar const end_char = *iter;
-    if ( *iter == '"' || *iter == '\'' ) {
-        iter += 1;
-    }
-
-    GString *temp = g_string_new(NULL);
-    for ( ; *iter ; iter = g_utf8_next_char(iter) ) {
-        if ( *iter == '(' || *iter == ')'  ||
-             *iter == '"' || *iter == '\'' ||
-             g_ascii_isspace(*iter)        ||
-             g_ascii_iscntrl(*iter)           )
-        {
-            break;
-        }
-        if ( *iter == '\\' ) {
-            iter = g_utf8_next_char(iter);
-        }
-        if ( *iter & (gchar)0x80 ) {
-            break;
-        } else {
-            g_string_append_c(temp, *iter);
-        }
-    }
-
-    if ( *iter == end_char && end_char != ')' ) {
-        iter = g_utf8_next_char(iter);
-    }
-    gchar *result;
-    if ( *iter == ')' ) {
-        result = temp->str;
-        g_string_free(temp, FALSE);
-    } else {
-        result = NULL;
-        g_string_free(temp, TRUE);
-    }
+#include <sigc++/functors/ptr_fun.h>
+#include <sigc++/adaptors/bind.h>
 
-    return result;
-}
-
-}
+using Inkscape::CSSOStringStream;
+using std::vector;
 
 #define BMAX 8192
 
@@ -110,6 +62,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 const *parent);
 static void sp_style_read_dash(SPStyle *style, gchar const *str);
 
 static SPTextStyle *sp_text_style_new(void);
@@ -129,6 +82,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(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 +98,11 @@ 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 sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
 
-static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunref, unsigned unset);
+static void sp_style_filter_clear(SPStyle *style);
 
 #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 +316,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.
  */
@@ -370,26 +331,130 @@ sp_style_object_release(SPObject *object, SPStyle *style)
     style->object = NULL;
 }
 
+/**
+ * Emit style modified signal on style's object if the filter changed.
+ */
+static void
+sp_style_filter_ref_modified(SPObject *obj, guint flags, SPStyle *style)
+{
+    SPFilter *filter=static_cast<SPFilter *>(obj);
+    if (style->getFilter() == filter)
+    {
+        if (style->object) {
+            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    }
+}
 
+/**
+ * Gets called when the filter is (re)attached to the style
+ */
+static void
+sp_style_filter_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
+{
+    if (old_ref) {
+        style->filter_modified_connection.disconnect();
+    }
+    if ( SP_IS_FILTER(ref))
+    {
+        style->filter_modified_connection = 
+           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_filter_ref_modified), style));
+    }
 
+    sp_style_filter_ref_modified(ref, 0, style);
+}
+
+/**
+ * Emit style modified signal on style's object if server is style's fill
+ * or stroke paint server.
+ */
+static void
+sp_style_paint_server_ref_modified(SPObject *obj, guint flags, SPStyle *style)
+{
+    SPPaintServer *server = static_cast<SPPaintServer *>(obj);
+
+    if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
+        && style->getFillPaintServer() == server)
+    {
+        if (style->object) {
+            /** \todo
+             * fixme: I do not know, whether it is optimal - we are
+             * forcing reread of everything (Lauris)
+             */
+            /** \todo
+             * fixme: We have to use object_modified flag, because parent
+             * flag is only available downstreams.
+             */
+            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
+        && style->getStrokePaintServer() == server)
+    {
+        if (style->object) {
+            /// \todo fixme:
+            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    } else {
+        g_assert_not_reached();
+    }
+}
+
+/**
+ * Gets called when the paintserver is (re)attached to the style
+ */
+static void
+sp_style_fill_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
+{
+    if (old_ref) {
+        style->fill_ps_modified_connection.disconnect();
+    }
+    if (SP_IS_PAINT_SERVER(ref)) {
+        style->fill_ps_modified_connection = 
+           ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
+    }
+
+    sp_style_paint_server_ref_modified(ref, 0, style);
+}
+
+/**
+ * Gets called when the paintserver is (re)attached to the style
+ */
+static void
+sp_style_stroke_paint_server_ref_changed(SPObject *old_ref, SPObject *ref, SPStyle *style)
+{
+    if (old_ref) {
+        style->stroke_ps_modified_connection.disconnect();
+    }
+    if (SP_IS_PAINT_SERVER(ref)) {
+        style->stroke_ps_modified_connection = 
+          ref->connectModified(sigc::bind(sigc::ptr_fun(&sp_style_paint_server_ref_modified), style));
+    }
+
+    sp_style_paint_server_ref_modified(ref, 0, style);
+}
 
 /**
  * Returns a new SPStyle object with settings as per sp_style_clear().
  */
 SPStyle *
-sp_style_new()
+sp_style_new(SPDocument *document)
 {
     SPStyle *const style = g_new0(SPStyle, 1);
 
     style->refcount = 1;
     style->object = NULL;
+    style->document = document;
     style->text = sp_text_style_new();
     style->text_private = TRUE;
 
     sp_style_clear(style);
 
     style->cloned = false;
-    style->hreffed = false;
+
+    new (&style->release_connection) sigc::connection();
+    new (&style->filter_modified_connection) sigc::connection();
+    new (&style->fill_ps_modified_connection) sigc::connection();
+    new (&style->stroke_ps_modified_connection) sigc::connection();
 
     return style;
 }
@@ -404,9 +469,9 @@ sp_style_new_from_object(SPObject *object)
     g_return_val_if_fail(object != NULL, NULL);
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
 
-    SPStyle *style = sp_style_new();
+    SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
     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;
@@ -443,12 +508,34 @@ 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);
+
+         if (style->fill.value.href) {
+             style->fill_ps_modified_connection.disconnect();
+             delete style->fill.value.href;
+             style->fill.value.href = NULL;
+         }
+         if (style->stroke.value.href) {
+             style->stroke_ps_modified_connection.disconnect();
+             delete style->stroke.value.href;
+             style->stroke.value.href = NULL;
+         }
+         if (style->filter.href) {
+             style->filter_modified_connection.disconnect();
+             delete style->filter.href;
+             style->filter.href = NULL;
+         }
+
+        style->filter_modified_connection.~connection();
+        style->fill_ps_modified_connection.~connection();
+        style->stroke_ps_modified_connection.~connection();
+
+        sp_style_paint_clear(style, &style->fill);
+        sp_style_paint_clear(style, &style->stroke);
+        sp_style_filter_clear(style);
+
         g_free(style->stroke_dash.dash);
         g_free(style);
     }
@@ -472,7 +559,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
         style->cloned = true;
     }
 
-    /* 1. Style itself */
+    /* 1. Style attribute */
     gchar const *val = repr->attribute("style");
     if (val != NULL) {
         sp_style_merge_from_style_string(style, val);
@@ -633,11 +720,22 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
         val = repr->attribute("font-family");
         if (val) {
             if (!style->text_private) sp_style_privatize_text(style);
-            sp_style_read_istring(&style->text->font_family, val);
-            css2_unescape_unquote(&style->text->font_family);
+            gchar *val_unquoted = attribute_unquote(val);
+            sp_style_read_istring(&style->text->font_family, val_unquoted);
+            if (val_unquoted) g_free (val_unquoted);
         }
     }
 
+    /* filter effects */
+    if (!style->filter.set) {
+        val = repr->attribute("filter");
+        if (val) {
+                     sp_style_read_ifilter(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) {
@@ -646,7 +744,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     } else {
         if (sp_repr_parent(repr)) {
             /// \todo fixme: This is not the prettiest thing (Lauris)
-            SPStyle *parent = sp_style_new();
+            SPStyle *parent = sp_style_new(NULL);
             sp_style_read(parent, NULL, sp_repr_parent(repr));
             sp_style_merge_from_parent(style, parent);
             sp_style_unref(parent);
@@ -657,7 +755,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
 
 /**
  * Read style properties from object's repr.
- * 
+ *
  * 1. Reset existing object style
  * 2. Load current effective object style
  * 3. Load i attributes from immediate parent (which has to be up-to-date)
@@ -691,7 +789,7 @@ sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
 
 
 /**
- * 
+ *
  */
 static void
 sp_style_privatize_text(SPStyle *style)
@@ -705,7 +803,7 @@ sp_style_privatize_text(SPStyle *style)
 
 /**
  * Merge property into style.
- * 
+ *
  * Should be called in order of highest to lowest precedence.
  * E.g. for a single style string, call from the last declaration to the first,
  * as CSS says that later declarations override earlier ones.
@@ -723,8 +821,9 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         case SP_PROP_FONT_FAMILY:
             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);
+                gchar *val_unquoted = attribute_unquote(val);
+                sp_style_read_istring(&style->text->font_family, val_unquoted);
+                if (val_unquoted) g_free (val_unquoted);
             }
             break;
         case SP_PROP_FONT_SIZE:
@@ -840,8 +939,8 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             SPS_READ_IENUM_IF_UNSET(&style->display, val, enum_display, true);
             break;
         case SP_PROP_OVERFLOW:
-            /** \todo 
-             * FIXME: not supported properly yet, we just read and write it, 
+            /** \todo
+             * FIXME: not supported properly yet, we just read and write it,
              * but act as if it is always "display".
              */
             SPS_READ_IENUM_IF_UNSET(&style->overflow, val, enum_overflow, true);
@@ -865,12 +964,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(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);
@@ -932,7 +1034,6 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         case SP_PROP_MARKER:
             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
             /* style->marker[SP_MARKER_LOC] = g_quark_from_string(val); */
-            marker_status("Setting SP_PROP_MARKER");
             if (!style->marker[SP_MARKER_LOC].set) {
                 g_free(style->marker[SP_MARKER_LOC].value);
                 style->marker[SP_MARKER_LOC].value = g_strdup(val);
@@ -943,7 +1044,6 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
 
         case SP_PROP_MARKER_START:
             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
-            marker_status("Setting SP_PROP_MARKER_START");
             if (!style->marker[SP_MARKER_LOC_START].set) {
                 g_free(style->marker[SP_MARKER_LOC_START].value);
                 style->marker[SP_MARKER_LOC_START].value = g_strdup(val);
@@ -953,7 +1053,6 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             break;
         case SP_PROP_MARKER_MID:
             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
-            marker_status("Setting SP_PROP_MARKER_MID");
             if (!style->marker[SP_MARKER_LOC_MID].set) {
                 g_free(style->marker[SP_MARKER_LOC_MID].value);
                 style->marker[SP_MARKER_LOC_MID].value = g_strdup(val);
@@ -963,7 +1062,6 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             break;
         case SP_PROP_MARKER_END:
             /* TODO:  Call sp_uri_reference_resolve(SPDocument *document, guchar const *uri) */
-            marker_status("Setting SP_PROP_MARKER_END");
             if (!style->marker[SP_MARKER_LOC_END].set) {
                 g_free(style->marker[SP_MARKER_LOC_END].value);
                 style->marker[SP_MARKER_LOC_END].value = g_strdup(val);
@@ -1032,15 +1130,15 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
 static void
 sp_style_merge_style_from_decl(SPStyle *const style, CRDeclaration const *const decl)
 {
-    /** \todo Ensure that property is lcased, as per 
+    /** \todo Ensure that property is lcased, as per
      * http://www.w3.org/TR/REC-CSS2/syndata.html#q4.
      * Should probably be done in libcroco.
      */
     unsigned const prop_idx = sp_attribute_lookup(decl->property->stryng->str);
     if (prop_idx != SP_ATTR_INVALID) {
-        /** \todo 
-         * effic: Test whether the property is already set before trying to 
-         * convert to string. Alternatively, set from CRTerm directly rather 
+        /** \todo
+         * effic: Test whether the property is already set before trying to
+         * convert to string. Alternatively, set from CRTerm directly rather
          * than converting to string.
          */
         guchar *const str_value_unsigned = cr_term_to_string(decl->value);
@@ -1075,6 +1173,9 @@ sp_style_merge_from_props(SPStyle *const style, CRPropList *const props)
 static void
 sp_style_merge_from_decl_list(SPStyle *const style, CRDeclaration const *const decl_list)
 {
+    // read the decls from end to start, using head recursion, so that latter declarations override
+    // (Ref: http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order point 4.)
+    // because sp_style_merge_style_from_decl only sets properties that are unset
     if (decl_list->next) {
         sp_style_merge_from_decl_list(style, decl_list->next);
     }
@@ -1091,12 +1192,12 @@ sp_repr_sel_eng()
      * Check whether we need to register any pseudo-class handlers.
      * libcroco has its own default handlers for first-child and lang.
      *
-     * We probably want handlers for link and arguably visited (though 
-     * inkscape can't visit links at the time of writing).  hover etc. 
+     * We probably want handlers for link and arguably visited (though
+     * inkscape can't visit links at the time of writing).  hover etc.
      * more useful in inkview than the editor inkscape.
      *
-     * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that 
-     * the following should be honoured, at least by inkview: 
+     * http://www.w3.org/TR/SVG11/styling.html#StylingWithCSS says that
+     * the following should be honoured, at least by inkview:
      * :hover, :active, :focus, :visited, :link.
      */
 
@@ -1132,13 +1233,13 @@ void
 sp_style_merge_from_style_string(SPStyle *const style, gchar const *const p)
 {
     /*
-     * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute: 
-     * ``When CSS styling is used, CSS inline style is specified by including 
-     * semicolon-separated property declarations of the form "name : value" 
+     * Reference: http://www.w3.org/TR/SVG11/styling.html#StyleAttribute:
+     * ``When CSS styling is used, CSS inline style is specified by including
+     * semicolon-separated property declarations of the form "name : value"
      * within the style attribute''.
      *
-     * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?  
-     * Why does it say "including", what else is allowed in the style 
+     * That's fairly ambiguous.  Is a `value' allowed to contain semicolons?
+     * Why does it say "including", what else is allowed in the style
      * attribute value?
      */
 
@@ -1162,7 +1263,7 @@ sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &pare
         child.computed = parent.computed;
     } else if (child.type == SP_FONT_SIZE_LITERAL) {
         /** \todo
-         * fixme: SVG and CSS do not specify clearly, whether we should use 
+         * fixme: SVG and CSS do not specify clearly, whether we should use
          * user or screen coordinates (Lauris)
          */
         if (child.value < SP_CSS_FONT_SIZE_SMALLER) {
@@ -1198,9 +1299,9 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
     g_return_if_fail(style != NULL);
 
     /** \todo
-     * fixme: Check for existing callers that might pass null parent.  
-     * This should probably be g_return_if_fail, or else we should make a 
-     * best attempt to set computed values correctly without having a parent 
+     * fixme: Check for existing callers that might pass null parent.
+     * This should probably be g_return_if_fail, or else we should make a
+     * best attempt to set computed values correctly without having a parent
      * (i.e., by assuming parent has initial values).
      */
     if (!parent)
@@ -1225,7 +1326,7 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         style->font_weight.computed = parent->font_weight.computed;
     } else if (style->font_weight.value == SP_CSS_FONT_WEIGHT_NORMAL) {
         /** \todo
-         * fixme: This is unconditional, i.e., happens even if parent not 
+         * fixme: This is unconditional, i.e., happens even if parent not
          * present.
          */
         style->font_weight.computed = SP_CSS_FONT_WEIGHT_400;
@@ -1238,15 +1339,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);
     }
 
@@ -1256,15 +1357,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);
     }
 
@@ -1373,7 +1474,7 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
 
     if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
         /** \todo
-         * This code looks wrong.  Why does the logic differ from the 
+         * This code looks wrong.  Why does the logic differ from the
          * above properties? Similarly dashoffset below.
          */
         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
@@ -1405,6 +1506,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.inherit) {
+        sp_style_merge_ifilter(style, &parent->filter);
+    }
+
+    if(style->enable_background.inherit) {
+        style->enable_background.value = parent->enable_background.value;
+    }
 }
 
 template <typename T>
@@ -1439,7 +1549,7 @@ sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
                                             SPIPaint &child, SPIPaint const &parent)
 {
     /** \todo
-     * I haven't given this much attention.  See comments below about 
+     * I haven't given this much attention.  See comments below about
      * currentColor, colorProfile, and relative URIs.
      */
     if (!child.set || child.inherit || child.currentcolor) {
@@ -1475,10 +1585,10 @@ sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &pa
         {
             child.set = false;
             /*
-             * Note that this can result in a change in computed value in the 
-             * rare case that the parent's setting was a no-op (i.e. if the 
-             * parent's parent's computed value was already ultra-condensed or 
-             * ultra-expanded).  However, I'd guess that the change is for the 
+             * Note that this can result in a change in computed value in the
+             * rare case that the parent's setting was a no-op (i.e. if the
+             * parent's parent's computed value was already ultra-condensed or
+             * ultra-expanded).  However, I'd guess that the change is for the
              * better: I'd guess that if the properties were specified
              * relatively, then the intent is to counteract parent's effect.
              * I.e. I believe this is the best code even in that case.
@@ -1486,17 +1596,17 @@ sp_style_merge_rel_enum_prop_from_dying_parent(SPIEnum &child, SPIEnum const &pa
         } else if (child.value == parent.value) {
             /* Leave as is. */
             /** \todo
-             * It's unclear what to do if style and parent specify the same 
-             * relative directive (narrower or wider).  We can either convert 
-             * to absolute specification or coalesce to a single relative 
+             * It's unclear what to do if style and parent specify the same
+             * relative directive (narrower or wider).  We can either convert
+             * to absolute specification or coalesce to a single relative
              * request (of half the strength of the original pair).
              *
-             * Converting to a single level of relative specification is a 
-             * better choice if the newly-unlinked clone is itself cloned to 
-             * other contexts (inheriting different font stretchiness): it 
+             * Converting to a single level of relative specification is a
+             * better choice if the newly-unlinked clone is itself cloned to
+             * other contexts (inheriting different font stretchiness): it
              * would preserve the effect that it will be narrower than
-             * the inherited context in each case.  The disadvantage is that 
-             * it will ~certainly affect the computed value of the 
+             * the inherited context in each case.  The disadvantage is that
+             * it will ~certainly affect the computed value of the
              * newly-unlinked clone.
              */
         } else {
@@ -1529,7 +1639,7 @@ sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &pare
             case SP_CSS_UNIT_EX:
                 child.value *= parent_child_em_ratio;
                 /** \todo
-                 * fixme: Have separate ex ratio parameter. 
+                 * fixme: Have separate ex ratio parameter.
                  * Get x height from libnrtype or pango.
                  */
                 if (!isFinite(child.value)) {
@@ -1604,7 +1714,7 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
 
     /* We do font-size first, to ensure that em size is up-to-date. */
     /** \todo
-     * fixme: We'll need to have more font-related things up the top once 
+     * fixme: We'll need to have more font-related things up the top once
      * we're getting x-height from pango or libnrtype.
      */
 
@@ -1698,8 +1808,8 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
         sp_style_merge_prop_from_dying_parent<SPIFloat>(style->stroke_miterlimit, parent->stroke_miterlimit);
 
         /** \todo
-         * We currently treat text-decoration as if it were a simple inherited 
-         * property (fixme). This code may need changing once we do the 
+         * We currently treat text-decoration as if it were a simple inherited
+         * property (fixme). This code may need changing once we do the
          * special fill/stroke inheritance mentioned by the spec.
          */
         sp_style_merge_prop_from_dying_parent<SPITextDecoration>(style->text_decoration,
@@ -1756,9 +1866,9 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
     /* Properties that involve a URI but are easy in other respects. */
     {
         /** \todo
-         * Could cause problems if original object was in another document 
-         * and it used a relative URL.  (At the time of writing, we don't 
-         * allow hrefs to other documents, so this isn't a problem yet.)  
+         * Could cause problems if original object was in another document
+         * and it used a relative URL.  (At the time of writing, we don't
+         * allow hrefs to other documents, so this isn't a problem yet.)
          * Paint properties also allow URIs.
          */
         //nyi: cursor,   // may involve change in effect, but we can't do much better
@@ -1846,7 +1956,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;
@@ -1858,9 +1968,29 @@ 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;
+        }
+
+        if (!style->filter.set || style->filter.inherit)
+        {
+            // FIXME:
+            // instead of just copying over, we need to _really merge_ the two filters by combining their
+            // filter primitives
+            sp_style_merge_ifilter(style, &parent->filter);
+        }
+
         /** \todo
-         * fixme: Check that we correctly handle all properties that don't 
-         * inherit by default (as shown in 
+         * fixme: Check that we correctly handle all properties that don't
+         * inherit by default (as shown in
          * http://www.w3.org/TR/SVG11/propidx.html for most SVG 1.1 properties).
          */
     }
@@ -1868,9 +1998,9 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
     /* SPIPaint properties (including color). */
     {
         /** \todo
-         * Think about the issues involved if specified as currentColor or 
-         * if specified relative to colorProfile, and if the currentColor or 
-         * colorProfile differs between parent \& child.  See also comments 
+         * Think about the issues involved if specified as currentColor or
+         * if specified relative to colorProfile, and if the currentColor or
+         * colorProfile differs between parent \& child.  See also comments
          * elsewhere in this function about URIs.
          */
         SPIPaint SPStyle::*const fields[] = {
@@ -1918,63 +2048,41 @@ 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_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
 {
-    if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->fill.value.paint.server))
-    {
-        sp_style_paint_clear(style, &style->fill, TRUE, FALSE);
+    // it may be that this style's SPIPaint has not yet created its URIReference;
+    // now that we have a document, we can create it here
+    if (!paint->value.href && document) {
+        paint->value.href = new SPPaintServerReference(document);
+        paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
     }
 
-    if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->stroke.value.paint.server))
-    {
-        sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
+    if (paint->value.href && paint->value.href->getObject())
+        paint->value.href->detach();
+
+    if (paint->value.href) {
+        try {
+            paint->value.href->attach(*uri);
+        } catch (Inkscape::BadURIException &e) {
+            g_warning("%s", e.what());
+            paint->value.href->detach();
+        }
     }
 }
 
-
-
-
-/**
- * Emit style modified signal on style's object if server is style's fill
- * or stroke paint server.
- */
 static void
-sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *style)
+sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
 {
-    if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->fill.value.paint.server))
-    {
-        if (style->object) {
-            /** \todo
-             * fixme: I do not know, whether it is optimal - we are 
-             * forcing reread of everything (Lauris)
-             */
-            /** \todo
-             * fixme: We have to use object_modified flag, because parent 
-             * flag is only available downstreams.
-             */
-            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
-        }
-    } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
-               && (server == style->stroke.value.paint.server))
-    {
-        if (style->object) {
-            /// \todo fixme:
-            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
-        }
-    } else {
-        g_assert_not_reached();
-    }
+    const Inkscape::URI IURI(uri);
+    sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
 }
 
-
+void
+sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
+{
+    sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
+}
 
 /**
  *
@@ -1982,7 +2090,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;
@@ -1997,19 +2105,8 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
             sp_color_copy(&paint->value.color, &parent->value.color);
             break;
         case SP_PAINT_TYPE_PAINTSERVER:
-            paint->value.paint.server = parent->value.paint.server;
-            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 (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);
-                }
+            if (parent->value.href) {
+                sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
             }
             break;
         case SP_PAINT_TYPE_NONE:
@@ -2022,10 +2119,34 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 
 
 /**
- * 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 
- * complete cascaded style to style_clipboard. When you need a CSS string 
- * for an object in the document tree, you normally call 
+ * Merge filter style from parent.
+ * Filter effects do not inherit by default
+ */
+static void
+sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
+{
+    sp_style_filter_clear(style);
+    style->filter.set = parent->set;
+    style->filter.inherit = parent->inherit;
+
+    if (style->filter.href && style->filter.href->getObject())
+       style->filter.href->detach();
+
+    if (style->filter.href && parent->href) {
+        try {
+            style->filter.href->attach(*parent->href->getURI());
+        } catch (Inkscape::BadURIException &e) {
+            g_warning("%s", e.what());
+            style->filter.href->detach();
+        }
+    }
+}
+
+/**
+ * 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
+ * complete cascaded style to style_clipboard. When you need a CSS string
+ * for an object in the document tree, you normally call
  * sp_style_write_difference instead to take into account the object's parent.
  *
  * \pre style != NULL.
@@ -2078,7 +2199,6 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap, &style->stroke_linecap, NULL, flags);
     p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin, &style->stroke_linejoin, NULL, flags);
 
-    marker_status("sp_style_write_string:  Writing markers");
     if (style->marker[SP_MARKER_LOC].set) {
         p += g_snprintf(p, c + BMAX - p, "marker:%s;", style->marker[SP_MARKER_LOC].value);
     } else if (flags == SP_STYLE_FLAG_ALWAYS) {
@@ -2142,6 +2262,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);
 
@@ -2162,7 +2287,7 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
 
 /**
  * Dumps style to CSS string, see sp_style_write_string()
- * 
+ *
  * \pre from != NULL.
  * \pre to != NULL.
  * \post ret != NULL.
@@ -2196,7 +2321,7 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
 
     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
 
-    /// \todo fixme: Per type methods need default flag too 
+    /// \todo fixme: Per type methods need default flag too
     if (from->opacity.set && from->opacity.value != SP_SCALE24_MAX) {
         p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &from->opacity, &to->opacity, SP_STYLE_FLAG_IFSET);
     }
@@ -2238,7 +2363,6 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
     p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
 
     /* markers */
-    marker_status("sp_style_write_difference:  Writing markers");
     if (from->marker[SP_MARKER_LOC].value != NULL) {
         p += g_snprintf(p, c + BMAX - p, "marker:%s;",       from->marker[SP_MARKER_LOC].value);
     }
@@ -2256,6 +2380,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
@@ -2285,22 +2414,48 @@ 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);
+
+    if (style->fill.value.href) 
+        delete style->fill.value.href;
+    if (style->stroke.value.href)
+        delete style->stroke.value.href;
+    if (style->filter.href)
+        delete style->filter.href;
+
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
 
     /** \todo fixme: Do that text manipulation via parents */
     SPObject *object = style->object;
+    SPDocument *document = style->document;
     gint const refcount = style->refcount;
     SPTextStyle *text = style->text;
     unsigned const text_private = style->text_private;
+
     memset(style, 0, sizeof(SPStyle));
+
     style->refcount = refcount;
     style->object = object;
+    style->document = document;
+
+    if (document) {
+        style->filter.href = new SPFilterReference(document);
+        style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
+
+        style->fill.value.href = new SPPaintServerReference(document);
+        style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
+
+        style->stroke.value.href = new SPPaintServerReference(document);
+        style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
+    }
+
     style->text = text;
     style->text_private = text_private;
+
     /* fixme: */
     style->text->font.set = FALSE;
     style->text->font_family.set = FALSE;
@@ -2406,12 +2561,16 @@ sp_style_clear(SPStyle *style)
         g_free(style->marker[i].value);
         style->marker[i].set = FALSE;
     }
+
+    style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
+    style->enable_background.set = false;
+    style->enable_background.inherit = false;
 }
 
 
 
 /**
- * 
+ *
  */
 static void
 sp_style_read_dash(SPStyle *style, gchar const *str)
@@ -2665,9 +2824,9 @@ sp_style_read_ilength(SPILength *val, gchar const *str)
         /** \todo fixme: Move this to standard place (Lauris) */
         value = g_ascii_strtod(str, &e);
         if ((gchar const *) e != str) {
-            /** \todo 
-             * Allow the number of px per inch to vary (document preferences, 
-             * X server or whatever).  E.g. don't fill in computed here, do 
+            /** \todo
+             * Allow the number of px per inch to vary (document preferences,
+             * X server or whatever).  E.g. don't fill in computed here, do
              * it at the same time as percentage units are done.
              */
             if (!*e) {
@@ -2816,7 +2975,9 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
 
 
 /**
- * Set SPIPaint object from string. 
+ * 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)
@@ -2829,20 +2990,18 @@ 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)) {
-        // 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);
-        if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
+    } else if (strneq(str, "url", 3) && paint != &style->color) {
+        gchar *uri = extract_uri(str);
+        if(uri == NULL || uri[0] == '\0') {
             paint->type = SP_PAINT_TYPE_NONE;
             return;
         }
@@ -2850,32 +3009,38 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
         paint->set = TRUE;
         paint->inherit = FALSE;
         paint->currentcolor = FALSE;
-        if (document) {
-            SPObject *ps = sp_uri_reference_resolve(document, str);
-            if (ps && SP_IS_PAINT_SERVER(ps)) {
-                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 (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);
-                }
-            } else {
-                paint->value.paint.server = NULL;
-            }
+
+        // it may be that this style's SPIPaint has not yet created its URIReference;
+        // now that we have a document, we can create it here
+        if (!paint->value.href && document) {
+            paint->value.href = new SPPaintServerReference(document);
+            paint->value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((paint == &style->fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), style));
         }
+
+        sp_style_set_ipaint_to_uri_string (style, paint, uri);
+
+        g_free (uri);
+        
     } 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->value.iccColor = tmp;
+            }
         }
     }
 }
@@ -2948,6 +3113,61 @@ sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
 
 
 
+/**
+ * Set SPIFilter object from string.
+ */
+static void
+sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
+{
+    SPIFilter *f = &(style->filter);
+    /* Try all possible values: inherit, none, uri */
+    if (streq(str, "inherit")) {
+        f->set = TRUE;
+        f->inherit = TRUE;
+        if (f->href && f->href->getObject())
+            f->href->detach(); 
+    } else if(streq(str, "none")) {
+        f->set = TRUE;
+        f->inherit = FALSE;
+        if (f->href && f->href->getObject())
+           f->href->detach(); 
+    } else if (strneq(str, "url", 3)) {
+        char *uri = extract_uri(str);
+        if(uri == NULL || uri[0] == '\0') {
+            g_warning("Specified filter url is empty");
+            f->set = TRUE;
+            f->inherit = FALSE;
+            return;
+        }
+        f->set = TRUE;
+        f->inherit = FALSE;
+        if (f->href && f->href->getObject())
+            f->href->detach();
+
+        // it may be that this style has not yet created its SPFilterReference;
+        // now that we have a document, we can create it here
+        if (!f->href && document) {
+            f->href = new SPFilterReference(document);
+            f->href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
+        }
+
+        try {
+            f->href->attach(Inkscape::URI(uri));
+        } catch (Inkscape::BadURIException &e) {
+            g_warning("%s", e.what());
+            f->href->detach();
+        }
+        g_free (uri);
+
+    } else {
+        /* We shouldn't reach this if SVG input is well-formed */
+        f->set = FALSE;
+        f->inherit = FALSE;
+        if (f->href && f->href->getObject())
+            f->href->detach(); 
+    }
+}
+
 /**
  * Set SPIEnum object from repr attribute.
  */
@@ -3095,7 +3315,9 @@ sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
         if (val->inherit) {
             return g_snprintf(p, len, "%s:inherit;", key);
         } else {
-            return g_snprintf(p, len, "%s:%s;", key, val->value);
+            gchar *val_quoted = css2_escape_quote(val->value);
+            return g_snprintf(p, len, "%s:%s;", key, val_quoted);
+            g_free (val_quoted);
         }
     }
     return 0;
@@ -3103,7 +3325,7 @@ sp_style_write_istring(gchar *p, gint const len, gchar const *const key,
 
 
 /**
- * 
+ *
  */
 static bool
 sp_length_differ(SPILength const *const a, SPILength const *const b)
@@ -3295,9 +3517,15 @@ 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->value.iccColor == b->value.iccColor)
+                     || (a->value.iccColor && b->value.iccColor
+                         && (a->value.iccColor->colorProfile == b->value.iccColor->colorProfile)
+                         && (a->value.iccColor->colors == b->value.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 (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
     return false;
 }
 
@@ -3321,14 +3549,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:
-                    return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
-                break;
-            default:
-                break;
+                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->value.iccColor) {
+                        CSSOStringStream css;
+                        css << color_buf << " icc-color(" << paint->value.iccColor->colorProfile;
+                        for (vector<double>::const_iterator i(paint->value.iccColor->colors.begin()),
+                                 iEnd(paint->value.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.href? paint->value.href->getURI()->toString() : "");
+                default:
+                    break;
             }
             return g_snprintf(b, len, "%s:none;", key);
         }
@@ -3392,31 +3633,80 @@ 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);
-        }
-        if (style->object || style->cloned) {
-            g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
-                                                 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
+    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->href && val->href->getURI()) {
+            return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
         }
-        paint->value.paint.server = NULL;
-        paint->value.paint.uri = NULL;
-        paint->type = SP_PAINT_TYPE_NONE;
     }
 
-    if (unset) {
-        paint->set = FALSE;
-        paint->inherit = 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->value.href && paint->value.href->getObject())
+        paint->value.href->detach(); 
+
+    paint->type = SP_PAINT_TYPE_NONE;
+    delete paint->value.iccColor;
+    paint->value.iccColor = NULL;
+}
+
+
+/**
+ * Clear filter object, and disconnect style from paintserver (if present).
+ */
+static void
+sp_style_filter_clear(SPStyle *style)
+{
+    if (style->filter.href && style->filter.href->getObject())
+        style->filter.href->detach(); 
+}
+
+
+// FIXME: Everything below this line belongs in a different file - css-chemistry?
+
+void
+sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *linked, bool recursive)
+{
+    Inkscape::XML::Node *repr = SP_OBJECT_REPR(item);
+
+    if (repr == NULL) return;
+
+    g_return_if_fail(linked != NULL);
+
+    gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
+
+    SPCSSAttr *css = sp_repr_css_attr_new();
+    sp_repr_css_set_property(css, property, val);
+    g_free(val);
+    if (recursive) {
+        sp_repr_css_change_recursive(repr, css, "style");
+    } else {
+        sp_repr_css_change(repr, css, "style");
     }
+    sp_repr_css_attr_unref(css);
 }
 
+
 /**
  * Clear all style property attributes in object.
  */
@@ -3488,6 +3778,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);
+    }
 }
 
 /**
@@ -3522,7 +3818,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);
 }
 
 /**
@@ -3564,16 +3860,16 @@ is_url(char const *p)
     if (p == NULL)
         return false;
 /** \todo
- * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs 
+ * FIXME: I'm not sure if this applies to SVG as well, but CSS2 says any URIs
  * in property values must start with 'url('.
  */
     return (g_ascii_strncasecmp(p, "url(", 4) == 0);
 }
 
 /**
- * Unset any properties that contain URI values. 
+ * Unset any properties that contain URI values.
  *
- * Used for storing style that will be reused across documents when carrying 
+ * Used for storing style that will be reused across documents when carrying
  * the referenced defs is impractical.
  */
 SPCSSAttr *
@@ -3671,28 +3967,81 @@ sp_css_attr_scale(SPCSSAttr *css, double ex)
 
 
 /**
- * Remove quotes from SPIString object value.
- * 
- * \todo FIXME: now used for font family, but perhaps this should apply to 
- * ALL strings (check CSS spec), in which case this should be part of 
- * read_istring.
+ * Remove quotes and escapes from a string. Returned value must be g_free'd.
+ * Note: in CSS (in style= and in stylesheets), unquoting and unescaping is done
+ * by libcroco, our CSS parser, though it adds a new pair of "" quotes for the strings
+ * it parsed for us. So this function is only used to remove those quotes and for
+ * presentation attributes, without any unescaping. (XML unescaping
+ * (&amp; etc) is done by XML parser.)
  */
-void
-css2_unescape_unquote (SPIString *val)
-{
-    if (val->set && val->value && strlen(val->value) >= 2) {
+gchar *
+attribute_unquote(gchar const *val)
+{
+    if (val) {
+        if (*val == '\'' || *val == '"') {
+            int l = strlen(val);
+            if (l >= 2) {
+                if ( ( val[0] == '"' && val[l - 1] == '"' )  ||
+                     ( val[0] == '\'' && val[l - 1] == '\'' )  ) {
+                    return (g_strndup (val+1, l-2));
+                }
+            }
+        }
+    }
 
-       /// \todo unescape all \-escaped chars
+    return (val? g_strdup (val) : NULL);
+}
 
-        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';
+/**
+ * Quote and/or escape string for writing to CSS (style=). Returned value must be g_free'd.
+ */
+gchar *
+css2_escape_quote(gchar const *val) {
+
+    Glib::ustring t;
+    bool quote = false;
+    bool last_was_space = false;
+
+    for (gchar const *i = val; *i; i++) {
+        bool is_space = ( *i == ' ' );
+        if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
+            // ASCII alphanumeric, - and _ don't require quotes
+            t.push_back(*i);
+        } else if ( is_space && !last_was_space ) {
+            // non-consecutive spaces don't require quotes
+            t.push_back(*i);
+        } else if (*i=='\'') {
+            // single quotes require escaping and quotes
+            t.push_back('\\');
+            t.push_back(*i);
+            quote = true;
+        } else {
+            // everything else requires quotes
+            t.push_back(*i);
+            quote = true;
+        }
+        if (i == val && !g_ascii_isalpha(*i)) {
+            // a non-ASCII/non-alpha initial character requires quotes
+            quote = true;
         }
+        last_was_space = is_space;
+    }
+
+    if (last_was_space) {
+        // a trailing space requires quotes
+        quote = true;
+    }
+
+    if (quote) {
+        // we use single quotes so the result can be stored in an XML
+        // attribute without incurring un-aesthetic additional quoting
+        // (our XML emitter always uses double quotes)
+        t.insert(t.begin(), '\'');
+        t.push_back('\'');
     }
-}
 
+    return (t.empty() ? NULL : g_strdup (t.c_str()));
+}
 
 /*
   Local Variables: