2 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
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.
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.
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.
22 #if ENABLE(SVG) && ENABLE(SVG_ANIMATION)
23 #include "SVGAnimatedNumber.h"
25 #include "SVGAnimateElement.h"
26 #include "SVGParserUtilities.h"
32 SVGAnimatedNumberAnimator::SVGAnimatedNumberAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
33 : SVGAnimatedTypeAnimator(AnimatedNumber, animationElement, contextElement)
37 PassOwnPtr<SVGAnimatedType> SVGAnimatedNumberAnimator::constructFromString(const String& string)
39 OwnPtr<SVGAnimatedType> animtedType = SVGAnimatedType::createNumber(new float);
40 float& animatedNumber = animtedType->number();
41 if (!parseNumberFromString(string, animatedNumber))
43 return animtedType.release();
46 void SVGAnimatedNumberAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
48 ASSERT(m_contextElement);
49 ASSERT(m_animationElement);
50 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
51 animationElement->determinePropertyValueTypes(fromString, toString);
53 from = constructFromString(fromString);
54 to = constructFromString(toString);
57 void SVGAnimatedNumberAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
59 ASSERT(m_contextElement);
60 ASSERT(m_animationElement);
61 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
62 animationElement->determinePropertyValueTypes(fromString, byString);
64 from = constructFromString(fromString);
65 to = constructFromString(byString);
67 to->number() += from->number();
70 void SVGAnimatedNumberAnimator::calculateAnimatedNumber(SVGAnimationElement* animationElement, float percentage, unsigned repeatCount, float& animatedNumber, float fromNumber, float toNumber)
73 if (animationElement->calcMode() == CalcModeDiscrete)
74 number = percentage < 0.5 ? fromNumber : toNumber;
76 number = (toNumber - fromNumber) * percentage + fromNumber;
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;
86 animatedNumber = number;
89 void SVGAnimatedNumberAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount,
90 OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated)
92 ASSERT(m_animationElement);
93 ASSERT(m_contextElement);
94 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
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;
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);
110 if (animationElement->toPropertyValueType() == InheritValue) {
111 String toNumberString;
112 animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toNumberString);
113 parseNumberFromString(toNumberString, toNumber);
116 calculateAnimatedNumber(animationElement, percentage, repeatCount, animatedNumber, fromNumber, toNumber);
119 float SVGAnimatedNumberAnimator::calculateDistance(const String& fromString, const String& toString)
121 ASSERT(m_contextElement);
124 parseNumberFromString(fromString, from);
125 parseNumberFromString(toString, to);
126 return fabsf(to - from);
131 #endif // ENABLE(SVG) && ENABLE(SVG_ANIMATION)