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 21 matching lines...) Expand all Loading... | |
32 #include "ui/gfx/text_utils.h" | 32 #include "ui/gfx/text_utils.h" |
33 | 33 |
34 using base::ASCIIToUTF16; | 34 using base::ASCIIToUTF16; |
35 using base::UTF8ToUTF16; | 35 using base::UTF8ToUTF16; |
36 using base::WideToUTF16; | 36 using base::WideToUTF16; |
37 | 37 |
38 namespace gfx { | 38 namespace gfx { |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 #if defined(OS_ANDROID) && !defined(USE_AURA) || defined(OS_IOS) | 42 #if defined(OS_ANDROID) || defined(OS_IOS) |
43 // The returned string will have at least one character besides the ellipsis | 43 // The returned string will have at least one character besides the ellipsis |
44 // on either side of '@'; if that's impossible, a single ellipsis is returned. | 44 // on either side of '@'; if that's impossible, a single ellipsis is returned. |
45 // If possible, only the username is elided. Otherwise, the domain is elided | 45 // If possible, only the username is elided. Otherwise, the domain is elided |
46 // in the middle, splitting available width equally with the elided username. | 46 // in the middle, splitting available width equally with the elided username. |
47 // If the username is short enough that it doesn't need half the available | 47 // If the username is short enough that it doesn't need half the available |
48 // width, the elided domain will occupy that extra width. | 48 // width, the elided domain will occupy that extra width. |
49 base::string16 ElideEmail(const base::string16& email, | 49 base::string16 ElideEmail(const base::string16& email, |
50 const FontList& font_list, | 50 const FontList& font_list, |
51 float available_pixel_width) { | 51 float available_pixel_width) { |
52 if (GetStringWidthF(email, font_list) <= available_pixel_width) | 52 if (GetStringWidthF(email, font_list) <= available_pixel_width) |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 base::string16 elided_name = | 187 base::string16 elided_name = |
188 ElideText(rootname, font_list, available_root_width, ELIDE_TAIL); | 188 ElideText(rootname, font_list, available_root_width, ELIDE_TAIL); |
189 elided_name += extension; | 189 elided_name += extension; |
190 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 190 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
191 } | 191 } |
192 | 192 |
193 base::string16 ElideText(const base::string16& text, | 193 base::string16 ElideText(const base::string16& text, |
194 const FontList& font_list, | 194 const FontList& font_list, |
195 float available_pixel_width, | 195 float available_pixel_width, |
196 ElideBehavior behavior) { | 196 ElideBehavior behavior) { |
197 #if defined(OS_ANDROID) && !defined(USE_AURA) || defined(OS_IOS) | 197 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
198 DCHECK_NE(behavior, FADE_TAIL); | |
199 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
200 render_text->SetCursorEnabled(false); | |
201 // Do not bother accurately sizing strings over 5000 characters here, for | |
202 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. | |
203 render_text->set_truncate_length(5000); | |
204 render_text->SetFontList(font_list); | |
205 available_pixel_width = std::ceil(available_pixel_width); | |
206 render_text->SetDisplayRect( | |
207 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width, 1)))); | |
208 render_text->SetElideBehavior(behavior); | |
209 render_text->SetText(text); | |
210 return render_text->GetDisplayText(); | |
211 #else | |
198 DCHECK_NE(behavior, FADE_TAIL); | 212 DCHECK_NE(behavior, FADE_TAIL); |
199 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || | 213 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || |
200 GetStringWidthF(text, font_list) <= available_pixel_width) { | 214 GetStringWidthF(text, font_list) <= available_pixel_width) { |
201 return text; | 215 return text; |
202 } | 216 } |
203 if (behavior == ELIDE_EMAIL) | 217 if (behavior == ELIDE_EMAIL) |
204 return ElideEmail(text, font_list, available_pixel_width); | 218 return ElideEmail(text, font_list, available_pixel_width); |
205 | 219 |
206 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); | 220 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); |
207 const bool elide_at_beginning = (behavior == ELIDE_HEAD); | 221 const bool elide_at_beginning = (behavior == ELIDE_HEAD); |
(...skipping 23 matching lines...) Expand all Loading... | |
231 hi = guess - 1; | 245 hi = guess - 1; |
232 // Move back on the loop terminating condition when the guess is too wide. | 246 // Move back on the loop terminating condition when the guess is too wide. |
233 if (hi < lo) | 247 if (hi < lo) |
234 lo = hi; | 248 lo = hi; |
235 } else { | 249 } else { |
236 lo = guess + 1; | 250 lo = guess + 1; |
237 } | 251 } |
238 } | 252 } |
239 | 253 |
240 return cut; | 254 return cut; |
241 #else | |
242 DCHECK_NE(behavior, FADE_TAIL); | |
243 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
244 render_text->SetCursorEnabled(false); | |
245 // TODO(bshe): 5000 is out dated. We should remove it. See crbug.com/551660. | |
msw
2016/01/06 18:58:11
It would have been nice to keep this updated comme
bshe
2016/01/06 19:05:30
Will upload a new patch to add the comment back.
| |
246 // Do not bother accurately sizing strings over 5000 characters here, for | |
247 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. | |
248 render_text->set_truncate_length(5000); | |
249 render_text->SetFontList(font_list); | |
250 available_pixel_width = std::ceil(available_pixel_width); | |
251 render_text->SetDisplayRect( | |
252 gfx::ToEnclosingRect(gfx::RectF(gfx::SizeF(available_pixel_width, 1)))); | |
253 render_text->SetElideBehavior(behavior); | |
254 render_text->SetText(text); | |
255 return render_text->GetDisplayText(); | |
256 #endif | 255 #endif |
257 } | 256 } |
258 | 257 |
259 bool ElideString(const base::string16& input, | 258 bool ElideString(const base::string16& input, |
260 int max_len, | 259 int max_len, |
261 base::string16* output) { | 260 base::string16* output) { |
262 DCHECK_GE(max_len, 0); | 261 DCHECK_GE(max_len, 0); |
263 if (static_cast<int>(input.length()) <= max_len) { | 262 if (static_cast<int>(input.length()) <= max_len) { |
264 output->assign(input); | 263 output->assign(input); |
265 return false; | 264 return false; |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
810 // of, and we can fit at least one character of the word in the elided string. | 809 // of, and we can fit at least one character of the word in the elided string. |
811 // Do that rather than just returning an ellipsis. | 810 // Do that rather than just returning an ellipsis. |
812 if (word_break && (index != static_cast<int32_t>(length - 1))) | 811 if (word_break && (index != static_cast<int32_t>(length - 1))) |
813 return string.substr(0, length - 1) + kElideString; | 812 return string.substr(0, length - 1) + kElideString; |
814 | 813 |
815 // Trying to break after only whitespace, elide all of it. | 814 // Trying to break after only whitespace, elide all of it. |
816 return kElideString; | 815 return kElideString; |
817 } | 816 } |
818 | 817 |
819 } // namespace gfx | 818 } // namespace gfx |
OLD | NEW |