Code

Use subdirectories with icon sizes.
[inkscape.git] / src / sp-pattern.cpp
index 711f2c4085e57bad8807dada14ccce97e9ea8078..fbba4cc48caaa56c21939104603deaffebd44288 100644 (file)
@@ -1,11 +1,11 @@
-#define __SP_PATTERN_C__
-
 /*
  * SVG <pattern> implementation
  *
  * Author:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma 
  *
  * Copyright (C) 2002 Lauris Kaplinski
  *
@@ -57,6 +57,8 @@ struct SPPatPainter {
        Geom::Matrix     pa2ca;
        NRRectL      cached_bbox;
        NRPixBlock   cached_tile;
+
+  std::map<SPObject *, sigc::connection> *_release_connections;
 };
 
 static void sp_pattern_class_init (SPPatternClass *klass);
@@ -154,18 +156,18 @@ sp_pattern_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *r
        if (((SPObjectClass *) pattern_parent_class)->build)
                (* ((SPObjectClass *) pattern_parent_class)->build) (object, document, repr);
 
-       sp_object_read_attr (object, "patternUnits");
-       sp_object_read_attr (object, "patternContentUnits");
-       sp_object_read_attr (object, "patternTransform");
-       sp_object_read_attr (object, "x");
-       sp_object_read_attr (object, "y");
-       sp_object_read_attr (object, "width");
-       sp_object_read_attr (object, "height");
-       sp_object_read_attr (object, "viewBox");
-       sp_object_read_attr (object, "xlink:href");
+       object->readAttr( "patternUnits" );
+       object->readAttr( "patternContentUnits" );
+       object->readAttr( "patternTransform" );
+       object->readAttr( "x" );
+       object->readAttr( "y" );
+       object->readAttr( "width" );
+       object->readAttr( "height" );
+       object->readAttr( "viewBox" );
+       object->readAttr( "xlink:href" );
 
        /* Register ourselves */
-       sp_document_add_resource (document, "pattern", object);
+       document->addResource("pattern", object);
 }
 
 static void
