| 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 DCHECK (!page_outstanding_); |
| 55 page_outstanding_ = true; |
| 55 | 56 |
| 56 // Adjust for the margins and apply the scale factor. | 57 // Adjust for the margins and apply the scale factor. |
| 57 SkMatrix transform; | 58 SkMatrix transform; |
| 58 transform.setTranslate(SkIntToScalar(content_area.x()), | 59 transform.setTranslate(SkIntToScalar(content_area.x()), |
| 59 SkIntToScalar(content_area.y())); | 60 SkIntToScalar(content_area.y())); |
| 60 transform.preScale(SkFloatToScalar(scale_factor), | 61 transform.preScale(SkFloatToScalar(scale_factor), |
| 61 SkFloatToScalar(scale_factor)); | 62 SkFloatToScalar(scale_factor)); |
| 62 | 63 |
| 63 // TODO(ctguil): Refactor: don't create the PDF device explicitly here. | 64 // TODO(ctguil): Refactor: don't create the PDF device explicitly here. |
| 64 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); | 65 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, | 78 const gfx::Rect& content_area, |
| 78 const float& scale_factor) { | 79 const float& scale_factor) { |
| 79 NOTREACHED(); | 80 NOTREACHED(); |
| 80 return NULL; | 81 return NULL; |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool PdfMetafileSkia::FinishPage() { | 84 bool PdfMetafileSkia::FinishPage() { |
| 84 DCHECK(data_->current_page_.get()); | 85 DCHECK(data_->current_page_.get()); |
| 85 | 86 |
| 86 data_->pdf_doc_.appendPage(data_->current_page_); | 87 data_->pdf_doc_.appendPage(data_->current_page_); |
| 87 data_->current_page_ = NULL; | 88 page_outstanding_ = false; |
| 88 return true; | 89 return true; |
| 89 } | 90 } |
| 90 | 91 |
| 91 bool PdfMetafileSkia::FinishDocument() { | 92 bool PdfMetafileSkia::FinishDocument() { |
| 92 // Don't do anything if we've already set the data in InitFromData. | 93 // Don't do anything if we've already set the data in InitFromData. |
| 93 if (data_->pdf_stream_.getOffset()) | 94 if (data_->pdf_stream_.getOffset()) |
| 94 return true; | 95 return true; |
| 95 | 96 |
| 96 if (data_->current_page_.get()) | 97 if (page_outstanding_) |
| 97 FinishPage(); | 98 FinishPage(); |
| 98 | 99 |
| 100 data_->current_page_ = NULL; |
| 99 base::hash_set<SkFontID> font_set; | 101 base::hash_set<SkFontID> font_set; |
| 100 | 102 |
| 101 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); | 103 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); |
| 102 for (int page_number = 0; page_number < pages.count(); page_number++) { | 104 for (int page_number = 0; page_number < pages.count(); page_number++) { |
| 103 const SkTDArray<SkPDFFont*>& font_resources = | 105 const SkTDArray<SkPDFFont*>& font_resources = |
| 104 pages[page_number]->getFontResources(); | 106 pages[page_number]->getFontResources(); |
| 105 for (int font = 0; font < font_resources.count(); font++) { | 107 for (int font = 0; font < font_resources.count(); font++) { |
| 106 SkFontID font_id = font_resources[font]->typeface()->uniqueID(); | 108 SkFontID font_id = font_resources[font]->typeface()->uniqueID(); |
| 107 if (font_set.find(font_id) == font_set.end()) { | 109 if (font_set.find(font_id) == font_set.end()) { |
| 108 font_set.insert(font_id); | 110 font_set.insert(font_id); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 DPLOG(WARNING) << "close"; | 229 DPLOG(WARNING) << "close"; |
| 228 result = false; | 230 result = false; |
| 229 } | 231 } |
| 230 } | 232 } |
| 231 return result; | 233 return result; |
| 232 } | 234 } |
| 233 #endif | 235 #endif |
| 234 | 236 |
| 235 PdfMetafileSkia::PdfMetafileSkia() | 237 PdfMetafileSkia::PdfMetafileSkia() |
| 236 : data_(new PdfMetafileSkiaData), | 238 : data_(new PdfMetafileSkiaData), |
| 237 draft_(false) {} | 239 draft_(false), |
| 240 page_outstanding_(false) {} |
| 241 |
| 242 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() { |
| 243 SkPDFDocument pdf_doc; |
| 244 SkDynamicMemoryWStream pdf_stream; |
| 245 if (!pdf_doc.appendPage(data_->current_page_)) |
| 246 return NULL; |
| 247 |
| 248 if (!pdf_doc.emitPDF(&pdf_stream)) |
| 249 return NULL; |
| 250 |
| 251 SkAutoDataUnref data(pdf_stream.copyToData()); |
| 252 if (data.size() == 0) |
| 253 return NULL; |
| 254 |
| 255 PdfMetafileSkia* metafile = new printing::PdfMetafileSkia; |
| 256 metafile->InitFromData(data.bytes(), data.size()); |
| 257 return metafile; |
| 258 } |
| 238 | 259 |
| 239 } // namespace printing | 260 } // namespace printing |
| OLD | NEW |