chromeos: dbus: add Bluetooth properties support
[chromium-blink-merge.git] / printing / pdf_metafile_skia.cc
blobe2409b941f738a86cad5ae0e7d1a158c5c046c42
1 // 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.
5 #include "printing/pdf_metafile_skia.h"
7 #include "base/eintr_wrapper.h"
8 #include "base/file_descriptor_posix.h"
9 #include "base/file_util.h"
10 #include "base/hash_tables.h"
11 #include "base/metrics/histogram.h"
12 #include "skia/ext/vector_platform_device_skia.h"
13 #include "third_party/skia/include/core/SkData.h"
14 #include "third_party/skia/include/core/SkRefCnt.h"
15 #include "third_party/skia/include/core/SkScalar.h"
16 #include "third_party/skia/include/core/SkStream.h"
17 #include "third_party/skia/include/core/SkTypeface.h"
18 #include "third_party/skia/include/pdf/SkPDFDevice.h"
19 #include "third_party/skia/include/pdf/SkPDFDocument.h"
20 #include "third_party/skia/include/pdf/SkPDFFont.h"
21 #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"
26 #if defined(OS_MACOSX)
27 #include "printing/pdf_metafile_cg_mac.h"
28 #endif
30 namespace printing {
32 struct PdfMetafileSkiaData {
33 SkRefPtr<SkPDFDevice> current_page_;
34 SkPDFDocument pdf_doc_;
35 SkDynamicMemoryWStream pdf_stream_;
36 #if defined(OS_MACOSX)
37 PdfMetafileCg pdf_cg_;
38 #endif
41 PdfMetafileSkia::~PdfMetafileSkia() {}
43 bool PdfMetafileSkia::Init() {
44 return true;
46 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
47 uint32 src_buffer_size) {
48 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
51 SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
52 const gfx::Size& page_size, const gfx::Rect& content_area,
53 const float& scale_factor) {
54 DCHECK(!page_outstanding_);
55 page_outstanding_ = true;
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));
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());
67 SkRefPtr<SkPDFDevice> pdf_device =
68 new skia::VectorPlatformDeviceSkia(pdf_page_size, pdf_content_size,
69 transform);
70 data_->current_page_ = pdf_device;
71 return pdf_device.get();
74 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
75 const gfx::Rect& content_area,
76 const float& scale_factor) {
77 NOTREACHED();
78 return NULL;
81 bool PdfMetafileSkia::FinishPage() {
82 DCHECK(data_->current_page_.get());
84 data_->pdf_doc_.appendPage(data_->current_page_.get());
85 page_outstanding_ = false;
86 return true;
89 bool 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;
94 if (page_outstanding_)
95 FinishPage();
97 data_->current_page_ = NULL;
98 base::hash_set<SkFontID> font_set;
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);
116 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
119 uint32 PdfMetafileSkia::GetDataSize() const {
120 return data_->pdf_stream_.getOffset();
123 bool PdfMetafileSkia::GetData(void* dst_buffer,
124 uint32 dst_buffer_size) const {
125 if (dst_buffer_size < GetDataSize())
126 return false;
128 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
129 memcpy(dst_buffer, data.bytes(), dst_buffer_size);
130 return true;
133 bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
134 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
135 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
136 if (file_util::WriteFile(file_path,
137 reinterpret_cast<const char*>(data.data()),
138 GetDataSize()) != static_cast<int>(GetDataSize())) {
139 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
140 return false;
142 return true;
145 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
146 // TODO(vandebo) add a method to get the page size for a given page to
147 // SkPDFDocument.
148 NOTIMPLEMENTED();
149 return gfx::Rect();
152 unsigned int PdfMetafileSkia::GetPageCount() const {
153 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
154 NOTIMPLEMENTED();
155 return 0;
158 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
159 NOTREACHED();
160 return NULL;
163 #if defined(OS_WIN)
164 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
165 const RECT* rect) const {
166 NOTREACHED();
167 return false;
170 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
171 NOTREACHED();
172 return false;
175 HENHMETAFILE PdfMetafileSkia::emf() const {
176 NOTREACHED();
177 return NULL;
179 #elif defined(OS_MACOSX)
180 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
181 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
182 the drawing of the PDF into the canvas may result in a rasterized output.
183 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
184 should do something like the following CL in PluginInstance::PrintPDFOutput:
185 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
187 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
188 CGContextRef context,
189 const CGRect rect,
190 bool shrink_to_fit,
191 bool stretch_to_fit,
192 bool center_horizontally,
193 bool center_vertically) const {
194 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
195 if (data_->pdf_cg_.GetDataSize() == 0) {
196 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
197 data_->pdf_cg_.InitFromData(data.bytes(), data.size());
199 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit,
200 stretch_to_fit, center_horizontally,
201 center_vertically);
203 #endif
205 #if defined(OS_CHROMEOS)
206 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
207 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
209 if (fd.fd < 0) {
210 DLOG(ERROR) << "Invalid file descriptor!";
211 return false;
214 bool result = true;
215 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
216 if (file_util::WriteFileDescriptor(fd.fd,
217 reinterpret_cast<const char*>(data.data()),
218 GetDataSize()) !=
219 static_cast<int>(GetDataSize())) {
220 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
221 result = false;
224 if (fd.auto_close) {
225 if (HANDLE_EINTR(close(fd.fd)) < 0) {
226 DPLOG(WARNING) << "close";
227 result = false;
230 return result;
232 #endif
234 PdfMetafileSkia::PdfMetafileSkia()
235 : data_(new PdfMetafileSkiaData),
236 page_outstanding_(false) {
239 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
240 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
241 SkDynamicMemoryWStream pdf_stream;
242 if (!pdf_doc.appendPage(data_->current_page_.get()))
243 return NULL;
245 if (!pdf_doc.emitPDF(&pdf_stream))
246 return NULL;
248 SkAutoDataUnref data(pdf_stream.copyToData());
249 if (data.size() == 0)
250 return NULL;
252 PdfMetafileSkia* metafile = new PdfMetafileSkia;
253 metafile->InitFromData(data.bytes(), data.size());
254 return metafile;
257 } // namespace printing