1 // Copyright 2011 The Chromium Authors
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_initializer_win.h"
9 #include "printing/backend/win_helper.h"
10 #include "printing/mojom/print.mojom.h"
11 #include "printing/print_settings.h"
17 bool HasEscapeSupport(HDC hdc, DWORD escape) {
18 const char* ptr = reinterpret_cast<const char*>(&escape);
19 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
22 bool IsTechnology(HDC hdc, const char* technology) {
23 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
26 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
30 memset(buf, 0, sizeof(buf));
31 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
33 return strcmp(buf, technology) == 0;
36 void 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);
43 int GetPrinterPostScriptLevel(HDC hdc) {
44 constexpr int param = FEATURESETTING_PSLEVEL;
45 const char* param_char_ptr = reinterpret_cast<const char*>(¶m);
47 char* param_out_char_ptr = reinterpret_cast<char*>(¶m_out);
48 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
49 sizeof(param_out), param_out_char_ptr) > 0) {
55 bool IsPrinterPostScript(HDC hdc, int* level) {
56 static constexpr char kPostScriptDriver[] = "PostScript";
58 // 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))
65 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
66 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
73 // 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
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;
91 bool IsPrinterXPS(HDC hdc) {
92 static constexpr char kXPSDriver[] =
93 "http://schemas.microsoft.com/xps/2005/06";
94 return IsTechnology(hdc, kXPSDriver);
97 bool IsPrinterTextOnly(HDC hdc) {
98 return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
103 void PrintSettingsInitializerWin::InitPrintSettings(
105 const DEVMODE& dev_mode,
106 PrintSettings* print_settings) {
108 DCHECK(print_settings);
110 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
111 int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
112 int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
113 print_settings->set_dpi_xy(dpi_x, dpi_y);
114 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
115 print_settings->set_supports_alpha_blend(
116 (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps);
118 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
119 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
121 // Initialize `page_setup_device_units_`.
122 // 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);
135 // 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() ||
138 !gfx::Rect(physical_size_device_units)
139 .Contains(printable_area_device_units)) {
140 printable_area_device_units = gfx::Rect(physical_size_device_units);
142 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
143 print_settings->SetPrinterPrintableArea(physical_size_device_units,
144 printable_area_device_units, false);
146 print_settings->set_color(IsDevModeWithColor(&dev_mode)
147 ? mojom::ColorModel::kColor
148 : mojom::ColorModel::kGray);
150 // Check for postscript first so that we can change the mode with the
153 if (IsPrinterPostScript(hdc, &level)) {
155 print_settings->set_printer_language_type(
156 mojom::PrinterLanguageType::kPostscriptLevel2);
160 print_settings->set_printer_language_type(
161 mojom::PrinterLanguageType::kPostscriptLevel3);
164 // Detects the generic / text only driver.
165 if (IsPrinterTextOnly(hdc)) {
166 print_settings->set_printer_language_type(
167 mojom::PrinterLanguageType::kTextOnly);
170 if (IsPrinterXPS(hdc)) {
171 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kXps);
174 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kNone);
177 } // namespace printing