blob: 899232dabc2ffbfafc728c62914c3946c5434ca6 [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
[email protected]14c1c232013-06-11 17:52:447#include "base/containers/hash_tables.h"
thestig22dfc4012014-09-05 08:29:448#include "base/files/file_util.h"
[email protected]67e16b392011-05-30 20:58:099#include "base/metrics/histogram.h"
[email protected]cb154062014-01-17 03:32:4010#include "base/numerics/safe_conversions.h"
[email protected]2025d002012-11-14 20:54:3511#include "base/posix/eintr_wrapper.h"
[email protected]584abe72013-05-18 09:40:2612#include "skia/ext/refptr.h"
[email protected]8f879292011-04-08 00:21:2013#include "skia/ext/vector_platform_device_skia.h"
[email protected]e195fbf52011-06-27 16:51:2014#include "third_party/skia/include/core/SkData.h"
[email protected]8f879292011-04-08 00:21:2015#include "third_party/skia/include/core/SkRefCnt.h"
[email protected]a34f0122011-04-12 17:36:3916#include "third_party/skia/include/core/SkScalar.h"
[email protected]8f879292011-04-08 00:21:2017#include "third_party/skia/include/core/SkStream.h"
[email protected]67e16b392011-05-30 20:58:0918#include "third_party/skia/include/core/SkTypeface.h"
[email protected]8f879292011-04-08 00:21:2019#include "third_party/skia/include/pdf/SkPDFDevice.h"
20#include "third_party/skia/include/pdf/SkPDFDocument.h"
[email protected]8f879292011-04-08 00:21:2021#include "ui/gfx/point.h"
22#include "ui/gfx/rect.h"
23#include "ui/gfx/size.h"
24
[email protected]b8d85bc2011-06-22 13:34:5725#if defined(OS_MACOSX)
26#include "printing/pdf_metafile_cg_mac.h"
27#endif
28
[email protected]0daaebfe2014-03-15 00:09:0529#if defined(OS_POSIX)
30#include "base/file_descriptor_posix.h"
31#endif
32
[email protected]8f879292011-04-08 00:21:2033namespace printing {
34
35struct PdfMetafileSkiaData {
[email protected]584abe72013-05-18 09:40:2636 skia::RefPtr<SkPDFDevice> current_page_;
[email protected]8f879292011-04-08 00:21:2037 SkPDFDocument pdf_doc_;
38 SkDynamicMemoryWStream pdf_stream_;
[email protected]b8d85bc2011-06-22 13:34:5739#if defined(OS_MACOSX)
40 PdfMetafileCg pdf_cg_;
41#endif
[email protected]8f879292011-04-08 00:21:2042};
43
44PdfMetafileSkia::~PdfMetafileSkia() {}
45
46bool PdfMetafileSkia::Init() {
47 return true;
48}
49bool PdfMetafileSkia::InitFromData(const void* src_buffer,
50 uint32 src_buffer_size) {
51 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
52}
53
[email protected]598e4f82013-08-26 18:37:2954SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas(
[email protected]534c4fb2011-08-02 16:44:2055 const gfx::Size& page_size, const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2056 const float& scale_factor) {
[email protected]534c4fb2011-08-02 16:44:2057 DCHECK(!page_outstanding_);
58 page_outstanding_ = true;
[email protected]8f879292011-04-08 00:21:2059
[email protected]a34f0122011-04-12 17:36:3960 // Adjust for the margins and apply the scale factor.
61 SkMatrix transform;
[email protected]39892b92011-04-30 02:24:4462 transform.setTranslate(SkIntToScalar(content_area.x()),
63 SkIntToScalar(content_area.y()));
[email protected]a34f0122011-04-12 17:36:3964 transform.preScale(SkFloatToScalar(scale_factor),
65 SkFloatToScalar(scale_factor));
66
[email protected]39892b92011-04-30 02:24:4467 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
68 SkISize pdf_content_size =
69 SkISize::Make(content_area.width(), content_area.height());
[email protected]584abe72013-05-18 09:40:2670 skia::RefPtr<SkPDFDevice> pdf_device =
71 skia::AdoptRef(new skia::VectorPlatformDeviceSkia(
72 pdf_page_size, pdf_content_size, transform));
[email protected]8ebe83a42011-08-28 16:51:3173 data_->current_page_ = pdf_device;
74 return pdf_device.get();
[email protected]8f879292011-04-08 00:21:2075}
76
77bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4478 const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2079 const float& scale_factor) {
80 NOTREACHED();
[email protected]abdeb5d22012-05-16 06:57:1381 return false;
[email protected]8f879292011-04-08 00:21:2082}
83
84bool PdfMetafileSkia::FinishPage() {
85 DCHECK(data_->current_page_.get());
86
[email protected]534c4fb2011-08-02 16:44:2087 data_->pdf_doc_.appendPage(data_->current_page_.get());
88 page_outstanding_ = false;
[email protected]8f879292011-04-08 00:21:2089 return true;
90}
91
92bool PdfMetafileSkia::FinishDocument() {
93 // Don't do anything if we've already set the data in InitFromData.
94 if (data_->pdf_stream_.getOffset())
95 return true;
96
[email protected]534c4fb2011-08-02 16:44:2097 if (page_outstanding_)
[email protected]8f879292011-04-08 00:21:2098 FinishPage();
[email protected]67e16b392011-05-30 20:58:0999
[email protected]584abe72013-05-18 09:40:26100 data_->current_page_.clear();
[email protected]54ffd6f62011-06-02 17:29:05101
[email protected]3cd37bc02013-12-09 20:52:11102 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2];
[email protected]0a4392a2012-03-23 17:50:19103 data_->pdf_doc_.getCountOfFontTypes(font_counts);
104 for (int type = 0;
[email protected]3cd37bc02013-12-09 20:52:11105 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1;
[email protected]0a4392a2012-03-23 17:50:19106 type++) {
107 for (int count = 0; count < font_counts[type]; count++) {
108 UMA_HISTOGRAM_ENUMERATION(
109 "PrintPreview.FontType", type,
[email protected]3cd37bc02013-12-09 20:52:11110 SkAdvancedTypefaceMetrics::kOther_Font + 2);
[email protected]54ffd6f62011-06-02 17:29:05111 }
[email protected]67e16b392011-05-30 20:58:09112 }
113
[email protected]8f879292011-04-08 00:21:20114 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
115}
116
117uint32 PdfMetafileSkia::GetDataSize() const {
[email protected]cb154062014-01-17 03:32:40118 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset());
[email protected]8f879292011-04-08 00:21:20119}
120
121bool PdfMetafileSkia::GetData(void* dst_buffer,
122 uint32 dst_buffer_size) const {
123 if (dst_buffer_size < GetDataSize())
124 return false;
125
[email protected]e195fbf52011-06-27 16:51:20126 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
[email protected]24b86abf2012-07-12 12:32:32127 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
[email protected]8f879292011-04-08 00:21:20128 return true;
129}
130
[email protected]79f63882013-02-10 05:15:45131bool PdfMetafileSkia::SaveTo(const base::FilePath& file_path) const {
[email protected]8f879292011-04-08 00:21:20132 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20133 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
[email protected]e5c2a22e2014-03-06 20:42:30134 if (base::WriteFile(file_path,
135 reinterpret_cast<const char*>(data->data()),
136 GetDataSize()) != static_cast<int>(GetDataSize())) {
[email protected]8f879292011-04-08 00:21:20137 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
138 return false;
139 }
140 return true;
141}
142
143gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
144 // TODO(vandebo) add a method to get the page size for a given page to
145 // SkPDFDocument.
146 NOTIMPLEMENTED();
147 return gfx::Rect();
148}
149
150unsigned int PdfMetafileSkia::GetPageCount() const {
151 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
152 NOTIMPLEMENTED();
153 return 0;
154}
155
156gfx::NativeDrawingContext PdfMetafileSkia::context() const {
157 NOTREACHED();
158 return NULL;
159}
160
161#if defined(OS_WIN)
162bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
163 const RECT* rect) const {
164 NOTREACHED();
165 return false;
166}
167
168bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
169 NOTREACHED();
170 return false;
171}
172
173HENHMETAFILE PdfMetafileSkia::emf() const {
174 NOTREACHED();
175 return NULL;
176}
[email protected]b8d85bc2011-06-22 13:34:57177#elif defined(OS_MACOSX)
178/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
179 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
180 the drawing of the PDF into the canvas may result in a rasterized output.
181 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
182 should do something like the following CL in PluginInstance::PrintPDFOutput:
183http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
184*/
185bool PdfMetafileSkia::RenderPage(unsigned int page_number,
186 CGContextRef context,
187 const CGRect rect,
[email protected]b5cf844c2012-06-18 21:49:20188 const MacRenderPageParams& params) const {
[email protected]b8d85bc2011-06-22 13:34:57189 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20190 if (data_->pdf_cg_.GetDataSize() == 0) {
191 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
[email protected]24b86abf2012-07-12 12:32:32192 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
[email protected]e195fbf52011-06-27 16:51:20193 }
[email protected]b5cf844c2012-06-18 21:49:20194 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
[email protected]b8d85bc2011-06-22 13:34:57195}
196#endif
[email protected]8f879292011-04-08 00:21:20197
[email protected]b25f0032013-08-19 22:26:25198#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]8f879292011-04-08 00:21:20199bool 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());
[email protected]e5c2a22e2014-03-06 20:42:30209 if (base::WriteFileDescriptor(fd.fd,
210 reinterpret_cast<const char*>(data->data()),
211 GetDataSize()) !=
[email protected]8f879292011-04-08 00:21:20212 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) {
[email protected]d89eec82013-12-03 14:10:59218 if (IGNORE_EINTR(close(fd.fd)) < 0) {
[email protected]8f879292011-04-08 00:21:20219 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());
[email protected]24b86abf2012-07-12 12:32:32242 if (data->size() == 0)
[email protected]597516372011-07-01 05:10:44243 return NULL;
244
[email protected]d91db112011-10-18 20:58:51245 PdfMetafileSkia* metafile = new PdfMetafileSkia;
[email protected]848c9c2f2013-01-21 17:58:28246 metafile->InitFromData(data->bytes(),
[email protected]cb154062014-01-17 03:32:40247 base::checked_cast<uint32>(data->size()));
[email protected]597516372011-07-01 05:10:44248 return metafile;
249}
[email protected]8f879292011-04-08 00:21:20250
[email protected]8f879292011-04-08 00:21:20251} // namespace printing