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" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 bool PdfMetafileSkia::GetData(void* dst_buffer, | 121 bool PdfMetafileSkia::GetData(void* dst_buffer, |
122 uint32 dst_buffer_size) const { | 122 uint32 dst_buffer_size) const { |
123 if (dst_buffer_size < GetDataSize()) | 123 if (dst_buffer_size < GetDataSize()) |
124 return false; | 124 return false; |
125 | 125 |
126 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 126 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
127 memcpy(dst_buffer, data->bytes(), dst_buffer_size); | 127 memcpy(dst_buffer, data->bytes(), dst_buffer_size); |
128 return true; | 128 return true; |
129 } | 129 } |
130 | 130 |
131 bool PdfMetafileSkia::SaveTo(const base::FilePath& file_path) const { | |
132 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | |
133 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | |
134 if (base::WriteFile(file_path, | |
135 reinterpret_cast<const char*>(data->data()), | |
136 GetDataSize()) != static_cast<int>(GetDataSize())) { | |
137 DLOG(ERROR) << "Failed to save file " << file_path.value().c_str(); | |
138 return false; | |
139 } | |
140 return true; | |
141 } | |
142 | |
143 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { | 131 gfx::Rect PdfMetafileSkia::GetPageBounds(unsigned int page_number) const { |
144 // TODO(vandebo) add a method to get the page size for a given page to | 132 // TODO(vandebo) add a method to get the page size for a given page to |
145 // SkPDFDocument. | 133 // SkPDFDocument. |
146 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
147 return gfx::Rect(); | 135 return gfx::Rect(); |
148 } | 136 } |
149 | 137 |
150 unsigned int PdfMetafileSkia::GetPageCount() const { | 138 unsigned int PdfMetafileSkia::GetPageCount() const { |
151 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. | 139 // TODO(vandebo) add a method to get the number of pages to SkPDFDocument. |
152 NOTIMPLEMENTED(); | 140 NOTIMPLEMENTED(); |
(...skipping 10 matching lines...) Expand all Loading... | |
163 const RECT* rect) const { | 151 const RECT* rect) const { |
164 NOTREACHED(); | 152 NOTREACHED(); |
165 return false; | 153 return false; |
166 } | 154 } |
167 | 155 |
168 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { | 156 bool PdfMetafileSkia::SafePlayback(gfx::NativeDrawingContext hdc) const { |
169 NOTREACHED(); | 157 NOTREACHED(); |
170 return false; | 158 return false; |
171 } | 159 } |
172 | 160 |
173 HENHMETAFILE PdfMetafileSkia::emf() const { | |
174 NOTREACHED(); | |
175 return NULL; | |
176 } | |
177 #elif defined(OS_MACOSX) | 161 #elif defined(OS_MACOSX) |
178 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in | 162 /* TODO(caryclark): The set up of PluginInstance::PrintPDFOutput may result in |
179 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, | 163 rasterized output. Even if that flow uses PdfMetafileCg::RenderPage, |
180 the drawing of the PDF into the canvas may result in a rasterized output. | 164 the drawing of the PDF into the canvas may result in a rasterized output. |
181 PDFMetafileSkia::RenderPage should be not implemented as shown and instead | 165 PDFMetafileSkia::RenderPage should be not implemented as shown and instead |
182 should do something like the following CL in PluginInstance::PrintPDFOutput: | 166 should do something like the following CL in PluginInstance::PrintPDFOutput: |
183 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc | 167 http://codereview.chromium.org/7200040/diff/1/webkit/plugins/ppapi/ppapi_plugin_ instance.cc |
184 */ | 168 */ |
185 bool PdfMetafileSkia::RenderPage(unsigned int page_number, | 169 bool PdfMetafileSkia::RenderPage(unsigned int page_number, |
186 CGContextRef context, | 170 CGContextRef context, |
187 const CGRect rect, | 171 const CGRect rect, |
188 const MacRenderPageParams& params) const { | 172 const MacRenderPageParams& params) const { |
189 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 173 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
190 if (data_->pdf_cg_.GetDataSize() == 0) { | 174 if (data_->pdf_cg_.GetDataSize() == 0) { |
191 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 175 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); |
192 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); | 176 data_->pdf_cg_.InitFromData(data->bytes(), data->size()); |
193 } | 177 } |
194 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); | 178 return data_->pdf_cg_.RenderPage(page_number, context, rect, params); |
195 } | 179 } |
196 #endif | 180 #endif |
197 | 181 |
198 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 182 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
199 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { | 183 bool PdfMetafileSkia::SaveToFD(const base::FileDescriptor& fd) const { |
200 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); | 184 DCHECK_GT(data_->pdf_stream_.getOffset(), 0U); |
201 | 185 |
202 if (fd.fd < 0) { | 186 if (fd.fd < 0) { |
203 DLOG(ERROR) << "Invalid file descriptor!"; | 187 DLOG(ERROR) << "Invalid file descriptor!"; |
204 return false; | 188 return false; |
205 } | 189 } |
190 base::File file(fd.fd); | |
191 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | |
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; | |
206 | 196 |
207 bool result = true; | 197 if (!fd.auto_close) |
208 SkAutoDataUnref data(data_->pdf_stream_.copyToData()); | 198 file.TakePlatformFile(); |
209 if (base::WriteFileDescriptor(fd.fd, | |
210 reinterpret_cast<const char*>(data->data()), | |
211 GetDataSize()) != | |
212 static_cast<int>(GetDataSize())) { | |
213 DLOG(ERROR) << "Failed to save file with fd " << fd.fd; | |
214 result = false; | |
215 } | |
216 | |
217 if (fd.auto_close) { | |
218 if (IGNORE_EINTR(close(fd.fd)) < 0) { | |
219 DPLOG(WARNING) << "close"; | |
220 result = false; | |
221 } | |
222 } | |
223 return result; | 199 return result; |
224 } | 200 } |
225 #endif | 201 #endif |
226 | 202 |
227 PdfMetafileSkia::PdfMetafileSkia() | 203 PdfMetafileSkia::PdfMetafileSkia() |
228 : data_(new PdfMetafileSkiaData), | 204 : data_(new PdfMetafileSkiaData), |
229 page_outstanding_(false) { | 205 page_outstanding_(false) { |
230 } | 206 } |
231 | 207 |
232 PdfMetafileSkia* PdfMetafileSkia::GetMetafileForCurrentPage() { | 208 scoped_ptr<PdfMetafileSkia> PdfMetafileSkia::GetMetafileForCurrentPage() { |
233 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); | 209 SkPDFDocument pdf_doc(SkPDFDocument::kDraftMode_Flags); |
234 SkDynamicMemoryWStream pdf_stream; | 210 SkDynamicMemoryWStream pdf_stream; |
235 if (!pdf_doc.appendPage(data_->current_page_.get())) | 211 if (!pdf_doc.appendPage(data_->current_page_.get())) |
236 return NULL; | 212 return scoped_ptr<PdfMetafileSkia>(); |
237 | 213 |
238 if (!pdf_doc.emitPDF(&pdf_stream)) | 214 if (!pdf_doc.emitPDF(&pdf_stream)) |
239 return NULL; | 215 return scoped_ptr<PdfMetafileSkia>(); |
240 | 216 |
241 SkAutoDataUnref data(pdf_stream.copyToData()); | 217 SkAutoDataUnref data_copy(pdf_stream.copyToData()); |
242 if (data->size() == 0) | 218 if (data_copy->size() == 0) |
243 return NULL; | 219 return scoped_ptr<PdfMetafileSkia>(); |
244 | 220 |
245 PdfMetafileSkia* metafile = new PdfMetafileSkia; | 221 scoped_ptr<PdfMetafileSkia> metafile(new PdfMetafileSkia); |
Lei Zhang
2014/09/11 23:31:10
nit: Just put an empty scoper at the top and alway
Vitaly Buka (NO REVIEWS)
2014/09/12 01:54:00
Done.
| |
246 metafile->InitFromData(data->bytes(), | 222 metafile->InitFromData(data_copy->bytes(), |
247 base::checked_cast<uint32>(data->size())); | 223 base::checked_cast<uint32>(data_copy->size())); |
248 return metafile; | 224 return metafile.Pass(); |
249 } | 225 } |
250 | 226 |
251 } // namespace printing | 227 } // namespace printing |
OLD | NEW |