pliard@chromium.org | 8a8443f | 2012-03-13 12:07:19 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
sverrir@google.com | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
deanm@google.com | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 7 | #include "base/atomic_sequence_num.h" |
halcanary | 73b63fd | 2015-11-06 00:02:14 | [diff] [blame] | 8 | #include "base/lazy_instance.h" |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 9 | #include "base/logging.h" |
vandebo@chromium.org | 06bd1866 | 2011-10-07 06:12:40 | [diff] [blame] | 10 | #include "printing/print_job_constants.h" |
maruel@chromium.org | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 11 | #include "printing/units.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
| 13 | namespace printing { |
| 14 | |
halcanary | 73b63fd | 2015-11-06 00:02:14 | [diff] [blame] | 15 | base::LazyInstance<std::string> g_user_agent; |
| 16 | |
| 17 | void SetAgent(const std::string& user_agent) { |
| 18 | g_user_agent.Get() = user_agent; |
| 19 | } |
| 20 | |
| 21 | const std::string& GetAgent() { |
| 22 | return g_user_agent.Get(); |
| 23 | } |
| 24 | |
thestig@chromium.org | fa879e5 | 2011-11-08 20:39:01 | [diff] [blame] | 25 | #if defined(USE_CUPS) |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 26 | void GetColorModelForMode( |
| 27 | int color_mode, std::string* color_setting_name, std::string* color_value) { |
| 28 | #if defined(OS_MACOSX) |
| 29 | const char kCUPSColorMode[] = "ColorMode"; |
| 30 | const char kCUPSColorModel[] = "ColorModel"; |
| 31 | const char kCUPSPrintoutMode[] = "PrintoutMode"; |
| 32 | const char kCUPSProcessColorModel[] = "ProcessColorModel"; |
| 33 | #else |
| 34 | const char kCUPSColorMode[] = "cups-ColorMode"; |
| 35 | const char kCUPSColorModel[] = "cups-ColorModel"; |
| 36 | const char kCUPSPrintoutMode[] = "cups-PrintoutMode"; |
| 37 | const char kCUPSProcessColorModel[] = "cups-ProcessColorModel"; |
thestig@chromium.org | fa879e5 | 2011-11-08 20:39:01 | [diff] [blame] | 38 | #endif // defined(OS_MACOSX) |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 39 | |
| 40 | color_setting_name->assign(kCUPSColorModel); |
| 41 | switch (color_mode) { |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 42 | case COLOR: |
| 43 | color_value->assign(kColor); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 44 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 45 | case CMYK: |
| 46 | color_value->assign(kCMYK); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 47 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 48 | case PRINTOUTMODE_NORMAL: |
| 49 | color_value->assign(kNormal); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 50 | color_setting_name->assign(kCUPSPrintoutMode); |
| 51 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 52 | case PRINTOUTMODE_NORMAL_GRAY: |
| 53 | color_value->assign(kNormalGray); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 54 | color_setting_name->assign(kCUPSPrintoutMode); |
| 55 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 56 | case RGB16: |
| 57 | color_value->assign(kRGB16); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 58 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 59 | case RGBA: |
| 60 | color_value->assign(kRGBA); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 61 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 62 | case RGB: |
| 63 | color_value->assign(kRGB); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 64 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 65 | case CMY: |
| 66 | color_value->assign(kCMY); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 67 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 68 | case CMY_K: |
| 69 | color_value->assign(kCMY_K); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 70 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 71 | case BLACK: |
| 72 | color_value->assign(kBlack); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 73 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 74 | case GRAY: |
| 75 | color_value->assign(kGray); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 76 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 77 | case COLORMODE_COLOR: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 78 | color_setting_name->assign(kCUPSColorMode); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 79 | color_value->assign(kColor); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 80 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 81 | case COLORMODE_MONOCHROME: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 82 | color_setting_name->assign(kCUPSColorMode); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 83 | color_value->assign(kMonochrome); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 84 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 85 | case HP_COLOR_COLOR: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 86 | color_setting_name->assign(kColor); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 87 | color_value->assign(kColor); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 88 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 89 | case HP_COLOR_BLACK: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 90 | color_setting_name->assign(kColor); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 91 | color_value->assign(kBlack); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 92 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 93 | case PROCESSCOLORMODEL_CMYK: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 94 | color_setting_name->assign(kCUPSProcessColorModel); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 95 | color_value->assign(kCMYK); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 96 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 97 | case PROCESSCOLORMODEL_GREYSCALE: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 98 | color_setting_name->assign(kCUPSProcessColorModel); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 99 | color_value->assign(kGreyscale); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 100 | break; |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 101 | case PROCESSCOLORMODEL_RGB: |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 102 | color_setting_name->assign(kCUPSProcessColorModel); |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 103 | color_value->assign(kRGB); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 104 | break; |
| 105 | default: |
thestig@chromium.org | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 106 | color_value->assign(kGrayscale); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 107 | break; |
| 108 | } |
| 109 | } |
thestig@chromium.org | fa879e5 | 2011-11-08 20:39:01 | [diff] [blame] | 110 | #endif // defined(USE_CUPS) |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 111 | |
thestig@chromium.org | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 112 | bool IsColorModelSelected(int color_mode) { |
| 113 | return (color_mode != GRAY && |
| 114 | color_mode != BLACK && |
| 115 | color_mode != PRINTOUTMODE_NORMAL_GRAY && |
| 116 | color_mode != COLORMODE_MONOCHROME && |
| 117 | color_mode != PROCESSCOLORMODEL_GREYSCALE && |
| 118 | color_mode != HP_COLOR_BLACK); |
kmadhusu@chromium.org | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 119 | } |
| 120 | |
deanm@google.com | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 121 | // Global SequenceNumber used for generating unique cookie values. |
pliard@chromium.org | 8a8443f | 2012-03-13 12:07:19 | [diff] [blame] | 122 | static base::StaticAtomicSequenceNumber cookie_seq; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 124 | PrintSettings::PrintSettings() { |
| 125 | Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | } |
| 127 | |
vmpstr | 04b8358f | 2016-02-26 01:38:29 | [diff] [blame] | 128 | PrintSettings::PrintSettings(const PrintSettings& other) = default; |
| 129 | |
erg@google.com | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 130 | PrintSettings::~PrintSettings() { |
| 131 | } |
| 132 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | void PrintSettings::Clear() { |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 134 | ranges_.clear(); |
| 135 | margin_type_ = DEFAULT_MARGINS; |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 136 | desired_dpi_ = 72; |
| 137 | selection_only_ = false; |
| 138 | title_ = base::string16(); |
| 139 | url_ = base::string16(); |
| 140 | display_header_footer_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | device_name_.clear(); |
alekseys@chromium.org | 72ddef9 | 2014-06-12 08:08:06 | [diff] [blame] | 142 | requested_media_ = RequestedMedia(); |
sanjeevr@chromium.org | 6ab86ac | 2010-05-29 07:18:29 | [diff] [blame] | 143 | page_setup_device_units_.Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | dpi_ = 0; |
| 145 | landscape_ = false; |
senorblanco@chromium.org | b2b0fce | 2011-01-12 16:34:40 | [diff] [blame] | 146 | supports_alpha_blend_ = true; |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 147 | should_print_backgrounds_ = false; |
| 148 | collate_ = false; |
| 149 | color_ = UNKNOWN_COLOR_MODEL; |
| 150 | copies_ = 0; |
| 151 | duplex_mode_ = UNKNOWN_DUPLEX_MODE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | } |
sverrir@chromium.org | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 153 | |
| 154 | void PrintSettings::SetPrinterPrintableArea( |
vitalybuka@chromium.org | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 155 | const gfx::Size& physical_size_device_units, |
| 156 | const gfx::Rect& printable_area_device_units, |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 157 | bool landscape_needs_flip) { |
vitalybuka@chromium.org | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 158 | int units_per_inch = device_units_per_inch(); |
sanjeevr@chromium.org | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 159 | int header_footer_text_height = 0; |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 160 | if (display_header_footer_) { |
sanjeevr@chromium.org | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 161 | // Hard-code text_height = 0.5cm = ~1/5 of inch. |
vandebo@chromium.org | 06bd1866 | 2011-10-07 06:12:40 | [diff] [blame] | 162 | header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice, |
| 163 | kPointsPerInch, units_per_inch); |
sanjeevr@chromium.org | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 164 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | |
sverrir@chromium.org | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 166 | PageMargins margins; |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 167 | switch (margin_type_) { |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 168 | case DEFAULT_MARGINS: { |
| 169 | // Default margins 1.0cm = ~2/5 of an inch. |
| 170 | int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, |
| 171 | units_per_inch); |
vandebo@chromium.org | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 172 | margins.header = header_footer_text_height; |
| 173 | margins.footer = header_footer_text_height; |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 174 | margins.top = margin_printer_units; |
| 175 | margins.bottom = margin_printer_units; |
| 176 | margins.left = margin_printer_units; |
| 177 | margins.right = margin_printer_units; |
| 178 | break; |
| 179 | } |
| 180 | case NO_MARGINS: |
| 181 | case PRINTABLE_AREA_MARGINS: { |
vandebo@chromium.org | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 182 | margins.header = 0; |
| 183 | margins.footer = 0; |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 184 | margins.top = 0; |
| 185 | margins.bottom = 0; |
| 186 | margins.left = 0; |
| 187 | margins.right = 0; |
| 188 | break; |
| 189 | } |
| 190 | case CUSTOM_MARGINS: { |
vandebo@chromium.org | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 191 | margins.header = 0; |
| 192 | margins.footer = 0; |
vandebo@chromium.org | b076a08 | 2011-10-20 01:26:32 | [diff] [blame] | 193 | margins.top = ConvertUnitDouble( |
| 194 | requested_custom_margins_in_points_.top, |
| 195 | kPointsPerInch, |
| 196 | units_per_inch); |
| 197 | margins.bottom = ConvertUnitDouble( |
| 198 | requested_custom_margins_in_points_.bottom, |
| 199 | kPointsPerInch, |
| 200 | units_per_inch); |
| 201 | margins.left = ConvertUnitDouble( |
| 202 | requested_custom_margins_in_points_.left, |
| 203 | kPointsPerInch, |
| 204 | units_per_inch); |
| 205 | margins.right = ConvertUnitDouble( |
| 206 | requested_custom_margins_in_points_.right, |
| 207 | kPointsPerInch, |
| 208 | units_per_inch); |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 209 | break; |
| 210 | } |
| 211 | default: { |
| 212 | NOTREACHED(); |
| 213 | } |
| 214 | } |
| 215 | |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 216 | if (margin_type_ == DEFAULT_MARGINS || margin_type_ == PRINTABLE_AREA_MARGINS) |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 217 | page_setup_device_units_.SetRequestedMargins(margins); |
| 218 | else |
| 219 | page_setup_device_units_.ForceRequestedMargins(margins); |
kmadhusu@chromium.org | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 220 | |
| 221 | page_setup_device_units_.Init(physical_size_device_units, |
| 222 | printable_area_device_units, |
| 223 | header_footer_text_height); |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 224 | if (landscape_ && landscape_needs_flip) |
| 225 | page_setup_device_units_.FlipOrientation(); |
vandebo@chromium.org | 1c23b4e | 2011-10-15 22:30:48 | [diff] [blame] | 226 | } |
| 227 | |
vandebo@chromium.org | b076a08 | 2011-10-20 01:26:32 | [diff] [blame] | 228 | void PrintSettings::SetCustomMargins( |
| 229 | const PageMargins& requested_margins_in_points) { |
| 230 | requested_custom_margins_in_points_ = requested_margins_in_points; |
vitalybuka@chromium.org | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 231 | margin_type_ = CUSTOM_MARGINS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int PrintSettings::NewCookie() { |
deanm@google.com | 13c9eec | 2008-08-06 13:42:44 | [diff] [blame] | 235 | // A cookie of 0 is used to mark a document as unassigned, count from 1. |
| 236 | return cookie_seq.GetNext() + 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | } |
| 238 | |
thestig@chromium.org | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 239 | void PrintSettings::SetOrientation(bool landscape) { |
| 240 | if (landscape_ != landscape) { |
| 241 | landscape_ = landscape; |
| 242 | page_setup_device_units_.FlipOrientation(); |
| 243 | } |
| 244 | } |
| 245 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | } // namespace printing |