epoger@google.com | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
vandebo@chromium.org | 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 | |
brettw@chromium.org | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 7 | #include "base/containers/hash_tables.h" |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
vandebo@chromium.org | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 9 | #include "base/metrics/histogram.h" |
jschuh@chromium.org | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 10 | #include "base/numerics/safe_conversions.h" |
brettw@chromium.org | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 11 | #include "base/posix/eintr_wrapper.h" |
enne@chromium.org | 584abe7 | 2013-05-18 09:40:26 | [diff] [blame] | 12 | #include "skia/ext/refptr.h" |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 13 | #include "skia/ext/vector_platform_device_skia.h" |
reed@google.com | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 14 | #include "third_party/skia/include/core/SkData.h" |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 15 | #include "third_party/skia/include/core/SkRefCnt.h" |
vandebo@chromium.org | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 16 | #include "third_party/skia/include/core/SkScalar.h" |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 17 | #include "third_party/skia/include/core/SkStream.h" |
vandebo@chromium.org | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 18 | #include "third_party/skia/include/core/SkTypeface.h" |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 19 | #include "third_party/skia/include/pdf/SkPDFDevice.h" |
| 20 | #include "third_party/skia/include/pdf/SkPDFDocument.h" |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 21 | #include "ui/gfx/point.h" |
| 22 | #include "ui/gfx/rect.h" |
| 23 | #include "ui/gfx/size.h" |
vandebo@chromium.org | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 24 | |
caryclark@chromium.org | 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 | |
rvargas@chromium.org | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 29 | #if defined(OS_POSIX) |
| 30 | #include "base/file_descriptor_posix.h" |
| 31 | #endif |
| 32 | |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 33 | namespace printing { |
| 34 | |
| 35 | struct PdfMetafileSkiaData { |
enne@chromium.org | 584abe7 | 2013-05-18 09:40:26 | [diff] [blame] | 36 | skia::RefPtr<SkPDFDevice> current_page_; |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 37 | SkPDFDocument pdf_doc_; |
| 38 | SkDynamicMemoryWStream pdf_stream_; |
caryclark@chromium.org | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 39 | #if defined(OS_MACOSX) |
| 40 | PdfMetafileCg pdf_cg_; |
| 41 | #endif |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | PdfMetafileSkia::~PdfMetafileSkia() {} |
| 45 | |
| 46 | bool PdfMetafileSkia::Init() { |
| 47 | return true; |
| 48 | } |
| 49 | bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 50 | uint32 src_buffer_size) { |
| 51 | return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
| 52 | } |
| 53 | |
robertphillips@google.com | 598e4f8 | 2013-08-26 18:37:29 | [diff] [blame] | 54 | SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas( |
kmadhusu@chromium.org | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 55 | const gfx::Size& page_size, const gfx::Rect& content_area, |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 56 | const float& scale_factor) { |
kmadhusu@chromium.org | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 57 | DCHECK(!page_outstanding_); |
| 58 | page_outstanding_ = true; |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 59 | |
vandebo@chromium.org | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 60 | // Adjust for the margins and apply the scale factor. |
| 61 | SkMatrix transform; |
ctguil@chromium.org | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 62 | transform.setTranslate(SkIntToScalar(content_area.x()), |
| 63 | SkIntToScalar(content_area.y())); |
vandebo@chromium.org | a34f012 | 2011-04-12 17:36:39 | [diff] [blame] | 64 | transform.preScale(SkFloatToScalar(scale_factor), |
| 65 | SkFloatToScalar(scale_factor)); |
| 66 | |
ctguil@chromium.org | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 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()); |
enne@chromium.org | 584abe7 | 2013-05-18 09:40:26 | [diff] [blame] | 70 | skia::RefPtr<SkPDFDevice> pdf_device = |
| 71 | skia::AdoptRef(new skia::VectorPlatformDeviceSkia( |
| 72 | pdf_page_size, pdf_content_size, transform)); |
twiz@google.com | 8ebe83a4 | 2011-08-28 16:51:31 | [diff] [blame] | 73 | data_->current_page_ = pdf_device; |
| 74 | return pdf_device.get(); |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
ctguil@chromium.org | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 78 | const gfx::Rect& content_area, |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 79 | const float& scale_factor) { |
| 80 | NOTREACHED(); |
thakis@chromium.org | abdeb5d2 | 2012-05-16 06:57:13 | [diff] [blame] | 81 | return false; |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool PdfMetafileSkia::FinishPage() { |
| 85 | DCHECK(data_->current_page_.get()); |
| 86 | |
kmadhusu@chromium.org | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 87 | data_->pdf_doc_.appendPage(data_->current_page_.get()); |
| 88 | page_outstanding_ = false; |
vandebo@chromium.org | 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 | |
kmadhusu@chromium.org | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 97 | if (page_outstanding_) |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 98 | FinishPage(); |
vandebo@chromium.org | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 99 | |
enne@chromium.org | 584abe7 | 2013-05-18 09:40:26 | [diff] [blame] | 100 | data_->current_page_.clear(); |
vandebo@chromium.org | 54ffd6f6 | 2011-06-02 17:29:05 | [diff] [blame] | 101 | |
vandebo@chromium.org | 3cd37bc0 | 2013-12-09 20:52:11 | [diff] [blame] | 102 | int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; |
epoger@google.com | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 103 | data_->pdf_doc_.getCountOfFontTypes(font_counts); |
| 104 | for (int type = 0; |
vandebo@chromium.org | 3cd37bc0 | 2013-12-09 20:52:11 | [diff] [blame] | 105 | type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; |
epoger@google.com | 0a4392a | 2012-03-23 17:50:19 | [diff] [blame] | 106 | type++) { |
| 107 | for (int count = 0; count < font_counts[type]; count++) { |
| 108 | UMA_HISTOGRAM_ENUMERATION( |
| 109 | "PrintPreview.FontType", type, |
vandebo@chromium.org | 3cd37bc0 | 2013-12-09 20:52:11 | [diff] [blame] | 110 | SkAdvancedTypefaceMetrics::kOther_Font + 2); |
vandebo@chromium.org | 54ffd6f6 | 2011-06-02 17:29:05 | [diff] [blame] | 111 | } |
vandebo@chromium.org | 67e16b39 | 2011-05-30 20:58:09 | [diff] [blame] | 112 | } |
| 113 | |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 114 | return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); |
| 115 | } |
| 116 | |
| 117 | uint32 PdfMetafileSkia::GetDataSize() const { |
jschuh@chromium.org | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 118 | return base::checked_cast<uint32>(data_->pdf_stream_.getOffset()); |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 122 | uint32 dst_buffer_size) const { |
| 123 | if (dst_buffer_size < GetDataSize()) |
| 124 | return false; |
| 125 | |
reed@google.com | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 126 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
reed@google.com | 24b86abf | 2012-07-12 12:32:32 | [diff] [blame] | 127 | memcpy(dst_buffer, data->bytes(), dst_buffer_size); |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 131 | gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
| 132 | // TODO(vandebo) add a method to get the page size for a given page to |
| 133 | // SkPDFDocument. |
| 134 | NOTIMPLEMENTED(); |
| 135 | return gfx::Rect(); |
| 136 | } |
| 137 | |
| 138 | unsigned int PdfMetafileSkia::GetPageCount() const { |
| 139 | // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. |
| 140 | NOTIMPLEMENTED(); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 145 | NOTREACHED(); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | #if defined(OS_WIN) |
| 150 | bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 151 | const RECT* rect) const { |
| 152 | NOTREACHED(); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
| 157 | NOTREACHED(); |
| 158 | return false; |
| 159 | } |
| 160 | |
caryclark@chromium.org | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 161 | #elif defined(OS_MACOSX) |
| 162 | /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
| 163 | rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 164 | the drawing of the PDF into the canvas may result in a rasterized output. |
| 165 | PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 166 | should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 167 | http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_instance.cc |
| 168 | */ |
| 169 | bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 170 | CGContextRef context, |
| 171 | const CGRect rect, |
thestig@chromium.org | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 172 | const MacRenderPageParams& params) const { |
caryclark@chromium.org | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 173 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
reed@google.com | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 174 | if (data_->pdf_cg_.GetDataSize() == 0) { |
| 175 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
reed@google.com | 24b86abf | 2012-07-12 12:32:32 | [diff] [blame] | 176 | data_->pdf_cg_.InitFromData(data->bytes(), data->size()); |
reed@google.com | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 177 | } |
thestig@chromium.org | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 178 | return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
caryclark@chromium.org | b8d85bc | 2011-06-22 13:34:57 | [diff] [blame] | 179 | } |
| 180 | #endif |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 181 | |
avayvod@chromium.org | b25f003 | 2013-08-19 22:26:25 | [diff] [blame] | 182 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 183 | bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
| 184 | DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
| 185 | |
| 186 | if (fd.fd < 0) { |
| 187 | DLOG(ERROR) << "Invalid file descriptor!"; |
| 188 | return false; |
| 189 | } |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 190 | base::File file(fd.fd); |
reed@google.com | e195fbf5 | 2011-06-27 16:51:20 | [diff] [blame] | 191 | SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 192 | bool result = |
| 193 | file.WriteAtCurrentPos(reinterpret_cast<const char*>(data->data()), |
| 194 | GetDataSize()) == static_cast<int>(GetDataSize()); |
| 195 | DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 196 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 197 | if (!fd.auto_close) |
| 198 | file.TakePlatformFile(); |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 199 | return result; |
| 200 | } |
| 201 | #endif |
| 202 | |
kmadhusu@chromium.org | c797a19 | 2011-06-15 16:25:09 | [diff] [blame] | 203 | PdfMetafileSkia::PdfMetafileSkia() |
| 204 | : data_(new PdfMetafileSkiaData), |
kmadhusu@chromium.org | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 205 | page_outstanding_(false) { |
vandebo@chromium.org | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 206 | } |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 207 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 208 | scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
| 209 | scoped_ptr<PdfMetafileSkia> metafile; |
vandebo@chromium.org | 19b9d3b | 2011-07-23 02:08:57 | [diff] [blame] | 210 | SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); |
reed@google.com | b1e544f | 2011-07-21 14:52:25 | [diff] [blame] | 211 | if (!pdf_doc.appendPage(data_->current_page_.get())) |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 212 | return metafile.Pass(); |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 213 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 214 | SkDynamicMemoryWStream pdf_stream; |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 215 | if (!pdf_doc.emitPDF(&pdf_stream)) |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 216 | return metafile.Pass(); |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 217 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 218 | SkAutoDataUnref data_copy(pdf_stream.copyToData()); |
| 219 | if (data_copy->size() == 0) |
| 220 | return scoped_ptr<PdfMetafileSkia>(); |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 221 | |
vitalybuka | 5d129058 | 2014-09-12 09:19:59 | [diff] [blame^] | 222 | metafile.reset(new PdfMetafileSkia); |
| 223 | if (!metafile->InitFromData(data_copy->bytes(), |
| 224 | base::checked_cast<uint32>(data_copy->size()))) { |
| 225 | metafile.reset(); |
| 226 | } |
| 227 | return metafile.Pass(); |
kmadhusu@chromium.org | 59751637 | 2011-07-01 05:10:44 | [diff] [blame] | 228 | } |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 229 | |
vandebo@chromium.org | 8f87929 | 2011-04-08 00:21:20 | [diff] [blame] | 230 | } // namespace printing |