blob: 2242cfc0175781e17b9dfd0393cdf535de38ca94 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "printing/pdf_metafile_skia.h"
6
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01007#include "base/containers/hash_tables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "base/file_descriptor_posix.h"
9#include "base/file_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "base/metrics/histogram.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000011#include "base/posix/eintr_wrapper.h"
12#include "base/safe_numerics.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010013#include "skia/ext/refptr.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "skia/ext/vector_platform_device_skia.h"
15#include "third_party/skia/include/core/SkData.h"
16#include "third_party/skia/include/core/SkRefCnt.h"
17#include "third_party/skia/include/core/SkScalar.h"
18#include "third_party/skia/include/core/SkStream.h"
19#include "third_party/skia/include/core/SkTypeface.h"
20#include "third_party/skia/include/pdf/SkPDFDevice.h"
21#include "third_party/skia/include/pdf/SkPDFDocument.h"
22#include "ui/gfx/point.h"
23#include "ui/gfx/rect.h"
24#include "ui/gfx/size.h"
25
26#if defined(OS_MACOSX)
27#include "printing/pdf_metafile_cg_mac.h"
28#endif
29
30namespace printing {
31
32struct PdfMetafileSkiaData {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010033 skia::RefPtr<SkPDFDevice> current_page_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000034 SkPDFDocument pdf_doc_;
35 SkDynamicMemoryWStream pdf_stream_;
36#if defined(OS_MACOSX)
37 PdfMetafileCg pdf_cg_;
38#endif
39};
40
41PdfMetafileSkia::~PdfMetafileSkia() {}
42
43bool PdfMetafileSkia::Init() {
44 return true;
45}
46bool PdfMetafileSkia::InitFromData(const void* src_buffer,
47 uint32 src_buffer_size) {
48 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
49}
50
Torne (Richard Coles)424c4d72013-08-30 15:14:49 +010051SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052 const gfx::Size& page_size, const gfx::Rect& content_area,
53 const float& scale_factor) {
54 DCHECK(!page_outstanding_);
55 page_outstanding_ = true;
56
57 // Adjust for the margins and apply the scale factor.
58 SkMatrix transform;
59 transform.setTranslate(SkIntToScalar(content_area.x()),
60 SkIntToScalar(content_area.y()));
61 transform.preScale(SkFloatToScalar(scale_factor),
62 SkFloatToScalar(scale_factor));
63
64 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
65 SkISize pdf_content_size =
66 SkISize::Make(content_area.width(), content_area.height());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010067 skia::RefPtr<SkPDFDevice> pdf_device =
68 skia::AdoptRef(new skia::VectorPlatformDeviceSkia(
69 pdf_page_size, pdf_content_size, transform));
Torne (Richard Coles)58218062012-11-14 11:43:16 +000070 data_->current_page_ = pdf_device;
71 return pdf_device.get();
72}
73
74bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
75 const gfx::Rect& content_area,
76 const float& scale_factor) {
77 NOTREACHED();
78 return false;
79}
80
81bool PdfMetafileSkia::FinishPage() {
82 DCHECK(data_->current_page_.get());
83
84 data_->pdf_doc_.appendPage(data_->current_page_.get());
85 page_outstanding_ = false;
86 return true;
87}
88
89bool PdfMetafileSkia::FinishDocument() {
90 // Don't do anything if we've already set the data in InitFromData.
91 if (data_->pdf_stream_.getOffset())
92 return true;
93
94 if (page_outstanding_)
95 FinishPage();
96
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010097 data_->current_page_.clear();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000098
99 int font_counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1];
100 data_->pdf_doc_.getCountOfFontTypes(font_counts);
101 for (int type = 0;
102 type <= SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
103 type++) {
104 for (int count = 0; count < font_counts[type]; count++) {
105 UMA_HISTOGRAM_ENUMERATION(
106 "PrintPreview.FontType", type,
107 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
108 }
109 }
110
111 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
112}
113
114uint32 PdfMetafileSkia::GetDataSize() const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000115 return base::checked_numeric_cast<uint32>(data_->pdf_stream_.getOffset());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116}
117
118bool PdfMetafileSkia::GetData(void* dst_buffer,
119 uint32 dst_buffer_size) const {
120 if (dst_buffer_size < GetDataSize())
121 return false;
122
123 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
124 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
125 return true;
126}
127
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000128bool PdfMetafileSkia::SaveTo(const base::FilePath& file_path) const {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000129 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
130 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
131 if (file_util::WriteFile(file_path,
132 reinterpret_cast<const char*>(data->data()),
133 GetDataSize()) != static_cast<int>(GetDataSize())) {
134 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
135 return false;
136 }
137 return true;
138}
139
140gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
141 // TODO(vandebo) add a method to get the page size for a given page to
142 // SkPDFDocument.
143 NOTIMPLEMENTED();
144 return gfx::Rect();
145}
146
147unsigned int PdfMetafileSkia::GetPageCount() const {
148 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
149 NOTIMPLEMENTED();
150 return 0;
151}
152
153gfx::NativeDrawingContext PdfMetafileSkia::context() const {
154 NOTREACHED();
155 return NULL;
156}
157
158#if defined(OS_WIN)
159bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
160 const RECT* rect) const {
161 NOTREACHED();
162 return false;
163}
164
165bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
166 NOTREACHED();
167 return false;
168}
169
170HENHMETAFILE PdfMetafileSkia::emf() const {
171 NOTREACHED();
172 return NULL;
173}
174#elif defined(OS_MACOSX)
175/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
176 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
177 the drawing of the PDF into the canvas may result in a rasterized output.
178 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
179 should do something like the following CL in PluginInstance::PrintPDFOutput:
180http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
181*/
182bool PdfMetafileSkia::RenderPage(unsigned int page_number,
183 CGContextRef context,
184 const CGRect rect,
185 const MacRenderPageParams& params) const {
186 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
187 if (data_->pdf_cg_.GetDataSize() == 0) {
188 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
189 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
190 }
191 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
192}
193#endif
194
Torne (Richard Coles)3551c9c2013-08-23 16:39:15 +0100195#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000196bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
197 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
198
199 if (fd.fd < 0) {
200 DLOG(ERROR) << "Invalid file descriptor!";
201 return false;
202 }
203
204 bool result = true;
205 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
206 if (file_util::WriteFileDescriptor(fd.fd,
207 reinterpret_cast<const char*>(data->data()),
208 GetDataSize()) !=
209 static_cast<int>(GetDataSize())) {
210 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
211 result = false;
212 }
213
214 if (fd.auto_close) {
215 if (HANDLE_EINTR(close(fd.fd)) < 0) {
216 DPLOG(WARNING) << "close";
217 result = false;
218 }
219 }
220 return result;
221}
222#endif
223
224PdfMetafileSkia::PdfMetafileSkia()
225 : data_(new PdfMetafileSkiaData),
226 page_outstanding_(false) {
227}
228
229PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
230 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
231 SkDynamicMemoryWStream pdf_stream;
232 if (!pdf_doc.appendPage(data_->current_page_.get()))
233 return NULL;
234
235 if (!pdf_doc.emitPDF(&pdf_stream))
236 return NULL;
237
238 SkAutoDataUnref data(pdf_stream.copyToData());
239 if (data->size() == 0)
240 return NULL;
241
242 PdfMetafileSkia* metafile = new PdfMetafileSkia;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000243 metafile->InitFromData(data->bytes(),
244 base::checked_numeric_cast<uint32>(data->size()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000245 return metafile;
246}
247
248} // namespace printing