Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[inkscape.git] / src / style.cpp
index 54c3a0c0ca38cd05ed5067cdfd1225ed05319135..551dd658516b594ea794405040c0aa6427b0d73b 100644 (file)
@@ -1,9 +1,7 @@
-#define __SP_STYLE_C__
-
-/** \file
- * SVG stylesheets implementation.
- *
- * Authors:
+/** @file
+ * @brief SVG stylesheets implementation.
+ */
+/* Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   Peter Moulder <pmoulder@mail.csse.monash.edu.au>
  *   bulia byak <buliabyak@users.sf.net>
 # include "config.h"
 #endif
 
+#include <cstring>
+#include <string>
+
 #include "libcroco/cr-sel-eng.h"
 #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 "style.h"
 #include "svg/css-ostringstream.h"
 #include "xml/repr.h"
+#include "xml/simple-document.h"
 #include "unit-constants.h"
-#include "isnan.h"
-
-namespace Inkscape {
-
-/**
- * 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);
-        }
-    }
+#include "2geom/isnan.h"
+#include "macros.h"
+#include "preferences.h"
 
-    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 "sp-filter-reference.h"
 
-    return result;
-}
+#include <sigc++/functors/ptr_fun.h>
+#include <sigc++/adaptors/bind.h>
 
-}
+using Inkscape::CSSOStringStream;
+using std::vector;
 
 #define BMAX 8192
 
@@ -111,6 +65,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);
@@ -128,12 +83,14 @@ static void sp_style_read_ilength(SPILength *val, gchar const *str);
 static void sp_style_read_ilengthornormal(SPILengthOrNormal *val, gchar const *str);
 static void sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str);
 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_ibaselineshift(SPIBaselineShift *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);
 static void sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const *key);
+static void sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key);
 static void sp_style_read_pfloat(SPIFloat *val, Inkscape::XML::Node *repr, gchar const *key);
 
 static gint sp_style_write_ifloat(gchar *p, gint len, gchar const *key, SPIFloat const *val, SPIFloat const *base, guint flags);
@@ -143,12 +100,12 @@ static gint sp_style_write_istring(gchar *p, gint len, gchar const *key, SPIStri
 static gint sp_style_write_ilength(gchar *p, gint len, gchar const *key, SPILength const *val, SPILength const *base, guint flags);
 static gint sp_style_write_ipaint(gchar *b, gint len, gchar const *key, SPIPaint const *paint, SPIPaint const *base, guint flags);
 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_ibaselineshift(gchar *p, gint len, gchar const *key, SPIBaselineShift const *val, SPIBaselineShift 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);
 
-static void css2_unescape_unquote(SPIString *val);
-
-static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunref, unsigned unset);
+static void sp_style_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));}
@@ -161,6 +118,9 @@ static void sp_style_paint_clear(SPStyle *style, SPIPaint *paint, unsigned hunre
 #define SPS_READ_IFONTSIZE_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ifontsize((v), (s));}
 #define SPS_READ_PFONTSIZE_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pfontsize((v), (r), (k));}
 
+#define SPS_READ_IBASELINE_SHIFT_IF_UNSET(v,s) if (!(v)->set) {sp_style_read_ibaselineshift((v), (s));}
+#define SPS_READ_PBASELINE_SHIFT_IF_UNSET(v,r,k) if (!(v)->set) {sp_style_read_pbaselineshift((v), (r), (k));}
+
 static void sp_style_merge_from_object_stylesheet(SPStyle *, SPObject const *);
 
 struct SPStyleEnum {
@@ -301,6 +261,13 @@ static SPStyleEnum const enum_writing_mode[] = {
     {NULL, -1}
 };
 
+static SPStyleEnum const enum_baseline_shift[] = {
+    {"baseline", SP_CSS_BASELINE_SHIFT_BASELINE},
+    {"sub",      SP_CSS_BASELINE_SHIFT_SUB},
+    {"super",    SP_CSS_BASELINE_SHIFT_SUPER},
+    {NULL, -1}
+};
+
 static SPStyleEnum const enum_visibility[] = {
     {"hidden", SP_CSS_VISIBILITY_HIDDEN},
     {"collapse", SP_CSS_VISIBILITY_COLLAPSE},
@@ -362,28 +329,137 @@ 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.
  */
 static void
 sp_style_object_release(SPObject *object, SPStyle *style)
 {
+    (void)object; // TODO
     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)
+{
+    (void)flags; // TODO
+    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)
+{
+    (void)flags; // TODO
+    SPPaintServer *server = static_cast<SPPaintServer *>(obj);
+
+    if ((style->fill.isPaintserver())
+        && 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.isPaintserver())
+        && style->getStrokePaintServer() == server)
+    {
+        if (style->object) {
+            /// \todo fixme:
+            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    } else if (server) {
+        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;
 
@@ -391,11 +467,10 @@ sp_style_new()
 
     style->cloned = false;
 
-    style->fill_hreffed = false;
-    style->stroke_hreffed = false;
-
-    style->fill_listening = false;
-    style->stroke_listening = 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;
 }
@@ -410,11 +485,11 @@ 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( object->document );
     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)) {
+    if (object && object->cloned) {
         style->cloned = true;
     }
 
@@ -449,12 +524,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();
+
+        style->fill.clear();
+        style->stroke.clear();
+        sp_style_filter_clear(style);
+
         g_free(style->stroke_dash.dash);
         g_free(style);
     }
@@ -474,11 +571,11 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
 
     sp_style_clear(style);
 
-    if (object && SP_OBJECT_IS_CLONED(object)) {
+    if (object && object->cloned) {
         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);
@@ -540,6 +637,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
                             enum_writing_mode, true);
     SPS_READ_PENUM_IF_UNSET(&style->text_anchor, repr, "text-anchor",
                             enum_text_anchor, true);
+    SPS_READ_PBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, repr, "baseline-shift");
 
     /* opacity */
     if (!style->opacity.set) {
@@ -561,7 +659,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     if (!style->fill.set) {
         val = repr->attribute("fill");
         if (val) {
-            sp_style_read_ipaint(&style->fill, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+            style->fill.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
         }
     }
     /* fill-opacity */
@@ -577,7 +675,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     if (!style->stroke.set) {
         val = repr->attribute("stroke");
         if (val) {
-            sp_style_read_ipaint(&style->stroke, val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+            style->stroke.read( val, *style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL );
         }
     }
     SPS_READ_PLENGTH_IF_UNSET(&style->stroke_width, repr, "stroke-width");
@@ -624,25 +722,50 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
             sp_style_read_dash(style, val);
         }
     }
+
     if (!style->stroke_dashoffset_set) {
-        /* fixme */
         val = repr->attribute("stroke-dashoffset");
         if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
             style->stroke_dashoffset_set = TRUE;
+        } else if (val && !strcmp(val, "inherit")) {
+            style->stroke_dashoffset_set = TRUE;
+            style->stroke_dashoffset_inherit = TRUE;
         } else {
             style->stroke_dashoffset_set = FALSE;
         }
     }
 
