printing_utils.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2013 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/printing_utils.h"
  5. #include <algorithm>
  6. #include <cstring>
  7. #include <string>
  8. #include "base/logging.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "third_party/icu/source/common/unicode/uchar.h"
  14. #include "ui/gfx/text_elider.h"
  15. #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
  16. #include <unicode/ulocdata.h>
  17. #include <cmath>
  18. #include "base/strings/string_piece.h"
  19. #include "printing/units.h"
  20. #include "ui/gfx/geometry/size.h"
  21. #endif
  22. namespace printing {
  23. namespace {
  24. constexpr size_t kMaxDocumentTitleLength = 80;
  25. #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
  26. constexpr gfx::Size kIsoA4Microns = gfx::Size(210000, 297000);
  27. #endif
  28. } // namespace
  29. std::u16string SimplifyDocumentTitleWithLength(const std::u16string& title,
  30. size_t length) {
  31. std::u16string no_controls(title);
  32. no_controls.erase(
  33. std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
  34. no_controls.end());
  35. static constexpr const char* kCharsToReplace[] = {
  36. "\\", "/", "<", ">", ":", "\"", "'", "|", "?", "*", "~",
  37. };
  38. for (const char* c : kCharsToReplace) {
  39. base::ReplaceChars(no_controls, base::ASCIIToUTF16(c), u"_", &no_controls);
  40. }
  41. std::u16string result;
  42. gfx::ElideString(no_controls, length, &result);
  43. return result;
  44. }
  45. std::u16string FormatDocumentTitleWithOwnerAndLength(
  46. const std::u16string& owner,
  47. const std::u16string& title,
  48. size_t length) {
  49. const std::u16string separator = u": ";
  50. DCHECK_LT(separator.size(), length);
  51. std::u16string short_title =
  52. SimplifyDocumentTitleWithLength(owner, length - separator.size());
  53. short_title += separator;
  54. if (short_title.size() < length) {
  55. short_title +=
  56. SimplifyDocumentTitleWithLength(title, length - short_title.size());
  57. }
  58. return short_title;
  59. }
  60. std::u16string SimplifyDocumentTitle(const std::u16string& title) {
  61. return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
  62. }
  63. std::u16string FormatDocumentTitleWithOwner(const std::u16string& owner,
  64. const std::u16string& title) {
  65. return FormatDocumentTitleWithOwnerAndLength(owner, title,
  66. kMaxDocumentTitleLength);
  67. }
  68. #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
  69. gfx::Size GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale) {
  70. if (locale.empty())
  71. return kIsoA4Microns;
  72. int32_t width = 0;
  73. int32_t height = 0;
  74. UErrorCode error = U_ZERO_ERROR;
  75. ulocdata_getPaperSize(std::string(locale).c_str(), &height, &width, &error);
  76. if (error > U_ZERO_ERROR) {
  77. // If the call failed, assume Letter paper size.
  78. LOG(WARNING) << "ulocdata_getPaperSize failed, using ISO A4 Paper, error: "
  79. << error;
  80. return kIsoA4Microns;
  81. }
  82. // Convert millis to microns
  83. return gfx::Size(width * 1000, height * 1000);
  84. }
  85. bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
  86. const gfx::Size& rhs,
  87. int epsilon) {
  88. DCHECK_GE(epsilon, 0);
  89. if (lhs.IsEmpty() && rhs.IsEmpty())
  90. return true;
  91. return std::abs(lhs.width() - rhs.width()) <= epsilon &&
  92. std::abs(lhs.height() - rhs.height()) <= epsilon;
  93. }
  94. #endif // defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
  95. #if BUILDFLAG(IS_WIN)
  96. gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
  97. const gfx::Size& page_size,
  98. const gfx::Rect& page_content_rect) {
  99. gfx::Rect content_rect = page_content_rect;
  100. if (paper_size.width() > page_size.width()) {
  101. int diff = paper_size.width() - page_size.width();
  102. content_rect.set_x(content_rect.x() + diff / 2);
  103. }
  104. if (paper_size.height() > page_size.height()) {
  105. int diff = paper_size.height() - page_size.height();
  106. content_rect.set_y(content_rect.y() + diff / 2);
  107. }
  108. return content_rect;
  109. }
  110. #endif // BUILDFLAG(IS_WIN)
  111. bool LooksLikePdf(base::span<const char> maybe_pdf_data) {
  112. return maybe_pdf_data.size() >= 50u &&
  113. std::memcmp(maybe_pdf_data.data(), "%PDF-", 5) == 0;
  114. }
  115. } // namespace printing