2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #define _USE_MATH_DEFINES 1
28 #include "PDFDocumentImage.h"
32 #include "GraphicsContext.h"
33 #include "ImageObserver.h"
34 #include "SharedBuffer.h"
35 #include <CoreGraphics/CGContext.h>
36 #include <CoreGraphics/CGPDFDocument.h>
37 #include <wtf/MathExtras.h>
38 #include <wtf/RetainPtr.h>
41 #include "ImageSourceCG.h"
48 PDFDocumentImage::PDFDocumentImage()
49 : Image(0) // PDFs don't animate
56 PDFDocumentImage::~PDFDocumentImage()
58 CGPDFDocumentRelease(m_document);
61 String PDFDocumentImage::filenameExtension() const
66 IntSize PDFDocumentImage::size() const
68 const float sina = sinf(-m_rotation);
69 const float cosa = cosf(-m_rotation);
70 const float width = m_mediaBox.size().width();
71 const float height = m_mediaBox.size().height();
72 const float rotWidth = width * cosa - height * sina;
73 const float rotHeight = width * sina + height * cosa;
75 return IntSize((int)(fabsf(rotWidth) + 0.5f), (int)(fabsf(rotHeight) + 0.5f));
78 bool PDFDocumentImage::dataChanged(bool allDataReceived)
80 if (allDataReceived && !m_document) {
82 // On Mac the NSData inside the SharedBuffer can be secretly appended to without the SharedBuffer's knowledge. We use SharedBuffer's ability
83 // to wrap itself inside CFData to get around this, ensuring that ImageIO is really looking at the SharedBuffer.
84 RetainPtr<CFDataRef> data(AdoptCF, this->data()->createCFData());
85 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateWithCFData(data.get()));
87 // Create a CGDataProvider to wrap the SharedBuffer.
88 // We use the GetBytesAtPosition callback rather than the GetBytePointer one because SharedBuffer
89 // does not provide a way to lock down the byte pointer and guarantee that it won't move, which
90 // is a requirement for using the GetBytePointer callback.
91 CGDataProviderDirectCallbacks providerCallbacks = { 0, 0, 0, sharedBufferGetBytesAtPosition, 0 };
92 RetainPtr<CGDataProviderRef> dataProvider(AdoptCF, CGDataProviderCreateDirect(this->data(), this->data()->size(), &providerCallbacks));
94 m_document = CGPDFDocumentCreateWithProvider(dataProvider.get());
97 return m_document; // return true if size is available
100 void PDFDocumentImage::adjustCTM(GraphicsContext* context) const
102 // rotate the crop box and calculate bounding box
103 float sina = sinf(-m_rotation);
104 float cosa = cosf(-m_rotation);
105 float width = m_cropBox.width();
106 float height = m_cropBox.height();
108 // calculate rotated x and y edges of the corp box. if they're negative, it means part of the image has
109 // been rotated outside of the bounds and we need to shift over the image so it lies inside the bounds again
110 CGPoint rx = CGPointMake(width * cosa, width * sina);
111 CGPoint ry = CGPointMake(-height * sina, height * cosa);
113 // adjust so we are at the crop box origin
114 const CGFloat zero = 0;
115 CGContextTranslateCTM(context->platformContext(), floorf(-min(zero, min(rx.x, ry.x))), floorf(-min(zero, min(rx.y, ry.y))));
117 // rotate -ve to remove rotation
118 CGContextRotateCTM(context->platformContext(), -m_rotation);
120 // shift so we are completely within media box
121 CGContextTranslateCTM(context->platformContext(), m_mediaBox.x() - m_cropBox.x(), m_mediaBox.y() - m_cropBox.y());
124 void PDFDocumentImage::setCurrentPage(int page)
129 if (page == m_currentPage)
132 if (!(page >= 0 && page < pageCount()))
135 m_currentPage = page;
137 CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document, page + 1);
139 // get media box (guaranteed)
140 m_mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
142 // get crop box (not always there). if not, use media box
143 CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
144 if (!CGRectIsEmpty(r))
147 m_cropBox = m_mediaBox;
149 // get page rotation angle
150 m_rotation = CGPDFPageGetRotationAngle(cgPage) * piFloat / 180.0f; // to radians
153 int PDFDocumentImage::pageCount() const
155 return m_document ? CGPDFDocumentGetNumberOfPages(m_document) : 0;
158 void PDFDocumentImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace, CompositeOperator op)
160 if (!m_document || m_currentPage == -1)
164 GraphicsContextStateSaver stateSaver(*context);
166 context->setCompositeOperation(op);
168 float hScale = dstRect.width() / srcRect.width();
169 float vScale = dstRect.height() / srcRect.height();
171 // Scale and translate so the document is rendered in the correct location,
172 // including accounting for the fact that a GraphicsContext is always flipped
173 // and doing appropriate flipping.
174 CGContextTranslateCTM(context->platformContext(), dstRect.x() - srcRect.x() * hScale, dstRect.y() - srcRect.y() * vScale);
175 CGContextScaleCTM(context->platformContext(), hScale, vScale);
176 CGContextScaleCTM(context->platformContext(), 1, -1);
177 CGContextTranslateCTM(context->platformContext(), 0, -srcRect.height());
178 CGContextClipToRect(context->platformContext(), CGRectIntegral(srcRect));
180 // Rotate translate image into position according to doc properties.
183 CGContextTranslateCTM(context->platformContext(), -m_mediaBox.x(), -m_mediaBox.y());
184 CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document, m_currentPage + 1));
188 imageObserver()->didDraw(this);