Code

don't need svg 'load and save'. in fact, it doesnt fit
[inkscape.git] / src / sp-item-transform.cpp
index 817bc1b44e6f7cef4c1784ce0c75c77a44948f5f..d622112453a9b222ea4b610dd9ef382407d94cb5 100644 (file)
@@ -25,42 +25,51 @@ static NR::translate inverse(NR::translate const m)
        return NR::translate(-m[0], -m[1]);
 }
 
-void 
+void
 sp_item_rotate_rel(SPItem *item, NR::rotate const &rotation)
 {
-       NR::translate const s(sp_item_bbox_desktop(item).midpoint());
-
-       // Rotate item.
-       sp_item_set_i2d_affine(item,
-                              sp_item_i2d_affine(item) * inverse(s) * rotation * s);
-
-       // Use each item's own transform writer, consistent with sp_selection_apply_affine()
-       sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
+    NR::Point center = item->getCenter();
+    NR::translate const s(item->getCenter());
+    NR::Matrix affine = NR::Matrix(inverse(s)) * NR::Matrix(rotation) * NR::Matrix(s);
+
+    // Rotate item.
+    sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
+    // Use each item's own transform writer, consistent with sp_selection_apply_affine()
+    sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
+
+    // Restore the center position (it's changed because the bbox center changed)
+    if (item->isCenterSet()) {
+        item->setCenter(center * affine);
+    }
 }
 
 void
 sp_item_scale_rel (SPItem *item, NR::scale const &scale)
 {
-       NR::translate const s(sp_item_bbox_desktop(item).midpoint());
+    NR::translate const s(sp_item_bbox_desktop(item).midpoint()); // use getCenter?
 
        sp_item_set_i2d_affine(item,
                               sp_item_i2d_affine(item) * inverse(s) * scale * s);
        sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
-} 
+}
 
 void
 sp_item_skew_rel (SPItem *item, double skewX, double skewY)
 {
-       NR::Rect bbox(sp_item_bbox_desktop(item));
+    NR::Point center = item->getCenter();
+    NR::translate const s(item->getCenter());
 
-       NR::translate const s(bbox.midpoint());
+    NR::Matrix const skew(1, skewY, skewX, 1, 0, 0);
+    NR::Matrix affine = NR::Matrix(inverse(s)) * skew * NR::Matrix(s);
 
-       NR::Matrix const skew(1, skewY, skewX, 1, 0, 0);
+    sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
+    sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
 
-       sp_item_set_i2d_affine(item,
-                              sp_item_i2d_affine(item) * inverse(s) * skew * s);
-       sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
-} 
+    // Restore the center position (it's changed because the bbox center changed)
+    if (item->isCenterSet()) {
+        item->setCenter(center * affine);
+    }
+}
 
 void sp_item_move_rel(SPItem *item, NR::translate const &tr)
 {
@@ -76,7 +85,7 @@ preference value passed to it. Has to solve a quadratic equation to make sure
 the goal is met exactly and the stroke scaling is obeyed.
 */
 
-NR::Matrix 
+NR::Matrix
 get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool transform_stroke, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
 {
     NR::Rect bbox (bbox_param);
@@ -93,15 +102,17 @@ get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool
     gdouble h1 = y1 - y0;
     gdouble r0 = strokewidth;
 
-    if (bbox.isEmpty() || bbox.extent(NR::X) < 1e-06 || bbox.extent(NR::Y) < 1e-06 || 
-        fabs(w0 - r0) < 1e-6 || fabs(h0 - r0) < 1e-6 ||
-        (!transform_stroke && (fabs(w1 - r0) < 1e-6 || fabs(h1 - r0) < 1e-6)) 
-        ) {
+    if (bbox.isEmpty() || bbox.extent(NR::X) < 1e-06 || bbox.extent(NR::Y) < 1e-06) {
         NR::Matrix move = NR::Matrix(NR::translate(x0 - bbox.min()[NR::X], y0 - bbox.min()[NR::Y]));
-        return (move); // sorry, cannot scale from or to empty boxes, so only translate
+        return (move); // cannot scale from empty boxes at all, so only translate
     }
 
     NR::Matrix direct = NR::Matrix (NR::scale(w1 / w0,   h1 / h0));
+
+    if (fabs(w0 - r0) < 1e-6 || fabs(h0 - r0) < 1e-6 || (!transform_stroke && (fabs(w1 - r0) < 1e-6 || fabs(h1 - r0) < 1e-6))) {
+        return (p2o * direct * o2n); // can't solve the equation: one of the dimensions is equal to stroke width, so return the straightforward scaler
+    }
+
     gdouble ratio_x = (w1 - r0) / (w0 - r0);
     gdouble ratio_y = (h1 - r0) / (h0 - r0);
     NR::Matrix direct_constant_r = NR::Matrix (NR::scale(ratio_x, ratio_y));