2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2008 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005, 2007 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Google, Inc.
6 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
8 * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com>
9 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org>
10 * Copyright (C) 2011 University of Szeged
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public License
23 * along with this library; see the file COPYING.LIB. If not, write to
24 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
30 #include "core/rendering/svg/RenderSVGPath.h"
32 #include "core/rendering/svg/RenderSVGResourceMarker.h"
33 #include "core/rendering/svg/SVGResources.h"
34 #include "core/rendering/svg/SVGResourcesCache.h"
35 #include "core/rendering/svg/SVGSubpathData.h"
36 #include "core/svg/SVGGraphicsElement.h"
40 RenderSVGPath::RenderSVGPath(SVGGraphicsElement* node)
41 : RenderSVGShape(node)
45 RenderSVGPath::~RenderSVGPath()
49 void RenderSVGPath::updateShapeFromElement()
51 RenderSVGShape::updateShapeFromElement();
52 updateZeroLengthSubpaths();
54 m_strokeBoundingBox = calculateUpdatedStrokeBoundingBox();
57 FloatRect RenderSVGPath::calculateUpdatedStrokeBoundingBox() const
59 FloatRect strokeBoundingBox = m_strokeBoundingBox;
61 if (!m_markerPositions.isEmpty())
62 strokeBoundingBox.unite(markerRect(strokeWidth()));
64 if (style()->svgStyle().hasStroke()) {
65 // FIXME: zero-length subpaths do not respect vector-effect = non-scaling-stroke.
66 float strokeWidth = this->strokeWidth();
67 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i)
68 strokeBoundingBox.unite(zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth));
71 return strokeBoundingBox;
74 bool RenderSVGPath::shapeDependentStrokeContains(const FloatPoint& point)
76 if (RenderSVGShape::shapeDependentStrokeContains(point))
79 const SVGRenderStyle& svgStyle = style()->svgStyle();
80 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) {
81 ASSERT(svgStyle.hasStroke());
82 float strokeWidth = this->strokeWidth();
83 if (svgStyle.capStyle() == SquareCap) {
84 if (zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWidth).contains(point))
87 ASSERT(svgStyle.capStyle() == RoundCap);
88 FloatPoint radiusVector(point.x() - m_zeroLengthLinecapLocations[i].x(), point.y() - m_zeroLengthLinecapLocations[i].y());
89 if (radiusVector.lengthSquared() < strokeWidth * strokeWidth * .25f)
96 bool RenderSVGPath::shouldStrokeZeroLengthSubpath() const
98 // Spec(11.4): Any zero length subpath shall not be stroked if the "stroke-linecap" property has a value of butt
99 // but shall be stroked if the "stroke-linecap" property has a value of round or square
100 return style()->svgStyle().hasStroke() && style()->svgStyle().capStyle() != ButtCap;
103 FloatRect RenderSVGPath::zeroLengthSubpathRect(const FloatPoint& linecapPosition, float strokeWidth)
105 return FloatRect(linecapPosition.x() - strokeWidth / 2, linecapPosition.y() - strokeWidth / 2, strokeWidth, strokeWidth);
108 void RenderSVGPath::updateZeroLengthSubpaths()
110 m_zeroLengthLinecapLocations.clear();
112 if (!strokeWidth() || !shouldStrokeZeroLengthSubpath())
115 SVGSubpathData subpathData(m_zeroLengthLinecapLocations);
116 path().apply(&subpathData, SVGSubpathData::updateFromPathElement);
117 subpathData.pathIsDone();
120 FloatRect RenderSVGPath::markerRect(float strokeWidth) const
122 ASSERT(!m_markerPositions.isEmpty());
124 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
127 RenderSVGResourceMarker* markerStart = resources->markerStart();
128 RenderSVGResourceMarker* markerMid = resources->markerMid();
129 RenderSVGResourceMarker* markerEnd = resources->markerEnd();
130 ASSERT(markerStart || markerMid || markerEnd);
132 FloatRect boundaries;
133 unsigned size = m_markerPositions.size();
134 for (unsigned i = 0; i < size; ++i) {
135 if (RenderSVGResourceMarker* marker = SVGMarkerData::markerForType(m_markerPositions[i].type, markerStart, markerMid, markerEnd))
136 boundaries.unite(marker->markerBoundaries(marker->markerTransformation(m_markerPositions[i].origin, m_markerPositions[i].angle, strokeWidth)));
141 bool RenderSVGPath::shouldGenerateMarkerPositions() const
143 if (!style()->svgStyle().hasMarkers())
146 if (!SVGResources::supportsMarkers(*toSVGGraphicsElement(element())))
149 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
153 return resources->markerStart() || resources->markerMid() || resources->markerEnd();
156 void RenderSVGPath::processMarkerPositions()
158 m_markerPositions.clear();
160 if (!shouldGenerateMarkerPositions())
163 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
166 RenderSVGResourceMarker* markerStart = resources->markerStart();
168 SVGMarkerData markerData(m_markerPositions, markerStart ? markerStart->orientType() == SVGMarkerOrientAutoStartReverse : false);
169 path().apply(&markerData, SVGMarkerData::updateFromPathElement);
170 markerData.pathIsDone();