Code

Small snap bug has been eliminated, flowed text snapping now uses baseline, replacing...
[inkscape.git] / src / sp-item.cpp
index 566ff8cb887645fcdccb8054f40b3927e283fea2..1a5ca6f772a72f7ba94fd99c153062bdabf9502b 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_ITEM_C__
-
 /** \file
  * Base class for visual SVG elements
  */
@@ -35,6 +33,7 @@
 #include "document.h"
 #include "uri.h"
 #include "inkscape.h"
+#include "desktop.h"
 #include "desktop-handles.h"
 
 #include "style.h"
 #include "sp-item-rm-unsatisfied-cns.h"
 #include "sp-pattern.h"
 #include "sp-switch.h"
+#include "sp-guide-constraint.h"
 #include "gradient-chemistry.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "conn-avoid-ref.h"
 #include "conditions.h"
 #include "sp-filter-reference.h"
 #include "filter-chemistry.h"
 #include "sp-guide.h"
+#include "sp-title.h"
+#include "sp-desc.h"
 
-#include "libnr/nr-matrix-div.h"
 #include "libnr/nr-matrix-fns.h"
 #include "libnr/nr-matrix-scale-ops.h"
 #include "libnr/nr-matrix-translate-ops.h"
 #include "libnr/nr-convert2geom.h"
 #include "algorithms/find-last-if.h"
 #include "util/reverse-list.h"
+#include <2geom/rect.h>
+#include <2geom/matrix.h>
+#include <2geom/transforms.h>
 
 #include "xml/repr.h"
 #include "extract-uri.h"
+#include "helper/geom.h"
 
 #include "live_effects/lpeobject.h"
 #include "live_effects/effect.h"
@@ -82,10 +87,10 @@ static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML:
 static void sp_item_release(SPObject *object);
 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
-static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static gchar *sp_item_private_description(SPItem *item);
-static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
+static void sp_item_private_snappoints(SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs);
 
 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
