1 /*
2     Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3                   2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 
5     This file is part of the KDE project
6 
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11 
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16 
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20     Boston, MA 02110-1301, USA.
21 */
22 
23 #include "wtf/Platform.h"
24 
25 #if ENABLE(SVG)
26 #include "SVGPathElement.h"
27 
28 #include "RenderPath.h"
29 #include "SVGNames.h"
30 #include "SVGParserUtilities.h"
31 #include "SVGPathSegArc.h"
32 #include "SVGPathSegClosePath.h"
33 #include "SVGPathSegCurvetoCubic.h"
34 #include "SVGPathSegCurvetoCubicSmooth.h"
35 #include "SVGPathSegCurvetoQuadratic.h"
36 #include "SVGPathSegCurvetoQuadraticSmooth.h"
37 #include "SVGPathSegLineto.h"
38 #include "SVGPathSegLinetoHorizontal.h"
39 #include "SVGPathSegLinetoVertical.h"
40 #include "SVGPathSegList.h"
41 #include "SVGPathSegMoveto.h"
42 #include "SVGSVGElement.h"
43 
44 namespace WebCore
45 {
46 
SVGPathElement(const QualifiedName & tagName,Document * doc)47 SVGPathElement::SVGPathElement(const QualifiedName &tagName, Document *doc)
48     : SVGStyledTransformableElement(tagName, doc)
49     , SVGTests()
50     , SVGLangSpace()
51     , SVGExternalResourcesRequired()
52     , m_pathLength(0.0f)
53 {
54 }
55 
~SVGPathElement()56 SVGPathElement::~SVGPathElement()
57 {
58 }
59 
ANIMATED_PROPERTY_DEFINITIONS(SVGPathElement,float,Number,number,PathLength,pathLength,SVGNames::pathLengthAttr,m_pathLength)60 ANIMATED_PROPERTY_DEFINITIONS(SVGPathElement, float, Number, number, PathLength, pathLength, SVGNames::pathLengthAttr, m_pathLength)
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     return toPathData().length();
66 }
67 
getPointAtLength(float length)68 FloatPoint SVGPathElement::getPointAtLength(float length)
69 {
70     // FIXME: this may wish to use the pathSegList instead of the pathdata if that's cheaper to build (or cached)
71     bool ok = false;
72     return toPathData().pointAtLength(length, ok);
73 }
74 
getPathSegAtLength(float length)75 unsigned long SVGPathElement::getPathSegAtLength(float length)
76 {
77     return pathSegList()->getPathSegAtLength(length);
78 }
79 
createSVGPathSegClosePath()80 PassRefPtr<SVGPathSegClosePath> SVGPathElement::createSVGPathSegClosePath()
81 {
82     return SVGPathSegClosePath::create();
83 }
84 
createSVGPathSegMovetoAbs(float x,float y)85 PassRefPtr<SVGPathSegMovetoAbs> SVGPathElement::createSVGPathSegMovetoAbs(float x, float y)
86 {
87     return SVGPathSegMovetoAbs::create(x, y);
88 }
89 
createSVGPathSegMovetoRel(float x,float y)90 PassRefPtr<SVGPathSegMovetoRel> SVGPathElement::createSVGPathSegMovetoRel(float x, float y)
91 {
92     return SVGPathSegMovetoRel::create(x, y);
93 }
94 
createSVGPathSegLinetoAbs(float x,float y)95 PassRefPtr<SVGPathSegLinetoAbs> SVGPathElement::createSVGPathSegLinetoAbs(float x, float y)
96 {
97     return SVGPathSegLinetoAbs::create(x, y);
98 }
99 
createSVGPathSegLinetoRel(float x,float y)100 PassRefPtr<SVGPathSegLinetoRel> SVGPathElement::createSVGPathSegLinetoRel(float x, float y)
101 {
102     return SVGPathSegLinetoRel::create(x, y);
103 }
104 
createSVGPathSegCurvetoCubicAbs(float x,float y,float x1,float y1,float x2,float y2)105 PassRefPtr<SVGPathSegCurvetoCubicAbs> SVGPathElement::createSVGPathSegCurvetoCubicAbs(float x, float y, float x1, float y1, float x2, float y2)
106 {
107     return SVGPathSegCurvetoCubicAbs::create(x, y, x1, y1, x2, y2);
108 }
109 
createSVGPathSegCurvetoCubicRel(float x,float y,float x1,float y1,float x2,float y2)110 PassRefPtr<SVGPathSegCurvetoCubicRel> SVGPathElement::createSVGPathSegCurvetoCubicRel(float x, float y, float x1, float y1, float x2, float y2)
111 {
112     return SVGPathSegCurvetoCubicRel::create(x, y, x1, y1, x2, y2);
113 }
114 
createSVGPathSegCurvetoQuadraticAbs(float x,float y,float x1,float y1)115 PassRefPtr<SVGPathSegCurvetoQuadraticAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticAbs(float x, float y, float x1, float y1)
116 {
117     return SVGPathSegCurvetoQuadraticAbs::create(x, y, x1, y1);
118 }
119 
createSVGPathSegCurvetoQuadraticRel(float x,float y,float x1,float y1)120 PassRefPtr<SVGPathSegCurvetoQuadraticRel> SVGPathElement::createSVGPathSegCurvetoQuadraticRel(float x, float y, float x1, float y1)
121 {
122     return SVGPathSegCurvetoQuadraticRel::create(x, y, x1, y1);
123 }
124 
createSVGPathSegArcAbs(float x,float y,float r1,float r2,float angle,bool largeArcFlag,bool sweepFlag)125 PassRefPtr<SVGPathSegArcAbs> SVGPathElement::createSVGPathSegArcAbs(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag)
126 {
127     return SVGPathSegArcAbs::create(x, y, r1, r2, angle, largeArcFlag, sweepFlag);
128 }
129 
createSVGPathSegArcRel(float x,float y,float r1,float r2,float angle,bool largeArcFlag,bool sweepFlag)130 PassRefPtr<SVGPathSegArcRel> SVGPathElement::createSVGPathSegArcRel(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag)
131 {
132     return SVGPathSegArcRel::create(x, y, r1, r2, angle, largeArcFlag, sweepFlag);
133 }
134 
createSVGPathSegLinetoHorizontalAbs(float x)135 PassRefPtr<SVGPathSegLinetoHorizontalAbs> SVGPathElement::createSVGPathSegLinetoHorizontalAbs(float x)
136 {
137     return SVGPathSegLinetoHorizontalAbs::create(x);
138 }
139 
createSVGPathSegLinetoHorizontalRel(float x)140 PassRefPtr<SVGPathSegLinetoHorizontalRel> SVGPathElement::createSVGPathSegLinetoHorizontalRel(float x)
141 {
142     return SVGPathSegLinetoHorizontalRel::create(x);
143 }
144 
createSVGPathSegLinetoVerticalAbs(float y)145 PassRefPtr<SVGPathSegLinetoVerticalAbs> SVGPathElement::createSVGPathSegLinetoVerticalAbs(float y)
146 {
147     return SVGPathSegLinetoVerticalAbs::create(y);
148 }
149 
createSVGPathSegLinetoVerticalRel(float y)150 PassRefPtr<SVGPathSegLinetoVerticalRel> SVGPathElement::createSVGPathSegLinetoVerticalRel(float y)
151 {
152     return SVGPathSegLinetoVerticalRel::create(y);
153 }
154 
createSVGPathSegCurvetoCubicSmoothAbs(float x,float y,float x2,float y2)155 PassRefPtr<SVGPathSegCurvetoCubicSmoothAbs> SVGPathElement::createSVGPathSegCurvetoCubicSmoothAbs(float x, float y, float x2, float y2)
156 {
157     return SVGPathSegCurvetoCubicSmoothAbs::create(x, y, x2, y2);
158 }
159 
createSVGPathSegCurvetoCubicSmoothRel(float x,float y,float x2,float y2)160 PassRefPtr<SVGPathSegCurvetoCubicSmoothRel> SVGPathElement::createSVGPathSegCurvetoCubicSmoothRel(float x, float y, float x2, float y2)
161 {
162     return SVGPathSegCurvetoCubicSmoothRel::create(x, y, x2, y2);
163 }
164 
createSVGPathSegCurvetoQuadraticSmoothAbs(float x,float y)165 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothAbs(float x, float y)
166 {
167     return SVGPathSegCurvetoQuadraticSmoothAbs::create(x, y);
168 }
169 
createSVGPathSegCurvetoQuadraticSmoothRel(float x,float y)170 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothRel> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothRel(float x, float y)
171 {
172     return SVGPathSegCurvetoQuadraticSmoothRel::create(x, y);
173 }
174 
parseMappedAttribute(MappedAttribute * attr)175 void SVGPathElement::parseMappedAttribute(MappedAttribute *attr)
176 {
177     if (attr->name() == SVGNames::dAttr) {
178         ExceptionCode ec;
179         pathSegList()->clear(ec);
180         pathSegListFromSVGData(pathSegList(), attr->value(), true);
181         /*FIXME khtml if (!pathSegListFromSVGData(pathSegList(), attr->value(), true))
182             document()->accessSVGExtensions()->reportError("Problem parsing d=\"" + attr->value() + "\"");*/
183     } else if (attr->name() == SVGNames::pathLengthAttr) {
184         m_pathLength = attr->value().toFloat();
185         if (m_pathLength < 0.0f) {
186             document()->accessSVGExtensions()->reportError("A negative value for path attribute <pathLength> is not allowed");
187         }
188     } else {
189         if (SVGTests::parseMappedAttribute(attr)) {
190             return;
191         }
192         if (SVGLangSpace::parseMappedAttribute(attr)) {
193             return;
194         }
195         if (SVGExternalResourcesRequired::parseMappedAttribute(attr)) {
196             return;
197         }
198         SVGStyledTransformableElement::parseMappedAttribute(attr);
199     }
200 }
201 
svgAttributeChanged(const QualifiedName & attrName)202 void SVGPathElement::svgAttributeChanged(const QualifiedName &attrName)
203 {
204     SVGStyledTransformableElement::svgAttributeChanged(attrName);
205 
206     if (!renderer()) {
207         return;
208     }
209 
210     if (attrName == SVGNames::dAttr || attrName == SVGNames::pathLengthAttr ||
211             SVGTests::isKnownAttribute(attrName) ||
212             SVGLangSpace::isKnownAttribute(attrName) ||
213             SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
214             SVGStyledTransformableElement::isKnownAttribute(attrName)) {
215         renderer()->setNeedsLayout(true);
216     }
217 }
218 
pathSegList() const219 SVGPathSegList *SVGPathElement::pathSegList() const
220 {
221     if (!m_pathSegList) {
222         m_pathSegList = SVGPathSegList::create(SVGNames::dAttr);
223     }
224 
225     return m_pathSegList.get();
226 }
227 
normalizedPathSegList() const228 SVGPathSegList *SVGPathElement::normalizedPathSegList() const
229 {
230     // TODO
231     return nullptr;
232 }
233 
animatedPathSegList() const234 SVGPathSegList *SVGPathElement::animatedPathSegList() const
235 {
236     // TODO
237     return nullptr;
238 }
239 
animatedNormalizedPathSegList() const240 SVGPathSegList *SVGPathElement::animatedNormalizedPathSegList() const
241 {
242     // TODO
243     return nullptr;
244 }
245 
toPathData() const246 Path SVGPathElement::toPathData() const
247 {
248     return pathSegList()->toPathData();
249 }
250 
251 // KHTML ElementImpl pure virtual method
id() const252 quint32 SVGPathElement::id() const
253 {
254     return SVGNames::pathTag.id();
255 }
256 
257 }
258 
259 #endif // ENABLE(SVG)
260