Chromium Code Reviews| 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 |
| 57 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size)); | 58 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size)); |
| 58 CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer), | 59 CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer), |
| 59 src_buffer_size); | 60 src_buffer_size); |
| 60 | 61 |
| 61 return true; | 62 return true; |
| 62 } | 63 } |
| 63 | 64 |
| 64 CGContextRef PdfMetafile::StartPage(const gfx::Size& page_size, | 65 CGContextRef PdfMetafile::StartPage(const gfx::Size& page_size, |
| 65 const gfx::Point& content_origin, const float& scale_factor) { | 66 const gfx::Point& content_origin, const float& scale_factor) { |
| 66 DCHECK(context_.get()); | 67 DCHECK(context_.get()); |
| 67 DCHECK(!page_is_open_); | 68 DCHECK(!page_is_open_); |
| 68 | 69 |
| 70 DLOG(INFO) << "size: " << page_size.height(); | |
|
vandebo (ex-Chrome)
2011/03/15 20:33:01
Remove
dpapad
2011/03/15 21:41:07
Done.
| |
| 69 double height = page_size.height(); | 71 double height = page_size.height(); |
| 70 double width = page_size.width(); | 72 double width = page_size.width(); |
| 71 | 73 |
| 72 CGRect bounds = CGRectMake(0, 0, width, height); | 74 CGRect bounds = CGRectMake(0, 0, width, height); |
| 73 CGContextBeginPage(context_, &bounds); | 75 CGContextBeginPage(context_, &bounds); |
| 74 page_is_open_ = true; | 76 page_is_open_ = true; |
| 75 CGContextSaveGState(context_); | 77 CGContextSaveGState(context_); |
| 76 | 78 |
| 77 // Flip the context. | 79 // Flip the context. |
| 78 CGContextTranslateCTM(context_, 0, height); | 80 CGContextTranslateCTM(context_, 0, height); |
| 79 CGContextScaleCTM(context_, scale_factor, -scale_factor); | 81 CGContextScaleCTM(context_, scale_factor, -scale_factor); |
| 80 | 82 |
| 81 // Move the context to origin. | 83 // Move the context to origin. |
| 82 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y()); | 84 CGContextTranslateCTM(context_, content_origin.x(), content_origin.y()); |
| 83 | 85 |
| 84 return context_.get(); | 86 return context_.get(); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void PdfMetafile::FinishPage() { | 89 bool PdfMetafile::FinishPage() { |
| 88 DCHECK(context_.get()); | 90 DCHECK(context_.get()); |
| 89 DCHECK(page_is_open_); | 91 DCHECK(page_is_open_); |
| 90 | 92 |
| 91 CGContextRestoreGState(context_); | 93 CGContextRestoreGState(context_); |
| 92 CGContextEndPage(context_); | 94 CGContextEndPage(context_); |
| 93 page_is_open_ = false; | 95 page_is_open_ = false; |
| 96 return true; | |
| 94 } | 97 } |
| 95 | 98 |
| 96 void PdfMetafile::Close() { | 99 bool PdfMetafile::Close() { |
| 97 DCHECK(context_.get()); | 100 DCHECK(context_.get()); |
| 98 DCHECK(!page_is_open_); | 101 DCHECK(!page_is_open_); |
| 99 | 102 |
| 100 #ifndef NDEBUG | 103 #ifndef NDEBUG |
| 101 // Check that the context will be torn down properly; if it's not, pdf_data_ | 104 // 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. | 105 // will be incomplete and generate invalid PDF files/documents. |
| 103 if (context_.get()) { | 106 if (context_.get()) { |
| 104 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1; | 107 CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1; |
| 105 if (extra_retain_count > 0) { | 108 if (extra_retain_count > 0) { |
| 106 LOG(ERROR) << "Metafile context has " << extra_retain_count | 109 LOG(ERROR) << "Metafile context has " << extra_retain_count |
| 107 << " extra retain(s) on Close"; | 110 << " extra retain(s) on Close"; |
| 108 } | 111 } |
| 109 } | 112 } |
| 110 #endif | 113 #endif |
| 111 CGPDFContextClose(context_.get()); | 114 CGPDFContextClose(context_.get()); |
| 112 context_.reset(NULL); | 115 context_.reset(NULL); |
| 116 return true; | |
| 113 } | 117 } |
| 114 | 118 |
| 115 bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context, | 119 bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context, |
| 116 const CGRect rect, bool shrink_to_fit, | 120 const CGRect rect, bool shrink_to_fit, |
| 117 bool stretch_to_fit, | 121 bool stretch_to_fit, |
| 118 bool center_horizontally, | 122 bool center_horizontally, |
| 119 bool center_vertically) const { | 123 bool center_vertically) const { |
| 120 CGPDFDocumentRef pdf_doc = GetPDFDocument(); | 124 CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 121 if (!pdf_doc) { | 125 if (!pdf_doc) { |
| 122 LOG(ERROR) << "Unable to create PDF document from data"; | 126 LOG(ERROR) << "Unable to create PDF document from data"; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 | 234 |
| 231 if (!pdf_doc_.get()) { | 235 if (!pdf_doc_.get()) { |
| 232 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( | 236 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
| 233 CGDataProviderCreateWithCFData(pdf_data_)); | 237 CGDataProviderCreateWithCFData(pdf_data_)); |
| 234 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); | 238 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
| 235 } | 239 } |
| 236 return pdf_doc_.get(); | 240 return pdf_doc_.get(); |
| 237 } | 241 } |
| 238 | 242 |
| 239 } // namespace printing | 243 } // namespace printing |
| OLD | NEW |