PDFDocumentImage.cpp [plain text]
#define _USE_MATH_DEFINES 1
#include "config.h"
#include "PDFDocumentImage.h"
#if USE(CG)
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "ImageObserver.h"
#include "Length.h"
#include "SharedBuffer.h"
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGPDFDocument.h>
#include <wtf/MathExtras.h>
#include <wtf/RetainPtr.h>
#if !PLATFORM(MAC)
#include "ImageSourceCG.h"
#endif
namespace WebCore {
PDFDocumentImage::PDFDocumentImage(ImageObserver* observer)
: Image(observer)
, m_cachedBytes(0)
, m_rotationDegrees(0)
, m_hasPage(false)
{
}
PDFDocumentImage::~PDFDocumentImage()
{
}
String PDFDocumentImage::filenameExtension() const
{
return "pdf";
}
IntSize PDFDocumentImage::size() const
{
IntSize expandedCropBoxSize = expandedIntSize(m_cropBox.size());
if (m_rotationDegrees == 90 || m_rotationDegrees == 270)
return expandedCropBoxSize.transposedSize();
return expandedCropBoxSize;
}
void PDFDocumentImage::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
{
Image::computeIntrinsicDimensions(intrinsicWidth, intrinsicHeight, intrinsicRatio);
intrinsicRatio = FloatSize();
}
bool PDFDocumentImage::dataChanged(bool allDataReceived)
{
ASSERT(!m_document);
if (allDataReceived && !m_document) {
createPDFDocument();
if (pageCount()) {
m_hasPage = true;
computeBoundsForCurrentPage();
}
}
return m_document; }
bool PDFDocumentImage::cacheParametersMatch(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect) const
{
if (dstRect.size() != m_cachedDestinationSize)
return false;
if (srcRect != m_cachedSourceRect)
return false;
AffineTransform::DecomposedType decomposedTransform;
context->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale).decompose(decomposedTransform);
AffineTransform::DecomposedType cachedDecomposedTransform;
m_cachedTransform.decompose(cachedDecomposedTransform);
if (decomposedTransform.scaleX != cachedDecomposedTransform.scaleX || decomposedTransform.scaleY != cachedDecomposedTransform.scaleY)
return false;
return true;
}
static void transformContextForPainting(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect)
{
float hScale = dstRect.width() / srcRect.width();
float vScale = dstRect.height() / srcRect.height();
if (hScale != vScale) {
float minimumScale = std::max((dstRect.width() - 0.5) / srcRect.width(), (dstRect.height() - 0.5) / srcRect.height());
float maximumScale = std::min((dstRect.width() + 0.5) / srcRect.width(), (dstRect.height() + 0.5) / srcRect.height());
if (minimumScale <= maximumScale) {
hScale = std::min(hScale, vScale);
vScale = hScale;
}
}
context->translate(srcRect.x() * hScale, srcRect.y() * vScale);
context->scale(FloatSize(hScale, -vScale));
context->translate(0, -srcRect.height());
}
void PDFDocumentImage::updateCachedImageIfNeeded(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect)
{
InterpolationQuality interpolationQuality = context->imageInterpolationQuality();
bool useLowQualityInterpolation = interpolationQuality == InterpolationNone || interpolationQuality == InterpolationLow;
if (!m_cachedImageBuffer || (!cacheParametersMatch(context, dstRect, srcRect) && !useLowQualityInterpolation)) {
m_cachedImageBuffer = context->createCompatibleBuffer(enclosingIntRect(dstRect).size());
if (!m_cachedImageBuffer)
return;
GraphicsContext* bufferContext = m_cachedImageBuffer->context();
if (!bufferContext) {
m_cachedImageBuffer = nullptr;
return;
}
transformContextForPainting(bufferContext, dstRect, srcRect);
drawPDFPage(bufferContext);
m_cachedTransform = context->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
m_cachedDestinationSize = dstRect.size();
m_cachedSourceRect = srcRect;
IntSize internalSize = m_cachedImageBuffer->internalSize();
size_t oldCachedBytes = m_cachedBytes;
m_cachedBytes = internalSize.width() * internalSize.height() * 4;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, safeCast<int>(m_cachedBytes) - safeCast<int>(oldCachedBytes));
}
}
void PDFDocumentImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace, CompositeOperator op, BlendMode)
{
if (!m_document || !m_hasPage)
return;
updateCachedImageIfNeeded(context, dstRect, srcRect);
{
GraphicsContextStateSaver stateSaver(*context);
context->setCompositeOperation(op);
if (m_cachedImageBuffer)
context->drawImageBuffer(m_cachedImageBuffer.get(), ColorSpaceDeviceRGB, dstRect);
else {
transformContextForPainting(context, dstRect, srcRect);
drawPDFPage(context);
}
}
if (imageObserver())
imageObserver()->didDraw(this);
}
void PDFDocumentImage::destroyDecodedData(bool)
{
m_cachedImageBuffer = nullptr;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, -safeCast<int>(m_cachedBytes));
m_cachedBytes = 0;
}
unsigned PDFDocumentImage::decodedSize() const
{
return m_cachedBytes;
}
#if !USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
void PDFDocumentImage::createPDFDocument()
{
RetainPtr<CFDataRef> data = adoptCF(this->data()->createCFData());
RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateWithCFData(data.get()));
m_document = CGPDFDocumentCreateWithProvider(dataProvider.get());
}
void PDFDocumentImage::computeBoundsForCurrentPage()
{
CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document.get(), 1);
m_mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
if (!CGRectIsEmpty(r))
m_cropBox = r;
else
m_cropBox = m_mediaBox;
m_rotationDegrees = CGPDFPageGetRotationAngle(cgPage);
}
unsigned PDFDocumentImage::pageCount() const
{
return CGPDFDocumentGetNumberOfPages(m_document.get());
}
static void applyRotationForPainting(GraphicsContext* context, IntSize size, int rotationDegrees)
{
if (rotationDegrees == 90)
context->translate(0, size.height());
else if (rotationDegrees == 180)
context->translate(size.width(), size.height());
else if (rotationDegrees == 270)
context->translate(size.width(), 0);
context->rotate(-deg2rad(static_cast<float>(rotationDegrees)));
}
void PDFDocumentImage::drawPDFPage(GraphicsContext* context)
{
applyRotationForPainting(context, size(), m_rotationDegrees);
context->translate(-m_cropBox.x(), -m_cropBox.y());
CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document.get(), 1));
}
#endif // !USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
}
#endif // USE(CG)