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 "SVGTransformListParser.h"
9 #include "mozilla/ArrayUtils.h"
10 #include "SVGContentUtils.h"
11 #include "SVGTransform.h"
12 #include "nsGkAtoms.h"
17 //----------------------------------------------------------------------
20 bool SVGTransformListParser::Parse() {
22 return ParseTransforms();
25 bool SVGTransformListParser::ParseTransforms() {
30 if (!ParseTransform()) {
35 // The SVG BNF allows multiple comma-wsp between transforms
36 while (*mIter
== ',') {
43 if (!ParseTransform()) {
50 bool SVGTransformListParser::ParseTransform() {
51 RangedPtr
<const char16_t
> start(mIter
);
52 while (IsAsciiAlpha(*mIter
)) {
60 // Didn't read anything
64 const nsAString
& transform
= Substring(start
.get(), mIter
.get());
65 nsStaticAtom
* keyAtom
= NS_GetStaticAtom(transform
);
67 if (!keyAtom
|| !SkipWsp()) {
71 if (keyAtom
== nsGkAtoms::translate
) {
72 return ParseTranslate();
74 if (keyAtom
== nsGkAtoms::scale
) {
77 if (keyAtom
== nsGkAtoms::rotate
) {
80 if (keyAtom
== nsGkAtoms::skewX
) {
83 if (keyAtom
== nsGkAtoms::skewY
) {
86 if (keyAtom
== nsGkAtoms::matrix
) {
92 bool SVGTransformListParser::ParseArguments(float* aResult
, uint32_t aMaxCount
,
93 uint32_t* aParsedCount
) {
103 if (!SVGContentUtils::ParseNumber(mIter
, mEnd
, aResult
[0])) {
113 if (*aParsedCount
== aMaxCount
) {
117 if (!SVGContentUtils::ParseNumber(mIter
, mEnd
,
118 aResult
[(*aParsedCount
)++])) {
125 bool SVGTransformListParser::ParseTranslate() {
129 if (!ParseArguments(t
, ArrayLength(t
), &count
)) {
138 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
142 transform
->SetTranslate(t
[0], t
[1]);
150 bool SVGTransformListParser::ParseScale() {
154 if (!ParseArguments(s
, ArrayLength(s
), &count
)) {
163 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
167 transform
->SetScale(s
[0], s
[1]);
175 bool SVGTransformListParser::ParseRotate() {
179 if (!ParseArguments(r
, ArrayLength(r
), &count
)) {
188 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
192 transform
->SetRotate(r
[0], r
[1], r
[2]);
200 bool SVGTransformListParser::ParseSkewX() {
204 if (!ParseArguments(&skew
, 1, &count
) || count
!= 1) {
208 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
212 transform
->SetSkewX(skew
);
217 bool SVGTransformListParser::ParseSkewY() {
221 if (!ParseArguments(&skew
, 1, &count
) || count
!= 1) {
225 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
229 transform
->SetSkewY(skew
);
234 bool SVGTransformListParser::ParseMatrix() {
238 if (!ParseArguments(m
, ArrayLength(m
), &count
) || count
!= 6) {
242 SVGTransform
* transform
= mTransforms
.AppendElement(fallible
);
246 transform
->SetMatrix(gfxMatrix(m
[0], m
[1], m
[2], m
[3], m
[4], m
[5]));
251 } // namespace mozilla