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 "SVGAnimatedLengthList.h"
22
23#include "SVGAnimateElement.h"
24#include "SVGAnimatedNumber.h"
25
26namespace WebCore {
27
28SVGAnimatedLengthListAnimator::SVGAnimatedLengthListAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
29    : SVGAnimatedTypeAnimator(AnimatedLengthList, animationElement, contextElement)
30    , m_lengthMode(SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()))
31{
32}
33
34std::unique_ptr<SVGAnimatedType> SVGAnimatedLengthListAnimator::constructFromString(const String& string)
35{
36    auto animatedType = SVGAnimatedType::createLengthList(std::make_unique<SVGLengthList>());
37    animatedType->lengthList().parse(string, m_lengthMode);
38    return animatedType;
39}
40
41std::unique_ptr<SVGAnimatedType> SVGAnimatedLengthListAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
42{
43    return SVGAnimatedType::createLengthList(constructFromBaseValue<SVGAnimatedLengthList>(animatedTypes));
44}
45
46void SVGAnimatedLengthListAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
47{
48    stopAnimValAnimationForType<SVGAnimatedLengthList>(animatedTypes);
49}
50
51void SVGAnimatedLengthListAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
52{
53    resetFromBaseValue<SVGAnimatedLengthList>(animatedTypes, type, &SVGAnimatedType::lengthList);
54}
55
56void SVGAnimatedLengthListAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
57{
58    animValWillChangeForType<SVGAnimatedLengthList>(animatedTypes);
59}
60
61void SVGAnimatedLengthListAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
62{
63    animValDidChangeForType<SVGAnimatedLengthList>(animatedTypes);
64}
65
66void SVGAnimatedLengthListAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
67{
68    ASSERT(from->type() == AnimatedLengthList);
69    ASSERT(from->type() == to->type());
70
71    const SVGLengthList& fromLengthList = from->lengthList();
72    SVGLengthList& toLengthList = to->lengthList();
73
74    unsigned fromLengthListSize = fromLengthList.size();
75    if (!fromLengthListSize || fromLengthListSize != toLengthList.size())
76        return;
77
78    SVGLengthContext lengthContext(m_contextElement);
79    for (unsigned i = 0; i < fromLengthListSize; ++i)
80        toLengthList[i].setValue(toLengthList[i].value(lengthContext) + fromLengthList[i].value(lengthContext), lengthContext, ASSERT_NO_EXCEPTION);
81}
82
83static SVGLengthList parseLengthListFromString(SVGAnimationElement* animationElement, const String& string)
84{
85    SVGLengthList lengthList;
86    lengthList.parse(string, SVGLength::lengthModeForAnimatedLengthAttribute(animationElement->attributeName()));
87    return lengthList;
88}
89
90void SVGAnimatedLengthListAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
91{
92    ASSERT(m_animationElement);
93    ASSERT(m_contextElement);
94
95    SVGLengthList fromLengthList = m_animationElement->animationMode() == ToAnimation ? animated->lengthList() : from->lengthList();
96    SVGLengthList toLengthList = to->lengthList();
97    const SVGLengthList& toAtEndOfDurationLengthList = toAtEndOfDuration->lengthList();
98    SVGLengthList& animatedLengthList = animated->lengthList();
99
100    // Apply CSS inheritance rules.
101    m_animationElement->adjustForInheritance<SVGLengthList>(parseLengthListFromString, m_animationElement->fromPropertyValueType(), fromLengthList, m_contextElement);
102    m_animationElement->adjustForInheritance<SVGLengthList>(parseLengthListFromString, m_animationElement->toPropertyValueType(), toLengthList, m_contextElement);
103
104    if (!m_animationElement->adjustFromToListValues<SVGLengthList>(fromLengthList, toLengthList, animatedLengthList, percentage))
105        return;
106
107    unsigned fromLengthListSize = fromLengthList.size();
108    unsigned toLengthListSize = toLengthList.size();
109    unsigned toAtEndOfDurationListSize = toAtEndOfDurationLengthList.size();
110
111    SVGLengthContext lengthContext(m_contextElement);
112    for (unsigned i = 0; i < toLengthListSize; ++i) {
113        float animatedNumber = animatedLengthList[i].value(lengthContext);
114        SVGLengthType unitType = toLengthList[i].unitType();
115        float effectiveFrom = 0;
116        if (fromLengthListSize) {
117            if (percentage < 0.5)
118                unitType = fromLengthList[i].unitType();
119            effectiveFrom = fromLengthList[i].value(lengthContext);
120        }
121        float effectiveToAtEnd = i < toAtEndOfDurationListSize ? toAtEndOfDurationLengthList[i].value(lengthContext) : 0;
122
123        m_animationElement->animateAdditiveNumber(percentage, repeatCount, effectiveFrom, toLengthList[i].value(lengthContext), effectiveToAtEnd, animatedNumber);
124        animatedLengthList[i].setValue(lengthContext, animatedNumber, m_lengthMode, unitType, ASSERT_NO_EXCEPTION);
125    }
126}
127
128float SVGAnimatedLengthListAnimator::calculateDistance(const String&, const String&)
129{
130    // FIXME: Distance calculation is not possible for SVGLengthList right now. We need the distance for every single value.
131    return -1;
132}
133
134}
135