fix_properly_honor_printing_page_ranges.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 5b469b3b6bd11d8e0baa08e9be257990ac9bc438..c1410e5d8112f0319de40e4fbf41dbaea4b9733c 100644
  11. --- a/printing/printing_context_mac.h
  12. +++ b/printing/printing_context_mac.h
  13. @@ -84,6 +84,10 @@ class COMPONENT_EXPORT(PRINTING) 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 f39f8b2dd3aa2b89498ed5331aa9b9ba6a02abf5..c0e155c14b2b4e81cde35cea1db284bc4c43f5e6 100644
  25. --- a/printing/printing_context_mac.mm
  26. +++ b/printing/printing_context_mac.mm
  27. @@ -520,7 +520,8 @@ bool IsIppColorModelColorful(mojom::ColorModel color_model) {
  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. @@ -673,6 +674,22 @@ bool IsIppColorModelColorful(mojom::ColorModel color_model) {
  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. + PMPrintSettings print_settings =
  46. + static_cast<PMPrintSettings>([print_info_ 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_ PMPrintSettings]);
  59. diff --git a/printing/printing_context_system_dialog_win.cc b/printing/printing_context_system_dialog_win.cc
  60. index 858e1bb00390b6097a27ffe20997672914e8f28f..953f203d21b4da559791efe228f27b56c2f85263 100644
  61. --- a/printing/printing_context_system_dialog_win.cc
  62. +++ b/printing/printing_context_system_dialog_win.cc
  63. @@ -73,14 +73,30 @@ 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 = std::size(ranges);
  73. +
  74. + auto page_ranges = settings_->ranges();
  75. + if (!page_ranges.empty()) {
  76. + UNSAFE_TODO({
  77. + for (size_t i = 0; i < page_ranges.size(); i++) {
  78. + auto range = page_ranges[i];
  79. + ranges[i].nFromPage = range.from + 1;
  80. + ranges[i].nToPage = range.to + 1;
  81. + }
  82. + });
  83. + dialog_options.nPageRanges = page_ranges.size();
  84. +
  85. + // Ensure the Pages radio button is selected.
  86. + dialog_options.Flags |= PD_PAGENUMS;
  87. + } else {
  88. + ranges[0].nFromPage = 1;
  89. + ranges[0].nToPage = max_pages;
  90. + dialog_options.nPageRanges = 1;
  91. + }
  92. +
  93. dialog_options.nMinPage = 1;
  94. dialog_options.nMaxPage = max_pages;
  95. + dialog_options.nMaxPageRanges = std::size(ranges);
  96. dialog_options.lpPageRanges = ranges;
  97. } else {
  98. // No need to bother, we don't know how many pages are available.
  99. diff --git a/ui/gtk/printing/print_dialog_gtk.cc b/ui/gtk/printing/print_dialog_gtk.cc
  100. index b04cb0b9a9fd25e638c8e5cfac08ddf5dbd1a67e..393815e33b8c700baa349482867fc7f1425ff09f 100644
  101. --- a/ui/gtk/printing/print_dialog_gtk.cc
  102. +++ b/ui/gtk/printing/print_dialog_gtk.cc
  103. @@ -247,6 +247,24 @@ void PrintDialogGtk::UpdateSettings(
  104. gtk_print_settings_set_n_copies(gtk_settings_, settings->copies());
  105. gtk_print_settings_set_collate(gtk_settings_, settings->collate());
  106. +
  107. + auto print_ranges = settings->ranges();
  108. + if (!print_ranges.empty()) {
  109. + // Tell the system that we only intend to print a subset of pages.
  110. + gtk_print_settings_set_print_pages(gtk_settings_, GTK_PRINT_PAGES_RANGES);
  111. +
  112. + GtkPageRange* ranges;
  113. + ranges = g_new(GtkPageRange, print_ranges.size());
  114. + for (size_t i = 0; i < print_ranges.size(); i++) {
  115. + auto range = print_ranges[i];
  116. + ranges[i].start = range.from;
  117. + ranges[i].end = range.to;
  118. + }
  119. +
  120. + gtk_print_settings_set_page_ranges(gtk_settings_, ranges, 1);
  121. + g_free(ranges);
  122. + }
  123. +
  124. if (settings->dpi_horizontal() > 0 && settings->dpi_vertical() > 0) {
  125. gtk_print_settings_set_resolution_xy(
  126. gtk_settings_, settings->dpi_horizontal(), settings->dpi_vertical());