| 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> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 #include <vector> |
| 11 |
| 7 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/memory/ptr_util.h" |
| 8 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 9 #include "printing/print_settings.h" | 15 #include "printing/print_settings.h" |
| 10 #include "third_party/skia/include/core/SkDocument.h" | 16 #include "third_party/skia/include/core/SkDocument.h" |
| 11 #include "third_party/skia/include/core/SkPictureRecorder.h" | 17 #include "third_party/skia/include/core/SkPictureRecorder.h" |
| 12 #include "third_party/skia/include/core/SkStream.h" | 18 #include "third_party/skia/include/core/SkStream.h" |
| 13 // Note that headers in third_party/skia/src are fragile. This is | 19 // Note that headers in third_party/skia/src are fragile. This is |
| 14 // an experimental, fragile, and diagnostic-only document type. | 20 // an experimental, fragile, and diagnostic-only document type. |
| 15 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" | 21 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" |
| 16 #include "ui/gfx/geometry/safe_integer_conversions.h" | 22 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 17 #include "ui/gfx/skia_util.h" | 23 #include "ui/gfx/skia_util.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 PdfMetafileSkia::~PdfMetafileSkia() {} | 111 PdfMetafileSkia::~PdfMetafileSkia() {} |
| 106 | 112 |
| 107 bool PdfMetafileSkia::Init() { | 113 bool PdfMetafileSkia::Init() { |
| 108 return true; | 114 return true; |
| 109 } | 115 } |
| 110 | 116 |
| 111 // TODO(halcanary): Create a Metafile class that only stores data. | 117 // TODO(halcanary): Create a Metafile class that only stores data. |
| 112 // Metafile::InitFromData is orthogonal to what the rest of | 118 // Metafile::InitFromData is orthogonal to what the rest of |
| 113 // PdfMetafileSkia does. | 119 // PdfMetafileSkia does. |
| 114 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 120 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 115 uint32_t src_buffer_size) { | 121 size_t src_buffer_size) { |
| 116 data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true)); | 122 data_->pdf_data_ = base::MakeUnique<SkMemoryStream>( |
| 123 src_buffer, src_buffer_size, true /* copy_data? */); |
| 117 return true; | 124 return true; |
| 118 } | 125 } |
| 119 | 126 |
| 120 void PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 127 void PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
| 121 const gfx::Rect& content_area, | 128 const gfx::Rect& content_area, |
| 122 const float& scale_factor) { | 129 const float& scale_factor) { |
| 123 DCHECK_GT(page_size.width(), 0); | 130 DCHECK_GT(page_size.width(), 0); |
| 124 DCHECK_GT(page_size.height(), 0); | 131 DCHECK_GT(page_size.height(), 0); |
| 125 DCHECK_GT(scale_factor, 0.0f); | 132 DCHECK_GT(scale_factor, 0.0f); |
| 126 if (data_->recorder_.getRecordingCanvas()) | 133 if (data_->recorder_.getRecordingCanvas()) |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 CGContextRef context, | 265 CGContextRef context, |
| 259 const CGRect rect, | 266 const CGRect rect, |
| 260 const MacRenderPageParams& params) const { | 267 const MacRenderPageParams& params) const { |
| 261 DCHECK_GT(GetDataSize(), 0U); | 268 DCHECK_GT(GetDataSize(), 0U); |
| 262 if (data_->pdf_cg_.GetDataSize() == 0) { | 269 if (data_->pdf_cg_.GetDataSize() == 0) { |
| 263 if (GetDataSize() == 0) | 270 if (GetDataSize() == 0) |
| 264 return false; | 271 return false; |
| 265 size_t length = data_->pdf_data_->getLength(); | 272 size_t length = data_->pdf_data_->getLength(); |
| 266 std::vector<uint8_t> buffer(length); | 273 std::vector<uint8_t> buffer(length); |
| 267 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); | 274 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
| 268 data_->pdf_cg_.InitFromData(&buffer[0], | 275 data_->pdf_cg_.InitFromData(&buffer[0], length); |
| 269 base::checked_cast<uint32_t>(length)); | |
| 270 } | 276 } |
| 271 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 277 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 272 } | 278 } |
| 273 #endif | 279 #endif |
| 274 | 280 |
| 275 bool PdfMetafileSkia::SaveTo(base::File* file) const { | 281 bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 276 if (GetDataSize() == 0U) | 282 if (GetDataSize() == 0U) |
| 277 return false; | 283 return false; |
| 278 | 284 |
| 279 // Calling duplicate() keeps original asset state unchanged. | 285 // Calling duplicate() keeps original asset state unchanged. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 320 |
| 315 metafile->data_->pages_.push_back(data_->pages_.back()); | 321 metafile->data_->pages_.push_back(data_->pages_.back()); |
| 316 | 322 |
| 317 if (!metafile->FinishDocument()) // Generate PDF. | 323 if (!metafile->FinishDocument()) // Generate PDF. |
| 318 metafile.reset(); | 324 metafile.reset(); |
| 319 | 325 |
| 320 return metafile; | 326 return metafile; |
| 321 } | 327 } |
| 322 | 328 |
| 323 } // namespace printing | 329 } // namespace printing |
| OLD | NEW |