[email protected] | c7a90019 | 2011-09-22 05:31:00 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 2 | // 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 Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 9 | #include "printing/backend/win_helper.h" |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 10 | #include "printing/print_settings.h" |
| 11 | |
| 12 | namespace printing { |
| 13 | |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 14 | namespace { |
| 15 | |
rbpotter | d8fd468 | 2017-02-27 20:06:40 | [diff] [blame] | 16 | bool HasEscapeSupport(HDC hdc, DWORD escape) { |
rbpotter | 51933c0 | 2017-01-17 21:44:12 | [diff] [blame] | 17 | const char* ptr = reinterpret_cast<const char*>(&escape); |
| 18 | return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0; |
| 19 | } |
| 20 | |
| 21 | bool IsTechnology(HDC hdc, const char* technology) { |
| 22 | if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER) |
| 23 | return false; |
| 24 | |
rbpotter | d8fd468 | 2017-02-27 20:06:40 | [diff] [blame] | 25 | if (!HasEscapeSupport(hdc, GETTECHNOLOGY)) |
rbpotter | 51933c0 | 2017-01-17 21:44:12 | [diff] [blame] | 26 | 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 | |
rbpotter | d8fd468 | 2017-02-27 20:06:40 | [diff] [blame] | 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); |
| 40 | } |
| 41 | |
| 42 | int GetPrinterPostScriptLevel(HDC hdc) { |
| 43 | constexpr int param = FEATURESETTING_PSLEVEL; |
| 44 | const char* param_char_ptr = reinterpret_cast<const char*>(¶m); |
| 45 | int param_out = 0; |
| 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) { |
| 49 | return param_out; |
| 50 | } |
| 51 | return 0; |
| 52 | } |
| 53 | |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 54 | bool IsPrinterPostScript(HDC hdc, int* level) { |
| 55 | static constexpr char kPostScriptDriver[] = "PostScript"; |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 56 | |
rbpotter | d8fd468 | 2017-02-27 20:06:40 | [diff] [blame] | 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)) |
| 63 | return false; |
| 64 | if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) || |
| 65 | !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) { |
| 66 | return false; |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 67 | } |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 68 | *level = 2; |
| 69 | return true; |
| 70 | } |
| 71 | |
rbpotter | d8fd468 | 2017-02-27 20:06:40 | [diff] [blame] | 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 |
| 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; |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 88 | } |
| 89 | |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 90 | bool IsPrinterXPS(HDC hdc) { |
rbpotter | 51933c0 | 2017-01-17 21:44:12 | [diff] [blame] | 91 | static constexpr char kXPSDriver[] = |
| 92 | "http://schemas.microsoft.com/xps/2005/06"; |
| 93 | return IsTechnology(hdc, kXPSDriver); |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 94 | } |
| 95 | |
rbpotter | 0437a171 | 2017-07-14 21:23:24 | [diff] [blame] | 96 | bool IsPrinterTextOnly(HDC hdc) { |
| 97 | return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM; |
| 98 | } |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 99 | } // namespace |
| 100 | |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 101 | // static |
| 102 | void PrintSettingsInitializerWin::InitPrintSettings( |
| 103 | HDC hdc, |
| 104 | const DEVMODE& dev_mode, |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 105 | PrintSettings* print_settings) { |
| 106 | DCHECK(hdc); |
| 107 | DCHECK(print_settings); |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 108 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 109 | print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE); |
rbpotter | 116c2e1 | 2017-04-04 19:21:28 | [diff] [blame] | 110 | 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] | b2b0fce | 2011-01-12 16:34:40 | [diff] [blame] | 113 | const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA; |
| 114 | print_settings->set_supports_alpha_blend( |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 115 | (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps); |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 116 | |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 117 | DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0); |
| 118 | DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0); |
| 119 | |
thestig | 1f8436b | 2016-10-06 01:09:25 | [diff] [blame] | 120 | // Initialize |page_setup_device_units_|. |
rbpotter | 116c2e1 | 2017-04-04 19:21:28 | [diff] [blame] | 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); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 133 | |
[email protected] | 44e4e1a4 | 2011-10-08 00:37:53 | [diff] [blame] | 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() || |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 137 | !gfx::Rect(physical_size_device_units) |
| 138 | .Contains(printable_area_device_units)) { |
[email protected] | 44e4e1a4 | 2011-10-08 00:37:53 | [diff] [blame] | 139 | printable_area_device_units = gfx::Rect(physical_size_device_units); |
| 140 | } |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 141 | DCHECK_EQ(print_settings->device_units_per_inch(), dpi); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 142 | print_settings->SetPrinterPrintableArea(physical_size_device_units, |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 143 | printable_area_device_units, false); |
rbpotter | 116c2e1 | 2017-04-04 19:21:28 | [diff] [blame] | 144 | |
Alan Screen | 3fb63bf | 2020-08-19 00:11:23 | [diff] [blame] | 145 | print_settings->set_color(IsDevModeWithColor(&dev_mode) |
| 146 | ? mojom::ColorModel::kColor |
| 147 | : mojom::ColorModel::kGray); |
Lei Zhang | 0c4f392de | 2018-05-15 23:05:26 | [diff] [blame] | 148 | |
rbpotter | 54f7fa8 | 2017-02-10 22:05:50 | [diff] [blame] | 149 | // Check for postscript first so that we can change the mode with the |
| 150 | // first command. |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 151 | 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 | } |
rbpotter | 0437a171 | 2017-07-14 21:23:24 | [diff] [blame] | 163 | // Detects the generic / text only driver. |
| 164 | if (IsPrinterTextOnly(hdc)) { |
| 165 | print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_TEXTONLY); |
| 166 | return; |
| 167 | } |
rbpotter | 54f7fa8 | 2017-02-10 22:05:50 | [diff] [blame] | 168 | if (IsPrinterXPS(hdc)) { |
| 169 | print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS); |
| 170 | return; |
| 171 | } |
rbpotter | 58bc882e | 2017-02-01 03:44:24 | [diff] [blame] | 172 | print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace printing |