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