Chromium Code Reviews| 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/platform_canvas.h" | |
| 12 #include "skia/ext/refptr.h" | 13 #include "skia/ext/refptr.h" |
| 13 #include "skia/ext/vector_platform_device_skia.h" | 14 #include "skia/ext/vector_canvas.h" |
| 14 #include "third_party/skia/include/core/SkData.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkDocument.h" | |
| 17 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 18 #include "third_party/skia/include/core/SkRect.h" | |
| 15 #include "third_party/skia/include/core/SkRefCnt.h" | 19 #include "third_party/skia/include/core/SkRefCnt.h" |
| 16 #include "third_party/skia/include/core/SkScalar.h" | 20 #include "third_party/skia/include/core/SkScalar.h" |
| 21 #include "third_party/skia/include/core/SkSize.h" | |
| 17 #include "third_party/skia/include/core/SkStream.h" | 22 #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" | |
| 21 #include "ui/gfx/point.h" | 23 #include "ui/gfx/point.h" |
| 22 #include "ui/gfx/rect.h" | 24 #include "ui/gfx/rect.h" |
| 23 #include "ui/gfx/size.h" | 25 #include "ui/gfx/size.h" |
| 24 | 26 |
| 25 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
| 26 #include "printing/pdf_metafile_cg_mac.h" | 28 #include "printing/pdf_metafile_cg_mac.h" |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 #if defined(OS_POSIX) | 31 #if defined(OS_POSIX) |
| 30 #include "base/file_descriptor_posix.h" | 32 #include "base/file_descriptor_posix.h" |
| 31 #endif | 33 #endif |
| 32 | 34 |
| 35 class SkBaseDevice; | |
| 36 | |
| 37 namespace { | |
| 38 // This struct represents all the data we need to draw and redraw this | |
| 39 // page into a SkDocument. | |
| 40 struct Page { | |
| 41 Page(const SkSize& page_size, const SkRect& content_area); | |
| 42 SkSize page_size_; | |
| 43 SkRect content_area_; | |
| 44 skia::RefPtr<SkPicture> content_; | |
| 45 }; | |
| 46 | |
| 47 Page::Page(const SkSize& page_size, const SkRect& content_area) | |
| 48 : page_size_(page_size), content_area_(content_area), content_(/*NULL*/) {} | |
| 49 | |
| 50 SkSize ToSkSize(const gfx::Size& size) { | |
| 51 return SkSize::Make(SkIntToScalar(size.width()), | |
| 52 SkIntToScalar(size.height())); | |
| 53 } | |
| 54 | |
| 55 SkRect ToSkRect(const gfx::Rect& rect) { | |
| 56 return SkRect::MakeLTRB(SkIntToScalar(rect.x()), | |
| 57 SkIntToScalar(rect.y()), | |
| 58 SkIntToScalar(rect.right()), | |
| 59 SkIntToScalar(rect.bottom())); | |
| 60 } | |
| 61 | |
| 62 gfx::Rect ToGfxRect(const SkRect& rect) { | |
| 63 return gfx::Rect(SkScalarTruncToInt(rect.x()), | |
| 64 SkScalarTruncToInt(rect.y()), | |
| 65 SkScalarTruncToInt(rect.width()), | |
| 66 SkScalarTruncToInt(rect.height())); | |
| 67 } | |
| 68 | |
| 69 bool WriteAssetToBuffer(SkStreamAsset* asset, void* buffer, size_t size) { | |
| 70 DCHECK(asset); | |
| 71 DCHECK(asset->getPosition() == 0); | |
| 72 size_t length = asset->getLength(); | |
| 73 if (length > size) | |
| 74 return false; | |
| 75 bool success = (length == asset->read(buffer, length)); | |
| 76 (void)asset->rewind(); | |
| 77 return success; | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 33 namespace printing { | 82 namespace printing { |
| 34 | 83 |
| 35 struct PdfMetafileSkiaData { | 84 struct PdfMetafileSkiaData { |
| 36 skia::RefPtr<SkPDFDevice> current_page_; | 85 scoped_ptr<SkPictureRecorder> recorder_; // Current recording |
| 37 SkPDFDocument pdf_doc_; | 86 |
| 38 SkDynamicMemoryWStream pdf_stream_; | 87 std::vector<Page> pages_; |
| 88 skia::RefPtr<SkStreamAsset> pdf_data_; | |
|
Vitaly Buka (NO REVIEWS)
2014/11/04 19:23:55
I guess order shouldbe be:
1. Methods
2. Members
hal.canary
2014/11/16 21:29:28
Acknowledged.
| |
| 89 | |
| 90 // True when the current page is outstanding (and still writable). | |
| 91 bool PageOutstanding() const; | |
| 92 | |
| 93 // True if InitFromData() or FinishDocument has been called. | |
| 94 bool HasData() const; | |
| 95 | |
| 96 size_t PageCount() const; | |
| 97 | |
| 98 // Return a const pointer to the page, or NULL on error. | |
| 99 // The page must be finished (via FinishPage()). | |
| 100 // The pointer is only valid while pages_ is unmodified. | |
| 101 const Page* GetPage(size_t page_number) const; | |
| 102 | |
| 39 #if defined(OS_MACOSX) | 103 #if defined(OS_MACOSX) |
| 40 PdfMetafileCg pdf_cg_; | 104 PdfMetafileCg pdf_cg_; |
| 41 #endif | 105 #endif |
| 42 }; | 106 }; |
| 43 | 107 |
| 108 bool PdfMetafileSkiaData::PageOutstanding() const { | |
| 109 return recorder_.get() != NULL; | |
| 110 } | |
| 111 | |
| 112 bool PdfMetafileSkiaData::HasData() const { return (pdf_data_.get() != NULL); } | |
| 113 | |
| 114 size_t PdfMetafileSkiaData::PageCount() const { return pages_.size(); } | |
| 115 | |
| 116 const Page* PdfMetafileSkiaData::GetPage(size_t page_number) const { | |
| 117 if (page_number >= PageCount()) | |
| 118 return NULL; | |
| 119 | |
| 120 // Last page and page not finished recording | |
| 121 if ((page_number == PageCount() - 1) && this->PageOutstanding()) | |
| 122 return NULL; | |
| 123 DCHECK(pages_[page_number].content_.get()); | |
| 124 return &(pages_[page_number]); | |
| 125 } | |
| 126 | |
| 44 PdfMetafileSkia::~PdfMetafileSkia() {} | 127 PdfMetafileSkia::~PdfMetafileSkia() {} |
| 45 | 128 |
| 46 bool PdfMetafileSkia::Init() { | 129 bool PdfMetafileSkia::Init() { |
| 47 return true; | 130 return true; |
| 48 } | 131 } |
| 132 | |
| 133 // TODO(halcanary): Create a Metafile class that only stores data. | |
| 134 // Metafile::InitFromData is orthogonal to what the rest of | |
| 135 // PdfMetafileSkia does. | |
| 49 bool PdfMetafileSkia::InitFromData(const void* src_buffer, | 136 bool PdfMetafileSkia::InitFromData(const void* src_buffer, |
| 50 uint32 src_buffer_size) { | 137 uint32 src_buffer_size) { |
| 51 return data_->pdf_stream_.write(src_buffer, src_buffer_size); | 138 if (data_->HasData()) |
| 139 data_->pdf_data_.clear(); // free up RAM first. | |
| 140 SkDynamicMemoryWStream dynamic_memory; | |
| 141 if (!dynamic_memory.write(src_buffer, src_buffer_size)) | |
| 142 return false; | |
| 143 data_->pdf_data_ = skia::AdoptRef(dynamic_memory.detachAsStream()); | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 skia::RefPtr<skia::VectorCanvas> PdfMetafileSkia::GetVectorCanvasForNewPage( | |
| 148 const gfx::Size& page_size, const gfx::Rect& content_area, | |
| 149 const float& scale_factor) { | |
| 150 return this->StartPage(page_size, content_area, scale_factor) | |
| 151 ? skia::SharePtr(data_->recorder_->getRecordingCanvas()) | |
| 152 : skia::RefPtr<skia::VectorCanvas>(/*NULL*/); | |
| 52 } | 153 } |
| 53 | 154 |
| 54 SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas( | 155 SkBaseDevice* PdfMetafileSkia::StartPageForVectorCanvas( |
| 55 const gfx::Size& page_size, const gfx::Rect& content_area, | 156 const gfx::Size& page_size, const gfx::Rect& content_area, |
| 56 const float& scale_factor) { | 157 const float& scale_factor) { |
| 57 DCHECK(!page_outstanding_); | 158 NOTREACHED(); // Skia has deprecated the use of SkPDFDevice. |
| 58 page_outstanding_ = true; | 159 return NULL; |
| 59 | 160 } |
| 60 // Adjust for the margins and apply the scale factor. | 161 |
| 61 SkMatrix transform; | |
| 62 transform.setTranslate(SkIntToScalar(content_area.x()), | |
| 63 SkIntToScalar(content_area.y())); | |
| 64 transform.preScale(SkFloatToScalar(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 skia::RefPtr<SkPDFDevice> pdf_device = | |
| 71 skia::AdoptRef(new skia::VectorPlatformDeviceSkia( | |
| 72 pdf_page_size, pdf_content_size, transform)); | |
| 73 data_->current_page_ = pdf_device; | |
| 74 return pdf_device.get(); | |
| 75 } | |
| 76 | 162 |
| 77 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, | 163 bool PdfMetafileSkia::StartPage(const gfx::Size& page_size, |
| 78 const gfx::Rect& content_area, | 164 const gfx::Rect& content_area, |
| 79 const float& scale_factor) { | 165 const float& scale_factor) { |
| 80 NOTREACHED(); | 166 if (data_->PageOutstanding()) |
| 81 return false; | 167 this->FinishPage(); |
| 168 | |
| 169 data_->recorder_.reset(SkNEW(SkPictureRecorder)); | |
| 170 SkSize sk_page_size = ToSkSize(page_size); | |
| 171 data_->pages_.push_back(Page(sk_page_size, ToSkRect(content_area))); | |
| 172 | |
| 173 SkCanvas* recordingCanvas = data_->recorder_->beginRecording( | |
| 174 sk_page_size.width(), sk_page_size.height(), NULL, 0); | |
| 175 // recordingCanvas is owned by the data_->recorder_. No ref() necessary. | |
| 176 if (!recordingCanvas) | |
| 177 return false; | |
| 178 recordingCanvas->scale(scale_factor, scale_factor); | |
| 179 return true; | |
| 82 } | 180 } |
| 83 | 181 |
| 84 bool PdfMetafileSkia::FinishPage() { | 182 bool PdfMetafileSkia::FinishPage() { |
| 85 DCHECK(data_->current_page_.get()); | 183 if (!data_->PageOutstanding()) |
| 86 | 184 return false; |
| 87 data_->pdf_doc_.appendPage(data_->current_page_.get()); | 185 DCHECK(!(data_->pages_.back().content_.get())); |
| 88 page_outstanding_ = false; | 186 data_->pages_.back().content_ = |
| 187 skia::AdoptRef(data_->recorder_->endRecording()); | |
| 188 data_->recorder_.reset(); | |
| 89 return true; | 189 return true; |
| 90 } | 190 } |
| 91 | 191 |
| 92 bool PdfMetafileSkia::FinishDocument() { | 192 bool PdfMetafileSkia::FinishDocument() { |
| 93 // Don't do anything if we've already set the data in InitFromData. | 193 // If we've already set the data in InitFromData, overwrite it. |
| 94 if (data_->pdf_stream_.getOffset()) | 194 if (data_->HasData()) |
| 95 return true; | 195 data_->pdf_data_.clear(); // Free up RAM first. |
| 96 | 196 |
| 97 if (page_outstanding_) | 197 if (data_->PageOutstanding()) |
| 98 FinishPage(); | 198 this->FinishPage(); |
| 99 | 199 |
| 100 data_->current_page_.clear(); | 200 SkDynamicMemoryWStream pdf_stream; |
| 101 | 201 skia::RefPtr<SkDocument> pdf_doc = |
| 102 int font_counts[SkAdvancedTypefaceMetrics::kOther_Font + 2]; | 202 skia::AdoptRef(SkDocument::CreatePDF(&pdf_stream)); |
| 103 data_->pdf_doc_.getCountOfFontTypes(font_counts); | 203 for (const auto& page : data_->pages_) { |
| 104 for (int type = 0; | 204 SkCanvas* canvas = |
| 105 type <= SkAdvancedTypefaceMetrics::kOther_Font + 1; | 205 pdf_doc->beginPage(page.page_size_.width(), page.page_size_.height(), |
| 106 type++) { | 206 &(page.content_area_)); |
|
Vitaly Buka (NO REVIEWS)
2014/11/04 19:23:55
just &page.content_area_
hal.canary
2014/11/16 21:29:28
Done.
| |
| 107 for (int count = 0; count < font_counts[type]; count++) { | 207 canvas->drawPicture(page.content_.get()); |
| 108 UMA_HISTOGRAM_ENUMERATION( | 208 pdf_doc->endPage(); |
| 109 "PrintPreview.FontType", type, | |
| 110 SkAdvancedTypefaceMetrics::kOther_Font + 2); | |
| 111 } | |
| 112 } | 209 } |
| 113 | 210 if (!pdf_doc->close()) |
| 114 return data_->pdf_doc_.emitPDF(&data_->pdf_stream_); | 211 return false; |
| 212 | |
| 213 data_->pdf_data_ = skia::AdoptRef(pdf_stream.detachAsStream()); | |
| 214 return true; | |
| 115 } | 215 } |
| 116 | 216 |
| 117 uint32 PdfMetafileSkia::GetDataSize() const { | 217 uint32 PdfMetafileSkia::GetDataSize() const { |
| 118 return base::checked_cast<uint32>(data_->pdf_stream_.getOffset()); | 218 if (!data_->pdf_data_) |
| 219 return 0; | |
| 220 return base::checked_cast<uint32>(data_->pdf_data_->getLength()); | |
| 119 } | 221 } |
| 120 | 222 |
| 121 bool PdfMetafileSkia::GetData(void* dst_buffer, | 223 bool PdfMetafileSkia::GetData(void* dst_buffer, |
| 122 uint32 dst_buffer_size) const { | 224 uint32 dst_buffer_size) const { |
| 123 if (dst_buffer_size < GetDataSize()) | 225 if (!data_->pdf_data_) |
| 124 return false; | 226 return false; |
| 125 | 227 return WriteAssetToBuffer(data_->pdf_data_.get(), dst_buffer, |
| 126 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 228 base::checked_cast<size_t>(dst_buffer_size)); |
| 127 memcpy(dst_buffer, data->bytes(), dst_buffer_size); | |
| 128 return true; | |
| 129 } | 229 } |
| 130 | 230 |
| 131 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 231 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 | 232 return ToGfxRect(data_->pages_[page_number].content_area_); |
| 133 // SkPDFDocument. | |
| 134 NOTIMPLEMENTED(); | |
| 135 return gfx::Rect(); | |
| 136 } | 233 } |
| 137 | 234 |
| 138 unsigned int PdfMetafileSkia::GetPageCount() const { | 235 unsigned int PdfMetafileSkia::GetPageCount() const { |
| 139 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. | 236 return base::checked_cast<int>(data_->PageCount()); |
| 140 NOTIMPLEMENTED(); | |
| 141 return 0; | |
| 142 } | 237 } |
| 143 | 238 |
| 144 gfx::NativeDrawingContext PdfMetafileSkia::context() const { | 239 gfx::NativeDrawingContext PdfMetafileSkia::context() const { |
| 145 NOTREACHED(); | 240 NOTREACHED(); |
| 146 return NULL; | 241 return NULL; |
| 147 } | 242 } |
| 148 | 243 |
| 149 #if defined(OS_WIN) | 244 #if defined(OS_WIN) |
| 150 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, | 245 bool PdfMetafileSkia::Playback(gfx::NativeDrawingContext hdc, |
| 151 const RECT* rect) const { | 246 const RECT* rect) const { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 163 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, | 258 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
| 164 the drawing of the PDF into the canvas may result in a rasterized output. | 259 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 | 260 PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
| 166 should do something like the following CL in PluginInstance::PrintPDFOutput: | 261 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 | 262 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc |
| 168 */ | 263 */ |
| 169 bool PdfMetafileSkia::RenderPage(unsigned int page_number, | 264 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
| 170 CGContextRef context, | 265 CGContextRef context, |
| 171 const CGRect rect, | 266 const CGRect rect, |
| 172 const MacRenderPageParams& params) const { | 267 const MacRenderPageParams& params) const { |
| 173 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 268 DCHECK_GT(GetDataSize(), 0U); |
| 174 if (data_->pdf_cg_.GetDataSize() == 0) { | 269 if (data_->pdf_cg_.GetDataSize() == 0) { |
| 175 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 270 if (GetDataSize() == 0) |
| 176 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); | 271 return false; |
| 272 size_t length = data_->pdf_data_->getLength(); | |
| 273 scoped_ptr<uint8_t[]> buffer(new uint8_t[length]); | |
|
Vitaly Buka (NO REVIEWS)
2014/11/04 19:23:55
just std::vector<uint8_t> buffer(length)?
hal.canary
2014/11/16 21:29:28
Done.
| |
| 274 (void)WriteAssetToBuffer(data_->pdf_data_.get(), buffer.get(), length); | |
| 275 data_->pdf_cg_.InitFromData(buffer.get(), | |
| 276 base::checked_cast<uint32>(length)); | |
| 177 } | 277 } |
| 178 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 278 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
| 179 } | 279 } |
| 180 #endif | 280 #endif |
| 181 | 281 |
| 282 bool PdfMetafileSkia::SaveTo(base::File* file) const { | |
| 283 DCHECK(file); | |
| 284 if (GetDataSize() == 0U) | |
| 285 return false; | |
| 286 DCHECK(data_->pdf_data_.get()); | |
| 287 SkStreamAsset* asset = data_->pdf_data_.get(); | |
| 288 DCHECK_EQ(asset->getPosition(), 0U); | |
| 289 | |
| 290 char buffer[4096]; | |
|
Vitaly Buka (NO REVIEWS)
2014/11/04 19:23:55
maybe std::vector for 1Mb?
hal.canary
2014/11/16 21:29:28
Done.
| |
| 291 do { | |
| 292 size_t read_size = asset->read(buffer, sizeof(buffer)); | |
|
Vitaly Buka (NO REVIEWS)
2014/11/04 19:23:55
read_size == 0 ?
hal.canary
2014/11/16 21:29:28
Done.
| |
| 293 DCHECK_GE(sizeof(buffer), read_size); | |
| 294 if (!file->WriteAtCurrentPos(buffer, base::checked_cast<int>(read_size))) { | |
| 295 (void)asset->rewind(); | |
| 296 return false; | |
| 297 } | |
| 298 } while (!asset->isAtEnd()); | |
| 299 | |
| 300 (void)asset->rewind(); | |
| 301 return true; | |
| 302 } | |
| 303 | |
| 182 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 304 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
| 305 | |
| 183 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 306 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
| 184 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 307 DCHECK_GT(GetDataSize(), 0U); |
| 185 | 308 |
| 186 if (fd.fd < 0) { | 309 if (fd.fd < 0) { |
| 187 DLOG(ERROR) << "Invalid file descriptor!"; | 310 DLOG(ERROR) << "Invalid file descriptor!"; |
| 188 return false; | 311 return false; |
| 189 } | 312 } |
| 190 base::File file(fd.fd); | 313 base::File file(fd.fd); |
| 191 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 314 bool result = this->SaveTo(&file); |
| 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; | 315 DLOG_IF(ERROR, !result) << "Failed to save file with fd " << fd.fd; |
| 196 | 316 |
| 197 if (!fd.auto_close) | 317 if (!fd.auto_close) |
| 198 file.TakePlatformFile(); | 318 file.TakePlatformFile(); |
| 199 return result; | 319 return result; |
| 200 } | 320 } |
| 201 #endif | 321 #endif |
| 202 | 322 |
| 203 PdfMetafileSkia::PdfMetafileSkia() | 323 PdfMetafileSkia::PdfMetafileSkia() : data_(new PdfMetafileSkiaData) { |
| 204 : data_(new PdfMetafileSkiaData), | 324 DCHECK(!data_->PageOutstanding()); |
| 205 page_outstanding_(false) { | |
| 206 } | 325 } |
| 207 | 326 |
| 208 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { | 327 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
| 209 scoped_ptr<PdfMetafileSkia> metafile; | 328 // If we only ever need the metafile for the last page, should we |
| 210 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); | 329 // only keep a handle on one SkPicture? |
| 211 if (!pdf_doc.appendPage(data_->current_page_.get())) | 330 DCHECK_NE(data_->PageCount(), 0U); |
| 331 DCHECK(!data_->PageOutstanding()); | |
| 332 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); | |
| 333 | |
| 334 if (data_->PageCount() == 0) | |
| 212 return metafile.Pass(); | 335 return metafile.Pass(); |
| 213 | 336 |
| 214 SkDynamicMemoryWStream pdf_stream; | 337 const Page* page = data_->GetPage(data_->PageCount() - 1); |
| 215 if (!pdf_doc.emitPDF(&pdf_stream)) | 338 if (!page) |
| 216 return metafile.Pass(); | 339 return metafile.Pass(); |
| 217 | 340 |
| 218 SkAutoDataUnref data_copy(pdf_stream.copyToData()); | 341 metafile->data_->pages_.push_back(*page); // Copy page data; |
| 219 if (data_copy->size() == 0) | 342 // Should increment refcnt on page->content_. |
| 220 return scoped_ptr<PdfMetafileSkia>(); | |
| 221 | 343 |
| 222 metafile.reset(new PdfMetafileSkia); | 344 if (!metafile->FinishDocument()) // Generate PDF. |
| 223 if (!metafile->InitFromData(data_copy->bytes(), | |
| 224 base::checked_cast<uint32>(data_copy->size()))) { | |
| 225 metafile.reset(); | 345 metafile.reset(); |
| 226 } | 346 |
| 227 return metafile.Pass(); | 347 return metafile.Pass(); |
| 228 } | 348 } |
| 229 | 349 |
| 230 } // namespace printing | 350 } // namespace printing |
| OLD | NEW |