blob: e665c17022172c1c3a153d692ce50abfb9bd27ab [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),
[email protected]19d1c2d2013-01-14 00:59:46120 should_print_backgrounds(false),
initial.commit09911bf2008-07-26 23:55:29121 dpi_(0),
[email protected]b2b0fce2011-01-12 16:34:40122 landscape_(false),
123 supports_alpha_blend_(true) {
initial.commit09911bf2008-07-26 23:55:29124}
125
[email protected]20f0487a2010-09-30 20:06:30126PrintSettings::~PrintSettings() {
127}
128
initial.commit09911bf2008-07-26 23:55:29129void PrintSettings::Clear() {
130 ranges.clear();
131 min_shrink = 1.25;
132 max_shrink = 2.;
133 desired_dpi = 72;
[email protected]c8ad40c2009-06-08 17:05:21134 selection_only = false;
[email protected]a100f752011-08-23 00:47:23135 date = string16();
136 title = string16();
137 url = string16();
138 display_header_footer = false;
initial.commit09911bf2008-07-26 23:55:29139 printer_name_.clear();
140 device_name_.clear();
[email protected]6ab86ac2010-05-29 07:18:29141 page_setup_device_units_.Clear();
initial.commit09911bf2008-07-26 23:55:29142 dpi_ = 0;
143 landscape_ = false;
[email protected]b2b0fce2011-01-12 16:34:40144 supports_alpha_blend_ = true;
[email protected]19d1c2d2013-01-14 00:59:46145 should_print_backgrounds = false;
initial.commit09911bf2008-07-26 23:55:29146}
147
[email protected]4ecd07452009-03-31 14:34:43148void PrintSettings::SetPrinterPrintableArea(
[email protected]6ab86ac2010-05-29 07:18:29149 gfx::Size const& physical_size_device_units,
150 gfx::Rect const& printable_area_device_units,
151 int units_per_inch) {
[email protected]38bba4f2010-03-12 05:29:07152 int header_footer_text_height = 0;
[email protected]1c23b4e82011-10-15 22:30:48153 if (display_header_footer) {
[email protected]38bba4f2010-03-12 05:29:07154 // Hard-code text_height = 0.5cm = ~1/5 of inch.
[email protected]06bd18662011-10-07 06:12:40155 header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
156 kPointsPerInch, units_per_inch);
[email protected]38bba4f2010-03-12 05:29:07157 }
initial.commit09911bf2008-07-26 23:55:29158
[email protected]4ecd07452009-03-31 14:34:43159 PageMargins margins;
[email protected]1c23b4e82011-10-15 22:30:48160 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);
[email protected]d69d322d2011-10-18 00:15:21165 margins.header = header_footer_text_height;
166 margins.footer = header_footer_text_height;
[email protected]1c23b4e82011-10-15 22:30:48167 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: {
[email protected]d69d322d2011-10-18 00:15:21175 margins.header = 0;
176 margins.footer = 0;
[email protected]1c23b4e82011-10-15 22:30:48177 margins.top = 0;
178 margins.bottom = 0;
179 margins.left = 0;
180 margins.right = 0;
181 break;
182 }
183 case CUSTOM_MARGINS: {
[email protected]d69d322d2011-10-18 00:15:21184 margins.header = 0;
185 margins.footer = 0;
[email protected]b076a082011-10-20 01:26:32186 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);
[email protected]1c23b4e82011-10-15 22:30:48202 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);
[email protected]b89615d2011-11-04 00:29:21213
214 page_setup_device_units_.Init(physical_size_device_units,
215 printable_area_device_units,
216 header_footer_text_height);
[email protected]1c23b4e82011-10-15 22:30:48217}
218
[email protected]b076a082011-10-20 01:26:32219void PrintSettings::SetCustomMargins(
220 const PageMargins& requested_margins_in_points) {
221 requested_custom_margins_in_points_ = requested_margins_in_points;
[email protected]1c23b4e82011-10-15 22:30:48222 margin_type = CUSTOM_MARGINS;
initial.commit09911bf2008-07-26 23:55:29223}
224
initial.commit09911bf2008-07-26 23:55:29225bool 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 &&
initial.commit09911bf2008-07-26 23:55:29233 device_name_ == rhs.device_name_ &&
[email protected]6ab86ac2010-05-29 07:18:29234 page_setup_device_units_.Equals(rhs.page_setup_device_units_) &&
initial.commit09911bf2008-07-26 23:55:29235 dpi_ == rhs.dpi_ &&
[email protected]19d1c2d2013-01-14 00:59:46236 landscape_ == rhs.landscape_ &&
237 should_print_backgrounds == rhs.should_print_backgrounds;
initial.commit09911bf2008-07-26 23:55:29238}
239
240int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44241 // A cookie of 0 is used to mark a document as unassigned, count from 1.
242 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29243}
244
[email protected]c48bee22011-03-29 02:36:26245void PrintSettings::SetOrientation(bool landscape) {
246 if (landscape_ != landscape) {
247 landscape_ = landscape;
248 page_setup_device_units_.FlipOrientation();
249 }
250}
251
initial.commit09911bf2008-07-26 23:55:29252} // namespace printing