Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / sp-item.cpp
index f24bf6ecc317f1584979a2c1182cf7bf976cdf43..f88d7b9e04b728f2047e9f031c752c98078f7275 100644 (file)
@@ -34,6 +34,8 @@
 #include "attributes.h"
 #include "document.h"
 #include "uri.h"
+#include "inkscape.h"
+#include "desktop-handles.h"
 
 #include "style.h"
 #include <glibmm/i18n.h>
@@ -51,9 +53,9 @@
 #include "conn-avoid-ref.h"
 #include "conditions.h"
 #include "sp-filter-reference.h"
+#include "filter-chemistry.h"
 #include "sp-guide.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/transforms.h>
 
 #include "xml/repr.h"
 #include "extract-uri.h"
 
 #include "live_effects/lpeobject.h"
 #include "live_effects/effect.h"
+#include "live_effects/lpeobject-reference.h"
 
 #define noSP_ITEM_DEBUG_IDLE
 
@@ -78,7 +83,7 @@ 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);
@@ -282,7 +287,7 @@ SPItem::setExplicitlyHidden(bool const val) {
  */
 void
 SPItem::setCenter(NR::Point object_centre) {
-    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+    boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
     if (bbox) {
         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
         if (fabs(transform_center_x) < 1e-5) // rounding error
@@ -304,7 +309,7 @@ bool SPItem::isCenterSet() {
 }
 
 NR::Point SPItem::getCenter() const {
-    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+    boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
     if (bbox) {
         return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
     } else {
@@ -409,8 +414,8 @@ sp_item_release(SPObject *object)
     item->_clip_ref_connection.disconnect();
     item->_mask_ref_connection.disconnect();
 
-    // Note: do this here before the clip_ref is deleted, since calling 
-    // sp_document_ensure_up_to_date for triggered routing may reference 
+    // Note: do this here before the clip_ref is deleted, since calling
+    // sp_document_ensure_up_to_date for triggered routing may reference
     // the deleted clip_ref.
     if (item->avoidRef) {
         delete item->avoidRef;
@@ -635,7 +640,7 @@ sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
     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;
+        boost::optional<NR::Rect> i_bbox = item_bbox;
 
         SPItemView *itemview = item->display;
         do {
@@ -650,7 +655,7 @@ 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)
 {
     SPItem *item = SP_ITEM(object);
 
@@ -682,28 +687,28 @@ 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,
+boost::optional<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
                                       SPItem::BBoxType type,
                                       unsigned int /*dkey*/) const
 {
-    NR::Maybe<NR::Rect> r = NR::Nothing();
+    boost::optional<NR::Rect> 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, boost::optional<NR::Rect> *bbox, NR::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 boost::optional<NR::Rect>
 void
 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
 {
@@ -715,14 +720,14 @@ sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transfor
  * 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, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
+sp_item_invoke_bbox_full(SPItem const *item, boost::optional<NR::Rect> *bbox, NR::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 = boost::optional<NR::Rect>();
     }
 
     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
@@ -736,37 +741,89 @@ sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matr
         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
     }
 
-    // unless this is geometric bbox, crop the bbox by clip path, if any
-    if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
-        NRRect b;
-        sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
-        nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
+    // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
+    if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
+        if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
+            SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
+            if (filter && SP_IS_FILTER(filter)) {
+                // default filer area per the SVG spec:
+                double x = -0.1;
+                double y = -0.1;
+                double w = 1.2;
+                double h = 1.2;
+
+                // if area is explicitly set, override:
+                if (SP_FILTER(filter)->x._set)
+                    x = SP_FILTER(filter)->x.computed;
+                if (SP_FILTER(filter)->y._set)
+                    y = SP_FILTER(filter)->y.computed;
+                if (SP_FILTER(filter)->width._set)
+                    w = SP_FILTER(filter)->width.computed;
+                if (SP_FILTER(filter)->height._set) 
+                    h = SP_FILTER(filter)->height.computed;
+
+                double dx0 = 0;
+                double dx1 = 0;
+                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 
+                    // 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));
+                    dx0 = -2.4 * r;
+                    dx1 = 2.4 * r;
+                    dy0 = -2.4 * r;
+                    dy1 = 2.4 * r;
+                } else {
+                    // otherwise, calculate expansion from relative to absolute units:
+                    dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
+                    dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
+                    dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
+                    dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
+                }
+
+                // 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);
+
+                // expand the bbox
+                temp_bbox.x0 += dx0;
+                temp_bbox.x1 += dx1;
+                temp_bbox.y0 += dy0;
+                temp_bbox.y1 += dy1;
+            }
+        }
+        if (item->clip_ref->getObject()) {
+            NRRect b;
+            sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
+            nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
+        }
     }
-    
+
     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
-        // We'll assume here that when x0 > x1 or y0 > y1, the bbox is "nothing"
-        // However it has never been explicitely defined this way for NRRects
-        // (as opposed to NR::Maybe<NR::Rect>)
-        *bbox = NR::Nothing();
-        return;
-    } 
-    
-    if (temp_bbox.x0 == temp_bbox.y0 == NR_HUGE && temp_bbox.x1 == temp_bbox.y1 == -NR_HUGE) {
-        // 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)
-        *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 boost::optional<NR::Rect>)
+        // 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 boost::optional<NR::Rect>()
     // 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 boost::optional<NR::Rect>() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
+    boost::optional<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
+
     *bbox = NR::union_bounds(*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 boost::optional<NR::Rect>
 /** 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,
@@ -840,16 +897,16 @@ 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)
+boost::optional<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
 {
-    NR::Maybe<NR::Rect> rect = NR::Nothing();
+    boost::optional<NR::Rect> rect = boost::optional<NR::Rect>();
     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)
 {
-    NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
+    boost::optional<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. */
 
@@ -866,16 +923,43 @@ static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
 
 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
 {
-    g_assert (item != NULL);
+       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);
     }
 
+    // Get the snappoints at the item's center
     if (includeItemCenter) {
        *p = item->getCenter();
+    }    
+    
+    // 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()));
+    
+    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)) {
+                       std::vector<NR::Point> p_clip_or_mask;                      
+                       // Please note the recursive call here!
+                       sp_item_snappoints(SP_ITEM(child), includeItemCenter, SnapPointsIter(p_clip_or_mask));
+                       // Take into account the transformation of the item being clipped or masked
+                       for (std::vector<NR::Point>::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
+                               *p = (*p_orig) * from_2geom(matrix_to_desktop (matrix_from_desktop (item->transform, item), item));
+                   }
+                   }
+               }
+       }
     }
 }
 
