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"
17 #include "base/files/file_path.h"
18 #include "base/i18n/break_iterator.h"
19 #include "base/i18n/char_iterator.h"
20 #include "base/i18n/rtl.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/sys_string_conversions.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "third_party/icu/source/common/unicode/rbbi.h"
27 #include "third_party/icu/source/common/unicode/uloc.h"
28 #include "third_party/icu/source/common/unicode/umachine.h"
29 #include "ui/gfx/font_list.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/render_text.h"
32 #include "ui/gfx/text_utils.h"
34 using base::ASCIIToUTF16
;
35 using base::UTF8ToUTF16
;
36 using base::WideToUTF16
;
42 #if defined(OS_ANDROID) || defined(OS_IOS)
43 // The returned string will have at least one character besides the ellipsis
44 // on either side of '@'; if that's impossible, a single ellipsis is returned.
45 // If possible, only the username is elided. Otherwise, the domain is elided
46 // in the middle, splitting available width equally with the elided username.
47 // If the username is short enough that it doesn't need half the available
48 // width, the elided domain will occupy that extra width.
49 base::string16
ElideEmail(const base::string16
& email
,
50 const FontList
& font_list
,
51 float available_pixel_width
) {
52 if (GetStringWidthF(email
, font_list
) <= available_pixel_width
)
55 // Split the email into its local-part (username) and domain-part. The email
56 // spec allows for @ symbols in the username under some special requirements,
57 // but not in the domain part, so splitting at the last @ symbol is safe.
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
;
107 const char kEllipsis
[] = "\xE2\x80\xA6";
108 const base::char16 kEllipsisUTF16
[] = { 0x2026, 0 };
109 const base::char16 kForwardSlash
= '/';
111 StringSlicer::StringSlicer(const base::string16
& text
,
112 const base::string16
& ellipsis
,
113 bool elide_in_middle
,
114 bool elide_at_beginning
)
117 elide_in_middle_(elide_in_middle
),
118 elide_at_beginning_(elide_at_beginning
) {
121 base::string16
StringSlicer::CutString(size_t length
,
122 bool insert_ellipsis
) const {
123 const base::string16 ellipsis_text
= insert_ellipsis
? ellipsis_
126 if (elide_at_beginning_
)
127 return ellipsis_text
+
129 FindValidBoundaryBefore(text_
, text_
.length() - length
));
131 if (!elide_in_middle_
)
132 return text_
.substr(0, FindValidBoundaryBefore(text_
, length
)) +
135 // We put the extra character, if any, before the cut.
136 const size_t half_length
= length
/ 2;
137 const size_t prefix_length
=
138 FindValidBoundaryBefore(text_
, length
- half_length
);
139 const size_t suffix_start
=
140 FindValidBoundaryAfter(text_
, text_
.length() - half_length
);
141 return text_
.substr(0, prefix_length
) + ellipsis_text
+
142 text_
.substr(suffix_start
);
145 base::string16
ElideFilename(const base::FilePath
& filename
,
146 const FontList
& font_list
,
147 float available_pixel_width
) {
149 base::string16 filename_utf16
= filename
.value();
150 base::string16 extension
= filename
.Extension();
151 base::string16 rootname
= filename
.BaseName().RemoveExtension().value();
152 #elif defined(OS_POSIX)
153 base::string16 filename_utf16
= WideToUTF16(base::SysNativeMBToWide(
155 base::string16 extension
= WideToUTF16(base::SysNativeMBToWide(
156 filename
.Extension()));
157 base::string16 rootname
= WideToUTF16(base::SysNativeMBToWide(
158 filename
.BaseName().RemoveExtension().value()));
161 const float full_width
= GetStringWidthF(filename_utf16
, font_list
);
162 if (full_width
<= available_pixel_width
)
163 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16
);
165 if (rootname
.empty() || extension
.empty()) {
166 const base::string16 elided_name
=
167 ElideText(filename_utf16
, font_list
, available_pixel_width
, ELIDE_TAIL
);
168 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
171 const float ext_width
= GetStringWidthF(extension
, font_list
);
172 const float root_width
= GetStringWidthF(rootname
, font_list
);
174 // We may have trimmed the path.
175 if (root_width
+ ext_width
<= available_pixel_width
) {
176 const base::string16 elided_name
= rootname
+ extension
;
177 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
180 if (ext_width
>= available_pixel_width
) {
181 const base::string16 elided_name
= ElideText(
182 rootname
+ extension
, font_list
, available_pixel_width
, ELIDE_MIDDLE
);
183 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
186 float available_root_width
= available_pixel_width
- ext_width
;
187 base::string16 elided_name
=
188 ElideText(rootname
, font_list
, available_root_width
, ELIDE_TAIL
);
189 elided_name
+= extension
;
190 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name
);
193 base::string16
ElideText(const base::string16
& text
,
194 const FontList
& font_list
,
195 float available_pixel_width
,
196 ElideBehavior behavior
) {
197 #if !defined(OS_ANDROID) && !defined(OS_IOS)
198 DCHECK_NE(behavior
, FADE_TAIL
);
199 scoped_ptr
<RenderText
> render_text(RenderText::CreateInstance());
200 render_text
->SetCursorEnabled(false);
201 // Do not bother accurately sizing strings over 5000 characters here, for
202 // performance purposes. This matches the behavior of Canvas::SizeStringFloat.
203 render_text
->set_truncate_length(5000);
204 render_text
->SetFontList(font_list
);
205 available_pixel_width
= std::ceil(available_pixel_width
);
206 render_text
->SetDisplayRect(
207 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width
, 1))));
208 render_text
->SetElideBehavior(behavior
);
209 render_text
->SetText(text
);
210 return render_text
->GetDisplayText();
212 DCHECK_NE(behavior
, FADE_TAIL
);
213 if (text
.empty() || behavior
== FADE_TAIL
|| behavior
== NO_ELIDE
||
214 GetStringWidthF(text
, font_list
) <= available_pixel_width
) {
217 if (behavior
== ELIDE_EMAIL
)
218 return ElideEmail(text
, font_list
, available_pixel_width
);
220 const bool elide_in_middle
= (behavior
== ELIDE_MIDDLE
);
221 const bool elide_at_beginning
= (behavior
== ELIDE_HEAD
);
222 const bool insert_ellipsis
= (behavior
!= TRUNCATE
);
223 const base::string16 ellipsis
= base::string16(kEllipsisUTF16
);
224 StringSlicer
slicer(text
, ellipsis
, elide_in_middle
, elide_at_beginning
);
226 if (insert_ellipsis
&&
227 GetStringWidthF(ellipsis
, font_list
) > available_pixel_width
)
228 return base::string16();
230 // Use binary search to compute the elided text.
232 size_t hi
= text
.length() - 1;
235 for (guess
= (lo
+ hi
) / 2; lo
<= hi
; guess
= (lo
+ hi
) / 2) {
236 // We check the width of the whole desired string at once to ensure we
237 // handle kerning/ligatures/etc. correctly.
238 // TODO(skanuj) : Handle directionality of ellipsis based on adjacent
239 // characters. See crbug.com/327963.
240 cut
= slicer
.CutString(guess
, insert_ellipsis
);
241 const float guess_width
= GetStringWidthF(cut
, font_list
);
242 if (guess_width
== available_pixel_width
)
244 if (guess_width
> available_pixel_width
) {
246 // Move back on the loop terminating condition when the guess is too wide.
258 bool ElideString(const base::string16
& input
,
260 base::string16
* output
) {
261 DCHECK_GE(max_len
, 0);
262 if (static_cast<int>(input
.length()) <= max_len
) {
263 output
->assign(input
);
272 output
->assign(input
.substr(0, 1));
275 output
->assign(input
.substr(0, 2));
278 output
->assign(input
.substr(0, 1) + ASCIIToUTF16(".") +
279 input
.substr(input
.length() - 1));
282 output
->assign(input
.substr(0, 1) + ASCIIToUTF16("..") +
283 input
.substr(input
.length() - 1));
286 int rstr_len
= (max_len
- 3) / 2;
287 int lstr_len
= rstr_len
+ ((max_len
- 3) % 2);
288 output
->assign(input
.substr(0, lstr_len
) + ASCIIToUTF16("...") +
289 input
.substr(input
.length() - rstr_len
));
299 // Internal class used to track progress of a rectangular string elide
300 // operation. Exists so the top-level ElideRectangleString() function
301 // can be broken into smaller methods sharing this state.
302 class RectangleString
{
304 RectangleString(size_t max_rows
, size_t max_cols
,
305 bool strict
, base::string16
*output
)
306 : max_rows_(max_rows
),
314 // Perform deferred initializations following creation. Must be called
315 // before any input can be added via AddString().
316 void Init() { output_
->clear(); }
318 // Add an input string, reformatting to fit the desired dimensions.
319 // AddString() may be called multiple times to concatenate together
320 // multiple strings into the region (the current caller doesn't do
322 void AddString(const base::string16
& input
);
324 // Perform any deferred output processing. Must be called after the
325 // last AddString() call has occurred.
329 // Add a line to the rectangular region at the current position,
330 // either by itself or by breaking it into words.
331 void AddLine(const base::string16
& line
);
333 // Add a word to the rectangular region at the current position,
334 // either by itself or by breaking it into characters.
335 void AddWord(const base::string16
& word
);
337 // Add text to the output string if the rectangular boundaries
338 // have not been exceeded, advancing the current position.
339 void Append(const base::string16
& string
);
341 // Set the current position to the beginning of the next line. If
342 // |output| is true, add a newline to the output string if the rectangular
343 // boundaries have not been exceeded. If |output| is false, we assume
344 // some other mechanism will (likely) do similar breaking after the fact.
345 void NewLine(bool output
);
347 // Maximum number of rows allowed in the output string.
350 // Maximum number of characters allowed in the output string.
353 // Current row position, always incremented and may exceed max_rows_
354 // when the input can not fit in the region. We stop appending to
355 // the output string, however, when this condition occurs. In the
356 // future, we may want to expose this value to allow the caller to
357 // determine how many rows would actually be required to hold the
361 // Current character position, should never exceed max_cols_.
364 // True when we do whitespace to newline conversions ourselves.
367 // True when some of the input has been truncated.
370 // String onto which the output is accumulated.
371 base::string16
* output_
;
373 DISALLOW_COPY_AND_ASSIGN(RectangleString
);
376 void RectangleString::AddString(const base::string16
& input
) {
377 base::i18n::BreakIterator
lines(input
,
378 base::i18n::BreakIterator::BREAK_NEWLINE
);
380 while (lines
.Advance())
381 AddLine(lines
.GetString());
383 NOTREACHED() << "BreakIterator (lines) init failed";
387 bool RectangleString::Finalize() {
389 output_
->append(ASCIIToUTF16("..."));
395 void RectangleString::AddLine(const base::string16
& line
) {
396 if (line
.length() < max_cols_
) {
399 base::i18n::BreakIterator
words(line
,
400 base::i18n::BreakIterator::BREAK_SPACE
);
402 while (words
.Advance())
403 AddWord(words
.GetString());
405 NOTREACHED() << "BreakIterator (words) init failed";
408 // Account for naturally-occuring newlines.
413 void RectangleString::AddWord(const base::string16
& word
) {
414 if (word
.length() < max_cols_
) {
415 // Word can be made to fit, no need to fragment it.
416 if (current_col_
+ word
.length() >= max_cols_
)
420 // Word is so big that it must be fragmented.
423 base::i18n::UTF16CharIterator
chars(&word
);
424 while (!chars
.end()) {
425 // When boundary is hit, add as much as will fit on this line.
426 if (current_col_
+ (chars
.char_pos() - char_start
) >= max_cols_
) {
427 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
429 array_start
= chars
.array_pos();
430 char_start
= chars
.char_pos();
434 // Add the last remaining fragment, if any.
435 if (array_start
!= chars
.array_pos())
436 Append(word
.substr(array_start
, chars
.array_pos() - array_start
));
440 void RectangleString::Append(const base::string16
& string
) {
441 if (current_row_
< max_rows_
)
442 output_
->append(string
);
445 current_col_
+= string
.length();
448 void RectangleString::NewLine(bool output
) {
449 if (current_row_
< max_rows_
) {
451 output_
->append(ASCIIToUTF16("\n"));
459 // Internal class used to track progress of a rectangular text elide
460 // operation. Exists so the top-level ElideRectangleText() function
461 // can be broken into smaller methods sharing this state.
462 class RectangleText
{
464 RectangleText(const FontList
& font_list
,
465 float available_pixel_width
,
466 int available_pixel_height
,
467 WordWrapBehavior wrap_behavior
,
468 std::vector
<base::string16
>* lines
)
469 : font_list_(font_list
),
470 line_height_(font_list
.GetHeight()),
471 available_pixel_width_(available_pixel_width
),
472 available_pixel_height_(available_pixel_height
),
473 wrap_behavior_(wrap_behavior
),
476 last_line_ended_in_lf_(false),
478 insufficient_width_(false),
479 insufficient_height_(false) {}
481 // Perform deferred initializions following creation. Must be called
482 // before any input can be added via AddString().
483 void Init() { lines_
->clear(); }
485 // Add an input string, reformatting to fit the desired dimensions.
486 // AddString() may be called multiple times to concatenate together
487 // multiple strings into the region (the current caller doesn't do
489 void AddString(const base::string16
& input
);
491 // Perform any deferred output processing. Must be called after the last
492 // AddString() call has occured. Returns a combination of
493 // |ReformattingResultFlags| indicating whether the given width or height was
494 // insufficient, leading to elision or truncation.
498 // Add a line to the rectangular region at the current position,
499 // either by itself or by breaking it into words.
500 void AddLine(const base::string16
& line
);
502 // Wrap the specified word across multiple lines.
503 int WrapWord(const base::string16
& word
);
505 // Add a long word - wrapping, eliding or truncating per the wrap behavior.
506 int AddWordOverflow(const base::string16
& word
);
508 // Add a word to the rectangluar region at the current position.
509 int AddWord(const base::string16
& word
);
511 // Append the specified |text| to the current output line, incrementing the
512 // running width by the specified amount. This is an optimization over
513 // |AddToCurrentLine()| when |text_width| is already known.
514 void AddToCurrentLineWithWidth(const base::string16
& text
, float text_width
);
516 // Append the specified |text| to the current output line.
517 void AddToCurrentLine(const base::string16
& text
);
519 // Set the current position to the beginning of the next line.
522 // The font list used for measuring text width.
523 const FontList
& font_list_
;
525 // The height of each line of text.
526 const int line_height_
;
528 // The number of pixels of available width in the rectangle.
529 const float available_pixel_width_
;
531 // The number of pixels of available height in the rectangle.
532 const int available_pixel_height_
;
534 // The wrap behavior for words that are too long to fit on a single line.
535 const WordWrapBehavior wrap_behavior_
;
537 // The current running width.
538 float current_width_
;
540 // The current running height.
543 // The current line of text.
544 base::string16 current_line_
;
546 // Indicates whether the last line ended with \n.
547 bool last_line_ended_in_lf_
;
549 // The output vector of lines.
550 std::vector
<base::string16
>* lines_
;
552 // Indicates whether a word was so long that it had to be truncated or elided
553 // to fit the available width.
554 bool insufficient_width_
;
556 // Indicates whether there were too many lines for the available height.
557 bool insufficient_height_
;
559 DISALLOW_COPY_AND_ASSIGN(RectangleText
);
562 void RectangleText::AddString(const base::string16
& input
) {
563 base::i18n::BreakIterator
lines(input
,
564 base::i18n::BreakIterator::BREAK_NEWLINE
);
566 while (!insufficient_height_
&& lines
.Advance()) {
567 base::string16 line
= lines
.GetString();
568 // The BREAK_NEWLINE iterator will keep the trailing newline character,
569 // except in the case of the last line, which may not have one. Remove
570 // the newline character, if it exists.
571 last_line_ended_in_lf_
= !line
.empty() && line
[line
.length() - 1] == '\n';
572 if (last_line_ended_in_lf_
)
573 line
.resize(line
.length() - 1);
577 NOTREACHED() << "BreakIterator (lines) init failed";
581 int RectangleText::Finalize() {
582 // Remove trailing whitespace from the last line or remove the last line
583 // completely, if it's just whitespace.
584 if (!insufficient_height_
&& !lines_
->empty()) {
585 base::TrimWhitespace(lines_
->back(), base::TRIM_TRAILING
, &lines_
->back());
586 if (lines_
->back().empty() && !last_line_ended_in_lf_
)
589 if (last_line_ended_in_lf_
)
590 lines_
->push_back(base::string16());
591 return (insufficient_width_
? INSUFFICIENT_SPACE_HORIZONTAL
: 0) |
592 (insufficient_height_
? INSUFFICIENT_SPACE_VERTICAL
: 0);
595 void RectangleText::AddLine(const base::string16
& line
) {
596 const float line_width
= GetStringWidthF(line
, font_list_
);
597 if (line_width
<= available_pixel_width_
) {
598 AddToCurrentLineWithWidth(line
, line_width
);
600 // Iterate over positions that are valid to break the line at. In general,
601 // these are word boundaries but after any punctuation following the word.
602 base::i18n::BreakIterator
words(line
,
603 base::i18n::BreakIterator::BREAK_LINE
);
605 while (words
.Advance()) {
606 const bool truncate
= !current_line_
.empty();
607 const base::string16
& word
= words
.GetString();
608 const int lines_added
= AddWord(word
);
611 // Trim trailing whitespace from the line that was added.
612 const int line
= lines_
->size() - lines_added
;
613 base::TrimWhitespace(lines_
->at(line
), base::TRIM_TRAILING
,
616 if (base::ContainsOnlyChars(word
, base::kWhitespaceUTF16
)) {
617 // Skip the first space if the previous line was carried over.
619 current_line_
.clear();
624 NOTREACHED() << "BreakIterator (words) init failed";
627 // Account for naturally-occuring newlines.
631 int RectangleText::WrapWord(const base::string16
& word
) {
632 // Word is so wide that it must be fragmented.
633 base::string16 text
= word
;
635 bool first_fragment
= true;
636 while (!insufficient_height_
&& !text
.empty()) {
637 base::string16 fragment
=
638 ElideText(text
, font_list_
, available_pixel_width_
, TRUNCATE
);
639 // At least one character has to be added at every line, even if the
640 // available space is too small.
641 if (fragment
.empty())
642 fragment
= text
.substr(0, 1);
643 if (!first_fragment
&& NewLine())
645 AddToCurrentLine(fragment
);
646 text
= text
.substr(fragment
.length());
647 first_fragment
= false;
652 int RectangleText::AddWordOverflow(const base::string16
& word
) {
655 // Unless this is the very first word, put it on a new line.
656 if (!current_line_
.empty()) {
662 if (wrap_behavior_
== IGNORE_LONG_WORDS
) {
663 current_line_
= word
;
664 current_width_
= available_pixel_width_
;
665 } else if (wrap_behavior_
== WRAP_LONG_WORDS
) {
666 lines_added
+= WrapWord(word
);
668 const ElideBehavior elide_behavior
=
669 (wrap_behavior_
== ELIDE_LONG_WORDS
? ELIDE_TAIL
: TRUNCATE
);
670 const base::string16 elided_word
=
671 ElideText(word
, font_list_
, available_pixel_width_
, elide_behavior
);
672 AddToCurrentLine(elided_word
);
673 insufficient_width_
= true;
679 int RectangleText::AddWord(const base::string16
& word
) {
681 base::string16 trimmed
;
682 base::TrimWhitespace(word
, base::TRIM_TRAILING
, &trimmed
);
683 const float trimmed_width
= GetStringWidthF(trimmed
, font_list_
);
684 if (trimmed_width
<= available_pixel_width_
) {
685 // Word can be made to fit, no need to fragment it.
686 if ((current_width_
+ trimmed_width
> available_pixel_width_
) && NewLine())
688 // Append the non-trimmed word, in case more words are added after.
689 AddToCurrentLine(word
);
691 lines_added
= AddWordOverflow(wrap_behavior_
== IGNORE_LONG_WORDS
?
697 void RectangleText::AddToCurrentLine(const base::string16
& text
) {
698 AddToCurrentLineWithWidth(text
, GetStringWidthF(text
, font_list_
));
701 void RectangleText::AddToCurrentLineWithWidth(const base::string16
& text
,
703 if (current_height_
>= available_pixel_height_
) {
704 insufficient_height_
= true;
707 current_line_
.append(text
);
708 current_width_
+= text_width
;
711 bool RectangleText::NewLine() {
712 bool line_added
= false;
713 if (current_height_
< available_pixel_height_
) {
714 lines_
->push_back(current_line_
);
715 current_line_
.clear();
718 insufficient_height_
= true;
720 current_height_
+= line_height_
;
727 bool ElideRectangleString(const base::string16
& input
, size_t max_rows
,
728 size_t max_cols
, bool strict
,
729 base::string16
* output
) {
730 RectangleString
rect(max_rows
, max_cols
, strict
, output
);
732 rect
.AddString(input
);
733 return rect
.Finalize();
736 int ElideRectangleText(const base::string16
& input
,
737 const FontList
& font_list
,
738 float available_pixel_width
,
739 int available_pixel_height
,
740 WordWrapBehavior wrap_behavior
,
741 std::vector
<base::string16
>* lines
) {
742 RectangleText
rect(font_list
,
743 available_pixel_width
,
744 available_pixel_height
,
748 rect
.AddString(input
);
749 return rect
.Finalize();
752 base::string16
TruncateString(const base::string16
& string
,
754 BreakType break_type
) {
755 DCHECK(break_type
== CHARACTER_BREAK
|| break_type
== WORD_BREAK
);
757 if (string
.size() <= length
)
758 // String fits, return it.
762 // No room for the elide string, return an empty string.
763 return base::string16();
765 size_t max
= length
- 1;
767 // Added to the end of strings that are too big.
768 static const base::char16 kElideString
[] = { 0x2026, 0 };
771 // Just enough room for the elide string.
774 int32_t index
= static_cast<int32_t>(max
);
775 if (break_type
== WORD_BREAK
) {
776 // Use a line iterator to find the first boundary.
777 UErrorCode status
= U_ZERO_ERROR
;
778 scoped_ptr
<icu::BreakIterator
> bi(
779 icu::RuleBasedBreakIterator::createLineInstance(
780 icu::Locale::getDefault(), status
));
781 if (U_FAILURE(status
))
782 return string
.substr(0, max
) + kElideString
;
783 bi
->setText(string
.c_str());
784 index
= bi
->preceding(index
);
785 if (index
== icu::BreakIterator::DONE
|| index
== 0) {
786 // We either found no valid line break at all, or one right at the
787 // beginning of the string. Go back to the end; we'll have to break in the
789 index
= static_cast<int32_t>(max
);
793 // Use a character iterator to find the previous non-whitespace character.
794 icu::StringCharacterIterator
char_iterator(string
.c_str());
795 char_iterator
.setIndex(index
);
796 while (char_iterator
.hasPrevious()) {
797 char_iterator
.previous();
798 if (!(u_isspace(char_iterator
.current()) ||
799 u_charType(char_iterator
.current()) == U_CONTROL_CHAR
||
800 u_charType(char_iterator
.current()) == U_NON_SPACING_MARK
)) {
801 // Not a whitespace character. Advance the iterator so that we
802 // include the current character in the truncated string.
803 char_iterator
.next();
807 if (char_iterator
.hasPrevious()) {
808 // Found a valid break point.
809 index
= char_iterator
.getIndex();
811 // String has leading whitespace, return the elide string.
815 return string
.substr(0, index
) + kElideString
;