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.
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.
27 #include "RenderSVGResourceGradient.h"
29 #include "GradientAttributes.h"
30 #include "GraphicsContext.h"
31 #include "SVGRenderSupport.h"
32 #include <wtf/UnusedParam.h>
36 RenderSVGResourceGradient::RenderSVGResourceGradient(SVGGradientElement* node)
37 : RenderSVGResourceContainer(node)
44 RenderSVGResourceGradient::~RenderSVGResourceGradient()
46 deleteAllValues(m_gradient);
50 void RenderSVGResourceGradient::invalidateClients()
52 const HashMap<RenderObject*, GradientData*>::const_iterator end = m_gradient.end();
53 for (HashMap<RenderObject*, GradientData*>::const_iterator it = m_gradient.begin(); it != end; ++it)
54 markForLayoutAndResourceInvalidation(it->first, false);
56 deleteAllValues(m_gradient);
60 void RenderSVGResourceGradient::invalidateClient(RenderObject* object)
63 if (!m_gradient.contains(object))
66 delete m_gradient.take(object);
67 markForLayoutAndResourceInvalidation(object, false);
71 static inline AffineTransform absoluteTransformFromContext(GraphicsContext* context)
73 // Extract current transformation matrix used in the original context. Note that this coordinate
74 // system is flipped compared to SVGs internal coordinate system, done in WebKit level. Fix
75 // this transformation by flipping the y component.
76 return context->getCTM() * AffineTransform().flipY();
79 static inline bool createMaskAndSwapContextForTextGradient(GraphicsContext*& context,
80 GraphicsContext*& savedContext,
81 OwnPtr<ImageBuffer>& imageBuffer,
82 const RenderObject* object)
84 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
86 AffineTransform transform(absoluteTransformFromContext(context));
87 FloatRect maskAbsoluteBoundingBox = transform.mapRect(textRootBlock->repaintRectInLocalCoordinates());
89 IntRect maskImageRect = enclosingIntRect(maskAbsoluteBoundingBox);
90 if (maskImageRect.isEmpty())
93 // Allocate an image buffer as big as the absolute unclipped size of the object
94 OwnPtr<ImageBuffer> maskImage = ImageBuffer::create(maskImageRect.size());
98 GraphicsContext* maskImageContext = maskImage->context();
100 // Transform the mask image coordinate system to absolute screen coordinates
101 maskImageContext->translate(-maskAbsoluteBoundingBox.x(), -maskAbsoluteBoundingBox.y());
102 maskImageContext->concatCTM(transform);
104 imageBuffer = maskImage.release();
105 savedContext = context;
106 context = maskImageContext;
111 static inline AffineTransform clipToTextMask(GraphicsContext* context,
112 OwnPtr<ImageBuffer>& imageBuffer,
113 const RenderObject* object,
114 GradientData* gradientData)
116 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
118 // The mask image has been created in the device coordinate space, as the image should not be scaled.
119 // So the actual masking process has to be done in the device coordinate space as well.
120 AffineTransform transform(absoluteTransformFromContext(context));
121 context->concatCTM(transform.inverse());
122 context->clipToImageBuffer(transform.mapRect(textRootBlock->repaintRectInLocalCoordinates()), imageBuffer.get());
123 context->concatCTM(transform);
125 AffineTransform matrix;
126 if (gradientData->boundingBoxMode) {
127 FloatRect maskBoundingBox = textRootBlock->objectBoundingBox();
128 matrix.translate(maskBoundingBox.x(), maskBoundingBox.y());
129 matrix.scaleNonUniform(maskBoundingBox.width(), maskBoundingBox.height());
131 matrix.multLeft(gradientData->transform);
136 bool RenderSVGResourceGradient::applyResource(RenderObject* object, RenderStyle* style, GraphicsContext*& context, unsigned short resourceMode)
141 ASSERT(resourceMode != ApplyToDefaultMode);
143 // Be sure to synchronize all SVG properties on the gradientElement _before_ processing any further.
144 // Otherwhise the call to collectGradientAttributes() in createTileImage(), may cause the SVG DOM property
145 // synchronization to kick in, which causes invalidateClients() to be called, which in turn deletes our
146 // GradientData object! Leaving out the line below will cause svg/dynamic-updates/SVG*GradientElement-svgdom* to crash.
147 SVGGradientElement* gradientElement = static_cast<SVGGradientElement*>(node());
148 if (!gradientElement)
151 gradientElement->updateAnimatedSVGAttribute(anyQName());
153 if (!m_gradient.contains(object))
154 m_gradient.set(object, new GradientData);
156 GradientData* gradientData = m_gradient.get(object);
158 bool isPaintingText = resourceMode & ApplyToTextMode;
160 // Create gradient object
161 if (!gradientData->gradient) {
162 buildGradient(gradientData, gradientElement);
164 // CG platforms will handle the gradient space transform for text after applying the
165 // resource, so don't apply it here. For non-CG platforms, we want the text bounding
166 // box applied to the gradient space transform now, so the gradient shader can use it.
168 if (gradientData->boundingBoxMode && !isPaintingText) {
170 if (gradientData->boundingBoxMode) {
172 FloatRect objectBoundingBox = object->objectBoundingBox();
173 gradientData->userspaceTransform.translate(objectBoundingBox.x(), objectBoundingBox.y());
174 gradientData->userspaceTransform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
177 gradientData->userspaceTransform.multLeft(gradientData->transform);
178 gradientData->gradient->setGradientSpaceTransform(gradientData->userspaceTransform);
181 if (!gradientData->gradient)
187 if (isPaintingText) {
189 if (!createMaskAndSwapContextForTextGradient(context, m_savedContext, m_imageBuffer, object)) {
195 context->setTextDrawingMode(resourceMode & ApplyToFillMode ? cTextFill : cTextStroke);
198 const SVGRenderStyle* svgStyle = style->svgStyle();
201 if (resourceMode & ApplyToFillMode) {
202 context->setAlpha(svgStyle->fillOpacity());
203 context->setFillGradient(gradientData->gradient);
204 context->setFillRule(svgStyle->fillRule());
205 } else if (resourceMode & ApplyToStrokeMode) {
206 if (svgStyle->vectorEffect() == VE_NON_SCALING_STROKE)
207 gradientData->gradient->setGradientSpaceTransform(transformOnNonScalingStroke(object, gradientData->userspaceTransform));
208 context->setAlpha(svgStyle->strokeOpacity());
209 context->setStrokeGradient(gradientData->gradient);
210 SVGRenderSupport::applyStrokeStyleToContext(context, style, object);
216 void RenderSVGResourceGradient::postApplyResource(RenderObject* object, GraphicsContext*& context, unsigned short resourceMode)
219 ASSERT(resourceMode != ApplyToDefaultMode);
221 if (resourceMode & ApplyToTextMode) {
223 // CG requires special handling for gradient on text
224 if (m_savedContext && m_gradient.contains(object)) {
225 GradientData* gradientData = m_gradient.get(object);
227 // Restore on-screen drawing context
228 context = m_savedContext;
231 gradientData->gradient->setGradientSpaceTransform(clipToTextMask(context, m_imageBuffer, object, gradientData));
232 context->setFillGradient(gradientData->gradient);
234 const RenderObject* textRootBlock = SVGRenderSupport::findTextRootObject(object);
235 context->fillRect(textRootBlock->repaintRectInLocalCoordinates());
237 m_imageBuffer.clear();
240 UNUSED_PARAM(object);
243 if (resourceMode & ApplyToFillMode)
245 else if (resourceMode & ApplyToStrokeMode)
246 context->strokePath();
252 void RenderSVGResourceGradient::addStops(GradientData* gradientData, const Vector<Gradient::ColorStop>& stops) const
254 ASSERT(gradientData->gradient);
256 const Vector<Gradient::ColorStop>::const_iterator end = stops.end();
257 for (Vector<Gradient::ColorStop>::const_iterator it = stops.begin(); it != end; ++it)
258 gradientData->gradient->addColorStop(*it);