1 /*
2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #define _USE_MATH_DEFINES 1
27 #include "config.h"
28 #include "PDFDocumentImage.h"
29
30 #if USE(CG)
31
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>
39
40 #if !PLATFORM(MAC)
41 #include "ImageSourceCG.h"
42 #endif
43
44 using namespace std;
45
46 namespace WebCore {
47
PDFDocumentImage()48 PDFDocumentImage::PDFDocumentImage()
49 : Image(0) // PDFs don't animate
50 , m_document(0)
51 , m_rotation(0.0f)
52 , m_currentPage(-1)
53 {
54 }
55
~PDFDocumentImage()56 PDFDocumentImage::~PDFDocumentImage()
57 {
58 CGPDFDocumentRelease(m_document);
59 }
60
filenameExtension() const61 String PDFDocumentImage::filenameExtension() const
62 {
63 return "pdf";
64 }
65
size() const66 IntSize PDFDocumentImage::size() const
67 {
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;
74
75 return IntSize((int)(fabsf(rotWidth) + 0.5f), (int)(fabsf(rotHeight) + 0.5f));
76 }
77
dataChanged(bool allDataReceived)78 bool PDFDocumentImage::dataChanged(bool allDataReceived)
79 {
80 if (allDataReceived && !m_document) {
81 #if PLATFORM(MAC)
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()));
86 #else
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));
93 #endif
94 m_document = CGPDFDocumentCreateWithProvider(dataProvider.get());
95 setCurrentPage(0);
96 }
97 return m_document; // return true if size is available
98 }
99
adjustCTM(GraphicsContext * context) const100 void PDFDocumentImage::adjustCTM(GraphicsContext* context) const
101 {
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();
107
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);
112
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))));
116
117 // rotate -ve to remove rotation
118 CGContextRotateCTM(context->platformContext(), -m_rotation);
119
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());
122 }
123
setCurrentPage(int page)124 void PDFDocumentImage::setCurrentPage(int page)
125 {
126 if (!m_document)
127 return;
128
129 if (page == m_currentPage)
130 return;
131
132 if (!(page >= 0 && page < pageCount()))
133 return;
134
135 m_currentPage = page;
136
137 CGPDFPageRef cgPage = CGPDFDocumentGetPage(m_document, page + 1);
138
139 // get media box (guaranteed)
140 m_mediaBox = CGPDFPageGetBoxRect(cgPage, kCGPDFMediaBox);
141
142 // get crop box (not always there). if not, use media box
143 CGRect r = CGPDFPageGetBoxRect(cgPage, kCGPDFCropBox);
144 if (!CGRectIsEmpty(r))
145 m_cropBox = r;
146 else
147 m_cropBox = m_mediaBox;
148
149 // get page rotation angle
150 m_rotation = CGPDFPageGetRotationAngle(cgPage) * piFloat / 180.0f; // to radians
151 }
152
pageCount() const153 int PDFDocumentImage::pageCount() const
154 {
155 return m_document ? CGPDFDocumentGetNumberOfPages(m_document) : 0;
156 }
157
draw(GraphicsContext * context,const FloatRect & dstRect,const FloatRect & srcRect,ColorSpace,CompositeOperator op)158 void PDFDocumentImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace, CompositeOperator op)
159 {
160 if (!m_document || m_currentPage == -1)
161 return;
162
163 {
164 GraphicsContextStateSaver stateSaver(*context);
165
166 context->setCompositeOperation(op);
167
168 float hScale = dstRect.width() / srcRect.width();
169 float vScale = dstRect.height() / srcRect.height();
170
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));
179
180 // Rotate translate image into position according to doc properties.
181 adjustCTM(context);
182
183 CGContextTranslateCTM(context->platformContext(), -m_mediaBox.x(), -m_mediaBox.y());
184 CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document, m_currentPage + 1));
185 }
186
187 if (imageObserver())
188 imageObserver()->didDraw(this);
189 }
190
191 }
192
193 #endif // USE(CG)
194