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::translate const s(sp_item_bbox_desktop(item).midpoint());
33 // Rotate item.
34 sp_item_set_i2d_affine(item,
35 sp_item_i2d_affine(item) * inverse(s) * rotation * s);
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);
39 }
41 void
42 sp_item_scale_rel (SPItem *item, NR::scale const &scale)
43 {
44 NR::translate const s(sp_item_bbox_desktop(item).midpoint());
46 sp_item_set_i2d_affine(item,
47 sp_item_i2d_affine(item) * inverse(s) * scale * s);
48 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
49 }
51 void
52 sp_item_skew_rel (SPItem *item, double skewX, double skewY)
53 {
54 NR::Rect bbox(sp_item_bbox_desktop(item));
56 NR::translate const s(bbox.midpoint());
58 NR::Matrix const skew(1, skewY, skewX, 1, 0, 0);
60 sp_item_set_i2d_affine(item,
61 sp_item_i2d_affine(item) * inverse(s) * skew * s);
62 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
63 }
65 void sp_item_move_rel(SPItem *item, NR::translate const &tr)
66 {
67 sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * tr);
69 sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
70 }
72 /*
73 ** Returns the matrix you need to apply to an object with given bbox and strokewidth to
74 scale/move it to the new box x0/y0/x1/y1. Takes into account the "scale stroke"
75 preference value passed to it. Has to solve a quadratic equation to make sure
76 the goal is met exactly and the stroke scaling is obeyed.
77 */
79 NR::Matrix
80 get_scale_transform_with_stroke (NR::Rect &bbox_param, gdouble strokewidth, bool transform_stroke, gdouble x0, gdouble y0, gdouble x1, gdouble y1)
81 {
82 NR::Rect bbox (bbox_param);
84 NR::Matrix p2o = NR::Matrix (NR::translate (-bbox.min()));
85 NR::Matrix o2n = NR::Matrix (NR::translate (x0, y0));
87 NR::Matrix scale = NR::Matrix (NR::scale (1, 1)); // scale component
88 NR::Matrix unbudge = NR::Matrix (NR::translate (0, 0)); // move component to compensate for the drift caused by stroke width change
90 gdouble w0 = bbox.extent(NR::X);
91 gdouble h0 = bbox.extent(NR::Y);
92 gdouble w1 = x1 - x0;
93 gdouble h1 = y1 - y0;
94 gdouble r0 = strokewidth;
96 if (bbox.isEmpty() || bbox.extent(NR::X) < 1e-06 || bbox.extent(NR::Y) < 1e-06 ||
97 fabs(w0 - r0) < 1e-6 || fabs(h0 - r0) < 1e-6 ||
98 (!transform_stroke && (fabs(w1 - r0) < 1e-6 || fabs(h1 - r0) < 1e-6))
99 ) {
100 NR::Matrix move = NR::Matrix(NR::translate(x0 - bbox.min()[NR::X], y0 - bbox.min()[NR::Y]));
101 return (move); // sorry, cannot scale from or to empty boxes, so only translate
102 }
104 NR::Matrix direct = NR::Matrix (NR::scale(w1 / w0, h1 / h0));
105 gdouble ratio_x = (w1 - r0) / (w0 - r0);
106 gdouble ratio_y = (h1 - r0) / (h0 - r0);
107 NR::Matrix direct_constant_r = NR::Matrix (NR::scale(ratio_x, ratio_y));
109 if (transform_stroke && r0 != 0 && r0 != NR_HUGE) { // there's stroke, and we need to scale it
110 // These coefficients are obtained from the assumption that scaling applies to the
111 // non-stroked "shape proper" and that stroke scale is scaled by the expansion of that
112 // matrix
113 gdouble A = -(w0 *h0) + r0*(w0 + h0);
114 gdouble B = -(w1 + h1) * r0*r0;
115 gdouble C = w1 * h1 * r0*r0;
116 if (B*B - 4*A*C > 0) {
117 gdouble r1 = (-B - sqrt (B*B - 4*A*C))/(2*A);
118 //gdouble r2 = (-B + sqrt (B*B - 4*A*C))/(2*A);
119 //std::cout << "r0" << r0 << " r1" << r1 << " r2" << r2 << "\n";
120 gdouble scale_x = (w1 - r1)/(w0 - r0);
121 gdouble scale_y = (h1 - r1)/(h0 - r0);
122 scale *= NR::scale(scale_x, scale_y);
123 unbudge *= NR::translate (-0.5 * (r0 * scale_x - r1), -0.5 * (r0 * scale_y - r1));
124 } else {
125 scale *= direct;
126 }
127 } else {
128 if (r0 == 0 || r0 == NR_HUGE) { // no stroke to scale
129 scale *= direct;
130 } else {// nonscaling strokewidth
131 scale *= direct_constant_r;
132 unbudge *= NR::translate (0.5 * r0 * (1 - ratio_x), 0.5 * r0 * (1 - ratio_y));
133 }
134 }
136 return (p2o * scale * unbudge * o2n);
137 }
139 /*
140 Local Variables:
141 mode:c++
142 c-file-style:"stroustrup"
143 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
144 indent-tabs-mode:nil
145 fill-column:99
146 End:
147 */
148 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :