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 18 matching lines...) Expand all Loading... | |
29 #endif | 29 #endif |
30 | 30 |
31 #if defined(OS_POSIX) | 31 #if defined(OS_POSIX) |
32 #include "base/file_descriptor_posix.h" | 32 #include "base/file_descriptor_posix.h" |
33 #endif | 33 #endif |
34 | 34 |
35 namespace { | 35 namespace { |
36 // This struct represents all the data we need to draw and redraw this | 36 // This struct represents all the data we need to draw and redraw this |
37 // page into a SkDocument. | 37 // page into a SkDocument. |
38 struct Page { | 38 struct Page { |
39 Page(const SkSize& page_size, const SkRect& content_area) | 39 Page(const SkSize& page_size, const SkRect& content_area, float scale) |
40 : page_size_(page_size), | 40 : page_size_(page_size), |
41 content_area_(content_area), | 41 content_area_(content_area), |
42 scale_factor_(scale), | |
42 content_(/*NULL*/) {} | 43 content_(/*NULL*/) {} |
43 SkSize page_size_; | 44 SkSize page_size_; |
44 SkRect content_area_; | 45 SkRect content_area_; |
46 float scale_factor_; | |
45 skia::RefPtr<SkPicture> content_; | 47 skia::RefPtr<SkPicture> content_; |
46 }; | 48 }; |
47 } // namespace | 49 } // namespace |
48 | 50 |
49 static bool WriteAssetToBuffer(const SkStreamAsset* asset, | 51 static bool WriteAssetToBuffer(const SkStreamAsset* asset, |
50 void* buffer, | 52 void* buffer, |
51 size_t size) { | 53 size_t size) { |
52 // Calling duplicate() keeps original asset state unchanged. | 54 // Calling duplicate() keeps original asset state unchanged. |
53 scoped_ptr<SkStreamAsset> assetCopy(asset->duplicate()); | 55 scoped_ptr<SkStreamAsset> assetCopy(asset->duplicate()); |
54 size_t length = assetCopy->getLength(); | 56 size_t length = assetCopy->getLength(); |
(...skipping 30 matching lines...) Expand all Loading... | |
85 return true; | 87 return true; |
86 } | 88 } |
87 | 89 |
88 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 90 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
89 const gfx::Rect& content_area, | 91 const gfx::Rect& content_area, |
90 const float& scale_factor) { | 92 const float& scale_factor) { |
91 if (data_->recorder_.getRecordingCanvas()) | 93 if (data_->recorder_.getRecordingCanvas()) |
92 this->FinishPage(); | 94 this->FinishPage(); |
93 DCHECK(!data_->recorder_.getRecordingCanvas()); | 95 DCHECK(!data_->recorder_.getRecordingCanvas()); |
94 SkSize sk_page_size = gfx::SizeFToSkSize(gfx::SizeF(page_size)); | 96 SkSize sk_page_size = gfx::SizeFToSkSize(gfx::SizeF(page_size)); |
95 data_->pages_.push_back(Page(sk_page_size, gfx::RectToSkRect(content_area))); | 97 data_->pages_.push_back( |
96 | 98 Page(sk_page_size, gfx::RectToSkRect(content_area), scale_factor)); |
97 SkCanvas* recordingCanvas = data_->recorder_.beginRecording( | 99 DCHECK_GT(scale_factor, 0.0f); |
Vitaly Buka (NO REVIEWS)
2015/04/03 20:51:39
DCHECK is unnecessary
crash on divided by 0 is ob
| |
98 sk_page_size.width(), sk_page_size.height(), NULL, 0); | 100 // We scale the recording canvas's size so that |
99 // recordingCanvas is owned by the data_->recorder_. No ref() necessary. | 101 // canvas->getTotalMatrix() returns a value that ignores the scale |
100 if (!recordingCanvas) | 102 // factor. We store the scale factor and re-apply it to the PDF |
101 return false; | 103 // Canvas later. http://crbug.com/469656 |
102 recordingCanvas->scale(scale_factor, scale_factor); | 104 // Recording canvas is owned by the data_->recorder_. No ref() necessary. |
103 return true; | 105 return !!data_->recorder_.beginRecording(sk_page_size.width() / scale_factor, |
106 sk_page_size.height() / scale_factor, | |
107 NULL, 0); | |
104 } | 108 } |
105 | 109 |
106 skia::PlatformCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( | 110 skia::PlatformCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
107 const gfx::Size& page_size, | 111 const gfx::Size& page_size, |
108 const gfx::Rect& content_area, | 112 const gfx::Rect& content_area, |
109 const float& scale_factor) { | 113 const float& scale_factor) { |
110 if (!StartPage(page_size, content_area, scale_factor)) | 114 if (!StartPage(page_size, content_area, scale_factor)) |
111 return nullptr; | 115 return nullptr; |
112 return data_->recorder_.getRecordingCanvas(); | 116 return data_->recorder_.getRecordingCanvas(); |
113 } | 117 } |
(...skipping 14 matching lines...) Expand all Loading... | |
128 | 132 |
129 if (data_->recorder_.getRecordingCanvas()) | 133 if (data_->recorder_.getRecordingCanvas()) |
130 this->FinishPage(); | 134 this->FinishPage(); |
131 | 135 |
132 SkDynamicMemoryWStream pdf_stream; | 136 SkDynamicMemoryWStream pdf_stream; |
133 skia::RefPtr<SkDocument> pdf_doc = | 137 skia::RefPtr<SkDocument> pdf_doc = |
134 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); | 138 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); |
135 for (const auto& page : data_->pages_) { | 139 for (const auto& page : data_->pages_) { |
136 SkCanvas* canvas = pdf_doc->beginPage( | 140 SkCanvas* canvas = pdf_doc->beginPage( |
137 page.page_size_.width(), page.page_size_.height(), &page.content_area_); | 141 page.page_size_.width(), page.page_size_.height(), &page.content_area_); |
142 // No need to save/restore, since this canvas is not reused after endPage() | |
143 canvas->scale(page.scale_factor_, page.scale_factor_); | |
138 canvas->drawPicture(page.content_.get()); | 144 canvas->drawPicture(page.content_.get()); |
139 pdf_doc->endPage(); | 145 pdf_doc->endPage(); |
140 } | 146 } |
141 if (!pdf_doc->close()) | 147 if (!pdf_doc->close()) |
142 return false; | 148 return false; |
143 | 149 |
144 data_->pdf_data_.reset(pdf_stream.detachAsStream()); | 150 data_->pdf_data_.reset(pdf_stream.detachAsStream()); |
145 return true; | 151 return true; |
146 } | 152 } |
147 | 153 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 | 278 |
273 metafile->data_->pages_.push_back(page); | 279 metafile->data_->pages_.push_back(page); |
274 | 280 |
275 if (!metafile->FinishDocument()) // Generate PDF. | 281 if (!metafile->FinishDocument()) // Generate PDF. |
276 metafile.reset(); | 282 metafile.reset(); |
277 | 283 |
278 return metafile.Pass(); | 284 return metafile.Pass(); |
279 } | 285 } |
280 | 286 |
281 } // namespace printing | 287 } // namespace printing |
OLD | NEW |