Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Side by Side Diff: printing/print_settings_initializer_win.cc

Issue 2714073002: Eliminate PS printing edge cases (Closed)
Patch Set: Use GDI mode for PS level query Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/printing/pdf_to_emf_converter.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "printing/print_settings_initializer_win.h" 5 #include "printing/print_settings_initializer_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "printing/print_settings.h" 9 #include "printing/print_settings.h"
10 10
11 namespace printing { 11 namespace printing {
12 12
13 namespace { 13 namespace {
14 14
15 bool HasEscapeSupprt(HDC hdc, DWORD escape) { 15 bool HasEscapeSupprt(HDC hdc, DWORD escape) {
Lei Zhang 2017/02/25 09:09:35 Looks like we didn't have the $250 and couldn't bu
rbpotter 2017/02/27 16:40:50 Done.
16 const char* ptr = reinterpret_cast<const char*>(&escape); 16 const char* ptr = reinterpret_cast<const char*>(&escape);
17 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0; 17 return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
18 } 18 }
19 19
20 bool IsTechnology(HDC hdc, const char* technology) { 20 bool IsTechnology(HDC hdc, const char* technology) {
21 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER) 21 if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
22 return false; 22 return false;
23 23
24 // If postscript, try to query Postscript Identify and then set to
25 // postscript mode before calling any more ExtEscape functions. Otherwise,
26 // PSLEVEL query will not work.
27 if (strcmp(technology, "PostScript") == 0 &&
28 HasEscapeSupprt(hdc, POSTSCRIPT_IDENTIFY)) {
29 DWORD mode = PSIDENT_PSCENTRIC;
30 const char* ptr = reinterpret_cast<const char*>(&mode);
31 ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
32 return true;
33 }
34
35 if (!HasEscapeSupprt(hdc, GETTECHNOLOGY)) 24 if (!HasEscapeSupprt(hdc, GETTECHNOLOGY))
36 return false; 25 return false;
37 26
38 char buf[256]; 27 char buf[256];
39 memset(buf, 0, sizeof(buf)); 28 memset(buf, 0, sizeof(buf));
40 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0) 29 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
41 return false; 30 return false;
42 return strcmp(buf, technology) == 0; 31 return strcmp(buf, technology) == 0;
43 } 32 }
44 33
34 bool SetPrinterToGdiMode(HDC hdc) {
35 // Try to set to GDI centric mode
36 DWORD mode = PSIDENT_GDICENTRIC;
37 const char* ptr = reinterpret_cast<const char*>(&mode);
38 int ret = ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
39 return ret > 0;
40 }
41
42 int GetPrinterPostScriptLevel(HDC hdc) {
43 constexpr int param = FEATURESETTING_PSLEVEL;
44 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
45 int param_out = 0;
46 char* param_out_char_ptr = reinterpret_cast<char*>(&param_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 -1;
Lei Zhang 2017/02/25 09:09:35 0 here as well?
rbpotter 2017/02/27 16:40:50 Done.
52 }
53
45 bool IsPrinterPostScript(HDC hdc, int* level) { 54 bool IsPrinterPostScript(HDC hdc, int* level) {
46 static constexpr char kPostScriptDriver[] = "PostScript"; 55 static constexpr char kPostScriptDriver[] = "PostScript";
47 if (!IsTechnology(hdc, kPostScriptDriver)) { 56 // Try to set to Gdi mode so that language level detection works (not
Lei Zhang 2017/02/25 09:09:35 s/Gdi/GDI/
rbpotter 2017/02/27 16:40:50 Done.
57 // supported in compatability mode). Use GDI mode instead of PS mode so that
58 // if level detection fails we can fall back to normal printing.
59 // Note: This has to occur before other escape functions are called. Also, if
60 // the printer supports POSTSCRIPT_IDENTIFY, we know it has a postscript
61 // driver.
62 if (HasEscapeSupprt(hdc, POSTSCRIPT_IDENTIFY)) {
63 SetPrinterToGdiMode(hdc);
Lei Zhang 2017/02/25 09:09:35 This patch set no longer checks the return value.
rbpotter 2017/02/27 16:40:50 Yes- don't need to check since GDI mode doesn't ca
64 } else if (!IsTechnology(hdc, kPostScriptDriver)) {
65 // Doesn't support POSTSCRIPT_IDENTIFY and doesn't look like PostScript.
66 return false;
67 } else if (!HasEscapeSupprt(hdc, POSTSCRIPT_PASSTHROUGH) ||
68 !HasEscapeSupprt(hdc, POSTSCRIPT_DATA)) {
69 // Can't change to GDI mode and doesn't have compatibility mode functions
48 return false; 70 return false;
49 } 71 }
50 72
51 // Query the PS Level if possible. 73 if (!HasEscapeSupprt(hdc, GET_PS_FEATURESETTING)) {
Lei Zhang 2017/02/25 09:09:35 If a printer does not support POSTSCRIPT_IDENTIFY,
rbpotter 2017/02/27 16:40:50 Done.
52 if (HasEscapeSupprt(hdc, GET_PS_FEATURESETTING)) { 74 // Can't query the level, use level 2 to be safe
53 constexpr int param = FEATURESETTING_PSLEVEL;
54 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
55 int param_out = -1;
56 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
57 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
58 sizeof(param_out), param_out_char_ptr) > 0) {
59 if (param_out < 2 || param_out > 3)
60 return false;
61
62 *level = param_out;
63 return true;
64 }
65 }
66
67 // If it looks like a PS printer.
68 if (HasEscapeSupprt(hdc, POSTSCRIPT_PASSTHROUGH) &&
69 HasEscapeSupprt(hdc, POSTSCRIPT_DATA)) {
70 *level = 2; 75 *level = 2;
71 return true; 76 return true;
72 } 77 }
73 78 *level = GetPrinterPostScriptLevel(hdc);
74 return false; 79 if (*level < 2 || *level > 3) {
80 // Invalid or insufficient postscript language level.
81 return false;
82 }
83 return true;
75 } 84 }
76 85
77 bool IsPrinterXPS(HDC hdc) { 86 bool IsPrinterXPS(HDC hdc) {
78 static constexpr char kXPSDriver[] = 87 static constexpr char kXPSDriver[] =
79 "http://schemas.microsoft.com/xps/2005/06"; 88 "http://schemas.microsoft.com/xps/2005/06";
80 return IsTechnology(hdc, kXPSDriver); 89 return IsTechnology(hdc, kXPSDriver);
81 } 90 }
82 91
83 } // namespace 92 } // namespace
84 93
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 return; 151 return;
143 } 152 }
144 if (IsPrinterXPS(hdc)) { 153 if (IsPrinterXPS(hdc)) {
145 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS); 154 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
146 return; 155 return;
147 } 156 }
148 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE); 157 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
149 } 158 }
150 159
151 } // namespace printing 160 } // namespace printing
OLDNEW
« no previous file with comments | « chrome/browser/printing/pdf_to_emf_converter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698