[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 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. |
| 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] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 10 | #include "base/hash_tables.h" |
| 11 | #include "base/metrics/histogram.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 12 | #include "skia/ext/vector_platform_device_skia.h" |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 13 | #include "third_party/skia/include/core/SkData.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkRefCnt.h" |
[email protected] | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkScalar.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 16 | #include "third_party/skia/include/core/SkStream.h" |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkTypeface.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 18 | #include "third_party/skia/include/pdf/SkPDFDevice.h" |
| 19 | #include "third_party/skia/include/pdf/SkPDFDocument.h" |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 20 | #include "third_party/skia/include/pdf/SkPDFFont.h" |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 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" |
| 25 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 26 | #if defined(OS_MACOSX) |
| 27 | #include "printing/pdf_metafile_cg_mac.h" |
| 28 | #endif |
| 29 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 30 | namespace printing { |
| 31 | |
| 32 | struct PdfMetafileSkiaData { |
| 33 | SkRefPtr<SkPDFDevice> current_page_; |
| 34 | SkPDFDocument pdf_doc_; |
| 35 | SkDynamicMemoryWStream pdf_stream_; |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 36 | #if defined(OS_MACOSX) |
| 37 | PdfMetafileCg pdf_cg_; |
| 38 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | PdfMetafileSkia::~PdfMetafileSkia() {} |
| 42 | |
| 43 | bool PdfMetafileSkia::Init() { |
| 44 | return true; |
| 45 | } |
| 46 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 47 | uint32 src_buffer_size) { |
| 48 | return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
| 49 | } |
| 50 | |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 51 | SkDevice* PdfMetafileSkia::StartPageForVectorCanvas( |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 52 | const gfx::Size& page_size, const gfx::Rect& content_area, |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 53 | const float& scale_factor) { |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame^] | 54 | DCHECK (!page_outstanding_); |
| 55 | page_outstanding_ = true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 56 | |
[email protected] | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 57 | // Adjust for the margins and apply the scale factor. |
| 58 | SkMatrix transform; |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 59 | transform.setTranslate(SkIntToScalar(content_area.x()), |
| 60 | SkIntToScalar(content_area.y())); |
[email protected] | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 61 | transform.preScale(SkFloatToScalar(scale_factor), |
| 62 | SkFloatToScalar(scale_factor)); |
| 63 | |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 64 | // TODO(ctguil): Refactor: don't create the PDF device explicitly here. |
| 65 | SkISize pdf_page_size = SkISize::Make(page_size.width(), page_size.height()); |
| 66 | SkISize pdf_content_size = |
| 67 | SkISize::Make(content_area.width(), content_area.height()); |
| 68 | SkRefPtr<SkPDFDevice> pdf_device = |
| 69 | new SkPDFDevice(pdf_page_size, pdf_content_size, transform); |
| 70 | pdf_device->unref(); // SkRefPtr and new both took a reference. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 71 | skia::VectorPlatformDeviceSkia* device = |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 72 | new skia::VectorPlatformDeviceSkia(pdf_device.get()); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 73 | data_->current_page_ = device->PdfDevice(); |
| 74 | return device; |
| 75 | } |
| 76 | |
| 77 | bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 78 | const gfx::Rect& content_area, |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 79 | const float& scale_factor) { |
| 80 | NOTREACHED(); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | bool PdfMetafileSkia::FinishPage() { |
| 85 | DCHECK(data_->current_page_.get()); |
| 86 | |
| 87 | data_->pdf_doc_.appendPage(data_->current_page_); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame^] | 88 | page_outstanding_ = false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool PdfMetafileSkia::FinishDocument() { |
| 93 | // Don't do anything if we've already set the data in InitFromData. |
| 94 | if (data_->pdf_stream_.getOffset()) |
| 95 | return true; |
| 96 | |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame^] | 97 | if (page_outstanding_) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 98 | FinishPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 99 | |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame^] | 100 | data_->current_page_ = NULL; |
[email protected] | 54ffd6f6 | 2011-06-02 17:29:05 | [diff] [blame] | 101 | base::hash_set<SkFontID> font_set; |
| 102 | |
| 103 | const SkTDArray<SkPDFPage*>& pages = data_->pdf_doc_.getPages(); |
| 104 | for (int page_number = 0; page_number < pages.count(); page_number++) { |
| 105 | const SkTDArray<SkPDFFont*>& font_resources = |
| 106 | pages[page_number]->getFontResources(); |
| 107 | for (int font = 0; font < font_resources.count(); font++) { |
| 108 | SkFontID font_id = font_resources[font]->typeface()->uniqueID(); |
| 109 | if (font_set.find(font_id) == font_set.end()) { |
| 110 | font_set.insert(font_id); |
| 111 | UMA_HISTOGRAM_ENUMERATION( |
| 112 | "PrintPreview.FontType", |
| 113 | font_resources[font]->getType(), |
| 114 | SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1); |
| 115 | } |
| 116 | } |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 119 | return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); |
| 120 | } |
| 121 | |
| 122 | uint32 PdfMetafileSkia::GetDataSize() const { |
| 123 | return data_->pdf_stream_.getOffset(); |
| 124 | } |
| 125 | |
| 126 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 127 | uint32 dst_buffer_size) const { |
| 128 | if (dst_buffer_size < GetDataSize()) |
| 129 | return false; |
| 130 | |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 131 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 132 | memcpy(dst_buffer, data.bytes(), dst_buffer_size); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const { |
| 137 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 138 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 139 | if (file_util::WriteFile(file_path, |
| 140 | reinterpret_cast<const char*>(data.data()), |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 141 | GetDataSize()) != static_cast<int>(GetDataSize())) { |
| 142 | DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
| 149 | // TODO(vandebo) add a method to get the page size for a given page to |
| 150 | // SkPDFDocument. |
| 151 | NOTIMPLEMENTED(); |
| 152 | return gfx::Rect(); |
| 153 | } |
| 154 | |
| 155 | unsigned int PdfMetafileSkia::GetPageCount() const { |
| 156 | // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. |
| 157 | NOTIMPLEMENTED(); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 162 | NOTREACHED(); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | #if defined(OS_WIN) |
| 167 | bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 168 | const RECT* rect) const { |
| 169 | NOTREACHED(); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
| 174 | NOTREACHED(); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | HENHMETAFILE PdfMetafileSkia::emf() const { |
| 179 | NOTREACHED(); |
| 180 | return NULL; |
| 181 | } |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 182 | #elif defined(OS_MACOSX) |
| 183 | /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 184 | rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 185 | the drawing of the PDF into the canvas may result in a rasterized output. |
| 186 | PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 187 | should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 188 | http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc |
| 189 | */ |
| 190 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 191 | CGContextRef context, |
| 192 | const CGRect rect, |
| 193 | bool shrink_to_fit, |
| 194 | bool stretch_to_fit, |
| 195 | bool center_horizontally, |
| 196 | bool center_vertically) const { |
| 197 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 198 | if (data_->pdf_cg_.GetDataSize() == 0) { |
| 199 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 200 | data_->pdf_cg_.InitFromData(data.bytes(), data.size()); |
| 201 | } |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 202 | return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit, |
| 203 | stretch_to_fit, center_horizontally, |
| 204 | center_vertically); |
| 205 | } |
| 206 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 207 | |
| 208 | #if defined(OS_CHROMEOS) |
| 209 | bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
| 210 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
| 211 | |
| 212 | if (fd.fd < 0) { |
| 213 | DLOG(ERROR) << "Invalid file descriptor!"; |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | bool result = true; |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 218 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 219 | if (file_util::WriteFileDescriptor(fd.fd, |
| 220 | reinterpret_cast<const char*>(data.data()), |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 221 | GetDataSize()) != |
| 222 | static_cast<int>(GetDataSize())) { |
| 223 | DLOG(ERROR) << "Failed to save file with fd " << fd.fd; |
| 224 | result = false; |
| 225 | } |
| 226 | |
| 227 | if (fd.auto_close) { |
| 228 | if (HANDLE_EINTR(close(fd.fd)) < 0) { |
| 229 | DPLOG(WARNING) << "close"; |
| 230 | result = false; |
| 231 | } |
| 232 | } |
| 233 | return result; |
| 234 | } |
| 235 | #endif |
| 236 | |
[email protected] | c797a19 | 2011-06-15 16:25:09 | [diff] [blame] | 237 | PdfMetafileSkia::PdfMetafileSkia() |
| 238 | : data_(new PdfMetafileSkiaData), |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame^] | 239 | draft_(false), |
| 240 | page_outstanding_(false) {} |
| 241 | |
| 242 | PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() { |
| 243 | SkPDFDocument pdf_doc; |
| 244 | SkDynamicMemoryWStream pdf_stream; |
| 245 | if (!pdf_doc.appendPage(data_->current_page_)) |
| 246 | return NULL; |
| 247 | |
| 248 | if (!pdf_doc.emitPDF(&pdf_stream)) |
| 249 | return NULL; |
| 250 | |
| 251 | SkAutoDataUnref data(pdf_stream.copyToData()); |
| 252 | if (data.size() == 0) |
| 253 | return NULL; |
| 254 | |
| 255 | PdfMetafileSkia* metafile = new printing::PdfMetafileSkia; |
| 256 | metafile->InitFromData(data.bytes(), data.size()); |
| 257 | return metafile; |
| 258 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 259 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 260 | } // namespace printing |