Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / printing_utils.cc
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 <stddef.h>
8
9 #include <algorithm>
10
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/icu/source/common/unicode/uchar.h"
15 #include "ui/gfx/text_elider.h"
16
17 namespace printing {
18
19 namespace {
20
21 const size_t kMaxDocumentTitleLength = 80;
22
23 }  // namespace
24
25 base::string16 SimplifyDocumentTitleWithLength(const base::string16& title,
26                                                size_t length) {
27   base::string16 no_controls(title);
28   no_controls.erase(
29       std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl),
30       no_controls.end());
31   base::ReplaceChars(no_controls, base::ASCIIToUTF16("\\"),
32                      base::ASCIIToUTF16("_"), &no_controls);
33   base::string16 result;
34   gfx::ElideString(no_controls, length, &result);
35   return result;
36 }
37
38 base::string16 FormatDocumentTitleWithOwnerAndLength(
39     const base::string16& owner,
40     const base::string16& title,
41     size_t length) {
42   const base::string16 separator = base::ASCIIToUTF16(": ");
43   DCHECK_LT(separator.size(), length);
44
45   base::string16 short_title =
46       SimplifyDocumentTitleWithLength(owner, length - separator.size());
47   short_title += separator;
48   if (short_title.size() < length) {
49     short_title +=
50         SimplifyDocumentTitleWithLength(title, length - short_title.size());
51   }
52
53   return short_title;
54 }
55
56 base::string16 SimplifyDocumentTitle(const base::string16& title) {
57   return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
58 }
59
60 base::string16 FormatDocumentTitleWithOwner(const base::string16& owner,
61                                             const base::string16& title) {
62   return FormatDocumentTitleWithOwnerAndLength(owner, title,
63                                                kMaxDocumentTitleLength);
64 }
65
66 }  // namespace printing