+    /* -inkscape-font-specification */
+    if (!style->text_private || !style->text->font_specification.set) {
+        val = repr->attribute("-inkscape-font-specification");
+        if (val) {
+            if (!style->text_private) sp_style_privatize_text(style);
+            gchar *val_unquoted = attribute_unquote(val);
+            sp_style_read_istring(&style->text->font_specification, val_unquoted);
+            if (val_unquoted) g_free (val_unquoted);
+        }
+    }
+
     /* font-family */
     if (!style->text_private || !style->text->font_family.set) {
         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) {
@@ -652,7 +775,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);
@@ -683,15 +806,33 @@ sp_style_read_from_object(SPStyle *style, SPObject *object)
 
 
 /**
- * Read style properties from repr only.
+ * Read style properties from preferences.
+ * @param style The style to write to
+ * @param path Preferences directory from which the style should be read
  */
 void
-sp_style_read_from_repr(SPStyle *style, Inkscape::XML::Node *repr)
+sp_style_read_from_prefs(SPStyle *style, Glib::ustring const &path)
 {
     g_return_if_fail(style != NULL);
-    g_return_if_fail(repr != NULL);
+    g_return_if_fail(path != "");
+
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+
+    // not optimal: we reconstruct the node based on the prefs, then pass it to
+    // sp_style_read for actual processing.
+    Inkscape::XML::SimpleDocument *tempdoc = new Inkscape::XML::SimpleDocument;
+    Inkscape::XML::Node *tempnode = tempdoc->createElement("temp");
 
-    sp_style_read(style, NULL, repr);
+    std::vector<Inkscape::Preferences::Entry> attrs = prefs->getAllEntries(path);
+    for (std::vector<Inkscape::Preferences::Entry>::iterator i = attrs.begin(); i != attrs.end(); ++i) {
+        tempnode->setAttribute(i->getEntryName().data(), i->getString().data());
+    }
+
+    sp_style_read(style, NULL, tempnode);
+
+    Inkscape::GC::release(tempnode);
+    Inkscape::GC::release(tempdoc);
+    delete tempdoc;
 }
 
 
@@ -724,13 +865,22 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
     g_return_if_fail(val != NULL);
 
     switch (id) {
+        case SP_PROP_INKSCAPE_FONT_SPEC:
+            if (!style->text_private) sp_style_privatize_text(style);
+            if (!style->text->font_specification.set) {
+                gchar *val_unquoted = attribute_unquote(val);
+                sp_style_read_istring(&style->text->font_specification, val_unquoted);
+                if (val_unquoted) g_free (val_unquoted);
+            }
+            break;
         /* CSS2 */
         /* Font */
         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:
@@ -805,6 +955,9 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         case SP_PROP_TEXT_ANCHOR:
             SPS_READ_IENUM_IF_UNSET(&style->text_anchor, val, enum_text_anchor, true);
             break;
+        case SP_PROP_BASELINE_SHIFT:
+            SPS_READ_IBASELINE_SHIFT_IF_UNSET(&style->baseline_shift, val);
+            break;
             /* Text (unimplemented) */
         case SP_PROP_TEXT_RENDERING: {
             /* Ignore the hint. */
@@ -815,9 +968,6 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         case SP_PROP_ALIGNMENT_BASELINE:
             g_warning("Unimplemented style property SP_PROP_ALIGNMENT_BASELINE: value: %s", val);
             break;
-        case SP_PROP_BASELINE_SHIFT:
-            g_warning("Unimplemented style property SP_PROP_BASELINE_SHIFT: value: %s", val);
-            break;
         case SP_PROP_DOMINANT_BASELINE:
             g_warning("Unimplemented style property SP_PROP_DOMINANT_BASELINE: value: %s", val);
             break;
@@ -858,25 +1008,39 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             /* SVG */
             /* Clip/Mask */
         case SP_PROP_CLIP_PATH:
-            g_warning("Unimplemented style property SP_PROP_CLIP_PATH: value: %s", val);
+            /** \todo
+             * This is a workaround. Inkscape only supports 'clip-path' as SVG attribute, not as
+             * style property. By having both CSS and SVG attribute set, editing of clip-path
+             * will fail, since CSS always overwrites SVG attributes.
+             * Fixes Bug #324849
+             */
+            g_warning("attribute 'clip-path' given as CSS");
+            style->object->repr->setAttribute("clip-path", val);
             break;
         case SP_PROP_CLIP_RULE:
             g_warning("Unimplemented style property SP_PROP_CLIP_RULE: value: %s", val);
             break;
         case SP_PROP_MASK:
-            g_warning("Unimplemented style property SP_PROP_MASK: value: %s", val);
+            /** \todo
+             * See comment for SP_PROP_CLIP_PATH
+             */
+            g_warning("attribute 'mask' given as CSS");
+            style->object->repr->setAttribute("mask", val);
             break;
         case SP_PROP_OPACITY:
             if (!style->opacity.set) {
                 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);
@@ -916,7 +1080,7 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
         }
         case SP_PROP_FILL:
             if (!style->fill.set) {
-                sp_style_read_ipaint(&style->fill, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+                style->fill.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
             }
             break;
         case SP_PROP_FILL_OPACITY:
@@ -938,7 +1102,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);
@@ -949,7 +1112,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);
@@ -959,7 +1121,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);
@@ -969,7 +1130,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);
@@ -987,7 +1147,7 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
 
         case SP_PROP_STROKE:
             if (!style->stroke.set) {
-                sp_style_read_ipaint(&style->stroke, val, style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL);
+                style->stroke.read( val, *style, (style->object) ? SP_OBJECT_DOCUMENT(style->object) : NULL );
             }
             break;
         case SP_PROP_STROKE_WIDTH:
@@ -1000,9 +1160,11 @@ sp_style_merge_property(SPStyle *style, gint id, gchar const *val)
             break;
         case SP_PROP_STROKE_DASHOFFSET:
             if (!style->stroke_dashoffset_set) {
-                /* fixme */
                 if (sp_svg_number_read_d(val, &style->stroke_dash.offset)) {
                     style->stroke_dashoffset_set = TRUE;
+                } else if (val && !strcmp(val, "inherit")) {
+                    style->stroke_dashoffset_set = TRUE;
+                    style->stroke_dashoffset_inherit = TRUE;
                 } else {
                     style->stroke_dashoffset_set = FALSE;
                 }
@@ -1081,6 +1243,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);
     }
@@ -1187,6 +1352,50 @@ sp_style_merge_font_size_from_parent(SPIFontSize &child, SPIFontSize const &pare
     }
 }
 
