OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / svg / SVGPathElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #if ENABLE(SVG)
24 #include "SVGPathElement.h"
25
26 #include "Attribute.h"
27 #include "RenderSVGPath.h"
28 #include "RenderSVGResource.h"
29 #include "SVGPathParserFactory.h"
30 #include "SVGPathSegArc.h"
31 #include "SVGPathSegClosePath.h"
32 #include "SVGPathSegCurvetoCubic.h"
33 #include "SVGPathSegCurvetoCubicSmooth.h"
34 #include "SVGPathSegCurvetoQuadratic.h"
35 #include "SVGPathSegCurvetoQuadraticSmooth.h"
36 #include "SVGPathSegLineto.h"
37 #include "SVGPathSegLinetoHorizontal.h"
38 #include "SVGPathSegLinetoVertical.h"
39 #include "SVGPathSegList.h"
40 #include "SVGPathSegListPropertyTearOff.h"
41 #include "SVGPathSegListBuilder.h"
42 #include "SVGPathSegMoveto.h"
43 #include "SVGSVGElement.h"
44
45 namespace WebCore {
46
47 // Animated property definitions
48 DEFINE_ANIMATED_NUMBER(SVGPathElement, SVGNames::pathLengthAttr, PathLength, pathLength)
49
50 inline SVGPathElement::SVGPathElement(const QualifiedName& tagName, Document* document)
51     : SVGStyledTransformableElement(tagName, document)
52     , m_pathByteStream(SVGPathByteStream::create())
53     , m_pathSegList(PathSegUnalteredRole)
54 {
55 }
56
57 PassRefPtr<SVGPathElement> SVGPathElement::create(const QualifiedName& tagName, Document* document)
58 {
59     return adoptRef(new SVGPathElement(tagName, document));
60 }
61
62 float SVGPathElement::getTotalLength()
63 {
64     // FIXME: this may wish to use the pathSegList instead of the pathdata if that's cheaper to build (or cached)
65     Path path;
66     toPathData(path);
67     return path.length();
68 }
69
70 FloatPoint SVGPathElement::getPointAtLength(float length)
71 {
72     // FIXME: this may wish to use the pathSegList instead of the pathdata if that's cheaper to build (or cached)
73     bool ok = false;
74     Path path;
75     toPathData(path);
76     return path.pointAtLength(length, ok);
77 }
78
79 unsigned long SVGPathElement::getPathSegAtLength(float length)
80 {
81     SVGPathParserFactory* factory = SVGPathParserFactory::self();
82     unsigned long pathSeg = 0;
83     factory->getSVGPathSegAtLengthFromSVGPathByteStream(m_pathByteStream.get(), length, pathSeg);
84     return pathSeg;
85 }
86
87 PassRefPtr<SVGPathSegClosePath> SVGPathElement::createSVGPathSegClosePath(SVGPathSegRole role)
88 {
89     return SVGPathSegClosePath::create(this, role);
90 }
91
92 PassRefPtr<SVGPathSegMovetoAbs> SVGPathElement::createSVGPathSegMovetoAbs(float x, float y, SVGPathSegRole role)
93 {
94     return SVGPathSegMovetoAbs::create(this, role, x, y);
95 }
96
97 PassRefPtr<SVGPathSegMovetoRel> SVGPathElement::createSVGPathSegMovetoRel(float x, float y, SVGPathSegRole role)
98 {
99     return SVGPathSegMovetoRel::create(this, role, x, y);
100 }
101
102 PassRefPtr<SVGPathSegLinetoAbs> SVGPathElement::createSVGPathSegLinetoAbs(float x, float y, SVGPathSegRole role)
103 {
104     return SVGPathSegLinetoAbs::create(this, role, x, y);
105 }
106
107 PassRefPtr<SVGPathSegLinetoRel> SVGPathElement::createSVGPathSegLinetoRel(float x, float y, SVGPathSegRole role)
108 {
109     return SVGPathSegLinetoRel::create(this, role, x, y);
110 }
111
112 PassRefPtr<SVGPathSegCurvetoCubicAbs> SVGPathElement::createSVGPathSegCurvetoCubicAbs(float x, float y, float x1, float y1, float x2, float y2, SVGPathSegRole role)
113 {
114     return SVGPathSegCurvetoCubicAbs::create(this, role, x, y, x1, y1, x2, y2);
115 }
116
117 PassRefPtr<SVGPathSegCurvetoCubicRel> SVGPathElement::createSVGPathSegCurvetoCubicRel(float x, float y, float x1, float y1, float x2, float y2, SVGPathSegRole role)
118 {
119     return SVGPathSegCurvetoCubicRel::create(this, role, x, y, x1, y1, x2, y2);
120 }
121
122 PassRefPtr<SVGPathSegCurvetoQuadraticAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticAbs(float x, float y, float x1, float y1, SVGPathSegRole role)
123 {
124     return SVGPathSegCurvetoQuadraticAbs::create(this, role, x, y, x1, y1);
125 }
126
127 PassRefPtr<SVGPathSegCurvetoQuadraticRel> SVGPathElement::createSVGPathSegCurvetoQuadraticRel(float x, float y, float x1, float y1, SVGPathSegRole role)
128 {
129     return SVGPathSegCurvetoQuadraticRel::create(this, role, x, y, x1, y1);
130 }
131
132 PassRefPtr<SVGPathSegArcAbs> SVGPathElement::createSVGPathSegArcAbs(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, SVGPathSegRole role)
133 {
134     return SVGPathSegArcAbs::create(this, role, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
135 }
136
137 PassRefPtr<SVGPathSegArcRel> SVGPathElement::createSVGPathSegArcRel(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, SVGPathSegRole role)
138 {
139     return SVGPathSegArcRel::create(this, role, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
140 }
141
142 PassRefPtr<SVGPathSegLinetoHorizontalAbs> SVGPathElement::createSVGPathSegLinetoHorizontalAbs(float x, SVGPathSegRole role)
143 {
144     return SVGPathSegLinetoHorizontalAbs::create(this, role, x);
145 }
146
147 PassRefPtr<SVGPathSegLinetoHorizontalRel> SVGPathElement::createSVGPathSegLinetoHorizontalRel(float x, SVGPathSegRole role)
148 {
149     return SVGPathSegLinetoHorizontalRel::create(this, role, x);
150 }
151
152 PassRefPtr<SVGPathSegLinetoVerticalAbs> SVGPathElement::createSVGPathSegLinetoVerticalAbs(float y, SVGPathSegRole role)
153 {
154     return SVGPathSegLinetoVerticalAbs::create(this, role, y);
155 }
156
157 PassRefPtr<SVGPathSegLinetoVerticalRel> SVGPathElement::createSVGPathSegLinetoVerticalRel(float y, SVGPathSegRole role)
158 {
159     return SVGPathSegLinetoVerticalRel::create(this, role, y);
160 }
161
162 PassRefPtr<SVGPathSegCurvetoCubicSmoothAbs> SVGPathElement::createSVGPathSegCurvetoCubicSmoothAbs(float x, float y, float x2, float y2, SVGPathSegRole role)
163 {
164     return SVGPathSegCurvetoCubicSmoothAbs::create(this, role, x, y, x2, y2);
165 }
166
167 PassRefPtr<SVGPathSegCurvetoCubicSmoothRel> SVGPathElement::createSVGPathSegCurvetoCubicSmoothRel(float x, float y, float x2, float y2, SVGPathSegRole role)
168 {
169     return SVGPathSegCurvetoCubicSmoothRel::create(this, role, x, y, x2, y2);
170 }
171
172 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothAbs(float x, float y, SVGPathSegRole role)
173 {
174     return SVGPathSegCurvetoQuadraticSmoothAbs::create(this, role, x, y);
175 }
176
177 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothRel> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothRel(float x, float y, SVGPathSegRole role)
178 {
179     return SVGPathSegCurvetoQuadraticSmoothRel::create(this, role, x, y);
180 }
181
182 void SVGPathElement::parseMappedAttribute(Attribute* attr)
183 {
184     if (attr->name() == SVGNames::dAttr) {
185         SVGPathParserFactory* factory = SVGPathParserFactory::self();
186         if (!factory->buildSVGPathByteStreamFromString(attr->value(), m_pathByteStream, UnalteredParsing))
187             document()->accessSVGExtensions()->reportError("Problem parsing d=\"" + attr->value() + "\"");
188     } else if (attr->name() == SVGNames::pathLengthAttr) {
189         setPathLengthBaseValue(attr->value().toFloat());
190         if (pathLengthBaseValue() < 0.0f)
191             document()->accessSVGExtensions()->reportError("A negative value for path attribute <pathLength> is not allowed");
192     } else {
193         if (SVGTests::parseMappedAttribute(attr))
194             return;
195         if (SVGLangSpace::parseMappedAttribute(attr))
196             return;
197         if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
198             return;
199         SVGStyledTransformableElement::parseMappedAttribute(attr);
200     }
201 }
202
203 void SVGPathElement::svgAttributeChanged(const QualifiedName& attrName)
204 {
205     SVGStyledTransformableElement::svgAttributeChanged(attrName);
206
207     if (SVGTests::handleAttributeChange(this, attrName))
208         return;
209
210     RenderSVGPath* renderer = static_cast<RenderSVGPath*>(this->renderer());
211
212     if (attrName == SVGNames::dAttr) {
213         if (m_animatablePathSegList) {
214             SVGPathSegList newList(PathSegUnalteredRole);
215             SVGPathParserFactory* factory = SVGPathParserFactory::self();
216             factory->buildSVGPathSegListFromByteStream(m_pathByteStream.get(), this, newList, UnalteredParsing);
217             m_pathSegList.value = newList;
218         }
219
220         if (!renderer)
221             return;
222
223         renderer->setNeedsPathUpdate();
224         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
225         return;
226     }
227
228     if (!renderer)
229         return;
230
231     if (SVGStyledTransformableElement::isKnownAttribute(attrName)) {
232         renderer->setNeedsTransformUpdate();
233         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
234         return;
235     }
236
237     if (attrName == SVGNames::pathLengthAttr
238         || SVGLangSpace::isKnownAttribute(attrName)
239         || SVGExternalResourcesRequired::isKnownAttribute(attrName))
240         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
241 }
242
243 void SVGPathElement::synchronizeProperty(const QualifiedName& attrName)
244 {
245     SVGStyledTransformableElement::synchronizeProperty(attrName);
246
247     if (attrName == anyQName()) {
248         synchronizeD();
249         synchronizePathLength();
250         synchronizeExternalResourcesRequired();
251         SVGTests::synchronizeProperties(this, attrName);
252         return;
253     }
254
255     if (attrName == SVGNames::dAttr)
256         synchronizeD();
257     else if (attrName == SVGNames::pathLengthAttr)
258         synchronizePathLength();
259     else if (SVGExternalResourcesRequired::isKnownAttribute(attrName))
260         synchronizeExternalResourcesRequired();
261     else if (SVGTests::isKnownAttribute(attrName))
262         SVGTests::synchronizeProperties(this, attrName);
263 }
264
265 void SVGPathElement::synchronizeD()
266 {
267     if (!m_pathSegList.shouldSynchronize)
268         return;
269
270     SVGAnimatedPropertySynchronizer<true>::synchronize(this, SVGNames::dAttr, m_pathSegList.value.valueAsString());
271 }
272
273 SVGPathSegListPropertyTearOff* SVGPathElement::pathSegList()
274 {
275     if (!m_animatablePathSegList) {
276         m_pathSegList.shouldSynchronize = true;
277
278         SVGPathParserFactory* factory = SVGPathParserFactory::self();
279         factory->buildSVGPathSegListFromByteStream(m_pathByteStream.get(), this, m_pathSegList.value, UnalteredParsing);
280
281         m_animatablePathSegList = SVGAnimatedProperty::lookupOrCreateWrapper<SVGAnimatedPathSegListPropertyTearOff, SVGPathSegList>
282                                  (this, SVGNames::dAttr, SVGNames::dAttr.localName(), m_pathSegList.value);
283     }
284
285     return static_cast<SVGPathSegListPropertyTearOff*>(m_animatablePathSegList->baseVal(PathSegUnalteredRole));
286 }
287
288 SVGPathSegListPropertyTearOff* SVGPathElement::normalizedPathSegList()
289 {
290     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
291     return 0;
292 }
293
294 SVGPathSegListPropertyTearOff* SVGPathElement::animatedPathSegList()
295 {
296     if (!m_animatablePathSegList) {
297         m_pathSegList.shouldSynchronize = true;
298
299         SVGPathParserFactory* factory = SVGPathParserFactory::self();
300         factory->buildSVGPathSegListFromByteStream(m_pathByteStream.get(), this, m_pathSegList.value, UnalteredParsing);
301
302         m_animatablePathSegList = SVGAnimatedProperty::lookupOrCreateWrapper<SVGAnimatedPathSegListPropertyTearOff, SVGPathSegList>
303                                  (this, SVGNames::dAttr, SVGNames::dAttr.localName(), m_pathSegList.value);
304     }
305
306     return static_cast<SVGPathSegListPropertyTearOff*>(m_animatablePathSegList->animVal(PathSegUnalteredRole));
307 }
308
309 SVGPathSegListPropertyTearOff* SVGPathElement::animatedNormalizedPathSegList()
310 {
311     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
312     return 0;
313 }
314
315 void SVGPathElement::toPathData(Path& path) const
316 {
317     ASSERT(path.isEmpty());
318
319     SVGPathParserFactory* factory = SVGPathParserFactory::self();
320     factory->buildPathFromByteStream(m_pathByteStream.get(), path);
321 }
322
323 void SVGPathElement::pathSegListChanged(SVGPathSegRole role)
324 {
325     SVGPathParserFactory* factory = SVGPathParserFactory::self();
326
327     switch (role) {
328     case PathSegNormalizedRole:
329         // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
330         break;
331     case PathSegUnalteredRole:
332         m_pathByteStream->clear();
333         factory->buildSVGPathByteStreamFromSVGPathSegList(m_pathSegList.value, m_pathByteStream, UnalteredParsing);
334         break;
335     case PathSegUndefinedRole:
336         return;
337     }
338
339     invalidateSVGAttributes();
340
341     RenderSVGPath* renderer = static_cast<RenderSVGPath*>(this->renderer());
342     if (!renderer)
343         return;
344
345     renderer->setNeedsPathUpdate();
346     RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
347 }
348
349 }
350
351 #endif // ENABLE(SVG)