@@ -177,7 +179,7 @@ sp_pattern_release (SPObject *object)
 
        if (SP_OBJECT_DOCUMENT (object)) {
                /* Unregister ourselves */
-               sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "pattern", SP_OBJECT (object));
+               SP_OBJECT_DOCUMENT (object)->removeResource("pattern", SP_OBJECT (object));
        }
 
        if (pat->ref) {
@@ -321,16 +323,16 @@ sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::
        if (((SPObjectClass *) (pattern_parent_class))->child_added)
                (* ((SPObjectClass *) (pattern_parent_class))->child_added) (object, child, ref);
 
-       SPObject *ochild = sp_object_get_child_by_repr(object, child);
+       SPObject *ochild = object->get_child_by_repr(child);
        if (SP_IS_ITEM (ochild)) {
 
                SPPaintServer *ps = SP_PAINT_SERVER (pat);
-               unsigned position = sp_item_pos_in_parent(SP_ITEM(ochild));
+               unsigned position = SP_ITEM(ochild)->pos_in_parent();
 
                for (SPPainter *p = ps->painters; p != NULL; p = p->next) {
 
                        SPPatPainter *pp = (SPPatPainter *) p;
-                       NRArenaItem *ai = sp_item_invoke_show (SP_ITEM (ochild), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
+                       NRArenaItem *ai = SP_ITEM (ochild)->invoke_show (pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
 
                        if (ai) {
                                nr_arena_item_add_child (pp->root, ai, NULL);
@@ -345,21 +347,20 @@ sp_pattern_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::
 
 /* fixme: We need ::order_changed handler too (Lauris) */
 
-GSList *
-pattern_getchildren (SPPattern *pat)
+GSList *pattern_getchildren(SPPattern *pat)
 {
-       GSList *l = NULL;
-
-       for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
-               if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
-                       for (SPObject *child = sp_object_first_child(SP_OBJECT (pat)) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
-                               l = g_slist_prepend (l, child);
-                       }
-                       break; // do not go further up the chain if children are found
-               }
+    GSList *l = NULL;
+
+    for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
+        if (pat_i->firstChild()) { // find the first one with children
+           for (SPObject *child = pat->firstChild() ; child ; child = child->getNext() ) {
+               l = g_slist_prepend (l, child);
+           }
+           break; // do not go further up the chain if children are found
        }
+    }
 
-       return l;
+  return l;
 }
 
 static void
@@ -430,19 +431,48 @@ 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 = o->firstChild(); child != NULL; child = child->next ) {
+        i += count_pattern_hrefs(child, pat);
+    }
+
+    return i;
 }
 
-SPPattern *
-pattern_chain (SPPattern *pattern)
+SPPattern *pattern_chain(SPPattern *pattern)
 {
        SPDocument *document = SP_OBJECT_DOCUMENT (pattern);
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+        Inkscape::XML::Document *xml_doc = document->getReprDoc();
        Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
 
        Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
@@ -462,7 +492,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 || pattern->hrefcount > count_pattern_hrefs(item, pattern)) {
                pattern = pattern_chain (pattern);
                gchar *href = g_strconcat ("url(#", SP_OBJECT_REPR (pattern)->attribute("id"), ")", NULL);
 
@@ -493,10 +523,9 @@ sp_pattern_transform_multiply (SPPattern *pattern, Geom::Matrix postmul, bool se
        g_free(c);
 }
 
-const gchar *
-pattern_tile (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matrix transform, Geom::Matrix move)
+const gchar *pattern_tile(GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matrix transform, Geom::Matrix move)
 {
-       Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
        Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
 
        Inkscape::XML::Node *repr = xml_doc->createElement("svg:pattern");
@@ -521,22 +550,21 @@ pattern_tile (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matr
                        dup_transform = Geom::identity();
                dup_transform *= move;
 
-               sp_item_write_transform(copy, SP_OBJECT_REPR(copy), dup_transform);
+               copy->doWriteTransform(SP_OBJECT_REPR(copy), dup_transform, NULL, false);
        }
 
        Inkscape::GC::release(repr);
        return pat_id;
 }
 
-SPPattern *
-pattern_getroot (SPPattern *pat)
+SPPattern *pattern_getroot(SPPattern *pat)
 {
-       for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
-               if (sp_object_first_child(SP_OBJECT(pat_i))) { // find the first one with children
-                       return pat_i;
-               }
-       }
-       return pat; // document is broken, we can't get to root; but at least we can return pat which is supposedly a valid pattern
+    for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
+        if ( pat_i->firstChild() ) { // find the first one with children
+            return pat_i;
+        }
+    }
+    return pat; // document is broken, we can't get to root; but at least we can return pat which is supposedly a valid pattern
 }
 
 
@@ -618,12 +646,13 @@ NRRect *pattern_viewBox (SPPattern *pat)
 
 bool pattern_hasItemChildren (SPPattern *pat)
 {
-       for (SPObject *child = sp_object_first_child(SP_OBJECT(pat)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
-               if (SP_IS_ITEM (child)) {
-                       return true;
-               }
-       }
-       return false;
+    bool hasChildren = false;
+    for (SPObject *child = pat->firstChild() ; child && !hasChildren ; child = child->getNext() ) {
+        if (SP_IS_ITEM(child)) {
+            hasChildren = true;
+        }
+    }
+    return hasChildren;
 }
 
 
@@ -632,6 +661,19 @@ bool pattern_hasItemChildren (SPPattern *pat)
 
 static void sp_pat_fill (SPPainter *painter, NRPixBlock *pb);
 
+// item in this pattern is about to be deleted, hide it on our arena and disconnect
+void
+sp_pattern_painter_release (SPObject *obj, SPPatPainter *painter)
+{
+       std::map<SPObject *, sigc::connection>::iterator iter = painter->_release_connections->find(obj);
+       if (iter != painter->_release_connections->end()) {
+               iter->second.disconnect();
+    painter->_release_connections->erase(obj);
+       }
+
+       SP_ITEM(obj)->invoke_hide(painter->dkey);
+}
+
 /**
 Creates a painter (i.e. the thing that does actual filling at the given zoom).
 See (*) below for why the parent_transform may be necessary.
@@ -711,24 +753,29 @@ sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, G
        /* Create arena */
        pp->arena = NRArena::create();
 
-       pp->dkey = sp_item_display_key_new (1);
+       pp->dkey = SPItem::display_key_new (1);
 
        /* Create group */
        pp->root = NRArenaGroup::create(pp->arena);
 
        /* Show items */
-       for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
-               if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
-                       for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
-                               if (SP_IS_ITEM (child)) {
-                                       NRArenaItem *cai;
-                                       cai = sp_item_invoke_show (SP_ITEM (child), pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
-                                       nr_arena_item_append_child (pp->root, cai);
-                               }
-                       }
-                       break; // do not go further up the chain if children are found
-               }
-       }
+        pp->_release_connections = new std::map<SPObject *, sigc::connection>;
+        for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
+            if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
+                for (SPObject *child = pat_i->firstChild() ; child; child = child->getNext() ) {
+                    if (SP_IS_ITEM (child)) {
+                        // for each item in pattern,
+                        // show it on our arena,
+                        NRArenaItem *cai = SP_ITEM(child)->invoke_show(pp->arena, pp->dkey, SP_ITEM_REFERENCE_FLAGS);
+                        // add to the group,
+                        nr_arena_item_append_child (pp->root, cai);
+                        // and connect to the release signal in case the item gets deleted
+                        pp->_release_connections->insert(std::make_pair(child, child->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_pattern_painter_release), pp))));
+                    }
+                }
+                break; // do not go further up the chain if children are found
+            }
+        }
 
        {
                NRRect    one_tile,tr_tile;
@@ -787,22 +834,25 @@ sp_pattern_painter_new (SPPaintServer *ps, Geom::Matrix const &full_transform, G
        return (SPPainter *) pp;
 }
 
+
 static void
 sp_pattern_painter_free (SPPaintServer */*ps*/, SPPainter *painter)
 {
        SPPatPainter *pp = (SPPatPainter *) painter;
-       SPPattern *pat = pp->pat;
-
-       for (SPPattern *pat_i = pat; pat_i != NULL; pat_i = pat_i->ref ? pat_i->ref->getObject() : NULL) {
-               if (pat_i && SP_IS_OBJECT (pat_i) && pattern_hasItemChildren(pat_i)) { // find the first one with item children
-                       for (SPObject *child = sp_object_first_child(SP_OBJECT(pat_i)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
-                               if (SP_IS_ITEM (child)) {
-                                               sp_item_invoke_hide (SP_ITEM (child), pp->dkey);
-                               }
-                       }
-                       break; // do not go further up the chain if children are found
-               }
+       // free our arena
+  if (pp->arena) {
+      ((NRObject *) pp->arena)->unreference();
+      pp->arena = NULL;
+  }
+
+       // disconnect all connections
+  std::map<SPObject *, sigc::connection>::iterator iter;
+  for (iter = pp->_release_connections->begin() ; iter!=pp->_release_connections->end() ; iter++) {
+         iter->second.disconnect();
        }
+       pp->_release_connections->clear();
+  delete pp->_release_connections;
+
        if ( pp->use_cached_tile ) nr_pixblock_release(&pp->cached_tile);
        g_free (pp);
 }
@@ -988,6 +1038,18 @@ sp_pat_fill (SPPainter *painter, NRPixBlock *pb)
                                nr_pixblock_release (&ppb);
                        }
                }
-        
+     } 
        }
 }
+
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :