2012-03-15 Nikolas Zimmermann <nzimmermann@rim.com>
[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)
23 #include "SVGAnimatedLength.h"
24
25 #include "SVGAnimateElement.h"
26 #include "SVGAnimatedNumber.h"
27
28 namespace WebCore {
29
30 SVGAnimatedLengthAnimator::SVGAnimatedLengthAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
31     : SVGAnimatedTypeAnimator(AnimatedLength, animationElement, contextElement)
32     , m_lengthMode(SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()))
33 {
34 }
35
36 static inline SVGLength& sharedSVGLength(SVGLengthMode mode, const String& valueAsString)
37 {
38     DEFINE_STATIC_LOCAL(SVGLength, sharedLength, ());
39     ExceptionCode ec = 0;
40     sharedLength.setValueAsString(valueAsString, mode, ec);
41     ASSERT(!ec);
42     return sharedLength;
43 }
44
45 PassOwnPtr<SVGAnimatedType> SVGAnimatedLengthAnimator::constructFromString(const String& string)
46 {
47     return SVGAnimatedType::createLength(new SVGLength(m_lengthMode, string));
48 }
49
50 PassOwnPtr<SVGAnimatedType> SVGAnimatedLengthAnimator::constructFromVariant(SVGGenericAnimatedType* animatedType)
51 {
52     ASSERT(animatedType);
53     return SVGAnimatedType::createLength(new SVGLength(*reinterpret_cast<SVGLength*>(animatedType)));
54 }
55
56 void SVGAnimatedLengthAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
57 {
58     ASSERT(m_contextElement);
59     ASSERT(m_animationElement);
60     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
61     animationElement->determinePropertyValueTypes(fromString, toString);
62     
63     from = constructFromString(fromString);
64     to = constructFromString(toString);
65 }
66
67 void SVGAnimatedLengthAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
68 {
69     ASSERT(m_contextElement);
70     ASSERT(m_animationElement);
71     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
72     animationElement->determinePropertyValueTypes(fromString, byString);
73     
74     from = constructFromString(fromString);
75     to = constructFromString(byString);
76     
77     SVGLengthContext lengthContext(m_contextElement);
78     SVGLength& fromLength = from->length();
79     SVGLength& toLength = to->length();
80     ExceptionCode ec = 0;
81     toLength.setValue(toLength.value(lengthContext) + fromLength.value(lengthContext), lengthContext, ec);
82     ASSERT(!ec);
83 }
84
85 void SVGAnimatedLengthAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount,
86                                                        OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, OwnPtr<SVGAnimatedType>& animated)
87 {
88     ASSERT(m_animationElement);
89     ASSERT(m_contextElement);
90
91     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
92     AnimationMode animationMode = animationElement->animationMode();
93
94     // To animation uses contributions from the lower priority animations as the base value.
95     SVGLength& animatedSVGLength = animated->length();
96     SVGLength& fromSVGLength = from->length();
97     if (animationMode == ToAnimation)
98         fromSVGLength = animatedSVGLength;
99     
100     // Replace 'inherit' by their computed property values.
101     SVGLength& toSVGLength = to->length();
102     if (animationElement->fromPropertyValueType() == InheritValue) {
103         String fromLengthString;
104         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), fromLengthString);
105         fromSVGLength = sharedSVGLength(m_lengthMode, fromLengthString);
106     }
107     if (animationElement->toPropertyValueType() == InheritValue) {
108         String toLengthString;
109         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toLengthString);
110         toSVGLength = sharedSVGLength(m_lengthMode, toLengthString); 
111     }
112     
113     SVGLengthContext lengthContext(m_contextElement);
114     float result = animatedSVGLength.value(lengthContext);
115     SVGLengthType unitType = percentage < 0.5 ? fromSVGLength.unitType() : toSVGLength.unitType();
116     SVGAnimatedNumberAnimator::calculateAnimatedNumber(animationElement, percentage, repeatCount, result, fromSVGLength.value(lengthContext), toSVGLength.value(lengthContext));
117
118     ExceptionCode ec = 0;
119     animatedSVGLength.setValue(lengthContext, result, m_lengthMode, unitType, ec);
120     ASSERT(!ec);
121 }
122
123 float SVGAnimatedLengthAnimator::calculateDistance(const String& fromString, const String& toString)
124 {
125     ASSERT(m_animationElement);
126     ASSERT(m_contextElement);
127     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);    
128     SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName());
129     SVGLength from = SVGLength(lengthMode, fromString);
130     SVGLength to = SVGLength(lengthMode, toString);
131     SVGLengthContext lengthContext(m_contextElement);
132     return fabsf(to.value(lengthContext) - from.value(lengthContext));
133 }
134
135 }
136
137 #endif // ENABLE(SVG)