@@ -926,6 +1010,11 @@ 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);
+            g_free (s);
+            s = snew;
+        }
         return s;
     }
 
@@ -1009,7 +1098,7 @@ sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
         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;
+        boost::optional<NR::Rect> i_bbox = item_bbox;
         nr_arena_item_set_item_bbox(ai, i_bbox);
     }
 
@@ -1227,35 +1316,43 @@ 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)
 {
-    if ( !SP_IS_SHAPE(item) )
+    if ( !SP_IS_LPE_ITEM(item) )
         return;
 
-    SPShape *shape = SP_SHAPE (item);
-    if ( sp_shape_has_path_effect(shape) ) {
-        LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
-        LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
-        if (new_lpeobj != lpeobj) {
-            sp_shape_set_path_effect(shape, new_lpeobj);
-        }
-
-        Inkscape::LivePathEffect::Effect * effect = sp_shape_get_livepatheffect(shape);
-        if (effect) {
-            effect->transform_multiply (to_2geom(postmul), set);
+    SPLPEItem *lpeitem = SP_LPE_ITEM (item);
+    if ( sp_lpe_item_has_path_effect(lpeitem) ) {
+        PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
+        for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
+        {
+            // 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;
+            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->lpe) {
+                    Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
+                    effect->transform_multiply(postmul, set);
+                }
+            }
         }
     }
 }
 
 /**
- * A temporary wrapper for the next function accepting the NRMatrix
+ * 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, NRMatrix const *transform, NR::Matrix const *adv)
+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, NR::Matrix (transform), adv);
+        sp_item_write_transform(item, repr, *transform, adv);
 }
 
 /**
@@ -1366,14 +1463,30 @@ sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
     }
 }
 
+void
+sp_item_convert_item_to_guides(SPItem *item) {
+    g_return_if_fail(item != NULL);
+    g_return_if_fail(SP_IS_ITEM(item));
+
+    /* Use derived method if present ... */
+    if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
+        (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
+        return;
+    }
+
+    /* .. otherwise simply place the guides around the item's bounding box */
+
+    sp_item_convert_to_guides(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 */
@@ -1387,11 +1500,11 @@ i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
     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 {
@@ -1402,7 +1515,7 @@ 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);
 }
