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
5 #include "printing/printing_utils.h"
6
7 #include <unicode/ulocdata.h>
8
9 #include <algorithm>
10 #include <cmath>
11 #include <string>
12
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "printing/units.h"
20 #include "third_party/icu/source/common/unicode/uchar.h"
21 #include "ui/gfx/geometry/size.h"
22 #include "ui/gfx/text_elider.h"
23
24 namespace printing {
25
26 namespace {
27
28 constexpr size_t kMaxDocumentTitleLength = 80;
29 constexpr gfx::Size kIsoA4Microns = gfx::Size(210000, 297000);
30
31 constexpr int kMicronsPerMM = 1000;
32 constexpr double kMMPerInch = 25.4;
33 constexpr double kMicronsPerInch = kMMPerInch * kMicronsPerMM;
34
35 // Defines two prefixes of a special breed of media sizes not meant for
36 // users' eyes. CUPS incidentally returns these IPP values to us, but
37 // we have no use for them.
38 constexpr base::StringPiece kMediaCustomMinPrefix = "custom_min";
39 constexpr base::StringPiece kMediaCustomMaxPrefix = "custom_max";
40
41 enum Unit {
42 INCHES,
43 MILLIMETERS,
44 };
45
DimensionsToMicrons(base::StringPiece value)46 gfx::Size DimensionsToMicrons(base::StringPiece value) {
47 Unit unit;
48 base::StringPiece dims;
49 size_t unit_position;
50 if ((unit_position = value.find("mm")) != base::StringPiece::npos) {
51 unit = MILLIMETERS;
52 dims = value.substr(0, unit_position);
53 } else if ((unit_position = value.find("in")) != base::StringPiece::npos) {
54 unit = INCHES;
55 dims = value.substr(0, unit_position);
56 } else {
57 LOG(WARNING) << "Could not parse paper dimensions";
58 return {0, 0};
59 }
60
61 double width;
62 double height;
63 std::vector<std::string> pieces = base::SplitString(
64 dims, "x", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
65 if (pieces.size() != 2 || !base::StringToDouble(pieces[0], &width) ||
66 !base::StringToDouble(pieces[1], &height)) {
67 return {0, 0};
68 }
69
70 int width_microns;
71 int height_microns;
72 switch (unit) {
73 case MILLIMETERS:
74 width_microns = width * kMicronsPerMM;
75 height_microns = height * kMicronsPerMM;
76 break;
77 case INCHES:
78 width_microns = width * kMicronsPerInch;
79 height_microns = height * kMicronsPerInch;
80 break;
81 default:
82 NOTREACHED();
83 break;
84 }
85
86 return gfx::Size{width_microns, height_microns};
87 }
88
89 } // namespace
90
SimplifyDocumentTitleWithLength(const base::string16 & title,size_t length)91 base::string16 SimplifyDocumentTitleWithLength(const base::string16& title,
92 size_t length) {
93 base::string16 no_controls(title);
94 no_controls.erase(
95 std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
96 no_controls.end());
97
98 static constexpr const char* kCharsToReplace[] = {
99 "\\", "/", "<", ">", ":", "\"", "'", "|", "?", "*", "~",
100 };
101 for (const char* c : kCharsToReplace) {
102 base::ReplaceChars(no_controls, base::ASCIIToUTF16(c),
103 base::ASCIIToUTF16("_"), &no_controls);
104 }
105
106 base::string16 result;
107 gfx::ElideString(no_controls, length, &result);
108 return result;
109 }
110
FormatDocumentTitleWithOwnerAndLength(const base::string16 & owner,const base::string16 & title,size_t length)111 base::string16 FormatDocumentTitleWithOwnerAndLength(
112 const base::string16& owner,
113 const base::string16& title,
114 size_t length) {
115 const base::string16 separator = base::ASCIIToUTF16(": ");
116 DCHECK_LT(separator.size(), length);
117
118 base::string16 short_title =
119 SimplifyDocumentTitleWithLength(owner, length - separator.size());
120 short_title += separator;
121 if (short_title.size() < length) {
122 short_title +=
123 SimplifyDocumentTitleWithLength(title, length - short_title.size());
124 }
125
126 return short_title;
127 }
128
SimplifyDocumentTitle(const base::string16 & title)129 base::string16 SimplifyDocumentTitle(const base::string16& title) {
130 return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
131 }
132
FormatDocumentTitleWithOwner(const base::string16 & owner,const base::string16 & title)133 base::string16 FormatDocumentTitleWithOwner(const base::string16& owner,
134 const base::string16& title) {
135 return FormatDocumentTitleWithOwnerAndLength(owner, title,
136 kMaxDocumentTitleLength);
137 }
138
GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale)139 gfx::Size GetDefaultPaperSizeFromLocaleMicrons(base::StringPiece locale) {
140 if (locale.empty())
141 return kIsoA4Microns;
142
143 int32_t width = 0;
144 int32_t height = 0;
145 UErrorCode error = U_ZERO_ERROR;
146 ulocdata_getPaperSize(locale.as_string().c_str(), &height, &width, &error);
147 if (error > U_ZERO_ERROR) {
148 // If the call failed, assume Letter paper size.
149 LOG(WARNING) << "ulocdata_getPaperSize failed, using ISO A4 Paper, error: "
150 << error;
151
152 return kIsoA4Microns;
153 }
154 // Convert millis to microns
155 return gfx::Size(width * 1000, height * 1000);
156 }
157
SizesEqualWithinEpsilon(const gfx::Size & lhs,const gfx::Size & rhs,int epsilon)158 bool SizesEqualWithinEpsilon(const gfx::Size& lhs,
159 const gfx::Size& rhs,
160 int epsilon) {
161 DCHECK_GE(epsilon, 0);
162
163 if (lhs.IsEmpty() && rhs.IsEmpty())
164 return true;
165
166 return std::abs(lhs.width() - rhs.width()) <= epsilon &&
167 std::abs(lhs.height() - rhs.height()) <= epsilon;
168 }
169
170 // We read the media name expressed by |value| and return a Paper
171 // with the vendor_id and size_um members populated.
172 // We don't handle l10n here. We do populate the display_name member
173 // with the prettified vendor ID, but fully expect the caller to clobber
174 // this if a better localization exists.
ParsePaper(base::StringPiece value)175 PrinterSemanticCapsAndDefaults::Paper ParsePaper(base::StringPiece value) {
176 // <name>_<width>x<height>{in,mm}
177 // e.g. na_letter_8.5x11in, iso_a4_210x297mm
178
179 std::vector<base::StringPiece> pieces = base::SplitStringPiece(
180 value, "_", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
181 // We expect at least a display string and a dimension string.
182 // Additionally, we drop the "custom_min*" and "custom_max*" special
183 // "sizes" (not for users' eyes).
184 if (pieces.size() < 2 || base::StartsWith(value, kMediaCustomMinPrefix) ||
185 base::StartsWith(value, kMediaCustomMaxPrefix)) {
186 return PrinterSemanticCapsAndDefaults::Paper();
187 }
188
189 base::StringPiece dimensions = pieces.back();
190
191 PrinterSemanticCapsAndDefaults::Paper paper;
192 paper.vendor_id = value.as_string();
193 paper.size_um = DimensionsToMicrons(dimensions);
194 // Omits the final token describing the media dimensions.
195 pieces.pop_back();
196 paper.display_name = base::JoinString(pieces, " ");
197
198 return paper;
199 }
200
201 } // namespace printing
202