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