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