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.
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.
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.
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.
26 #include "RenderSVGResource.h"
28 #include "RenderSVGResourceContainer.h"
29 #include "RenderSVGResourceSolidColor.h"
30 #include "SVGResources.h"
31 #include "SVGResourcesCache.h"
32 #include "SVGURIReference.h"
36 static inline bool inheritColorFromParentStyleIfNeeded(RenderObject* object, bool applyToFill, Color& color)
40 if (!object->parent() || !object->parent()->style())
42 const SVGRenderStyle* parentSVGStyle = object->parent()->style()->svgStyle();
43 color = applyToFill ? parentSVGStyle->fillPaintColor() : parentSVGStyle->strokePaintColor();
47 static inline RenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderObject* object, const RenderStyle* style, Color& fallbackColor)
52 // If we have no style at all, ignore it.
53 const SVGRenderStyle* svgStyle = style->svgStyle();
57 // If we have no fill/stroke, return 0.
58 if (mode == ApplyToFillMode) {
59 if (!svgStyle->hasFill())
62 if (!svgStyle->hasStroke())
66 bool applyToFill = mode == ApplyToFillMode;
67 SVGPaint::SVGPaintType paintType = applyToFill ? svgStyle->fillPaintType() : svgStyle->strokePaintType();
68 if (paintType == SVGPaint::SVG_PAINTTYPE_NONE)
73 case SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR:
74 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR:
75 case SVGPaint::SVG_PAINTTYPE_RGBCOLOR_ICCCOLOR:
76 case SVGPaint::SVG_PAINTTYPE_URI_CURRENTCOLOR:
77 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR:
78 case SVGPaint::SVG_PAINTTYPE_URI_RGBCOLOR_ICCCOLOR:
79 color = applyToFill ? svgStyle->fillPaintColor() : svgStyle->strokePaintColor();
84 if (style->insideLink() == InsideVisitedLink) {
85 // FIXME: This code doesn't support the uri component of the visited link paint, https://bugs.webkit.org/show_bug.cgi?id=70006
86 SVGPaint::SVGPaintType visitedPaintType = applyToFill ? svgStyle->visitedLinkFillPaintType() : svgStyle->visitedLinkStrokePaintType();
88 // For SVG_PAINTTYPE_CURRENTCOLOR, 'color' already contains the 'visitedColor'.
89 if (visitedPaintType < SVGPaint::SVG_PAINTTYPE_URI_NONE && visitedPaintType != SVGPaint::SVG_PAINTTYPE_CURRENTCOLOR) {
90 const Color& visitedColor = applyToFill ? svgStyle->visitedLinkFillPaintColor() : svgStyle->visitedLinkStrokePaintColor();
91 if (visitedColor.isValid())
92 color = Color(visitedColor.red(), visitedColor.green(), visitedColor.blue(), color.alpha());
96 // If the primary resource is just a color, return immediately.
97 RenderSVGResourceSolidColor* colorResource = RenderSVGResource::sharedSolidPaintingResource();
98 if (paintType < SVGPaint::SVG_PAINTTYPE_URI_NONE) {
99 if (!inheritColorFromParentStyleIfNeeded(object, applyToFill, color))
102 colorResource->setColor(color);
103 return colorResource;
106 // If no resources are associated with the given renderer, return the color resource.
107 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
109 if (paintType == SVGPaint::SVG_PAINTTYPE_URI_NONE || !inheritColorFromParentStyleIfNeeded(object, applyToFill, color))
112 colorResource->setColor(color);
113 return colorResource;
116 // If the requested resource is not available, return the color resource.
117 RenderSVGResource* uriResource = mode == ApplyToFillMode ? resources->fill() : resources->stroke();
119 if (!inheritColorFromParentStyleIfNeeded(object, applyToFill, color))
122 colorResource->setColor(color);
123 return colorResource;
126 // The paint server resource exists, though it may be invalid (pattern with width/height=0). Pass the fallback color to our caller
127 // so it can use the solid color painting resource, if applyResource() on the URI resource failed.
128 fallbackColor = color;
132 RenderSVGResource* RenderSVGResource::fillPaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
134 return requestPaintingResource(ApplyToFillMode, object, style, fallbackColor);
137 RenderSVGResource* RenderSVGResource::strokePaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
139 return requestPaintingResource(ApplyToStrokeMode, object, style, fallbackColor);
142 RenderSVGResourceSolidColor* RenderSVGResource::sharedSolidPaintingResource()
144 static RenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
145 if (!s_sharedSolidPaintingResource)
146 s_sharedSolidPaintingResource = new RenderSVGResourceSolidColor;
147 return s_sharedSolidPaintingResource;
150 void RenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject* object, bool needsLayout)
154 object->setNeedsLayout(true);
156 // Invalidate resources in ancestor chain, if needed.
157 RenderObject* current = object->parent();
159 if (current->isSVGResourceContainer()) {
160 current->toRenderSVGResourceContainer()->removeAllClientsFromCache();
164 current = current->parent();