Enable animVal support for SVGNumberOptionalNumber
[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::constructFromBaseValue(const Vector<SVGAnimatedProperty*>& properties, Vector<SVGGenericAnimatedType*>& types)
51 {
52     return SVGAnimatedType::createLength(constructFromOneBaseValue<SVGLength>(properties, types));
53 }
54
55 void SVGAnimatedLengthAnimator::resetAnimatedTypeToBaseValue(const Vector<SVGAnimatedProperty*>& properties, SVGAnimatedType* type)
56 {
57     resetAnimatedTypeFromOneBaseValue<SVGLength>(properties, type, &SVGAnimatedType::length);
58 }
59
60 void SVGAnimatedLengthAnimator::calculateFromAndToValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& toString)
61 {
62     ASSERT(m_contextElement);
63     ASSERT(m_animationElement);
64     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
65     animationElement->determinePropertyValueTypes(fromString, toString);
66     
67     from = constructFromString(fromString);
68     to = constructFromString(toString);
69 }
70
71 void SVGAnimatedLengthAnimator::calculateFromAndByValues(OwnPtr<SVGAnimatedType>& from, OwnPtr<SVGAnimatedType>& to, const String& fromString, const String& byString)
72 {
73     ASSERT(m_contextElement);
74     ASSERT(m_animationElement);
75     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
76     animationElement->determinePropertyValueTypes(fromString, byString);
77     
78     from = constructFromString(fromString);
79     to = constructFromString(byString);
80     
81     SVGLengthContext lengthContext(m_contextElement);
82     SVGLength& fromLength = from->length();
83     SVGLength& toLength = to->length();
84     ExceptionCode ec = 0;
85     toLength.setValue(toLength.value(lengthContext) + fromLength.value(lengthContext), lengthContext, ec);
86     ASSERT(!ec);
87 }
88
89 void SVGAnimatedLengthAnimator::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
95     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);
96     AnimationMode animationMode = animationElement->animationMode();
97
98     // To animation uses contributions from the lower priority animations as the base value.
99     SVGLength& animatedSVGLength = animated->length();
100     SVGLength& fromSVGLength = from->length();
101     if (animationMode == ToAnimation)
102         fromSVGLength = animatedSVGLength;
103     
104     // Replace 'inherit' by their computed property values.
105     SVGLength& toSVGLength = to->length();
106     if (animationElement->fromPropertyValueType() == InheritValue) {
107         String fromLengthString;
108         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), fromLengthString);
109         fromSVGLength = sharedSVGLength(m_lengthMode, fromLengthString);
110     }
111     if (animationElement->toPropertyValueType() == InheritValue) {
112         String toLengthString;
113         animationElement->adjustForInheritance(m_contextElement, animationElement->attributeName(), toLengthString);
114         toSVGLength = sharedSVGLength(m_lengthMode, toLengthString); 
115     }
116     
117     SVGLengthContext lengthContext(m_contextElement);
118     float result = animatedSVGLength.value(lengthContext);
119     SVGLengthType unitType = percentage < 0.5 ? fromSVGLength.unitType() : toSVGLength.unitType();
120     SVGAnimatedNumberAnimator::calculateAnimatedNumber(animationElement, percentage, repeatCount, result, fromSVGLength.value(lengthContext), toSVGLength.value(lengthContext));
121
122     ExceptionCode ec = 0;
123     animatedSVGLength.setValue(lengthContext, result, m_lengthMode, unitType, ec);
124     ASSERT(!ec);
125 }
126
127 float SVGAnimatedLengthAnimator::calculateDistance(const String& fromString, const String& toString)
128 {
129     ASSERT(m_animationElement);
130     ASSERT(m_contextElement);
131     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);    
132     SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName());
133     SVGLength from = SVGLength(lengthMode, fromString);
134     SVGLength to = SVGLength(lengthMode, toString);
135     SVGLengthContext lengthContext(m_contextElement);
136     return fabsf(to.value(lengthContext) - from.value(lengthContext));
137 }
138
139 }
140
141 #endif // ENABLE(SVG)