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

Side by Side Diff: printing/print_settings_initializer_win.cc

Issue 2714073002: Eliminate PS printing edge cases (Closed)
Patch Set: Fix compile errors 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
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) {
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 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?
25 // postscript mode before calling any more ExtEscape functions. Otherwise, 25 // postscript mode before calling any more ExtEscape functions. Otherwise,
26 // PSLEVEL query will not work. 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)) 27 if (!HasEscapeSupprt(hdc, GETTECHNOLOGY))
36 return false; 28 return false;
37 29
38 char buf[256]; 30 char buf[256];
39 memset(buf, 0, sizeof(buf)); 31 memset(buf, 0, sizeof(buf));
40 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0) 32 if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
41 return false; 33 return false;
42 return strcmp(buf, technology) == 0; 34 return strcmp(buf, technology) == 0;
43 } 35 }
44 36
37 bool SetPrinterPostScript(HDC hdc) {
Lei Zhang 2017/02/25 03:03:35 Maybe SetPrinterToPostScriptMode() ?
38 // Try to set to PS centric mode
39 DWORD mode = PSIDENT_PSCENTRIC;
40 const char* ptr = reinterpret_cast<const char*>(&mode);
41 int ret = ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
42 return ret > 0;
43 }
44
45 int GetPrinterPostScriptLevel(HDC hdc) {
Lei Zhang 2017/02/25 03:24:53 Does this require PS centric mode? Or does it just
46 constexpr int param = FEATURESETTING_PSLEVEL;
47 const char* param_char_ptr = reinterpret_cast<const char*>(&param);
48 int param_out = -1;
Lei Zhang 2017/02/25 03:24:53 Can we just use 0 or do we need to distinguish bet
49 char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
50 if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
51 sizeof(param_out), param_out_char_ptr) > 0) {
52 return param_out;
53 }
54 return -1;
55 }
56
45 bool IsPrinterPostScript(HDC hdc, int* level) { 57 bool IsPrinterPostScript(HDC hdc, int* level) {
46 static constexpr char kPostScriptDriver[] = "PostScript"; 58 static constexpr char kPostScriptDriver[] = "PostScript";
47 if (!IsTechnology(hdc, kPostScriptDriver)) { 59 bool set_postscript = false;
60 // Try to set to PostScript mode so that language level detection works.
61 // This has to occur before other escape functions are called.
62 if (HasEscapeSupprt(hdc, POSTSCRIPT_IDENTIFY)) {
Lei Zhang 2017/02/25 03:24:53 Have you found printers that support POSTSCRIPT_ID
63 set_postscript = SetPrinterPostScript(hdc);
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 PS 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)) {
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 int ps_level = GetPrinterPostScriptLevel(hdc);
74 return false; 79 if ((ps_level < 2 || ps_level > 3) && !set_postscript) {
80 // if the PS level is < 2 or > 3 and we haven't set the driver to PS mode
81 return false;
82 }
83 // If we set the driver to PS mode, set to level 2 or 3 if it is not already.
84 // Once the driver is in PS mode, other modes won't work so we have to try
85 // postscript.
86 *level = ps_level <= 2 ? 2 : 3;
87 return true;
75 } 88 }
76 89
77 bool IsPrinterXPS(HDC hdc) { 90 bool IsPrinterXPS(HDC hdc) {
78 static constexpr char kXPSDriver[] = 91 static constexpr char kXPSDriver[] =
79 "http://schemas.microsoft.com/xps/2005/06"; 92 "http://schemas.microsoft.com/xps/2005/06";
80 return IsTechnology(hdc, kXPSDriver); 93 return IsTechnology(hdc, kXPSDriver);
81 } 94 }
82 95
83 } // namespace 96 } // namespace
84 97
85 // static 98 // static
86 void PrintSettingsInitializerWin::InitPrintSettings( 99 void PrintSettingsInitializerWin::InitPrintSettings(
87 HDC hdc, 100 HDC hdc,
88 const DEVMODE& dev_mode, 101 const DEVMODE& dev_mode,
89 PrintSettings* print_settings) { 102 PrintSettings* print_settings,
103 bool postscript_enabled) {
90 DCHECK(hdc); 104 DCHECK(hdc);
91 DCHECK(print_settings); 105 DCHECK(print_settings);
92 106
93 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE); 107 print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
94 108
95 int dpi = GetDeviceCaps(hdc, LOGPIXELSX); 109 int dpi = GetDeviceCaps(hdc, LOGPIXELSX);
96 print_settings->set_dpi(dpi); 110 print_settings->set_dpi(dpi);
97 111
98 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA; 112 const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
99 print_settings->set_supports_alpha_blend( 113 print_settings->set_supports_alpha_blend(
(...skipping 20 matching lines...) Expand all
120 // area rect of 0, 0, 0, 0, so it seems some drivers don't set it. 134 // area rect of 0, 0, 0, 0, so it seems some drivers don't set it.
121 if (printable_area_device_units.IsEmpty() || 135 if (printable_area_device_units.IsEmpty() ||
122 !gfx::Rect(physical_size_device_units).Contains( 136 !gfx::Rect(physical_size_device_units).Contains(
123 printable_area_device_units)) { 137 printable_area_device_units)) {
124 printable_area_device_units = gfx::Rect(physical_size_device_units); 138 printable_area_device_units = gfx::Rect(physical_size_device_units);
125 } 139 }
126 DCHECK_EQ(print_settings->device_units_per_inch(), dpi); 140 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
127 print_settings->SetPrinterPrintableArea(physical_size_device_units, 141 print_settings->SetPrinterPrintableArea(physical_size_device_units,
128 printable_area_device_units, 142 printable_area_device_units,
129 false); 143 false);
130 // Check for postscript first so that we can change the mode with the 144 // 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
131 // first command. 145 // first command.
132 int level; 146 if (postscript_enabled) {
133 if (IsPrinterPostScript(hdc, &level)) { 147 int level;
134 if (level == 2) { 148 if (IsPrinterPostScript(hdc, &level)) {
149 if (level == 2) {
150 print_settings->set_printer_type(
151 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2);
152 return;
153 }
154 DCHECK_EQ(3, level);
135 print_settings->set_printer_type( 155 print_settings->set_printer_type(
136 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2); 156 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
137 return; 157 return;
138 } 158 }
139 DCHECK_EQ(3, level);
140 print_settings->set_printer_type(
141 PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
142 return;
143 } 159 }
144 if (IsPrinterXPS(hdc)) { 160 if (IsPrinterXPS(hdc)) {
145 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS); 161 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
146 return; 162 return;
147 } 163 }
148 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE); 164 print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
149 } 165 }
150 166
151 } // namespace printing 167 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698