1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "mozilla/ArrayUtils.h"
9 #include "SVGTransformListParser.h"
10 #include "SVGContentUtils.h"
11 #include "nsSVGTransform.h"
12 #include "nsGkAtoms.h"
15 using namespace mozilla
;
17 //----------------------------------------------------------------------
21 SVGTransformListParser::Parse()
24 return ParseTransforms();
28 SVGTransformListParser::ParseTransforms()
34 if (!ParseTransform()) {
39 // The SVG BNF allows multiple comma-wsp between transforms
40 while (*mIter
== ',') {
47 if (!ParseTransform()) {
55 SVGTransformListParser::ParseTransform()
57 RangedPtr
<const char16_t
> start(mIter
);
58 while (IsAlpha(*mIter
)) {
66 // Didn't read anything
70 const nsAString
& transform
= Substring(start
.get(), mIter
.get());
71 nsStaticAtom
* keyAtom
= NS_GetStaticAtom(transform
);
73 if (!keyAtom
|| !SkipWsp()) {
77 if (keyAtom
== nsGkAtoms::translate
) {
78 return ParseTranslate();
80 if (keyAtom
== nsGkAtoms::scale
) {
83 if (keyAtom
== nsGkAtoms::rotate
) {
86 if (keyAtom
== nsGkAtoms::skewX
) {
89 if (keyAtom
== nsGkAtoms::skewY
) {
92 if (keyAtom
== nsGkAtoms::matrix
) {
99 SVGTransformListParser::ParseArguments(float* aResult
,
101 uint32_t* aParsedCount
)
112 if (!SVGContentUtils::ParseNumber(mIter
, mEnd
, aResult
[0])) {
118 RangedPtr
<const char16_t
> iter(mIter
);
126 if (*aParsedCount
== aMaxCount
) {
131 // There must be either whitespace or a comma between values
134 if (!SVGContentUtils::ParseNumber(mIter
, mEnd
, aResult
[(*aParsedCount
)++])) {
142 SVGTransformListParser::ParseTranslate()
147 if (!ParseArguments(t
, ArrayLength(t
), &count
)) {
157 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
161 transform
->SetTranslate(t
[0], t
[1]);
170 SVGTransformListParser::ParseScale()
175 if (!ParseArguments(s
, ArrayLength(s
), &count
)) {
185 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
189 transform
->SetScale(s
[0], s
[1]);
199 SVGTransformListParser::ParseRotate()
204 if (!ParseArguments(r
, ArrayLength(r
), &count
)) {
214 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
218 transform
->SetRotate(r
[0], r
[1], r
[2]);
227 SVGTransformListParser::ParseSkewX()
232 if (!ParseArguments(&skew
, 1, &count
) || count
!= 1) {
236 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
240 transform
->SetSkewX(skew
);
246 SVGTransformListParser::ParseSkewY()
251 if (!ParseArguments(&skew
, 1, &count
) || count
!= 1) {
255 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
259 transform
->SetSkewY(skew
);
265 SVGTransformListParser::ParseMatrix()
270 if (!ParseArguments(m
, ArrayLength(m
), &count
) || count
!= 6) {
274 nsSVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
278 transform
->SetMatrix(gfxMatrix(m
[0], m
[1], m
[2], m
[3], m
[4], m
[5]));