| 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/base/text/text_elider.h" | 10 #include "ui/base/text/text_elider.h" |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/i18n/break_iterator.h" | 16 #include "base/i18n/break_iterator.h" |
| 17 #include "base/i18n/char_iterator.h" | 17 #include "base/i18n/char_iterator.h" |
| 18 #include "base/i18n/rtl.h" | 18 #include "base/i18n/rtl.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 22 #include "base/strings/sys_string_conversions.h" | 22 #include "base/strings/sys_string_conversions.h" |
| 23 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 24 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
| 25 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 27 #include "third_party/icu/source/common/unicode/rbbi.h" | 27 #include "third_party/icu/source/common/unicode/rbbi.h" |
| 28 #include "third_party/icu/source/common/unicode/uloc.h" | 28 #include "third_party/icu/source/common/unicode/uloc.h" |
| 29 #include "ui/gfx/font.h" | 29 #include "ui/gfx/font_list.h" |
| 30 #include "ui/gfx/text_utils.h" |
| 30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 31 | 32 |
| 32 namespace ui { | 33 namespace ui { |
| 33 | 34 |
| 34 // U+2026 in utf8 | 35 // U+2026 in utf8 |
| 35 const char kEllipsis[] = "\xE2\x80\xA6"; | 36 const char kEllipsis[] = "\xE2\x80\xA6"; |
| 36 const char16 kEllipsisUTF16[] = { 0x2026, 0 }; | 37 const char16 kEllipsisUTF16[] = { 0x2026, 0 }; |
| 37 const char16 kForwardSlash = '/'; | 38 const char16 kForwardSlash = '/'; |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 size_t num_components) { | 109 size_t num_components) { |
| 109 // Add the initial elements of the path. | 110 // Add the initial elements of the path. |
| 110 string16 path = path_prefix; | 111 string16 path = path_prefix; |
| 111 | 112 |
| 112 // Build path from first |num_components| elements. | 113 // Build path from first |num_components| elements. |
| 113 for (size_t j = 0; j < num_components; ++j) | 114 for (size_t j = 0; j < num_components; ++j) |
| 114 path += path_elements[j] + kForwardSlash; | 115 path += path_elements[j] + kForwardSlash; |
| 115 | 116 |
| 116 // Add |filename|, ellipsis if necessary. | 117 // Add |filename|, ellipsis if necessary. |
| 117 if (num_components != (path_elements.size() - 1)) | 118 if (num_components != (path_elements.size() - 1)) |
| 118 path += UTF8ToUTF16(kEllipsis) + kForwardSlash; | 119 path += string16(kEllipsisUTF16) + kForwardSlash; |
| 119 path += filename; | 120 path += filename; |
| 120 | 121 |
| 121 return path; | 122 return path; |
| 122 } | 123 } |
| 123 | 124 |
| 124 // 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 |
| 125 // components and elides if possible. Returns a string containing the longest | 126 // components and elides if possible. Returns a string containing the longest |
| 126 // 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. |
| 127 string16 ElideComponentizedPath(const string16& url_path_prefix, | 128 string16 ElideComponentizedPath(const string16& url_path_prefix, |
| 128 const std::vector<string16>& url_path_elements, | 129 const std::vector<string16>& url_path_elements, |
| 129 const string16& url_filename, | 130 const string16& url_filename, |
| 130 const string16& url_query, | 131 const string16& url_query, |
| 131 const gfx::Font& font, | 132 const gfx::FontList& font_list, |
| 132 int available_pixel_width) { | 133 int available_pixel_width) { |
| 133 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(); |
| 134 | 135 |
| 135 CHECK(url_path_number_of_elements); | 136 CHECK(url_path_number_of_elements); |
| 136 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) { |
| 137 string16 elided_path = BuildPathFromComponents(url_path_prefix, | 138 string16 elided_path = BuildPathFromComponents(url_path_prefix, |
| 138 url_path_elements, url_filename, i); | 139 url_path_elements, url_filename, i); |
| 139 if (available_pixel_width >= font.GetStringWidth(elided_path)) | 140 if (available_pixel_width >= gfx::GetStringWidth(elided_path, font_list)) |
| 140 return ElideText(elided_path + url_query, | 141 return ElideText(elided_path + url_query, font_list, |
| 141 font, available_pixel_width, ELIDE_AT_END); | 142 available_pixel_width, ELIDE_AT_END); |
| 142 } | 143 } |
| 143 | 144 |
| 144 return string16(); | 145 return string16(); |
| 145 } | 146 } |
| 146 | 147 |
| 147 } // namespace | 148 } // namespace |
| 148 | 149 |
| 149 string16 ElideEmail(const string16& email, | 150 string16 ElideEmail(const string16& email, |
| 150 const gfx::Font& font, | 151 const gfx::FontList& font_list, |
| 151 int available_pixel_width) { | 152 int available_pixel_width) { |
| 152 if (font.GetStringWidth(email) <= available_pixel_width) | 153 if (gfx::GetStringWidth(email, font_list) <= available_pixel_width) |
| 153 return email; | 154 return email; |
| 154 | 155 |
| 155 // 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 |
| 156 // 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 |
| 157 // 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 @ |
| 158 // 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 @ |
| 159 // symbol is safe. | 160 // symbol is safe. |
| 160 const size_t split_index = email.find_last_of('@'); | 161 const size_t split_index = email.find_last_of('@'); |
| 161 DCHECK_NE(split_index, string16::npos); | 162 DCHECK_NE(split_index, string16::npos); |
| 162 string16 username = email.substr(0, split_index); | 163 string16 username = email.substr(0, split_index); |
| 163 string16 domain = email.substr(split_index + 1); | 164 string16 domain = email.substr(split_index + 1); |
| 164 DCHECK(!username.empty()); | 165 DCHECK(!username.empty()); |
| 165 DCHECK(!domain.empty()); | 166 DCHECK(!domain.empty()); |
| 166 | 167 |
| 167 // Subtract the @ symbol from the available width as it is mandatory. | 168 // Subtract the @ symbol from the available width as it is mandatory. |
| 168 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); | 169 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); |
| 169 available_pixel_width -= font.GetStringWidth(kAtSignUTF16); | 170 available_pixel_width -= gfx::GetStringWidth(kAtSignUTF16, font_list); |
| 170 | 171 |
| 171 // Check whether eliding the domain is necessary: if eliding the username | 172 // Check whether eliding the domain is necessary: if eliding the username |
| 172 // is sufficient, the domain will not be elided. | 173 // is sufficient, the domain will not be elided. |
| 173 const int full_username_width = font.GetStringWidth(username); | 174 const int full_username_width = gfx::GetStringWidth(username, font_list); |
| 174 const int available_domain_width = | 175 const int available_domain_width = |
| 175 available_pixel_width - | 176 available_pixel_width - |
| 176 std::min(full_username_width, | 177 std::min(full_username_width, |
| 177 font.GetStringWidth(username.substr(0, 1) + kEllipsisUTF16)); | 178 gfx::GetStringWidth(username.substr(0, 1) + kEllipsisUTF16, |
| 178 if (font.GetStringWidth(domain) > available_domain_width) { | 179 font_list)); |
| 180 if (gfx::GetStringWidth(domain, font_list) > available_domain_width) { |
| 179 // 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. |
| 180 // 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 |
| 181 // domain will occupy the leftover width. | 183 // domain will occupy the leftover width. |
| 182 // If |desired_domain_width| is greater than |available_domain_width|: the | 184 // If |desired_domain_width| is greater than |available_domain_width|: the |
| 183 // minimal username elision allowed by the specifications will not fit; thus | 185 // minimal username elision allowed by the specifications will not fit; thus |
| 184 // |desired_domain_width| must be <= |available_domain_width| at all cost. | 186 // |desired_domain_width| must be <= |available_domain_width| at all cost. |
| 185 const int desired_domain_width = | 187 const int desired_domain_width = |
| 186 std::min(available_domain_width, | 188 std::min(available_domain_width, |
| 187 std::max(available_pixel_width - full_username_width, | 189 std::max(available_pixel_width - full_username_width, |
| 188 available_pixel_width / 2)); | 190 available_pixel_width / 2)); |
| 189 domain = ElideText(domain, font, desired_domain_width, ELIDE_IN_MIDDLE); | 191 domain = ElideText(domain, font_list, desired_domain_width, |
| 192 ELIDE_IN_MIDDLE); |
| 190 // 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 |
| 191 // (other than the ellipsis itself) remains: return a single ellipsis. | 194 // (other than the ellipsis itself) remains: return a single ellipsis. |
| 192 if (domain.length() <= 1U) | 195 if (domain.length() <= 1U) |
| 193 return string16(kEllipsisUTF16); | 196 return string16(kEllipsisUTF16); |
| 194 } | 197 } |
| 195 | 198 |
| 196 // 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 |
| 197 // 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 |
| 198 // precautions taken earlier). | 201 // precautions taken earlier). |
| 199 username = ElideText(username, | 202 available_pixel_width -= gfx::GetStringWidth(domain, font_list); |
| 200 font, | 203 username = ElideText(username, font_list, available_pixel_width, |
| 201 available_pixel_width - font.GetStringWidth(domain), | |
| 202 ELIDE_AT_END); | 204 ELIDE_AT_END); |
| 203 | 205 |
| 204 return username + kAtSignUTF16 + domain; | 206 return username + kAtSignUTF16 + domain; |
| 205 } | 207 } |
| 206 | 208 |
| 209 string16 ElideEmail(const string16& email, |
| 210 const gfx::Font& font, |
| 211 int available_pixel_width) { |
| 212 return ElideEmail(email, gfx::FontList(font), available_pixel_width); |
| 213 } |
| 214 |
| 207 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 215 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
| 208 // 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 |
| 209 // 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 |
| 210 // suspect it could be made simpler. | 218 // suspect it could be made simpler. |
| 211 string16 ElideUrl(const GURL& url, | 219 string16 ElideUrl(const GURL& url, |
| 212 const gfx::Font& font, | 220 const gfx::FontList& font_list, |
| 213 int available_pixel_width, | 221 int available_pixel_width, |
| 214 const std::string& languages) { | 222 const std::string& languages) { |
| 215 // Get a formatted string and corresponding parsing of the url. | 223 // Get a formatted string and corresponding parsing of the url. |
| 216 url_parse::Parsed parsed; | 224 url_parse::Parsed parsed; |
| 217 const string16 url_string = | 225 const string16 url_string = |
| 218 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 226 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
| 219 net::UnescapeRule::SPACES, &parsed, NULL, NULL); | 227 net::UnescapeRule::SPACES, &parsed, NULL, NULL); |
| 220 if (available_pixel_width <= 0) | 228 if (available_pixel_width <= 0) |
| 221 return url_string; | 229 return url_string; |
| 222 | 230 |
| 223 // If non-standard, return plain eliding. | 231 // If non-standard, return plain eliding. |
| 224 if (!url.IsStandard()) | 232 if (!url.IsStandard()) |
| 225 return ElideText(url_string, font, available_pixel_width, ELIDE_AT_END); | 233 return ElideText(url_string, font_list, available_pixel_width, |
| 234 ELIDE_AT_END); |
| 226 | 235 |
| 227 // Now start eliding url_string to fit within available pixel width. | 236 // Now start eliding url_string to fit within available pixel width. |
| 228 // Fist pass - check to see whether entire url_string fits. | 237 // Fist pass - check to see whether entire url_string fits. |
| 229 const int pixel_width_url_string = font.GetStringWidth(url_string); | 238 const int pixel_width_url_string = gfx::GetStringWidth(url_string, font_list); |
| 230 if (available_pixel_width >= pixel_width_url_string) | 239 if (available_pixel_width >= pixel_width_url_string) |
| 231 return url_string; | 240 return url_string; |
| 232 | 241 |
| 233 // Get the path substring, including query and reference. | 242 // Get the path substring, including query and reference. |
| 234 const size_t path_start_index = parsed.path.begin; | 243 const size_t path_start_index = parsed.path.begin; |
| 235 const size_t path_len = parsed.path.len; | 244 const size_t path_len = parsed.path.len; |
| 236 string16 url_path_query_etc = url_string.substr(path_start_index); | 245 string16 url_path_query_etc = url_string.substr(path_start_index); |
| 237 string16 url_path = url_string.substr(path_start_index, path_len); | 246 string16 url_path = url_string.substr(path_start_index, path_len); |
| 238 | 247 |
| 239 // Return general elided text if url minus the query fits. | 248 // Return general elided text if url minus the query fits. |
| 240 const string16 url_minus_query = | 249 const string16 url_minus_query = |
| 241 url_string.substr(0, path_start_index + path_len); | 250 url_string.substr(0, path_start_index + path_len); |
| 242 if (available_pixel_width >= font.GetStringWidth(url_minus_query)) | 251 if (available_pixel_width >= gfx::GetStringWidth(url_minus_query, font_list)) |
| 243 return ElideText(url_string, font, available_pixel_width, ELIDE_AT_END); | 252 return ElideText(url_string, font_list, available_pixel_width, |
| 253 ELIDE_AT_END); |
| 244 | 254 |
| 245 // Get Host. | 255 // Get Host. |
| 246 string16 url_host = UTF8ToUTF16(url.host()); | 256 string16 url_host = UTF8ToUTF16(url.host()); |
| 247 | 257 |
| 248 // Get domain and registry information from the URL. | 258 // Get domain and registry information from the URL. |
| 249 string16 url_domain = UTF8ToUTF16( | 259 string16 url_domain = UTF8ToUTF16( |
| 250 net::registry_controlled_domains::GetDomainAndRegistry( | 260 net::registry_controlled_domains::GetDomainAndRegistry( |
| 251 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); | 261 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
| 252 if (url_domain.empty()) | 262 if (url_domain.empty()) |
| 253 url_domain = url_host; | 263 url_domain = url_host; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 281 url_domain.clear(); | 291 url_domain.clear(); |
| 282 url_subdomain.clear(); | 292 url_subdomain.clear(); |
| 283 | 293 |
| 284 const string16 kColon = UTF8ToUTF16(":"); | 294 const string16 kColon = UTF8ToUTF16(":"); |
| 285 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; |
| 286 url_path_query_etc = url_path = file_path_split.at(1); | 296 url_path_query_etc = url_path = file_path_split.at(1); |
| 287 } | 297 } |
| 288 } | 298 } |
| 289 | 299 |
| 290 // Second Pass - remove scheme - the rest fits. | 300 // Second Pass - remove scheme - the rest fits. |
| 291 const int pixel_width_url_host = font.GetStringWidth(url_host); | 301 const int pixel_width_url_host = gfx::GetStringWidth(url_host, font_list); |
| 292 const int pixel_width_url_path = font.GetStringWidth(url_path_query_etc); | 302 const int pixel_width_url_path = gfx::GetStringWidth(url_path_query_etc, |
| 303 font_list); |
| 293 if (available_pixel_width >= | 304 if (available_pixel_width >= |
| 294 pixel_width_url_host + pixel_width_url_path) | 305 pixel_width_url_host + pixel_width_url_path) |
| 295 return url_host + url_path_query_etc; | 306 return url_host + url_path_query_etc; |
| 296 | 307 |
| 297 // Third Pass: Subdomain, domain and entire path fits. | 308 // Third Pass: Subdomain, domain and entire path fits. |
| 298 const int pixel_width_url_domain = font.GetStringWidth(url_domain); | 309 const int pixel_width_url_domain = gfx::GetStringWidth(url_domain, font_list); |
| 299 const int pixel_width_url_subdomain = font.GetStringWidth(url_subdomain); | 310 const int pixel_width_url_subdomain = gfx::GetStringWidth(url_subdomain, |
| 311 font_list); |
| 300 if (available_pixel_width >= | 312 if (available_pixel_width >= |
| 301 pixel_width_url_subdomain + pixel_width_url_domain + | 313 pixel_width_url_subdomain + pixel_width_url_domain + |
| 302 pixel_width_url_path) | 314 pixel_width_url_path) |
| 303 return url_subdomain + url_domain + url_path_query_etc; | 315 return url_subdomain + url_domain + url_path_query_etc; |
| 304 | 316 |
| 305 // Query element. | 317 // Query element. |
| 306 string16 url_query; | 318 string16 url_query; |
| 307 const int kPixelWidthDotsTrailer = | 319 const int kPixelWidthDotsTrailer = gfx::GetStringWidth( |
| 308 font.GetStringWidth(UTF8ToUTF16(kEllipsis)); | 320 string16(kEllipsisUTF16), font_list); |
| 309 if (parsed.query.is_nonempty()) { | 321 if (parsed.query.is_nonempty()) { |
| 310 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); | 322 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); |
| 311 if (available_pixel_width >= (pixel_width_url_subdomain + | 323 if (available_pixel_width >= |
| 312 pixel_width_url_domain + pixel_width_url_path - | 324 (pixel_width_url_subdomain + pixel_width_url_domain + |
| 313 font.GetStringWidth(url_query))) { | 325 pixel_width_url_path - gfx::GetStringWidth(url_query, font_list))) { |
| 314 return ElideText(url_subdomain + url_domain + url_path_query_etc, | 326 return ElideText(url_subdomain + url_domain + url_path_query_etc, |
| 315 font, available_pixel_width, ELIDE_AT_END); | 327 font_list, available_pixel_width, ELIDE_AT_END); |
| 316 } | 328 } |
| 317 } | 329 } |
| 318 | 330 |
| 319 // Parse url_path using '/'. | 331 // Parse url_path using '/'. |
| 320 std::vector<string16> url_path_elements; | 332 std::vector<string16> url_path_elements; |
| 321 base::SplitString(url_path, kForwardSlash, &url_path_elements); | 333 base::SplitString(url_path, kForwardSlash, &url_path_elements); |
| 322 | 334 |
| 323 // Get filename - note that for a path ending with / | 335 // Get filename - note that for a path ending with / |
| 324 // such as www.google.com/intl/ads/, the file name is ads/. | 336 // such as www.google.com/intl/ads/, the file name is ads/. |
| 325 size_t url_path_number_of_elements = url_path_elements.size(); | 337 size_t url_path_number_of_elements = url_path_elements.size(); |
| 326 DCHECK(url_path_number_of_elements != 0); | 338 DCHECK(url_path_number_of_elements != 0); |
| 327 string16 url_filename; | 339 string16 url_filename; |
| 328 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) { | 340 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) { |
| 329 url_filename = *(url_path_elements.end() - 1); | 341 url_filename = *(url_path_elements.end() - 1); |
| 330 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'. | 342 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'. |
| 331 url_filename = url_path_elements.at(url_path_number_of_elements - 2) + | 343 url_filename = url_path_elements.at(url_path_number_of_elements - 2) + |
| 332 kForwardSlash; | 344 kForwardSlash; |
| 333 url_path_number_of_elements--; | 345 url_path_number_of_elements--; |
| 334 } | 346 } |
| 335 DCHECK(url_path_number_of_elements != 0); | 347 DCHECK(url_path_number_of_elements != 0); |
| 336 | 348 |
| 337 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; | 349 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; |
| 338 if (url_path_number_of_elements <= 1 || | 350 if (url_path_number_of_elements <= 1 || |
| 339 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { | 351 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { |
| 340 // 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) |
| 341 // Just elide this as a text string. | 353 // Just elide this as a text string. |
| 342 return ElideText(url_subdomain + url_domain + url_path_query_etc, font, | 354 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, |
| 343 available_pixel_width, ELIDE_AT_END); | 355 available_pixel_width, ELIDE_AT_END); |
| 344 } | 356 } |
| 345 | 357 |
| 346 // Start eliding the path and replacing elements by ".../". | 358 // Start eliding the path and replacing elements by ".../". |
| 347 const string16 kEllipsisAndSlash = UTF8ToUTF16(kEllipsis) + kForwardSlash; | 359 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; |
| 348 const int pixel_width_ellipsis_slash = font.GetStringWidth(kEllipsisAndSlash); | 360 const int pixel_width_ellipsis_slash = gfx::GetStringWidth(kEllipsisAndSlash, |
| 361 font_list); |
| 349 | 362 |
| 350 // Check with both subdomain and domain. | 363 // Check with both subdomain and domain. |
| 351 string16 elided_path = | 364 string16 elided_path = |
| 352 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, | 365 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, |
| 353 url_filename, url_query, font, | 366 url_filename, url_query, font_list, |
| 354 available_pixel_width); | 367 available_pixel_width); |
| 355 if (!elided_path.empty()) | 368 if (!elided_path.empty()) |
| 356 return elided_path; | 369 return elided_path; |
| 357 | 370 |
| 358 // Check with only domain. | 371 // Check with only domain. |
| 359 // If a subdomain is present, add an ellipsis before domain. | 372 // If a subdomain is present, add an ellipsis before domain. |
| 360 // This is added only if the subdomain pixel width is larger than | 373 // This is added only if the subdomain pixel width is larger than |
| 361 // the pixel width of kEllipsis. Otherwise, subdomain remains, | 374 // the pixel width of kEllipsis. Otherwise, subdomain remains, |
| 362 // which means that this case has been resolved earlier. | 375 // which means that this case has been resolved earlier. |
| 363 string16 url_elided_domain = url_subdomain + url_domain; | 376 string16 url_elided_domain = url_subdomain + url_domain; |
| 364 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { | 377 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { |
| 365 if (!url_subdomain.empty()) | 378 if (!url_subdomain.empty()) |
| 366 url_elided_domain = kEllipsisAndSlash[0] + url_domain; | 379 url_elided_domain = kEllipsisAndSlash[0] + url_domain; |
| 367 else | 380 else |
| 368 url_elided_domain = url_domain; | 381 url_elided_domain = url_domain; |
| 369 | 382 |
| 370 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, | 383 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, |
| 371 url_filename, url_query, font, | 384 url_filename, url_query, font_list, |
| 372 available_pixel_width); | 385 available_pixel_width); |
| 373 | 386 |
| 374 if (!elided_path.empty()) | 387 if (!elided_path.empty()) |
| 375 return elided_path; | 388 return elided_path; |
| 376 } | 389 } |
| 377 | 390 |
| 378 // Return elided domain/.../filename anyway. | 391 // Return elided domain/.../filename anyway. |
| 379 string16 final_elided_url_string(url_elided_domain); | 392 string16 final_elided_url_string(url_elided_domain); |
| 380 const int url_elided_domain_width = font.GetStringWidth(url_elided_domain); | 393 const int url_elided_domain_width = gfx::GetStringWidth(url_elided_domain, |
| 394 font_list); |
| 381 | 395 |
| 382 // A hack to prevent trailing ".../...". | 396 // A hack to prevent trailing ".../...". |
| 383 if ((available_pixel_width - url_elided_domain_width) > | 397 if ((available_pixel_width - url_elided_domain_width) > |
| 384 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + | 398 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + |
| 385 font.GetStringWidth(ASCIIToUTF16("UV"))) { | 399 gfx::GetStringWidth(ASCIIToUTF16("UV"), font_list)) { |
| 386 final_elided_url_string += BuildPathFromComponents(string16(), | 400 final_elided_url_string += BuildPathFromComponents(string16(), |
| 387 url_path_elements, url_filename, 1); | 401 url_path_elements, url_filename, 1); |
| 388 } else { | 402 } else { |
| 389 final_elided_url_string += url_path; | 403 final_elided_url_string += url_path; |
| 390 } | 404 } |
| 391 | 405 |
| 392 return ElideText(final_elided_url_string, font, available_pixel_width, | 406 return ElideText(final_elided_url_string, font_list, available_pixel_width, |
| 393 ELIDE_AT_END); | 407 ELIDE_AT_END); |
| 394 } | 408 } |
| 395 | 409 |
| 410 string16 ElideUrl(const GURL& url, |
| 411 const gfx::Font& font, |
| 412 int available_pixel_width, |
| 413 const std::string& languages) { |
| 414 return ElideUrl(url, gfx::FontList(font), available_pixel_width, languages); |
| 415 } |
| 416 |
| 396 string16 ElideFilename(const base::FilePath& filename, | 417 string16 ElideFilename(const base::FilePath& filename, |
| 397 const gfx::Font& font, | 418 const gfx::FontList& font_list, |
| 398 int available_pixel_width) { | 419 int available_pixel_width) { |
| 399 #if defined(OS_WIN) | 420 #if defined(OS_WIN) |
| 400 string16 filename_utf16 = filename.value(); | 421 string16 filename_utf16 = filename.value(); |
| 401 string16 extension = filename.Extension(); | 422 string16 extension = filename.Extension(); |
| 402 string16 rootname = filename.BaseName().RemoveExtension().value(); | 423 string16 rootname = filename.BaseName().RemoveExtension().value(); |
| 403 #elif defined(OS_POSIX) | 424 #elif defined(OS_POSIX) |
| 404 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( | 425 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( |
| 405 filename.value())); | 426 filename.value())); |
| 406 string16 extension = WideToUTF16(base::SysNativeMBToWide( | 427 string16 extension = WideToUTF16(base::SysNativeMBToWide( |
| 407 filename.Extension())); | 428 filename.Extension())); |
| 408 string16 rootname = WideToUTF16(base::SysNativeMBToWide( | 429 string16 rootname = WideToUTF16(base::SysNativeMBToWide( |
| 409 filename.BaseName().RemoveExtension().value())); | 430 filename.BaseName().RemoveExtension().value())); |
| 410 #endif | 431 #endif |
| 411 | 432 |
| 412 const int full_width = font.GetStringWidth(filename_utf16); | 433 const int full_width = gfx::GetStringWidth(filename_utf16, font_list); |
| 413 if (full_width <= available_pixel_width) | 434 if (full_width <= available_pixel_width) |
| 414 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); | 435 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); |
| 415 | 436 |
| 416 if (rootname.empty() || extension.empty()) { | 437 if (rootname.empty() || extension.empty()) { |
| 417 const string16 elided_name = ElideText(filename_utf16, font, | 438 const string16 elided_name = ElideText(filename_utf16, font_list, |
| 418 available_pixel_width, ELIDE_AT_END); | 439 available_pixel_width, ELIDE_AT_END); |
| 419 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 440 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 420 } | 441 } |
| 421 | 442 |
| 422 const int ext_width = font.GetStringWidth(extension); | 443 const int ext_width = gfx::GetStringWidth(extension, font_list); |
| 423 const int root_width = font.GetStringWidth(rootname); | 444 const int root_width = gfx::GetStringWidth(rootname, font_list); |
| 424 | 445 |
| 425 // We may have trimmed the path. | 446 // We may have trimmed the path. |
| 426 if (root_width + ext_width <= available_pixel_width) { | 447 if (root_width + ext_width <= available_pixel_width) { |
| 427 const string16 elided_name = rootname + extension; | 448 const string16 elided_name = rootname + extension; |
| 428 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 449 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 429 } | 450 } |
| 430 | 451 |
| 431 if (ext_width >= available_pixel_width) { | 452 if (ext_width >= available_pixel_width) { |
| 432 const string16 elided_name = ElideText(rootname + extension, font, | 453 const string16 elided_name = ElideText(rootname + extension, font_list, |
| 433 available_pixel_width, | 454 available_pixel_width, |
| 434 ELIDE_IN_MIDDLE); | 455 ELIDE_IN_MIDDLE); |
| 435 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 456 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 436 } | 457 } |
| 437 | 458 |
| 438 int available_root_width = available_pixel_width - ext_width; | 459 int available_root_width = available_pixel_width - ext_width; |
| 439 string16 elided_name = | 460 string16 elided_name = |
| 440 ElideText(rootname, font, available_root_width, ELIDE_AT_END); | 461 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); |
| 441 elided_name += extension; | 462 elided_name += extension; |
| 442 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 463 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
| 443 } | 464 } |
| 444 | 465 |
| 466 string16 ElideFilename(const base::FilePath& filename, |
| 467 const gfx::Font& font, |
| 468 int available_pixel_width) { |
| 469 return ElideFilename(filename, gfx::FontList(font), available_pixel_width); |
| 470 } |
| 471 |
| 445 string16 ElideText(const string16& text, | 472 string16 ElideText(const string16& text, |
| 446 const gfx::Font& font, | 473 const gfx::FontList& font_list, |
| 447 int available_pixel_width, | 474 int available_pixel_width, |
| 448 ElideBehavior elide_behavior) { | 475 ElideBehavior elide_behavior) { |
| 449 if (text.empty()) | 476 if (text.empty()) |
| 450 return text; | 477 return text; |
| 451 | 478 |
| 452 const int current_text_pixel_width = font.GetStringWidth(text); | 479 const int current_text_pixel_width = gfx::GetStringWidth(text, font_list); |
| 453 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); | 480 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); |
| 454 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); | 481 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); |
| 455 | 482 |
| 456 const string16 ellipsis = string16(kEllipsisUTF16); | 483 const string16 ellipsis = string16(kEllipsisUTF16); |
| 457 StringSlicer slicer(text, ellipsis, elide_in_middle); | 484 StringSlicer slicer(text, ellipsis, elide_in_middle); |
| 458 | 485 |
| 459 // 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 |
| 460 // half and try again. | 487 // half and try again. |
| 461 // This is caused by an int overflow in Pango (specifically, in | 488 // This is caused by an int overflow in Pango (specifically, in |
| 462 // 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 |
| 463 // 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 |
| 464 // return positive numbers again. Detecting that is probably not worth it | 491 // return positive numbers again. Detecting that is probably not worth it |
| 465 // (eliding way too much from a ridiculous string is probably still | 492 // (eliding way too much from a ridiculous string is probably still |
| 466 // 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. |
| 467 if (current_text_pixel_width <= 0 && !text.empty()) { | 494 if (current_text_pixel_width <= 0 && !text.empty()) { |
| 468 const string16 cut = slicer.CutString(text.length() / 2, false); | 495 const string16 cut = slicer.CutString(text.length() / 2, false); |
| 469 return ElideText(cut, font, available_pixel_width, elide_behavior); | 496 return ElideText(cut, font_list, available_pixel_width, elide_behavior); |
| 470 } | 497 } |
| 471 | 498 |
| 472 if (current_text_pixel_width <= available_pixel_width) | 499 if (current_text_pixel_width <= available_pixel_width) |
| 473 return text; | 500 return text; |
| 474 | 501 |
| 475 if (insert_ellipsis && font.GetStringWidth(ellipsis) > available_pixel_width) | 502 if (insert_ellipsis && |
| 503 gfx::GetStringWidth(ellipsis, font_list) > available_pixel_width) |
| 476 return string16(); | 504 return string16(); |
| 477 | 505 |
| 478 // Use binary search to compute the elided text. | 506 // Use binary search to compute the elided text. |
| 479 size_t lo = 0; | 507 size_t lo = 0; |
| 480 size_t hi = text.length() - 1; | 508 size_t hi = text.length() - 1; |
| 481 size_t guess; | 509 size_t guess; |
| 482 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | 510 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { |
| 483 // 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 |
| 484 // handle kerning/ligatures/etc. correctly. | 512 // handle kerning/ligatures/etc. correctly. |
| 485 const string16 cut = slicer.CutString(guess, insert_ellipsis); | 513 const string16 cut = slicer.CutString(guess, insert_ellipsis); |
| 486 const int guess_length = font.GetStringWidth(cut); | 514 const int guess_length = gfx::GetStringWidth(cut, font_list); |
| 487 // 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 |
| 488 // current string in half and start over. | 516 // current string in half and start over. |
| 489 if (guess_length <= 0) { | 517 if (guess_length <= 0) { |
| 490 return ElideText(slicer.CutString(guess / 2, false), | 518 return ElideText(slicer.CutString(guess / 2, false), |
| 491 font, available_pixel_width, elide_behavior); | 519 font_list, available_pixel_width, elide_behavior); |
| 492 } | 520 } |
| 493 if (guess_length > available_pixel_width) | 521 if (guess_length > available_pixel_width) |
| 494 hi = guess - 1; | 522 hi = guess - 1; |
| 495 else | 523 else |
| 496 lo = guess + 1; | 524 lo = guess + 1; |
| 497 } | 525 } |
| 498 | 526 |
| 499 return slicer.CutString(guess, insert_ellipsis); | 527 return slicer.CutString(guess, insert_ellipsis); |
| 500 } | 528 } |
| 501 | 529 |
| 530 string16 ElideText(const string16& text, |
| 531 const gfx::Font& font, |
| 532 int available_pixel_width, |
| 533 ElideBehavior elide_behavior) { |
| 534 return ElideText(text, gfx::FontList(font), available_pixel_width, |
| 535 elide_behavior); |
| 536 } |
| 537 |
| 502 SortedDisplayURL::SortedDisplayURL(const GURL& url, | 538 SortedDisplayURL::SortedDisplayURL(const GURL& url, |
| 503 const std::string& languages) { | 539 const std::string& languages) { |
| 504 net::AppendFormattedHost(url, languages, &sort_host_); | 540 net::AppendFormattedHost(url, languages, &sort_host_); |
| 505 string16 host_minus_www = net::StripWWW(sort_host_); | 541 string16 host_minus_www = net::StripWWW(sort_host_); |
| 506 url_parse::Parsed parsed; | 542 url_parse::Parsed parsed; |
| 507 display_url_ = | 543 display_url_ = |
| 508 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 544 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
| 509 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); | 545 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); |
| 510 if (sort_host_.length() > host_minus_www.length()) { | 546 if (sort_host_.length() > host_minus_www.length()) { |
| 511 prefix_end_ += sort_host_.length() - host_minus_www.length(); | 547 prefix_end_ += sort_host_.length() - host_minus_www.length(); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 } | 804 } |
| 769 ++current_row_; | 805 ++current_row_; |
| 770 current_col_ = 0; | 806 current_col_ = 0; |
| 771 } | 807 } |
| 772 | 808 |
| 773 // Internal class used to track progress of a rectangular text elide | 809 // Internal class used to track progress of a rectangular text elide |
| 774 // operation. Exists so the top-level ElideRectangleText() function | 810 // operation. Exists so the top-level ElideRectangleText() function |
| 775 // can be broken into smaller methods sharing this state. | 811 // can be broken into smaller methods sharing this state. |
| 776 class RectangleText { | 812 class RectangleText { |
| 777 public: | 813 public: |
| 778 RectangleText(const gfx::Font& font, | 814 RectangleText(const gfx::FontList& font_list, |
| 779 int available_pixel_width, | 815 int available_pixel_width, |
| 780 int available_pixel_height, | 816 int available_pixel_height, |
| 781 ui::WordWrapBehavior wrap_behavior, | 817 ui::WordWrapBehavior wrap_behavior, |
| 782 std::vector<string16>* lines) | 818 std::vector<string16>* lines) |
| 783 : font_(font), | 819 : font_list_(font_list), |
| 784 line_height_(font.GetHeight()), | 820 line_height_(font_list.GetHeight()), |
| 785 available_pixel_width_(available_pixel_width), | 821 available_pixel_width_(available_pixel_width), |
| 786 available_pixel_height_(available_pixel_height), | 822 available_pixel_height_(available_pixel_height), |
| 787 wrap_behavior_(wrap_behavior), | 823 wrap_behavior_(wrap_behavior), |
| 788 current_width_(0), | 824 current_width_(0), |
| 789 current_height_(0), | 825 current_height_(0), |
| 790 last_line_ended_in_lf_(false), | 826 last_line_ended_in_lf_(false), |
| 791 lines_(lines), | 827 lines_(lines), |
| 792 insufficient_width_(false), | 828 insufficient_width_(false), |
| 793 insufficient_height_(false) {} | 829 insufficient_height_(false) {} |
| 794 | 830 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 // running width by the specified amount. This is an optimization over | 862 // running width by the specified amount. This is an optimization over |
| 827 // |AddToCurrentLine()| when |text_width| is already known. | 863 // |AddToCurrentLine()| when |text_width| is already known. |
| 828 void AddToCurrentLineWithWidth(const string16& text, int text_width); | 864 void AddToCurrentLineWithWidth(const string16& text, int text_width); |
| 829 | 865 |
| 830 // Append the specified |text| to the current output line. | 866 // Append the specified |text| to the current output line. |
| 831 void AddToCurrentLine(const string16& text); | 867 void AddToCurrentLine(const string16& text); |
| 832 | 868 |
| 833 // Set the current position to the beginning of the next line. | 869 // Set the current position to the beginning of the next line. |
| 834 bool NewLine(); | 870 bool NewLine(); |
| 835 | 871 |
| 836 // The font used for measuring text width. | 872 // The font list used for measuring text width. |
| 837 const gfx::Font& font_; | 873 const gfx::FontList& font_list_; |
| 838 | 874 |
| 839 // The height of each line of text. | 875 // The height of each line of text. |
| 840 const int line_height_; | 876 const int line_height_; |
| 841 | 877 |
| 842 // The number of pixels of available width in the rectangle. | 878 // The number of pixels of available width in the rectangle. |
| 843 const int available_pixel_width_; | 879 const int available_pixel_width_; |
| 844 | 880 |
| 845 // The number of pixels of available height in the rectangle. | 881 // The number of pixels of available height in the rectangle. |
| 846 const int available_pixel_height_; | 882 const int available_pixel_height_; |
| 847 | 883 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 if (lines_->back().empty() && !last_line_ended_in_lf_) | 936 if (lines_->back().empty() && !last_line_ended_in_lf_) |
| 901 lines_->pop_back(); | 937 lines_->pop_back(); |
| 902 } | 938 } |
| 903 if (last_line_ended_in_lf_) | 939 if (last_line_ended_in_lf_) |
| 904 lines_->push_back(string16()); | 940 lines_->push_back(string16()); |
| 905 return (insufficient_width_ ? ui::INSUFFICIENT_SPACE_HORIZONTAL : 0) | | 941 return (insufficient_width_ ? ui::INSUFFICIENT_SPACE_HORIZONTAL : 0) | |
| 906 (insufficient_height_ ? ui::INSUFFICIENT_SPACE_VERTICAL : 0); | 942 (insufficient_height_ ? ui::INSUFFICIENT_SPACE_VERTICAL : 0); |
| 907 } | 943 } |
| 908 | 944 |
| 909 void RectangleText::AddLine(const string16& line) { | 945 void RectangleText::AddLine(const string16& line) { |
| 910 const int line_width = font_.GetStringWidth(line); | 946 const int line_width = gfx::GetStringWidth(line, font_list_); |
| 911 if (line_width <= available_pixel_width_) { | 947 if (line_width <= available_pixel_width_) { |
| 912 AddToCurrentLineWithWidth(line, line_width); | 948 AddToCurrentLineWithWidth(line, line_width); |
| 913 } else { | 949 } else { |
| 914 // 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, |
| 915 // these are word boundaries but after any punctuation following the word. | 951 // these are word boundaries but after any punctuation following the word. |
| 916 base::i18n::BreakIterator words(line, | 952 base::i18n::BreakIterator words(line, |
| 917 base::i18n::BreakIterator::BREAK_LINE); | 953 base::i18n::BreakIterator::BREAK_LINE); |
| 918 if (words.Init()) { | 954 if (words.Init()) { |
| 919 while (words.Advance()) { | 955 while (words.Advance()) { |
| 920 const bool truncate = !current_line_.empty(); | 956 const bool truncate = !current_line_.empty(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 941 NewLine(); | 977 NewLine(); |
| 942 } | 978 } |
| 943 | 979 |
| 944 int RectangleText::WrapWord(const string16& word) { | 980 int RectangleText::WrapWord(const string16& word) { |
| 945 // Word is so wide that it must be fragmented. | 981 // Word is so wide that it must be fragmented. |
| 946 string16 text = word; | 982 string16 text = word; |
| 947 int lines_added = 0; | 983 int lines_added = 0; |
| 948 bool first_fragment = true; | 984 bool first_fragment = true; |
| 949 while (!insufficient_height_ && !text.empty()) { | 985 while (!insufficient_height_ && !text.empty()) { |
| 950 string16 fragment = | 986 string16 fragment = |
| 951 ui::ElideText(text, font_, available_pixel_width_, ui::TRUNCATE_AT_END); | 987 ui::ElideText(text, font_list_, available_pixel_width_, |
| 988 ui::TRUNCATE_AT_END); |
| 952 // At least one character has to be added at every line, even if the | 989 // At least one character has to be added at every line, even if the |
| 953 // available space is too small. | 990 // available space is too small. |
| 954 if(fragment.empty()) | 991 if(fragment.empty()) |
| 955 fragment = text.substr(0, 1); | 992 fragment = text.substr(0, 1); |
| 956 if (!first_fragment && NewLine()) | 993 if (!first_fragment && NewLine()) |
| 957 lines_added++; | 994 lines_added++; |
| 958 AddToCurrentLine(fragment); | 995 AddToCurrentLine(fragment); |
| 959 text = text.substr(fragment.length()); | 996 text = text.substr(fragment.length()); |
| 960 first_fragment = false; | 997 first_fragment = false; |
| 961 } | 998 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 975 if (wrap_behavior_ == ui::IGNORE_LONG_WORDS) { | 1012 if (wrap_behavior_ == ui::IGNORE_LONG_WORDS) { |
| 976 current_line_ = word; | 1013 current_line_ = word; |
| 977 current_width_ = available_pixel_width_; | 1014 current_width_ = available_pixel_width_; |
| 978 } else if (wrap_behavior_ == ui::WRAP_LONG_WORDS) { | 1015 } else if (wrap_behavior_ == ui::WRAP_LONG_WORDS) { |
| 979 lines_added += WrapWord(word); | 1016 lines_added += WrapWord(word); |
| 980 } else { | 1017 } else { |
| 981 const ui::ElideBehavior elide_behavior = | 1018 const ui::ElideBehavior elide_behavior = |
| 982 (wrap_behavior_ == ui::ELIDE_LONG_WORDS ? ui::ELIDE_AT_END : | 1019 (wrap_behavior_ == ui::ELIDE_LONG_WORDS ? ui::ELIDE_AT_END : |
| 983 ui::TRUNCATE_AT_END); | 1020 ui::TRUNCATE_AT_END); |
| 984 const string16 elided_word = | 1021 const string16 elided_word = |
| 985 ui::ElideText(word, font_, available_pixel_width_, elide_behavior); | 1022 ui::ElideText(word, font_list_, available_pixel_width_, elide_behavior); |
| 986 AddToCurrentLine(elided_word); | 1023 AddToCurrentLine(elided_word); |
| 987 insufficient_width_ = true; | 1024 insufficient_width_ = true; |
| 988 } | 1025 } |
| 989 | 1026 |
| 990 return lines_added; | 1027 return lines_added; |
| 991 } | 1028 } |
| 992 | 1029 |
| 993 int RectangleText::AddWord(const string16& word) { | 1030 int RectangleText::AddWord(const string16& word) { |
| 994 int lines_added = 0; | 1031 int lines_added = 0; |
| 995 string16 trimmed; | 1032 string16 trimmed; |
| 996 TrimWhitespace(word, TRIM_TRAILING, &trimmed); | 1033 TrimWhitespace(word, TRIM_TRAILING, &trimmed); |
| 997 const int trimmed_width = font_.GetStringWidth(trimmed); | 1034 const int trimmed_width = gfx::GetStringWidth(trimmed, font_list_); |
| 998 if (trimmed_width <= available_pixel_width_) { | 1035 if (trimmed_width <= available_pixel_width_) { |
| 999 // Word can be made to fit, no need to fragment it. | 1036 // Word can be made to fit, no need to fragment it. |
| 1000 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) | 1037 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) |
| 1001 lines_added++; | 1038 lines_added++; |
| 1002 // Append the non-trimmed word, in case more words are added after. | 1039 // Append the non-trimmed word, in case more words are added after. |
| 1003 AddToCurrentLine(word); | 1040 AddToCurrentLine(word); |
| 1004 } else { | 1041 } else { |
| 1005 lines_added = AddWordOverflow(wrap_behavior_ == ui::IGNORE_LONG_WORDS ? | 1042 lines_added = AddWordOverflow(wrap_behavior_ == ui::IGNORE_LONG_WORDS ? |
| 1006 trimmed : word); | 1043 trimmed : word); |
| 1007 } | 1044 } |
| 1008 return lines_added; | 1045 return lines_added; |
| 1009 } | 1046 } |
| 1010 | 1047 |
| 1011 void RectangleText::AddToCurrentLine(const string16& text) { | 1048 void RectangleText::AddToCurrentLine(const string16& text) { |
| 1012 AddToCurrentLineWithWidth(text, font_.GetStringWidth(text)); | 1049 AddToCurrentLineWithWidth(text, gfx::GetStringWidth(text, font_list_)); |
| 1013 } | 1050 } |
| 1014 | 1051 |
| 1015 void RectangleText::AddToCurrentLineWithWidth(const string16& text, | 1052 void RectangleText::AddToCurrentLineWithWidth(const string16& text, |
| 1016 int text_width) { | 1053 int text_width) { |
| 1017 if (current_height_ >= available_pixel_height_) { | 1054 if (current_height_ >= available_pixel_height_) { |
| 1018 insufficient_height_ = true; | 1055 insufficient_height_ = true; |
| 1019 return; | 1056 return; |
| 1020 } | 1057 } |
| 1021 current_line_.append(text); | 1058 current_line_.append(text); |
| 1022 current_width_ += text_width; | 1059 current_width_ += text_width; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1042 | 1079 |
| 1043 bool ElideRectangleString(const string16& input, size_t max_rows, | 1080 bool ElideRectangleString(const string16& input, size_t max_rows, |
| 1044 size_t max_cols, bool strict, string16* output) { | 1081 size_t max_cols, bool strict, string16* output) { |
| 1045 RectangleString rect(max_rows, max_cols, strict, output); | 1082 RectangleString rect(max_rows, max_cols, strict, output); |
| 1046 rect.Init(); | 1083 rect.Init(); |
| 1047 rect.AddString(input); | 1084 rect.AddString(input); |
| 1048 return rect.Finalize(); | 1085 return rect.Finalize(); |
| 1049 } | 1086 } |
| 1050 | 1087 |
| 1051 int ElideRectangleText(const string16& input, | 1088 int ElideRectangleText(const string16& input, |
| 1052 const gfx::Font& font, | 1089 const gfx::FontList& font_list, |
| 1053 int available_pixel_width, | 1090 int available_pixel_width, |
| 1054 int available_pixel_height, | 1091 int available_pixel_height, |
| 1055 WordWrapBehavior wrap_behavior, | 1092 WordWrapBehavior wrap_behavior, |
| 1056 std::vector<string16>* lines) { | 1093 std::vector<string16>* lines) { |
| 1057 RectangleText rect(font, | 1094 RectangleText rect(font_list, |
| 1058 available_pixel_width, | 1095 available_pixel_width, |
| 1059 available_pixel_height, | 1096 available_pixel_height, |
| 1060 wrap_behavior, | 1097 wrap_behavior, |
| 1061 lines); | 1098 lines); |
| 1062 rect.Init(); | 1099 rect.Init(); |
| 1063 rect.AddString(input); | 1100 rect.AddString(input); |
| 1064 return rect.Finalize(); | 1101 return rect.Finalize(); |
| 1065 } | 1102 } |
| 1066 | 1103 |
| 1104 int ElideRectangleText(const string16& input, |
| 1105 const gfx::Font& font, |
| 1106 int available_pixel_width, |
| 1107 int available_pixel_height, |
| 1108 WordWrapBehavior wrap_behavior, |
| 1109 std::vector<string16>* lines) { |
| 1110 return ElideRectangleText(input, gfx::FontList(font), |
| 1111 available_pixel_width, available_pixel_height, |
| 1112 wrap_behavior, lines); |
| 1113 } |
| 1114 |
| 1067 string16 TruncateString(const string16& string, size_t length) { | 1115 string16 TruncateString(const string16& string, size_t length) { |
| 1068 if (string.size() <= length) | 1116 if (string.size() <= length) |
| 1069 // String fits, return it. | 1117 // String fits, return it. |
| 1070 return string; | 1118 return string; |
| 1071 | 1119 |
| 1072 if (length == 0) | 1120 if (length == 0) |
| 1073 // No room for the elide string, return an empty string. | 1121 // No room for the elide string, return an empty string. |
| 1074 return string16(); | 1122 return string16(); |
| 1075 | 1123 |
| 1076 size_t max = length - 1; | 1124 size_t max = length - 1; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1120 index = char_iterator.getIndex(); | 1168 index = char_iterator.getIndex(); |
| 1121 } else { | 1169 } else { |
| 1122 // String has leading whitespace, return the elide string. | 1170 // String has leading whitespace, return the elide string. |
| 1123 return kElideString; | 1171 return kElideString; |
| 1124 } | 1172 } |
| 1125 } | 1173 } |
| 1126 return string.substr(0, index) + kElideString; | 1174 return string.substr(0, index) + kElideString; |
| 1127 } | 1175 } |
| 1128 | 1176 |
| 1129 } // namespace ui | 1177 } // namespace ui |
| OLD | NEW |