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 <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/i18n/break_iterator.h" | 16 #include "base/i18n/break_iterator.h" |
17 #include "base/i18n/char_iterator.h" | 17 #include "base/i18n/char_iterator.h" |
18 #include "base/i18n/rtl.h" | 18 #include "base/i18n/rtl.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
22 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
24 #include "third_party/icu/source/common/unicode/rbbi.h" | 24 #include "third_party/icu/source/common/unicode/rbbi.h" |
25 #include "third_party/icu/source/common/unicode/uloc.h" | 25 #include "third_party/icu/source/common/unicode/uloc.h" |
26 #include "ui/gfx/font_list.h" | 26 #include "ui/gfx/font_list.h" |
| 27 #include "ui/gfx/geometry/rect_conversions.h" |
27 #include "ui/gfx/render_text.h" | 28 #include "ui/gfx/render_text.h" |
28 #include "ui/gfx/text_utils.h" | 29 #include "ui/gfx/text_utils.h" |
29 | 30 |
30 using base::ASCIIToUTF16; | 31 using base::ASCIIToUTF16; |
31 using base::UTF8ToUTF16; | 32 using base::UTF8ToUTF16; |
32 using base::WideToUTF16; | 33 using base::WideToUTF16; |
33 | 34 |
34 namespace gfx { | 35 namespace gfx { |
35 | 36 |
36 namespace { | 37 namespace { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 ElideBehavior behavior) { | 205 ElideBehavior behavior) { |
205 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 206 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
206 DCHECK_NE(behavior, FADE_TAIL); | 207 DCHECK_NE(behavior, FADE_TAIL); |
207 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 208 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
208 render_text->SetCursorEnabled(false); | 209 render_text->SetCursorEnabled(false); |
209 // Do not bother accurately sizing strings over 5000 characters here, for | 210 // Do not bother accurately sizing strings over 5000 characters here, for |
210 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. | 211 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. |
211 render_text->set_truncate_length(5000); | 212 render_text->set_truncate_length(5000); |
212 render_text->SetFontList(font_list); | 213 render_text->SetFontList(font_list); |
213 available_pixel_width = std::ceil(available_pixel_width); | 214 available_pixel_width = std::ceil(available_pixel_width); |
214 render_text->SetDisplayRect(gfx::Rect(gfx::Size(available_pixel_width, 1))); | 215 render_text->SetDisplayRect( |
| 216 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width, 1)))); |
215 render_text->SetElideBehavior(behavior); | 217 render_text->SetElideBehavior(behavior); |
216 render_text->SetText(text); | 218 render_text->SetText(text); |
217 return render_text->layout_text(); | 219 return render_text->layout_text(); |
218 #else | 220 #else |
219 DCHECK_NE(behavior, FADE_TAIL); | 221 DCHECK_NE(behavior, FADE_TAIL); |
220 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || | 222 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || |
221 GetStringWidthF(text, font_list) <= available_pixel_width) { | 223 GetStringWidthF(text, font_list) <= available_pixel_width) { |
222 return text; | 224 return text; |
223 } | 225 } |
224 if (behavior == ELIDE_EMAIL) | 226 if (behavior == ELIDE_EMAIL) |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 index = char_iterator.getIndex(); | 817 index = char_iterator.getIndex(); |
816 } else { | 818 } else { |
817 // String has leading whitespace, return the elide string. | 819 // String has leading whitespace, return the elide string. |
818 return kElideString; | 820 return kElideString; |
819 } | 821 } |
820 | 822 |
821 return string.substr(0, index) + kElideString; | 823 return string.substr(0, index) + kElideString; |
822 } | 824 } |
823 | 825 |
824 } // namespace gfx | 826 } // namespace gfx |
OLD | NEW |