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" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "skia/ext/vector_platform_device_skia.h" | 12 #include "skia/ext/vector_platform_device_skia.h" |
13 #include "third_party/skia/include/core/SkRefCnt.h" | 13 #include "third_party/skia/include/core/SkRefCnt.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 14 #include "third_party/skia/include/core/SkScalar.h" |
15 #include "third_party/skia/include/core/SkStream.h" | 15 #include "third_party/skia/include/core/SkStream.h" |
16 #include "third_party/skia/include/core/SkTypeface.h" | 16 #include "third_party/skia/include/core/SkTypeface.h" |
17 #include "third_party/skia/include/pdf/SkPDFDevice.h" | 17 #include "third_party/skia/include/pdf/SkPDFDevice.h" |
18 #include "third_party/skia/include/pdf/SkPDFDocument.h" | 18 #include "third_party/skia/include/pdf/SkPDFDocument.h" |
19 #include "third_party/skia/include/pdf/SkPDFFont.h" | 19 #include "third_party/skia/include/pdf/SkPDFFont.h" |
20 #include "third_party/skia/include/pdf/SkPDFPage.h" | 20 #include "third_party/skia/include/pdf/SkPDFPage.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 namespace { | |
26 typedef base::hash_map<SkFontID, SkAdvancedTypefaceMetrics::FontType> | |
27 FontTypeMap; | |
28 }; | |
29 | |
30 namespace printing { | 25 namespace printing { |
31 | 26 |
32 struct PdfMetafileSkiaData { | 27 struct PdfMetafileSkiaData { |
33 SkRefPtr<SkPDFDevice> current_page_; | 28 SkRefPtr<SkPDFDevice> current_page_; |
34 SkPDFDocument pdf_doc_; | 29 SkPDFDocument pdf_doc_; |
35 SkDynamicMemoryWStream pdf_stream_; | 30 SkDynamicMemoryWStream pdf_stream_; |
36 FontTypeMap font_type_stats_; | |
37 }; | 31 }; |
38 | 32 |
39 PdfMetafileSkia::~PdfMetafileSkia() {} | 33 PdfMetafileSkia::~PdfMetafileSkia() {} |
40 | 34 |
41 bool PdfMetafileSkia::Init() { | 35 bool PdfMetafileSkia::Init() { |
42 return true; | 36 return true; |
43 } | 37 } |
44 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 38 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
45 uint32 src_buffer_size) { | 39 uint32 src_buffer_size) { |
46 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | 40 return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
(...skipping 27 matching lines...) Expand all Loading... |
74 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 68 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
75 const gfx::Rect& content_area, | 69 const gfx::Rect& content_area, |
76 const float& scale_factor) { | 70 const float& scale_factor) { |
77 NOTREACHED(); | 71 NOTREACHED(); |
78 return NULL; | 72 return NULL; |
79 } | 73 } |
80 | 74 |
81 bool PdfMetafileSkia::FinishPage() { | 75 bool PdfMetafileSkia::FinishPage() { |
82 DCHECK(data_->current_page_.get()); | 76 DCHECK(data_->current_page_.get()); |
83 | 77 |
84 const SkTDArray<SkPDFFont*>& font_resources = | |
85 data_->current_page_->getFontResources(); | |
86 for (int i = 0; i < font_resources.count(); i++) { | |
87 SkFontID key = font_resources[i]->typeface()->uniqueID(); | |
88 data_->font_type_stats_[key] = font_resources[i]->getType(); | |
89 } | |
90 | |
91 data_->pdf_doc_.appendPage(data_->current_page_); | 78 data_->pdf_doc_.appendPage(data_->current_page_); |
92 data_->current_page_ = NULL; | 79 data_->current_page_ = NULL; |
93 return true; | 80 return true; |
94 } | 81 } |
95 | 82 |
96 bool PdfMetafileSkia::FinishDocument() { | 83 bool PdfMetafileSkia::FinishDocument() { |
97 // Don't do anything if we've already set the data in InitFromData. | 84 // Don't do anything if we've already set the data in InitFromData. |
98 if (data_->pdf_stream_.getOffset()) | 85 if (data_->pdf_stream_.getOffset()) |
99 return true; | 86 return true; |
100 | 87 |
101 if (data_->current_page_.get()) | 88 if (data_->current_page_.get()) |
102 FinishPage(); | 89 FinishPage(); |
103 | 90 |
104 for (FontTypeMap::const_iterator it = data_->font_type_stats_.begin(); | 91 base::hash_set<SkFontID> font_set; |
105 it != data_->font_type_stats_.end(); | 92 |
106 it++) { | 93 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); |
107 UMA_HISTOGRAM_ENUMERATION( | 94 for (int page_number = 0; page_number < pages.count(); page_number++) { |
108 "PrintPreview.FontType", | 95 const SkTDArray<SkPDFFont*>& font_resources = |
109 it->second, | 96 pages[page_number]->getFontResources(); |
110 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1); | 97 for (int font = 0; font < font_resources.count(); font++) { |
| 98 SkFontID font_id = font_resources[font]->typeface()->uniqueID(); |
| 99 if (font_set.find(font_id) == font_set.end()) { |
| 100 font_set.insert(font_id); |
| 101 UMA_HISTOGRAM_ENUMERATION( |
| 102 "PrintPreview.FontType", |
| 103 font_resources[font]->getType(), |
| 104 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1); |
| 105 } |
| 106 } |
111 } | 107 } |
112 | 108 |
113 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); | 109 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); |
114 } | 110 } |
115 | 111 |
116 uint32 PdfMetafileSkia::GetDataSize() const { | 112 uint32 PdfMetafileSkia::GetDataSize() const { |
117 return data_->pdf_stream_.getOffset(); | 113 return data_->pdf_stream_.getOffset(); |
118 } | 114 } |
119 | 115 |
120 bool PdfMetafileSkia::GetData(void* dst_buffer, | 116 bool PdfMetafileSkia::GetData(void* dst_buffer, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 result = false; | 191 result = false; |
196 } | 192 } |
197 } | 193 } |
198 return result; | 194 return result; |
199 } | 195 } |
200 #endif | 196 #endif |
201 | 197 |
202 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {} | 198 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {} |
203 | 199 |
204 } // namespace printing | 200 } // namespace printing |
OLD | NEW |