Code

whitespace
[inkscape.git] / src / style.cpp
index e15c54636adf10b496766cfd5eebe0431e27ff93..357fe5f0e2a81dd95eb3003a4468036699405142 100644 (file)
@@ -31,6 +31,7 @@
 #include "document.h"
 #include "extract-uri.h"
 #include "uri-references.h"
+#include "uri.h"
 #include "sp-paint-server.h"
 #include "streq.h"
 #include "strneq.h"
@@ -39,6 +40,9 @@
 #include "xml/repr.h"
 #include "unit-constants.h"
 #include "isnan.h"
+#include "macros.h"
+
+#include "sp-filter-reference.h"
 
 #include <sigc++/functors/ptr_fun.h>
 #include <sigc++/adaptors/bind.h>
 using Inkscape::CSSOStringStream;
 using std::vector;
 
-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);
-        }
-    }
-
-    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);
-    }
-
-    return result;
-}
-
-}
-
 #define BMAX 8192
 
 class SPStyleEnum;
@@ -155,8 +100,6 @@ static gint sp_style_write_ilengthornormal(gchar *p, gint const len, gchar const
 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 sp_style_paint_clear(SPStyle *style, SPIPaint *paint);
-
 static void sp_style_filter_clear(SPStyle *style);
 
 #define SPS_READ_IENUM_IF_UNSET(v,s,d,i) if (!(v)->set) {sp_style_read_ienum((v), (s), (d), (i));}
@@ -383,22 +326,125 @@ static SPStyleEnum const enum_enable_background[] = {
 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;
 
@@ -406,20 +452,10 @@ sp_style_new()
 
     style->cloned = false;
 
-    style->fill_hreffed = false;
-    style->stroke_hreffed = false;
-    style->filter_hreffed = false;
-
     new (&style->release_connection) sigc::connection();
-
-    new (&style->fill_release_connection) sigc::connection();
-    new (&style->fill_modified_connection) sigc::connection();
-
-    new (&style->stroke_release_connection) sigc::connection();
-    new (&style->stroke_modified_connection) sigc::connection();
-
-    new (&style->filter_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;
 }
@@ -434,7 +470,7 @@ sp_style_new_from_object(SPObject *object)
     g_return_val_if_fail(object != NULL, NULL);
     g_return_val_if_fail(SP_IS_OBJECT(object), NULL);
 
-    SPStyle *style = sp_style_new();
+    SPStyle *style = sp_style_new(SP_OBJECT_DOCUMENT(object));
     style->object = object;
     style->release_connection = object->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_object_release), style));
 
@@ -476,21 +512,31 @@ sp_style_unref(SPStyle *style)
         style->release_connection.disconnect();
         style->release_connection.~connection();
         if (style->text) sp_text_style_unref(style->text);
-        sp_style_paint_clear(style, &style->fill);
-        sp_style_paint_clear(style, &style->stroke);
-        sp_style_filter_clear(style);
-        style->fill_release_connection.disconnect();
-        style->fill_release_connection.~connection();
-        style->fill_modified_connection.disconnect();
-        style->fill_modified_connection.~connection();
-        style->stroke_release_connection.disconnect();
-        style->stroke_release_connection.~connection();
-        style->stroke_modified_connection.disconnect();
-        style->stroke_modified_connection.~connection();
-        style->filter_modified_connection.disconnect();
+
+         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->filter_release_connection.disconnect();
-        style->filter_release_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);
     }
