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