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/. */
8 #include "nsSVGTransform.h"
9 #include "nsContentUtils.h" // for NS_ENSURE_FINITE
10 #include "nsTextFormatter.h"
13 const double kRadPerDegree
= 2.0 * M_PI
/ 360.0;
19 nsSVGTransform::GetValueAsString(nsAString
& aValue
) const
24 case SVG_TRANSFORM_TRANSLATE
:
25 // The spec say that if Y is not provided, it is assumed to be zero.
27 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
28 MOZ_UTF16("translate(%g, %g)"),
29 mMatrix
._31
, mMatrix
._32
);
31 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
32 MOZ_UTF16("translate(%g)"),
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
);
41 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
42 MOZ_UTF16("rotate(%g)"), mAngle
);
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
);
49 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
50 MOZ_UTF16("scale(%g)"), mMatrix
._11
);
52 case SVG_TRANSFORM_SKEWX
:
53 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
54 MOZ_UTF16("skewX(%g)"), mAngle
);
56 case SVG_TRANSFORM_SKEWY
:
57 nsTextFormatter::snprintf(buf
, sizeof(buf
)/sizeof(char16_t
),
58 MOZ_UTF16("skewY(%g)"), mAngle
);
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
);
69 NS_ERROR("unknown transformation type");
77 nsSVGTransform::SetMatrix(const gfxMatrix
& aMatrix
)
79 mType
= SVG_TRANSFORM_MATRIX
;
81 // We set the other members here too, since operator== requires it and
82 // the DOM requires it for mAngle.
89 nsSVGTransform::SetTranslate(float aTx
, float aTy
)
91 mType
= SVG_TRANSFORM_TRANSLATE
;
101 nsSVGTransform::SetScale(float aSx
, float aSy
)
103 mType
= SVG_TRANSFORM_SCALE
;
113 nsSVGTransform::SetRotate(float aAngle
, float aCx
, float aCy
)
115 mType
= SVG_TRANSFORM_ROTATE
;
117 mMatrix
.Translate(aCx
, aCy
);
118 mMatrix
.Rotate(aAngle
*kRadPerDegree
);
119 mMatrix
.Translate(-aCx
, -aCy
);
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
;
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
;
155 SVGTransformSMILData::SVGTransformSMILData(const nsSVGTransform
& aTransform
)
156 : mTransformType(aTransform
.Type())
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
) {
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
);
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
);
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
);
190 case SVG_TRANSFORM_ROTATE
:
191 mParams
[0] = aTransform
.Angle();
192 aTransform
.GetRotationOrigin(mParams
[1], mParams
[2]);
195 case SVG_TRANSFORM_SKEWX
:
196 case SVG_TRANSFORM_SKEWY
:
197 mParams
[0] = aTransform
.Angle();
201 NS_NOTREACHED("Unexpected transform type");
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]));
218 case SVG_TRANSFORM_TRANSLATE
:
219 result
.SetTranslate(mParams
[0], mParams
[1]);
222 case SVG_TRANSFORM_SCALE
:
223 result
.SetScale(mParams
[0], mParams
[1]);
226 case SVG_TRANSFORM_ROTATE
:
227 result
.SetRotate(mParams
[0], mParams
[1], mParams
[2]);
230 case SVG_TRANSFORM_SKEWX
:
231 result
.SetSkewX(mParams
[0]);
234 case SVG_TRANSFORM_SKEWY
:
235 result
.SetSkewY(mParams
[0]);
239 NS_NOTREACHED("Unexpected transform type");
245 } // namespace mozilla