| 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" |
| 11 | 11 |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 | 13 |
| 14 #include <memory> |
| 14 #include <string> | 15 #include <string> |
| 15 #include <vector> | 16 #include <vector> |
| 16 | 17 |
| 17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 18 #include "base/i18n/break_iterator.h" | 19 #include "base/i18n/break_iterator.h" |
| 19 #include "base/i18n/char_iterator.h" | 20 #include "base/i18n/char_iterator.h" |
| 20 #include "base/i18n/rtl.h" | 21 #include "base/i18n/rtl.h" |
| 21 #include "base/macros.h" | 22 #include "base/macros.h" |
| 22 #include "base/memory/scoped_ptr.h" | |
| 23 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/sys_string_conversions.h" | 25 #include "base/strings/sys_string_conversions.h" |
| 26 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 28 #include "third_party/icu/source/common/unicode/rbbi.h" | 28 #include "third_party/icu/source/common/unicode/rbbi.h" |
| 29 #include "third_party/icu/source/common/unicode/uloc.h" | 29 #include "third_party/icu/source/common/unicode/uloc.h" |
| 30 #include "third_party/icu/source/common/unicode/umachine.h" | 30 #include "third_party/icu/source/common/unicode/umachine.h" |
| 31 #include "ui/gfx/font_list.h" | 31 #include "ui/gfx/font_list.h" |
| 32 #include "ui/gfx/geometry/rect_conversions.h" | 32 #include "ui/gfx/geometry/rect_conversions.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 elided_name += extension; | 191 elided_name += extension; |
| 192 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 192 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 193 } | 193 } |
| 194 | 194 |
| 195 base::string16 ElideText(const base::string16& text, | 195 base::string16 ElideText(const base::string16& text, |
| 196 const FontList& font_list, | 196 const FontList& font_list, |
| 197 float available_pixel_width, | 197 float available_pixel_width, |
| 198 ElideBehavior behavior) { | 198 ElideBehavior behavior) { |
| 199 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 199 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 200 DCHECK_NE(behavior, FADE_TAIL); | 200 DCHECK_NE(behavior, FADE_TAIL); |
| 201 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 201 std::unique_ptr<RenderText> render_text(RenderText::CreateInstance()); |
| 202 render_text->SetCursorEnabled(false); | 202 render_text->SetCursorEnabled(false); |
| 203 // TODO(bshe): 5000 is out dated. We should remove it. See crbug.com/551660. | 203 // TODO(bshe): 5000 is out dated. We should remove it. See crbug.com/551660. |
| 204 // Do not bother accurately sizing strings over 5000 characters here, for | 204 // Do not bother accurately sizing strings over 5000 characters here, for |
| 205 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. | 205 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. |
| 206 render_text->set_truncate_length(5000); | 206 render_text->set_truncate_length(5000); |
| 207 render_text->SetFontList(font_list); | 207 render_text->SetFontList(font_list); |
| 208 available_pixel_width = std::ceil(available_pixel_width); | 208 available_pixel_width = std::ceil(available_pixel_width); |
| 209 render_text->SetDisplayRect( | 209 render_text->SetDisplayRect( |
| 210 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width, 1)))); | 210 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width, 1)))); |
| 211 render_text->SetElideBehavior(behavior); | 211 render_text->SetElideBehavior(behavior); |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 // Added to the end of strings that are too big. | 766 // Added to the end of strings that are too big. |
| 767 static const base::char16 kElideString[] = { 0x2026, 0 }; | 767 static const base::char16 kElideString[] = { 0x2026, 0 }; |
| 768 | 768 |
| 769 if (length == 1) | 769 if (length == 1) |
| 770 return kElideString; // Only room for an ellipsis. | 770 return kElideString; // Only room for an ellipsis. |
| 771 | 771 |
| 772 int32_t index = static_cast<int32_t>(length - 1); | 772 int32_t index = static_cast<int32_t>(length - 1); |
| 773 if (word_break) { | 773 if (word_break) { |
| 774 // Use a word iterator to find the first boundary. | 774 // Use a word iterator to find the first boundary. |
| 775 UErrorCode status = U_ZERO_ERROR; | 775 UErrorCode status = U_ZERO_ERROR; |
| 776 scoped_ptr<icu::BreakIterator> bi( | 776 std::unique_ptr<icu::BreakIterator> bi( |
| 777 icu::RuleBasedBreakIterator::createWordInstance( | 777 icu::RuleBasedBreakIterator::createWordInstance( |
| 778 icu::Locale::getDefault(), status)); | 778 icu::Locale::getDefault(), status)); |
| 779 if (U_FAILURE(status)) | 779 if (U_FAILURE(status)) |
| 780 return string.substr(0, length - 1) + kElideString; | 780 return string.substr(0, length - 1) + kElideString; |
| 781 bi->setText(string.c_str()); | 781 bi->setText(string.c_str()); |
| 782 index = bi->preceding(static_cast<int32_t>(length)); | 782 index = bi->preceding(static_cast<int32_t>(length)); |
| 783 if (index == icu::BreakIterator::DONE || index == 0) { | 783 if (index == icu::BreakIterator::DONE || index == 0) { |
| 784 // We either found no valid word break at all, or one right at the | 784 // We either found no valid word break at all, or one right at the |
| 785 // beginning of the string. Go back to the end; we'll have to break in the | 785 // beginning of the string. Go back to the end; we'll have to break in the |
| 786 // middle of a word. | 786 // middle of a word. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 811 // of, and we can fit at least one character of the word in the elided string. | 811 // of, and we can fit at least one character of the word in the elided string. |
| 812 // Do that rather than just returning an ellipsis. | 812 // Do that rather than just returning an ellipsis. |
| 813 if (word_break && (index != static_cast<int32_t>(length - 1))) | 813 if (word_break && (index != static_cast<int32_t>(length - 1))) |
| 814 return string.substr(0, length - 1) + kElideString; | 814 return string.substr(0, length - 1) + kElideString; |
| 815 | 815 |
| 816 // Trying to break after only whitespace, elide all of it. | 816 // Trying to break after only whitespace, elide all of it. |
| 817 return kElideString; | 817 return kElideString; |
| 818 } | 818 } |
| 819 | 819 |
| 820 } // namespace gfx | 820 } // namespace gfx |
| OLD | NEW |