1 // Copyright (c) 2011 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.
5 #include "printing/print_settings.h"
7 #include "base/atomic_sequence_num.h"
8 #include "base/logging.h"
9 #include "printing/print_job_constants.h"
10 #include "printing/units.h"
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";
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)
29 color_setting_name
->assign(kCUPSColorModel
);
32 color_value
->assign(kColor
);
35 color_value
->assign(kCMYK
);
37 case PRINTOUTMODE_NORMAL
:
38 color_value
->assign(kNormal
);
39 color_setting_name
->assign(kCUPSPrintoutMode
);
41 case PRINTOUTMODE_NORMAL_GRAY
:
42 color_value
->assign(kNormalGray
);
43 color_setting_name
->assign(kCUPSPrintoutMode
);
46 color_value
->assign(kRGB16
);
49 color_value
->assign(kRGBA
);
52 color_value
->assign(kRGB
);
55 color_value
->assign(kCMY
);
58 color_value
->assign(kCMY_K
);
61 color_value
->assign(kBlack
);
64 color_value
->assign(kGray
);
67 color_setting_name
->assign(kCUPSColorMode
);
68 color_value
->assign(kColor
);
70 case COLORMODE_MONOCHROME
:
71 color_setting_name
->assign(kCUPSColorMode
);
72 color_value
->assign(kMonochrome
);
75 color_setting_name
->assign(kColor
);
76 color_value
->assign(kColor
);
79 color_setting_name
->assign(kColor
);
80 color_value
->assign(kBlack
);
82 case PROCESSCOLORMODEL_CMYK
:
83 color_setting_name
->assign(kCUPSProcessColorModel
);
84 color_value
->assign(kCMYK
);
86 case PROCESSCOLORMODEL_GREYSCALE
:
87 color_setting_name
->assign(kCUPSProcessColorModel
);
88 color_value
->assign(kGreyscale
);
90 case PROCESSCOLORMODEL_RGB
:
91 color_setting_name
->assign(kCUPSProcessColorModel
);
92 color_value
->assign(kRGB
);
95 color_value
->assign(kGrayscale
);
99 #endif // defined(USE_CUPS)
101 bool isColorModelSelected(int model
) {
102 return (model
!= GRAY
&&
104 model
!= PRINTOUTMODE_NORMAL_GRAY
&&
105 model
!= COLORMODE_MONOCHROME
&&
106 model
!= PROCESSCOLORMODEL_GREYSCALE
&&
107 model
!= HP_COLOR_BLACK
);
110 // Global SequenceNumber used for generating unique cookie values.
111 static base::AtomicSequenceNumber
cookie_seq(base::LINKER_INITIALIZED
);
113 PrintSettings::PrintSettings()
117 selection_only(false),
118 margin_type(DEFAULT_MARGINS
),
119 display_header_footer(false),
122 supports_alpha_blend_(true) {
125 PrintSettings::~PrintSettings() {
128 void PrintSettings::Clear() {
133 selection_only
= false;
137 display_header_footer
= false;
138 printer_name_
.clear();
139 device_name_
.clear();
140 page_setup_device_units_
.Clear();
143 supports_alpha_blend_
= true;
146 void PrintSettings::SetPrinterPrintableArea(
147 gfx::Size
const& physical_size_device_units
,
148 gfx::Rect
const& printable_area_device_units
,
149 int units_per_inch
) {
150 int header_footer_text_height
= 0;
151 if (display_header_footer
) {
152 // Hard-code text_height = 0.5cm = ~1/5 of inch.
153 header_footer_text_height
= ConvertUnit(kSettingHeaderFooterInterstice
,
154 kPointsPerInch
, units_per_inch
);
158 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
,
163 margins
.header
= header_footer_text_height
;
164 margins
.footer
= header_footer_text_height
;
165 margins
.top
= margin_printer_units
;
166 margins
.bottom
= margin_printer_units
;
167 margins
.left
= margin_printer_units
;
168 margins
.right
= margin_printer_units
;
172 case PRINTABLE_AREA_MARGINS
: {
181 case CUSTOM_MARGINS
: {
184 margins
.top
= ConvertUnitDouble(
185 requested_custom_margins_in_points_
.top
,
188 margins
.bottom
= ConvertUnitDouble(
189 requested_custom_margins_in_points_
.bottom
,
192 margins
.left
= ConvertUnitDouble(
193 requested_custom_margins_in_points_
.left
,
196 margins
.right
= ConvertUnitDouble(
197 requested_custom_margins_in_points_
.right
,
207 if (margin_type
== DEFAULT_MARGINS
|| margin_type
== PRINTABLE_AREA_MARGINS
)
208 page_setup_device_units_
.SetRequestedMargins(margins
);
210 page_setup_device_units_
.ForceRequestedMargins(margins
);
212 page_setup_device_units_
.Init(physical_size_device_units
,
213 printable_area_device_units
,
214 header_footer_text_height
);
217 void PrintSettings::SetCustomMargins(
218 const PageMargins
& requested_margins_in_points
) {
219 requested_custom_margins_in_points_
= requested_margins_in_points
;
220 margin_type
= CUSTOM_MARGINS
;
223 bool 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
&&
231 device_name_
== rhs
.device_name_
&&
232 page_setup_device_units_
.Equals(rhs
.page_setup_device_units_
) &&
234 landscape_
== rhs
.landscape_
;
237 int PrintSettings::NewCookie() {
238 // A cookie of 0 is used to mark a document as unassigned, count from 1.
239 return cookie_seq
.GetNext() + 1;
242 void PrintSettings::SetOrientation(bool landscape
) {
243 if (landscape_
!= landscape
) {
244 landscape_
= landscape
;
245 page_setup_device_units_
.FlipOrientation();
249 } // namespace printing