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_canvas.h" | 15 #include "cc/paint/paint_canvas.h" |
16 #include "cc/paint/paint_record.h" | 16 #include "cc/paint/paint_record.h" |
17 #include "cc/paint/paint_recorder.h" | 17 #include "cc/paint/paint_recorder.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 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
28 #include "printing/pdf_metafile_cg_mac.h" | 27 #include "printing/pdf_metafile_cg_mac.h" |
29 #endif | 28 #endif |
30 | 29 |
31 #if defined(OS_POSIX) | 30 #if defined(OS_POSIX) |
32 #include "base/file_descriptor_posix.h" | 31 #include "base/file_descriptor_posix.h" |
33 #endif | 32 #endif |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 bool WriteAssetToBuffer(const SkStreamAsset* asset, | 36 bool WriteAssetToBuffer(const SkStreamAsset* asset, |
38 void* buffer, | 37 void* buffer, |
39 size_t size) { | 38 size_t size) { |
40 // Calling duplicate() keeps original asset state unchanged. | 39 // Calling duplicate() keeps original asset state unchanged. |
41 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); | 40 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
42 size_t length = assetCopy->getLength(); | 41 size_t length = assetCopy->getLength(); |
43 if (length > size) | 42 if (length > size) |
44 return false; | 43 return false; |
45 return (length == assetCopy->read(buffer, length)); | 44 return (length == assetCopy->read(buffer, length)); |
46 } | 45 } |
47 | 46 |
48 SkTime::DateTime TimeToSkTime(base::Time time) { | |
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); | |
75 } | |
76 | |
77 } // namespace | 47 } // namespace |
78 | 48 |
79 namespace printing { | 49 namespace printing { |
80 | 50 |
81 struct Page { | 51 struct Page { |
82 Page(SkSize s, sk_sp<cc::PaintRecord> c) : size_(s), content_(std::move(c)) {} | 52 Page(SkSize s, sk_sp<cc::PaintRecord> c) : size_(s), content_(std::move(c)) {} |
83 Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} | 53 Page(Page&& that) : size_(that.size_), content_(std::move(that.content_)) {} |
84 Page(const Page&) = default; | 54 Page(const Page&) = default; |
85 Page& operator=(const Page&) = default; | 55 Page& operator=(const Page&) = default; |
86 Page& operator=(Page&& that) { | 56 Page& operator=(Page&& that) { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 if (data_->pdf_data_) | 155 if (data_->pdf_data_) |
186 return false; | 156 return false; |
187 | 157 |
188 if (data_->recorder_.getRecordingCanvas()) | 158 if (data_->recorder_.getRecordingCanvas()) |
189 FinishPage(); | 159 FinishPage(); |
190 | 160 |
191 SkDynamicMemoryWStream stream; | 161 SkDynamicMemoryWStream stream; |
192 sk_sp<SkDocument> doc; | 162 sk_sp<SkDocument> doc; |
193 switch (data_->type_) { | 163 switch (data_->type_) { |
194 case PDF_SKIA_DOCUMENT_TYPE: | 164 case PDF_SKIA_DOCUMENT_TYPE: |
195 doc = MakePdfDocument(&stream); | 165 doc = MakePdfDocument(printing::GetAgent(), &stream); |
196 break; | 166 break; |
197 case MSKP_SKIA_DOCUMENT_TYPE: | 167 case MSKP_SKIA_DOCUMENT_TYPE: |
198 doc = SkMakeMultiPictureDocument(&stream); | 168 doc = SkMakeMultiPictureDocument(&stream); |
199 break; | 169 break; |
200 } | 170 } |
201 | 171 |
202 for (const Page& page : data_->pages_) { | 172 for (const Page& page : data_->pages_) { |
203 cc::SkiaPaintCanvas canvas( | 173 cc::SkiaPaintCanvas canvas( |
204 doc->beginPage(page.size_.width(), page.size_.height())); | 174 doc->beginPage(page.size_.width(), page.size_.height())); |
205 canvas.drawPicture(page.content_); | 175 canvas.drawPicture(page.content_); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 | 293 |
324 metafile->data_->pages_.push_back(data_->pages_.back()); | 294 metafile->data_->pages_.push_back(data_->pages_.back()); |
325 | 295 |
326 if (!metafile->FinishDocument()) // Generate PDF. | 296 if (!metafile->FinishDocument()) // Generate PDF. |
327 metafile.reset(); | 297 metafile.reset(); |
328 | 298 |
329 return metafile; | 299 return metafile; |
330 } | 300 } |
331 | 301 |
332 } // namespace printing | 302 } // namespace printing |
OLD | NEW |