print_settings.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PRINTING_PRINT_SETTINGS_H_
  5. #define PRINTING_PRINT_SETTINGS_H_
  6. #include <algorithm>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "build/build_config.h"
  10. #include "printing/mojom/print.mojom.h"
  11. #include "printing/page_range.h"
  12. #include "printing/page_setup.h"
  13. #include "printing/print_job_constants.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "ui/gfx/geometry/rect.h"
  16. #include "ui/gfx/geometry/size.h"
  17. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  18. #include <map>
  19. #include "base/values.h"
  20. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  21. namespace printing {
  22. // Convert from `color_mode` into a `color_model`. An invalid `color_mode`
  23. // will give a result of `mojom::ColorModel::kUnknownColorModel`.
  24. COMPONENT_EXPORT(PRINTING)
  25. mojom::ColorModel ColorModeToColorModel(int color_mode);
  26. // Returns true if `color_model` is color and false if it is B&W. Callers
  27. // are not supposed to pass in `mojom::ColorModel::kUnknownColorModel`, but
  28. // if they do then the result will be absl::nullopt.
  29. COMPONENT_EXPORT(PRINTING)
  30. absl::optional<bool> IsColorModelSelected(mojom::ColorModel color_model);
  31. #if defined(USE_CUPS)
  32. // Get the color model setting name and value for the `color_model`.
  33. COMPONENT_EXPORT(PRINTING)
  34. void GetColorModelForModel(mojom::ColorModel color_model,
  35. std::string* color_setting_name,
  36. std::string* color_value);
  37. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
  38. // Convert from `color_model` to a print-color-mode value from PWG 5100.13.
  39. COMPONENT_EXPORT(PRINTING)
  40. std::string GetIppColorModelForModel(mojom::ColorModel color_model);
  41. #endif
  42. #endif // defined(USE_CUPS)
  43. class COMPONENT_EXPORT(PRINTING) PrintSettings {
  44. public:
  45. // Media properties requested by the user. Default instance represents
  46. // default media selection.
  47. struct RequestedMedia {
  48. // Size of the media, in microns.
  49. gfx::Size size_microns;
  50. // Platform specific id to map it back to the particular media.
  51. std::string vendor_id;
  52. bool IsDefault() const {
  53. return size_microns.IsEmpty() && vendor_id.empty();
  54. }
  55. };
  56. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  57. using AdvancedSettings = std::map<std::string, base::Value>;
  58. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  59. PrintSettings();
  60. PrintSettings(const PrintSettings&);
  61. PrintSettings& operator=(const PrintSettings&);
  62. ~PrintSettings();
  63. // Reinitialize the settings to the default values.
  64. void Clear();
  65. void SetCustomMargins(const PageMargins& requested_margins_in_points);
  66. const PageMargins& requested_custom_margins_in_points() const {
  67. return requested_custom_margins_in_points_;
  68. }
  69. void set_margin_type(mojom::MarginType margin_type) {
  70. margin_type_ = margin_type;
  71. }
  72. mojom::MarginType margin_type() const { return margin_type_; }
  73. // Updates the orientation and flip the page if needed.
  74. void SetOrientation(bool landscape);
  75. bool landscape() const { return landscape_; }
  76. // Updates user requested media.
  77. void set_requested_media(const RequestedMedia& media) {
  78. requested_media_ = media;
  79. }
  80. // Media properties requested by the user. Translated into device media by the
  81. // platform specific layers.
  82. const RequestedMedia& requested_media() const { return requested_media_; }
  83. // Set printer printable area in in device units.
  84. // Some platforms already provide flipped area. Set `landscape_needs_flip`
  85. // to false on those platforms to avoid double flipping.
  86. // This method assumes correct DPI is already set.
  87. void SetPrinterPrintableArea(const gfx::Size& physical_size_device_units,
  88. const gfx::Rect& printable_area_device_units,
  89. bool landscape_needs_flip);
  90. const PageSetup& page_setup_device_units() const {
  91. return page_setup_device_units_;
  92. }
  93. // `set_page_setup_device_units()` intended to be used only for mojom
  94. // validation.
  95. void set_page_setup_device_units(const PageSetup& page_setup_device_units) {
  96. page_setup_device_units_ = page_setup_device_units;
  97. }
  98. void set_device_name(const std::u16string& device_name) {
  99. device_name_ = device_name;
  100. }
  101. const std::u16string& device_name() const { return device_name_; }
  102. void set_dpi(int dpi) { dpi_ = gfx::Size(dpi, dpi); }
  103. void set_dpi_xy(int dpi_horizontal, int dpi_vertical) {
  104. dpi_ = gfx::Size(dpi_horizontal, dpi_vertical);
  105. }
  106. int dpi() const { return std::max(dpi_.width(), dpi_.height()); }
  107. int dpi_horizontal() const { return dpi_.width(); }
  108. int dpi_vertical() const { return dpi_.height(); }
  109. const gfx::Size& dpi_size() const { return dpi_; }
  110. void set_scale_factor(double scale_factor) { scale_factor_ = scale_factor; }
  111. double scale_factor() const { return scale_factor_; }
  112. void set_rasterize_pdf(bool rasterize_pdf) { rasterize_pdf_ = rasterize_pdf; }
  113. bool rasterize_pdf() const { return rasterize_pdf_; }
  114. void set_rasterize_pdf_dpi(int32_t dpi) { rasterize_pdf_dpi_ = dpi; }
  115. int32_t rasterize_pdf_dpi() const { return rasterize_pdf_dpi_; }
  116. void set_supports_alpha_blend(bool supports_alpha_blend) {
  117. supports_alpha_blend_ = supports_alpha_blend;
  118. }
  119. bool supports_alpha_blend() const { return supports_alpha_blend_; }
  120. int device_units_per_inch() const {
  121. #if BUILDFLAG(IS_MAC)
  122. return 72;
  123. #else // BUILDFLAG(IS_MAC)
  124. return dpi();
  125. #endif // BUILDFLAG(IS_MAC)
  126. }
  127. void set_ranges(const PageRanges& ranges) { ranges_ = ranges; }
  128. const PageRanges& ranges() const { return ranges_; }
  129. void set_selection_only(bool selection_only) {
  130. selection_only_ = selection_only;
  131. }
  132. bool selection_only() const { return selection_only_; }
  133. void set_should_print_backgrounds(bool should_print_backgrounds) {
  134. should_print_backgrounds_ = should_print_backgrounds;
  135. }
  136. bool should_print_backgrounds() const { return should_print_backgrounds_; }
  137. void set_display_header_footer(bool display_header_footer) {
  138. display_header_footer_ = display_header_footer;
  139. }
  140. bool display_header_footer() const { return display_header_footer_; }
  141. void set_title(const std::u16string& title) { title_ = title; }
  142. const std::u16string& title() const { return title_; }
  143. void set_url(const std::u16string& url) { url_ = url; }
  144. const std::u16string& url() const { return url_; }
  145. void set_collate(bool collate) { collate_ = collate; }
  146. bool collate() const { return collate_; }
  147. void set_color(mojom::ColorModel color) { color_ = color; }
  148. mojom::ColorModel color() const { return color_; }
  149. void set_copies(int copies) { copies_ = copies; }
  150. int copies() const { return copies_; }
  151. void set_duplex_mode(mojom::DuplexMode duplex_mode) {
  152. duplex_mode_ = duplex_mode;
  153. }
  154. mojom::DuplexMode duplex_mode() const { return duplex_mode_; }
  155. #if BUILDFLAG(IS_WIN)
  156. void set_printer_language_type(mojom::PrinterLanguageType type) {
  157. printer_language_type_ = type;
  158. }
  159. mojom::PrinterLanguageType printer_language_type() const {
  160. return printer_language_type_;
  161. }
  162. bool printer_language_is_textonly() const {
  163. return printer_language_type_ == mojom::PrinterLanguageType::kTextOnly;
  164. }
  165. bool printer_language_is_xps() const {
  166. return printer_language_type_ == mojom::PrinterLanguageType::kXps;
  167. }
  168. bool printer_language_is_ps2() const {
  169. return printer_language_type_ ==
  170. mojom::PrinterLanguageType::kPostscriptLevel2;
  171. }
  172. bool printer_language_is_ps3() const {
  173. return printer_language_type_ ==
  174. mojom::PrinterLanguageType::kPostscriptLevel3;
  175. }
  176. #endif
  177. void set_is_modifiable(bool is_modifiable) { is_modifiable_ = is_modifiable; }
  178. bool is_modifiable() const { return is_modifiable_; }
  179. int pages_per_sheet() const { return pages_per_sheet_; }
  180. void set_pages_per_sheet(int pages_per_sheet) {
  181. pages_per_sheet_ = pages_per_sheet;
  182. }
  183. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  184. AdvancedSettings& advanced_settings() { return advanced_settings_; }
  185. const AdvancedSettings& advanced_settings() const {
  186. return advanced_settings_;
  187. }
  188. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  189. #if BUILDFLAG(IS_CHROMEOS)
  190. void set_send_user_info(bool send_user_info) {
  191. send_user_info_ = send_user_info;
  192. }
  193. bool send_user_info() const { return send_user_info_; }
  194. void set_username(const std::string& username) { username_ = username; }
  195. const std::string& username() const { return username_; }
  196. void set_pin_value(const std::string& pin_value) { pin_value_ = pin_value; }
  197. const std::string& pin_value() const { return pin_value_; }
  198. #endif // BUILDFLAG(IS_CHROMEOS)
  199. // Cookie generator. It is used to initialize `PrintedDocument` with its
  200. // associated `PrintSettings`, to be sure that each generated `PrintedPage`
  201. // is correctly associated with its corresponding `PrintedDocument`.
  202. static int NewCookie();
  203. private:
  204. // Multi-page printing. Each `PageRange` describes a from-to page combination.
  205. // This permits printing selected pages only.
  206. PageRanges ranges_;
  207. // Indicates if the user only wants to print the current selection.
  208. bool selection_only_;
  209. // Indicates what kind of margins should be applied to the printable area.
  210. mojom::MarginType margin_type_;
  211. // Strings to be printed as headers and footers if requested by the user.
  212. std::u16string title_;
  213. std::u16string url_;
  214. // True if the user wants headers and footers to be displayed.
  215. bool display_header_footer_;
  216. // True if the user wants to print CSS backgrounds.
  217. bool should_print_backgrounds_;
  218. // True if the user wants to print with collate.
  219. bool collate_;
  220. // Color model type for the printer to use.
  221. mojom::ColorModel color_;
  222. // Number of copies user wants to print.
  223. int copies_;
  224. // Duplex type user wants to use.
  225. mojom::DuplexMode duplex_mode_;
  226. // Printer device name as opened by the OS.
  227. std::u16string device_name_;
  228. // Media requested by the user.
  229. RequestedMedia requested_media_;
  230. // Page setup in device units.
  231. PageSetup page_setup_device_units_;
  232. // Printer's device effective dots per inch in both axes. The two values will
  233. // generally be identical. However, on Windows, there are a few rare printers
  234. // that support resolutions with different DPI in different dimensions.
  235. gfx::Size dpi_;
  236. // Scale factor
  237. double scale_factor_;
  238. // True if PDF should be printed as a raster PDF
  239. bool rasterize_pdf_;
  240. // The DPI which overrides the calculated value normally used when
  241. // rasterizing a PDF. A non-positive value would be an invalid choice of a
  242. // DPI and indicates no override.
  243. int32_t rasterize_pdf_dpi_;
  244. // Is the orientation landscape or portrait.
  245. bool landscape_;
  246. // True if this printer supports AlphaBlend.
  247. bool supports_alpha_blend_;
  248. #if BUILDFLAG(IS_WIN)
  249. mojom::PrinterLanguageType printer_language_type_;
  250. #endif
  251. bool is_modifiable_;
  252. // If margin type is custom, this is what was requested.
  253. PageMargins requested_custom_margins_in_points_;
  254. // Number of pages per sheet.
  255. int pages_per_sheet_;
  256. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  257. // Advanced settings.
  258. AdvancedSettings advanced_settings_;
  259. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  260. #if BUILDFLAG(IS_CHROMEOS)
  261. // Whether to send user info.
  262. bool send_user_info_;
  263. // Username if it's required by the printer.
  264. std::string username_;
  265. // PIN code entered by the user.
  266. std::string pin_value_;
  267. #endif
  268. };
  269. } // namespace printing
  270. #endif // PRINTING_PRINT_SETTINGS_H_