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/text_utils.h"
29 using base::ASCIIToUTF16
;
30 using base::UTF8ToUTF16
;
31 using base::WideToUTF16
;
37 // Elides a well-formed email address (e.g. username@domain.com) to fit into
38 // |available_pixel_width| using the specified |font_list|.
39 // This function guarantees that the string returned will contain at least one
40 // character, other than the ellipses, on either side of the '@'. If it is
41 // impossible to achieve these requirements: only an ellipsis will be returned.
42 // If possible: this elides only the username portion of the |email|. Otherwise,
43 // the domain is elided in the middle so that it splits the available width
44 // equally with the elided username (should the username be short enough that it
45 // doesn't need half the available width: the elided domain will occupy that
47 base::string16
ElideEmail(const base::string16
& email
,
48 const FontList
& font_list
,
49 float available_pixel_width
) {
50 if (GetStringWidthF(email
, font_list
) <= available_pixel_width
)
53 // Split the email into its local-part (username) and domain-part. The email
54 // spec technically allows for @ symbols in the local-part (username) of the
55 // email under some special requirements. It is guaranteed that there is no @
56 // symbol in the domain part of the email however so splitting at the last @
58 const size_t split_index
= email
.find_last_of('@');
59 DCHECK_NE(split_index
, base::string16::npos
);
60 base::string16 username
= email
.substr(0, split_index
);
61 base::string16 domain
= email
.substr(split_index
+ 1);
62 DCHECK(!username
.empty());
63 DCHECK(!domain
.empty());
65 // Subtract the @ symbol from the available width as it is mandatory.
66 const base::string16 kAtSignUTF16
= ASCIIToUTF16("@");
67 available_pixel_width
-= GetStringWidthF(kAtSignUTF16
, font_list
);
69 // Check whether eliding the domain is necessary: if eliding the username
70 // is sufficient, the domain will not be elided.
71 const float full_username_width
= GetStringWidthF(username
, font_list
);
72 const float available_domain_width
=
73 available_pixel_width
-
74 std::min(full_username_width
,
75 GetStringWidthF(username
.substr(0, 1) + kEllipsisUTF16
,
77 if (GetStringWidthF(domain
, font_list
) > available_domain_width
) {
78 // Elide the domain so that it only takes half of the available width.
79 // Should the username not need all the width available in its half, the
80 // domain will occupy the leftover width.
81 // If |desired_domain_width| is greater than |available_domain_width|: the
82 // minimal username elision allowed by the specifications will not fit; thus
83 // |desired_domain_width| must be <= |available_domain_width| at all cost.
84 const float desired_domain_width
=
85 std::min(available_domain_width
,
86 std::max(available_pixel_width
- full_username_width
,
87 available_pixel_width
/ 2));
88 domain
= ElideText(domain
, font_list
, desired_domain_width
, ELIDE_MIDDLE
);
89 // Failing to elide the domain such that at least one character remains
90 // (other than the ellipsis itself) remains: return a single ellipsis.
91 if (domain
.length() <= 1U)
92 return base::string16(kEllipsisUTF16
);
95 // Fit the username in the remaining width (at this point the elided username
96 // is guaranteed to fit with at least one character remaining given all the
97 // precautions taken earlier).
98 available_pixel_width
-= GetStringWidthF(domain
, font_list
);
99 username
= ElideText(username
, font_list
, available_pixel_width
, ELIDE_TAIL
);
100 return username
+ kAtSignUTF16
+ domain
;
106 const char kEllipsis
[] = "\xE2\x80\xA6";
107 const base::char16 kEllipsisUTF16
[] = { 0x2026, 0 };
108 const base::char16 kForwardSlash
= '/';
110 StringSlicer::StringSlicer(const base::string16
& text
,
111 const base::string16
& ellipsis
,
112 bool elide_in_middle
,
113 bool elide_at_beginning
)
116 elide_in_middle_(elide_in_middle
),
117 elide_at_beginning_(elide_at_beginning
) {
120 base::string16
StringSlicer::CutString(size_t length
, bool insert_ellipsis
) {
121 const base::string16 ellipsis_text
= insert_ellipsis
? ellipsis_
124 if (elide_at_beginning_
)
125 return ellipsis_text
+
126 text_
.substr(FindValidBoundaryBefore(text_
.length() - length
));
128 if (!elide_in_middle_
)
129 return text_
.substr(0, FindValidBoundaryBefore(length
)) + ellipsis_text
;
131 // We put the extra character, if any, before the cut.
132 const size_t half_length
= length
/ 2;
133 const size_t prefix_length
= FindValidBoundaryBefore(length
- half_length
);
134 const size_t suffix_start_guess
= text_
.length() - half_length
;
135 const size_t suffix_start
= FindValidBoundaryAfter(suffix_start_guess
);
136 const size_t suffix_length
=
137 half_length
- (suffix_start_guess
- suffix_start
);
138 return text_
.substr(0, prefix_length
) + ellipsis_text
+
139 text_
.substr(suffix_start
, suffix_length
);
142 size_t StringSlicer::FindValidBoundaryBefore(size_t index
) const {
143 DCHECK_LE(index
, text_
.length());
144 if (index
!= text_
.length())
145 U16_SET_CP_START(text_
.data(), 0, index
);
149 size_t StringSlicer::FindValidBoundaryAfter(size_t index
) const {
150 DCHECK_LE(index
, text_
.length());
151 if (index
!= text_
.length())
152 U16_SET_CP_LIMIT(text_
.data(), 0, index
, text_
.length());
156 base::string16
ElideFilename(const base::FilePath
& filename
,
157 const FontList
& font_list
,
158 float available_pixel_width
) {
160 base::string16 filename_utf16
= filename
.value();
161 base::string16 extension
= filename
.Extension();
162 base::string16 rootname
= filename
.BaseName().RemoveExtension().value();
163 #elif defined(OS_POSIX)
164 base::string16 filename_utf16
= WideToUTF16(base::SysNativeMBToWide(
166 base::string16 extension
= WideToUTF16(base::SysNativeMBToWide(
167 filename
.Extension()));
168 base::string16 rootname
= WideToUTF16(base::SysNativeMBToWide(
169 filename
.BaseName().RemoveExtension().value()));
172 const float full_width
= GetStringWidthF(filename_utf16
, font_list
);
173 if (full_width
<= available_pixel_width
)
174 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16
);
176 if (rootname
.empty() || extension
.empty()) {
177 const base::string16 elided_name
=
178 ElideText(filename_utf16
, font_list
, available_pixel_width
, ELIDE_TAIL
);
179 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
182 const float ext_width
= GetStringWidthF(extension
, font_list
);
183 const float root_width
= GetStringWidthF(rootname
, font_list
);
185 // We may have trimmed the path.
186 if (root_width
+ ext_width
<= available_pixel_width
) {
187 const base::string16 elided_name
= rootname
+ extension
;
188 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
191 if (ext_width
>= available_pixel_width
) {
192 const base::string16 elided_name
= ElideText(
193 rootname
+ extension
, font_list
, available_pixel_width
, ELIDE_MIDDLE
);
194 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
197 float available_root_width
= available_pixel_width
- ext_width
;
198 base::string16 elided_name
=
199 ElideText(rootname
, font_list
, available_root_width
, ELIDE_TAIL
);
200 elided_name
+= extension
;
201 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
204 base::string16
ElideText(const base::string16
& text
,
205 const FontList
& font_list
,
206 float available_pixel_width
,
207 ElideBehavior behavior
) {
208 DCHECK_NE(behavior
, FADE_TAIL
);
209 if (text
.empty() || behavior
== FADE_TAIL
)
211 if (behavior
== ELIDE_EMAIL
)
212 return ElideEmail(text
, font_list
, available_pixel_width
);
214 const float current_text_pixel_width
= GetStringWidthF(text
, font_list
);
215 const bool elide_in_middle
= (behavior
== ELIDE_MIDDLE
);
216 const bool elide_at_beginning
= (behavior
== ELIDE_HEAD
);
217 const bool insert_ellipsis
= (behavior
!= TRUNCATE
);
218 const base::string16 ellipsis
= base::string16(kEllipsisUTF16
);
219 StringSlicer
slicer(text
, ellipsis
, elide_in_middle
, elide_at_beginning
);
221 // Pango will return 0 width for absurdly long strings. Cut the string in
222 // half and try again.
223 // This is caused by an int overflow in Pango (specifically, in
224 // pango_glyph_string_extents_range). It's actually more subtle than just
225 // returning 0, since on super absurdly long strings, the int can wrap and
226 // return positive numbers again. Detecting that is probably not worth it
227 // (eliding way too much from a ridiculous string is probably still
228 // ridiculous), but we should check other widths for bogus values as well.
229 if (current_text_pixel_width
<= 0) {
230 const base::string16 cut
=
231 slicer
.CutString(text
.length() / 2, insert_ellipsis
);
232 return ElideText(cut
, font_list
, available_pixel_width
, behavior
);
235 if (current_text_pixel_width
<= available_pixel_width
)
238 if (insert_ellipsis
&&
239 GetStringWidthF(ellipsis
, font_list
) > available_pixel_width
)
240 return base::string16();
242 // Use binary search to compute the elided text.
244 size_t hi
= text
.length() - 1;
246 for (guess
= (lo
+ hi
) / 2; lo
<= hi
; guess
= (lo
+ hi
) / 2) {
247 // We check the width of the whole desired string at once to ensure we
248 // handle kerning/ligatures/etc. correctly.
249 // TODO(skanuj) : Handle directionality of ellipsis based on adjacent
250 // characters. See crbug.com/327963.
251 const base::string16 cut
= slicer
.CutString(guess
, insert_ellipsis
);
252 const float guess_width
= GetStringWidthF(cut
, font_list
);
253 if (guess_width
== available_pixel_width
)
255 if (guess_width
> available_pixel_width
) {
257 // Move back if we are on loop terminating condition, and guess is wider
266 return slicer
.CutString(guess
, insert_ellipsis
);
269 bool ElideString(const base::string16
& input
,
271 base::string16
* output
) {
272 DCHECK_GE(max_len
, 0);
273 if (static_cast<int>(input
.length()) <= max_len
) {
274 output
->assign(input
);
283 output
->assign(input
.substr(0, 1));
286 output
->assign(input
.substr(0, 2));
289 output
->assign(input
.substr(0, 1) + ASCIIToUTF16(".") +
290 input
.substr(input
.length() - 1));
293 output
->assign(input
.substr(0, 1) + ASCIIToUTF16("..") +
294 input
.substr(input
.length() - 1));
297 int rstr_len
= (max_len
- 3) / 2;
298 int lstr_len
= rstr_len
+ ((max_len
- 3) % 2);
299 output
->assign(input
.substr(0, lstr_len
) + ASCIIToUTF16("...") +
300 input
.substr(input
.length() - rstr_len
));
310 // Internal class used to track progress of a rectangular string elide
311 // operation. Exists so the top-level ElideRectangleString() function
312 // can be broken into smaller methods sharing this state.
313 class RectangleString
{
315 RectangleString(size_t max_rows
, size_t max_cols
,
316 bool strict
, base::string16
*output
)
317 : max_rows_(max_rows
),
325 // Perform deferred initializations following creation. Must be called
326 // before any input can be added via AddString().
327 void Init() { output_
->clear(); }
329 // Add an input string, reformatting to fit the desired dimensions.
330 // AddString() may be called multiple times to concatenate together
331 // multiple strings into the region (the current caller doesn't do
333 void AddString(const base::string16
& input
);
335 // Perform any deferred output processing. Must be called after the
336 // last AddString() call has occurred.
340 // Add a line to the rectangular region at the current position,
341 // either by itself or by breaking it into words.
342 void AddLine(const base::string16
& line
);
344 // Add a word to the rectangular region at the current position,
345 // either by itself or by breaking it into characters.
346 void AddWord(const base::string16
& word
);
348 // Add text to the output string if the rectangular boundaries
349 // have not been exceeded, advancing the current position.
350 void Append(const base::string16
& string
);
352 // Set the current position to the beginning of the next line. If
353 // |output| is true, add a newline to the output string if the rectangular
354 // boundaries have not been exceeded. If |output| is false, we assume
355 // some other mechanism will (likely) do similar breaking after the fact.
356 void NewLine(bool output
);
358 // Maximum number of rows allowed in the output string.
361 // Maximum number of characters allowed in the output string.
364 // Current row position, always incremented and may exceed max_rows_
365 // when the input can not fit in the region. We stop appending to
366 // the output string, however, when this condition occurs. In the
367 // future, we may want to expose this value to allow the caller to
368 // determine how many rows would actually be required to hold the
372 // Current character position, should never exceed max_cols_.
375 // True when we do whitespace to newline conversions ourselves.
378 // True when some of the input has been truncated.
381 // String onto which the output is accumulated.
382 base::string16
* output_
;
384 DISALLOW_COPY_AND_ASSIGN(RectangleString
);
387 void RectangleString::AddString(const base::string16
& input
) {
388 base::i18n::BreakIterator
lines(input
,
389 base::i18n::BreakIterator::BREAK_NEWLINE
);
391 while (lines
.Advance())
392 AddLine(lines
.GetString());
394 NOTREACHED() << "BreakIterator (lines) init failed";
398 bool RectangleString::Finalize() {
400 output_
->append(ASCIIToUTF16("..."));
406 void RectangleString::AddLine(const base::string16
& line
) {
407 if (line
.length() < max_cols_
) {
410 base::i18n::BreakIterator
words(line
,
411 base::i18n::BreakIterator::BREAK_SPACE
);
413 while (words
.Advance())
414 AddWord(words
.GetString());
416 NOTREACHED() << "BreakIterator (words) init failed";
419 // Account for naturally-occuring newlines.
424 void RectangleString::AddWord(const base::string16
& word
) {
425 if (word
.length() < max_cols_
) {
426 // Word can be made to fit, no need to fragment it.
427 if (current_col_
+ word
.length() >= max_cols_
)
431 // Word is so big that it must be fragmented.
434 base::i18n::UTF16CharIterator
chars(&word
);
435 while (!chars
.end()) {
436 // When boundary is hit, add as much as will fit on this line.
437 if (current_col_
+ (chars
.char_pos() - char_start
) >= max_cols_
) {
438 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
440 array_start
= chars
.array_pos();
441 char_start
= chars
.char_pos();
445 // Add the last remaining fragment, if any.
446 if (array_start
!= chars
.array_pos())
447 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
451 void RectangleString::Append(const base::string16
& string
) {
452 if (current_row_
< max_rows_
)
453 output_
->append(string
);
456 current_col_
+= string
.length();
459 void RectangleString::NewLine(bool output
) {
460 if (current_row_
< max_rows_
) {
462 output_
->append(ASCIIToUTF16("\n"));
470 // Internal class used to track progress of a rectangular text elide
471 // operation. Exists so the top-level ElideRectangleText() function
472 // can be broken into smaller methods sharing this state.
473 class RectangleText
{
475 RectangleText(const FontList
& font_list
,
476 float available_pixel_width
,
477 int available_pixel_height
,
478 WordWrapBehavior wrap_behavior
,
479 std::vector
<base::string16
>* lines
)
480 : font_list_(font_list
),
481 line_height_(font_list
.GetHeight()),
482 available_pixel_width_(available_pixel_width
),
483 available_pixel_height_(available_pixel_height
),
484 wrap_behavior_(wrap_behavior
),
487 last_line_ended_in_lf_(false),
489 insufficient_width_(false),
490 insufficient_height_(false) {}
492 // Perform deferred initializions following creation. Must be called
493 // before any input can be added via AddString().
494 void Init() { lines_
->clear(); }
496 // Add an input string, reformatting to fit the desired dimensions.
497 // AddString() may be called multiple times to concatenate together
498 // multiple strings into the region (the current caller doesn't do
500 void AddString(const base::string16
& input
);
502 // Perform any deferred output processing. Must be called after the last
503 // AddString() call has occured. Returns a combination of
504 // |ReformattingResultFlags| indicating whether the given width or height was
505 // insufficient, leading to elision or truncation.
509 // Add a line to the rectangular region at the current position,
510 // either by itself or by breaking it into words.
511 void AddLine(const base::string16
& line
);
513 // Wrap the specified word across multiple lines.
514 int WrapWord(const base::string16
& word
);
516 // Add a long word - wrapping, eliding or truncating per the wrap behavior.
517 int AddWordOverflow(const base::string16
& word
);
519 // Add a word to the rectangluar region at the current position.
520 int AddWord(const base::string16
& word
);
522 // Append the specified |text| to the current output line, incrementing the
523 // running width by the specified amount. This is an optimization over
524 // |AddToCurrentLine()| when |text_width| is already known.
525 void AddToCurrentLineWithWidth(const base::string16
& text
, float text_width
);
527 // Append the specified |text| to the current output line.
528 void AddToCurrentLine(const base::string16
& text
);
530 // Set the current position to the beginning of the next line.
533 // The font list used for measuring text width.
534 const FontList
& font_list_
;
536 // The height of each line of text.
537 const int line_height_
;
539 // The number of pixels of available width in the rectangle.
540 const float available_pixel_width_
;
542 // The number of pixels of available height in the rectangle.
543 const int available_pixel_height_
;
545 // The wrap behavior for words that are too long to fit on a single line.
546 const WordWrapBehavior wrap_behavior_
;
548 // The current running width.
549 float current_width_
;
551 // The current running height.
554 // The current line of text.
555 base::string16 current_line_
;
557 // Indicates whether the last line ended with \n.
558 bool last_line_ended_in_lf_
;
560 // The output vector of lines.
561 std::vector
<base::string16
>* lines_
;
563 // Indicates whether a word was so long that it had to be truncated or elided
564 // to fit the available width.
565 bool insufficient_width_
;
567 // Indicates whether there were too many lines for the available height.
568 bool insufficient_height_
;
570 DISALLOW_COPY_AND_ASSIGN(RectangleText
);
573 void RectangleText::AddString(const base::string16
& input
) {
574 base::i18n::BreakIterator
lines(input
,
575 base::i18n::BreakIterator::BREAK_NEWLINE
);
577 while (!insufficient_height_
&& lines
.Advance()) {
578 base::string16 line
= lines
.GetString();
579 // The BREAK_NEWLINE iterator will keep the trailing newline character,
580 // except in the case of the last line, which may not have one. Remove
581 // the newline character, if it exists.
582 last_line_ended_in_lf_
= !line
.empty() && line
[line
.length() - 1] == '\n';
583 if (last_line_ended_in_lf_
)
584 line
.resize(line
.length() - 1);
588 NOTREACHED() << "BreakIterator (lines) init failed";
592 int RectangleText::Finalize() {
593 // Remove trailing whitespace from the last line or remove the last line
594 // completely, if it's just whitespace.
595 if (!insufficient_height_
&& !lines_
->empty()) {
596 base::TrimWhitespace(lines_
->back(), base::TRIM_TRAILING
, &lines_
->back());
597 if (lines_
->back().empty() && !last_line_ended_in_lf_
)
600 if (last_line_ended_in_lf_
)
601 lines_
->push_back(base::string16());
602 return (insufficient_width_
? INSUFFICIENT_SPACE_HORIZONTAL
: 0) |
603 (insufficient_height_
? INSUFFICIENT_SPACE_VERTICAL
: 0);
606 void RectangleText::AddLine(const base::string16
& line
) {
607 const float line_width
= GetStringWidthF(line
, font_list_
);
608 if (line_width
<= available_pixel_width_
) {
609 AddToCurrentLineWithWidth(line
, line_width
);
611 // Iterate over positions that are valid to break the line at. In general,
612 // these are word boundaries but after any punctuation following the word.
613 base::i18n::BreakIterator
words(line
,
614 base::i18n::BreakIterator::BREAK_LINE
);
616 while (words
.Advance()) {
617 const bool truncate
= !current_line_
.empty();
618 const base::string16
& word
= words
.GetString();
619 const int lines_added
= AddWord(word
);
622 // Trim trailing whitespace from the line that was added.
623 const int line
= lines_
->size() - lines_added
;
624 base::TrimWhitespace(lines_
->at(line
), base::TRIM_TRAILING
,
627 if (base::ContainsOnlyChars(word
, base::kWhitespaceUTF16
)) {
628 // Skip the first space if the previous line was carried over.
630 current_line_
.clear();
635 NOTREACHED() << "BreakIterator (words) init failed";
638 // Account for naturally-occuring newlines.
642 int RectangleText::WrapWord(const base::string16
& word
) {
643 // Word is so wide that it must be fragmented.
644 base::string16 text
= word
;
646 bool first_fragment
= true;
647 while (!insufficient_height_
&& !text
.empty()) {
648 base::string16 fragment
=
649 ElideText(text
, font_list_
, available_pixel_width_
, TRUNCATE
);
650 // At least one character has to be added at every line, even if the
651 // available space is too small.
652 if (fragment
.empty())
653 fragment
= text
.substr(0, 1);
654 if (!first_fragment
&& NewLine())
656 AddToCurrentLine(fragment
);
657 text
= text
.substr(fragment
.length());
658 first_fragment
= false;
663 int RectangleText::AddWordOverflow(const base::string16
& word
) {
666 // Unless this is the very first word, put it on a new line.
667 if (!current_line_
.empty()) {
673 if (wrap_behavior_
== IGNORE_LONG_WORDS
) {
674 current_line_
= word
;
675 current_width_
= available_pixel_width_
;
676 } else if (wrap_behavior_
== WRAP_LONG_WORDS
) {
677 lines_added
+= WrapWord(word
);
679 const ElideBehavior elide_behavior
=
680 (wrap_behavior_
== ELIDE_LONG_WORDS
? ELIDE_TAIL
: TRUNCATE
);
681 const base::string16 elided_word
=
682 ElideText(word
, font_list_
, available_pixel_width_
, elide_behavior
);
683 AddToCurrentLine(elided_word
);
684 insufficient_width_
= true;
690 int RectangleText::AddWord(const base::string16
& word
) {
692 base::string16 trimmed
;
693 base::TrimWhitespace(word
, base::TRIM_TRAILING
, &trimmed
);
694 const float trimmed_width
= GetStringWidthF(trimmed
, font_list_
);
695 if (trimmed_width
<= available_pixel_width_
) {
696 // Word can be made to fit, no need to fragment it.
697 if ((current_width_
+ trimmed_width
> available_pixel_width_
) && NewLine())
699 // Append the non-trimmed word, in case more words are added after.
700 AddToCurrentLine(word
);
702 lines_added
= AddWordOverflow(wrap_behavior_
== IGNORE_LONG_WORDS
?
708 void RectangleText::AddToCurrentLine(const base::string16
& text
) {
709 AddToCurrentLineWithWidth(text
, GetStringWidthF(text
, font_list_
));
712 void RectangleText::AddToCurrentLineWithWidth(const base::string16
& text
,
714 if (current_height_
>= available_pixel_height_
) {
715 insufficient_height_
= true;
718 current_line_
.append(text
);
719 current_width_
+= text_width
;
722 bool RectangleText::NewLine() {
723 bool line_added
= false;
724 if (current_height_
< available_pixel_height_
) {
725 lines_
->push_back(current_line_
);
726 current_line_
.clear();
729 insufficient_height_
= true;
731 current_height_
+= line_height_
;
738 bool ElideRectangleString(const base::string16
& input
, size_t max_rows
,
739 size_t max_cols
, bool strict
,
740 base::string16
* output
) {
741 RectangleString
rect(max_rows
, max_cols
, strict
, output
);
743 rect
.AddString(input
);
744 return rect
.Finalize();
747 int ElideRectangleText(const base::string16
& input
,
748 const FontList
& font_list
,
749 float available_pixel_width
,
750 int available_pixel_height
,
751 WordWrapBehavior wrap_behavior
,
752 std::vector
<base::string16
>* lines
) {
753 RectangleText
rect(font_list
,
754 available_pixel_width
,
755 available_pixel_height
,
759 rect
.AddString(input
);
760 return rect
.Finalize();
763 base::string16
TruncateString(const base::string16
& string
, size_t length
) {
764 if (string
.size() <= length
)
765 // String fits, return it.
769 // No room for the elide string, return an empty string.
770 return base::string16();
772 size_t max
= length
- 1;
774 // Added to the end of strings that are too big.
775 static const base::char16 kElideString
[] = { 0x2026, 0 };
778 // Just enough room for the elide string.
781 // Use a line iterator to find the first boundary.
782 UErrorCode status
= U_ZERO_ERROR
;
783 scoped_ptr
<icu::RuleBasedBreakIterator
> bi(
784 static_cast<icu::RuleBasedBreakIterator
*>(
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 int32_t index
= bi
->preceding(static_cast<int32_t>(max
));
791 if (index
== icu::BreakIterator::DONE
) {
792 index
= static_cast<int32_t>(max
);
794 // Found a valid break (may be the beginning of the string). Now use
795 // a character iterator to find the previous non-whitespace character.
796 icu::StringCharacterIterator
char_iterator(string
.c_str());
798 // No valid line breaks. Start at the end again. This ensures we break
799 // on a valid character boundary.
800 index
= static_cast<int32_t>(max
);
802 char_iterator
.setIndex(index
);
803 while (char_iterator
.hasPrevious()) {
804 char_iterator
.previous();
805 if (!(u_isspace(char_iterator
.current()) ||
806 u_charType(char_iterator
.current()) == U_CONTROL_CHAR
||
807 u_charType(char_iterator
.current()) == U_NON_SPACING_MARK
)) {
808 // Not a whitespace character. Advance the iterator so that we
809 // include the current character in the truncated string.
810 char_iterator
.next();
814 if (char_iterator
.hasPrevious()) {
815 // Found a valid break point.
816 index
= char_iterator
.getIndex();
818 // String has leading whitespace, return the elide string.
822 return string
.substr(0, index
) + kElideString
;