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 "SVGAnimatedLength.h"
25 #include "SVGAnimateElement.h"
26 #include "SVGAnimatedNumber.h"
30 SVGAnimatedLengthAnimator::SVGAnimatedLengthAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
31 : SVGAnimatedTypeAnimator(AnimatedLength, animationElement, contextElement)
32 , m_lengthMode(SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()))
36 static inline SVGLength& sharedSVGLength(SVGLengthMode mode, const String& valueAsString)
38 DEFINE_STATIC_LOCAL(SVGLength, sharedLength, ());
40 sharedLength.setValueAsString(valueAsString, mode, ec);
45 PassOwnPtr<SVGAnimatedType> SVGAnimatedLengthAnimator::constructFromString(const String& string)
47 return SVGAnimatedType::createLength(new SVGLength(m_lengthMode, string));
50 void SVGAnimatedLengthAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
52 ASSERT(m_contextElement);
53 ASSERT(m_animationElement);
54 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
55 animationElement->determinePropertyValueTypes(fromString, toString);
57 from = constructFromString(fromString);
58 to = constructFromString(toString);
61 void SVGAnimatedLengthAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
63 ASSERT(m_contextElement);
64 ASSERT(m_animationElement);
65 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
66 animationElement->determinePropertyValueTypes(fromString, byString);
68 from = constructFromString(fromString);
69 to = constructFromString(byString);
71 SVGLength& fromLength = from->length();
72 SVGLength& toLength = to->length();
74 toLength.setValue(toLength.value(m_contextElement) + fromLength.value(m_contextElement), m_contextElement, ec);
78 void SVGAnimatedLengthAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount,
79 OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated)
81 ASSERT(m_animationElement);
82 ASSERT(m_contextElement);
84 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
85 AnimationMode animationMode = animationElement->animationMode();
87 // To animation uses contributions from the lower priority animations as the base value.
88 SVGLength& animatedSVGLength = animated->length();
89 SVGLength& fromSVGLength = from->length();
90 if (animationMode == ToAnimation)
91 fromSVGLength = animatedSVGLength;
93 // Replace 'inherit' by their computed property values.
94 SVGLength& toSVGLength = to->length();
95 if (animationElement->fromPropertyValueType() == InheritValue) {
96 String fromLengthString;
97 animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), fromLengthString);
98 fromSVGLength = sharedSVGLength(m_lengthMode, fromLengthString);
100 if (animationElement->toPropertyValueType() == InheritValue) {
101 String toLengthString;
102 animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toLengthString);
103 toSVGLength = sharedSVGLength(m_lengthMode, toLengthString);
106 float result = animatedSVGLength.value(m_contextElement);
107 SVGLengthType unitType = percentage < 0.5 ? fromSVGLength.unitType() : toSVGLength.unitType();
108 SVGAnimatedNumberAnimator::calculateAnimatedNumber(animationElement, percentage, repeatCount, result, fromSVGLength.value(m_contextElement), toSVGLength.value(m_contextElement));
110 ExceptionCode ec = 0;
111 animatedSVGLength.setValue(m_contextElement, result, m_lengthMode, unitType, ec);
115 float SVGAnimatedLengthAnimator::calculateDistance(const String& fromString, const String& toString)
117 ASSERT(m_animationElement);
118 ASSERT(m_contextElement);
119 SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
120 SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName());
121 SVGLength from = SVGLength(lengthMode, fromString);
122 SVGLength to = SVGLength(lengthMode, toString);
123 return fabsf(to.value(m_contextElement) - from.value(m_contextElement));
128 #endif // ENABLE(SVG) && ENABLE(SVG_ANIMATION)