blob: 968c47cecaa9fedf59d3d7d287d891fbe5004d21 [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"
Peter Kasting76ed0662017-09-14 08:28:0714#include "base/numerics/math_constants.h"
thestig6b4461f2016-10-28 19:34:3015#include "base/numerics/safe_conversions.h"
[email protected]13ac53532013-03-30 00:27:0016#include "base/strings/sys_string_conversions.h"
tfarina3b0452d2014-12-31 15:20:0917#include "ui/gfx/geometry/rect.h"
tfarinaebe974f02015-01-03 04:25:3218#include "ui/gfx/geometry/size.h"
[email protected]14b210792009-10-12 18:45:3119
[email protected]3df79f42013-06-24 18:49:0520using base::ScopedCFTypeRef;
[email protected]df0ca6c82010-10-17 04:09:0621
[email protected]df6df682011-05-02 19:50:5022namespace {
23
thestig23fe51772014-09-27 17:40:5124// Rotate a page by |num_rotations| * 90 degrees, counter-clockwise.
25void RotatePage(CGContextRef context, const CGRect rect, int num_rotations) {
26 switch (num_rotations) {
27 case 0:
28 break;
29 case 1:
30 // After rotating by 90 degrees with the axis at the origin, the page
31 // content is now "off screen". Shift it right to move it back on screen.
32 CGContextTranslateCTM(context, rect.size.width, 0);
33 // Rotates counter-clockwise by 90 degrees.
Peter Kasting76ed0662017-09-14 08:28:0734 CGContextRotateCTM(context, base::kPiDouble / 2);
thestig23fe51772014-09-27 17:40:5135 break;
36 case 2:
37 // After rotating by 180 degrees with the axis at the origin, the page
38 // content is now "off screen". Shift it right and up to move it back on
39 // screen.
40 CGContextTranslateCTM(context, rect.size.width, rect.size.height);
41 // Rotates counter-clockwise by 90 degrees.
Peter Kasting76ed0662017-09-14 08:28:0742 CGContextRotateCTM(context, base::kPiDouble);
thestig23fe51772014-09-27 17:40:5143 break;
44 case 3:
45 // After rotating by 270 degrees with the axis at the origin, the page
46 // content is now "off screen". Shift it right to move it back on screen.
47 CGContextTranslateCTM(context, 0, rect.size.height);
48 // Rotates counter-clockwise by 90 degrees.
Peter Kasting76ed0662017-09-14 08:28:0749 CGContextRotateCTM(context, -base::kPiDouble / 2);
thestig23fe51772014-09-27 17:40:5150 break;
51 default:
52 NOTREACHED();
53 break;
thestig707a24b22015-09-14 18:16:3354 }
thestig23fe51772014-09-27 17:40:5155}
56
[email protected]df6df682011-05-02 19:50:5057} // namespace
58
[email protected]14b210792009-10-12 18:45:3159namespace printing {
60
thestig3520b802016-04-04 22:12:5261PdfMetafileCg::PdfMetafileCg() : page_is_open_(false) {}
[email protected]14b210792009-10-12 18:45:3162
thestig3520b802016-04-04 22:12:5263PdfMetafileCg::~PdfMetafileCg() {}
[email protected]1d20cdf2011-02-16 18:09:2864
[email protected]9b617532011-04-04 23:15:0665bool PdfMetafileCg::Init() {
[email protected]14b210792009-10-12 18:45:3166 // Ensure that Init hasn't already been called.
67 DCHECK(!context_.get());
68 DCHECK(!pdf_data_.get());
69
70 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0));
71 if (!pdf_data_.get()) {
72 LOG(ERROR) << "Failed to create pdf data for metafile";
[email protected]8f17cd3e2011-03-16 01:39:4273 return false;
[email protected]14b210792009-10-12 18:45:3174 }
[email protected]df0ca6c82010-10-17 04:09:0675 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer(
[email protected]14b210792009-10-12 18:45:3176 CGDataConsumerCreateWithCFData(pdf_data_));
77 if (!pdf_consumer.get()) {
78 LOG(ERROR) << "Failed to create data consumer for metafile";
thestig3520b802016-04-04 22:12:5279 pdf_data_.reset();
[email protected]8f17cd3e2011-03-16 01:39:4280 return false;
[email protected]14b210792009-10-12 18:45:3181 }
thestig3520b802016-04-04 22:12:5282 context_.reset(CGPDFContextCreate(pdf_consumer, nullptr, nullptr));
[email protected]14b210792009-10-12 18:45:3183 if (!context_.get()) {
84 LOG(ERROR) << "Failed to create pdf context for metafile";
thestig3520b802016-04-04 22:12:5285 pdf_data_.reset();
[email protected]14b210792009-10-12 18:45:3186 }
87
[email protected]8f17cd3e2011-03-16 01:39:4288 return true;
[email protected]14b210792009-10-12 18:45:3189}
90
[email protected]9b617532011-04-04 23:15:0691bool PdfMetafileCg::InitFromData(const void* src_buffer,
thestig6b4461f2016-10-28 19:34:3092 size_t src_buffer_size) {
[email protected]14b210792009-10-12 18:45:3193 DCHECK(!context_.get());
94 DCHECK(!pdf_data_.get());
95
thestig6b4461f2016-10-28 19:34:3096 if (!src_buffer || !src_buffer_size)
[email protected]14b210792009-10-12 18:45:3197 return false;
thestig6b4461f2016-10-28 19:34:3098
99 if (!base::IsValueInRangeForNumericType<CFIndex>(src_buffer_size))
100 return false;
[email protected]14b210792009-10-12 18:45:31101
102 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size));
103 CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer),
104 src_buffer_size);
105
106 return true;
107}
108
thestig192677ec2016-06-09 07:43:17109void PdfMetafileCg::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:44110 const gfx::Rect& content_area,
[email protected]9b617532011-04-04 23:15:06111 const float& scale_factor) {
[email protected]14b210792009-10-12 18:45:31112 DCHECK(context_.get());
113 DCHECK(!page_is_open_);
114
[email protected]15c2bb322010-12-21 22:30:10115 double height = page_size.height();
116 double width = page_size.width();
117
[email protected]14b210792009-10-12 18:45:31118 CGRect bounds = CGRectMake(0, 0, width, height);
119 CGContextBeginPage(context_, &bounds);
120 page_is_open_ = true;
121 CGContextSaveGState(context_);
122
[email protected]9d42d0722011-07-26 19:22:29123 // Move to the context origin.
124 CGContextTranslateCTM(context_, content_area.x(), -content_area.y());
125
[email protected]14b210792009-10-12 18:45:31126 // Flip the context.
127 CGContextTranslateCTM(context_, 0, height);
128 CGContextScaleCTM(context_, scale_factor, -scale_factor);
129}
130
[email protected]9b617532011-04-04 23:15:06131bool PdfMetafileCg::FinishPage() {
[email protected]14b210792009-10-12 18:45:31132 DCHECK(context_.get());
133 DCHECK(page_is_open_);
134
135 CGContextRestoreGState(context_);
136 CGContextEndPage(context_);
137 page_is_open_ = false;
[email protected]8f17cd3e2011-03-16 01:39:42138 return true;
[email protected]14b210792009-10-12 18:45:31139}
140
[email protected]9b617532011-04-04 23:15:06141bool PdfMetafileCg::FinishDocument() {
[email protected]14b210792009-10-12 18:45:31142 DCHECK(context_.get());
143 DCHECK(!page_is_open_);
144
[email protected]72f966b2009-10-14 20:02:27145#ifndef NDEBUG
146 // Check that the context will be torn down properly; if it's not, pdf_data_
147 // will be incomplete and generate invalid PDF files/documents.
148 if (context_.get()) {
149 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1;
150 if (extra_retain_count > 0) {
151 LOG(ERROR) << "Metafile context has " << extra_retain_count
152 << " extra retain(s) on Close";
153 }
154 }
155#endif
[email protected]14619482010-04-01 16:46:23156 CGPDFContextClose(context_.get());
thestig3520b802016-04-04 22:12:52157 context_.reset();
[email protected]8f17cd3e2011-03-16 01:39:42158 return true;
[email protected]14b210792009-10-12 18:45:31159}
160
thestig99fc2132017-04-27 03:37:54161bool PdfMetafileCg::RenderPage(unsigned int page_number,
[email protected]9b617532011-04-04 23:15:06162 CGContextRef context,
163 const CGRect rect,
thestig99fc2132017-04-27 03:37:54164 const MacRenderPageParams& params) const {
165 CGPDFDocumentRef pdf_doc = GetPDFDocument();
[email protected]72f966b2009-10-14 20:02:27166 if (!pdf_doc) {
167 LOG(ERROR) << "Unable to create PDF document from data";
168 return false;
169 }
170 CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
[email protected]a09ef7e2011-09-28 21:59:33171 CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox);
thestig23fe51772014-09-27 17:40:51172 int pdf_src_rotation = CGPDFPageGetRotationAngle(pdf_page);
[email protected]f3cab622010-06-28 18:56:30173 float scaling_factor = 1.0;
[email protected]b5cf844c2012-06-18 21:49:20174 const bool source_is_landscape =
175 (source_rect.size.width > source_rect.size.height);
176 const bool dest_is_landscape = (rect.size.width > rect.size.height);
177 const bool rotate =
178 params.autorotate ? (source_is_landscape != dest_is_landscape) : false;
179 const float source_width =
180 rotate ? source_rect.size.height : source_rect.size.width;
181 const float source_height =
182 rotate ? source_rect.size.width : source_rect.size.height;
[email protected]72f966b2009-10-14 20:02:27183
[email protected]b5cf844c2012-06-18 21:49:20184 // See if we need to scale the output.
185 const bool scaling_needed =
186 (params.shrink_to_fit && ((source_width > rect.size.width) ||
187 (source_height > rect.size.height))) ||
188 (params.stretch_to_fit && ((source_width < rect.size.width) &&
189 (source_height < rect.size.height)));
190 if (scaling_needed) {
191 float x_scaling_factor = rect.size.width / source_width;
192 float y_scaling_factor = rect.size.height / source_height;
193 scaling_factor = std::min(x_scaling_factor, y_scaling_factor);
[email protected]f3cab622010-06-28 18:56:30194 }
[email protected]b5cf844c2012-06-18 21:49:20195 // Some PDFs have a non-zero origin. Need to take that into account and align
196 // the PDF to the origin.
197 const float x_origin_offset = -1 * source_rect.origin.x;
198 const float y_origin_offset = -1 * source_rect.origin.y;
199
200 // If the PDF needs to be centered, calculate the offsets here.
201 float x_offset = params.center_horizontally ?
202 ((rect.size.width - (source_width * scaling_factor)) / 2) : 0;
203 if (rotate)
204 x_offset = -x_offset;
205
206 float y_offset = params.center_vertically ?
207 ((rect.size.height - (source_height * scaling_factor)) / 2) : 0;
208
[email protected]72f966b2009-10-14 20:02:27209 CGContextSaveGState(context);
[email protected]b5cf844c2012-06-18 21:49:20210
211 // The transform operations specified here gets applied in reverse order.
212 // i.e. the origin offset translation happens first.
213 // Origin is at bottom-left.
[email protected]f3cab622010-06-28 18:56:30214 CGContextTranslateCTM(context, x_offset, y_offset);
thestig23fe51772014-09-27 17:40:51215
216 int num_rotations = 0;
[email protected]b5cf844c2012-06-18 21:49:20217 if (rotate) {
thestig23fe51772014-09-27 17:40:51218 if (pdf_src_rotation == 0 || pdf_src_rotation == 270) {
219 num_rotations = 1;
220 } else {
221 num_rotations = 3;
222 }
223 } else {
224 if (pdf_src_rotation == 180 || pdf_src_rotation == 270) {
225 num_rotations = 2;
226 }
[email protected]b5cf844c2012-06-18 21:49:20227 }
thestig23fe51772014-09-27 17:40:51228 RotatePage(context, rect, num_rotations);
229
[email protected]f3cab622010-06-28 18:56:30230 CGContextScaleCTM(context, scaling_factor, scaling_factor);
[email protected]b5cf844c2012-06-18 21:49:20231 CGContextTranslateCTM(context, x_origin_offset, y_origin_offset);
232
[email protected]72f966b2009-10-14 20:02:27233 CGContextDrawPDFPage(context, pdf_page);
234 CGContextRestoreGState(context);
[email protected]b5cf844c2012-06-18 21:49:20235
[email protected]72f966b2009-10-14 20:02:27236 return true;
237}
238
[email protected]9b617532011-04-04 23:15:06239unsigned int PdfMetafileCg::GetPageCount() const {
[email protected]72f966b2009-10-14 20:02:27240 CGPDFDocumentRef pdf_doc = GetPDFDocument();
241 return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0;
242}
243
[email protected]9b617532011-04-04 23:15:06244gfx::Rect PdfMetafileCg::GetPageBounds(unsigned int page_number) const {
[email protected]72f966b2009-10-14 20:02:27245 CGPDFDocumentRef pdf_doc = GetPDFDocument();
246 if (!pdf_doc) {
247 LOG(ERROR) << "Unable to create PDF document from data";
248 return gfx::Rect();
249 }
250 if (page_number > GetPageCount()) {
251 LOG(ERROR) << "Invalid page number: " << page_number;
252 return gfx::Rect();
253 }
254 CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
255 CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox);
256 return gfx::Rect(page_rect);
257}
258
thestig707a24b22015-09-14 18:16:33259uint32_t PdfMetafileCg::GetDataSize() const {
[email protected]14b210792009-10-12 18:45:31260 // PDF data is only valid/complete once the context is released.
261 DCHECK(!context_);
262
263 if (!pdf_data_)
264 return 0;
thestig707a24b22015-09-14 18:16:33265 return static_cast<uint32_t>(CFDataGetLength(pdf_data_));
[email protected]14b210792009-10-12 18:45:31266}
267
thestig707a24b22015-09-14 18:16:33268bool PdfMetafileCg::GetData(void* dst_buffer, uint32_t dst_buffer_size) const {
[email protected]14b210792009-10-12 18:45:31269 // PDF data is only valid/complete once the context is released.
270 DCHECK(!context_);
271 DCHECK(pdf_data_);
272 DCHECK(dst_buffer);
273 DCHECK_GT(dst_buffer_size, 0U);
274
thestig707a24b22015-09-14 18:16:33275 uint32_t data_size = GetDataSize();
[email protected]14b210792009-10-12 18:45:31276 if (dst_buffer_size > data_size) {
277 return false;
278 }
279
280 CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size),
281 static_cast<UInt8*>(dst_buffer));
282 return true;
283}
284
[email protected]9b617532011-04-04 23:15:06285CGContextRef PdfMetafileCg::context() const {
[email protected]8f17cd3e2011-03-16 01:39:42286 return context_.get();
287}
288
[email protected]9b617532011-04-04 23:15:06289CGPDFDocumentRef PdfMetafileCg::GetPDFDocument() const {
[email protected]72f966b2009-10-14 20:02:27290 // Make sure that we have data, and that it's not being modified any more.
291 DCHECK(pdf_data_.get());
292 DCHECK(!context_.get());
293
294 if (!pdf_doc_.get()) {
[email protected]df0ca6c82010-10-17 04:09:06295 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider(
[email protected]72f966b2009-10-14 20:02:27296 CGDataProviderCreateWithCFData(pdf_data_));
297 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider));
298 }
299 return pdf_doc_.get();
300}
301
[email protected]14b210792009-10-12 18:45:31302} // namespace printing