OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // This file implements utility functions for eliding and formatting UI text. | 5 // This file implements utility functions for eliding and formatting UI text. |
6 // | 6 // |
7 // Note that several of the functions declared in text_elider.h are implemented | 7 // Note that several of the functions declared in text_elider.h are implemented |
8 // in this file using helper classes in an unnamed namespace. | 8 // in this file using helper classes in an unnamed namespace. |
9 | 9 |
10 #include "ui/gfx/text_elider.h" | 10 #include "ui/gfx/text_elider.h" |
(...skipping 16 matching lines...) Expand all Loading... |
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_list.h" | 29 #include "ui/gfx/font_list.h" |
30 #include "ui/gfx/text_utils.h" | 30 #include "ui/gfx/text_utils.h" |
31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
32 | 32 |
33 namespace gfx { | 33 namespace gfx { |
34 | 34 |
35 // U+2026 in utf8 | 35 // U+2026 in utf8 |
36 const char kEllipsis[] = "\xE2\x80\xA6"; | 36 const char kEllipsis[] = "\xE2\x80\xA6"; |
37 const char16 kEllipsisUTF16[] = { 0x2026, 0 }; | 37 const base::char16 kEllipsisUTF16[] = { 0x2026, 0 }; |
38 const char16 kForwardSlash = '/'; | 38 const base::char16 kForwardSlash = '/'; |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. | 42 // Helper class to split + elide text, while respecting UTF16 surrogate pairs. |
43 class StringSlicer { | 43 class StringSlicer { |
44 public: | 44 public: |
45 StringSlicer(const string16& text, | 45 StringSlicer(const base::string16& text, |
46 const string16& ellipsis, | 46 const base::string16& ellipsis, |
47 bool elide_in_middle) | 47 bool elide_in_middle) |
48 : text_(text), | 48 : text_(text), |
49 ellipsis_(ellipsis), | 49 ellipsis_(ellipsis), |
50 elide_in_middle_(elide_in_middle) { | 50 elide_in_middle_(elide_in_middle) { |
51 } | 51 } |
52 | 52 |
53 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true, | 53 // Cuts |text_| to be |length| characters long. If |elide_in_middle_| is true, |
54 // the middle of the string is removed to leave equal-length pieces from the | 54 // the middle of the string is removed to leave equal-length pieces from the |
55 // beginning and end of the string; otherwise, the end of the string is | 55 // beginning and end of the string; otherwise, the end of the string is |
56 // removed and only the beginning remains. If |insert_ellipsis| is true, | 56 // removed and only the beginning remains. If |insert_ellipsis| is true, |
57 // then an ellipsis character will be inserted at the cut point. | 57 // then an ellipsis character will be inserted at the cut point. |
58 string16 CutString(size_t length, bool insert_ellipsis) { | 58 base::string16 CutString(size_t length, bool insert_ellipsis) { |
59 const string16 ellipsis_text = insert_ellipsis ? ellipsis_ : string16(); | 59 const base::string16 ellipsis_text = insert_ellipsis ? ellipsis_ |
| 60 : base::string16(); |
60 | 61 |
61 if (!elide_in_middle_) | 62 if (!elide_in_middle_) |
62 return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text; | 63 return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text; |
63 | 64 |
64 // We put the extra character, if any, before the cut. | 65 // We put the extra character, if any, before the cut. |
65 const size_t half_length = length / 2; | 66 const size_t half_length = length / 2; |
66 const size_t prefix_length = FindValidBoundaryBefore(length - half_length); | 67 const size_t prefix_length = FindValidBoundaryBefore(length - half_length); |
67 const size_t suffix_start_guess = text_.length() - half_length; | 68 const size_t suffix_start_guess = text_.length() - half_length; |
68 const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); | 69 const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); |
69 const size_t suffix_length = | 70 const size_t suffix_length = |
(...skipping 13 matching lines...) Expand all Loading... |
83 | 84 |
84 // Returns a valid cut boundary at or after |index|. | 85 // Returns a valid cut boundary at or after |index|. |
85 size_t FindValidBoundaryAfter(size_t index) const { | 86 size_t FindValidBoundaryAfter(size_t index) const { |
86 DCHECK_LE(index, text_.length()); | 87 DCHECK_LE(index, text_.length()); |
87 if (index != text_.length()) | 88 if (index != text_.length()) |
88 U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length()); | 89 U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length()); |
89 return index; | 90 return index; |
90 } | 91 } |
91 | 92 |
92 // The text to be sliced. | 93 // The text to be sliced. |
93 const string16& text_; | 94 const base::string16& text_; |
94 | 95 |
95 // Ellipsis string to use. | 96 // Ellipsis string to use. |
96 const string16& ellipsis_; | 97 const base::string16& ellipsis_; |
97 | 98 |
98 // If true, the middle of the string will be elided. | 99 // If true, the middle of the string will be elided. |
99 bool elide_in_middle_; | 100 bool elide_in_middle_; |
100 | 101 |
101 DISALLOW_COPY_AND_ASSIGN(StringSlicer); | 102 DISALLOW_COPY_AND_ASSIGN(StringSlicer); |
102 }; | 103 }; |
103 | 104 |
104 // Build a path from the first |num_components| elements in |path_elements|. | 105 // Build a path from the first |num_components| elements in |path_elements|. |
105 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. | 106 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. |
106 string16 BuildPathFromComponents(const string16& path_prefix, | 107 base::string16 BuildPathFromComponents( |
107 const std::vector<string16>& path_elements, | 108 const base::string16& path_prefix, |
108 const string16& filename, | 109 const std::vector<base::string16>& path_elements, |
109 size_t num_components) { | 110 const base::string16& filename, |
| 111 size_t num_components) { |
110 // Add the initial elements of the path. | 112 // Add the initial elements of the path. |
111 string16 path = path_prefix; | 113 base::string16 path = path_prefix; |
112 | 114 |
113 // Build path from first |num_components| elements. | 115 // Build path from first |num_components| elements. |
114 for (size_t j = 0; j < num_components; ++j) | 116 for (size_t j = 0; j < num_components; ++j) |
115 path += path_elements[j] + kForwardSlash; | 117 path += path_elements[j] + kForwardSlash; |
116 | 118 |
117 // Add |filename|, ellipsis if necessary. | 119 // Add |filename|, ellipsis if necessary. |
118 if (num_components != (path_elements.size() - 1)) | 120 if (num_components != (path_elements.size() - 1)) |
119 path += string16(kEllipsisUTF16) + kForwardSlash; | 121 path += base::string16(kEllipsisUTF16) + kForwardSlash; |
120 path += filename; | 122 path += filename; |
121 | 123 |
122 return path; | 124 return path; |
123 } | 125 } |
124 | 126 |
125 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path | 127 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path |
126 // components and elides if possible. Returns a string containing the longest | 128 // components and elides if possible. Returns a string containing the longest |
127 // possible elided path, or an empty string if elision is not possible. | 129 // possible elided path, or an empty string if elision is not possible. |
128 string16 ElideComponentizedPath(const string16& url_path_prefix, | 130 base::string16 ElideComponentizedPath( |
129 const std::vector<string16>& url_path_elements, | 131 const base::string16& url_path_prefix, |
130 const string16& url_filename, | 132 const std::vector<base::string16>& url_path_elements, |
131 const string16& url_query, | 133 const base::string16& url_filename, |
132 const FontList& font_list, | 134 const base::string16& url_query, |
133 float available_pixel_width) { | 135 const FontList& font_list, |
| 136 float available_pixel_width) { |
134 const size_t url_path_number_of_elements = url_path_elements.size(); | 137 const size_t url_path_number_of_elements = url_path_elements.size(); |
135 | 138 |
136 CHECK(url_path_number_of_elements); | 139 CHECK(url_path_number_of_elements); |
137 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { | 140 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { |
138 string16 elided_path = BuildPathFromComponents(url_path_prefix, | 141 base::string16 elided_path = BuildPathFromComponents(url_path_prefix, |
139 url_path_elements, url_filename, i); | 142 url_path_elements, url_filename, i); |
140 if (available_pixel_width >= GetStringWidthF(elided_path, font_list)) | 143 if (available_pixel_width >= GetStringWidthF(elided_path, font_list)) |
141 return ElideText(elided_path + url_query, font_list, | 144 return ElideText(elided_path + url_query, font_list, |
142 available_pixel_width, ELIDE_AT_END); | 145 available_pixel_width, ELIDE_AT_END); |
143 } | 146 } |
144 | 147 |
145 return string16(); | 148 return base::string16(); |
146 } | 149 } |
147 | 150 |
148 } // namespace | 151 } // namespace |
149 | 152 |
150 string16 ElideEmail(const string16& email, | 153 base::string16 ElideEmail(const base::string16& email, |
151 const FontList& font_list, | 154 const FontList& font_list, |
152 float available_pixel_width) { | 155 float available_pixel_width) { |
153 if (GetStringWidthF(email, font_list) <= available_pixel_width) | 156 if (GetStringWidthF(email, font_list) <= available_pixel_width) |
154 return email; | 157 return email; |
155 | 158 |
156 // Split the email into its local-part (username) and domain-part. The email | 159 // Split the email into its local-part (username) and domain-part. The email |
157 // spec technically allows for @ symbols in the local-part (username) of the | 160 // spec technically allows for @ symbols in the local-part (username) of the |
158 // email under some special requirements. It is guaranteed that there is no @ | 161 // email under some special requirements. It is guaranteed that there is no @ |
159 // symbol in the domain part of the email however so splitting at the last @ | 162 // symbol in the domain part of the email however so splitting at the last @ |
160 // symbol is safe. | 163 // symbol is safe. |
161 const size_t split_index = email.find_last_of('@'); | 164 const size_t split_index = email.find_last_of('@'); |
162 DCHECK_NE(split_index, string16::npos); | 165 DCHECK_NE(split_index, base::string16::npos); |
163 string16 username = email.substr(0, split_index); | 166 base::string16 username = email.substr(0, split_index); |
164 string16 domain = email.substr(split_index + 1); | 167 base::string16 domain = email.substr(split_index + 1); |
165 DCHECK(!username.empty()); | 168 DCHECK(!username.empty()); |
166 DCHECK(!domain.empty()); | 169 DCHECK(!domain.empty()); |
167 | 170 |
168 // Subtract the @ symbol from the available width as it is mandatory. | 171 // Subtract the @ symbol from the available width as it is mandatory. |
169 const string16 kAtSignUTF16 = ASCIIToUTF16("@"); | 172 const base::string16 kAtSignUTF16 = ASCIIToUTF16("@"); |
170 available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list); | 173 available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list); |
171 | 174 |
172 // Check whether eliding the domain is necessary: if eliding the username | 175 // Check whether eliding the domain is necessary: if eliding the username |
173 // is sufficient, the domain will not be elided. | 176 // is sufficient, the domain will not be elided. |
174 const float full_username_width = GetStringWidthF(username, font_list); | 177 const float full_username_width = GetStringWidthF(username, font_list); |
175 const float available_domain_width = | 178 const float available_domain_width = |
176 available_pixel_width - | 179 available_pixel_width - |
177 std::min(full_username_width, | 180 std::min(full_username_width, |
178 GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16, | 181 GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16, |
179 font_list)); | 182 font_list)); |
180 if (GetStringWidthF(domain, font_list) > available_domain_width) { | 183 if (GetStringWidthF(domain, font_list) > available_domain_width) { |
181 // Elide the domain so that it only takes half of the available width. | 184 // Elide the domain so that it only takes half of the available width. |
182 // Should the username not need all the width available in its half, the | 185 // Should the username not need all the width available in its half, the |
183 // domain will occupy the leftover width. | 186 // domain will occupy the leftover width. |
184 // If |desired_domain_width| is greater than |available_domain_width|: the | 187 // If |desired_domain_width| is greater than |available_domain_width|: the |
185 // minimal username elision allowed by the specifications will not fit; thus | 188 // minimal username elision allowed by the specifications will not fit; thus |
186 // |desired_domain_width| must be <= |available_domain_width| at all cost. | 189 // |desired_domain_width| must be <= |available_domain_width| at all cost. |
187 const float desired_domain_width = | 190 const float desired_domain_width = |
188 std::min(available_domain_width, | 191 std::min(available_domain_width, |
189 std::max(available_pixel_width - full_username_width, | 192 std::max(available_pixel_width - full_username_width, |
190 available_pixel_width / 2)); | 193 available_pixel_width / 2)); |
191 domain = ElideText(domain, font_list, desired_domain_width, | 194 domain = ElideText(domain, font_list, desired_domain_width, |
192 ELIDE_IN_MIDDLE); | 195 ELIDE_IN_MIDDLE); |
193 // Failing to elide the domain such that at least one character remains | 196 // Failing to elide the domain such that at least one character remains |
194 // (other than the ellipsis itself) remains: return a single ellipsis. | 197 // (other than the ellipsis itself) remains: return a single ellipsis. |
195 if (domain.length() <= 1U) | 198 if (domain.length() <= 1U) |
196 return string16(kEllipsisUTF16); | 199 return base::string16(kEllipsisUTF16); |
197 } | 200 } |
198 | 201 |
199 // Fit the username in the remaining width (at this point the elided username | 202 // Fit the username in the remaining width (at this point the elided username |
200 // is guaranteed to fit with at least one character remaining given all the | 203 // is guaranteed to fit with at least one character remaining given all the |
201 // precautions taken earlier). | 204 // precautions taken earlier). |
202 available_pixel_width -= GetStringWidthF(domain, font_list); | 205 available_pixel_width -= GetStringWidthF(domain, font_list); |
203 username = ElideText(username, font_list, available_pixel_width, | 206 username = ElideText(username, font_list, available_pixel_width, |
204 ELIDE_AT_END); | 207 ELIDE_AT_END); |
205 | 208 |
206 return username + kAtSignUTF16 + domain; | 209 return username + kAtSignUTF16 + domain; |
207 } | 210 } |
208 | 211 |
209 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 212 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
210 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 213 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of |
211 // a rendered string is always the sum of the widths of its substrings. Also I | 214 // a rendered string is always the sum of the widths of its substrings. Also I |
212 // suspect it could be made simpler. | 215 // suspect it could be made simpler. |
213 string16 ElideUrl(const GURL& url, | 216 base::string16 ElideUrl(const GURL& url, |
214 const FontList& font_list, | 217 const FontList& font_list, |
215 float available_pixel_width, | 218 float available_pixel_width, |
216 const std::string& languages) { | 219 const std::string& languages) { |
217 // Get a formatted string and corresponding parsing of the url. | 220 // Get a formatted string and corresponding parsing of the url. |
218 url_parse::Parsed parsed; | 221 url_parse::Parsed parsed; |
219 const string16 url_string = | 222 const base::string16 url_string = |
220 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 223 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
221 net::UnescapeRule::SPACES, &parsed, NULL, NULL); | 224 net::UnescapeRule::SPACES, &parsed, NULL, NULL); |
222 if (available_pixel_width <= 0) | 225 if (available_pixel_width <= 0) |
223 return url_string; | 226 return url_string; |
224 | 227 |
225 // If non-standard, return plain eliding. | 228 // If non-standard, return plain eliding. |
226 if (!url.IsStandard()) | 229 if (!url.IsStandard()) |
227 return ElideText(url_string, font_list, available_pixel_width, | 230 return ElideText(url_string, font_list, available_pixel_width, |
228 ELIDE_AT_END); | 231 ELIDE_AT_END); |
229 | 232 |
230 // Now start eliding url_string to fit within available pixel width. | 233 // Now start eliding url_string to fit within available pixel width. |
231 // Fist pass - check to see whether entire url_string fits. | 234 // Fist pass - check to see whether entire url_string fits. |
232 const float pixel_width_url_string = GetStringWidthF(url_string, font_list); | 235 const float pixel_width_url_string = GetStringWidthF(url_string, font_list); |
233 if (available_pixel_width >= pixel_width_url_string) | 236 if (available_pixel_width >= pixel_width_url_string) |
234 return url_string; | 237 return url_string; |
235 | 238 |
236 // Get the path substring, including query and reference. | 239 // Get the path substring, including query and reference. |
237 const size_t path_start_index = parsed.path.begin; | 240 const size_t path_start_index = parsed.path.begin; |
238 const size_t path_len = parsed.path.len; | 241 const size_t path_len = parsed.path.len; |
239 string16 url_path_query_etc = url_string.substr(path_start_index); | 242 base::string16 url_path_query_etc = url_string.substr(path_start_index); |
240 string16 url_path = url_string.substr(path_start_index, path_len); | 243 base::string16 url_path = url_string.substr(path_start_index, path_len); |
241 | 244 |
242 // Return general elided text if url minus the query fits. | 245 // Return general elided text if url minus the query fits. |
243 const string16 url_minus_query = | 246 const base::string16 url_minus_query = |
244 url_string.substr(0, path_start_index + path_len); | 247 url_string.substr(0, path_start_index + path_len); |
245 if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list)) | 248 if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list)) |
246 return ElideText(url_string, font_list, available_pixel_width, | 249 return ElideText(url_string, font_list, available_pixel_width, |
247 ELIDE_AT_END); | 250 ELIDE_AT_END); |
248 | 251 |
249 // Get Host. | 252 // Get Host. |
250 string16 url_host = UTF8ToUTF16(url.host()); | 253 base::string16 url_host = UTF8ToUTF16(url.host()); |
251 | 254 |
252 // Get domain and registry information from the URL. | 255 // Get domain and registry information from the URL. |
253 string16 url_domain = UTF8ToUTF16( | 256 base::string16 url_domain = UTF8ToUTF16( |
254 net::registry_controlled_domains::GetDomainAndRegistry( | 257 net::registry_controlled_domains::GetDomainAndRegistry( |
255 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); | 258 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
256 if (url_domain.empty()) | 259 if (url_domain.empty()) |
257 url_domain = url_host; | 260 url_domain = url_host; |
258 | 261 |
259 // Add port if required. | 262 // Add port if required. |
260 if (!url.port().empty()) { | 263 if (!url.port().empty()) { |
261 url_host += UTF8ToUTF16(":" + url.port()); | 264 url_host += UTF8ToUTF16(":" + url.port()); |
262 url_domain += UTF8ToUTF16(":" + url.port()); | 265 url_domain += UTF8ToUTF16(":" + url.port()); |
263 } | 266 } |
264 | 267 |
265 // Get sub domain. | 268 // Get sub domain. |
266 string16 url_subdomain; | 269 base::string16 url_subdomain; |
267 const size_t domain_start_index = url_host.find(url_domain); | 270 const size_t domain_start_index = url_host.find(url_domain); |
268 if (domain_start_index != string16::npos) | 271 if (domain_start_index != base::string16::npos) |
269 url_subdomain = url_host.substr(0, domain_start_index); | 272 url_subdomain = url_host.substr(0, domain_start_index); |
270 const string16 kWwwPrefix = UTF8ToUTF16("www."); | 273 const base::string16 kWwwPrefix = UTF8ToUTF16("www."); |
271 if ((url_subdomain == kWwwPrefix || url_subdomain.empty() || | 274 if ((url_subdomain == kWwwPrefix || url_subdomain.empty() || |
272 url.SchemeIsFile())) { | 275 url.SchemeIsFile())) { |
273 url_subdomain.clear(); | 276 url_subdomain.clear(); |
274 } | 277 } |
275 | 278 |
276 // If this is a file type, the path is now defined as everything after ":". | 279 // If this is a file type, the path is now defined as everything after ":". |
277 // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the | 280 // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the |
278 // domain is now C: - this is a nice hack for eliding to work pleasantly. | 281 // domain is now C: - this is a nice hack for eliding to work pleasantly. |
279 if (url.SchemeIsFile()) { | 282 if (url.SchemeIsFile()) { |
280 // Split the path string using ":" | 283 // Split the path string using ":" |
281 std::vector<string16> file_path_split; | 284 std::vector<base::string16> file_path_split; |
282 base::SplitString(url_path, ':', &file_path_split); | 285 base::SplitString(url_path, ':', &file_path_split); |
283 if (file_path_split.size() > 1) { // File is of type "file:///C:/.." | 286 if (file_path_split.size() > 1) { // File is of type "file:///C:/.." |
284 url_host.clear(); | 287 url_host.clear(); |
285 url_domain.clear(); | 288 url_domain.clear(); |
286 url_subdomain.clear(); | 289 url_subdomain.clear(); |
287 | 290 |
288 const string16 kColon = UTF8ToUTF16(":"); | 291 const base::string16 kColon = UTF8ToUTF16(":"); |
289 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; | 292 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; |
290 url_path_query_etc = url_path = file_path_split.at(1); | 293 url_path_query_etc = url_path = file_path_split.at(1); |
291 } | 294 } |
292 } | 295 } |
293 | 296 |
294 // Second Pass - remove scheme - the rest fits. | 297 // Second Pass - remove scheme - the rest fits. |
295 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); | 298 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); |
296 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc, | 299 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc, |
297 font_list); | 300 font_list); |
298 if (available_pixel_width >= | 301 if (available_pixel_width >= |
299 pixel_width_url_host + pixel_width_url_path) | 302 pixel_width_url_host + pixel_width_url_path) |
300 return url_host + url_path_query_etc; | 303 return url_host + url_path_query_etc; |
301 | 304 |
302 // Third Pass: Subdomain, domain and entire path fits. | 305 // Third Pass: Subdomain, domain and entire path fits. |
303 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); | 306 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); |
304 const float pixel_width_url_subdomain = | 307 const float pixel_width_url_subdomain = |
305 GetStringWidthF(url_subdomain, font_list); | 308 GetStringWidthF(url_subdomain, font_list); |
306 if (available_pixel_width >= | 309 if (available_pixel_width >= |
307 pixel_width_url_subdomain + pixel_width_url_domain + | 310 pixel_width_url_subdomain + pixel_width_url_domain + |
308 pixel_width_url_path) | 311 pixel_width_url_path) |
309 return url_subdomain + url_domain + url_path_query_etc; | 312 return url_subdomain + url_domain + url_path_query_etc; |
310 | 313 |
311 // Query element. | 314 // Query element. |
312 string16 url_query; | 315 base::string16 url_query; |
313 const float kPixelWidthDotsTrailer = GetStringWidthF( | 316 const float kPixelWidthDotsTrailer = GetStringWidthF( |
314 string16(kEllipsisUTF16), font_list); | 317 base::string16(kEllipsisUTF16), font_list); |
315 if (parsed.query.is_nonempty()) { | 318 if (parsed.query.is_nonempty()) { |
316 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); | 319 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); |
317 if (available_pixel_width >= | 320 if (available_pixel_width >= |
318 (pixel_width_url_subdomain + pixel_width_url_domain + | 321 (pixel_width_url_subdomain + pixel_width_url_domain + |
319 pixel_width_url_path - GetStringWidthF(url_query, font_list))) { | 322 pixel_width_url_path - GetStringWidthF(url_query, font_list))) { |
320 return ElideText(url_subdomain + url_domain + url_path_query_etc, | 323 return ElideText(url_subdomain + url_domain + url_path_query_etc, |
321 font_list, available_pixel_width, ELIDE_AT_END); | 324 font_list, available_pixel_width, ELIDE_AT_END); |
322 } | 325 } |
323 } | 326 } |
324 | 327 |
325 // Parse url_path using '/'. | 328 // Parse url_path using '/'. |
326 std::vector<string16> url_path_elements; | 329 std::vector<base::string16> url_path_elements; |
327 base::SplitString(url_path, kForwardSlash, &url_path_elements); | 330 base::SplitString(url_path, kForwardSlash, &url_path_elements); |
328 | 331 |
329 // Get filename - note that for a path ending with / | 332 // Get filename - note that for a path ending with / |
330 // such as www.google.com/intl/ads/, the file name is ads/. | 333 // such as www.google.com/intl/ads/, the file name is ads/. |
331 size_t url_path_number_of_elements = url_path_elements.size(); | 334 size_t url_path_number_of_elements = url_path_elements.size(); |
332 DCHECK(url_path_number_of_elements != 0); | 335 DCHECK(url_path_number_of_elements != 0); |
333 string16 url_filename; | 336 base::string16 url_filename; |
334 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) { | 337 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) { |
335 url_filename = *(url_path_elements.end() - 1); | 338 url_filename = *(url_path_elements.end() - 1); |
336 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'. | 339 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'. |
337 url_filename = url_path_elements.at(url_path_number_of_elements - 2) + | 340 url_filename = url_path_elements.at(url_path_number_of_elements - 2) + |
338 kForwardSlash; | 341 kForwardSlash; |
339 url_path_number_of_elements--; | 342 url_path_number_of_elements--; |
340 } | 343 } |
341 DCHECK(url_path_number_of_elements != 0); | 344 DCHECK(url_path_number_of_elements != 0); |
342 | 345 |
343 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; | 346 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; |
344 if (url_path_number_of_elements <= 1 || | 347 if (url_path_number_of_elements <= 1 || |
345 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { | 348 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { |
346 // No path to elide, or too long of a path (could overflow in loop below) | 349 // No path to elide, or too long of a path (could overflow in loop below) |
347 // Just elide this as a text string. | 350 // Just elide this as a text string. |
348 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, | 351 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, |
349 available_pixel_width, ELIDE_AT_END); | 352 available_pixel_width, ELIDE_AT_END); |
350 } | 353 } |
351 | 354 |
352 // Start eliding the path and replacing elements by ".../". | 355 // Start eliding the path and replacing elements by ".../". |
353 const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; | 356 const base::string16 kEllipsisAndSlash = base::string16(kEllipsisUTF16) + kFor
wardSlash; |
354 const float pixel_width_ellipsis_slash = | 357 const float pixel_width_ellipsis_slash = |
355 GetStringWidthF(kEllipsisAndSlash, font_list); | 358 GetStringWidthF(kEllipsisAndSlash, font_list); |
356 | 359 |
357 // Check with both subdomain and domain. | 360 // Check with both subdomain and domain. |
358 string16 elided_path = | 361 base::string16 elided_path = |
359 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, | 362 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, |
360 url_filename, url_query, font_list, | 363 url_filename, url_query, font_list, |
361 available_pixel_width); | 364 available_pixel_width); |
362 if (!elided_path.empty()) | 365 if (!elided_path.empty()) |
363 return elided_path; | 366 return elided_path; |
364 | 367 |
365 // Check with only domain. | 368 // Check with only domain. |
366 // If a subdomain is present, add an ellipsis before domain. | 369 // If a subdomain is present, add an ellipsis before domain. |
367 // This is added only if the subdomain pixel width is larger than | 370 // This is added only if the subdomain pixel width is larger than |
368 // the pixel width of kEllipsis. Otherwise, subdomain remains, | 371 // the pixel width of kEllipsis. Otherwise, subdomain remains, |
369 // which means that this case has been resolved earlier. | 372 // which means that this case has been resolved earlier. |
370 string16 url_elided_domain = url_subdomain + url_domain; | 373 base::string16 url_elided_domain = url_subdomain + url_domain; |
371 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { | 374 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { |
372 if (!url_subdomain.empty()) | 375 if (!url_subdomain.empty()) |
373 url_elided_domain = kEllipsisAndSlash[0] + url_domain; | 376 url_elided_domain = kEllipsisAndSlash[0] + url_domain; |
374 else | 377 else |
375 url_elided_domain = url_domain; | 378 url_elided_domain = url_domain; |
376 | 379 |
377 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, | 380 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, |
378 url_filename, url_query, font_list, | 381 url_filename, url_query, font_list, |
379 available_pixel_width); | 382 available_pixel_width); |
380 | 383 |
381 if (!elided_path.empty()) | 384 if (!elided_path.empty()) |
382 return elided_path; | 385 return elided_path; |
383 } | 386 } |
384 | 387 |
385 // Return elided domain/.../filename anyway. | 388 // Return elided domain/.../filename anyway. |
386 string16 final_elided_url_string(url_elided_domain); | 389 base::string16 final_elided_url_string(url_elided_domain); |
387 const float url_elided_domain_width = GetStringWidthF(url_elided_domain, | 390 const float url_elided_domain_width = GetStringWidthF(url_elided_domain, |
388 font_list); | 391 font_list); |
389 | 392 |
390 // A hack to prevent trailing ".../...". | 393 // A hack to prevent trailing ".../...". |
391 if ((available_pixel_width - url_elided_domain_width) > | 394 if ((available_pixel_width - url_elided_domain_width) > |
392 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + | 395 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + |
393 GetStringWidthF(ASCIIToUTF16("UV"), font_list)) { | 396 GetStringWidthF(ASCIIToUTF16("UV"), font_list)) { |
394 final_elided_url_string += BuildPathFromComponents(string16(), | 397 final_elided_url_string += BuildPathFromComponents(base::string16(), |
395 url_path_elements, url_filename, 1); | 398 url_path_elements, url_filename, 1); |
396 } else { | 399 } else { |
397 final_elided_url_string += url_path; | 400 final_elided_url_string += url_path; |
398 } | 401 } |
399 | 402 |
400 return ElideText(final_elided_url_string, font_list, available_pixel_width, | 403 return ElideText(final_elided_url_string, font_list, available_pixel_width, |
401 ELIDE_AT_END); | 404 ELIDE_AT_END); |
402 } | 405 } |
403 | 406 |
404 string16 ElideFilename(const base::FilePath& filename, | 407 base::string16 ElideFilename(const base::FilePath& filename, |
405 const FontList& font_list, | 408 const FontList& font_list, |
406 float available_pixel_width) { | 409 float available_pixel_width) { |
407 #if defined(OS_WIN) | 410 #if defined(OS_WIN) |
408 string16 filename_utf16 = filename.value(); | 411 base::string16 filename_utf16 = filename.value(); |
409 string16 extension = filename.Extension(); | 412 base::string16 extension = filename.Extension(); |
410 string16 rootname = filename.BaseName().RemoveExtension().value(); | 413 base::string16 rootname = filename.BaseName().RemoveExtension().value(); |
411 #elif defined(OS_POSIX) | 414 #elif defined(OS_POSIX) |
412 string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( | 415 base::string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( |
413 filename.value())); | 416 filename.value())); |
414 string16 extension = WideToUTF16(base::SysNativeMBToWide( | 417 base::string16 extension = WideToUTF16(base::SysNativeMBToWide( |
415 filename.Extension())); | 418 filename.Extension())); |
416 string16 rootname = WideToUTF16(base::SysNativeMBToWide( | 419 base::string16 rootname = WideToUTF16(base::SysNativeMBToWide( |
417 filename.BaseName().RemoveExtension().value())); | 420 filename.BaseName().RemoveExtension().value())); |
418 #endif | 421 #endif |
419 | 422 |
420 const float full_width = GetStringWidthF(filename_utf16, font_list); | 423 const float full_width = GetStringWidthF(filename_utf16, font_list); |
421 if (full_width <= available_pixel_width) | 424 if (full_width <= available_pixel_width) |
422 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); | 425 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); |
423 | 426 |
424 if (rootname.empty() || extension.empty()) { | 427 if (rootname.empty() || extension.empty()) { |
425 const string16 elided_name = ElideText(filename_utf16, font_list, | 428 const base::string16 elided_name = ElideText(filename_utf16, font_list, |
426 available_pixel_width, ELIDE_AT_END); | 429 available_pixel_width, ELIDE_AT_END); |
427 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 430 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
428 } | 431 } |
429 | 432 |
430 const float ext_width = GetStringWidthF(extension, font_list); | 433 const float ext_width = GetStringWidthF(extension, font_list); |
431 const float root_width = GetStringWidthF(rootname, font_list); | 434 const float root_width = GetStringWidthF(rootname, font_list); |
432 | 435 |
433 // We may have trimmed the path. | 436 // We may have trimmed the path. |
434 if (root_width + ext_width <= available_pixel_width) { | 437 if (root_width + ext_width <= available_pixel_width) { |
435 const string16 elided_name = rootname + extension; | 438 const base::string16 elided_name = rootname + extension; |
436 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 439 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
437 } | 440 } |
438 | 441 |
439 if (ext_width >= available_pixel_width) { | 442 if (ext_width >= available_pixel_width) { |
440 const string16 elided_name = ElideText(rootname + extension, font_list, | 443 const base::string16 elided_name = ElideText(rootname + extension, font_list
, |
441 available_pixel_width, | 444 available_pixel_width, |
442 ELIDE_IN_MIDDLE); | 445 ELIDE_IN_MIDDLE); |
443 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 446 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
444 } | 447 } |
445 | 448 |
446 float available_root_width = available_pixel_width - ext_width; | 449 float available_root_width = available_pixel_width - ext_width; |
447 string16 elided_name = | 450 base::string16 elided_name = |
448 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); | 451 ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); |
449 elided_name += extension; | 452 elided_name += extension; |
450 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | 453 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); |
451 } | 454 } |
452 | 455 |
453 string16 ElideText(const string16& text, | 456 base::string16 ElideText(const base::string16& text, |
454 const FontList& font_list, | 457 const FontList& font_list, |
455 float available_pixel_width, | 458 float available_pixel_width, |
456 ElideBehavior elide_behavior) { | 459 ElideBehavior elide_behavior) { |
457 if (text.empty()) | 460 if (text.empty()) |
458 return text; | 461 return text; |
459 | 462 |
460 const float current_text_pixel_width = GetStringWidthF(text, font_list); | 463 const float current_text_pixel_width = GetStringWidthF(text, font_list); |
461 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); | 464 const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); |
462 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); | 465 const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); |
463 | 466 |
464 const string16 ellipsis = string16(kEllipsisUTF16); | 467 const base::string16 ellipsis = base::string16(kEllipsisUTF16); |
465 StringSlicer slicer(text, ellipsis, elide_in_middle); | 468 StringSlicer slicer(text, ellipsis, elide_in_middle); |
466 | 469 |
467 // Pango will return 0 width for absurdly long strings. Cut the string in | 470 // Pango will return 0 width for absurdly long strings. Cut the string in |
468 // half and try again. | 471 // half and try again. |
469 // This is caused by an int overflow in Pango (specifically, in | 472 // This is caused by an int overflow in Pango (specifically, in |
470 // pango_glyph_string_extents_range). It's actually more subtle than just | 473 // pango_glyph_string_extents_range). It's actually more subtle than just |
471 // returning 0, since on super absurdly long strings, the int can wrap and | 474 // returning 0, since on super absurdly long strings, the int can wrap and |
472 // return positive numbers again. Detecting that is probably not worth it | 475 // return positive numbers again. Detecting that is probably not worth it |
473 // (eliding way too much from a ridiculous string is probably still | 476 // (eliding way too much from a ridiculous string is probably still |
474 // ridiculous), but we should check other widths for bogus values as well. | 477 // ridiculous), but we should check other widths for bogus values as well. |
475 if (current_text_pixel_width <= 0 && !text.empty()) { | 478 if (current_text_pixel_width <= 0 && !text.empty()) { |
476 const string16 cut = slicer.CutString(text.length() / 2, false); | 479 const base::string16 cut = slicer.CutString(text.length() / 2, false); |
477 return ElideText(cut, font_list, available_pixel_width, elide_behavior); | 480 return ElideText(cut, font_list, available_pixel_width, elide_behavior); |
478 } | 481 } |
479 | 482 |
480 if (current_text_pixel_width <= available_pixel_width) | 483 if (current_text_pixel_width <= available_pixel_width) |
481 return text; | 484 return text; |
482 | 485 |
483 if (insert_ellipsis && | 486 if (insert_ellipsis && |
484 GetStringWidthF(ellipsis, font_list) > available_pixel_width) | 487 GetStringWidthF(ellipsis, font_list) > available_pixel_width) |
485 return string16(); | 488 return base::string16(); |
486 | 489 |
487 // Use binary search to compute the elided text. | 490 // Use binary search to compute the elided text. |
488 size_t lo = 0; | 491 size_t lo = 0; |
489 size_t hi = text.length() - 1; | 492 size_t hi = text.length() - 1; |
490 size_t guess; | 493 size_t guess; |
491 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | 494 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { |
492 // We check the length of the whole desired string at once to ensure we | 495 // We check the length of the whole desired string at once to ensure we |
493 // handle kerning/ligatures/etc. correctly. | 496 // handle kerning/ligatures/etc. correctly. |
494 const string16 cut = slicer.CutString(guess, insert_ellipsis); | 497 const base::string16 cut = slicer.CutString(guess, insert_ellipsis); |
495 const float guess_length = GetStringWidthF(cut, font_list); | 498 const float guess_length = GetStringWidthF(cut, font_list); |
496 // Check again that we didn't hit a Pango width overflow. If so, cut the | 499 // Check again that we didn't hit a Pango width overflow. If so, cut the |
497 // current string in half and start over. | 500 // current string in half and start over. |
498 if (guess_length <= 0) { | 501 if (guess_length <= 0) { |
499 return ElideText(slicer.CutString(guess / 2, false), | 502 return ElideText(slicer.CutString(guess / 2, false), |
500 font_list, available_pixel_width, elide_behavior); | 503 font_list, available_pixel_width, elide_behavior); |
501 } | 504 } |
502 if (guess_length > available_pixel_width) | 505 if (guess_length > available_pixel_width) |
503 hi = guess - 1; | 506 hi = guess - 1; |
504 else | 507 else |
505 lo = guess + 1; | 508 lo = guess + 1; |
506 } | 509 } |
507 | 510 |
508 return slicer.CutString(guess, insert_ellipsis); | 511 return slicer.CutString(guess, insert_ellipsis); |
509 } | 512 } |
510 | 513 |
511 string16 ElideText(const string16& text, | 514 base::string16 ElideText(const base::string16& text, |
512 const Font& font, | 515 const Font& font, |
513 float available_pixel_width, | 516 float available_pixel_width, |
514 ElideBehavior elide_behavior) { | 517 ElideBehavior elide_behavior) { |
515 return ElideText(text, FontList(font), available_pixel_width, elide_behavior); | 518 return ElideText(text, FontList(font), available_pixel_width, elide_behavior); |
516 } | 519 } |
517 | 520 |
518 SortedDisplayURL::SortedDisplayURL(const GURL& url, | 521 SortedDisplayURL::SortedDisplayURL(const GURL& url, |
519 const std::string& languages) { | 522 const std::string& languages) { |
520 net::AppendFormattedHost(url, languages, &sort_host_); | 523 net::AppendFormattedHost(url, languages, &sort_host_); |
521 string16 host_minus_www = net::StripWWW(sort_host_); | 524 base::string16 host_minus_www = net::StripWWW(sort_host_); |
522 url_parse::Parsed parsed; | 525 url_parse::Parsed parsed; |
523 display_url_ = | 526 display_url_ = |
524 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, | 527 net::FormatUrl(url, languages, net::kFormatUrlOmitAll, |
525 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); | 528 net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); |
526 if (sort_host_.length() > host_minus_www.length()) { | 529 if (sort_host_.length() > host_minus_www.length()) { |
527 prefix_end_ += sort_host_.length() - host_minus_www.length(); | 530 prefix_end_ += sort_host_.length() - host_minus_www.length(); |
528 sort_host_.swap(host_minus_www); | 531 sort_host_.swap(host_minus_www); |
529 } | 532 } |
530 } | 533 } |
531 | 534 |
(...skipping 11 matching lines...) Expand all Loading... |
543 static_cast<const UChar*>(sort_host_.c_str()), | 546 static_cast<const UChar*>(sort_host_.c_str()), |
544 static_cast<int>(sort_host_.length()), | 547 static_cast<int>(sort_host_.length()), |
545 static_cast<const UChar*>(other.sort_host_.c_str()), | 548 static_cast<const UChar*>(other.sort_host_.c_str()), |
546 static_cast<int>(other.sort_host_.length()), | 549 static_cast<int>(other.sort_host_.length()), |
547 compare_status); | 550 compare_status); |
548 DCHECK(U_SUCCESS(compare_status)); | 551 DCHECK(U_SUCCESS(compare_status)); |
549 if (host_compare_result != 0) | 552 if (host_compare_result != 0) |
550 return host_compare_result; | 553 return host_compare_result; |
551 | 554 |
552 // Hosts match, compare on the portion of the url after the host. | 555 // Hosts match, compare on the portion of the url after the host. |
553 string16 path = this->AfterHost(); | 556 base::string16 path = this->AfterHost(); |
554 string16 o_path = other.AfterHost(); | 557 base::string16 o_path = other.AfterHost(); |
555 compare_status = U_ZERO_ERROR; | 558 compare_status = U_ZERO_ERROR; |
556 UCollationResult path_compare_result = collator->compare( | 559 UCollationResult path_compare_result = collator->compare( |
557 static_cast<const UChar*>(path.c_str()), | 560 static_cast<const UChar*>(path.c_str()), |
558 static_cast<int>(path.length()), | 561 static_cast<int>(path.length()), |
559 static_cast<const UChar*>(o_path.c_str()), | 562 static_cast<const UChar*>(o_path.c_str()), |
560 static_cast<int>(o_path.length()), | 563 static_cast<int>(o_path.length()), |
561 compare_status); | 564 compare_status); |
562 DCHECK(U_SUCCESS(compare_status)); | 565 DCHECK(U_SUCCESS(compare_status)); |
563 if (path_compare_result != 0) | 566 if (path_compare_result != 0) |
564 return path_compare_result; | 567 return path_compare_result; |
565 | 568 |
566 // Hosts and paths match, compare on the complete url. This'll push the www. | 569 // Hosts and paths match, compare on the complete url. This'll push the www. |
567 // ones to the end. | 570 // ones to the end. |
568 compare_status = U_ZERO_ERROR; | 571 compare_status = U_ZERO_ERROR; |
569 UCollationResult display_url_compare_result = collator->compare( | 572 UCollationResult display_url_compare_result = collator->compare( |
570 static_cast<const UChar*>(display_url_.c_str()), | 573 static_cast<const UChar*>(display_url_.c_str()), |
571 static_cast<int>(display_url_.length()), | 574 static_cast<int>(display_url_.length()), |
572 static_cast<const UChar*>(other.display_url_.c_str()), | 575 static_cast<const UChar*>(other.display_url_.c_str()), |
573 static_cast<int>(other.display_url_.length()), | 576 static_cast<int>(other.display_url_.length()), |
574 compare_status); | 577 compare_status); |
575 DCHECK(U_SUCCESS(compare_status)); | 578 DCHECK(U_SUCCESS(compare_status)); |
576 return display_url_compare_result; | 579 return display_url_compare_result; |
577 } | 580 } |
578 | 581 |
579 string16 SortedDisplayURL::AfterHost() const { | 582 base::string16 SortedDisplayURL::AfterHost() const { |
580 const size_t slash_index = display_url_.find(sort_host_, prefix_end_); | 583 const size_t slash_index = display_url_.find(sort_host_, prefix_end_); |
581 if (slash_index == string16::npos) { | 584 if (slash_index == base::string16::npos) { |
582 NOTREACHED(); | 585 NOTREACHED(); |
583 return string16(); | 586 return base::string16(); |
584 } | 587 } |
585 return display_url_.substr(slash_index + sort_host_.length()); | 588 return display_url_.substr(slash_index + sort_host_.length()); |
586 } | 589 } |
587 | 590 |
588 bool ElideString(const string16& input, int max_len, string16* output) { | 591 bool ElideString(const base::string16& input, int max_len, |
| 592 base::string16* output) { |
589 DCHECK_GE(max_len, 0); | 593 DCHECK_GE(max_len, 0); |
590 if (static_cast<int>(input.length()) <= max_len) { | 594 if (static_cast<int>(input.length()) <= max_len) { |
591 output->assign(input); | 595 output->assign(input); |
592 return false; | 596 return false; |
593 } | 597 } |
594 | 598 |
595 switch (max_len) { | 599 switch (max_len) { |
596 case 0: | 600 case 0: |
597 output->clear(); | 601 output->clear(); |
598 break; | 602 break; |
(...skipping 24 matching lines...) Expand all Loading... |
623 } | 627 } |
624 | 628 |
625 namespace { | 629 namespace { |
626 | 630 |
627 // Internal class used to track progress of a rectangular string elide | 631 // Internal class used to track progress of a rectangular string elide |
628 // operation. Exists so the top-level ElideRectangleString() function | 632 // operation. Exists so the top-level ElideRectangleString() function |
629 // can be broken into smaller methods sharing this state. | 633 // can be broken into smaller methods sharing this state. |
630 class RectangleString { | 634 class RectangleString { |
631 public: | 635 public: |
632 RectangleString(size_t max_rows, size_t max_cols, | 636 RectangleString(size_t max_rows, size_t max_cols, |
633 bool strict, string16 *output) | 637 bool strict, base::string16 *output) |
634 : max_rows_(max_rows), | 638 : max_rows_(max_rows), |
635 max_cols_(max_cols), | 639 max_cols_(max_cols), |
636 current_row_(0), | 640 current_row_(0), |
637 current_col_(0), | 641 current_col_(0), |
638 strict_(strict), | 642 strict_(strict), |
639 suppressed_(false), | 643 suppressed_(false), |
640 output_(output) {} | 644 output_(output) {} |
641 | 645 |
642 // Perform deferred initializations following creation. Must be called | 646 // Perform deferred initializations following creation. Must be called |
643 // before any input can be added via AddString(). | 647 // before any input can be added via AddString(). |
644 void Init() { output_->clear(); } | 648 void Init() { output_->clear(); } |
645 | 649 |
646 // Add an input string, reformatting to fit the desired dimensions. | 650 // Add an input string, reformatting to fit the desired dimensions. |
647 // AddString() may be called multiple times to concatenate together | 651 // AddString() may be called multiple times to concatenate together |
648 // multiple strings into the region (the current caller doesn't do | 652 // multiple strings into the region (the current caller doesn't do |
649 // this, however). | 653 // this, however). |
650 void AddString(const string16& input); | 654 void AddString(const base::string16& input); |
651 | 655 |
652 // Perform any deferred output processing. Must be called after the | 656 // Perform any deferred output processing. Must be called after the |
653 // last AddString() call has occurred. | 657 // last AddString() call has occurred. |
654 bool Finalize(); | 658 bool Finalize(); |
655 | 659 |
656 private: | 660 private: |
657 // Add a line to the rectangular region at the current position, | 661 // Add a line to the rectangular region at the current position, |
658 // either by itself or by breaking it into words. | 662 // either by itself or by breaking it into words. |
659 void AddLine(const string16& line); | 663 void AddLine(const base::string16& line); |
660 | 664 |
661 // Add a word to the rectangular region at the current position, | 665 // Add a word to the rectangular region at the current position, |
662 // either by itself or by breaking it into characters. | 666 // either by itself or by breaking it into characters. |
663 void AddWord(const string16& word); | 667 void AddWord(const base::string16& word); |
664 | 668 |
665 // Add text to the output string if the rectangular boundaries | 669 // Add text to the output string if the rectangular boundaries |
666 // have not been exceeded, advancing the current position. | 670 // have not been exceeded, advancing the current position. |
667 void Append(const string16& string); | 671 void Append(const base::string16& string); |
668 | 672 |
669 // Set the current position to the beginning of the next line. If | 673 // Set the current position to the beginning of the next line. If |
670 // |output| is true, add a newline to the output string if the rectangular | 674 // |output| is true, add a newline to the output string if the rectangular |
671 // boundaries have not been exceeded. If |output| is false, we assume | 675 // boundaries have not been exceeded. If |output| is false, we assume |
672 // some other mechanism will (likely) do similar breaking after the fact. | 676 // some other mechanism will (likely) do similar breaking after the fact. |
673 void NewLine(bool output); | 677 void NewLine(bool output); |
674 | 678 |
675 // Maximum number of rows allowed in the output string. | 679 // Maximum number of rows allowed in the output string. |
676 size_t max_rows_; | 680 size_t max_rows_; |
677 | 681 |
(...skipping 11 matching lines...) Expand all Loading... |
689 // Current character position, should never exceed max_cols_. | 693 // Current character position, should never exceed max_cols_. |
690 size_t current_col_; | 694 size_t current_col_; |
691 | 695 |
692 // True when we do whitespace to newline conversions ourselves. | 696 // True when we do whitespace to newline conversions ourselves. |
693 bool strict_; | 697 bool strict_; |
694 | 698 |
695 // True when some of the input has been truncated. | 699 // True when some of the input has been truncated. |
696 bool suppressed_; | 700 bool suppressed_; |
697 | 701 |
698 // String onto which the output is accumulated. | 702 // String onto which the output is accumulated. |
699 string16* output_; | 703 base::string16* output_; |
700 | 704 |
701 DISALLOW_COPY_AND_ASSIGN(RectangleString); | 705 DISALLOW_COPY_AND_ASSIGN(RectangleString); |
702 }; | 706 }; |
703 | 707 |
704 void RectangleString::AddString(const string16& input) { | 708 void RectangleString::AddString(const base::string16& input) { |
705 base::i18n::BreakIterator lines(input, | 709 base::i18n::BreakIterator lines(input, |
706 base::i18n::BreakIterator::BREAK_NEWLINE); | 710 base::i18n::BreakIterator::BREAK_NEWLINE); |
707 if (lines.Init()) { | 711 if (lines.Init()) { |
708 while (lines.Advance()) | 712 while (lines.Advance()) |
709 AddLine(lines.GetString()); | 713 AddLine(lines.GetString()); |
710 } else { | 714 } else { |
711 NOTREACHED() << "BreakIterator (lines) init failed"; | 715 NOTREACHED() << "BreakIterator (lines) init failed"; |
712 } | 716 } |
713 } | 717 } |
714 | 718 |
715 bool RectangleString::Finalize() { | 719 bool RectangleString::Finalize() { |
716 if (suppressed_) { | 720 if (suppressed_) { |
717 output_->append(ASCIIToUTF16("...")); | 721 output_->append(ASCIIToUTF16("...")); |
718 return true; | 722 return true; |
719 } | 723 } |
720 return false; | 724 return false; |
721 } | 725 } |
722 | 726 |
723 void RectangleString::AddLine(const string16& line) { | 727 void RectangleString::AddLine(const base::string16& line) { |
724 if (line.length() < max_cols_) { | 728 if (line.length() < max_cols_) { |
725 Append(line); | 729 Append(line); |
726 } else { | 730 } else { |
727 base::i18n::BreakIterator words(line, | 731 base::i18n::BreakIterator words(line, |
728 base::i18n::BreakIterator::BREAK_SPACE); | 732 base::i18n::BreakIterator::BREAK_SPACE); |
729 if (words.Init()) { | 733 if (words.Init()) { |
730 while (words.Advance()) | 734 while (words.Advance()) |
731 AddWord(words.GetString()); | 735 AddWord(words.GetString()); |
732 } else { | 736 } else { |
733 NOTREACHED() << "BreakIterator (words) init failed"; | 737 NOTREACHED() << "BreakIterator (words) init failed"; |
734 } | 738 } |
735 } | 739 } |
736 // Account for naturally-occuring newlines. | 740 // Account for naturally-occuring newlines. |
737 ++current_row_; | 741 ++current_row_; |
738 current_col_ = 0; | 742 current_col_ = 0; |
739 } | 743 } |
740 | 744 |
741 void RectangleString::AddWord(const string16& word) { | 745 void RectangleString::AddWord(const base::string16& word) { |
742 if (word.length() < max_cols_) { | 746 if (word.length() < max_cols_) { |
743 // Word can be made to fit, no need to fragment it. | 747 // Word can be made to fit, no need to fragment it. |
744 if (current_col_ + word.length() >= max_cols_) | 748 if (current_col_ + word.length() >= max_cols_) |
745 NewLine(strict_); | 749 NewLine(strict_); |
746 Append(word); | 750 Append(word); |
747 } else { | 751 } else { |
748 // Word is so big that it must be fragmented. | 752 // Word is so big that it must be fragmented. |
749 int array_start = 0; | 753 int array_start = 0; |
750 int char_start = 0; | 754 int char_start = 0; |
751 base::i18n::UTF16CharIterator chars(&word); | 755 base::i18n::UTF16CharIterator chars(&word); |
752 while (!chars.end()) { | 756 while (!chars.end()) { |
753 // When boundary is hit, add as much as will fit on this line. | 757 // When boundary is hit, add as much as will fit on this line. |
754 if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { | 758 if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { |
755 Append(word.substr(array_start, chars.array_pos() - array_start)); | 759 Append(word.substr(array_start, chars.array_pos() - array_start)); |
756 NewLine(true); | 760 NewLine(true); |
757 array_start = chars.array_pos(); | 761 array_start = chars.array_pos(); |
758 char_start = chars.char_pos(); | 762 char_start = chars.char_pos(); |
759 } | 763 } |
760 chars.Advance(); | 764 chars.Advance(); |
761 } | 765 } |
762 // Add the last remaining fragment, if any. | 766 // Add the last remaining fragment, if any. |
763 if (array_start != chars.array_pos()) | 767 if (array_start != chars.array_pos()) |
764 Append(word.substr(array_start, chars.array_pos() - array_start)); | 768 Append(word.substr(array_start, chars.array_pos() - array_start)); |
765 } | 769 } |
766 } | 770 } |
767 | 771 |
768 void RectangleString::Append(const string16& string) { | 772 void RectangleString::Append(const base::string16& string) { |
769 if (current_row_ < max_rows_) | 773 if (current_row_ < max_rows_) |
770 output_->append(string); | 774 output_->append(string); |
771 else | 775 else |
772 suppressed_ = true; | 776 suppressed_ = true; |
773 current_col_ += string.length(); | 777 current_col_ += string.length(); |
774 } | 778 } |
775 | 779 |
776 void RectangleString::NewLine(bool output) { | 780 void RectangleString::NewLine(bool output) { |
777 if (current_row_ < max_rows_) { | 781 if (current_row_ < max_rows_) { |
778 if (output) | 782 if (output) |
779 output_->append(ASCIIToUTF16("\n")); | 783 output_->append(ASCIIToUTF16("\n")); |
780 } else { | 784 } else { |
781 suppressed_ = true; | 785 suppressed_ = true; |
782 } | 786 } |
783 ++current_row_; | 787 ++current_row_; |
784 current_col_ = 0; | 788 current_col_ = 0; |
785 } | 789 } |
786 | 790 |
787 // Internal class used to track progress of a rectangular text elide | 791 // Internal class used to track progress of a rectangular text elide |
788 // operation. Exists so the top-level ElideRectangleText() function | 792 // operation. Exists so the top-level ElideRectangleText() function |
789 // can be broken into smaller methods sharing this state. | 793 // can be broken into smaller methods sharing this state. |
790 class RectangleText { | 794 class RectangleText { |
791 public: | 795 public: |
792 RectangleText(const FontList& font_list, | 796 RectangleText(const FontList& font_list, |
793 float available_pixel_width, | 797 float available_pixel_width, |
794 int available_pixel_height, | 798 int available_pixel_height, |
795 WordWrapBehavior wrap_behavior, | 799 WordWrapBehavior wrap_behavior, |
796 std::vector<string16>* lines) | 800 std::vector<base::string16>* lines) |
797 : font_list_(font_list), | 801 : font_list_(font_list), |
798 line_height_(font_list.GetHeight()), | 802 line_height_(font_list.GetHeight()), |
799 available_pixel_width_(available_pixel_width), | 803 available_pixel_width_(available_pixel_width), |
800 available_pixel_height_(available_pixel_height), | 804 available_pixel_height_(available_pixel_height), |
801 wrap_behavior_(wrap_behavior), | 805 wrap_behavior_(wrap_behavior), |
802 current_width_(0), | 806 current_width_(0), |
803 current_height_(0), | 807 current_height_(0), |
804 last_line_ended_in_lf_(false), | 808 last_line_ended_in_lf_(false), |
805 lines_(lines), | 809 lines_(lines), |
806 insufficient_width_(false), | 810 insufficient_width_(false), |
807 insufficient_height_(false) {} | 811 insufficient_height_(false) {} |
808 | 812 |
809 // Perform deferred initializions following creation. Must be called | 813 // Perform deferred initializions following creation. Must be called |
810 // before any input can be added via AddString(). | 814 // before any input can be added via AddString(). |
811 void Init() { lines_->clear(); } | 815 void Init() { lines_->clear(); } |
812 | 816 |
813 // Add an input string, reformatting to fit the desired dimensions. | 817 // Add an input string, reformatting to fit the desired dimensions. |
814 // AddString() may be called multiple times to concatenate together | 818 // AddString() may be called multiple times to concatenate together |
815 // multiple strings into the region (the current caller doesn't do | 819 // multiple strings into the region (the current caller doesn't do |
816 // this, however). | 820 // this, however). |
817 void AddString(const string16& input); | 821 void AddString(const base::string16& input); |
818 | 822 |
819 // Perform any deferred output processing. Must be called after the last | 823 // Perform any deferred output processing. Must be called after the last |
820 // AddString() call has occured. Returns a combination of | 824 // AddString() call has occured. Returns a combination of |
821 // |ReformattingResultFlags| indicating whether the given width or height was | 825 // |ReformattingResultFlags| indicating whether the given width or height was |
822 // insufficient, leading to elision or truncation. | 826 // insufficient, leading to elision or truncation. |
823 int Finalize(); | 827 int Finalize(); |
824 | 828 |
825 private: | 829 private: |
826 // Add a line to the rectangular region at the current position, | 830 // Add a line to the rectangular region at the current position, |
827 // either by itself or by breaking it into words. | 831 // either by itself or by breaking it into words. |
828 void AddLine(const string16& line); | 832 void AddLine(const base::string16& line); |
829 | 833 |
830 // Wrap the specified word across multiple lines. | 834 // Wrap the specified word across multiple lines. |
831 int WrapWord(const string16& word); | 835 int WrapWord(const base::string16& word); |
832 | 836 |
833 // Add a long word - wrapping, eliding or truncating per the wrap behavior. | 837 // Add a long word - wrapping, eliding or truncating per the wrap behavior. |
834 int AddWordOverflow(const string16& word); | 838 int AddWordOverflow(const base::string16& word); |
835 | 839 |
836 // Add a word to the rectangluar region at the current position. | 840 // Add a word to the rectangluar region at the current position. |
837 int AddWord(const string16& word); | 841 int AddWord(const base::string16& word); |
838 | 842 |
839 // Append the specified |text| to the current output line, incrementing the | 843 // Append the specified |text| to the current output line, incrementing the |
840 // running width by the specified amount. This is an optimization over | 844 // running width by the specified amount. This is an optimization over |
841 // |AddToCurrentLine()| when |text_width| is already known. | 845 // |AddToCurrentLine()| when |text_width| is already known. |
842 void AddToCurrentLineWithWidth(const string16& text, float text_width); | 846 void AddToCurrentLineWithWidth(const base::string16& text, float text_width); |
843 | 847 |
844 // Append the specified |text| to the current output line. | 848 // Append the specified |text| to the current output line. |
845 void AddToCurrentLine(const string16& text); | 849 void AddToCurrentLine(const base::string16& text); |
846 | 850 |
847 // Set the current position to the beginning of the next line. | 851 // Set the current position to the beginning of the next line. |
848 bool NewLine(); | 852 bool NewLine(); |
849 | 853 |
850 // The font list used for measuring text width. | 854 // The font list used for measuring text width. |
851 const FontList& font_list_; | 855 const FontList& font_list_; |
852 | 856 |
853 // The height of each line of text. | 857 // The height of each line of text. |
854 const int line_height_; | 858 const int line_height_; |
855 | 859 |
856 // The number of pixels of available width in the rectangle. | 860 // The number of pixels of available width in the rectangle. |
857 const float available_pixel_width_; | 861 const float available_pixel_width_; |
858 | 862 |
859 // The number of pixels of available height in the rectangle. | 863 // The number of pixels of available height in the rectangle. |
860 const int available_pixel_height_; | 864 const int available_pixel_height_; |
861 | 865 |
862 // The wrap behavior for words that are too long to fit on a single line. | 866 // The wrap behavior for words that are too long to fit on a single line. |
863 const WordWrapBehavior wrap_behavior_; | 867 const WordWrapBehavior wrap_behavior_; |
864 | 868 |
865 // The current running width. | 869 // The current running width. |
866 float current_width_; | 870 float current_width_; |
867 | 871 |
868 // The current running height. | 872 // The current running height. |
869 int current_height_; | 873 int current_height_; |
870 | 874 |
871 // The current line of text. | 875 // The current line of text. |
872 string16 current_line_; | 876 base::string16 current_line_; |
873 | 877 |
874 // Indicates whether the last line ended with \n. | 878 // Indicates whether the last line ended with \n. |
875 bool last_line_ended_in_lf_; | 879 bool last_line_ended_in_lf_; |
876 | 880 |
877 // The output vector of lines. | 881 // The output vector of lines. |
878 std::vector<string16>* lines_; | 882 std::vector<base::string16>* lines_; |
879 | 883 |
880 // Indicates whether a word was so long that it had to be truncated or elided | 884 // Indicates whether a word was so long that it had to be truncated or elided |
881 // to fit the available width. | 885 // to fit the available width. |
882 bool insufficient_width_; | 886 bool insufficient_width_; |
883 | 887 |
884 // Indicates whether there were too many lines for the available height. | 888 // Indicates whether there were too many lines for the available height. |
885 bool insufficient_height_; | 889 bool insufficient_height_; |
886 | 890 |
887 DISALLOW_COPY_AND_ASSIGN(RectangleText); | 891 DISALLOW_COPY_AND_ASSIGN(RectangleText); |
888 }; | 892 }; |
889 | 893 |
890 void RectangleText::AddString(const string16& input) { | 894 void RectangleText::AddString(const base::string16& input) { |
891 base::i18n::BreakIterator lines(input, | 895 base::i18n::BreakIterator lines(input, |
892 base::i18n::BreakIterator::BREAK_NEWLINE); | 896 base::i18n::BreakIterator::BREAK_NEWLINE); |
893 if (lines.Init()) { | 897 if (lines.Init()) { |
894 while (!insufficient_height_ && lines.Advance()) { | 898 while (!insufficient_height_ && lines.Advance()) { |
895 string16 line = lines.GetString(); | 899 base::string16 line = lines.GetString(); |
896 // The BREAK_NEWLINE iterator will keep the trailing newline character, | 900 // The BREAK_NEWLINE iterator will keep the trailing newline character, |
897 // except in the case of the last line, which may not have one. Remove | 901 // except in the case of the last line, which may not have one. Remove |
898 // the newline character, if it exists. | 902 // the newline character, if it exists. |
899 last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n'; | 903 last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n'; |
900 if (last_line_ended_in_lf_) | 904 if (last_line_ended_in_lf_) |
901 line.resize(line.length() - 1); | 905 line.resize(line.length() - 1); |
902 AddLine(line); | 906 AddLine(line); |
903 } | 907 } |
904 } else { | 908 } else { |
905 NOTREACHED() << "BreakIterator (lines) init failed"; | 909 NOTREACHED() << "BreakIterator (lines) init failed"; |
906 } | 910 } |
907 } | 911 } |
908 | 912 |
909 int RectangleText::Finalize() { | 913 int RectangleText::Finalize() { |
910 // Remove trailing whitespace from the last line or remove the last line | 914 // Remove trailing whitespace from the last line or remove the last line |
911 // completely, if it's just whitespace. | 915 // completely, if it's just whitespace. |
912 if (!insufficient_height_ && !lines_->empty()) { | 916 if (!insufficient_height_ && !lines_->empty()) { |
913 TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back()); | 917 TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back()); |
914 if (lines_->back().empty() && !last_line_ended_in_lf_) | 918 if (lines_->back().empty() && !last_line_ended_in_lf_) |
915 lines_->pop_back(); | 919 lines_->pop_back(); |
916 } | 920 } |
917 if (last_line_ended_in_lf_) | 921 if (last_line_ended_in_lf_) |
918 lines_->push_back(string16()); | 922 lines_->push_back(base::string16()); |
919 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | | 923 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | |
920 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); | 924 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); |
921 } | 925 } |
922 | 926 |
923 void RectangleText::AddLine(const string16& line) { | 927 void RectangleText::AddLine(const base::string16& line) { |
924 const float line_width = GetStringWidthF(line, font_list_); | 928 const float line_width = GetStringWidthF(line, font_list_); |
925 if (line_width <= available_pixel_width_) { | 929 if (line_width <= available_pixel_width_) { |
926 AddToCurrentLineWithWidth(line, line_width); | 930 AddToCurrentLineWithWidth(line, line_width); |
927 } else { | 931 } else { |
928 // Iterate over positions that are valid to break the line at. In general, | 932 // Iterate over positions that are valid to break the line at. In general, |
929 // these are word boundaries but after any punctuation following the word. | 933 // these are word boundaries but after any punctuation following the word. |
930 base::i18n::BreakIterator words(line, | 934 base::i18n::BreakIterator words(line, |
931 base::i18n::BreakIterator::BREAK_LINE); | 935 base::i18n::BreakIterator::BREAK_LINE); |
932 if (words.Init()) { | 936 if (words.Init()) { |
933 while (words.Advance()) { | 937 while (words.Advance()) { |
934 const bool truncate = !current_line_.empty(); | 938 const bool truncate = !current_line_.empty(); |
935 const string16& word = words.GetString(); | 939 const base::string16& word = words.GetString(); |
936 const int lines_added = AddWord(word); | 940 const int lines_added = AddWord(word); |
937 if (lines_added) { | 941 if (lines_added) { |
938 if (truncate) { | 942 if (truncate) { |
939 // Trim trailing whitespace from the line that was added. | 943 // Trim trailing whitespace from the line that was added. |
940 const int line = lines_->size() - lines_added; | 944 const int line = lines_->size() - lines_added; |
941 TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line)); | 945 TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line)); |
942 } | 946 } |
943 if (ContainsOnlyWhitespace(word)) { | 947 if (ContainsOnlyWhitespace(word)) { |
944 // Skip the first space if the previous line was carried over. | 948 // Skip the first space if the previous line was carried over. |
945 current_width_ = 0; | 949 current_width_ = 0; |
946 current_line_.clear(); | 950 current_line_.clear(); |
947 } | 951 } |
948 } | 952 } |
949 } | 953 } |
950 } else { | 954 } else { |
951 NOTREACHED() << "BreakIterator (words) init failed"; | 955 NOTREACHED() << "BreakIterator (words) init failed"; |
952 } | 956 } |
953 } | 957 } |
954 // Account for naturally-occuring newlines. | 958 // Account for naturally-occuring newlines. |
955 NewLine(); | 959 NewLine(); |
956 } | 960 } |
957 | 961 |
958 int RectangleText::WrapWord(const string16& word) { | 962 int RectangleText::WrapWord(const base::string16& word) { |
959 // Word is so wide that it must be fragmented. | 963 // Word is so wide that it must be fragmented. |
960 string16 text = word; | 964 base::string16 text = word; |
961 int lines_added = 0; | 965 int lines_added = 0; |
962 bool first_fragment = true; | 966 bool first_fragment = true; |
963 while (!insufficient_height_ && !text.empty()) { | 967 while (!insufficient_height_ && !text.empty()) { |
964 string16 fragment = | 968 base::string16 fragment = |
965 ElideText(text, font_list_, available_pixel_width_, | 969 ElideText(text, font_list_, available_pixel_width_, |
966 TRUNCATE_AT_END); | 970 TRUNCATE_AT_END); |
967 // At least one character has to be added at every line, even if the | 971 // At least one character has to be added at every line, even if the |
968 // available space is too small. | 972 // available space is too small. |
969 if(fragment.empty()) | 973 if(fragment.empty()) |
970 fragment = text.substr(0, 1); | 974 fragment = text.substr(0, 1); |
971 if (!first_fragment && NewLine()) | 975 if (!first_fragment && NewLine()) |
972 lines_added++; | 976 lines_added++; |
973 AddToCurrentLine(fragment); | 977 AddToCurrentLine(fragment); |
974 text = text.substr(fragment.length()); | 978 text = text.substr(fragment.length()); |
975 first_fragment = false; | 979 first_fragment = false; |
976 } | 980 } |
977 return lines_added; | 981 return lines_added; |
978 } | 982 } |
979 | 983 |
980 int RectangleText::AddWordOverflow(const string16& word) { | 984 int RectangleText::AddWordOverflow(const base::string16& word) { |
981 int lines_added = 0; | 985 int lines_added = 0; |
982 | 986 |
983 // Unless this is the very first word, put it on a new line. | 987 // Unless this is the very first word, put it on a new line. |
984 if (!current_line_.empty()) { | 988 if (!current_line_.empty()) { |
985 if (!NewLine()) | 989 if (!NewLine()) |
986 return 0; | 990 return 0; |
987 lines_added++; | 991 lines_added++; |
988 } | 992 } |
989 | 993 |
990 if (wrap_behavior_ == IGNORE_LONG_WORDS) { | 994 if (wrap_behavior_ == IGNORE_LONG_WORDS) { |
991 current_line_ = word; | 995 current_line_ = word; |
992 current_width_ = available_pixel_width_; | 996 current_width_ = available_pixel_width_; |
993 } else if (wrap_behavior_ == WRAP_LONG_WORDS) { | 997 } else if (wrap_behavior_ == WRAP_LONG_WORDS) { |
994 lines_added += WrapWord(word); | 998 lines_added += WrapWord(word); |
995 } else { | 999 } else { |
996 const ElideBehavior elide_behavior = | 1000 const ElideBehavior elide_behavior = |
997 (wrap_behavior_ == ELIDE_LONG_WORDS ? ELIDE_AT_END : TRUNCATE_AT_END); | 1001 (wrap_behavior_ == ELIDE_LONG_WORDS ? ELIDE_AT_END : TRUNCATE_AT_END); |
998 const string16 elided_word = | 1002 const base::string16 elided_word = |
999 ElideText(word, font_list_, available_pixel_width_, elide_behavior); | 1003 ElideText(word, font_list_, available_pixel_width_, elide_behavior); |
1000 AddToCurrentLine(elided_word); | 1004 AddToCurrentLine(elided_word); |
1001 insufficient_width_ = true; | 1005 insufficient_width_ = true; |
1002 } | 1006 } |
1003 | 1007 |
1004 return lines_added; | 1008 return lines_added; |
1005 } | 1009 } |
1006 | 1010 |
1007 int RectangleText::AddWord(const string16& word) { | 1011 int RectangleText::AddWord(const base::string16& word) { |
1008 int lines_added = 0; | 1012 int lines_added = 0; |
1009 string16 trimmed; | 1013 base::string16 trimmed; |
1010 TrimWhitespace(word, TRIM_TRAILING, &trimmed); | 1014 TrimWhitespace(word, TRIM_TRAILING, &trimmed); |
1011 const float trimmed_width = GetStringWidthF(trimmed, font_list_); | 1015 const float trimmed_width = GetStringWidthF(trimmed, font_list_); |
1012 if (trimmed_width <= available_pixel_width_) { | 1016 if (trimmed_width <= available_pixel_width_) { |
1013 // Word can be made to fit, no need to fragment it. | 1017 // Word can be made to fit, no need to fragment it. |
1014 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) | 1018 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) |
1015 lines_added++; | 1019 lines_added++; |
1016 // Append the non-trimmed word, in case more words are added after. | 1020 // Append the non-trimmed word, in case more words are added after. |
1017 AddToCurrentLine(word); | 1021 AddToCurrentLine(word); |
1018 } else { | 1022 } else { |
1019 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? | 1023 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? |
1020 trimmed : word); | 1024 trimmed : word); |
1021 } | 1025 } |
1022 return lines_added; | 1026 return lines_added; |
1023 } | 1027 } |
1024 | 1028 |
1025 void RectangleText::AddToCurrentLine(const string16& text) { | 1029 void RectangleText::AddToCurrentLine(const base::string16& text) { |
1026 AddToCurrentLineWithWidth(text, GetStringWidthF(text, font_list_)); | 1030 AddToCurrentLineWithWidth(text, GetStringWidthF(text, font_list_)); |
1027 } | 1031 } |
1028 | 1032 |
1029 void RectangleText::AddToCurrentLineWithWidth(const string16& text, | 1033 void RectangleText::AddToCurrentLineWithWidth(const base::string16& text, |
1030 float text_width) { | 1034 float text_width) { |
1031 if (current_height_ >= available_pixel_height_) { | 1035 if (current_height_ >= available_pixel_height_) { |
1032 insufficient_height_ = true; | 1036 insufficient_height_ = true; |
1033 return; | 1037 return; |
1034 } | 1038 } |
1035 current_line_.append(text); | 1039 current_line_.append(text); |
1036 current_width_ += text_width; | 1040 current_width_ += text_width; |
1037 } | 1041 } |
1038 | 1042 |
1039 bool RectangleText::NewLine() { | 1043 bool RectangleText::NewLine() { |
1040 bool line_added = false; | 1044 bool line_added = false; |
1041 if (current_height_ < available_pixel_height_) { | 1045 if (current_height_ < available_pixel_height_) { |
1042 lines_->push_back(current_line_); | 1046 lines_->push_back(current_line_); |
1043 current_line_.clear(); | 1047 current_line_.clear(); |
1044 line_added = true; | 1048 line_added = true; |
1045 } else { | 1049 } else { |
1046 insufficient_height_ = true; | 1050 insufficient_height_ = true; |
1047 } | 1051 } |
1048 current_height_ += line_height_; | 1052 current_height_ += line_height_; |
1049 current_width_ = 0; | 1053 current_width_ = 0; |
1050 return line_added; | 1054 return line_added; |
1051 } | 1055 } |
1052 | 1056 |
1053 } // namespace | 1057 } // namespace |
1054 | 1058 |
1055 bool ElideRectangleString(const string16& input, size_t max_rows, | 1059 bool ElideRectangleString(const base::string16& input, size_t max_rows, |
1056 size_t max_cols, bool strict, string16* output) { | 1060 size_t max_cols, bool strict, |
| 1061 base::string16* output) { |
1057 RectangleString rect(max_rows, max_cols, strict, output); | 1062 RectangleString rect(max_rows, max_cols, strict, output); |
1058 rect.Init(); | 1063 rect.Init(); |
1059 rect.AddString(input); | 1064 rect.AddString(input); |
1060 return rect.Finalize(); | 1065 return rect.Finalize(); |
1061 } | 1066 } |
1062 | 1067 |
1063 int ElideRectangleText(const string16& input, | 1068 int ElideRectangleText(const base::string16& input, |
1064 const FontList& font_list, | 1069 const FontList& font_list, |
1065 float available_pixel_width, | 1070 float available_pixel_width, |
1066 int available_pixel_height, | 1071 int available_pixel_height, |
1067 WordWrapBehavior wrap_behavior, | 1072 WordWrapBehavior wrap_behavior, |
1068 std::vector<string16>* lines) { | 1073 std::vector<base::string16>* lines) { |
1069 RectangleText rect(font_list, | 1074 RectangleText rect(font_list, |
1070 available_pixel_width, | 1075 available_pixel_width, |
1071 available_pixel_height, | 1076 available_pixel_height, |
1072 wrap_behavior, | 1077 wrap_behavior, |
1073 lines); | 1078 lines); |
1074 rect.Init(); | 1079 rect.Init(); |
1075 rect.AddString(input); | 1080 rect.AddString(input); |
1076 return rect.Finalize(); | 1081 return rect.Finalize(); |
1077 } | 1082 } |
1078 | 1083 |
1079 string16 TruncateString(const string16& string, size_t length) { | 1084 base::string16 TruncateString(const base::string16& string, size_t length) { |
1080 if (string.size() <= length) | 1085 if (string.size() <= length) |
1081 // String fits, return it. | 1086 // String fits, return it. |
1082 return string; | 1087 return string; |
1083 | 1088 |
1084 if (length == 0) | 1089 if (length == 0) |
1085 // No room for the elide string, return an empty string. | 1090 // No room for the elide string, return an empty string. |
1086 return string16(); | 1091 return base::string16(); |
1087 | 1092 |
1088 size_t max = length - 1; | 1093 size_t max = length - 1; |
1089 | 1094 |
1090 // Added to the end of strings that are too big. | 1095 // Added to the end of strings that are too big. |
1091 static const char16 kElideString[] = { 0x2026, 0 }; | 1096 static const base::char16 kElideString[] = { 0x2026, 0 }; |
1092 | 1097 |
1093 if (max == 0) | 1098 if (max == 0) |
1094 // Just enough room for the elide string. | 1099 // Just enough room for the elide string. |
1095 return kElideString; | 1100 return kElideString; |
1096 | 1101 |
1097 // Use a line iterator to find the first boundary. | 1102 // Use a line iterator to find the first boundary. |
1098 UErrorCode status = U_ZERO_ERROR; | 1103 UErrorCode status = U_ZERO_ERROR; |
1099 scoped_ptr<icu::RuleBasedBreakIterator> bi( | 1104 scoped_ptr<icu::RuleBasedBreakIterator> bi( |
1100 static_cast<icu::RuleBasedBreakIterator*>( | 1105 static_cast<icu::RuleBasedBreakIterator*>( |
1101 icu::RuleBasedBreakIterator::createLineInstance( | 1106 icu::RuleBasedBreakIterator::createLineInstance( |
(...skipping 30 matching lines...) Expand all Loading... |
1132 index = char_iterator.getIndex(); | 1137 index = char_iterator.getIndex(); |
1133 } else { | 1138 } else { |
1134 // String has leading whitespace, return the elide string. | 1139 // String has leading whitespace, return the elide string. |
1135 return kElideString; | 1140 return kElideString; |
1136 } | 1141 } |
1137 } | 1142 } |
1138 return string.substr(0, index) + kElideString; | 1143 return string.substr(0, index) + kElideString; |
1139 } | 1144 } |
1140 | 1145 |
1141 } // namespace gfx | 1146 } // namespace gfx |
OLD | NEW |