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

Side by Side Diff: printing/print_settings_initializer_win.cc

Issue 2714073002: Eliminate PS printing edge cases (Closed)
Patch Set: Address comments Created 3 years, 9 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 HasEscapeSupport(HDC hdc, DWORD escape) {
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 24 if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
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))
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 void 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 ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
39 }
40
41 int GetPrinterPostScriptLevel(HDC hdc) {
42 constexpr int param = FEATURESETTING_PSLEVEL;
43 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
44 int param_out = 0;
45 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
46 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
47 sizeof(param_out), param_out_char_ptr) > 0) {
48 return param_out;
49 }
50 return 0;
51 }
52
45 bool IsPrinterPostScript(HDC hdc, int* level) { 53 bool IsPrinterPostScript(HDC hdc, int* level) {
46 static constexpr char kPostScriptDriver[] = "PostScript"; 54 static constexpr char kPostScriptDriver[] = "PostScript";
47 if (!IsTechnology(hdc, kPostScriptDriver)) {
48 return false;
49 }
50 55
51 // Query the PS Level if possible. 56 // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
52 if (HasEscapeSupprt(hdc, GET_PS_FEATURESETTING)) { 57 // mode to check the language level supported. See if it looks like a
53 constexpr int param = FEATURESETTING_PSLEVEL; 58 // postscript printer and supports the postscript functions that are
54 const char* param_char_ptr = reinterpret_cast<const char*>(&param); 59 // supported in compatability mode. If so set to level 2 postscript.
55 int param_out = -1; 60 if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
56 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out); 61 if (!IsTechnology(hdc, kPostScriptDriver))
57 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr, 62 return false;
58 sizeof(param_out), param_out_char_ptr) > 0) { 63 if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
59 if (param_out < 2 || param_out > 3) 64 !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
60 return false; 65 return false;
61
62 *level = param_out;
63 return true;
64 } 66 }
65 }
66
67 // If it looks like a PS printer.
68 if (HasEscapeSupprt(hdc, POSTSCRIPT_PASSTHROUGH) &&
69 HasEscapeSupprt(hdc, POSTSCRIPT_DATA)) {
70 *level = 2; 67 *level = 2;
71 return true; 68 return true;
72 } 69 }
73 70
74 return false; 71 // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
72 // driver. Set the printer to GDI mode in order to query the postscript
73 // level. Use GDI mode instead of PostScript mode so that if level detection
74 // fails or returns language level < 2 we can fall back to normal printing.
75 // Note: This escape must be called before other escapes.
76 SetPrinterToGdiMode(hdc);
77 if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
78 // Can't query the level, use level 2 to be safe
79 *level = 2;
80 return true;
81 }
82
83 // Get the language level. If invalid or < 2, return false to set printer to
84 // normal printing mode.
85 *level = GetPrinterPostScriptLevel(hdc);
86 return *level == 2 || *level == 3;
75 } 87 }
76 88
77 bool IsPrinterXPS(HDC hdc) { 89 bool IsPrinterXPS(HDC hdc) {
78 static constexpr char kXPSDriver[] = 90 static constexpr char kXPSDriver[] =
79 "http://schemas.microsoft.com/xps/2005/06"; 91 "http://schemas.microsoft.com/xps/2005/06";
80 return IsTechnology(hdc, kXPSDriver); 92 return IsTechnology(hdc, kXPSDriver);
81 } 93 }
82 94
83 } // namespace 95 } // namespace
84 96
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 return; 154 return;
143 } 155 }
144 if (IsPrinterXPS(hdc)) { 156 if (IsPrinterXPS(hdc)) {
145 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS); 157 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
146 return; 158 return;
147 } 159 }
148 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE); 160 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
149 } 161 }
150 162
151 } // namespace printing 163 } // 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