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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsPrinterWin.h"
#include <algorithm>
#include <windows.h>
#include <winspool.h>
#include "mozilla/Array.h"
#include "nsPaper.h"
#include "nsPrintSettingsImpl.h"
#include "nsWindowsHelpers.h"
#include "WinUtils.h"
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::widget;
static const double kPointsPerTenthMM = 72.0 / 254.0;
static const double kPointsPerInch = 72.0;
nsPrinterWin::nsPrinterWin(const CommonPaperInfoArray* aArray,
const nsAString& aName)
: nsPrinterBase(aArray),
mName(aName),
mDefaultDevmodeWStorage("nsPrinterWin::mDefaultDevmodeWStorage") {}
// static
already_AddRefed<nsPrinterWin> nsPrinterWin::Create(
const CommonPaperInfoArray* aArray, const nsAString& aName) {
return do_AddRef(new nsPrinterWin(aArray, aName));
}
template <class T>
static nsTArray<T> GetDeviceCapabilityArray(const LPWSTR aPrinterName,
WORD aCapabilityID,
const DEVMODEW* aDevmodeW,
int& aCount) {
MOZ_ASSERT(aCount >= 0, "Possibly passed aCount from previous error case.");
nsTArray<T> caps;
// We only want to access printer drivers in the parent process.
if (!XRE_IsParentProcess()) {
return caps;
}
// Both the call to get the size and the call to actually populate the array
// are relatively expensive, so as sometimes the lengths of the arrays that we
// retrieve depend on each other we allow a count to be passed in to save the
// first call. As we allocate double the count anyway this should allay any
// safety worries.
if (!aCount) {
// Passing nullptr as the port here seems to work. Given that we would have
// to OpenPrinter with just the name anyway to get the port that makes
// sense. Also, the printer set-up seems to stop you from having two
// printers with the same name. Note: this (and the call below) are blocking
// calls, which could be slow.
aCount = ::DeviceCapabilitiesW(aPrinterName, nullptr, aCapabilityID,
nullptr, aDevmodeW);
if (aCount <= 0) {
return caps;
}
}
// As DeviceCapabilitiesW doesn't take a size, there is a greater risk of the
// buffer being overflowed, so we over-allocate for safety.
caps.SetLength(aCount * 2);
int count = ::DeviceCapabilitiesW(aPrinterName, nullptr, aCapabilityID,
reinterpret_cast<LPWSTR>(caps.Elements()),
aDevmodeW);
if (count <= 0) {
caps.Clear();
return caps;
}
// We know from bug 1673708 that sometimes the final array returned is smaller
// than the required array count. Assert here to see if this is reproduced on
// test servers.
MOZ_ASSERT(count == aCount, "Different array count returned than expected.");
// Note that TruncateLength will crash if count > caps.Length().
caps.TruncateLength(count);
return caps;
}
NS_IMETHODIMP
nsPrinterWin::GetName(nsAString& aName) {
aName.Assign(mName);
return NS_OK;
}
NS_IMETHODIMP
nsPrinterWin::GetSystemName(nsAString& aName) {
aName.Assign(mName);
return NS_OK;
}
bool nsPrinterWin::SupportsDuplex() const {
// Some printer drivers have threading issues around using their default
// DEVMODE, so we use a copy of our cached one.
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return false;
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
return ::DeviceCapabilitiesW(mName.get(), nullptr, DC_DUPLEX, nullptr,
devmode) == 1;
}
bool nsPrinterWin::SupportsColor() const {
// Some printer drivers have threading issues around using their default
// DEVMODE, so we use a copy of our cached one.
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return false;
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
return ::DeviceCapabilitiesW(mName.get(), nullptr, DC_COLORDEVICE, nullptr,
devmode) == 1;
}
bool nsPrinterWin::SupportsMonochrome() const {
if (!SupportsColor()) {
return true;
}
nsHPRINTER hPrinter = nullptr;
if (NS_WARN_IF(!::OpenPrinterW(mName.get(), &hPrinter, nullptr))) {
return false;
}
nsAutoPrinter autoPrinter(hPrinter);
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return false;
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
devmode->dmFields |= DM_COLOR;
devmode->dmColor = DMCOLOR_MONOCHROME;
// Try to modify the devmode settings and see if the setting sticks.
//
// This has been the only reliable way to detect it that we've found.
LONG ret =
::DocumentPropertiesW(nullptr, autoPrinter.get(), mName.get(), devmode,
devmode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (ret != IDOK) {
return false;
}
return !(devmode->dmFields & DM_COLOR) ||
devmode->dmColor == DMCOLOR_MONOCHROME;
}
bool nsPrinterWin::SupportsCollation() const {
// Some printer drivers have threading issues around using their default
// DEVMODE, so we use a copy of our cached one.
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return false;
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
return ::DeviceCapabilitiesW(mName.get(), nullptr, DC_COLLATE, nullptr,
devmode) == 1;
}
nsPrinterBase::PrinterInfo nsPrinterWin::CreatePrinterInfo() const {
return PrinterInfo{PaperList(), DefaultSettings()};
}
mozilla::gfx::MarginDouble nsPrinterWin::GetMarginsForPaper(
nsString aPaperId) const {
gfx::MarginDouble margin;
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return margin;
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
devmode->dmFields = DM_PAPERSIZE;
devmode->dmPaperSize = _wtoi((const wchar_t*)aPaperId.BeginReading());
nsAutoHDC printerDc(::CreateICW(nullptr, mName.get(), nullptr, devmode));
MOZ_ASSERT(printerDc, "CreateICW failed");
if (!printerDc) {
return margin;
}
margin = WinUtils::GetUnwriteableMarginsForDeviceInInches(printerDc);
margin.top *= kPointsPerInch;
margin.right *= kPointsPerInch;
margin.bottom *= kPointsPerInch;
margin.left *= kPointsPerInch;
return margin;
}
nsTArray<uint8_t> nsPrinterWin::CopyDefaultDevmodeW() const {
nsTArray<uint8_t> devmodeStorageW;
auto devmodeStorageWLock = mDefaultDevmodeWStorage.Lock();
if (devmodeStorageWLock->IsEmpty()) {
nsHPRINTER hPrinter = nullptr;
// OpenPrinter could fail if, for example, the printer has been removed
// or otherwise become inaccessible since it was selected.
if (NS_WARN_IF(!::OpenPrinterW(mName.get(), &hPrinter, nullptr))) {
return devmodeStorageW;
}
nsAutoPrinter autoPrinter(hPrinter);
// Allocate devmode storage of the correct size.
LONG bytesNeeded = ::DocumentPropertiesW(nullptr, autoPrinter.get(),
mName.get(), nullptr, nullptr, 0);
// Note that we must cast the sizeof() to a signed type so that comparison
// with the signed, potentially-negative bytesNeeded will work!
MOZ_ASSERT(bytesNeeded >= LONG(sizeof(DEVMODEW)),
"DocumentPropertiesW failed to get valid size");
if (bytesNeeded < LONG(sizeof(DEVMODEW))) {
return devmodeStorageW;
}
// Allocate extra space in case of bad drivers that return a too-small
// result from DocumentProperties.
// (See https://bugzilla.mozilla.org/show_bug.cgi?id=1664530#c5)
devmodeStorageWLock->SetLength(bytesNeeded * 2);
auto* devmode =
reinterpret_cast<DEVMODEW*>(devmodeStorageWLock->Elements());
LONG ret = ::DocumentPropertiesW(nullptr, autoPrinter.get(), mName.get(),
devmode, nullptr, DM_OUT_BUFFER);
MOZ_ASSERT(ret == IDOK, "DocumentPropertiesW failed");
if (ret != IDOK) {
return devmodeStorageW;
}
}
devmodeStorageW.Assign(devmodeStorageWLock.ref());
return devmodeStorageW;
}
nsTArray<mozilla::PaperInfo> nsPrinterWin::PaperList() const {
// Some printer drivers have threading issues around using their default
// DEVMODE, so we use a copy of our cached one.
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return {};
}
auto* devmode = reinterpret_cast<DEVMODEW*>(devmodeWStorage.Elements());
// Paper IDs are returned as WORDs.
int requiredArrayCount = 0;
auto paperIds = GetDeviceCapabilityArray<WORD>(mName.get(), DC_PAPERS,
devmode, requiredArrayCount);
if (!paperIds.Length()) {
return {};
}
// Paper names are returned in 64 long character buffers.
auto paperNames = GetDeviceCapabilityArray<Array<wchar_t, 64>>(
mName.get(), DC_PAPERNAMES, devmode, requiredArrayCount);
// Check that we have the same number of names as IDs.
if (paperNames.Length() != paperIds.Length()) {
return {};
}
// Paper sizes are returned as POINT structs with a tenth of a millimeter as
// the unit.
auto paperSizes = GetDeviceCapabilityArray<POINT>(
mName.get(), DC_PAPERSIZE, devmode, requiredArrayCount);
// Check that we have the same number of sizes as IDs.
if (paperSizes.Length() != paperIds.Length()) {
return {};
}
nsTArray<mozilla::PaperInfo> paperList;
paperList.SetCapacity(paperNames.Length());
for (size_t i = 0; i < paperNames.Length(); ++i) {
// Paper names are null terminated unless they are 64 characters long.
auto firstNull =
std::find(paperNames[i].cbegin(), paperNames[i].cend(), L'\0');
auto nameLength = firstNull - paperNames[i].cbegin();
double width = paperSizes[i].x * kPointsPerTenthMM;
double height = paperSizes[i].y * kPointsPerTenthMM;
// Skip if no name or invalid size.
if (!nameLength || width <= 0 || height <= 0) {
continue;
}
// Windows paper IDs are 16-bit integers; we stringify them to store in the
// PaperInfo.mId field.
nsString paperIdString;
paperIdString.AppendInt(paperIds[i]);
// We don't resolve the margins eagerly because they're really expensive (on
// the order of seconds for some drivers).
nsDependentSubstring name(paperNames[i].cbegin(), nameLength);
paperList.AppendElement(mozilla::PaperInfo(paperIdString, nsString(name),
{width, height}, Nothing()));
}
return paperList;
}
PrintSettingsInitializer nsPrinterWin::DefaultSettings() const {
// Initialize to something reasonable, in case we fail to get usable data
// from the devmode below.
nsString paperIdString(u"1"); // DMPAPER_LETTER
bool color = true;
nsTArray<uint8_t> devmodeWStorage = CopyDefaultDevmodeW();
if (devmodeWStorage.IsEmpty()) {
return {};
}
const auto* devmode =
reinterpret_cast<const DEVMODEW*>(devmodeWStorage.Elements());
// XXX If DM_PAPERSIZE is not set, or is not a known value, should we
// be returning a "Custom" name of some kind?
if (devmode->dmFields & DM_PAPERSIZE) {
paperIdString.Truncate(0);
paperIdString.AppendInt(devmode->dmPaperSize);
}
if (devmode->dmFields & DM_COLOR) {
// See comment for PrintSettingsInitializer.mPrintInColor
color = devmode->dmColor != DMCOLOR_MONOCHROME;
}
nsAutoHDC printerDc(::CreateICW(nullptr, mName.get(), nullptr, devmode));
MOZ_ASSERT(printerDc, "CreateICW failed");
if (!printerDc) {
return {};
}
int pixelsPerInchY = ::GetDeviceCaps(printerDc, LOGPIXELSY);
int physicalHeight = ::GetDeviceCaps(printerDc, PHYSICALHEIGHT);
double heightInInches = double(physicalHeight) / pixelsPerInchY;
int pixelsPerInchX = ::GetDeviceCaps(printerDc, LOGPIXELSX);
int physicalWidth = ::GetDeviceCaps(printerDc, PHYSICALWIDTH);
double widthInches = double(physicalWidth) / pixelsPerInchX;
if (devmode->dmFields & DM_ORIENTATION &&
devmode->dmOrientation == DMORIENT_LANDSCAPE) {
std::swap(widthInches, heightInInches);
}
SizeDouble paperSize(widthInches * kPointsPerInch,
heightInInches * kPointsPerInch);
gfx::MarginDouble margin =
WinUtils::GetUnwriteableMarginsForDeviceInInches(printerDc);
margin.top *= kPointsPerInch;
margin.right *= kPointsPerInch;
margin.bottom *= kPointsPerInch;
margin.left *= kPointsPerInch;
// Using Y to match existing code for print scaling calculations.
int resolution = pixelsPerInchY;
// We don't actually need the paper name in the settings because the
// paperIdString is used to look up the paper details in the front end.
nsString paperName;
return PrintSettingsInitializer{
mName, PaperInfo(paperIdString, paperName, paperSize, Some(margin)),
color, resolution, std::move(devmodeWStorage)};
}
|