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 "SVGAnimatedNumber.h"
22
23#include "SVGAnimateElement.h"
24#include "SVGParserUtilities.h"
25
26namespace WebCore {
27
28SVGAnimatedNumberAnimator::SVGAnimatedNumberAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
29    : SVGAnimatedTypeAnimator(AnimatedNumber, animationElement, contextElement)
30{
31}
32
33std::unique_ptr<SVGAnimatedType> SVGAnimatedNumberAnimator::constructFromString(const String& string)
34{
35    auto animatedType = SVGAnimatedType::createNumber(std::make_unique<float>());
36    float& animatedNumber = animatedType->number();
37    if (!parseNumberFromString(string, animatedNumber))
38        animatedNumber = 0;
39    return animatedType;
40}
41
42std::unique_ptr<SVGAnimatedType> SVGAnimatedNumberAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
43{
44    return SVGAnimatedType::createNumber(constructFromBaseValue<SVGAnimatedNumber>(animatedTypes));
45}
46
47void SVGAnimatedNumberAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
48{
49    stopAnimValAnimationForType<SVGAnimatedNumber>(animatedTypes);
50}
51
52void SVGAnimatedNumberAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
53{
54    resetFromBaseValue<SVGAnimatedNumber>(animatedTypes, type, &SVGAnimatedType::number);
55}
56
57void SVGAnimatedNumberAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
58{
59    animValWillChangeForType<SVGAnimatedNumber>(animatedTypes);
60}
61
62void SVGAnimatedNumberAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
63{
64    animValDidChangeForType<SVGAnimatedNumber>(animatedTypes);
65}
66
67void SVGAnimatedNumberAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
68{
69    ASSERT(from->type() == AnimatedNumber);
70    ASSERT(from->type() == to->type());
71
72    to->number() += from->number();
73}
74
75static float parseNumberFromString(SVGAnimationElement*, const String& string)
76{
77    float number = 0;
78    parseNumberFromString(string, number);
79    return number;
80}
81
82void SVGAnimatedNumberAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
83{
84    ASSERT(m_animationElement);
85    ASSERT(m_contextElement);
86
87    float fromNumber = m_animationElement->animationMode() == ToAnimation ? animated->number() : from->number();
88    float toNumber = to->number();
89    float toAtEndOfDurationNumber = toAtEndOfDuration->number();
90    float& animatedNumber = animated->number();
91
92    // Apply CSS inheritance rules.
93    m_animationElement->adjustForInheritance<float>(parseNumberFromString, m_animationElement->fromPropertyValueType(), fromNumber, m_contextElement);
94    m_animationElement->adjustForInheritance<float>(parseNumberFromString, m_animationElement->toPropertyValueType(), toNumber, m_contextElement);
95
96    m_animationElement->animateAdditiveNumber(percentage, repeatCount, fromNumber, toNumber, toAtEndOfDurationNumber, animatedNumber);
97}
98
99float SVGAnimatedNumberAnimator::calculateDistance(const String& fromString, const String& toString)
100{
101    ASSERT(m_contextElement);
102    float from = 0;
103    float to = 0;
104    parseNumberFromString(fromString, from);
105    parseNumberFromString(toString, to);
106    return fabsf(to - from);
107}
108
109}
110