SVGImageBufferTools.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGImageBufferTools.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "RenderObject.h"
#include "RenderSVGContainer.h"
#include "RenderSVGRoot.h"
namespace WebCore {
static AffineTransform& currentContentTransformation()
{
DEFINE_STATIC_LOCAL(AffineTransform, s_currentContentTransformation, ());
return s_currentContentTransformation;
}
void SVGImageBufferTools::calculateTransformationToOutermostSVGCoordinateSystem(const RenderObject* renderer, AffineTransform& absoluteTransform)
{
const RenderObject* current = renderer;
ASSERT(current);
absoluteTransform = currentContentTransformation();
while (current) {
absoluteTransform = current->localToParentTransform() * absoluteTransform;
if (current->isSVGRoot())
break;
current = current->parent();
}
}
bool SVGImageBufferTools::createImageBuffer(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer, ColorSpace colorSpace)
{
IntSize imageSize(roundedImageBufferSize(clampedAbsoluteTargetRect.size()));
IntSize unclampedImageSize(SVGImageBufferTools::roundedImageBufferSize(absoluteTargetRect.size()));
if (imageSize.isEmpty())
return false;
OwnPtr<ImageBuffer> image = ImageBuffer::create(imageSize, colorSpace);
if (!image)
return false;
GraphicsContext* imageContext = image->context();
ASSERT(imageContext);
imageContext->scale(FloatSize(unclampedImageSize.width() / absoluteTargetRect.width(), unclampedImageSize.height() / absoluteTargetRect.height()));
imageBuffer = image.release();
return true;
}
void SVGImageBufferTools::renderSubtreeToImageBuffer(ImageBuffer* image, RenderObject* item, const AffineTransform& subtreeContentTransformation)
{
ASSERT(item);
ASSERT(image);
ASSERT(image->context());
PaintInfo info(image->context(), PaintInfo::infiniteRect(), PaintPhaseForeground, 0, 0, 0);
AffineTransform& contentTransformation = currentContentTransformation();
AffineTransform savedContentTransformation = contentTransformation;
contentTransformation = subtreeContentTransformation * contentTransformation;
item->layoutIfNeeded();
item->paint(info, 0, 0);
contentTransformation = savedContentTransformation;
}
void SVGImageBufferTools::clipToImageBuffer(GraphicsContext* context, const AffineTransform& absoluteTransform, const FloatRect& clampedAbsoluteTargetRect, OwnPtr<ImageBuffer>& imageBuffer)
{
ASSERT(context);
ASSERT(imageBuffer);
context->concatCTM(absoluteTransform.inverse());
context->clipToImageBuffer(imageBuffer.get(), clampedAbsoluteTargetRect);
context->concatCTM(absoluteTransform);
if (!currentContentTransformation().isIdentity())
imageBuffer.clear();
}
IntSize SVGImageBufferTools::roundedImageBufferSize(const FloatSize& size)
{
return IntSize(static_cast<int>(lroundf(size.width())), static_cast<int>(lroundf(size.height())));
}
FloatRect SVGImageBufferTools::clampedAbsoluteTargetRectForRenderer(const RenderObject* renderer, const FloatRect& absoluteTargetRect)
{
ASSERT(renderer);
const RenderSVGRoot* svgRoot = SVGRenderSupport::findTreeRootObject(renderer);
FloatRect clampedAbsoluteTargetRect = absoluteTargetRect;
clampedAbsoluteTargetRect.intersect(svgRoot->frameRect());
return clampedAbsoluteTargetRect;
}
void SVGImageBufferTools::clear2DRotation(AffineTransform& transform)
{
TransformationMatrix::DecomposedType decomposition;
TransformationMatrix tm = transform.toTransformationMatrix();
tm.decompose(decomposition);
decomposition.quaternionZ = 0.0;
tm.recompose(decomposition);
transform = tm.toAffineTransform();
}
}
#endif // ENABLE(SVG)