@@ -156,23 +161,25 @@ void SPItem::init() {
     this->_is_evaluated = true;
     this->_evaluated_status = StatusUnknown;
 
-    this->transform = NR::identity();
+    this->transform = Geom::identity();
 
     this->display = NULL;
 
     this->clip_ref = new SPClipPathReference(this);
-               sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
-               sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
+    sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
+    sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
     _clip_ref_connection = cs1.connect(sl1);
 
     this->mask_ref = new SPMaskReference(this);
-               sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
-               sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
+    sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
+    sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
     _mask_ref_connection = cs2.connect(sl2);
 
     this->avoidRef = new SPAvoidRef(this);
 
-    new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
+    new (&this->constraints) std::vector<SPGuideConstraint>();
+
+    new (&this->_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
 }
 
 bool SPItem::isVisibleAndUnlocked() const {
@@ -266,7 +273,7 @@ bool
 SPItem::isExplicitlyHidden() const
 {
     return (this->style->display.set
-           && this->style->display.value == SP_CSS_DISPLAY_NONE);
+            && this->style->display.value == SP_CSS_DISPLAY_NONE);
 }
 
 /**
@@ -285,13 +292,16 @@ SPItem::setExplicitlyHidden(bool const val) {
  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
  */
 void
-SPItem::setCenter(NR::Point object_centre) {
-    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+SPItem::setCenter(Geom::Point object_centre) {
+    // for getBounds() to work
+    sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(this));
+
+    Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
     if (bbox) {
-        transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
+        transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
         if (fabs(transform_center_x) < 1e-5) // rounding error
             transform_center_x = 0;
-        transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
+        transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
         if (fabs(transform_center_y) < 1e-5) // rounding error
             transform_center_y = 0;
     }
@@ -307,12 +317,15 @@ bool SPItem::isCenterSet() {
     return (transform_center_x != 0 || transform_center_y != 0);
 }
 
-NR::Point SPItem::getCenter() const {
-    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+Geom::Point SPItem::getCenter() const {
+    // for getBounds() to work
+    sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(this));
+
+    Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
     if (bbox) {
-        return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
+        return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
     } else {
-        return NR::Point (0, 0); // something's wrong!
+        return Geom::Point (0, 0); // something's wrong!
     }
 }
 
@@ -399,6 +412,7 @@ sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
     sp_object_read_attr(object, "inkscape:transform-center-x");
     sp_object_read_attr(object, "inkscape:transform-center-y");
     sp_object_read_attr(object, "inkscape:connector-avoid");
+    sp_object_read_attr(object, "inkscape:connection-points");
 
     if (((SPObjectClass *) (parent_class))->build) {
         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
@@ -452,11 +466,11 @@ sp_item_set(SPObject *object, unsigned key, gchar const *value)
 
     switch (key) {
         case SP_ATTR_TRANSFORM: {
-            NR::Matrix t;
+            Geom::Matrix t;
             if (value && sp_svg_transform_read(value, &t)) {
                 sp_item_set_item_transform(item, t);
             } else {
-                sp_item_set_item_transform(item, NR::identity());
+                sp_item_set_item_transform(item, Geom::identity());
             }
             break;
         }
@@ -501,6 +515,9 @@ sp_item_set(SPObject *object, unsigned key, gchar const *value)
         case SP_ATTR_CONNECTOR_AVOID:
             item->avoidRef->setAvoid(value);
             break;
+        case SP_ATTR_CONNECTION_POINTS:
+            item->avoidRef->setConnectionPoints(value);
+            break;
         case SP_ATTR_TRANSFORM_CENTER_X:
             if (value) {
                 item->transform_center_x = g_strtod(value, NULL);
@@ -550,7 +567,7 @@ clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
     }
     if (SP_IS_CLIPPATH(clip)) {
         NRRect bbox;
-        sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+        sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
         for (SPItemView *v = item->display; v != NULL; v = v->next) {
             if (!v->arenaitem->key) {
                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
@@ -578,7 +595,7 @@ mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
     }
     if (SP_IS_MASK(mask)) {
         NRRect bbox;
-        sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+        sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
         for (SPItemView *v = item->display; v != NULL; v = v->next) {
             if (!v->arenaitem->key) {
                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
@@ -614,7 +631,7 @@ sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
 
         if ( clip_path || mask ) {
             NRRect bbox;
-            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
             if (clip_path) {
                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
@@ -637,14 +654,13 @@ sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
 
     /* Update bounding box data used by filters */
     if (item->style->filter.set && item->display) {
-        NRRect item_bbox;
-        sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
-        NR::Maybe<NR::Rect> i_bbox = item_bbox;
+        Geom::OptRect item_bbox;
+        sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
 
         SPItemView *itemview = item->display;
         do {
             if (itemview->arenaitem)
-                nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
+                nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
         } while ( (itemview = itemview->next) );
     }
 
@@ -654,10 +670,34 @@ sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
 }
 
 static Inkscape::XML::Node *
-sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
+sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
+    SPObject *child;
     SPItem *item = SP_ITEM(object);
 
+    // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
+    // so we need to add any children from the underlying object to the new repr
+    if (flags & SP_OBJECT_WRITE_BUILD) {
+        Inkscape::XML::Node *crepr;
+        GSList *l;
+        l = NULL;
+        for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
+            if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
+            crepr = child->updateRepr(xml_doc, NULL, flags);
+            if (crepr) l = g_slist_prepend (l, crepr);
+        }
+        while (l) {
+            repr->addChild((Inkscape::XML::Node *) l->data, NULL);
+            Inkscape::GC::release((Inkscape::XML::Node *) l->data);
+            l = g_slist_remove (l, l->data);
+        }
+    } else {
+        for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+            if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
+            child->updateRepr(flags);
+        }
+    }
+
     gchar *c = sp_svg_transform_write(item->transform);
     repr->setAttribute("transform", c);
     g_free(c);
@@ -686,30 +726,34 @@ sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     return repr;
 }
 
-NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
+/**
+ * \return  There is no guarantee that the return value will contain a rectangle.
+            If this item does not have a boundingbox, it might well be empty.
+ */
+Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
                                       SPItem::BBoxType type,
                                       unsigned int /*dkey*/) const
 {
-    NR::Maybe<NR::Rect> r = NR::Nothing();
-    sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
+    Geom::OptRect r;
+    sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
     return r;
 }
 
 void
-sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
+sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
 {
     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
 }
 
-// DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
+// DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
 void
-sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
+sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
 {
     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
 }
@@ -717,19 +761,21 @@ sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transfor
 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
- * clones), in turn, call this function in their bbox methods. */
+ * clones), in turn, call this function in their bbox methods.
+ * \retval bbox  Note that there is no guarantee that bbox will contain a rectangle when the
+ *               function returns. If this item does not have a boundingbox, this might well be empty.
+ */
 void
-sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
+sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
 {
     g_assert(item != NULL);
     g_assert(SP_IS_ITEM(item));
-    g_assert(bbox != NULL);
 
     if (clear) {
-        *bbox = NR::Nothing();
+        bbox = Geom::OptRect();
     }
 
-    // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
+    // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
 
     NRRect temp_bbox;
     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
@@ -758,7 +804,7 @@ sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matr
                     y = SP_FILTER(filter)->y.computed;
                 if (SP_FILTER(filter)->width._set)
                     w = SP_FILTER(filter)->width.computed;
-                if (SP_FILTER(filter)->height._set) 
+                if (SP_FILTER(filter)->height._set)
                     h = SP_FILTER(filter)->height.computed;
 
                 double dx0 = 0;
@@ -766,7 +812,7 @@ sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matr
                 double dy0 = 0;
                 double dy1 = 0;
                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
-                    // if this is a single blur, use 2.4*radius 
+                    // if this is a single blur, use 2.4*radius
                     // which may be smaller than the default area;
                     // see set_filter_area for why it's 2.4
                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
@@ -783,11 +829,11 @@ sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matr
                 }
 
                 // transform the expansions by the item's transform:
-                NR::Matrix i2d = sp_item_i2d_affine (item);
-                dx0 *= NR::expansionX(i2d);
-                dx1 *= NR::expansionX(i2d);
-                dy0 *= NR::expansionY(i2d);
-                dy1 *= NR::expansionY(i2d);
+                Geom::Matrix i2d(sp_item_i2d_affine (item));
+                dx0 *= i2d.expansionX();
+                dx1 *= i2d.expansionX();
+                dy0 *= i2d.expansionY();
+                dy1 *= i2d.expansionY();
 
                 // expand the bbox
                 temp_bbox.x0 += dx0;
@@ -804,31 +850,31 @@ sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matr
     }
 
     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
-        // Either the bbox hasn't been touched by the SPItemClass' bbox method 
+        // Either the bbox hasn't been touched by the SPItemClass' bbox method
         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
-        
-        // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been 
-        // explicitely defined this way for NRRects (as opposed to NR::Maybe<NR::Rect>)
-        *bbox = NR::Nothing();
+
+        // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
+        // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
+        // So union bbox with nothing = do nothing, just return
         return;
     }
 
-    // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
+    // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
-    // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
-    NR::Maybe<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
+    // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
+    Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
 
-    *bbox = NR::union_bounds(*bbox, temp_bbox_new);
+    bbox = Geom::unify(bbox, temp_bbox_new);
 }
 
-// DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
+// DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
  * clones), in turn, call this function in their bbox methods. */
 void
-sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
+sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
 {
     g_assert(item != NULL);
     g_assert(SP_IS_ITEM(item));
@@ -896,42 +942,76 @@ sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
 }
 
-NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
+Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
 {
-    NR::Maybe<NR::Rect> rect = NR::Nothing();
-    sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
+    Geom::OptRect rect = Geom::OptRect();
+    sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
     return rect;
 }
 
-static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
+static void sp_item_private_snappoints(SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const */*snapprefs*/)
 {
-    NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
-    /* Just the corners of the bounding box suffices given that we don't yet
-       support angled guide lines. */
+    /* This will only be called if the derived class doesn't override this.
+     * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
+     * We don't know what shape we could be dealing with here, so we'll just
+     * return the corners of the bounding box */
+
+    Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
 
     if (bbox) {
-        NR::Point p1, p2;
+        Geom::Point p1, p2;
         p1 = bbox->min();
         p2 = bbox->max();
-        *p = p1;
-        *p = NR::Point(p1[NR::X], p2[NR::Y]);
-        *p = p2;
-        *p = NR::Point(p1[NR::Y], p2[NR::X]);
+        int type = target ? int(Inkscape::SNAPTARGET_BBOX_CORNER) : int(Inkscape::SNAPSOURCE_BBOX_CORNER);
+        p.push_back(std::make_pair(p1, type));
+        p.push_back(std::make_pair(Geom::Point(p1[Geom::X], p2[Geom::Y]), type));
+        p.push_back(std::make_pair(p2, type));
+        p.push_back(std::make_pair(Geom::Point(p2[Geom::X], p1[Geom::Y]), type));
     }
+
 }
 
