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

Side by Side 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: use TruncateString 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This file implements utility functions for eliding and formatting UI text. 5 // This file implements utility functions for eliding and formatting UI text.
6 // 6 //
7 // Note that several of the functions declared in text_elider.h are implemented 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. 8 // in this file using helper classes in an unnamed namespace.
9 9
10 #include "ui/gfx/text_elider.h" 10 #include "ui/gfx/text_elider.h"
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 RectangleText rect(font_list, 748 RectangleText rect(font_list,
749 available_pixel_width, 749 available_pixel_width,
750 available_pixel_height, 750 available_pixel_height,
751 wrap_behavior, 751 wrap_behavior,
752 lines); 752 lines);
753 rect.Init(); 753 rect.Init();
754 rect.AddString(input); 754 rect.AddString(input);
755 return rect.Finalize(); 755 return rect.Finalize();
756 } 756 }
757 757
758 base::string16 TruncateString(const base::string16& string, size_t length) { 758 base::string16 TruncateString(
759 const base::string16& string, size_t length, bool only_at_word_breaks) {
759 if (string.size() <= length) 760 if (string.size() <= length)
760 // String fits, return it. 761 // String fits, return it.
761 return string; 762 return string;
762 763
763 if (length == 0) 764 if (length == 0)
764 // No room for the elide string, return an empty string. 765 // No room for the elide string, return an empty string.
765 return base::string16(); 766 return base::string16();
766 767
767 size_t max = length - 1; 768 size_t max = length - 1;
768 769
769 // Added to the end of strings that are too big. 770 // Added to the end of strings that are too big.
770 static const base::char16 kElideString[] = { 0x2026, 0 }; 771 static const base::char16 kElideString[] = { 0x2026, 0 };
771 772
772 if (max == 0) 773 if (max == 0)
773 // Just enough room for the elide string. 774 // Just enough room for the elide string.
774 return kElideString; 775 return kElideString;
775 776
776 // Use a line iterator to find the first boundary. 777 int32_t index = static_cast<int32_t>(max);
777 UErrorCode status = U_ZERO_ERROR; 778 if (only_at_word_breaks) {
778 scoped_ptr<icu::RuleBasedBreakIterator> bi( 779 // Use a line iterator to find the first boundary.
779 static_cast<icu::RuleBasedBreakIterator*>( 780 UErrorCode status = U_ZERO_ERROR;
780 icu::RuleBasedBreakIterator::createLineInstance( 781 scoped_ptr<icu::BreakIterator> bi(
781 icu::Locale::getDefault(), status))); 782 icu::RuleBasedBreakIterator::createLineInstance(
782 if (U_FAILURE(status)) 783 icu::Locale::getDefault(), status));
783 return string.substr(0, max) + kElideString; 784 if (U_FAILURE(status))
784 bi->setText(string.c_str()); 785 return string.substr(0, max) + kElideString;
785 int32_t index = bi->preceding(static_cast<int32_t>(max)); 786 bi->setText(string.c_str());
786 if (index == icu::BreakIterator::DONE) { 787 index = bi->preceding(index);
787 index = static_cast<int32_t>(max); 788 if (index == icu::BreakIterator::DONE || index == 0) {
788 } else { 789 // We either found no valid line break at all, or one right at the
789 // Found a valid break (may be the beginning of the string). Now use 790 // beginning of the string. Go back to the end; we'll have to break in the
790 // a character iterator to find the previous non-whitespace character. 791 // middle of a word.
791 icu::StringCharacterIterator char_iterator(string.c_str());
792 if (index == 0) {
793 // No valid line breaks. Start at the end again. This ensures we break
794 // on a valid character boundary.
795 index = static_cast<int32_t>(max); 792 index = static_cast<int32_t>(max);
796 } 793 }
797 char_iterator.setIndex(index); 794 }
798 while (char_iterator.hasPrevious()) { 795
799 char_iterator.previous(); 796 // Use a character iterator to find the previous non-whitespace character.
800 if (!(u_isspace(char_iterator.current()) || 797 icu::StringCharacterIterator char_iterator(string.c_str());
801 u_charType(char_iterator.current()) == U_CONTROL_CHAR || 798 char_iterator.setIndex(index);
802 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { 799 while (char_iterator.hasPrevious()) {
803 // Not a whitespace character. Advance the iterator so that we 800 char_iterator.previous();
804 // include the current character in the truncated string. 801 if (!(u_isspace(char_iterator.current()) ||
805 char_iterator.next(); 802 u_charType(char_iterator.current()) == U_CONTROL_CHAR ||
806 break; 803 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) {
807 } 804 // Not a whitespace character. Advance the iterator so that we
808 } 805 // include the current character in the truncated string.
809 if (char_iterator.hasPrevious()) { 806 char_iterator.next();
810 // Found a valid break point. 807 break;
811 index = char_iterator.getIndex();
812 } else {
813 // String has leading whitespace, return the elide string.
814 return kElideString;
815 } 808 }
816 } 809 }
810 if (char_iterator.hasPrevious()) {
811 // Found a valid break point.
812 index = char_iterator.getIndex();
813 } else {
814 // String has leading whitespace, return the elide string.
815 return kElideString;
816 }
817
817 return string.substr(0, index) + kElideString; 818 return string.substr(0, index) + kElideString;
818 } 819 }
819 820
820 } // namespace gfx 821 } // namespace gfx
OLDNEW
« ui/gfx/text_elider.h ('K') | « ui/gfx/text_elider.h ('k') | ui/gfx/text_elider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698