2 Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4 This file is part of the WebKit project
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
23 #include "wtf/Platform.h"
25 #include "SVGTransformDistance.h"
27 #include "FloatConversion.h"
28 #include "FloatPoint.h"
29 #include "FloatSize.h"
30 #include "SVGTransform.h"
36 SVGTransformDistance::SVGTransformDistance()
37 : m_type(SVGTransform::SVG_TRANSFORM_UNKNOWN
)
42 SVGTransformDistance::SVGTransformDistance(SVGTransform::SVGTransformType type
, float angle
, float cx
, float cy
, const AffineTransform
& transform
)
47 , m_transform(transform
)
51 SVGTransformDistance::SVGTransformDistance(const SVGTransform
& fromSVGTransform
, const SVGTransform
& toSVGTransform
)
52 : m_type(fromSVGTransform
.type())
57 ASSERT(m_type
== toSVGTransform
.type());
60 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
62 case SVGTransform::SVG_TRANSFORM_MATRIX
:
63 // FIXME: need to be able to subtract to matrices
65 case SVGTransform::SVG_TRANSFORM_ROTATE
:
67 FloatSize centerDistance
= toSVGTransform
.rotationCenter() - fromSVGTransform
.rotationCenter();
68 m_angle
= toSVGTransform
.angle() - fromSVGTransform
.angle();
69 m_cx
= centerDistance
.width();
70 m_cy
= centerDistance
.height();
73 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
75 FloatSize translationDistance
= toSVGTransform
.translate() - fromSVGTransform
.translate();
76 m_transform
.translate(translationDistance
.width(), translationDistance
.height());
79 case SVGTransform::SVG_TRANSFORM_SCALE
:
81 float scaleX
= toSVGTransform
.scale().width() - fromSVGTransform
.scale().width();
82 float scaleY
= toSVGTransform
.scale().height() - fromSVGTransform
.scale().height();
83 m_transform
.scale(scaleX
, scaleY
);
86 case SVGTransform::SVG_TRANSFORM_SKEWX
:
87 case SVGTransform::SVG_TRANSFORM_SKEWY
:
88 m_angle
= toSVGTransform
.angle() - fromSVGTransform
.angle();
93 SVGTransformDistance
SVGTransformDistance::scaledDistance(float scaleFactor
) const
96 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
97 return SVGTransformDistance();
98 case SVGTransform::SVG_TRANSFORM_ROTATE
:
99 return SVGTransformDistance(m_type
, m_angle
* scaleFactor
, m_cx
* scaleFactor
, m_cy
* scaleFactor
, AffineTransform());
100 case SVGTransform::SVG_TRANSFORM_SCALE
:
101 case SVGTransform::SVG_TRANSFORM_MATRIX
:
102 return SVGTransformDistance(m_type
, m_angle
* scaleFactor
, m_cx
* scaleFactor
, m_cy
* scaleFactor
, AffineTransform(m_transform
).scale(scaleFactor
));
103 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
105 AffineTransform
newTransform(m_transform
);
106 newTransform
.setE(m_transform
.e() * scaleFactor
);
107 newTransform
.setF(m_transform
.f() * scaleFactor
);
108 return SVGTransformDistance(m_type
, 0, 0, 0, newTransform
);
110 case SVGTransform::SVG_TRANSFORM_SKEWX
:
111 case SVGTransform::SVG_TRANSFORM_SKEWY
:
112 return SVGTransformDistance(m_type
, m_angle
* scaleFactor
, m_cx
* scaleFactor
, m_cy
* scaleFactor
, AffineTransform());
115 ASSERT_NOT_REACHED();
116 return SVGTransformDistance();
119 SVGTransform
SVGTransformDistance::addSVGTransforms(const SVGTransform
& first
, const SVGTransform
& second
)
121 ASSERT(first
.type() == second
.type());
123 SVGTransform transform
;
125 switch (first
.type()) {
126 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
127 return SVGTransform();
128 case SVGTransform::SVG_TRANSFORM_ROTATE
:
130 transform
.setRotate(first
.angle() + second
.angle(), first
.rotationCenter().x() + second
.rotationCenter().x(),
131 first
.rotationCenter().y() + second
.rotationCenter().y());
134 case SVGTransform::SVG_TRANSFORM_MATRIX
:
135 transform
.setMatrix(first
.matrix() * second
.matrix());
137 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
139 float dx
= first
.translate().x() + second
.translate().x();
140 float dy
= first
.translate().y() + second
.translate().y();
141 transform
.setTranslate(dx
, dy
);
144 case SVGTransform::SVG_TRANSFORM_SCALE
:
146 FloatSize scale
= first
.scale() + second
.scale();
147 transform
.setScale(scale
.width(), scale
.height());
150 case SVGTransform::SVG_TRANSFORM_SKEWX
:
151 transform
.setSkewX(first
.angle() + second
.angle());
153 case SVGTransform::SVG_TRANSFORM_SKEWY
:
154 transform
.setSkewY(first
.angle() + second
.angle());
158 ASSERT_NOT_REACHED();
159 return SVGTransform();
162 void SVGTransformDistance::addSVGTransform(const SVGTransform
& transform
, bool absoluteValue
)
164 // If this is the first add, set the type for this SVGTransformDistance
165 if (m_type
== SVGTransform::SVG_TRANSFORM_UNKNOWN
)
166 m_type
= transform
.type();
168 ASSERT(m_type
== transform
.type());
171 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
173 case SVGTransform::SVG_TRANSFORM_MATRIX
:
174 m_transform
*= transform
.matrix(); // FIXME: what does 'distance' between two transforms mean? how should we respect 'absoluteValue' here?
176 case SVGTransform::SVG_TRANSFORM_ROTATE
:
177 m_angle
+= absoluteValue
? fabsf(transform
.angle()) : transform
.angle();
178 m_cx
+= absoluteValue
? fabsf(transform
.rotationCenter().x()) : transform
.rotationCenter().x();
179 m_cy
+= absoluteValue
? fabsf(transform
.rotationCenter().y()) : transform
.rotationCenter().y();
181 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
183 float dx
= absoluteValue
? fabsf(transform
.translate().x()) : transform
.translate().x();
184 float dy
= absoluteValue
? fabsf(transform
.translate().y()) : transform
.translate().y();
185 m_transform
.translate(dx
, dy
);
188 case SVGTransform::SVG_TRANSFORM_SCALE
:
190 float scaleX
= absoluteValue
? fabsf(transform
.scale().width()) : transform
.scale().width();
191 float scaleY
= absoluteValue
? fabsf(transform
.scale().height()) : transform
.scale().height();
192 m_transform
.scale(scaleX
, scaleY
);
195 case SVGTransform::SVG_TRANSFORM_SKEWX
:
196 case SVGTransform::SVG_TRANSFORM_SKEWY
:
197 m_angle
+= absoluteValue
? fabsf(transform
.angle()) : transform
.angle();
201 ASSERT_NOT_REACHED();
205 SVGTransform
SVGTransformDistance::addToSVGTransform(const SVGTransform
& transform
) const
207 ASSERT(m_type
== transform
.type() || transform
== SVGTransform());
209 SVGTransform
newTransform(transform
);
212 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
213 return SVGTransform();
214 case SVGTransform::SVG_TRANSFORM_MATRIX
:
215 return SVGTransform(transform
.matrix() * m_transform
);
216 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
218 FloatPoint translation
= transform
.translate();
219 translation
+= FloatSize::narrowPrecision(m_transform
.e(), m_transform
.f());
220 newTransform
.setTranslate(translation
.x(), translation
.y());
223 case SVGTransform::SVG_TRANSFORM_SCALE
:
225 FloatSize scale
= transform
.scale();
226 scale
+= FloatSize::narrowPrecision(m_transform
.a(), m_transform
.d());
227 newTransform
.setScale(scale
.width(), scale
.height());
230 case SVGTransform::SVG_TRANSFORM_ROTATE
:
232 // FIXME: I'm not certain the translation is calculated correctly here
233 FloatPoint center
= transform
.rotationCenter();
234 newTransform
.setRotate(transform
.angle() + m_angle
,
239 case SVGTransform::SVG_TRANSFORM_SKEWX
:
240 newTransform
.setSkewX(transform
.angle() + m_angle
);
242 case SVGTransform::SVG_TRANSFORM_SKEWY
:
243 newTransform
.setSkewY(transform
.angle() + m_angle
);
247 ASSERT_NOT_REACHED();
248 return SVGTransform();
251 bool SVGTransformDistance::isZero() const
253 return (m_transform
== AffineTransform() && m_angle
== 0);
256 float SVGTransformDistance::distance() const
259 case SVGTransform::SVG_TRANSFORM_UNKNOWN
:
261 case SVGTransform::SVG_TRANSFORM_ROTATE
:
262 return sqrtf(m_angle
* m_angle
+ m_cx
* m_cx
+ m_cy
* m_cy
);
263 case SVGTransform::SVG_TRANSFORM_MATRIX
:
264 return 0.0f
; // I'm not quite sure yet what distance between two matrices means.
265 case SVGTransform::SVG_TRANSFORM_SCALE
:
266 return static_cast<float>(sqrt(m_transform
.a() * m_transform
.a() + m_transform
.d() * m_transform
.d()));
267 case SVGTransform::SVG_TRANSFORM_TRANSLATE
:
268 return static_cast<float>(sqrt(m_transform
.e() * m_transform
.e() + m_transform
.f() * m_transform
.f()));
269 case SVGTransform::SVG_TRANSFORM_SKEWX
:
270 case SVGTransform::SVG_TRANSFORM_SKEWY
:
273 ASSERT_NOT_REACHED();