Remove expectations added in webkit http://trac.webkit.org/changeset/146743
[chromium-blink-merge.git] / printing / pdf_metafile_skia.cc
blobe4027ed35d48167ea057f15118b2467f92d20605
1 // Copyright (c) 2012 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/file_descriptor_posix.h"
8 #include "base/file_util.h"
9 #include "base/hash_tables.h"
10 #include "base/metrics/histogram.h"
11 #include "base/posix/eintr_wrapper.h"
12 #include "base/safe_numerics.h"
13 #include "skia/ext/vector_platform_device_skia.h"
14 #include "third_party/skia/include/core/SkData.h"
15 #include "third_party/skia/include/core/SkRefCnt.h"
16 #include "third_party/skia/include/core/SkScalar.h"
17 #include "third_party/skia/include/core/SkStream.h"
18 #include "third_party/skia/include/core/SkTypeface.h"
19 #include "third_party/skia/include/pdf/SkPDFDevice.h"
20 #include "third_party/skia/include/pdf/SkPDFDocument.h"
21 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h"
25 #if defined(OS_MACOSX)
26 #include "printing/pdf_metafile_cg_mac.h"
27 #endif
29 namespace printing {
31 struct PdfMetafileSkiaData {
32 SkRefPtr<SkPDFDevice> current_page_;
33 SkPDFDocument pdf_doc_;
34 SkDynamicMemoryWStream pdf_stream_;
35 #if defined(OS_MACOSX)
36 PdfMetafileCg pdf_cg_;
37 #endif
40 PdfMetafileSkia::~PdfMetafileSkia() {}
42 bool PdfMetafileSkia::Init() {
43 return true;
45 bool PdfMetafileSkia::InitFromData(const void* src_buffer,
46 uint32 src_buffer_size) {
47 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
50 SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
51 const gfx::Size& page_size, const gfx::Rect& content_area,
52 const float& scale_factor) {
53 DCHECK(!page_outstanding_);
54 page_outstanding_ = true;
56 // Adjust for the margins and apply the scale factor.
57 SkMatrix transform;
58 transform.setTranslate(SkIntToScalar(content_area.x()),
59 SkIntToScalar(content_area.y()));
60 transform.preScale(SkFloatToScalar(scale_factor),
61 SkFloatToScalar(scale_factor));
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 skia::VectorPlatformDeviceSkia(pdf_page_size, pdf_content_size,
68 transform);
69 data_->current_page_ = pdf_device;
70 return pdf_device.get();
73 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
74 const gfx::Rect& content_area,
75 const float& scale_factor) {
76 NOTREACHED();
77 return false;
80 bool PdfMetafileSkia::FinishPage() {
81 DCHECK(data_->current_page_.get());
83 data_->pdf_doc_.appendPage(data_->current_page_.get());
84 page_outstanding_ = false;
85 return true;
88 bool PdfMetafileSkia::FinishDocument() {
89 // Don't do anything if we've already set the data in InitFromData.
90 if (data_->pdf_stream_.getOffset())
91 return true;
93 if (page_outstanding_)
94 FinishPage();
96 data_->current_page_ = NULL;
98 int font_counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1];
99 data_->pdf_doc_.getCountOfFontTypes(font_counts);
100 for (int type = 0;
101 type <= SkAdvancedTypefaceMetrics::kNotEmbeddable_Font;
102 type++) {
103 for (int count = 0; count < font_counts[type]; count++) {
104 UMA_HISTOGRAM_ENUMERATION(
105 "PrintPreview.FontType", type,
106 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
110 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
113 uint32 PdfMetafileSkia::GetDataSize() const {
114 return base::checked_numeric_cast<uint32>(data_->pdf_stream_.getOffset());
117 bool PdfMetafileSkia::GetData(void* dst_buffer,
118 uint32 dst_buffer_size) const {
119 if (dst_buffer_size < GetDataSize())
120 return false;
122 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
123 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
124 return true;
127 bool PdfMetafileSkia::SaveTo(const base::FilePath& file_path) const {
128 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
129 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
130 if (file_util::WriteFile(file_path,
131 reinterpret_cast<const char*>(data->data()),
132 GetDataSize()) != static_cast<int>(GetDataSize())) {
133 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
134 return false;
136 return true;
139 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
140 // TODO(vandebo) add a method to get the page size for a given page to
141 // SkPDFDocument.
142 NOTIMPLEMENTED();
143 return gfx::Rect();
146 unsigned int PdfMetafileSkia::GetPageCount() const {
147 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
148 NOTIMPLEMENTED();
149 return 0;
152 gfx::NativeDrawingContext PdfMetafileSkia::context() const {
153 NOTREACHED();
154 return NULL;
157 #if defined(OS_WIN)
158 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
159 const RECT* rect) const {
160 NOTREACHED();
161 return false;
164 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
165 NOTREACHED();
166 return false;
169 HENHMETAFILE PdfMetafileSkia::emf() const {
170 NOTREACHED();
171 return NULL;
173 #elif defined(OS_MACOSX)
174 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
175 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
176 the drawing of the PDF into the canvas may result in a rasterized output.
177 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
178 should do something like the following CL in PluginInstance::PrintPDFOutput:
179 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
181 bool PdfMetafileSkia::RenderPage(unsigned int page_number,
182 CGContextRef context,
183 const CGRect rect,
184 const MacRenderPageParams& params) const {
185 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
186 if (data_->pdf_cg_.GetDataSize() == 0) {
187 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
188 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
190 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
192 #endif
194 #if defined(OS_CHROMEOS)
195 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
196 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
198 if (fd.fd < 0) {
199 DLOG(ERROR) << "Invalid file descriptor!";
200 return false;
203 bool result = true;
204 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
205 if (file_util::WriteFileDescriptor(fd.fd,
206 reinterpret_cast<const char*>(data->data()),
207 GetDataSize()) !=
208 static_cast<int>(GetDataSize())) {
209 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
210 result = false;
213 if (fd.auto_close) {
214 if (HANDLE_EINTR(close(fd.fd)) < 0) {
215 DPLOG(WARNING) << "close";
216 result = false;
219 return result;
221 #endif
223 PdfMetafileSkia::PdfMetafileSkia()
224 : data_(new PdfMetafileSkiaData),
225 page_outstanding_(false) {
228 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() {
229 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
230 SkDynamicMemoryWStream pdf_stream;
231 if (!pdf_doc.appendPage(data_->current_page_.get()))
232 return NULL;
234 if (!pdf_doc.emitPDF(&pdf_stream))
235 return NULL;
237 SkAutoDataUnref data(pdf_stream.copyToData());
238 if (data->size() == 0)
239 return NULL;
241 PdfMetafileSkia* metafile = new PdfMetafileSkia;
242 metafile->InitFromData(data->bytes(),
243 base::checked_numeric_cast<uint32>(data->size()));
244 return metafile;
247 } // namespace printing