Chromium Code Reviews| 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/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "printing/print_settings.h" | 9 #include "printing/print_settings.h" |
| 10 #include "third_party/skia/include/core/SkDocument.h" | 10 #include "third_party/skia/include/core/SkDocument.h" |
| 11 #include "third_party/skia/include/core/SkPictureRecorder.h" | 11 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 12 #include "third_party/skia/include/core/SkStream.h" | 12 #include "third_party/skia/include/core/SkStream.h" |
| 13 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" | |
|
Lei Zhang
2016/06/13 21:01:34
The printing/DEPS rule just says "+third_party/ski
hal.canary
2016/06/14 16:34:49
Acknowledged. I have noted the situation.
| |
| 13 #include "ui/gfx/geometry/safe_integer_conversions.h" | 14 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 14 #include "ui/gfx/skia_util.h" | 15 #include "ui/gfx/skia_util.h" |
| 15 | 16 |
| 16 #if defined(OS_MACOSX) | 17 #if defined(OS_MACOSX) |
| 17 #include "printing/pdf_metafile_cg_mac.h" | 18 #include "printing/pdf_metafile_cg_mac.h" |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 #if defined(OS_POSIX) | 21 #if defined(OS_POSIX) |
| 21 #include "base/file_descriptor_posix.h" | 22 #include "base/file_descriptor_posix.h" |
| 22 #endif | 23 #endif |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 SkPictureRecorder recorder_; // Current recording | 86 SkPictureRecorder recorder_; // Current recording |
| 86 | 87 |
| 87 std::vector<Page> pages_; | 88 std::vector<Page> pages_; |
| 88 std::unique_ptr<SkStreamAsset> pdf_data_; | 89 std::unique_ptr<SkStreamAsset> pdf_data_; |
| 89 | 90 |
| 90 // The scale factor is used because Blink occasionally calls | 91 // The scale factor is used because Blink occasionally calls |
| 91 // SkCanvas::getTotalMatrix() even though the total matrix is not as | 92 // SkCanvas::getTotalMatrix() even though the total matrix is not as |
| 92 // meaningful for a vector canvas as for a raster canvas. | 93 // meaningful for a vector canvas as for a raster canvas. |
| 93 float scale_factor_; | 94 float scale_factor_; |
| 94 SkSize size_; | 95 SkSize size_; |
| 96 printing::SkiaDocumentType type_; | |
|
Lei Zhang
2016/06/13 21:01:34
Generally no need for $foo:: in namespace $foo.
hal.canary
2016/06/14 16:34:49
Done.
| |
| 95 | 97 |
| 96 #if defined(OS_MACOSX) | 98 #if defined(OS_MACOSX) |
| 97 PdfMetafileCg pdf_cg_; | 99 PdfMetafileCg pdf_cg_; |
| 98 #endif | 100 #endif |
| 99 }; | 101 }; |
| 100 | 102 |
| 101 PdfMetafileSkia::~PdfMetafileSkia() {} | 103 PdfMetafileSkia::~PdfMetafileSkia() {} |
| 102 | 104 |
| 103 bool PdfMetafileSkia::Init() { | 105 bool PdfMetafileSkia::Init() { |
| 104 return true; | 106 return true; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 StartPage(page_size, content_area, scale_factor); | 152 StartPage(page_size, content_area, scale_factor); |
| 151 return data_->recorder_.getRecordingCanvas(); | 153 return data_->recorder_.getRecordingCanvas(); |
| 152 } | 154 } |
| 153 | 155 |
| 154 bool PdfMetafileSkia::FinishPage() { | 156 bool PdfMetafileSkia::FinishPage() { |
| 155 if (!data_->recorder_.getRecordingCanvas()) | 157 if (!data_->recorder_.getRecordingCanvas()) |
| 156 return false; | 158 return false; |
| 157 | 159 |
| 158 sk_sp<SkPicture> pic = data_->recorder_.finishRecordingAsPicture(); | 160 sk_sp<SkPicture> pic = data_->recorder_.finishRecordingAsPicture(); |
| 159 if (data_->scale_factor_ != 1.0f) { | 161 if (data_->scale_factor_ != 1.0f) { |
| 160 SkCanvas* canvas = | 162 SkCanvas* canvas = data_->recorder_.beginRecording(data_->size_.width(), |
| 161 data_->recorder_.beginRecording(data_->size_.width(), | 163 data_->size_.height()); |
| 162 data_->size_.height()); | |
| 163 canvas->scale(data_->scale_factor_, data_->scale_factor_); | 164 canvas->scale(data_->scale_factor_, data_->scale_factor_); |
| 164 canvas->drawPicture(pic); | 165 canvas->drawPicture(pic); |
| 165 pic = data_->recorder_.finishRecordingAsPicture(); | 166 pic = data_->recorder_.finishRecordingAsPicture(); |
| 166 } | 167 } |
| 167 data_->pages_.emplace_back(data_->size_, std::move(pic)); | 168 data_->pages_.emplace_back(data_->size_, std::move(pic)); |
| 168 return true; | 169 return true; |
| 169 } | 170 } |
| 170 | 171 |
| 171 bool PdfMetafileSkia::FinishDocument() { | 172 bool PdfMetafileSkia::FinishDocument() { |
| 172 // If we've already set the data in InitFromData, leave it be. | 173 // If we've already set the data in InitFromData, leave it be. |
| 173 if (data_->pdf_data_) | 174 if (data_->pdf_data_) |
| 174 return false; | 175 return false; |
| 175 | 176 |
| 176 if (data_->recorder_.getRecordingCanvas()) | 177 if (data_->recorder_.getRecordingCanvas()) |
| 177 FinishPage(); | 178 FinishPage(); |
| 178 | 179 |
| 179 SkDynamicMemoryWStream stream; | 180 SkDynamicMemoryWStream stream; |
| 180 // TODO(halcanary): support more document types (XPS, a sequence of display | 181 sk_sp<SkDocument> doc; |
| 181 // lists). | 182 switch (data_->type_) { |
| 182 sk_sp<SkDocument> doc = MakePdfDocument(&stream); | 183 case PDF_SKIA_DOCUMENT_TYPE: |
| 184 doc = MakePdfDocument(&stream); | |
| 185 break; | |
| 186 case MSKP_SKIA_DOCUMENT_TYPE: | |
| 187 doc = SkMakeMultiPictureDocument(&stream); | |
| 188 break; | |
| 189 default: | |
|
Lei Zhang
2016/06/13 21:01:34
No need. I believe if you add a new enum value and
hal.canary
2016/06/14 16:34:49
Done.
| |
| 190 NOTREACHED(); | |
| 191 return false; | |
| 192 } | |
| 183 | 193 |
| 184 for (const Page& page : data_->pages_) { | 194 for (const Page& page : data_->pages_) { |
| 185 SkCanvas* canvas = doc->beginPage(page.size_.width(), page.size_.height()); | 195 SkCanvas* canvas = doc->beginPage(page.size_.width(), page.size_.height()); |
| 186 canvas->drawPicture(page.content_); | 196 canvas->drawPicture(page.content_); |
| 187 doc->endPage(); | 197 doc->endPage(); |
| 188 } | 198 } |
| 189 if (!doc->close()) | 199 if (!doc->close()) |
| 190 return false; | 200 return false; |
| 191 | 201 |
| 192 data_->pdf_data_.reset(stream.detachAsStream()); | 202 data_->pdf_data_.reset(stream.detachAsStream()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 DCHECK_GE(buffer.size(), read_size); | 290 DCHECK_GE(buffer.size(), read_size); |
| 281 if (!file->WriteAtCurrentPos(&buffer[0], | 291 if (!file->WriteAtCurrentPos(&buffer[0], |
| 282 base::checked_cast<int>(read_size))) { | 292 base::checked_cast<int>(read_size))) { |
| 283 return false; | 293 return false; |
| 284 } | 294 } |
| 285 } while (!asset->isAtEnd()); | 295 } while (!asset->isAtEnd()); |
| 286 | 296 |
| 287 return true; | 297 return true; |
| 288 } | 298 } |
| 289 | 299 |
| 290 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { | 300 PdfMetafileSkia::PdfMetafileSkia(printing::SkiaDocumentType type) |
| 301 : data_(new PdfMetafileSkiaData) { | |
| 302 data_->type_ = type; | |
| 291 } | 303 } |
| 292 | 304 |
| 293 std::unique_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { | 305 std::unique_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage( |
| 306 printing::SkiaDocumentType type) { | |
| 294 // If we only ever need the metafile for the last page, should we | 307 // If we only ever need the metafile for the last page, should we |
| 295 // only keep a handle on one SkPicture? | 308 // only keep a handle on one SkPicture? |
| 296 std::unique_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); | 309 std::unique_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia(type)); |
| 297 | 310 |
| 298 if (data_->pages_.size() == 0) | 311 if (data_->pages_.size() == 0) |
| 299 return metafile; | 312 return metafile; |
| 300 | 313 |
| 301 if (data_->recorder_.getRecordingCanvas()) // page outstanding | 314 if (data_->recorder_.getRecordingCanvas()) // page outstanding |
| 302 return metafile; | 315 return metafile; |
| 303 | 316 |
| 304 metafile->data_->pages_.push_back(data_->pages_.back()); | 317 metafile->data_->pages_.push_back(data_->pages_.back()); |
| 305 | 318 |
| 306 if (!metafile->FinishDocument()) // Generate PDF. | 319 if (!metafile->FinishDocument()) // Generate PDF. |
| 307 metafile.reset(); | 320 metafile.reset(); |
| 308 | 321 |
| 309 return metafile; | 322 return metafile; |
| 310 } | 323 } |
| 311 | 324 |
| 312 } // namespace printing | 325 } // namespace printing |
| OLD | NEW |