Index: printing/printing_context_mac.mm |
diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm |
index 9008eadea79ea91930fc83933259d7f1967e4a4e..5b11c1898fe3ae5c9db9f5c677673bc1aeb8dff7 100644 |
--- a/printing/printing_context_mac.mm |
+++ b/printing/printing_context_mac.mm |
@@ -143,6 +143,8 @@ PrintingContext::Result PrintingContextMac::UpdatePrinterSettings( |
if (!SetOutputColor(color)) |
return OnError(); |
} |
+ if (!UpdatePageFormatWithPaperInfo()) |
+ return OnError(); |
if (!SetOrientationIsLandscape(landscape)) |
return OnError(); |
@@ -197,6 +199,95 @@ bool PrintingContextMac::SetPrinter(const std::string& device_name) { |
return status == noErr; |
} |
+bool PrintingContextMac::UpdatePageFormatWithPaperInfo() { |
+ PMPrintSession print_session = |
+ static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
+ |
+ PMPageFormat default_page_format = |
+ static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); |
+ |
+ PMPaper default_paper; |
+ if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr) |
+ return false; |
+ |
+ double default_page_width, default_page_height; |
+ if (PMPaperGetWidth(default_paper, &default_page_width) != noErr) |
+ return false; |
+ |
+ if (PMPaperGetHeight(default_paper, &default_page_height) != noErr) |
+ return false; |
+ |
+ PMPrinter current_printer = NULL; |
+ if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) |
+ return false; |
+ |
+ if (current_printer == nil) |
+ return false; |
+ |
+ CFArrayRef paper_list = NULL; |
+ if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) |
+ return false; |
+ |
+ PMPaper best_matching_paper = kPMNoData; |
+ int num_papers = CFArrayGetCount(paper_list); |
+ for (int i = 0; i < num_papers; ++i) { |
+ PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i]; |
+ double paper_width, paper_height; |
+ PMPaperGetWidth(paper, &paper_width); |
+ PMPaperGetHeight(paper, &paper_height); |
+ if (default_page_width == paper_width && |
+ default_page_height == paper_height) { |
+ best_matching_paper = paper; |
+ break; |
+ } |
+ // Trying to find the best matching paper. |
+ if (fabs(default_page_width - paper_width) < 2 && |
+ fabs(default_page_height - paper_height) < 2) { |
+ best_matching_paper = paper; |
+ } |
+ } |
+ |
+ if (best_matching_paper == kPMNoData) { |
+ PMPaper paper = kPMNoData; |
+ // Create a custom paper for the specified default page size. |
+ PMPaperMargins default_margins; |
+ if (PMPaperGetMargins(default_paper, &default_margins) != noErr) |
+ return false; |
+ |
+ const PMPaperMargins margins = |
+ {default_margins.top, default_margins.left, default_margins.bottom, |
+ default_margins.right}; |
+ CFStringRef paper_id = CFSTR("Custom paper ID"); |
+ CFStringRef paper_name = CFSTR("Custom paper"); |
+ if (PMPaperCreateCustom(current_printer, paper_id, paper_name, |
+ default_page_width, default_page_height, &margins, &paper) != |
+ noErr) { |
+ return false; |
+ } |
+ [print_info_.get() updateFromPMPageFormat]; |
+ PMRelease(paper); |
+ } else { |
+ PMPageFormat chosen_page_format = NULL; |
+ if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr) |
+ return false; |
+ |
+ // Create page format from that paper. |
+ if (PMCreatePageFormatWithPMPaper(&chosen_page_format, |
+ best_matching_paper) != noErr) { |
+ PMRelease(chosen_page_format); |
+ return false; |
+ } |
+ // Copy over the original format with the new page format. |
+ if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) { |
+ PMRelease(chosen_page_format); |
+ return false; |
+ } |
+ [print_info_.get() updateFromPMPageFormat]; |
+ PMRelease(chosen_page_format); |
+ } |
+ return true; |
+} |
+ |
bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { |
if (copies < 1) |
return false; |
@@ -221,6 +312,11 @@ bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) { |
if (PMSetOrientation(page_format, orientation, false) != noErr) |
return false; |
+ PMPrintSession print_session = |
+ static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
+ |
+ PMSessionValidatePageFormat(print_session, page_format, kPMDontWantBoolean); |
+ |
[print_info_.get() updateFromPMPageFormat]; |
return true; |
} |