[email protected] | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 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] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 20 | #include "ui/gfx/point.h" |
| 21 | #include "ui/gfx/rect.h" |
| 22 | #include "ui/gfx/size.h" |
| 23 | |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 24 | #if defined(OS_MACOSX) |
| 25 | #include "printing/pdf_metafile_cg_mac.h" |
| 26 | #endif |
| 27 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 28 | namespace printing { |
| 29 | |
| 30 | struct PdfMetafileSkiaData { |
| 31 | SkRefPtr<SkPDFDevice> current_page_; |
| 32 | SkPDFDocument pdf_doc_; |
| 33 | SkDynamicMemoryWStream pdf_stream_; |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 34 | #if defined(OS_MACOSX) |
| 35 | PdfMetafileCg pdf_cg_; |
| 36 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | PdfMetafileSkia::~PdfMetafileSkia() {} |
| 40 | |
| 41 | bool PdfMetafileSkia::Init() { |
| 42 | return true; |
| 43 | } |
| 44 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 45 | uint32 src_buffer_size) { |
| 46 | return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
| 47 | } |
| 48 | |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 49 | SkDevice* PdfMetafileSkia::StartPageForVectorCanvas( |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 50 | const gfx::Size& page_size, const gfx::Rect& content_area, |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 51 | const float& scale_factor) { |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 52 | DCHECK(!page_outstanding_); |
| 53 | page_outstanding_ = true; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 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 | 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 = |
[email protected] | 8ebe83a4 | 2011-08-28 16:51:31 | [diff] [blame] | 66 | new skia::VectorPlatformDeviceSkia(pdf_page_size, pdf_content_size, |
| 67 | transform); |
| 68 | data_->current_page_ = pdf_device; |
| 69 | return pdf_device.get(); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 73 | const gfx::Rect& content_area, |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 74 | const float& scale_factor) { |
| 75 | NOTREACHED(); |
[email protected] | abdeb5d2 | 2012-05-16 06:57:13 | [diff] [blame^] | 76 | return false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool PdfMetafileSkia::FinishPage() { |
| 80 | DCHECK(data_->current_page_.get()); |
| 81 | |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 82 | data_->pdf_doc_.appendPage(data_->current_page_.get()); |
| 83 | page_outstanding_ = false; |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool PdfMetafileSkia::FinishDocument() { |
| 88 | // Don't do anything if we've already set the data in InitFromData. |
| 89 | if (data_->pdf_stream_.getOffset()) |
| 90 | return true; |
| 91 | |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 92 | if (page_outstanding_) |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 93 | FinishPage(); |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 94 | |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 95 | data_->current_page_ = NULL; |
[email protected] | 54ffd6f6 | 2011-06-02 17:29:05 | [diff] [blame] | 96 | |
[email protected] | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 97 | int font_counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]; |
[email protected] | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 98 | data_->pdf_doc_.getCountOfFontTypes(font_counts); |
| 99 | for (int type = 0; |
| 100 | type <= SkAdvancedTypefaceMetrics::kNotEmbeddable_Font; |
| 101 | type++) { |
| 102 | for (int count = 0; count < font_counts[type]; count++) { |
| 103 | UMA_HISTOGRAM_ENUMERATION( |
| 104 | "PrintPreview.FontType", type, |
| 105 | SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1); |
[email protected] | 54ffd6f6 | 2011-06-02 17:29:05 | [diff] [blame] | 106 | } |
[email protected] | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 109 | return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); |
| 110 | } |
| 111 | |
| 112 | uint32 PdfMetafileSkia::GetDataSize() const { |
| 113 | return data_->pdf_stream_.getOffset(); |
| 114 | } |
| 115 | |
| 116 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 117 | uint32 dst_buffer_size) const { |
| 118 | if (dst_buffer_size < GetDataSize()) |
| 119 | return false; |
| 120 | |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 121 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 122 | memcpy(dst_buffer, data.bytes(), dst_buffer_size); |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const { |
| 127 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 128 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 129 | if (file_util::WriteFile(file_path, |
| 130 | reinterpret_cast<const char*>(data.data()), |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 131 | GetDataSize()) != static_cast<int>(GetDataSize())) { |
| 132 | DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 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 |
| 140 | // SkPDFDocument. |
| 141 | NOTIMPLEMENTED(); |
| 142 | return gfx::Rect(); |
| 143 | } |
| 144 | |
| 145 | unsigned int PdfMetafileSkia::GetPageCount() const { |
| 146 | // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. |
| 147 | NOTIMPLEMENTED(); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 152 | NOTREACHED(); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | #if defined(OS_WIN) |
| 157 | bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 158 | const RECT* rect) const { |
| 159 | NOTREACHED(); |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
| 164 | NOTREACHED(); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | HENHMETAFILE PdfMetafileSkia::emf() const { |
| 169 | NOTREACHED(); |
| 170 | return NULL; |
| 171 | } |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 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 |
| 179 | */ |
| 180 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 181 | CGContextRef context, |
| 182 | const CGRect rect, |
| 183 | bool shrink_to_fit, |
| 184 | bool stretch_to_fit, |
| 185 | bool center_horizontally, |
| 186 | bool center_vertically) const { |
| 187 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 188 | if (data_->pdf_cg_.GetDataSize() == 0) { |
| 189 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 190 | data_->pdf_cg_.InitFromData(data.bytes(), data.size()); |
| 191 | } |
[email protected] | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 192 | return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit, |
| 193 | stretch_to_fit, center_horizontally, |
| 194 | center_vertically); |
| 195 | } |
| 196 | #endif |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 197 | |
| 198 | #if defined(OS_CHROMEOS) |
| 199 | bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
| 200 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
| 201 | |
| 202 | if (fd.fd < 0) { |
| 203 | DLOG(ERROR) << "Invalid file descriptor!"; |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool result = true; |
[email protected] | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 208 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 209 | if (file_util::WriteFileDescriptor(fd.fd, |
| 210 | reinterpret_cast<const char*>(data.data()), |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 211 | GetDataSize()) != |
| 212 | static_cast<int>(GetDataSize())) { |
| 213 | DLOG(ERROR) << "Failed to save file with fd " << fd.fd; |
| 214 | result = false; |
| 215 | } |
| 216 | |
| 217 | if (fd.auto_close) { |
| 218 | if (HANDLE_EINTR(close(fd.fd)) < 0) { |
| 219 | DPLOG(WARNING) << "close"; |
| 220 | result = false; |
| 221 | } |
| 222 | } |
| 223 | return result; |
| 224 | } |
| 225 | #endif |
| 226 | |
[email protected] | c797a19 | 2011-06-15 16:25:09 | [diff] [blame] | 227 | PdfMetafileSkia::PdfMetafileSkia() |
| 228 | : data_(new PdfMetafileSkiaData), |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 229 | page_outstanding_(false) { |
[email protected] | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 230 | } |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 231 | |
| 232 | PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() { |
[email protected] | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 233 | SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 234 | SkDynamicMemoryWStream pdf_stream; |
[email protected] | b1e544f | 2011-07-21 14:52:25 | [diff] [blame] | 235 | if (!pdf_doc.appendPage(data_->current_page_.get())) |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 236 | return NULL; |
| 237 | |
| 238 | if (!pdf_doc.emitPDF(&pdf_stream)) |
| 239 | return NULL; |
| 240 | |
| 241 | SkAutoDataUnref data(pdf_stream.copyToData()); |
| 242 | if (data.size() == 0) |
| 243 | return NULL; |
| 244 | |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 245 | PdfMetafileSkia* metafile = new PdfMetafileSkia; |
[email protected] | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 246 | metafile->InitFromData(data.bytes(), data.size()); |
| 247 | return metafile; |
| 248 | } |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 249 | |
[email protected] | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 250 | } // namespace printing |