blob: 0e1a74304a965d5a9c7501eee06d0290c3b0c602 [file] [log] [blame]
[email protected]c48bee22011-03-29 02:36:261// Copyright (c) 2011 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]63313ae2011-10-13 00:40:3914#if defined (USE_CUPS)
15void 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
101bool 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]d30e8e642008-08-06 12:05:24110// Global SequenceNumber used for generating unique cookie values.
[email protected]a10a16b2008-09-02 13:11:46111static base::AtomicSequenceNumber cookie_seq(base::LINKER_INITIALIZED);
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 }
[email protected]6ab86ac2010-05-29 07:18:29156 page_setup_device_units_.Init(physical_size_device_units,
157 printable_area_device_units,
158 header_footer_text_height);
initial.commit09911bf2008-07-26 23:55:29159
[email protected]4ecd07452009-03-31 14:34:43160 PageMargins margins;
[email protected]be3b19a2009-07-13 14:58:18161 margins.header = header_footer_text_height;
162 margins.footer = header_footer_text_height;
[email protected]1c23b4e82011-10-15 22:30:48163 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
208void PrintSettings::SetCustomMargins(const PageMargins& margins_in_points) {
209 custom_margins_in_points_ = margins_in_points;
210 margin_type = CUSTOM_MARGINS;
initial.commit09911bf2008-07-26 23:55:29211}
212
initial.commit09911bf2008-07-26 23:55:29213bool 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.commit09911bf2008-07-26 23:55:29221 device_name_ == rhs.device_name_ &&
[email protected]6ab86ac2010-05-29 07:18:29222 page_setup_device_units_.Equals(rhs.page_setup_device_units_) &&
initial.commit09911bf2008-07-26 23:55:29223 dpi_ == rhs.dpi_ &&
224 landscape_ == rhs.landscape_;
225}
226
227int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44228 // A cookie of 0 is used to mark a document as unassigned, count from 1.
229 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29230}
231
[email protected]c48bee22011-03-29 02:36:26232void PrintSettings::SetOrientation(bool landscape) {
233 if (landscape_ != landscape) {
234 landscape_ = landscape;
235 page_setup_device_units_.FlipOrientation();
236 }
237}
238
initial.commit09911bf2008-07-26 23:55:29239} // namespace printing