+// Some shifts are defined relative to parent.
+static void
+sp_style_merge_baseline_shift_from_parent(SPIBaselineShift &child, SPIBaselineShift const &parent,
+                                          SPIFontSize const &pfont_size)
+{
+    /* 'baseline-shift' */
+    if (!child.set || child.inherit) {
+        /* Inherit the computed value.  Reference: http://www.w3.org/TR/SVG11/styling.html#Inheritance */
+        child.computed = parent.computed;  // Does this make sense (applying a shift a second time)?
+    } else if (child.type == SP_BASELINE_SHIFT_LITERAL) {
+        if( child.literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
+            child.computed = 0; // No change
+        } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUB ) {
+            // Should use subscript position from font relative to alphabetic baseline
+            // OpenOffice, Adobe: -0.33, Word -0.14, LaTex about -0.2.
+            child.computed = -0.2 * pfont_size.computed; 
+        } else if (child.literal == SP_CSS_BASELINE_SHIFT_SUPER ) {
+            // Should use superscript position from font relative to alphabetic baseline
+            // OpenOffice, Adobe: 0.33, Word 0.35, LaTex about 0.45.
+            child.computed =  0.4 * pfont_size.computed; 
+        } else {
+            /* Illegal value */
+        }
+    } else if (child.type == SP_BASELINE_SHIFT_PERCENTAGE) {
+        // Percentage for baseline shift is relative to computed "line-height"
+        // which is just font-size (see SVG1.1 'font').
+        child.computed = pfont_size.computed * child.value;
+    } else if (child.type == SP_BASELINE_SHIFT_LENGTH) {
+        switch (child.unit) {
+            case SP_CSS_UNIT_EM:
+                child.computed = child.value * pfont_size.computed; 
+                break;
+            case SP_CSS_UNIT_EX:
+                child.computed = child.value * 0.5 * pfont_size.computed; 
+                break;
+            default:
+                /* No change */
+                break;
+        }
+    }
+    // baseline-shifts are relative to parent baseline
+    child.computed += parent.computed;
+}
+
 /**
  * Sets computed values in \a style, which may involve inheriting from (or in some other way
  * calculating from) corresponding computed values of \a parent.
@@ -1291,16 +1500,19 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
     }
 
     if (!style->line_height.set || style->line_height.inherit) {
+        style->line_height.value = parent->line_height.value;
         style->line_height.computed = parent->line_height.computed;
         style->line_height.normal = parent->line_height.normal;
     }
 
     if (!style->letter_spacing.set || style->letter_spacing.inherit) {
+        style->letter_spacing.value = parent->letter_spacing.value;
         style->letter_spacing.computed = parent->letter_spacing.computed;
         style->letter_spacing.normal = parent->letter_spacing.normal;
     }
 
     if (!style->word_spacing.set || style->word_spacing.inherit) {
+        style->word_spacing.value = parent->word_spacing.value;
         style->word_spacing.computed = parent->word_spacing.computed;
         style->word_spacing.normal = parent->word_spacing.normal;
     }
@@ -1325,6 +1537,10 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         style->text_anchor.computed = parent->text_anchor.computed;
     }
 
+    /* Baseline Shift... Some shifts are relative to parent. */
+    sp_style_merge_baseline_shift_from_parent(style->baseline_shift, parent->baseline_shift,
+                                              parent->font_size);
+
     if (style->opacity.inherit) {
         style->opacity.value = parent->opacity.value;
     }
@@ -1377,11 +1593,7 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         style->stroke_miterlimit.value = parent->stroke_miterlimit.value;
     }
 
-    if (!style->stroke_dasharray_set && parent->stroke_dasharray_set) {
-        /** \todo
-         * This code looks wrong.  Why does the logic differ from the
-         * above properties? Similarly dashoffset below.
-         */
+    if (!style->stroke_dasharray_set || style->stroke_dasharray_inherit) {
         style->stroke_dash.n_dash = parent->stroke_dash.n_dash;
         if (style->stroke_dash.n_dash > 0) {
             style->stroke_dash.dash = g_new(gdouble, style->stroke_dash.n_dash);
@@ -1389,7 +1601,7 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         }
     }
 
-    if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
+    if (!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) {
         style->stroke_dash.offset = parent->stroke_dash.offset;
     }
 
@@ -1404,6 +1616,13 @@ sp_style_merge_from_parent(SPStyle *const style, SPStyle const *const parent)
         }
     }
 
