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" | 19 #include "third_party/skia/include/core/SkDocument.h" |
20 #include "third_party/skia/include/core/SkStream.h" | 20 #include "third_party/skia/include/core/SkStream.h" |
21 // Note that headers in third_party/skia/src are fragile. This is | 21 // Note that headers in third_party/skia/src are fragile. This is |
22 // an experimental, fragile, and diagnostic-only document type. | 22 // an experimental, fragile, and diagnostic-only document type. |
23 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" | 23 #include "third_party/skia/src/utils/SkMultiPictureDocument.h" |
24 #include "ui/gfx/geometry/safe_integer_conversions.h" | 24 #include "ui/gfx/geometry/safe_integer_conversions.h" |
25 #include "ui/gfx/skia_util.h" | 25 #include "ui/gfx/skia_util.h" |
26 | 26 |
| 27 #if defined(OS_MACOSX) |
| 28 #include "printing/pdf_metafile_cg_mac.h" |
| 29 #endif |
| 30 |
| 31 #if defined(OS_POSIX) |
| 32 #include "base/file_descriptor_posix.h" |
| 33 #endif |
| 34 |
27 namespace { | 35 namespace { |
28 | 36 |
29 bool WriteAssetToBuffer(const SkStreamAsset* asset, | 37 bool WriteAssetToBuffer(const SkStreamAsset* asset, |
30 void* buffer, | 38 void* buffer, |
31 size_t size) { | 39 size_t size) { |
32 // Calling duplicate() keeps original asset state unchanged. | 40 // Calling duplicate() keeps original asset state unchanged. |
33 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); | 41 std::unique_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
34 size_t length = assetCopy->getLength(); | 42 size_t length = assetCopy->getLength(); |
35 if (length > size) | 43 if (length > size) |
36 return false; | 44 return false; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 97 |
90 std::vector<Page> pages_; | 98 std::vector<Page> pages_; |
91 std::unique_ptr<SkStreamAsset> pdf_data_; | 99 std::unique_ptr<SkStreamAsset> pdf_data_; |
92 | 100 |
93 // The scale factor is used because Blink occasionally calls | 101 // The scale factor is used because Blink occasionally calls |
94 // PaintCanvas::getTotalMatrix() even though the total matrix is not as | 102 // PaintCanvas::getTotalMatrix() even though the total matrix is not as |
95 // meaningful for a vector canvas as for a raster canvas. | 103 // meaningful for a vector canvas as for a raster canvas. |
96 float scale_factor_; | 104 float scale_factor_; |
97 SkSize size_; | 105 SkSize size_; |
98 SkiaDocumentType type_; | 106 SkiaDocumentType type_; |
| 107 |
| 108 #if defined(OS_MACOSX) |
| 109 PdfMetafileCg pdf_cg_; |
| 110 #endif |
99 }; | 111 }; |
100 | 112 |
101 PdfMetafileSkia::~PdfMetafileSkia() {} | 113 PdfMetafileSkia::~PdfMetafileSkia() {} |
102 | 114 |
103 bool PdfMetafileSkia::Init() { | 115 bool PdfMetafileSkia::Init() { |
104 return true; | 116 return true; |
105 } | 117 } |
106 | 118 |
107 // TODO(halcanary): Create a Metafile class that only stores data. | 119 // TODO(halcanary): Create a Metafile class that only stores data. |
108 // Metafile::InitFromData is orthogonal to what the rest of | 120 // Metafile::InitFromData is orthogonal to what the rest of |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 bool PdfMetafileSkia::Playback(skia::NativeDrawingContext hdc, | 248 bool PdfMetafileSkia::Playback(skia::NativeDrawingContext hdc, |
237 const RECT* rect) const { | 249 const RECT* rect) const { |
238 NOTREACHED(); | 250 NOTREACHED(); |
239 return false; | 251 return false; |
240 } | 252 } |
241 | 253 |
242 bool PdfMetafileSkia::SafePlayback(skia::NativeDrawingContext hdc) const { | 254 bool PdfMetafileSkia::SafePlayback(skia::NativeDrawingContext hdc) const { |
243 NOTREACHED(); | 255 NOTREACHED(); |
244 return false; | 256 return false; |
245 } | 257 } |
| 258 |
| 259 #elif defined(OS_MACOSX) |
| 260 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 261 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 262 the drawing of the PDF into the canvas may result in a rasterized output. |
| 263 PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 264 should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 265 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_
instance.cc |
| 266 */ |
| 267 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 268 CGContextRef context, |
| 269 const CGRect rect, |
| 270 const MacRenderPageParams& params) const { |
| 271 DCHECK_GT(GetDataSize(), 0U); |
| 272 if (data_->pdf_cg_.GetDataSize() == 0) { |
| 273 if (GetDataSize() == 0) |
| 274 return false; |
| 275 size_t length = data_->pdf_data_->getLength(); |
| 276 std::vector<uint8_t> buffer(length); |
| 277 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); |
| 278 data_->pdf_cg_.InitFromData(&buffer[0], length); |
| 279 } |
| 280 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 281 } |
246 #endif | 282 #endif |
247 | 283 |
248 bool PdfMetafileSkia::SaveTo(base::File* file) const { | 284 bool PdfMetafileSkia::SaveTo(base::File* file) const { |
249 if (GetDataSize() == 0U) | 285 if (GetDataSize() == 0U) |
250 return false; | 286 return false; |
251 | 287 |
252 // Calling duplicate() keeps original asset state unchanged. | 288 // Calling duplicate() keeps original asset state unchanged. |
253 std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); | 289 std::unique_ptr<SkStreamAsset> asset(data_->pdf_data_->duplicate()); |
254 | 290 |
255 const size_t kMaximumBufferSize = 1024 * 1024; | 291 const size_t kMaximumBufferSize = 1024 * 1024; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 323 |
288 metafile->data_->pages_.push_back(data_->pages_.back()); | 324 metafile->data_->pages_.push_back(data_->pages_.back()); |
289 | 325 |
290 if (!metafile->FinishDocument()) // Generate PDF. | 326 if (!metafile->FinishDocument()) // Generate PDF. |
291 metafile.reset(); | 327 metafile.reset(); |
292 | 328 |
293 return metafile; | 329 return metafile; |
294 } | 330 } |
295 | 331 |
296 } // namespace printing | 332 } // namespace printing |
OLD | NEW |