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

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

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync Created 7 years, 2 months 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 | Annotate | Revision Log
« no previous file with comments | « ui/gfx/text_elider.h ('k') | ui/gfx/text_elider_unittest.cc » ('j') | 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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 return path; 122 return path;
123 } 123 }
124 124
125 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path 125 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path
126 // components and elides if possible. Returns a string containing the longest 126 // components and elides if possible. Returns a string containing the longest
127 // possible elided path, or an empty string if elision is not possible. 127 // possible elided path, or an empty string if elision is not possible.
128 string16 ElideComponentizedPath(const string16& url_path_prefix, 128 string16 ElideComponentizedPath(const string16& url_path_prefix,
129 const std::vector<string16>& url_path_elements, 129 const std::vector<string16>& url_path_elements,
130 const string16& url_filename, 130 const string16& url_filename,
131 const string16& url_query, 131 const string16& url_query,
132 const gfx::FontList& font_list, 132 const FontList& font_list,
133 int available_pixel_width) { 133 float available_pixel_width) {
134 const size_t url_path_number_of_elements = url_path_elements.size(); 134 const size_t url_path_number_of_elements = url_path_elements.size();
135 135
136 CHECK(url_path_number_of_elements); 136 CHECK(url_path_number_of_elements);
137 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { 137 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) {
138 string16 elided_path = BuildPathFromComponents(url_path_prefix, 138 string16 elided_path = BuildPathFromComponents(url_path_prefix,
139 url_path_elements, url_filename, i); 139 url_path_elements, url_filename, i);
140 if (available_pixel_width >= gfx::GetStringWidth(elided_path, font_list)) 140 if (available_pixel_width >= GetStringWidthF(elided_path, font_list))
141 return ElideText(elided_path + url_query, font_list, 141 return ElideText(elided_path + url_query, font_list,
142 available_pixel_width, ELIDE_AT_END); 142 available_pixel_width, ELIDE_AT_END);
143 } 143 }
144 144
145 return string16(); 145 return string16();
146 } 146 }
147 147
148 } // namespace 148 } // namespace
149 149
150 string16 ElideEmail(const string16& email, 150 string16 ElideEmail(const string16& email,
151 const gfx::FontList& font_list, 151 const FontList& font_list,
152 int available_pixel_width) { 152 float available_pixel_width) {
153 if (gfx::GetStringWidth(email, font_list) <= available_pixel_width) 153 if (GetStringWidthF(email, font_list) <= available_pixel_width)
154 return email; 154 return email;
155 155
156 // Split the email into its local-part (username) and domain-part. The email 156 // Split the email into its local-part (username) and domain-part. The email
157 // spec technically allows for @ symbols in the local-part (username) of the 157 // spec technically allows for @ symbols in the local-part (username) of the
158 // email under some special requirements. It is guaranteed that there is no @ 158 // email under some special requirements. It is guaranteed that there is no @
159 // symbol in the domain part of the email however so splitting at the last @ 159 // symbol in the domain part of the email however so splitting at the last @
160 // symbol is safe. 160 // symbol is safe.
161 const size_t split_index = email.find_last_of('@'); 161 const size_t split_index = email.find_last_of('@');
162 DCHECK_NE(split_index, string16::npos); 162 DCHECK_NE(split_index, string16::npos);
163 string16 username = email.substr(0, split_index); 163 string16 username = email.substr(0, split_index);
164 string16 domain = email.substr(split_index + 1); 164 string16 domain = email.substr(split_index + 1);
165 DCHECK(!username.empty()); 165 DCHECK(!username.empty());
166 DCHECK(!domain.empty()); 166 DCHECK(!domain.empty());
167 167
168 // Subtract the @ symbol from the available width as it is mandatory. 168 // Subtract the @ symbol from the available width as it is mandatory.
169 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); 169 const string16 kAtSignUTF16 = ASCIIToUTF16("@");
170 available_pixel_width -= gfx::GetStringWidth(kAtSignUTF16, font_list); 170 available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list);
171 171
172 // Check whether eliding the domain is necessary: if eliding the username 172 // Check whether eliding the domain is necessary: if eliding the username
173 // is sufficient, the domain will not be elided. 173 // is sufficient, the domain will not be elided.
174 const int full_username_width = gfx::GetStringWidth(username, font_list); 174 const float full_username_width = GetStringWidthF(username, font_list);
175 const int available_domain_width = 175 const float available_domain_width =
176 available_pixel_width - 176 available_pixel_width -
177 std::min(full_username_width, 177 std::min(full_username_width,
178 gfx::GetStringWidth(username.substr(0, 1) + kEllipsisUTF16, 178 GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16,
179 font_list)); 179 font_list));
180 if (gfx::GetStringWidth(domain, font_list) > available_domain_width) { 180 if (GetStringWidthF(domain, font_list) > available_domain_width) {
181 // Elide the domain so that it only takes half of the available width. 181 // Elide the domain so that it only takes half of the available width.
182 // Should the username not need all the width available in its half, the 182 // Should the username not need all the width available in its half, the
183 // domain will occupy the leftover width. 183 // domain will occupy the leftover width.
184 // If |desired_domain_width| is greater than |available_domain_width|: the 184 // If |desired_domain_width| is greater than |available_domain_width|: the
185 // minimal username elision allowed by the specifications will not fit; thus 185 // minimal username elision allowed by the specifications will not fit; thus
186 // |desired_domain_width| must be <= |available_domain_width| at all cost. 186 // |desired_domain_width| must be <= |available_domain_width| at all cost.
187 const int desired_domain_width = 187 const float desired_domain_width =
188 std::min(available_domain_width, 188 std::min(available_domain_width,
189 std::max(available_pixel_width - full_username_width, 189 std::max(available_pixel_width - full_username_width,
190 available_pixel_width / 2)); 190 available_pixel_width / 2));
191 domain = ElideText(domain, font_list, desired_domain_width, 191 domain = ElideText(domain, font_list, desired_domain_width,
192 ELIDE_IN_MIDDLE); 192 ELIDE_IN_MIDDLE);
193 // Failing to elide the domain such that at least one character remains 193 // Failing to elide the domain such that at least one character remains
194 // (other than the ellipsis itself) remains: return a single ellipsis. 194 // (other than the ellipsis itself) remains: return a single ellipsis.
195 if (domain.length() <= 1U) 195 if (domain.length() <= 1U)
196 return string16(kEllipsisUTF16); 196 return string16(kEllipsisUTF16);
197 } 197 }
198 198
199 // Fit the username in the remaining width (at this point the elided username 199 // Fit the username in the remaining width (at this point the elided username
200 // is guaranteed to fit with at least one character remaining given all the 200 // is guaranteed to fit with at least one character remaining given all the
201 // precautions taken earlier). 201 // precautions taken earlier).
202 available_pixel_width -= gfx::GetStringWidth(domain, font_list); 202 available_pixel_width -= GetStringWidthF(domain, font_list);
203 username = ElideText(username, font_list, available_pixel_width, 203 username = ElideText(username, font_list, available_pixel_width,
204 ELIDE_AT_END); 204 ELIDE_AT_END);
205 205
206 return username + kAtSignUTF16 + domain; 206 return username + kAtSignUTF16 + domain;
207 } 207 }
208 208
209 string16 ElideEmail(const string16& email, 209 string16 ElideEmail(const string16& email,
210 const gfx::Font& font, 210 const Font& font,
211 int available_pixel_width) { 211 float available_pixel_width) {
212 return ElideEmail(email, gfx::FontList(font), available_pixel_width); 212 return ElideEmail(email, FontList(font), available_pixel_width);
213 } 213 }
214 214
215 // TODO(pkasting): http://crbug.com/77883 This whole function gets 215 // TODO(pkasting): http://crbug.com/77883 This whole function gets
216 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of 216 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of
217 // a rendered string is always the sum of the widths of its substrings. Also I 217 // a rendered string is always the sum of the widths of its substrings. Also I
218 // suspect it could be made simpler. 218 // suspect it could be made simpler.
219 string16 ElideUrl(const GURL& url, 219 string16 ElideUrl(const GURL& url,
220 const gfx::FontList& font_list, 220 const FontList& font_list,
221 int available_pixel_width, 221 float available_pixel_width,
222 const std::string& languages) { 222 const std::string& languages) {
223 // Get a formatted string and corresponding parsing of the url. 223 // Get a formatted string and corresponding parsing of the url.
224 url_parse::Parsed parsed; 224 url_parse::Parsed parsed;
225 const string16 url_string = 225 const string16 url_string =
226 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, 226 net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
227 net::UnescapeRule::SPACES, &parsed, NULL, NULL); 227 net::UnescapeRule::SPACES, &parsed, NULL, NULL);
228 if (available_pixel_width <= 0) 228 if (available_pixel_width <= 0)
229 return url_string; 229 return url_string;
230 230
231 // If non-standard, return plain eliding. 231 // If non-standard, return plain eliding.
232 if (!url.IsStandard()) 232 if (!url.IsStandard())
233 return ElideText(url_string, font_list, available_pixel_width, 233 return ElideText(url_string, font_list, available_pixel_width,
234 ELIDE_AT_END); 234 ELIDE_AT_END);
235 235
236 // Now start eliding url_string to fit within available pixel width. 236 // Now start eliding url_string to fit within available pixel width.
237 // Fist pass - check to see whether entire url_string fits. 237 // Fist pass - check to see whether entire url_string fits.
238 const int pixel_width_url_string = gfx::GetStringWidth(url_string, font_list); 238 const float pixel_width_url_string = GetStringWidthF(url_string, font_list);
239 if (available_pixel_width >= pixel_width_url_string) 239 if (available_pixel_width >= pixel_width_url_string)
240 return url_string; 240 return url_string;
241 241
242 // Get the path substring, including query and reference. 242 // Get the path substring, including query and reference.
243 const size_t path_start_index = parsed.path.begin; 243 const size_t path_start_index = parsed.path.begin;
244 const size_t path_len = parsed.path.len; 244 const size_t path_len = parsed.path.len;
245 string16 url_path_query_etc = url_string.substr(path_start_index); 245 string16 url_path_query_etc = url_string.substr(path_start_index);
246 string16 url_path = url_string.substr(path_start_index, path_len); 246 string16 url_path = url_string.substr(path_start_index, path_len);
247 247
248 // Return general elided text if url minus the query fits. 248 // Return general elided text if url minus the query fits.
249 const string16 url_minus_query = 249 const string16 url_minus_query =
250 url_string.substr(0, path_start_index + path_len); 250 url_string.substr(0, path_start_index + path_len);
251 if (available_pixel_width >= gfx::GetStringWidth(url_minus_query, font_list)) 251 if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list))
252 return ElideText(url_string, font_list, available_pixel_width, 252 return ElideText(url_string, font_list, available_pixel_width,
253 ELIDE_AT_END); 253 ELIDE_AT_END);
254 254
255 // Get Host. 255 // Get Host.
256 string16 url_host = UTF8ToUTF16(url.host()); 256 string16 url_host = UTF8ToUTF16(url.host());
257 257
258 // Get domain and registry information from the URL. 258 // Get domain and registry information from the URL.
259 string16 url_domain = UTF8ToUTF16( 259 string16 url_domain = UTF8ToUTF16(
260 net::registry_controlled_domains::GetDomainAndRegistry( 260 net::registry_controlled_domains::GetDomainAndRegistry(
261 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); 261 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES));
(...skipping 29 matching lines...) Expand all
291 url_domain.clear(); 291 url_domain.clear();
292 url_subdomain.clear(); 292 url_subdomain.clear();
293 293
294 const string16 kColon = UTF8ToUTF16(":"); 294 const string16 kColon = UTF8ToUTF16(":");
295 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; 295 url_host = url_domain = file_path_split.at(0).substr(1) + kColon;
296 url_path_query_etc = url_path = file_path_split.at(1); 296 url_path_query_etc = url_path = file_path_split.at(1);
297 } 297 }
298 } 298 }
299 299
300 // Second Pass - remove scheme - the rest fits. 300 // Second Pass - remove scheme - the rest fits.
301 const int pixel_width_url_host = gfx::GetStringWidth(url_host, font_list); 301 const float pixel_width_url_host = GetStringWidthF(url_host, font_list);
302 const int pixel_width_url_path = gfx::GetStringWidth(url_path_query_etc, 302 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc,
303 font_list); 303 font_list);
304 if (available_pixel_width >= 304 if (available_pixel_width >=
305 pixel_width_url_host + pixel_width_url_path) 305 pixel_width_url_host + pixel_width_url_path)
306 return url_host + url_path_query_etc; 306 return url_host + url_path_query_etc;
307 307
308 // Third Pass: Subdomain, domain and entire path fits. 308 // Third Pass: Subdomain, domain and entire path fits.
309 const int pixel_width_url_domain = gfx::GetStringWidth(url_domain, font_list); 309 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list);
310 const int pixel_width_url_subdomain = gfx::GetStringWidth(url_subdomain, 310 const float pixel_width_url_subdomain =
311 font_list); 311 GetStringWidthF(url_subdomain, font_list);
312 if (available_pixel_width >= 312 if (available_pixel_width >=
313 pixel_width_url_subdomain + pixel_width_url_domain + 313 pixel_width_url_subdomain + pixel_width_url_domain +
314 pixel_width_url_path) 314 pixel_width_url_path)
315 return url_subdomain + url_domain + url_path_query_etc; 315 return url_subdomain + url_domain + url_path_query_etc;
316 316
317 // Query element. 317 // Query element.
318 string16 url_query; 318 string16 url_query;
319 const int kPixelWidthDotsTrailer = gfx::GetStringWidth( 319 const float kPixelWidthDotsTrailer = GetStringWidthF(
320 string16(kEllipsisUTF16), font_list); 320 string16(kEllipsisUTF16), font_list);
321 if (parsed.query.is_nonempty()) { 321 if (parsed.query.is_nonempty()) {
322 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); 322 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin);
323 if (available_pixel_width >= 323 if (available_pixel_width >=
324 (pixel_width_url_subdomain + pixel_width_url_domain + 324 (pixel_width_url_subdomain + pixel_width_url_domain +
325 pixel_width_url_path - gfx::GetStringWidth(url_query, font_list))) { 325 pixel_width_url_path - GetStringWidthF(url_query, font_list))) {
326 return ElideText(url_subdomain + url_domain + url_path_query_etc, 326 return ElideText(url_subdomain + url_domain + url_path_query_etc,
327 font_list, available_pixel_width, ELIDE_AT_END); 327 font_list, available_pixel_width, ELIDE_AT_END);
328 } 328 }
329 } 329 }
330 330
331 // Parse url_path using '/'. 331 // Parse url_path using '/'.
332 std::vector<string16> url_path_elements; 332 std::vector<string16> url_path_elements;
333 base::SplitString(url_path, kForwardSlash, &url_path_elements); 333 base::SplitString(url_path, kForwardSlash, &url_path_elements);
334 334
335 // Get filename - note that for a path ending with / 335 // Get filename - note that for a path ending with /
(...skipping 14 matching lines...) Expand all
350 if (url_path_number_of_elements <= 1 || 350 if (url_path_number_of_elements <= 1 ||
351 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { 351 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) {
352 // No path to elide, or too long of a path (could overflow in loop below) 352 // No path to elide, or too long of a path (could overflow in loop below)
353 // Just elide this as a text string. 353 // Just elide this as a text string.
354 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, 354 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list,
355 available_pixel_width, ELIDE_AT_END); 355 available_pixel_width, ELIDE_AT_END);
356 } 356 }
357 357
358 // Start eliding the path and replacing elements by ".../". 358 // Start eliding the path and replacing elements by ".../".
359 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; 359 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash;
360 const int pixel_width_ellipsis_slash = gfx::GetStringWidth(kEllipsisAndSlash, 360 const float pixel_width_ellipsis_slash =
361 font_list); 361 GetStringWidthF(kEllipsisAndSlash, font_list);
362 362
363 // Check with both subdomain and domain. 363 // Check with both subdomain and domain.
364 string16 elided_path = 364 string16 elided_path =
365 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, 365 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements,
366 url_filename, url_query, font_list, 366 url_filename, url_query, font_list,
367 available_pixel_width); 367 available_pixel_width);
368 if (!elided_path.empty()) 368 if (!elided_path.empty())
369 return elided_path; 369 return elided_path;
370 370
371 // Check with only domain. 371 // Check with only domain.
(...skipping 11 matching lines...) Expand all
383 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, 383 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements,
384 url_filename, url_query, font_list, 384 url_filename, url_query, font_list,
385 available_pixel_width); 385 available_pixel_width);
386 386
387 if (!elided_path.empty()) 387 if (!elided_path.empty())
388 return elided_path; 388 return elided_path;
389 } 389 }
390 390
391 // Return elided domain/.../filename anyway. 391 // Return elided domain/.../filename anyway.
392 string16 final_elided_url_string(url_elided_domain); 392 string16 final_elided_url_string(url_elided_domain);
393 const int url_elided_domain_width = gfx::GetStringWidth(url_elided_domain, 393 const float url_elided_domain_width = GetStringWidthF(url_elided_domain,
394 font_list); 394 font_list);
395 395
396 // A hack to prevent trailing ".../...". 396 // A hack to prevent trailing ".../...".
397 if ((available_pixel_width - url_elided_domain_width) > 397 if ((available_pixel_width - url_elided_domain_width) >
398 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + 398 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer +
399 gfx::GetStringWidth(ASCIIToUTF16("UV"), font_list)) { 399 GetStringWidthF(ASCIIToUTF16("UV"), font_list)) {
400 final_elided_url_string += BuildPathFromComponents(string16(), 400 final_elided_url_string += BuildPathFromComponents(string16(),
401 url_path_elements, url_filename, 1); 401 url_path_elements, url_filename, 1);
402 } else { 402 } else {
403 final_elided_url_string += url_path; 403 final_elided_url_string += url_path;
404 } 404 }
405 405
406 return ElideText(final_elided_url_string, font_list, available_pixel_width, 406 return ElideText(final_elided_url_string, font_list, available_pixel_width,
407 ELIDE_AT_END); 407 ELIDE_AT_END);
408 } 408 }
409 409
410 string16 ElideUrl(const GURL& url, 410 string16 ElideUrl(const GURL& url,
411 const gfx::Font& font, 411 const Font& font,
412 int available_pixel_width, 412 float available_pixel_width,
413 const std::string& languages) { 413 const std::string& languages) {
414 return ElideUrl(url, gfx::FontList(font), available_pixel_width, languages); 414 return ElideUrl(url, FontList(font), available_pixel_width, languages);
415 } 415 }
416 416
417 string16 ElideFilename(const base::FilePath& filename, 417 string16 ElideFilename(const base::FilePath& filename,
418 const gfx::FontList& font_list, 418 const FontList& font_list,
419 int available_pixel_width) { 419 float available_pixel_width) {
420 #if defined(OS_WIN) 420 #if defined(OS_WIN)
421 string16 filename_utf16 = filename.value(); 421 string16 filename_utf16 = filename.value();
422 string16 extension = filename.Extension(); 422 string16 extension = filename.Extension();
423 string16 rootname = filename.BaseName().RemoveExtension().value(); 423 string16 rootname = filename.BaseName().RemoveExtension().value();
424 #elif defined(OS_POSIX) 424 #elif defined(OS_POSIX)
425 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( 425 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide(
426 filename.value())); 426 filename.value()));
427 string16 extension = WideToUTF16(base::SysNativeMBToWide( 427 string16 extension = WideToUTF16(base::SysNativeMBToWide(
428 filename.Extension())); 428 filename.Extension()));
429 string16 rootname = WideToUTF16(base::SysNativeMBToWide( 429 string16 rootname = WideToUTF16(base::SysNativeMBToWide(
430 filename.BaseName().RemoveExtension().value())); 430 filename.BaseName().RemoveExtension().value()));
431 #endif 431 #endif
432 432
433 const int full_width = gfx::GetStringWidth(filename_utf16, font_list); 433 const float full_width = GetStringWidthF(filename_utf16, font_list);
434 if (full_width <= available_pixel_width) 434 if (full_width <= available_pixel_width)
435 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); 435 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16);
436 436
437 if (rootname.empty() || extension.empty()) { 437 if (rootname.empty() || extension.empty()) {
438 const string16 elided_name = ElideText(filename_utf16, font_list, 438 const string16 elided_name = ElideText(filename_utf16, font_list,
439 available_pixel_width, ELIDE_AT_END); 439 available_pixel_width, ELIDE_AT_END);
440 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 440 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
441 } 441 }
442 442
443 const int ext_width = gfx::GetStringWidth(extension, font_list); 443 const float ext_width = GetStringWidthF(extension, font_list);
444 const int root_width = gfx::GetStringWidth(rootname, font_list); 444 const float root_width = GetStringWidthF(rootname, font_list);
445 445
446 // We may have trimmed the path. 446 // We may have trimmed the path.
447 if (root_width + ext_width <= available_pixel_width) { 447 if (root_width + ext_width <= available_pixel_width) {
448 const string16 elided_name = rootname + extension; 448 const string16 elided_name = rootname + extension;
449 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 449 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
450 } 450 }
451 451
452 if (ext_width >= available_pixel_width) { 452 if (ext_width >= available_pixel_width) {
453 const string16 elided_name = ElideText(rootname + extension, font_list, 453 const string16 elided_name = ElideText(rootname + extension, font_list,
454 available_pixel_width, 454 available_pixel_width,
455 ELIDE_IN_MIDDLE); 455 ELIDE_IN_MIDDLE);
456 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 456 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
457 } 457 }
458 458
459 int available_root_width = available_pixel_width - ext_width; 459 float available_root_width = available_pixel_width - ext_width;
460 string16 elided_name = 460 string16 elided_name =
461 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); 461 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END);
462 elided_name += extension; 462 elided_name += extension;
463 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); 463 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
464 } 464 }
465 465
466 string16 ElideFilename(const base::FilePath& filename, 466 string16 ElideFilename(const base::FilePath& filename,
467 const gfx::Font& font, 467 const Font& font,
468 int available_pixel_width) { 468 float available_pixel_width) {
469 return ElideFilename(filename, gfx::FontList(font), available_pixel_width); 469 return ElideFilename(filename, FontList(font), available_pixel_width);
470 } 470 }
471 471
472 string16 ElideText(const string16& text, 472 string16 ElideText(const string16& text,
473 const gfx::FontList& font_list, 473 const FontList& font_list,
474 int available_pixel_width, 474 float available_pixel_width,
475 ElideBehavior elide_behavior) { 475 ElideBehavior elide_behavior) {
476 if (text.empty()) 476 if (text.empty())
477 return text; 477 return text;
478 478
479 const int current_text_pixel_width = gfx::GetStringWidth(text, font_list); 479 const float current_text_pixel_width = GetStringWidthF(text, font_list);
480 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); 480 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE);
481 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); 481 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END);
482 482
483 const string16 ellipsis = string16(kEllipsisUTF16); 483 const string16 ellipsis = string16(kEllipsisUTF16);
484 StringSlicer slicer(text, ellipsis, elide_in_middle); 484 StringSlicer slicer(text, ellipsis, elide_in_middle);
485 485
486 // Pango will return 0 width for absurdly long strings. Cut the string in 486 // Pango will return 0 width for absurdly long strings. Cut the string in
487 // half and try again. 487 // half and try again.
488 // This is caused by an int overflow in Pango (specifically, in 488 // This is caused by an int overflow in Pango (specifically, in
489 // pango_glyph_string_extents_range). It's actually more subtle than just 489 // pango_glyph_string_extents_range). It's actually more subtle than just
490 // returning 0, since on super absurdly long strings, the int can wrap and 490 // returning 0, since on super absurdly long strings, the int can wrap and
491 // return positive numbers again. Detecting that is probably not worth it 491 // return positive numbers again. Detecting that is probably not worth it
492 // (eliding way too much from a ridiculous string is probably still 492 // (eliding way too much from a ridiculous string is probably still
493 // ridiculous), but we should check other widths for bogus values as well. 493 // ridiculous), but we should check other widths for bogus values as well.
494 if (current_text_pixel_width <= 0 && !text.empty()) { 494 if (current_text_pixel_width <= 0 && !text.empty()) {
495 const string16 cut = slicer.CutString(text.length() / 2, false); 495 const string16 cut = slicer.CutString(text.length() / 2, false);
496 return ElideText(cut, font_list, available_pixel_width, elide_behavior); 496 return ElideText(cut, font_list, available_pixel_width, elide_behavior);
497 } 497 }
498 498
499 if (current_text_pixel_width <= available_pixel_width) 499 if (current_text_pixel_width <= available_pixel_width)
500 return text; 500 return text;
501 501
502 if (insert_ellipsis && 502 if (insert_ellipsis &&
503 gfx::GetStringWidth(ellipsis, font_list) > available_pixel_width) 503 GetStringWidthF(ellipsis, font_list) > available_pixel_width)
504 return string16(); 504 return string16();
505 505
506 // Use binary search to compute the elided text. 506 // Use binary search to compute the elided text.
507 size_t lo = 0; 507 size_t lo = 0;
508 size_t hi = text.length() - 1; 508 size_t hi = text.length() - 1;
509 size_t guess; 509 size_t guess;
510 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { 510 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) {
511 // We check the length of the whole desired string at once to ensure we 511 // We check the length of the whole desired string at once to ensure we
512 // handle kerning/ligatures/etc. correctly. 512 // handle kerning/ligatures/etc. correctly.
513 const string16 cut = slicer.CutString(guess, insert_ellipsis); 513 const string16 cut = slicer.CutString(guess, insert_ellipsis);
514 const int guess_length = gfx::GetStringWidth(cut, font_list); 514 const float guess_length = GetStringWidthF(cut, font_list);
515 // Check again that we didn't hit a Pango width overflow. If so, cut the 515 // Check again that we didn't hit a Pango width overflow. If so, cut the
516 // current string in half and start over. 516 // current string in half and start over.
517 if (guess_length <= 0) { 517 if (guess_length <= 0) {
518 return ElideText(slicer.CutString(guess / 2, false), 518 return ElideText(slicer.CutString(guess / 2, false),
519 font_list, available_pixel_width, elide_behavior); 519 font_list, available_pixel_width, elide_behavior);
520 } 520 }
521 if (guess_length > available_pixel_width) 521 if (guess_length > available_pixel_width)
522 hi = guess - 1; 522 hi = guess - 1;
523 else 523 else
524 lo = guess + 1; 524 lo = guess + 1;
525 } 525 }
526 526
527 return slicer.CutString(guess, insert_ellipsis); 527 return slicer.CutString(guess, insert_ellipsis);
528 } 528 }
529 529
530 string16 ElideText(const string16& text, 530 string16 ElideText(const string16& text,
531 const gfx::Font& font, 531 const Font& font,
532 int available_pixel_width, 532 float available_pixel_width,
533 ElideBehavior elide_behavior) { 533 ElideBehavior elide_behavior) {
534 return ElideText(text, gfx::FontList(font), available_pixel_width, 534 return ElideText(text, FontList(font), available_pixel_width, elide_behavior);
535 elide_behavior);
536 } 535 }
537 536
538 SortedDisplayURL::SortedDisplayURL(const GURL& url, 537 SortedDisplayURL::SortedDisplayURL(const GURL& url,
539 const std::string& languages) { 538 const std::string& languages) {
540 net::AppendFormattedHost(url, languages, &sort_host_); 539 net::AppendFormattedHost(url, languages, &sort_host_);
541 string16 host_minus_www = net::StripWWW(sort_host_); 540 string16 host_minus_www = net::StripWWW(sort_host_);
542 url_parse::Parsed parsed; 541 url_parse::Parsed parsed;
543 display_url_ = 542 display_url_ =
544 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, 543 net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
545 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); 544 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL);
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 } 801 }
803 ++current_row_; 802 ++current_row_;
804 current_col_ = 0; 803 current_col_ = 0;
805 } 804 }
806 805
807 // Internal class used to track progress of a rectangular text elide 806 // Internal class used to track progress of a rectangular text elide
808 // operation. Exists so the top-level ElideRectangleText() function 807 // operation. Exists so the top-level ElideRectangleText() function
809 // can be broken into smaller methods sharing this state. 808 // can be broken into smaller methods sharing this state.
810 class RectangleText { 809 class RectangleText {
811 public: 810 public:
812 RectangleText(const gfx::FontList& font_list, 811 RectangleText(const FontList& font_list,
813 int available_pixel_width, 812 float available_pixel_width,
814 int available_pixel_height, 813 int available_pixel_height,
815 WordWrapBehavior wrap_behavior, 814 WordWrapBehavior wrap_behavior,
816 std::vector<string16>* lines) 815 std::vector<string16>* lines)
817 : font_list_(font_list), 816 : font_list_(font_list),
818 line_height_(font_list.GetHeight()), 817 line_height_(font_list.GetHeight()),
819 available_pixel_width_(available_pixel_width), 818 available_pixel_width_(available_pixel_width),
820 available_pixel_height_(available_pixel_height), 819 available_pixel_height_(available_pixel_height),
821 wrap_behavior_(wrap_behavior), 820 wrap_behavior_(wrap_behavior),
822 current_width_(0), 821 current_width_(0),
823 current_height_(0), 822 current_height_(0),
(...skipping 28 matching lines...) Expand all
852 851
853 // Add a long word - wrapping, eliding or truncating per the wrap behavior. 852 // Add a long word - wrapping, eliding or truncating per the wrap behavior.
854 int AddWordOverflow(const string16& word); 853 int AddWordOverflow(const string16& word);
855 854
856 // Add a word to the rectangluar region at the current position. 855 // Add a word to the rectangluar region at the current position.
857 int AddWord(const string16& word); 856 int AddWord(const string16& word);
858 857
859 // Append the specified |text| to the current output line, incrementing the 858 // Append the specified |text| to the current output line, incrementing the
860 // running width by the specified amount. This is an optimization over 859 // running width by the specified amount. This is an optimization over
861 // |AddToCurrentLine()| when |text_width| is already known. 860 // |AddToCurrentLine()| when |text_width| is already known.
862 void AddToCurrentLineWithWidth(const string16& text, int text_width); 861 void AddToCurrentLineWithWidth(const string16& text, float text_width);
863 862
864 // Append the specified |text| to the current output line. 863 // Append the specified |text| to the current output line.
865 void AddToCurrentLine(const string16& text); 864 void AddToCurrentLine(const string16& text);
866 865
867 // Set the current position to the beginning of the next line. 866 // Set the current position to the beginning of the next line.
868 bool NewLine(); 867 bool NewLine();
869 868
870 // The font list used for measuring text width. 869 // The font list used for measuring text width.
871 const gfx::FontList& font_list_; 870 const FontList& font_list_;
872 871
873 // The height of each line of text. 872 // The height of each line of text.
874 const int line_height_; 873 const int line_height_;
875 874
876 // The number of pixels of available width in the rectangle. 875 // The number of pixels of available width in the rectangle.
877 const int available_pixel_width_; 876 const float available_pixel_width_;
878 877
879 // The number of pixels of available height in the rectangle. 878 // The number of pixels of available height in the rectangle.
880 const int available_pixel_height_; 879 const int available_pixel_height_;
881 880
882 // The wrap behavior for words that are too long to fit on a single line. 881 // The wrap behavior for words that are too long to fit on a single line.
883 const WordWrapBehavior wrap_behavior_; 882 const WordWrapBehavior wrap_behavior_;
884 883
885 // The current running width. 884 // The current running width.
886 int current_width_; 885 float current_width_;
887 886
888 // The current running height. 887 // The current running height.
889 int current_height_; 888 int current_height_;
890 889
891 // The current line of text. 890 // The current line of text.
892 string16 current_line_; 891 string16 current_line_;
893 892
894 // Indicates whether the last line ended with \n. 893 // Indicates whether the last line ended with \n.
895 bool last_line_ended_in_lf_; 894 bool last_line_ended_in_lf_;
896 895
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 if (lines_->back().empty() && !last_line_ended_in_lf_) 933 if (lines_->back().empty() && !last_line_ended_in_lf_)
935 lines_->pop_back(); 934 lines_->pop_back();
936 } 935 }
937 if (last_line_ended_in_lf_) 936 if (last_line_ended_in_lf_)
938 lines_->push_back(string16()); 937 lines_->push_back(string16());
939 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | 938 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) |
940 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); 939 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0);
941 } 940 }
942 941
943 void RectangleText::AddLine(const string16& line) { 942 void RectangleText::AddLine(const string16& line) {
944 const int line_width = gfx::GetStringWidth(line, font_list_); 943 const float line_width = GetStringWidthF(line, font_list_);
945 if (line_width <= available_pixel_width_) { 944 if (line_width <= available_pixel_width_) {
946 AddToCurrentLineWithWidth(line, line_width); 945 AddToCurrentLineWithWidth(line, line_width);
947 } else { 946 } else {
948 // Iterate over positions that are valid to break the line at. In general, 947 // Iterate over positions that are valid to break the line at. In general,
949 // these are word boundaries but after any punctuation following the word. 948 // these are word boundaries but after any punctuation following the word.
950 base::i18n::BreakIterator words(line, 949 base::i18n::BreakIterator words(line,
951 base::i18n::BreakIterator::BREAK_LINE); 950 base::i18n::BreakIterator::BREAK_LINE);
952 if (words.Init()) { 951 if (words.Init()) {
953 while (words.Advance()) { 952 while (words.Advance()) {
954 const bool truncate = !current_line_.empty(); 953 const bool truncate = !current_line_.empty();
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 insufficient_width_ = true; 1020 insufficient_width_ = true;
1022 } 1021 }
1023 1022
1024 return lines_added; 1023 return lines_added;
1025 } 1024 }
1026 1025
1027 int RectangleText::AddWord(const string16& word) { 1026 int RectangleText::AddWord(const string16& word) {
1028 int lines_added = 0; 1027 int lines_added = 0;
1029 string16 trimmed; 1028 string16 trimmed;
1030 TrimWhitespace(word, TRIM_TRAILING, &trimmed); 1029 TrimWhitespace(word, TRIM_TRAILING, &trimmed);
1031 const int trimmed_width = gfx::GetStringWidth(trimmed, font_list_); 1030 const float trimmed_width = GetStringWidthF(trimmed, font_list_);
1032 if (trimmed_width <= available_pixel_width_) { 1031 if (trimmed_width <= available_pixel_width_) {
1033 // Word can be made to fit, no need to fragment it. 1032 // Word can be made to fit, no need to fragment it.
1034 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) 1033 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine())
1035 lines_added++; 1034 lines_added++;
1036 // Append the non-trimmed word, in case more words are added after. 1035 // Append the non-trimmed word, in case more words are added after.
1037 AddToCurrentLine(word); 1036 AddToCurrentLine(word);
1038 } else { 1037 } else {
1039 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? 1038 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ?
1040 trimmed : word); 1039 trimmed : word);
1041 } 1040 }
1042 return lines_added; 1041 return lines_added;
1043 } 1042 }
1044 1043
1045 void RectangleText::AddToCurrentLine(const string16& text) { 1044 void RectangleText::AddToCurrentLine(const string16& text) {
1046 AddToCurrentLineWithWidth(text, gfx::GetStringWidth(text, font_list_)); 1045 AddToCurrentLineWithWidth(text, GetStringWidthF(text, font_list_));
1047 } 1046 }
1048 1047
1049 void RectangleText::AddToCurrentLineWithWidth(const string16& text, 1048 void RectangleText::AddToCurrentLineWithWidth(const string16& text,
1050 int text_width) { 1049 float text_width) {
1051 if (current_height_ >= available_pixel_height_) { 1050 if (current_height_ >= available_pixel_height_) {
1052 insufficient_height_ = true; 1051 insufficient_height_ = true;
1053 return; 1052 return;
1054 } 1053 }
1055 current_line_.append(text); 1054 current_line_.append(text);
1056 current_width_ += text_width; 1055 current_width_ += text_width;
1057 } 1056 }
1058 1057
1059 bool RectangleText::NewLine() { 1058 bool RectangleText::NewLine() {
1060 bool line_added = false; 1059 bool line_added = false;
(...skipping 13 matching lines...) Expand all
1074 1073
1075 bool ElideRectangleString(const string16& input, size_t max_rows, 1074 bool ElideRectangleString(const string16& input, size_t max_rows,
1076 size_t max_cols, bool strict, string16* output) { 1075 size_t max_cols, bool strict, string16* output) {
1077 RectangleString rect(max_rows, max_cols, strict, output); 1076 RectangleString rect(max_rows, max_cols, strict, output);
1078 rect.Init(); 1077 rect.Init();
1079 rect.AddString(input); 1078 rect.AddString(input);
1080 return rect.Finalize(); 1079 return rect.Finalize();
1081 } 1080 }
1082 1081
1083 int ElideRectangleText(const string16& input, 1082 int ElideRectangleText(const string16& input,
1084 const gfx::FontList& font_list, 1083 const FontList& font_list,
1085 int available_pixel_width, 1084 float available_pixel_width,
1086 int available_pixel_height, 1085 int available_pixel_height,
1087 WordWrapBehavior wrap_behavior, 1086 WordWrapBehavior wrap_behavior,
1088 std::vector<string16>* lines) { 1087 std::vector<string16>* lines) {
1089 RectangleText rect(font_list, 1088 RectangleText rect(font_list,
1090 available_pixel_width, 1089 available_pixel_width,
1091 available_pixel_height, 1090 available_pixel_height,
1092 wrap_behavior, 1091 wrap_behavior,
1093 lines); 1092 lines);
1094 rect.Init(); 1093 rect.Init();
1095 rect.AddString(input); 1094 rect.AddString(input);
1096 return rect.Finalize(); 1095 return rect.Finalize();
1097 } 1096 }
1098 1097
1099 int ElideRectangleText(const string16& input, 1098 int ElideRectangleText(const string16& input,
1100 const gfx::Font& font, 1099 const Font& font,
1101 int available_pixel_width, 1100 float available_pixel_width,
1102 int available_pixel_height, 1101 int available_pixel_height,
1103 WordWrapBehavior wrap_behavior, 1102 WordWrapBehavior wrap_behavior,
1104 std::vector<string16>* lines) { 1103 std::vector<string16>* lines) {
1105 return ElideRectangleText(input, gfx::FontList(font), 1104 return ElideRectangleText(input, FontList(font),
1106 available_pixel_width, available_pixel_height, 1105 available_pixel_width, available_pixel_height,
1107 wrap_behavior, lines); 1106 wrap_behavior, lines);
1108 } 1107 }
1109 1108
1110 string16 TruncateString(const string16& string, size_t length) { 1109 string16 TruncateString(const string16& string, size_t length) {
1111 if (string.size() <= length) 1110 if (string.size() <= length)
1112 // String fits, return it. 1111 // String fits, return it.
1113 return string; 1112 return string;
1114 1113
1115 if (length == 0) 1114 if (length == 0)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1163 index = char_iterator.getIndex(); 1162 index = char_iterator.getIndex();
1164 } else { 1163 } else {
1165 // String has leading whitespace, return the elide string. 1164 // String has leading whitespace, return the elide string.
1166 return kElideString; 1165 return kElideString;
1167 } 1166 }
1168 } 1167 }
1169 return string.substr(0, index) + kElideString; 1168 return string.substr(0, index) + kElideString;
1170 } 1169 }
1171 1170
1172 } // namespace gfx 1171 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/text_elider.h ('k') | ui/gfx/text_elider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698