PDFDocumentImage.cpp [plain text]
#define _USE_MATH_DEFINES 1
#include "config.h"
#include "PDFDocumentImage.h"
#if USE(CG)
#import <CoreGraphics/CGContextPrivate.h>
#import <CoreGraphics/CGContextGState.h>
#import <CoreGraphics/CoreGraphics.h>
#import <ImageIO/ImageIO.h>
#include "GraphicsContext.h"
#include "ImageObserver.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
using namespace std;
namespace WebCore {
PDFDocumentImage::PDFDocumentImage()
: Image(0) , m_document(0)
, m_rotation(0.0f)
, m_currentPage(-1)
{
}
PDFDocumentImage::~PDFDocumentImage()
{
CGPDFDocumentRelease(m_document);
}
String PDFDocumentImage::filenameExtension() const
{
return "pdf";
}
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)
RetainPtr<CFDataRef> data(AdoptCF, this->data()->createCFData());
RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(data.get()));
#else
CGDataProviderDirectCallbacks providerCallbacks = { 0, 0, 0, sharedBufferGetBytesAtPosition, 0 };
RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateDirect(this->data(), this->data()->size(), &providerCallbacks));
#endif
m_document = CGPDFDocumentCreateWithProvider(dataProvider.get());
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, ColorSpace, CompositeOperator op)
{
if (!m_document || m_currentPage == -1)
return;
{
GraphicsContextStateSaver stateSaver(*context);
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));
}
if (imageObserver())
imageObserver()->didDraw(this);
}
}
#endif // USE(CG)