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