OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> | |
3 * Copyright (C) 2007 Rob Buis <buis@kde.org> | |
4 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> | |
5 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | |
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 "config.h" | |
24 #include "core/rendering/svg/RenderSVGResourcePaintServer.h" | |
25 | |
26 #include "core/layout/style/LayoutStyle.h" | |
27 #include "core/layout/svg/SVGResources.h" | |
28 #include "core/layout/svg/SVGResourcesCache.h" | |
29 #include "platform/graphics/GraphicsContext.h" | |
30 #include "platform/graphics/GraphicsContextStateSaver.h" | |
31 | |
32 namespace blink { | |
33 | |
34 SVGPaintServer::SVGPaintServer(Color color) | |
35 : m_color(color) | |
36 { | |
37 } | |
38 | |
39 SVGPaintServer::SVGPaintServer(PassRefPtr<Gradient> gradient) | |
40 : m_gradient(gradient) | |
41 , m_color(Color::black) | |
42 { | |
43 } | |
44 | |
45 SVGPaintServer::SVGPaintServer(PassRefPtr<Pattern> pattern) | |
46 : m_pattern(pattern) | |
47 , m_color(Color::black) | |
48 { | |
49 } | |
50 | |
51 void SVGPaintServer::apply(GraphicsContext& context, RenderSVGResourceMode resou
rceMode, float paintAlpha, GraphicsContextStateSaver& stateSaver) | |
52 { | |
53 ASSERT(resourceMode == ApplyToFillMode || resourceMode == ApplyToStrokeMode)
; | |
54 | |
55 if (m_gradient || m_pattern) | |
56 stateSaver.saveIfNeeded(); | |
57 | |
58 if (resourceMode == ApplyToFillMode) { | |
59 if (m_pattern) | |
60 context.setFillPattern(m_pattern, paintAlpha); | |
61 else if (m_gradient) | |
62 context.setFillGradient(m_gradient, paintAlpha); | |
63 else | |
64 context.setFillColor(scaleAlpha(m_color.rgb(), paintAlpha)); | |
65 } else { | |
66 if (m_pattern) | |
67 context.setStrokePattern(m_pattern, paintAlpha); | |
68 else if (m_gradient) | |
69 context.setStrokeGradient(m_gradient, paintAlpha); | |
70 else | |
71 context.setStrokeColor(scaleAlpha(m_color.rgb(), paintAlpha)); | |
72 } | |
73 } | |
74 | |
75 void SVGPaintServer::prependTransform(const AffineTransform& transform) | |
76 { | |
77 ASSERT(m_gradient || m_pattern); | |
78 if (m_pattern) | |
79 m_pattern->setPatternSpaceTransform(transform * m_pattern->patternSpaceT
ransform()); | |
80 else | |
81 m_gradient->setGradientSpaceTransform(transform * m_gradient->gradientSp
aceTransform()); | |
82 } | |
83 | |
84 static SVGPaintDescription requestPaint(const LayoutObject& object, const Layout
Style& style, RenderSVGResourceMode mode) | |
85 { | |
86 // If we have no style at all, ignore it. | |
87 const SVGLayoutStyle& svgStyle = style.svgStyle(); | |
88 | |
89 // If we have no fill/stroke, return 0. | |
90 if (mode == ApplyToFillMode) { | |
91 if (!svgStyle.hasFill()) | |
92 return SVGPaintDescription(); | |
93 } else { | |
94 if (!svgStyle.hasStroke()) | |
95 return SVGPaintDescription(); | |
96 } | |
97 | |
98 bool applyToFill = mode == ApplyToFillMode; | |
99 SVGPaintType paintType = applyToFill ? svgStyle.fillPaintType() : svgStyle.s
trokePaintType(); | |
100 ASSERT(paintType != SVG_PAINTTYPE_NONE); | |
101 | |
102 Color color; | |
103 bool hasColor = false; | |
104 switch (paintType) { | |
105 case SVG_PAINTTYPE_CURRENTCOLOR: | |
106 case SVG_PAINTTYPE_RGBCOLOR: | |
107 case SVG_PAINTTYPE_URI_CURRENTCOLOR: | |
108 case SVG_PAINTTYPE_URI_RGBCOLOR: | |
109 color = applyToFill ? svgStyle.fillPaintColor() : svgStyle.strokePaintCo
lor(); | |
110 hasColor = true; | |
111 default: | |
112 break; | |
113 } | |
114 | |
115 if (style.insideLink() == InsideVisitedLink) { | |
116 // FIXME: This code doesn't support the uri component of the visited lin
k paint, https://bugs.webkit.org/show_bug.cgi?id=70006 | |
117 SVGPaintType visitedPaintType = applyToFill ? svgStyle.visitedLinkFillPa
intType() : svgStyle.visitedLinkStrokePaintType(); | |
118 | |
119 // For SVG_PAINTTYPE_CURRENTCOLOR, 'color' already contains the 'visited
Color'. | |
120 if (visitedPaintType < SVG_PAINTTYPE_URI_NONE && visitedPaintType != SVG
_PAINTTYPE_CURRENTCOLOR) { | |
121 const Color& visitedColor = applyToFill ? svgStyle.visitedLinkFillPa
intColor() : svgStyle.visitedLinkStrokePaintColor(); | |
122 color = Color(visitedColor.red(), visitedColor.green(), visitedColor
.blue(), color.alpha()); | |
123 hasColor = true; | |
124 } | |
125 } | |
126 | |
127 // If the primary resource is just a color, return immediately. | |
128 if (paintType < SVG_PAINTTYPE_URI_NONE) { | |
129 // |paintType| will be either <current-color> or <rgb-color> here - both
of which will have a color. | |
130 ASSERT(hasColor); | |
131 return SVGPaintDescription(color); | |
132 } | |
133 | |
134 RenderSVGResourcePaintServer* uriResource = 0; | |
135 if (SVGResources* resources = SVGResourcesCache::cachedResourcesForLayoutObj
ect(&object)) | |
136 uriResource = applyToFill ? resources->fill() : resources->stroke(); | |
137 | |
138 // If the requested resource is not available, return the color resource or
'none'. | |
139 if (!uriResource) { | |
140 // The fallback is 'none'. (SVG2 say 'none' is implied when no fallback
is specified.) | |
141 if (paintType == SVG_PAINTTYPE_URI_NONE || !hasColor) | |
142 return SVGPaintDescription(); | |
143 | |
144 return SVGPaintDescription(color); | |
145 } | |
146 | |
147 // The paint server resource exists, though it may be invalid (pattern with
width/height=0). | |
148 // Return the fallback color to our caller so it can use it, if | |
149 // preparePaintServer() on the resource container failed. | |
150 if (hasColor) | |
151 return SVGPaintDescription(uriResource, color); | |
152 | |
153 return SVGPaintDescription(uriResource); | |
154 } | |
155 | |
156 SVGPaintServer SVGPaintServer::requestForRenderer(const LayoutObject& renderer,
const LayoutStyle& style, RenderSVGResourceMode resourceMode) | |
157 { | |
158 ASSERT(resourceMode == ApplyToFillMode || resourceMode == ApplyToStrokeMode)
; | |
159 | |
160 SVGPaintDescription paintDescription = requestPaint(renderer, style, resourc
eMode); | |
161 if (!paintDescription.isValid) | |
162 return invalid(); | |
163 if (!paintDescription.resource) | |
164 return SVGPaintServer(paintDescription.color); | |
165 SVGPaintServer paintServer = paintDescription.resource->preparePaintServer(r
enderer); | |
166 if (paintServer.isValid()) | |
167 return paintServer; | |
168 if (paintDescription.hasFallback) | |
169 return SVGPaintServer(paintDescription.color); | |
170 return invalid(); | |
171 } | |
172 | |
173 bool SVGPaintServer::existsForRenderer(const LayoutObject& renderer, const Layou
tStyle& style, RenderSVGResourceMode resourceMode) | |
174 { | |
175 return requestPaint(renderer, style, resourceMode).isValid; | |
176 } | |
177 | |
178 RenderSVGResourcePaintServer::RenderSVGResourcePaintServer(SVGElement* element) | |
179 : RenderSVGResourceContainer(element) | |
180 { | |
181 } | |
182 | |
183 RenderSVGResourcePaintServer::~RenderSVGResourcePaintServer() | |
184 { | |
185 } | |
186 | |
187 SVGPaintDescription RenderSVGResourcePaintServer::requestPaintDescription(const
LayoutObject& renderer, const LayoutStyle& style, RenderSVGResourceMode resource
Mode) | |
188 { | |
189 return requestPaint(renderer, style, resourceMode); | |
190 } | |
191 | |
192 } | |
OLD | NEW |