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