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"
30 #include "BasicShapes.h"
32 #include "FrameView.h"
33 #include "RenderSVGResource.h"
34 #include "RenderSVGResourceClipper.h"
35 #include "RenderSVGResourceFilter.h"
36 #include "RenderSVGResourceMasker.h"
37 #include "SVGResources.h"
38 #include "SVGResourcesCache.h"
40 static int kMaxImageBufferSize = 4096;
44 static inline bool isRenderingMaskImage(RenderObject* object)
46 if (object->frame() && object->frame()->view())
47 return object->frame()->view()->paintBehavior() & PaintBehaviorRenderingSVGMask;
51 SVGRenderingContext::~SVGRenderingContext()
53 // Fast path if we don't need to restore anything.
54 if (!(m_renderingFlags & ActionsNeeded))
57 ASSERT(m_object && m_paintInfo);
60 if (m_renderingFlags & EndFilterLayer) {
62 m_filter->postApplyResource(static_cast<RenderSVGShape*>(m_object), m_paintInfo->context, ApplyToDefaultMode, 0, 0);
63 m_paintInfo->context = m_savedContext;
67 if (m_renderingFlags & EndOpacityLayer)
68 m_paintInfo->context->endTransparencyLayer();
70 if (m_renderingFlags & EndShadowLayer)
71 m_paintInfo->context->endTransparencyLayer();
73 if (m_renderingFlags & RestoreGraphicsContext)
74 m_paintInfo->context->restore();
77 void SVGRenderingContext::prepareToRenderSVGContent(RenderObject* object, PaintInfo& paintInfo, NeedsGraphicsContextSave needsGraphicsContextSave)
82 // This function must not be called twice!
83 ASSERT(!(m_renderingFlags & PrepareToRenderSVGContentWasCalled));
84 m_renderingFlags |= PrepareToRenderSVGContentWasCalled;
88 m_paintInfo = &paintInfo;
93 // We need to save / restore the context even if the initialization failed.
94 if (needsGraphicsContextSave == SaveGraphicsContext) {
95 m_paintInfo->context->save();
96 m_renderingFlags |= RestoreGraphicsContext;
99 RenderStyle* style = m_object->style();
102 const SVGRenderStyle* svgStyle = style->svgStyle();
105 // Setup transparency layers before setting up SVG resources!
106 bool isRenderingMask = isRenderingMaskImage(m_object);
107 float opacity = isRenderingMask ? 1 : style->opacity();
108 const ShadowData* shadow = svgStyle->shadow();
109 if (opacity < 1 || shadow) {
110 FloatRect repaintRect = m_object->repaintRectInLocalCoordinates();
113 m_paintInfo->context->clip(repaintRect);
114 m_paintInfo->context->beginTransparencyLayer(opacity);
115 m_renderingFlags |= EndOpacityLayer;
119 m_paintInfo->context->clip(repaintRect);
120 m_paintInfo->context->setShadow(IntSize(roundToInt(shadow->x()), roundToInt(shadow->y())), shadow->blur(), shadow->color(), style->colorSpace());
121 m_paintInfo->context->beginTransparencyLayer(1);
122 m_renderingFlags |= EndShadowLayer;
126 ClipPathOperation* clipPathOperation = style->clipPath();
127 if (clipPathOperation && clipPathOperation->getOperationType() == ClipPathOperation::SHAPE) {
128 ShapeClipPathOperation* clipPath = static_cast<ShapeClipPathOperation*>(clipPathOperation);
129 m_paintInfo->context->clipPath(clipPath->path(object->objectBoundingBox()), clipPath->windRule());
132 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(m_object);
135 if (svgStyle->hasFilter())
138 m_renderingFlags |= RenderingPrepared;
142 if (!isRenderingMask) {
143 if (RenderSVGResourceMasker* masker = resources->masker()) {
144 if (!masker->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
149 RenderSVGResourceClipper* clipper = resources->clipper();
150 if (!clipPathOperation && clipper) {
151 if (!clipper->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
156 if (!isRenderingMask) {
157 m_filter = resources->filter();
159 m_savedContext = m_paintInfo->context;
160 // Return with false here may mean that we don't need to draw the content
161 // (because it was either drawn before or empty) but we still need to apply the filter.
162 m_renderingFlags |= EndFilterLayer;
163 if (!m_filter->applyResource(m_object, style, m_paintInfo->context, ApplyToDefaultMode))
169 m_renderingFlags |= RenderingPrepared;
172 static AffineTransform& currentContentTransformation()
174 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
175 return s_currentContentTransformation;
178 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject* renderer)
183 calculateTransformationToOutermostSVGCoordinateSystem(renderer, ctm);
184 return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
187 void SVGRenderingContext::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
189 const RenderObject* current = renderer;
192 absoluteTransform = currentContentTransformation();
194 absoluteTransform = current->localToParentTransform() * absoluteTransform;
195 if (current->isSVGRoot())
197 current = current->parent();
201 bool SVGRenderingContext::createImageBuffer(const FloatRect& targetRect, const AffineTransform& absoluteTransform, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
203 IntRect paintRect = calculateImageBufferRect(targetRect, absoluteTransform);
204 // Don't create empty ImageBuffers.
205 if (paintRect.isEmpty())
208 IntSize clampedSize = clampedAbsoluteSize(paintRect.size());
209 OwnPtr<ImageBuffer> image = ImageBuffer::create(clampedSize, 1, colorSpace, renderingMode);
213 GraphicsContext* imageContext = image->context();
214 ASSERT(imageContext);
216 imageContext->scale(FloatSize(static_cast<float>(clampedSize.width()) / paintRect.width(),
217 static_cast<float>(clampedSize.height()) / paintRect.height()));
218 imageContext->translate(-paintRect.x(), -paintRect.y());
219 imageContext->concatCTM(absoluteTransform);
221 imageBuffer = image.release();
225 bool SVGRenderingContext::createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace, RenderingMode renderingMode)
227 IntSize imageSize(roundedIntSize(clampedAbsoluteTargetRect.size()));
228 IntSize unclampedImageSize(roundedIntSize(absoluteTargetRect.size()));
230 // Don't create empty ImageBuffers.
231 if (imageSize.isEmpty())
234 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, 1, colorSpace, renderingMode);
238 GraphicsContext* imageContext = image->context();
239 ASSERT(imageContext);
241 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
242 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
244 imageBuffer = image.release();
248 void SVGRenderingContext::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
252 ASSERT(image->context());
254 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, PaintBehaviorNormal);
256 AffineTransform& contentTransformation = currentContentTransformation();
257 AffineTransform savedContentTransformation = contentTransformation;
258 contentTransformation = subtreeContentTransformation * contentTransformation;
260 ASSERT(!item->needsLayout());
261 item->paint(info, IntPoint());
263 contentTransformation = savedContentTransformation;
266 void SVGRenderingContext::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& targetRect, OwnPtr<ImageBuffer>& imageBuffer, bool safeToClear)
271 FloatRect absoluteTargetRect = calculateImageBufferRect(targetRect, absoluteTransform);
273 // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
274 // So the actual masking process has to be done in the absolute coordinate space as well.
275 context->concatCTM(absoluteTransform.inverse());
276 context->clipToImageBuffer(imageBuffer.get(), absoluteTargetRect);
277 context->concatCTM(absoluteTransform);
279 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
280 // resulting image buffer as the parent resource already caches the result.
281 if (safeToClear && !currentContentTransformation().isIdentity())
285 FloatRect SVGRenderingContext::clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect)
287 const FloatSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
288 return FloatRect(absoluteTargetRect.location(), absoluteTargetRect.size().shrunkTo(maxImageBufferSize));
291 IntSize SVGRenderingContext::clampedAbsoluteSize(const IntSize& absoluteSize)
293 const IntSize maxImageBufferSize(kMaxImageBufferSize, kMaxImageBufferSize);
294 return absoluteSize.shrunkTo(maxImageBufferSize);
297 void SVGRenderingContext::clear2DRotation(AffineTransform& transform)
299 AffineTransform::DecomposedType decomposition;
300 transform.decompose(decomposition);
301 decomposition.angle = 0;
302 transform.recompose(decomposition);