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/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 "ui/gfx/point.h"
21 #include "ui/gfx/rect.h"
22 #include "ui/gfx/size.h"
24 #if defined(OS_MACOSX)
25 #include "printing/pdf_metafile_cg_mac.h"
30 struct PdfMetafileSkiaData
{
31 SkRefPtr
<SkPDFDevice
> current_page_
;
32 SkPDFDocument pdf_doc_
;
33 SkDynamicMemoryWStream pdf_stream_
;
34 #if defined(OS_MACOSX)
35 PdfMetafileCg pdf_cg_
;
39 PdfMetafileSkia::~PdfMetafileSkia() {}
41 bool PdfMetafileSkia::Init() {
44 bool PdfMetafileSkia::InitFromData(const void* src_buffer
,
45 uint32 src_buffer_size
) {
46 return data_
->pdf_stream_
.write(src_buffer
, src_buffer_size
);
49 SkDevice
* PdfMetafileSkia::StartPageForVectorCanvas(
50 const gfx::Size
& page_size
, const gfx::Rect
& content_area
,
51 const float& scale_factor
) {
52 DCHECK(!page_outstanding_
);
53 page_outstanding_
= true;
55 // Adjust for the margins and apply the scale factor.
57 transform
.setTranslate(SkIntToScalar(content_area
.x()),
58 SkIntToScalar(content_area
.y()));
59 transform
.preScale(SkFloatToScalar(scale_factor
),
60 SkFloatToScalar(scale_factor
));
62 SkISize pdf_page_size
= SkISize::Make(page_size
.width(), page_size
.height());
63 SkISize pdf_content_size
=
64 SkISize::Make(content_area
.width(), content_area
.height());
65 SkRefPtr
<SkPDFDevice
> pdf_device
=
66 new skia::VectorPlatformDeviceSkia(pdf_page_size
, pdf_content_size
,
68 data_
->current_page_
= pdf_device
;
69 return pdf_device
.get();
72 bool PdfMetafileSkia::StartPage(const gfx::Size
& page_size
,
73 const gfx::Rect
& content_area
,
74 const float& scale_factor
) {
79 bool PdfMetafileSkia::FinishPage() {
80 DCHECK(data_
->current_page_
.get());
82 data_
->pdf_doc_
.appendPage(data_
->current_page_
.get());
83 page_outstanding_
= false;
87 bool PdfMetafileSkia::FinishDocument() {
88 // Don't do anything if we've already set the data in InitFromData.
89 if (data_
->pdf_stream_
.getOffset())
92 if (page_outstanding_
)
95 data_
->current_page_
= NULL
;
97 int font_counts
[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font
+ 1];
98 data_
->pdf_doc_
.getCountOfFontTypes(font_counts
);
100 type
<= SkAdvancedTypefaceMetrics::kNotEmbeddable_Font
;
102 for (int count
= 0; count
< font_counts
[type
]; count
++) {
103 UMA_HISTOGRAM_ENUMERATION(
104 "PrintPreview.FontType", type
,
105 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font
+ 1);
109 return data_
->pdf_doc_
.emitPDF(&data_
->pdf_stream_
);
112 uint32
PdfMetafileSkia::GetDataSize() const {
113 return data_
->pdf_stream_
.getOffset();
116 bool PdfMetafileSkia::GetData(void* dst_buffer
,
117 uint32 dst_buffer_size
) const {
118 if (dst_buffer_size
< GetDataSize())
121 SkAutoDataUnref
data(data_
->pdf_stream_
.copyToData());
122 memcpy(dst_buffer
, data
.bytes(), dst_buffer_size
);
126 bool PdfMetafileSkia::SaveTo(const FilePath
& file_path
) const {
127 DCHECK_GT(data_
->pdf_stream_
.getOffset(), 0U);
128 SkAutoDataUnref
data(data_
->pdf_stream_
.copyToData());
129 if (file_util::WriteFile(file_path
,
130 reinterpret_cast<const char*>(data
.data()),
131 GetDataSize()) != static_cast<int>(GetDataSize())) {
132 DLOG(ERROR
) << "Failed to save file " << file_path
.value().c_str();
138 gfx::Rect
PdfMetafileSkia::GetPageBounds(unsigned int page_number
) const {
139 // TODO(vandebo) add a method to get the page size for a given page to
145 unsigned int PdfMetafileSkia::GetPageCount() const {
146 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
151 gfx::NativeDrawingContext
PdfMetafileSkia::context() const {
157 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc
,
158 const RECT
* rect
) const {
163 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc
) const {
168 HENHMETAFILE
PdfMetafileSkia::emf() const {
172 #elif defined(OS_MACOSX)
173 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
174 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
175 the drawing of the PDF into the canvas may result in a rasterized output.
176 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
177 should do something like the following CL in PluginInstance::PrintPDFOutput:
178 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
180 bool PdfMetafileSkia::RenderPage(unsigned int page_number
,
181 CGContextRef context
,
185 bool center_horizontally
,
186 bool center_vertically
) const {
187 DCHECK_GT(data_
->pdf_stream_
.getOffset(), 0U);
188 if (data_
->pdf_cg_
.GetDataSize() == 0) {
189 SkAutoDataUnref
data(data_
->pdf_stream_
.copyToData());
190 data_
->pdf_cg_
.InitFromData(data
.bytes(), data
.size());
192 return data_
->pdf_cg_
.RenderPage(page_number
, context
, rect
, shrink_to_fit
,
193 stretch_to_fit
, center_horizontally
,
198 #if defined(OS_CHROMEOS)
199 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor
& fd
) const {
200 DCHECK_GT(data_
->pdf_stream_
.getOffset(), 0U);
203 DLOG(ERROR
) << "Invalid file descriptor!";
208 SkAutoDataUnref
data(data_
->pdf_stream_
.copyToData());
209 if (file_util::WriteFileDescriptor(fd
.fd
,
210 reinterpret_cast<const char*>(data
.data()),
212 static_cast<int>(GetDataSize())) {
213 DLOG(ERROR
) << "Failed to save file with fd " << fd
.fd
;
218 if (HANDLE_EINTR(close(fd
.fd
)) < 0) {
219 DPLOG(WARNING
) << "close";
227 PdfMetafileSkia::PdfMetafileSkia()
228 : data_(new PdfMetafileSkiaData
),
229 page_outstanding_(false) {
232 PdfMetafileSkia
* PdfMetafileSkia::GetMetafileForCurrentPage() {
233 SkPDFDocument
pdf_doc(SkPDFDocument::kDraftMode_Flags
);
234 SkDynamicMemoryWStream pdf_stream
;
235 if (!pdf_doc
.appendPage(data_
->current_page_
.get()))
238 if (!pdf_doc
.emitPDF(&pdf_stream
))
241 SkAutoDataUnref
data(pdf_stream
.copyToData());
242 if (data
.size() == 0)
245 PdfMetafileSkia
* metafile
= new PdfMetafileSkia
;
246 metafile
->InitFromData(data
.bytes(), data
.size());
250 } // namespace printing