OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/eintr_wrapper.h" | 7 #include "base/eintr_wrapper.h" |
8 #include "base/file_descriptor_posix.h" | 8 #include "base/file_descriptor_posix.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 return true; | 44 return true; |
45 } | 45 } |
46 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 46 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
47 uint32 src_buffer_size) { | 47 uint32 src_buffer_size) { |
48 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | 48 return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
49 } | 49 } |
50 | 50 |
51 SkDevice* PdfMetafileSkia::StartPageForVectorCanvas( | 51 SkDevice* PdfMetafileSkia::StartPageForVectorCanvas( |
52 const gfx::Size& page_size, const gfx::Rect& content_area, | 52 const gfx::Size& page_size, const gfx::Rect& content_area, |
53 const float& scale_factor) { | 53 const float& scale_factor) { |
54 DCHECK(data_->current_page_.get() == NULL); | 54 if (did_finish_page_) |
vandebo (ex-Chrome)
2011/06/29 20:23:45
I think this should just be DCHECK(did_finish_page
kmadhusu
2011/06/30 06:10:55
Renamed the variable to outstanding_finish_page_.
| |
55 data_->current_page_ = NULL; | |
56 did_finish_page_ = false; | |
55 | 57 |
56 // Adjust for the margins and apply the scale factor. | 58 // Adjust for the margins and apply the scale factor. |
57 SkMatrix transform; | 59 SkMatrix transform; |
58 transform.setTranslate(SkIntToScalar(content_area.x()), | 60 transform.setTranslate(SkIntToScalar(content_area.x()), |
59 SkIntToScalar(content_area.y())); | 61 SkIntToScalar(content_area.y())); |
60 transform.preScale(SkFloatToScalar(scale_factor), | 62 transform.preScale(SkFloatToScalar(scale_factor), |
61 SkFloatToScalar(scale_factor)); | 63 SkFloatToScalar(scale_factor)); |
62 | 64 |
63 // TODO(ctguil): Refactor: don't create the PDF device explicitly here. | 65 // TODO(ctguil): Refactor: don't create the PDF device explicitly here. |
64 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); | 66 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); |
(...skipping 12 matching lines...) Expand all Loading... | |
77 const gfx::Rect& content_area, | 79 const gfx::Rect& content_area, |
78 const float& scale_factor) { | 80 const float& scale_factor) { |
79 NOTREACHED(); | 81 NOTREACHED(); |
80 return NULL; | 82 return NULL; |
81 } | 83 } |
82 | 84 |
83 bool PdfMetafileSkia::FinishPage() { | 85 bool PdfMetafileSkia::FinishPage() { |
84 DCHECK(data_->current_page_.get()); | 86 DCHECK(data_->current_page_.get()); |
85 | 87 |
86 data_->pdf_doc_.appendPage(data_->current_page_); | 88 data_->pdf_doc_.appendPage(data_->current_page_); |
87 data_->current_page_ = NULL; | 89 did_finish_page_ = true; |
88 return true; | 90 return true; |
89 } | 91 } |
90 | 92 |
91 bool PdfMetafileSkia::FinishDocument() { | 93 bool PdfMetafileSkia::FinishDocument() { |
92 // Don't do anything if we've already set the data in InitFromData. | 94 // Don't do anything if we've already set the data in InitFromData. |
93 if (data_->pdf_stream_.getOffset()) | 95 if (data_->pdf_stream_.getOffset()) |
94 return true; | 96 return true; |
95 | 97 |
96 if (data_->current_page_.get()) | 98 if (!did_finish_page_ && data_->current_page_.get()) |
vandebo (ex-Chrome)
2011/06/29 20:23:45
Don't (shouldn't) need to current_page_.get() chec
kmadhusu
2011/06/30 06:10:55
Done.
| |
97 FinishPage(); | 99 FinishPage(); |
98 | 100 |
101 data_->current_page_ = NULL; | |
99 base::hash_set<SkFontID> font_set; | 102 base::hash_set<SkFontID> font_set; |
100 | 103 |
101 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); | 104 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); |
102 for (int page_number = 0; page_number < pages.count(); page_number++) { | 105 for (int page_number = 0; page_number < pages.count(); page_number++) { |
103 const SkTDArray<SkPDFFont*>& font_resources = | 106 const SkTDArray<SkPDFFont*>& font_resources = |
104 pages[page_number]->getFontResources(); | 107 pages[page_number]->getFontResources(); |
105 for (int font = 0; font < font_resources.count(); font++) { | 108 for (int font = 0; font < font_resources.count(); font++) { |
106 SkFontID font_id = font_resources[font]->typeface()->uniqueID(); | 109 SkFontID font_id = font_resources[font]->typeface()->uniqueID(); |
107 if (font_set.find(font_id) == font_set.end()) { | 110 if (font_set.find(font_id) == font_set.end()) { |
108 font_set.insert(font_id); | 111 font_set.insert(font_id); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 DPLOG(WARNING) << "close"; | 230 DPLOG(WARNING) << "close"; |
228 result = false; | 231 result = false; |
229 } | 232 } |
230 } | 233 } |
231 return result; | 234 return result; |
232 } | 235 } |
233 #endif | 236 #endif |
234 | 237 |
235 PdfMetafileSkia::PdfMetafileSkia() | 238 PdfMetafileSkia::PdfMetafileSkia() |
236 : data_(new PdfMetafileSkiaData), | 239 : data_(new PdfMetafileSkiaData), |
237 draft_(false) {} | 240 draft_(false), |
241 did_finish_page_(false) {} | |
238 | 242 |
239 void PdfMetafileSkia::set_draft(bool draft) const { | 243 void PdfMetafileSkia::set_draft(bool draft) const { |
240 draft_ = draft; | 244 draft_ = draft; |
241 } | 245 } |
246 | |
247 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() { | |
248 SkPDFDocument pdf_doc; | |
249 SkDynamicMemoryWStream pdf_stream; | |
250 if (!pdf_doc.appendPage(data_->current_page_)) | |
251 return NULL; | |
252 | |
253 if (!pdf_doc.emitPDF(&pdf_stream)) | |
254 return NULL; | |
255 | |
256 SkAutoDataUnref data(pdf_stream.copyToData()); | |
257 if (data.size() == 0) | |
258 return NULL; | |
259 | |
260 PdfMetafileSkia* metafile = new printing::PdfMetafileSkia; | |
261 metafile->InitFromData(data.bytes(),data.size()); | |
262 return metafile; | |
263 } | |
264 | |
242 } // namespace printing | 265 } // namespace printing |
OLD | NEW |