blob: b1b35edde81a32fc7b758029844b5a11c678520d [file] [log] [blame]
[email protected]0a4392a2012-03-23 17:50:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8f879292011-04-08 00:21:202// 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
7#include "base/eintr_wrapper.h"
8#include "base/file_descriptor_posix.h"
9#include "base/file_util.h"
[email protected]67e16b392011-05-30 20:58:0910#include "base/hash_tables.h"
11#include "base/metrics/histogram.h"
[email protected]8f879292011-04-08 00:21:2012#include "skia/ext/vector_platform_device_skia.h"
[email protected]e195fbf52011-06-27 16:51:2013#include "third_party/skia/include/core/SkData.h"
[email protected]8f879292011-04-08 00:21:2014#include "third_party/skia/include/core/SkRefCnt.h"
[email protected]a34f0122011-04-12 17:36:3915#include "third_party/skia/include/core/SkScalar.h"
[email protected]8f879292011-04-08 00:21:2016#include "third_party/skia/include/core/SkStream.h"
[email protected]67e16b392011-05-30 20:58:0917#include "third_party/skia/include/core/SkTypeface.h"
[email protected]8f879292011-04-08 00:21:2018#include "third_party/skia/include/pdf/SkPDFDevice.h"
19#include "third_party/skia/include/pdf/SkPDFDocument.h"
[email protected]8f879292011-04-08 00:21:2020#include "ui/gfx/point.h"
21#include "ui/gfx/rect.h"
22#include "ui/gfx/size.h"
23
[email protected]b8d85bc2011-06-22 13:34:5724#if defined(OS_MACOSX)
25#include "printing/pdf_metafile_cg_mac.h"
26#endif
27
[email protected]8f879292011-04-08 00:21:2028namespace printing {
29
30struct PdfMetafileSkiaData {
31 SkRefPtr<SkPDFDevice> current_page_;
32 SkPDFDocument pdf_doc_;
33 SkDynamicMemoryWStream pdf_stream_;
[email protected]b8d85bc2011-06-22 13:34:5734#if defined(OS_MACOSX)
35 PdfMetafileCg pdf_cg_;
36#endif
[email protected]8f879292011-04-08 00:21:2037};
38
39PdfMetafileSkia::~PdfMetafileSkia() {}
40
41bool PdfMetafileSkia::Init() {
42 return true;
43}
44bool PdfMetafileSkia::InitFromData(const void* src_buffer,
45 uint32 src_buffer_size) {
46 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
47}
48
[email protected]62f2e802011-05-26 14:28:3549SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
[email protected]534c4fb2011-08-02 16:44:2050 const gfx::Size& page_size, const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2051 const float& scale_factor) {
[email protected]534c4fb2011-08-02 16:44:2052 DCHECK(!page_outstanding_);
53 page_outstanding_ = true;
[email protected]8f879292011-04-08 00:21:2054
[email protected]a34f0122011-04-12 17:36:3955 // Adjust for the margins and apply the scale factor.
56 SkMatrix transform;
[email protected]39892b92011-04-30 02:24:4457 transform.setTranslate(SkIntToScalar(content_area.x()),
58 SkIntToScalar(content_area.y()));
[email protected]a34f0122011-04-12 17:36:3959 transform.preScale(SkFloatToScalar(scale_factor),
60 SkFloatToScalar(scale_factor));
61
[email protected]39892b92011-04-30 02:24:4462 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 =
[email protected]8ebe83a42011-08-28 16:51:3166 new skia::VectorPlatformDeviceSkia(pdf_page_size, pdf_content_size,
67 transform);
68 data_->current_page_ = pdf_device;
69 return pdf_device.get();
[email protected]8f879292011-04-08 00:21:2070}
71
72bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4473 const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2074 const float& scale_factor) {
75 NOTREACHED();
[email protected]abdeb5d22012-05-16 06:57:1376 return false;
[email protected]8f879292011-04-08 00:21:2077}
78
79bool PdfMetafileSkia::FinishPage() {
80 DCHECK(data_->current_page_.get());
81
[email protected]534c4fb2011-08-02 16:44:2082 data_->pdf_doc_.appendPage(data_->current_page_.get());
83 page_outstanding_ = false;
[email protected]8f879292011-04-08 00:21:2084 return true;
85}
86
87bool 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;
91
[email protected]534c4fb2011-08-02 16:44:2092 if (page_outstanding_)
[email protected]8f879292011-04-08 00:21:2093 FinishPage();
[email protected]67e16b392011-05-30 20:58:0994
[email protected]597516372011-07-01 05:10:4495 data_->current_page_ = NULL;
[email protected]54ffd6f62011-06-02 17:29:0596
[email protected]0a4392a2012-03-23 17:50:1997 int font_counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1];
[email protected]0a4392a2012-03-23 17:50:1998 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);
[email protected]54ffd6f62011-06-02 17:29:05106 }
[email protected]67e16b392011-05-30 20:58:09107 }
108
[email protected]8f879292011-04-08 00:21:20109 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
110}
111
112uint32 PdfMetafileSkia::GetDataSize() const {
113 return data_->pdf_stream_.getOffset();
114}
115
116bool PdfMetafileSkia::GetData(void* dst_buffer,
117 uint32 dst_buffer_size) const {
118 if (dst_buffer_size < GetDataSize())
119 return false;
120
[email protected]e195fbf52011-06-27 16:51:20121 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
122 memcpy(dst_buffer, data.bytes(), dst_buffer_size);
[email protected]8f879292011-04-08 00:21:20123 return true;
124}
125
126bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
127 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20128 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
129 if (file_util::WriteFile(file_path,
130 reinterpret_cast<const char*>(data.data()),
[email protected]8f879292011-04-08 00:21:20131 GetDataSize()) != static_cast<int>(GetDataSize())) {
132 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
133 return false;
134 }
135 return true;
136}
137
138gfx::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();
143}
144
145unsigned int PdfMetafileSkia::GetPageCount() const {
146 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
147 NOTIMPLEMENTED();
148 return 0;
149}
150
151gfx::NativeDrawingContext PdfMetafileSkia::context() const {
152 NOTREACHED();
153 return NULL;
154}
155
156#if defined(OS_WIN)
157bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
158 const RECT* rect) const {
159 NOTREACHED();
160 return false;
161}
162
163bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
164 NOTREACHED();
165 return false;
166}
167
168HENHMETAFILE PdfMetafileSkia::emf() const {
169 NOTREACHED();
170 return NULL;
171}
[email protected]b8d85bc2011-06-22 13:34:57172#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:
178http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
179*/
180bool 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);
[email protected]e195fbf52011-06-27 16:51:20188 if (data_->pdf_cg_.GetDataSize() == 0) {
189 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
190 data_->pdf_cg_.InitFromData(data.bytes(), data.size());
191 }
[email protected]b8d85bc2011-06-22 13:34:57192 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
193 stretch_to_fit, center_horizontally,
194 center_vertically);
195}
196#endif
[email protected]8f879292011-04-08 00:21:20197
198#if defined(OS_CHROMEOS)
199bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
200 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
201
202 if (fd.fd < 0) {
203 DLOG(ERROR) << "Invalid file descriptor!";
204 return false;
205 }
206
207 bool result = true;
[email protected]e195fbf52011-06-27 16:51:20208 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
209 if (file_util::WriteFileDescriptor(fd.fd,
210 reinterpret_cast<const char*>(data.data()),
[email protected]8f879292011-04-08 00:21:20211 GetDataSize()) !=
212 static_cast<int>(GetDataSize())) {
213 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
214 result = false;
215 }
216
217 if (fd.auto_close) {
218 if (HANDLE_EINTR(close(fd.fd)) < 0) {
219 DPLOG(WARNING) << "close";
220 result = false;
221 }
222 }
223 return result;
224}
225#endif
226
[email protected]c797a192011-06-15 16:25:09227PdfMetafileSkia::PdfMetafileSkia()
228 : data_(new PdfMetafileSkiaData),
[email protected]534c4fb2011-08-02 16:44:20229 page_outstanding_(false) {
[email protected]19b9d3b2011-07-23 02:08:57230}
[email protected]597516372011-07-01 05:10:44231
232PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
[email protected]19b9d3b2011-07-23 02:08:57233 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
[email protected]597516372011-07-01 05:10:44234 SkDynamicMemoryWStream pdf_stream;
[email protected]b1e544f2011-07-21 14:52:25235 if (!pdf_doc.appendPage(data_->current_page_.get()))
[email protected]597516372011-07-01 05:10:44236 return NULL;
237
238 if (!pdf_doc.emitPDF(&pdf_stream))
239 return NULL;
240
241 SkAutoDataUnref data(pdf_stream.copyToData());
242 if (data.size() == 0)
243 return NULL;
244
[email protected]d91db112011-10-18 20:58:51245 PdfMetafileSkia* metafile = new PdfMetafileSkia;
[email protected]597516372011-07-01 05:10:44246 metafile->InitFromData(data.bytes(), data.size());
247 return metafile;
248}
[email protected]8f879292011-04-08 00:21:20249
[email protected]8f879292011-04-08 00:21:20250} // namespace printing