1/*
2 * Copyright (C) Research In Motion Limited 2011, 2012. 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#include "core/svg/SVGAnimatedPath.h"
23
24#include "core/svg/SVGAnimateElement.h"
25#include "core/svg/SVGPathUtilities.h"
26#include "core/svg/properties/SVGAnimatedPathSegListPropertyTearOff.h"
27
28namespace WebCore {
29
30SVGAnimatedPathAnimator::SVGAnimatedPathAnimator(SVGAnimationElement* animationElement, SVGElement* contextElement)
31    : SVGAnimatedTypeAnimator(AnimatedPath, animationElement, contextElement)
32{
33}
34
35PassOwnPtr<SVGAnimatedType> SVGAnimatedPathAnimator::constructFromString(const String& string)
36{
37    OwnPtr<SVGPathByteStream> byteStream = SVGPathByteStream::create();
38    buildSVGPathByteStreamFromString(string, byteStream.get(), UnalteredParsing);
39    return SVGAnimatedType::createPath(byteStream.release());
40}
41
42PassOwnPtr<SVGAnimatedType> SVGAnimatedPathAnimator::startAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
43{
44    ASSERT(animatedTypes.size() >= 1);
45    SVGAnimatedPathSegListPropertyTearOff* property = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes[0].properties[0].get());
46    const SVGPathSegList& baseValue = property->currentBaseValue();
47
48    // Build initial path byte stream.
49    OwnPtr<SVGPathByteStream> byteStream = SVGPathByteStream::create();
50    buildSVGPathByteStreamFromSVGPathSegList(baseValue, byteStream.get(), UnalteredParsing);
51
52    Vector<RefPtr<SVGAnimatedPathSegListPropertyTearOff> > result;
53
54    SVGElementAnimatedPropertyList::const_iterator end = animatedTypes.end();
55    for (SVGElementAnimatedPropertyList::const_iterator it = animatedTypes.begin(); it != end; ++it)
56        result.append(castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(it->properties[0].get()));
57
58    SVGElementInstance::InstanceUpdateBlocker blocker(property->contextElement());
59
60    size_t resultSize = result.size();
61    for (size_t i = 0; i < resultSize; ++i)
62        result[i]->animationStarted(byteStream.get(), &baseValue);
63
64    return SVGAnimatedType::createPath(byteStream.release());
65}
66
67void SVGAnimatedPathAnimator::stopAnimValAnimation(const SVGElementAnimatedPropertyList& animatedTypes)
68{
69    stopAnimValAnimationForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
70}
71
72void SVGAnimatedPathAnimator::resetAnimValToBaseVal(const SVGElementAnimatedPropertyList& animatedTypes, SVGAnimatedType* type)
73{
74    ASSERT(animatedTypes.size() >= 1);
75    ASSERT(type);
76    ASSERT(type->type() == m_type);
77    const SVGPathSegList& baseValue = castAnimatedPropertyToActualType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes[0].properties[0].get())->currentBaseValue();
78    buildSVGPathByteStreamFromSVGPathSegList(baseValue, type->path(), UnalteredParsing);
79}
80
81void SVGAnimatedPathAnimator::animValWillChange(const SVGElementAnimatedPropertyList& animatedTypes)
82{
83    animValWillChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
84}
85
86void SVGAnimatedPathAnimator::animValDidChange(const SVGElementAnimatedPropertyList& animatedTypes)
87{
88    animValDidChangeForType<SVGAnimatedPathSegListPropertyTearOff>(animatedTypes);
89}
90
91void SVGAnimatedPathAnimator::addAnimatedTypes(SVGAnimatedType* from, SVGAnimatedType* to)
92{
93    ASSERT(from->type() == AnimatedPath);
94    ASSERT(from->type() == to->type());
95
96    SVGPathByteStream* fromPath = from->path();
97    SVGPathByteStream* toPath = to->path();
98    unsigned fromPathSize = fromPath->size();
99    if (!fromPathSize || fromPathSize != toPath->size())
100        return;
101    addToSVGPathByteStream(toPath, fromPath);
102}
103
104void SVGAnimatedPathAnimator::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGAnimatedType* from, SVGAnimatedType* to, SVGAnimatedType* toAtEndOfDuration, SVGAnimatedType* animated)
105{
106    ASSERT(m_animationElement);
107    ASSERT(m_contextElement);
108
109    SVGPathByteStream* fromPath = from->path();
110    SVGPathByteStream* toPath = to->path();
111    SVGPathByteStream* toAtEndOfDurationPath = toAtEndOfDuration->path();
112    SVGPathByteStream* animatedPath = animated->path();
113
114    OwnPtr<SVGPathByteStream> underlyingPath;
115    bool isToAnimation = m_animationElement->animationMode() == ToAnimation;
116    if (isToAnimation) {
117        underlyingPath = animatedPath->copy();
118        fromPath = underlyingPath.get();
119    }
120
121    // Cache the current animated value before the buildAnimatedSVGPathByteStream() clears animatedPath.
122    OwnPtr<SVGPathByteStream> lastAnimatedPath;
123    if (!fromPath->size() || (m_animationElement->isAdditive() && !isToAnimation))
124        lastAnimatedPath = animatedPath->copy();
125
126    // Pass false to 'resizeAnimatedListIfNeeded' here, as the path animation is not a regular Vector<SVGXXX> type, but a SVGPathByteStream, that works differently.
127    if (!m_animationElement->adjustFromToListValues<SVGPathByteStream>(*fromPath, *toPath, *animatedPath, percentage, false))
128        return;
129
130    buildAnimatedSVGPathByteStream(fromPath, toPath, animatedPath, percentage);
131
132    // Handle additive='sum'.
133    if (lastAnimatedPath)
134        addToSVGPathByteStream(animatedPath, lastAnimatedPath.get());
135
136    // Handle accumulate='sum'.
137    if (m_animationElement->isAccumulated() && repeatCount)
138        addToSVGPathByteStream(animatedPath, toAtEndOfDurationPath, repeatCount);
139}
140
141float SVGAnimatedPathAnimator::calculateDistance(const String&, const String&)
142{
143    // FIXME: Support paced animations.
144    return -1;
145}
146
147}
148