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
25 #if ENABLE(SVG)
26 #include "RenderSVGResource.h"
27
28 #include "RenderSVGResourceContainer.h"
29 #include "RenderSVGResourceSolidColor.h"
30 #include "SVGResources.h"
31 #include "SVGURIReference.h"
32
33 namespace WebCore {
34
requestPaintingResource(RenderSVGResourceMode mode,RenderObject * object,const RenderStyle * style,Color & fallbackColor)35 static inline RenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderObject* object, const RenderStyle* style, Color& fallbackColor)
36 {
37 ASSERT(object);
38 ASSERT(style);
39
40 // If we have no style at all, ignore it.
41 const SVGRenderStyle* svgStyle = style->svgStyle();
42 if (!svgStyle)
43 return 0;
44
45 // If we have no fill/stroke, return 0.
46 if (mode == ApplyToFillMode) {
47 if (!svgStyle->hasFill())
48 return 0;
49 } else {
50 if (!svgStyle->hasStroke())
51 return 0;
52 }
53
54 bool applyToFill = mode == ApplyToFillMode;
55 SVGPaint::SVGPaintType paintType = applyToFill ? svgStyle->fillPaintType() : svgStyle->strokePaintType();
56 if (paintType == SVGPaint::SVG_PAINTTYPE_NONE)
57 return 0;
58
59 Color color;
60 switch (paintType) {
61 case SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR:
62 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR:
63 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
64 case SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR:
65 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR:
66 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
67 color = applyToFill ? svgStyle->fillPaintColor() : svgStyle->strokePaintColor();
68 default:
69 break;
70 }
71
72 if (style->insideLink() == InsideVisitedLink) {
73 RenderStyle* visitedStyle = style->getCachedPseudoStyle(VISITED_LINK);
74 ASSERT(visitedStyle);
75
76 const SVGRenderStyle* svgVisitedStyle = visitedStyle->svgStyle();
77 SVGPaint::SVGPaintType visitedPaintType = applyToFill ? svgVisitedStyle->fillPaintType() : svgVisitedStyle->strokePaintType();
78
79 // For SVG_PAINTTYPE_CURRENTCOLOR, 'color' already contains the 'visitedColor'.
80 if (visitedPaintType < SVGPaint::SVG_PAINTTYPE_URI_NONE && visitedPaintType != SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR) {
81 const Color& visitedColor = applyToFill ? svgVisitedStyle->fillPaintColor() : svgVisitedStyle->strokePaintColor();
82 if (visitedColor.isValid())
83 color = Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), color.alpha());
84 }
85 }
86
87 // If the primary resource is just a color, return immediately.
88 RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
89 if (paintType < SVGPaint::SVG_PAINTTYPE_URI_NONE) {
90 // If an invalid fill color is specified, fallback to fill/stroke="none".
91 if (!color.isValid())
92 return 0;
93
94 colorResource->setColor(color);
95 return colorResource;
96 }
97
98 // If no resources are associated with the given renderer, return the color resource.
99 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
100 if (!resources) {
101 // If a paint server is specified, and no or an invalid fallback color is given, default to fill/stroke="black".
102 if (!color.isValid())
103 color = Color::black;
104
105 colorResource->setColor(color);
106 return colorResource;
107 }
108
109 // If the requested resource is not available, return the color resource.
110 RenderSVGResource* uriResource = mode == ApplyToFillMode ? resources->fill() : resources->stroke();
111 if (!uriResource) {
112 // If a paint server is specified, and no or an invalid fallback color is given, default to fill/stroke="black".
113 if (!color.isValid())
114 color = Color::black;
115
116 colorResource->setColor(color);
117 return colorResource;
118 }
119
120 // The paint server resource exists, though it may be invalid (pattern with width/height=0). Pass the fallback color to our caller
121 // so it can use the solid color painting resource, if applyResource() on the URI resource failed.
122 fallbackColor = color;
123 return uriResource;
124 }
125
fillPaintingResource(RenderObject * object,const RenderStyle * style,Color & fallbackColor)126 RenderSVGResource* RenderSVGResource::fillPaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
127 {
128 return requestPaintingResource(ApplyToFillMode, object, style, fallbackColor);
129 }
130
strokePaintingResource(RenderObject * object,const RenderStyle * style,Color & fallbackColor)131 RenderSVGResource* RenderSVGResource::strokePaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
132 {
133 return requestPaintingResource(ApplyToStrokeMode, object, style, fallbackColor);
134 }
135
sharedSolidPaintingResource()136 RenderSVGResourceSolidColor* RenderSVGResource::sharedSolidPaintingResource()
137 {
138 static RenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
139 if (!s_sharedSolidPaintingResource)
140 s_sharedSolidPaintingResource = new RenderSVGResourceSolidColor;
141 return s_sharedSolidPaintingResource;
142 }
143
markForLayoutAndParentResourceInvalidation(RenderObject * object,bool needsLayout)144 void RenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject* object, bool needsLayout)
145 {
146 ASSERT(object);
147 if (needsLayout)
148 object->setNeedsLayout(true);
149
150 // Invalidate resources in ancestor chain, if needed.
151 RenderObject* current = object->parent();
152 while (current) {
153 if (current->isSVGResourceContainer()) {
154 current->toRenderSVGResourceContainer()->removeAllClientsFromCache();
155 break;
156 }
157
158 current = current->parent();
159 }
160 }
161
162 }
163
164 #endif
165