forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_settings.cc
298 lines (277 loc) · 8.96 KB
/
print_settings.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/print_settings.h"
#include "base/atomic_sequence_num.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "printing/print_job_constants.h"
#include "printing/units.h"
namespace printing {
base::LazyInstance<std::string>::Leaky g_user_agent;
void SetAgent(const std::string& user_agent) {
g_user_agent.Get() = user_agent;
}
const std::string& GetAgent() {
return g_user_agent.Get();
}
#if defined(USE_CUPS)
void GetColorModelForMode(
int color_mode, std::string* color_setting_name, std::string* color_value) {
#if defined(OS_MACOSX)
constexpr char kCUPSColorMode[] = "ColorMode";
constexpr char kCUPSColorModel[] = "ColorModel";
constexpr char kCUPSPrintoutMode[] = "PrintoutMode";
constexpr char kCUPSProcessColorModel[] = "ProcessColorModel";
constexpr char kCUPSBrotherMonoColor[] = "BRMonoColor";
constexpr char kCUPSBrotherPrintQuality[] = "BRPrintQuality";
#else
constexpr char kCUPSColorMode[] = "cups-ColorMode";
constexpr char kCUPSColorModel[] = "cups-ColorModel";
constexpr char kCUPSPrintoutMode[] = "cups-PrintoutMode";
constexpr char kCUPSProcessColorModel[] = "cups-ProcessColorModel";
constexpr char kCUPSBrotherMonoColor[] = "cups-BRMonoColor";
constexpr char kCUPSBrotherPrintQuality[] = "cups-BRPrintQuality";
#endif // defined(OS_MACOSX)
color_setting_name->assign(kCUPSColorModel);
switch (color_mode) {
case GRAY:
color_value->assign(kGray);
break;
case COLOR:
color_value->assign(kColor);
break;
case CMYK:
color_value->assign(kCMYK);
break;
case CMY:
color_value->assign(kCMY);
break;
case KCMY:
color_value->assign(kKCMY);
break;
case CMY_K:
color_value->assign(kCMY_K);
break;
case BLACK:
color_value->assign(kBlack);
break;
case GRAYSCALE:
color_value->assign(kGrayscale);
break;
case RGB:
color_value->assign(kRGB);
break;
case RGB16:
color_value->assign(kRGB16);
break;
case RGBA:
color_value->assign(kRGBA);
break;
case COLORMODE_COLOR:
color_setting_name->assign(kCUPSColorMode);
color_value->assign(kColor);
break;
case COLORMODE_MONOCHROME:
color_setting_name->assign(kCUPSColorMode);
color_value->assign(kMonochrome);
break;
case HP_COLOR_COLOR:
color_setting_name->assign(kColor);
color_value->assign(kColor);
break;
case HP_COLOR_BLACK:
color_setting_name->assign(kColor);
color_value->assign(kBlack);
break;
case PRINTOUTMODE_NORMAL:
color_setting_name->assign(kCUPSPrintoutMode);
color_value->assign(kNormal);
break;
case PRINTOUTMODE_NORMAL_GRAY:
color_setting_name->assign(kCUPSPrintoutMode);
color_value->assign(kNormalGray);
break;
case PROCESSCOLORMODEL_CMYK:
color_setting_name->assign(kCUPSProcessColorModel);
color_value->assign(kCMYK);
break;
case PROCESSCOLORMODEL_GREYSCALE:
color_setting_name->assign(kCUPSProcessColorModel);
color_value->assign(kGreyscale);
break;
case PROCESSCOLORMODEL_RGB:
color_setting_name->assign(kCUPSProcessColorModel);
color_value->assign(kRGB);
break;
case BROTHER_CUPS_COLOR:
color_setting_name->assign(kCUPSBrotherMonoColor);
color_value->assign(kFullColor);
break;
case BROTHER_CUPS_MONO:
color_setting_name->assign(kCUPSBrotherMonoColor);
color_value->assign(kMono);
break;
case BROTHER_BRSCRIPT3_COLOR:
color_setting_name->assign(kCUPSBrotherPrintQuality);
color_value->assign(kColor);
break;
case BROTHER_BRSCRIPT3_BLACK:
color_setting_name->assign(kCUPSBrotherPrintQuality);
color_value->assign(kBlack);
break;
default:
color_value->assign(kGrayscale);
break;
}
}
#endif // defined(USE_CUPS)
bool IsColorModelSelected(int color_mode) {
return (color_mode != GRAY && color_mode != BLACK &&
color_mode != PRINTOUTMODE_NORMAL_GRAY &&
color_mode != COLORMODE_MONOCHROME &&
color_mode != PROCESSCOLORMODEL_GREYSCALE &&
color_mode != BROTHER_CUPS_MONO &&
color_mode != BROTHER_BRSCRIPT3_BLACK &&
color_mode != HP_COLOR_BLACK);
}
// Global SequenceNumber used for generating unique cookie values.
static base::AtomicSequenceNumber cookie_seq;
PrintSettings::PrintSettings() {
Clear();
}
PrintSettings::PrintSettings(const PrintSettings& other) = default;
PrintSettings::~PrintSettings() = default;
void PrintSettings::Clear() {
ranges_.clear();
selection_only_ = false;
margin_type_ = DEFAULT_MARGINS;
title_.clear();
url_.clear();
display_header_footer_ = false;
should_print_backgrounds_ = false;
collate_ = false;
color_ = UNKNOWN_COLOR_MODEL;
copies_ = 0;
duplex_mode_ = UNKNOWN_DUPLEX_MODE;
device_name_.clear();
requested_media_ = RequestedMedia();
page_setup_device_units_.Clear();
dpi_ = gfx::Size();
scale_factor_ = 1.0f;
rasterize_pdf_ = false;
landscape_ = false;
supports_alpha_blend_ = true;
#if defined(OS_WIN)
print_text_with_gdi_ = false;
printer_type_ = PrintSettings::PrinterType::TYPE_NONE;
#endif
is_modifiable_ = true;
pages_per_sheet_ = 1;
}
void PrintSettings::SetPrinterPrintableArea(
const gfx::Size& physical_size_device_units,
const gfx::Rect& printable_area_device_units,
bool landscape_needs_flip) {
int units_per_inch = device_units_per_inch();
int header_footer_text_height = 0;
if (display_header_footer_) {
// Hard-code text_height = 0.5cm = ~1/5 of inch.
header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
kPointsPerInch, units_per_inch);
}
PageMargins margins;
bool small_paper_size = false;
switch (margin_type_) {
case DEFAULT_MARGINS: {
// Default margins 1.0cm = ~2/5 of an inch, unless a page dimension is
// less than 2.54 cm = ~1 inch, in which case set the margins in that
// dimension to 0.
static constexpr double kCmInMicrons = 10000;
int margin_printer_units =
ConvertUnit(kCmInMicrons, kMicronsPerInch, units_per_inch);
int min_size_printer_units = units_per_inch;
margins.header = header_footer_text_height;
margins.footer = header_footer_text_height;
if (physical_size_device_units.height() > min_size_printer_units) {
margins.top = margin_printer_units;
margins.bottom = margin_printer_units;
} else {
margins.top = 0;
margins.bottom = 0;
small_paper_size = true;
}
if (physical_size_device_units.width() > min_size_printer_units) {
margins.left = margin_printer_units;
margins.right = margin_printer_units;
} else {
margins.left = 0;
margins.right = 0;
small_paper_size = true;
}
break;
}
case NO_MARGINS:
case PRINTABLE_AREA_MARGINS: {
margins.header = 0;
margins.footer = 0;
margins.top = 0;
margins.bottom = 0;
margins.left = 0;
margins.right = 0;
break;
}
case CUSTOM_MARGINS: {
margins.header = 0;
margins.footer = 0;
margins.top = ConvertUnitDouble(
requested_custom_margins_in_points_.top,
kPointsPerInch,
units_per_inch);
margins.bottom = ConvertUnitDouble(
requested_custom_margins_in_points_.bottom,
kPointsPerInch,
units_per_inch);
margins.left = ConvertUnitDouble(
requested_custom_margins_in_points_.left,
kPointsPerInch,
units_per_inch);
margins.right = ConvertUnitDouble(
requested_custom_margins_in_points_.right,
kPointsPerInch,
units_per_inch);
break;
}
default: {
NOTREACHED();
}
}
if ((margin_type_ == DEFAULT_MARGINS ||
margin_type_ == PRINTABLE_AREA_MARGINS) &&
!small_paper_size) {
page_setup_device_units_.SetRequestedMargins(margins);
} else {
page_setup_device_units_.ForceRequestedMargins(margins);
}
page_setup_device_units_.Init(physical_size_device_units,
printable_area_device_units,
header_footer_text_height);
if (landscape_ && landscape_needs_flip)
page_setup_device_units_.FlipOrientation();
}
void PrintSettings::SetCustomMargins(
const PageMargins& requested_margins_in_points) {
requested_custom_margins_in_points_ = requested_margins_in_points;
margin_type_ = CUSTOM_MARGINS;
}
int PrintSettings::NewCookie() {
// A cookie of 0 is used to mark a document as unassigned, count from 1.
return cookie_seq.GetNext() + 1;
}
void PrintSettings::SetOrientation(bool landscape) {
if (landscape_ != landscape) {
landscape_ = landscape;
page_setup_device_units_.FlipOrientation();
}
}
} // namespace printing