blob: bec165a6e7b609cf185df191b8afad1f91c48356 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 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) 2008 Cameron McCormack <cam@mcc.id.au>
7 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#ifndef SVGAnimationElement_h
26#define SVGAnimationElement_h
27
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010028#include "core/svg/SVGAnimatedBoolean.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010029#include "core/svg/SVGTests.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010030#include "core/svg/animation/SVGSMILElement.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010031#include "platform/animation/UnitBezier.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000032#include "wtf/Functional.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033
34namespace WebCore {
35
36enum AnimationMode {
37 NoAnimation,
38 FromToAnimation,
39 FromByAnimation,
40 ToAnimation,
41 ByAnimation,
42 ValuesAnimation,
43 PathAnimation // Used by AnimateMotion.
44};
45
Torne (Richard Coles)09380292014-02-21 12:17:33 +000046// If we have 'inherit' as animation value, we need to grab the value
47// during the animation since the value can be animated itself.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010048enum AnimatedPropertyValueType {
49 RegularPropertyValue,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010050 InheritValue
51};
52
53enum CalcMode {
54 CalcModeDiscrete,
55 CalcModeLinear,
56 CalcModePaced,
57 CalcModeSpline
58};
59
60class ConditionEventListener;
61class TimeContainer;
62class SVGAnimatedType;
63
64class SVGAnimationElement : public SVGSMILElement,
Torne (Richard Coles)09380292014-02-21 12:17:33 +000065 public SVGTests {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010066public:
67 // SVGAnimationElement
68 float getStartTime() const;
69 float getCurrentTime() const;
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010070 float getSimpleDuration() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071
Ben Murdoch591b9582013-07-10 11:41:44 +010072 void beginElement();
73 void beginElementAt(float offset);
74 void endElement();
75 void endElementAt(float offset);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010076
77 static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&);
78
79 virtual bool isAdditive() const;
80 bool isAccumulated() const;
81 AnimationMode animationMode() const { return m_animationMode; }
82 CalcMode calcMode() const { return m_calcMode; }
83
84 enum ShouldApplyAnimation {
85 DontApplyAnimation,
86 ApplyCSSAnimation,
87 ApplyXMLAnimation
88 };
89
90 ShouldApplyAnimation shouldApplyAnimation(SVGElement* targetElement, const QualifiedName& attributeName);
91
92 AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; }
93 AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; }
94
Torne (Richard Coles)09380292014-02-21 12:17:33 +000095 // FIXME: In C++11, remove this as we can use default template argument.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096 template<typename AnimatedType>
97 void adjustForInheritance(AnimatedType (*parseTypeFromString)(SVGAnimationElement*, const String&),
98 AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement)
99 {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000100 ASSERT(parseTypeFromString);
101 adjustForInheritance<AnimatedType, AnimatedType (*)(SVGAnimationElement*, const String&)>(parseTypeFromString, valueType, animatedType, contextElement);
102 }
103
104 template<typename AnimatedType, typename ParseTypeFromStringType>
105 void adjustForInheritance(ParseTypeFromStringType parseTypeFromString, AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement)
106 {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100107 if (valueType != InheritValue)
108 return;
109 // Replace 'inherit' by its computed property value.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100110 String typeString;
111 adjustForInheritance(contextElement, attributeName(), typeString);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000112 animatedType = parseTypeFromString(this, typeString);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100113 }
114
115 template<typename AnimatedType>
116 bool adjustFromToListValues(const AnimatedType& fromList, const AnimatedType& toList, AnimatedType& animatedList, float percentage, bool resizeAnimatedListIfNeeded = true)
117 {
118 // If no 'to' value is given, nothing to animate.
119 unsigned toListSize = toList.size();
120 if (!toListSize)
121 return false;
122
123 // If the 'from' value is given and it's length doesn't match the 'to' value list length, fallback to a discrete animation.
124 unsigned fromListSize = fromList.size();
125 if (fromListSize != toListSize && fromListSize) {
126 if (percentage < 0.5) {
127 if (animationMode() != ToAnimation)
128 animatedList = AnimatedType(fromList);
129 } else
130 animatedList = AnimatedType(toList);
131
132 return false;
133 }
134
135 ASSERT(!fromListSize || fromListSize == toListSize);
136 if (resizeAnimatedListIfNeeded && animatedList.size() < toListSize)
137 animatedList.resize(toListSize);
138
139 return true;
140 }
141
142 template<typename AnimatedType>
143 void animateDiscreteType(float percentage, const AnimatedType& fromType, const AnimatedType& toType, AnimatedType& animatedType)
144 {
145 if ((animationMode() == FromToAnimation && percentage > 0.5) || animationMode() == ToAnimation || percentage == 1) {
146 animatedType = AnimatedType(toType);
147 return;
148 }
149 animatedType = AnimatedType(fromType);
150 }
151
152 void animateAdditiveNumber(float percentage, unsigned repeatCount, float fromNumber, float toNumber, float toAtEndOfDurationNumber, float& animatedNumber)
153 {
154 float number;
155 if (calcMode() == CalcModeDiscrete)
156 number = percentage < 0.5 ? fromNumber : toNumber;
157 else
158 number = (toNumber - fromNumber) * percentage + fromNumber;
159
160 if (isAccumulated() && repeatCount)
161 number += toAtEndOfDurationNumber * repeatCount;
162
163 if (isAdditive() && animationMode() != ToAnimation)
164 animatedNumber += number;
165 else
166 animatedNumber = number;
167 }
168
169protected:
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100170 SVGAnimationElement(const QualifiedName&, Document&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100171
172 void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000173 void determinePropertyValueTypes(const String& from, const String& to);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100174
175 bool isSupportedAttribute(const QualifiedName&);
176 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
177 virtual void svgAttributeChanged(const QualifiedName&) OVERRIDE;
178
179 enum AttributeType {
180 AttributeTypeCSS,
181 AttributeTypeXML,
182 AttributeTypeAuto
183 };
184 AttributeType attributeType() const { return m_attributeType; }
185
186 String toValue() const;
187 String byValue() const;
188 String fromValue() const;
189
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100190 // from SVGSMILElement
191 virtual void startedActiveInterval() OVERRIDE;
192 virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) OVERRIDE;
193
194 AnimatedPropertyValueType m_fromPropertyValueType;
195 AnimatedPropertyValueType m_toPropertyValueType;
196
197 virtual void setTargetElement(SVGElement*) OVERRIDE;
198 virtual void setAttributeName(const QualifiedName&) OVERRIDE;
199 bool hasInvalidCSSAttributeType() const { return m_hasInvalidCSSAttributeType; }
200
201 virtual void updateAnimationMode();
202 void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; }
203 void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; }
204
205private:
206 virtual void animationAttributeChanged() OVERRIDE;
207 void setAttributeType(const AtomicString&);
208
209 void checkInvalidCSSAttributeType(SVGElement*);
210
211 virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0;
212 virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0;
213 virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0;
214 virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0;
215 virtual float calculateDistance(const String& /*fromString*/, const String& /*toString*/) { return -1.f; }
216
217 void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to);
218 void calculateKeyTimesForCalcModePaced();
219 float calculatePercentFromKeyPoints(float percent) const;
220 void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const;
221 float calculatePercentForSpline(float percent, unsigned splineIndex) const;
222 float calculatePercentForFromTo(float percent) const;
223 unsigned calculateKeyTimesIndex(float percent) const;
224
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100225 void adjustForInheritance(SVGElement* targetElement, const QualifiedName& attributeName, String&);
226
227 BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGAnimationElement)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100228 END_DECLARE_ANIMATED_PROPERTIES
229
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100230 void setCalcMode(const AtomicString&);
231
232 bool m_animationValid;
233
234 AttributeType m_attributeType;
235 Vector<String> m_values;
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100236 // FIXME: We should probably use doubles for this, but there's no point
237 // making such a change unless all SVG logic for sampling animations is
238 // changed to use doubles.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100239 Vector<float> m_keyTimes;
240 Vector<float> m_keyPoints;
241 Vector<UnitBezier> m_keySplines;
242 String m_lastValuesAnimationFrom;
243 String m_lastValuesAnimationTo;
244 bool m_hasInvalidCSSAttributeType;
245 CalcMode m_calcMode;
246 AnimationMode m_animationMode;
247};
248
249} // namespace WebCore
250
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100251#endif // SVGAnimationElement_h