fix_properly_honor_printing_page_ranges.patch 5.5 KB

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