|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Shelley Vohr <shelley.vohr@gmail.com> |
| 3 | +Date: Wed, 23 Sep 2020 11:16:12 -0700 |
| 4 | +Subject: fix: properly honor printing page ranges |
| 5 | + |
| 6 | +The print ranges in Chromium's print job settings were not being properly |
| 7 | +plumbed through to PMPrintSettings on mcOS. This fixes that by setting |
| 8 | +them should they exist. |
| 9 | + |
| 10 | +diff --git a/printing/printing_context_mac.h b/printing/printing_context_mac.h |
| 11 | +index 909aaf6ce803fed32eb1c64c0abcf272731762da..ba431655fe5894c7d9b04464ea36307a145ac198 100644 |
| 12 | +--- a/printing/printing_context_mac.h |
| 13 | ++++ b/printing/printing_context_mac.h |
| 14 | +@@ -82,6 +82,10 @@ class PRINTING_EXPORT PrintingContextMac : public PrintingContext { |
| 15 | + // Returns true if the orientation was set. |
| 16 | + bool SetOrientationIsLandscape(bool landscape); |
| 17 | + |
| 18 | ++ // Set the page range in native print info object. |
| 19 | ++ // Returns true if the range was set. |
| 20 | ++ bool SetPrintRangeInPrintSettings(const PageRanges& ranges); |
| 21 | ++ |
| 22 | + // Sets duplex mode in PMPrintSettings. |
| 23 | + // Returns true if duplex mode is set. |
| 24 | + bool SetDuplexModeInPrintSettings(mojom::DuplexMode mode); |
| 25 | +diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm |
| 26 | +index 51c8ff31497fd03842ac9e7da4eab8a5919ec898..53e11ca26e82f2eac18a3465cabde2605b061319 100644 |
| 27 | +--- a/printing/printing_context_mac.mm |
| 28 | ++++ b/printing/printing_context_mac.mm |
| 29 | +@@ -188,7 +188,8 @@ PMPaper MatchPaper(CFArrayRef paper_list, |
| 30 | + !SetCopiesInPrintSettings(settings_->copies()) || |
| 31 | + !SetCollateInPrintSettings(settings_->collate()) || |
| 32 | + !SetDuplexModeInPrintSettings(settings_->duplex_mode()) || |
| 33 | +- !SetOutputColor(static_cast<int>(settings_->color()))) { |
| 34 | ++ !SetOutputColor(static_cast<int>(settings_->color())) || |
| 35 | ++ !SetPrintRangeInPrintSettings(settings_->ranges())) { |
| 36 | + return OnError(); |
| 37 | + } |
| 38 | + } |
| 39 | +@@ -341,6 +342,22 @@ PMPaper MatchPaper(CFArrayRef paper_list, |
| 40 | + return PMSetCopies(print_settings, copies, false) == noErr; |
| 41 | + } |
| 42 | + |
| 43 | ++bool PrintingContextMac::SetPrintRangeInPrintSettings(const PageRanges& ranges) { |
| 44 | ++ // Default is already NSPrintAllPages - we can safely bail. |
| 45 | ++ if (ranges.empty()) |
| 46 | ++ return true; |
| 47 | ++ |
| 48 | ++ auto* print_settings = |
| 49 | ++ static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
| 50 | ++ |
| 51 | ++ // macOS does not allow multiple ranges, so pluck the first. |
| 52 | ++ auto range = ranges.front(); |
| 53 | ++ bool set_first_page = PMSetFirstPage(print_settings, range.from + 1, false) == noErr; |
| 54 | ++ bool set_last_page = PMSetLastPage(print_settings, range.to + 1, false) == noErr; |
| 55 | ++ |
| 56 | ++ return set_first_page && set_last_page; |
| 57 | ++} |
| 58 | ++ |
| 59 | + bool PrintingContextMac::SetCollateInPrintSettings(bool collate) { |
| 60 | + PMPrintSettings print_settings = |
| 61 | + static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
| 62 | +diff --git a/printing/printing_context_system_dialog_win.cc b/printing/printing_context_system_dialog_win.cc |
| 63 | +index d3c8677f30d72efc49b28f293260c74c7b8d8b4e..f6e66aaa58ab1881d64dcbb320ae8b5ac7631b28 100644 |
| 64 | +--- a/printing/printing_context_system_dialog_win.cc |
| 65 | ++++ b/printing/printing_context_system_dialog_win.cc |
| 66 | +@@ -52,14 +52,28 @@ void PrintingContextSystemDialogWin::AskUserForSettings( |
| 67 | + PRINTPAGERANGE ranges[32]; |
| 68 | + dialog_options.nStartPage = START_PAGE_GENERAL; |
| 69 | + if (max_pages) { |
| 70 | +- // Default initialize to print all the pages. |
| 71 | + memset(ranges, 0, sizeof(ranges)); |
| 72 | +- ranges[0].nFromPage = 1; |
| 73 | +- ranges[0].nToPage = max_pages; |
| 74 | +- dialog_options.nPageRanges = 1; |
| 75 | +- dialog_options.nMaxPageRanges = base::size(ranges); |
| 76 | ++ |
| 77 | ++ auto page_ranges = settings_->ranges(); |
| 78 | ++ if (!page_ranges.empty()) { |
| 79 | ++ for (size_t i = 0; i < page_ranges.size(); i++) { |
| 80 | ++ auto range = page_ranges[i]; |
| 81 | ++ ranges[i].nFromPage = range.from + 1; |
| 82 | ++ ranges[i].nToPage = range.to + 1; |
| 83 | ++ } |
| 84 | ++ dialog_options.nPageRanges = page_ranges.size(); |
| 85 | ++ |
| 86 | ++ // Ensure the Pages radio button is selected. |
| 87 | ++ dialog_options.Flags |= PD_PAGENUMS; |
| 88 | ++ } else { |
| 89 | ++ ranges[0].nFromPage = 1; |
| 90 | ++ ranges[0].nToPage = max_pages; |
| 91 | ++ dialog_options.nPageRanges = 1; |
| 92 | ++ } |
| 93 | ++ |
| 94 | + dialog_options.nMinPage = 1; |
| 95 | + dialog_options.nMaxPage = max_pages; |
| 96 | ++ dialog_options.nMaxPageRanges = base::size(ranges); |
| 97 | + dialog_options.lpPageRanges = ranges; |
| 98 | + } else { |
| 99 | + // No need to bother, we don't know how many pages are available. |
| 100 | +diff --git a/ui/gtk/printing/print_dialog_gtk.cc b/ui/gtk/printing/print_dialog_gtk.cc |
| 101 | +index f2ed36e1258f4f3ef1bfce972e215e3d5d7335b6..5b38bf1369a68546f0aeea8948abba995c65e7f7 100644 |
| 102 | +--- a/ui/gtk/printing/print_dialog_gtk.cc |
| 103 | ++++ b/ui/gtk/printing/print_dialog_gtk.cc |
| 104 | +@@ -239,6 +239,24 @@ void PrintDialogGtk::UpdateSettings( |
| 105 | + |
| 106 | + gtk_print_settings_set_n_copies(gtk_settings_, settings->copies()); |
| 107 | + gtk_print_settings_set_collate(gtk_settings_, settings->collate()); |
| 108 | ++ |
| 109 | ++ auto print_ranges = settings->ranges(); |
| 110 | ++ if (!print_ranges.empty()) { |
| 111 | ++ // Tell the system that we only intend to print a subset of pages. |
| 112 | ++ gtk_print_settings_set_print_pages(gtk_settings_, GTK_PRINT_PAGES_RANGES); |
| 113 | ++ |
| 114 | ++ GtkPageRange* ranges; |
| 115 | ++ ranges = g_new(GtkPageRange, print_ranges.size()); |
| 116 | ++ for (size_t i = 0; i < print_ranges.size(); i++) { |
| 117 | ++ auto range = print_ranges[i]; |
| 118 | ++ ranges[i].start = range.from; |
| 119 | ++ ranges[i].end = range.to; |
| 120 | ++ } |
| 121 | ++ |
| 122 | ++ gtk_print_settings_set_page_ranges(gtk_settings_, ranges, 1); |
| 123 | ++ g_free(ranges); |
| 124 | ++ } |
| 125 | ++ |
| 126 | + if (settings->dpi_horizontal() > 0 && settings->dpi_vertical() > 0) { |
| 127 | + gtk_print_settings_set_resolution_xy( |
| 128 | + gtk_settings_, settings->dpi_horizontal(), settings->dpi_vertical()); |
0 commit comments