Bumping manifests a=b2g-bump
[gecko.git] / dom / svg / nsSVGTransform.cpp
blobf1739d73e6543dfc1586433dd49fc486a806a5ef
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsError.h"
8 #include "nsSVGTransform.h"
9 #include "nsContentUtils.h" // for NS_ENSURE_FINITE
10 #include "nsTextFormatter.h"
12 namespace {
13 const double kRadPerDegree = 2.0 * M_PI / 360.0;
16 namespace mozilla {
18 void
19 nsSVGTransform::GetValueAsString(nsAString& aValue) const
21 char16_t buf[256];
23 switch (mType) {
24 case SVG_TRANSFORM_TRANSLATE:
25 // The spec say that if Y is not provided, it is assumed to be zero.
26 if (mMatrix._32 != 0)
27 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
28 MOZ_UTF16("translate(%g, %g)"),
29 mMatrix._31, mMatrix._32);
30 else
31 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
32 MOZ_UTF16("translate(%g)"),
33 mMatrix._31);
34 break;
35 case SVG_TRANSFORM_ROTATE:
36 if (mOriginX != 0.0f || mOriginY != 0.0f)
37 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
38 MOZ_UTF16("rotate(%g, %g, %g)"),
39 mAngle, mOriginX, mOriginY);
40 else
41 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
42 MOZ_UTF16("rotate(%g)"), mAngle);
43 break;
44 case SVG_TRANSFORM_SCALE:
45 if (mMatrix._11 != mMatrix._22)
46 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
47 MOZ_UTF16("scale(%g, %g)"), mMatrix._11, mMatrix._22);
48 else
49 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
50 MOZ_UTF16("scale(%g)"), mMatrix._11);
51 break;
52 case SVG_TRANSFORM_SKEWX:
53 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
54 MOZ_UTF16("skewX(%g)"), mAngle);
55 break;
56 case SVG_TRANSFORM_SKEWY:
57 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
58 MOZ_UTF16("skewY(%g)"), mAngle);
59 break;
60 case SVG_TRANSFORM_MATRIX:
61 nsTextFormatter::snprintf(buf, sizeof(buf)/sizeof(char16_t),
62 MOZ_UTF16("matrix(%g, %g, %g, %g, %g, %g)"),
63 mMatrix._11, mMatrix._12,
64 mMatrix._21, mMatrix._22,
65 mMatrix._31, mMatrix._32);
66 break;
67 default:
68 buf[0] = '\0';
69 NS_ERROR("unknown transformation type");
70 break;
73 aValue.Assign(buf);
76 void
77 nsSVGTransform::SetMatrix(const gfxMatrix& aMatrix)
79 mType = SVG_TRANSFORM_MATRIX;
80 mMatrix = aMatrix;
81 // We set the other members here too, since operator== requires it and
82 // the DOM requires it for mAngle.
83 mAngle = 0.f;
84 mOriginX = 0.f;
85 mOriginY = 0.f;
88 void
89 nsSVGTransform::SetTranslate(float aTx, float aTy)
91 mType = SVG_TRANSFORM_TRANSLATE;
92 mMatrix.Reset();
93 mMatrix._31 = aTx;
94 mMatrix._32 = aTy;
95 mAngle = 0.f;
96 mOriginX = 0.f;
97 mOriginY = 0.f;
100 void
101 nsSVGTransform::SetScale(float aSx, float aSy)
103 mType = SVG_TRANSFORM_SCALE;
104 mMatrix.Reset();
105 mMatrix._11 = aSx;
106 mMatrix._22 = aSy;
107 mAngle = 0.f;
108 mOriginX = 0.f;
109 mOriginY = 0.f;
112 void
113 nsSVGTransform::SetRotate(float aAngle, float aCx, float aCy)
115 mType = SVG_TRANSFORM_ROTATE;
116 mMatrix.Reset();
117 mMatrix.Translate(aCx, aCy);
118 mMatrix.Rotate(aAngle*kRadPerDegree);
119 mMatrix.Translate(-aCx, -aCy);
120 mAngle = aAngle;
121 mOriginX = aCx;
122 mOriginY = aCy;
125 nsresult
126 nsSVGTransform::SetSkewX(float aAngle)
128 double ta = tan(aAngle*kRadPerDegree);
129 NS_ENSURE_FINITE(ta, NS_ERROR_RANGE_ERR);
131 mType = SVG_TRANSFORM_SKEWX;
132 mMatrix.Reset();
133 mMatrix._21 = ta;
134 mAngle = aAngle;
135 mOriginX = 0.f;
136 mOriginY = 0.f;
137 return NS_OK;
140 nsresult
141 nsSVGTransform::SetSkewY(float aAngle)
143 double ta = tan(aAngle*kRadPerDegree);
144 NS_ENSURE_FINITE(ta, NS_ERROR_RANGE_ERR);
146 mType = SVG_TRANSFORM_SKEWY;
147 mMatrix.Reset();
148 mMatrix._12 = ta;
149 mAngle = aAngle;
150 mOriginX = 0.f;
151 mOriginY = 0.f;
152 return NS_OK;
155 SVGTransformSMILData::SVGTransformSMILData(const nsSVGTransform& aTransform)
156 : mTransformType(aTransform.Type())
158 NS_ABORT_IF_FALSE(
159 mTransformType >= SVG_TRANSFORM_MATRIX &&
160 mTransformType <= SVG_TRANSFORM_SKEWY,
161 "Unexpected transform type");
163 for (uint32_t i = 0; i < NUM_STORED_PARAMS; ++i) {
164 mParams[i] = 0.f;
167 switch (mTransformType) {
168 case SVG_TRANSFORM_MATRIX: {
169 const gfxMatrix& mx = aTransform.GetMatrix();
170 mParams[0] = static_cast<float>(mx._11);
171 mParams[1] = static_cast<float>(mx._12);
172 mParams[2] = static_cast<float>(mx._21);
173 mParams[3] = static_cast<float>(mx._22);
174 mParams[4] = static_cast<float>(mx._31);
175 mParams[5] = static_cast<float>(mx._32);
176 break;
178 case SVG_TRANSFORM_TRANSLATE: {
179 const gfxMatrix& mx = aTransform.GetMatrix();
180 mParams[0] = static_cast<float>(mx._31);
181 mParams[1] = static_cast<float>(mx._32);
182 break;
184 case SVG_TRANSFORM_SCALE: {
185 const gfxMatrix& mx = aTransform.GetMatrix();
186 mParams[0] = static_cast<float>(mx._11);
187 mParams[1] = static_cast<float>(mx._22);
188 break;
190 case SVG_TRANSFORM_ROTATE:
191 mParams[0] = aTransform.Angle();
192 aTransform.GetRotationOrigin(mParams[1], mParams[2]);
193 break;
195 case SVG_TRANSFORM_SKEWX:
196 case SVG_TRANSFORM_SKEWY:
197 mParams[0] = aTransform.Angle();
198 break;
200 default:
201 NS_NOTREACHED("Unexpected transform type");
202 break;
206 nsSVGTransform
207 SVGTransformSMILData::ToSVGTransform() const
209 nsSVGTransform result;
211 switch (mTransformType) {
212 case SVG_TRANSFORM_MATRIX:
213 result.SetMatrix(gfxMatrix(mParams[0], mParams[1],
214 mParams[2], mParams[3],
215 mParams[4], mParams[5]));
216 break;
218 case SVG_TRANSFORM_TRANSLATE:
219 result.SetTranslate(mParams[0], mParams[1]);
220 break;
222 case SVG_TRANSFORM_SCALE:
223 result.SetScale(mParams[0], mParams[1]);
224 break;
226 case SVG_TRANSFORM_ROTATE:
227 result.SetRotate(mParams[0], mParams[1], mParams[2]);
228 break;
230 case SVG_TRANSFORM_SKEWX:
231 result.SetSkewX(mParams[0]);
232 break;
234 case SVG_TRANSFORM_SKEWY:
235 result.SetSkewY(mParams[0]);
236 break;
238 default:
239 NS_NOTREACHED("Unexpected transform type");
240 break;
242 return result;
245 } // namespace mozilla