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>
5 * Copyright (C) 2008 Apple Inc. All rights reserved.
6 * Copyright (C) Research In Motion Limited 2012. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "core/svg/SVGTransformList.h"
28 #include "core/SVGNames.h"
29 #include "core/svg/SVGAnimateTransformElement.h"
30 #include "core/svg/SVGAnimatedNumber.h"
31 #include "core/svg/SVGParserUtilities.h"
32 #include "core/svg/SVGTransformDistance.h"
33 #include "wtf/text/StringBuilder.h"
34 #include "wtf/text/WTFString.h"
38 inline PassRefPtr<SVGTransformList> toSVGTransformList(PassRefPtr<SVGPropertyBase> passBase)
40 RefPtr<SVGPropertyBase> base = passBase;
41 ASSERT(base->type() == SVGTransformList::classType());
42 return static_pointer_cast<SVGTransformList>(base.release());
45 SVGTransformList::SVGTransformList()
49 SVGTransformList::~SVGTransformList()
53 PassRefPtr<SVGTransform> SVGTransformList::consolidate()
55 AffineTransform matrix;
56 if (!concatenate(matrix))
57 return SVGTransform::create();
59 RefPtr<SVGTransform> transform = SVGTransform::create(matrix);
61 return appendItem(transform);
64 bool SVGTransformList::concatenate(AffineTransform& result) const
69 ConstIterator it = begin();
70 ConstIterator itEnd = end();
71 for (; it != itEnd; ++it)
72 result *= it->matrix();
79 template<typename CharType>
80 int parseTransformParamList(const CharType*& ptr, const CharType* end, float* values, int required, int optional)
83 int maxPossibleParams = required + optional;
85 bool trailingDelimiter = false;
87 skipOptionalSVGSpaces(ptr, end);
88 while (parsedParams < maxPossibleParams) {
89 if (!parseNumber(ptr, end, values[parsedParams], DisallowWhitespace))
94 if (skipOptionalSVGSpaces(ptr, end) && *ptr == ',') {
96 skipOptionalSVGSpaces(ptr, end);
98 trailingDelimiter = true;
100 trailingDelimiter = false;
104 if (trailingDelimiter || !(parsedParams == required || parsedParams == maxPossibleParams))
110 // These should be kept in sync with enum SVGTransformType
111 static const int requiredValuesForType[] = {0, 6, 1, 1, 1, 1, 1};
112 static const int optionalValuesForType[] = {0, 0, 1, 1, 2, 0, 0};
114 template<typename CharType>
115 PassRefPtr<SVGTransform> parseTransformOfType(unsigned type, const CharType*& ptr, const CharType* end)
117 if (type == SVG_TRANSFORM_UNKNOWN)
121 float values[] = {0, 0, 0, 0, 0, 0};
122 if ((valueCount = parseTransformParamList(ptr, end, values, requiredValuesForType[type], optionalValuesForType[type])) < 0) {
126 RefPtr<SVGTransform> transform = SVGTransform::create();
129 case SVG_TRANSFORM_SKEWX:
130 transform->setSkewX(values[0]);
132 case SVG_TRANSFORM_SKEWY:
133 transform->setSkewY(values[0]);
135 case SVG_TRANSFORM_SCALE:
136 if (valueCount == 1) // Spec: if only one param given, assume uniform scaling
137 transform->setScale(values[0], values[0]);
139 transform->setScale(values[0], values[1]);
141 case SVG_TRANSFORM_TRANSLATE:
142 if (valueCount == 1) // Spec: if only one param given, assume 2nd param to be 0
143 transform->setTranslate(values[0], 0);
145 transform->setTranslate(values[0], values[1]);
147 case SVG_TRANSFORM_ROTATE:
149 transform->setRotate(values[0], 0, 0);
151 transform->setRotate(values[0], values[1], values[2]);
153 case SVG_TRANSFORM_MATRIX:
154 transform->setMatrix(AffineTransform(values[0], values[1], values[2], values[3], values[4], values[5]));
158 return transform.release();
163 template<typename CharType>
164 bool SVGTransformList::parseInternal(const CharType*& ptr, const CharType* end)
168 bool delimParsed = false;
171 SVGTransformType transformType = SVG_TRANSFORM_UNKNOWN;
172 skipOptionalSVGSpaces(ptr, end);
174 if (!parseAndSkipTransformType(ptr, end, transformType))
177 if (!skipOptionalSVGSpaces(ptr, end) || *ptr != '(')
181 RefPtr<SVGTransform> transform = parseTransformOfType(transformType, ptr, end);
185 if (!skipOptionalSVGSpaces(ptr, end) || *ptr != ')')
189 append(transform.release());
191 skipOptionalSVGSpaces(ptr, end);
192 if (ptr < end && *ptr == ',') {
195 skipOptionalSVGSpaces(ptr, end);
202 bool SVGTransformList::parse(const UChar*& ptr, const UChar* end)
204 return parseInternal(ptr, end);
207 bool SVGTransformList::parse(const LChar*& ptr, const LChar* end)
209 return parseInternal(ptr, end);
212 String SVGTransformList::valueAsString() const
214 StringBuilder builder;
216 ConstIterator it = begin();
217 ConstIterator itEnd = end();
218 while (it != itEnd) {
219 builder.append(it->valueAsString());
225 return builder.toString();
228 void SVGTransformList::setValueAsString(const String& value, ExceptionState& exceptionState)
230 if (value.isEmpty()) {
236 if (value.is8Bit()) {
237 const LChar* ptr = value.characters8();
238 const LChar* end = ptr + value.length();
239 valid = parse(ptr, end);
241 const UChar* ptr = value.characters16();
242 const UChar* end = ptr + value.length();
243 valid = parse(ptr, end);
248 exceptionState.throwDOMException(SyntaxError, "Problem parsing transform list=\""+value+"\"");
252 PassRefPtr<SVGPropertyBase> SVGTransformList::cloneForAnimation(const String& value) const
254 ASSERT_NOT_REACHED();
258 PassRefPtr<SVGTransformList> SVGTransformList::create(SVGTransformType transformType, const String& value)
260 RefPtr<SVGTransform> transform;
261 if (value.isEmpty()) {
262 } else if (value.is8Bit()) {
263 const LChar* ptr = value.characters8();
264 const LChar* end = ptr + value.length();
265 transform = parseTransformOfType(transformType, ptr, end);
267 const UChar* ptr = value.characters16();
268 const UChar* end = ptr + value.length();
269 transform = parseTransformOfType(transformType, ptr, end);
272 RefPtr<SVGTransformList> svgTransformList = SVGTransformList::create();
274 svgTransformList->append(transform);
275 return svgTransformList.release();
278 void SVGTransformList::add(PassRefPtrWillBeRawPtr<SVGPropertyBase> other, SVGElement* contextElement)
283 RefPtr<SVGTransformList> otherList = toSVGTransformList(other);
284 if (length() != otherList->length())
287 ASSERT(length() == 1);
288 RefPtr<SVGTransform> fromTransform = at(0);
289 RefPtr<SVGTransform> toTransform = otherList->at(0);
291 ASSERT(fromTransform->transformType() == toTransform->transformType());
293 append(SVGTransformDistance::addSVGTransforms(fromTransform, toTransform));
296 void SVGTransformList::calculateAnimatedValue(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, PassRefPtr<SVGPropertyBase> fromValue, PassRefPtr<SVGPropertyBase> toValue, PassRefPtr<SVGPropertyBase> toAtEndOfDurationValue, SVGElement* contextElement)
298 ASSERT(animationElement);
299 bool isToAnimation = animationElement->animationMode() == ToAnimation;
301 // Spec: To animations provide specific functionality to get a smooth change from the underlying value to the
302 // ‘to’ attribute value, which conflicts mathematically with the requirement for additive transform animations
303 // to be post-multiplied. As a consequence, in SVG 1.1 the behavior of to animations for ‘animateTransform’ is undefined
304 // FIXME: This is not taken into account yet.
305 RefPtr<SVGTransformList> fromList = isToAnimation ? this : toSVGTransformList(fromValue);
306 RefPtr<SVGTransformList> toList = toSVGTransformList(toValue);
307 RefPtr<SVGTransformList> toAtEndOfDurationList = toSVGTransformList(toAtEndOfDurationValue);
309 size_t toListSize = toList->length();
313 // Get a reference to the from value before potentially cleaning it out (in the case of a To animation.)
314 RefPtr<SVGTransform> toTransform = toList->at(0);
315 RefPtr<SVGTransform> effectiveFrom;
316 // If there's an existing 'from'/underlying value of the same type use that, else use a "zero transform".
317 if (fromList->length() && fromList->at(0)->transformType() == toTransform->transformType())
318 effectiveFrom = fromList->at(0);
320 effectiveFrom = SVGTransform::create(toTransform->transformType(), SVGTransform::ConstructZeroTransform);
322 // Never resize the animatedTransformList to the toList size, instead either clear the list or append to it.
323 if (!isEmpty() && (!animationElement->isAdditive() || isToAnimation))
326 RefPtr<SVGTransform> currentTransform = SVGTransformDistance(effectiveFrom, toTransform).scaledDistance(percentage).addToSVGTransform(effectiveFrom);
327 if (animationElement->isAccumulated() && repeatCount) {
328 RefPtr<SVGTransform> effectiveToAtEnd = !toAtEndOfDurationList->isEmpty() ? toAtEndOfDurationList->at(0) : SVGTransform::create(toTransform->transformType(), SVGTransform::ConstructZeroTransform);
329 append(SVGTransformDistance::addSVGTransforms(currentTransform, effectiveToAtEnd, repeatCount));
331 append(currentTransform);
335 float SVGTransformList::calculateDistance(PassRefPtr<SVGPropertyBase> toValue, SVGElement*)
337 // FIXME: This is not correct in all cases. The spec demands that each component (translate x and y for example)
338 // is paced separately. To implement this we need to treat each component as individual animation everywhere.
340 RefPtr<SVGTransformList> toList = toSVGTransformList(toValue);
341 if (isEmpty() || length() != toList->length())
344 ASSERT(length() == 1);
345 if (at(0)->transformType() == toList->at(0)->transformType())
348 // Spec: http://www.w3.org/TR/SVG/animate.html#complexDistances
349 // Paced animations assume a notion of distance between the various animation values defined by the ‘to’, ‘from’, ‘by’ and ‘values’ attributes.
350 // Distance is defined only for scalar types (such as <length>), colors and the subset of transformation types that are supported by ‘animateTransform’.
351 return SVGTransformDistance(at(0), toList->at(0)).distance();