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 BUILDFLAG(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"
37 constexpr size_t kMaxDocumentTitleLength = 80;
39 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
40 constexpr gfx::Size kIsoA4Microns = gfx::Size(210000, 297000);
45 std::u16string SimplifyDocumentTitleWithLength(const std::u16string& title,
47 std::u16string no_controls(title);
49 std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
52 static constexpr const char* kCharsToReplace[] = {
53 "\\", "/", "<", ">", ":", "\"", "'", "|", "?", "*", "~",
55 for (const char* c : kCharsToReplace) {
56 base::ReplaceChars(no_controls, base::ASCIIToUTF16(c), u"_", &no_controls);
59 std::u16string result;
60 gfx::ElideString(no_controls, length, &result);
64 std::u16string FormatDocumentTitleWithOwnerAndLength(
65 const std::u16string& owner,
66 const std::u16string& title,
68 const std::u16string separator = u": ";
69 DCHECK_LT(separator.size(), length);
71 std::u16string short_title =
72 SimplifyDocumentTitleWithLength(owner, length - separator.size());
73 short_title += separator;
74 if (short_title.size() < length) {
76 SimplifyDocumentTitleWithLength(title, length - short_title.size());
82 std::u16string SimplifyDocumentTitle(const std::u16string& title) {
83 return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
86 std::u16string FormatDocumentTitleWithOwner(const std::u16string& owner,
87 const std::u16string& title) {
88 return FormatDocumentTitleWithOwnerAndLength(owner, title,
89 kMaxDocumentTitleLength);
92 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
93 gfx::Size GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale) {
99 UErrorCode error = U_ZERO_ERROR;
100 ulocdata_getPaperSize(std::string(locale).c_str(), &height, &width, &error);
101 if (error > U_ZERO_ERROR) {
102 // If the call failed, assume Letter paper size.
103 LOG(WARNING) << "ulocdata_getPaperSize failed, using ISO A4 Paper, error: "
106 return kIsoA4Microns;
108 // Convert millis to microns
109 return gfx::Size(width * kMicronsPerMm, height * kMicronsPerMm);
112 bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
113 const gfx::Size& rhs,
115 DCHECK_GE(epsilon, 0);
117 if (lhs.IsEmpty() && rhs.IsEmpty())
120 return std::abs(lhs.width() - rhs.width()) <= epsilon &&
121 std::abs(lhs.height() - rhs.height()) <= epsilon;
123 #endif // BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
125 #if BUILDFLAG(IS_WIN)
126 gfx::Rect GetCenteredPageContentRect(const gfx::Size& paper_size,
127 const gfx::Size& page_size,
128 const gfx::Rect& page_content_rect) {
129 gfx::Rect content_rect = page_content_rect;
130 if (paper_size.width() > page_size.width()) {
131 int diff = paper_size.width() - page_size.width();
132 content_rect.set_x(content_rect.x() + diff / 2);
134 if (paper_size.height() > page_size.height()) {
135 int diff = paper_size.height() - page_size.height();
136 content_rect.set_y(content_rect.y() + diff / 2);
141 gfx::Rect GetPrintableAreaDeviceUnits(HDC hdc) {
144 gfx::Size physical_size_device_units(GetDeviceCaps(hdc, PHYSICALWIDTH),
145 GetDeviceCaps(hdc, PHYSICALHEIGHT));
146 gfx::Rect printable_area_device_units(
147 GetDeviceCaps(hdc, PHYSICALOFFSETX), GetDeviceCaps(hdc, PHYSICALOFFSETY),
148 GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
150 // Sanity check the printable_area: we've seen crashes caused by a printable
151 // area rect of 0, 0, 0, 0, so it seems some drivers don't set it.
152 if (printable_area_device_units.IsEmpty() ||
153 !gfx::Rect(physical_size_device_units)
154 .Contains(printable_area_device_units)) {
155 printable_area_device_units = gfx::Rect(physical_size_device_units);
158 return printable_area_device_units;
160 #endif // BUILDFLAG(IS_WIN)
162 bool LooksLikePdf(base::span<const char> maybe_pdf_data) {
163 return maybe_pdf_data.size() >= 50u &&
164 std::memcmp(maybe_pdf_data.data(), "%PDF-", 5) == 0;
167 } // namespace printing