diff options
author | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-28 18:56:30 +0000 |
---|---|---|
committer | sanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-28 18:56:30 +0000 |
commit | f3cab625e9f5a94b12449e1ca552cca67a17ce3b (patch) | |
tree | 9795bdcd8474d93629ef0987df7981a873c73b4b /printing | |
parent | 52a69186b43c61c0f3c96abd583dc9a9e3cf447d (diff) | |
download | chromium_src-f3cab625e9f5a94b12449e1ca552cca67a17ce3b.zip chromium_src-f3cab625e9f5a94b12449e1ca552cca67a17ce3b.tar.gz chromium_src-f3cab625e9f5a94b12449e1ca552cca67a17ce3b.tar.bz2 |
Added support for vector printing of Pepper v1 plugins on the Mac.
BUG=None.
TEST=Test printing from Chrome PDF plugin on the Mac.
Review URL: http://codereview.chromium.org/2808029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51008 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
-rw-r--r-- | printing/image.cc | 3 | ||||
-rw-r--r-- | printing/pdf_metafile_mac.cc | 42 | ||||
-rw-r--r-- | printing/pdf_metafile_mac.h | 19 | ||||
-rw-r--r-- | printing/printed_document_mac.cc | 3 |
4 files changed, 57 insertions, 10 deletions
diff --git a/printing/image.cc b/printing/image.cc index dbee79d..d12d3d60 100644 --- a/printing/image.cc +++ b/printing/image.cc @@ -250,7 +250,8 @@ bool Image::LoadMetafile(const NativeMetafile& metafile) { kCGImageAlphaPremultipliedLast)); DCHECK(bitmap_context.get()); metafile.RenderPage(page_number, bitmap_context, - CGRectMake(0, 0, size_.width(), size_.height())); + CGRectMake(0, 0, size_.width(), size_.height()), + true, false, false, false); } #else NOTIMPLEMENTED(); diff --git a/printing/pdf_metafile_mac.cc b/printing/pdf_metafile_mac.cc index c4aa4f0..1726ba0 100644 --- a/printing/pdf_metafile_mac.cc +++ b/printing/pdf_metafile_mac.cc @@ -105,7 +105,10 @@ void PdfMetafile::Close() { } bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context, - const CGRect rect) const { + const CGRect rect, bool shrink_to_fit, + bool stretch_to_fit, + bool center_horizontally, + bool center_vertically) const { CGPDFDocumentRef pdf_doc = GetPDFDocument(); if (!pdf_doc) { LOG(ERROR) << "Unable to create PDF document from data"; @@ -113,14 +116,43 @@ bool PdfMetafile::RenderPage(unsigned int page_number, CGContextRef context, } CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox); + float scaling_factor = 1.0; + // See if we need to scale the output. + bool scaling_needed = + (shrink_to_fit && ((source_rect.size.width > rect.size.width) || + (source_rect.size.height > rect.size.height))) || + (stretch_to_fit && (source_rect.size.width < rect.size.width) && + (source_rect.size.height < rect.size.height)); + if (scaling_needed) { + float x_scaling_factor = rect.size.width / source_rect.size.width; + float y_scaling_factor = rect.size.height / source_rect.size.height; + if (x_scaling_factor > y_scaling_factor) { + scaling_factor = y_scaling_factor; + } else { + scaling_factor = x_scaling_factor; + } + } + // Some PDFs have a non-zero origin. Need to take that into account. + float x_offset = rect.origin.x - (source_rect.origin.x * scaling_factor); + float y_offset = rect.origin.y - (source_rect.origin.y * scaling_factor); + if (center_vertically) { + x_offset += (rect.size.width - + (source_rect.size.width * scaling_factor))/2; + } + if (center_horizontally) { + y_offset += (rect.size.height - + (source_rect.size.height * scaling_factor))/2; + } else { + // Since 0 y begins at the bottom, we need to adjust so the output appears + // nearer the top if we are not centering horizontally. + y_offset += rect.size.height - (source_rect.size.height * scaling_factor); + } CGContextSaveGState(context); - CGContextTranslateCTM(context, rect.origin.x, rect.origin.y); - CGContextScaleCTM(context, rect.size.width / source_rect.size.width, - rect.size.height / source_rect.size.height); + CGContextTranslateCTM(context, x_offset, y_offset); + CGContextScaleCTM(context, scaling_factor, scaling_factor); CGContextDrawPDFPage(context, pdf_page); CGContextRestoreGState(context); - return true; } diff --git a/printing/pdf_metafile_mac.h b/printing/pdf_metafile_mac.h index 6e50913..264e970 100644 --- a/printing/pdf_metafile_mac.h +++ b/printing/pdf_metafile_mac.h @@ -51,10 +51,23 @@ class PdfMetafile { void Close(); // Renders the given page into |rect| in the given context. - // Pages use a 1-based index. + // Pages use a 1-based index. The rendering uses the following arguments + // to determine scaling and translation factors. + // |shrink_to_fit| specifies whether the output should be shrunk to fit the + // supplied |rect| if the page size is larger than |rect| in any dimension. + // If this is false, parts of the PDF page that lie outside the bounds will be + // clipped. + // |stretch_to_fit| specifies whether the output should be stretched to fit + // the supplied bounds if the page size is smaller than |rect| in all + // dimensions. + // |center_horizontally| specifies whether the final image (after any scaling + // is done) should be centered horizontally within the given |rect|. + // |center_vertically| specifies whether the final image (after any scaling + // is done) should be centered vertically within the given |rect|. + // Note that all scaling preserves the original aspect ratio of the page. bool RenderPage(unsigned int page_number, CGContextRef context, - const CGRect rect) const; - + const CGRect rect, bool shrink_to_fit, bool stretch_to_fit, + bool center_horizontally, bool center_vertically) const; unsigned int GetPageCount() const; // Returns the bounds of the given page. diff --git a/printing/printed_document_mac.cc b/printing/printed_document_mac.cc index 422dacf..a3ea8c4 100644 --- a/printing/printed_document_mac.cc +++ b/printing/printed_document_mac.cc @@ -31,7 +31,8 @@ void PrintedDocument::RenderPrintedPage( const printing::NativeMetafile* metafile = page.native_metafile(); // Each NativeMetafile is a one-page PDF, and pages use 1-based indexing. const int page_number = 1; - metafile->RenderPage(page_number, context, target_rect); + metafile->RenderPage(page_number, context, target_rect, true, false, false, + false); // TODO(stuartmorgan): Print the header and footer. } |