[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 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 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
vitalybuka | 27e401c | 2015-07-07 18:21:16 | [diff] [blame] | 11 | #include "base/logging.h" |
thestig | 3ccfd1f | 2016-02-03 21:46:31 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
vitalybuka | 27e401c | 2015-07-07 18:21:16 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 14 | #include "third_party/icu/source/common/unicode/uchar.h" |
| 15 | #include "ui/gfx/text_elider.h" |
| 16 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 17 | namespace printing { |
| 18 | |
vitalybuka | 27e401c | 2015-07-07 18:21:16 | [diff] [blame] | 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) { |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 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()); |
thestig | 3ccfd1f | 2016-02-03 21:46:31 | [diff] [blame] | 31 | base::ReplaceChars(no_controls, base::ASCIIToUTF16("\\"), |
| 32 | base::ASCIIToUTF16("_"), &no_controls); |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 33 | base::string16 result; |
thestig | bbf93ac | 2016-02-02 00:24:49 | [diff] [blame] | 34 | gfx::ElideString(no_controls, length, &result); |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 35 | return result; |
| 36 | } |
| 37 | |
vitalybuka | 27e401c | 2015-07-07 18:21:16 | [diff] [blame] | 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(": "); |
thestig | 3ccfd1f | 2016-02-03 21:46:31 | [diff] [blame] | 43 | DCHECK_LT(separator.size(), length); |
vitalybuka | 27e401c | 2015-07-07 18:21:16 | [diff] [blame] | 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 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 66 | } // namespace printing |