Code

Indent support for XSLT extensions output.
[inkscape.git] / src / sp-use.cpp
index dc1774e3ddac495271526904bd9a161ae6f719cd..505c64fd225b326e448dca557ad9e1fb31b4c525 100644 (file)
@@ -1,11 +1,11 @@
-#define __SP_USE_C__
-
 /*
  * SVG <use> implementation
  *
  * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2005 authors
  * Copyright (C) 2000-2001 Ximian, Inc.
@@ -22,7 +22,7 @@
 
 #include <libnr/nr-matrix-ops.h>
 #include <libnr/nr-matrix-fns.h>
-#include "libnr/nr-matrix-translate-ops.h"
+#include <2geom/transforms.h>
 #include <glibmm/i18n.h>
 #include "display/nr-arena-group.h"
 #include "attributes.h"
@@ -32,7 +32,7 @@
 #include "uri.h"
 #include "print.h"
 #include "xml/repr.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "style.h"
 #include "sp-symbol.h"
 #include "sp-use.h"
@@ -47,12 +47,12 @@ static void sp_use_finalize(GObject *obj);
 static void sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
 static void sp_use_release(SPObject *object);
 static void sp_use_set(SPObject *object, unsigned key, gchar const *value);
-static Inkscape::XML::Node *sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_use_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_use_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_use_modified(SPObject *object, guint flags);
 
-static void sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
-static void sp_use_snappoints(SPItem const *item, SnapPointsIter p);
+static void sp_use_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
+static void sp_use_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
 static void sp_use_print(SPItem *item, SPPrintContext *ctx);
 static gchar *sp_use_description(SPItem *item);
 static NRArenaItem *sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
@@ -64,7 +64,7 @@ static void sp_use_delete_self(SPObject *deleted, SPUse *self);
 
 static SPItemClass *parent_class;
 
-//void m_print(gchar *say, NR::Matrix m)
+//void m_print(gchar *say, Geom::Matrix m)
 //{ g_print("%s %g %g %g %g %g %g\n", say, m[0], m[1], m[2], m[3], m[4], m[5]); }
 
 GType
@@ -139,6 +139,12 @@ sp_use_finalize(GObject *obj)
 {
     SPUse *use = (SPUse *) obj;
 
+    if (use->child) {
+        SP_OBJECT(obj)->detach(use->child);
+        use->child = NULL;
+    }
+
+    use->ref->detach();
     delete use->ref;
 
     use->_delete_connection.~connection();
@@ -154,11 +160,11 @@ sp_use_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
         (* ((SPObjectClass *) parent_class)->build)(object, document, repr);
     }
 
-    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, "xlink:href");
+    object->readAttr( "x" );
+    object->readAttr( "y" );
+    object->readAttr( "width" );
+    object->readAttr( "height" );
+    object->readAttr( "xlink:href" );
 
     // We don't need to create child here:
     // reading xlink:href will attach ref, and that will cause the changed signal to be emitted,
@@ -170,7 +176,10 @@ sp_use_release(SPObject *object)
 {
     SPUse *use = SP_USE(object);
 
-    use->child = NULL;
+    if (use->child) {
+        object->detach(use->child);
+        use->child = NULL;
+    }
 
     use->_delete_connection.disconnect();
     use->_changed_connection.disconnect();
@@ -242,17 +251,16 @@ sp_use_set(SPObject *object, unsigned key, gchar const *value)
 }
 
 static Inkscape::XML::Node *
-sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_use_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPUse *use = SP_USE(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:use");
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     sp_repr_set_svg_double(repr, "x", use->x.computed);
@@ -270,17 +278,24 @@ sp_use_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
 }
 
 static void
-sp_use_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
+sp_use_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
 {
     SPUse const *use = SP_USE(item);
 
     if (use->child && SP_IS_ITEM(use->child)) {
         SPItem *child = SP_ITEM(use->child);
-        NR::Matrix const ct( child->transform
-                             * NR::translate(use->x.computed,
-                                             use->y.computed)
+        Geom::Matrix const ct( child->transform
+                             * Geom::Translate(use->x.computed,
+                                               use->y.computed)
                              * transform );
-        sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
+        Geom::OptRect optbbox;
+        child->invoke_bbox_full( optbbox, ct, flags, FALSE);
+        if (optbbox) {
+            bbox->x0 = (*optbbox)[0][0];
+            bbox->y0 = (*optbbox)[1][0];
+            bbox->x1 = (*optbbox)[0][1];
+            bbox->y1 = (*optbbox)[1][1];
+        }
     }
 }
 
@@ -288,17 +303,16 @@ static void
 sp_use_print(SPItem *item, SPPrintContext *ctx)
 {
     bool translated = false;
-    NRMatrix tp;
     SPUse *use = SP_USE(item);
 
     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
-        nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
-        sp_print_bind(ctx, &tp, 1.0);
+        Geom::Matrix tp(Geom::Translate(use->x.computed, use->y.computed));
+        sp_print_bind(ctx, tp, 1.0);
         translated = true;
     }
 
     if (use->child && SP_IS_ITEM(use->child)) {
-        sp_item_invoke_print(SP_ITEM(use->child), ctx);
+        SP_ITEM(use->child)->invoke_print(ctx);
     }
 
     if (translated) {
@@ -322,7 +336,7 @@ sp_use_description(SPItem *item)
              * a <use>, and giving its description. */
         }
         ++recursion_depth;