+    if (style->text && parent->text) {
+        if (!style->text->font_specification.set || style->text->font_specification.inherit) {
+            g_free(style->text->font_specification.value);
+            style->text->font_specification.value = g_strdup(parent->text->font_specification.value);
+        }
+    }
+
     /* Markers - Free the old value and make copy of the new */
     for (unsigned i = SP_MARKER_LOC; i < SP_MARKER_LOC_QTY; i++) {
         if (!style->marker[i].set || style->marker[i].inherit) {
@@ -1411,6 +1630,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>
@@ -1448,7 +1676,7 @@ sp_style_merge_paint_prop_from_dying_parent(SPStyle *style,
      * I haven't given this much attention.  See comments below about
      * currentColor, colorProfile, and relative URIs.
      */
-    if (!child.set || child.inherit || child.currentcolor) {
+    if (!child.set || child.inherit) {
         sp_style_merge_ipaint(style, &child, &parent);
         child.set = parent.set;
         child.inherit = parent.inherit;
@@ -1538,7 +1766,7 @@ sp_style_merge_length_prop_from_dying_parent(LengthT &child, LengthT const &pare
                  * fixme: Have separate ex ratio parameter.
                  * Get x height from libnrtype or pango.
                  */
-                if (!isFinite(child.value)) {
+                if (!IS_FINITE(child.value)) {
                     child.value = child.computed;
                     child.unit = SP_CSS_UNIT_NONE;
                 }
@@ -1780,10 +2008,14 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
     /* Font */
 
     if (style->text && parent->text) {
+        sp_style_merge_string_prop_from_dying_parent(style->text->font_specification,
+                                                     parent->text->font_specification);
+
         sp_style_merge_string_prop_from_dying_parent(style->text->font_family,
                                                      parent->text->font_family);
     }
 
+
     /* Properties that don't inherit by default.  Most of these need special handling. */
     {
         /*
@@ -1864,6 +2096,23 @@ 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)
+        {
+            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
@@ -1914,130 +2163,125 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
     }
 
     /* Note: this will need length handling once dasharray_offset supports units. */
-    if (!style->stroke_dashoffset_set && parent->stroke_dashoffset_set) {
+    if ((!style->stroke_dashoffset_set || style->stroke_dashoffset_inherit) && parent->stroke_dashoffset_set && !parent->stroke_dashoffset_inherit) {
         style->stroke_dash.offset = parent->stroke_dash.offset;
         style->stroke_dashoffset_set = parent->stroke_dashoffset_set;
-        /* fixme: we don't currently allow stroke-dashoffset to be `inherit'.  TODO: Try to
+        style->stroke_dashoffset_inherit = parent->stroke_dashoffset_inherit;
+        /* TODO: Try to
          * represent it as a normal SPILength; though will need to do something about existing
          * users of stroke_dash.offset and stroke_dashoffset_set. */
     }
 }
 
 
-
-/**
- * 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 (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();
+        }
     }
+}
 
-    if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->stroke.value.paint.server))
-    {
-        sp_style_paint_clear(style, &style->stroke, TRUE, FALSE);
+static void
+sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
+{
+    try {
+        const Inkscape::URI IURI(uri);
+        sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
+    } catch (...) {
+        g_warning("URI failed to parse: %s", uri);
     }
 }
 
-
-
+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);
+}
 
 /**
- * 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_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 {
-    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);
+    if ((paint->set && paint->currentcolor) || parent->currentcolor) {
+        bool isset = paint->set;
+        paint->clear();
+        paint->set = isset;
+        paint->currentcolor = TRUE;
+        paint->setColor(style->color.value.color);
+        return;
+    }
+
+    paint->clear();
+    if ( parent->isPaintserver() ) {
+        if (parent->value.href) {
+            sp_style_set_ipaint_to_uri(style, paint, parent->value.href->getURI(), parent->value.href->getOwnerDocument());
+        } else {
+            g_warning("Expected paint server not found.");
         }
+    } else if ( parent->isColor() ) {
+        paint->setColor( parent->value.color );
+    } else if ( parent->isNoneSet() ) {
+        paint->noneSet = TRUE;
+    } else if ( parent->isNone() ) {
+        //
     } else {
         g_assert_not_reached();
     }
 }
 
 
-
 /**
- *
+ * Merge filter style from parent.
+ * Filter effects do not inherit by default
  */
 static void
-sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
+sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
 {
-    sp_style_paint_clear(style, paint, TRUE, FALSE);
+    // FIXME:
+    // instead of just copying over, we need to _really merge_ the two filters by combining their
+    // filter primitives
 
-    if ((paint->set && paint->currentcolor) || parent->currentcolor) {
-        paint->currentcolor = TRUE;
-        paint->type = SP_PAINT_TYPE_COLOR;
-        sp_color_copy(&paint->value.color, &style->color.value.color);
-        return;
+    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();
+
+    // it may be that this style has not yet created its SPFilterReference
+    if (!style->filter.href && style->object && SP_OBJECT_DOCUMENT(style->object)) {
+            style->filter.href = new SPFilterReference(SP_OBJECT_DOCUMENT(style->object));
+            style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
     }
 
-    paint->type = parent->type;
-    switch (paint->type) {
-        case SP_PAINT_TYPE_COLOR:
-            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
-                    sp_object_href(SP_OBJECT(paint->value.paint.server), style);
-                    if (paint == &style->fill) {
-                        style->fill_hreffed = true;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_hreffed = true;
-                    }
-                }
-                if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "release",
-                                     G_CALLBACK(sp_style_paint_server_release), style);
-                    g_signal_connect(G_OBJECT(paint->value.paint.server), "modified",
-                                     G_CALLBACK(sp_style_paint_server_modified), style);
-                    if (paint == &style->fill) {
-                        style->fill_listening = true;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_listening = true;
-                    }
-                }
-            }
-            break;
-        case SP_PAINT_TYPE_NONE:
-            break;
-        default:
-            g_assert_not_reached();
-            break;
+    if (style->filter.href && parent->href && parent->href->getObject()) {
+        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
@@ -2083,82 +2327,119 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &style->writing_mode, NULL, flags);
 
     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, NULL, flags);
+    p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &style->baseline_shift, NULL, flags);
+
 
     /// \todo fixme: Per type methods need default flag too (lauris)
-    p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
-    p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
+
+    if (style->opacity.value != SP_SCALE24_MAX) {
+        p += sp_style_write_iscale24(p, c + BMAX - p, "opacity", &style->opacity, NULL, flags);
+    }
+
+    if (!style->color.noneSet) { // CSS does not permit "none" for color
+        p += sp_style_write_ipaint(p, c + BMAX - p, "color", &style->color, NULL, flags);
+    }
+
     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &style->fill, NULL, flags);
-    p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
-    p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
+    // if fill:none, skip writing fill properties
+    if (!style->fill.noneSet) {
+        p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &style->fill_opacity, NULL, flags);
+        p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &style->fill_rule, NULL, flags);
+    }
+
     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &style->stroke, NULL, flags);
-    p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, 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");
+    // stroke width affects markers, so write it if there's stroke OR any markers
+    if (!style->stroke.noneSet ||
+        style->marker[SP_MARKER_LOC].set ||
+        style->marker[SP_MARKER_LOC_START].set ||
+        style->marker[SP_MARKER_LOC_MID].set ||
+        style->marker[SP_MARKER_LOC_END].set) {
+        p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &style->stroke_width, NULL, flags);
+    }
+
+    // if stroke:none, skip writing stroke properties
+    if (!style->stroke.noneSet) {
+        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);
+        p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
+        p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
+
+        /** \todo fixme: */
+        if ((flags == SP_STYLE_FLAG_ALWAYS)
+            || style->stroke_dasharray_set)
+        {
+            if (style->stroke_dasharray_inherit) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
+            } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
+                gint i;
+                for (i = 0; i < style->stroke_dash.n_dash; i++) {
+                    Inkscape::CSSOStringStream os;
+                    if (i) {
+                        os << ", ";
+                    }
+                    os << style->stroke_dash.dash[i];
+                    p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
+                }
+                if (p < c + BMAX) {
+                    *p++ = ';';
+                }
+            } else {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
+            }
+        }
+
+        /** \todo fixme: */
+        if (style->stroke_dashoffset_set) {
+            if (style->stroke_dashoffset_inherit) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
+            } else {
+                Inkscape::CSSOStringStream os;
+                os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
+                p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
+            }
+        } else if (flags == SP_STYLE_FLAG_ALWAYS) {
+            p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
+        }
+    }
+
+    bool marker_none = false;
+    gchar *master = style->marker[SP_MARKER_LOC].value;
     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) {
         p += g_snprintf(p, c + BMAX - p, "marker:none;");
+        marker_none = true;
     }
