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 // Ignore the @ symbol and add it back at the end. | |
asvitkine_google
2012/03/01 22:10:56
Nit: Can you reword this better? The second part o
| |
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 the other half, | |
179 // let the domain take that extra space. | |
asvitkine_google
2012/03/01 22:10:56
If the username is short, shouldn't you elide the
gab
2012/03/01 23:03:46
Yes, this is exactly what this does. I reworded th
| |
180 const int desired_domain_width = | |
181 std::max(available_pixel_width - full_username_width, | |
182 available_pixel_width / 2); | |
183 domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE); | |
184 if (domain.length() <= 1U) | |
185 return kEllipsisUTF16; | |
186 } | |
187 | |
188 const int final_domain_width = font.GetStringWidth(domain); | |
asvitkine_google
2012/03/01 22:10:56
Nit: How about:
available_pixel_width -= font.Get
gab
2012/03/01 23:03:46
I like that :)!
| |
189 if (full_username_width + final_domain_width > available_pixel_width) { | |
190 username = ElideText(username, | |
191 font, | |
192 available_pixel_width - final_domain_width, | |
193 ELIDE_AT_END); | |
194 if (username.length() <= 1U) | |
195 return kEllipsisUTF16; | |
196 } | |
197 | |
198 return username + kAtSignUTF16 + domain; | |
199 } | |
200 | |
158 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 201 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
159 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 202 // 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 | 203 // a rendered string is always the sum of the widths of its substrings. Also I |
161 // suspect it could be made simpler. | 204 // suspect it could be made simpler. |
162 string16 ElideUrl(const GURL& url, | 205 string16 ElideUrl(const GURL& url, |
163 const gfx::Font& font, | 206 const gfx::Font& font, |
164 int available_pixel_width, | 207 int available_pixel_width, |
165 const std::string& languages) { | 208 const std::string& languages) { |
166 // Get a formatted string and corresponding parsing of the url. | 209 // Get a formatted string and corresponding parsing of the url. |
167 url_parse::Parsed parsed; | 210 url_parse::Parsed parsed; |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
386 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 429 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
387 } | 430 } |
388 | 431 |
389 int available_root_width = available_pixel_width - ext_width; | 432 int available_root_width = available_pixel_width - ext_width; |
390 string16 elided_name = | 433 string16 elided_name = |
391 ElideText(rootname, font, available_root_width, ELIDE_AT_END); | 434 ElideText(rootname, font, available_root_width, ELIDE_AT_END); |
392 elided_name += extension; | 435 elided_name += extension; |
393 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 436 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
394 } | 437 } |
395 | 438 |
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, | 439 string16 ElideText(const string16& text, |
399 const gfx::Font& font, | 440 const gfx::Font& font, |
400 int available_pixel_width, | 441 int available_pixel_width, |
401 ElideBehavior elide_behavior) { | 442 ElideBehavior elide_behavior) { |
402 if (text.empty()) | 443 if (text.empty()) |
403 return text; | 444 return text; |
404 | 445 |
405 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); | 446 const string16 kEllipsisUTF16 = UTF8ToUTF16(kEllipsis); |
406 | 447 |
407 const int current_text_pixel_width = font.GetStringWidth(text); | 448 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(); | 1102 index = char_iterator.getIndex(); |
1062 } else { | 1103 } else { |
1063 // String has leading whitespace, return the elide string. | 1104 // String has leading whitespace, return the elide string. |
1064 return kElideString; | 1105 return kElideString; |
1065 } | 1106 } |
1066 } | 1107 } |
1067 return string.substr(0, index) + kElideString; | 1108 return string.substr(0, index) + kElideString; |
1068 } | 1109 } |
1069 | 1110 |
1070 } // namespace ui | 1111 } // namespace ui |
OLD | NEW |