blob: bcb5af931c32eb160ee7f2b67563768c7d91bb4f [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
25namespace printing {
26
27struct PdfMetafileSkiaData {
28 SkRefPtr<SkPDFDevice> current_page_;
29 SkPDFDocument pdf_doc_;
30 SkDynamicMemoryWStream pdf_stream_;
31};
32
33PdfMetafileSkia::~PdfMetafileSkia() {}
34
35bool PdfMetafileSkia::Init() {
36 return true;
37}
38bool PdfMetafileSkia::InitFromData(const void* src_buffer,
39 uint32 src_buffer_size) {
40 return data_->pdf_stream_.write(src_buffer, src_buffer_size);
41}
42
[email protected]62f2e802011-05-26 14:28:3543SkDevice* PdfMetafileSkia::StartPageForVectorCanvas(
[email protected]39892b92011-04-30 02:24:4444 const gfx::Size& page_size, const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2045 const float& scale_factor) {
46 DCHECK(data_->current_page_.get() == NULL);
47
[email protected]a34f0122011-04-12 17:36:3948 // Adjust for the margins and apply the scale factor.
49 SkMatrix transform;
[email protected]39892b92011-04-30 02:24:4450 transform.setTranslate(SkIntToScalar(content_area.x()),
51 SkIntToScalar(content_area.y()));
[email protected]a34f0122011-04-12 17:36:3952 transform.preScale(SkFloatToScalar(scale_factor),
53 SkFloatToScalar(scale_factor));
54
[email protected]39892b92011-04-30 02:24:4455 // TODO(ctguil): Refactor: don't create the PDF device explicitly here.
56 SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height());
57 SkISize pdf_content_size =
58 SkISize::Make(content_area.width(), content_area.height());
59 SkRefPtr<SkPDFDevice> pdf_device =
60 new SkPDFDevice(pdf_page_size, pdf_content_size, transform);
61 pdf_device->unref(); // SkRefPtr and new both took a reference.
[email protected]8f879292011-04-08 00:21:2062 skia::VectorPlatformDeviceSkia* device =
[email protected]39892b92011-04-30 02:24:4463 new skia::VectorPlatformDeviceSkia(pdf_device.get());
[email protected]8f879292011-04-08 00:21:2064 data_->current_page_ = device->PdfDevice();
65 return device;
66}
67
68bool PdfMetafileSkia::StartPage(const gfx::Size& page_size,
[email protected]39892b92011-04-30 02:24:4469 const gfx::Rect& content_area,
[email protected]8f879292011-04-08 00:21:2070 const float& scale_factor) {
71 NOTREACHED();
72 return NULL;
73}
74
75bool PdfMetafileSkia::FinishPage() {
76 DCHECK(data_->current_page_.get());
77
78 data_->pdf_doc_.appendPage(data_->current_page_);
79 data_->current_page_ = NULL;
80 return true;
81}
82
83bool PdfMetafileSkia::FinishDocument() {
84 // Don't do anything if we've already set the data in InitFromData.
85 if (data_->pdf_stream_.getOffset())
86 return true;
87
88 if (data_->current_page_.get())
89 FinishPage();
[email protected]67e16b392011-05-30 20:58:0990
[email protected]54ffd6f62011-06-02 17:29:0591 base::hash_set<SkFontID> font_set;
92
93 const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages();
94 for (int page_number = 0; page_number < pages.count(); page_number++) {
95 const SkTDArray<SkPDFFont*>& font_resources =
96 pages[page_number]->getFontResources();
97 for (int font = 0; font < font_resources.count(); font++) {
98 SkFontID font_id = font_resources[font]->typeface()->uniqueID();
99 if (font_set.find(font_id) == font_set.end()) {
100 font_set.insert(font_id);
101 UMA_HISTOGRAM_ENUMERATION(
102 "PrintPreview.FontType",
103 font_resources[font]->getType(),
104 SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1);
105 }
106 }
[email protected]67e16b392011-05-30 20:58:09107 }
108
[email protected]8f879292011-04-08 00:21:20109 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_);
110}
111
112uint32 PdfMetafileSkia::GetDataSize() const {
113 return data_->pdf_stream_.getOffset();
114}
115
116bool PdfMetafileSkia::GetData(void* dst_buffer,
117 uint32 dst_buffer_size) const {
118 if (dst_buffer_size < GetDataSize())
119 return false;
120
121 memcpy(dst_buffer, data_->pdf_stream_.getStream(), dst_buffer_size);
122 return true;
123}
124
125bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const {
126 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
127 if (file_util::WriteFile(file_path, data_->pdf_stream_.getStream(),
128 GetDataSize()) != static_cast<int>(GetDataSize())) {
129 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str();
130 return false;
131 }
132 return true;
133}
134
135gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const {
136 // TODO(vandebo) add a method to get the page size for a given page to
137 // SkPDFDocument.
138 NOTIMPLEMENTED();
139 return gfx::Rect();
140}
141
142unsigned int PdfMetafileSkia::GetPageCount() const {
143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument.
144 NOTIMPLEMENTED();
145 return 0;
146}
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
165HENHMETAFILE PdfMetafileSkia::emf() const {
166 NOTREACHED();
167 return NULL;
168}
169#endif // if defined(OS_WIN)
170
171#if defined(OS_CHROMEOS)
172bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const {
173 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U);
174
175 if (fd.fd < 0) {
176 DLOG(ERROR) << "Invalid file descriptor!";
177 return false;
178 }
179
180 bool result = true;
181 if (file_util::WriteFileDescriptor(fd.fd, data_->pdf_stream_.getStream(),
182 GetDataSize()) !=
183 static_cast<int>(GetDataSize())) {
184 DLOG(ERROR) << "Failed to save file with fd " << fd.fd;
185 result = false;
186 }
187
188 if (fd.auto_close) {
189 if (HANDLE_EINTR(close(fd.fd)) < 0) {
190 DPLOG(WARNING) << "close";
191 result = false;
192 }
193 }
194 return result;
195}
196#endif
197
[email protected]c797a192011-06-15 16:25:09198PdfMetafileSkia::PdfMetafileSkia()
199 : data_(new PdfMetafileSkiaData),
200 draft_(false) {}
[email protected]8f879292011-04-08 00:21:20201
[email protected]c797a192011-06-15 16:25:09202void PdfMetafileSkia::set_draft(bool draft) const {
203 draft_ = draft;
204}
[email protected]8f879292011-04-08 00:21:20205} // namespace printing