1 // Copyright (c) 2012 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.
5 // This file implements utility functions for eliding and formatting UI text.
7 // Note that several of the functions declared in text_elider.h are implemented
8 // in this file using helper classes in an unnamed namespace.
10 #include "ui/gfx/text_elider.h"
15 #include "base/files/file_path.h"
16 #include "base/i18n/break_iterator.h"
17 #include "base/i18n/char_iterator.h"
18 #include "base/i18n/rtl.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/strings/string_split.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/sys_string_conversions.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "third_party/icu/source/common/unicode/rbbi.h"
25 #include "third_party/icu/source/common/unicode/uloc.h"
26 #include "ui/gfx/font_list.h"
27 #include "ui/gfx/render_text.h"
28 #include "ui/gfx/text_utils.h"
30 using base::ASCIIToUTF16
;
31 using base::UTF8ToUTF16
;
32 using base::WideToUTF16
;
38 #if defined(OS_ANDROID) || defined(OS_IOS)
39 // The returned string will have at least one character besides the ellipsis
40 // on either side of '@'; if that's impossible, a single ellipsis is returned.
41 // If possible, only the username is elided. Otherwise, the domain is elided
42 // in the middle, splitting available width equally with the elided username.
43 // If the username is short enough that it doesn't need half the available
44 // width, the elided domain will occupy that extra width.
45 base::string16
ElideEmail(const base::string16
& email
,
46 const FontList
& font_list
,
47 float available_pixel_width
) {
48 if (GetStringWidthF(email
, font_list
) <= available_pixel_width
)
51 // Split the email into its local-part (username) and domain-part. The email
52 // spec allows for @ symbols in the username under some special requirements,
53 // but not in the domain part, so splitting at the last @ symbol is safe.
54 const size_t split_index
= email
.find_last_of('@');
55 DCHECK_NE(split_index
, base::string16::npos
);
56 base::string16 username
= email
.substr(0, split_index
);
57 base::string16 domain
= email
.substr(split_index
+ 1);
58 DCHECK(!username
.empty());
59 DCHECK(!domain
.empty());
61 // Subtract the @ symbol from the available width as it is mandatory.
62 const base::string16 kAtSignUTF16
= ASCIIToUTF16("@");
63 available_pixel_width
-= GetStringWidthF(kAtSignUTF16
, font_list
);
65 // Check whether eliding the domain is necessary: if eliding the username
66 // is sufficient, the domain will not be elided.
67 const float full_username_width
= GetStringWidthF(username
, font_list
);
68 const float available_domain_width
=
69 available_pixel_width
-
70 std::min(full_username_width
,
71 GetStringWidthF(username
.substr(0, 1) + kEllipsisUTF16
,
73 if (GetStringWidthF(domain
, font_list
) > available_domain_width
) {
74 // Elide the domain so that it only takes half of the available width.
75 // Should the username not need all the width available in its half, the
76 // domain will occupy the leftover width.
77 // If |desired_domain_width| is greater than |available_domain_width|: the
78 // minimal username elision allowed by the specifications will not fit; thus
79 // |desired_domain_width| must be <= |available_domain_width| at all cost.
80 const float desired_domain_width
=
81 std::min(available_domain_width
,
82 std::max(available_pixel_width
- full_username_width
,
83 available_pixel_width
/ 2));
84 domain
= ElideText(domain
, font_list
, desired_domain_width
, ELIDE_MIDDLE
);
85 // Failing to elide the domain such that at least one character remains
86 // (other than the ellipsis itself) remains: return a single ellipsis.
87 if (domain
.length() <= 1U)
88 return base::string16(kEllipsisUTF16
);
91 // Fit the username in the remaining width (at this point the elided username
92 // is guaranteed to fit with at least one character remaining given all the
93 // precautions taken earlier).
94 available_pixel_width
-= GetStringWidthF(domain
, font_list
);
95 username
= ElideText(username
, font_list
, available_pixel_width
, ELIDE_TAIL
);
96 return username
+ kAtSignUTF16
+ domain
;
103 const char kEllipsis
[] = "\xE2\x80\xA6";
104 const base::char16 kEllipsisUTF16
[] = { 0x2026, 0 };
105 const base::char16 kForwardSlash
= '/';
107 StringSlicer::StringSlicer(const base::string16
& text
,
108 const base::string16
& ellipsis
,
109 bool elide_in_middle
,
110 bool elide_at_beginning
)
113 elide_in_middle_(elide_in_middle
),
114 elide_at_beginning_(elide_at_beginning
) {
117 base::string16
StringSlicer::CutString(size_t length
, bool insert_ellipsis
) {
118 const base::string16 ellipsis_text
= insert_ellipsis
? ellipsis_
121 if (elide_at_beginning_
)
122 return ellipsis_text
+
123 text_
.substr(FindValidBoundaryBefore(text_
.length() - length
));
125 if (!elide_in_middle_
)
126 return text_
.substr(0, FindValidBoundaryBefore(length
)) + ellipsis_text
;
128 // We put the extra character, if any, before the cut.
129 const size_t half_length
= length
/ 2;
130 const size_t prefix_length
= FindValidBoundaryBefore(length
- half_length
);
131 const size_t suffix_start_guess
= text_
.length() - half_length
;
132 const size_t suffix_start
= FindValidBoundaryAfter(suffix_start_guess
);
133 const size_t suffix_length
=
134 half_length
- (suffix_start_guess
- suffix_start
);
135 return text_
.substr(0, prefix_length
) + ellipsis_text
+
136 text_
.substr(suffix_start
, suffix_length
);
139 size_t StringSlicer::FindValidBoundaryBefore(size_t index
) const {
140 DCHECK_LE(index
, text_
.length());
141 if (index
!= text_
.length())
142 U16_SET_CP_START(text_
.data(), 0, index
);
146 size_t StringSlicer::FindValidBoundaryAfter(size_t index
) const {
147 DCHECK_LE(index
, text_
.length());
148 if (index
!= text_
.length())
149 U16_SET_CP_LIMIT(text_
.data(), 0, index
, text_
.length());
153 base::string16
ElideFilename(const base::FilePath
& filename
,
154 const FontList
& font_list
,
155 float available_pixel_width
) {
157 base::string16 filename_utf16
= filename
.value();
158 base::string16 extension
= filename
.Extension();
159 base::string16 rootname
= filename
.BaseName().RemoveExtension().value();
160 #elif defined(OS_POSIX)
161 base::string16 filename_utf16
= WideToUTF16(base::SysNativeMBToWide(
163 base::string16 extension
= WideToUTF16(base::SysNativeMBToWide(
164 filename
.Extension()));
165 base::string16 rootname
= WideToUTF16(base::SysNativeMBToWide(
166 filename
.BaseName().RemoveExtension().value()));
169 const float full_width
= GetStringWidthF(filename_utf16
, font_list
);
170 if (full_width
<= available_pixel_width
)
171 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16
);
173 if (rootname
.empty() || extension
.empty()) {
174 const base::string16 elided_name
=
175 ElideText(filename_utf16
, font_list
, available_pixel_width
, ELIDE_TAIL
);
176 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
179 const float ext_width
= GetStringWidthF(extension
, font_list
);
180 const float root_width
= GetStringWidthF(rootname
, font_list
);
182 // We may have trimmed the path.
183 if (root_width
+ ext_width
<= available_pixel_width
) {
184 const base::string16 elided_name
= rootname
+ extension
;
185 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
188 if (ext_width
>= available_pixel_width
) {
189 const base::string16 elided_name
= ElideText(
190 rootname
+ extension
, font_list
, available_pixel_width
, ELIDE_MIDDLE
);
191 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
194 float available_root_width
= available_pixel_width
- ext_width
;
195 base::string16 elided_name
=
196 ElideText(rootname
, font_list
, available_root_width
, ELIDE_TAIL
);
197 elided_name
+= extension
;
198 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
201 base::string16
ElideText(const base::string16
& text
,
202 const FontList
& font_list
,
203 float available_pixel_width
,
204 ElideBehavior behavior
) {
205 #if !defined(OS_ANDROID) && !defined(OS_IOS)
206 DCHECK_NE(behavior
, FADE_TAIL
);
207 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
208 render_text
->SetCursorEnabled(false);
209 // Do not bother accurately sizing strings over 5000 characters here, for
210 // performance purposes. This matches the behavior of Canvas::SizeStringFloat.
211 render_text
->set_truncate_length(5000);
212 render_text
->SetFontList(font_list
);
213 available_pixel_width
= std::ceil(available_pixel_width
);
214 render_text
->SetDisplayRect(gfx::Rect(gfx::Size(available_pixel_width
, 1)));
215 render_text
->SetElideBehavior(behavior
);
216 render_text
->SetText(text
);
217 return render_text
->layout_text();
219 DCHECK_NE(behavior
, FADE_TAIL
);
220 if (text
.empty() || behavior
== FADE_TAIL
|| behavior
== NO_ELIDE
||
221 GetStringWidthF(text
, font_list
) <= available_pixel_width
) {
224 if (behavior
== ELIDE_EMAIL
)
225 return ElideEmail(text
, font_list
, available_pixel_width
);
227 const bool elide_in_middle
= (behavior
== ELIDE_MIDDLE
);
228 const bool elide_at_beginning
= (behavior
== ELIDE_HEAD
);
229 const bool insert_ellipsis
= (behavior
!= TRUNCATE
);
230 const base::string16 ellipsis
= base::string16(kEllipsisUTF16
);
231 StringSlicer
slicer(text
, ellipsis
, elide_in_middle
, elide_at_beginning
);
233 if (insert_ellipsis
&&
234 GetStringWidthF(ellipsis
, font_list
) > available_pixel_width
)
235 return base::string16();
237 // Use binary search to compute the elided text.
239 size_t hi
= text
.length() - 1;
241 for (guess
= (lo
+ hi
) / 2; lo
<= hi
; guess
= (lo
+ hi
) / 2) {
242 // We check the width of the whole desired string at once to ensure we
243 // handle kerning/ligatures/etc. correctly.
244 // TODO(skanuj) : Handle directionality of ellipsis based on adjacent
245 // characters. See crbug.com/327963.
246 const base::string16 cut
= slicer
.CutString(guess
, insert_ellipsis
);
247 const float guess_width
= GetStringWidthF(cut
, font_list
);
248 if (guess_width
== available_pixel_width
)
250 if (guess_width
> available_pixel_width
) {
252 // Move back on the loop terminating condition when the guess is too wide.
260 return slicer
.CutString(guess
, insert_ellipsis
);
264 bool ElideString(const base::string16
& input
,
266 base::string16
* output
) {
267 DCHECK_GE(max_len
, 0);
268 if (static_cast<int>(input
.length()) <= max_len
) {
269 output
->assign(input
);
278 output
->assign(input
.substr(0, 1));
281 output
->assign(input
.substr(0, 2));
284 output
->assign(input
.substr(0, 1) + ASCIIToUTF16(".") +
285 input
.substr(input
.length() - 1));
288 output
->assign(input
.substr(0, 1) + ASCIIToUTF16("..") +
289 input
.substr(input
.length() - 1));
292 int rstr_len
= (max_len
- 3) / 2;
293 int lstr_len
= rstr_len
+ ((max_len
- 3) % 2);
294 output
->assign(input
.substr(0, lstr_len
) + ASCIIToUTF16("...") +
295 input
.substr(input
.length() - rstr_len
));
305 // Internal class used to track progress of a rectangular string elide
306 // operation. Exists so the top-level ElideRectangleString() function
307 // can be broken into smaller methods sharing this state.
308 class RectangleString
{
310 RectangleString(size_t max_rows
, size_t max_cols
,
311 bool strict
, base::string16
*output
)
312 : max_rows_(max_rows
),
320 // Perform deferred initializations following creation. Must be called
321 // before any input can be added via AddString().
322 void Init() { output_
->clear(); }
324 // Add an input string, reformatting to fit the desired dimensions.
325 // AddString() may be called multiple times to concatenate together
326 // multiple strings into the region (the current caller doesn't do
328 void AddString(const base::string16
& input
);
330 // Perform any deferred output processing. Must be called after the
331 // last AddString() call has occurred.
335 // Add a line to the rectangular region at the current position,
336 // either by itself or by breaking it into words.
337 void AddLine(const base::string16
& line
);
339 // Add a word to the rectangular region at the current position,
340 // either by itself or by breaking it into characters.
341 void AddWord(const base::string16
& word
);
343 // Add text to the output string if the rectangular boundaries
344 // have not been exceeded, advancing the current position.
345 void Append(const base::string16
& string
);
347 // Set the current position to the beginning of the next line. If
348 // |output| is true, add a newline to the output string if the rectangular
349 // boundaries have not been exceeded. If |output| is false, we assume
350 // some other mechanism will (likely) do similar breaking after the fact.
351 void NewLine(bool output
);
353 // Maximum number of rows allowed in the output string.
356 // Maximum number of characters allowed in the output string.
359 // Current row position, always incremented and may exceed max_rows_
360 // when the input can not fit in the region. We stop appending to
361 // the output string, however, when this condition occurs. In the
362 // future, we may want to expose this value to allow the caller to
363 // determine how many rows would actually be required to hold the
367 // Current character position, should never exceed max_cols_.
370 // True when we do whitespace to newline conversions ourselves.
373 // True when some of the input has been truncated.
376 // String onto which the output is accumulated.
377 base::string16
* output_
;
379 DISALLOW_COPY_AND_ASSIGN(RectangleString
);
382 void RectangleString::AddString(const base::string16
& input
) {
383 base::i18n::BreakIterator
lines(input
,
384 base::i18n::BreakIterator::BREAK_NEWLINE
);
386 while (lines
.Advance())
387 AddLine(lines
.GetString());
389 NOTREACHED() << "BreakIterator (lines) init failed";
393 bool RectangleString::Finalize() {
395 output_
->append(ASCIIToUTF16("..."));
401 void RectangleString::AddLine(const base::string16
& line
) {
402 if (line
.length() < max_cols_
) {
405 base::i18n::BreakIterator
words(line
,
406 base::i18n::BreakIterator::BREAK_SPACE
);
408 while (words
.Advance())
409 AddWord(words
.GetString());
411 NOTREACHED() << "BreakIterator (words) init failed";
414 // Account for naturally-occuring newlines.
419 void RectangleString::AddWord(const base::string16
& word
) {
420 if (word
.length() < max_cols_
) {
421 // Word can be made to fit, no need to fragment it.
422 if (current_col_
+ word
.length() >= max_cols_
)
426 // Word is so big that it must be fragmented.
429 base::i18n::UTF16CharIterator
chars(&word
);
430 while (!chars
.end()) {
431 // When boundary is hit, add as much as will fit on this line.
432 if (current_col_
+ (chars
.char_pos() - char_start
) >= max_cols_
) {
433 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
435 array_start
= chars
.array_pos();
436 char_start
= chars
.char_pos();
440 // Add the last remaining fragment, if any.
441 if (array_start
!= chars
.array_pos())
442 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
446 void RectangleString::Append(const base::string16
& string
) {
447 if (current_row_
< max_rows_
)
448 output_
->append(string
);
451 current_col_
+= string
.length();
454 void RectangleString::NewLine(bool output
) {
455 if (current_row_
< max_rows_
) {
457 output_
->append(ASCIIToUTF16("\n"));
465 // Internal class used to track progress of a rectangular text elide
466 // operation. Exists so the top-level ElideRectangleText() function
467 // can be broken into smaller methods sharing this state.
468 class RectangleText
{
470 RectangleText(const FontList
& font_list
,
471 float available_pixel_width
,
472 int available_pixel_height
,
473 WordWrapBehavior wrap_behavior
,
474 std::vector
<base::string16
>* lines
)
475 : font_list_(font_list
),
476 line_height_(font_list
.GetHeight()),
477 available_pixel_width_(available_pixel_width
),
478 available_pixel_height_(available_pixel_height
),
479 wrap_behavior_(wrap_behavior
),
482 last_line_ended_in_lf_(false),
484 insufficient_width_(false),
485 insufficient_height_(false) {}
487 // Perform deferred initializions following creation. Must be called
488 // before any input can be added via AddString().
489 void Init() { lines_
->clear(); }
491 // Add an input string, reformatting to fit the desired dimensions.
492 // AddString() may be called multiple times to concatenate together
493 // multiple strings into the region (the current caller doesn't do
495 void AddString(const base::string16
& input
);
497 // Perform any deferred output processing. Must be called after the last
498 // AddString() call has occured. Returns a combination of
499 // |ReformattingResultFlags| indicating whether the given width or height was
500 // insufficient, leading to elision or truncation.
504 // Add a line to the rectangular region at the current position,
505 // either by itself or by breaking it into words.
506 void AddLine(const base::string16
& line
);
508 // Wrap the specified word across multiple lines.
509 int WrapWord(const base::string16
& word
);
511 // Add a long word - wrapping, eliding or truncating per the wrap behavior.
512 int AddWordOverflow(const base::string16
& word
);
514 // Add a word to the rectangluar region at the current position.
515 int AddWord(const base::string16
& word
);
517 // Append the specified |text| to the current output line, incrementing the
518 // running width by the specified amount. This is an optimization over
519 // |AddToCurrentLine()| when |text_width| is already known.
520 void AddToCurrentLineWithWidth(const base::string16
& text
, float text_width
);
522 // Append the specified |text| to the current output line.
523 void AddToCurrentLine(const base::string16
& text
);
525 // Set the current position to the beginning of the next line.
528 // The font list used for measuring text width.
529 const FontList
& font_list_
;
531 // The height of each line of text.
532 const int line_height_
;
534 // The number of pixels of available width in the rectangle.
535 const float available_pixel_width_
;
537 // The number of pixels of available height in the rectangle.
538 const int available_pixel_height_
;
540 // The wrap behavior for words that are too long to fit on a single line.
541 const WordWrapBehavior wrap_behavior_
;
543 // The current running width.
544 float current_width_
;
546 // The current running height.
549 // The current line of text.
550 base::string16 current_line_
;
552 // Indicates whether the last line ended with \n.
553 bool last_line_ended_in_lf_
;
555 // The output vector of lines.
556 std::vector
<base::string16
>* lines_
;
558 // Indicates whether a word was so long that it had to be truncated or elided
559 // to fit the available width.
560 bool insufficient_width_
;
562 // Indicates whether there were too many lines for the available height.
563 bool insufficient_height_
;
565 DISALLOW_COPY_AND_ASSIGN(RectangleText
);
568 void RectangleText::AddString(const base::string16
& input
) {
569 base::i18n::BreakIterator
lines(input
,
570 base::i18n::BreakIterator::BREAK_NEWLINE
);
572 while (!insufficient_height_
&& lines
.Advance()) {
573 base::string16 line
= lines
.GetString();
574 // The BREAK_NEWLINE iterator will keep the trailing newline character,
575 // except in the case of the last line, which may not have one. Remove
576 // the newline character, if it exists.
577 last_line_ended_in_lf_
= !line
.empty() && line
[line
.length() - 1] == '\n';
578 if (last_line_ended_in_lf_
)
579 line
.resize(line
.length() - 1);
583 NOTREACHED() << "BreakIterator (lines) init failed";
587 int RectangleText::Finalize() {
588 // Remove trailing whitespace from the last line or remove the last line
589 // completely, if it's just whitespace.
590 if (!insufficient_height_
&& !lines_
->empty()) {
591 base::TrimWhitespace(lines_
->back(), base::TRIM_TRAILING
, &lines_
->back());
592 if (lines_
->back().empty() && !last_line_ended_in_lf_
)
595 if (last_line_ended_in_lf_
)
596 lines_
->push_back(base::string16());
597 return (insufficient_width_
? INSUFFICIENT_SPACE_HORIZONTAL
: 0) |
598 (insufficient_height_
? INSUFFICIENT_SPACE_VERTICAL
: 0);
601 void RectangleText::AddLine(const base::string16
& line
) {
602 const float line_width
= GetStringWidthF(line
, font_list_
);
603 if (line_width
<= available_pixel_width_
) {
604 AddToCurrentLineWithWidth(line
, line_width
);
606 // Iterate over positions that are valid to break the line at. In general,
607 // these are word boundaries but after any punctuation following the word.
608 base::i18n::BreakIterator
words(line
,
609 base::i18n::BreakIterator::BREAK_LINE
);
611 while (words
.Advance()) {
612 const bool truncate
= !current_line_
.empty();
613 const base::string16
& word
= words
.GetString();
614 const int lines_added
= AddWord(word
);
617 // Trim trailing whitespace from the line that was added.
618 const int line
= lines_
->size() - lines_added
;
619 base::TrimWhitespace(lines_
->at(line
), base::TRIM_TRAILING
,
622 if (base::ContainsOnlyChars(word
, base::kWhitespaceUTF16
)) {
623 // Skip the first space if the previous line was carried over.
625 current_line_
.clear();
630 NOTREACHED() << "BreakIterator (words) init failed";
633 // Account for naturally-occuring newlines.
637 int RectangleText::WrapWord(const base::string16
& word
) {
638 // Word is so wide that it must be fragmented.
639 base::string16 text
= word
;
641 bool first_fragment
= true;
642 while (!insufficient_height_
&& !text
.empty()) {
643 base::string16 fragment
=
644 ElideText(text
, font_list_
, available_pixel_width_
, TRUNCATE
);
645 // At least one character has to be added at every line, even if the
646 // available space is too small.
647 if (fragment
.empty())
648 fragment
= text
.substr(0, 1);
649 if (!first_fragment
&& NewLine())
651 AddToCurrentLine(fragment
);
652 text
= text
.substr(fragment
.length());
653 first_fragment
= false;
658 int RectangleText::AddWordOverflow(const base::string16
& word
) {
661 // Unless this is the very first word, put it on a new line.
662 if (!current_line_
.empty()) {
668 if (wrap_behavior_
== IGNORE_LONG_WORDS
) {
669 current_line_
= word
;
670 current_width_
= available_pixel_width_
;
671 } else if (wrap_behavior_
== WRAP_LONG_WORDS
) {
672 lines_added
+= WrapWord(word
);
674 const ElideBehavior elide_behavior
=
675 (wrap_behavior_
== ELIDE_LONG_WORDS
? ELIDE_TAIL
: TRUNCATE
);
676 const base::string16 elided_word
=
677 ElideText(word
, font_list_
, available_pixel_width_
, elide_behavior
);
678 AddToCurrentLine(elided_word
);
679 insufficient_width_
= true;
685 int RectangleText::AddWord(const base::string16
& word
) {
687 base::string16 trimmed
;
688 base::TrimWhitespace(word
, base::TRIM_TRAILING
, &trimmed
);
689 const float trimmed_width
= GetStringWidthF(trimmed
, font_list_
);
690 if (trimmed_width
<= available_pixel_width_
) {
691 // Word can be made to fit, no need to fragment it.
692 if ((current_width_
+ trimmed_width
> available_pixel_width_
) && NewLine())
694 // Append the non-trimmed word, in case more words are added after.
695 AddToCurrentLine(word
);
697 lines_added
= AddWordOverflow(wrap_behavior_
== IGNORE_LONG_WORDS
?
703 void RectangleText::AddToCurrentLine(const base::string16
& text
) {
704 AddToCurrentLineWithWidth(text
, GetStringWidthF(text
, font_list_
));
707 void RectangleText::AddToCurrentLineWithWidth(const base::string16
& text
,
709 if (current_height_
>= available_pixel_height_
) {
710 insufficient_height_
= true;
713 current_line_
.append(text
);
714 current_width_
+= text_width
;
717 bool RectangleText::NewLine() {
718 bool line_added
= false;
719 if (current_height_
< available_pixel_height_
) {
720 lines_
->push_back(current_line_
);
721 current_line_
.clear();
724 insufficient_height_
= true;
726 current_height_
+= line_height_
;
733 bool ElideRectangleString(const base::string16
& input
, size_t max_rows
,
734 size_t max_cols
, bool strict
,
735 base::string16
* output
) {
736 RectangleString
rect(max_rows
, max_cols
, strict
, output
);
738 rect
.AddString(input
);
739 return rect
.Finalize();
742 int ElideRectangleText(const base::string16
& input
,
743 const FontList
& font_list
,
744 float available_pixel_width
,
745 int available_pixel_height
,
746 WordWrapBehavior wrap_behavior
,
747 std::vector
<base::string16
>* lines
) {
748 RectangleText
rect(font_list
,
749 available_pixel_width
,
750 available_pixel_height
,
754 rect
.AddString(input
);
755 return rect
.Finalize();
758 base::string16
TruncateString(const base::string16
& string
,
760 BreakType break_type
) {
761 DCHECK(break_type
== CHARACTER_BREAK
|| break_type
== WORD_BREAK
);
763 if (string
.size() <= length
)
764 // String fits, return it.
768 // No room for the elide string, return an empty string.
769 return base::string16();
771 size_t max
= length
- 1;
773 // Added to the end of strings that are too big.
774 static const base::char16 kElideString
[] = { 0x2026, 0 };
777 // Just enough room for the elide string.
780 int32_t index
= static_cast<int32_t>(max
);
781 if (break_type
== WORD_BREAK
) {
782 // Use a line iterator to find the first boundary.
783 UErrorCode status
= U_ZERO_ERROR
;
784 scoped_ptr
<icu::BreakIterator
> bi(
785 icu::RuleBasedBreakIterator::createLineInstance(
786 icu::Locale::getDefault(), status
));
787 if (U_FAILURE(status
))
788 return string
.substr(0, max
) + kElideString
;
789 bi
->setText(string
.c_str());
790 index
= bi
->preceding(index
);
791 if (index
== icu::BreakIterator::DONE
|| index
== 0) {
792 // We either found no valid line break at all, or one right at the
793 // beginning of the string. Go back to the end; we'll have to break in the
795 index
= static_cast<int32_t>(max
);
799 // Use a character iterator to find the previous non-whitespace character.
800 icu::StringCharacterIterator
char_iterator(string
.c_str());
801 char_iterator
.setIndex(index
);
802 while (char_iterator
.hasPrevious()) {
803 char_iterator
.previous();
804 if (!(u_isspace(char_iterator
.current()) ||
805 u_charType(char_iterator
.current()) == U_CONTROL_CHAR
||
806 u_charType(char_iterator
.current()) == U_NON_SPACING_MARK
)) {
807 // Not a whitespace character. Advance the iterator so that we
808 // include the current character in the truncated string.
809 char_iterator
.next();
813 if (char_iterator
.hasPrevious()) {
814 // Found a valid break point.
815 index
= char_iterator
.getIndex();
817 // String has leading whitespace, return the elide string.
821 return string
.substr(0, index
) + kElideString
;