| 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 "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/time/time.h" | |
| 13 #include "skia/ext/refptr.h" | 12 #include "skia/ext/refptr.h" |
| 14 #include "third_party/skia/include/core/SkData.h" | 13 #include "third_party/skia/include/core/SkData.h" |
| 15 #include "third_party/skia/include/core/SkDocument.h" | 14 #include "third_party/skia/include/core/SkDocument.h" |
| 16 #include "third_party/skia/include/core/SkPictureRecorder.h" | 15 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 17 #include "third_party/skia/include/core/SkRect.h" | 16 #include "third_party/skia/include/core/SkRect.h" |
| 18 #include "third_party/skia/include/core/SkRefCnt.h" | 17 #include "third_party/skia/include/core/SkRefCnt.h" |
| 19 #include "third_party/skia/include/core/SkScalar.h" | 18 #include "third_party/skia/include/core/SkScalar.h" |
| 20 #include "third_party/skia/include/core/SkSize.h" | 19 #include "third_party/skia/include/core/SkSize.h" |
| 21 #include "third_party/skia/include/core/SkStream.h" | 20 #include "third_party/skia/include/core/SkStream.h" |
| 22 #include "ui/gfx/geometry/point.h" | 21 #include "ui/gfx/geometry/point.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 120 |
| 122 bool PdfMetafileSkia::FinishPage() { | 121 bool PdfMetafileSkia::FinishPage() { |
| 123 if (!data_->recorder_.getRecordingCanvas()) | 122 if (!data_->recorder_.getRecordingCanvas()) |
| 124 return false; | 123 return false; |
| 125 DCHECK(!(data_->pages_.back().content_)); | 124 DCHECK(!(data_->pages_.back().content_)); |
| 126 data_->pages_.back().content_ = | 125 data_->pages_.back().content_ = |
| 127 skia::AdoptRef(data_->recorder_.endRecordingAsPicture()); | 126 skia::AdoptRef(data_->recorder_.endRecordingAsPicture()); |
| 128 return true; | 127 return true; |
| 129 } | 128 } |
| 130 | 129 |
| 131 static SkTime::DateTime TimeToSkTime(base::Time time) { | |
| 132 base::Time::Exploded exploded; | |
| 133 time.UTCExplode(&exploded); | |
| 134 SkTime::DateTime skdate; | |
| 135 skdate.fTimeZoneMinutes = 0; | |
| 136 skdate.fYear = exploded.year; | |
| 137 skdate.fMonth = exploded.month; | |
| 138 skdate.fDayOfWeek = exploded.day_of_week; | |
| 139 skdate.fDay = exploded.day_of_month; | |
| 140 skdate.fHour = exploded.hour; | |
| 141 skdate.fMinute = exploded.minute; | |
| 142 skdate.fSecond = exploded.second; | |
| 143 return skdate; | |
| 144 } | |
| 145 | |
| 146 bool PdfMetafileSkia::FinishDocument() { | 130 bool PdfMetafileSkia::FinishDocument() { |
| 147 // If we've already set the data in InitFromData, leave it be. | 131 // If we've already set the data in InitFromData, leave it be. |
| 148 if (data_->pdf_data_) | 132 if (data_->pdf_data_) |
| 149 return false; | 133 return false; |
| 150 | 134 |
| 151 if (data_->recorder_.getRecordingCanvas()) | 135 if (data_->recorder_.getRecordingCanvas()) |
| 152 this->FinishPage(); | 136 this->FinishPage(); |
| 153 | 137 |
| 154 SkDynamicMemoryWStream pdf_stream; | 138 SkDynamicMemoryWStream pdf_stream; |
| 155 skia::RefPtr<SkDocument> pdf_doc = | 139 skia::RefPtr<SkDocument> pdf_doc = |
| 156 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); | 140 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); |
| 157 for (const auto& page : data_->pages_) { | 141 for (const auto& page : data_->pages_) { |
| 158 SkCanvas* canvas = pdf_doc->beginPage( | 142 SkCanvas* canvas = pdf_doc->beginPage( |
| 159 page.page_size_.width(), page.page_size_.height(), &page.content_area_); | 143 page.page_size_.width(), page.page_size_.height(), &page.content_area_); |
| 160 // No need to save/restore, since this canvas is not reused after endPage() | 144 // No need to save/restore, since this canvas is not reused after endPage() |
| 161 canvas->scale(page.scale_factor_, page.scale_factor_); | 145 canvas->scale(page.scale_factor_, page.scale_factor_); |
| 162 canvas->drawPicture(page.content_.get()); | 146 canvas->drawPicture(page.content_.get()); |
| 163 pdf_doc->endPage(); | 147 pdf_doc->endPage(); |
| 164 } | 148 } |
| 165 SkTArray<SkDocument::Attribute> info; | |
| 166 info.emplace_back(SkString("Creator"), SkString("Chromium")); | |
| 167 SkTime::DateTime now = TimeToSkTime(base::Time::Now()); | |
| 168 pdf_doc->setMetadata(info, &now, &now); | |
| 169 if (!pdf_doc->close()) | 149 if (!pdf_doc->close()) |
| 170 return false; | 150 return false; |
| 171 | 151 |
| 172 data_->pdf_data_.reset(pdf_stream.detachAsStream()); | 152 data_->pdf_data_.reset(pdf_stream.detachAsStream()); |
| 173 return true; | 153 return true; |
| 174 } | 154 } |
| 175 | 155 |
| 176 uint32_t PdfMetafileSkia::GetDataSize() const { | 156 uint32_t PdfMetafileSkia::GetDataSize() const { |
| 177 if (!data_->pdf_data_) | 157 if (!data_->pdf_data_) |
| 178 return 0; | 158 return 0; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 281 |
| 302 metafile->data_->pages_.push_back(page); | 282 metafile->data_->pages_.push_back(page); |
| 303 | 283 |
| 304 if (!metafile->FinishDocument()) // Generate PDF. | 284 if (!metafile->FinishDocument()) // Generate PDF. |
| 305 metafile.reset(); | 285 metafile.reset(); |
| 306 | 286 |
| 307 return metafile.Pass(); | 287 return metafile.Pass(); |
| 308 } | 288 } |
| 309 | 289 |
| 310 } // namespace printing | 290 } // namespace printing |
| OLD | NEW |