@@ -669,6 +715,17 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
             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) {
@@ -685,7 +742,7 @@ sp_style_read(SPStyle *style, SPObject *object, Inkscape::XML::Node *repr)
     if (!style->filter.set) {
         val = repr->attribute("filter");
         if (val) {
-               sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
+                     sp_style_read_ifilter(val, style, (object) ? SP_OBJECT_DOCUMENT(object) : NULL);
         }
     }
     SPS_READ_PENUM_IF_UNSET(&style->enable_background, repr,
@@ -699,7 +756,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);
@@ -771,6 +828,14 @@ 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:
@@ -1454,6 +1519,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) {
@@ -1839,10 +1911,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. */
     {
         /*
@@ -2003,151 +2079,68 @@ sp_style_merge_from_dying_parent(SPStyle *const style, SPStyle const *const pare
 }
 
 
-
-/**
- * Disconnects from possible fill and stroke paint servers.
- */
-static void
-sp_style_paint_server_release(SPObject *obj, SPStyle *style)
-{
-    SPPaintServer *server=static_cast<SPPaintServer *>(obj);
-    if ((style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->fill.value.paint.server))
-    {
-        sp_style_paint_clear(style, &style->fill);
-    }
-
-    if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
-        && (server == style->stroke.value.paint.server))
-    {
-        sp_style_paint_clear(style, &style->stroke);
-    }
-}
-
-
-
-
-/**
- * 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(SPObject *obj, guint flags, SPStyle *style)
+sp_style_set_ipaint_to_uri(SPStyle *style, SPIPaint *paint, const Inkscape::URI *uri, SPDocument *document)
 {
-    SPPaintServer *server = static_cast<SPPaintServer *>(obj);
-    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);
+    // 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();
         }
-    } else if ((style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)
-               && (server == style->stroke.value.paint.server))
-    {
-        if (style->object) {
-            /// \todo fixme:
-            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
-        }
-    } else {
-        g_assert_not_reached();
     }
 }
 
-
-
-/**
- * Disconnects from filter.
- */
 static void
-sp_style_filter_release(SPObject *obj, SPStyle *style)
+sp_style_set_ipaint_to_uri_string (SPStyle *style, SPIPaint *paint, const gchar *uri)
 {
-    SPFilter *filter=static_cast<SPFilter *>(obj);
-    if (style->filter.filter == filter)
-    {
-        sp_style_filter_clear(style);
-    }
+    const Inkscape::URI IURI(uri);
+    sp_style_set_ipaint_to_uri(style, paint, &IURI, style->document);
 }
 
-
-/**
- * Emit style modified signal on style's object if the filter changed.
- */
-static void
-sp_style_filter_modified(SPObject *obj, guint flags, SPStyle *style)
+void
+sp_style_set_to_uri_string (SPStyle *style, bool isfill, const gchar *uri)
 {
-    SPFilter *filter=static_cast<SPFilter *>(obj);
-    if (style->filter.filter == filter)
-    {
-        if (style->object) {
-            style->object->requestModified(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
-        }
-    }
+    sp_style_set_ipaint_to_uri_string (style, isfill? &style->fill : &style->stroke, uri);
 }
 
-
 /**
  *
  */
 static void
 sp_style_merge_ipaint(SPStyle *style, SPIPaint *paint, SPIPaint const *parent)
 {
-    sp_style_paint_clear(style, paint);
-
     if ((paint->set && paint->currentcolor) || parent->currentcolor) {
+        paint->clear();
         paint->currentcolor = TRUE;
-        paint->type = SP_PAINT_TYPE_COLOR;
-        sp_color_copy(&paint->value.color, &style->color.value.color);
+        paint->setColor(style->color.value.color);
         return;
     }
 
-    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)
-                    SPObject *server = paint->value.paint.server;
-                    sigc::connection release_connection
-                      = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
-                    sigc::connection modified_connection
-                      = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
-                    if (paint == &style->fill) {
-                        style->fill_release_connection = release_connection;
-                        style->fill_modified_connection = modified_connection;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_release_connection = release_connection;
-                        style->stroke_modified_connection = modified_connection;
-                    }
-                }
-            }
-            break;
-        case SP_PAINT_TYPE_NONE:
-            break;
-        default:
-            g_assert_not_reached();
-            break;
+    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->isNone() ) {
+        //
+    } else {
+        g_assert_not_reached();
     }
 }
 
@@ -2162,19 +2155,16 @@ sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
     sp_style_filter_clear(style);
     style->filter.set = parent->set;
     style->filter.inherit = parent->inherit;
-    style->filter.filter = parent->filter;
-    style->filter.uri = parent->uri;
-    if (style->filter.filter) {
-        if (style->object && !style->cloned) { // href filter for style of non-clones only
-            sp_object_href(SP_OBJECT(style->filter.filter), style);
-            style->filter_hreffed = true;
-        }
-        if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
-            SPObject *filter = style->filter.filter;
-            style->filter_release_connection
-                = filter->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_filter_release), style));
-            style->filter_modified_connection
-                = filter->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_filter_modified), style));
+
+    if (style->filter.href && style->filter.href->getObject())
+       style->filter.href->detach();
+
+    if (style->filter.href && parent->href) {
+        try {
+            style->filter.href->attach(*parent->href->getURI());
+        } catch (Inkscape::BadURIException &e) {
+            g_warning("%s", e.what());
+            style->filter.href->detach();
         }
     }
 }
@@ -2451,24 +2441,49 @@ sp_style_clear(SPStyle *style)
 {
     g_return_if_fail(style != NULL);
 
-    sp_style_paint_clear(style, &style->fill);
-    sp_style_paint_clear(style, &style->stroke);
+    style->fill.clear();
+    style->stroke.clear();
     sp_style_filter_clear(style);
+
+    if (style->fill.value.href)
+        delete style->fill.value.href;
+    if (style->stroke.value.href)
+        delete style->stroke.value.href;
+    if (style->filter.href)
+        delete style->filter.href;
+
     if (style->stroke_dash.dash) {
         g_free(style->stroke_dash.dash);
     }
 
     /** \todo fixme: Do that text manipulation via parents */
     SPObject *object = style->object;
+    SPDocument *document = style->document;
     gint const refcount = style->refcount;
     SPTextStyle *text = style->text;
     unsigned const text_private = style->text_private;
+
     memset(style, 0, sizeof(SPStyle));
+
     style->refcount = refcount;
     style->object = object;
+    style->document = document;
+
+    if (document) {
+        style->filter.href = new SPFilterReference(document);
+        style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
+
+        style->fill.value.href = new SPPaintServerReference(document);
+        style->fill.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_fill_paint_server_ref_changed), style));
+
+        style->stroke.value.href = new SPPaintServerReference(document);
+        style->stroke.value.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_stroke_paint_server_ref_changed), style));
+    }
+
     style->text = text;
     style->text_private = text_private;
-    /* fixme: */
+
+    style->text->font_specification.set = FALSE;
     style->text->font.set = FALSE;
     style->text->font_family.set = FALSE;
 
@@ -2540,17 +2555,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;
@@ -2574,10 +2587,6 @@ sp_style_clear(SPStyle *style)
         style->marker[i].set = FALSE;
     }
 
-    style->filter.set = FALSE;
-    style->filter.uri = NULL;
-    style->filter.filter = NULL;
-
     style->enable_background.value = SP_CSS_BACKGROUND_ACCUMULATE;
     style->enable_background.set = false;
     style->enable_background.inherit = false;
@@ -2654,6 +2663,7 @@ sp_text_style_new()
     ts->refcount = 1;
     sp_text_style_clear(ts);
 
+    ts->font_specification.value = g_strdup("Bitstream Vera Sans");
     ts->font.value = g_strdup("Bitstream Vera Sans");
     ts->font_family.value = g_strdup("Bitstream Vera Sans");
 
@@ -2667,6 +2677,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;
 }
@@ -2682,6 +2693,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);
@@ -2701,6 +2713,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);
 
@@ -2722,6 +2735,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;
 }
 
@@ -2772,7 +2786,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);
         }
     }
@@ -2974,6 +2988,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;
@@ -2981,9 +2997,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->set = TRUE;
+            paint->setColor(rgb0);
             paint->inherit = FALSE;
         }
     }
@@ -2998,86 +3012,61 @@ sp_style_read_icolor(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocume
 static void
 sp_style_read_ipaint(SPIPaint *paint, gchar const *str, SPStyle *style, SPDocument *document)
 {
-    while (isspace(*str)) {
+    while (g_ascii_isspace(*str)) {
         ++str;
     }
 
+    paint->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;
-        }
-        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 (style->object || style->cloned) {
-                    SPObject *server = paint->value.paint.server;
-                    sigc::connection release_connection
-                      = server->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_paint_server_release), style));
-                    sigc::connection modified_connection
-                      = server->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_paint_server_modified), style));
-                    if (paint == &style->fill) {
-                        style->fill_release_connection = release_connection;
-                        style->fill_modified_connection = modified_connection;
-                    } else {
-                        assert(paint == &style->stroke);
-                        style->stroke_release_connection = release_connection;
-                        style->stroke_modified_connection = modified_connection;
-                    }
+    } else {
+        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 && (paint != &style->color) ) {
+                paint->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 (!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));
                 }
-            } else {
-                paint->value.paint.server = NULL;
+
+                // TODO check what this does in light of move away from union
+                sp_style_set_ipaint_to_uri_string (style, paint, uri);
             }
+            g_free( uri );
         }
-    } else {
-        guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
-        if (rgb0 != 0xff) {
-            paint->type = SP_PAINT_TYPE_COLOR;
-            sp_color_set_rgb_rgba32(&paint->value.color, rgb0);
+
+        if (streq(str, "currentColor") && paint != &style->color) {
             paint->set = TRUE;
-            paint->inherit = FALSE;
-            paint->currentcolor = FALSE;
+            paint->currentcolor = TRUE;
+        } else if (streq(str, "none") && paint != &style->color) {
+            paint->set = TRUE;
+            paint->noneSet = TRUE;
+        } else {
+            guint32 const rgb0 = sp_svg_read_color(str, &str, 0xff);
+            if (rgb0 != 0xff) {
+                paint->setColor( rgb0 );
+                paint->set = TRUE;
 
-            while (g_ascii_isspace(*str)) {
-                ++str;
-            }
-            if (strneq(str, "icc-color(", 10)) {
-                SVGICCColor* tmp = new SVGICCColor();
-                if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
-                    delete tmp;
-                    tmp = 0;
+                while (g_ascii_isspace(*str)) {
+                    ++str;
+                }
+                if (strneq(str, "icc-color(", 10)) {
+                    SVGICCColor* tmp = new SVGICCColor();
+                    if ( ! sp_svg_read_icc_color( str, &str, tmp ) ) {
+                        delete tmp;
+                        tmp = 0;
+                    }
+                    paint->value.color.icc = tmp;
                 }
-                paint->value.iccColor = tmp;
             }
         }
     }
@@ -3162,49 +3151,47 @@ sp_style_read_ifilter(gchar const *str, SPStyle * style, SPDocument *document)
     if (streq(str, "inherit")) {
         f->set = TRUE;
         f->inherit = TRUE;
-        f->filter = NULL;
+        if (f->href && f->href->getObject())
+            f->href->detach(); 
     } else if(streq(str, "none")) {
         f->set = TRUE;
         f->inherit = FALSE;
-        f->filter = NULL;
+        if (f->href && f->href->getObject())
+           f->href->detach(); 
     } else if (strneq(str, "url", 3)) {
-        f->uri = extract_uri(str);
-        if(f->uri == NULL || f->uri[0] == '\0') {
+        char *uri = extract_uri(str);
+        if(uri == NULL || uri[0] == '\0') {
             g_warning("Specified filter url is empty");
             f->set = TRUE;
             f->inherit = FALSE;
-            f->filter = NULL;
             return;
         }
         f->set = TRUE;
         f->inherit = FALSE;
-        f->filter = NULL;
-        if (document) {
-            SPObject *obj = sp_uri_reference_resolve(document, str);
-            if (SP_IS_FILTER(obj)) {
-                f->filter = SP_FILTER(obj);
-                if (style->object && !style->cloned) {
-                    sp_object_href(obj, style);
-                    style->filter_hreffed = true;
-                }
-                if (style->object || style->cloned) { // connect to signals for style of real objects or clones (this excludes temp styles)
-                    SPObject *filter = style->filter.filter;
-                    style->filter_release_connection
-                        = filter->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_style_filter_release), style));
-                    style->filter_modified_connection
-                        = filter->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_style_filter_modified), style));
-                }
-            } else {
-                g_warning("Element '%s' not found or is not a filter", f->uri);
-            }
+        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;
-        f->filter = NULL;
-        f->uri = NULL;
+        if (f->href && f->href->getObject())
+            f->href->detach(); 
     }
 }
 