-void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
+void sp_item_snappoints(SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs)
 {
     g_assert (item != NULL);
     g_assert (SP_IS_ITEM(item));
 
+    // Get the snappoints of the item
     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
     if (item_class.snappoints) {
-        item_class.snappoints(item, p);
-    }
-
-    if (includeItemCenter) {
-       *p = item->getCenter();
+        item_class.snappoints(item, target, p, snapprefs);
+    }
+
+    // Get the snappoints at the item's center
+    if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
+       p.push_back(std::make_pair(item->getCenter(), target ? int(Inkscape::SNAPTARGET_ROTATION_CENTER) : int(Inkscape::SNAPSOURCE_ROTATION_CENTER)));
+    }
+
+    // Get the snappoints of clipping paths and mask, if any
+    std::list<SPObject const *> clips_and_masks;
+
+    clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
+    clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
+
+    SPDesktop *desktop = inkscape_active_desktop();
+    for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
+        if (*o) {
+            // obj is a group object, the children are the actual clippers
+            for (SPObject *child = (*o)->children ; child ; child = child->next) {
+                if (SP_IS_ITEM(child)) {
+                       SnapPointsWithType p_clip_or_mask;
+                    // Please note the recursive call here!
+                    sp_item_snappoints(SP_ITEM(child), target, p_clip_or_mask, snapprefs);
+                    // Take into account the transformation of the item being clipped or masked
+                    for (SnapPointsWithType::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
+                        // All snappoints are in desktop coordinates, but the item's transformation is
+                        // in document coordinates. Hence the awkward construction below
+                        Geom::Point pt = desktop->dt2doc((*p_orig).first) * sp_item_i2d_affine(item);
+                        p.push_back(std::make_pair(pt, (*p_orig).second));
+                    }
+                }
+            }
+        }
     }
 }
 
