OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/eintr_wrapper.h" | 7 #include "base/eintr_wrapper.h" |
8 #include "base/file_descriptor_posix.h" | 8 #include "base/file_descriptor_posix.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "skia/ext/vector_platform_device_skia.h" | 12 #include "skia/ext/vector_platform_device_skia.h" |
| 13 #include "third_party/skia/include/core/SkData.h" |
13 #include "third_party/skia/include/core/SkRefCnt.h" | 14 #include "third_party/skia/include/core/SkRefCnt.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 15 #include "third_party/skia/include/core/SkScalar.h" |
15 #include "third_party/skia/include/core/SkStream.h" | 16 #include "third_party/skia/include/core/SkStream.h" |
16 #include "third_party/skia/include/core/SkTypeface.h" | 17 #include "third_party/skia/include/core/SkTypeface.h" |
17 #include "third_party/skia/include/pdf/SkPDFDevice.h" | 18 #include "third_party/skia/include/pdf/SkPDFDevice.h" |
18 #include "third_party/skia/include/pdf/SkPDFDocument.h" | 19 #include "third_party/skia/include/pdf/SkPDFDocument.h" |
19 #include "third_party/skia/include/pdf/SkPDFFont.h" | 20 #include "third_party/skia/include/pdf/SkPDFFont.h" |
20 #include "third_party/skia/include/pdf/SkPDFPage.h" | 21 #include "third_party/skia/include/pdf/SkPDFPage.h" |
21 #include "ui/gfx/point.h" | 22 #include "ui/gfx/point.h" |
22 #include "ui/gfx/rect.h" | 23 #include "ui/gfx/rect.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 119 |
119 uint32 PdfMetafileSkia::GetDataSize() const { | 120 uint32 PdfMetafileSkia::GetDataSize() const { |
120 return data_->pdf_stream_.getOffset(); | 121 return data_->pdf_stream_.getOffset(); |
121 } | 122 } |
122 | 123 |
123 bool PdfMetafileSkia::GetData(void* dst_buffer, | 124 bool PdfMetafileSkia::GetData(void* dst_buffer, |
124 uint32 dst_buffer_size) const { | 125 uint32 dst_buffer_size) const { |
125 if (dst_buffer_size < GetDataSize()) | 126 if (dst_buffer_size < GetDataSize()) |
126 return false; | 127 return false; |
127 | 128 |
128 memcpy(dst_buffer, data_->pdf_stream_.getStream(), dst_buffer_size); | 129 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 130 memcpy(dst_buffer, data.bytes(), dst_buffer_size); |
129 return true; | 131 return true; |
130 } | 132 } |
131 | 133 |
132 bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const { | 134 bool PdfMetafileSkia::SaveTo(const FilePath& file_path) const { |
133 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 135 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
134 if (file_util::WriteFile(file_path, data_->pdf_stream_.getStream(), | 136 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 137 if (file_util::WriteFile(file_path, |
| 138 reinterpret_cast<const char*>(data.data()), |
135 GetDataSize()) != static_cast<int>(GetDataSize())) { | 139 GetDataSize()) != static_cast<int>(GetDataSize())) { |
136 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); | 140 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); |
137 return false; | 141 return false; |
138 } | 142 } |
139 return true; | 143 return true; |
140 } | 144 } |
141 | 145 |
142 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 146 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
143 // TODO(vandebo) add a method to get the page size for a given page to | 147 // TODO(vandebo) add a method to get the page size for a given page to |
144 // SkPDFDocument. | 148 // SkPDFDocument. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_
instance.cc | 186 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_
instance.cc |
183 */ | 187 */ |
184 bool PdfMetafileSkia::RenderPage(unsigned int page_number, | 188 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
185 CGContextRef context, | 189 CGContextRef context, |
186 const CGRect rect, | 190 const CGRect rect, |
187 bool shrink_to_fit, | 191 bool shrink_to_fit, |
188 bool stretch_to_fit, | 192 bool stretch_to_fit, |
189 bool center_horizontally, | 193 bool center_horizontally, |
190 bool center_vertically) const { | 194 bool center_vertically) const { |
191 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 195 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
192 if (data_->pdf_cg_.GetDataSize() == 0) | 196 if (data_->pdf_cg_.GetDataSize() == 0) { |
193 data_->pdf_cg_.InitFromData(data_->pdf_stream_.getStream(), | 197 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
194 data_->pdf_stream_.getOffset()); | 198 data_->pdf_cg_.InitFromData(data.bytes(), data.size()); |
| 199 } |
195 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit, | 200 return data_->pdf_cg_.RenderPage(page_number, context, rect, shrink_to_fit, |
196 stretch_to_fit, center_horizontally, | 201 stretch_to_fit, center_horizontally, |
197 center_vertically); | 202 center_vertically); |
198 } | 203 } |
199 #endif | 204 #endif |
200 | 205 |
201 #if defined(OS_CHROMEOS) | 206 #if defined(OS_CHROMEOS) |
202 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 207 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
203 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 208 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
204 | 209 |
205 if (fd.fd < 0) { | 210 if (fd.fd < 0) { |
206 DLOG(ERROR) << "Invalid file descriptor!"; | 211 DLOG(ERROR) << "Invalid file descriptor!"; |
207 return false; | 212 return false; |
208 } | 213 } |
209 | 214 |
210 bool result = true; | 215 bool result = true; |
211 if (file_util::WriteFileDescriptor(fd.fd, data_->pdf_stream_.getStream(), | 216 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
| 217 if (file_util::WriteFileDescriptor(fd.fd, |
| 218 reinterpret_cast<const char*>(data.data()), |
212 GetDataSize()) != | 219 GetDataSize()) != |
213 static_cast<int>(GetDataSize())) { | 220 static_cast<int>(GetDataSize())) { |
214 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; | 221 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; |
215 result = false; | 222 result = false; |
216 } | 223 } |
217 | 224 |
218 if (fd.auto_close) { | 225 if (fd.auto_close) { |
219 if (HANDLE_EINTR(close(fd.fd)) < 0) { | 226 if (HANDLE_EINTR(close(fd.fd)) < 0) { |
220 DPLOG(WARNING) << "close"; | 227 DPLOG(WARNING) << "close"; |
221 result = false; | 228 result = false; |
222 } | 229 } |
223 } | 230 } |
224 return result; | 231 return result; |
225 } | 232 } |
226 #endif | 233 #endif |
227 | 234 |
228 PdfMetafileSkia::PdfMetafileSkia() | 235 PdfMetafileSkia::PdfMetafileSkia() |
229 : data_(new PdfMetafileSkiaData), | 236 : data_(new PdfMetafileSkiaData), |
230 draft_(false) {} | 237 draft_(false) {} |
231 | 238 |
232 void PdfMetafileSkia::set_draft(bool draft) const { | 239 void PdfMetafileSkia::set_draft(bool draft) const { |
233 draft_ = draft; | 240 draft_ = draft; |
234 } | 241 } |
235 } // namespace printing | 242 } // namespace printing |
OLD | NEW |