| Index: printing/pdf_metafile_mac.cc
|
| diff --git a/printing/pdf_metafile_mac.cc b/printing/pdf_metafile_mac.cc
|
| index ffd80e8f8d7c765119a5e669e30da4c20fb0a545..f05f6e6871d5a0256940ac50f8351c859e99f350 100644
|
| --- a/printing/pdf_metafile_mac.cc
|
| +++ b/printing/pdf_metafile_mac.cc
|
| @@ -9,6 +9,7 @@
|
| #include "base/mac/scoped_cftyperef.h"
|
| #include "base/sys_string_conversions.h"
|
| #include "ui/gfx/rect.h"
|
| +#include "ui/gfx/size.h"
|
|
|
| using base::mac::ScopedCFTypeRef;
|
|
|
| @@ -20,7 +21,7 @@ PdfMetafile::PdfMetafile()
|
|
|
| PdfMetafile::~PdfMetafile() {}
|
|
|
| -CGContextRef PdfMetafile::Init() {
|
| +bool PdfMetafile::Init() {
|
| // Ensure that Init hasn't already been called.
|
| DCHECK(!context_.get());
|
| DCHECK(!pdf_data_.get());
|
| @@ -28,14 +29,14 @@ CGContextRef PdfMetafile::Init() {
|
| pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0));
|
| if (!pdf_data_.get()) {
|
| LOG(ERROR) << "Failed to create pdf data for metafile";
|
| - return NULL;
|
| + return false;
|
| }
|
| ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer(
|
| CGDataConsumerCreateWithCFData(pdf_data_));
|
| if (!pdf_consumer.get()) {
|
| LOG(ERROR) << "Failed to create data consumer for metafile";
|
| pdf_data_.reset(NULL);
|
| - return NULL;
|
| + return false;
|
| }
|
| context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL));
|
| if (!context_.get()) {
|
| @@ -43,7 +44,7 @@ CGContextRef PdfMetafile::Init() {
|
| pdf_data_.reset(NULL);
|
| }
|
|
|
| - return context_.get();
|
| + return true;
|
| }
|
|
|
| bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) {
|
| @@ -61,36 +62,40 @@ bool PdfMetafile::Init(const void* src_buffer, uint32 src_buffer_size) {
|
| return true;
|
| }
|
|
|
| -CGContextRef PdfMetafile::StartPage(const gfx::Size& page_size,
|
| - const gfx::Point& content_origin, const float& scale_factor) {
|
| - DCHECK(context_.get());
|
| - DCHECK(!page_is_open_);
|
| -
|
| - double height = page_size.height();
|
| - double width = page_size.width();
|
| +uint32 PdfMetafile::GetDataSize() const {
|
| + // PDF data is only valid/complete once the context is released.
|
| + DCHECK(!context_);
|
|
|
| - CGRect bounds = CGRectMake(0, 0, width, height);
|
| - CGContextBeginPage(context_, &bounds);
|
| - page_is_open_ = true;
|
| - CGContextSaveGState(context_);
|
| + if (!pdf_data_)
|
| + return 0;
|
| + return static_cast<uint32>(CFDataGetLength(pdf_data_));
|
| +}
|
|
|
| - // Flip the context.
|
| - CGContextTranslateCTM(context_, 0, height);
|
| - CGContextScaleCTM(context_, scale_factor, -scale_factor);
|
| +bool PdfMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
|
| + // PDF data is only valid/complete once the context is released.
|
| + DCHECK(!context_);
|
| + DCHECK(pdf_data_);
|
| + DCHECK(dst_buffer);
|
| + DCHECK_GT(dst_buffer_size, 0U);
|
|
|
| - // Move the context to origin.
|
| - CGContextTranslateCTM(context_, content_origin.x(), content_origin.y());
|
| + uint32 data_size = GetDataSize();
|
| + if (dst_buffer_size > data_size) {
|
| + return false;
|
| + }
|
|
|
| - return context_.get();
|
| + CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size),
|
| + static_cast<UInt8*>(dst_buffer));
|
| + return true;
|
| }
|
|
|
| -void PdfMetafile::FinishPage() {
|
| +bool PdfMetafile::FinishPage() {
|
| DCHECK(context_.get());
|
| DCHECK(page_is_open_);
|
|
|
| CGContextRestoreGState(context_);
|
| CGContextEndPage(context_);
|
| page_is_open_ = false;
|
| + return true;
|
| }
|
|
|
| void PdfMetafile::Close() {
|
| @@ -112,6 +117,62 @@ void PdfMetafile::Close() {
|
| context_.reset(NULL);
|
| }
|
|
|
| +bool PdfMetafile::SaveTo(const FilePath& file_path) const {
|
| + DCHECK(pdf_data_.get());
|
| + DCHECK(!context_.get());
|
| +
|
| + std::string path_string = file_path.value();
|
| + ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation(
|
| + kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()),
|
| + path_string.length(), false));
|
| + SInt32 error_code;
|
| + CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code);
|
| + return error_code == 0;
|
| +}
|
| +
|
| +gfx::Rect PdfMetafile::GetPageBounds(unsigned int page_number) const {
|
| + CGPDFDocumentRef pdf_doc = GetPDFDocument();
|
| + if (!pdf_doc) {
|
| + LOG(ERROR) << "Unable to create PDF document from data";
|
| + return gfx::Rect();
|
| + }
|
| + if (page_number > GetPageCount()) {
|
| + LOG(ERROR) << "Invalid page number: " << page_number;
|
| + return gfx::Rect();
|
| + }
|
| + CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
|
| + CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox);
|
| + return gfx::Rect(page_rect);
|
| +}
|
| +
|
| +unsigned int PdfMetafile::GetPageCount() const {
|
| + CGPDFDocumentRef pdf_doc = GetPDFDocument();
|
| + return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0;
|
| +}
|
| +
|
| +CGContextRef PdfMetafile::StartPage(const gfx::Size& page_size,
|
| + const gfx::Point& content_origin, const float& scale_factor) {
|
| + DCHECK(context_.get());
|
| + DCHECK(!page_is_open_);
|
| +
|
| + double height = page_size.height();
|
| + double width = page_size.width();
|
| +
|
| + CGRect bounds = CGRectMake(0, 0, width, height);
|
| + CGContextBeginPage(context_, &bounds);
|
| + page_is_open_ = true;
|
| + CGContextSaveGState(context_);
|
| +
|
| + // Flip the context.
|
| + CGContextTranslateCTM(context_, 0, height);
|
| + CGContextScaleCTM(context_, scale_factor, -scale_factor);
|
| +
|
| + // Move the context to origin.
|
| + CGContextTranslateCTM(context_, content_origin.x(), content_origin.y());
|
| +
|
| + return context_.get();
|
| +}
|
| +
|
| bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context,
|
| const CGRect rect, bool shrink_to_fit,
|
| bool stretch_to_fit,
|
| @@ -164,65 +225,6 @@ bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context,
|
| return true;
|
| }
|
|
|
| -unsigned int PdfMetafile::GetPageCount() const {
|
| - CGPDFDocumentRef pdf_doc = GetPDFDocument();
|
| - return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0;
|
| -}
|
| -
|
| -gfx::Rect PdfMetafile::GetPageBounds(unsigned int page_number) const {
|
| - CGPDFDocumentRef pdf_doc = GetPDFDocument();
|
| - if (!pdf_doc) {
|
| - LOG(ERROR) << "Unable to create PDF document from data";
|
| - return gfx::Rect();
|
| - }
|
| - if (page_number > GetPageCount()) {
|
| - LOG(ERROR) << "Invalid page number: " << page_number;
|
| - return gfx::Rect();
|
| - }
|
| - CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
|
| - CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox);
|
| - return gfx::Rect(page_rect);
|
| -}
|
| -
|
| -uint32 PdfMetafile::GetDataSize() const {
|
| - // PDF data is only valid/complete once the context is released.
|
| - DCHECK(!context_);
|
| -
|
| - if (!pdf_data_)
|
| - return 0;
|
| - return static_cast<uint32>(CFDataGetLength(pdf_data_));
|
| -}
|
| -
|
| -bool PdfMetafile::GetData(void* dst_buffer, uint32 dst_buffer_size) const {
|
| - // PDF data is only valid/complete once the context is released.
|
| - DCHECK(!context_);
|
| - DCHECK(pdf_data_);
|
| - DCHECK(dst_buffer);
|
| - DCHECK_GT(dst_buffer_size, 0U);
|
| -
|
| - uint32 data_size = GetDataSize();
|
| - if (dst_buffer_size > data_size) {
|
| - return false;
|
| - }
|
| -
|
| - CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size),
|
| - static_cast<UInt8*>(dst_buffer));
|
| - return true;
|
| -}
|
| -
|
| -bool PdfMetafile::SaveTo(const FilePath& file_path) const {
|
| - DCHECK(pdf_data_.get());
|
| - DCHECK(!context_.get());
|
| -
|
| - std::string path_string = file_path.value();
|
| - ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation(
|
| - kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()),
|
| - path_string.length(), false));
|
| - SInt32 error_code;
|
| - CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code);
|
| - return error_code == 0;
|
| -}
|
| -
|
| CGPDFDocumentRef PdfMetafile::GetPDFDocument() const {
|
| // Make sure that we have data, and that it's not being modified any more.
|
| DCHECK(pdf_data_.get());
|
|
|