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

Side by Side Diff: ui/gfx/text_elider.cc

Issue 1428593004: Enable url_formatter::ElideUrl and url_formatter::ElideHost for aura Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refer to bug Created 5 years, 1 month 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
« no previous file with comments | « components/url_formatter/url_formatter.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 21 matching lines...) Expand all
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(OS_IOS) 42 #if defined(OS_ANDROID) && !defined(USE_AURA) || 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
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(OS_IOS) 197 #if defined(OS_ANDROID) && !defined(USE_AURA) || 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
212 DCHECK_NE(behavior, FADE_TAIL); 198 DCHECK_NE(behavior, FADE_TAIL);
213 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || 199 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE ||
214 GetStringWidthF(text, font_list) <= available_pixel_width) { 200 GetStringWidthF(text, font_list) <= available_pixel_width) {
215 return text; 201 return text;
216 } 202 }
217 if (behavior == ELIDE_EMAIL) 203 if (behavior == ELIDE_EMAIL)
218 return ElideEmail(text, font_list, available_pixel_width); 204 return ElideEmail(text, font_list, available_pixel_width);
219 205
220 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); 206 const bool elide_in_middle = (behavior == ELIDE_MIDDLE);
221 const bool elide_at_beginning = (behavior == ELIDE_HEAD); 207 const bool elide_at_beginning = (behavior == ELIDE_HEAD);
(...skipping 23 matching lines...) Expand all
245 hi = guess - 1; 231 hi = guess - 1;
246 // Move back on the loop terminating condition when the guess is too wide. 232 // Move back on the loop terminating condition when the guess is too wide.
247 if (hi < lo) 233 if (hi < lo)
248 lo = hi; 234 lo = hi;
249 } else { 235 } else {
250 lo = guess + 1; 236 lo = guess + 1;
251 } 237 }
252 } 238 }
253 239
254 return cut; 240 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.
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();
255 #endif 256 #endif
256 } 257 }
257 258
258 bool ElideString(const base::string16& input, 259 bool ElideString(const base::string16& input,
259 int max_len, 260 int max_len,
260 base::string16* output) { 261 base::string16* output) {
261 DCHECK_GE(max_len, 0); 262 DCHECK_GE(max_len, 0);
262 if (static_cast<int>(input.length()) <= max_len) { 263 if (static_cast<int>(input.length()) <= max_len) {
263 output->assign(input); 264 output->assign(input);
264 return false; 265 return false;
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 // of, and we can fit at least one character of the word in the elided string. 810 // of, and we can fit at least one character of the word in the elided string.
810 // Do that rather than just returning an ellipsis. 811 // Do that rather than just returning an ellipsis.
811 if (word_break && (index != static_cast<int32_t>(length - 1))) 812 if (word_break && (index != static_cast<int32_t>(length - 1)))
812 return string.substr(0, length - 1) + kElideString; 813 return string.substr(0, length - 1) + kElideString;
813 814
814 // Trying to break after only whitespace, elide all of it. 815 // Trying to break after only whitespace, elide all of it.
815 return kElideString; 816 return kElideString;
816 } 817 }
817 818
818 } // namespace gfx 819 } // namespace gfx
OLDNEW
« no previous file with comments | « components/url_formatter/url_formatter.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698