[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[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 #include "printing/printing_utils.h"
13
14 namespace printing {
15
16 namespace {
17
18 bool HasEscapeSupport(HDC hdc, DWORD escape) {
19   const char* ptr = reinterpret_cast<const char*>(&escape);
20   return ExtEscape(hdc, QUERYESCSUPPORT, sizeof(escape), ptr, 0, nullptr) > 0;
21 }
22
23 bool IsTechnology(HDC hdc, const char* technology) {
24   if (::GetDeviceCaps(hdc, TECHNOLOGY) != DT_RASPRINTER)
25     return false;
26
27   if (!HasEscapeSupport(hdc, GETTECHNOLOGY))
28     return false;
29
30   char buf[256];
31   memset(buf, 0, sizeof(buf));
32   if (ExtEscape(hdc, GETTECHNOLOGY, 0, nullptr, sizeof(buf) - 1, buf) <= 0)
33     return false;
34   return strcmp(buf, technology) == 0;
35 }
36
37 void SetPrinterToGdiMode(HDC hdc) {
38   // Try to set to GDI centric mode
39   DWORD mode = PSIDENT_GDICENTRIC;
40   const char* ptr = reinterpret_cast<const char*>(&mode);
41   ExtEscape(hdc, POSTSCRIPT_IDENTIFY, sizeof(DWORD), ptr, 0, nullptr);
42 }
43
44 int GetPrinterPostScriptLevel(HDC hdc) {
45   constexpr int param = FEATURESETTING_PSLEVEL;
46   const char* param_char_ptr = reinterpret_cast<const char*>(&param);
47   int param_out = 0;
48   char* param_out_char_ptr = reinterpret_cast<char*>(&param_out);
49   if (ExtEscape(hdc, GET_PS_FEATURESETTING, sizeof(param), param_char_ptr,
50                 sizeof(param_out), param_out_char_ptr) > 0) {
51     return param_out;
52   }
53   return 0;
54 }
55
56 bool IsPrinterPostScript(HDC hdc, int* level) {
57   static constexpr char kPostScriptDriver[] = "PostScript";
58
59   // If printer does not support POSTSCRIPT_IDENTIFY, it cannot be set to GDI
60   // mode to check the language level supported. See if it looks like a
61   // postscript printer and supports the postscript functions that are
62   // supported in compatability mode. If so set to level 2 postscript.
63   if (!HasEscapeSupport(hdc, POSTSCRIPT_IDENTIFY)) {
64     if (!IsTechnology(hdc, kPostScriptDriver))
65       return false;
66     if (!HasEscapeSupport(hdc, POSTSCRIPT_PASSTHROUGH) ||
67         !HasEscapeSupport(hdc, POSTSCRIPT_DATA)) {
68       return false;
69     }
70     *level = 2;
71     return true;
72   }
73
74   // Printer supports POSTSCRIPT_IDENTIFY so we can assume it has a postscript
75   // driver. Set the printer to GDI mode in order to query the postscript
76   // level. Use GDI mode instead of PostScript mode so that if level detection
77   // fails or returns language level < 2 we can fall back to normal printing.
78   // Note: This escape must be called before other escapes.
79   SetPrinterToGdiMode(hdc);
80   if (!HasEscapeSupport(hdc, GET_PS_FEATURESETTING)) {
81     // Can't query the level, use level 2 to be safe
82     *level = 2;
83     return true;
84   }
85
86   // Get the language level. If invalid or < 2, return false to set printer to
87   // normal printing mode.
88   *level = GetPrinterPostScriptLevel(hdc);
89   return *level == 2 || *level == 3;
90 }
91
92 bool IsPrinterXPS(HDC hdc) {
93   static constexpr char kXPSDriver[] =
94       "http://schemas.microsoft.com/xps/2005/06";
95   return IsTechnology(hdc, kXPSDriver);
96 }
97
98 bool IsPrinterTextOnly(HDC hdc) {
99   return ::GetDeviceCaps(hdc, TECHNOLOGY) == DT_CHARSTREAM;
100 }
101 }  // namespace
102
103 // static
104 void PrintSettingsInitializerWin::InitPrintSettings(
105     HDC hdc,
106     const DEVMODE& dev_mode,
107     PrintSettings* print_settings) {
108   DCHECK(hdc);
109   DCHECK(print_settings);
110
111   print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE);
112   int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
113   int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
114   print_settings->set_dpi_xy(dpi_x, dpi_y);
115   int dpi = print_settings->dpi();
116   DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
117
118   DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0);
119   DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0);
120
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   gfx::Size physical_size_scaled(
125       GetDeviceCaps(hdc, PHYSICALWIDTH) * dpi / dpi_x,
126       GetDeviceCaps(hdc, PHYSICALHEIGHT) * dpi / dpi_y);
127   gfx::Rect printable_area_device_units = GetPrintableAreaDeviceUnits(hdc);
128   gfx::Rect printable_area_scaled =
129       gfx::Rect(printable_area_device_units.x() * dpi / dpi_x,
130                 printable_area_device_units.y() * dpi / dpi_y,
131                 printable_area_device_units.width() * dpi / dpi_x,
132                 printable_area_device_units.height() * dpi / dpi_y);
133   print_settings->SetPrinterPrintableArea(physical_size_scaled,
134                                           printable_area_scaled, false);
135
136   print_settings->set_color(IsDevModeWithColor(&dev_mode)
137                                 ? mojom::ColorModel::kColor
138                                 : mojom::ColorModel::kGray);
139
140   // Check for postscript first so that we can change the mode with the
141   // first command.
142   int level;
143   if (IsPrinterPostScript(hdc, &level)) {
144     if (level == 2) {
145       print_settings->set_printer_language_type(
146           mojom::PrinterLanguageType::kPostscriptLevel2);
147       return;
148     }
149     DCHECK_EQ(3, level);
150     print_settings->set_printer_language_type(
151         mojom::PrinterLanguageType::kPostscriptLevel3);
152     return;
153   }
154   // Detects the generic / text only driver.
155   if (IsPrinterTextOnly(hdc)) {
156     print_settings->set_printer_language_type(
157         mojom::PrinterLanguageType::kTextOnly);
158     return;
159   }
160   if (IsPrinterXPS(hdc)) {
161     print_settings->set_printer_language_type(mojom::PrinterLanguageType::kXps);
162     return;
163   }
164   print_settings->set_printer_language_type(mojom::PrinterLanguageType::kNone);
165 }
166
167 }  // namespace printing