-        char *child_desc = sp_item_description(SP_ITEM(use->child));
+        char *child_desc = SP_ITEM(use->child)->description();
         --recursion_depth;
 
         ret = g_strdup_printf(_("<b>Clone</b> of: %s"), child_desc);
@@ -343,14 +357,13 @@ sp_use_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
     nr_arena_group_set_style(NR_ARENA_GROUP(ai), SP_OBJECT_STYLE(item));
 
     if (use->child) {
-        NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(use->child), arena, key, flags);
+        NRArenaItem *ac = SP_ITEM(use->child)->invoke_show(arena, key, flags);
         if (ac) {
             nr_arena_item_add_child(ai, ac, NULL);
-            nr_arena_item_unref(ac);
         }
-        NR::translate t(use->x.computed,
+        Geom::Translate t(use->x.computed,
                         use->y.computed);
-        nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), NR::Matrix(t));
+        nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), Geom::Matrix(t));
     }
 
     return ai;
@@ -362,7 +375,7 @@ sp_use_hide(SPItem *item, unsigned key)
     SPUse *use = SP_USE(item);
 
     if (use->child) {
-        sp_item_invoke_hide(SP_ITEM(use->child), key);
+        SP_ITEM(use->child)->invoke_hide(key);
     }
 
     if (((SPItemClass *) parent_class)->hide) {
@@ -372,7 +385,8 @@ sp_use_hide(SPItem *item, unsigned key)
 
 /**
  * Returns the ultimate original of a SPUse (i.e. the first object in the chain of its originals
- * which is not an SPUse).
+ * which is not an SPUse). If no original is found, NULL is returned (it is the responsibility
+ * of the caller to make sure that this is handled correctly).
  *
  * Note that the returned is the clone object, i.e. the child of an SPUse (of the argument one for
  * the trivial case) and not the "true original".
@@ -381,10 +395,11 @@ SPItem *
 sp_use_root(SPUse *use)
 {
     SPObject *orig = use->child;
-    while (SP_IS_USE(orig)) {
+    while (orig && SP_IS_USE(orig)) {
         orig = SP_USE(orig)->child;
     }
-    g_assert(SP_IS_ITEM(orig));
+    if (!orig || !SP_IS_ITEM(orig))
+        return NULL;
     return SP_ITEM(orig);
 }
 
@@ -392,7 +407,7 @@ sp_use_root(SPUse *use)
  * Returns the effective transform that goes from the ultimate original to given SPUse, both ends
  * included.
  */
-NR::Matrix
+Geom::Matrix
 sp_use_get_root_transform(SPUse *use)
 {
     //track the ultimate source of a chain of uses
@@ -407,7 +422,7 @@ sp_use_get_root_transform(SPUse *use)
 
 
     //calculate the accummulated transform, starting from the original
-    NR::Matrix t(NR::identity());
+    Geom::Matrix t(Geom::identity());
     for (GSList *i = chain; i != NULL; i = i->next) {
         SPItem *i_tem = SP_ITEM(i->data);
 
@@ -417,7 +432,7 @@ sp_use_get_root_transform(SPUse *use)
         if (SP_IS_USE(i_tem)) {
             SPUse *i_use = SP_USE(i_tem);
             if ((i_use->x._set && i_use->x.computed != 0) || (i_use->y._set && i_use->y.computed != 0)) {
-                t = t * NR::translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
+                t = t * Geom::Translate(i_use->x._set ? i_use->x.computed : 0, i_use->y._set ? i_use->y.computed : 0);
             }
         }
 
@@ -432,12 +447,12 @@ sp_use_get_root_transform(SPUse *use)
  * Returns the transform that leads to the use from its immediate original.
  * Does not inlcude the original's transform if any.
  */
-NR::Matrix
+Geom::Matrix
 sp_use_get_parent_transform(SPUse *use)
 {
-    NR::Matrix t(NR::identity());
+    Geom::Matrix t(Geom::identity());
     if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
-        t *= NR::translate(use->x._set ? use->x.computed : 0,
+        t *= Geom::Translate(use->x._set ? use->x.computed : 0,
                            use->y._set ? use->y.computed : 0);
     }
 
@@ -451,7 +466,7 @@ sp_use_get_parent_transform(SPUse *use)
  * clone's transform.
  */
 static void
-sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
+sp_use_move_compensate(Geom::Matrix const *mp, SPItem */*original*/, SPUse *self)
 {
     // the clone is orphaned; or this is not a real use, but a clone of another use;
     // we skip it, otherwise duplicate compensation will occur
@@ -464,31 +479,32 @@ sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
         return;
     }
 
-    guint mode = prefs_get_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_PARALLEL);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    guint mode = prefs->getInt("/options/clonecompensation/value", SP_CLONE_COMPENSATION_PARALLEL);
     // user wants no compensation
     if (mode == SP_CLONE_COMPENSATION_NONE)
         return;
 
-    NR::Matrix m(*mp);
+    Geom::Matrix m(*mp);
 
     // this is not a simple move, do not try to compensate
-    if (!(m.is_translation()))
+    if (!(m.isTranslation()))
         return;
 
     // restore item->transform field from the repr, in case it was changed by seltrans
-    sp_object_read_attr (SP_OBJECT (self), "transform");
+    SP_OBJECT (self)->readAttr ("transform");
 
-    NR::Matrix t = sp_use_get_parent_transform(self);
-    NR::Matrix clone_move = t.inverse() * m * t;
+    Geom::Matrix t = sp_use_get_parent_transform(self);
+    Geom::Matrix clone_move = t.inverse() * m * t;
 
     // calculate the compensation matrix and the advertized movement matrix
-    NR::Matrix advertized_move;
+    Geom::Matrix advertized_move;
     if (mode == SP_CLONE_COMPENSATION_PARALLEL) {
         clone_move = clone_move.inverse() * m;
         advertized_move = m;
     } else if (mode == SP_CLONE_COMPENSATION_UNMOVED) {
         clone_move = clone_move.inverse();
-        advertized_move.set_identity();
+        advertized_move.setIdentity();
     } else {
         g_assert_not_reached();
     }
@@ -496,7 +512,7 @@ sp_use_move_compensate(NR::Matrix const *mp, SPItem */*original*/, SPUse *self)
     // commit the compensation
     SPItem *item = SP_ITEM(self);
     item->transform *= clone_move;
-    sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, &advertized_move);
+    item->doWriteTransform(SP_OBJECT_REPR(item), item->transform, &advertized_move);
     SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
@@ -509,7 +525,7 @@ sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
     use->_transformed_connection.disconnect();
 
     if (use->child) {
-        sp_object_detach(SP_OBJECT(use), use->child);
+        SP_OBJECT(use)->detach(use->child);
         use->child = NULL;
     }
 
@@ -521,16 +537,15 @@ sp_use_href_changed(SPObject */*old_ref*/, SPObject */*ref*/, SPUse *use)
             g_return_if_fail(type > G_TYPE_NONE);
             if (g_type_is_a(type, SP_TYPE_ITEM)) {
                 use->child = (SPObject*) g_object_new(type, 0);
-                sp_object_attach(SP_OBJECT(use), use->child, use->lastChild());
+                SP_OBJECT(use)->attach(use->child, use->lastChild());
                 sp_object_unref(use->child, SP_OBJECT(use));
-                sp_object_invoke_build(use->child, SP_OBJECT(use)->document, childrepr, TRUE);
+                (use->child)->invoke_build(SP_OBJECT(use)->document, childrepr, TRUE);
 
                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
                     NRArenaItem *ai;
-                    ai = sp_item_invoke_show(SP_ITEM(use->child), NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
+                    ai = SP_ITEM(use->child)->invoke_show(NR_ARENA_ITEM_ARENA(v->arenaitem), v->key, v->flags);
                     if (ai) {
                         nr_arena_item_add_child(v->arenaitem, ai, NULL);
-                        nr_arena_item_unref(ai);
                     }
                 }
 
@@ -550,7 +565,8 @@ sp_use_delete_self(SPObject */*deleted*/, SPUse *self)
         return;
     }
 
-    guint const mode = prefs_get_int_attribute("options.cloneorphans", "value",
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    guint const mode = prefs->getInt("/options/cloneorphans/value",
                                                SP_CLONE_ORPHANS_UNLINK);
 
     if (mode == SP_CLONE_ORPHANS_UNLINK) {
@@ -576,7 +592,7 @@ sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
 
     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
       for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
-       nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
+        nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
       }
     }
 
@@ -597,7 +613,7 @@ sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
     cctx.vp.y0 = 0.0;
     cctx.vp.x1 = use->width.computed;
     cctx.vp.y1 = use->height.computed;
-    cctx.i2vp = NR::identity();
+    cctx.i2vp = Geom::identity();
     flags&=~SP_OBJECT_USER_MODIFIED_FLAG_B;
 
     if (use->child) {
@@ -617,9 +633,8 @@ sp_use_update(SPObject *object, SPCtx *ctx, unsigned flags)
 
     /* As last step set additional transform of arena group */
     for (SPItemView *v = item->display; v != NULL; v = v->next) {
-        NRMatrix t;
-        nr_matrix_set_translate(&t, use->x.computed, use->y.computed);
-        nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
+        Geom::Matrix t(Geom::Translate(use->x.computed, use->y.computed));
+        nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), t);
     }
 }
 
