1 #define __SP_ITEM_TRANSFORM_C__
3 /*
4 * Transforming single items
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * bulia byak <buliabyak@gmail.com>
10 *
11 * Copyright (C) 1999-2005 authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <libnr/nr-matrix-ops.h>
17 #include "libnr/nr-matrix-rotate-ops.h"
18 #include "libnr/nr-matrix-scale-ops.h"
19 #include "libnr/nr-matrix-translate-ops.h"
20 #include "sp-item.h"
22 static NR::translate inverse(NR::translate const m)
23 {
24 /* TODO: Move this to nr-matrix-fns.h or the like. */
25 return NR::translate(-m[0], -m[1]);
26 }
28 void
29 sp_item_rotate_rel(SPItem *item, NR::rotate const &rotation)
30 {
31 NR::Point center = item->getCenter();
32 NR::translate const s(item->getCenter());
33 NR::Matrix affine = NR::Matrix(inverse(s)) * NR::Matrix(rotation) * NR::Matrix(s);
35 // Rotate item.
36 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
37 // Use each item's own transform writer, consistent with sp_selection_apply_affine()
38 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
40 // Restore the center position (it's changed because the bbox center changed)
41 if (item->isCenterSet()) {
42 item->setCenter(center * affine);
43 }
44 }
46 void
47 sp_item_scale_rel (SPItem *item, NR::scale const &scale)
48 {
49 NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item);
50 if (bbox) {
51 NR::translate const s(bbox->midpoint()); // use getCenter?
52 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * inverse(s) * scale * s);
53 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
54 }
55 }
57 void
58 sp_item_skew_rel (SPItem *item, double skewX, double skewY)
59 {
60 NR::Point center = item->getCenter();
61 NR::translate const s(item->getCenter());
63 NR::Matrix const skew(1, skewY, skewX, 1, 0, 0);
64 NR::Matrix affine = NR::Matrix(inverse(s)) * skew * NR::Matrix(s);
66 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
67 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
69 // Restore the center position (it's changed because the bbox center changed)
70 if (item->isCenterSet()) {
71 item->setCenter(center * affine);
72 }
73 }
75 void sp_item_move_rel(SPItem *item, NR::translate const &tr)
76 {
77 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * tr);
79 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
80 }
82 /*
83 ** Returns the matrix you need to apply to an object with given bbox and strokewidth to
84 scale/move it to the new box x0/y0/x1/y1. Takes into account the "scale stroke"
85 preference value passed to it. Has to solve a quadratic equation to make sure
86 the goal is met exactly and the stroke scaling is obeyed.
87 */
89 NR::Matrix
90 get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool transform_stroke, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
91 {
92 NR::Rect bbox (bbox_param);
94 NR::Matrix p2o = NR::Matrix (NR::translate (-bbox.min()));
95 NR::Matrix o2n = NR::Matrix (NR::translate (x0, y0));
97 NR::Matrix scale = NR::Matrix (NR::scale (1, 1)); // scale component
98 NR::Matrix unbudge = NR::Matrix (NR::translate (0, 0)); // move component to compensate for the drift caused by stroke width change
100 gdouble w0 = bbox.extent(NR::X); // will return a value >= 0, as required further down the road
101 gdouble h0 = bbox.extent(NR::Y);
102 gdouble w1 = x1 - x0; // can have any sign
103 gdouble h1 = y1 - y0;
104 gdouble r0 = strokewidth;
106 if (bbox.isEmpty()) {
107 NR::Matrix move = NR::Matrix(NR::translate(x0 - bbox.min()[NR::X], y0 - bbox.min()[NR::Y]));
108 return (move); // cannot scale from empty boxes at all, so only translate
109 }
111 NR::Matrix direct = NR::Matrix (NR::scale(w1 / w0, h1 / h0));
113 if (fabs(w0 - r0) < 1e-6 || fabs(h0 - r0) < 1e-6 || (!transform_stroke && (fabs(w1 - r0) < 1e-6 || fabs(h1 - r0) < 1e-6))) {
114 return (p2o * direct * o2n); // can't solve the equation: one of the dimensions is equal to stroke width, so return the straightforward scaler
115 }
117 int flip_x = (w1 > 0) ? 1 : -1;
118 int flip_y = (h1 > 0) ? 1 : -1;
120 // w1 and h1 will be negative when mirroring, but if so then e.g. w1-r0 won't make sense
121 // Therefore we will use the absolute values from this point on
122 w1 = fabs(w1);
123 h1 = fabs(h1);
124 r0 = fabs(r0);
125 // w0 and h0 will always be positive due to the definition extent()
127 gdouble ratio_x = (w1 - r0) / (w0 - r0);
128 gdouble ratio_y = (h1 - r0) / (h0 - r0);
130 NR::Matrix direct_constant_r = NR::Matrix (NR::scale(flip_x * ratio_x, flip_y * ratio_y));
132 if (transform_stroke && r0 != 0 && r0 != NR_HUGE) { // there's stroke, and we need to scale it
133 // These coefficients are obtained from the assumption that scaling applies to the
134 // non-stroked "shape proper" and that stroke scale is scaled by the expansion of that
135 // matrix. We're trying to solve this equation:
136 // r1 = r0 * sqrt (((w1-r0)/(w0-r0))*((h1-r1)/(h0-r0)))
137 // The operant of the sqrt() must be positive, which is ensured by the fabs() a few lines above
138 gdouble A = -w0*h0 + r0*(w0 + h0);
139 gdouble B = -(w1 + h1) * r0*r0;
140 gdouble C = w1 * h1 * r0*r0;
141 if (B*B - 4*A*C > 0) {
142 gdouble r1 = fabs((-B - sqrt(B*B - 4*A*C))/(2*A));
143 //gdouble r2 = (-B + sqrt (B*B - 4*A*C))/(2*A);
144 //std::cout << "r0" << r0 << " r1" << r1 << " r2" << r2 << "\n";
145 //
146 // If w1 < 0 then the scale will be wrong if we just do
147 // gdouble scale_x = (w1 - r1)/(w0 - r0);
148 // Here we also need the absolute values of w0, w1, h0, h1, and r1
149 gdouble scale_x = (w1 - r1)/(w0 - r0);
150 gdouble scale_y = (h1 - r1)/(h0 - r0);
151 scale *= NR::scale(flip_x * scale_x, flip_y * scale_y);
152 unbudge *= NR::translate (-flip_x * 0.5 * (r0 * scale_x - r1), -flip_y * 0.5 * (r0 * scale_y - r1));
153 } else {
154 scale *= direct;
155 }
156 } else {
157 if (r0 == 0 || r0 == NR_HUGE) { // no stroke to scale
158 scale *= direct;
159 } else {// nonscaling strokewidth
160 scale *= direct_constant_r;
161 unbudge *= NR::translate (flip_x * 0.5 * r0 * (1 - ratio_x), flip_y * 0.5 * r0 * (1 - ratio_y));
162 }
163 }
165 return (p2o * scale * unbudge * o2n);
166 }
168 /*
169 Local Variables:
170 mode:c++
171 c-file-style:"stroustrup"
172 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
173 indent-tabs-mode:nil
174 fill-column:99
175 End:
176 */
177 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :