Code

Extensions. XAML export improvements (flowRoot, text decoration, baseline alignement...
[inkscape.git] / src / sp-rect.cpp
index c568b44be7479f200f7ec54c1f9a5b3f08160c52..94be7551b3a00a5db40c24ada82fdb4ff99be567 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_RECT_C__
-
 /*
  * SVG <rect> implementation
  *
@@ -21,6 +19,7 @@
 #include <display/curve.h>
 #include <libnr/nr-matrix-ops.h>
 #include <libnr/nr-matrix-fns.h>
+#include <2geom/rect.h>
 
 #include "inkscape.h"
 #include "document.h"
@@ -47,7 +46,7 @@ static Geom::Matrix sp_rect_set_transform(SPItem *item, Geom::Matrix const &xfor
 static void sp_rect_convert_to_guides(SPItem *item);
 
 static void sp_rect_set_shape(SPShape *shape);
-static void sp_rect_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
+static void sp_rect_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
 
 static SPShapeClass *parent_class;
 
@@ -114,12 +113,12 @@ sp_rect_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
     if (((SPObjectClass *) parent_class)->build)
         ((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, "rx");
-    sp_object_read_attr(object, "ry");
+    object->readAttr( "x" );
+    object->readAttr( "y" );
+    object->readAttr( "width" );
+    object->readAttr( "height" );
+    object->readAttr( "rx" );
+    object->readAttr( "ry" );
 }
 
 static void
@@ -176,9 +175,8 @@ sp_rect_update(SPObject *object, SPCtx *ctx, guint flags)
         SPRect *rect = (SPRect *) object;
         SPStyle *style = object->style;
         SPItemCtx const *ictx = (SPItemCtx const *) ctx;
-        double const d = ictx->i2vp.descrim();
-        double const w = (ictx->vp.x1 - ictx->vp.x0) / d;
-        double const h = (ictx->vp.y1 - ictx->vp.y0) / d;
+        double const w = (ictx->vp.x1 - ictx->vp.x0);
+        double const h = (ictx->vp.y1 - ictx->vp.y0);
         double const em = style->font_size.computed;
         double const ex = 0.5 * em;  // fixme: get x height from pango or libnrtype.
         rect->x.update(em, ex, w);
@@ -187,7 +185,7 @@ sp_rect_update(SPObject *object, SPCtx *ctx, guint flags)
         rect->height.update(em, ex, h);
         rect->rx.update(em, ex, w);
         rect->ry.update(em, ex, h);
-        sp_shape_set_shape((SPShape *) object);
+        ((SPShape *) object)->setShape();
         flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
     }
 
@@ -233,7 +231,7 @@ sp_rect_set_shape(SPShape *shape)
     SPRect *rect = (SPRect *) shape;
 
     if ((rect->height.computed < 1e-18) || (rect->width.computed < 1e-18)) {
-        sp_shape_set_curve_insync(SP_SHAPE(rect), NULL, TRUE);
+        SP_SHAPE(rect)->setCurveInsync( NULL, TRUE);
         return;
     }
 
@@ -280,11 +278,10 @@ sp_rect_set_shape(SPShape *shape)
         c->lineto(x + w, y + 0.0);
         c->lineto(x + w, y + h);
         c->lineto(x + 0.0, y + h);
-        c->lineto(x + 0.0, y + 0.0);
     }
 
-    c->closepath_current();
-    sp_shape_set_curve_insync(SP_SHAPE(rect), c, TRUE);
+    c->closepath();
+    SP_SHAPE(rect)->setCurveInsync( c, TRUE);
     c->unref();
 }
 
@@ -382,13 +379,13 @@ sp_rect_set_transform(SPItem *item, Geom::Matrix const &xform)
     sp_rect_set_shape(rect);
 
     // Adjust stroke width
-    sp_item_adjust_stroke(item, sqrt(fabs(sw * sh)));
+    item->adjust_stroke(sqrt(fabs(sw * sh)));
 
     // Adjust pattern fill
-    sp_item_adjust_pattern(item, xform * ret.inverse());
+    item->adjust_pattern(xform * ret.inverse());
 
     // Adjust gradient fill
-    sp_item_adjust_gradient(item, xform * ret.inverse());
+    item->adjust_gradient(xform * ret.inverse());
 
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
 
@@ -461,6 +458,14 @@ sp_rect_get_visible_ry(SPRect *rect)
         SP_ITEM(rect)->transform);
 }
 
+Geom::Rect
+sp_rect_get_rect (SPRect *rect)
+{
+    Geom::Point p0 = Geom::Point(rect->x.computed, rect->y.computed);
+    Geom::Point p2 = Geom::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
+    return Geom::Rect(p0, p2);
+}
+
 void
 sp_rect_compensate_rxry(SPRect *rect, Geom::Matrix xform)
 {
@@ -546,26 +551,50 @@ sp_rect_get_visible_height(SPRect *rect)
 /**
  * Sets the snappoint p to the unrounded corners of the rectangle
  */
-static void sp_rect_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
+static void sp_rect_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
 {
     /* This method overrides sp_shape_snappoints, which is the default for any shape. The default method
     returns all eight points along the path of a rounded rectangle, but not the real corners. Snapping
-    the startpoint and endpoint of each rounded corner is not very usefull and really confusing. Instead 
+    the startpoint and endpoint of each rounded corner is not very useful and really confusing. Instead
     we could snap either the real corners, or not snap at all. Bulia Byak opted to snap the real corners,
-    but it should be noted that this might be confusing in some cases with relatively large radii. With 
+    but it should be noted that this might be confusing in some cases with relatively large radii. With
     small radii though the user will easily understand which point is snapping. */
-    
+
     g_assert(item != NULL);
     g_assert(SP_IS_RECT(item));
 
+    // Help enforcing strict snapping, i.e. only return nodes when we're snapping nodes to nodes or a guide to nodes
+    if (!(snapprefs->getSnapModeNode() || snapprefs->getSnapModeGuide())) {
+        return;
+    }
+
     SPRect *rect = SP_RECT(item);
 
-    Geom::Matrix const i2d (sp_item_i2d_affine (item));
+    Geom::Matrix const i2d (item->i2d_affine ());
+
+    Geom::Point p0 = Geom::Point(rect->x.computed, rect->y.computed) * i2d;
+    Geom::Point p1 = Geom::Point(rect->x.computed, rect->y.computed + rect->height.computed) * i2d;
+    Geom::Point p2 = Geom::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed) * i2d;
+    Geom::Point p3 = Geom::Point(rect->x.computed + rect->width.computed, rect->y.computed) * i2d;
+
+    if (snapprefs->getSnapToItemNode()) {
+        p.push_back(Inkscape::SnapCandidatePoint(p0, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
+        p.push_back(Inkscape::SnapCandidatePoint(p1, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
+        p.push_back(Inkscape::SnapCandidatePoint(p2, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
+        p.push_back(Inkscape::SnapCandidatePoint(p3, Inkscape::SNAPSOURCE_CORNER, Inkscape::SNAPTARGET_CORNER));
+    }
+
+    if (snapprefs->getSnapLineMidpoints()) { // only do this when we're snapping nodes (enforce strict snapping)
+        p.push_back(Inkscape::SnapCandidatePoint((p0 + p1)/2, Inkscape::SNAPSOURCE_LINE_MIDPOINT, Inkscape::SNAPTARGET_LINE_MIDPOINT));
+        p.push_back(Inkscape::SnapCandidatePoint((p1 + p2)/2, Inkscape::SNAPSOURCE_LINE_MIDPOINT, Inkscape::SNAPTARGET_LINE_MIDPOINT));
+        p.push_back(Inkscape::SnapCandidatePoint((p2 + p3)/2, Inkscape::SNAPSOURCE_LINE_MIDPOINT, Inkscape::SNAPTARGET_LINE_MIDPOINT));
+        p.push_back(Inkscape::SnapCandidatePoint((p3 + p0)/2, Inkscape::SNAPSOURCE_LINE_MIDPOINT, Inkscape::SNAPTARGET_LINE_MIDPOINT));
+    }
+
+    if (snapprefs->getSnapObjectMidpoints()) { // only do this when we're snapping nodes (enforce strict snapping)
+        p.push_back(Inkscape::SnapCandidatePoint((p0 + p2)/2, Inkscape::SNAPSOURCE_OBJECT_MIDPOINT, Inkscape::SNAPTARGET_OBJECT_MIDPOINT));
+    }
 
-    *p = Geom::Point(rect->x.computed, rect->y.computed) * i2d;
-    *p = Geom::Point(rect->x.computed, rect->y.computed + rect->height.computed) * i2d;
-    *p = Geom::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed) * i2d;
-    *p = Geom::Point(rect->x.computed + rect->width.computed, rect->y.computed) * i2d;
 }
 
 void
@@ -574,13 +603,13 @@ sp_rect_convert_to_guides(SPItem *item) {
 
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     if (!prefs->getBool("/tools/shapes/rect/convertguides", true)) {
-        sp_item_convert_to_guides(SP_ITEM(rect));
+        SP_ITEM(rect)->convert_to_guides();
         return;
     }
 
     std::list<std::pair<Geom::Point, Geom::Point> > pts;
 
-    Geom::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(rect)));
+    Geom::Matrix const i2d (SP_ITEM(rect)->i2d_affine());
 
     Geom::Point A1(Geom::Point(rect->x.computed, rect->y.computed) * i2d);
     Geom::Point A2(Geom::Point(rect->x.computed, rect->y.computed + rect->height.computed) * i2d);