Chromium Code Reviews| Index: printing/print_settings_initializer_win.cc |
| diff --git a/printing/print_settings_initializer_win.cc b/printing/print_settings_initializer_win.cc |
| index 95d2fced08b5ff262a5d92de0105878ffee9e397..cd18ef071154fca7e74b74a55fbb99a511dd063f 100644 |
| --- a/printing/print_settings_initializer_win.cc |
| +++ b/printing/print_settings_initializer_win.cc |
| @@ -24,14 +24,6 @@ bool IsTechnology(HDC hdc, const char* technology) { |
| // If postscript, try to query Postscript Identify and then set to |
|
Lei Zhang
2017/02/25 03:03:35
Do we need to move this comment?
|
| // postscript mode before calling any more ExtEscape functions. Otherwise, |
| // PSLEVEL query will not work. |
| - if (strcmp(technology, "PostScript") == 0 && |
| - HasEscapeSupprt(hdc, POSTSCRIPT_IDENTIFY)) { |
| - DWORD mode = PSIDENT_PSCENTRIC; |
| - const char* ptr = reinterpret_cast<const char*>(&mode); |
| - ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr); |
| - return true; |
| - } |
| - |
| if (!HasEscapeSupprt(hdc, GETTECHNOLOGY)) |
| return false; |
| @@ -42,36 +34,57 @@ bool IsTechnology(HDC hdc, const char* technology) { |
| return strcmp(buf, technology) == 0; |
| } |
| +bool SetPrinterPostScript(HDC hdc) { |
|
Lei Zhang
2017/02/25 03:03:35
Maybe SetPrinterToPostScriptMode() ?
|
| + // Try to set to PS centric mode |
| + DWORD mode = PSIDENT_PSCENTRIC; |
| + const char* ptr = reinterpret_cast<const char*>(&mode); |
| + int ret = ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr); |
| + return ret > 0; |
| +} |
| + |
| +int GetPrinterPostScriptLevel(HDC hdc) { |
|
Lei Zhang
2017/02/25 03:24:53
Does this require PS centric mode? Or does it just
|
| + constexpr int param = FEATURESETTING_PSLEVEL; |
| + const char* param_char_ptr = reinterpret_cast<const char*>(¶m); |
| + int param_out = -1; |
|
Lei Zhang
2017/02/25 03:24:53
Can we just use 0 or do we need to distinguish bet
|
| + char* param_out_char_ptr = reinterpret_cast<char*>(¶m_out); |
| + if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr, |
| + sizeof(param_out), param_out_char_ptr) > 0) { |
| + return param_out; |
| + } |
| + return -1; |
| +} |
| + |
| bool IsPrinterPostScript(HDC hdc, int* level) { |
| static constexpr char kPostScriptDriver[] = "PostScript"; |
| - if (!IsTechnology(hdc, kPostScriptDriver)) { |
| + bool set_postscript = false; |
| + // Try to set to PostScript mode so that language level detection works. |
| + // This has to occur before other escape functions are called. |
| + if (HasEscapeSupprt(hdc, POSTSCRIPT_IDENTIFY)) { |
|
Lei Zhang
2017/02/25 03:24:53
Have you found printers that support POSTSCRIPT_ID
|
| + set_postscript = SetPrinterPostScript(hdc); |
| + } else if (!IsTechnology(hdc, kPostScriptDriver)) { |
| + // Doesn't support POSTSCRIPT_IDENTIFY and doesn't look like PostScript. |
| + return false; |
| + } else if (!HasEscapeSupprt(hdc, POSTSCRIPT_PASSTHROUGH) || |
| + !HasEscapeSupprt(hdc, POSTSCRIPT_DATA)) { |
| + // Can't change to PS mode and doesn't have compatibility mode functions |
| return false; |
| } |
| - // Query the PS Level if possible. |
| - if (HasEscapeSupprt(hdc, GET_PS_FEATURESETTING)) { |
| - constexpr int param = FEATURESETTING_PSLEVEL; |
| - const char* param_char_ptr = reinterpret_cast<const char*>(¶m); |
| - int param_out = -1; |
| - char* param_out_char_ptr = reinterpret_cast<char*>(¶m_out); |
| - if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr, |
| - sizeof(param_out), param_out_char_ptr) > 0) { |
| - if (param_out < 2 || param_out > 3) |
| - return false; |
| - |
| - *level = param_out; |
| - return true; |
| - } |
| - } |
| - |
| - // If it looks like a PS printer. |
| - if (HasEscapeSupprt(hdc, POSTSCRIPT_PASSTHROUGH) && |
| - HasEscapeSupprt(hdc, POSTSCRIPT_DATA)) { |
| + if (!HasEscapeSupprt(hdc, GET_PS_FEATURESETTING)) { |
| + // Can't query the level, use level 2 to be safe |
| *level = 2; |
| return true; |
| } |
| - |
| - return false; |
| + int ps_level = GetPrinterPostScriptLevel(hdc); |
| + if ((ps_level < 2 || ps_level > 3) && !set_postscript) { |
| + // if the PS level is < 2 or > 3 and we haven't set the driver to PS mode |
| + return false; |
| + } |
| + // If we set the driver to PS mode, set to level 2 or 3 if it is not already. |
| + // Once the driver is in PS mode, other modes won't work so we have to try |
| + // postscript. |
| + *level = ps_level <= 2 ? 2 : 3; |
| + return true; |
| } |
| bool IsPrinterXPS(HDC hdc) { |
| @@ -86,7 +99,8 @@ bool IsPrinterXPS(HDC hdc) { |
| void PrintSettingsInitializerWin::InitPrintSettings( |
| HDC hdc, |
| const DEVMODE& dev_mode, |
| - PrintSettings* print_settings) { |
| + PrintSettings* print_settings, |
| + bool postscript_enabled) { |
| DCHECK(hdc); |
| DCHECK(print_settings); |
| @@ -129,17 +143,19 @@ void PrintSettingsInitializerWin::InitPrintSettings( |
| false); |
| // Check for postscript first so that we can change the mode with the |
|
Lei Zhang
2017/02/25 03:24:53
This is a leftover question from the last CL. Why
|
| // first command. |
| - int level; |
| - if (IsPrinterPostScript(hdc, &level)) { |
| - if (level == 2) { |
| + if (postscript_enabled) { |
| + int level; |
| + if (IsPrinterPostScript(hdc, &level)) { |
| + if (level == 2) { |
| + print_settings->set_printer_type( |
| + PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2); |
| + return; |
| + } |
| + DCHECK_EQ(3, level); |
| print_settings->set_printer_type( |
| - PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2); |
| + PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3); |
| return; |
| } |
| - DCHECK_EQ(3, level); |
| - print_settings->set_printer_type( |
| - PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3); |
| - return; |
| } |
| if (IsPrinterXPS(hdc)) { |
| print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS); |