| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/pdf_metafile_skia.h" | 5 #include "printing/pdf_metafile_skia.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "cc/paint/paint_record.h" | 15 #include "cc/paint/paint_record.h" |
| 16 #include "cc/paint/paint_recorder.h" | 16 #include "cc/paint/paint_recorder.h" |
| 17 #include "cc/paint/skia_paint_canvas.h" | 17 #include "cc/paint/skia_paint_canvas.h" |
| 18 #include "printing/print_settings.h" | 18 #include "printing/print_settings.h" |
| 19 #include "third_party/skia/include/core/SkDocument.h" | |
| 20 #include "third_party/skia/include/core/SkStream.h" | 19 #include "third_party/skia/include/core/SkStream.h" |
| 21 // Note that headers in third_party/skia/src are fragile. This is | 20 // Note that headers in third_party/skia/src are fragile. This is |
| 22 // an experimental, fragile, and diagnostic-only document type. | 21 // an experimental, fragile, and diagnostic-only document type. |
| 23 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" | 22 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" |
| 24 #include "ui/gfx/geometry/safe_integer_conversions.h" | 23 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 25 #include "ui/gfx/skia_util.h" | 24 #include "ui/gfx/skia_util.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 bool WriteAssetToBuffer(const SkStreamAsset* asset, | 28 bool WriteAssetToBuffer(const SkStreamAsset* asset, |
| 30 void* buffer, | 29 void* buffer, |
| 31 size_t size) { | 30 size_t size) { |
| 32 // Calling duplicate() keeps original asset state unchanged. | 31 // Calling duplicate() keeps original asset state unchanged. |
| 33 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); | 32 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
| 34 size_t length = assetCopy->getLength(); | 33 size_t length = assetCopy->getLength(); |
| 35 if (length > size) | 34 if (length > size) |
| 36 return false; | 35 return false; |
| 37 return (length == assetCopy->read(buffer, length)); | 36 return (length == assetCopy->read(buffer, length)); |
| 38 } | 37 } |
| 39 | 38 |
| 40 SkTime::DateTime TimeToSkTime(base::Time time) { | |
| 41 base::Time::Exploded exploded; | |
| 42 time.UTCExplode(&exploded); | |
| 43 SkTime::DateTime skdate; | |
| 44 skdate.fTimeZoneMinutes = 0; | |
| 45 skdate.fYear = exploded.year; | |
| 46 skdate.fMonth = exploded.month; | |
| 47 skdate.fDayOfWeek = exploded.day_of_week; | |
| 48 skdate.fDay = exploded.day_of_month; | |
| 49 skdate.fHour = exploded.hour; | |
| 50 skdate.fMinute = exploded.minute; | |
| 51 skdate.fSecond = exploded.second; | |
| 52 return skdate; | |
| 53 } | |
| 54 | |
| 55 sk_sp<SkDocument> MakePdfDocument(SkWStream* wStream) { | |
| 56 SkDocument::PDFMetadata metadata; | |
| 57 SkTime::DateTime now = TimeToSkTime(base::Time::Now()); | |
| 58 metadata.fCreation.fEnabled = true; | |
| 59 metadata.fCreation.fDateTime = now; | |
| 60 metadata.fModified.fEnabled = true; | |
| 61 metadata.fModified.fDateTime = now; | |
| 62 const std::string& agent = printing::GetAgent(); | |
| 63 metadata.fCreator = agent.empty() ? SkString("Chromium") | |
| 64 : SkString(agent.c_str(), agent.size()); | |
| 65 return SkDocument::MakePDF(wStream, SK_ScalarDefaultRasterDPI, metadata, | |
| 66 nullptr, false); | |
| 67 } | |
| 68 | |
| 69 } // namespace | 39 } // namespace |
| 70 | 40 |
| 71 namespace printing { | 41 namespace printing { |
| 72 | 42 |
| 73 struct Page { | 43 struct Page { |
| 74 Page(SkSize s, sk_sp<cc::PaintRecord> c) : size_(s), content_(std::move(c)) {} | 44 Page(SkSize s, sk_sp<cc::PaintRecord> c) : size_(s), content_(std::move(c)) {} |
| 75 Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} | 45 Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} |
| 76 Page(const Page&) = default; | 46 Page(const Page&) = default; |
| 77 Page& operator=(const Page&) = default; | 47 Page& operator=(const Page&) = default; |
| 78 Page& operator=(Page&& that) { | 48 Page& operator=(Page&& that) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 if (data_->pdf_data_) | 143 if (data_->pdf_data_) |
| 174 return false; | 144 return false; |
| 175 | 145 |
| 176 if (data_->recorder_.getRecordingCanvas()) | 146 if (data_->recorder_.getRecordingCanvas()) |
| 177 FinishPage(); | 147 FinishPage(); |
| 178 | 148 |
| 179 SkDynamicMemoryWStream stream; | 149 SkDynamicMemoryWStream stream; |
| 180 sk_sp<SkDocument> doc; | 150 sk_sp<SkDocument> doc; |
| 181 switch (data_->type_) { | 151 switch (data_->type_) { |
| 182 case PDF_SKIA_DOCUMENT_TYPE: | 152 case PDF_SKIA_DOCUMENT_TYPE: |
| 183 doc = MakePdfDocument(&stream); | 153 doc = MakePdfDocument(printing::GetAgent(), &stream); |
| 184 break; | 154 break; |
| 185 case MSKP_SKIA_DOCUMENT_TYPE: | 155 case MSKP_SKIA_DOCUMENT_TYPE: |
| 186 doc = SkMakeMultiPictureDocument(&stream); | 156 doc = SkMakeMultiPictureDocument(&stream); |
| 187 break; | 157 break; |
| 188 } | 158 } |
| 189 | 159 |
| 190 for (const Page& page : data_->pages_) { | 160 for (const Page& page : data_->pages_) { |
| 191 cc::SkiaPaintCanvas canvas( | 161 cc::SkiaPaintCanvas canvas( |
| 192 doc->beginPage(page.size_.width(), page.size_.height())); | 162 doc->beginPage(page.size_.width(), page.size_.height())); |
| 193 canvas.drawPicture(page.content_); | 163 canvas.drawPicture(page.content_); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 251 |
| 282 metafile->data_->pages_.push_back(data_->pages_.back()); | 252 metafile->data_->pages_.push_back(data_->pages_.back()); |
| 283 | 253 |
| 284 if (!metafile->FinishDocument()) // Generate PDF. | 254 if (!metafile->FinishDocument()) // Generate PDF. |
| 285 metafile.reset(); | 255 metafile.reset(); |
| 286 | 256 |
| 287 return metafile; | 257 return metafile; |
| 288 } | 258 } |
| 289 | 259 |
| 290 } // namespace printing | 260 } // namespace printing |
| OLD | NEW |