1 // Copyright 2013 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.
5 #include "printing/printing_utils.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "build/build_config.h"
15 #include "build/chromeos_buildflags.h"
16 #include "third_party/icu/source/common/unicode/uchar.h"
17 #include "ui/gfx/text_elider.h"
19 #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
20 #include <unicode/ulocdata.h>
24 #include "base/strings/string_piece.h"
25 #include "printing/units.h"
26 #include "ui/gfx/geometry/size.h"
33 constexpr size_t kMaxDocumentTitleLength = 80;
35 #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
36 constexpr gfx::Size kIsoA4Microns = gfx::Size(210000, 297000);
41 std::u16string SimplifyDocumentTitleWithLength(const std::u16string& title,
43 std::u16string no_controls(title);
45 std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
48 static constexpr const char* kCharsToReplace[] = {
49 "\\", "/", "<", ">", ":", "\"", "'", "|", "?", "*", "~",
51 for (const char* c : kCharsToReplace) {
52 base::ReplaceChars(no_controls, base::ASCIIToUTF16(c), u"_", &no_controls);
55 std::u16string result;
56 gfx::ElideString(no_controls, length, &result);
60 std::u16string FormatDocumentTitleWithOwnerAndLength(
61 const std::u16string& owner,
62 const std::u16string& title,
64 const std::u16string separator = u": ";
65 DCHECK_LT(separator.size(), length);
67 std::u16string short_title =
68 SimplifyDocumentTitleWithLength(owner, length - separator.size());
69 short_title += separator;
70 if (short_title.size() < length) {
72 SimplifyDocumentTitleWithLength(title, length - short_title.size());
78 std::u16string SimplifyDocumentTitle(const std::u16string& title) {
79 return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
82 std::u16string FormatDocumentTitleWithOwner(const std::u16string& owner,
83 const std::u16string& title) {
84 return FormatDocumentTitleWithOwnerAndLength(owner, title,
85 kMaxDocumentTitleLength);
88 #if defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
89 gfx::Size GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale) {
95 UErrorCode error = U_ZERO_ERROR;
96 ulocdata_getPaperSize(std::string(locale).c_str(), &height, &width, &error);
97 if (error > U_ZERO_ERROR) {
98 // If the call failed, assume Letter paper size.
99 LOG(WARNING) << "ulocdata_getPaperSize failed, using ISO A4 Paper, error: "
102 return kIsoA4Microns;
104 // Convert millis to microns
105 return gfx::Size(width * 1000, height * 1000);
108 bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
109 const gfx::Size& rhs,
111 DCHECK_GE(epsilon, 0);
113 if (lhs.IsEmpty() && rhs.IsEmpty())
116 return std::abs(lhs.width() - rhs.width()) <= epsilon &&
117 std::abs(lhs.height() - rhs.height()) <= epsilon;
119 #endif // defined(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
121 #if BUILDFLAG(IS_WIN)
122 gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
123 const gfx::Size& page_size,
124 const gfx::Rect& page_content_rect) {
125 gfx::Rect content_rect = page_content_rect;
126 if (paper_size.width() > page_size.width()) {
127 int diff = paper_size.width() - page_size.width();
128 content_rect.set_x(content_rect.x() + diff / 2);
130 if (paper_size.height() > page_size.height()) {
131 int diff = paper_size.height() - page_size.height();
132 content_rect.set_y(content_rect.y() + diff / 2);
136 #endif // BUILDFLAG(IS_WIN)
138 bool LooksLikePdf(base::span<const char> maybe_pdf_data) {
139 return maybe_pdf_data.size() >= 50u &&
140 std::memcmp(maybe_pdf_data.data(), "%PDF-", 5) == 0;
143 } // namespace printing