1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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/print_settings.h"
16 bool HasEscapeSupport(HDC hdc, DWORD escape) {
17 const char* ptr = reinterpret_cast<const char*>(&escape);
18 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
21 bool IsTechnology(HDC hdc, const char* technology) {
22 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
25 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
29 memset(buf, 0, sizeof(buf));
30 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
32 return strcmp(buf, technology) == 0;
35 void 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);
42 int GetPrinterPostScriptLevel(HDC hdc) {
43 constexpr int param = FEATURESETTING_PSLEVEL;
44 const char* param_char_ptr = reinterpret_cast<const char*>(¶m);
46 char* param_out_char_ptr = reinterpret_cast<char*>(¶m_out);
47 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
48 sizeof(param_out), param_out_char_ptr) > 0) {
54 bool IsPrinterPostScript(HDC hdc, int* level) {
55 static constexpr char kPostScriptDriver[] = "PostScript";
57 // 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))
64 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
65 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
72 // 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
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;
90 bool IsPrinterXPS(HDC hdc) {
91 static constexpr char kXPSDriver[] =
92 "http://schemas.microsoft.com/xps/2005/06";
93 return IsTechnology(hdc, kXPSDriver);
96 bool IsPrinterTextOnly(HDC hdc) {
97 return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
102 void PrintSettingsInitializerWin::InitPrintSettings(
104 const DEVMODE& dev_mode,
105 PrintSettings* print_settings) {
107 DCHECK(print_settings);
109 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
110 int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
111 int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
112 print_settings->set_dpi_xy(dpi_x, dpi_y);
113 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
114 print_settings->set_supports_alpha_blend(
115 (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps);
117 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
118 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
120 // Initialize |page_setup_device_units_|.
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 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);
134 // 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() ||
137 !gfx::Rect(physical_size_device_units).Contains(
138 printable_area_device_units)) {
139 printable_area_device_units = gfx::Rect(physical_size_device_units);
141 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
142 print_settings->SetPrinterPrintableArea(physical_size_device_units,
143 printable_area_device_units,
146 print_settings->set_color(IsDevModeWithColor(&dev_mode) ? COLOR : GRAY);
148 // Check for postscript first so that we can change the mode with the
151 if (IsPrinterPostScript(hdc, &level)) {
153 print_settings->set_printer_type(
154 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2);
158 print_settings->set_printer_type(
159 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
162 // Detects the generic / text only driver.
163 if (IsPrinterTextOnly(hdc)) {
164 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_TEXTONLY);
167 if (IsPrinterXPS(hdc)) {
168 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
171 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
174 } // namespace printing