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