2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include "SVGTransform.h"
26 #include "FloatConversion.h"
27 #include "FloatPoint.h"
28 #include "FloatSize.h"
30 #include "SVGSVGElement.h"
31 #include <wtf/MathExtras.h>
32 #include <wtf/text/StringBuilder.h>
33 #include <wtf/text/WTFString.h>
37 SVGTransform::SVGTransform()
38 : m_type(SVG_TRANSFORM_UNKNOWN)
43 SVGTransform::SVGTransform(SVGTransformType type)
49 SVGTransform::SVGTransform(const AffineTransform& matrix)
50 : m_type(SVG_TRANSFORM_MATRIX)
56 void SVGTransform::setMatrix(const AffineTransform& matrix)
58 m_type = SVG_TRANSFORM_MATRIX;
63 void SVGTransform::updateMatrix()
65 // The underlying matrix has been changed, alter the transformation type.
66 // Spec: In case the matrix object is changed directly (i.e., without using the methods on the SVGTransform interface itself)
67 // then the type of the SVGTransform changes to SVG_TRANSFORM_MATRIX.
68 m_type = SVG_TRANSFORM_MATRIX;
72 void SVGTransform::setTranslate(float tx, float ty)
74 m_type = SVG_TRANSFORM_TRANSLATE;
77 m_matrix.makeIdentity();
78 m_matrix.translate(tx, ty);
81 FloatPoint SVGTransform::translate() const
83 return FloatPoint::narrowPrecision(m_matrix.e(), m_matrix.f());
86 void SVGTransform::setScale(float sx, float sy)
88 m_type = SVG_TRANSFORM_SCALE;
90 m_center = FloatPoint();
92 m_matrix.makeIdentity();
93 m_matrix.scaleNonUniform(sx, sy);
96 FloatSize SVGTransform::scale() const
98 return FloatSize::narrowPrecision(m_matrix.a(), m_matrix.d());
101 void SVGTransform::setRotate(float angle, float cx, float cy)
103 m_type = SVG_TRANSFORM_ROTATE;
105 m_center = FloatPoint(cx, cy);
107 // TODO: toString() implementation, which can show cx, cy (need to be stored?)
108 m_matrix.makeIdentity();
109 m_matrix.translate(cx, cy);
110 m_matrix.rotate(angle);
111 m_matrix.translate(-cx, -cy);
114 void SVGTransform::setSkewX(float angle)
116 m_type = SVG_TRANSFORM_SKEWX;
119 m_matrix.makeIdentity();
120 m_matrix.skewX(angle);
123 void SVGTransform::setSkewY(float angle)
125 m_type = SVG_TRANSFORM_SKEWY;
128 m_matrix.makeIdentity();
129 m_matrix.skewY(angle);
132 String SVGTransform::valueAsString() const
135 case SVG_TRANSFORM_UNKNOWN:
137 case SVG_TRANSFORM_MATRIX: {
138 DEFINE_STATIC_LOCAL(String, matrixString, ("matrix("));
139 StringBuilder builder;
140 builder.append(matrixString + String::number(m_matrix.a()) + ' ' + String::number(m_matrix.b()) + ' ' + String::number(m_matrix.c()) + ' ' +
141 String::number(m_matrix.d()) + ' ' + String::number(m_matrix.e()) + ' ' + String::number(m_matrix.f()) + ')');
142 return builder.toString();
144 case SVG_TRANSFORM_TRANSLATE: {
145 DEFINE_STATIC_LOCAL(String, translateString, ("translate("));
146 return translateString + String::number(m_matrix.e()) + ' ' + String::number(m_matrix.f()) + ')';
148 case SVG_TRANSFORM_SCALE: {
149 DEFINE_STATIC_LOCAL(String, scaleString, ("scale("));
150 return scaleString + String::number(m_matrix.xScale()) + ' ' + String::number(m_matrix.yScale()) + ')';
152 case SVG_TRANSFORM_ROTATE: {
153 DEFINE_STATIC_LOCAL(String, rotateString, ("rotate("));
154 double angleInRad = deg2rad(m_angle);
155 double cosAngle = cos(angleInRad);
156 double sinAngle = sin(angleInRad);
157 float cx = narrowPrecisionToFloat(cosAngle != 1 ? (m_matrix.e() * (1 - cosAngle) - m_matrix.f() * sinAngle) / (1 - cosAngle) / 2 : 0);
158 float cy = narrowPrecisionToFloat(cosAngle != 1 ? (m_matrix.e() * sinAngle / (1 - cosAngle) + m_matrix.f()) / 2 : 0);
160 return rotateString + String::number(m_angle) + ' ' + String::number(cx) + ' ' + String::number(cy) + ')';
161 return rotateString + String::number(m_angle) + ')';
163 case SVG_TRANSFORM_SKEWX: {
164 DEFINE_STATIC_LOCAL(String, skewXString, ("skewX("));
165 return skewXString + String::number(m_angle) + ')';
167 case SVG_TRANSFORM_SKEWY: {
168 DEFINE_STATIC_LOCAL(String, skewYString, ("skewY("));
169 return skewYString + String::number(m_angle) + ')';
173 ASSERT_NOT_REACHED();
177 } // namespace WebCore
179 #endif // ENABLE(SVG)