PDFDocumentImage.cpp [plain text]
#define _USE_MATH_DEFINES 1
#include "config.h"
#include "PDFDocumentImage.h"
#if PLATFORM(CG)
#include "GraphicsContext.h"
#include "ImageObserver.h"
#include <wtf/MathExtras.h>
using namespace std;
namespace WebCore {
PDFDocumentImage::PDFDocumentImage()
: Image(0) , m_document(0)
, m_rotation(0.0f)
, m_currentPage(-1)
{
}
PDFDocumentImage::~PDFDocumentImage()
{
CGPDFDocumentRelease(m_document);
}
IntSize PDFDocumentImage::size() const
{
const float sina = sinf(-m_rotation);
const float cosa = cosf(-m_rotation);
const float width = m_mediaBox.size().width();
const float height = m_mediaBox.size().height();
const float rotWidth = width * cosa - height * sina;
const float rotHeight = width * sina + height * cosa;
return IntSize((int)(fabsf(rotWidth) + 0.5f), (int)(fabsf(rotHeight) + 0.5f));
}
bool PDFDocumentImage::dataChanged(bool allDataReceived)
{
if (allDataReceived && !m_document) {
#if PLATFORM(MAC)
CFDataRef data = m_data->createCFData();
#else
CFDataRef data = CFDataCreateWithBytesNoCopy(0, reinterpret_cast<const UInt8*>(m_data->data()), m_data->size(), kCFAllocatorNull);
#endif
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data);
CFRelease(data);
m_document = CGPDFDocumentCreateWithProvider(dataProvider);
CGDataProviderRelease(dataProvider);
setCurrentPage(0);
}
return m_document; }
void PDFDocumentImage::adjustCTM(GraphicsContext* context) const
{
float sina = sinf(-m_rotation);
float cosa = cosf(-m_rotation);
float width = m_cropBox.width();
float height = m_cropBox.height();
CGPoint rx = CGPointMake(width * cosa, width * sina);
CGPoint ry = CGPointMake(-height * sina, height * cosa);
const CGFloat zero = 0;
CGContextTranslateCTM(context->platformContext(), floorf(-min(zero, min(rx.x, ry.x))), floorf(-min(zero, min(rx.y, ry.y))));
CGContextRotateCTM(context->platformContext(), -m_rotation);
CGContextTranslateCTM(context->platformContext(), m_mediaBox.x() - m_cropBox.x(), m_mediaBox.y() - m_cropBox.y());
}
void PDFDocumentImage::setCurrentPage(int page)
{
if (!m_document)
return;
if (page == m_currentPage)
return;
if (!(page >= 0 && page < pageCount()))
return;
m_currentPage = page;
CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document, page + 1);
m_mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
if (!CGRectIsEmpty(r))
m_cropBox = r;
else
m_cropBox = m_mediaBox;
m_rotation = CGPDFPageGetRotationAngle(cgPage) * piFloat / 180.0f; }
int PDFDocumentImage::pageCount() const
{
return m_document ? CGPDFDocumentGetNumberOfPages(m_document) : 0;
}
void PDFDocumentImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator op)
{
if (!m_document || m_currentPage == -1)
return;
context->save();
context->setCompositeOperation(op);
float hScale = dstRect.width() / srcRect.width();
float vScale = dstRect.height() / srcRect.height();
CGContextTranslateCTM(context->platformContext(), dstRect.x() - srcRect.x() * hScale, dstRect.y() - srcRect.y() * vScale);
CGContextScaleCTM(context->platformContext(), hScale, vScale);
CGContextScaleCTM(context->platformContext(), 1, -1);
CGContextTranslateCTM(context->platformContext(), 0, -srcRect.height());
CGContextClipToRect(context->platformContext(), CGRectIntegral(srcRect));
adjustCTM(context);
CGContextTranslateCTM(context->platformContext(), -m_mediaBox.x(), -m_mediaBox.y());
CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document, m_currentPage + 1));
context->restore();
if (imageObserver())
imageObserver()->didDraw(this);
}
}
#endif // PLATFORM(CG)