-    if (style->marker[SP_MARKER_LOC_START].set) {
+    if (style->marker[SP_MARKER_LOC_START].set
+       && (!master || strcmp(master, style->marker[SP_MARKER_LOC_START].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", style->marker[SP_MARKER_LOC_START].value);
-    } else if (flags == SP_STYLE_FLAG_ALWAYS) {
+    } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
         p += g_snprintf(p, c + BMAX - p, "marker-start:none;");
     }
-    if (style->marker[SP_MARKER_LOC_MID].set) {
+    if (style->marker[SP_MARKER_LOC_MID].set
+       && (!master || strcmp(master, style->marker[SP_MARKER_LOC_MID].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;", style->marker[SP_MARKER_LOC_MID].value);
-    } else if (flags == SP_STYLE_FLAG_ALWAYS) {
+    } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
         p += g_snprintf(p, c + BMAX - p, "marker-mid:none;");
     }
-    if (style->marker[SP_MARKER_LOC_END].set) {
+    if (style->marker[SP_MARKER_LOC_END].set
+       && (!master || strcmp(master, style->marker[SP_MARKER_LOC_END].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;", style->marker[SP_MARKER_LOC_END].value);
-    } else if (flags == SP_STYLE_FLAG_ALWAYS) {
+    } else if (flags == SP_STYLE_FLAG_ALWAYS && !marker_none) {
         p += g_snprintf(p, c + BMAX - p, "marker-end:none;");
     }
 
-    p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit", &style->stroke_miterlimit, NULL, flags);
-
-    /** \todo fixme: */
-    if ((flags == SP_STYLE_FLAG_ALWAYS)
-        || style->stroke_dasharray_set)
-    {
-        if (style->stroke_dasharray_inherit) {
-            p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
-        } else if (style->stroke_dash.n_dash && style->stroke_dash.dash) {
-            p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
-            gint i;
-            for (i = 0; i < style->stroke_dash.n_dash; i++) {
-                Inkscape::CSSOStringStream os;
-                if (i) {
-                    os << ", ";
-                }
-                os << style->stroke_dash.dash[i];
-                p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
-            }
-            if (p < c + BMAX) {
-                *p++ = ';';
-            }
-        } else {
-            p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:none;");
-        }
-    }
-
-    /** \todo fixme: */
-    if (style->stroke_dashoffset_set) {
-        Inkscape::CSSOStringStream os;
-        os << "stroke-dashoffset:" << style->stroke_dash.offset << ";";
-        p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
-    } else if (flags == SP_STYLE_FLAG_ALWAYS) {
-        p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:0;");
-    }
-
-    p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &style->stroke_opacity, NULL, flags);
-
     p += sp_style_write_ienum(p, c + BMAX - p, "visibility", enum_visibility, &style->visibility, NULL, 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);
 
@@ -2212,60 +2493,85 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
     p += sp_style_write_ienum(p, c + BMAX - p, "writing-mode", enum_writing_mode, &from->writing_mode, &to->writing_mode, SP_STYLE_FLAG_IFDIFF);
 
     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &from->text_anchor, &to->text_anchor, SP_STYLE_FLAG_IFDIFF);
+    p += sp_style_write_ibaselineshift(p, c + BMAX - p, "baseline-shift", &from->baseline_shift, &to->baseline_shift, SP_STYLE_FLAG_IFDIFF);
 
     /// \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);
     }
-    p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
+
+    if (!from->color.noneSet) { // CSS does not permit "none" for color
+        p += sp_style_write_ipaint(p, c + BMAX - p, "color", &from->color, &to->color, SP_STYLE_FLAG_IFSET);
+    }
+
     p += sp_style_write_ipaint(p, c + BMAX - p, "fill", &from->fill, &to->fill, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
+    // if fill:none, skip writing fill properties
+    if (!from->fill.noneSet) {
+        p += sp_style_write_iscale24(p, c + BMAX - p, "fill-opacity", &from->fill_opacity, &to->fill_opacity, SP_STYLE_FLAG_IFDIFF);
+        p += sp_style_write_ienum(p, c + BMAX - p, "fill-rule", enum_fill_rule, &from->fill_rule, &to->fill_rule, SP_STYLE_FLAG_IFDIFF);
+    }
+
     p += sp_style_write_ipaint(p, c + BMAX - p, "stroke", &from->stroke, &to->stroke, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
-                              &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
-                              &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
-    p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
-                               &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
-    /** \todo fixme: */
-    if (from->stroke_dasharray_set) {
-        if (from->stroke_dasharray_inherit) {
-            p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
-        } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
-            p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
-            for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
-                Inkscape::CSSOStringStream os;
-                if (i) {
-                    os << ", ";
+
+    // stroke width affects markers, so write it if there's stroke OR any markers
+    if (!from->stroke.noneSet ||
+        from->marker[SP_MARKER_LOC].set ||
+        from->marker[SP_MARKER_LOC_START].set ||
+        from->marker[SP_MARKER_LOC_MID].set ||
+        from->marker[SP_MARKER_LOC_END].set) {
+        p += sp_style_write_ilength(p, c + BMAX - p, "stroke-width", &from->stroke_width, &to->stroke_width, SP_STYLE_FLAG_IFDIFF);
+    }
+
+    // if stroke:none, skip writing stroke properties
+    if (!from->stroke.noneSet) {
+        p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linecap", enum_stroke_linecap,
+                                  &from->stroke_linecap, &to->stroke_linecap, SP_STYLE_FLAG_IFDIFF);
+        p += sp_style_write_ienum(p, c + BMAX - p, "stroke-linejoin", enum_stroke_linejoin,
+                                  &from->stroke_linejoin, &to->stroke_linejoin, SP_STYLE_FLAG_IFDIFF);
+        p += sp_style_write_ifloat(p, c + BMAX - p, "stroke-miterlimit",
+                                   &from->stroke_miterlimit, &to->stroke_miterlimit, SP_STYLE_FLAG_IFDIFF);
+        /** \todo fixme: */
+        if (from->stroke_dasharray_set) {
+            if (from->stroke_dasharray_inherit) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:inherit;");
+            } else if (from->stroke_dash.n_dash && from->stroke_dash.dash) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dasharray:");
+                for (gint i = 0; i < from->stroke_dash.n_dash; i++) {
+                    Inkscape::CSSOStringStream os;
+                    if (i) {
+                        os << ", ";
+                    }
+                    os << from->stroke_dash.dash[i];
+                    p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
                 }
-                os << from->stroke_dash.dash[i];
+                p += g_snprintf(p, c + BMAX - p, ";");
+            }
+        }
+        /* fixme: */
+        if (from->stroke_dashoffset_set) {
+            if (from->stroke_dashoffset_inherit) {
+                p += g_snprintf(p, c + BMAX - p, "stroke-dashoffset:inherit;");
+            } else {
+                Inkscape::CSSOStringStream os;
+                os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
                 p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
             }
-            p += g_snprintf(p, c + BMAX - p, ";");
         }
+        p += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
     }
-    /* fixme: */
-    if (from->stroke_dashoffset_set) {
-        Inkscape::CSSOStringStream os;
-        os << "stroke-dashoffset:" << from->stroke_dash.offset << ";";
-        p += g_strlcpy(p, os.str().c_str(), c + BMAX - p);
-    }
-    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);
+    gchar *master = from->marker[SP_MARKER_LOC].value;
+    if (master != NULL) {
+        p += g_snprintf(p, c + BMAX - p, "marker:%s;", master);
     }
-    if (from->marker[SP_MARKER_LOC_START].value != NULL) {
+    if (from->marker[SP_MARKER_LOC_START].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_START].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-start:%s;", from->marker[SP_MARKER_LOC_START].value);
     }
-    if (from->marker[SP_MARKER_LOC_MID].value != NULL) {
+    if (from->marker[SP_MARKER_LOC_MID].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_MID].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-mid:%s;",   from->marker[SP_MARKER_LOC_MID].value);
     }
-    if (from->marker[SP_MARKER_LOC_END].value != NULL) {
+    if (from->marker[SP_MARKER_LOC_END].value != NULL && (!master || strcmp(master, from->marker[SP_MARKER_LOC_END].value))) {
         p += g_snprintf(p, c + BMAX - p, "marker-end:%s;",   from->marker[SP_MARKER_LOC_END].value);
     }
 
@@ -2273,6 +2579,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
@@ -2302,23 +2613,58 @@ 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);
+    style->fill.clear();
+    style->stroke.clear();
+    sp_style_filter_clear(style);
+
+    if (style->fill.value.href) {
+        delete style->fill.value.href;
+        style->fill.value.href = NULL;
+    }
+    if (style->stroke.value.href) {
+        delete style->stroke.value.href;
+        style->stroke.value.href = NULL;
+    }
+    if (style->filter.href) {
+        delete style->filter.href;
+        style->filter.href = NULL;
+    }
+
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
 
+    style->stroke_dasharray_inherit = FALSE;
+    style->stroke_dashoffset_inherit = FALSE;
+
     /** \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_specification.set = FALSE;
     style->text->font.set = FALSE;
     style->text->font_family.set = FALSE;
 
@@ -2365,6 +2711,13 @@ sp_style_clear(SPStyle *style)
     style->word_spacing.normal = TRUE;
     style->word_spacing.value = style->word_spacing.computed = 0.0;
 
+    style->baseline_shift.set = FALSE;
+    style->baseline_shift.type = SP_BASELINE_SHIFT_LITERAL;
+    style->baseline_shift.unit = SP_CSS_UNIT_NONE;
+    style->baseline_shift.literal = SP_CSS_BASELINE_SHIFT_BASELINE; 
+    style->baseline_shift.value = 0.0;
+    style->baseline_shift.computed = 0.0;
+
     style->text_transform.set = FALSE;
     style->text_transform.value = style->text_transform.computed = SP_CSS_TEXT_TRANSFORM_NONE;
 
@@ -2377,7 +2730,6 @@ sp_style_clear(SPStyle *style)
     style->writing_mode.set = FALSE;
     style->writing_mode.value = style->writing_mode.computed = SP_CSS_WRITING_MODE_LR_TB;
 
-
     style->text_anchor.set = FALSE;
     style->text_anchor.value = style->text_anchor.computed = SP_CSS_TEXT_ANCHOR_START;
 
@@ -2390,17 +2742,15 @@ sp_style_clear(SPStyle *style)
     style->overflow.set = FALSE;
     style->overflow.value = style->overflow.computed = SP_CSS_OVERFLOW_VISIBLE;
 
-    style->color.type = SP_PAINT_TYPE_COLOR;
-    sp_color_set_rgb_float(&style->color.value.color, 0.0, 0.0, 0.0);
+    style->color.clear();
+    style->color.setColor(0.0, 0.0, 0.0);
 
-    style->fill.type = SP_PAINT_TYPE_COLOR;
-    sp_color_set_rgb_float(&style->fill.value.color, 0.0, 0.0, 0.0);
+    style->fill.clear();
+    style->fill.setColor(0.0, 0.0, 0.0);
     style->fill_opacity.value = SP_SCALE24_MAX;
     style->fill_rule.value = style->fill_rule.computed = SP_WIND_RULE_NONZERO;
 
-    style->stroke.type = SP_PAINT_TYPE_NONE;
-    style->stroke.set = FALSE;
-    sp_color_set_rgb_float(&style->stroke.value.color, 0.0, 0.0, 0.0);
+    style->stroke.clear();
     style->stroke_opacity.value = SP_SCALE24_MAX;
 
     style->stroke_width.set = FALSE;
@@ -2423,6 +2773,10 @@ 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;
 }
 
 
@@ -2496,8 +2850,9 @@ sp_text_style_new()
     ts->refcount = 1;
     sp_text_style_clear(ts);
 
-    ts->font.value = g_strdup("Bitstream Vera Sans");
-    ts->font_family.value = g_strdup("Bitstream Vera Sans");
+    ts->font_specification.value = g_strdup("Sans");
+    ts->font.value = g_strdup("Sans");
+    ts->font_family.value = g_strdup("Sans");
 
     return ts;
 }
@@ -2509,6 +2864,7 @@ sp_text_style_new()
 static void
 sp_text_style_clear(SPTextStyle *ts)
 {
+    ts->font_specification.set = FALSE;
     ts->font.set = FALSE;
     ts->font_family.set = FALSE;
 }
@@ -2524,6 +2880,7 @@ sp_text_style_unref(SPTextStyle *st)
     st->refcount -= 1;
 
     if (st->refcount < 1) {
+        g_free(st->font_specification.value);
         g_free(st->font.value);
         g_free(st->font_family.value);
         g_free(st);
@@ -2543,6 +2900,7 @@ sp_text_style_duplicate_unset(SPTextStyle *st)
     SPTextStyle *nt = g_new0(SPTextStyle, 1);
     nt->refcount = 1;
 
+    nt->font_specification.value = g_strdup(st->font_specification.value);
     nt->font.value = g_strdup(st->font.value);
     nt->font_family.value = g_strdup(st->font_family.value);
 
@@ -2564,6 +2922,7 @@ sp_text_style_write(gchar *p, guint const len, SPTextStyle const *const st, guin
         flags = SP_STYLE_FLAG_IFSET;
 
     d += sp_style_write_istring(p + d, len - d, "font-family", &st->font_family, NULL, flags);
+    d += sp_style_write_istring(p + d, len - d, "-inkscape-font-specification", &st->font_specification, NULL, flags);
     return d;
 }
 
@@ -2614,7 +2973,7 @@ sp_style_read_iscale24(SPIScale24 *val, gchar const *str)
         if (sp_svg_number_read_f(str, &value)) {
             val->set = TRUE;
             val->inherit = FALSE;
-            value = CLAMP(value, 0.0f, (gfloat) SP_SCALE24_MAX);
+            value = CLAMP(value, 0.0, 1.0);
             val->value = SP_SCALE24_FROM_FLOAT(value);
         }
     }
@@ -2816,6 +3175,8 @@ sp_style_read_itextdecoration(SPITextDecoration *val, gchar const *str)
 static void
 sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
 {
+    (void)style; // TODO
+    (void)document; // TODO
     paint->currentcolor = FALSE;  /* currentColor not a valid <color>. */
     if (!strcmp(str, "inherit")) {
         paint->set = TRUE;
@@ -2823,8 +3184,7 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
     } else {
         guint32 const rgb0 = sp_svg_read_color(str, 0xff);
         if (rgb0 != 0xff) {
-            paint->type = SP_PAINT_TYPE_COLOR;
-            sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
+            paint->setColor(rgb0);
             paint->set = TRUE;
             paint->inherit = FALSE;
         }
@@ -2837,76 +3197,65 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
  *
  * \pre paint == \&style.fill || paint == \&style.stroke.
  */
-static void
-sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
+void SPIPaint::read( gchar const *str, SPStyle &style, SPDocument *document )
 {
-    while (isspace(*str)) {
+    while (g_ascii_isspace(*str)) {
         ++str;
     }
 
+    clear();
+
     if (streq(str, "inherit")) {
-        paint->set = TRUE;
-        paint->inherit = TRUE;
-        paint->currentcolor = FALSE;
-    } else if (streq(str, "currentColor") && paint != &style->color) {
-        paint->set = TRUE;
-        paint->inherit = FALSE;
-        paint->currentcolor = TRUE;
-    } 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) && paint != &style->color) {
-        // this is alloc'd uri, but seems to be shared with a parent
-        // potentially, so it's not any easy g_free case...
-        paint->value.paint.uri = extract_uri(str);
-        if (paint->value.paint.uri == NULL || *(paint->value.paint.uri) == '\0') {
-            paint->type = SP_PAINT_TYPE_NONE;
-            return;
+        set = TRUE;
+        inherit = TRUE;
+    } else {
+        if ( strneq(str, "url", 3) ) {
+            gchar *uri = extract_uri( str, &str );
+            while ( g_ascii_isspace(*str) ) {
+                ++str;
+            }
+            // TODO check on and comment the comparrison "paint != &style->color".
+            if ( uri && *uri && (this != &style.color) ) {
+                set = TRUE;
+
+                // 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 (!value.href && document) {
+                    value.href = new SPPaintServerReference(document);
+                    value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun((this == &style.fill)? sp_style_fill_paint_server_ref_changed : sp_style_stroke_paint_server_ref_changed), &style));
+                }
+
+                // TODO check what this does in light of move away from union
+                sp_style_set_ipaint_to_uri_string (&style, this, uri);
+            }
+            g_free( uri );
         }
-        paint->type = SP_PAINT_TYPE_PAINTSERVER;
-        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);
-                    if (paint == &style->fill) {
-                        style->fill_hreffed = true;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_hreffed = true;
-                    }
+
+        if (streq(str, "currentColor") && (this != &style.color)) {
+            set = TRUE;
+            currentcolor = TRUE;
+        } else if (streq(str, "none") && (this != &style.color)) {
+            set = TRUE;
+            noneSet = TRUE;
+        } else {
+            guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
+            if (rgb0 != 0xff) {
+                setColor( rgb0 );
+                set = TRUE;
+
+                while (g_ascii_isspace(*str)) {
+                    ++str;
                 }
-                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);
-                    if (paint == &style->fill) {
-                        style->fill_listening = true;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_listening = true;
+                if (strneq(str, "icc-color(", 10)) {
+                    SVGICCColor* tmp = new SVGICCColor();
+                    if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                        delete tmp;
+                        tmp = 0;
                     }
+                    value.color.icc = tmp;
                 }
-            } else {
-                paint->value.paint.server = NULL;
             }
         }
-    } else {
-        guint32 const rgb0 = sp_svg_read_color(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;
-        }
     }
 }
 
