OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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_mac.h" | 5 #include "printing/pdf_metafile_mac.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/mac/scoped_cftyperef.h" | 9 #include "base/mac/scoped_cftyperef.h" |
10 #include "base/sys_string_conversions.h" | 10 #include "base/sys_string_conversions.h" |
11 #include "ui/gfx/rect.h" | 11 #include "ui/gfx/rect.h" |
12 | 12 |
13 using base::mac::ScopedCFTypeRef; | 13 using base::mac::ScopedCFTypeRef; |
14 | 14 |
15 namespace printing { | 15 namespace printing { |
16 | 16 |
17 PdfMetafile::PdfMetafile() | 17 PdfMetafile::PdfMetafile() |
18 : page_is_open_(false) { | 18 : page_is_open_(false) { |
19 } | 19 } |
20 | 20 |
21 PdfMetafile::~PdfMetafile() {} | 21 PdfMetafile::~PdfMetafile() {} |
22 | 22 |
23 CGContextRef PdfMetafile::Init() { | 23 bool PdfMetafile::Init() { |
24 // Ensure that Init hasn't already been called. | 24 // Ensure that Init hasn't already been called. |
25 DCHECK(!context_.get()); | 25 DCHECK(!context_.get()); |
26 DCHECK(!pdf_data_.get()); | 26 DCHECK(!pdf_data_.get()); |
27 | 27 |
28 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); | 28 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); |
29 if (!pdf_data_.get()) { | 29 if (!pdf_data_.get()) { |
30 LOG(ERROR) << "Failed to create pdf data for metafile"; | 30 LOG(ERROR) << "Failed to create pdf data for metafile"; |
31 return NULL; | 31 return false; |
32 } | 32 } |
33 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( | 33 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( |
34 CGDataConsumerCreateWithCFData(pdf_data_)); | 34 CGDataConsumerCreateWithCFData(pdf_data_)); |
35 if (!pdf_consumer.get()) { | 35 if (!pdf_consumer.get()) { |
36 LOG(ERROR) << "Failed to create data consumer for metafile"; | 36 LOG(ERROR) << "Failed to create data consumer for metafile"; |
37 pdf_data_.reset(NULL); | 37 pdf_data_.reset(NULL); |
38 return NULL; | 38 return false; |
39 } | 39 } |
40 context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL)); | 40 context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL)); |
41 if (!context_.get()) { | 41 if (!context_.get()) { |
42 LOG(ERROR) << "Failed to create pdf context for metafile"; | 42 LOG(ERROR) << "Failed to create pdf context for metafile"; |
43 pdf_data_.reset(NULL); | 43 pdf_data_.reset(NULL); |
44 } | 44 } |
45 | 45 |
46 return context_.get(); | 46 return true; |
47 } | 47 } |
48 | 48 |
49 bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { | 49 bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { |
50 DCHECK(!context_.get()); | 50 DCHECK(!context_.get()); |
51 DCHECK(!pdf_data_.get()); | 51 DCHECK(!pdf_data_.get()); |
52 | 52 |
53 if (!src_buffer || src_buffer_size == 0) { | 53 if (!src_buffer || src_buffer_size == 0) { |
54 return false; | 54 return false; |
55 } | 55 } |
56 | 56 |
(...skipping 20 matching lines...) Expand all Loading... |
77 // Flip the context. | 77 // Flip the context. |
78 CGContextTranslateCTM(context_, 0, height); | 78 CGContextTranslateCTM(context_, 0, height); |
79 CGContextScaleCTM(context_, scale_factor, -scale_factor); | 79 CGContextScaleCTM(context_, scale_factor, -scale_factor); |
80 | 80 |
81 // Move the context to origin. | 81 // Move the context to origin. |
82 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y()); | 82 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y()); |
83 | 83 |
84 return context_.get(); | 84 return context_.get(); |
85 } | 85 } |
86 | 86 |
87 void PdfMetafile::FinishPage() { | 87 bool PdfMetafile::FinishPage() { |
88 DCHECK(context_.get()); | 88 DCHECK(context_.get()); |
89 DCHECK(page_is_open_); | 89 DCHECK(page_is_open_); |
90 | 90 |
91 CGContextRestoreGState(context_); | 91 CGContextRestoreGState(context_); |
92 CGContextEndPage(context_); | 92 CGContextEndPage(context_); |
93 page_is_open_ = false; | 93 page_is_open_ = false; |
| 94 return true; |
94 } | 95 } |
95 | 96 |
96 void PdfMetafile::Close() { | 97 void PdfMetafile::Close() { |
97 DCHECK(context_.get()); | 98 DCHECK(context_.get()); |
98 DCHECK(!page_is_open_); | 99 DCHECK(!page_is_open_); |
99 | 100 |
100 #ifndef NDEBUG | 101 #ifndef NDEBUG |
101 // Check that the context will be torn down properly; if it's not, pdf_data_ | 102 // Check that the context will be torn down properly; if it's not, pdf_data_ |
102 // will be incomplete and generate invalid PDF files/documents. | 103 // will be incomplete and generate invalid PDF files/documents. |
103 if (context_.get()) { | 104 if (context_.get()) { |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 | 231 |
231 if (!pdf_doc_.get()) { | 232 if (!pdf_doc_.get()) { |
232 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( | 233 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
233 CGDataProviderCreateWithCFData(pdf_data_)); | 234 CGDataProviderCreateWithCFData(pdf_data_)); |
234 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); | 235 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
235 } | 236 } |
236 return pdf_doc_.get(); | 237 return pdf_doc_.get(); |
237 } | 238 } |
238 | 239 |
239 } // namespace printing | 240 } // namespace printing |
OLD | NEW |