blob: 2b9b6f4455f34ebb9bf47afc66074ae73636fdf4 [file] [log] [blame]
[email protected]8a8443f2012-03-13 12:07:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]d30e8e642008-08-06 12:05:247#include "base/atomic_sequence_num.h"
[email protected]1c23b4e82011-10-15 22:30:488#include "base/logging.h"
[email protected]06bd18662011-10-07 06:12:409#include "printing/print_job_constants.h"
[email protected]4ae30d082009-02-20 17:55:5510#include "printing/units.h"
initial.commit09911bf2008-07-26 23:55:2911
12namespace printing {
13
[email protected]fa879e512011-11-08 20:39:0114#if defined(USE_CUPS)
[email protected]63313ae2011-10-13 00:40:3915void 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]fa879e512011-11-08 20:39:0127#endif // defined(OS_MACOSX)
[email protected]63313ae2011-10-13 00:40:3928
29 color_setting_name->assign(kCUPSColorModel);
30 switch (color_mode) {
[email protected]d91db112011-10-18 20:58:5131 case COLOR:
32 color_value->assign(kColor);
[email protected]63313ae2011-10-13 00:40:3933 break;
[email protected]d91db112011-10-18 20:58:5134 case CMYK:
35 color_value->assign(kCMYK);
[email protected]63313ae2011-10-13 00:40:3936 break;
[email protected]d91db112011-10-18 20:58:5137 case PRINTOUTMODE_NORMAL:
38 color_value->assign(kNormal);
[email protected]63313ae2011-10-13 00:40:3939 color_setting_name->assign(kCUPSPrintoutMode);
40 break;
[email protected]d91db112011-10-18 20:58:5141 case PRINTOUTMODE_NORMAL_GRAY:
42 color_value->assign(kNormalGray);
[email protected]63313ae2011-10-13 00:40:3943 color_setting_name->assign(kCUPSPrintoutMode);
44 break;
[email protected]d91db112011-10-18 20:58:5145 case RGB16:
46 color_value->assign(kRGB16);
[email protected]63313ae2011-10-13 00:40:3947 break;
[email protected]d91db112011-10-18 20:58:5148 case RGBA:
49 color_value->assign(kRGBA);
[email protected]63313ae2011-10-13 00:40:3950 break;
[email protected]d91db112011-10-18 20:58:5151 case RGB:
52 color_value->assign(kRGB);
[email protected]63313ae2011-10-13 00:40:3953 break;
[email protected]d91db112011-10-18 20:58:5154 case CMY:
55 color_value->assign(kCMY);
[email protected]63313ae2011-10-13 00:40:3956 break;
[email protected]d91db112011-10-18 20:58:5157 case CMY_K:
58 color_value->assign(kCMY_K);
[email protected]63313ae2011-10-13 00:40:3959 break;
[email protected]d91db112011-10-18 20:58:5160 case BLACK:
61 color_value->assign(kBlack);
[email protected]63313ae2011-10-13 00:40:3962 break;
[email protected]d91db112011-10-18 20:58:5163 case GRAY:
64 color_value->assign(kGray);
[email protected]63313ae2011-10-13 00:40:3965 break;
[email protected]d91db112011-10-18 20:58:5166 case COLORMODE_COLOR:
[email protected]63313ae2011-10-13 00:40:3967 color_setting_name->assign(kCUPSColorMode);
[email protected]d91db112011-10-18 20:58:5168 color_value->assign(kColor);
[email protected]63313ae2011-10-13 00:40:3969 break;
[email protected]d91db112011-10-18 20:58:5170 case COLORMODE_MONOCHROME:
[email protected]63313ae2011-10-13 00:40:3971 color_setting_name->assign(kCUPSColorMode);
[email protected]d91db112011-10-18 20:58:5172 color_value->assign(kMonochrome);
[email protected]63313ae2011-10-13 00:40:3973 break;
[email protected]d91db112011-10-18 20:58:5174 case HP_COLOR_COLOR:
[email protected]63313ae2011-10-13 00:40:3975 color_setting_name->assign(kColor);
[email protected]d91db112011-10-18 20:58:5176 color_value->assign(kColor);
[email protected]63313ae2011-10-13 00:40:3977 break;
[email protected]d91db112011-10-18 20:58:5178 case HP_COLOR_BLACK:
[email protected]63313ae2011-10-13 00:40:3979 color_setting_name->assign(kColor);
[email protected]d91db112011-10-18 20:58:5180 color_value->assign(kBlack);
[email protected]63313ae2011-10-13 00:40:3981 break;
[email protected]d91db112011-10-18 20:58:5182 case PROCESSCOLORMODEL_CMYK:
[email protected]63313ae2011-10-13 00:40:3983 color_setting_name->assign(kCUPSProcessColorModel);
[email protected]d91db112011-10-18 20:58:5184 color_value->assign(kCMYK);
[email protected]63313ae2011-10-13 00:40:3985 break;
[email protected]d91db112011-10-18 20:58:5186 case PROCESSCOLORMODEL_GREYSCALE:
[email protected]63313ae2011-10-13 00:40:3987 color_setting_name->assign(kCUPSProcessColorModel);
[email protected]d91db112011-10-18 20:58:5188 color_value->assign(kGreyscale);
[email protected]63313ae2011-10-13 00:40:3989 break;
[email protected]d91db112011-10-18 20:58:5190 case PROCESSCOLORMODEL_RGB:
[email protected]63313ae2011-10-13 00:40:3991 color_setting_name->assign(kCUPSProcessColorModel);
[email protected]d91db112011-10-18 20:58:5192 color_value->assign(kRGB);
[email protected]63313ae2011-10-13 00:40:3993 break;
94 default:
[email protected]d91db112011-10-18 20:58:5195 color_value->assign(kGrayscale);
[email protected]63313ae2011-10-13 00:40:3996 break;
97 }
98}
[email protected]fa879e512011-11-08 20:39:0199#endif // defined(USE_CUPS)
[email protected]63313ae2011-10-13 00:40:39100
101bool isColorModelSelected(int model) {
[email protected]d91db112011-10-18 20:58:51102 return (model != GRAY &&
103 model != BLACK &&
104 model != PRINTOUTMODE_NORMAL_GRAY &&
105 model != COLORMODE_MONOCHROME &&
106 model != PROCESSCOLORMODEL_GREYSCALE &&
107 model != HP_COLOR_BLACK);
[email protected]63313ae2011-10-13 00:40:39108}
109
[email protected]d30e8e642008-08-06 12:05:24110// Global SequenceNumber used for generating unique cookie values.
[email protected]8a8443f2012-03-13 12:07:19111static base::StaticAtomicSequenceNumber cookie_seq;
initial.commit09911bf2008-07-26 23:55:29112
113PrintSettings::PrintSettings()
114 : min_shrink(1.25),
115 max_shrink(2.0),
116 desired_dpi(72),
[email protected]c8ad40c2009-06-08 17:05:21117 selection_only(false),
[email protected]1c23b4e82011-10-15 22:30:48118 margin_type(DEFAULT_MARGINS),
[email protected]a100f752011-08-23 00:47:23119 display_header_footer(false),
initial.commit09911bf2008-07-26 23:55:29120 dpi_(0),
[email protected]b2b0fce2011-01-12 16:34:40121 landscape_(false),
122 supports_alpha_blend_(true) {
initial.commit09911bf2008-07-26 23:55:29123}
124
[email protected]20f0487a2010-09-30 20:06:30125PrintSettings::~PrintSettings() {
126}
127
initial.commit09911bf2008-07-26 23:55:29128void PrintSettings::Clear() {
129 ranges.clear();
130 min_shrink = 1.25;
131 max_shrink = 2.;
132 desired_dpi = 72;
[email protected]c8ad40c2009-06-08 17:05:21133 selection_only = false;
[email protected]a100f752011-08-23 00:47:23134 date = string16();
135 title = string16();
136 url = string16();
137 display_header_footer = false;
initial.commit09911bf2008-07-26 23:55:29138 printer_name_.clear();
139 device_name_.clear();
[email protected]6ab86ac2010-05-29 07:18:29140 page_setup_device_units_.Clear();
initial.commit09911bf2008-07-26 23:55:29141 dpi_ = 0;
142 landscape_ = false;
[email protected]b2b0fce2011-01-12 16:34:40143 supports_alpha_blend_ = true;
initial.commit09911bf2008-07-26 23:55:29144}
145
[email protected]4ecd07452009-03-31 14:34:43146void PrintSettings::SetPrinterPrintableArea(
[email protected]6ab86ac2010-05-29 07:18:29147 gfx::Size const& physical_size_device_units,
148 gfx::Rect const& printable_area_device_units,
149 int units_per_inch) {
[email protected]38bba4f2010-03-12 05:29:07150 int header_footer_text_height = 0;
[email protected]1c23b4e82011-10-15 22:30:48151 if (display_header_footer) {
[email protected]38bba4f2010-03-12 05:29:07152 // Hard-code text_height = 0.5cm = ~1/5 of inch.
[email protected]06bd18662011-10-07 06:12:40153 header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
154 kPointsPerInch, units_per_inch);
[email protected]38bba4f2010-03-12 05:29:07155 }
initial.commit09911bf2008-07-26 23:55:29156
[email protected]4ecd07452009-03-31 14:34:43157 PageMargins margins;
[email protected]1c23b4e82011-10-15 22:30:48158 switch (margin_type) {
159 case DEFAULT_MARGINS: {
160 // Default margins 1.0cm = ~2/5 of an inch.
161 int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch,
162 units_per_inch);
[email protected]d69d322d2011-10-18 00:15:21163 margins.header = header_footer_text_height;
164 margins.footer = header_footer_text_height;
[email protected]1c23b4e82011-10-15 22:30:48165 margins.top = margin_printer_units;
166 margins.bottom = margin_printer_units;
167 margins.left = margin_printer_units;
168 margins.right = margin_printer_units;
169 break;
170 }
171 case NO_MARGINS:
172 case PRINTABLE_AREA_MARGINS: {
[email protected]d69d322d2011-10-18 00:15:21173 margins.header = 0;
174 margins.footer = 0;
[email protected]1c23b4e82011-10-15 22:30:48175 margins.top = 0;
176 margins.bottom = 0;
177 margins.left = 0;
178 margins.right = 0;
179 break;
180 }
181 case CUSTOM_MARGINS: {
[email protected]d69d322d2011-10-18 00:15:21182 margins.header = 0;
183 margins.footer = 0;
[email protected]b076a082011-10-20 01:26:32184 margins.top = ConvertUnitDouble(
185 requested_custom_margins_in_points_.top,
186 kPointsPerInch,
187 units_per_inch);
188 margins.bottom = ConvertUnitDouble(
189 requested_custom_margins_in_points_.bottom,
190 kPointsPerInch,
191 units_per_inch);
192 margins.left = ConvertUnitDouble(
193 requested_custom_margins_in_points_.left,
194 kPointsPerInch,
195 units_per_inch);
196 margins.right = ConvertUnitDouble(
197 requested_custom_margins_in_points_.right,
198 kPointsPerInch,
199 units_per_inch);
[email protected]1c23b4e82011-10-15 22:30:48200 break;
201 }
202 default: {
203 NOTREACHED();
204 }
205 }
206
207 if (margin_type == DEFAULT_MARGINS || margin_type == PRINTABLE_AREA_MARGINS)
208 page_setup_device_units_.SetRequestedMargins(margins);
209 else
210 page_setup_device_units_.ForceRequestedMargins(margins);
[email protected]b89615d2011-11-04 00:29:21211
212 page_setup_device_units_.Init(physical_size_device_units,
213 printable_area_device_units,
214 header_footer_text_height);
[email protected]1c23b4e82011-10-15 22:30:48215}
216
[email protected]b076a082011-10-20 01:26:32217void PrintSettings::SetCustomMargins(
218 const PageMargins& requested_margins_in_points) {
219 requested_custom_margins_in_points_ = requested_margins_in_points;
[email protected]1c23b4e82011-10-15 22:30:48220 margin_type = CUSTOM_MARGINS;
initial.commit09911bf2008-07-26 23:55:29221}
222
initial.commit09911bf2008-07-26 23:55:29223bool PrintSettings::Equals(const PrintSettings& rhs) const {
224 // Do not test the display device name (printer_name_) for equality since it
225 // may sometimes be chopped off at 30 chars. As long as device_name is the
226 // same, that's fine.
227 return ranges == rhs.ranges &&
228 min_shrink == rhs.min_shrink &&
229 max_shrink == rhs.max_shrink &&
230 desired_dpi == rhs.desired_dpi &&
initial.commit09911bf2008-07-26 23:55:29231 device_name_ == rhs.device_name_ &&
[email protected]6ab86ac2010-05-29 07:18:29232 page_setup_device_units_.Equals(rhs.page_setup_device_units_) &&
initial.commit09911bf2008-07-26 23:55:29233 dpi_ == rhs.dpi_ &&
234 landscape_ == rhs.landscape_;
235}
236
237int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44238 // A cookie of 0 is used to mark a document as unassigned, count from 1.
239 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29240}
241
[email protected]c48bee22011-03-29 02:36:26242void PrintSettings::SetOrientation(bool landscape) {
243 if (landscape_ != landscape) {
244 landscape_ = landscape;
245 page_setup_device_units_.FlipOrientation();
246 }
247}
248
initial.commit09911bf2008-07-26 23:55:29249} // namespace printing