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