Code

use touchpath selection when rubberbanding with Alt; do move-selected with Alt only...
[inkscape.git] / src / style.cpp
index 230d8f46694f1a9be73efdee78d909279a0a724e..e15c54636adf10b496766cfd5eebe0431e27ff93 100644 (file)
@@ -30,7 +30,6 @@
 #include "attributes.h"
 #include "document.h"
 #include "extract-uri.h"
-#include "marker-status.h"
 #include "uri-references.h"
 #include "sp-paint-server.h"
 #include "streq.h"
 #include "xml/repr.h"
 #include "unit-constants.h"
 #include "isnan.h"
-#include <errno.h>
+
+#include <sigc++/functors/ptr_fun.h>
+#include <sigc++/adaptors/bind.h>
+
 using Inkscape::CSSOStringStream;
 using std::vector;
 
@@ -115,6 +117,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);
@@ -134,6 +137,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);
@@ -149,11 +153,12 @@ 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 void css2_unescape_unquote(SPIString *val);
+static gint sp_style_write_ifilter(gchar *b, gint len, gchar const *key, SPIFilter const *filter, SPIFilter const *base, guint flags);
 
 static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
 
+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));}
 
@@ -366,6 +371,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.
  */
@@ -397,9 +408,18 @@ sp_style_new()
 
     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();
 
-    style->fill_listening = false;
-    style->stroke_listening = false;
+    new (&style->filter_release_connection) sigc::connection();
+    new (&style->filter_modified_connection) sigc::connection();
 
     return style;
 }
@@ -416,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;
@@ -453,12 +473,24 @@ 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);
         sp_style_paint_clear(style, &style->stroke);
+        sp_style_filter_clear(style);
+        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();
+        style->filter_modified_connection.disconnect();
+        style->filter_modified_connection.~connection();
+        style->filter_release_connection.disconnect();
+        style->filter_release_connection.~connection();
         g_free(style->stroke_dash.dash);
         g_free(style);
     }
@@ -482,7 +514,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);
@@ -643,11 +675,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) {
@@ -733,8 +776,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:
@@ -875,12 +919,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);
@@ -942,7 +989,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);
@@ -953,7 +999,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);
@@ -963,7 +1008,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);
@@ -973,7 +1017,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);
@@ -1085,6 +1128,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);
     }
@@ -1415,6 +1461,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>
@@ -1868,6 +1923,26 @@ 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
@@ -1933,8 +2008,9 @@ 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))
     {
@@ -1956,8 +2032,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))
     {
@@ -1986,6 +2063,36 @@ sp_style_paint_server_modified(SPPaintServer *server, guint flags, SPStyle *styl
 
 
 
+/**
+ * Disconnects from filter.
+ */
+static void
+sp_style_filter_release(SPObject *obj, SPStyle *style)
+{
+    SPFilter *filter=static_cast<SPFilter *>(obj);
+    if (style->filter.filter == filter)
+    {
+        sp_style_filter_clear(style);
+    }
+}
+
+
+/**
+ * Emit style modified signal on style's object if the filter changed.
+ */
+static void
+sp_style_filter_modified(SPObject *obj, guint flags, SPStyle *style)
+{
+    SPFilter *filter=static_cast<SPFilter *>(obj);
+    if (style->filter.filter == filter)
+    {
+        if (style->object) {
+            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    }
+}
+
+
 /**
  *
  */
@@ -2020,15 +2127,18 @@ sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
                     }
                 }
                 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);
+                    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_listening = true;
+                        style->fill_release_connection = release_connection;
+                        style->fill_modified_connection = modified_connection;
                     } else {
                         assert(paint == &style->stroke);
-                        style->stroke_listening = true;
+                        style->stroke_release_connection = release_connection;
+                        style->stroke_modified_connection = modified_connection;
                     }
                 }
             }
