| 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 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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 gfx::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 >= gfx::GetStringWidth(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 gfx::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 (gfx::GetStringWidth(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 -= gfx::GetStringWidth(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 = gfx::GetStringWidth(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 gfx::GetStringWidth(username.substr(0, 1) + kEllipsisUTF16, |
| 179 font_list)); | 179 font_list)); |
| 180 if (gfx::GetStringWidth(domain, font_list) > available_domain_width) { | 180 if (gfx::GetStringWidth(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.0f)); |
| 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 -= gfx::GetStringWidth(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 gfx::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, gfx::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 gfx::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 = |
| 239 gfx::GetStringWidth(url_string, font_list); |
| 239 if (available_pixel_width >= pixel_width_url_string) | 240 if (available_pixel_width >= pixel_width_url_string) |
| 240 return url_string; | 241 return url_string; |
| 241 | 242 |
| 242 // Get the path substring, including query and reference. | 243 // Get the path substring, including query and reference. |
| 243 const size_t path_start_index = parsed.path.begin; | 244 const size_t path_start_index = parsed.path.begin; |
| 244 const size_t path_len = parsed.path.len; | 245 const size_t path_len = parsed.path.len; |
| 245 string16 url_path_query_etc = url_string.substr(path_start_index); | 246 string16 url_path_query_etc = url_string.substr(path_start_index); |
| 246 string16 url_path = url_string.substr(path_start_index, path_len); | 247 string16 url_path = url_string.substr(path_start_index, path_len); |
| 247 | 248 |
| 248 // Return general elided text if url minus the query fits. | 249 // Return general elided text if url minus the query fits. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 url_domain.clear(); | 292 url_domain.clear(); |
| 292 url_subdomain.clear(); | 293 url_subdomain.clear(); |
| 293 | 294 |
| 294 const string16 kColon = UTF8ToUTF16(":"); | 295 const string16 kColon = UTF8ToUTF16(":"); |
| 295 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; | 296 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; |
| 296 url_path_query_etc = url_path = file_path_split.at(1); | 297 url_path_query_etc = url_path = file_path_split.at(1); |
| 297 } | 298 } |
| 298 } | 299 } |
| 299 | 300 |
| 300 // Second Pass - remove scheme - the rest fits. | 301 // Second Pass - remove scheme - the rest fits. |
| 301 const int pixel_width_url_host = gfx::GetStringWidth(url_host, font_list); | 302 const float pixel_width_url_host = gfx::GetStringWidth(url_host, font_list); |
| 302 const int pixel_width_url_path = gfx::GetStringWidth(url_path_query_etc, | 303 const float pixel_width_url_path = gfx::GetStringWidth(url_path_query_etc, |
| 303 font_list); | 304 font_list); |
| 304 if (available_pixel_width >= | 305 if (available_pixel_width >= |
| 305 pixel_width_url_host + pixel_width_url_path) | 306 pixel_width_url_host + pixel_width_url_path) |
| 306 return url_host + url_path_query_etc; | 307 return url_host + url_path_query_etc; |
| 307 | 308 |
| 308 // Third Pass: Subdomain, domain and entire path fits. | 309 // Third Pass: Subdomain, domain and entire path fits. |
| 309 const int pixel_width_url_domain = gfx::GetStringWidth(url_domain, font_list); | 310 const float pixel_width_url_domain = |
| 310 const int pixel_width_url_subdomain = gfx::GetStringWidth(url_subdomain, | 311 gfx::GetStringWidth(url_domain, font_list); |
| 311 font_list); | 312 const float pixel_width_url_subdomain = |
| 313 gfx::GetStringWidth(url_subdomain, font_list); |
| 312 if (available_pixel_width >= | 314 if (available_pixel_width >= |
| 313 pixel_width_url_subdomain + pixel_width_url_domain + | 315 pixel_width_url_subdomain + pixel_width_url_domain + |
| 314 pixel_width_url_path) | 316 pixel_width_url_path) |
| 315 return url_subdomain + url_domain + url_path_query_etc; | 317 return url_subdomain + url_domain + url_path_query_etc; |
| 316 | 318 |
| 317 // Query element. | 319 // Query element. |
| 318 string16 url_query; | 320 string16 url_query; |
| 319 const int kPixelWidthDotsTrailer = gfx::GetStringWidth( | 321 const float kPixelWidthDotsTrailer = gfx::GetStringWidth( |
| 320 string16(kEllipsisUTF16), font_list); | 322 string16(kEllipsisUTF16), font_list); |
| 321 if (parsed.query.is_nonempty()) { | 323 if (parsed.query.is_nonempty()) { |
| 322 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); | 324 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); |
| 323 if (available_pixel_width >= | 325 if (available_pixel_width >= |
| 324 (pixel_width_url_subdomain + pixel_width_url_domain + | 326 (pixel_width_url_subdomain + pixel_width_url_domain + |
| 325 pixel_width_url_path - gfx::GetStringWidth(url_query, font_list))) { | 327 pixel_width_url_path - gfx::GetStringWidth(url_query, font_list))) { |
| 326 return ElideText(url_subdomain + url_domain + url_path_query_etc, | 328 return ElideText(url_subdomain + url_domain + url_path_query_etc, |
| 327 font_list, available_pixel_width, ELIDE_AT_END); | 329 font_list, available_pixel_width, ELIDE_AT_END); |
| 328 } | 330 } |
| 329 } | 331 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 350 if (url_path_number_of_elements <= 1 || | 352 if (url_path_number_of_elements <= 1 || |
| 351 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { | 353 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { |
| 352 // No path to elide, or too long of a path (could overflow in loop below) | 354 // No path to elide, or too long of a path (could overflow in loop below) |
| 353 // Just elide this as a text string. | 355 // Just elide this as a text string. |
| 354 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, | 356 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, |
| 355 available_pixel_width, ELIDE_AT_END); | 357 available_pixel_width, ELIDE_AT_END); |
| 356 } | 358 } |
| 357 | 359 |
| 358 // Start eliding the path and replacing elements by ".../". | 360 // Start eliding the path and replacing elements by ".../". |
| 359 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; | 361 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; |
| 360 const int pixel_width_ellipsis_slash = gfx::GetStringWidth(kEllipsisAndSlash, | 362 const float pixel_width_ellipsis_slash = gfx::GetStringWidth( |
| 361 font_list); | 363 kEllipsisAndSlash, font_list); |
| 362 | 364 |
| 363 // Check with both subdomain and domain. | 365 // Check with both subdomain and domain. |
| 364 string16 elided_path = | 366 string16 elided_path = |
| 365 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, | 367 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, |
| 366 url_filename, url_query, font_list, | 368 url_filename, url_query, font_list, |
| 367 available_pixel_width); | 369 available_pixel_width); |
| 368 if (!elided_path.empty()) | 370 if (!elided_path.empty()) |
| 369 return elided_path; | 371 return elided_path; |
| 370 | 372 |
| 371 // Check with only domain. | 373 // Check with only domain. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 383 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, | 385 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, |
| 384 url_filename, url_query, font_list, | 386 url_filename, url_query, font_list, |
| 385 available_pixel_width); | 387 available_pixel_width); |
| 386 | 388 |
| 387 if (!elided_path.empty()) | 389 if (!elided_path.empty()) |
| 388 return elided_path; | 390 return elided_path; |
| 389 } | 391 } |
| 390 | 392 |
| 391 // Return elided domain/.../filename anyway. | 393 // Return elided domain/.../filename anyway. |
| 392 string16 final_elided_url_string(url_elided_domain); | 394 string16 final_elided_url_string(url_elided_domain); |
| 393 const int url_elided_domain_width = gfx::GetStringWidth(url_elided_domain, | 395 const float url_elided_domain_width = gfx::GetStringWidth(url_elided_domain, |
| 394 font_list); | 396 font_list); |
| 395 | 397 |
| 396 // A hack to prevent trailing ".../...". | 398 // A hack to prevent trailing ".../...". |
| 397 if ((available_pixel_width - url_elided_domain_width) > | 399 if ((available_pixel_width - url_elided_domain_width) > |
| 398 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + | 400 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + |
| 399 gfx::GetStringWidth(ASCIIToUTF16("UV"), font_list)) { | 401 gfx::GetStringWidth(ASCIIToUTF16("UV"), font_list)) { |
| 400 final_elided_url_string += BuildPathFromComponents(string16(), | 402 final_elided_url_string += BuildPathFromComponents(string16(), |
| 401 url_path_elements, url_filename, 1); | 403 url_path_elements, url_filename, 1); |
| 402 } else { | 404 } else { |
| 403 final_elided_url_string += url_path; | 405 final_elided_url_string += url_path; |
| 404 } | 406 } |
| 405 | 407 |
| 406 return ElideText(final_elided_url_string, font_list, available_pixel_width, | 408 return ElideText(final_elided_url_string, font_list, available_pixel_width, |
| 407 ELIDE_AT_END); | 409 ELIDE_AT_END); |
| 408 } | 410 } |
| 409 | 411 |
| 410 string16 ElideUrl(const GURL& url, | 412 string16 ElideUrl(const GURL& url, |
| 411 const gfx::Font& font, | 413 const gfx::Font& font, |
| 412 int available_pixel_width, | 414 float available_pixel_width, |
| 413 const std::string& languages) { | 415 const std::string& languages) { |
| 414 return ElideUrl(url, gfx::FontList(font), available_pixel_width, languages); | 416 return ElideUrl(url, gfx::FontList(font), available_pixel_width, languages); |
| 415 } | 417 } |
| 416 | 418 |
| 417 string16 ElideFilename(const base::FilePath& filename, | 419 string16 ElideFilename(const base::FilePath& filename, |
| 418 const gfx::FontList& font_list, | 420 const gfx::FontList& font_list, |
| 419 int available_pixel_width) { | 421 float available_pixel_width) { |
| 420 #if defined(OS_WIN) | 422 #if defined(OS_WIN) |
| 421 string16 filename_utf16 = filename.value(); | 423 string16 filename_utf16 = filename.value(); |
| 422 string16 extension = filename.Extension(); | 424 string16 extension = filename.Extension(); |
| 423 string16 rootname = filename.BaseName().RemoveExtension().value(); | 425 string16 rootname = filename.BaseName().RemoveExtension().value(); |
| 424 #elif defined(OS_POSIX) | 426 #elif defined(OS_POSIX) |
| 425 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( | 427 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( |
| 426 filename.value())); | 428 filename.value())); |
| 427 string16 extension = WideToUTF16(base::SysNativeMBToWide( | 429 string16 extension = WideToUTF16(base::SysNativeMBToWide( |
| 428 filename.Extension())); | 430 filename.Extension())); |
| 429 string16 rootname = WideToUTF16(base::SysNativeMBToWide( | 431 string16 rootname = WideToUTF16(base::SysNativeMBToWide( |
| 430 filename.BaseName().RemoveExtension().value())); | 432 filename.BaseName().RemoveExtension().value())); |
| 431 #endif | 433 #endif |
| 432 | 434 |
| 433 const int full_width = gfx::GetStringWidth(filename_utf16, font_list); | 435 const float full_width = gfx::GetStringWidth(filename_utf16, font_list); |
| 434 if (full_width <= available_pixel_width) | 436 if (full_width <= available_pixel_width) |
| 435 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); | 437 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); |
| 436 | 438 |
| 437 if (rootname.empty() || extension.empty()) { | 439 if (rootname.empty() || extension.empty()) { |
| 438 const string16 elided_name = ElideText(filename_utf16, font_list, | 440 const string16 elided_name = ElideText(filename_utf16, font_list, |
| 439 available_pixel_width, ELIDE_AT_END); | 441 available_pixel_width, ELIDE_AT_END); |
| 440 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 442 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 441 } | 443 } |
| 442 | 444 |
| 443 const int ext_width = gfx::GetStringWidth(extension, font_list); | 445 const float ext_width = gfx::GetStringWidth(extension, font_list); |
| 444 const int root_width = gfx::GetStringWidth(rootname, font_list); | 446 const float root_width = gfx::GetStringWidth(rootname, font_list); |
| 445 | 447 |
| 446 // We may have trimmed the path. | 448 // We may have trimmed the path. |
| 447 if (root_width + ext_width <= available_pixel_width) { | 449 if (root_width + ext_width <= available_pixel_width) { |
| 448 const string16 elided_name = rootname + extension; | 450 const string16 elided_name = rootname + extension; |
| 449 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 451 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 450 } | 452 } |
| 451 | 453 |
| 452 if (ext_width >= available_pixel_width) { | 454 if (ext_width >= available_pixel_width) { |
| 453 const string16 elided_name = ElideText(rootname + extension, font_list, | 455 const string16 elided_name = ElideText(rootname + extension, font_list, |
| 454 available_pixel_width, | 456 available_pixel_width, |
| 455 ELIDE_IN_MIDDLE); | 457 ELIDE_IN_MIDDLE); |
| 456 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 458 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 457 } | 459 } |
| 458 | 460 |
| 459 int available_root_width = available_pixel_width - ext_width; | 461 float available_root_width = available_pixel_width - ext_width; |
| 460 string16 elided_name = | 462 string16 elided_name = |
| 461 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); | 463 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); |
| 462 elided_name += extension; | 464 elided_name += extension; |
| 463 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 465 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 464 } | 466 } |
| 465 | 467 |
| 466 string16 ElideFilename(const base::FilePath& filename, | 468 string16 ElideFilename(const base::FilePath& filename, |
| 467 const gfx::Font& font, | 469 const gfx::Font& font, |
| 468 int available_pixel_width) { | 470 float available_pixel_width) { |
| 469 return ElideFilename(filename, gfx::FontList(font), available_pixel_width); | 471 return ElideFilename(filename, gfx::FontList(font), available_pixel_width); |
| 470 } | 472 } |
| 471 | 473 |
| 472 string16 ElideText(const string16& text, | 474 string16 ElideText(const string16& text, |
| 473 const gfx::FontList& font_list, | 475 const gfx::FontList& font_list, |
| 474 int available_pixel_width, | 476 float available_pixel_width, |
| 475 ElideBehavior elide_behavior) { | 477 ElideBehavior elide_behavior) { |
| 476 if (text.empty()) | 478 if (text.empty()) |
| 477 return text; | 479 return text; |
| 478 | 480 |
| 479 const int current_text_pixel_width = gfx::GetStringWidth(text, font_list); | 481 const float current_text_pixel_width = gfx::GetStringWidth(text, font_list); |
| 480 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); | 482 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); |
| 481 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); | 483 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); |
| 482 | 484 |
| 483 const string16 ellipsis = string16(kEllipsisUTF16); | 485 const string16 ellipsis = string16(kEllipsisUTF16); |
| 484 StringSlicer slicer(text, ellipsis, elide_in_middle); | 486 StringSlicer slicer(text, ellipsis, elide_in_middle); |
| 485 | 487 |
| 486 // Pango will return 0 width for absurdly long strings. Cut the string in | 488 // Pango will return 0 width for absurdly long strings. Cut the string in |
| 487 // half and try again. | 489 // half and try again. |
| 488 // This is caused by an int overflow in Pango (specifically, in | 490 // This is caused by an int overflow in Pango (specifically, in |
| 489 // pango_glyph_string_extents_range). It's actually more subtle than just | 491 // pango_glyph_string_extents_range). It's actually more subtle than just |
| (...skipping 14 matching lines...) Expand all Loading... |
| 504 return string16(); | 506 return string16(); |
| 505 | 507 |
| 506 // Use binary search to compute the elided text. | 508 // Use binary search to compute the elided text. |
| 507 size_t lo = 0; | 509 size_t lo = 0; |
| 508 size_t hi = text.length() - 1; | 510 size_t hi = text.length() - 1; |
| 509 size_t guess; | 511 size_t guess; |
| 510 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | 512 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 | 513 // We check the length of the whole desired string at once to ensure we |
| 512 // handle kerning/ligatures/etc. correctly. | 514 // handle kerning/ligatures/etc. correctly. |
| 513 const string16 cut = slicer.CutString(guess, insert_ellipsis); | 515 const string16 cut = slicer.CutString(guess, insert_ellipsis); |
| 514 const int guess_length = gfx::GetStringWidth(cut, font_list); | 516 const float guess_length = gfx::GetStringWidth(cut, font_list); |
| 515 // Check again that we didn't hit a Pango width overflow. If so, cut the | 517 // Check again that we didn't hit a Pango width overflow. If so, cut the |
| 516 // current string in half and start over. | 518 // current string in half and start over. |
| 517 if (guess_length <= 0) { | 519 if (guess_length <= 0) { |
| 518 return ElideText(slicer.CutString(guess / 2, false), | 520 return ElideText(slicer.CutString(guess / 2, false), |
| 519 font_list, available_pixel_width, elide_behavior); | 521 font_list, available_pixel_width, elide_behavior); |
| 520 } | 522 } |
| 521 if (guess_length > available_pixel_width) | 523 if (guess_length > available_pixel_width) |
| 522 hi = guess - 1; | 524 hi = guess - 1; |
| 523 else | 525 else |
| 524 lo = guess + 1; | 526 lo = guess + 1; |
| 525 } | 527 } |
| 526 | 528 |
| 527 return slicer.CutString(guess, insert_ellipsis); | 529 return slicer.CutString(guess, insert_ellipsis); |
| 528 } | 530 } |
| 529 | 531 |
| 530 string16 ElideText(const string16& text, | 532 string16 ElideText(const string16& text, |
| 531 const gfx::Font& font, | 533 const gfx::Font& font, |
| 532 int available_pixel_width, | 534 float available_pixel_width, |
| 533 ElideBehavior elide_behavior) { | 535 ElideBehavior elide_behavior) { |
| 534 return ElideText(text, gfx::FontList(font), available_pixel_width, | 536 return ElideText(text, gfx::FontList(font), available_pixel_width, |
| 535 elide_behavior); | 537 elide_behavior); |
| 536 } | 538 } |
| 537 | 539 |
| 538 SortedDisplayURL::SortedDisplayURL(const GURL& url, | 540 SortedDisplayURL::SortedDisplayURL(const GURL& url, |
| 539 const std::string& languages) { | 541 const std::string& languages) { |
| 540 net::AppendFormattedHost(url, languages, &sort_host_); | 542 net::AppendFormattedHost(url, languages, &sort_host_); |
| 541 string16 host_minus_www = net::StripWWW(sort_host_); | 543 string16 host_minus_www = net::StripWWW(sort_host_); |
| 542 url_parse::Parsed parsed; | 544 url_parse::Parsed parsed; |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 803 ++current_row_; | 805 ++current_row_; |
| 804 current_col_ = 0; | 806 current_col_ = 0; |
| 805 } | 807 } |
| 806 | 808 |
| 807 // Internal class used to track progress of a rectangular text elide | 809 // Internal class used to track progress of a rectangular text elide |
| 808 // operation. Exists so the top-level ElideRectangleText() function | 810 // operation. Exists so the top-level ElideRectangleText() function |
| 809 // can be broken into smaller methods sharing this state. | 811 // can be broken into smaller methods sharing this state. |
| 810 class RectangleText { | 812 class RectangleText { |
| 811 public: | 813 public: |
| 812 RectangleText(const gfx::FontList& font_list, | 814 RectangleText(const gfx::FontList& font_list, |
| 813 int available_pixel_width, | 815 float available_pixel_width, |
| 814 int available_pixel_height, | 816 float available_pixel_height, |
| 815 WordWrapBehavior wrap_behavior, | 817 WordWrapBehavior wrap_behavior, |
| 816 std::vector<string16>* lines) | 818 std::vector<string16>* lines) |
| 817 : font_list_(font_list), | 819 : font_list_(font_list), |
| 818 line_height_(font_list.GetHeight()), | 820 line_height_(font_list.GetHeight()), |
| 819 available_pixel_width_(available_pixel_width), | 821 available_pixel_width_(available_pixel_width), |
| 820 available_pixel_height_(available_pixel_height), | 822 available_pixel_height_(available_pixel_height), |
| 821 wrap_behavior_(wrap_behavior), | 823 wrap_behavior_(wrap_behavior), |
| 822 current_width_(0), | 824 current_width_(0), |
| 823 current_height_(0), | 825 current_height_(0), |
| 824 last_line_ended_in_lf_(false), | 826 last_line_ended_in_lf_(false), |
| (...skipping 27 matching lines...) Expand all Loading... |
| 852 | 854 |
| 853 // Add a long word - wrapping, eliding or truncating per the wrap behavior. | 855 // Add a long word - wrapping, eliding or truncating per the wrap behavior. |
| 854 int AddWordOverflow(const string16& word); | 856 int AddWordOverflow(const string16& word); |
| 855 | 857 |
| 856 // Add a word to the rectangluar region at the current position. | 858 // Add a word to the rectangluar region at the current position. |
| 857 int AddWord(const string16& word); | 859 int AddWord(const string16& word); |
| 858 | 860 |
| 859 // Append the specified |text| to the current output line, incrementing the | 861 // Append the specified |text| to the current output line, incrementing the |
| 860 // running width by the specified amount. This is an optimization over | 862 // running width by the specified amount. This is an optimization over |
| 861 // |AddToCurrentLine()| when |text_width| is already known. | 863 // |AddToCurrentLine()| when |text_width| is already known. |
| 862 void AddToCurrentLineWithWidth(const string16& text, int text_width); | 864 void AddToCurrentLineWithWidth(const string16& text, float text_width); |
| 863 | 865 |
| 864 // Append the specified |text| to the current output line. | 866 // Append the specified |text| to the current output line. |
| 865 void AddToCurrentLine(const string16& text); | 867 void AddToCurrentLine(const string16& text); |
| 866 | 868 |
| 867 // Set the current position to the beginning of the next line. | 869 // Set the current position to the beginning of the next line. |
| 868 bool NewLine(); | 870 bool NewLine(); |
| 869 | 871 |
| 870 // The font list used for measuring text width. | 872 // The font list used for measuring text width. |
| 871 const gfx::FontList& font_list_; | 873 const gfx::FontList& font_list_; |
| 872 | 874 |
| 873 // The height of each line of text. | 875 // The height of each line of text. |
| 874 const int line_height_; | 876 const float line_height_; |
| 875 | 877 |
| 876 // The number of pixels of available width in the rectangle. | 878 // The number of pixels of available width in the rectangle. |
| 877 const int available_pixel_width_; | 879 const float available_pixel_width_; |
| 878 | 880 |
| 879 // The number of pixels of available height in the rectangle. | 881 // The number of pixels of available height in the rectangle. |
| 880 const int available_pixel_height_; | 882 const float available_pixel_height_; |
| 881 | 883 |
| 882 // The wrap behavior for words that are too long to fit on a single line. | 884 // The wrap behavior for words that are too long to fit on a single line. |
| 883 const WordWrapBehavior wrap_behavior_; | 885 const WordWrapBehavior wrap_behavior_; |
| 884 | 886 |
| 885 // The current running width. | 887 // The current running width. |
| 886 int current_width_; | 888 float current_width_; |
| 887 | 889 |
| 888 // The current running height. | 890 // The current running height. |
| 889 int current_height_; | 891 float current_height_; |
| 890 | 892 |
| 891 // The current line of text. | 893 // The current line of text. |
| 892 string16 current_line_; | 894 string16 current_line_; |
| 893 | 895 |
| 894 // Indicates whether the last line ended with \n. | 896 // Indicates whether the last line ended with \n. |
| 895 bool last_line_ended_in_lf_; | 897 bool last_line_ended_in_lf_; |
| 896 | 898 |
| 897 // The output vector of lines. | 899 // The output vector of lines. |
| 898 std::vector<string16>* lines_; | 900 std::vector<string16>* lines_; |
| 899 | 901 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 if (lines_->back().empty() && !last_line_ended_in_lf_) | 936 if (lines_->back().empty() && !last_line_ended_in_lf_) |
| 935 lines_->pop_back(); | 937 lines_->pop_back(); |
| 936 } | 938 } |
| 937 if (last_line_ended_in_lf_) | 939 if (last_line_ended_in_lf_) |
| 938 lines_->push_back(string16()); | 940 lines_->push_back(string16()); |
| 939 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | | 941 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | |
| 940 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); | 942 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); |
| 941 } | 943 } |
| 942 | 944 |
| 943 void RectangleText::AddLine(const string16& line) { | 945 void RectangleText::AddLine(const string16& line) { |
| 944 const int line_width = gfx::GetStringWidth(line, font_list_); | 946 const float line_width = gfx::GetStringWidth(line, font_list_); |
| 945 if (line_width <= available_pixel_width_) { | 947 if (line_width <= available_pixel_width_) { |
| 946 AddToCurrentLineWithWidth(line, line_width); | 948 AddToCurrentLineWithWidth(line, line_width); |
| 947 } else { | 949 } else { |
| 948 // Iterate over positions that are valid to break the line at. In general, | 950 // 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. | 951 // these are word boundaries but after any punctuation following the word. |
| 950 base::i18n::BreakIterator words(line, | 952 base::i18n::BreakIterator words(line, |
| 951 base::i18n::BreakIterator::BREAK_LINE); | 953 base::i18n::BreakIterator::BREAK_LINE); |
| 952 if (words.Init()) { | 954 if (words.Init()) { |
| 953 while (words.Advance()) { | 955 while (words.Advance()) { |
| 954 const bool truncate = !current_line_.empty(); | 956 const bool truncate = !current_line_.empty(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1021 insufficient_width_ = true; | 1023 insufficient_width_ = true; |
| 1022 } | 1024 } |
| 1023 | 1025 |
| 1024 return lines_added; | 1026 return lines_added; |
| 1025 } | 1027 } |
| 1026 | 1028 |
| 1027 int RectangleText::AddWord(const string16& word) { | 1029 int RectangleText::AddWord(const string16& word) { |
| 1028 int lines_added = 0; | 1030 int lines_added = 0; |
| 1029 string16 trimmed; | 1031 string16 trimmed; |
| 1030 TrimWhitespace(word, TRIM_TRAILING, &trimmed); | 1032 TrimWhitespace(word, TRIM_TRAILING, &trimmed); |
| 1031 const int trimmed_width = gfx::GetStringWidth(trimmed, font_list_); | 1033 const float trimmed_width = gfx::GetStringWidth(trimmed, font_list_); |
| 1032 if (trimmed_width <= available_pixel_width_) { | 1034 if (trimmed_width <= available_pixel_width_) { |
| 1033 // Word can be made to fit, no need to fragment it. | 1035 // Word can be made to fit, no need to fragment it. |
| 1034 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) | 1036 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) |
| 1035 lines_added++; | 1037 lines_added++; |
| 1036 // Append the non-trimmed word, in case more words are added after. | 1038 // Append the non-trimmed word, in case more words are added after. |
| 1037 AddToCurrentLine(word); | 1039 AddToCurrentLine(word); |
| 1038 } else { | 1040 } else { |
| 1039 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? | 1041 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? |
| 1040 trimmed : word); | 1042 trimmed : word); |
| 1041 } | 1043 } |
| 1042 return lines_added; | 1044 return lines_added; |
| 1043 } | 1045 } |
| 1044 | 1046 |
| 1045 void RectangleText::AddToCurrentLine(const string16& text) { | 1047 void RectangleText::AddToCurrentLine(const string16& text) { |
| 1046 AddToCurrentLineWithWidth(text, gfx::GetStringWidth(text, font_list_)); | 1048 AddToCurrentLineWithWidth(text, gfx::GetStringWidth(text, font_list_)); |
| 1047 } | 1049 } |
| 1048 | 1050 |
| 1049 void RectangleText::AddToCurrentLineWithWidth(const string16& text, | 1051 void RectangleText::AddToCurrentLineWithWidth(const string16& text, |
| 1050 int text_width) { | 1052 float text_width) { |
| 1051 if (current_height_ >= available_pixel_height_) { | 1053 if (current_height_ >= available_pixel_height_) { |
| 1052 insufficient_height_ = true; | 1054 insufficient_height_ = true; |
| 1053 return; | 1055 return; |
| 1054 } | 1056 } |
| 1055 current_line_.append(text); | 1057 current_line_.append(text); |
| 1056 current_width_ += text_width; | 1058 current_width_ += text_width; |
| 1057 } | 1059 } |
| 1058 | 1060 |
| 1059 bool RectangleText::NewLine() { | 1061 bool RectangleText::NewLine() { |
| 1060 bool line_added = false; | 1062 bool line_added = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1075 bool ElideRectangleString(const string16& input, size_t max_rows, | 1077 bool ElideRectangleString(const string16& input, size_t max_rows, |
| 1076 size_t max_cols, bool strict, string16* output) { | 1078 size_t max_cols, bool strict, string16* output) { |
| 1077 RectangleString rect(max_rows, max_cols, strict, output); | 1079 RectangleString rect(max_rows, max_cols, strict, output); |
| 1078 rect.Init(); | 1080 rect.Init(); |
| 1079 rect.AddString(input); | 1081 rect.AddString(input); |
| 1080 return rect.Finalize(); | 1082 return rect.Finalize(); |
| 1081 } | 1083 } |
| 1082 | 1084 |
| 1083 int ElideRectangleText(const string16& input, | 1085 int ElideRectangleText(const string16& input, |
| 1084 const gfx::FontList& font_list, | 1086 const gfx::FontList& font_list, |
| 1085 int available_pixel_width, | 1087 float available_pixel_width, |
| 1086 int available_pixel_height, | 1088 float available_pixel_height, |
| 1087 WordWrapBehavior wrap_behavior, | 1089 WordWrapBehavior wrap_behavior, |
| 1088 std::vector<string16>* lines) { | 1090 std::vector<string16>* lines) { |
| 1089 RectangleText rect(font_list, | 1091 RectangleText rect(font_list, |
| 1090 available_pixel_width, | 1092 available_pixel_width, |
| 1091 available_pixel_height, | 1093 available_pixel_height, |
| 1092 wrap_behavior, | 1094 wrap_behavior, |
| 1093 lines); | 1095 lines); |
| 1094 rect.Init(); | 1096 rect.Init(); |
| 1095 rect.AddString(input); | 1097 rect.AddString(input); |
| 1096 return rect.Finalize(); | 1098 return rect.Finalize(); |
| 1097 } | 1099 } |
| 1098 | 1100 |
| 1099 int ElideRectangleText(const string16& input, | 1101 int ElideRectangleText(const string16& input, |
| 1100 const gfx::Font& font, | 1102 const gfx::Font& font, |
| 1101 int available_pixel_width, | 1103 float available_pixel_width, |
| 1102 int available_pixel_height, | 1104 float available_pixel_height, |
| 1103 WordWrapBehavior wrap_behavior, | 1105 WordWrapBehavior wrap_behavior, |
| 1104 std::vector<string16>* lines) { | 1106 std::vector<string16>* lines) { |
| 1105 return ElideRectangleText(input, gfx::FontList(font), | 1107 return ElideRectangleText(input, gfx::FontList(font), |
| 1106 available_pixel_width, available_pixel_height, | 1108 available_pixel_width, available_pixel_height, |
| 1107 wrap_behavior, lines); | 1109 wrap_behavior, lines); |
| 1108 } | 1110 } |
| 1109 | 1111 |
| 1110 string16 TruncateString(const string16& string, size_t length) { | 1112 string16 TruncateString(const string16& string, size_t length) { |
| 1111 if (string.size() <= length) | 1113 if (string.size() <= length) |
| 1112 // String fits, return it. | 1114 // String fits, return it. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1163 index = char_iterator.getIndex(); | 1165 index = char_iterator.getIndex(); |
| 1164 } else { | 1166 } else { |
| 1165 // String has leading whitespace, return the elide string. | 1167 // String has leading whitespace, return the elide string. |
| 1166 return kElideString; | 1168 return kElideString; |
| 1167 } | 1169 } |
| 1168 } | 1170 } |
| 1169 return string.substr(0, index) + kElideString; | 1171 return string.substr(0, index) + kElideString; |
| 1170 } | 1172 } |
| 1171 | 1173 |
| 1172 } // namespace gfx | 1174 } // namespace gfx |
| OLD | NEW |