blob: fa25ae9781765b878b4134510f250c5e34a8f3b2 [file] [log] [blame]
[email protected]c7a900192011-09-22 05:31:001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]4993f342010-10-26 17:57:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "printing/print_settings_initializer_win.h"
6
7#include <windows.h>
8
Lei Zhang0c4f392de2018-05-15 23:05:269#include "printing/backend/win_helper.h"
[email protected]4993f342010-10-26 17:57:5210#include "printing/print_settings.h"
11
12namespace printing {
13
thestig1f8436b2016-10-06 01:09:2514namespace {
15
rbpotterd8fd4682017-02-27 20:06:4016bool HasEscapeSupport(HDC hdc, DWORD escape) {
rbpotter51933c02017-01-17 21:44:1217 const char* ptr = reinterpret_cast<const char*>(&escape);
18 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
19}
20
21bool IsTechnology(HDC hdc, const char* technology) {
22 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
23 return false;
24
rbpotterd8fd4682017-02-27 20:06:4025 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
rbpotter51933c02017-01-17 21:44:1226 return false;
27
28 char buf[256];
29 memset(buf, 0, sizeof(buf));
30 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
31 return false;
32 return strcmp(buf, technology) == 0;
33}
34
rbpotterd8fd4682017-02-27 20:06:4035void SetPrinterToGdiMode(HDC hdc) {
36 // Try to set to GDI centric mode
37 DWORD mode = PSIDENT_GDICENTRIC;
38 const char* ptr = reinterpret_cast<const char*>(&mode);
39 ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
40}
41
42int GetPrinterPostScriptLevel(HDC hdc) {
43 constexpr int param = FEATURESETTING_PSLEVEL;
44 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
45 int param_out = 0;
46 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
47 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
48 sizeof(param_out), param_out_char_ptr) > 0) {
49 return param_out;
50 }
51 return 0;
52}
53
rbpotter58bc882e2017-02-01 03:44:2454bool IsPrinterPostScript(HDC hdc, int* level) {
55 static constexpr char kPostScriptDriver[] = "PostScript";
rbpotter58bc882e2017-02-01 03:44:2456
rbpotterd8fd4682017-02-27 20:06:4057 // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
58 // mode to check the language level supported. See if it looks like a
59 // postscript printer and supports the postscript functions that are
60 // supported in compatability mode. If so set to level 2 postscript.
61 if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
62 if (!IsTechnology(hdc, kPostScriptDriver))
63 return false;
64 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
65 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
66 return false;
rbpotter58bc882e2017-02-01 03:44:2467 }
rbpotter58bc882e2017-02-01 03:44:2468 *level = 2;
69 return true;
70 }
71
rbpotterd8fd4682017-02-27 20:06:4072 // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
73 // driver. Set the printer to GDI mode in order to query the postscript
74 // level. Use GDI mode instead of PostScript mode so that if level detection
75 // fails or returns language level < 2 we can fall back to normal printing.
76 // Note: This escape must be called before other escapes.
77 SetPrinterToGdiMode(hdc);
78 if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
79 // Can't query the level, use level 2 to be safe
80 *level = 2;
81 return true;
82 }
83
84 // Get the language level. If invalid or < 2, return false to set printer to
85 // normal printing mode.
86 *level = GetPrinterPostScriptLevel(hdc);
87 return *level == 2 || *level == 3;
rbpotter58bc882e2017-02-01 03:44:2488}
89
thestig1f8436b2016-10-06 01:09:2590bool IsPrinterXPS(HDC hdc) {
rbpotter51933c02017-01-17 21:44:1291 static constexpr char kXPSDriver[] =
92 "http://schemas.microsoft.com/xps/2005/06";
93 return IsTechnology(hdc, kXPSDriver);
thestig1f8436b2016-10-06 01:09:2594}
95
rbpotter0437a1712017-07-14 21:23:2496bool IsPrinterTextOnly(HDC hdc) {
97 return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
98}
thestig1f8436b2016-10-06 01:09:2599} // namespace
100
[email protected]4993f342010-10-26 17:57:52101// static
102void PrintSettingsInitializerWin::InitPrintSettings(
103 HDC hdc,
104 const DEVMODE& dev_mode,
[email protected]4993f342010-10-26 17:57:52105 PrintSettings* print_settings) {
106 DCHECK(hdc);
107 DCHECK(print_settings);
thestig1f8436b2016-10-06 01:09:25108
[email protected]e5324b52013-10-29 03:16:37109 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
rbpotter116c2e12017-04-04 19:21:28110 int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
111 int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
112 print_settings->set_dpi_xy(dpi_x, dpi_y);
[email protected]b2b0fce2011-01-12 16:34:40113 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
114 print_settings->set_supports_alpha_blend(
Lei Zhang01a1d3c02019-05-21 04:59:10115 (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps);
thestig1f8436b2016-10-06 01:09:25116
[email protected]4993f342010-10-26 17:57:52117 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
118 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
119
thestig1f8436b2016-10-06 01:09:25120 // Initialize |page_setup_device_units_|.
rbpotter116c2e12017-04-04 19:21:28121 // Blink doesn't support different dpi settings in X and Y axis. However,
122 // some printers use them. So, to avoid a bad page calculation, scale page
123 // size components based on the dpi in the appropriate dimension.
124 int dpi = print_settings->dpi();
125 gfx::Size physical_size_device_units(
126 GetDeviceCaps(hdc, PHYSICALWIDTH) * dpi / dpi_x,
127 GetDeviceCaps(hdc, PHYSICALHEIGHT) * dpi / dpi_y);
128 gfx::Rect printable_area_device_units(
129 GetDeviceCaps(hdc, PHYSICALOFFSETX) * dpi / dpi_x,
130 GetDeviceCaps(hdc, PHYSICALOFFSETY) * dpi / dpi_y,
131 GetDeviceCaps(hdc, HORZRES) * dpi / dpi_x,
132 GetDeviceCaps(hdc, VERTRES) * dpi / dpi_y);
[email protected]4993f342010-10-26 17:57:52133
[email protected]44e4e1a42011-10-08 00:37:53134 // Sanity check the printable_area: we've seen crashes caused by a printable
135 // area rect of 0, 0, 0, 0, so it seems some drivers don't set it.
136 if (printable_area_device_units.IsEmpty() ||
Lei Zhang01a1d3c02019-05-21 04:59:10137 !gfx::Rect(physical_size_device_units)
138 .Contains(printable_area_device_units)) {
[email protected]44e4e1a42011-10-08 00:37:53139 printable_area_device_units = gfx::Rect(physical_size_device_units);
140 }
[email protected]4c9054b2013-11-04 18:34:29141 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
[email protected]4993f342010-10-26 17:57:52142 print_settings->SetPrinterPrintableArea(physical_size_device_units,
Lei Zhang01a1d3c02019-05-21 04:59:10143 printable_area_device_units, false);
rbpotter116c2e12017-04-04 19:21:28144
Alan Screen3fb63bf2020-08-19 00:11:23145 print_settings->set_color(IsDevModeWithColor(&dev_mode)
146 ? mojom::ColorModel::kColor
147 : mojom::ColorModel::kGray);
Lei Zhang0c4f392de2018-05-15 23:05:26148
rbpotter54f7fa82017-02-10 22:05:50149 // Check for postscript first so that we can change the mode with the
150 // first command.
rbpotter58bc882e2017-02-01 03:44:24151 int level;
152 if (IsPrinterPostScript(hdc, &level)) {
153 if (level == 2) {
154 print_settings->set_printer_type(
155 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2);
156 return;
157 }
158 DCHECK_EQ(3, level);
159 print_settings->set_printer_type(
160 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
161 return;
162 }
rbpotter0437a1712017-07-14 21:23:24163 // Detects the generic / text only driver.
164 if (IsPrinterTextOnly(hdc)) {
165 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_TEXTONLY);
166 return;
167 }
rbpotter54f7fa82017-02-10 22:05:50168 if (IsPrinterXPS(hdc)) {
169 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
170 return;
171 }
rbpotter58bc882e2017-02-01 03:44:24172 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
[email protected]4993f342010-10-26 17:57:52173}
174
175} // namespace printing