Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / print_settings_initializer_win.cc
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
3 // found in the LICENSE file.
4
5 #include "printing/print_settings_initializer_win.h"
6
7 #include <windows.h>
8
9 #include "printing/backend/win_helper.h"
10 #include "printing/print_settings.h"
11
12 namespace printing {
13
14 namespace {
15
16 bool HasEscapeSupport(HDC hdc, DWORD escape) {
17   const char* ptr = reinterpret_cast<const char*>(&escape);
18   return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
19 }
20
21 bool IsTechnology(HDC hdc, const char* technology) {
22   if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
23     return false;
24
25   if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
26     return false;
27
28   char buf[256];
29   memset(buf, 0, sizeof(buf));
30   if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
31     return false;
32   return strcmp(buf, technology) == 0;
33 }
34
35 void SetPrinterToGdiMode(HDC hdc) {
36   // Try to set to GDI centric mode
37   DWORD mode = PSIDENT_GDICENTRIC;
38   const char* ptr = reinterpret_cast<const char*>(&mode);
39   ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
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 0;
52 }
53
54 bool IsPrinterPostScript(HDC hdc, int* level) {
55   static constexpr char kPostScriptDriver[] = "PostScript";
56
57   // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
58   // mode to check the language level supported. See if it looks like a
59   // postscript printer and supports the postscript functions that are
60   // supported in compatability mode. If so set to level 2 postscript.
61   if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
62     if (!IsTechnology(hdc, kPostScriptDriver))
63       return false;
64     if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
65         !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
66       return false;
67     }
68     *level = 2;
69     return true;
70   }
71
72   // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
73   // driver. Set the printer to GDI mode in order to query the postscript
74   // level. Use GDI mode instead of PostScript mode so that if level detection
75   // fails or returns language level < 2 we can fall back to normal printing.
76   // Note: This escape must be called before other escapes.
77   SetPrinterToGdiMode(hdc);
78   if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
79     // Can't query the level, use level 2 to be safe
80     *level = 2;
81     return true;
82   }
83
84   // Get the language level. If invalid or < 2, return false to set printer to
85   // normal printing mode.
86   *level = GetPrinterPostScriptLevel(hdc);
87   return *level == 2 || *level == 3;
88 }
89
90 bool IsPrinterXPS(HDC hdc) {
91   static constexpr char kXPSDriver[] =
92       "http://schemas.microsoft.com/xps/2005/06";
93   return IsTechnology(hdc, kXPSDriver);
94 }
95
96 bool IsPrinterTextOnly(HDC hdc) {
97   return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
98 }
99 }  // namespace
100
101 // static
102 void PrintSettingsInitializerWin::InitPrintSettings(
103     HDC hdc,
104     const DEVMODE& dev_mode,
105     PrintSettings* print_settings) {
106   DCHECK(hdc);
107   DCHECK(print_settings);
108
109   print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
110   int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
111   int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
112   print_settings->set_dpi_xy(dpi_x, dpi_y);
113   const int kAlphaCaps = SB_CONST_ALPHA | SB_PIXEL_ALPHA;
114   print_settings->set_supports_alpha_blend(
115     (GetDeviceCaps(hdc, SHADEBLENDCAPS) & kAlphaCaps) == kAlphaCaps);
116
117   DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
118   DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
119
120   // Initialize |page_setup_device_units_|.
121   // Blink doesn't support different dpi settings in X and Y axis. However,
122   // some printers use them. So, to avoid a bad page calculation, scale page
123   // size components based on the dpi in the appropriate dimension.
124   int dpi = print_settings->dpi();
125   gfx::Size physical_size_device_units(
126       GetDeviceCaps(hdc, PHYSICALWIDTH) * dpi / dpi_x,
127       GetDeviceCaps(hdc, PHYSICALHEIGHT) * dpi / dpi_y);
128   gfx::Rect printable_area_device_units(
129       GetDeviceCaps(hdc, PHYSICALOFFSETX) * dpi / dpi_x,
130       GetDeviceCaps(hdc, PHYSICALOFFSETY) * dpi / dpi_y,
131       GetDeviceCaps(hdc, HORZRES) * dpi / dpi_x,
132       GetDeviceCaps(hdc, VERTRES) * dpi / dpi_y);
133
134   // Sanity check the printable_area: we've seen crashes caused by a printable
135   // area rect of 0, 0, 0, 0, so it seems some drivers don't set it.
136   if (printable_area_device_units.IsEmpty() ||
137       !gfx::Rect(physical_size_device_units).Contains(
138           printable_area_device_units)) {
139     printable_area_device_units = gfx::Rect(physical_size_device_units);
140   }
141   DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
142   print_settings->SetPrinterPrintableArea(physical_size_device_units,
143                                           printable_area_device_units,
144                                           false);
145
146   print_settings->set_color(IsDevModeWithColor(&dev_mode) ? COLOR : GRAY);
147
148   // Check for postscript first so that we can change the mode with the
149   // first command.
150   int level;
151   if (IsPrinterPostScript(hdc, &level)) {
152     if (level == 2) {
153       print_settings->set_printer_type(
154           PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL2);
155       return;
156     }
157     DCHECK_EQ(3, level);
158     print_settings->set_printer_type(
159         PrintSettings::PrinterType::TYPE_POSTSCRIPT_LEVEL3);
160     return;
161   }
162   // Detects the generic / text only driver.
163   if (IsPrinterTextOnly(hdc)) {
164     print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_TEXTONLY);
165     return;
166   }
167   if (IsPrinterXPS(hdc)) {
168     print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_XPS);
169     return;
170   }
171   print_settings->set_printer_type(PrintSettings::PrinterType::TYPE_NONE);
172 }
173
174 }  // namespace printing