@@ -2922,6 +3271,7 @@ sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
         val->set = TRUE;
         val->inherit = TRUE;
     } else if ((*str == 'x') || (*str == 's') || (*str == 'm') || (*str == 'l')) {
+        // xx-small, x-small, etc.
         for (unsigned i = 0; enum_font_size[i].key; i++) {
             if (!strcmp(str, enum_font_size[i].key)) {
                 val->set = TRUE;
@@ -2977,6 +3327,100 @@ sp_style_read_ifontsize(SPIFontSize *val, gchar const *str)
 }
 
 
+/**
+ * Set SPIBaselineShift object from string.
+ */
+static void
+sp_style_read_ibaselineshift(SPIBaselineShift *val, gchar const *str)
+{
+    if (!strcmp(str, "inherit")) {
+        val->set = TRUE;
+        val->inherit = TRUE;
+    } else if ((*str == 'b') || (*str == 's')) {
+        // baseline or sub or super
+        for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
+            if (!strcmp(str, enum_baseline_shift[i].key)) {
+                val->set = TRUE;
+                val->inherit = FALSE;
+                val->type = SP_BASELINE_SHIFT_LITERAL;
+                val->literal = enum_baseline_shift[i].value;
+                return;
+            }
+        }
+        /* Invalid */
+        return;
+    } else {
+        SPILength length;
+        sp_style_read_ilength(&length, str);
+        val->set      = length.set;
+        val->inherit  = length.inherit;
+        val->unit     = length.unit;
+        val->value    = length.value;
+        val->computed = length.computed;
+        if( val->unit == SP_CSS_UNIT_PERCENT ) {
+            val->type = SP_BASELINE_SHIFT_PERCENTAGE;
+        } else {
+            val->type = SP_BASELINE_SHIFT_LENGTH;
+        }
+        return;
+    }
+}
+
+
+/**
+ * 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.
@@ -3019,6 +3463,19 @@ sp_style_read_pfontsize(SPIFontSize *val, Inkscape::XML::Node *repr, gchar const
 }
 
 
+/**
+ * Set SPIBaselineShift object from repr attribute.
+ */
+static void
+sp_style_read_pbaselineshift(SPIBaselineShift *val, Inkscape::XML::Node *repr, gchar const *key)
+{
+    gchar const *str = repr->attribute(key);
+    if (str) {
+        sp_style_read_ibaselineshift(val, str);
+    }
+}
+
+
 /**
  * Set SPIFloat object from repr attribute.
  */
@@ -3125,7 +3582,11 @@ 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);
+            if (val_quoted) {
+                return g_snprintf(p, len, "%s:%s;", key, val_quoted);
+                g_free (val_quoted);
+            }
         }
     }
     return 0;