@@ -940,7 +1020,7 @@ sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
 {
     if (!item->isHidden()) {
         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
-            if (!item->transform.test_identity()
+            if (!item->transform.isIdentity()
                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
             {
                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
@@ -982,8 +1062,14 @@ sp_item_description(SPItem *item)
             g_free (s);
             s = snew;
         }
-        if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
-            gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
+        if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href && SP_OBJECT_STYLE(item)->filter.href->getObject()) {
+            const gchar *label = SP_OBJECT_STYLE(item)->filter.href->getObject()->label();
+            gchar *snew;
+            if (label) {
+                snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, _(label));
+            } else {
+                snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
+            }
             g_free (s);
             s = snew;
         }
@@ -1044,7 +1130,7 @@ sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
 
             // Update bbox, in case the clip uses bbox units
             NRRect bbox;
-            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
@@ -1063,15 +1149,14 @@ sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
 
             // Update bbox, in case the mask uses bbox units
             NRRect bbox;
-            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
         NR_ARENA_ITEM_SET_DATA(ai, item);
-        NRRect item_bbox;
-        sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
-        NR::Maybe<NR::Rect> i_bbox = item_bbox;
-        nr_arena_item_set_item_bbox(ai, i_bbox);
+        Geom::OptRect item_bbox;
+        sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
+        nr_arena_item_set_item_bbox(ai, item_bbox);
     }
 
     return ai;
@@ -1118,7 +1203,7 @@ sp_item_invoke_hide(SPItem *item, unsigned key)
 // Adjusters
 
 void
-sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
+sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
 {
     SPStyle *style = SP_OBJECT_STYLE (item);
 
@@ -1141,7 +1226,7 @@ sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
 }
 
 void
-sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
+sp_item_adjust_gradient (SPItem *item, Geom::Matrix const &postmul, bool set)
 {
     SPStyle *style = SP_OBJECT_STYLE (item);
 
@@ -1198,13 +1283,13 @@ sp_item_adjust_stroke (SPItem *item, gdouble ex)
 /**
  * Find out the inverse of previous transform of an item (from its repr)
  */
-NR::Matrix
+Geom::Matrix
 sp_item_transform_repr (SPItem *item)
 {
-    NR::Matrix t_old(NR::identity());
+    Geom::Matrix t_old(Geom::identity());
     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
     if (t_attr) {
-        NR::Matrix t;
+        Geom::Matrix t;
         if (sp_svg_transform_read(t_attr, &t)) {
             t_old = t;
         }
@@ -1236,7 +1321,7 @@ sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
  * Recursively adjust rx and ry of rects.
  */
 void
-sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
+sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
 {
     if (SP_IS_RECT (item)) {
         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
@@ -1252,13 +1337,13 @@ sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
  * Recursively compensate pattern or gradient transform.
  */
 void
-sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
+sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
 {
 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
-    NR::Matrix t_item = sp_item_transform_repr (item);
-    NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
+    Geom::Matrix t_item = sp_item_transform_repr (item);
+    Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
 
 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
 // also we do not recurse into clones, because a clone's child is the ghost of its original -
@@ -1286,7 +1371,7 @@ sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, N
 }
 
 void
-sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
+sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
 {
     if ( !SP_IS_LPE_ITEM(item) )
         return;
@@ -1299,32 +1384,21 @@ sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set
             // If the path effect is used by 2 or more items, fork it
             // so that each object has its own independent copy of the effect
             LivePathEffectObject *lpeobj = (*it)->lpeobject;
-            LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
-            if (new_lpeobj != lpeobj) {
-                sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
-            }
-        
-            if (lpeobj->lpe) {
-                Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
-                effect->transform_multiply(to_2geom(postmul), set);
+            if (lpeobj) {
+                LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
+                if (new_lpeobj != lpeobj) {
+                    sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
+                }
+
+                if (lpeobj->get_lpe()) {
+                    Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
+                    effect->transform_multiply(postmul, set);
+                }
             }
         }
     }
 }
 
-/**
- * A temporary wrapper for the next function accepting the NR::Matrix
- * instead of NR::Matrix
- */
-void
-sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
-{
-    if (transform == NULL)
-        sp_item_write_transform(item, repr, NR::identity(), adv);
-    else
-        sp_item_write_transform(item, repr, *transform, adv);
-}
-
 /**
  * Set a new transform on an object.
  *
@@ -1334,57 +1408,58 @@ sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix cons
  * the repr is updated with the new transform.
  */
 void
-sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
+sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
 {
     g_return_if_fail(item != NULL);
     g_return_if_fail(SP_IS_ITEM(item));
     g_return_if_fail(repr != NULL);
 
     // calculate the relative transform, if not given by the adv attribute
-    NR::Matrix advertized_transform;
+    Geom::Matrix advertized_transform;
     if (adv != NULL) {
         advertized_transform = *adv;
     } else {
         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
     }
 
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     if (compensate) {
 
          // recursively compensate for stroke scaling, depending on user preference
-        if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
-            double const expansion = 1. / NR::expansion(advertized_transform);
+        if (!prefs->getBool("/options/transform/stroke", true)) {
+            double const expansion = 1. / advertized_transform.descrim();
             sp_item_adjust_stroke_width_recursive(item, expansion);
         }
 
         // recursively compensate rx/ry of a rect if requested
-        if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
+        if (!prefs->getBool("/options/transform/rectcorners", true)) {
             sp_item_adjust_rects_recursive(item, advertized_transform);
         }
 
         // recursively compensate pattern fill if it's not to be transformed
-        if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
-            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
+        if (!prefs->getBool("/options/transform/pattern", true)) {
+            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
         }
         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
         /// recursively compensate gradient fill if it's not to be transformed
-        if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
-            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
+        if (!prefs->getBool("/options/transform/gradient", true)) {
+            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
         } else {
             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
             // it here _before_ the new transform is set, so as to use the pre-transform bbox
-            sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
+            sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
         }
 
     } // endif(compensate)
 
-    gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
-    NR::Matrix transform_attr (transform);
+    gint preserve = prefs->getBool("/options/preservetransform/value", 0);
+    Geom::Matrix transform_attr (transform);
     if ( // run the object's set_transform (i.e. embed transform) only if:
          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
              !preserve && // user did not chose to preserve all transforms
              !item->clip_ref->getObject() && // the object does not have a clippath
              !item->mask_ref->getObject() && // the object does not have a mask
-         !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
+         !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
         ) {
         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
@@ -1419,7 +1494,7 @@ sp_item_event(SPItem *item, SPEvent *event)
  * gradients, patterns as sp_item_write_transform does.
  */
 void
-sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
+sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
 {
     g_return_if_fail(item != NULL);
     g_return_if_fail(SP_IS_ITEM(item));
@@ -1454,30 +1529,31 @@ sp_item_convert_item_to_guides(SPItem *item) {
  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
  */
-NR::Matrix
+Geom::Matrix
 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
-    NR::Matrix ret(NR::identity());
+    Geom::Matrix ret(Geom::identity());
     g_return_val_if_fail(object != NULL, ret);
 
     /* stop at first non-renderable ancestor */
     while ( object != ancestor && SP_IS_ITEM(object) ) {
         if (SP_IS_ROOT(object)) {
             ret *= SP_ROOT(object)->c2p;
+        } else {
+            ret *= SP_ITEM(object)->transform;
         }
-        ret *= SP_ITEM(object)->transform;
         object = SP_OBJECT_PARENT(object);
     }
     return ret;
 }
 
-NR::Matrix
+Geom::Matrix
 i2i_affine(SPObject const *src, SPObject const *dest) {
-    g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
+    g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
     SPObject const *ancestor = src->nearestCommonAncestor(dest);
-    return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
+    return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
 }
 
-NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
+Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
     return i2i_affine(this, dest);
 }
 
@@ -1485,102 +1561,47 @@ NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
  * \pre (item != NULL) and SP_IS_ITEM(item).
  */
-NR::Matrix sp_item_i2doc_affine(SPItem const *item)
+Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
 {
     return i2anc_affine(item, NULL);
 }
 
 /**
- * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
- * Used in path operations mostly.
- * \pre (item != NULL) and SP_IS_ITEM(item).
+ * Returns the transformation from item to desktop coords
  */
-NR::Matrix sp_item_i2root_affine(SPItem const *item)
-{
-    g_assert(item != NULL);
-    g_assert(SP_IS_ITEM(item));
-
-    NR::Matrix ret(NR::identity());
-    g_assert(ret.test_identity());
-    while ( NULL != SP_OBJECT_PARENT(item) ) {
-        ret *= item->transform;
-        item = SP_ITEM(SP_OBJECT_PARENT(item));
-    }
-    g_assert(SP_IS_ROOT(item));
-
-    ret *= item->transform;
-
-    return ret;
-}
-
-/* fixme: This is EVIL!!! */
-
-NR::Matrix sp_item_i2d_affine(SPItem const *item)
-{
-    g_assert(item != NULL);
-    g_assert(SP_IS_ITEM(item));
-
-    NR::Matrix const ret( sp_item_i2doc_affine(item)
-                          * NR::scale(1, -1)
-                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
-    return ret;
-}
-
-// same as i2d but with i2root instead of i2doc
-NR::Matrix sp_item_i2r_affine(SPItem const *item)
+Geom::Matrix sp_item_i2d_affine(SPItem const *item)
 {
     g_assert(item != NULL);
     g_assert(SP_IS_ITEM(item));
 
-    NR::Matrix const ret( sp_item_i2root_affine(item)
-                          * NR::scale(1, -1)
-                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
-    return ret;
-}
-
-/**
- * Converts a matrix \a m into the desktop coords of the \a item.
- * Will become a noop when we eliminate the coordinate flipping.
- */
-NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
-{
-    NR::Matrix const ret(m
-                         * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
-                         * NR::scale(1, -1));
-    return ret;
-}
-
-/**
- * Converts a matrix \a m from the desktop coords of the \a item.
- * Will become a noop when we eliminate the coordinate flipping.
- */
-NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
-{
-    NR::Matrix const ret(NR::scale(1, -1)
-                         * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
-                         * m);
+    Geom::Matrix const ret( sp_item_i2doc_affine(item)
+                          * Geom::Scale(1, -1)
+                          * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
     return ret;
 }
 
-void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
+void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
 {
     g_return_if_fail( item != NULL );
     g_return_if_fail( SP_IS_ITEM(item) );
 
-    NR::Matrix dt2p; /* desktop to item parent transform */
+    Geom::Matrix dt2p; /* desktop to item parent transform */
     if (SP_OBJECT_PARENT(item)) {
         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
     } else {
-        dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
-                 * NR::scale(1, -1) );
+        dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
+                 * Geom::Scale(1, -1) );
     }
 
-    NR::Matrix const i2p( i2dt * dt2p );
+    Geom::Matrix const i2p( i2dt * dt2p );
     sp_item_set_item_transform(item, i2p);
 }
 
 
-NR::Matrix
+/**
+ * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
+ */
+Geom::Matrix
 sp_item_dt2i_affine(SPItem const *item)
 {
     /* fixme: Implement the right way (Lauris) */
@@ -1604,7 +1625,7 @@ sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigne
     new_view->next = list;
     new_view->flags = flags;
     new_view->key = key;
-    new_view->arenaitem = nr_arena_item_ref(arenaitem);
+    new_view->arenaitem = arenaitem;
 
     return new_view;
 }
@@ -1666,11 +1687,12 @@ sp_item_convert_to_guides(SPItem *item) {
     SPNamedView *nv = sp_desktop_namedview(dt);
     (void)nv;
 
-    int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
-    SPItem::BBoxType bbox_type = (prefs_bbox ==0)? 
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
+    SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
 
-    NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
+    Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
     if (!bbox) {
         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
         return;
@@ -1678,17 +1700,17 @@ sp_item_convert_to_guides(SPItem *item) {
 
     std::list<std::pair<Geom::Point, Geom::Point> > pts;
 
-    NR::Point A((*bbox).min());
-    NR::Point C((*bbox).max());
-    NR::Point B(A[NR::X], C[NR::Y]);
-    NR::Point D(C[NR::X], A[NR::Y]);
+    Geom::Point A((*bbox).min());
+    Geom::Point C((*bbox).max());
+    Geom::Point B(A[Geom::X], C[Geom::Y]);
+    Geom::Point D(C[Geom::X], A[Geom::Y]);
 
-    pts.push_back(std::make_pair(A.to_2geom(), B.to_2geom()));
-    pts.push_back(std::make_pair(B.to_2geom(), C.to_2geom()));
-    pts.push_back(std::make_pair(C.to_2geom(), D.to_2geom()));
-    pts.push_back(std::make_pair(D.to_2geom(), A.to_2geom()));
+    pts.push_back(std::make_pair(A, B));
+    pts.push_back(std::make_pair(B, C));
+    pts.push_back(std::make_pair(C, D));
+    pts.push_back(std::make_pair(D, A));
 
-    sp_guide_pt_pairs_to_guides(SP_OBJECT_DOCUMENT(item), pts);
+    sp_guide_pt_pairs_to_guides(dt, pts);
 }
 
 /*