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