Code

fix Bug #674109 - Pattern spam when adjusting a pattern in text
[inkscape.git] / src / sp-pattern.cpp
index 90af65b97cb8f3c072509d3268c272497a71d72d..a2fc9a6b29b76840eb3c752cf3a0c304522b02d6 100644 (file)
@@ -432,12 +432,43 @@ pattern_ref_modified (SPObject */*ref*/, guint /*flags*/, SPPattern *pattern)
 {
        if (SP_IS_OBJECT (pattern))
                SP_OBJECT (pattern)->requestModified(SP_OBJECT_MODIFIED_FLAG);
+        /* Conditional to avoid causing infinite loop if there's a cycle in the href chain. */
 }
 
+
+/**
+Count how many times pat is used by the styles of o and its descendants
+*/
 guint
-pattern_users (SPPattern *pattern)
+count_pattern_hrefs(SPObject *o, SPPattern *pat)
 {
-       return SP_OBJECT (pattern)->hrefcount;
+    if (!o)
+        return 1;
+
+    guint i = 0;
+
+    SPStyle *style = SP_OBJECT_STYLE(o);
+    if (style
+        && style->fill.isPaintserver()
+        && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(style))
+        && SP_PATTERN(SP_STYLE_FILL_SERVER(style)) == pat)
+    {
+        i ++;
+    }
+    if (style
+        && style->stroke.isPaintserver()
+        && SP_IS_PATTERN(SP_STYLE_STROKE_SERVER(style))
+        && SP_PATTERN(SP_STYLE_STROKE_SERVER(style)) == pat)
+    {
+        i ++;
+    }
+
+    for (SPObject *child = sp_object_first_child(o);
+         child != NULL; child = SP_OBJECT_NEXT(child)) {
+        i += count_pattern_hrefs(child, pat);
+    }
+
+    return i;
 }
 
 SPPattern *
@@ -464,7 +495,7 @@ pattern_chain (SPPattern *pattern)
 SPPattern *
 sp_pattern_clone_if_necessary (SPItem *item, SPPattern *pattern, const gchar *property)
 {
-       if (pattern_users(pattern) > 1) {
+       if (!pattern->href || SP_OBJECT_HREFCOUNT(pattern) > count_pattern_hrefs(item, pattern)) {
                pattern = pattern_chain (pattern);
                gchar *href = g_strconcat ("url(#", SP_OBJECT_REPR (pattern)->attribute("id"), ")", NULL);