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