OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org> | 2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org> |
3 * Copyright (C) 2004, 2005, 2008 Rob Buis <buis@kde.org> | 3 * Copyright (C) 2004, 2005, 2008 Rob Buis <buis@kde.org> |
4 * Copyright (C) 2005, 2007 Eric Seidel <eric@webkit.org> | 4 * Copyright (C) 2005, 2007 Eric Seidel <eric@webkit.org> |
5 * Copyright (C) 2009 Google, Inc. | 5 * Copyright (C) 2009 Google, Inc. |
6 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> | 6 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
7 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | 7 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
8 * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com> | 8 * Copyright (C) 2009 Jeff Schiller <codedread@gmail.com> |
9 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org> | 9 * Copyright (C) 2011 Renata Hodovan <reni@webkit.org> |
10 * Copyright (C) 2011 University of Szeged | 10 * Copyright (C) 2011 University of Szeged |
(...skipping 11 matching lines...) Expand all Loading... |
22 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 24 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 * Boston, MA 02110-1301, USA. | 25 * Boston, MA 02110-1301, USA. |
26 */ | 26 */ |
27 | 27 |
28 #include "config.h" | 28 #include "config.h" |
29 | 29 |
30 #include "core/rendering/svg/RenderSVGPath.h" | 30 #include "core/rendering/svg/RenderSVGPath.h" |
31 | 31 |
| 32 #include "core/rendering/svg/RenderSVGResourceMarker.h" |
| 33 #include "core/rendering/svg/SVGResources.h" |
| 34 #include "core/rendering/svg/SVGResourcesCache.h" |
32 #include "core/rendering/svg/SVGSubpathData.h" | 35 #include "core/rendering/svg/SVGSubpathData.h" |
33 #include "platform/graphics/GraphicsContextStateSaver.h" | 36 #include "core/svg/SVGGraphicsElement.h" |
34 | 37 |
35 namespace blink { | 38 namespace blink { |
36 | 39 |
37 RenderSVGPath::RenderSVGPath(SVGGraphicsElement* node) | 40 RenderSVGPath::RenderSVGPath(SVGGraphicsElement* node) |
38 : RenderSVGShape(node) | 41 : RenderSVGShape(node) |
39 { | 42 { |
40 } | 43 } |
41 | 44 |
42 RenderSVGPath::~RenderSVGPath() | 45 RenderSVGPath::~RenderSVGPath() |
43 { | 46 { |
44 } | 47 } |
45 | 48 |
46 void RenderSVGPath::updateShapeFromElement() | 49 void RenderSVGPath::updateShapeFromElement() |
47 { | 50 { |
48 RenderSVGShape::updateShapeFromElement(); | 51 RenderSVGShape::updateShapeFromElement(); |
49 updateZeroLengthSubpaths(); | 52 updateZeroLengthSubpaths(); |
50 | 53 |
51 m_strokeBoundingBox = calculateUpdatedStrokeBoundingBox(); | 54 m_strokeBoundingBox = calculateUpdatedStrokeBoundingBox(); |
52 } | 55 } |
53 | 56 |
54 FloatRect RenderSVGPath::calculateUpdatedStrokeBoundingBox() const | 57 FloatRect RenderSVGPath::calculateUpdatedStrokeBoundingBox() const |
55 { | 58 { |
56 FloatRect strokeBoundingBox = m_strokeBoundingBox; | 59 FloatRect strokeBoundingBox = m_strokeBoundingBox; |
57 | 60 |
| 61 if (!m_markerPositions.isEmpty()) |
| 62 strokeBoundingBox.unite(markerRect(strokeWidth())); |
| 63 |
58 if (style()->svgStyle().hasStroke()) { | 64 if (style()->svgStyle().hasStroke()) { |
59 // FIXME: zero-length subpaths do not respect vector-effect = non-scalin
g-stroke. | 65 // FIXME: zero-length subpaths do not respect vector-effect = non-scalin
g-stroke. |
60 float strokeWidth = this->strokeWidth(); | 66 float strokeWidth = this->strokeWidth(); |
61 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) | 67 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) |
62 strokeBoundingBox.unite(zeroLengthSubpathRect(m_zeroLengthLinecapLoc
ations[i], strokeWidth)); | 68 strokeBoundingBox.unite(zeroLengthSubpathRect(m_zeroLengthLinecapLoc
ations[i], strokeWidth)); |
63 } | 69 } |
64 | 70 |
65 return strokeBoundingBox; | 71 return strokeBoundingBox; |
66 } | 72 } |
67 | 73 |
68 static void useStrokeStyleToFill(GraphicsContext* context) | |
69 { | |
70 if (Gradient* gradient = context->strokeGradient()) | |
71 context->setFillGradient(gradient); | |
72 else if (Pattern* pattern = context->strokePattern()) | |
73 context->setFillPattern(pattern); | |
74 else | |
75 context->setFillColor(context->strokeColor()); | |
76 } | |
77 | |
78 void RenderSVGPath::strokeShape(GraphicsContext* context) const | |
79 { | |
80 if (!style()->svgStyle().hasVisibleStroke()) | |
81 return; | |
82 | |
83 RenderSVGShape::strokeShape(context); | |
84 | |
85 if (m_zeroLengthLinecapLocations.isEmpty()) | |
86 return; | |
87 | |
88 Path* usePath; | |
89 AffineTransform nonScalingTransform; | |
90 | |
91 if (hasNonScalingStroke()) | |
92 nonScalingTransform = nonScalingStrokeTransform(); | |
93 | |
94 GraphicsContextStateSaver stateSaver(*context, true); | |
95 useStrokeStyleToFill(context); | |
96 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) { | |
97 usePath = zeroLengthLinecapPath(m_zeroLengthLinecapLocations[i]); | |
98 if (hasNonScalingStroke()) | |
99 usePath = nonScalingStrokePath(usePath, nonScalingTransform); | |
100 context->fillPath(*usePath); | |
101 } | |
102 } | |
103 | |
104 bool RenderSVGPath::shapeDependentStrokeContains(const FloatPoint& point) | 74 bool RenderSVGPath::shapeDependentStrokeContains(const FloatPoint& point) |
105 { | 75 { |
106 if (RenderSVGShape::shapeDependentStrokeContains(point)) | 76 if (RenderSVGShape::shapeDependentStrokeContains(point)) |
107 return true; | 77 return true; |
108 | 78 |
109 const SVGRenderStyle& svgStyle = style()->svgStyle(); | 79 const SVGRenderStyle& svgStyle = style()->svgStyle(); |
110 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) { | 80 for (size_t i = 0; i < m_zeroLengthLinecapLocations.size(); ++i) { |
111 ASSERT(svgStyle.hasStroke()); | 81 ASSERT(svgStyle.hasStroke()); |
112 float strokeWidth = this->strokeWidth(); | 82 float strokeWidth = this->strokeWidth(); |
113 if (svgStyle.capStyle() == SquareCap) { | 83 if (svgStyle.capStyle() == SquareCap) { |
114 if (zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWid
th).contains(point)) | 84 if (zeroLengthSubpathRect(m_zeroLengthLinecapLocations[i], strokeWid
th).contains(point)) |
115 return true; | 85 return true; |
116 } else { | 86 } else { |
117 ASSERT(svgStyle.capStyle() == RoundCap); | 87 ASSERT(svgStyle.capStyle() == RoundCap); |
118 FloatPoint radiusVector(point.x() - m_zeroLengthLinecapLocations[i].
x(), point.y() - m_zeroLengthLinecapLocations[i].y()); | 88 FloatPoint radiusVector(point.x() - m_zeroLengthLinecapLocations[i].
x(), point.y() - m_zeroLengthLinecapLocations[i].y()); |
119 if (radiusVector.lengthSquared() < strokeWidth * strokeWidth * .25f) | 89 if (radiusVector.lengthSquared() < strokeWidth * strokeWidth * .25f) |
120 return true; | 90 return true; |
121 } | 91 } |
122 } | 92 } |
123 return false; | 93 return false; |
124 } | 94 } |
125 | 95 |
126 bool RenderSVGPath::shouldStrokeZeroLengthSubpath() const | 96 bool RenderSVGPath::shouldStrokeZeroLengthSubpath() const |
127 { | 97 { |
128 // Spec(11.4): Any zero length subpath shall not be stroked if the "stroke-l
inecap" property has a value of butt | 98 // Spec(11.4): Any zero length subpath shall not be stroked if the "stroke-l
inecap" property has a value of butt |
129 // but shall be stroked if the "stroke-linecap" property has a value of roun
d or square | 99 // but shall be stroked if the "stroke-linecap" property has a value of roun
d or square |
130 return style()->svgStyle().hasStroke() && style()->svgStyle().capStyle() !=
ButtCap; | 100 return style()->svgStyle().hasStroke() && style()->svgStyle().capStyle() !=
ButtCap; |
131 } | 101 } |
132 | 102 |
133 Path* RenderSVGPath::zeroLengthLinecapPath(const FloatPoint& linecapPosition) co
nst | 103 FloatRect RenderSVGPath::zeroLengthSubpathRect(const FloatPoint& linecapPosition
, float strokeWidth) |
134 { | |
135 DEFINE_STATIC_LOCAL(Path, tempPath, ()); | |
136 | |
137 tempPath.clear(); | |
138 if (style()->svgStyle().capStyle() == SquareCap) | |
139 tempPath.addRect(zeroLengthSubpathRect(linecapPosition, this->strokeWidt
h())); | |
140 else | |
141 tempPath.addEllipse(zeroLengthSubpathRect(linecapPosition, this->strokeW
idth())); | |
142 | |
143 return &tempPath; | |
144 } | |
145 | |
146 FloatRect RenderSVGPath::zeroLengthSubpathRect(const FloatPoint& linecapPosition
, float strokeWidth) const | |
147 { | 104 { |
148 return FloatRect(linecapPosition.x() - strokeWidth / 2, linecapPosition.y()
- strokeWidth / 2, strokeWidth, strokeWidth); | 105 return FloatRect(linecapPosition.x() - strokeWidth / 2, linecapPosition.y()
- strokeWidth / 2, strokeWidth, strokeWidth); |
149 } | 106 } |
150 | 107 |
151 void RenderSVGPath::updateZeroLengthSubpaths() | 108 void RenderSVGPath::updateZeroLengthSubpaths() |
152 { | 109 { |
153 m_zeroLengthLinecapLocations.clear(); | 110 m_zeroLengthLinecapLocations.clear(); |
154 | 111 |
155 if (!strokeWidth() || !shouldStrokeZeroLengthSubpath()) | 112 if (!strokeWidth() || !shouldStrokeZeroLengthSubpath()) |
156 return; | 113 return; |
157 | 114 |
158 SVGSubpathData subpathData(m_zeroLengthLinecapLocations); | 115 SVGSubpathData subpathData(m_zeroLengthLinecapLocations); |
159 path().apply(&subpathData, SVGSubpathData::updateFromPathElement); | 116 path().apply(&subpathData, SVGSubpathData::updateFromPathElement); |
160 subpathData.pathIsDone(); | 117 subpathData.pathIsDone(); |
161 } | 118 } |
162 | 119 |
| 120 FloatRect RenderSVGPath::markerRect(float strokeWidth) const |
| 121 { |
| 122 ASSERT(!m_markerPositions.isEmpty()); |
| 123 |
| 124 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(
this); |
| 125 ASSERT(resources); |
| 126 |
| 127 RenderSVGResourceMarker* markerStart = resources->markerStart(); |
| 128 RenderSVGResourceMarker* markerMid = resources->markerMid(); |
| 129 RenderSVGResourceMarker* markerEnd = resources->markerEnd(); |
| 130 ASSERT(markerStart || markerMid || markerEnd); |
| 131 |
| 132 FloatRect boundaries; |
| 133 unsigned size = m_markerPositions.size(); |
| 134 for (unsigned i = 0; i < size; ++i) { |
| 135 if (RenderSVGResourceMarker* marker = SVGMarkerData::markerForType(m_mar
kerPositions[i].type, markerStart, markerMid, markerEnd)) |
| 136 boundaries.unite(marker->markerBoundaries(marker->markerTransformati
on(m_markerPositions[i].origin, m_markerPositions[i].angle, strokeWidth))); |
| 137 } |
| 138 return boundaries; |
163 } | 139 } |
| 140 |
| 141 bool RenderSVGPath::shouldGenerateMarkerPositions() const |
| 142 { |
| 143 if (!style()->svgStyle().hasMarkers()) |
| 144 return false; |
| 145 |
| 146 if (!SVGResources::supportsMarkers(*toSVGGraphicsElement(element()))) |
| 147 return false; |
| 148 |
| 149 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(
this); |
| 150 if (!resources) |
| 151 return false; |
| 152 |
| 153 return resources->markerStart() || resources->markerMid() || resources->mark
erEnd(); |
| 154 } |
| 155 |
| 156 void RenderSVGPath::processMarkerPositions() |
| 157 { |
| 158 m_markerPositions.clear(); |
| 159 |
| 160 if (!shouldGenerateMarkerPositions()) |
| 161 return; |
| 162 |
| 163 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(
this); |
| 164 ASSERT(resources); |
| 165 |
| 166 RenderSVGResourceMarker* markerStart = resources->markerStart(); |
| 167 |
| 168 SVGMarkerData markerData(m_markerPositions, markerStart ? markerStart->orien
tType() == SVGMarkerOrientAutoStartReverse : false); |
| 169 path().apply(&markerData, SVGMarkerData::updateFromPathElement); |
| 170 markerData.pathIsDone(); |
| 171 } |
| 172 |
| 173 } |
OLD | NEW |