Sync shouldn't install external extensions that user removed
[chromium-blink-merge.git] / printing / pdf_metafile_skia.cc
blobfdff44f56cf80175944d2be42ebe2278853e4c57
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
3 // found in the LICENSE file.
5 #include "printing/pdf_metafile_skia.h"
7 #include "base/eintr_wrapper.h"
8 #include "base/file_descriptor_posix.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"
13 #include "third_party/skia/include/core/SkData.h"
14 #include "third_party/skia/include/core/SkRefCnt.h"
15 #include "third_party/skia/include/core/SkScalar.h"
16 #include "third_party/skia/include/core/SkStream.h"
17 #include "third_party/skia/include/core/SkTypeface.h"
18 #include "third_party/skia/include/pdf/SkPDFDevice.h"
19 #include "third_party/skia/include/pdf/SkPDFDocument.h"
20 #include "ui/gfx/point.h"
21 #include "ui/gfx/rect.h"
22 #include "ui/gfx/size.h"
24 #if defined(OS_MACOSX)
25 #include "printing/pdf_metafile_cg_mac.h"
26 #endif
28 namespace printing {
30 struct PdfMetafileSkiaData {
31 SkRefPtr<SkPDFDevice> current_page_;
32 SkPDFDocument pdf_doc_;
33 SkDynamicMemoryWStream pdf_stream_;
34 #if defined(OS_MACOSX)
35 PdfMetafileCg pdf_cg_;
36 #endif
39 PdfMetafileSkia::~PdfMetafileSkia() {}
41 bool PdfMetafileSkia::Init() {
42 return true;
44 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
45 uint32 src_buffer_size) {
46 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
49 SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
50 const gfx::Size& page_size, const gfx::Rect& content_area,
51 const float& scale_factor) {
52 DCHECK(!page_outstanding_);
53 page_outstanding_ = true;
55 // Adjust for the margins and apply the scale factor.
56 SkMatrix transform;
57 transform.setTranslate(SkIntToScalar(content_area.x()),
58 SkIntToScalar(content_area.y()));
59 transform.preScale(SkFloatToScalar(scale_factor),
60 SkFloatToScalar(scale_factor));
62 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
63 SkISize pdf_content_size =
64 SkISize::Make(content_area.width(), content_area.height());
65 SkRefPtr<SkPDFDevice> pdf_device =
66 new skia::VectorPlatformDeviceSkia(pdf_page_size, pdf_content_size,
67 transform);
68 data_->current_page_ = pdf_device;
69 return pdf_device.get();
72 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
73 const gfx::Rect& content_area,
74 const float& scale_factor) {
75 NOTREACHED();
76 return NULL;
79 bool PdfMetafileSkia::FinishPage() {
80 DCHECK(data_->current_page_.get());
82 data_->pdf_doc_.appendPage(data_->current_page_.get());
83 page_outstanding_ = false;
84 return true;
87 bool PdfMetafileSkia::FinishDocument() {
88 // Don't do anything if we've already set the data in InitFromData.
89 if (data_->pdf_stream_.getOffset())
90 return true;
92 if (page_outstanding_)
93 FinishPage();
95 data_->current_page_ = NULL;
97 int font_counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1];
98 data_->pdf_doc_.getCountOfFontTypes(font_counts);
99 for (int type = 0;
100 type <= SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
101 type++) {
102 for (int count = 0; count < font_counts[type]; count++) {
103 UMA_HISTOGRAM_ENUMERATION(
104 "PrintPreview.FontType", type,
105 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
109 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
112 uint32 PdfMetafileSkia::GetDataSize() const {
113 return data_->pdf_stream_.getOffset();
116 bool PdfMetafileSkia::GetData(void* dst_buffer,
117 uint32 dst_buffer_size) const {
118 if (dst_buffer_size < GetDataSize())
119 return false;
121 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
122 memcpy(dst_buffer, data.bytes(), dst_buffer_size);
123 return true;
126 bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
127 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
128 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
129 if (file_util::WriteFile(file_path,
130 reinterpret_cast<const char*>(data.data()),
131 GetDataSize()) != static_cast<int>(GetDataSize())) {
132 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
133 return false;
135 return true;
138 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
139 // TODO(vandebo) add a method to get the page size for a given page to
140 // SkPDFDocument.
141 NOTIMPLEMENTED();
142 return gfx::Rect();
145 unsigned int PdfMetafileSkia::GetPageCount() const {
146 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
147 NOTIMPLEMENTED();
148 return 0;
151 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
152 NOTREACHED();
153 return NULL;
156 #if defined(OS_WIN)
157 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
158 const RECT* rect) const {
159 NOTREACHED();
160 return false;
163 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
164 NOTREACHED();
165 return false;
168 HENHMETAFILE PdfMetafileSkia::emf() const {
169 NOTREACHED();
170 return NULL;
172 #elif defined(OS_MACOSX)
173 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
174 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
175 the drawing of the PDF into the canvas may result in a rasterized output.
176 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
177 should do something like the following CL in PluginInstance::PrintPDFOutput:
178 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
180 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
181 CGContextRef context,
182 const CGRect rect,
183 bool shrink_to_fit,
184 bool stretch_to_fit,
185 bool center_horizontally,
186 bool center_vertically) const {
187 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
188 if (data_->pdf_cg_.GetDataSize() == 0) {
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
190 data_->pdf_cg_.InitFromData(data.bytes(), data.size());
192 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
193 stretch_to_fit, center_horizontally,
194 center_vertically);
196 #endif
198 #if defined(OS_CHROMEOS)
199 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
200 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
202 if (fd.fd < 0) {
203 DLOG(ERROR) << "Invalid file descriptor!";
204 return false;
207 bool result = true;
208 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
209 if (file_util::WriteFileDescriptor(fd.fd,
210 reinterpret_cast<const char*>(data.data()),
211 GetDataSize()) !=
212 static_cast<int>(GetDataSize())) {
213 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
214 result = false;
217 if (fd.auto_close) {
218 if (HANDLE_EINTR(close(fd.fd)) < 0) {
219 DPLOG(WARNING) << "close";
220 result = false;
223 return result;
225 #endif
227 PdfMetafileSkia::PdfMetafileSkia()
228 : data_(new PdfMetafileSkiaData),
229 page_outstanding_(false) {
232 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
233 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
234 SkDynamicMemoryWStream pdf_stream;
235 if (!pdf_doc.appendPage(data_->current_page_.get()))
236 return NULL;
238 if (!pdf_doc.emitPDF(&pdf_stream))
239 return NULL;
241 SkAutoDataUnref data(pdf_stream.copyToData());
242 if (data.size() == 0)
243 return NULL;
245 PdfMetafileSkia* metafile = new PdfMetafileSkia;
246 metafile->InitFromData(data.bytes(), data.size());
247 return metafile;
250 } // namespace printing