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