[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" |
Morten Stenshorne | eebf2db | 2020-05-25 09:00:05 | [diff] [blame] | 17 | #include "printing/mojom/print.mojom.h" |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 18 | #include "ui/gfx/geometry/rect.h" |
tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 19 | #include "ui/gfx/geometry/size.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 20 | |
[email protected] | 3df79f4 | 2013-06-24 18:49:05 | [diff] [blame] | 21 | using base::ScopedCFTypeRef; |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 22 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 23 | namespace { |
| 24 | |
Daniel Hosseinian | 3553e27 | 2021-04-24 00:51:18 | [diff] [blame] | 25 | // Rotate a page by `num_rotations` * 90 degrees, counter-clockwise. |
Lei Zhang | f69ea3b | 2020-03-24 17:53:48 | [diff] [blame] | 26 | void RotatePage(CGContextRef context, const CGRect& rect, int num_rotations) { |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 27 | switch (num_rotations) { |
| 28 | case 0: |
| 29 | break; |
| 30 | case 1: |
| 31 | // After rotating by 90 degrees with the axis at the origin, the page |
| 32 | // content is now "off screen". Shift it right to move it back on screen. |
| 33 | CGContextTranslateCTM(context, rect.size.width, 0); |
| 34 | // Rotates counter-clockwise by 90 degrees. |
Peter Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 35 | CGContextRotateCTM(context, base::kPiDouble / 2); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 36 | break; |
| 37 | case 2: |
| 38 | // After rotating by 180 degrees with the axis at the origin, the page |
| 39 | // content is now "off screen". Shift it right and up to move it back on |
| 40 | // screen. |
| 41 | CGContextTranslateCTM(context, rect.size.width, rect.size.height); |
| 42 | // Rotates counter-clockwise by 90 degrees. |
Peter Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 43 | CGContextRotateCTM(context, base::kPiDouble); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 44 | break; |
| 45 | case 3: |
| 46 | // After rotating by 270 degrees with the axis at the origin, the page |
| 47 | // content is now "off screen". Shift it right to move it back on screen. |
| 48 | CGContextTranslateCTM(context, 0, rect.size.height); |
| 49 | // Rotates counter-clockwise by 90 degrees. |
Peter Kasting | 76ed066 | 2017-09-14 08:28:07 | [diff] [blame] | 50 | CGContextRotateCTM(context, -base::kPiDouble / 2); |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 51 | break; |
| 52 | default: |
| 53 | NOTREACHED(); |
| 54 | break; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 55 | } |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 58 | } // namespace |
| 59 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 60 | namespace printing { |
| 61 | |
Lei Zhang | f69ea3b | 2020-03-24 17:53:48 | [diff] [blame] | 62 | PdfMetafileCg::PdfMetafileCg() = default; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 63 | |
Lei Zhang | f69ea3b | 2020-03-24 17:53:48 | [diff] [blame] | 64 | PdfMetafileCg::~PdfMetafileCg() = default; |
[email protected] | 1d20cdf | 2011-02-16 18:09:28 | [diff] [blame] | 65 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 66 | bool PdfMetafileCg::Init() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 67 | // Ensure that Init hasn't already been called. |
| 68 | DCHECK(!context_.get()); |
| 69 | DCHECK(!pdf_data_.get()); |
| 70 | |
| 71 | pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); |
| 72 | if (!pdf_data_.get()) { |
| 73 | LOG(ERROR) << "Failed to create pdf data for metafile"; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 74 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 75 | } |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 76 | ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 77 | CGDataConsumerCreateWithCFData(pdf_data_)); |
| 78 | if (!pdf_consumer.get()) { |
| 79 | LOG(ERROR) << "Failed to create data consumer for metafile"; |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 80 | pdf_data_.reset(); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 81 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 82 | } |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 83 | context_.reset(CGPDFContextCreate(pdf_consumer, nullptr, nullptr)); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 84 | if (!context_.get()) { |
| 85 | LOG(ERROR) << "Failed to create pdf context for metafile"; |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 86 | pdf_data_.reset(); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 89 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 90 | } |
| 91 | |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 92 | bool PdfMetafileCg::InitFromData(base::span<const uint8_t> data) { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 93 | DCHECK(!context_.get()); |
| 94 | DCHECK(!pdf_data_.get()); |
| 95 | |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 96 | if (data.empty()) |
[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 | |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 99 | if (!base::IsValueInRangeForNumericType<CFIndex>(data.size())) |
thestig | 6b4461f | 2016-10-28 19:34:30 | [diff] [blame] | 100 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 101 | |
Lei Zhang | b4ef0d5d | 2020-03-27 23:18:29 | [diff] [blame] | 102 | pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, data.size())); |
| 103 | CFDataAppendBytes(pdf_data_, data.data(), data.size()); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 107 | void PdfMetafileCg::StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 108 | const gfx::Rect& content_area, |
Morten Stenshorne | eebf2db | 2020-05-25 09:00:05 | [diff] [blame] | 109 | float scale_factor, |
| 110 | mojom::PageOrientation page_orientation) { |
| 111 | DCHECK_EQ(page_orientation, mojom::PageOrientation::kUpright) |
| 112 | << "Not implemented"; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 113 | DCHECK(context_.get()); |
| 114 | DCHECK(!page_is_open_); |
| 115 | |
Lei Zhang | f69ea3b | 2020-03-24 17:53:48 | [diff] [blame] | 116 | page_is_open_ = true; |
| 117 | float height = page_size.height(); |
| 118 | float width = page_size.width(); |
[email protected] | 15c2bb32 | 2010-12-21 22:30:10 | [diff] [blame] | 119 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 120 | CGRect bounds = CGRectMake(0, 0, width, height); |
| 121 | CGContextBeginPage(context_, &bounds); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 122 | CGContextSaveGState(context_); |
| 123 | |
[email protected] | 9d42d072 | 2011-07-26 19:22:29 | [diff] [blame] | 124 | // Move to the context origin. |
| 125 | CGContextTranslateCTM(context_, content_area.x(), -content_area.y()); |
| 126 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 127 | // Flip the context. |
| 128 | CGContextTranslateCTM(context_, 0, height); |
| 129 | CGContextScaleCTM(context_, scale_factor, -scale_factor); |
| 130 | } |
| 131 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 132 | bool PdfMetafileCg::FinishPage() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 133 | DCHECK(context_.get()); |
| 134 | DCHECK(page_is_open_); |
| 135 | |
| 136 | CGContextRestoreGState(context_); |
| 137 | CGContextEndPage(context_); |
| 138 | page_is_open_ = false; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 139 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 142 | bool PdfMetafileCg::FinishDocument() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 143 | DCHECK(context_.get()); |
| 144 | DCHECK(!page_is_open_); |
| 145 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 146 | #ifndef NDEBUG |
Daniel Hosseinian | 3553e27 | 2021-04-24 00:51:18 | [diff] [blame] | 147 | // 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] | 148 | // will be incomplete and generate invalid PDF files/documents. |
| 149 | if (context_.get()) { |
| 150 | CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1; |
| 151 | if (extra_retain_count > 0) { |
| 152 | LOG(ERROR) << "Metafile context has " << extra_retain_count |
| 153 | << " extra retain(s) on Close"; |
| 154 | } |
| 155 | } |
| 156 | #endif |
[email protected] | 1461948 | 2010-04-01 16:46:23 | [diff] [blame] | 157 | CGPDFContextClose(context_.get()); |
thestig | 3520b80 | 2016-04-04 22:12:52 | [diff] [blame] | 158 | context_.reset(); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 159 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 160 | } |
| 161 | |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 162 | bool PdfMetafileCg::RenderPage(unsigned int page_number, |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 163 | CGContextRef context, |
Lei Zhang | f69ea3b | 2020-03-24 17:53:48 | [diff] [blame] | 164 | const CGRect& rect, |
Daniel Hosseinian | 345ecfa | 2020-04-08 03:45:16 | [diff] [blame] | 165 | bool autorotate, |
| 166 | bool fit_to_page) const { |
thestig | 99fc213 | 2017-04-27 03:37:54 | [diff] [blame] | 167 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 168 | if (!pdf_doc) { |
| 169 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 170 | return false; |
| 171 | } |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 172 | |
Lei Zhang | c3f3466 | 2017-11-16 20:29:41 | [diff] [blame] | 173 | const unsigned int page_count = GetPageCount(); |
| 174 | DCHECK_NE(page_count, 0U); |
| 175 | DCHECK_NE(page_number, 0U); |
| 176 | DCHECK_LE(page_number, page_count); |
| 177 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 178 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
[email protected] | a09ef7e | 2011-09-28 21:59:33 | [diff] [blame] | 179 | CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox); |
Daniel Hosseinian | c3fd3d5 | 2020-04-08 04:03:17 | [diff] [blame] | 180 | const int pdf_src_rotation = CGPDFPageGetRotationAngle(pdf_page); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 181 | const bool source_is_landscape = |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 182 | (source_rect.size.width > source_rect.size.height); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 183 | const bool dest_is_landscape = (rect.size.width > rect.size.height); |
Daniel Hosseinian | 345ecfa | 2020-04-08 03:45:16 | [diff] [blame] | 184 | const bool rotate = autorotate && (source_is_landscape != dest_is_landscape); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 185 | const float source_width = |
| 186 | rotate ? source_rect.size.height : source_rect.size.width; |
| 187 | const float source_height = |
| 188 | rotate ? source_rect.size.width : source_rect.size.height; |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 189 | |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 190 | // See if we need to scale the output. |
Lei Zhang | 76446180 | 2017-11-15 23:36:07 | [diff] [blame] | 191 | float scaling_factor = 1.0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 192 | const bool scaling_needed = |
Daniel Hosseinian | 345ecfa | 2020-04-08 03:45:16 | [diff] [blame] | 193 | fit_to_page && ((source_width != rect.size.width) || |
| 194 | (source_height != rect.size.height)); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 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 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 201 | CGContextSaveGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 202 | |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 203 | int num_rotations = 0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 204 | if (rotate) { |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 205 | if (pdf_src_rotation == 0 || pdf_src_rotation == 270) { |
| 206 | num_rotations = 1; |
| 207 | } else { |
| 208 | num_rotations = 3; |
| 209 | } |
| 210 | } else { |
| 211 | if (pdf_src_rotation == 180 || pdf_src_rotation == 270) { |
| 212 | num_rotations = 2; |
| 213 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 214 | } |
thestig | 23fe5177 | 2014-09-27 17:40:51 | [diff] [blame] | 215 | RotatePage(context, rect, num_rotations); |
| 216 | |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 217 | CGContextScaleCTM(context, scaling_factor, scaling_factor); |
Daniel Hosseinian | c3fd3d5 | 2020-04-08 04:03:17 | [diff] [blame] | 218 | |
| 219 | // Some PDFs have a non-zero origin. Need to take that into account and align |
| 220 | // the PDF to the CoreGraphics's coordinate system origin. Also realign the |
| 221 | // contents from the bottom-left of the page to top-left in order to stay |
| 222 | // consistent with Print Preview. |
| 223 | // A rotational vertical offset is calculated to determine how much to offset |
| 224 | // the y-component of the origin to move the origin from bottom-left to |
| 225 | // top-right. When the source is not rotated, the offset is simply the |
| 226 | // difference between the paper height and the source height. When rotated, |
| 227 | // the y-axis of the source falls along the width of the source and paper, so |
| 228 | // the offset becomes the difference between the paper width and the source |
| 229 | // width. |
| 230 | const float rotational_vertical_offset = |
| 231 | rotate ? (rect.size.width - (scaling_factor * source_width)) |
| 232 | : (rect.size.height - (scaling_factor * source_height)); |
| 233 | const float x_origin_offset = -1 * source_rect.origin.x; |
| 234 | const float y_origin_offset = |
| 235 | rotational_vertical_offset - source_rect.origin.y; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 236 | CGContextTranslateCTM(context, x_origin_offset, y_origin_offset); |
| 237 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 238 | CGContextDrawPDFPage(context, pdf_page); |
| 239 | CGContextRestoreGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 240 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 244 | unsigned int PdfMetafileCg::GetPageCount() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 245 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 246 | return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0; |
| 247 | } |
| 248 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 249 | gfx::Rect PdfMetafileCg::GetPageBounds(unsigned int page_number) const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 250 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 251 | if (!pdf_doc) { |
| 252 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 253 | return gfx::Rect(); |
| 254 | } |
Lei Zhang | d67a66b2 | 2020-03-24 23:31:31 | [diff] [blame] | 255 | if (page_number == 0 || page_number > GetPageCount()) { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 256 | LOG(ERROR) << "Invalid page number: " << page_number; |
| 257 | return gfx::Rect(); |
| 258 | } |
| 259 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
| 260 | CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox); |
| 261 | return gfx::Rect(page_rect); |
| 262 | } |
| 263 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 264 | uint32_t PdfMetafileCg::GetDataSize() const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 265 | // PDF data is only valid/complete once the context is released. |
| 266 | DCHECK(!context_); |
| 267 | |
| 268 | if (!pdf_data_) |
| 269 | return 0; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 270 | return static_cast<uint32_t>(CFDataGetLength(pdf_data_)); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 271 | } |
| 272 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 273 | bool PdfMetafileCg::GetData(void* dst_buffer, uint32_t dst_buffer_size) const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 274 | // PDF data is only valid/complete once the context is released. |
| 275 | DCHECK(!context_); |
| 276 | DCHECK(pdf_data_); |
| 277 | DCHECK(dst_buffer); |
| 278 | DCHECK_GT(dst_buffer_size, 0U); |
| 279 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 280 | uint32_t data_size = GetDataSize(); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 281 | if (dst_buffer_size > data_size) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size), |
| 286 | static_cast<UInt8*>(dst_buffer)); |
| 287 | return true; |
| 288 | } |
| 289 | |
Alan Screen | e35d263 | 2021-12-02 18:44:43 | [diff] [blame] | 290 | bool PdfMetafileCg::ShouldCopySharedMemoryRegionData() const { |
| 291 | // Since `InitFromData()` copies the data, the caller doesn't have to. |
| 292 | return false; |
| 293 | } |
| 294 | |
Alan Screen | 313f508 | 2021-09-15 21:16:53 | [diff] [blame] | 295 | mojom::MetafileDataType PdfMetafileCg::GetDataType() const { |
| 296 | return mojom::MetafileDataType::kPDF; |
| 297 | } |
| 298 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 299 | CGContextRef PdfMetafileCg::context() const { |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 300 | return context_.get(); |
| 301 | } |
| 302 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 303 | CGPDFDocumentRef PdfMetafileCg::GetPDFDocument() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 304 | // Make sure that we have data, and that it's not being modified any more. |
| 305 | DCHECK(pdf_data_.get()); |
| 306 | DCHECK(!context_.get()); |
| 307 | |
| 308 | if (!pdf_doc_.get()) { |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 309 | ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 310 | CGDataProviderCreateWithCFData(pdf_data_)); |
| 311 | pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
| 312 | } |
| 313 | return pdf_doc_.get(); |
| 314 | } |
| 315 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 316 | } // namespace printing |