@@ -2042,6 +2152,33 @@ 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 const *parent)
+{
+    sp_style_filter_clear(style);
+    style->filter.set = parent->set;
+    style->filter.inherit = parent->inherit;
+    style->filter.filter = parent->filter;
+    style->filter.uri = parent->uri;
+    if (style->filter.filter) {
+        if (style->object && !style->cloned) { // href filter for style of non-clones only
+            sp_object_href(SP_OBJECT(style->filter.filter), style);
+            style->filter_hreffed = true;
+        }
+        if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
+            SPObject *filter = style->filter.filter;
+            style->filter_release_connection
+                = filter->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_filter_release), style));
+            style->filter_modified_connection
+                = filter->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_filter_modified), style));
+        }
+    }
+}
+
 /**
  * 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
@@ -2099,7 +2236,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) {
@@ -2163,6 +2299,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);
 
@@ -2259,7 +2400,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);
     }
@@ -2277,6 +2417,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
@@ -2308,6 +2453,7 @@ sp_style_clear(SPStyle *style)
 
     sp_style_paint_clear(style, &style->fill);
     sp_style_paint_clear(style, &style->stroke);
+    sp_style_filter_clear(style);
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
@@ -2427,6 +2573,14 @@ sp_style_clear(SPStyle *style)
         g_free(style->marker[i].value);
         style->marker[i].set = FALSE;
     }
+
+    style->filter.set = FALSE;
+    style->filter.uri = NULL;
+    style->filter.filter = NULL;
+
+    style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
+    style->enable_background.set = false;
+    style->enable_background.inherit = false;
 }
 
 
@@ -2887,15 +3041,18 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                     }
                 }
                 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);
+                    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_listening = true;
+                        style->fill_release_connection = release_connection;
+                        style->fill_modified_connection = modified_connection;
                     } else {
                         assert(paint == &style->stroke);
-                        style->stroke_listening = true;
+                        style->stroke_release_connection = release_connection;
+                        style->stroke_modified_connection = modified_connection;
                     }
                 }
             } else {
@@ -2915,38 +3072,12 @@ sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
                 ++str;
             }
             if (strneq(str, "icc-color(", 10)) {
-                str += 10;
-
                 SVGICCColor* tmp = new SVGICCColor();
-                while ( *str && *str != ' ' && *str!= ',' && *str != ')' ) {
-                    tmp->colorProfile += *str++;
+                if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                    delete tmp;
+                    tmp = 0;
                 }
-                while ( g_ascii_isspace(*str) || *str == ',' ) {
-                    ++str;
-                }
-
-                while ( *str && *str != ')' ) {
-                    if ( g_ascii_isdigit(*str) || *str == '.' ) {
-                        gchar* endPtr = 0;
-                        gdouble dbl = g_ascii_strtod( str, &endPtr );
-                        if ( !errno ) {
-                            tmp->colors.push_back( dbl );
-                            str = endPtr;
-                        } else {
-                            delete tmp;
-                            tmp = 0;
-                            break;
-                        }
-
-                        while ( tmp && g_ascii_isspace(*str) || *str == ',' ) {
-                            ++str;
-                        }
-                    } else {
-                        break;
-                    }
-
-                }
-                paint->iccColor = tmp;
+                paint->value.iccColor = tmp;
             }
         }
     }
@@ -3020,6 +3151,63 @@ 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;
+        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 = sp_uri_reference_resolve(document, str);
+            if (SP_IS_FILTER(obj)) {
+                f->filter = SP_FILTER(obj);
+                if (style->object && !style->cloned) {
+                    sp_object_href(obj, style);
+                    style->filter_hreffed = true;
+                }
+                if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
+                    SPObject *filter = style->filter.filter;
+                    style->filter_release_connection
+                        = filter->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_filter_release), style));
+                    style->filter_modified_connection
+                        = filter->connectModified(sigc::bind<2>(sigc::ptr_fun(&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.
  */
@@ -3167,7 +3355,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;
@@ -3368,10 +3558,10 @@ sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
         return true;
     if (a->type == SP_PAINT_TYPE_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))));
+                 && ((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)
@@ -3402,11 +3592,11 @@ sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
                 case SP_PAINT_TYPE_COLOR: {
                     char color_buf[8];
                     sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
-                    if (paint->iccColor) {
+                    if (paint->value.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());
+                        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;
                         }
@@ -3482,6 +3672,30 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 }
 
 
+/**
+ * Write SPIFilter object into string.
+ */
+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 ((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);
+        }
+    }
+
+
+    return 0;
+}
+
+
 /**
  * Clear paint object, and disconnect style from paintserver (if present).
  */
@@ -3494,32 +3708,70 @@ sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
                 sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
                 style->fill_hreffed = false;
             }
-            if (style->fill_listening) {
-                g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
-                                                 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
-                style->fill_listening = 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;
             }
-            if (style->stroke_listening) {
-                g_signal_handlers_disconnect_matched(G_OBJECT(paint->value.paint.server),
-                                                 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, style);
-                style->stroke_listening = 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;
-    delete paint->iccColor;
-    paint->iccColor = NULL;
+    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_hreffed) {
+        sp_object_hunref(SP_OBJECT(style->filter.filter), style);
+        style->filter_hreffed = false;
+    }
+    style->filter_release_connection.disconnect();
+    style->filter_modified_connection.disconnect();
+
+    style->filter.filter = NULL;
+}
+
+
+// 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.
  */
@@ -3591,6 +3843,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);
+    }
 }
 
 /**
@@ -3774,28 +4032,63 @@ 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.)
  */
-static void
-css2_unescape_unquote(SPIString *val)
+gchar *
+attribute_unquote(gchar const *val)
 {
-    if (val->set && val->value && strlen(val->value) >= 2) {
+    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;
+
+    for (gchar const *i = val; *i; i++) {
+        if (g_ascii_isalnum(*i) || *i=='-' || *i=='_') {
+            t.push_back(*i);
+        } else if (*i=='\'') {
+            t.push_back('\\');
+            t.push_back(*i);
+            quote = true;
+        } else {
+            t.push_back(*i);
+            quote = true;
+        }
+        if (i == val && !g_ascii_isalpha(*i)) {
+            quote = true;
         }
     }
-}
 
+    if (quote) { // we use the ' quotes so the result can go to the XML attribute
+        t.insert(t.begin(), '\'');
+        t.push_back('\'');
+    }
+
+    return (t.empty() ? NULL : g_strdup (t.c_str()));
+}
 
 /*
   Local Variables: