fix_properly_honor_printing_page_ranges.patch 5.4 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 c9a13aee1e64f359cd1d5c2e440b4df204420997..7790c311400fc84bfda6c99e1ebd8da728031a77 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 ee900c7541af99c61844920433ae39e2648ae90b..59137008aeddc03d60c58a463206dc91a0ef5c1f 100644
  25. --- a/printing/printing_context_mac.mm
  26. +++ b/printing/printing_context_mac.mm
  27. @@ -464,7 +464,8 @@ void ApplySystemPrintDialogData(
  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. @@ -617,6 +618,22 @@ void ApplySystemPrintDialogData(
  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 07847521e7217c78480205812a73cc89503c00d2..586e866ca7ec0eb0b573d23e3bd95792c22bff20 100644
  61. --- a/printing/printing_context_system_dialog_win.cc
  62. +++ b/printing/printing_context_system_dialog_win.cc
  63. @@ -75,14 +75,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 = std::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 = std::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 f3892e3f9ff2557bf604b48a8abc0fc4cbd3df1f..ba57bfc2dcef007dee536be0be6eb64d86532d09 100644
  99. --- a/ui/gtk/printing/print_dialog_gtk.cc
  100. +++ b/ui/gtk/printing/print_dialog_gtk.cc
  101. @@ -258,6 +258,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());