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" |
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 // True when finish page is outstanding for current page. | |
37 bool PageOutstanding() { return current_page_canvas_; } | |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
Can you make it pure struct without methods?
I see
hal.canary
2014/11/05 23:10:58
Done.
| |
38 | |
36 skia::RefPtr<SkPDFDevice> current_page_; | 39 skia::RefPtr<SkPDFDevice> current_page_; |
40 skia::RefPtr<SkCanvas> current_page_canvas_; | |
37 SkPDFDocument pdf_doc_; | 41 SkPDFDocument pdf_doc_; |
38 SkDynamicMemoryWStream pdf_stream_; | 42 SkDynamicMemoryWStream pdf_stream_; |
39 #if defined(OS_MACOSX) | 43 #if defined(OS_MACOSX) |
40 PdfMetafileCg pdf_cg_; | 44 PdfMetafileCg pdf_cg_; |
41 #endif | 45 #endif |
42 }; | 46 }; |
43 | 47 |
44 PdfMetafileSkia::~PdfMetafileSkia() {} | 48 PdfMetafileSkia::~PdfMetafileSkia() {} |
45 | 49 |
46 bool PdfMetafileSkia::Init() { | 50 bool PdfMetafileSkia::Init() { |
47 return true; | 51 return true; |
48 } | 52 } |
49 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 53 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
50 uint32 src_buffer_size) { | 54 uint32 src_buffer_size) { |
51 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | 55 return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
52 } | 56 } |
53 | 57 |
54 SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas( | 58 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
55 const gfx::Size& page_size, const gfx::Rect& content_area, | 59 const gfx::Rect& content_area, |
56 const float& scale_factor) { | 60 const float& scale_factor) { |
57 DCHECK(!page_outstanding_); | 61 DCHECK(!data_->PageOutstanding()); |
58 page_outstanding_ = true; | |
59 | 62 |
60 // Adjust for the margins and apply the scale factor. | 63 // Adjust for the margins and apply the scale factor. |
61 SkMatrix transform; | 64 SkMatrix transform; |
62 transform.setTranslate(SkIntToScalar(content_area.x()), | 65 transform.setTranslate(SkIntToScalar(content_area.x()), |
63 SkIntToScalar(content_area.y())); | 66 SkIntToScalar(content_area.y())); |
64 transform.preScale(SkFloatToScalar(scale_factor), | 67 transform.preScale(SkFloatToScalar(scale_factor), |
65 SkFloatToScalar(scale_factor)); | 68 SkFloatToScalar(scale_factor)); |
66 | 69 |
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); | 70 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); |
68 SkISize pdf_content_size = | 71 SkISize pdf_content_size = |
69 SkISize::Make(content_area.width(), content_area.height()); | 72 SkISize::Make(content_area.width(), content_area.height()); |
73 | |
70 skia::RefPtr<SkPDFDevice> pdf_device = | 74 skia::RefPtr<SkPDFDevice> pdf_device = |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
can you just assing directly into data_->current_p
hal.canary
2014/11/05 23:10:58
Done.
| |
71 skia::AdoptRef(new skia::VectorPlatformDeviceSkia( | 75 skia::AdoptRef(new SkPDFDevice( |
72 pdf_page_size, pdf_content_size, transform)); | 76 pdf_page_size, pdf_content_size, transform)); |
73 data_->current_page_ = pdf_device; | 77 data_->current_page_ = pdf_device; |
74 return pdf_device.get(); | 78 data_->current_page_canvas_ = |
79 skia::AdoptRef(new SkCanvas(data_->current_page_.get())); | |
80 return true; | |
75 } | 81 } |
76 | 82 |
77 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 83 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
78 const gfx::Rect& content_area, | 84 const gfx::Size& page_size, const gfx::Rect& content_area, |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
seems running "git cl format" may fix this formatt
hal.canary
2014/11/05 23:10:58
Done.
For some reason I had to `chmod +x buildtoo
| |
79 const float& scale_factor) { | 85 const float& scale_factor) { |
80 NOTREACHED(); | 86 (void)this->StartPage(page_size, content_area, scale_factor); |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
if (!StartPage(page_size, content_area, scale_fact
hal.canary
2014/11/05 23:10:58
Done.
| |
81 return false; | 87 return data_->current_page_canvas_.get(); |
82 } | 88 } |
83 | 89 |
84 bool PdfMetafileSkia::FinishPage() { | 90 bool PdfMetafileSkia::FinishPage() { |
91 DCHECK(data_->PageOutstanding()); | |
85 DCHECK(data_->current_page_.get()); | 92 DCHECK(data_->current_page_.get()); |
86 | 93 |
94 data_->current_page_canvas_.clear(); // Unref SkCanvas. | |
87 data_->pdf_doc_.appendPage(data_->current_page_.get()); | 95 data_->pdf_doc_.appendPage(data_->current_page_.get()); |
88 page_outstanding_ = false; | |
89 return true; | 96 return true; |
90 } | 97 } |
91 | 98 |
92 bool PdfMetafileSkia::FinishDocument() { | 99 bool PdfMetafileSkia::FinishDocument() { |
93 // Don't do anything if we've already set the data in InitFromData. | 100 // Don't do anything if we've already set the data in InitFromData. |
94 if (data_->pdf_stream_.getOffset()) | 101 if (data_->pdf_stream_.getOffset()) |
95 return true; | 102 return true; |
96 | 103 |
97 if (page_outstanding_) | 104 if (data_->PageOutstanding()) |
98 FinishPage(); | 105 FinishPage(); |
99 | 106 |
100 data_->current_page_.clear(); | 107 data_->current_page_.clear(); |
101 | 108 |
102 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; | 109 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; |
103 data_->pdf_doc_.getCountOfFontTypes(font_counts); | 110 data_->pdf_doc_.getCountOfFontTypes(font_counts); |
104 for (int type = 0; | 111 for (int type = 0; |
105 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; | 112 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; |
106 type++) { | 113 type++) { |
107 for (int count = 0; count < font_counts[type]; count++) { | 114 for (int count = 0; count < font_counts[type]; count++) { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 const MacRenderPageParams& params) const { | 179 const MacRenderPageParams& params) const { |
173 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 180 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
174 if (data_->pdf_cg_.GetDataSize() == 0) { | 181 if (data_->pdf_cg_.GetDataSize() == 0) { |
175 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 182 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
176 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); | 183 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); |
177 } | 184 } |
178 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 185 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
179 } | 186 } |
180 #endif | 187 #endif |
181 | 188 |
189 bool PdfMetafileSkia::SaveTo(base::File* file) const { | |
190 DCHECK(file); | |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
no need DCHECK here. It will crash on file-> anywa
hal.canary
2014/11/05 23:10:58
Done.
| |
191 if (GetDataSize() == 0U) | |
192 return false; | |
193 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | |
194 // TODO(halcanary): rewrite this function without extra data copy | |
195 // using SkStreamAsset. | |
196 const char* ptr = reinterpret_cast<const char*>(data->data()); | |
197 int size = base::checked_cast<int>(data->size()); | |
198 return file->WriteAtCurrentPos(ptr, size) == size; | |
199 } | |
200 | |
182 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 201 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
183 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 202 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
184 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 203 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
185 | 204 |
186 if (fd.fd < 0) { | 205 if (fd.fd < 0) { |
187 DLOG(ERROR) << "Invalid file descriptor!"; | 206 DLOG(ERROR) << "Invalid file descriptor!"; |
188 return false; | 207 return false; |
189 } | 208 } |
190 base::File file(fd.fd); | 209 base::File file(fd.fd); |
191 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 210 bool result = this->SaveTo(&file); |
Vitaly Buka (NO REVIEWS)
2014/11/05 22:21:38
no need this->
hal.canary
2014/11/05 23:10:58
Done.
| |
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; | 211 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
196 | 212 |
197 if (!fd.auto_close) | 213 if (!fd.auto_close) |
198 file.TakePlatformFile(); | 214 file.TakePlatformFile(); |
199 return result; | 215 return result; |
200 } | 216 } |
201 #endif | 217 #endif |
202 | 218 |
203 PdfMetafileSkia::PdfMetafileSkia() | 219 PdfMetafileSkia::PdfMetafileSkia() |
204 : data_(new PdfMetafileSkiaData), | 220 : data_(new PdfMetafileSkiaData) { |
205 page_outstanding_(false) { | |
206 } | 221 } |
207 | 222 |
208 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { | 223 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
209 scoped_ptr<PdfMetafileSkia> metafile; | 224 scoped_ptr<PdfMetafileSkia> metafile; |
210 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); | 225 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); |
211 if (!pdf_doc.appendPage(data_->current_page_.get())) | 226 if (!pdf_doc.appendPage(data_->current_page_.get())) |
212 return metafile.Pass(); | 227 return metafile.Pass(); |
213 | 228 |
214 SkDynamicMemoryWStream pdf_stream; | 229 SkDynamicMemoryWStream pdf_stream; |
215 if (!pdf_doc.emitPDF(&pdf_stream)) | 230 if (!pdf_doc.emitPDF(&pdf_stream)) |
216 return metafile.Pass(); | 231 return metafile.Pass(); |
217 | 232 |
218 SkAutoDataUnref data_copy(pdf_stream.copyToData()); | 233 SkAutoDataUnref data_copy(pdf_stream.copyToData()); |
219 if (data_copy->size() == 0) | 234 if (data_copy->size() == 0) |
220 return scoped_ptr<PdfMetafileSkia>(); | 235 return scoped_ptr<PdfMetafileSkia>(); |
221 | 236 |
222 metafile.reset(new PdfMetafileSkia); | 237 metafile.reset(new PdfMetafileSkia); |
223 if (!metafile->InitFromData(data_copy->bytes(), | 238 if (!metafile->InitFromData(data_copy->bytes(), |
224 base::checked_cast<uint32>(data_copy->size()))) { | 239 base::checked_cast<uint32>(data_copy->size()))) { |
225 metafile.reset(); | 240 metafile.reset(); |
226 } | 241 } |
227 return metafile.Pass(); | 242 return metafile.Pass(); |
228 } | 243 } |
229 | 244 |
230 } // namespace printing | 245 } // namespace printing |
OLD | NEW |