printing_gtk_util.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2014 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 "ui/gtk/printing/printing_gtk_util.h"
  5. #include <string>
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "printing/print_settings.h"
  8. #include "printing/printing_context_linux.h"
  9. #include "printing/units.h"
  10. #include "ui/gfx/geometry/size.h"
  11. #include "ui/gfx/geometry/size_f.h"
  12. #include "ui/gtk/gtk_compat.h"
  13. namespace {
  14. const double kTopMarginInInch = 0.25;
  15. const double kBottomMarginInInch = 0.56;
  16. const double kLeftMarginInInch = 0.25;
  17. const double kRightMarginInInch = 0.25;
  18. } // namespace
  19. gfx::Size GetPdfPaperSizeDeviceUnitsGtk(
  20. printing::PrintingContextLinux* context) {
  21. GtkPageSetup* page_setup = gtk_page_setup_new();
  22. gfx::SizeF paper_size(
  23. gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH),
  24. gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH));
  25. g_object_unref(page_setup);
  26. const printing::PrintSettings& settings = context->settings();
  27. return gfx::Size(paper_size.width() * settings.device_units_per_inch(),
  28. paper_size.height() * settings.device_units_per_inch());
  29. }
  30. void InitPrintSettingsGtk(GtkPrintSettings* settings,
  31. GtkPageSetup* page_setup,
  32. printing::PrintSettings* print_settings) {
  33. DCHECK(settings);
  34. DCHECK(page_setup);
  35. DCHECK(print_settings);
  36. const char* printer_name = gtk_print_settings_get_printer(settings);
  37. std::u16string name =
  38. printer_name ? base::UTF8ToUTF16(printer_name) : std::u16string();
  39. print_settings->set_device_name(name);
  40. gfx::Size physical_size_device_units;
  41. gfx::Rect printable_area_device_units;
  42. int dpi = gtk_print_settings_get_resolution(settings);
  43. if (dpi) {
  44. // Initialize page_setup_device_units_.
  45. physical_size_device_units.SetSize(
  46. gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi,
  47. gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi);
  48. printable_area_device_units.SetRect(
  49. gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi,
  50. gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi,
  51. gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi,
  52. gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi);
  53. } else {
  54. // Use default values if we cannot get valid values from the print dialog.
  55. dpi = printing::kPixelsPerInch;
  56. double page_width_in_pixel = printing::kLetterWidthInch * dpi;
  57. double page_height_in_pixel = printing::kLetterHeightInch * dpi;
  58. physical_size_device_units.SetSize(static_cast<int>(page_width_in_pixel),
  59. static_cast<int>(page_height_in_pixel));
  60. printable_area_device_units.SetRect(
  61. static_cast<int>(kLeftMarginInInch * dpi),
  62. static_cast<int>(kTopMarginInInch * dpi),
  63. page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi,
  64. page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi);
  65. }
  66. print_settings->set_dpi(dpi);
  67. // Note: With the normal GTK print dialog, when the user selects the landscape
  68. // orientation, all that does is change the paper size. Which seems to be
  69. // enough to render the right output and send it to the printer.
  70. // The orientation value stays as portrait and does not actually affect
  71. // printing.
  72. // Thus this is only useful in print preview mode, where we manually set the
  73. // orientation and change the paper size ourselves.
  74. GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
  75. // Set before SetPrinterPrintableArea to make it flip area if necessary.
  76. print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);
  77. DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
  78. print_settings->SetPrinterPrintableArea(physical_size_device_units,
  79. printable_area_device_units, true);
  80. }