@@ -635,7 +650,7 @@ sp_use_modified(SPObject *object, guint flags)
 
     if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
       for (SPItemView *v = SP_ITEM(object)->display; v != NULL; v = v->next) {
-       nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
+        nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
       }
     }
 
@@ -649,23 +664,29 @@ sp_use_modified(SPObject *object, guint flags)
     }
 }
 
-SPItem *
-sp_use_unlink(SPUse *use)
+SPItem *sp_use_unlink(SPUse *use)
 {
-    if (!use) return NULL;
+    if (!use) {
+        return NULL;
+    }
 
     Inkscape::XML::Node *repr = SP_OBJECT_REPR(use);
-    if (!repr) return NULL;
+    if (!repr) {
+        return NULL;
+    }
 
     Inkscape::XML::Node *parent = sp_repr_parent(repr);
     SPDocument *document = SP_OBJECT(use)->document;
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+    Inkscape::XML::Document *xml_doc = document->getReprDoc();
 
     // Track the ultimate source of a chain of uses.
     SPItem *orig = sp_use_root(use);
+    if (!orig) {
+        return NULL;
+    }
 
     // Calculate the accumulated transform, starting from the original.
-    NR::Matrix t = sp_use_get_root_transform(use);
+    Geom::Matrix t = sp_use_get_root_transform(use);
 
     Inkscape::XML::Node *copy = NULL;
     if (SP_IS_SYMBOL(orig)) { // make a group, copy children
@@ -718,10 +739,9 @@ sp_use_unlink(SPUse *use)
     SPItem *item = SP_ITEM(unlinked);
     // Set the accummulated transform.
     {
-        NR::Matrix nomove(NR::identity());
-        NRMatrix ctrans = t.operator const NRMatrix&();
+        Geom::Matrix nomove(Geom::identity());
         // Advertise ourselves as not moving.
-        sp_item_write_transform(item, SP_OBJECT_REPR(item), &ctrans, &nomove);
+        item->doWriteTransform(SP_OBJECT_REPR(item), t, &nomove);
     }
     return item;
 }
@@ -734,18 +754,20 @@ sp_use_get_original(SPUse *use)
 }
 
 static void
-sp_use_snappoints(SPItem const *item, SnapPointsIter p)
+sp_use_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
 {
     g_assert (item != NULL);
     g_assert (SP_IS_ITEM(item));
     g_assert (SP_IS_USE(item));
-    
+
     SPUse *use = SP_USE(item);
     SPItem *root = sp_use_root(use);
-    
+    if (!root)
+        return;
+
     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(root);
     if (item_class.snappoints) {
-        item_class.snappoints(root, p);
+        item_class.snappoints(root, p, snapprefs);
     }
 }