@@ -3322,12 +3783,30 @@ sp_style_write_itextdecoration(gchar *p, gint const len, gchar const *const key,
 static bool
 sp_paint_differ(SPIPaint const *const a, SPIPaint const *const b)
 {
-    if (a->type != b->type)
+    if ( (a->isColor() != b->isColor())
+         || (a->isPaintserver() != b->isPaintserver())
+         || (a->set != b->set)
+         || (a->currentcolor != b->currentcolor)
+         || (a->inherit!= b->inherit) ) {
         return true;
-    if (a->type == SP_PAINT_TYPE_COLOR)
-        return !sp_color_is_equal(&a->value.color, &b->value.color);
-    if (a->type == SP_PAINT_TYPE_PAINTSERVER)
-        return (a->value.paint.server != b->value.paint.server);
+    }
+
+    // TODO refactor to allow for mixed paints (rgb() *and* url(), etc)
+
+    if ( a->isPaintserver() ) {
+        return (a->value.href == NULL || b->value.href == NULL || a->value.href->getObject() != b->value.href->getObject());
+    }
+
+    if ( a->isColor() ) {
+        return !( (a->value.color == b->value.color)
+                 && ((a->value.color.icc == b->value.color.icc)
+                     || (a->value.color.icc && b->value.color.icc
+                         && (a->value.color.icc->colorProfile == b->value.color.icc->colorProfile)
+                         && (a->value.color.icc->colors == b->value.color.icc->colors))));
+    /* todo: Allow for epsilon differences in iccColor->colors, e.g. changes small enough not to show up
+     * in the string representation. */
+    }
+
     return false;
 }
 
@@ -3340,31 +3819,66 @@ static gint
 sp_style_write_ipaint(gchar *b, gint const len, gchar const *const key,
                       SPIPaint const *const paint, SPIPaint const *const base, guint const flags)
 {
+    int retval = 0;
+
     if ((flags & SP_STYLE_FLAG_ALWAYS)
         || ((flags & SP_STYLE_FLAG_IFSET) && paint->set)
         || ((flags & SP_STYLE_FLAG_IFDIFF) && paint->set
             && (!base->set || sp_paint_differ(paint, base))))
     {
+        CSSOStringStream css;
+
         if (paint->inherit) {
-            return g_snprintf(b, len, "%s:inherit;", key);
-        } else if (paint->currentcolor) {
-            return g_snprintf(b, len, "%s:currentColor;", key);
+            css << "inherit";
         } else {
-            switch (paint->type) {
-                case SP_PAINT_TYPE_COLOR: {
-                    char color_buf[8];
-                    sp_svg_write_color(color_buf, sizeof(color_buf), sp_color_get_rgba32_ualpha(&paint->value.color, 0));
-                    return g_snprintf(b, len, "%s:%s;", key, color_buf);
+            if ( paint->value.href && paint->value.href->getURI() ) {
+                const gchar* uri = paint->value.href->getURI()->toString();
+                css << "url(" << uri << ")";
+            }
+
+            if ( paint->noneSet ) {
+                if ( !css.str().empty() ) {
+                    css << " ";
                 }
-                case SP_PAINT_TYPE_PAINTSERVER:
-                    return g_snprintf(b, len, "%s:url(%s);", key, paint->value.paint.uri);
-                default:
-                    break;
+                css << "none";
+            }
+
+            if ( paint->currentcolor ) {
+                if ( !css.str().empty() ) {
+                    css << " ";
+                }
+                css << "currentColor";
+            }
+
+            if ( paint->colorSet && !paint->currentcolor ) {
+                if ( !css.str().empty() ) {
+                    css << " ";
+                }
+                char color_buf[8];
+                sp_svg_write_color(color_buf, sizeof(color_buf), paint->value.color.toRGBA32( 0 ));
+                css << color_buf;
+            }
+
+            if (paint->value.color.icc && !paint->currentcolor) {
+                if ( !css.str().empty() ) {
+                    css << " ";
+                }
+                css << "icc-color(" << paint->value.color.icc->colorProfile;
+                for (vector<double>::const_iterator i(paint->value.color.icc->colors.begin()),
+                         iEnd(paint->value.color.icc->colors.end());
+                     i != iEnd; ++i) {
+                    css << ", " << *i;
+                }
+                css << ')';
             }
-            return g_snprintf(b, len, "%s:none;", key);
+        }
+
+        if ( !css.str().empty() ) {
+            retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
         }
     }
-    return 0;
+
+    return retval;
 }
 
 
@@ -3422,48 +3936,156 @@ sp_style_write_ifontsize(gchar *p, gint const len, gchar const *key,
 }
 
 
+/*
+ * baseline-shift is relative to parent. The only time it should
+ * not be written out is if it is zero (or not set).
+ */
+static bool
+sp_baseline_shift_notzero(SPIBaselineShift const *const a )
+{
+    if( a->type == SP_BASELINE_SHIFT_LITERAL ) {
+        if( a->literal == SP_CSS_BASELINE_SHIFT_BASELINE ) {
+            return false;
+        }
+    } else {
+        if( a->value == 0.0 ) {
+            return false;
+        }
+    }
+    return true;
+}
+
 /**
- * Clear paint object; conditionally disconnect style from paintserver.
+ * Write SPIBaselineShift object into string.
  */
-static void
-sp_style_paint_clear(SPStyle *style, SPIPaint *paint,
-                     unsigned hunref, unsigned unset)
-{
-    if (hunref && (paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
-        if (paint == &style->fill) {
-            if (style->fill_hreffed) {
-                sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
-                style->fill_hreffed = false;
-            }
-            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;
-            }
-        } 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;
+static gint
+sp_style_write_ibaselineshift(gchar *p, gint const len, gchar const *key,
+                              SPIBaselineShift const *const val, SPIBaselineShift 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
+            && (!base->set || sp_baseline_shift_notzero(val) )))
+    {
+        if (val->inherit) {
+            return g_snprintf(p, len, "%s:inherit;", key);
+        } else if (val->type == SP_BASELINE_SHIFT_LITERAL) {
+            for (unsigned i = 0; enum_baseline_shift[i].key; i++) {
+                if (enum_baseline_shift[i].value == static_cast< gint > (val->literal) ) {
+                    return g_snprintf(p, len, "%s:%s;", key, enum_baseline_shift[i].key);
+                }
             }
-            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;
+        } else if (val->type == SP_BASELINE_SHIFT_LENGTH) {
+            if( val->unit == SP_CSS_UNIT_EM || val->unit == SP_CSS_UNIT_EX ) {
+                Inkscape::CSSOStringStream os;
+                os << key << ":" << val->value << (val->unit == SP_CSS_UNIT_EM ? "em;" : "ex;");
+                return g_strlcpy(p, os.str().c_str(), len);
+            } else {
+                Inkscape::CSSOStringStream os;
+                os << key << ":" << val->computed << "px;";      // must specify px, see inkscape bug 1221626, mozilla bug 234789
+                return g_strlcpy(p, os.str().c_str(), len);
             }
+        } else if (val->type == SP_BASELINE_SHIFT_PERCENTAGE) {
+            Inkscape::CSSOStringStream os;
+            os << key << ":" << (val->value * 100.0) << "%;";
+            return g_strlcpy(p, os.str().c_str(), len);
         }
+    }
+    return 0;
+}
+
+
 
-        paint->value.paint.server = NULL;
-        paint->value.paint.uri = NULL;
-        paint->type = SP_PAINT_TYPE_NONE;
+/**
+ * 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)
+{
+    (void)base; // TODO
+    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());
+        }
+    }
+
+
+    return 0;
+}
+
+SPIPaint::SPIPaint() :
+    set(0),
+    inherit(0),
+    currentcolor(0),
+    colorSet(0),
+    noneSet(0),
+    value()
+{
+    value.color.set( 0 );
+    value.href = 0;
+}
+
+void SPIPaint::clear()
+{
+    set = false;
+    inherit = false;
+    currentcolor = false;
+    colorSet = false;
+    noneSet = false;
+    value.color.set( 0 );
+    if ( value.href && value.href->getObject() )
+    {
+        value.href->detach();
+    }
+}
+
+
+/**
+ * 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;
+
+    SPCSSAttr *css = sp_repr_css_attr_new();
+    if (linked) {
+        gchar *val = g_strdup_printf("url(#%s)", linked->getId());
+        sp_repr_css_set_property(css, property, val);
+        g_free(val);
+    } else {
+        sp_repr_css_unset_property(css, "filter");
     }
 
-    if (unset) {
-        paint->set = FALSE;
-        paint->inherit = FALSE;
+    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.
  */
@@ -3526,6 +4148,9 @@ sp_style_unset_property_attrs(SPObject *o)
     if (style->stroke_dashoffset_set) {
         repr->setAttribute("stroke-dashoffset", NULL);
     }
+    if (style->text_private && style->text->font_specification.set) {
+        repr->setAttribute("-inkscape-font-specification", NULL);
+    }
     if (style->text_private && style->text->font_family.set) {
         repr->setAttribute("font-family", NULL);
     }
@@ -3535,6 +4160,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);
+    }
 }
 
 /**
@@ -3579,6 +4210,7 @@ SPCSSAttr *
 sp_css_attr_unset_text(SPCSSAttr *css)
 {
     sp_repr_css_set_property(css, "font", NULL); // not implemented yet
+    sp_repr_css_set_property(css, "-inkscape-font-specification", NULL);
     sp_repr_css_set_property(css, "font-size", NULL);
     sp_repr_css_set_property(css, "font-size-adjust", NULL); // not implemented yet
     sp_repr_css_set_property(css, "font-style", NULL);
@@ -3600,7 +4232,7 @@ sp_css_attr_unset_text(SPCSSAttr *css)
     sp_repr_css_set_property(css, "kerning", NULL); // not implemented yet
     sp_repr_css_set_property(css, "dominant-baseline", NULL); // not implemented yet
     sp_repr_css_set_property(css, "alignment-baseline", NULL); // not implemented yet
-    sp_repr_css_set_property(css, "baseline-shift", NULL); // not implemented yet
+    sp_repr_css_set_property(css, "baseline-shift", NULL);
 
     return css;
 }
@@ -3631,6 +4263,7 @@ sp_css_attr_unset_uris(SPCSSAttr *css)
     if (is_url(sp_repr_css_property(css, "color-profile", NULL))) sp_repr_css_set_property(css, "color-profile", NULL);
     if (is_url(sp_repr_css_property(css, "cursor", NULL))) sp_repr_css_set_property(css, "cursor", NULL);
     if (is_url(sp_repr_css_property(css, "filter", NULL))) sp_repr_css_set_property(css, "filter", NULL);
+    if (is_url(sp_repr_css_property(css, "marker", NULL))) sp_repr_css_set_property(css, "marker", NULL);
     if (is_url(sp_repr_css_property(css, "marker-start", NULL))) sp_repr_css_set_property(css, "marker-start", NULL);
     if (is_url(sp_repr_css_property(css, "marker-mid", NULL))) sp_repr_css_set_property(css, "marker-mid", NULL);
     if (is_url(sp_repr_css_property(css, "marker-end", NULL))) sp_repr_css_set_property(css, "marker-end", NULL);
@@ -3718,28 +4351,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.)
  */
-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;
+    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:
@@ -3750,4 +4436,4 @@ css2_unescape_unquote(SPIString *val)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :