Code

Make curvature work again by fixing a minor omission
[inkscape.git] / src / style.cpp
index 2b24f37ed63a4057e192621a2a1b5c6082b5914f..0b946f3488ede5da9b9bcabcb56ced53586c2889 100644 (file)
@@ -1,9 +1,9 @@
 #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 "style.h"
 #include "svg/css-ostringstream.h"
 #include "xml/repr.h"
+#include "xml/simple-document.h"
 #include "unit-constants.h"
 #include "2geom/isnan.h"
 #include "macros.h"
+#include "preferences.h"
 
 #include "sp-filter-reference.h"
 
@@ -709,11 +711,14 @@ 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;
         }
@@ -790,15 +795,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();
 
-    sp_style_read(style, NULL, repr);
+    // 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");
+    
+    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;
 }
 
 
@@ -974,13 +997,24 @@ 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) {
@@ -1115,9 +1149,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;
                 }
@@ -1495,11 +1531,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);
@@ -1507,7 +1539,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;
     }
 
@@ -2069,10 +2101,11 @@ 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. */
     }
@@ -2177,7 +2210,7 @@ sp_style_merge_ifilter(SPStyle *style, SPIFilter const *parent)
             style->filter.href->changedSignal().connect(sigc::bind(sigc::ptr_fun(sp_style_filter_ref_changed), style));
     }
 
-    if (style->filter.href && parent->href) {
+    if (style->filter.href && parent->href && parent->href->getObject()) {
         try {
             style->filter.href->attach(*parent->href->getURI());
         } catch (Inkscape::BadURIException &e) {
@@ -2234,8 +2267,14 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
     p += sp_style_write_ienum(p, c + BMAX - p, "text-anchor", enum_text_anchor, &style->text_anchor, 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);
     // if fill:none, skip writing fill properties
@@ -2289,32 +2328,42 @@ sp_style_write_string(SPStyle const *const style, guint const flags)
 
         /** \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);
+            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;");
     }
 
@@ -2385,7 +2434,10 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
     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);
     // if fill:none, skip writing fill properties
@@ -2432,24 +2484,29 @@ sp_style_write_difference(SPStyle const *const from, SPStyle const *const to)
         }
         /* 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);
+            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 += sp_style_write_iscale24(p, c + BMAX - p, "stroke-opacity", &from->stroke_opacity, &to->stroke_opacity, SP_STYLE_FLAG_IFDIFF);
     }
 
     /* 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);
     }
 
@@ -2512,6 +2569,9 @@ sp_style_clear(SPStyle *style)
         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;
@@ -3813,13 +3873,15 @@ sp_style_set_property_url (SPObject *item, gchar const *property, SPObject *link
 
     if (repr == NULL) return;
 
-    g_return_if_fail(linked != NULL);
-
-    gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
-
     SPCSSAttr *css = sp_repr_css_attr_new();
-    sp_repr_css_set_property(css, property, val);
-    g_free(val);
+    if (linked) {
+        gchar *val = g_strdup_printf("url(#%s)", SP_OBJECT_ID(linked));
+        sp_repr_css_set_property(css, property, val);
+        g_free(val);
+    } else {
+        sp_repr_css_unset_property(css, "filter");
+    }
+
     if (recursive) {
         sp_repr_css_change_recursive(repr, css, "style");
     } else {
@@ -4006,6 +4068,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);