initial import
[vuplus_webkit] / Source / WebCore / svg / SVGAnimatedNumber.cpp
1 /*
2  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #if ENABLE(SVG) && ENABLE(SVG_ANIMATION)
23 #include "SVGAnimatedNumber.h"
24
25 #include "SVGAnimateElement.h"
26 #include "SVGParserUtilities.h"
27
28 using namespace std;
29
30 namespace WebCore {
31
32 SVGAnimatedNumberAnimator::SVGAnimatedNumberAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
33     : SVGAnimatedTypeAnimator(AnimatedNumber, animationElement, contextElement)
34 {
35 }
36
37 PassOwnPtr<SVGAnimatedType> SVGAnimatedNumberAnimator::constructFromString(const String& string)
38 {
39     OwnPtr<SVGAnimatedType> animtedType = SVGAnimatedType::createNumber(new float);
40     float& animatedNumber = animtedType->number();
41     if (!parseNumberFromString(string, animatedNumber))
42         animatedNumber = 0;
43     return animtedType.release();
44 }
45
46 void SVGAnimatedNumberAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
47 {
48     ASSERT(m_contextElement);
49     ASSERT(m_animationElement);
50     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
51     animationElement->determinePropertyValueTypes(fromString, toString);
52     
53     from = constructFromString(fromString);
54     to = constructFromString(toString);
55 }
56
57 void SVGAnimatedNumberAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
58 {
59     ASSERT(m_contextElement);
60     ASSERT(m_animationElement);
61     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
62     animationElement->determinePropertyValueTypes(fromString, byString);
63     
64     from = constructFromString(fromString);
65     to = constructFromString(byString);
66
67     to->number() += from->number();
68 }
69
70 void SVGAnimatedNumberAnimator::calculateAnimatedNumber(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, float& animatedNumber, float fromNumber, float toNumber)
71 {
72     float number;
73     if (animationElement->calcMode() == CalcModeDiscrete)
74         number = percentage < 0.5 ? fromNumber : toNumber;
75     else
76         number = (toNumber - fromNumber) * percentage + fromNumber;
77         
78     // FIXME: This is not correct for values animation. Right now we transform values-animation to multiple from-to-animations and
79     // accumulate every single value to the previous one. But accumulation should just take into account after a complete cycle
80     // of values-animaiton. See example at: http://www.w3.org/TR/2001/REC-smil-animation-20010904/#RepeatingAnim
81     if (animationElement->isAccumulated() && repeatCount)
82         number += toNumber * repeatCount;
83     if (animationElement->isAdditive() && animationElement->animationMode() != ToAnimation)
84         animatedNumber += number;
85     else
86         animatedNumber = number;
87 }
88
89 void SVGAnimatedNumberAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount,
90                                                        OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated)
91 {
92     ASSERT(m_animationElement);
93     ASSERT(m_contextElement);
94     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
95     
96     AnimationMode animationMode = animationElement->animationMode();
97     // To animation uses contributions from the lower priority animations as the base value.
98     float& animatedNumber = animated->number();
99     if (animationMode == ToAnimation)
100         from->number() = animatedNumber;
101     
102     // Replace 'inherit' by their computed property values.
103     float& fromNumber = from->number();
104     float& toNumber = to->number();
105     if (animationElement->fromPropertyValueType() == InheritValue) {
106         String fromNumberString;
107         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), fromNumberString);
108         parseNumberFromString(fromNumberString, fromNumber); 
109     }
110     if (animationElement->toPropertyValueType() == InheritValue) {
111         String toNumberString;
112         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toNumberString);
113         parseNumberFromString(toNumberString, toNumber); 
114     }
115     
116     calculateAnimatedNumber(animationElement, percentage, repeatCount, animatedNumber, fromNumber, toNumber);
117 }
118
119 float SVGAnimatedNumberAnimator::calculateDistance(const String& fromString, const String& toString)
120 {
121     ASSERT(m_contextElement);
122     float from = 0;
123     float to = 0;
124     parseNumberFromString(fromString, from);
125     parseNumberFromString(toString, to);
126     return fabsf(to - from);
127 }
128
129 }
130
131 #endif // ENABLE(SVG) && ENABLE(SVG_ANIMATION)