2 * Copyright (C) 2007, 2008 Rob Buis <buis@kde.org>
3 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
28 #include "SVGRenderingContext.h"
31 #include "FrameView.h"
32 #include "RenderSVGResource.h"
33 #include "RenderSVGResourceClipper.h"
34 #include "RenderSVGResourceFilter.h"
35 #include "RenderSVGResourceMasker.h"
36 #include "SVGResources.h"
37 #include "SVGResourcesCache.h"
39 static int kMaxImageBufferSize = 4096;
43 static inline bool isRenderingMaskImage(RenderObject* object)
45 if (object->frame() && object->frame()->view())
46 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
50 SVGRenderingContext::~SVGRenderingContext()
52 // Fast path if we don't need to restore anything.
53 if (!(m_renderingFlags & ActionsNeeded))
56 ASSERT(m_object && m_paintInfo);
59 if (m_renderingFlags & EndFilterLayer) {
61 m_filter->postApplyResource(static_cast<RenderSVGShape*>(m_object), m_paintInfo->context, ApplyToDefaultMode, 0, 0);
62 m_paintInfo->context = m_savedContext;
66 if (m_renderingFlags & EndOpacityLayer)
67 m_paintInfo->context->endTransparencyLayer();
69 if (m_renderingFlags & EndShadowLayer)
70 m_paintInfo->context->endTransparencyLayer();
72 if (m_renderingFlags & RestoreGraphicsContext)
73 m_paintInfo->context->restore();
76 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave)
81 // This function must not be called twice!
82 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
83 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
87 m_paintInfo = &paintInfo;
92 // We need to save / restore the context even if the initialization failed.
93 if (needsGraphicsContextSave == SaveGraphicsContext) {
94 m_paintInfo->context->save();
95 m_renderingFlags |= RestoreGraphicsContext;
98 RenderStyle* style = m_object->style();
101 const SVGRenderStyle* svgStyle = style->svgStyle();
104 // Setup transparency layers before setting up SVG resources!
105 bool isRenderingMask = isRenderingMaskImage(m_object);
106 float opacity = isRenderingMask ? 1 : style->opacity();
107 const ShadowData* shadow = svgStyle->shadow();
108 if (opacity < 1 || shadow) {
109 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates();
112 m_paintInfo->context->clip(repaintRect);
113 m_paintInfo->context->beginTransparencyLayer(opacity);
114 m_renderingFlags |= EndOpacityLayer;
118 m_paintInfo->context->clip(repaintRect);
119 m_paintInfo->context->setShadow(IntSize(roundToInt(shadow->x()), roundToInt(shadow->y())), shadow->blur(), shadow->color(), style->colorSpace());
120 m_paintInfo->context->beginTransparencyLayer(1);
121 m_renderingFlags |= EndShadowLayer;
125 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
128 if (svgStyle->hasFilter())
131 m_renderingFlags |= RenderingPrepared;
135 if (!isRenderingMask) {
136 if (RenderSVGResourceMasker* masker = resources->masker()) {
137 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
142 if (RenderSVGResourceClipper* clipper = resources->clipper()) {
143 if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
148 if (!isRenderingMask) {
149 m_filter = resources->filter();
151 m_savedContext = m_paintInfo->context;
152 // Return with false here may mean that we don't need to draw the content
153 // (because it was either drawn before or empty) but we still need to apply the filter.
154 m_renderingFlags |= EndFilterLayer;
155 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
161 m_renderingFlags |= RenderingPrepared;
164 static AffineTransform& currentContentTransformation()
166 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
167 return s_currentContentTransformation;
170 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
175 calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
176 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
179 void SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
181 const RenderObject* current = renderer;
184 absoluteTransform = currentContentTransformation();
186 absoluteTransform = current->localToParentTransform() * absoluteTransform;
187 if (current->isSVGRoot())
189 current = current->parent();
193 bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
195 IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform);
196 // Don't create empty ImageBuffers.
197 if (paintRect.isEmpty())
200 IntSize clampedSize = clampedAbsoluteSize(paintRect.size());
201 OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode);
205 GraphicsContext* imageContext = image->context();
206 ASSERT(imageContext);
208 imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(),
209 static_cast<float>(clampedSize.height()) / paintRect.height()));
210 imageContext->translate(-paintRect.x(), -paintRect.y());
211 imageContext->concatCTM(absoluteTransform);
213 imageBuffer = image.release();
217 bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
219 IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size()));
220 IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size()));
222 // Don't create empty ImageBuffers.
223 if (imageSize.isEmpty())
226 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode);
230 GraphicsContext* imageContext = image->context();
231 ASSERT(imageContext);
233 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
234 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
236 imageBuffer = image.release();
240 void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
244 ASSERT(image->context());
246 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, 0, 0, 0, 0);
248 AffineTransform& contentTransformation = currentContentTransformation();
249 AffineTransform savedContentTransformation = contentTransformation;
250 contentTransformation = subtreeContentTransformation * contentTransformation;
252 ASSERT(!item->needsLayout());
253 item->paint(info, IntPoint());
255 contentTransformation = savedContentTransformation;
258 void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear)
263 FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform);
265 // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
266 // So the actual masking process has to be done in the absolute coordinate space as well.
267 context->concatCTM(absoluteTransform.inverse());
268 context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect);
269 context->concatCTM(absoluteTransform);
271 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
272 // resulting image buffer as the parent resource already caches the result.
273 if (safeToClear && !currentContentTransformation().isIdentity())
277 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
279 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
280 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
283 IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize)
285 const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
286 return absoluteSize.shrunkTo(maxImageBufferSize);
289 void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
291 AffineTransform::DecomposedType decomposition;
292 transform.decompose(decomposition);
293 decomposition.angle = 0;
294 transform.recompose(decomposition);