blob: 16744bfde6c702ab27c3e3fa91df1cde8bd6148f [file] [log] [blame]
[email protected]9fc44162012-01-23 22:56:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]14b210792009-10-12 18:45:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]9b617532011-04-04 23:15:065#include "printing/pdf_metafile_cg_mac.h"
[email protected]14b210792009-10-12 18:45:316
avi126e93c2015-12-21 21:48:167#include <stdint.h>
8
[email protected]b5cf844c2012-06-18 21:49:209#include <algorithm>
10
[email protected]14b210792009-10-12 18:45:3111#include "base/logging.h"
[email protected]ba64e2b2011-06-14 18:18:3812#include "base/mac/mac_util.h"
[email protected]df0ca6c82010-10-17 04:09:0613#include "base/mac/scoped_cftyperef.h"
thestig6b4461f2016-10-28 19:34:3014#include "base/numerics/safe_conversions.h"
[email protected]13ac53532013-03-30 00:27:0015#include "base/strings/sys_string_conversions.h"
tfarina3b0452d2014-12-31 15:20:0916#include "ui/gfx/geometry/rect.h"
tfarinaebe974f02015-01-03 04:25:3217#include "ui/gfx/geometry/size.h"
[email protected]14b210792009-10-12 18:45:3118
[email protected]3df79f42013-06-24 18:49:0519using base::ScopedCFTypeRef;
[email protected]df0ca6c82010-10-17 04:09:0620
[email protected]df6df682011-05-02 19:50:5021namespace {
22
thestig23fe51772014-09-27 17:40:5123// Rotate a page by |num_rotations| * 90 degrees, counter-clockwise.
24void RotatePage(CGContextRef context, const CGRect rect, int num_rotations) {
25 switch (num_rotations) {
26 case 0:
27 break;
28 case 1:
29 // After rotating by 90 degrees with the axis at the origin, the page
30 // content is now "off screen". Shift it right to move it back on screen.
31 CGContextTranslateCTM(context, rect.size.width, 0);
32 // Rotates counter-clockwise by 90 degrees.
33 CGContextRotateCTM(context, M_PI_2);
34 break;
35 case 2:
36 // After rotating by 180 degrees with the axis at the origin, the page
37 // content is now "off screen". Shift it right and up to move it back on
38 // screen.
39 CGContextTranslateCTM(context, rect.size.width, rect.size.height);
40 // Rotates counter-clockwise by 90 degrees.
41 CGContextRotateCTM(context, M_PI);
42 break;
43 case 3:
44 // After rotating by 270 degrees with the axis at the origin, the page
45 // content is now "off screen". Shift it right to move it back on screen.
46 CGContextTranslateCTM(context, 0, rect.size.height);
47 // Rotates counter-clockwise by 90 degrees.
48 CGContextRotateCTM(context, -M_PI_2);
49 break;
50 default:
51 NOTREACHED();
52 break;
thestig707a24b22015-09-14 18:16:3353 }
thestig23fe51772014-09-27 17:40:5154}
55
[email protected]df6df682011-05-02 19:50:5056} // namespace
57
[email protected]14b210792009-10-12 18:45:3158namespace printing {
59
thestig3520b802016-04-04 22:12:5260PdfMetafileCg::PdfMetafileCg() : page_is_open_(false) {}
[email protected]14b210792009-10-12 18:45:3161
thestig3520b802016-04-04 22:12:5262PdfMetafileCg::~PdfMetafileCg() {}
[email protected]1d20cdf2011-02-16 18:09:2863
[email protected]9b617532011-04-04 23:15:0664bool PdfMetafileCg::Init() {
[email protected]14b210792009-10-12 18:45:3165 // Ensure that Init hasn't already been called.
66 DCHECK(!context_.get());
67 DCHECK(!pdf_data_.get());
68
69 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0));
70 if (!pdf_data_.get()) {
71 LOG(ERROR) << "Failed to create pdf data for metafile";
[email protected]8f17cd3e2011-03-16 01:39:4272 return false;
[email protected]14b210792009-10-12 18:45:3173 }
[email protected]df0ca6c82010-10-17 04:09:0674 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer(
[email protected]14b210792009-10-12 18:45:3175 CGDataConsumerCreateWithCFData(pdf_data_));
76 if (!pdf_consumer.get()) {
77 LOG(ERROR) << "Failed to create data consumer for metafile";
thestig3520b802016-04-04 22:12:5278 pdf_data_.reset();
[email protected]8f17cd3e2011-03-16 01:39:4279 return false;
[email protected]14b210792009-10-12 18:45:3180 }
thestig3520b802016-04-04 22:12:5281 context_.reset(CGPDFContextCreate(pdf_consumer, nullptr, nullptr));
[email protected]14b210792009-10-12 18:45:3182 if (!context_.get()) {
83 LOG(ERROR) << "Failed to create pdf context for metafile";
thestig3520b802016-04-04 22:12:5284 pdf_data_.reset();
[email protected]14b210792009-10-12 18:45:3185 }
86
[email protected]8f17cd3e2011-03-16 01:39:4287 return true;
[email protected]14b210792009-10-12 18:45:3188}
89
[email protected]9b617532011-04-04 23:15:0690bool PdfMetafileCg::InitFromData(const void* src_buffer,
thestig6b4461f2016-10-28 19:34:3091 size_t src_buffer_size) {
[email protected]14b210792009-10-12 18:45:3192 DCHECK(!context_.get());
93 DCHECK(!pdf_data_.get());
94
thestig6b4461f2016-10-28 19:34:3095 if (!src_buffer || !src_buffer_size)
[email protected]14b210792009-10-12 18:45:3196 return false;
thestig6b4461f2016-10-28 19:34:3097
98 if (!base::IsValueInRangeForNumericType<CFIndex>(src_buffer_size))
99 return false;
[email protected]14b210792009-10-12 18:45:31100
101 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size));
102 CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer),
103 src_buffer_size);
104
105 return true;
106}
107
thestig192677ec2016-06-09 07:43:17108void PdfMetafileCg::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:44109 const gfx::Rect& content_area,
[email protected]9b617532011-04-04 23:15:06110 const float& scale_factor) {
[email protected]14b210792009-10-12 18:45:31111 DCHECK(context_.get());
112 DCHECK(!page_is_open_);
113
[email protected]15c2bb322010-12-21 22:30:10114 double height = page_size.height();
115 double width = page_size.width();
116
[email protected]14b210792009-10-12 18:45:31117 CGRect bounds = CGRectMake(0, 0, width, height);
118 CGContextBeginPage(context_, &bounds);
119 page_is_open_ = true;
120 CGContextSaveGState(context_);
121
[email protected]9d42d0722011-07-26 19:22:29122 // Move to the context origin.
123 CGContextTranslateCTM(context_, content_area.x(), -content_area.y());
124
[email protected]14b210792009-10-12 18:45:31125 // Flip the context.
126 CGContextTranslateCTM(context_, 0, height);
127 CGContextScaleCTM(context_, scale_factor, -scale_factor);
128}
129
[email protected]9b617532011-04-04 23:15:06130bool PdfMetafileCg::FinishPage() {
[email protected]14b210792009-10-12 18:45:31131 DCHECK(context_.get());
132 DCHECK(page_is_open_);
133
134 CGContextRestoreGState(context_);
135 CGContextEndPage(context_);
136 page_is_open_ = false;
[email protected]8f17cd3e2011-03-16 01:39:42137 return true;
[email protected]14b210792009-10-12 18:45:31138}
139
[email protected]9b617532011-04-04 23:15:06140bool PdfMetafileCg::FinishDocument() {
[email protected]14b210792009-10-12 18:45:31141 DCHECK(context_.get());
142 DCHECK(!page_is_open_);
143
[email protected]72f966b2009-10-14 20:02:27144#ifndef NDEBUG
145 // Check that the context will be torn down properly; if it's not, pdf_data_
146 // will be incomplete and generate invalid PDF files/documents.
147 if (context_.get()) {
148 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1;
149 if (extra_retain_count > 0) {
150 LOG(ERROR) << "Metafile context has " << extra_retain_count
151 << " extra retain(s) on Close";
152 }
153 }
154#endif
[email protected]14619482010-04-01 16:46:23155 CGPDFContextClose(context_.get());
thestig3520b802016-04-04 22:12:52156 context_.reset();
[email protected]8f17cd3e2011-03-16 01:39:42157 return true;
[email protected]14b210792009-10-12 18:45:31158}
159
thestig99fc2132017-04-27 03:37:54160bool PdfMetafileCg::RenderPage(unsigned int page_number,
[email protected]9b617532011-04-04 23:15:06161 CGContextRef context,
162 const CGRect rect,
thestig99fc2132017-04-27 03:37:54163 const MacRenderPageParams& params) const {
164 CGPDFDocumentRef pdf_doc = GetPDFDocument();
[email protected]72f966b2009-10-14 20:02:27165 if (!pdf_doc) {
166 LOG(ERROR) << "Unable to create PDF document from data";
167 return false;
168 }
169 CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
[email protected]a09ef7e2011-09-28 21:59:33170 CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox);
thestig23fe51772014-09-27 17:40:51171 int pdf_src_rotation = CGPDFPageGetRotationAngle(pdf_page);
[email protected]f3cab622010-06-28 18:56:30172 float scaling_factor = 1.0;
[email protected]b5cf844c2012-06-18 21:49:20173 const bool source_is_landscape =
174 (source_rect.size.width > source_rect.size.height);
175 const bool dest_is_landscape = (rect.size.width > rect.size.height);
176 const bool rotate =
177 params.autorotate ? (source_is_landscape != dest_is_landscape) : false;
178 const float source_width =
179 rotate ? source_rect.size.height : source_rect.size.width;
180 const float source_height =
181 rotate ? source_rect.size.width : source_rect.size.height;
[email protected]72f966b2009-10-14 20:02:27182
[email protected]b5cf844c2012-06-18 21:49:20183 // See if we need to scale the output.
184 const bool scaling_needed =
185 (params.shrink_to_fit && ((source_width > rect.size.width) ||
186 (source_height > rect.size.height))) ||
187 (params.stretch_to_fit && ((source_width < rect.size.width) &&
188 (source_height < rect.size.height)));
189 if (scaling_needed) {
190 float x_scaling_factor = rect.size.width / source_width;
191 float y_scaling_factor = rect.size.height / source_height;
192 scaling_factor = std::min(x_scaling_factor, y_scaling_factor);
[email protected]f3cab622010-06-28 18:56:30193 }
[email protected]b5cf844c2012-06-18 21:49:20194 // Some PDFs have a non-zero origin. Need to take that into account and align
195 // the PDF to the origin.
196 const float x_origin_offset = -1 * source_rect.origin.x;
197 const float y_origin_offset = -1 * source_rect.origin.y;
198
199 // If the PDF needs to be centered, calculate the offsets here.
200 float x_offset = params.center_horizontally ?
201 ((rect.size.width - (source_width * scaling_factor)) / 2) : 0;
202 if (rotate)
203 x_offset = -x_offset;
204
205 float y_offset = params.center_vertically ?
206 ((rect.size.height - (source_height * scaling_factor)) / 2) : 0;
207
[email protected]72f966b2009-10-14 20:02:27208 CGContextSaveGState(context);
[email protected]b5cf844c2012-06-18 21:49:20209
210 // The transform operations specified here gets applied in reverse order.
211 // i.e. the origin offset translation happens first.
212 // Origin is at bottom-left.
[email protected]f3cab622010-06-28 18:56:30213 CGContextTranslateCTM(context, x_offset, y_offset);
thestig23fe51772014-09-27 17:40:51214
215 int num_rotations = 0;
[email protected]b5cf844c2012-06-18 21:49:20216 if (rotate) {
thestig23fe51772014-09-27 17:40:51217 if (pdf_src_rotation == 0 || pdf_src_rotation == 270) {
218 num_rotations = 1;
219 } else {
220 num_rotations = 3;
221 }
222 } else {
223 if (pdf_src_rotation == 180 || pdf_src_rotation == 270) {
224 num_rotations = 2;
225 }
[email protected]b5cf844c2012-06-18 21:49:20226 }
thestig23fe51772014-09-27 17:40:51227 RotatePage(context, rect, num_rotations);
228
[email protected]f3cab622010-06-28 18:56:30229 CGContextScaleCTM(context, scaling_factor, scaling_factor);
[email protected]b5cf844c2012-06-18 21:49:20230 CGContextTranslateCTM(context, x_origin_offset, y_origin_offset);
231
[email protected]72f966b2009-10-14 20:02:27232 CGContextDrawPDFPage(context, pdf_page);
233 CGContextRestoreGState(context);
[email protected]b5cf844c2012-06-18 21:49:20234
[email protected]72f966b2009-10-14 20:02:27235 return true;
236}
237
[email protected]9b617532011-04-04 23:15:06238unsigned int PdfMetafileCg::GetPageCount() const {
[email protected]72f966b2009-10-14 20:02:27239 CGPDFDocumentRef pdf_doc = GetPDFDocument();
240 return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0;
241}
242
[email protected]9b617532011-04-04 23:15:06243gfx::Rect PdfMetafileCg::GetPageBounds(unsigned int page_number) const {
[email protected]72f966b2009-10-14 20:02:27244 CGPDFDocumentRef pdf_doc = GetPDFDocument();
245 if (!pdf_doc) {
246 LOG(ERROR) << "Unable to create PDF document from data";
247 return gfx::Rect();
248 }
249 if (page_number > GetPageCount()) {
250 LOG(ERROR) << "Invalid page number: " << page_number;
251 return gfx::Rect();
252 }
253 CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
254 CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox);
255 return gfx::Rect(page_rect);
256}
257
thestig707a24b22015-09-14 18:16:33258uint32_t PdfMetafileCg::GetDataSize() const {
[email protected]14b210792009-10-12 18:45:31259 // PDF data is only valid/complete once the context is released.
260 DCHECK(!context_);
261
262 if (!pdf_data_)
263 return 0;
thestig707a24b22015-09-14 18:16:33264 return static_cast<uint32_t>(CFDataGetLength(pdf_data_));
[email protected]14b210792009-10-12 18:45:31265}
266
thestig707a24b22015-09-14 18:16:33267bool PdfMetafileCg::GetData(void* dst_buffer, uint32_t dst_buffer_size) const {
[email protected]14b210792009-10-12 18:45:31268 // PDF data is only valid/complete once the context is released.
269 DCHECK(!context_);
270 DCHECK(pdf_data_);
271 DCHECK(dst_buffer);
272 DCHECK_GT(dst_buffer_size, 0U);
273
thestig707a24b22015-09-14 18:16:33274 uint32_t data_size = GetDataSize();
[email protected]14b210792009-10-12 18:45:31275 if (dst_buffer_size > data_size) {
276 return false;
277 }
278
279 CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size),
280 static_cast<UInt8*>(dst_buffer));
281 return true;
282}
283
[email protected]9b617532011-04-04 23:15:06284CGContextRef PdfMetafileCg::context() const {
[email protected]8f17cd3e2011-03-16 01:39:42285 return context_.get();
286}
287
[email protected]9b617532011-04-04 23:15:06288CGPDFDocumentRef PdfMetafileCg::GetPDFDocument() const {
[email protected]72f966b2009-10-14 20:02:27289 // Make sure that we have data, and that it's not being modified any more.
290 DCHECK(pdf_data_.get());
291 DCHECK(!context_.get());
292
293 if (!pdf_doc_.get()) {
[email protected]df0ca6c82010-10-17 04:09:06294 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider(
[email protected]72f966b2009-10-14 20:02:27295 CGDataProviderCreateWithCFData(pdf_data_));
296 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider));
297 }
298 return pdf_doc_.get();
299}
300
[email protected]14b210792009-10-12 18:45:31301} // namespace printing