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 the anonymous namespace. | 8 // in this file using helper classes in the anonymous namespace. |
9 | 9 |
10 #include "ui/base/text/text_elider.h" | 10 #include "ui/base/text/text_elider.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 if (available_pixel_width >= font.GetStringWidth(elided_path)) | 142 if (available_pixel_width >= font.GetStringWidth(elided_path)) |
143 return ElideText(elided_path + url_query, | 143 return ElideText(elided_path + url_query, |
144 font, available_pixel_width, ELIDE_AT_END); | 144 font, available_pixel_width, ELIDE_AT_END); |
145 } | 145 } |
146 | 146 |
147 return string16(); | 147 return string16(); |
148 } | 148 } |
149 | 149 |
150 } // namespace | 150 } // namespace |
151 | 151 |
152 // This function takes a GURL object and elides it. It returns a string | 152 string16 ElideEmail(const string16& email, |
153 // which composed of parts from subdomain, domain, path, filename and query. | 153 const gfx::Font& font, |
154 // A "..." is added automatically at the end if the elided string is bigger | 154 int available_pixel_width) { |
155 // than the available pixel width. For available pixel width = 0, a formatted, | 155 if (font.GetStringWidth(email) <= available_pixel_width) |
156 // but un-elided, string is returned. | 156 return email; |
157 // | 157 |
158 std::vector<string16> email_split; | |
159 base::SplitString(email, '@', &email_split); | |
160 DCHECK_EQ(email_split.size(), 2U); | |
161 string16 username = email_split.front(); | |
162 string16 domain = email_split.back(); | |
gab
2012/03/01 19:37:25
@asvitkine: I remember you said you prefer string1
asvitkine_google
2012/03/01 20:13:29
This is fine. The run time will be dominated by ca
| |
163 | |
164 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); | |
165 const int kEllipsisSize = font.GetStringWidth(kEllipsisUTF16); | |
asvitkine_google
2012/03/01 20:13:29
Please rename all the variables that end in Size o
| |
166 | |
167 // Ignore the @ symbol and add it back at the end. | |
168 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); | |
169 available_pixel_width -= font.GetStringWidth(kAtSignUTF16); | |
170 | |
171 // Reserve characters for each of: username ellipsis, @ symbol, and at least | |
172 // one character remaining in the username. | |
asvitkine_google
2012/03/01 20:13:29
I would comment this slightly differently - have t
| |
173 const int available_domain_size = available_pixel_width - | |
174 kEllipsisSize - | |
175 font.GetStringWidth(username.substr(0, 1)); | |
asvitkine_google
2012/03/01 20:13:29
Please use font.GetStringWidth(username.substr(0,
| |
176 const int full_username_size = font.GetStringWidth(username); | |
177 if (font.GetStringWidth(domain) > available_domain_size) { | |
178 const int desired_domain_size = | |
179 std::max(available_pixel_width - full_username_size, | |
asvitkine_google
2012/03/01 20:13:29
Please comment the logic behind this.
gab
2012/03/01 21:48:18
I was trying to be less verbose by leaving all the
| |
180 available_pixel_width / 2); | |
181 domain = ElideText(domain, font, desired_domain_size, ELIDE_IN_MIDDLE); | |
182 if (domain.length() <= 1U) | |
183 return kEllipsisUTF16; | |
184 } | |
185 | |
186 const int final_domain_size = font.GetStringWidth(domain); | |
187 if (full_username_size + final_domain_size > available_pixel_width) { | |
188 username = ElideText(username, | |
189 font, | |
190 available_pixel_width - final_domain_size, | |
191 ELIDE_AT_END); | |
192 if (username.length() <= 1U) | |
193 return kEllipsisUTF16; | |
194 } | |
195 | |
196 return username + kAtSignUTF16 + domain; | |
197 } | |
198 | |
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 199 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 200 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of |
160 // a rendered string is always the sum of the widths of its substrings. Also I | 201 // a rendered string is always the sum of the widths of its substrings. Also I |
161 // suspect it could be made simpler. | 202 // suspect it could be made simpler. |
162 string16 ElideUrl(const GURL& url, | 203 string16 ElideUrl(const GURL& url, |
163 const gfx::Font& font, | 204 const gfx::Font& font, |
164 int available_pixel_width, | 205 int available_pixel_width, |
165 const std::string& languages) { | 206 const std::string& languages) { |
166 // Get a formatted string and corresponding parsing of the url. | 207 // Get a formatted string and corresponding parsing of the url. |
167 url_parse::Parsed parsed; | 208 url_parse::Parsed parsed; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 427 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
387 } | 428 } |
388 | 429 |
389 int available_root_width = available_pixel_width - ext_width; | 430 int available_root_width = available_pixel_width - ext_width; |
390 string16 elided_name = | 431 string16 elided_name = |
391 ElideText(rootname, font, available_root_width, ELIDE_AT_END); | 432 ElideText(rootname, font, available_root_width, ELIDE_AT_END); |
392 elided_name += extension; | 433 elided_name += extension; |
393 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 434 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
394 } | 435 } |
395 | 436 |
396 // This function adds an ellipsis at the end of the text if the text | |
397 // does not fit the given pixel width. | |
398 string16 ElideText(const string16& text, | 437 string16 ElideText(const string16& text, |
399 const gfx::Font& font, | 438 const gfx::Font& font, |
400 int available_pixel_width, | 439 int available_pixel_width, |
401 ElideBehavior elide_behavior) { | 440 ElideBehavior elide_behavior) { |
402 if (text.empty()) | 441 if (text.empty()) |
403 return text; | 442 return text; |
404 | 443 |
405 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); | 444 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); |
406 | 445 |
407 const int current_text_pixel_width = font.GetStringWidth(text); | 446 const int current_text_pixel_width = font.GetStringWidth(text); |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1061 index = char_iterator.getIndex(); | 1100 index = char_iterator.getIndex(); |
1062 } else { | 1101 } else { |
1063 // String has leading whitespace, return the elide string. | 1102 // String has leading whitespace, return the elide string. |
1064 return kElideString; | 1103 return kElideString; |
1065 } | 1104 } |
1066 } | 1105 } |
1067 return string.substr(0, index) + kElideString; | 1106 return string.substr(0, index) + kElideString; |
1068 } | 1107 } |
1069 | 1108 |
1070 } // namespace ui | 1109 } // namespace ui |
OLD | NEW |