| 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 #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 Loading... |
| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 | 233 |
| 231 if (!pdf_doc_.get()) { | 234 if (!pdf_doc_.get()) { |
| 232 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( | 235 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
| 233 CGDataProviderCreateWithCFData(pdf_data_)); | 236 CGDataProviderCreateWithCFData(pdf_data_)); |
| 234 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); | 237 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
| 235 } | 238 } |
| 236 return pdf_doc_.get(); | 239 return pdf_doc_.get(); |
| 237 } | 240 } |
| 238 | 241 |
| 239 } // namespace printing | 242 } // namespace printing |
| OLD | NEW |