Move SVG from ExceptionCode to Exception
[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 #include "SVGAnimatedLength.h"
22
23 #include "SVGAnimateElementBase.h"
24 #include "SVGAnimatedNumber.h"
25 #include <wtf/NeverDestroyed.h>
26
27 namespace WebCore {
28
29 SVGAnimatedLengthAnimator::SVGAnimatedLengthAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
30     : SVGAnimatedTypeAnimator(AnimatedLength, animationElement, contextElement)
31     , m_lengthMode(SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()))
32 {
33 }
34
35 std::unique_ptr<SVGAnimatedType> SVGAnimatedLengthAnimator::constructFromString(const String& string)
36 {
37     return SVGAnimatedType::createLength(std::make_unique<SVGLength>(m_lengthMode, string));
38 }
39
40 std::unique_ptr<SVGAnimatedType> SVGAnimatedLengthAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
41 {
42     return SVGAnimatedType::createLength(constructFromBaseValue<SVGAnimatedLength>(animatedTypes));
43 }
44
45 void SVGAnimatedLengthAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
46 {
47     stopAnimValAnimationForType<SVGAnimatedLength>(animatedTypes);
48 }
49
50 void SVGAnimatedLengthAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType& type)
51 {
52     resetFromBaseValue<SVGAnimatedLength>(animatedTypes, type, &SVGAnimatedType::length);
53 }
54
55 void SVGAnimatedLengthAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
56 {
57     animValWillChangeForType<SVGAnimatedLength>(animatedTypes);
58 }
59
60 void SVGAnimatedLengthAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
61 {
62     animValDidChangeForType<SVGAnimatedLength>(animatedTypes);
63 }
64
65 void SVGAnimatedLengthAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
66 {
67     ASSERT(from->type() == AnimatedLength);
68     ASSERT(from->type() == to->type());
69
70     SVGLengthContext lengthContext(m_contextElement);
71     const SVGLength& fromLength = from->length();
72     SVGLength& toLength = to->length();
73
74     toLength.setValue(toLength.value(lengthContext) + fromLength.value(lengthContext), lengthContext);
75 }
76
77 static SVGLength parseLengthFromString(SVGAnimationElement* animationElement, const String& string)
78 {
79     SVGLength length;
80     length.setValueAsString(string, SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
81     return length;
82 }
83
84 void SVGAnimatedLengthAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
85 {
86     ASSERT(m_animationElement);
87     ASSERT(m_contextElement);
88
89     SVGLength fromSVGLength = m_animationElement->animationMode() == ToAnimation ? animated->length() : from->length();
90     SVGLength toSVGLength = to->length();
91     const SVGLength& toAtEndOfDurationSVGLength = toAtEndOfDuration->length();
92     SVGLength& animatedSVGLength = animated->length();
93
94     // Apply CSS inheritance rules.
95     m_animationElement->adjustForInheritance<SVGLength>(parseLengthFromString, m_animationElement->fromPropertyValueType(), fromSVGLength, m_contextElement);
96     m_animationElement->adjustForInheritance<SVGLength>(parseLengthFromString, m_animationElement->toPropertyValueType(), toSVGLength, m_contextElement);
97
98     SVGLengthContext lengthContext(m_contextElement);
99     float animatedNumber = animatedSVGLength.value(lengthContext);
100     SVGLengthType unitType = percentage < 0.5 ? fromSVGLength.unitType() : toSVGLength.unitType();
101     m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromSVGLength.value(lengthContext), toSVGLength.value(lengthContext), toAtEndOfDurationSVGLength.value(lengthContext), animatedNumber);
102
103     animatedSVGLength.setValue(lengthContext, animatedNumber, m_lengthMode, unitType);
104 }
105
106 float SVGAnimatedLengthAnimator::calculateDistance(const String& fromString, const String& toString)
107 {
108     ASSERT(m_animationElement);
109     ASSERT(m_contextElement);
110     SVGLengthMode lengthMode = SVGLength::lengthModeForAnimatedLengthAttribute(m_animationElement->attributeName());
111     SVGLength from = SVGLength(lengthMode, fromString);
112     SVGLength to = SVGLength(lengthMode, toString);
113     SVGLengthContext lengthContext(m_contextElement);
114     return fabsf(to.value(lengthContext) - from.value(lengthContext));
115 }
116
117 }