2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "SVGImageBufferTools.h"
25 #include "FrameView.h"
26 #include "GraphicsContext.h"
27 #include "RenderObject.h"
28 #include "RenderSVGContainer.h"
29 #include "RenderSVGRoot.h"
33 static AffineTransform& currentContentTransformation()
35 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
36 return s_currentContentTransformation;
39 void SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
41 const RenderObject* current = renderer;
44 absoluteTransform = currentContentTransformation();
46 absoluteTransform = current->localToParentTransform() * absoluteTransform;
47 if (current->isSVGRoot())
49 current = current->parent();
53 bool SVGImageBufferTools::createImageBuffer(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace)
55 IntSize imageSize(roundedImageBufferSize(clampedAbsoluteTargetRect.size()));
56 IntSize unclampedImageSize(SVGImageBufferTools::roundedImageBufferSize(absoluteTargetRect.size()));
58 // Don't create empty ImageBuffers.
59 if (imageSize.isEmpty())
62 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, colorSpace);
66 GraphicsContext* imageContext = image->context();
69 // Compensate rounding effects, as the absolute target rect is using floating-point numbers and the image buffer size is integer.
70 imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
72 imageBuffer = image.release();
76 void SVGImageBufferTools::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
80 ASSERT(image->context());
82 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, 0, 0, 0);
84 AffineTransform& contentTransformation = currentContentTransformation();
85 AffineTransform savedContentTransformation = contentTransformation;
86 contentTransformation = subtreeContentTransformation * contentTransformation;
88 item->layoutIfNeeded();
89 item->paint(info, IntPoint());
91 contentTransformation = savedContentTransformation;
94 void SVGImageBufferTools::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer)
99 // The mask image has been created in the absolute coordinate space, as the image should not be scaled.
100 // So the actual masking process has to be done in the absolute coordinate space as well.
101 context->concatCTM(absoluteTransform.inverse());
102 context->clipToImageBuffer(imageBuffer.get(), clampedAbsoluteTargetRect);
103 context->concatCTM(absoluteTransform);
105 // When nesting resources, with objectBoundingBox as content unit types, there's no use in caching the
106 // resulting image buffer as the parent resource already caches the result.
107 if (!currentContentTransformation().isIdentity())
111 IntSize SVGImageBufferTools::roundedImageBufferSize(const FloatSize& size)
113 return IntSize(static_cast<int>(lroundf(size.width())), static_cast<int>(lroundf(size.height())));
116 FloatRect SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(const RenderObject* renderer, const FloatRect& absoluteTargetRect)
120 const RenderSVGRoot* svgRoot = SVGRenderSupport::findTreeRootObject(renderer);
121 FloatRect clampedAbsoluteTargetRect = absoluteTargetRect;
122 clampedAbsoluteTargetRect.intersect(svgRoot->frameRect());
123 return clampedAbsoluteTargetRect;
126 void SVGImageBufferTools::clear2DRotation(AffineTransform& transform)
128 AffineTransform::DecomposedType decomposition;
129 transform.decompose(decomposition);
130 decomposition.angle = 0;
131 transform.recompose(decomposition);
136 #endif // ENABLE(SVG)