2 * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "SVGTransformable.h"
25 #include "AffineTransform.h"
26 #include "FloatConversion.h"
27 #include "SVGElement.h"
29 #include "SVGParserUtilities.h"
30 #include "SVGTransformList.h"
31 #include <wtf/text/StringView.h>
35 static int parseTransformParamList(const UChar*& ptr, const UChar* end, float* values, int required, int optional)
37 int optionalParams = 0, requiredParams = 0;
39 if (!skipOptionalSVGSpaces(ptr, end) || *ptr != '(')
44 skipOptionalSVGSpaces(ptr, end);
46 while (requiredParams < required) {
47 if (ptr >= end || !parseNumber(ptr, end, values[requiredParams], false))
50 if (requiredParams < required)
51 skipOptionalSVGSpacesOrDelimiter(ptr, end);
53 if (!skipOptionalSVGSpaces(ptr, end))
56 bool delimParsed = skipOptionalSVGSpacesOrDelimiter(ptr, end);
61 if (*ptr == ')') { // skip optionals
66 while (optionalParams < optional) {
67 if (ptr >= end || !parseNumber(ptr, end, values[requiredParams + optionalParams], false))
70 if (optionalParams < optional)
71 skipOptionalSVGSpacesOrDelimiter(ptr, end);
74 if (!skipOptionalSVGSpaces(ptr, end))
77 delimParsed = skipOptionalSVGSpacesOrDelimiter(ptr, end);
79 if (ptr >= end || *ptr != ')' || delimParsed)
84 return requiredParams + optionalParams;
87 // These should be kept in sync with enum SVGTransformType
88 static const int requiredValuesForType[] = {0, 6, 1, 1, 1, 1, 1};
89 static const int optionalValuesForType[] = {0, 0, 1, 1, 2, 0, 0};
91 // This destructor is needed in order to link correctly with Intel ICC.
92 SVGTransformable::~SVGTransformable()
96 bool SVGTransformable::parseTransformValue(unsigned type, const UChar*& ptr, const UChar* end, SVGTransform& transform)
98 if (type == SVGTransform::SVG_TRANSFORM_UNKNOWN)
102 float values[] = {0, 0, 0, 0, 0, 0};
103 if ((valueCount = parseTransformParamList(ptr, end, values, requiredValuesForType[type], optionalValuesForType[type])) < 0)
107 case SVGTransform::SVG_TRANSFORM_SKEWX:
108 transform.setSkewX(values[0]);
110 case SVGTransform::SVG_TRANSFORM_SKEWY:
111 transform.setSkewY(values[0]);
113 case SVGTransform::SVG_TRANSFORM_SCALE:
114 if (valueCount == 1) // Spec: if only one param given, assume uniform scaling
115 transform.setScale(values[0], values[0]);
117 transform.setScale(values[0], values[1]);
119 case SVGTransform::SVG_TRANSFORM_TRANSLATE:
120 if (valueCount == 1) // Spec: if only one param given, assume 2nd param to be 0
121 transform.setTranslate(values[0], 0);
123 transform.setTranslate(values[0], values[1]);
125 case SVGTransform::SVG_TRANSFORM_ROTATE:
127 transform.setRotate(values[0], 0, 0);
129 transform.setRotate(values[0], values[1], values[2]);
131 case SVGTransform::SVG_TRANSFORM_MATRIX:
132 transform.setMatrix(AffineTransform(values[0], values[1], values[2], values[3], values[4], values[5]));
139 static const UChar skewXDesc[] = {'s', 'k', 'e', 'w', 'X'};
140 static const UChar skewYDesc[] = {'s', 'k', 'e', 'w', 'Y'};
141 static const UChar scaleDesc[] = {'s', 'c', 'a', 'l', 'e'};
142 static const UChar translateDesc[] = {'t', 'r', 'a', 'n', 's', 'l', 'a', 't', 'e'};
143 static const UChar rotateDesc[] = {'r', 'o', 't', 'a', 't', 'e'};
144 static const UChar matrixDesc[] = {'m', 'a', 't', 'r', 'i', 'x'};
146 static inline bool parseAndSkipType(const UChar*& currTransform, const UChar* end, unsigned short& type)
148 if (currTransform >= end)
151 if (*currTransform == 's') {
152 if (skipString(currTransform, end, skewXDesc, WTF_ARRAY_LENGTH(skewXDesc)))
153 type = SVGTransform::SVG_TRANSFORM_SKEWX;
154 else if (skipString(currTransform, end, skewYDesc, WTF_ARRAY_LENGTH(skewYDesc)))
155 type = SVGTransform::SVG_TRANSFORM_SKEWY;
156 else if (skipString(currTransform, end, scaleDesc, WTF_ARRAY_LENGTH(scaleDesc)))
157 type = SVGTransform::SVG_TRANSFORM_SCALE;
160 } else if (skipString(currTransform, end, translateDesc, WTF_ARRAY_LENGTH(translateDesc)))
161 type = SVGTransform::SVG_TRANSFORM_TRANSLATE;
162 else if (skipString(currTransform, end, rotateDesc, WTF_ARRAY_LENGTH(rotateDesc)))
163 type = SVGTransform::SVG_TRANSFORM_ROTATE;
164 else if (skipString(currTransform, end, matrixDesc, WTF_ARRAY_LENGTH(matrixDesc)))
165 type = SVGTransform::SVG_TRANSFORM_MATRIX;
172 SVGTransform::SVGTransformType SVGTransformable::parseTransformType(const String& typeString)
174 unsigned short type = SVGTransform::SVG_TRANSFORM_UNKNOWN;
175 auto upconvertedCharacters = StringView(typeString).upconvertedCharacters();
176 const UChar* characters = upconvertedCharacters;
177 parseAndSkipType(characters, characters + typeString.length(), type);
178 return static_cast<SVGTransform::SVGTransformType>(type);
181 bool SVGTransformable::parseTransformAttribute(SVGTransformList& list, const UChar*& currTransform, const UChar* end, TransformParsingMode mode)
183 if (mode == ClearList)
186 bool delimParsed = false;
187 while (currTransform < end) {
189 unsigned short type = SVGTransform::SVG_TRANSFORM_UNKNOWN;
190 skipOptionalSVGSpaces(currTransform, end);
192 if (!parseAndSkipType(currTransform, end, type))
195 SVGTransform transform;
196 if (!parseTransformValue(type, currTransform, end, transform))
199 list.append(transform);
200 skipOptionalSVGSpaces(currTransform, end);
201 if (currTransform < end && *currTransform == ',') {
205 skipOptionalSVGSpaces(currTransform, end);