blob: 05a7869fcb93d8b84d3fd6ed42b75edb1a6ac8d1 [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
[email protected]675a1ae2013-10-14 20:24:37101bool 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]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]b5fa4ee2013-10-01 07:19:07135 title = base::string16();
136 url = base::string16();
[email protected]a100f752011-08-23 00:47:23137 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;
[email protected]19d1c2d2013-01-14 00:59:46144 should_print_backgrounds = false;
initial.commit09911bf2008-07-26 23:55:29145}
146
[email protected]4ecd07452009-03-31 14:34:43147void PrintSettings::SetPrinterPrintableArea(
[email protected]6ab86ac2010-05-29 07:18:29148 gfx::Size const& physical_size_device_units,
149 gfx::Rect const& printable_area_device_units,
150 int units_per_inch) {
[email protected]38bba4f2010-03-12 05:29:07151 int header_footer_text_height = 0;
[email protected]1c23b4e82011-10-15 22:30:48152 if (display_header_footer) {
[email protected]38bba4f2010-03-12 05:29:07153 // Hard-code text_height = 0.5cm = ~1/5 of inch.
[email protected]06bd18662011-10-07 06:12:40154 header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
155 kPointsPerInch, units_per_inch);
[email protected]38bba4f2010-03-12 05:29:07156 }
initial.commit09911bf2008-07-26 23:55:29157
[email protected]4ecd07452009-03-31 14:34:43158 PageMargins margins;
[email protected]1c23b4e82011-10-15 22:30:48159 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]d69d322d2011-10-18 00:15:21164 margins.header = header_footer_text_height;
165 margins.footer = header_footer_text_height;
[email protected]1c23b4e82011-10-15 22:30:48166 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]d69d322d2011-10-18 00:15:21174 margins.header = 0;
175 margins.footer = 0;
[email protected]1c23b4e82011-10-15 22:30:48176 margins.top = 0;
177 margins.bottom = 0;
178 margins.left = 0;
179 margins.right = 0;
180 break;
181 }
182 case CUSTOM_MARGINS: {
[email protected]d69d322d2011-10-18 00:15:21183 margins.header = 0;
184 margins.footer = 0;
[email protected]b076a082011-10-20 01:26:32185 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]1c23b4e82011-10-15 22:30:48201 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]b89615d2011-11-04 00:29:21212
213 page_setup_device_units_.Init(physical_size_device_units,
214 printable_area_device_units,
215 header_footer_text_height);
[email protected]1c23b4e82011-10-15 22:30:48216}
217
[email protected]b076a082011-10-20 01:26:32218void PrintSettings::SetCustomMargins(
219 const PageMargins& requested_margins_in_points) {
220 requested_custom_margins_in_points_ = requested_margins_in_points;
[email protected]1c23b4e82011-10-15 22:30:48221 margin_type = CUSTOM_MARGINS;
initial.commit09911bf2008-07-26 23:55:29222}
223
initial.commit09911bf2008-07-26 23:55:29224bool 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.commit09911bf2008-07-26 23:55:29232 device_name_ == rhs.device_name_ &&
[email protected]6ab86ac2010-05-29 07:18:29233 page_setup_device_units_.Equals(rhs.page_setup_device_units_) &&
initial.commit09911bf2008-07-26 23:55:29234 dpi_ == rhs.dpi_ &&
[email protected]19d1c2d2013-01-14 00:59:46235 landscape_ == rhs.landscape_ &&
236 should_print_backgrounds == rhs.should_print_backgrounds;
initial.commit09911bf2008-07-26 23:55:29237}
238
239int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44240 // A cookie of 0 is used to mark a document as unassigned, count from 1.
241 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29242}
243
[email protected]c48bee22011-03-29 02:36:26244void PrintSettings::SetOrientation(bool landscape) {
245 if (landscape_ != landscape) {
246 landscape_ = landscape;
247 page_setup_device_units_.FlipOrientation();
248 }
249}
250
initial.commit09911bf2008-07-26 23:55:29251} // namespace printing