tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / rendering / svg / RenderSVGResource.cpp
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 "SVGResourcesCache.h"
32 #include "SVGURIReference.h"
33
34 namespace WebCore {
35
36 static inline bool inheritColorFromParentStyleIfNeeded(RenderObject* object, bool applyToFill, Color& color)
37 {
38     if (color.isValid())
39         return true;
40     if (!object->parent() || !object->parent()->style())
41         return false;
42     const SVGRenderStyle* parentSVGStyle = object->parent()->style()->svgStyle();
43     color = applyToFill ? parentSVGStyle->fillPaintColor() : parentSVGStyle->strokePaintColor();
44     return true;
45 }
46
47 static inline RenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderObject* object, const RenderStyle* style, Color& fallbackColor)
48 {
49     ASSERT(object);
50     ASSERT(style);
51
52     // If we have no style at all, ignore it.
53     const SVGRenderStyle* svgStyle = style->svgStyle();
54     if (!svgStyle)
55         return 0;
56
57     // If we have no fill/stroke, return 0.
58     if (mode == ApplyToFillMode) {
59         if (!svgStyle->hasFill())
60             return 0;
61     } else {
62         if (!svgStyle->hasStroke())
63             return 0;
64     }
65
66     bool applyToFill = mode == ApplyToFillMode;
67     SVGPaint::SVGPaintType paintType = applyToFill ? svgStyle->fillPaintType() : svgStyle->strokePaintType();
68     if (paintType == SVGPaint::SVG_PAINTTYPE_NONE)
69         return 0;
70
71     Color color;
72     switch (paintType) {
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();
80     default:
81         break;
82     }
83
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();
87
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());
93         }
94     }
95
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))
100             return 0;
101
102         colorResource->setColor(color);
103         return colorResource;
104     }
105
106     // If no resources are associated with the given renderer, return the color resource.
107     SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(object);
108     if (!resources) {
109         if (paintType == SVGPaint::SVG_PAINTTYPE_URI_NONE || !inheritColorFromParentStyleIfNeeded(object, applyToFill, color))
110             return 0;
111
112         colorResource->setColor(color);
113         return colorResource;
114     }
115
116     // If the requested resource is not available, return the color resource.
117     RenderSVGResource* uriResource = mode == ApplyToFillMode ? resources->fill() : resources->stroke();
118     if (!uriResource) {
119         if (!inheritColorFromParentStyleIfNeeded(object, applyToFill, color))
120             return 0;
121
122         colorResource->setColor(color);
123         return colorResource;
124     }
125
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;
129     return uriResource;
130 }
131
132 RenderSVGResource* RenderSVGResource::fillPaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
133 {
134     return requestPaintingResource(ApplyToFillMode, object, style, fallbackColor);
135 }
136
137 RenderSVGResource* RenderSVGResource::strokePaintingResource(RenderObject* object, const RenderStyle* style, Color& fallbackColor)
138 {
139     return requestPaintingResource(ApplyToStrokeMode, object, style, fallbackColor);
140 }
141
142 RenderSVGResourceSolidColor* RenderSVGResource::sharedSolidPaintingResource()
143 {
144     static RenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
145     if (!s_sharedSolidPaintingResource)
146         s_sharedSolidPaintingResource = new RenderSVGResourceSolidColor;
147     return s_sharedSolidPaintingResource;
148 }
149
150 void RenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject* object, bool needsLayout)
151 {
152     ASSERT(object);
153     if (needsLayout)
154         object->setNeedsLayout(true);
155
156     // Invalidate resources in ancestor chain, if needed.
157     RenderObject* current = object->parent();
158     while (current) {
159         if (current->isSVGResourceContainer()) {
160             current->toRenderSVGResourceContainer()->removeAllClientsFromCache();
161             break;
162         }
163
164         current = current->parent();
165     }
166 }
167
168 }
169
170 #endif
171