@@ -3554,18 +3541,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)
-                 && ((a->value.iccColor == b->value.iccColor)
-                     || (a->value.iccColor && b->value.iccColor
-                         && (a->value.iccColor->colorProfile == b->value.iccColor->colorProfile)
-                         && (a->value.iccColor->colors == b->value.iccColor->colors))));
+    }
+
+    // TODO 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. */
-    if (a->type == SP_PAINT_TYPE_PAINTSERVER)
-        return (a->value.paint.server != b->value.paint.server);
+    }
+
     return false;
 }
 
@@ -3578,43 +3577,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));
-                    if (paint->value.iccColor) {
-                        CSSOStringStream css;
-                        css << color_buf << " icc-color(" << paint->value.iccColor->colorProfile;
-                        for (vector<double>::const_iterator i(paint->value.iccColor->colors.begin()),
-                                 iEnd(paint->value.iccColor->colors.end());
-                             i != iEnd; ++i) {
-                            css << ", " << *i;
-                        }
-                        css << ')';
-                        return g_snprintf(b, len, "%s:%s;", key, css.gcharp());
-                    } else {
-                        return g_snprintf(b, len, "%s:%s;", key, color_buf);
-                    }
+            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";
             }
-            return g_snprintf(b, len, "%s:none;", key);
+
+            if ( paint->colorSet ) {
+                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) {
+                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 << ')';
+            }
+        }
+
+        if ( !css.str().empty() ) {
+            retval = g_snprintf( b, len, "%s:%s;", key, css.str().c_str() );
         }
     }
-    return 0;
+
+    return retval;
 }
 
 
@@ -3680,14 +3702,15 @@ 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->uri) {
-            return g_snprintf(p, len, "%s:url(%s);", key, val->uri);
+        } else if (val->href && val->href->getURI()) {
+            return g_snprintf(p, len, "%s:url(%s);", key, val->href->getURI()->toString());
         }
     }
 
@@ -3696,36 +3719,18 @@ sp_style_write_ifilter(gchar *p, gint const len, gchar const *key,
 }
 
 
-/**
- * Clear paint object, and disconnect style from paintserver (if present).
- */
-static void
-sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
+void SPIPaint::clear()
 {
-    if ((paint->type == SP_PAINT_TYPE_PAINTSERVER) && paint->value.paint.server) {
-        if (paint == &style->fill) {
-            if (style->fill_hreffed) {
-                sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
-                style->fill_hreffed = false;
-            }
-            style->fill_release_connection.disconnect();
-            style->fill_modified_connection.disconnect();
-        } else {
-            assert(paint == &style->stroke);  // Only fill & stroke can have a paint server.
-            if (style->stroke_hreffed) {
-                sp_object_hunref(SP_OBJECT(paint->value.paint.server), style);
-                style->stroke_hreffed = false;
-            }
-            style->stroke_release_connection.disconnect();
-            style->stroke_modified_connection.disconnect();
-        }
-
-        paint->value.paint.server = NULL;
+    set = false;
+    inherit = false;
+    currentcolor = false;
+    colorSet = false;
+    noneSet = false;
+    value.color.set( 0 );
+    if ( value.href && value.href->getObject() )
+    {
+        value.href->detach();
     }
-    paint->value.paint.uri = NULL;
-    paint->type = SP_PAINT_TYPE_NONE;
-    delete paint->value.iccColor;
-    paint->value.iccColor = NULL;
 }
 
 
@@ -3735,15 +3740,8 @@ sp_style_paint_clear(SPStyle *style, SPIPaint *paint)
 static void
 sp_style_filter_clear(SPStyle *style)
 {
-
-    if (style->filter_hreffed) {
-        sp_object_hunref(SP_OBJECT(style->filter.filter), style);
-        style->filter_hreffed = false;
-    }
-    style->filter_release_connection.disconnect();
-    style->filter_modified_connection.disconnect();
-
-    style->filter.filter = NULL;
+    if (style->filter.href && style->filter.href->getObject())
+        style->filter.href->detach();
 }
 
 
@@ -3834,6 +3832,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);
     }
@@ -3893,6 +3894,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);
@@ -4065,24 +4067,42 @@ 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 the ' quotes so the result can go to the XML attribute
+    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('\'');
     }