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

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: 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 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(const base::string16& string,
759 size_t length,
760 BreakType break_type) {
761 DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK);
762
759 if (string.size() <= length) 763 if (string.size() <= length)
760 // String fits, return it. 764 // String fits, return it.
761 return string; 765 return string;
762 766
763 if (length == 0) 767 if (length == 0)
764 // No room for the elide string, return an empty string. 768 // No room for the elide string, return an empty string.
765 return base::string16(); 769 return base::string16();
766 770
767 size_t max = length - 1; 771 size_t max = length - 1;
768 772
769 // Added to the end of strings that are too big. 773 // Added to the end of strings that are too big.
770 static const base::char16 kElideString[] = { 0x2026, 0 }; 774 static const base::char16 kElideString[] = { 0x2026, 0 };
771 775
772 if (max == 0) 776 if (max == 0)
773 // Just enough room for the elide string. 777 // Just enough room for the elide string.
774 return kElideString; 778 return kElideString;
775 779
776 // Use a line iterator to find the first boundary. 780 int32_t index = static_cast<int32_t>(max);
777 UErrorCode status = U_ZERO_ERROR; 781 if (break_type == WORD_BREAK) {
778 scoped_ptr<icu::RuleBasedBreakIterator> bi( 782 // Use a line iterator to find the first boundary.
779 static_cast<icu::RuleBasedBreakIterator*>( 783 UErrorCode status = U_ZERO_ERROR;
780 icu::RuleBasedBreakIterator::createLineInstance( 784 scoped_ptr<icu::BreakIterator> bi(
781 icu::Locale::getDefault(), status))); 785 icu::RuleBasedBreakIterator::createLineInstance(
782 if (U_FAILURE(status)) 786 icu::Locale::getDefault(), status));
783 return string.substr(0, max) + kElideString; 787 if (U_FAILURE(status))
784 bi->setText(string.c_str()); 788 return string.substr(0, max) + kElideString;
785 int32_t index = bi->preceding(static_cast<int32_t>(max)); 789 bi->setText(string.c_str());
786 if (index == icu::BreakIterator::DONE) { 790 index = bi->preceding(index);
787 index = static_cast<int32_t>(max); 791 if (index == icu::BreakIterator::DONE || index == 0) {
788 } else { 792 // 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 793 // 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. 794 // 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); 795 index = static_cast<int32_t>(max);
796 } 796 }
797 char_iterator.setIndex(index); 797 }
798 while (char_iterator.hasPrevious()) { 798
799 char_iterator.previous(); 799 // Use a character iterator to find the previous non-whitespace character.
800 if (!(u_isspace(char_iterator.current()) || 800 icu::StringCharacterIterator char_iterator(string.c_str());
801 u_charType(char_iterator.current()) == U_CONTROL_CHAR || 801 char_iterator.setIndex(index);
802 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { 802 while (char_iterator.hasPrevious()) {
803 // Not a whitespace character. Advance the iterator so that we 803 char_iterator.previous();
804 // include the current character in the truncated string. 804 if (!(u_isspace(char_iterator.current()) ||
805 char_iterator.next(); 805 u_charType(char_iterator.current()) == U_CONTROL_CHAR ||
806 break; 806 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) {
807 } 807 // Not a whitespace character. Advance the iterator so that we
808 } 808 // include the current character in the truncated string.
809 if (char_iterator.hasPrevious()) { 809 char_iterator.next();
810 // Found a valid break point. 810 break;
811 index = char_iterator.getIndex();
812 } else {
813 // String has leading whitespace, return the elide string.
814 return kElideString;
815 } 811 }
816 } 812 }
813 if (char_iterator.hasPrevious()) {
814 // Found a valid break point.
815 index = char_iterator.getIndex();
816 } else {
817 // String has leading whitespace, return the elide string.
818 return kElideString;
819 }
820
817 return string.substr(0, index) + kElideString; 821 return string.substr(0, index) + kElideString;
818 } 822 }
819 823
820 } // namespace gfx 824 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698