blob: 486b3abb62438f11546eaa82a59abd3c2a041a51 [file] [log] [blame]
[email protected]0a4392a2012-03-23 17:50:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8f879292011-04-08 00:21:202// 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
[email protected]14c1c232013-06-11 17:52:447#include "base/containers/hash_tables.h"
thestig22dfc4012014-09-05 08:29:448#include "base/files/file_util.h"
[email protected]67e16b392011-05-30 20:58:099#include "base/metrics/histogram.h"
[email protected]cb154062014-01-17 03:32:4010#include "base/numerics/safe_conversions.h"
[email protected]2025d002012-11-14 20:54:3511#include "base/posix/eintr_wrapper.h"
[email protected]584abe72013-05-18 09:40:2612#include "skia/ext/refptr.h"
halcanary5be808e2014-11-10 22:20:0513#include "skia/ext/vector_canvas.h"
[email protected]e195fbf52011-06-27 16:51:2014#include "third_party/skia/include/core/SkData.h"
[email protected]8f879292011-04-08 00:21:2015#include "third_party/skia/include/core/SkRefCnt.h"
[email protected]a34f0122011-04-12 17:36:3916#include "third_party/skia/include/core/SkScalar.h"
[email protected]8f879292011-04-08 00:21:2017#include "third_party/skia/include/core/SkStream.h"
vitalybukad054bfdd2014-11-18 01:25:5918#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"
tfarina655f81d2014-12-23 02:38:5021#include "ui/gfx/geometry/point.h"
[email protected]8f879292011-04-08 00:21:2022#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]0daaebfe2014-03-15 00:09:0529#if defined(OS_POSIX)
30#include "base/file_descriptor_posix.h"
31#endif
32
[email protected]8f879292011-04-08 00:21:2033namespace printing {
34
35struct PdfMetafileSkiaData {
vitalybukad054bfdd2014-11-18 01:25:5936 skia::RefPtr<SkPDFDevice> current_page_;
37 skia::RefPtr<SkCanvas> current_page_canvas_;
38 SkPDFDocument pdf_doc_;
39 SkDynamicMemoryWStream pdf_stream_;
[email protected]b8d85bc2011-06-22 13:34:5740#if defined(OS_MACOSX)
41 PdfMetafileCg pdf_cg_;
42#endif
[email protected]8f879292011-04-08 00:21:2043};
44
45PdfMetafileSkia::~PdfMetafileSkia() {}
46
47bool PdfMetafileSkia::Init() {
48 return true;
49}
50bool PdfMetafileSkia::InitFromData(const void* src_buffer,
51 uint32 src_buffer_size) {
vitalybukad054bfdd2014-11-18 01:25:5952 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
[email protected]8f879292011-04-08 00:21:2053}
54
halcanary5be808e2014-11-10 22:20:0555bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
56 const gfx::Rect& content_area,
57 const float& scale_factor) {
vitalybukad054bfdd2014-11-18 01:25:5958 DCHECK(!data_->current_page_canvas_);
[email protected]8f879292011-04-08 00:21:2059
vitalybukad054bfdd2014-11-18 01:25:5960 // Adjust for the margins and apply the scale factor.
61 SkMatrix transform;
62 transform.setTranslate(SkIntToScalar(content_area.x()),
63 SkIntToScalar(content_area.y()));
64 transform.preScale(SkFloatToScalar(scale_factor),
65 SkFloatToScalar(scale_factor));
66
67 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
68 SkISize pdf_content_size =
69 SkISize::Make(content_area.width(), content_area.height());
70
71 data_->current_page_ = skia::AdoptRef(
72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform));
73 data_->current_page_canvas_ =
74 skia::AdoptRef(new SkCanvas(data_->current_page_.get()));
halcanary5be808e2014-11-10 22:20:0575 return true;
[email protected]8f879292011-04-08 00:21:2076}
77
halcanary5be808e2014-11-10 22:20:0578skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage(
79 const gfx::Size& page_size,
80 const gfx::Rect& content_area,
81 const float& scale_factor) {
82 if (!StartPage(page_size, content_area, scale_factor))
83 return nullptr;
vitalybukad054bfdd2014-11-18 01:25:5984 return data_->current_page_canvas_.get();
[email protected]8f879292011-04-08 00:21:2085}
86
87bool PdfMetafileSkia::FinishPage() {
vitalybukad054bfdd2014-11-18 01:25:5988 DCHECK(data_->current_page_canvas_);
89 DCHECK(data_->current_page_);
90
91 data_->current_page_canvas_.clear(); // Unref SkCanvas.
92 data_->pdf_doc_.appendPage(data_->current_page_.get());
[email protected]8f879292011-04-08 00:21:2093 return true;
94}
95
96bool PdfMetafileSkia::FinishDocument() {
vitalybukad054bfdd2014-11-18 01:25:5997 // Don't do anything if we've already set the data in InitFromData.
98 if (data_->pdf_stream_.getOffset())
99 return true;
[email protected]8f879292011-04-08 00:21:20100
vitalybukad054bfdd2014-11-18 01:25:59101 if (data_->current_page_canvas_)
102 FinishPage();
[email protected]67e16b392011-05-30 20:58:09103
vitalybukad054bfdd2014-11-18 01:25:59104 data_->current_page_.clear();
105
106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2];
107 data_->pdf_doc_.getCountOfFontTypes(font_counts);
108 for (int type = 0;
109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1;
110 type++) {
111 for (int count = 0; count < font_counts[type]; count++) {
112 UMA_HISTOGRAM_ENUMERATION(
113 "PrintPreview.FontType", type,
114 SkAdvancedTypefaceMetrics::kOther_Font + 2);
115 }
[email protected]67e16b392011-05-30 20:58:09116 }
117
vitalybukad054bfdd2014-11-18 01:25:59118 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
[email protected]8f879292011-04-08 00:21:20119}
120
121uint32 PdfMetafileSkia::GetDataSize() const {
vitalybukad054bfdd2014-11-18 01:25:59122 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset());
[email protected]8f879292011-04-08 00:21:20123}
124
125bool PdfMetafileSkia::GetData(void* dst_buffer,
126 uint32 dst_buffer_size) const {
vitalybukad054bfdd2014-11-18 01:25:59127 if (dst_buffer_size < GetDataSize())
[email protected]8f879292011-04-08 00:21:20128 return false;
vitalybukad054bfdd2014-11-18 01:25:59129
130 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
131 memcpy(dst_buffer, data->bytes(), dst_buffer_size);
132 return true;
[email protected]8f879292011-04-08 00:21:20133}
134
[email protected]8f879292011-04-08 00:21:20135gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
vitalybukad054bfdd2014-11-18 01:25:59136 // TODO(vandebo) add a method to get the page size for a given page to
137 // SkPDFDocument.
138 NOTIMPLEMENTED();
[email protected]8f879292011-04-08 00:21:20139 return gfx::Rect();
140}
141
142unsigned int PdfMetafileSkia::GetPageCount() const {
vitalybukad054bfdd2014-11-18 01:25:59143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
144 NOTIMPLEMENTED();
145 return 0;
[email protected]8f879292011-04-08 00:21:20146}
147
148gfx::NativeDrawingContext PdfMetafileSkia::context() const {
149 NOTREACHED();
150 return NULL;
151}
152
153#if defined(OS_WIN)
154bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc,
155 const RECT* rect) const {
156 NOTREACHED();
157 return false;
158}
159
160bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const {
161 NOTREACHED();
162 return false;
163}
164
[email protected]b8d85bc2011-06-22 13:34:57165#elif defined(OS_MACOSX)
166/* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in
167 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage,
168 the drawing of the PDF into the canvas may result in a rasterized output.
169 PDFMetafileSkia::RenderPage should be not implemented as shown and instead
170 should do something like the following CL in PluginInstance::PrintPDFOutput:
171http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc
172*/
173bool PdfMetafileSkia::RenderPage(unsigned int page_number,
174 CGContextRef context,
175 const CGRect rect,
[email protected]b5cf844c2012-06-18 21:49:20176 const MacRenderPageParams& params) const {
vitalybukad054bfdd2014-11-18 01:25:59177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]e195fbf52011-06-27 16:51:20178 if (data_->pdf_cg_.GetDataSize() == 0) {
vitalybukad054bfdd2014-11-18 01:25:59179 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
180 data_->pdf_cg_.InitFromData(data->bytes(), data->size());
[email protected]e195fbf52011-06-27 16:51:20181 }
[email protected]b5cf844c2012-06-18 21:49:20182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params);
[email protected]b8d85bc2011-06-22 13:34:57183}
184#endif
[email protected]8f879292011-04-08 00:21:20185
halcanary5be808e2014-11-10 22:20:05186bool PdfMetafileSkia::SaveTo(base::File* file) const {
187 if (GetDataSize() == 0U)
188 return false;
vitalybukad054bfdd2014-11-18 01:25:59189 SkAutoDataUnref data(data_->pdf_stream_.copyToData());
190 // TODO(halcanary): rewrite this function without extra data copy
191 // using SkStreamAsset.
192 const char* ptr = reinterpret_cast<const char*>(data->data());
193 int size = base::checked_cast<int>(data->size());
194 return file->WriteAtCurrentPos(ptr, size) == size;
halcanary5be808e2014-11-10 22:20:05195}
196
[email protected]b25f0032013-08-19 22:26:25197#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]8f879292011-04-08 00:21:20198bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
vitalybukad054bfdd2014-11-18 01:25:59199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
[email protected]8f879292011-04-08 00:21:20200
201 if (fd.fd < 0) {
202 DLOG(ERROR) << "Invalid file descriptor!";
203 return false;
204 }
vitalybuka5d1290582014-09-12 09:19:59205 base::File file(fd.fd);
halcanary5be808e2014-11-10 22:20:05206 bool result = SaveTo(&file);
vitalybuka5d1290582014-09-12 09:19:59207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd;
[email protected]8f879292011-04-08 00:21:20208
vitalybuka5d1290582014-09-12 09:19:59209 if (!fd.auto_close)
210 file.TakePlatformFile();
[email protected]8f879292011-04-08 00:21:20211 return result;
212}
213#endif
214
halcanary5be808e2014-11-10 22:20:05215PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) {
[email protected]19b9d3b2011-07-23 02:08:57216}
[email protected]597516372011-07-01 05:10:44217
vitalybuka5d1290582014-09-12 09:19:59218scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() {
vitalybukad054bfdd2014-11-18 01:25:59219 scoped_ptr<PdfMetafileSkia> metafile;
220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags);
221 if (!pdf_doc.appendPage(data_->current_page_.get()))
vitalybuka5d1290582014-09-12 09:19:59222 return metafile.Pass();
[email protected]597516372011-07-01 05:10:44223
vitalybukad054bfdd2014-11-18 01:25:59224 SkDynamicMemoryWStream pdf_stream;
225 if (!pdf_doc.emitPDF(&pdf_stream))
vitalybuka5d1290582014-09-12 09:19:59226 return metafile.Pass();
[email protected]597516372011-07-01 05:10:44227
vitalybukad054bfdd2014-11-18 01:25:59228 SkAutoDataUnref data_copy(pdf_stream.copyToData());
229 if (data_copy->size() == 0)
230 return scoped_ptr<PdfMetafileSkia>();
[email protected]597516372011-07-01 05:10:44231
vitalybukad054bfdd2014-11-18 01:25:59232 metafile.reset(new PdfMetafileSkia);
233 if (!metafile->InitFromData(data_copy->bytes(),
234 base::checked_cast<uint32>(data_copy->size()))) {
vitalybuka5d1290582014-09-12 09:19:59235 metafile.reset();
vitalybukad054bfdd2014-11-18 01:25:59236 }
vitalybuka5d1290582014-09-12 09:19:59237 return metafile.Pass();
[email protected]597516372011-07-01 05:10:44238}
[email protected]8f879292011-04-08 00:21:20239
[email protected]8f879292011-04-08 00:21:20240} // namespace printing