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(); | |
163 | |
164 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); | |
165 | |
166 // Subtract the @ symbol from the available width as it is mandatory. | |
167 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); | |
168 available_pixel_width -= font.GetStringWidth(kAtSignUTF16); | |
169 | |
170 // Check whether eliding the domain is necessary: if eliding the username | |
171 // is sufficient, the domain will not be elided. | |
172 const int available_domain_width = | |
173 available_pixel_width - | |
174 font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16); | |
175 const int full_username_width = font.GetStringWidth(username); | |
176 if (font.GetStringWidth(domain) > available_domain_width) { | |
177 // Elide the domain so that it only takes half of the available width. | |
178 // Should the username not need all the width available in its half, the | |
179 // domain will occupy the leftover width. | |
180 const int desired_domain_width = | |
181 std::max(available_pixel_width - full_username_width, | |
182 available_pixel_width / 2); | |
Alexei Svitkine (slow)
2012/03/02 20:20:44
I think the "available_pixel_width / 2" is not str
gab
2012/03/02 21:57:37
Actually, if available_pixel_width/2 > available_d
asvitkine_google
2012/03/02 22:08:41
Okay. Please add a comment here explaining this.
| |
183 domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE); | |
184 if (domain.length() <= 1U) | |
185 return kEllipsisUTF16; | |
186 } | |
187 | |
188 // Fit the username in the remaining width. | |
189 available_pixel_width -= font.GetStringWidth(domain); | |
190 if (full_username_width > available_pixel_width) { | |
191 username = ElideText(username, | |
Alexei Svitkine (slow)
2012/03/02 20:20:44
I think this can fit on one line now.
gab
2012/03/02 21:57:37
Yay :)!
| |
192 font, | |
193 available_pixel_width, | |
194 ELIDE_AT_END); | |
195 if (username.length() <= 1U) | |
196 return kEllipsisUTF16; | |
197 } | |
198 | |
199 return username + kAtSignUTF16 + domain; | |
200 } | |
201 | |
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 202 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 203 // 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 | 204 // a rendered string is always the sum of the widths of its substrings. Also I |
161 // suspect it could be made simpler. | 205 // suspect it could be made simpler. |
162 string16 ElideUrl(const GURL& url, | 206 string16 ElideUrl(const GURL& url, |
163 const gfx::Font& font, | 207 const gfx::Font& font, |
164 int available_pixel_width, | 208 int available_pixel_width, |
165 const std::string& languages) { | 209 const std::string& languages) { |
166 // Get a formatted string and corresponding parsing of the url. | 210 // Get a formatted string and corresponding parsing of the url. |
167 url_parse::Parsed parsed; | 211 url_parse::Parsed parsed; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 430 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
387 } | 431 } |
388 | 432 |
389 int available_root_width = available_pixel_width - ext_width; | 433 int available_root_width = available_pixel_width - ext_width; |
390 string16 elided_name = | 434 string16 elided_name = |
391 ElideText(rootname, font, available_root_width, ELIDE_AT_END); | 435 ElideText(rootname, font, available_root_width, ELIDE_AT_END); |
392 elided_name += extension; | 436 elided_name += extension; |
393 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 437 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
394 } | 438 } |
395 | 439 |
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, | 440 string16 ElideText(const string16& text, |
399 const gfx::Font& font, | 441 const gfx::Font& font, |
400 int available_pixel_width, | 442 int available_pixel_width, |
401 ElideBehavior elide_behavior) { | 443 ElideBehavior elide_behavior) { |
402 if (text.empty()) | 444 if (text.empty()) |
403 return text; | 445 return text; |
404 | 446 |
405 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); | 447 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); |
406 | 448 |
407 const int current_text_pixel_width = font.GetStringWidth(text); | 449 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(); | 1103 index = char_iterator.getIndex(); |
1062 } else { | 1104 } else { |
1063 // String has leading whitespace, return the elide string. | 1105 // String has leading whitespace, return the elide string. |
1064 return kElideString; | 1106 return kElideString; |
1065 } | 1107 } |
1066 } | 1108 } |
1067 return string.substr(0, index) + kElideString; | 1109 return string.substr(0, index) + kElideString; |
1068 } | 1110 } |
1069 | 1111 |
1070 } // namespace ui | 1112 } // namespace ui |
OLD | NEW |