2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
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.
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.
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.
24 #include "SVGPathElement.h"
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"
47 // Animated property definitions
48 DEFINE_ANIMATED_NUMBER(SVGPathElement, SVGNames::pathLengthAttr, PathLength, pathLength)
50 inline SVGPathElement::SVGPathElement(const QualifiedName& tagName, Document* document)
51 : SVGStyledTransformableElement(tagName, document)
52 , m_pathByteStream(SVGPathByteStream::create())
53 , m_pathSegList(PathSegUnalteredRole)
57 PassRefPtr<SVGPathElement> SVGPathElement::create(const QualifiedName& tagName, Document* document)
59 return adoptRef(new SVGPathElement(tagName, document));
62 float SVGPathElement::getTotalLength()
64 // FIXME: this may wish to use the pathSegList instead of the pathdata if that's cheaper to build (or cached)
70 FloatPoint SVGPathElement::getPointAtLength(float length)
72 // FIXME: this may wish to use the pathSegList instead of the pathdata if that's cheaper to build (or cached)
76 return path.pointAtLength(length, ok);
79 unsigned long SVGPathElement::getPathSegAtLength(float length)
81 SVGPathParserFactory* factory = SVGPathParserFactory::self();
82 unsigned long pathSeg = 0;
83 factory->getSVGPathSegAtLengthFromSVGPathByteStream(m_pathByteStream.get(), length, pathSeg);
87 PassRefPtr<SVGPathSegClosePath> SVGPathElement::createSVGPathSegClosePath(SVGPathSegRole role)
89 return SVGPathSegClosePath::create(this, role);
92 PassRefPtr<SVGPathSegMovetoAbs> SVGPathElement::createSVGPathSegMovetoAbs(float x, float y, SVGPathSegRole role)
94 return SVGPathSegMovetoAbs::create(this, role, x, y);
97 PassRefPtr<SVGPathSegMovetoRel> SVGPathElement::createSVGPathSegMovetoRel(float x, float y, SVGPathSegRole role)
99 return SVGPathSegMovetoRel::create(this, role, x, y);
102 PassRefPtr<SVGPathSegLinetoAbs> SVGPathElement::createSVGPathSegLinetoAbs(float x, float y, SVGPathSegRole role)
104 return SVGPathSegLinetoAbs::create(this, role, x, y);
107 PassRefPtr<SVGPathSegLinetoRel> SVGPathElement::createSVGPathSegLinetoRel(float x, float y, SVGPathSegRole role)
109 return SVGPathSegLinetoRel::create(this, role, x, y);
112 PassRefPtr<SVGPathSegCurvetoCubicAbs> SVGPathElement::createSVGPathSegCurvetoCubicAbs(float x, float y, float x1, float y1, float x2, float y2, SVGPathSegRole role)
114 return SVGPathSegCurvetoCubicAbs::create(this, role, x, y, x1, y1, x2, y2);
117 PassRefPtr<SVGPathSegCurvetoCubicRel> SVGPathElement::createSVGPathSegCurvetoCubicRel(float x, float y, float x1, float y1, float x2, float y2, SVGPathSegRole role)
119 return SVGPathSegCurvetoCubicRel::create(this, role, x, y, x1, y1, x2, y2);
122 PassRefPtr<SVGPathSegCurvetoQuadraticAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticAbs(float x, float y, float x1, float y1, SVGPathSegRole role)
124 return SVGPathSegCurvetoQuadraticAbs::create(this, role, x, y, x1, y1);
127 PassRefPtr<SVGPathSegCurvetoQuadraticRel> SVGPathElement::createSVGPathSegCurvetoQuadraticRel(float x, float y, float x1, float y1, SVGPathSegRole role)
129 return SVGPathSegCurvetoQuadraticRel::create(this, role, x, y, x1, y1);
132 PassRefPtr<SVGPathSegArcAbs> SVGPathElement::createSVGPathSegArcAbs(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, SVGPathSegRole role)
134 return SVGPathSegArcAbs::create(this, role, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
137 PassRefPtr<SVGPathSegArcRel> SVGPathElement::createSVGPathSegArcRel(float x, float y, float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, SVGPathSegRole role)
139 return SVGPathSegArcRel::create(this, role, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
142 PassRefPtr<SVGPathSegLinetoHorizontalAbs> SVGPathElement::createSVGPathSegLinetoHorizontalAbs(float x, SVGPathSegRole role)
144 return SVGPathSegLinetoHorizontalAbs::create(this, role, x);
147 PassRefPtr<SVGPathSegLinetoHorizontalRel> SVGPathElement::createSVGPathSegLinetoHorizontalRel(float x, SVGPathSegRole role)
149 return SVGPathSegLinetoHorizontalRel::create(this, role, x);
152 PassRefPtr<SVGPathSegLinetoVerticalAbs> SVGPathElement::createSVGPathSegLinetoVerticalAbs(float y, SVGPathSegRole role)
154 return SVGPathSegLinetoVerticalAbs::create(this, role, y);
157 PassRefPtr<SVGPathSegLinetoVerticalRel> SVGPathElement::createSVGPathSegLinetoVerticalRel(float y, SVGPathSegRole role)
159 return SVGPathSegLinetoVerticalRel::create(this, role, y);
162 PassRefPtr<SVGPathSegCurvetoCubicSmoothAbs> SVGPathElement::createSVGPathSegCurvetoCubicSmoothAbs(float x, float y, float x2, float y2, SVGPathSegRole role)
164 return SVGPathSegCurvetoCubicSmoothAbs::create(this, role, x, y, x2, y2);
167 PassRefPtr<SVGPathSegCurvetoCubicSmoothRel> SVGPathElement::createSVGPathSegCurvetoCubicSmoothRel(float x, float y, float x2, float y2, SVGPathSegRole role)
169 return SVGPathSegCurvetoCubicSmoothRel::create(this, role, x, y, x2, y2);
172 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothAbs> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothAbs(float x, float y, SVGPathSegRole role)
174 return SVGPathSegCurvetoQuadraticSmoothAbs::create(this, role, x, y);
177 PassRefPtr<SVGPathSegCurvetoQuadraticSmoothRel> SVGPathElement::createSVGPathSegCurvetoQuadraticSmoothRel(float x, float y, SVGPathSegRole role)
179 return SVGPathSegCurvetoQuadraticSmoothRel::create(this, role, x, y);
182 void SVGPathElement::parseMappedAttribute(Attribute* attr)
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");
193 if (SVGTests::parseMappedAttribute(attr))
195 if (SVGLangSpace::parseMappedAttribute(attr))
197 if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
199 SVGStyledTransformableElement::parseMappedAttribute(attr);
203 void SVGPathElement::svgAttributeChanged(const QualifiedName& attrName)
205 SVGStyledTransformableElement::svgAttributeChanged(attrName);
207 if (SVGTests::handleAttributeChange(this, attrName))
210 RenderSVGPath* renderer = static_cast<RenderSVGPath*>(this->renderer());
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;
223 renderer->setNeedsPathUpdate();
224 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
231 if (SVGStyledTransformableElement::isKnownAttribute(attrName)) {
232 renderer->setNeedsTransformUpdate();
233 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
237 if (attrName == SVGNames::pathLengthAttr
238 || SVGLangSpace::isKnownAttribute(attrName)
239 || SVGExternalResourcesRequired::isKnownAttribute(attrName))
240 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
243 void SVGPathElement::synchronizeProperty(const QualifiedName& attrName)
245 SVGStyledTransformableElement::synchronizeProperty(attrName);
247 if (attrName == anyQName()) {
249 synchronizePathLength();
250 synchronizeExternalResourcesRequired();
251 SVGTests::synchronizeProperties(this, attrName);
255 if (attrName == SVGNames::dAttr)
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);
265 void SVGPathElement::synchronizeD()
267 if (!m_pathSegList.shouldSynchronize)
270 SVGAnimatedPropertySynchronizer<true>::synchronize(this, SVGNames::dAttr, m_pathSegList.value.valueAsString());
273 SVGPathSegListPropertyTearOff* SVGPathElement::pathSegList()
275 if (!m_animatablePathSegList) {
276 m_pathSegList.shouldSynchronize = true;
278 SVGPathParserFactory* factory = SVGPathParserFactory::self();
279 factory->buildSVGPathSegListFromByteStream(m_pathByteStream.get(), this, m_pathSegList.value, UnalteredParsing);
281 m_animatablePathSegList = SVGAnimatedProperty::lookupOrCreateWrapper<SVGAnimatedPathSegListPropertyTearOff, SVGPathSegList>
282 (this, SVGNames::dAttr, SVGNames::dAttr.localName(), m_pathSegList.value);
285 return static_cast<SVGPathSegListPropertyTearOff*>(m_animatablePathSegList->baseVal(PathSegUnalteredRole));
288 SVGPathSegListPropertyTearOff* SVGPathElement::normalizedPathSegList()
290 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
294 SVGPathSegListPropertyTearOff* SVGPathElement::animatedPathSegList()
296 if (!m_animatablePathSegList) {
297 m_pathSegList.shouldSynchronize = true;
299 SVGPathParserFactory* factory = SVGPathParserFactory::self();
300 factory->buildSVGPathSegListFromByteStream(m_pathByteStream.get(), this, m_pathSegList.value, UnalteredParsing);
302 m_animatablePathSegList = SVGAnimatedProperty::lookupOrCreateWrapper<SVGAnimatedPathSegListPropertyTearOff, SVGPathSegList>
303 (this, SVGNames::dAttr, SVGNames::dAttr.localName(), m_pathSegList.value);
306 return static_cast<SVGPathSegListPropertyTearOff*>(m_animatablePathSegList->animVal(PathSegUnalteredRole));
309 SVGPathSegListPropertyTearOff* SVGPathElement::animatedNormalizedPathSegList()
311 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
315 void SVGPathElement::toPathData(Path& path) const
317 ASSERT(path.isEmpty());
319 SVGPathParserFactory* factory = SVGPathParserFactory::self();
320 factory->buildPathFromByteStream(m_pathByteStream.get(), path);
323 void SVGPathElement::pathSegListChanged(SVGPathSegRole role)
325 SVGPathParserFactory* factory = SVGPathParserFactory::self();
328 case PathSegNormalizedRole:
329 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=15412 - Implement normalized path segment lists!
331 case PathSegUnalteredRole:
332 m_pathByteStream->clear();
333 factory->buildSVGPathByteStreamFromSVGPathSegList(m_pathSegList.value, m_pathByteStream, UnalteredParsing);
335 case PathSegUndefinedRole:
339 invalidateSVGAttributes();
341 RenderSVGPath* renderer = static_cast<RenderSVGPath*>(this->renderer());
345 renderer->setNeedsPathUpdate();
346 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
351 #endif // ENABLE(SVG)