Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: printing/pdf_metafile_mac.cc

Issue 6611032: Unifying NativeMetafile class interface (as much as possible) for Linux, Mac, Win (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Making virtual methods not virtual (for clang bots) Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « printing/pdf_metafile_mac.h ('k') | printing/pdf_metafile_mac_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "ui/gfx/size.h"
12 13
13 using base::mac::ScopedCFTypeRef; 14 using base::mac::ScopedCFTypeRef;
14 15
15 namespace printing { 16 namespace printing {
16 17
17 PdfMetafile::PdfMetafile() 18 PdfMetafile::PdfMetafile()
18 : page_is_open_(false) { 19 : page_is_open_(false) {
19 } 20 }
20 21
21 PdfMetafile::~PdfMetafile() {} 22 PdfMetafile::~PdfMetafile() {}
22 23
23 CGContextRef PdfMetafile::Init() { 24 bool PdfMetafile::Init() {
24 // Ensure that Init hasn't already been called. 25 // Ensure that Init hasn't already been called.
25 DCHECK(!context_.get()); 26 DCHECK(!context_.get());
26 DCHECK(!pdf_data_.get()); 27 DCHECK(!pdf_data_.get());
27 28
28 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); 29 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0));
29 if (!pdf_data_.get()) { 30 if (!pdf_data_.get()) {
30 LOG(ERROR) << "Failed to create pdf data for metafile"; 31 LOG(ERROR) << "Failed to create pdf data for metafile";
31 return NULL; 32 return false;
32 } 33 }
33 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( 34 ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer(
34 CGDataConsumerCreateWithCFData(pdf_data_)); 35 CGDataConsumerCreateWithCFData(pdf_data_));
35 if (!pdf_consumer.get()) { 36 if (!pdf_consumer.get()) {
36 LOG(ERROR) << "Failed to create data consumer for metafile"; 37 LOG(ERROR) << "Failed to create data consumer for metafile";
37 pdf_data_.reset(NULL); 38 pdf_data_.reset(NULL);
38 return NULL; 39 return false;
39 } 40 }
40 context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL)); 41 context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL));
41 if (!context_.get()) { 42 if (!context_.get()) {
42 LOG(ERROR) << "Failed to create pdf context for metafile"; 43 LOG(ERROR) << "Failed to create pdf context for metafile";
43 pdf_data_.reset(NULL); 44 pdf_data_.reset(NULL);
44 } 45 }
45 46
46 return context_.get(); 47 return true;
47 } 48 }
48 49
49 bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) { 50 bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) {
50 DCHECK(!context_.get()); 51 DCHECK(!context_.get());
51 DCHECK(!pdf_data_.get()); 52 DCHECK(!pdf_data_.get());
52 53
53 if (!src_buffer || src_buffer_size == 0) { 54 if (!src_buffer || src_buffer_size == 0) {
54 return false; 55 return false;
55 } 56 }
56 57
(...skipping 20 matching lines...) Expand all
77 // Flip the context. 78 // Flip the context.
78 CGContextTranslateCTM(context_, 0, height); 79 CGContextTranslateCTM(context_, 0, height);
79 CGContextScaleCTM(context_, scale_factor, -scale_factor); 80 CGContextScaleCTM(context_, scale_factor, -scale_factor);
80 81
81 // Move the context to origin. 82 // Move the context to origin.
82 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y()); 83 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y());
83 84
84 return context_.get(); 85 return context_.get();
85 } 86 }
86 87
87 void PdfMetafile::FinishPage() { 88 bool PdfMetafile::FinishPage() {
88 DCHECK(context_.get()); 89 DCHECK(context_.get());
89 DCHECK(page_is_open_); 90 DCHECK(page_is_open_);
90 91
91 CGContextRestoreGState(context_); 92 CGContextRestoreGState(context_);
92 CGContextEndPage(context_); 93 CGContextEndPage(context_);
93 page_is_open_ = false; 94 page_is_open_ = false;
95 return true;
94 } 96 }
95 97
96 void PdfMetafile::Close() { 98 bool PdfMetafile::Close() {
97 DCHECK(context_.get()); 99 DCHECK(context_.get());
98 DCHECK(!page_is_open_); 100 DCHECK(!page_is_open_);
99 101
100 #ifndef NDEBUG 102 #ifndef NDEBUG
101 // Check that the context will be torn down properly; if it's not, pdf_data_ 103 // 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. 104 // will be incomplete and generate invalid PDF files/documents.
103 if (context_.get()) { 105 if (context_.get()) {
104 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1; 106 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1;
105 if (extra_retain_count > 0) { 107 if (extra_retain_count > 0) {
106 LOG(ERROR) << "Metafile context has " << extra_retain_count 108 LOG(ERROR) << "Metafile context has " << extra_retain_count
107 << " extra retain(s) on Close"; 109 << " extra retain(s) on Close";
108 } 110 }
109 } 111 }
110 #endif 112 #endif
111 CGPDFContextClose(context_.get()); 113 CGPDFContextClose(context_.get());
112 context_.reset(NULL); 114 context_.reset(NULL);
115 return true;
113 } 116 }
114 117
115 bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context, 118 bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context,
116 const CGRect rect, bool shrink_to_fit, 119 const CGRect rect, bool shrink_to_fit,
117 bool stretch_to_fit, 120 bool stretch_to_fit,
118 bool center_horizontally, 121 bool center_horizontally,
119 bool center_vertically) const { 122 bool center_vertically) const {
120 CGPDFDocumentRef pdf_doc = GetPDFDocument(); 123 CGPDFDocumentRef pdf_doc = GetPDFDocument();
121 if (!pdf_doc) { 124 if (!pdf_doc) {
122 LOG(ERROR) << "Unable to create PDF document from data"; 125 LOG(ERROR) << "Unable to create PDF document from data";
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 219
217 std::string path_string = file_path.value(); 220 std::string path_string = file_path.value();
218 ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation( 221 ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation(
219 kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()), 222 kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()),
220 path_string.length(), false)); 223 path_string.length(), false));
221 SInt32 error_code; 224 SInt32 error_code;
222 CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code); 225 CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code);
223 return error_code == 0; 226 return error_code == 0;
224 } 227 }
225 228
229 CGContextRef PdfMetafile::context() const {
230 return context_.get();
231 }
232
226 CGPDFDocumentRef PdfMetafile::GetPDFDocument() const { 233 CGPDFDocumentRef PdfMetafile::GetPDFDocument() const {
227 // Make sure that we have data, and that it's not being modified any more. 234 // Make sure that we have data, and that it's not being modified any more.
228 DCHECK(pdf_data_.get()); 235 DCHECK(pdf_data_.get());
229 DCHECK(!context_.get()); 236 DCHECK(!context_.get());
230 237
231 if (!pdf_doc_.get()) { 238 if (!pdf_doc_.get()) {
232 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( 239 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider(
233 CGDataProviderCreateWithCFData(pdf_data_)); 240 CGDataProviderCreateWithCFData(pdf_data_));
234 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); 241 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider));
235 } 242 }
236 return pdf_doc_.get(); 243 return pdf_doc_.get();
237 } 244 }
238 245
239 } // namespace printing 246 } // namespace printing
OLDNEW
« no previous file with comments | « printing/pdf_metafile_mac.h ('k') | printing/pdf_metafile_mac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698