Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: Source/core/paint/SVGShapePainter.cpp

Issue 678863002: Move SVG shape painting code to SVGShapePainter (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Cleanup for review Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/paint/SVGShapePainter.h"
7
8 #include "core/paint/ObjectPainter.h"
9 #include "core/paint/SVGMarkerPainter.h"
10 #include "core/rendering/GraphicsContextAnnotator.h"
11 #include "core/rendering/PaintInfo.h"
12 #include "core/rendering/svg/RenderSVGPath.h"
13 #include "core/rendering/svg/RenderSVGShape.h"
14 #include "core/rendering/svg/SVGMarkerData.h"
15 #include "core/rendering/svg/SVGRenderSupport.h"
16 #include "core/rendering/svg/SVGRenderingContext.h"
17 #include "core/rendering/svg/SVGResources.h"
18 #include "core/rendering/svg/SVGResourcesCache.h"
19
20 namespace blink {
21
22 static bool setupNonScalingStrokeContext(AffineTransform& strokeTransform, Graph icsContextStateSaver& stateSaver)
23 {
24 if (!strokeTransform.isInvertible())
25 return false;
26
27 stateSaver.save();
28 stateSaver.context()->concatCTM(strokeTransform.inverse());
29 return true;
30 }
31
32 static void useStrokeStyleToFill(GraphicsContext* context)
33 {
34 if (Gradient* gradient = context->strokeGradient())
35 context->setFillGradient(gradient);
36 else if (Pattern* pattern = context->strokePattern())
37 context->setFillPattern(pattern);
38 else
39 context->setFillColor(context->strokeColor());
40 }
41
42 void SVGShapePainter::paint(PaintInfo& paintInfo)
43 {
44 ANNOTATE_GRAPHICS_CONTEXT(paintInfo, &m_renderSVGShape);
45 if (paintInfo.phase != PaintPhaseForeground
46 || m_renderSVGShape.style()->visibility() == HIDDEN
47 || m_renderSVGShape.isShapeEmpty())
48 return;
49
50 FloatRect boundingBox = m_renderSVGShape.paintInvalidationRectInLocalCoordin ates();
51 if (!SVGRenderSupport::paintInfoIntersectsPaintInvalidationRect(boundingBox, m_renderSVGShape.localTransform(), paintInfo))
52 return;
53
54 PaintInfo childPaintInfo(paintInfo);
55
56 GraphicsContextStateSaver stateSaver(*childPaintInfo.context);
57 childPaintInfo.applyTransform(m_renderSVGShape.localTransform());
58
59 SVGRenderingContext renderingContext(&m_renderSVGShape, childPaintInfo);
60
61 if (renderingContext.isRenderingPrepared()) {
62 const SVGRenderStyle& svgStyle = m_renderSVGShape.style()->svgStyle();
63 if (svgStyle.shapeRendering() == SR_CRISPEDGES)
64 childPaintInfo.context->setShouldAntialias(false);
65
66 for (int i = 0; i < 3; i++) {
67 switch (svgStyle.paintOrderType(i)) {
68 case PT_FILL: {
69 GraphicsContextStateSaver stateSaver(*childPaintInfo.context, fa lse);
70 if (!SVGRenderSupport::updateGraphicsContext(stateSaver, m_rende rSVGShape.style(), m_renderSVGShape, ApplyToFillMode))
71 break;
72 fillShape(childPaintInfo.context);
73 break;
74 }
75 case PT_STROKE:
76 if (svgStyle.hasVisibleStroke()) {
77 GraphicsContextStateSaver stateSaver(*childPaintInfo.context , false);
78 AffineTransform nonScalingTransform;
79 const AffineTransform* additionalPaintServerTransform = 0;
80
81 if (m_renderSVGShape.hasNonScalingStroke()) {
82 nonScalingTransform = m_renderSVGShape.nonScalingStrokeT ransform();
83 if (!setupNonScalingStrokeContext(nonScalingTransform, s tateSaver))
84 return;
85
86 // Non-scaling stroke needs to reset the transform back to the host transform.
87 additionalPaintServerTransform = &nonScalingTransform;
88 }
89
90 if (!SVGRenderSupport::updateGraphicsContext(stateSaver, m_r enderSVGShape.style(), m_renderSVGShape, ApplyToStrokeMode, additionalPaintServe rTransform))
91 break;
92 strokeShape(childPaintInfo.context);
93 }
94 break;
95 case PT_MARKERS:
96 paintMarkers(childPaintInfo);
97 break;
98 default:
99 ASSERT_NOT_REACHED();
100 break;
101 }
102 }
103 }
104
105 if (m_renderSVGShape.style()->outlineWidth())
106 ObjectPainter(m_renderSVGShape).paintOutline(childPaintInfo, IntRect(bou ndingBox));
107 }
108
109 void SVGShapePainter::fillShape(GraphicsContext* context)
110 {
111 if (m_renderSVGShape.useRectRenderingFastPath()) {
112 context->fillRect(m_renderSVGShape.objectBoundingBox());
113 return;
114 }
115
116 if (m_renderSVGShape.useEllipseRenderingFastPath()) {
117 context->fillEllipse(m_renderSVGShape.objectBoundingBox());
118 return;
119 }
120
121 context->fillPath(m_renderSVGShape.path());
122 }
123
124 void SVGShapePainter::strokeShape(GraphicsContext* context)
125 {
126 if (!m_renderSVGShape.style()->svgStyle().hasVisibleStroke())
127 return;
128
129 if (m_renderSVGShape.useRectRenderingFastPath()) {
130 context->strokeRect(m_renderSVGShape.objectBoundingBox(), m_renderSVGSha pe.strokeWidth());
131 return;
132 }
133
134 if (m_renderSVGShape.useEllipseRenderingFastPath()) {
135 context->strokeEllipse(m_renderSVGShape.objectBoundingBox());
136 return;
137 }
138
139 ASSERT(m_renderSVGShape.hasPath());
140 Path* usePath = &m_renderSVGShape.path();
141 if (m_renderSVGShape.hasNonScalingStroke())
142 usePath = m_renderSVGShape.nonScalingStrokePath(usePath, m_renderSVGShap e.nonScalingStrokeTransform());
143
144 context->strokePath(*usePath);
145
146 strokeZeroLengthLineCaps(context);
147 }
148
149 void SVGShapePainter::paintMarkers(PaintInfo& paintInfo)
150 {
151 const Vector<MarkerPosition>* markerPositions = m_renderSVGShape.markerPosit ions();
152 if (!markerPositions || markerPositions->isEmpty())
153 return;
154
155 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject( &m_renderSVGShape);
156 if (!resources)
157 return;
158
159 RenderSVGResourceMarker* markerStart = resources->markerStart();
160 RenderSVGResourceMarker* markerMid = resources->markerMid();
161 RenderSVGResourceMarker* markerEnd = resources->markerEnd();
162 if (!markerStart && !markerMid && !markerEnd)
163 return;
164
165 float strokeWidth = m_renderSVGShape.strokeWidth();
166 unsigned size = markerPositions->size();
167 for (unsigned i = 0; i < size; ++i) {
168 if (RenderSVGResourceMarker* marker = SVGMarkerData::markerForType((*mar kerPositions)[i].type, markerStart, markerMid, markerEnd))
169 SVGMarkerPainter(*marker).paint(paintInfo, (*markerPositions)[i], st rokeWidth);
170 }
171 }
172
173 void SVGShapePainter::strokeZeroLengthLineCaps(GraphicsContext* context)
174 {
175 const Vector<FloatPoint>* zeroLengthLineCaps = m_renderSVGShape.zeroLengthLi neCaps();
176 if (!zeroLengthLineCaps || zeroLengthLineCaps->isEmpty())
177 return;
178
179 Path* usePath;
180 AffineTransform nonScalingTransform;
181
182 if (m_renderSVGShape.hasNonScalingStroke())
183 nonScalingTransform = m_renderSVGShape.nonScalingStrokeTransform();
184
185 GraphicsContextStateSaver stateSaver(*context, true);
186 useStrokeStyleToFill(context);
187 for (size_t i = 0; i < zeroLengthLineCaps->size(); ++i) {
188 usePath = zeroLengthLinecapPath((*zeroLengthLineCaps)[i]);
189 if (m_renderSVGShape.hasNonScalingStroke())
190 usePath = m_renderSVGShape.nonScalingStrokePath(usePath, nonScalingT ransform);
191 context->fillPath(*usePath);
192 }
193 }
194
195 Path* SVGShapePainter::zeroLengthLinecapPath(const FloatPoint& linecapPosition) const
196 {
197 DEFINE_STATIC_LOCAL(Path, tempPath, ());
198
199 tempPath.clear();
200 if (m_renderSVGShape.style()->svgStyle().capStyle() == SquareCap)
201 tempPath.addRect(RenderSVGPath::zeroLengthSubpathRect(linecapPosition, m _renderSVGShape.strokeWidth()));
202 else
203 tempPath.addEllipse(RenderSVGPath::zeroLengthSubpathRect(linecapPosition , m_renderSVGShape.strokeWidth()));
204
205 return &tempPath;
206 }
207
208 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698