OSDN Git Service

am cd011297: am ff93a851: Bug fix 2337042 <base> URL + <img> URL = URL that starts...
[android-x86/external-webkit.git] / WebCore / rendering / RenderSVGResourceGradient.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  *               2008 Eric Seidel <eric@webkit.org>
4  *               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
24 #include "config.h"
25
26 #if ENABLE(SVG)
27 #include "RenderSVGResourceGradient.h"
28
29 #include "GradientAttributes.h"
30 #include "GraphicsContext.h"
31 #include "SVGImageBufferTools.h"
32 #include "SVGRenderSupport.h"
33 #include <wtf/UnusedParam.h>
34
35 namespace WebCore {
36
37 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
38     : RenderSVGResourceContainer(node)
39 #if PLATFORM(CG)
40     , m_savedContext(0)
41 #endif
42 {
43 }
44
45 RenderSVGResourceGradient::~RenderSVGResourceGradient()
46 {
47     if (m_gradient.isEmpty())
48         return;
49
50     deleteAllValues(m_gradient);
51     m_gradient.clear();
52 }
53
54 void RenderSVGResourceGradient::removeAllClientsFromCache(bool markForInvalidation)
55 {
56     if (!m_gradient.isEmpty()) {
57         deleteAllValues(m_gradient);
58         m_gradient.clear();
59     }
60
61     markAllClientsForInvalidation(markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
62 }
63
64 void RenderSVGResourceGradient::removeClientFromCache(RenderObject* client, bool markForInvalidation)
65 {
66     ASSERT(client);
67
68     if (m_gradient.contains(client))
69         delete m_gradient.take(client);
70
71     markClientForInvalidation(client, markForInvalidation ? RepaintInvalidation : ParentOnlyInvalidation);
72 }
73
74 #if PLATFORM(CG)
75 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
76                                                            GraphicsContext*& savedContext,
77                                                            OwnPtr<ImageBuffer>& imageBuffer,
78                                                            const RenderObject* object)
79 {
80     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
81     ASSERT(textRootBlock);
82
83     AffineTransform absoluteTransform(SVGImageBufferTools::absoluteTransformFromContext(context));
84     FloatRect absoluteTargetRect = absoluteTransform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
85
86     OwnPtr<ImageBuffer> maskImage;
87     if (!SVGImageBufferTools::createImageBuffer(absoluteTransform, absoluteTargetRect, maskImage, DeviceRGB))
88         return false;
89
90     ASSERT(maskImage);
91     savedContext = context;
92     context = maskImage->context();
93     imageBuffer = maskImage.release();
94     return true;
95 }
96
97 static inline AffineTransform clipToTextMask(GraphicsContext* context,
98                                              OwnPtr<ImageBuffer>& imageBuffer,
99                                              FloatRect& repaintRect,
100                                              const RenderObject* object,
101                                              GradientData* gradientData)
102 {
103     const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
104     ASSERT(textRootBlock);
105
106     repaintRect = textRootBlock->repaintRectInLocalCoordinates();
107
108     AffineTransform absoluteTransform(SVGImageBufferTools::absoluteTransformFromContext(context));
109     FloatRect absoluteTargetRect = absoluteTransform.mapRect(repaintRect);
110
111     SVGImageBufferTools::clipToImageBuffer(context, absoluteTransform, absoluteTargetRect, imageBuffer.get());
112
113     AffineTransform matrix;
114     if (gradientData->boundingBoxMode) {
115         FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
116         matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
117         matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
118     }
119     matrix.multLeft(gradientData->transform);
120     return matrix;
121 }
122 #endif
123
124 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
125 {
126     ASSERT(object);
127     ASSERT(style);
128     ASSERT(context);
129     ASSERT(resourceMode != ApplyToDefaultMode);
130
131     // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
132     // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
133     // synchronization to kick in, which causes removeAllClientsFromCache() to be called, which in turn deletes our
134     // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
135     SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
136     if (!gradientElement)
137         return false;
138
139     gradientElement->updateAnimatedSVGAttribute(anyQName());
140
141     if (!m_gradient.contains(object))
142         m_gradient.set(object, new GradientData);
143
144     GradientData* gradientData = m_gradient.get(object);
145
146     bool isPaintingText = resourceMode & ApplyToTextMode;
147
148     // Create gradient object
149     if (!gradientData->gradient) {
150         buildGradient(gradientData, gradientElement);
151
152         // CG platforms will handle the gradient space transform for text after applying the
153         // resource, so don't apply it here. For non-CG platforms, we want the text bounding
154         // box applied to the gradient space transform now, so the gradient shader can use it.
155 #if PLATFORM(CG)
156         if (gradientData->boundingBoxMode && !isPaintingText) {
157 #else
158         if (gradientData->boundingBoxMode) {
159 #endif
160             FloatRect objectBoundingBox = object->objectBoundingBox();
161             gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
162             gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
163         }
164
165         gradientData->userspaceTransform.multLeft(gradientData->transform);
166         gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
167     }
168
169     if (!gradientData->gradient)
170         return false;
171
172     // Draw gradient
173     context->save();
174
175     if (isPaintingText) {
176 #if PLATFORM(CG)
177         if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
178             context->restore();
179             return false;
180         }
181 #endif
182
183         context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
184     }
185
186     const SVGRenderStyle* svgStyle = style->svgStyle();
187     ASSERT(svgStyle);
188
189     if (resourceMode & ApplyToFillMode) {
190         context->setAlpha(svgStyle->fillOpacity());
191         context->setFillGradient(gradientData->gradient);
192         context->setFillRule(svgStyle->fillRule());
193     } else if (resourceMode & ApplyToStrokeMode) {
194         if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
195             gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
196         context->setAlpha(svgStyle->strokeOpacity());
197         context->setStrokeGradient(gradientData->gradient);
198         SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
199     }
200
201     return true;
202 }
203
204 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
205 {
206     ASSERT(context);
207     ASSERT(resourceMode != ApplyToDefaultMode);
208
209     if (resourceMode & ApplyToTextMode) {
210 #if PLATFORM(CG)
211         // CG requires special handling for gradient on text
212         if (m_savedContext && m_gradient.contains(object)) {
213             GradientData* gradientData = m_gradient.get(object);
214
215             // Restore on-screen drawing context
216             context = m_savedContext;
217             m_savedContext = 0;
218
219             FloatRect repaintRect;
220             gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, repaintRect, object, gradientData));
221             context->setFillGradient(gradientData->gradient);
222
223             context->fillRect(repaintRect);
224             m_imageBuffer.clear();
225         }
226 #else
227         UNUSED_PARAM(object);
228 #endif
229     } else {
230         if (resourceMode & ApplyToFillMode)
231             context->fillPath();
232         else if (resourceMode & ApplyToStrokeMode)
233             context->strokePath();
234     }
235
236     context->restore();
237 }
238
239 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
240 {
241     ASSERT(gradientData->gradient);
242
243     const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
244     for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
245         gradientData->gradient->addColorStop(*it);
246 }
247
248 }
249
250 #endif