blob: c3c021ddc8ae65cec0031981313ce54572430741 [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/platform/graphics/UnitBezier.h"
29#include "core/svg/ElementTimeControl.h"
30#include "core/svg/SVGAnimatedBoolean.h"
31#include "core/svg/SVGExternalResourcesRequired.h"
32#include "core/svg/SVGStringList.h"
33#include "core/svg/SVGTests.h"
34#include "core/svg/animation/SMILTime.h"
35#include "core/svg/animation/SVGSMILElement.h"
36
37namespace WebCore {
38
39enum AnimationMode {
40 NoAnimation,
41 FromToAnimation,
42 FromByAnimation,
43 ToAnimation,
44 ByAnimation,
45 ValuesAnimation,
46 PathAnimation // Used by AnimateMotion.
47};
48
49// If we have 'currentColor' or 'inherit' as animation value, we need to grab
50// the value during the animation since the value can be animated itself.
51enum AnimatedPropertyValueType {
52 RegularPropertyValue,
53 CurrentColorValue,
54 InheritValue
55};
56
57enum CalcMode {
58 CalcModeDiscrete,
59 CalcModeLinear,
60 CalcModePaced,
61 CalcModeSpline
62};
63
64class ConditionEventListener;
65class TimeContainer;
66class SVGAnimatedType;
67
68class SVGAnimationElement : public SVGSMILElement,
69 public SVGTests,
70 public SVGExternalResourcesRequired,
71 public ElementTimeControl {
72public:
73 // SVGAnimationElement
74 float getStartTime() const;
75 float getCurrentTime() const;
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010076 float getSimpleDuration() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010077
78 // ElementTimeControl
79 virtual void beginElement();
80 virtual void beginElementAt(float offset);
81 virtual void endElement();
82 virtual void endElementAt(float offset);
83
84 static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&);
85
86 virtual bool isAdditive() const;
87 bool isAccumulated() const;
88 AnimationMode animationMode() const { return m_animationMode; }
89 CalcMode calcMode() const { return m_calcMode; }
90
91 enum ShouldApplyAnimation {
92 DontApplyAnimation,
93 ApplyCSSAnimation,
94 ApplyXMLAnimation
95 };
96
97 ShouldApplyAnimation shouldApplyAnimation(SVGElement* targetElement, const QualifiedName& attributeName);
98
99 AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; }
100 AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; }
101
102 template<typename AnimatedType>
103 void adjustForInheritance(AnimatedType (*parseTypeFromString)(SVGAnimationElement*, const String&),
104 AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement)
105 {
106 if (valueType != InheritValue)
107 return;
108 // Replace 'inherit' by its computed property value.
109 ASSERT(parseTypeFromString);
110 String typeString;
111 adjustForInheritance(contextElement, attributeName(), typeString);
112 animatedType = (*parseTypeFromString)(this, typeString);
113 }
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:
170 SVGAnimationElement(const QualifiedName&, Document*);
171
172 void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value);
173 virtual void determinePropertyValueTypes(const String& from, const String& to);
174
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
190 String targetAttributeBaseValue();
191
192 // from SVGSMILElement
193 virtual void startedActiveInterval() OVERRIDE;
194 virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) OVERRIDE;
195
196 AnimatedPropertyValueType m_fromPropertyValueType;
197 AnimatedPropertyValueType m_toPropertyValueType;
198
199 virtual void setTargetElement(SVGElement*) OVERRIDE;
200 virtual void setAttributeName(const QualifiedName&) OVERRIDE;
201 bool hasInvalidCSSAttributeType() const { return m_hasInvalidCSSAttributeType; }
202
203 virtual void updateAnimationMode();
204 void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; }
205 void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; }
206
207private:
208 virtual void animationAttributeChanged() OVERRIDE;
209 void setAttributeType(const AtomicString&);
210
211 void checkInvalidCSSAttributeType(SVGElement*);
212
213 virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0;
214 virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0;
215 virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0;
216 virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0;
217 virtual float calculateDistance(const String& /*fromString*/, const String& /*toString*/) { return -1.f; }
218
219 void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to);
220 void calculateKeyTimesForCalcModePaced();
221 float calculatePercentFromKeyPoints(float percent) const;
222 void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const;
223 float calculatePercentForSpline(float percent, unsigned splineIndex) const;
224 float calculatePercentForFromTo(float percent) const;
225 unsigned calculateKeyTimesIndex(float percent) const;
226
227 void applyAnimatedValue(ShouldApplyAnimation, SVGElement* targetElement, const QualifiedName& attributeName, SVGAnimatedType*);
228 void adjustForInheritance(SVGElement* targetElement, const QualifiedName& attributeName, String&);
229
230 BEGIN_DECLARE_ANIMATED_PROPERTIES(SVGAnimationElement)
231 DECLARE_ANIMATED_BOOLEAN(ExternalResourcesRequired, externalResourcesRequired)
232 END_DECLARE_ANIMATED_PROPERTIES
233
234 // SVGTests
235 virtual void synchronizeRequiredFeatures() { SVGTests::synchronizeRequiredFeatures(this); }
236 virtual void synchronizeRequiredExtensions() { SVGTests::synchronizeRequiredExtensions(this); }
237 virtual void synchronizeSystemLanguage() { SVGTests::synchronizeSystemLanguage(this); }
238
239 void setCalcMode(const AtomicString&);
240
241 bool m_animationValid;
242
243 AttributeType m_attributeType;
244 Vector<String> m_values;
245 Vector<float> m_keyTimes;
246 Vector<float> m_keyPoints;
247 Vector<UnitBezier> m_keySplines;
248 String m_lastValuesAnimationFrom;
249 String m_lastValuesAnimationTo;
250 bool m_hasInvalidCSSAttributeType;
251 CalcMode m_calcMode;
252 AnimationMode m_animationMode;
253};
254
255} // namespace WebCore
256
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100257#endif // SVGAnimationElement_h