Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / sp-clippath.cpp
index 33feea8ce2eef50669b8a78841becde558822f69..4bbabc965fbaf3360018461fe9bec96615e81192 100644 (file)
@@ -12,7 +12,8 @@
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-
+#include <cstring>
+#include <string>
 
 #include "display/nr-arena.h"
 #include "display/nr-arena-group.h"
@@ -24,6 +25,9 @@
 #include "document-private.h"
 #include "sp-item.h"
 
+#include "libnr/nr-matrix-ops.h"
+#include <2geom/transforms.h>
+
 #include "sp-clippath.h"
 
 struct SPClipPathView {
@@ -42,7 +46,7 @@ static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *val
 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_clippath_modified(SPObject *object, guint flags);
-static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
@@ -162,13 +166,12 @@ sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::
     if (SP_IS_ITEM(ochild)) {
         SPClipPath *cp = SP_CLIPPATH(object);
         for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
-            NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild), 
-                                                  NR_ARENA_ITEM_ARENA(v->arenaitem), 
-                                                  v->key, 
+            NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild),
+                                                  NR_ARENA_ITEM_ARENA(v->arenaitem),
+                                                  v->key,
                                                   SP_ITEM_REFERENCE_FLAGS);
             if (ac) {
                 nr_arena_item_add_child(v->arenaitem, ac, NULL);
-                nr_arena_item_unref(ac);
             }
         }
     }
@@ -202,10 +205,9 @@ sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
     SPClipPath *cp = SP_CLIPPATH(object);
     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
         if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
-            NRMatrix t;
-            nr_matrix_set_scale(&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
-            t.c[4] = v->bbox.x0;
-            t.c[5] = v->bbox.y0;
+            Geom::Matrix t(Geom::Scale(v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0));
+            t[4] = v->bbox.x0;
+            t[5] = v->bbox.y0;
             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
         } else {
             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
@@ -240,14 +242,14 @@ sp_clippath_modified(SPObject *object, guint flags)
 }
 
 static Inkscape::XML::Node *
-sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_clippath_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:clipPath");
+        repr = xml_doc->createElement("svg:clipPath");
     }
 
     if (((SPObjectClass *) (parent_class))->write)
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -269,16 +271,14 @@ sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
             if (ac) {
                 /* The order is not important in clippath */
                 nr_arena_item_add_child(ai, ac, NULL);
-                nr_arena_item_unref(ac);
             }
         }
     }
 
     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
-        NRMatrix t;
-        nr_matrix_set_scale(&t, cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0);
-        t.c[4] = cp->display->bbox.x0;
-        t.c[5] = cp->display->bbox.y0;
+        Geom::Matrix t(Geom::Scale(cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0));
+        t[4] = cp->display->bbox.x0;
+        t[5] = cp->display->bbox.y0;
         nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
     }
 
@@ -325,14 +325,24 @@ sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
 }
 
 void
-sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
+sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
 {
-       for (SPObject *o = sp_object_first_child(SP_OBJECT(cp)); o != NULL; o = SP_OBJECT_NEXT(o)) {
-               if (SP_IS_ITEM(o)) {
-                       SPItem *child = SP_ITEM(o);
-                       sp_item_invoke_bbox_full(child, bbox, transform, flags, FALSE);
-               }
-       }
+    SPObject *i;
+    for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
+    if (!i) return;
+
+    sp_item_invoke_bbox_full(SP_ITEM(i), bbox, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
+    SPObject *i_start = i;
+
+    while (i != NULL) {
+        if (i != i_start) {
+            NRRect i_box;
+            sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
+            nr_rect_d_union (bbox, bbox, &i_box);
+        }
+        i = SP_OBJECT_NEXT(i);
+        for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
+    }
 }
 
 /* ClipPath views */
@@ -371,22 +381,29 @@ sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
 
 // Create a mask element (using passed elements), add it to <defs>
 const gchar *
-sp_clippath_create (GSList *reprs, SPDocument *document)
+sp_clippath_create (GSList *reprs, SPDocument *document, Geom::Matrix const* applyTransform)
 {
     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
 
-    Inkscape::XML::Node *repr = sp_repr_new ("svg:clipPath");
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
     repr->setAttribute("clipPathUnits", "userSpaceOnUse");
-    
+
     defsrepr->appendChild(repr);
     const gchar *id = repr->attribute("id");
     SPObject *clip_path_object = document->getObjectById(id);
-    
+
     for (GSList *it = reprs; it != NULL; it = it->next) {
         Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
-        clip_path_object->appendChildRepr(node);
+        SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
+
+        if (NULL != applyTransform) {
+            Geom::Matrix transform (item->transform);
+            transform *= (*applyTransform);
+            sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
+        }
     }
-    
+
     Inkscape::GC::release(repr);
     return id;
 }