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

Unified Diff: printing/pdf_metafile_cg_mac.cc

Issue 10560021: Mac printing: Autorotate printed pages so their orientation is the same as the destination page. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: printing/pdf_metafile_cg_mac.cc
===================================================================
--- printing/pdf_metafile_cg_mac.cc (revision 142466)
+++ printing/pdf_metafile_cg_mac.cc (working copy)
@@ -4,6 +4,8 @@
#include "printing/pdf_metafile_cg_mac.h"
+#include <algorithm>
+
#include "base/file_path.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
@@ -172,10 +174,7 @@
bool PdfMetafileCg::RenderPage(unsigned int page_number,
CGContextRef context,
const CGRect rect,
- bool shrink_to_fit,
- bool stretch_to_fit,
- bool center_horizontally,
- bool center_vertically) const {
+ const MacRenderPageParams& params) const {
CGPDFDocumentRef pdf_doc = GetPDFDocument();
if (!pdf_doc) {
LOG(ERROR) << "Unable to create PDF document from data";
@@ -184,38 +183,60 @@
CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number);
CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox);
float scaling_factor = 1.0;
+ const bool source_is_landscape =
+ (source_rect.size.width > source_rect.size.height);
+ const bool dest_is_landscape = (rect.size.width > rect.size.height);
+ const bool rotate =
+ params.autorotate ? (source_is_landscape != dest_is_landscape) : false;
+ const float source_width =
+ rotate ? source_rect.size.height : source_rect.size.width;
+ const float source_height =
+ rotate ? source_rect.size.width : source_rect.size.height;
+
// 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));
+ const bool scaling_needed =
+ (params.shrink_to_fit && ((source_width > rect.size.width) ||
+ (source_height > rect.size.height))) ||
+ (params.stretch_to_fit && ((source_width < rect.size.width) &&
+ (source_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;
- }
+ float x_scaling_factor = rect.size.width / source_width;
+ float y_scaling_factor = rect.size.height / source_height;
+ scaling_factor = std::min(x_scaling_factor, y_scaling_factor);
}
- // Some PDFs have a non-zero origin. Need to take that into account.
- float x_offset = -1 * source_rect.origin.x * scaling_factor;
- float y_offset = -1 * source_rect.origin.y * scaling_factor;
+ // Some PDFs have a non-zero origin. Need to take that into account and align
+ // the PDF to the origin.
+ const float x_origin_offset = -1 * source_rect.origin.x;
+ const float y_origin_offset = -1 * source_rect.origin.y;
- if (center_horizontally) {
- x_offset += (rect.size.width -
- (source_rect.size.width * scaling_factor))/2;
- }
- if (center_vertically) {
- y_offset += (rect.size.height -
- (source_rect.size.height * scaling_factor))/2;
- }
+ // If the PDF needs to be centered, calculate the offsets here.
+ float x_offset = params.center_horizontally ?
+ ((rect.size.width - (source_width * scaling_factor)) / 2) : 0;
+ if (rotate)
+ x_offset = -x_offset;
+
+ float y_offset = params.center_vertically ?
+ ((rect.size.height - (source_height * scaling_factor)) / 2) : 0;
+
CGContextSaveGState(context);
+
+ // The transform operations specified here gets applied in reverse order.
+ // i.e. the origin offset translation happens first.
+ // Origin is at bottom-left.
CGContextTranslateCTM(context, x_offset, y_offset);
+ if (rotate) {
+ // After rotating by 90 degrees with the axis at the origin, the page
+ // content is now "off screen". Shift it right to move it back on screen.
+ CGContextTranslateCTM(context, rect.size.width, 0);
+ // Rotates counter-clockwise by 90 degrees.
+ CGContextRotateCTM(context, M_PI_2);
+ }
CGContextScaleCTM(context, scaling_factor, scaling_factor);
+ CGContextTranslateCTM(context, x_origin_offset, y_origin_offset);
+
CGContextDrawPDFPage(context, pdf_page);
CGContextRestoreGState(context);
+
return true;
}

Powered by Google App Engine
This is Rietveld 408576698