| 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" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 PdfMetafileSkia::~PdfMetafileSkia() {} | 77 PdfMetafileSkia::~PdfMetafileSkia() {} |
| 78 | 78 |
| 79 bool PdfMetafileSkia::Init() { | 79 bool PdfMetafileSkia::Init() { |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // TODO(halcanary): Create a Metafile class that only stores data. | 83 // TODO(halcanary): Create a Metafile class that only stores data. |
| 84 // Metafile::InitFromData is orthogonal to what the rest of | 84 // Metafile::InitFromData is orthogonal to what the rest of |
| 85 // PdfMetafileSkia does. | 85 // PdfMetafileSkia does. |
| 86 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 86 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 87 uint32 src_buffer_size) { | 87 uint32_t src_buffer_size) { |
| 88 data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true)); | 88 data_->pdf_data_.reset(new SkMemoryStream(src_buffer, src_buffer_size, true)); |
| 89 return true; | 89 return true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 92 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
| 93 const gfx::Rect& content_area, | 93 const gfx::Rect& content_area, |
| 94 const float& scale_factor) { | 94 const float& scale_factor) { |
| 95 if (data_->recorder_.getRecordingCanvas()) | 95 if (data_->recorder_.getRecordingCanvas()) |
| 96 this->FinishPage(); | 96 this->FinishPage(); |
| 97 DCHECK(!data_->recorder_.getRecordingCanvas()); | 97 DCHECK(!data_->recorder_.getRecordingCanvas()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 canvas->drawPicture(page.content_.get()); | 146 canvas->drawPicture(page.content_.get()); |
| 147 pdf_doc->endPage(); | 147 pdf_doc->endPage(); |
| 148 } | 148 } |
| 149 if (!pdf_doc->close()) | 149 if (!pdf_doc->close()) |
| 150 return false; | 150 return false; |
| 151 | 151 |
| 152 data_->pdf_data_.reset(pdf_stream.detachAsStream()); | 152 data_->pdf_data_.reset(pdf_stream.detachAsStream()); |
| 153 return true; | 153 return true; |
| 154 } | 154 } |
| 155 | 155 |
| 156 uint32 PdfMetafileSkia::GetDataSize() const { | 156 uint32_t PdfMetafileSkia::GetDataSize() const { |
| 157 if (!data_->pdf_data_) | 157 if (!data_->pdf_data_) |
| 158 return 0; | 158 return 0; |
| 159 return base::checked_cast<uint32>(data_->pdf_data_->getLength()); | 159 return base::checked_cast<uint32_t>(data_->pdf_data_->getLength()); |
| 160 } | 160 } |
| 161 | 161 |
| 162 bool PdfMetafileSkia::GetData(void* dst_buffer, | 162 bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 163 uint32 dst_buffer_size) const { | 163 uint32_t dst_buffer_size) const { |
| 164 if (!data_->pdf_data_) | 164 if (!data_->pdf_data_) |
| 165 return false; | 165 return false; |
| 166 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, | 166 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
| 167 base::checked_cast<size_t>(dst_buffer_size)); | 167 base::checked_cast<size_t>(dst_buffer_size)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 170 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
| 171 if (page_number < data_->pages_.size()) { | 171 if (page_number < data_->pages_.size()) { |
| 172 return gfx::Rect(gfx::ToFlooredSize( | 172 return gfx::Rect(gfx::ToFlooredSize( |
| 173 gfx::SkSizeToSizeF(data_->pages_[page_number].page_size_))); | 173 gfx::SkSizeToSizeF(data_->pages_[page_number].page_size_))); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 CGContextRef context, | 208 CGContextRef context, |
| 209 const CGRect rect, | 209 const CGRect rect, |
| 210 const MacRenderPageParams& params) const { | 210 const MacRenderPageParams& params) const { |
| 211 DCHECK_GT(GetDataSize(), 0U); | 211 DCHECK_GT(GetDataSize(), 0U); |
| 212 if (data_->pdf_cg_.GetDataSize() == 0) { | 212 if (data_->pdf_cg_.GetDataSize() == 0) { |
| 213 if (GetDataSize() == 0) | 213 if (GetDataSize() == 0) |
| 214 return false; | 214 return false; |
| 215 size_t length = data_->pdf_data_->getLength(); | 215 size_t length = data_->pdf_data_->getLength(); |
| 216 std::vector<uint8_t> buffer(length); | 216 std::vector<uint8_t> buffer(length); |
| 217 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); | 217 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
| 218 data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length)); | 218 data_->pdf_cg_.InitFromData(&buffer[0], |
| 219 base::checked_cast<uint32_t>(length)); |
| 219 } | 220 } |
| 220 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 221 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 221 } | 222 } |
| 222 #endif | 223 #endif |
| 223 | 224 |
| 224 bool PdfMetafileSkia::SaveTo(base::File* file) const { | 225 bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 225 if (GetDataSize() == 0U) | 226 if (GetDataSize() == 0U) |
| 226 return false; | 227 return false; |
| 227 | 228 |
| 228 // Calling duplicate() keeps original asset state unchanged. | 229 // Calling duplicate() keeps original asset state unchanged. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 281 |
| 281 metafile->data_->pages_.push_back(page); | 282 metafile->data_->pages_.push_back(page); |
| 282 | 283 |
| 283 if (!metafile->FinishDocument()) // Generate PDF. | 284 if (!metafile->FinishDocument()) // Generate PDF. |
| 284 metafile.reset(); | 285 metafile.reset(); |
| 285 | 286 |
| 286 return metafile.Pass(); | 287 return metafile.Pass(); |
| 287 } | 288 } |
| 288 | 289 |
| 289 } // namespace printing | 290 } // namespace printing |
| OLD | NEW |