2011-06-13 Dirk Schulze <krit@webkit.org>
[WebKit-https.git] / Source / WebCore / svg / SVGAnimatedLength.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 "SVGAnimatedLength.h"
24
25 namespace WebCore {
26
27 SVGAnimatedLengthAnimator::SVGAnimatedLengthAnimator(SVGElement* contextElement, const QualifiedName& attributeName)
28     : SVGAnimatedTypeAnimator(AnimatedLength, contextElement)
29     , m_lengthMode(SVGLength::lengthModeForAnimatedLengthAttribute(attributeName))
30 {
31 }
32
33 static inline SVGLength& sharedSVGLength(SVGLengthMode mode, const String& valueAsString)
34 {
35     DEFINE_STATIC_LOCAL(SVGLength, sharedLength, ());
36     sharedLength.setValueAsString(valueAsString, mode);
37     return sharedLength;
38 }
39
40 PassOwnPtr<SVGAnimatedType> SVGAnimatedLengthAnimator::constructFromString(const String& string)
41 {
42     return SVGAnimatedType::createLength(new SVGLength(m_lengthMode, string));
43 }
44
45 void SVGAnimatedLengthAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
46 {
47     from = constructFromString(fromString);
48     to = constructFromString(toString);
49 }
50
51 void SVGAnimatedLengthAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
52 {
53     ASSERT(m_contextElement);
54     
55     from = constructFromString(fromString);
56     to = constructFromString(byString);
57     
58     SVGLength& fromLength = from->length();
59     SVGLength& toLength = to->length();
60     ExceptionCode ec = 0;
61     toLength.setValue(toLength.value(m_contextElement) + fromLength.value(m_contextElement), m_contextElement, ec);
62     ASSERT(!ec);
63 }
64
65 void SVGAnimatedLengthAnimator::calculateAnimatedValue(SVGSMILElement* smilElement, float percentage, unsigned repeatCount,
66                                                        OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated,
67                                                        bool fromPropertyInherits, bool toPropertyInherits)
68 {
69     ASSERT(smilElement);
70     ASSERT(m_contextElement);
71     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(smilElement);
72     
73     AnimationMode animationMode = animationElement->animationMode();
74     // To animation uses contributions from the lower priority animations as the base value.
75     // FIXME: Avoid string parsing.
76     if (animationMode == ToAnimation)
77         from = constructFromString(animated->length().valueAsString());
78     
79     // Replace 'inherit' by their computed property values.
80     float number;
81     float fromLength = from->length().value(m_contextElement);
82     float toLength = to->length().value(m_contextElement);
83     
84     if (fromPropertyInherits) {
85         String fromLengthString;
86         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), fromLengthString);
87         fromLength = sharedSVGLength(m_lengthMode, fromLengthString).value(m_contextElement);
88     }
89     if (toPropertyInherits) {
90         String toLengthString;
91         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toLengthString);
92         toLength = sharedSVGLength(m_lengthMode, toLengthString).value(m_contextElement); 
93     }
94     
95     if (animationElement->calcMode() == CalcModeDiscrete)
96         number = percentage < 0.5f ? fromLength : toLength;
97     else
98         number = (toLength - fromLength) * percentage + fromLength;
99     
100     // FIXME: This is not correct for values animation.
101     if (animationElement->isAccumulated() && repeatCount)
102         number += toLength * repeatCount;
103     ExceptionCode ec = 0;
104     SVGLength& animatedSVGLength = animated->length();
105     if (animationElement->isAdditive() && animationMode != ToAnimation) {
106         float animatedSVGLengthValue = animatedSVGLength.value(m_contextElement);
107         animatedSVGLengthValue += number;
108         animatedSVGLength.setValue(animatedSVGLengthValue, m_contextElement, ec);
109     } else
110         animatedSVGLength.setValue(number, m_contextElement, ec);
111     ASSERT(!ec);
112 }
113
114 float SVGAnimatedLengthAnimator::calculateDistance(SVGSMILElement* smilElement, const String& fromString, const String& toString)
115 {
116     ASSERT(smilElement);
117     ASSERT(m_contextElement);
118     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(smilElement);    
119     SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName());
120     SVGLength from = SVGLength(lengthMode, fromString);
121     SVGLength to = SVGLength(lengthMode, toString);
122     return fabsf(to.value(m_contextElement) - from.value(m_contextElement));
123 }
124
125 }
126
127 #endif // ENABLE(SVG) && ENABLE(SVG_ANIMATION)