[M120 Migration][VD] Remove accessing oom_score_adj in zygote process
[platform/framework/web/chromium-efl.git] / printing / printing_utils.cc
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.
4
5 #include "printing/printing_utils.h"
6
7 #include <algorithm>
8 #include <cstring>
9 #include <string>
10
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"
18
19 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
20 #include <unicode/ulocdata.h>
21
22 #include <cmath>
23
24 #include "base/strings/string_piece.h"
25 #include "printing/units.h"
26 #include "ui/gfx/geometry/size.h"
27 #endif
28
29 #if BUILDFLAG(IS_WIN)
30 #include <windows.h>
31 #endif
32
33 namespace printing {
34
35 namespace {
36
37 constexpr size_t kMaxDocumentTitleLength = 80;
38
39 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
40 constexpr gfx::Size kIsoA4Microns = gfx::Size(210000, 297000);
41 #endif
42
43 }  // namespace
44
45 std::u16string SimplifyDocumentTitleWithLength(const std::u16string& title,
46                                                size_t length) {
47   std::u16string no_controls(title);
48   no_controls.erase(
49       std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
50       no_controls.end());
51
52   static constexpr const char* kCharsToReplace[] = {
53       "\\", "/", "<", ">", ":", "\"", "'", "|", "?", "*", "~",
54   };
55   for (const char* c : kCharsToReplace) {
56     base::ReplaceChars(no_controls, base::ASCIIToUTF16(c), u"_", &no_controls);
57   }
58
59   std::u16string result;
60   gfx::ElideString(no_controls, length, &result);
61   return result;
62 }
63
64 std::u16string FormatDocumentTitleWithOwnerAndLength(
65     const std::u16string& owner,
66     const std::u16string& title,
67     size_t length) {
68   const std::u16string separator = u": ";
69   DCHECK_LT(separator.size(), length);
70
71   std::u16string short_title =
72       SimplifyDocumentTitleWithLength(owner, length - separator.size());
73   short_title += separator;
74   if (short_title.size() < length) {
75     short_title +=
76         SimplifyDocumentTitleWithLength(title, length - short_title.size());
77   }
78
79   return short_title;
80 }
81
82 std::u16string SimplifyDocumentTitle(const std::u16string& title) {
83   return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
84 }
85
86 std::u16string FormatDocumentTitleWithOwner(const std::u16string& owner,
87                                             const std::u16string& title) {
88   return FormatDocumentTitleWithOwnerAndLength(owner, title,
89                                                kMaxDocumentTitleLength);
90 }
91
92 #if BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
93 gfx::Size GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale) {
94   if (locale.empty())
95     return kIsoA4Microns;
96
97   int32_t width = 0;
98   int32_t height = 0;
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: "
104                  << error;
105
106     return kIsoA4Microns;
107   }
108   // Convert millis to microns
109   return gfx::Size(width * kMicronsPerMm, height * kMicronsPerMm);
110 }
111
112 bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
113                              const gfx::Size& rhs,
114                              int epsilon) {
115   DCHECK_GE(epsilon, 0);
116
117   if (lhs.IsEmpty() && rhs.IsEmpty())
118     return true;
119
120   return std::abs(lhs.width() - rhs.width()) <= epsilon &&
121          std::abs(lhs.height() - rhs.height()) <= epsilon;
122 }
123 #endif  // BUILDFLAG(USE_CUPS) && !BUILDFLAG(IS_CHROMEOS_ASH)
124
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);
133   }
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);
137   }
138   return content_rect;
139 }
140
141 gfx::Rect GetPrintableAreaDeviceUnits(HDC hdc) {
142   DCHECK(hdc);
143
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));
149
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);
156   }
157
158   return printable_area_device_units;
159 }
160 #endif  // BUILDFLAG(IS_WIN)
161
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;
165 }
166
167 }  // namespace printing