1 /* 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. 3 * 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. 8 * 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. 13 * 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. 18 */ 19 20 #include "config.h" 21 22 #if ENABLE(SVG) 23 #include "SVGImageBufferTools.h" 24 25 #include "FrameView.h" 26 #include "GraphicsContext.h" 27 #include "RenderObject.h" 28 #include "RenderSVGContainer.h" 29 #include "RenderSVGRoot.h" 30 31 namespace WebCore { 32 33 static AffineTransform& currentContentTransformation() 34 { 35 DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ()); 36 return s_currentContentTransformation; 37 } 38 39 void SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform) 40 { 41 const RenderObject* current = renderer; 42 ASSERT(current); 43 44 absoluteTransform = currentContentTransformation(); 45 while (current) { 46 absoluteTransform = current->localToParentTransform() * absoluteTransform; 47 if (current->isSVGRoot()) 48 break; 49 current = current->parent(); 50 } 51 } 52 53 bool SVGImageBufferTools::createImageBuffer(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace) 54 { 55 IntSize imageSize(roundedImageBufferSize(clampedAbsoluteTargetRect.size())); 56 IntSize unclampedImageSize(SVGImageBufferTools::roundedImageBufferSize(absoluteTargetRect.size())); 57 58 // Don't create empty ImageBuffers. 59 if (imageSize.isEmpty()) 60 return false; 61 62 OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, colorSpace); 63 if (!image) 64 return false; 65 66 GraphicsContext* imageContext = image->context(); 67 ASSERT(imageContext); 68 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())); 71 72 imageBuffer = image.release(); 73 return true; 74 } 75 76 void SVGImageBufferTools::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation) 77 { 78 ASSERT(item); 79 ASSERT(image); 80 ASSERT(image->context()); 81 82 PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, 0, 0, 0); 83 84 AffineTransform& contentTransformation = currentContentTransformation(); 85 AffineTransform savedContentTransformation = contentTransformation; 86 contentTransformation = subtreeContentTransformation * contentTransformation; 87 88 item->layoutIfNeeded(); 89 item->paint(info, 0, 0); 90 91 contentTransformation = savedContentTransformation; 92 } 93 94 void SVGImageBufferTools::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer) 95 { 96 ASSERT(context); 97 ASSERT(imageBuffer); 98 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); 104 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()) 108 imageBuffer.clear(); 109 } 110 111 IntSize SVGImageBufferTools::roundedImageBufferSize(const FloatSize& size) 112 { 113 return IntSize(static_cast<int>(lroundf(size.width())), static_cast<int>(lroundf(size.height()))); 114 } 115 116 FloatRect SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(const RenderObject* renderer, const FloatRect& absoluteTargetRect) 117 { 118 ASSERT(renderer); 119 120 const RenderSVGRoot* svgRoot = SVGRenderSupport::findTreeRootObject(renderer); 121 FloatRect clampedAbsoluteTargetRect = absoluteTargetRect; 122 clampedAbsoluteTargetRect.intersect(svgRoot->contentBoxRect()); 123 return clampedAbsoluteTargetRect; 124 } 125 126 } 127 128 #endif // ENABLE(SVG) 129