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