Unreviewed, rolling out r142118.
[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::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
51 {
52     return SVGAnimatedType::createLength(constructFromBaseValue<SVGAnimatedLength>(animatedTypes));
53 }
54
55 void SVGAnimatedLengthAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
56 {
57     stopAnimValAnimationForType<SVGAnimatedLength>(animatedTypes);
58 }
59
60 void SVGAnimatedLengthAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
61 {
62     resetFromBaseValue<SVGAnimatedLength>(animatedTypes, type, &SVGAnimatedType::length);
63 }
64
65 void SVGAnimatedLengthAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
66 {
67     animValWillChangeForType<SVGAnimatedLength>(animatedTypes);
68 }
69
70 void SVGAnimatedLengthAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
71 {
72     animValDidChangeForType<SVGAnimatedLength>(animatedTypes);
73 }
74
75 void SVGAnimatedLengthAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
76 {
77     ASSERT(from->type() == AnimatedLength);
78     ASSERT(from->type() == to->type());
79
80     SVGLengthContext lengthContext(m_contextElement);
81     const SVGLength& fromLength = from->length();
82     SVGLength& toLength = to->length();
83
84     ExceptionCode ec = 0;
85     toLength.setValue(toLength.value(lengthContext) + fromLength.value(lengthContext), lengthContext, ec);
86     ASSERT(!ec);
87 }
88
89 static SVGLength parseLengthFromString(SVGAnimationElement* animationElement, const String& string)
90 {
91     return sharedSVGLength(SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()), string);
92 }
93
94 void SVGAnimatedLengthAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
95 {
96     ASSERT(m_animationElement);
97     ASSERT(m_contextElement);
98
99     SVGLength fromSVGLength = m_animationElement->animationMode() == ToAnimation ? animated->length() : from->length();
100     SVGLength toSVGLength = to->length();
101     const SVGLength& toAtEndOfDurationSVGLength = toAtEndOfDuration->length();
102     SVGLength& animatedSVGLength = animated->length();
103
104     // Apply CSS inheritance rules.
105     m_animationElement->adjustForInheritance<SVGLength>(parseLengthFromString, m_animationElement->fromPropertyValueType(), fromSVGLength, m_contextElement);
106     m_animationElement->adjustForInheritance<SVGLength>(parseLengthFromString, m_animationElement->toPropertyValueType(), toSVGLength, m_contextElement);
107
108     SVGLengthContext lengthContext(m_contextElement);
109     float animatedNumber = animatedSVGLength.value(lengthContext);
110     SVGLengthType unitType = percentage < 0.5 ? fromSVGLength.unitType() : toSVGLength.unitType();
111     m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromSVGLength.value(lengthContext), toSVGLength.value(lengthContext), toAtEndOfDurationSVGLength.value(lengthContext), animatedNumber);
112
113     ExceptionCode ec = 0;
114     animatedSVGLength.setValue(lengthContext, animatedNumber, m_lengthMode, unitType, ec);
115     ASSERT(!ec);
116 }
117
118 float SVGAnimatedLengthAnimator::calculateDistance(const String& fromString, const String& toString)
119 {
120     ASSERT(m_animationElement);
121     ASSERT(m_contextElement);
122     SVGAnimateElement* animationElement = static_cast<SVGAnimateElement*>(m_animationElement);    
123     SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName());
124     SVGLength from = SVGLength(lengthMode, fromString);
125     SVGLength to = SVGLength(lengthMode, toString);
126     SVGLengthContext lengthContext(m_contextElement);
127     return fabsf(to.value(lengthContext) - from.value(lengthContext));
128 }
129
130 }
131
132 #endif // ENABLE(SVG)