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