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