print_settings_initializer_win.cc 6.2 KB

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