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