Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Unified Diff: ui/gfx/text_elider.cc

Issue 381953002: New avatar button: Consolidate text elision between Mac and Win/Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/text_elider.cc
diff --git a/ui/gfx/text_elider.cc b/ui/gfx/text_elider.cc
index 56ed501b834ed8b0d8b329051f4ca08082a557bd..279e5cb3be87ff3142647f3f3ab5d15fde476f97 100644
--- a/ui/gfx/text_elider.cc
+++ b/ui/gfx/text_elider.cc
@@ -755,7 +755,11 @@ int ElideRectangleText(const base::string16& input,
return rect.Finalize();
}
-base::string16 TruncateString(const base::string16& string, size_t length) {
+base::string16 TruncateString(const base::string16& string,
+ size_t length,
+ BreakType break_type) {
+ DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK);
+
if (string.size() <= length)
// String fits, return it.
return string;
@@ -773,47 +777,47 @@ base::string16 TruncateString(const base::string16& string, size_t length) {
// Just enough room for the elide string.
return kElideString;
- // Use a line iterator to find the first boundary.
- UErrorCode status = U_ZERO_ERROR;
- scoped_ptr<icu::RuleBasedBreakIterator> bi(
- static_cast<icu::RuleBasedBreakIterator*>(
- icu::RuleBasedBreakIterator::createLineInstance(
- icu::Locale::getDefault(), status)));
- if (U_FAILURE(status))
- return string.substr(0, max) + kElideString;
- bi->setText(string.c_str());
- int32_t index = bi->preceding(static_cast<int32_t>(max));
- if (index == icu::BreakIterator::DONE) {
- index = static_cast<int32_t>(max);
- } else {
- // Found a valid break (may be the beginning of the string). Now use
- // a character iterator to find the previous non-whitespace character.
- icu::StringCharacterIterator char_iterator(string.c_str());
- if (index == 0) {
- // No valid line breaks. Start at the end again. This ensures we break
- // on a valid character boundary.
+ int32_t index = static_cast<int32_t>(max);
+ if (break_type == WORD_BREAK) {
+ // Use a line iterator to find the first boundary.
+ UErrorCode status = U_ZERO_ERROR;
+ scoped_ptr<icu::BreakIterator> bi(
+ icu::RuleBasedBreakIterator::createLineInstance(
+ icu::Locale::getDefault(), status));
+ if (U_FAILURE(status))
+ return string.substr(0, max) + kElideString;
+ bi->setText(string.c_str());
+ index = bi->preceding(index);
+ if (index == icu::BreakIterator::DONE || index == 0) {
+ // We either found no valid line break at all, or one right at the
+ // beginning of the string. Go back to the end; we'll have to break in the
+ // middle of a word.
index = static_cast<int32_t>(max);
}
- char_iterator.setIndex(index);
- while (char_iterator.hasPrevious()) {
- char_iterator.previous();
- if (!(u_isspace(char_iterator.current()) ||
- u_charType(char_iterator.current()) == U_CONTROL_CHAR ||
- u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) {
- // Not a whitespace character. Advance the iterator so that we
- // include the current character in the truncated string.
- char_iterator.next();
- break;
- }
- }
- if (char_iterator.hasPrevious()) {
- // Found a valid break point.
- index = char_iterator.getIndex();
- } else {
- // String has leading whitespace, return the elide string.
- return kElideString;
+ }
+
+ // Use a character iterator to find the previous non-whitespace character.
+ icu::StringCharacterIterator char_iterator(string.c_str());
+ char_iterator.setIndex(index);
+ while (char_iterator.hasPrevious()) {
+ char_iterator.previous();
+ if (!(u_isspace(char_iterator.current()) ||
+ u_charType(char_iterator.current()) == U_CONTROL_CHAR ||
+ u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) {
+ // Not a whitespace character. Advance the iterator so that we
+ // include the current character in the truncated string.
+ char_iterator.next();
+ break;
}
}
+ if (char_iterator.hasPrevious()) {
+ // Found a valid break point.
+ index = char_iterator.getIndex();
+ } else {
+ // String has leading whitespace, return the elide string.
+ return kElideString;
+ }
+
return string.substr(0, index) + kElideString;
}

Powered by Google App Engine
This is Rietveld 408576698