1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(SVG)
#include "SVGTransformDistance.h"
#include "FloatConversion.h"
#include "FloatPoint.h"
#include "FloatSize.h"
#include "SVGTransform.h"
#include <math.h>
namespace WebCore {
SVGTransformDistance::SVGTransformDistance()
: m_type(SVGTransform::SVG_TRANSFORM_UNKNOWN)
, m_angle(0)
, m_cx(0)
, m_cy(0)
{
}
SVGTransformDistance::SVGTransformDistance(SVGTransform::SVGTransformType type, float angle, float cx, float cy, const AffineTransform& transform)
: m_type(type)
, m_angle(angle)
, m_cx(cx)
, m_cy(cy)
, m_transform(transform)
{
}
SVGTransformDistance::SVGTransformDistance(const SVGTransform& fromSVGTransform, const SVGTransform& toSVGTransform)
: m_type(fromSVGTransform.type())
, m_angle(0)
, m_cx(0)
, m_cy(0)
{
ASSERT(m_type == toSVGTransform.type());
switch (m_type) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return;
case SVGTransform::SVG_TRANSFORM_MATRIX:
// FIXME: need to be able to subtract to matrices
return;
case SVGTransform::SVG_TRANSFORM_ROTATE:
{
FloatSize centerDistance = toSVGTransform.rotationCenter() - fromSVGTransform.rotationCenter();
m_angle = toSVGTransform.angle() - fromSVGTransform.angle();
m_cx = centerDistance.width();
m_cy = centerDistance.height();
return;
}
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
{
FloatSize translationDistance = toSVGTransform.translate() - fromSVGTransform.translate();
m_transform.translate(translationDistance.width(), translationDistance.height());
return;
}
case SVGTransform::SVG_TRANSFORM_SCALE:
{
float scaleX = toSVGTransform.scale().width() - fromSVGTransform.scale().width();
float scaleY = toSVGTransform.scale().height() - fromSVGTransform.scale().height();
m_transform.scaleNonUniform(scaleX, scaleY);
return;
}
case SVGTransform::SVG_TRANSFORM_SKEWX:
case SVGTransform::SVG_TRANSFORM_SKEWY:
m_angle = toSVGTransform.angle() - fromSVGTransform.angle();
return;
}
}
SVGTransformDistance SVGTransformDistance::scaledDistance(float scaleFactor) const
{
switch (m_type) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return SVGTransformDistance();
case SVGTransform::SVG_TRANSFORM_ROTATE:
return SVGTransformDistance(m_type, m_angle * scaleFactor, m_cx * scaleFactor, m_cy * scaleFactor, AffineTransform());
case SVGTransform::SVG_TRANSFORM_SCALE:
case SVGTransform::SVG_TRANSFORM_MATRIX:
return SVGTransformDistance(m_type, m_angle * scaleFactor, m_cx * scaleFactor, m_cy * scaleFactor, AffineTransform(m_transform).scale(scaleFactor));
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
{
AffineTransform newTransform(m_transform);
newTransform.setE(m_transform.e() * scaleFactor);
newTransform.setF(m_transform.f() * scaleFactor);
return SVGTransformDistance(m_type, 0, 0, 0, newTransform);
}
case SVGTransform::SVG_TRANSFORM_SKEWX:
case SVGTransform::SVG_TRANSFORM_SKEWY:
return SVGTransformDistance(m_type, m_angle * scaleFactor, m_cx * scaleFactor, m_cy * scaleFactor, AffineTransform());
}
ASSERT_NOT_REACHED();
return SVGTransformDistance();
}
SVGTransform SVGTransformDistance::addSVGTransforms(const SVGTransform& first, const SVGTransform& second)
{
ASSERT(first.type() == second.type());
SVGTransform transform;
switch (first.type()) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return SVGTransform();
case SVGTransform::SVG_TRANSFORM_ROTATE:
{
transform.setRotate(first.angle() + second.angle(), first.rotationCenter().x() + second.rotationCenter().x(),
first.rotationCenter().y() + second.rotationCenter().y());
return transform;
}
case SVGTransform::SVG_TRANSFORM_MATRIX:
transform.setMatrix(first.matrix() * second.matrix());
return transform;
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
{
float dx = first.translate().x() + second.translate().x();
float dy = first.translate().y() + second.translate().y();
transform.setTranslate(dx, dy);
return transform;
}
case SVGTransform::SVG_TRANSFORM_SCALE:
{
FloatSize scale = first.scale() + second.scale();
transform.setScale(scale.width(), scale.height());
return transform;
}
case SVGTransform::SVG_TRANSFORM_SKEWX:
transform.setSkewX(first.angle() + second.angle());
return transform;
case SVGTransform::SVG_TRANSFORM_SKEWY:
transform.setSkewY(first.angle() + second.angle());
return transform;
}
ASSERT_NOT_REACHED();
return SVGTransform();
}
void SVGTransformDistance::addSVGTransform(const SVGTransform& transform, bool absoluteValue)
{
// If this is the first add, set the type for this SVGTransformDistance
if (m_type == SVGTransform::SVG_TRANSFORM_UNKNOWN)
m_type = transform.type();
ASSERT(m_type == transform.type());
switch (m_type) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return;
case SVGTransform::SVG_TRANSFORM_MATRIX:
m_transform *= transform.matrix(); // FIXME: what does 'distance' between two transforms mean? how should we respect 'absoluteValue' here?
return;
case SVGTransform::SVG_TRANSFORM_ROTATE:
m_angle += absoluteValue ? fabsf(transform.angle()) : transform.angle();
m_cx += absoluteValue ? fabsf(transform.rotationCenter().x()) : transform.rotationCenter().x();
m_cy += absoluteValue ? fabsf(transform.rotationCenter().y()) : transform.rotationCenter().y();
// fall through
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
{
float dx = absoluteValue ? fabsf(transform.translate().x()) : transform.translate().x();
float dy = absoluteValue ? fabsf(transform.translate().y()) : transform.translate().y();
m_transform.translate(dx, dy);
return;
}
case SVGTransform::SVG_TRANSFORM_SCALE:
{
float scaleX = absoluteValue ? fabsf(transform.scale().width()) : transform.scale().width();
float scaleY = absoluteValue ? fabsf(transform.scale().height()) : transform.scale().height();
m_transform.scaleNonUniform(scaleX, scaleY);
return;
}
case SVGTransform::SVG_TRANSFORM_SKEWX:
case SVGTransform::SVG_TRANSFORM_SKEWY:
m_angle += absoluteValue ? fabsf(transform.angle()) : transform.angle();
return;
}
ASSERT_NOT_REACHED();
return;
}
SVGTransform SVGTransformDistance::addToSVGTransform(const SVGTransform& transform) const
{
ASSERT(m_type == transform.type() || transform == SVGTransform());
SVGTransform newTransform(transform);
switch (m_type) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return SVGTransform();
case SVGTransform::SVG_TRANSFORM_MATRIX:
return SVGTransform(transform.matrix() * m_transform);
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
{
FloatPoint translation = transform.translate();
translation += FloatSize::narrowPrecision(m_transform.e(), m_transform.f());
newTransform.setTranslate(translation.x(), translation.y());
return newTransform;
}
case SVGTransform::SVG_TRANSFORM_SCALE:
{
FloatSize scale = transform.scale();
scale += FloatSize::narrowPrecision(m_transform.a(), m_transform.d());
newTransform.setScale(scale.width(), scale.height());
return newTransform;
}
case SVGTransform::SVG_TRANSFORM_ROTATE:
{
// FIXME: I'm not certain the translation is calculated correctly here
FloatPoint center = transform.rotationCenter();
newTransform.setRotate(transform.angle() + m_angle,
center.x() + m_cx,
center.y() + m_cy);
return newTransform;
}
case SVGTransform::SVG_TRANSFORM_SKEWX:
newTransform.setSkewX(transform.angle() + m_angle);
return newTransform;
case SVGTransform::SVG_TRANSFORM_SKEWY:
newTransform.setSkewY(transform.angle() + m_angle);
return newTransform;
}
ASSERT_NOT_REACHED();
return SVGTransform();
}
bool SVGTransformDistance::isZero() const
{
return (m_transform == AffineTransform() && m_angle == 0);
}
float SVGTransformDistance::distance() const
{
switch (m_type) {
case SVGTransform::SVG_TRANSFORM_UNKNOWN:
return 0.0f;
case SVGTransform::SVG_TRANSFORM_ROTATE:
return sqrtf(m_angle * m_angle + m_cx * m_cx + m_cy * m_cy);
case SVGTransform::SVG_TRANSFORM_MATRIX:
return 0.0f; // I'm not quite sure yet what distance between two matrices means.
case SVGTransform::SVG_TRANSFORM_SCALE:
return static_cast<float>(sqrt(m_transform.a() * m_transform.a() + m_transform.d() * m_transform.d()));
case SVGTransform::SVG_TRANSFORM_TRANSLATE:
return static_cast<float>(sqrt(m_transform.e() * m_transform.e() + m_transform.f() * m_transform.f()));
case SVGTransform::SVG_TRANSFORM_SKEWX:
case SVGTransform::SVG_TRANSFORM_SKEWY:
return m_angle;
}
ASSERT_NOT_REACHED();
return 0.0f;
}
}
#endif
|