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"
12 #include "printing/printing_utils.h"
18 bool HasEscapeSupport(HDC hdc, DWORD escape) {
19 const char* ptr = reinterpret_cast<const char*>(&escape);
20 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
23 bool IsTechnology(HDC hdc, const char* technology) {
24 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
27 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
31 memset(buf, 0, sizeof(buf));
32 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
34 return strcmp(buf, technology) == 0;
37 void SetPrinterToGdiMode(HDC hdc) {
38 // Try to set to GDI centric mode
39 DWORD mode = PSIDENT_GDICENTRIC;
40 const char* ptr = reinterpret_cast<const char*>(&mode);
41 ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
44 int GetPrinterPostScriptLevel(HDC hdc) {
45 constexpr int param = FEATURESETTING_PSLEVEL;
46 const char* param_char_ptr = reinterpret_cast<const char*>(¶m);
48 char* param_out_char_ptr = reinterpret_cast<char*>(¶m_out);
49 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
50 sizeof(param_out), param_out_char_ptr) > 0) {
56 bool IsPrinterPostScript(HDC hdc, int* level) {
57 static constexpr char kPostScriptDriver[] = "PostScript";
59 // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
60 // mode to check the language level supported. See if it looks like a
61 // postscript printer and supports the postscript functions that are
62 // supported in compatability mode. If so set to level 2 postscript.
63 if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
64 if (!IsTechnology(hdc, kPostScriptDriver))
66 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
67 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
74 // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
75 // driver. Set the printer to GDI mode in order to query the postscript
76 // level. Use GDI mode instead of PostScript mode so that if level detection
77 // fails or returns language level < 2 we can fall back to normal printing.
78 // Note: This escape must be called before other escapes.
79 SetPrinterToGdiMode(hdc);
80 if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
81 // Can't query the level, use level 2 to be safe
86 // Get the language level. If invalid or < 2, return false to set printer to
87 // normal printing mode.
88 *level = GetPrinterPostScriptLevel(hdc);
89 return *level == 2 || *level == 3;
92 bool IsPrinterXPS(HDC hdc) {
93 static constexpr char kXPSDriver[] =
94 "http://schemas.microsoft.com/xps/2005/06";
95 return IsTechnology(hdc, kXPSDriver);
98 bool IsPrinterTextOnly(HDC hdc) {
99 return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
104 void PrintSettingsInitializerWin::InitPrintSettings(
106 const DEVMODE& dev_mode,
107 PrintSettings* print_settings) {
109 DCHECK(print_settings);
111 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
112 int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
113 int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
114 print_settings->set_dpi_xy(dpi_x, dpi_y);
115 int dpi = print_settings->dpi();
116 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
118 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
119 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
121 // 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 gfx::Size physical_size_scaled(
125 GetDeviceCaps(hdc, PHYSICALWIDTH) * dpi / dpi_x,
126 GetDeviceCaps(hdc, PHYSICALHEIGHT) * dpi / dpi_y);
127 gfx::Rect printable_area_device_units = GetPrintableAreaDeviceUnits(hdc);
128 gfx::Rect printable_area_scaled =
129 gfx::Rect(printable_area_device_units.x() * dpi / dpi_x,
130 printable_area_device_units.y() * dpi / dpi_y,
131 printable_area_device_units.width() * dpi / dpi_x,
132 printable_area_device_units.height() * dpi / dpi_y);
133 print_settings->SetPrinterPrintableArea(physical_size_scaled,
134 printable_area_scaled, false);
136 print_settings->set_color(IsDevModeWithColor(&dev_mode)
137 ? mojom::ColorModel::kColor
138 : mojom::ColorModel::kGray);
140 // Check for postscript first so that we can change the mode with the
143 if (IsPrinterPostScript(hdc, &level)) {
145 print_settings->set_printer_language_type(
146 mojom::PrinterLanguageType::kPostscriptLevel2);
150 print_settings->set_printer_language_type(
151 mojom::PrinterLanguageType::kPostscriptLevel3);
154 // Detects the generic / text only driver.
155 if (IsPrinterTextOnly(hdc)) {
156 print_settings->set_printer_language_type(
157 mojom::PrinterLanguageType::kTextOnly);
160 if (IsPrinterXPS(hdc)) {
161 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kXps);
164 print_settings->set_printer_language_type(mojom::PrinterLanguageType::kNone);
167 } // namespace printing