Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: printing/pdf_metafile_skia.cc

Issue 704813002: PdfMetafileSkia gives out a SkCanvas, not a SkBaseDevice. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « printing/pdf_metafile_skia.h ('k') | skia/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/posix/eintr_wrapper.h" 11 #include "base/posix/eintr_wrapper.h"
12 #include "skia/ext/refptr.h" 12 #include "skia/ext/refptr.h"
13 #include "skia/ext/vector_platform_device_skia.h" 13 #include "skia/ext/vector_canvas.h"
14 #include "third_party/skia/include/core/SkData.h" 14 #include "third_party/skia/include/core/SkData.h"
15 #include "third_party/skia/include/core/SkRefCnt.h" 15 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkScalar.h" 16 #include "third_party/skia/include/core/SkScalar.h"
17 #include "third_party/skia/include/core/SkStream.h" 17 #include "third_party/skia/include/core/SkStream.h"
18 #include "third_party/skia/include/core/SkTypeface.h" 18 #include "third_party/skia/include/core/SkTypeface.h"
19 #include "third_party/skia/include/pdf/SkPDFDevice.h" 19 #include "third_party/skia/include/pdf/SkPDFDevice.h"
20 #include "third_party/skia/include/pdf/SkPDFDocument.h" 20 #include "third_party/skia/include/pdf/SkPDFDocument.h"
21 #include "ui/gfx/point.h" 21 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h" 22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h" 23 #include "ui/gfx/size.h"
24 24
25 #if defined(OS_MACOSX) 25 #if defined(OS_MACOSX)
26 #include "printing/pdf_metafile_cg_mac.h" 26 #include "printing/pdf_metafile_cg_mac.h"
27 #endif 27 #endif
28 28
29 #if defined(OS_POSIX) 29 #if defined(OS_POSIX)
30 #include "base/file_descriptor_posix.h" 30 #include "base/file_descriptor_posix.h"
31 #endif 31 #endif
32 32
33 namespace printing { 33 namespace printing {
34 34
35 struct PdfMetafileSkiaData { 35 struct PdfMetafileSkiaData {
36 skia::RefPtr<SkPDFDevice> current_page_; 36 skia::RefPtr<SkPDFDevice> current_page_;
37 skia::RefPtr<SkCanvas> current_page_canvas_;
37 SkPDFDocument pdf_doc_; 38 SkPDFDocument pdf_doc_;
38 SkDynamicMemoryWStream pdf_stream_; 39 SkDynamicMemoryWStream pdf_stream_;
39 #if defined(OS_MACOSX) 40 #if defined(OS_MACOSX)
40 PdfMetafileCg pdf_cg_; 41 PdfMetafileCg pdf_cg_;
41 #endif 42 #endif
42 }; 43 };
43 44
44 PdfMetafileSkia::~PdfMetafileSkia() {} 45 PdfMetafileSkia::~PdfMetafileSkia() {}
45 46
46 bool PdfMetafileSkia::Init() { 47 bool PdfMetafileSkia::Init() {
47 return true; 48 return true;
48 } 49 }
49 bool PdfMetafileSkia::InitFromData(const void* src_buffer, 50 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
50 uint32 src_buffer_size) { 51 uint32 src_buffer_size) {
51 return data_->pdf_stream_.write(src_buffer, src_buffer_size); 52 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
52 } 53 }
53 54
54 SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas( 55 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
55 const gfx::Size& page_size, const gfx::Rect& content_area, 56 const gfx::Rect& content_area,
56 const float& scale_factor) { 57 const float& scale_factor) {
57 DCHECK(!page_outstanding_); 58 DCHECK(!data_->current_page_canvas_);
58 page_outstanding_ = true;
59 59
60 // Adjust for the margins and apply the scale factor. 60 // Adjust for the margins and apply the scale factor.
61 SkMatrix transform; 61 SkMatrix transform;
62 transform.setTranslate(SkIntToScalar(content_area.x()), 62 transform.setTranslate(SkIntToScalar(content_area.x()),
63 SkIntToScalar(content_area.y())); 63 SkIntToScalar(content_area.y()));
64 transform.preScale(SkFloatToScalar(scale_factor), 64 transform.preScale(SkFloatToScalar(scale_factor),
65 SkFloatToScalar(scale_factor)); 65 SkFloatToScalar(scale_factor));
66 66
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); 67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
68 SkISize pdf_content_size = 68 SkISize pdf_content_size =
69 SkISize::Make(content_area.width(), content_area.height()); 69 SkISize::Make(content_area.width(), content_area.height());
70 skia::RefPtr<SkPDFDevice> pdf_device = 70
71 skia::AdoptRef(new skia::VectorPlatformDeviceSkia( 71 data_->current_page_ = skia::AdoptRef(
72 pdf_page_size, pdf_content_size, transform)); 72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform));
73 data_->current_page_ = pdf_device; 73 data_->current_page_canvas_ =
74 return pdf_device.get(); 74 skia::AdoptRef(new SkCanvas(data_->current_page_.get()));
75 return true;
75 } 76 }
76 77
77 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, 78 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage(
78 const gfx::Rect& content_area, 79 const gfx::Size& page_size,
79 const float& scale_factor) { 80 const gfx::Rect& content_area,
80 NOTREACHED(); 81 const float& scale_factor) {
81 return false; 82 if (!StartPage(page_size, content_area, scale_factor))
83 return nullptr;
84 return data_->current_page_canvas_.get();
82 } 85 }
83 86
84 bool PdfMetafileSkia::FinishPage() { 87 bool PdfMetafileSkia::FinishPage() {
85 DCHECK(data_->current_page_.get()); 88 DCHECK(data_->current_page_canvas_);
89 DCHECK(data_->current_page_);
86 90
91 data_->current_page_canvas_.clear(); // Unref SkCanvas.
87 data_->pdf_doc_.appendPage(data_->current_page_.get()); 92 data_->pdf_doc_.appendPage(data_->current_page_.get());
88 page_outstanding_ = false;
89 return true; 93 return true;
90 } 94 }
91 95
92 bool PdfMetafileSkia::FinishDocument() { 96 bool PdfMetafileSkia::FinishDocument() {
93 // Don't do anything if we've already set the data in InitFromData. 97 // Don't do anything if we've already set the data in InitFromData.
94 if (data_->pdf_stream_.getOffset()) 98 if (data_->pdf_stream_.getOffset())
95 return true; 99 return true;
96 100
97 if (page_outstanding_) 101 if (data_->current_page_canvas_)
98 FinishPage(); 102 FinishPage();
99 103
100 data_->current_page_.clear(); 104 data_->current_page_.clear();
101 105
102 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; 106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2];
103 data_->pdf_doc_.getCountOfFontTypes(font_counts); 107 data_->pdf_doc_.getCountOfFontTypes(font_counts);
104 for (int type = 0; 108 for (int type = 0;
105 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; 109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1;
106 type++) { 110 type++) {
107 for (int count = 0; count < font_counts[type]; count++) { 111 for (int count = 0; count < font_counts[type]; count++) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 const MacRenderPageParams& params) const { 176 const MacRenderPageParams& params) const {
173 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); 177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
174 if (data_->pdf_cg_.GetDataSize() == 0) { 178 if (data_->pdf_cg_.GetDataSize() == 0) {
175 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); 179 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
176 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); 180 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
177 } 181 }
178 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); 182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
179 } 183 }
180 #endif 184 #endif
181 185
186 bool PdfMetafileSkia::SaveTo(base::File* file) const {
187 if (GetDataSize() == 0U)
188 return false;
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
190 // TODO(halcanary): rewrite this function without extra data copy
191 // using SkStreamAsset.
192 const char* ptr = reinterpret_cast<const char*>(data->data());
193 int size = base::checked_cast<int>(data->size());
194 return file->WriteAtCurrentPos(ptr, size) == size;
195 }
196
182 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) 197 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
183 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { 198 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
184 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); 199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
185 200
186 if (fd.fd < 0) { 201 if (fd.fd < 0) {
187 DLOG(ERROR) << "Invalid file descriptor!"; 202 DLOG(ERROR) << "Invalid file descriptor!";
188 return false; 203 return false;
189 } 204 }
190 base::File file(fd.fd); 205 base::File file(fd.fd);
191 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); 206 bool result = SaveTo(&file);
192 bool result =
193 file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->data()),
194 GetDataSize()) == static_cast<int>(GetDataSize());
195 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; 207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
196 208
197 if (!fd.auto_close) 209 if (!fd.auto_close)
198 file.TakePlatformFile(); 210 file.TakePlatformFile();
199 return result; 211 return result;
200 } 212 }
201 #endif 213 #endif
202 214
203 PdfMetafileSkia::PdfMetafileSkia() 215 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {
204 : data_(new PdfMetafileSkiaData),
205 page_outstanding_(false) {
206 } 216 }
207 217
208 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { 218 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
209 scoped_ptr<PdfMetafileSkia> metafile; 219 scoped_ptr<PdfMetafileSkia> metafile;
210 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); 220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
211 if (!pdf_doc.appendPage(data_->current_page_.get())) 221 if (!pdf_doc.appendPage(data_->current_page_.get()))
212 return metafile.Pass(); 222 return metafile.Pass();
213 223
214 SkDynamicMemoryWStream pdf_stream; 224 SkDynamicMemoryWStream pdf_stream;
215 if (!pdf_doc.emitPDF(&pdf_stream)) 225 if (!pdf_doc.emitPDF(&pdf_stream))
216 return metafile.Pass(); 226 return metafile.Pass();
217 227
218 SkAutoDataUnref data_copy(pdf_stream.copyToData()); 228 SkAutoDataUnref data_copy(pdf_stream.copyToData());
219 if (data_copy->size() == 0) 229 if (data_copy->size() == 0)
220 return scoped_ptr<PdfMetafileSkia>(); 230 return scoped_ptr<PdfMetafileSkia>();
221 231
222 metafile.reset(new PdfMetafileSkia); 232 metafile.reset(new PdfMetafileSkia);
223 if (!metafile->InitFromData(data_copy->bytes(), 233 if (!metafile->InitFromData(data_copy->bytes(),
224 base::checked_cast<uint32>(data_copy->size()))) { 234 base::checked_cast<uint32>(data_copy->size()))) {
225 metafile.reset(); 235 metafile.reset();
226 } 236 }
227 return metafile.Pass(); 237 return metafile.Pass();
228 } 238 }
229 239
230 } // namespace printing 240 } // namespace printing
OLDNEW
« no previous file with comments | « printing/pdf_metafile_skia.h ('k') | skia/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698