blob: 84a4a4f17ecd08cadb6a5c5c0ce85ec48c3f7b7e [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"
Alan Screenee634e72021-07-02 20:08:5110#include "printing/mojom/print.mojom.h"
[email protected]4993f342010-10-26 17:57:5211#include "printing/print_settings.h"
12
13namespace printing {
14
thestig1f8436b2016-10-06 01:09:2515namespace {
16
rbpotterd8fd4682017-02-27 20:06:4017bool HasEscapeSupport(HDC hdc, DWORD escape) {
rbpotter51933c02017-01-17 21:44:1218 const char* ptr = reinterpret_cast<const char*>(&escape);
19 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
20}
21
22bool IsTechnology(HDC hdc, const char* technology) {
23 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
24 return false;
25
rbpotterd8fd4682017-02-27 20:06:4026 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
rbpotter51933c02017-01-17 21:44:1227 return false;
28
29 char buf[256];
30 memset(buf, 0, sizeof(buf));
31 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
32 return false;
33 return strcmp(buf, technology) == 0;
34}
35
rbpotterd8fd4682017-02-27 20:06:4036void SetPrinterToGdiMode(HDC hdc) {
37 // Try to set to GDI centric mode
38 DWORD mode = PSIDENT_GDICENTRIC;
39 const char* ptr = reinterpret_cast<const char*>(&mode);
40 ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
41}
42
43int GetPrinterPostScriptLevel(HDC hdc) {
44 constexpr int param = FEATURESETTING_PSLEVEL;
45 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
46 int param_out = 0;
47 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
48 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
49 sizeof(param_out), param_out_char_ptr) > 0) {
50 return param_out;
51 }
52 return 0;
53}
54
rbpotter58bc882e2017-02-01 03:44:2455bool IsPrinterPostScript(HDC hdc, int* level) {
56 static constexpr char kPostScriptDriver[] = "PostScript";
rbpotter58bc882e2017-02-01 03:44:2457
rbpotterd8fd4682017-02-27 20:06:4058 // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
59 // mode to check the language level supported. See if it looks like a
60 // postscript printer and supports the postscript functions that are
61 // supported in compatability mode. If so set to level 2 postscript.
62 if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
63 if (!IsTechnology(hdc, kPostScriptDriver))
64 return false;
65 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
66 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
67 return false;
rbpotter58bc882e2017-02-01 03:44:2468 }
rbpotter58bc882e2017-02-01 03:44:2469 *level = 2;
70 return true;
71 }
72
rbpotterd8fd4682017-02-27 20:06:4073 // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
74 // driver. Set the printer to GDI mode in order to query the postscript
75 // level. Use GDI mode instead of PostScript mode so that if level detection
76 // fails or returns language level < 2 we can fall back to normal printing.
77 // Note: This escape must be called before other escapes.
78 SetPrinterToGdiMode(hdc);
79 if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
80 // Can't query the level, use level 2 to be safe
81 *level = 2;
82 return true;
83 }
84
85 // Get the language level. If invalid or < 2, return false to set printer to
86 // normal printing mode.
87 *level = GetPrinterPostScriptLevel(hdc);
88 return *level == 2 || *level == 3;
rbpotter58bc882e2017-02-01 03:44:2489}
90
thestig1f8436b2016-10-06 01:09:2591bool IsPrinterXPS(HDC hdc) {
rbpotter51933c02017-01-17 21:44:1292 static constexpr char kXPSDriver[] =
93 "http://schemas.microsoft.com/xps/2005/06";
94 return IsTechnology(hdc, kXPSDriver);
thestig1f8436b2016-10-06 01:09:2595}
96
rbpotter0437a1712017-07-14 21:23:2497bool IsPrinterTextOnly(HDC hdc) {
98 return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
99}
thestig1f8436b2016-10-06 01:09:25100} // namespace
101
[email protected]4993f342010-10-26 17:57:52102// static
103void PrintSettingsInitializerWin::InitPrintSettings(
104 HDC hdc,
105 const DEVMODE& dev_mode,
[email protected]4993f342010-10-26 17:57:52106 PrintSettings* print_settings) {
107 DCHECK(hdc);
108 DCHECK(print_settings);
thestig1f8436b2016-10-06 01:09:25109
[email protected]e5324b52013-10-29 03:16:37110 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
rbpotter116c2e12017-04-04 19:21:28111 int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
112 int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
113 print_settings->set_dpi_xy(dpi_x, dpi_y);
[email protected]b2b0fce2011-01-12 16:34:40114 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
115 print_settings->set_supports_alpha_blend(
Lei Zhang01a1d3c02019-05-21 04:59:10116 (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps);
thestig1f8436b2016-10-06 01:09:25117
[email protected]4993f342010-10-26 17:57:52118 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
119 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
120
Daniel Hosseinian3553e272021-04-24 00:51:18121 // Initialize `page_setup_device_units_`.
rbpotter116c2e12017-04-04 19:21:28122 // Blink doesn't support different dpi settings in X and Y axis. However,
123 // some printers use them. So, to avoid a bad page calculation, scale page
124 // size components based on the dpi in the appropriate dimension.
125 int dpi = print_settings->dpi();
126 gfx::Size physical_size_device_units(
127 GetDeviceCaps(hdc, PHYSICALWIDTH) * dpi / dpi_x,
128 GetDeviceCaps(hdc, PHYSICALHEIGHT) * dpi / dpi_y);
129 gfx::Rect printable_area_device_units(
130 GetDeviceCaps(hdc, PHYSICALOFFSETX) * dpi / dpi_x,
131 GetDeviceCaps(hdc, PHYSICALOFFSETY) * dpi / dpi_y,
132 GetDeviceCaps(hdc, HORZRES) * dpi / dpi_x,
133 GetDeviceCaps(hdc, VERTRES) * dpi / dpi_y);
[email protected]4993f342010-10-26 17:57:52134
[email protected]44e4e1a42011-10-08 00:37:53135 // Sanity check the printable_area: we've seen crashes caused by a printable
136 // area rect of 0, 0, 0, 0, so it seems some drivers don't set it.
137 if (printable_area_device_units.IsEmpty() ||
Lei Zhang01a1d3c02019-05-21 04:59:10138 !gfx::Rect(physical_size_device_units)
139 .Contains(printable_area_device_units)) {
[email protected]44e4e1a42011-10-08 00:37:53140 printable_area_device_units = gfx::Rect(physical_size_device_units);
141 }
[email protected]4c9054b2013-11-04 18:34:29142 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
[email protected]4993f342010-10-26 17:57:52143 print_settings->SetPrinterPrintableArea(physical_size_device_units,
Lei Zhang01a1d3c02019-05-21 04:59:10144 printable_area_device_units, false);
rbpotter116c2e12017-04-04 19:21:28145
Alan Screen3fb63bf2020-08-19 00:11:23146 print_settings->set_color(IsDevModeWithColor(&dev_mode)
147 ? mojom::ColorModel::kColor
148 : mojom::ColorModel::kGray);
Lei Zhang0c4f392de2018-05-15 23:05:26149
rbpotter54f7fa82017-02-10 22:05:50150 // Check for postscript first so that we can change the mode with the
151 // first command.
rbpotter58bc882e2017-02-01 03:44:24152 int level;
153 if (IsPrinterPostScript(hdc, &level)) {
154 if (level == 2) {
Alan Screenee634e72021-07-02 20:08:51155 print_settings->set_printer_language_type(
156 mojom::PrinterLanguageType::kPostscriptLevel2);
rbpotter58bc882e2017-02-01 03:44:24157 return;
158 }
159 DCHECK_EQ(3, level);
Alan Screenee634e72021-07-02 20:08:51160 print_settings->set_printer_language_type(
161 mojom::PrinterLanguageType::kPostscriptLevel3);
rbpotter58bc882e2017-02-01 03:44:24162 return;
163 }
rbpotter0437a1712017-07-14 21:23:24164 // Detects the generic / text only driver.
165 if (IsPrinterTextOnly(hdc)) {
Alan Screenee634e72021-07-02 20:08:51166 print_settings->set_printer_language_type(
167 mojom::PrinterLanguageType::kTextOnly);
rbpotter0437a1712017-07-14 21:23:24168 return;
169 }
rbpotter54f7fa82017-02-10 22:05:50170 if (IsPrinterXPS(hdc)) {
Alan Screenee634e72021-07-02 20:08:51171 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kXps);
rbpotter54f7fa82017-02-10 22:05:50172 return;
173 }
Alan Screenee634e72021-07-02 20:08:51174 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kNone);
[email protected]4993f342010-10-26 17:57:52175}
176
177} // namespace printing