blob: d265ca02a13f50f5e3bee2d84cecc1a29a327463 [file] [log] [blame]
[email protected]8f879292011-04-08 00:21:201// 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
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]67e16b392011-05-30 20:58:0920#include "third_party/skia/include/pdf/SkPDFFont.h"
[email protected]8f879292011-04-08 00:21:2021#include "third_party/skia/include/pdf/SkPDFPage.h"
22#include "ui/gfx/point.h"
23#include "ui/gfx/rect.h"
24#include "ui/gfx/size.h"
25
[email protected]b8d85bc2011-06-22 13:34:5726#if defined(OS_MACOSX)
27#include "printing/pdf_metafile_cg_mac.h"
28#endif
29
[email protected]8f879292011-04-08 00:21:2030namespace printing {
31
32struct PdfMetafileSkiaData {
33 SkRefPtr<SkPDFDevice> current_page_;
34 SkPDFDocument pdf_doc_;
35 SkDynamicMemoryWStream pdf_stream_;
[email protected]b8d85bc2011-06-22 13:34:5736#if defined(OS_MACOSX)
37 PdfMetafileCg pdf_cg_;
38#endif
[email protected]8f879292011-04-08 00:21:2039};
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
[email protected]62f2e802011-05-26 14:28:3551SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
[email protected]39892b92011-04-30 02:24:4452 const gfx::Size& page_size, const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2053 const float& scale_factor) {
[email protected]597516372011-07-01 05:10:4454 DCHECK (!page_outstanding_);
55 page_outstanding_ = true;
[email protected]8f879292011-04-08 00:21:2056
[email protected]a34f0122011-04-12 17:36:3957 // Adjust for the margins and apply the scale factor.
58 SkMatrix transform;
[email protected]39892b92011-04-30 02:24:4459 transform.setTranslate(SkIntToScalar(content_area.x()),
60 SkIntToScalar(content_area.y()));
[email protected]a34f0122011-04-12 17:36:3961 transform.preScale(SkFloatToScalar(scale_factor),
62 SkFloatToScalar(scale_factor));
63
[email protected]39892b92011-04-30 02:24:4464 // TODO(ctguil): Refactor: don't create the PDF device explicitly here.
65 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
66 SkISize pdf_content_size =
67 SkISize::Make(content_area.width(), content_area.height());
68 SkRefPtr<SkPDFDevice> pdf_device =
69 new SkPDFDevice(pdf_page_size, pdf_content_size, transform);
70 pdf_device->unref(); // SkRefPtr and new both took a reference.
[email protected]8f879292011-04-08 00:21:2071 skia::VectorPlatformDeviceSkia* device =
[email protected]39892b92011-04-30 02:24:4472 new skia::VectorPlatformDeviceSkia(pdf_device.get());
[email protected]8f879292011-04-08 00:21:2073 data_->current_page_ = device->PdfDevice();
74 return device;
75}
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();
81 return NULL;
82}
83
84bool PdfMetafileSkia::FinishPage() {
85 DCHECK(data_->current_page_.get());
86
87 data_->pdf_doc_.appendPage(data_->current_page_);
[email protected]597516372011-07-01 05:10:4488 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]597516372011-07-01 05:10:4497 if (page_outstanding_)
[email protected]8f879292011-04-08 00:21:2098 FinishPage();
[email protected]67e16b392011-05-30 20:58:0999
[email protected]597516372011-07-01 05:10:44100 data_->current_page_ = NULL;
[email protected]54ffd6f62011-06-02 17:29:05101 base::hash_set<SkFontID> font_set;
102
103 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages();
104 for (int page_number = 0; page_number < pages.count(); page_number++) {
105 const SkTDArray<SkPDFFont*>& font_resources =
106 pages[page_number]->getFontResources();
107 for (int font = 0; font < font_resources.count(); font++) {
108 SkFontID font_id = font_resources[font]->typeface()->uniqueID();
109 if (font_set.find(font_id) == font_set.end()) {
110 font_set.insert(font_id);
111 UMA_HISTOGRAM_ENUMERATION(
112 "PrintPreview.FontType",
113 font_resources[font]->getType(),
114 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
115 }
116 }
[email protected]67e16b392011-05-30 20:58:09117 }
118
[email protected]8f879292011-04-08 00:21:20119 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
120}
121
122uint32 PdfMetafileSkia::GetDataSize() const {
123 return data_->pdf_stream_.getOffset();
124}
125
126bool PdfMetafileSkia::GetData(void* dst_buffer,
127 uint32 dst_buffer_size) const {
128 if (dst_buffer_size < GetDataSize())
129 return false;
130
[email protected]e195fbf52011-06-27 16:51:20131 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
132 memcpy(dst_buffer, data.bytes(), dst_buffer_size);
[email protected]8f879292011-04-08 00:21:20133 return true;
134}
135
136bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
137 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20138 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
139 if (file_util::WriteFile(file_path,
140 reinterpret_cast<const char*>(data.data()),
[email protected]8f879292011-04-08 00:21:20141 GetDataSize()) != static_cast<int>(GetDataSize())) {
142 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
143 return false;
144 }
145 return true;
146}
147
148gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
149 // TODO(vandebo) add a method to get the page size for a given page to
150 // SkPDFDocument.
151 NOTIMPLEMENTED();
152 return gfx::Rect();
153}
154
155unsigned int PdfMetafileSkia::GetPageCount() const {
156 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
157 NOTIMPLEMENTED();
158 return 0;
159}
160
161gfx::NativeDrawingContext PdfMetafileSkia::context() const {
162 NOTREACHED();
163 return NULL;
164}
165
166#if defined(OS_WIN)
167bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
168 const RECT* rect) const {
169 NOTREACHED();
170 return false;
171}
172
173bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
174 NOTREACHED();
175 return false;
176}
177
178HENHMETAFILE PdfMetafileSkia::emf() const {
179 NOTREACHED();
180 return NULL;
181}
[email protected]b8d85bc2011-06-22 13:34:57182#elif defined(OS_MACOSX)
183/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
184 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
185 the drawing of the PDF into the canvas may result in a rasterized output.
186 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
187 should do something like the following CL in PluginInstance::PrintPDFOutput:
188http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
189*/
190bool PdfMetafileSkia::RenderPage(unsigned int page_number,
191 CGContextRef context,
192 const CGRect rect,
193 bool shrink_to_fit,
194 bool stretch_to_fit,
195 bool center_horizontally,
196 bool center_vertically) const {
197 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20198 if (data_->pdf_cg_.GetDataSize() == 0) {
199 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
200 data_->pdf_cg_.InitFromData(data.bytes(), data.size());
201 }
[email protected]b8d85bc2011-06-22 13:34:57202 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
203 stretch_to_fit, center_horizontally,
204 center_vertically);
205}
206#endif
[email protected]8f879292011-04-08 00:21:20207
208#if defined(OS_CHROMEOS)
209bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
210 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
211
212 if (fd.fd < 0) {
213 DLOG(ERROR) << "Invalid file descriptor!";
214 return false;
215 }
216
217 bool result = true;
[email protected]e195fbf52011-06-27 16:51:20218 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
219 if (file_util::WriteFileDescriptor(fd.fd,
220 reinterpret_cast<const char*>(data.data()),
[email protected]8f879292011-04-08 00:21:20221 GetDataSize()) !=
222 static_cast<int>(GetDataSize())) {
223 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
224 result = false;
225 }
226
227 if (fd.auto_close) {
228 if (HANDLE_EINTR(close(fd.fd)) < 0) {
229 DPLOG(WARNING) << "close";
230 result = false;
231 }
232 }
233 return result;
234}
235#endif
236
[email protected]c797a192011-06-15 16:25:09237PdfMetafileSkia::PdfMetafileSkia()
238 : data_(new PdfMetafileSkiaData),
[email protected]597516372011-07-01 05:10:44239 draft_(false),
240 page_outstanding_(false) {}
241
242PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
243 SkPDFDocument pdf_doc;
244 SkDynamicMemoryWStream pdf_stream;
245 if (!pdf_doc.appendPage(data_->current_page_))
246 return NULL;
247
248 if (!pdf_doc.emitPDF(&pdf_stream))
249 return NULL;
250
251 SkAutoDataUnref data(pdf_stream.copyToData());
252 if (data.size() == 0)
253 return NULL;
254
255 PdfMetafileSkia* metafile = new printing::PdfMetafileSkia;
256 metafile->InitFromData(data.bytes(), data.size());
257 return metafile;
258}
[email protected]8f879292011-04-08 00:21:20259
[email protected]8f879292011-04-08 00:21:20260} // namespace printing