| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/pdf_metafile_skia.h" | 5 #include "printing/pdf_metafile_skia.h" |
| 6 | 6 |
| 7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "skia/ext/refptr.h" | 12 #include "skia/ext/refptr.h" |
| 13 #include "skia/ext/vector_canvas.h" | 13 #include "skia/ext/vector_canvas.h" |
| 14 #include "third_party/skia/include/core/SkData.h" | 14 #include "third_party/skia/include/core/SkData.h" |
| 15 #include "third_party/skia/include/core/SkDocument.h" | |
| 16 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 17 #include "third_party/skia/include/core/SkRect.h" | |
| 18 #include "third_party/skia/include/core/SkRefCnt.h" | 15 #include "third_party/skia/include/core/SkRefCnt.h" |
| 19 #include "third_party/skia/include/core/SkScalar.h" | 16 #include "third_party/skia/include/core/SkScalar.h" |
| 20 #include "third_party/skia/include/core/SkSize.h" | |
| 21 #include "third_party/skia/include/core/SkStream.h" | 17 #include "third_party/skia/include/core/SkStream.h" |
| 18 #include "third_party/skia/include/core/SkTypeface.h" |
| 19 #include "third_party/skia/include/pdf/SkPDFDevice.h" |
| 20 #include "third_party/skia/include/pdf/SkPDFDocument.h" |
| 22 #include "ui/gfx/point.h" | 21 #include "ui/gfx/point.h" |
| 23 #include "ui/gfx/rect.h" | 22 #include "ui/gfx/rect.h" |
| 24 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
| 25 | 24 |
| 26 #if defined(OS_MACOSX) | 25 #if defined(OS_MACOSX) |
| 27 #include "printing/pdf_metafile_cg_mac.h" | 26 #include "printing/pdf_metafile_cg_mac.h" |
| 28 #endif | 27 #endif |
| 29 | 28 |
| 30 #if defined(OS_POSIX) | 29 #if defined(OS_POSIX) |
| 31 #include "base/file_descriptor_posix.h" | 30 #include "base/file_descriptor_posix.h" |
| 32 #endif | 31 #endif |
| 33 | 32 |
| 34 namespace { | |
| 35 // This struct represents all the data we need to draw and redraw this | |
| 36 // page into a SkDocument. | |
| 37 struct Page { | |
| 38 Page(const SkSize& page_size, const SkRect& content_area) | |
| 39 : page_size_(page_size), | |
| 40 content_area_(content_area), | |
| 41 content_(/*NULL*/) {} | |
| 42 SkSize page_size_; | |
| 43 SkRect content_area_; | |
| 44 skia::RefPtr<SkPicture> content_; | |
| 45 }; | |
| 46 } // namespace | |
| 47 | |
| 48 static SkSize ToSkSize(const gfx::Size& size) { | |
| 49 return SkSize::Make(SkIntToScalar(size.width()), | |
| 50 SkIntToScalar(size.height())); | |
| 51 } | |
| 52 | |
| 53 static SkRect ToSkRect(const gfx::Rect& rect) { | |
| 54 return SkRect::MakeLTRB(SkIntToScalar(rect.x()), SkIntToScalar(rect.y()), | |
| 55 SkIntToScalar(rect.right()), | |
| 56 SkIntToScalar(rect.bottom())); | |
| 57 } | |
| 58 | |
| 59 static gfx::Size ToGfxSize(const SkSize& size) { | |
| 60 return gfx::Size(SkScalarTruncToInt(size.width()), | |
| 61 SkScalarTruncToInt(size.height())); | |
| 62 } | |
| 63 | |
| 64 static bool WriteAssetToBuffer(SkStreamAsset* asset, | |
| 65 void* buffer, | |
| 66 size_t size) { | |
| 67 DCHECK(asset->getPosition() == 0); // Be kind: please rewind. | |
| 68 size_t length = asset->getLength(); | |
| 69 if (length > size) | |
| 70 return false; | |
| 71 bool success = (length == asset->read(buffer, length)); | |
| 72 (void)asset->rewind(); | |
| 73 return success; | |
| 74 } | |
| 75 | |
| 76 namespace printing { | 33 namespace printing { |
| 77 | 34 |
| 78 struct PdfMetafileSkiaData { | 35 struct PdfMetafileSkiaData { |
| 79 scoped_ptr<SkPictureRecorder> recorder_; // Current recording | 36 skia::RefPtr<SkPDFDevice> current_page_; |
| 80 | 37 skia::RefPtr<SkCanvas> current_page_canvas_; |
| 81 std::vector<Page> pages_; | 38 SkPDFDocument pdf_doc_; |
| 82 skia::RefPtr<SkStreamAsset> pdf_data_; | 39 SkDynamicMemoryWStream pdf_stream_; |
| 83 | |
| 84 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
| 85 PdfMetafileCg pdf_cg_; | 41 PdfMetafileCg pdf_cg_; |
| 86 #endif | 42 #endif |
| 87 }; | 43 }; |
| 88 | 44 |
| 89 PdfMetafileSkia::~PdfMetafileSkia() {} | 45 PdfMetafileSkia::~PdfMetafileSkia() {} |
| 90 | 46 |
| 91 bool PdfMetafileSkia::Init() { | 47 bool PdfMetafileSkia::Init() { |
| 92 return true; | 48 return true; |
| 93 } | 49 } |
| 94 | |
| 95 // TODO(halcanary): Create a Metafile class that only stores data. | |
| 96 // Metafile::InitFromData is orthogonal to what the rest of | |
| 97 // PdfMetafileSkia does. | |
| 98 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 50 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 99 uint32 src_buffer_size) { | 51 uint32 src_buffer_size) { |
| 100 if (data_->pdf_data_) | 52 return data_->pdf_stream_.write(src_buffer, src_buffer_size); |
| 101 data_->pdf_data_.clear(); // free up RAM first. | |
| 102 SkDynamicMemoryWStream dynamic_memory; | |
| 103 if (!dynamic_memory.write(src_buffer, src_buffer_size)) | |
| 104 return false; | |
| 105 data_->pdf_data_ = skia::AdoptRef(dynamic_memory.detachAsStream()); | |
| 106 return true; | |
| 107 } | 53 } |
| 108 | 54 |
| 109 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 55 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
| 110 const gfx::Rect& content_area, | 56 const gfx::Rect& content_area, |
| 111 const float& scale_factor) { | 57 const float& scale_factor) { |
| 112 if (data_->recorder_) | 58 DCHECK(!data_->current_page_canvas_); |
| 113 this->FinishPage(); | |
| 114 DCHECK(!data_->recorder_); | |
| 115 data_->recorder_.reset(SkNEW(SkPictureRecorder)); | |
| 116 SkSize sk_page_size = ToSkSize(page_size); | |
| 117 data_->pages_.push_back(Page(sk_page_size, ToSkRect(content_area))); | |
| 118 | 59 |
| 119 SkCanvas* recordingCanvas = data_->recorder_->beginRecording( | 60 // Adjust for the margins and apply the scale factor. |
| 120 sk_page_size.width(), sk_page_size.height(), NULL, 0); | 61 SkMatrix transform; |
| 121 // recordingCanvas is owned by the data_->recorder_. No ref() necessary. | 62 transform.setTranslate(SkIntToScalar(content_area.x()), |
| 122 if (!recordingCanvas) | 63 SkIntToScalar(content_area.y())); |
| 123 return false; | 64 transform.preScale(SkFloatToScalar(scale_factor), |
| 124 recordingCanvas->scale(scale_factor, scale_factor); | 65 SkFloatToScalar(scale_factor)); |
| 66 |
| 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()); |
| 70 |
| 71 data_->current_page_ = skia::AdoptRef( |
| 72 new SkPDFDevice(pdf_page_size, pdf_content_size, transform)); |
| 73 data_->current_page_canvas_ = |
| 74 skia::AdoptRef(new SkCanvas(data_->current_page_.get())); |
| 125 return true; | 75 return true; |
| 126 } | 76 } |
| 127 | 77 |
| 128 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( | 78 skia::VectorCanvas* PdfMetafileSkia::GetVectorCanvasForNewPage( |
| 129 const gfx::Size& page_size, | 79 const gfx::Size& page_size, |
| 130 const gfx::Rect& content_area, | 80 const gfx::Rect& content_area, |
| 131 const float& scale_factor) { | 81 const float& scale_factor) { |
| 132 if (!StartPage(page_size, content_area, scale_factor)) | 82 if (!StartPage(page_size, content_area, scale_factor)) |
| 133 return nullptr; | 83 return nullptr; |
| 134 return data_->recorder_->getRecordingCanvas(); | 84 return data_->current_page_canvas_.get(); |
| 135 } | 85 } |
| 136 | 86 |
| 137 bool PdfMetafileSkia::FinishPage() { | 87 bool PdfMetafileSkia::FinishPage() { |
| 138 if (!data_->recorder_) | 88 DCHECK(data_->current_page_canvas_); |
| 139 return false; | 89 DCHECK(data_->current_page_); |
| 140 DCHECK(!(data_->pages_.back().content_)); | 90 |
| 141 data_->pages_.back().content_ = | 91 data_->current_page_canvas_.clear(); // Unref SkCanvas. |
| 142 skia::AdoptRef(data_->recorder_->endRecording()); | 92 data_->pdf_doc_.appendPage(data_->current_page_.get()); |
| 143 data_->recorder_.reset(); | |
| 144 return true; | 93 return true; |
| 145 } | 94 } |
| 146 | 95 |
| 147 bool PdfMetafileSkia::FinishDocument() { | 96 bool PdfMetafileSkia::FinishDocument() { |
| 148 // If we've already set the data in InitFromData, overwrite it. | 97 // Don't do anything if we've already set the data in InitFromData. |
| 149 if (data_->pdf_data_) | 98 if (data_->pdf_stream_.getOffset()) |
| 150 data_->pdf_data_.clear(); // Free up RAM first. | 99 return true; |
| 151 | 100 |
| 152 if (data_->recorder_) | 101 if (data_->current_page_canvas_) |
| 153 this->FinishPage(); | 102 FinishPage(); |
| 154 | 103 |
| 155 SkDynamicMemoryWStream pdf_stream; | 104 data_->current_page_.clear(); |
| 156 skia::RefPtr<SkDocument> pdf_doc = | 105 |
| 157 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); | 106 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; |
| 158 for (const auto& page : data_->pages_) { | 107 data_->pdf_doc_.getCountOfFontTypes(font_counts); |
| 159 SkCanvas* canvas = pdf_doc->beginPage( | 108 for (int type = 0; |
| 160 page.page_size_.width(), page.page_size_.height(), &page.content_area_); | 109 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; |
| 161 canvas->drawPicture(page.content_.get()); | 110 type++) { |
| 162 pdf_doc->endPage(); | 111 for (int count = 0; count < font_counts[type]; count++) { |
| 112 UMA_HISTOGRAM_ENUMERATION( |
| 113 "PrintPreview.FontType", type, |
| 114 SkAdvancedTypefaceMetrics::kOther_Font + 2); |
| 115 } |
| 163 } | 116 } |
| 164 if (!pdf_doc->close()) | |
| 165 return false; | |
| 166 | 117 |
| 167 data_->pdf_data_ = skia::AdoptRef(pdf_stream.detachAsStream()); | 118 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); |
| 168 return true; | |
| 169 } | 119 } |
| 170 | 120 |
| 171 uint32 PdfMetafileSkia::GetDataSize() const { | 121 uint32 PdfMetafileSkia::GetDataSize() const { |
| 172 if (!data_->pdf_data_) | 122 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset()); |
| 173 return 0; | |
| 174 return base::checked_cast<uint32>(data_->pdf_data_->getLength()); | |
| 175 } | 123 } |
| 176 | 124 |
| 177 bool PdfMetafileSkia::GetData(void* dst_buffer, | 125 bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 178 uint32 dst_buffer_size) const { | 126 uint32 dst_buffer_size) const { |
| 179 if (!data_->pdf_data_) | 127 if (dst_buffer_size < GetDataSize()) |
| 180 return false; | 128 return false; |
| 181 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, | 129 |
| 182 base::checked_cast<size_t>(dst_buffer_size)); | 130 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 131 memcpy(dst_buffer, data->bytes(), dst_buffer_size); |
| 132 return true; |
| 183 } | 133 } |
| 184 | 134 |
| 185 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 135 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
| 186 if (page_number < data_->pages_.size()) | 136 // TODO(vandebo) add a method to get the page size for a given page to |
| 187 return gfx::Rect(ToGfxSize(data_->pages_[page_number].page_size_)); | 137 // SkPDFDocument. |
| 138 NOTIMPLEMENTED(); |
| 188 return gfx::Rect(); | 139 return gfx::Rect(); |
| 189 } | 140 } |
| 190 | 141 |
| 191 unsigned int PdfMetafileSkia::GetPageCount() const { | 142 unsigned int PdfMetafileSkia::GetPageCount() const { |
| 192 return base::checked_cast<unsigned int>(data_->pages_.size()); | 143 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. |
| 144 NOTIMPLEMENTED(); |
| 145 return 0; |
| 193 } | 146 } |
| 194 | 147 |
| 195 gfx::NativeDrawingContext PdfMetafileSkia::context() const { | 148 gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 196 NOTREACHED(); | 149 NOTREACHED(); |
| 197 return NULL; | 150 return NULL; |
| 198 } | 151 } |
| 199 | 152 |
| 200 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
| 201 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, | 154 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 202 const RECT* rect) const { | 155 const RECT* rect) const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 214 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, | 167 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 215 the drawing of the PDF into the canvas may result in a rasterized output. | 168 the drawing of the PDF into the canvas may result in a rasterized output. |
| 216 PDFMetafileSkia::RenderPage should be not implemented as shown and instead | 169 PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 217 should do something like the following CL in PluginInstance::PrintPDFOutput: | 170 should do something like the following CL in PluginInstance::PrintPDFOutput: |
| 218 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_
instance.cc | 171 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_
instance.cc |
| 219 */ | 172 */ |
| 220 bool PdfMetafileSkia::RenderPage(unsigned int page_number, | 173 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 221 CGContextRef context, | 174 CGContextRef context, |
| 222 const CGRect rect, | 175 const CGRect rect, |
| 223 const MacRenderPageParams& params) const { | 176 const MacRenderPageParams& params) const { |
| 224 DCHECK_GT(GetDataSize(), 0U); | 177 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
| 225 if (data_->pdf_cg_.GetDataSize() == 0) { | 178 if (data_->pdf_cg_.GetDataSize() == 0) { |
| 226 if (GetDataSize() == 0) | 179 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 227 return false; | 180 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); |
| 228 size_t length = data_->pdf_data_->getLength(); | |
| 229 std::vector<uint8_t> buffer(length); | |
| 230 (void)WriteAssetToBuffer(data_->pdf_data_.get(), &buffer[0], length); | |
| 231 data_->pdf_cg_.InitFromData(&buffer[0], base::checked_cast<uint32>(length)); | |
| 232 } | 181 } |
| 233 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 182 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 234 } | 183 } |
| 235 #endif | 184 #endif |
| 236 | 185 |
| 237 bool PdfMetafileSkia::SaveTo(base::File* file) const { | 186 bool PdfMetafileSkia::SaveTo(base::File* file) const { |
| 238 if (GetDataSize() == 0U) | 187 if (GetDataSize() == 0U) |
| 239 return false; | 188 return false; |
| 240 DCHECK(data_->pdf_data_.get()); | 189 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 241 SkStreamAsset* asset = data_->pdf_data_.get(); | 190 // TODO(halcanary): rewrite this function without extra data copy |
| 242 DCHECK_EQ(asset->getPosition(), 0U); | 191 // using SkStreamAsset. |
| 243 | 192 const char* ptr = reinterpret_cast<const char*>(data->data()); |
| 244 const size_t maximum_buffer_size = 1024 * 1024; | 193 int size = base::checked_cast<int>(data->size()); |
| 245 std::vector<char> buffer(std::min(maximum_buffer_size, asset->getLength())); | 194 return file->WriteAtCurrentPos(ptr, size) == size; |
| 246 do { | |
| 247 size_t read_size = asset->read(&buffer[0], buffer.size()); | |
| 248 if (read_size == 0) | |
| 249 break; | |
| 250 DCHECK_GE(buffer.size(), read_size); | |
| 251 if (!file->WriteAtCurrentPos(&buffer[0], | |
| 252 base::checked_cast<int>(read_size))) { | |
| 253 (void)asset->rewind(); | |
| 254 return false; | |
| 255 } | |
| 256 } while (!asset->isAtEnd()); | |
| 257 | |
| 258 (void)asset->rewind(); | |
| 259 return true; | |
| 260 } | 195 } |
| 261 | 196 |
| 262 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 197 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 263 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 198 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
| 264 DCHECK_GT(GetDataSize(), 0U); | 199 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
| 265 | 200 |
| 266 if (fd.fd < 0) { | 201 if (fd.fd < 0) { |
| 267 DLOG(ERROR) << "Invalid file descriptor!"; | 202 DLOG(ERROR) << "Invalid file descriptor!"; |
| 268 return false; | 203 return false; |
| 269 } | 204 } |
| 270 base::File file(fd.fd); | 205 base::File file(fd.fd); |
| 271 bool result = SaveTo(&file); | 206 bool result = SaveTo(&file); |
| 272 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; | 207 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
| 273 | 208 |
| 274 if (!fd.auto_close) | 209 if (!fd.auto_close) |
| 275 file.TakePlatformFile(); | 210 file.TakePlatformFile(); |
| 276 return result; | 211 return result; |
| 277 } | 212 } |
| 278 #endif | 213 #endif |
| 279 | 214 |
| 280 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { | 215 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { |
| 281 } | 216 } |
| 282 | 217 |
| 283 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { | 218 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
| 284 // If we only ever need the metafile for the last page, should we | 219 scoped_ptr<PdfMetafileSkia> metafile; |
| 285 // only keep a handle on one SkPicture? | 220 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); |
| 286 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); | 221 if (!pdf_doc.appendPage(data_->current_page_.get())) |
| 287 | |
| 288 if (data_->pages_.size() == 0) | |
| 289 return metafile.Pass(); | 222 return metafile.Pass(); |
| 290 | 223 |
| 291 if (data_->recorder_) // page outstanding | 224 SkDynamicMemoryWStream pdf_stream; |
| 225 if (!pdf_doc.emitPDF(&pdf_stream)) |
| 292 return metafile.Pass(); | 226 return metafile.Pass(); |
| 293 | 227 |
| 294 const Page& page = data_->pages_.back(); | 228 SkAutoDataUnref data_copy(pdf_stream.copyToData()); |
| 229 if (data_copy->size() == 0) |
| 230 return scoped_ptr<PdfMetafileSkia>(); |
| 295 | 231 |
| 296 metafile->data_->pages_.push_back(page); // Copy page data; | 232 metafile.reset(new PdfMetafileSkia); |
| 297 // Should increment refcnt on page->content_. | 233 if (!metafile->InitFromData(data_copy->bytes(), |
| 298 | 234 base::checked_cast<uint32>(data_copy->size()))) { |
| 299 if (!metafile->FinishDocument()) // Generate PDF. | |
| 300 metafile.reset(); | 235 metafile.reset(); |
| 301 | 236 } |
| 302 return metafile.Pass(); | 237 return metafile.Pass(); |
| 303 } | 238 } |
| 304 | 239 |
| 305 } // namespace printing | 240 } // namespace printing |
| OLD | NEW |