[email protected] | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [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 | |
| 5 | #include "printing/pdf_metafile_skia.h" |
| 6 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 7 | #include "base/files/file.h" |
halcanary | 67ce00b8 | 2015-09-27 21:59:53 | [diff] [blame] | 8 | #include "base/time/time.h" |
halcanary | 73b63fd | 2015-11-06 00:02:14 | [diff] [blame] | 9 | #include "printing/print_settings.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 10 | #include "third_party/skia/include/core/SkDocument.h" |
| 11 | #include "third_party/skia/include/core/SkPictureRecorder.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 12 | #include "third_party/skia/include/core/SkStream.h" |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 13 | #include "ui/gfx/geometry/safe_integer_conversions.h" |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 14 | #include "ui/gfx/skia_util.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 15 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 16 | #if defined(OS_MACOSX) |
| 17 | #include "printing/pdf_metafile_cg_mac.h" |
| 18 | #endif |
| 19 | |
[email protected] | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 20 | #if defined(OS_POSIX) |
| 21 | #include "base/file_descriptor_posix.h" |
| 22 | #endif |
| 23 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 24 | namespace { |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 25 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 26 | bool WriteAssetToBuffer(const SkStreamAsset* asset, |
| 27 | void* buffer, |
| 28 | size_t size) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 29 | // Calling duplicate() keeps original asset state unchanged. |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 30 | std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 31 | size_t length = assetCopy->getLength(); |
| 32 | if (length > size) |
| 33 | return false; |
| 34 | return (length == assetCopy->read(buffer, length)); |
| 35 | } |
| 36 | |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 37 | SkTime::DateTime TimeToSkTime(base::Time time) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 38 | base::Time::Exploded exploded; |
| 39 | time.UTCExplode(&exploded); |
| 40 | SkTime::DateTime skdate; |
| 41 | skdate.fTimeZoneMinutes = 0; |
| 42 | skdate.fYear = exploded.year; |
| 43 | skdate.fMonth = exploded.month; |
| 44 | skdate.fDayOfWeek = exploded.day_of_week; |
| 45 | skdate.fDay = exploded.day_of_month; |
| 46 | skdate.fHour = exploded.hour; |
| 47 | skdate.fMinute = exploded.minute; |
| 48 | skdate.fSecond = exploded.second; |
| 49 | return skdate; |
| 50 | } |
| 51 | |
| 52 | sk_sp<SkDocument> MakePdfDocument(SkWStream* wStream) { |
| 53 | SkDocument::PDFMetadata metadata; |
| 54 | SkTime::DateTime now = TimeToSkTime(base::Time::Now()); |
| 55 | metadata.fCreation.fEnabled = true; |
| 56 | metadata.fCreation.fDateTime = now; |
| 57 | metadata.fModified.fEnabled = true; |
| 58 | metadata.fModified.fDateTime = now; |
| 59 | const std::string& agent = printing::GetAgent(); |
| 60 | metadata.fCreator = agent.empty() ? SkString("Chromium") |
| 61 | : SkString(agent.c_str(), agent.size()); |
| 62 | return SkDocument::MakePDF(wStream, SK_ScalarDefaultRasterDPI, metadata, |
| 63 | nullptr, false); |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 64 | } |
| 65 | |
thestig | e3f5f5d4 | 2015-07-16 19:46:24 | [diff] [blame] | 66 | } // namespace |
| 67 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 68 | namespace printing { |
| 69 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 70 | struct Page { |
| 71 | Page(SkSize s, sk_sp<SkPicture> c) : size_(s), content_(std::move(c)) {} |
| 72 | Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} |
| 73 | Page(const Page&) = default; |
| 74 | Page& operator=(const Page&) = default; |
| 75 | Page& operator=(Page&& that) { |
| 76 | size_ = that.size_; |
| 77 | content_ = std::move(that.content_); |
| 78 | return *this; |
| 79 | } |
| 80 | SkSize size_; |
| 81 | sk_sp<SkPicture> content_; |
| 82 | }; |
| 83 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 84 | struct PdfMetafileSkiaData { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 85 | SkPictureRecorder recorder_; // Current recording |
| 86 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 87 | std::vector<Page> pages_; |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 88 | std::unique_ptr<SkStreamAsset> pdf_data_; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 89 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 90 | // The scale factor is used because Blink occasionally calls |
| 91 | // SkCanvas::getTotalMatrix() even though the total matrix is not as |
| 92 | // meaningful for a vector canvas as for a raster canvas. |
| 93 | float scale_factor_; |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 94 | SkSize size_; |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 95 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 96 | #if defined(OS_MACOSX) |
| 97 | PdfMetafileCg pdf_cg_; |
| 98 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | PdfMetafileSkia::~PdfMetafileSkia() {} |
| 102 | |
| 103 | bool PdfMetafileSkia::Init() { |
| 104 | return true; |
| 105 | } |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 106 | |
| 107 | // TODO(halcanary): Create a Metafile class that only stores data. |
| 108 | // Metafile::InitFromData is orthogonal to what the rest of |
| 109 | // PdfMetafileSkia does. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 110 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 111 | uint32_t src_buffer_size) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 112 | data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true)); |
| 113 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 114 | } |
| 115 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 116 | void PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 117 | const gfx::Rect& content_area, |
| 118 | const float& scale_factor) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 119 | DCHECK_GT(page_size.width(), 0); |
| 120 | DCHECK_GT(page_size.height(), 0); |
| 121 | DCHECK_GT(scale_factor, 0.0f); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 122 | if (data_->recorder_.getRecordingCanvas()) |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 123 | FinishPage(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 124 | DCHECK(!data_->recorder_.getRecordingCanvas()); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 125 | |
| 126 | float inverse_scale = 1.0 / scale_factor; |
| 127 | SkCanvas* canvas = data_->recorder_.beginRecording( |
| 128 | inverse_scale * page_size.width(), inverse_scale * page_size.height()); |
| 129 | if (content_area != gfx::Rect(page_size)) { |
| 130 | canvas->scale(inverse_scale, inverse_scale); |
| 131 | SkRect sk_content_area = gfx::RectToSkRect(content_area); |
| 132 | canvas->clipRect(sk_content_area); |
| 133 | canvas->translate(sk_content_area.x(), sk_content_area.y()); |
| 134 | canvas->scale(scale_factor, scale_factor); |
| 135 | } |
| 136 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 137 | data_->size_ = gfx::SizeFToSkSize(gfx::SizeF(page_size)); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 138 | data_->scale_factor_ = scale_factor; |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 139 | // We scale the recording canvas's size so that |
| 140 | // canvas->getTotalMatrix() returns a value that ignores the scale |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 141 | // factor. We store the scale factor and re-apply it later. |
| 142 | // http://crbug.com/469656 |
halcanary | 5baa8fb4 | 2015-04-03 21:39:30 | [diff] [blame] | 143 | // Recording canvas is owned by the data_->recorder_. No ref() necessary. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 144 | } |
| 145 | |
tomhudson | ed673beb | 2016-01-28 14:14:22 | [diff] [blame] | 146 | SkCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 147 | const gfx::Size& page_size, |
| 148 | const gfx::Rect& content_area, |
| 149 | const float& scale_factor) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 150 | StartPage(page_size, content_area, scale_factor); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 151 | return data_->recorder_.getRecordingCanvas(); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool PdfMetafileSkia::FinishPage() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 155 | if (!data_->recorder_.getRecordingCanvas()) |
| 156 | return false; |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 157 | |
| 158 | sk_sp<SkPicture> pic = data_->recorder_.finishRecordingAsPicture(); |
| 159 | if (data_->scale_factor_ != 1.0f) { |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 160 | SkCanvas* canvas = |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 161 | data_->recorder_.beginRecording(data_->size_.width(), |
| 162 | data_->size_.height()); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 163 | canvas->scale(data_->scale_factor_, data_->scale_factor_); |
| 164 | canvas->drawPicture(pic); |
| 165 | pic = data_->recorder_.finishRecordingAsPicture(); |
| 166 | } |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 167 | data_->pages_.emplace_back(data_->size_, std::move(pic)); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
| 171 | bool PdfMetafileSkia::FinishDocument() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 172 | // If we've already set the data in InitFromData, leave it be. |
| 173 | if (data_->pdf_data_) |
| 174 | return false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 175 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 176 | if (data_->recorder_.getRecordingCanvas()) |
thestig | 5ff7db2 | 2016-02-13 04:42:45 | [diff] [blame] | 177 | FinishPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 178 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 179 | SkDynamicMemoryWStream stream; |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 180 | // TODO(halcanary): support more document types (XPS, a sequence of display |
| 181 | // lists). |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 182 | sk_sp<SkDocument> doc = MakePdfDocument(&stream); |
halcanary | fb23228 | 2016-04-28 18:34:24 | [diff] [blame] | 183 | |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 184 | for (const Page& page : data_->pages_) { |
| 185 | SkCanvas* canvas = doc->beginPage(page.size_.width(), page.size_.height()); |
| 186 | canvas->drawPicture(page.content_); |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 187 | doc->endPage(); |
halcanary | 0aeeb333 | 2016-03-18 14:32:39 | [diff] [blame] | 188 | } |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 189 | if (!doc->close()) |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 190 | return false; |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 191 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 192 | data_->pdf_data_.reset(stream.detachAsStream()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 193 | return true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 194 | } |
| 195 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 196 | uint32_t PdfMetafileSkia::GetDataSize() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 197 | if (!data_->pdf_data_) |
| 198 | return 0; |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 199 | return base::checked_cast<uint32_t>(data_->pdf_data_->getLength()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 203 | uint32_t dst_buffer_size) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 204 | if (!data_->pdf_data_) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 205 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 206 | return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
| 207 | base::checked_cast<size_t>(dst_buffer_size)); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 210 | gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 211 | if (page_number < data_->pages_.size()) { |
halcanary | 8dd6f024 | 2016-06-13 19:40:23 | [diff] [blame^] | 212 | SkSize size = data_->pages_[page_number].size_; |
| 213 | return gfx::Rect(gfx::ToRoundedInt(size.width()), |
| 214 | gfx::ToRoundedInt(size.height())); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 215 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 216 | return gfx::Rect(); |
| 217 | } |
| 218 | |
| 219 | unsigned int PdfMetafileSkia::GetPageCount() const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 220 | return base::checked_cast<unsigned int>(data_->pages_.size()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 224 | NOTREACHED(); |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 225 | return nullptr; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 226 | } |
| 227 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 228 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 229 | #if defined(OS_WIN) |
| 230 | bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 231 | const RECT* rect) const { |
| 232 | NOTREACHED(); |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
| 237 | NOTREACHED(); |
| 238 | return false; |
| 239 | } |
| 240 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 241 | #elif defined(OS_MACOSX) |
| 242 | /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 243 | rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 244 | the drawing of the PDF into the canvas may result in a rasterized output. |
| 245 | PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 246 | should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 247 | http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc |
| 248 | */ |
| 249 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 250 | CGContextRef context, |
| 251 | const CGRect rect, |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 252 | const MacRenderPageParams& params) const { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 253 | DCHECK_GT(GetDataSize(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 254 | if (data_->pdf_cg_.GetDataSize() == 0) { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 255 | if (GetDataSize() == 0) |
| 256 | return false; |
| 257 | size_t length = data_->pdf_data_->getLength(); |
| 258 | std::vector<uint8_t> buffer(length); |
| 259 | (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 260 | data_->pdf_cg_.InitFromData(&buffer[0], |
| 261 | base::checked_cast<uint32_t>(length)); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 262 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 263 | return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 264 | } |
| 265 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 266 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 267 | bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 268 | if (GetDataSize() == 0U) |
| 269 | return false; |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 270 | |
| 271 | // Calling duplicate() keeps original asset state unchanged. |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 272 | std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 273 | |
thestig | 192677ec | 2016-06-09 07:43:17 | [diff] [blame] | 274 | const size_t kMaximumBufferSize = 1024 * 1024; |
| 275 | std::vector<char> buffer(std::min(kMaximumBufferSize, asset->getLength())); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 276 | do { |
| 277 | size_t read_size = asset->read(&buffer[0], buffer.size()); |
| 278 | if (read_size == 0) |
| 279 | break; |
| 280 | DCHECK_GE(buffer.size(), read_size); |
| 281 | if (!file->WriteAtCurrentPos(&buffer[0], |
| 282 | base::checked_cast<int>(read_size))) { |
| 283 | return false; |
| 284 | } |
| 285 | } while (!asset->isAtEnd()); |
| 286 | |
| 287 | return true; |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 288 | } |
| 289 | |
halcanary | 5be808e | 2014-11-10 22:20:05 | [diff] [blame] | 290 | PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { |
[email protected] | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 291 | } |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 292 | |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 293 | std::unique_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 294 | // If we only ever need the metafile for the last page, should we |
| 295 | // only keep a handle on one SkPicture? |
dcheng | c3df9ba | 2016-04-07 23:09:32 | [diff] [blame] | 296 | std::unique_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 297 | |
| 298 | if (data_->pages_.size() == 0) |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 299 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 300 | |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 301 | if (data_->recorder_.getRecordingCanvas()) // page outstanding |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 302 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 303 | |
halcanary | 223f70a | 2016-06-09 00:07:42 | [diff] [blame] | 304 | metafile->data_->pages_.push_back(data_->pages_.back()); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 305 | |
| 306 | if (!metafile->FinishDocument()) // Generate PDF. |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame] | 307 | metafile.reset(); |
halcanary | ff21476 | 2015-01-22 20:34:32 | [diff] [blame] | 308 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 309 | return metafile; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 310 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 311 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 312 | } // namespace printing |