blob: d335545a0a18af8a63ea0dd6694d3246d9055751 [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"
13#include "third_party/skia/include/core/SkRefCnt.h"
[email protected]a34f0122011-04-12 17:36:3914#include "third_party/skia/include/core/SkScalar.h"
[email protected]8f879292011-04-08 00:21:2015#include "third_party/skia/include/core/SkStream.h"
[email protected]67e16b392011-05-30 20:58:0916#include "third_party/skia/include/core/SkTypeface.h"
[email protected]8f879292011-04-08 00:21:2017#include "third_party/skia/include/pdf/SkPDFDevice.h"
18#include "third_party/skia/include/pdf/SkPDFDocument.h"
[email protected]67e16b392011-05-30 20:58:0919#include "third_party/skia/include/pdf/SkPDFFont.h"
[email protected]8f879292011-04-08 00:21:2020#include "third_party/skia/include/pdf/SkPDFPage.h"
21#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]8f879292011-04-08 00:21:2029namespace printing {
30
31struct PdfMetafileSkiaData {
32 SkRefPtr<SkPDFDevice> current_page_;
33 SkPDFDocument pdf_doc_;
34 SkDynamicMemoryWStream pdf_stream_;
[email protected]b8d85bc2011-06-22 13:34:5735#if defined(OS_MACOSX)
36 PdfMetafileCg pdf_cg_;
37#endif
[email protected]8f879292011-04-08 00:21:2038};
39
40PdfMetafileSkia::~PdfMetafileSkia() {}
41
42bool PdfMetafileSkia::Init() {
43 return true;
44}
45bool PdfMetafileSkia::InitFromData(const void* src_buffer,
46 uint32 src_buffer_size) {
47 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
48}
49
[email protected]62f2e802011-05-26 14:28:3550SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
[email protected]39892b92011-04-30 02:24:4451 const gfx::Size& page_size, const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2052 const float& scale_factor) {
53 DCHECK(data_->current_page_.get() == NULL);
54
[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 // TODO(ctguil): Refactor: don't create the PDF device explicitly here.
63 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
64 SkISize pdf_content_size =
65 SkISize::Make(content_area.width(), content_area.height());
66 SkRefPtr<SkPDFDevice> pdf_device =
67 new SkPDFDevice(pdf_page_size, pdf_content_size, transform);
68 pdf_device->unref(); // SkRefPtr and new both took a reference.
[email protected]8f879292011-04-08 00:21:2069 skia::VectorPlatformDeviceSkia* device =
[email protected]39892b92011-04-30 02:24:4470 new skia::VectorPlatformDeviceSkia(pdf_device.get());
[email protected]8f879292011-04-08 00:21:2071 data_->current_page_ = device->PdfDevice();
72 return device;
73}
74
75bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4476 const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2077 const float& scale_factor) {
78 NOTREACHED();
79 return NULL;
80}
81
82bool PdfMetafileSkia::FinishPage() {
83 DCHECK(data_->current_page_.get());
84
85 data_->pdf_doc_.appendPage(data_->current_page_);
86 data_->current_page_ = NULL;
87 return true;
88}
89
90bool PdfMetafileSkia::FinishDocument() {
91 // Don't do anything if we've already set the data in InitFromData.
92 if (data_->pdf_stream_.getOffset())
93 return true;
94
95 if (data_->current_page_.get())
96 FinishPage();
[email protected]67e16b392011-05-30 20:58:0997
[email protected]54ffd6f62011-06-02 17:29:0598 base::hash_set<SkFontID> font_set;
99
100 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages();
101 for (int page_number = 0; page_number < pages.count(); page_number++) {
102 const SkTDArray<SkPDFFont*>& font_resources =
103 pages[page_number]->getFontResources();
104 for (int font = 0; font < font_resources.count(); font++) {
105 SkFontID font_id = font_resources[font]->typeface()->uniqueID();
106 if (font_set.find(font_id) == font_set.end()) {
107 font_set.insert(font_id);
108 UMA_HISTOGRAM_ENUMERATION(
109 "PrintPreview.FontType",
110 font_resources[font]->getType(),
111 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
112 }
113 }
[email protected]67e16b392011-05-30 20:58:09114 }
115
[email protected]8f879292011-04-08 00:21:20116 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
117}
118
119uint32 PdfMetafileSkia::GetDataSize() const {
120 return data_->pdf_stream_.getOffset();
121}
122
123bool PdfMetafileSkia::GetData(void* dst_buffer,
124 uint32 dst_buffer_size) const {
125 if (dst_buffer_size < GetDataSize())
126 return false;
127
128 memcpy(dst_buffer, data_->pdf_stream_.getStream(), dst_buffer_size);
129 return true;
130}
131
132bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
133 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
134 if (file_util::WriteFile(file_path, data_->pdf_stream_.getStream(),
135 GetDataSize()) != static_cast<int>(GetDataSize())) {
136 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
137 return false;
138 }
139 return true;
140}
141
142gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
143 // TODO(vandebo) add a method to get the page size for a given page to
144 // SkPDFDocument.
145 NOTIMPLEMENTED();
146 return gfx::Rect();
147}
148
149unsigned int PdfMetafileSkia::GetPageCount() const {
150 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
151 NOTIMPLEMENTED();
152 return 0;
153}
154
155gfx::NativeDrawingContext PdfMetafileSkia::context() const {
156 NOTREACHED();
157 return NULL;
158}
159
160#if defined(OS_WIN)
161bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
162 const RECT* rect) const {
163 NOTREACHED();
164 return false;
165}
166
167bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
168 NOTREACHED();
169 return false;
170}
171
172HENHMETAFILE PdfMetafileSkia::emf() const {
173 NOTREACHED();
174 return NULL;
175}
[email protected]b8d85bc2011-06-22 13:34:57176#elif defined(OS_MACOSX)
177/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
178 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
179 the drawing of the PDF into the canvas may result in a rasterized output.
180 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
181 should do something like the following CL in PluginInstance::PrintPDFOutput:
182http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
183*/
184bool PdfMetafileSkia::RenderPage(unsigned int page_number,
185 CGContextRef context,
186 const CGRect rect,
187 bool shrink_to_fit,
188 bool stretch_to_fit,
189 bool center_horizontally,
190 bool center_vertically) const {
191 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
192 if (data_->pdf_cg_.GetDataSize() == 0)
193 data_->pdf_cg_.InitFromData(data_->pdf_stream_.getStream(),
194 data_->pdf_stream_.getOffset());
195 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
196 stretch_to_fit, center_horizontally,
197 center_vertically);
198}
199#endif
[email protected]8f879292011-04-08 00:21:20200
201#if defined(OS_CHROMEOS)
202bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
203 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
204
205 if (fd.fd < 0) {
206 DLOG(ERROR) << "Invalid file descriptor!";
207 return false;
208 }
209
210 bool result = true;
211 if (file_util::WriteFileDescriptor(fd.fd, data_->pdf_stream_.getStream(),
212 GetDataSize()) !=
213 static_cast<int>(GetDataSize())) {
214 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
215 result = false;
216 }
217
218 if (fd.auto_close) {
219 if (HANDLE_EINTR(close(fd.fd)) < 0) {
220 DPLOG(WARNING) << "close";
221 result = false;
222 }
223 }
224 return result;
225}
226#endif
227
[email protected]c797a192011-06-15 16:25:09228PdfMetafileSkia::PdfMetafileSkia()
229 : data_(new PdfMetafileSkiaData),
230 draft_(false) {}
[email protected]8f879292011-04-08 00:21:20231
[email protected]c797a192011-06-15 16:25:09232void PdfMetafileSkia::set_draft(bool draft) const {
233 draft_ = draft;
234}
[email protected]8f879292011-04-08 00:21:20235} // namespace printing