[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 5 | #include "printing/pdf_metafile_cg_mac.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | ba64e2b | 2011-06-14 18:18:38 | [diff] [blame] | 12 | #include "base/mac/mac_util.h" |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 13 | #include "base/mac/scoped_cftyperef.h" |
Peter Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 14 | #include "base/numerics/math_constants.h" |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 15 | #include "base/numerics/safe_conversions.h" |
[email protected] | 13ac5353 | 2013-03-30 00:27:00 | [diff] [blame] | 16 | #include "base/strings/sys_string_conversions.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 18 | #include "ui/gfx/geometry/size.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 19 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 20 | using base::ScopedCFTypeRef; |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 21 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 22 | namespace { |
| 23 | |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 24 | // Rotate a page by |num_rotations| * 90 degrees, counter-clockwise. |
| 25 | void 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 Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 34 | CGContextRotateCTM(context, base::kPiDouble / 2); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 35 | 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 Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 42 | CGContextRotateCTM(context, base::kPiDouble); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 43 | 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 Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 49 | CGContextRotateCTM(context, -base::kPiDouble / 2); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 50 | break; |
| 51 | default: |
| 52 | NOTREACHED(); |
| 53 | break; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 54 | } |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 55 | } |
| 56 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 57 | } // namespace |
| 58 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 59 | namespace printing { |
| 60 | |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 61 | PdfMetafileCg::PdfMetafileCg() : page_is_open_(false) {} |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 62 | |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 63 | PdfMetafileCg::~PdfMetafileCg() {} |
[email protected] | 1d20cdf | 2011-02-16 18:09:28 | [diff] [blame] | 64 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 65 | bool PdfMetafileCg::Init() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 66 | // 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] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 73 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 74 | } |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 75 | ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 76 | CGDataConsumerCreateWithCFData(pdf_data_)); |
| 77 | if (!pdf_consumer.get()) { |
| 78 | LOG(ERROR) << "Failed to create data consumer for metafile"; |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 79 | pdf_data_.reset(); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 80 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 81 | } |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 82 | context_.reset(CGPDFContextCreate(pdf_consumer, nullptr, nullptr)); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 83 | if (!context_.get()) { |
| 84 | LOG(ERROR) << "Failed to create pdf context for metafile"; |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 85 | pdf_data_.reset(); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 88 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 91 | bool PdfMetafileCg::InitFromData(const void* src_buffer, |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 92 | size_t src_buffer_size) { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 93 | DCHECK(!context_.get()); |
| 94 | DCHECK(!pdf_data_.get()); |
| 95 | |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 96 | if (!src_buffer || !src_buffer_size) |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 97 | return false; |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 98 | |
| 99 | if (!base::IsValueInRangeForNumericType<CFIndex>(src_buffer_size)) |
| 100 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 101 | |
| 102 | pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size)); |
| 103 | CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer), |
| 104 | src_buffer_size); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 108 | void PdfMetafileCg::StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 109 | const gfx::Rect& content_area, |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 110 | const float& scale_factor) { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 111 | DCHECK(context_.get()); |
| 112 | DCHECK(!page_is_open_); |
| 113 | |
[email protected] | 15c2bb32 | 2010-12-21 22:30:10 | [diff] [blame] | 114 | double height = page_size.height(); |
| 115 | double width = page_size.width(); |
| 116 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 117 | CGRect bounds = CGRectMake(0, 0, width, height); |
| 118 | CGContextBeginPage(context_, &bounds); |
| 119 | page_is_open_ = true; |
| 120 | CGContextSaveGState(context_); |
| 121 | |
[email protected] | 9d42d072 | 2011-07-26 19:22:29 | [diff] [blame] | 122 | // Move to the context origin. |
| 123 | CGContextTranslateCTM(context_, content_area.x(), -content_area.y()); |
| 124 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 125 | // Flip the context. |
| 126 | CGContextTranslateCTM(context_, 0, height); |
| 127 | CGContextScaleCTM(context_, scale_factor, -scale_factor); |
| 128 | } |
| 129 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 130 | bool PdfMetafileCg::FinishPage() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 131 | DCHECK(context_.get()); |
| 132 | DCHECK(page_is_open_); |
| 133 | |
| 134 | CGContextRestoreGState(context_); |
| 135 | CGContextEndPage(context_); |
| 136 | page_is_open_ = false; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 137 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 140 | bool PdfMetafileCg::FinishDocument() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 141 | DCHECK(context_.get()); |
| 142 | DCHECK(!page_is_open_); |
| 143 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 144 | #ifndef NDEBUG |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 145 | // Check that the context will be torn down properly; if it's not, |pdf_data| |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 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] | 1461948 | 2010-04-01 16:46:23 | [diff] [blame] | 155 | CGPDFContextClose(context_.get()); |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 156 | context_.reset(); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 157 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 158 | } |
| 159 | |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 160 | bool PdfMetafileCg::RenderPage(unsigned int page_number, |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 161 | CGContextRef context, |
| 162 | const CGRect rect, |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 163 | const MacRenderPageParams& params) const { |
| 164 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 165 | if (!pdf_doc) { |
| 166 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 167 | return false; |
| 168 | } |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 169 | |
Lei Zhang | c3f3466 | 2017-11-16 20:29:41 | [diff] [blame] | 170 | const unsigned int page_count = GetPageCount(); |
| 171 | DCHECK_NE(page_count, 0U); |
| 172 | DCHECK_NE(page_number, 0U); |
| 173 | DCHECK_LE(page_number, page_count); |
| 174 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 175 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
[email protected] | a09ef7e | 2011-09-28 21:59:33 | [diff] [blame] | 176 | CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 177 | int pdf_src_rotation = CGPDFPageGetRotationAngle(pdf_page); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 178 | const bool source_is_landscape = |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 179 | (source_rect.size.width > source_rect.size.height); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 180 | const bool dest_is_landscape = (rect.size.width > rect.size.height); |
| 181 | const bool rotate = |
| 182 | params.autorotate ? (source_is_landscape != dest_is_landscape) : false; |
| 183 | const float source_width = |
| 184 | rotate ? source_rect.size.height : source_rect.size.width; |
| 185 | const float source_height = |
| 186 | rotate ? source_rect.size.width : source_rect.size.height; |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 187 | |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 188 | // See if we need to scale the output. |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 189 | float scaling_factor = 1.0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 190 | const bool scaling_needed = |
| 191 | (params.shrink_to_fit && ((source_width > rect.size.width) || |
| 192 | (source_height > rect.size.height))) || |
| 193 | (params.stretch_to_fit && ((source_width < rect.size.width) && |
| 194 | (source_height < rect.size.height))); |
| 195 | if (scaling_needed) { |
| 196 | float x_scaling_factor = rect.size.width / source_width; |
| 197 | float y_scaling_factor = rect.size.height / source_height; |
| 198 | scaling_factor = std::min(x_scaling_factor, y_scaling_factor); |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 199 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 200 | // Some PDFs have a non-zero origin. Need to take that into account and align |
| 201 | // the PDF to the origin. |
| 202 | const float x_origin_offset = -1 * source_rect.origin.x; |
| 203 | const float y_origin_offset = -1 * source_rect.origin.y; |
| 204 | |
| 205 | // If the PDF needs to be centered, calculate the offsets here. |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 206 | float x_offset = |
| 207 | params.center_horizontally |
| 208 | ? ((rect.size.width - (source_width * scaling_factor)) / 2) |
| 209 | : 0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 210 | if (rotate) |
| 211 | x_offset = -x_offset; |
| 212 | |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 213 | float y_offset = |
| 214 | params.center_vertically |
| 215 | ? ((rect.size.height - (source_height * scaling_factor)) / 2) |
| 216 | : 0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 217 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 218 | CGContextSaveGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 219 | |
| 220 | // The transform operations specified here gets applied in reverse order. |
| 221 | // i.e. the origin offset translation happens first. |
| 222 | // Origin is at bottom-left. |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 223 | CGContextTranslateCTM(context, x_offset, y_offset); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 224 | |
| 225 | int num_rotations = 0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 226 | if (rotate) { |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 227 | if (pdf_src_rotation == 0 || pdf_src_rotation == 270) { |
| 228 | num_rotations = 1; |
| 229 | } else { |
| 230 | num_rotations = 3; |
| 231 | } |
| 232 | } else { |
| 233 | if (pdf_src_rotation == 180 || pdf_src_rotation == 270) { |
| 234 | num_rotations = 2; |
| 235 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 236 | } |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 237 | RotatePage(context, rect, num_rotations); |
| 238 | |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 239 | CGContextScaleCTM(context, scaling_factor, scaling_factor); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 240 | CGContextTranslateCTM(context, x_origin_offset, y_origin_offset); |
| 241 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 242 | CGContextDrawPDFPage(context, pdf_page); |
| 243 | CGContextRestoreGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 244 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 248 | unsigned int PdfMetafileCg::GetPageCount() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 249 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 250 | return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0; |
| 251 | } |
| 252 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 253 | gfx::Rect PdfMetafileCg::GetPageBounds(unsigned int page_number) const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 254 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 255 | if (!pdf_doc) { |
| 256 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 257 | return gfx::Rect(); |
| 258 | } |
| 259 | if (page_number > GetPageCount()) { |
| 260 | LOG(ERROR) << "Invalid page number: " << page_number; |
| 261 | return gfx::Rect(); |
| 262 | } |
| 263 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
| 264 | CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox); |
| 265 | return gfx::Rect(page_rect); |
| 266 | } |
| 267 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 268 | uint32_t PdfMetafileCg::GetDataSize() const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 269 | // PDF data is only valid/complete once the context is released. |
| 270 | DCHECK(!context_); |
| 271 | |
| 272 | if (!pdf_data_) |
| 273 | return 0; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 274 | return static_cast<uint32_t>(CFDataGetLength(pdf_data_)); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 275 | } |
| 276 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 277 | bool PdfMetafileCg::GetData(void* dst_buffer, uint32_t dst_buffer_size) const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 278 | // PDF data is only valid/complete once the context is released. |
| 279 | DCHECK(!context_); |
| 280 | DCHECK(pdf_data_); |
| 281 | DCHECK(dst_buffer); |
| 282 | DCHECK_GT(dst_buffer_size, 0U); |
| 283 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 284 | uint32_t data_size = GetDataSize(); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 285 | if (dst_buffer_size > data_size) { |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size), |
| 290 | static_cast<UInt8*>(dst_buffer)); |
| 291 | return true; |
| 292 | } |
| 293 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 294 | CGContextRef PdfMetafileCg::context() const { |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 295 | return context_.get(); |
| 296 | } |
| 297 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 298 | CGPDFDocumentRef PdfMetafileCg::GetPDFDocument() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 299 | // Make sure that we have data, and that it's not being modified any more. |
| 300 | DCHECK(pdf_data_.get()); |
| 301 | DCHECK(!context_.get()); |
| 302 | |
| 303 | if (!pdf_doc_.get()) { |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 304 | ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 305 | CGDataProviderCreateWithCFData(pdf_data_)); |
| 306 | pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
| 307 | } |
| 308 | return pdf_doc_.get(); |
| 309 | } |
| 310 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 311 | } // namespace printing |