blob: 353a0aab1e9a4aca65857e0a6f9f61eee6268528 [file] [log] [blame]
[email protected]ec91bfa2013-11-09 17:24:271// 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
avi126e93c2015-12-21 21:48:167#include <stddef.h>
8
[email protected]ec91bfa2013-11-09 17:24:279#include <algorithm>
10
vitalybuka27e401c2015-07-07 18:21:1611#include "base/logging.h"
thestig3ccfd1f2016-02-03 21:46:3112#include "base/strings/string_util.h"
vitalybuka27e401c2015-07-07 18:21:1613#include "base/strings/utf_string_conversions.h"
[email protected]ec91bfa2013-11-09 17:24:2714#include "third_party/icu/source/common/unicode/uchar.h"
15#include "ui/gfx/text_elider.h"
16
[email protected]ec91bfa2013-11-09 17:24:2717namespace printing {
18
vitalybuka27e401c2015-07-07 18:21:1619namespace {
20
21const size_t kMaxDocumentTitleLength = 80;
22
23} // namespace
24
25base::string16 SimplifyDocumentTitleWithLength(const base::string16& title,
26 size_t length) {
[email protected]ec91bfa2013-11-09 17:24:2727 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());
thestig3ccfd1f2016-02-03 21:46:3131 base::ReplaceChars(no_controls, base::ASCIIToUTF16("\\"),
32 base::ASCIIToUTF16("_"), &no_controls);
[email protected]ec91bfa2013-11-09 17:24:2733 base::string16 result;
thestigbbf93ac2016-02-02 00:24:4934 gfx::ElideString(no_controls, length, &result);
[email protected]ec91bfa2013-11-09 17:24:2735 return result;
36}
37
vitalybuka27e401c2015-07-07 18:21:1638base::string16 FormatDocumentTitleWithOwnerAndLength(
39 const base::string16& owner,
40 const base::string16& title,
41 size_t length) {
42 const base::string16 separator = base::ASCIIToUTF16(": ");
thestig3ccfd1f2016-02-03 21:46:3143 DCHECK_LT(separator.size(), length);
vitalybuka27e401c2015-07-07 18:21:1644
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
56base::string16 SimplifyDocumentTitle(const base::string16& title) {
57 return SimplifyDocumentTitleWithLength(title, kMaxDocumentTitleLength);
58}
59
60base::string16 FormatDocumentTitleWithOwner(const base::string16& owner,
61 const base::string16& title) {
62 return FormatDocumentTitleWithOwnerAndLength(owner, title,
63 kMaxDocumentTitleLength);
64}
65
[email protected]ec91bfa2013-11-09 17:24:2766} // namespace printing