| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // This file implements utility functions for eliding and formatting UI text. | |
| 6 // | |
| 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. | |
| 9 | |
| 10 #include "ui/gfx/text_elider.h" | |
| 11 | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/i18n/break_iterator.h" | |
| 17 #include "base/i18n/char_iterator.h" | |
| 18 #include "base/i18n/rtl.h" | |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/numerics/safe_conversions.h" | |
| 21 #include "base/strings/string_split.h" | |
| 22 #include "base/strings/string_util.h" | |
| 23 #include "base/strings/sys_string_conversions.h" | |
| 24 #include "base/strings/utf_string_conversions.h" | |
| 25 #include "third_party/icu/source/common/unicode/rbbi.h" | |
| 26 #include "third_party/icu/source/common/unicode/uloc.h" | |
| 27 #include "ui/gfx/font_list.h" | |
| 28 #include "ui/gfx/render_text.h" | |
| 29 #include "ui/gfx/text_utils.h" | |
| 30 | |
| 31 using base::ASCIIToUTF16; | |
| 32 using base::UTF8ToUTF16; | |
| 33 using base::WideToUTF16; | |
| 34 | |
| 35 namespace gfx { | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 40 // The returned string will have at least one character besides the ellipsis | |
| 41 // on either side of '@'; if that's impossible, a single ellipsis is returned. | |
| 42 // If possible, only the username is elided. Otherwise, the domain is elided | |
| 43 // in the middle, splitting available width equally with the elided username. | |
| 44 // If the username is short enough that it doesn't need half the available | |
| 45 // width, the elided domain will occupy that extra width. | |
| 46 base::string16 ElideEmail(const base::string16& email, | |
| 47 const FontList& font_list, | |
| 48 float available_pixel_width) { | |
| 49 if (GetStringWidthF(email, font_list) <= available_pixel_width) | |
| 50 return email; | |
| 51 | |
| 52 // Split the email into its local-part (username) and domain-part. The email | |
| 53 // spec allows for @ symbols in the username under some special requirements, | |
| 54 // but not in the domain part, so splitting at the last @ symbol is safe. | |
| 55 const size_t split_index = email.find_last_of('@'); | |
| 56 DCHECK_NE(split_index, base::string16::npos); | |
| 57 base::string16 username = email.substr(0, split_index); | |
| 58 base::string16 domain = email.substr(split_index + 1); | |
| 59 DCHECK(!username.empty()); | |
| 60 DCHECK(!domain.empty()); | |
| 61 | |
| 62 // Subtract the @ symbol from the available width as it is mandatory. | |
| 63 const base::string16 kAtSignUTF16 = ASCIIToUTF16("@"); | |
| 64 available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list); | |
| 65 | |
| 66 // Check whether eliding the domain is necessary: if eliding the username | |
| 67 // is sufficient, the domain will not be elided. | |
| 68 const float full_username_width = GetStringWidthF(username, font_list); | |
| 69 const float available_domain_width = | |
| 70 available_pixel_width - | |
| 71 std::min(full_username_width, | |
| 72 GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16, | |
| 73 font_list)); | |
| 74 if (GetStringWidthF(domain, font_list) > available_domain_width) { | |
| 75 // Elide the domain so that it only takes half of the available width. | |
| 76 // Should the username not need all the width available in its half, the | |
| 77 // domain will occupy the leftover width. | |
| 78 // If |desired_domain_width| is greater than |available_domain_width|: the | |
| 79 // minimal username elision allowed by the specifications will not fit; thus | |
| 80 // |desired_domain_width| must be <= |available_domain_width| at all cost. | |
| 81 const float desired_domain_width = | |
| 82 std::min(available_domain_width, | |
| 83 std::max(available_pixel_width - full_username_width, | |
| 84 available_pixel_width / 2)); | |
| 85 domain = ElideText(domain, font_list, desired_domain_width, ELIDE_MIDDLE); | |
| 86 // Failing to elide the domain such that at least one character remains | |
| 87 // (other than the ellipsis itself) remains: return a single ellipsis. | |
| 88 if (domain.length() <= 1U) | |
| 89 return base::string16(kEllipsisUTF16); | |
| 90 } | |
| 91 | |
| 92 // Fit the username in the remaining width (at this point the elided username | |
| 93 // is guaranteed to fit with at least one character remaining given all the | |
| 94 // precautions taken earlier). | |
| 95 available_pixel_width -= GetStringWidthF(domain, font_list); | |
| 96 username = ElideText(username, font_list, available_pixel_width, ELIDE_TAIL); | |
| 97 return username + kAtSignUTF16 + domain; | |
| 98 } | |
| 99 #endif | |
| 100 | |
| 101 } // namespace | |
| 102 | |
| 103 // U+2026 in utf8 | |
| 104 const char kEllipsis[] = "\xE2\x80\xA6"; | |
| 105 const base::char16 kEllipsisUTF16[] = { 0x2026, 0 }; | |
| 106 const base::char16 kForwardSlash = '/'; | |
| 107 | |
| 108 StringSlicer::StringSlicer(const base::string16& text, | |
| 109 const base::string16& ellipsis, | |
| 110 bool elide_in_middle, | |
| 111 bool elide_at_beginning) | |
| 112 : text_(text), | |
| 113 ellipsis_(ellipsis), | |
| 114 elide_in_middle_(elide_in_middle), | |
| 115 elide_at_beginning_(elide_at_beginning) { | |
| 116 } | |
| 117 | |
| 118 base::string16 StringSlicer::CutString(size_t length, bool insert_ellipsis) { | |
| 119 const base::string16 ellipsis_text = insert_ellipsis ? ellipsis_ | |
| 120 : base::string16(); | |
| 121 | |
| 122 if (elide_at_beginning_) | |
| 123 return ellipsis_text + | |
| 124 text_.substr(FindValidBoundaryBefore(text_.length() - length)); | |
| 125 | |
| 126 if (!elide_in_middle_) | |
| 127 return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text; | |
| 128 | |
| 129 // We put the extra character, if any, before the cut. | |
| 130 const size_t half_length = length / 2; | |
| 131 const size_t prefix_length = FindValidBoundaryBefore(length - half_length); | |
| 132 const size_t suffix_start_guess = text_.length() - half_length; | |
| 133 const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); | |
| 134 const size_t suffix_length = | |
| 135 half_length - (suffix_start_guess - suffix_start); | |
| 136 return text_.substr(0, prefix_length) + ellipsis_text + | |
| 137 text_.substr(suffix_start, suffix_length); | |
| 138 } | |
| 139 | |
| 140 size_t StringSlicer::FindValidBoundaryBefore(size_t index) const { | |
| 141 DCHECK_LE(index, text_.length()); | |
| 142 if (index != text_.length()) | |
| 143 U16_SET_CP_START(text_.data(), 0, index); | |
| 144 return index; | |
| 145 } | |
| 146 | |
| 147 size_t StringSlicer::FindValidBoundaryAfter(size_t index) const { | |
| 148 DCHECK_LE(index, text_.length()); | |
| 149 if (index == text_.length()) | |
| 150 return index; | |
| 151 | |
| 152 int32_t text_index = base::checked_cast<int32_t>(index); | |
| 153 int32_t text_length = base::checked_cast<int32_t>(text_.length()); | |
| 154 U16_SET_CP_LIMIT(text_.data(), 0, text_index, text_length); | |
| 155 return static_cast<size_t>(text_index); | |
| 156 } | |
| 157 | |
| 158 base::string16 ElideFilename(const base::FilePath& filename, | |
| 159 const FontList& font_list, | |
| 160 float available_pixel_width) { | |
| 161 #if defined(OS_WIN) | |
| 162 base::string16 filename_utf16 = filename.value(); | |
| 163 base::string16 extension = filename.Extension(); | |
| 164 base::string16 rootname = filename.BaseName().RemoveExtension().value(); | |
| 165 #elif defined(OS_POSIX) | |
| 166 base::string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( | |
| 167 filename.value())); | |
| 168 base::string16 extension = WideToUTF16(base::SysNativeMBToWide( | |
| 169 filename.Extension())); | |
| 170 base::string16 rootname = WideToUTF16(base::SysNativeMBToWide( | |
| 171 filename.BaseName().RemoveExtension().value())); | |
| 172 #endif | |
| 173 | |
| 174 const float full_width = GetStringWidthF(filename_utf16, font_list); | |
| 175 if (full_width <= available_pixel_width) | |
| 176 return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); | |
| 177 | |
| 178 if (rootname.empty() || extension.empty()) { | |
| 179 const base::string16 elided_name = | |
| 180 ElideText(filename_utf16, font_list, available_pixel_width, ELIDE_TAIL); | |
| 181 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | |
| 182 } | |
| 183 | |
| 184 const float ext_width = GetStringWidthF(extension, font_list); | |
| 185 const float root_width = GetStringWidthF(rootname, font_list); | |
| 186 | |
| 187 // We may have trimmed the path. | |
| 188 if (root_width + ext_width <= available_pixel_width) { | |
| 189 const base::string16 elided_name = rootname + extension; | |
| 190 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | |
| 191 } | |
| 192 | |
| 193 if (ext_width >= available_pixel_width) { | |
| 194 const base::string16 elided_name = ElideText( | |
| 195 rootname + extension, font_list, available_pixel_width, ELIDE_MIDDLE); | |
| 196 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | |
| 197 } | |
| 198 | |
| 199 float available_root_width = available_pixel_width - ext_width; | |
| 200 base::string16 elided_name = | |
| 201 ElideText(rootname, font_list, available_root_width, ELIDE_TAIL); | |
| 202 elided_name += extension; | |
| 203 return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); | |
| 204 } | |
| 205 | |
| 206 base::string16 ElideText(const base::string16& text, | |
| 207 const FontList& font_list, | |
| 208 float available_pixel_width, | |
| 209 ElideBehavior behavior) { | |
| 210 #if !defined(OS_ANDROID) && !defined(OS_IOS) | |
| 211 DCHECK_NE(behavior, FADE_TAIL); | |
| 212 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | |
| 213 render_text->SetCursorEnabled(false); | |
| 214 // Do not bother accurately sizing strings over 5000 characters here, for | |
| 215 // performance purposes. This matches the behavior of Canvas::SizeStringFloat. | |
| 216 render_text->set_truncate_length(5000); | |
| 217 render_text->SetFontList(font_list); | |
| 218 available_pixel_width = std::ceil(available_pixel_width); | |
| 219 render_text->SetDisplayRect(gfx::Rect(gfx::Size(available_pixel_width, 1))); | |
| 220 render_text->SetElideBehavior(behavior); | |
| 221 render_text->SetText(text); | |
| 222 return render_text->layout_text(); | |
| 223 #else | |
| 224 DCHECK_NE(behavior, FADE_TAIL); | |
| 225 if (text.empty() || behavior == FADE_TAIL || behavior == NO_ELIDE || | |
| 226 GetStringWidthF(text, font_list) <= available_pixel_width) { | |
| 227 return text; | |
| 228 } | |
| 229 if (behavior == ELIDE_EMAIL) | |
| 230 return ElideEmail(text, font_list, available_pixel_width); | |
| 231 | |
| 232 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); | |
| 233 const bool elide_at_beginning = (behavior == ELIDE_HEAD); | |
| 234 const bool insert_ellipsis = (behavior != TRUNCATE); | |
| 235 const base::string16 ellipsis = base::string16(kEllipsisUTF16); | |
| 236 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning); | |
| 237 | |
| 238 if (insert_ellipsis && | |
| 239 GetStringWidthF(ellipsis, font_list) > available_pixel_width) | |
| 240 return base::string16(); | |
| 241 | |
| 242 // Use binary search to compute the elided text. | |
| 243 size_t lo = 0; | |
| 244 size_t hi = text.length() - 1; | |
| 245 size_t guess; | |
| 246 for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | |
| 247 // We check the width of the whole desired string at once to ensure we | |
| 248 // handle kerning/ligatures/etc. correctly. | |
| 249 // TODO(skanuj) : Handle directionality of ellipsis based on adjacent | |
| 250 // characters. See crbug.com/327963. | |
| 251 const base::string16 cut = slicer.CutString(guess, insert_ellipsis); | |
| 252 const float guess_width = GetStringWidthF(cut, font_list); | |
| 253 if (guess_width == available_pixel_width) | |
| 254 break; | |
| 255 if (guess_width > available_pixel_width) { | |
| 256 hi = guess - 1; | |
| 257 // Move back on the loop terminating condition when the guess is too wide. | |
| 258 if (hi < lo) | |
| 259 lo = hi; | |
| 260 } else { | |
| 261 lo = guess + 1; | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 return slicer.CutString(guess, insert_ellipsis); | |
| 266 #endif | |
| 267 } | |
| 268 | |
| 269 bool ElideString(const base::string16& input, | |
| 270 int max_len, | |
| 271 base::string16* output) { | |
| 272 DCHECK_GE(max_len, 0); | |
| 273 if (static_cast<int>(input.length()) <= max_len) { | |
| 274 output->assign(input); | |
| 275 return false; | |
| 276 } | |
| 277 | |
| 278 switch (max_len) { | |
| 279 case 0: | |
| 280 output->clear(); | |
| 281 break; | |
| 282 case 1: | |
| 283 output->assign(input.substr(0, 1)); | |
| 284 break; | |
| 285 case 2: | |
| 286 output->assign(input.substr(0, 2)); | |
| 287 break; | |
| 288 case 3: | |
| 289 output->assign(input.substr(0, 1) + ASCIIToUTF16(".") + | |
| 290 input.substr(input.length() - 1)); | |
| 291 break; | |
| 292 case 4: | |
| 293 output->assign(input.substr(0, 1) + ASCIIToUTF16("..") + | |
| 294 input.substr(input.length() - 1)); | |
| 295 break; | |
| 296 default: { | |
| 297 int rstr_len = (max_len - 3) / 2; | |
| 298 int lstr_len = rstr_len + ((max_len - 3) % 2); | |
| 299 output->assign(input.substr(0, lstr_len) + ASCIIToUTF16("...") + | |
| 300 input.substr(input.length() - rstr_len)); | |
| 301 break; | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 return true; | |
| 306 } | |
| 307 | |
| 308 namespace { | |
| 309 | |
| 310 // Internal class used to track progress of a rectangular string elide | |
| 311 // operation. Exists so the top-level ElideRectangleString() function | |
| 312 // can be broken into smaller methods sharing this state. | |
| 313 class RectangleString { | |
| 314 public: | |
| 315 RectangleString(size_t max_rows, size_t max_cols, | |
| 316 bool strict, base::string16 *output) | |
| 317 : max_rows_(max_rows), | |
| 318 max_cols_(max_cols), | |
| 319 current_row_(0), | |
| 320 current_col_(0), | |
| 321 strict_(strict), | |
| 322 suppressed_(false), | |
| 323 output_(output) {} | |
| 324 | |
| 325 // Perform deferred initializations following creation. Must be called | |
| 326 // before any input can be added via AddString(). | |
| 327 void Init() { output_->clear(); } | |
| 328 | |
| 329 // Add an input string, reformatting to fit the desired dimensions. | |
| 330 // AddString() may be called multiple times to concatenate together | |
| 331 // multiple strings into the region (the current caller doesn't do | |
| 332 // this, however). | |
| 333 void AddString(const base::string16& input); | |
| 334 | |
| 335 // Perform any deferred output processing. Must be called after the | |
| 336 // last AddString() call has occurred. | |
| 337 bool Finalize(); | |
| 338 | |
| 339 private: | |
| 340 // Add a line to the rectangular region at the current position, | |
| 341 // either by itself or by breaking it into words. | |
| 342 void AddLine(const base::string16& line); | |
| 343 | |
| 344 // Add a word to the rectangular region at the current position, | |
| 345 // either by itself or by breaking it into characters. | |
| 346 void AddWord(const base::string16& word); | |
| 347 | |
| 348 // Add text to the output string if the rectangular boundaries | |
| 349 // have not been exceeded, advancing the current position. | |
| 350 void Append(const base::string16& string); | |
| 351 | |
| 352 // Set the current position to the beginning of the next line. If | |
| 353 // |output| is true, add a newline to the output string if the rectangular | |
| 354 // boundaries have not been exceeded. If |output| is false, we assume | |
| 355 // some other mechanism will (likely) do similar breaking after the fact. | |
| 356 void NewLine(bool output); | |
| 357 | |
| 358 // Maximum number of rows allowed in the output string. | |
| 359 size_t max_rows_; | |
| 360 | |
| 361 // Maximum number of characters allowed in the output string. | |
| 362 size_t max_cols_; | |
| 363 | |
| 364 // Current row position, always incremented and may exceed max_rows_ | |
| 365 // when the input can not fit in the region. We stop appending to | |
| 366 // the output string, however, when this condition occurs. In the | |
| 367 // future, we may want to expose this value to allow the caller to | |
| 368 // determine how many rows would actually be required to hold the | |
| 369 // formatted string. | |
| 370 size_t current_row_; | |
| 371 | |
| 372 // Current character position, should never exceed max_cols_. | |
| 373 size_t current_col_; | |
| 374 | |
| 375 // True when we do whitespace to newline conversions ourselves. | |
| 376 bool strict_; | |
| 377 | |
| 378 // True when some of the input has been truncated. | |
| 379 bool suppressed_; | |
| 380 | |
| 381 // String onto which the output is accumulated. | |
| 382 base::string16* output_; | |
| 383 | |
| 384 DISALLOW_COPY_AND_ASSIGN(RectangleString); | |
| 385 }; | |
| 386 | |
| 387 void RectangleString::AddString(const base::string16& input) { | |
| 388 base::i18n::BreakIterator lines(input, | |
| 389 base::i18n::BreakIterator::BREAK_NEWLINE); | |
| 390 if (lines.Init()) { | |
| 391 while (lines.Advance()) | |
| 392 AddLine(lines.GetString()); | |
| 393 } else { | |
| 394 NOTREACHED() << "BreakIterator (lines) init failed"; | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 bool RectangleString::Finalize() { | |
| 399 if (suppressed_) { | |
| 400 output_->append(ASCIIToUTF16("...")); | |
| 401 return true; | |
| 402 } | |
| 403 return false; | |
| 404 } | |
| 405 | |
| 406 void RectangleString::AddLine(const base::string16& line) { | |
| 407 if (line.length() < max_cols_) { | |
| 408 Append(line); | |
| 409 } else { | |
| 410 base::i18n::BreakIterator words(line, | |
| 411 base::i18n::BreakIterator::BREAK_SPACE); | |
| 412 if (words.Init()) { | |
| 413 while (words.Advance()) | |
| 414 AddWord(words.GetString()); | |
| 415 } else { | |
| 416 NOTREACHED() << "BreakIterator (words) init failed"; | |
| 417 } | |
| 418 } | |
| 419 // Account for naturally-occuring newlines. | |
| 420 ++current_row_; | |
| 421 current_col_ = 0; | |
| 422 } | |
| 423 | |
| 424 void RectangleString::AddWord(const base::string16& word) { | |
| 425 if (word.length() < max_cols_) { | |
| 426 // Word can be made to fit, no need to fragment it. | |
| 427 if (current_col_ + word.length() >= max_cols_) | |
| 428 NewLine(strict_); | |
| 429 Append(word); | |
| 430 } else { | |
| 431 // Word is so big that it must be fragmented. | |
| 432 int array_start = 0; | |
| 433 int char_start = 0; | |
| 434 base::i18n::UTF16CharIterator chars(&word); | |
| 435 while (!chars.end()) { | |
| 436 // When boundary is hit, add as much as will fit on this line. | |
| 437 if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { | |
| 438 Append(word.substr(array_start, chars.array_pos() - array_start)); | |
| 439 NewLine(true); | |
| 440 array_start = chars.array_pos(); | |
| 441 char_start = chars.char_pos(); | |
| 442 } | |
| 443 chars.Advance(); | |
| 444 } | |
| 445 // Add the last remaining fragment, if any. | |
| 446 if (array_start != chars.array_pos()) | |
| 447 Append(word.substr(array_start, chars.array_pos() - array_start)); | |
| 448 } | |
| 449 } | |
| 450 | |
| 451 void RectangleString::Append(const base::string16& string) { | |
| 452 if (current_row_ < max_rows_) | |
| 453 output_->append(string); | |
| 454 else | |
| 455 suppressed_ = true; | |
| 456 current_col_ += string.length(); | |
| 457 } | |
| 458 | |
| 459 void RectangleString::NewLine(bool output) { | |
| 460 if (current_row_ < max_rows_) { | |
| 461 if (output) | |
| 462 output_->append(ASCIIToUTF16("\n")); | |
| 463 } else { | |
| 464 suppressed_ = true; | |
| 465 } | |
| 466 ++current_row_; | |
| 467 current_col_ = 0; | |
| 468 } | |
| 469 | |
| 470 // Internal class used to track progress of a rectangular text elide | |
| 471 // operation. Exists so the top-level ElideRectangleText() function | |
| 472 // can be broken into smaller methods sharing this state. | |
| 473 class RectangleText { | |
| 474 public: | |
| 475 RectangleText(const FontList& font_list, | |
| 476 float available_pixel_width, | |
| 477 int available_pixel_height, | |
| 478 WordWrapBehavior wrap_behavior, | |
| 479 std::vector<base::string16>* lines) | |
| 480 : font_list_(font_list), | |
| 481 line_height_(font_list.GetHeight()), | |
| 482 available_pixel_width_(available_pixel_width), | |
| 483 available_pixel_height_(available_pixel_height), | |
| 484 wrap_behavior_(wrap_behavior), | |
| 485 current_width_(0), | |
| 486 current_height_(0), | |
| 487 last_line_ended_in_lf_(false), | |
| 488 lines_(lines), | |
| 489 insufficient_width_(false), | |
| 490 insufficient_height_(false) {} | |
| 491 | |
| 492 // Perform deferred initializions following creation. Must be called | |
| 493 // before any input can be added via AddString(). | |
| 494 void Init() { lines_->clear(); } | |
| 495 | |
| 496 // Add an input string, reformatting to fit the desired dimensions. | |
| 497 // AddString() may be called multiple times to concatenate together | |
| 498 // multiple strings into the region (the current caller doesn't do | |
| 499 // this, however). | |
| 500 void AddString(const base::string16& input); | |
| 501 | |
| 502 // Perform any deferred output processing. Must be called after the last | |
| 503 // AddString() call has occured. Returns a combination of | |
| 504 // |ReformattingResultFlags| indicating whether the given width or height was | |
| 505 // insufficient, leading to elision or truncation. | |
| 506 int Finalize(); | |
| 507 | |
| 508 private: | |
| 509 // Add a line to the rectangular region at the current position, | |
| 510 // either by itself or by breaking it into words. | |
| 511 void AddLine(const base::string16& line); | |
| 512 | |
| 513 // Wrap the specified word across multiple lines. | |
| 514 int WrapWord(const base::string16& word); | |
| 515 | |
| 516 // Add a long word - wrapping, eliding or truncating per the wrap behavior. | |
| 517 int AddWordOverflow(const base::string16& word); | |
| 518 | |
| 519 // Add a word to the rectangluar region at the current position. | |
| 520 int AddWord(const base::string16& word); | |
| 521 | |
| 522 // Append the specified |text| to the current output line, incrementing the | |
| 523 // running width by the specified amount. This is an optimization over | |
| 524 // |AddToCurrentLine()| when |text_width| is already known. | |
| 525 void AddToCurrentLineWithWidth(const base::string16& text, float text_width); | |
| 526 | |
| 527 // Append the specified |text| to the current output line. | |
| 528 void AddToCurrentLine(const base::string16& text); | |
| 529 | |
| 530 // Set the current position to the beginning of the next line. | |
| 531 bool NewLine(); | |
| 532 | |
| 533 // The font list used for measuring text width. | |
| 534 const FontList& font_list_; | |
| 535 | |
| 536 // The height of each line of text. | |
| 537 const int line_height_; | |
| 538 | |
| 539 // The number of pixels of available width in the rectangle. | |
| 540 const float available_pixel_width_; | |
| 541 | |
| 542 // The number of pixels of available height in the rectangle. | |
| 543 const int available_pixel_height_; | |
| 544 | |
| 545 // The wrap behavior for words that are too long to fit on a single line. | |
| 546 const WordWrapBehavior wrap_behavior_; | |
| 547 | |
| 548 // The current running width. | |
| 549 float current_width_; | |
| 550 | |
| 551 // The current running height. | |
| 552 int current_height_; | |
| 553 | |
| 554 // The current line of text. | |
| 555 base::string16 current_line_; | |
| 556 | |
| 557 // Indicates whether the last line ended with \n. | |
| 558 bool last_line_ended_in_lf_; | |
| 559 | |
| 560 // The output vector of lines. | |
| 561 std::vector<base::string16>* lines_; | |
| 562 | |
| 563 // Indicates whether a word was so long that it had to be truncated or elided | |
| 564 // to fit the available width. | |
| 565 bool insufficient_width_; | |
| 566 | |
| 567 // Indicates whether there were too many lines for the available height. | |
| 568 bool insufficient_height_; | |
| 569 | |
| 570 DISALLOW_COPY_AND_ASSIGN(RectangleText); | |
| 571 }; | |
| 572 | |
| 573 void RectangleText::AddString(const base::string16& input) { | |
| 574 base::i18n::BreakIterator lines(input, | |
| 575 base::i18n::BreakIterator::BREAK_NEWLINE); | |
| 576 if (lines.Init()) { | |
| 577 while (!insufficient_height_ && lines.Advance()) { | |
| 578 base::string16 line = lines.GetString(); | |
| 579 // The BREAK_NEWLINE iterator will keep the trailing newline character, | |
| 580 // except in the case of the last line, which may not have one. Remove | |
| 581 // the newline character, if it exists. | |
| 582 last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n'; | |
| 583 if (last_line_ended_in_lf_) | |
| 584 line.resize(line.length() - 1); | |
| 585 AddLine(line); | |
| 586 } | |
| 587 } else { | |
| 588 NOTREACHED() << "BreakIterator (lines) init failed"; | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 int RectangleText::Finalize() { | |
| 593 // Remove trailing whitespace from the last line or remove the last line | |
| 594 // completely, if it's just whitespace. | |
| 595 if (!insufficient_height_ && !lines_->empty()) { | |
| 596 base::TrimWhitespace(lines_->back(), base::TRIM_TRAILING, &lines_->back()); | |
| 597 if (lines_->back().empty() && !last_line_ended_in_lf_) | |
| 598 lines_->pop_back(); | |
| 599 } | |
| 600 if (last_line_ended_in_lf_) | |
| 601 lines_->push_back(base::string16()); | |
| 602 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | | |
| 603 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); | |
| 604 } | |
| 605 | |
| 606 void RectangleText::AddLine(const base::string16& line) { | |
| 607 const float line_width = GetStringWidthF(line, font_list_); | |
| 608 if (line_width <= available_pixel_width_) { | |
| 609 AddToCurrentLineWithWidth(line, line_width); | |
| 610 } else { | |
| 611 // Iterate over positions that are valid to break the line at. In general, | |
| 612 // these are word boundaries but after any punctuation following the word. | |
| 613 base::i18n::BreakIterator words(line, | |
| 614 base::i18n::BreakIterator::BREAK_LINE); | |
| 615 if (words.Init()) { | |
| 616 while (words.Advance()) { | |
| 617 const bool truncate = !current_line_.empty(); | |
| 618 const base::string16& word = words.GetString(); | |
| 619 const int lines_added = AddWord(word); | |
| 620 if (lines_added) { | |
| 621 if (truncate) { | |
| 622 // Trim trailing whitespace from the line that was added. | |
| 623 const int line = lines_->size() - lines_added; | |
| 624 base::TrimWhitespace(lines_->at(line), base::TRIM_TRAILING, | |
| 625 &lines_->at(line)); | |
| 626 } | |
| 627 if (base::ContainsOnlyChars(word, base::kWhitespaceUTF16)) { | |
| 628 // Skip the first space if the previous line was carried over. | |
| 629 current_width_ = 0; | |
| 630 current_line_.clear(); | |
| 631 } | |
| 632 } | |
| 633 } | |
| 634 } else { | |
| 635 NOTREACHED() << "BreakIterator (words) init failed"; | |
| 636 } | |
| 637 } | |
| 638 // Account for naturally-occuring newlines. | |
| 639 NewLine(); | |
| 640 } | |
| 641 | |
| 642 int RectangleText::WrapWord(const base::string16& word) { | |
| 643 // Word is so wide that it must be fragmented. | |
| 644 base::string16 text = word; | |
| 645 int lines_added = 0; | |
| 646 bool first_fragment = true; | |
| 647 while (!insufficient_height_ && !text.empty()) { | |
| 648 base::string16 fragment = | |
| 649 ElideText(text, font_list_, available_pixel_width_, TRUNCATE); | |
| 650 // At least one character has to be added at every line, even if the | |
| 651 // available space is too small. | |
| 652 if (fragment.empty()) | |
| 653 fragment = text.substr(0, 1); | |
| 654 if (!first_fragment && NewLine()) | |
| 655 lines_added++; | |
| 656 AddToCurrentLine(fragment); | |
| 657 text = text.substr(fragment.length()); | |
| 658 first_fragment = false; | |
| 659 } | |
| 660 return lines_added; | |
| 661 } | |
| 662 | |
| 663 int RectangleText::AddWordOverflow(const base::string16& word) { | |
| 664 int lines_added = 0; | |
| 665 | |
| 666 // Unless this is the very first word, put it on a new line. | |
| 667 if (!current_line_.empty()) { | |
| 668 if (!NewLine()) | |
| 669 return 0; | |
| 670 lines_added++; | |
| 671 } | |
| 672 | |
| 673 if (wrap_behavior_ == IGNORE_LONG_WORDS) { | |
| 674 current_line_ = word; | |
| 675 current_width_ = available_pixel_width_; | |
| 676 } else if (wrap_behavior_ == WRAP_LONG_WORDS) { | |
| 677 lines_added += WrapWord(word); | |
| 678 } else { | |
| 679 const ElideBehavior elide_behavior = | |
| 680 (wrap_behavior_ == ELIDE_LONG_WORDS ? ELIDE_TAIL : TRUNCATE); | |
| 681 const base::string16 elided_word = | |
| 682 ElideText(word, font_list_, available_pixel_width_, elide_behavior); | |
| 683 AddToCurrentLine(elided_word); | |
| 684 insufficient_width_ = true; | |
| 685 } | |
| 686 | |
| 687 return lines_added; | |
| 688 } | |
| 689 | |
| 690 int RectangleText::AddWord(const base::string16& word) { | |
| 691 int lines_added = 0; | |
| 692 base::string16 trimmed; | |
| 693 base::TrimWhitespace(word, base::TRIM_TRAILING, &trimmed); | |
| 694 const float trimmed_width = GetStringWidthF(trimmed, font_list_); | |
| 695 if (trimmed_width <= available_pixel_width_) { | |
| 696 // Word can be made to fit, no need to fragment it. | |
| 697 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) | |
| 698 lines_added++; | |
| 699 // Append the non-trimmed word, in case more words are added after. | |
| 700 AddToCurrentLine(word); | |
| 701 } else { | |
| 702 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? | |
| 703 trimmed : word); | |
| 704 } | |
| 705 return lines_added; | |
| 706 } | |
| 707 | |
| 708 void RectangleText::AddToCurrentLine(const base::string16& text) { | |
| 709 AddToCurrentLineWithWidth(text, GetStringWidthF(text, font_list_)); | |
| 710 } | |
| 711 | |
| 712 void RectangleText::AddToCurrentLineWithWidth(const base::string16& text, | |
| 713 float text_width) { | |
| 714 if (current_height_ >= available_pixel_height_) { | |
| 715 insufficient_height_ = true; | |
| 716 return; | |
| 717 } | |
| 718 current_line_.append(text); | |
| 719 current_width_ += text_width; | |
| 720 } | |
| 721 | |
| 722 bool RectangleText::NewLine() { | |
| 723 bool line_added = false; | |
| 724 if (current_height_ < available_pixel_height_) { | |
| 725 lines_->push_back(current_line_); | |
| 726 current_line_.clear(); | |
| 727 line_added = true; | |
| 728 } else { | |
| 729 insufficient_height_ = true; | |
| 730 } | |
| 731 current_height_ += line_height_; | |
| 732 current_width_ = 0; | |
| 733 return line_added; | |
| 734 } | |
| 735 | |
| 736 } // namespace | |
| 737 | |
| 738 bool ElideRectangleString(const base::string16& input, size_t max_rows, | |
| 739 size_t max_cols, bool strict, | |
| 740 base::string16* output) { | |
| 741 RectangleString rect(max_rows, max_cols, strict, output); | |
| 742 rect.Init(); | |
| 743 rect.AddString(input); | |
| 744 return rect.Finalize(); | |
| 745 } | |
| 746 | |
| 747 int ElideRectangleText(const base::string16& input, | |
| 748 const FontList& font_list, | |
| 749 float available_pixel_width, | |
| 750 int available_pixel_height, | |
| 751 WordWrapBehavior wrap_behavior, | |
| 752 std::vector<base::string16>* lines) { | |
| 753 RectangleText rect(font_list, | |
| 754 available_pixel_width, | |
| 755 available_pixel_height, | |
| 756 wrap_behavior, | |
| 757 lines); | |
| 758 rect.Init(); | |
| 759 rect.AddString(input); | |
| 760 return rect.Finalize(); | |
| 761 } | |
| 762 | |
| 763 base::string16 TruncateString(const base::string16& string, | |
| 764 size_t length, | |
| 765 BreakType break_type) { | |
| 766 DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK); | |
| 767 | |
| 768 if (string.size() <= length) | |
| 769 // String fits, return it. | |
| 770 return string; | |
| 771 | |
| 772 if (length == 0) | |
| 773 // No room for the elide string, return an empty string. | |
| 774 return base::string16(); | |
| 775 | |
| 776 size_t max = length - 1; | |
| 777 | |
| 778 // Added to the end of strings that are too big. | |
| 779 static const base::char16 kElideString[] = { 0x2026, 0 }; | |
| 780 | |
| 781 if (max == 0) | |
| 782 // Just enough room for the elide string. | |
| 783 return kElideString; | |
| 784 | |
| 785 int32_t index = static_cast<int32_t>(max); | |
| 786 if (break_type == WORD_BREAK) { | |
| 787 // Use a line iterator to find the first boundary. | |
| 788 UErrorCode status = U_ZERO_ERROR; | |
| 789 scoped_ptr<icu::BreakIterator> bi( | |
| 790 icu::RuleBasedBreakIterator::createLineInstance( | |
| 791 icu::Locale::getDefault(), status)); | |
| 792 if (U_FAILURE(status)) | |
| 793 return string.substr(0, max) + kElideString; | |
| 794 bi->setText(string.c_str()); | |
| 795 index = bi->preceding(index); | |
| 796 if (index == icu::BreakIterator::DONE || index == 0) { | |
| 797 // We either found no valid line break at all, or one right at the | |
| 798 // beginning of the string. Go back to the end; we'll have to break in the | |
| 799 // middle of a word. | |
| 800 index = static_cast<int32_t>(max); | |
| 801 } | |
| 802 } | |
| 803 | |
| 804 // Use a character iterator to find the previous non-whitespace character. | |
| 805 icu::StringCharacterIterator char_iterator(string.c_str()); | |
| 806 char_iterator.setIndex(index); | |
| 807 while (char_iterator.hasPrevious()) { | |
| 808 char_iterator.previous(); | |
| 809 if (!(u_isspace(char_iterator.current()) || | |
| 810 u_charType(char_iterator.current()) == U_CONTROL_CHAR || | |
| 811 u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { | |
| 812 // Not a whitespace character. Advance the iterator so that we | |
| 813 // include the current character in the truncated string. | |
| 814 char_iterator.next(); | |
| 815 break; | |
| 816 } | |
| 817 } | |
| 818 if (char_iterator.hasPrevious()) { | |
| 819 // Found a valid break point. | |
| 820 index = char_iterator.getIndex(); | |
| 821 } else { | |
| 822 // String has leading whitespace, return the elide string. | |
| 823 return kElideString; | |
| 824 } | |
| 825 | |
| 826 return string.substr(0, index) + kElideString; | |
| 827 } | |
| 828 | |
| 829 } // namespace gfx | |
| OLD | NEW |