[email protected] | 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 | |
[email protected] | 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 | |
[email protected] | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 7 | #include "base/atomic_sequence_num.h" |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 06bd1866 | 2011-10-07 06:12:40 | [diff] [blame] | 9 | #include "printing/print_job_constants.h" |
[email protected] | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 10 | #include "printing/units.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
| 12 | namespace printing { |
| 13 | |
[email protected] | fa879e51 | 2011-11-08 20:39:01 | [diff] [blame] | 14 | #if defined(USE_CUPS) |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 15 | void GetColorModelForMode( |
| 16 | int color_mode, std::string* color_setting_name, std::string* color_value) { |
| 17 | #if defined(OS_MACOSX) |
| 18 | const char kCUPSColorMode[] = "ColorMode"; |
| 19 | const char kCUPSColorModel[] = "ColorModel"; |
| 20 | const char kCUPSPrintoutMode[] = "PrintoutMode"; |
| 21 | const char kCUPSProcessColorModel[] = "ProcessColorModel"; |
| 22 | #else |
| 23 | const char kCUPSColorMode[] = "cups-ColorMode"; |
| 24 | const char kCUPSColorModel[] = "cups-ColorModel"; |
| 25 | const char kCUPSPrintoutMode[] = "cups-PrintoutMode"; |
| 26 | const char kCUPSProcessColorModel[] = "cups-ProcessColorModel"; |
[email protected] | fa879e51 | 2011-11-08 20:39:01 | [diff] [blame] | 27 | #endif // defined(OS_MACOSX) |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 28 | |
| 29 | color_setting_name->assign(kCUPSColorModel); |
| 30 | switch (color_mode) { |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 31 | case COLOR: |
| 32 | color_value->assign(kColor); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 33 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 34 | case CMYK: |
| 35 | color_value->assign(kCMYK); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 36 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 37 | case PRINTOUTMODE_NORMAL: |
| 38 | color_value->assign(kNormal); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 39 | color_setting_name->assign(kCUPSPrintoutMode); |
| 40 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 41 | case PRINTOUTMODE_NORMAL_GRAY: |
| 42 | color_value->assign(kNormalGray); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 43 | color_setting_name->assign(kCUPSPrintoutMode); |
| 44 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 45 | case RGB16: |
| 46 | color_value->assign(kRGB16); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 47 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 48 | case RGBA: |
| 49 | color_value->assign(kRGBA); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 50 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 51 | case RGB: |
| 52 | color_value->assign(kRGB); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 53 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 54 | case CMY: |
| 55 | color_value->assign(kCMY); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 56 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 57 | case CMY_K: |
| 58 | color_value->assign(kCMY_K); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 59 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 60 | case BLACK: |
| 61 | color_value->assign(kBlack); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 62 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 63 | case GRAY: |
| 64 | color_value->assign(kGray); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 65 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 66 | case COLORMODE_COLOR: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 67 | color_setting_name->assign(kCUPSColorMode); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 68 | color_value->assign(kColor); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 69 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 70 | case COLORMODE_MONOCHROME: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 71 | color_setting_name->assign(kCUPSColorMode); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 72 | color_value->assign(kMonochrome); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 73 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 74 | case HP_COLOR_COLOR: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 75 | color_setting_name->assign(kColor); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 76 | color_value->assign(kColor); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 77 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 78 | case HP_COLOR_BLACK: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 79 | color_setting_name->assign(kColor); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 80 | color_value->assign(kBlack); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 81 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 82 | case PROCESSCOLORMODEL_CMYK: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 83 | color_setting_name->assign(kCUPSProcessColorModel); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 84 | color_value->assign(kCMYK); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 85 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 86 | case PROCESSCOLORMODEL_GREYSCALE: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 87 | color_setting_name->assign(kCUPSProcessColorModel); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 88 | color_value->assign(kGreyscale); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 89 | break; |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 90 | case PROCESSCOLORMODEL_RGB: |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 91 | color_setting_name->assign(kCUPSProcessColorModel); |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 92 | color_value->assign(kRGB); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 93 | break; |
| 94 | default: |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 95 | color_value->assign(kGrayscale); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | } |
[email protected] | fa879e51 | 2011-11-08 20:39:01 | [diff] [blame] | 99 | #endif // defined(USE_CUPS) |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 100 | |
[email protected] | 675a1ae | 2013-10-14 20:24:37 | [diff] [blame] | 101 | bool IsColorModelSelected(int color_mode) { |
| 102 | return (color_mode != GRAY && |
| 103 | color_mode != BLACK && |
| 104 | color_mode != PRINTOUTMODE_NORMAL_GRAY && |
| 105 | color_mode != COLORMODE_MONOCHROME && |
| 106 | color_mode != PROCESSCOLORMODEL_GREYSCALE && |
| 107 | color_mode != HP_COLOR_BLACK); |
[email protected] | 63313ae | 2011-10-13 00:40:39 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 110 | // Global SequenceNumber used for generating unique cookie values. |
[email protected] | 8a8443f | 2012-03-13 12:07:19 | [diff] [blame] | 111 | static base::StaticAtomicSequenceNumber cookie_seq; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
| 113 | PrintSettings::PrintSettings() |
| 114 | : min_shrink(1.25), |
| 115 | max_shrink(2.0), |
| 116 | desired_dpi(72), |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 117 | selection_only(false), |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 118 | margin_type(DEFAULT_MARGINS), |
[email protected] | a100f75 | 2011-08-23 00:47:23 | [diff] [blame] | 119 | display_header_footer(false), |
[email protected] | 19d1c2d | 2013-01-14 00:59:46 | [diff] [blame] | 120 | should_print_backgrounds(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | dpi_(0), |
[email protected] | b2b0fce | 2011-01-12 16:34:40 | [diff] [blame] | 122 | landscape_(false), |
| 123 | supports_alpha_blend_(true) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 126 | PrintSettings::~PrintSettings() { |
| 127 | } |
| 128 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | void PrintSettings::Clear() { |
| 130 | ranges.clear(); |
| 131 | min_shrink = 1.25; |
| 132 | max_shrink = 2.; |
| 133 | desired_dpi = 72; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 134 | selection_only = false; |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 135 | title = base::string16(); |
| 136 | url = base::string16(); |
[email protected] | a100f75 | 2011-08-23 00:47:23 | [diff] [blame] | 137 | display_header_footer = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | printer_name_.clear(); |
| 139 | device_name_.clear(); |
[email protected] | 6ab86ac | 2010-05-29 07:18:29 | [diff] [blame] | 140 | page_setup_device_units_.Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | dpi_ = 0; |
| 142 | landscape_ = false; |
[email protected] | b2b0fce | 2011-01-12 16:34:40 | [diff] [blame] | 143 | supports_alpha_blend_ = true; |
[email protected] | 19d1c2d | 2013-01-14 00:59:46 | [diff] [blame] | 144 | should_print_backgrounds = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 147 | void PrintSettings::SetPrinterPrintableArea( |
[email protected] | 6ab86ac | 2010-05-29 07:18:29 | [diff] [blame] | 148 | gfx::Size const& physical_size_device_units, |
| 149 | gfx::Rect const& printable_area_device_units, |
| 150 | int units_per_inch) { |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 151 | int header_footer_text_height = 0; |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 152 | if (display_header_footer) { |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 153 | // Hard-code text_height = 0.5cm = ~1/5 of inch. |
[email protected] | 06bd1866 | 2011-10-07 06:12:40 | [diff] [blame] | 154 | header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice, |
| 155 | kPointsPerInch, units_per_inch); |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 156 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 158 | PageMargins margins; |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 159 | switch (margin_type) { |
| 160 | case DEFAULT_MARGINS: { |
| 161 | // Default margins 1.0cm = ~2/5 of an inch. |
| 162 | int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, |
| 163 | units_per_inch); |
[email protected] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 164 | margins.header = header_footer_text_height; |
| 165 | margins.footer = header_footer_text_height; |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 166 | margins.top = margin_printer_units; |
| 167 | margins.bottom = margin_printer_units; |
| 168 | margins.left = margin_printer_units; |
| 169 | margins.right = margin_printer_units; |
| 170 | break; |
| 171 | } |
| 172 | case NO_MARGINS: |
| 173 | case PRINTABLE_AREA_MARGINS: { |
[email protected] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 174 | margins.header = 0; |
| 175 | margins.footer = 0; |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 176 | margins.top = 0; |
| 177 | margins.bottom = 0; |
| 178 | margins.left = 0; |
| 179 | margins.right = 0; |
| 180 | break; |
| 181 | } |
| 182 | case CUSTOM_MARGINS: { |
[email protected] | d69d322d | 2011-10-18 00:15:21 | [diff] [blame] | 183 | margins.header = 0; |
| 184 | margins.footer = 0; |
[email protected] | b076a08 | 2011-10-20 01:26:32 | [diff] [blame] | 185 | margins.top = ConvertUnitDouble( |
| 186 | requested_custom_margins_in_points_.top, |
| 187 | kPointsPerInch, |
| 188 | units_per_inch); |
| 189 | margins.bottom = ConvertUnitDouble( |
| 190 | requested_custom_margins_in_points_.bottom, |
| 191 | kPointsPerInch, |
| 192 | units_per_inch); |
| 193 | margins.left = ConvertUnitDouble( |
| 194 | requested_custom_margins_in_points_.left, |
| 195 | kPointsPerInch, |
| 196 | units_per_inch); |
| 197 | margins.right = ConvertUnitDouble( |
| 198 | requested_custom_margins_in_points_.right, |
| 199 | kPointsPerInch, |
| 200 | units_per_inch); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | default: { |
| 204 | NOTREACHED(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if (margin_type == DEFAULT_MARGINS || margin_type == PRINTABLE_AREA_MARGINS) |
| 209 | page_setup_device_units_.SetRequestedMargins(margins); |
| 210 | else |
| 211 | page_setup_device_units_.ForceRequestedMargins(margins); |
[email protected] | b89615d | 2011-11-04 00:29:21 | [diff] [blame] | 212 | |
| 213 | page_setup_device_units_.Init(physical_size_device_units, |
| 214 | printable_area_device_units, |
| 215 | header_footer_text_height); |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | b076a08 | 2011-10-20 01:26:32 | [diff] [blame] | 218 | void PrintSettings::SetCustomMargins( |
| 219 | const PageMargins& requested_margins_in_points) { |
| 220 | requested_custom_margins_in_points_ = requested_margins_in_points; |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 221 | margin_type = CUSTOM_MARGINS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | } |
| 223 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 224 | bool PrintSettings::Equals(const PrintSettings& rhs) const { |
| 225 | // Do not test the display device name (printer_name_) for equality since it |
| 226 | // may sometimes be chopped off at 30 chars. As long as device_name is the |
| 227 | // same, that's fine. |
| 228 | return ranges == rhs.ranges && |
| 229 | min_shrink == rhs.min_shrink && |
| 230 | max_shrink == rhs.max_shrink && |
| 231 | desired_dpi == rhs.desired_dpi && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | device_name_ == rhs.device_name_ && |
[email protected] | 6ab86ac | 2010-05-29 07:18:29 | [diff] [blame] | 233 | page_setup_device_units_.Equals(rhs.page_setup_device_units_) && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | dpi_ == rhs.dpi_ && |
[email protected] | 19d1c2d | 2013-01-14 00:59:46 | [diff] [blame] | 235 | landscape_ == rhs.landscape_ && |
| 236 | should_print_backgrounds == rhs.should_print_backgrounds; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | int PrintSettings::NewCookie() { |
[email protected] | 13c9eec | 2008-08-06 13:42:44 | [diff] [blame] | 240 | // A cookie of 0 is used to mark a document as unassigned, count from 1. |
| 241 | return cookie_seq.GetNext() + 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 244 | void PrintSettings::SetOrientation(bool landscape) { |
| 245 | if (landscape_ != landscape) { |
| 246 | landscape_ = landscape; |
| 247 | page_setup_device_units_.FlipOrientation(); |
| 248 | } |
| 249 | } |
| 250 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | } // namespace printing |