PDFDocumentImage.cpp [plain text]
#include "config.h"
#include "PDFDocumentImage.h"
#if USE(CG)
#if PLATFORM(IOS)
#include <CoreGraphics/CoreGraphics.h>
#include <ImageIO/ImageIO.h>
#endif
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "ImageObserver.h"
#include "IntRect.h"
#include "Length.h"
#include "SharedBuffer.h"
#include "TextStream.h"
#include <CoreGraphics/CGContext.h>
#include <CoreGraphics/CGPDFDocument.h>
#include <wtf/MathExtras.h>
#include <wtf/RAMSize.h>
#include <wtf/RetainPtr.h>
#include <wtf/StdLibExtras.h>
#if !PLATFORM(COCOA)
#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";
}
FloatSize PDFDocumentImage::size() const
{
FloatSize expandedCropBoxSize = FloatSize(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; }
void PDFDocumentImage::setCachedPDFImageEnabled(bool enabled)
{
if (m_isCachedPDFImageEnabled == enabled)
return;
if (!(m_isCachedPDFImageEnabled = enabled))
destroyDecodedData();
}
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;
if (!m_cachedImageRect.contains(context.clipBounds()))
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(dstRect.x() - srcRect.x(), dstRect.y() - srcRect.y());
context.scale(FloatSize(hScale, -vScale));
context.translate(0, -srcRect.height());
}
#if PLATFORM(IOS)
static const size_t s_maxCachedImageSide = 4 * 1024;
static const size_t s_maxCachedImageArea = s_maxCachedImageSide * s_maxCachedImageSide;
static const size_t s_maxDecodedDataSize = s_maxCachedImageArea * 4;
static size_t s_allDecodedDataSize = 0;
static FloatRect cachedImageRect(GraphicsContext& context, const FloatRect& dstRect)
{
FloatSize maxSize = s_maxCachedImageSide / context.scaleFactor();
FloatRect rect = context.clipBounds();
rect.setX(std::max(rect.center().x() - maxSize.width() / 2, dstRect.x()));
rect.setY(std::max(rect.center().y() - maxSize.height() / 2, dstRect.y()));
rect.setWidth(std::min(maxSize.width(), dstRect.width()));
rect.setHeight(std::min(maxSize.height(), dstRect.height()));
return rect;
}
#endif
void PDFDocumentImage::decodedSizeChanged(size_t newCachedBytes)
{
if (!m_cachedBytes && !newCachedBytes)
return;
if (imageObserver())
imageObserver()->decodedSizeChanged(this, -safeCast<int>(m_cachedBytes) + newCachedBytes);
#if PLATFORM(IOS)
ASSERT(s_allDecodedDataSize >= m_cachedBytes);
s_allDecodedDataSize -= m_cachedBytes;
s_allDecodedDataSize += newCachedBytes;
#endif
m_cachedBytes = newCachedBytes;
}
void PDFDocumentImage::updateCachedImageIfNeeded(GraphicsContext& context, const FloatRect& dstRect, const FloatRect& srcRect)
{
#if PLATFORM(IOS)
bool repaintIfNecessary = true;
#else
InterpolationQuality interpolationQuality = context.imageInterpolationQuality();
bool repaintIfNecessary = interpolationQuality != InterpolationNone && interpolationQuality != InterpolationLow;
#endif
if (!m_isCachedPDFImageEnabled || (m_cachedImageBuffer && (!repaintIfNecessary || cacheParametersMatch(context, dstRect, srcRect))))
return;
#if PLATFORM(IOS)
m_cachedImageRect = cachedImageRect(context, dstRect);
#else
m_cachedImageRect = dstRect;
#endif
FloatSize cachedImageSize = FloatRect(enclosingIntRect(m_cachedImageRect)).size();
#if PLATFORM(IOS)
IntSize scaledSize = ImageBuffer::compatibleBufferSize(cachedImageSize, context);
if (s_allDecodedDataSize + safeCast<size_t>(scaledSize.width()) * scaledSize.height() * 4 - m_cachedBytes > s_maxDecodedDataSize) {
destroyDecodedData();
return;
}
#endif
m_cachedImageBuffer = ImageBuffer::createCompatibleBuffer(cachedImageSize, context);
if (!m_cachedImageBuffer) {
destroyDecodedData();
return;
}
auto& bufferContext = m_cachedImageBuffer->context();
transformContextForPainting(bufferContext, FloatRect({ }, dstRect.size()), srcRect);
drawPDFPage(bufferContext);
m_cachedTransform = context.getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
m_cachedDestinationSize = dstRect.size();
m_cachedSourceRect = srcRect;
IntSize internalSize = m_cachedImageBuffer->internalSize();
decodedSizeChanged(safeCast<size_t>(internalSize.width()) * internalSize.height() * 4);
}
void PDFDocumentImage::draw(GraphicsContext& context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator op, BlendMode, ImageOrientationDescription)
{
if (!m_document || !m_hasPage)
return;
updateCachedImageIfNeeded(context, dstRect, srcRect);
{
GraphicsContextStateSaver stateSaver(context);
context.setCompositeOperation(op);
if (m_cachedImageBuffer)
context.drawImageBuffer(*m_cachedImageBuffer, dstRect);
else {
transformContextForPainting(context, dstRect, srcRect);
drawPDFPage(context);
}
}
if (imageObserver())
imageObserver()->didDraw(this);
}
void PDFDocumentImage::destroyDecodedData(bool)
{
m_cachedImageBuffer = nullptr;
m_cachedImageRect = FloatRect();
decodedSizeChanged(0);
}
#if !USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
void PDFDocumentImage::createPDFDocument()
{
RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateWithCFData(data()->createCFData().get()));
m_document = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));
}
void PDFDocumentImage::computeBoundsForCurrentPage()
{
ASSERT(pageCount() > 0);
CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document.get(), 1);
CGRect mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
if (!CGRectIsEmpty(r))
m_cropBox = r;
else
m_cropBox = mediaBox;
m_rotationDegrees = CGPDFPageGetRotationAngle(cgPage);
}
unsigned PDFDocumentImage::pageCount() const
{
return CGPDFDocumentGetNumberOfPages(m_document.get());
}
static void applyRotationForPainting(GraphicsContext& context, FloatSize 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)
void PDFDocumentImage::dump(TextStream& ts) const
{
Image::dump(ts);
ts.dumpProperty("page-count", pageCount());
ts.dumpProperty("crop-box", m_cropBox);
if (m_rotationDegrees)
ts.dumpProperty("rotation", m_rotationDegrees);
}
}
#endif // USE(CG)