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(SVGTransform::SVGTransformType 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_UNKNOWN:
108 ASSERT_NOT_REACHED();
110 case SVGTransform::SVG_TRANSFORM_SKEWX:
111 transform.setSkewX(values[0]);
113 case SVGTransform::SVG_TRANSFORM_SKEWY:
114 transform.setSkewY(values[0]);
116 case SVGTransform::SVG_TRANSFORM_SCALE:
117 if (valueCount == 1) // Spec: if only one param given, assume uniform scaling
118 transform.setScale(values[0], values[0]);
120 transform.setScale(values[0], values[1]);
122 case SVGTransform::SVG_TRANSFORM_TRANSLATE:
123 if (valueCount == 1) // Spec: if only one param given, assume 2nd param to be 0
124 transform.setTranslate(values[0], 0);
126 transform.setTranslate(values[0], values[1]);
128 case SVGTransform::SVG_TRANSFORM_ROTATE:
130 transform.setRotate(values[0], 0, 0);
132 transform.setRotate(values[0], values[1], values[2]);
134 case SVGTransform::SVG_TRANSFORM_MATRIX:
135 transform.setMatrix(AffineTransform(values[0], values[1], values[2], values[3], values[4], values[5]));
142 static const UChar skewXDesc[] = {'s', 'k', 'e', 'w', 'X'};
143 static const UChar skewYDesc[] = {'s', 'k', 'e', 'w', 'Y'};
144 static const UChar scaleDesc[] = {'s', 'c', 'a', 'l', 'e'};
145 static const UChar translateDesc[] = {'t', 'r', 'a', 'n', 's', 'l', 'a', 't', 'e'};
146 static const UChar rotateDesc[] = {'r', 'o', 't', 'a', 't', 'e'};
147 static const UChar matrixDesc[] = {'m', 'a', 't', 'r', 'i', 'x'};
149 static inline bool parseAndSkipType(const UChar*& currTransform, const UChar* end, SVGTransform::SVGTransformType& type)
151 if (currTransform >= end)
154 if (*currTransform == 's') {
155 if (skipString(currTransform, end, skewXDesc, WTF_ARRAY_LENGTH(skewXDesc)))
156 type = SVGTransform::SVG_TRANSFORM_SKEWX;
157 else if (skipString(currTransform, end, skewYDesc, WTF_ARRAY_LENGTH(skewYDesc)))
158 type = SVGTransform::SVG_TRANSFORM_SKEWY;
159 else if (skipString(currTransform, end, scaleDesc, WTF_ARRAY_LENGTH(scaleDesc)))
160 type = SVGTransform::SVG_TRANSFORM_SCALE;
163 } else if (skipString(currTransform, end, translateDesc, WTF_ARRAY_LENGTH(translateDesc)))
164 type = SVGTransform::SVG_TRANSFORM_TRANSLATE;
165 else if (skipString(currTransform, end, rotateDesc, WTF_ARRAY_LENGTH(rotateDesc)))
166 type = SVGTransform::SVG_TRANSFORM_ROTATE;
167 else if (skipString(currTransform, end, matrixDesc, WTF_ARRAY_LENGTH(matrixDesc)))
168 type = SVGTransform::SVG_TRANSFORM_MATRIX;
175 SVGTransform::SVGTransformType SVGTransformable::parseTransformType(const String& typeString)
177 SVGTransform::SVGTransformType type = SVGTransform::SVG_TRANSFORM_UNKNOWN;
178 auto upconvertedCharacters = StringView(typeString).upconvertedCharacters();
179 const UChar* characters = upconvertedCharacters;
180 parseAndSkipType(characters, characters + typeString.length(), type);
181 return static_cast<SVGTransform::SVGTransformType>(type);
184 bool SVGTransformable::parseTransformAttribute(SVGTransformList& list, const UChar*& currTransform, const UChar* end, TransformParsingMode mode)
186 if (mode == ClearList)
189 bool delimParsed = false;
190 while (currTransform < end) {
192 SVGTransform::SVGTransformType type = SVGTransform::SVG_TRANSFORM_UNKNOWN;
193 skipOptionalSVGSpaces(currTransform, end);
195 if (!parseAndSkipType(currTransform, end, type))
198 SVGTransform transform;
199 if (!parseTransformValue(type, currTransform, end, transform))
202 list.append(transform);
203 skipOptionalSVGSpaces(currTransform, end);
204 if (currTransform < end && *currTransform == ',') {
208 skipOptionalSVGSpaces(currTransform, end);