@@ -1412,13 +1525,13 @@ NR::Matrix sp_item_i2doc_affine(SPItem const *item)
  * Used in path operations mostly.
  * \pre (item != NULL) and SP_IS_ITEM(item).
  */
-NR::Matrix sp_item_i2root_affine(SPItem const *item)
+Geom::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());
+    Geom::Matrix ret(Geom::identity());
+    g_assert(ret.isIdentity());
     while ( NULL != SP_OBJECT_PARENT(item) ) {
         ret *= item->transform;
         item = SP_ITEM(SP_OBJECT_PARENT(item));
@@ -1431,27 +1544,27 @@ NR::Matrix sp_item_i2root_affine(SPItem const *item)
 }
 
 /* fixme: This is EVIL!!! */
-
-NR::Matrix sp_item_i2d_affine(SPItem const *item)
+// fix this note: why/what evil? :)
+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_i2doc_affine(item)
-                          * NR::scale(1, -1)
-                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
+    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;
 }
 
 // same as i2d but with i2root instead of i2doc
-NR::Matrix sp_item_i2r_affine(SPItem const *item)
+Geom::Matrix sp_item_i2r_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))) );
+    Geom::Matrix const ret( sp_item_i2root_affine(item)
+                          * Geom::Scale(1, -1)
+                          * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
     return ret;
 }
 
@@ -1459,11 +1572,11 @@ NR::Matrix sp_item_i2r_affine(SPItem const *item)
  * 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)
+Geom::Matrix matrix_to_desktop(Geom::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));
+    Geom::Matrix const ret(m
+                         * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
+                         * Geom::Scale(1, -1));
     return ret;
 }
 
@@ -1471,33 +1584,33 @@ NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
  * 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)
+Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
 {
-    NR::Matrix const ret(NR::scale(1, -1)
-                         * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
+    Geom::Matrix const ret(Geom::Scale(1, -1)
+                         * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
                          * m);
     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
+Geom::Matrix
 sp_item_dt2i_affine(SPItem const *item)
 {
     /* fixme: Implement the right way (Lauris) */
@@ -1579,9 +1692,17 @@ sp_item_first_item_child (SPObject *obj)
 
 void
 sp_item_convert_to_guides(SPItem *item) {
-    NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, SPItem::GEOMETRIC_BBOX);
+    SPDesktop *dt = inkscape_active_desktop();
+    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)? 
+        SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
+
+    boost::optional<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
     if (!bbox) {
-        g_print ("Cannot determine bounding box. Doing nothing.\n");
+        g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
         return;
     }
 
@@ -1592,14 +1713,12 @@ sp_item_convert_to_guides(SPItem *item) {
     NR::Point B(A[NR::X], C[NR::Y]);
     NR::Point D(C[NR::X], A[NR::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_OBJECT(item)->deleteObject(true);
+    sp_guide_pt_pairs_to_guides(dt, pts);
 }
 
 /*