Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGAnimationElement.h
1 /*
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
28 #include "core/svg/SVGAnimatedBoolean.h"
29 #include "core/svg/animation/SVGSMILElement.h"
30 #include "platform/animation/UnitBezier.h"
31 #include "wtf/Functional.h"
32
33 namespace blink {
34
35 enum AnimationMode {
36     NoAnimation,
37     FromToAnimation,
38     FromByAnimation,
39     ToAnimation,
40     ByAnimation,
41     ValuesAnimation,
42     PathAnimation // Used by AnimateMotion.
43 };
44
45 // If we have 'inherit' as animation value, we need to grab the value
46 // during the animation since the value can be animated itself.
47 enum AnimatedPropertyValueType {
48     RegularPropertyValue,
49     InheritValue
50 };
51
52 enum CalcMode {
53     CalcModeDiscrete,
54     CalcModeLinear,
55     CalcModePaced,
56     CalcModeSpline
57 };
58
59 class SVGAnimationElement : public SVGSMILElement {
60 public:
61     // SVGAnimationElement
62     float getStartTime() const;
63     float getCurrentTime() const;
64     float getSimpleDuration() const;
65
66     void beginElement();
67     void beginElementAt(float offset);
68     void endElement();
69     void endElementAt(float offset);
70
71     DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(begin, beginEvent);
72     DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(end, endEvent);
73     DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(repeat, repeatEvent);
74
75     static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&);
76
77     virtual bool isAdditive();
78     bool isAccumulated() const;
79     AnimationMode animationMode() const { return m_animationMode; }
80     CalcMode calcMode() const { return m_calcMode; }
81
82     enum ShouldApplyAnimation {
83         DontApplyAnimation,
84         ApplyCSSAnimation,
85         ApplyXMLAnimation
86     };
87
88     ShouldApplyAnimation shouldApplyAnimation(SVGElement* targetElement, const QualifiedName& attributeName);
89
90     AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; }
91     AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; }
92
93     template<typename AnimatedType, typename ParseTypeFromStringType>
94     void adjustForInheritance(ParseTypeFromStringType parseTypeFromString, AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement)
95     {
96         if (valueType != InheritValue)
97             return;
98         // Replace 'inherit' by its computed property value.
99         String typeString;
100         adjustForInheritance(contextElement, attributeName(), typeString);
101         animatedType = parseTypeFromString(this, typeString);
102     }
103
104     template<typename AnimatedType>
105     void animateDiscreteType(float percentage, const AnimatedType& fromType, const AnimatedType& toType, AnimatedType& animatedType)
106     {
107         if ((animationMode() == FromToAnimation && percentage > 0.5) || animationMode() == ToAnimation || percentage == 1) {
108             animatedType = AnimatedType(toType);
109             return;
110         }
111         animatedType = AnimatedType(fromType);
112     }
113
114     void animateAdditiveNumber(float percentage, unsigned repeatCount, float fromNumber, float toNumber, float toAtEndOfDurationNumber, float& animatedNumber)
115     {
116         float number;
117         if (calcMode() == CalcModeDiscrete)
118             number = percentage < 0.5 ? fromNumber : toNumber;
119         else
120             number = (toNumber - fromNumber) * percentage + fromNumber;
121
122         if (isAccumulated() && repeatCount)
123             number += toAtEndOfDurationNumber * repeatCount;
124
125         if (isAdditive() && animationMode() != ToAnimation)
126             animatedNumber += number;
127         else
128             animatedNumber = number;
129     }
130
131 protected:
132     SVGAnimationElement(const QualifiedName&, Document&);
133
134     void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value);
135     void determinePropertyValueTypes(const String& from, const String& to);
136
137     bool isSupportedAttribute(const QualifiedName&);
138     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
139     virtual void svgAttributeChanged(const QualifiedName&) OVERRIDE;
140
141     enum AttributeType {
142         AttributeTypeCSS,
143         AttributeTypeXML,
144         AttributeTypeAuto
145     };
146     AttributeType attributeType() const { return m_attributeType; }
147
148     String toValue() const;
149     String byValue() const;
150     String fromValue() const;
151
152     // from SVGSMILElement
153     virtual void startedActiveInterval() OVERRIDE;
154     virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) OVERRIDE;
155
156     AnimatedPropertyValueType m_fromPropertyValueType;
157     AnimatedPropertyValueType m_toPropertyValueType;
158
159     virtual void setTargetElement(SVGElement*) OVERRIDE;
160     virtual void setAttributeName(const QualifiedName&) OVERRIDE;
161
162     bool hasInvalidCSSAttributeType() const { return m_hasInvalidCSSAttributeType; }
163
164     virtual void updateAnimationMode();
165     void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; }
166     void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; }
167
168 private:
169     virtual bool isValid() const OVERRIDE FINAL { return SVGTests::isValid(); }
170
171     virtual void animationAttributeChanged() OVERRIDE;
172     void setAttributeType(const AtomicString&);
173
174     void checkInvalidCSSAttributeType(SVGElement*);
175
176     virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0;
177     virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0;
178     virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0;
179     virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0;
180     virtual float calculateDistance(const String& /*fromString*/, const String& /*toString*/) { return -1.f; }
181
182     void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to);
183     void calculateKeyTimesForCalcModePaced();
184     float calculatePercentFromKeyPoints(float percent) const;
185     void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const;
186     float calculatePercentForSpline(float percent, unsigned splineIndex) const;
187     float calculatePercentForFromTo(float percent) const;
188     unsigned calculateKeyTimesIndex(float percent) const;
189
190     void adjustForInheritance(SVGElement* targetElement, const QualifiedName& attributeName, String&);
191
192     void setCalcMode(const AtomicString&);
193
194     bool m_animationValid;
195
196     AttributeType m_attributeType;
197     Vector<String> m_values;
198     // FIXME: We should probably use doubles for this, but there's no point
199     // making such a change unless all SVG logic for sampling animations is
200     // changed to use doubles.
201     Vector<float> m_keyTimes;
202     Vector<float> m_keyPoints;
203     Vector<UnitBezier> m_keySplines;
204     String m_lastValuesAnimationFrom;
205     String m_lastValuesAnimationTo;
206     bool m_hasInvalidCSSAttributeType;
207     CalcMode m_calcMode;
208     AnimationMode m_animationMode;
209 };
210
211 } // namespace blink
212
213 #endif // SVGAnimationElement_h