Home | History | Annotate | Download | only in gfx
      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/strings/string_split.h"
     21 #include "base/strings/string_util.h"
     22 #include "base/strings/sys_string_conversions.h"
     23 #include "base/strings/utf_string_conversions.h"
     24 #include "third_party/icu/source/common/unicode/rbbi.h"
     25 #include "third_party/icu/source/common/unicode/uloc.h"
     26 #include "ui/gfx/font_list.h"
     27 #include "ui/gfx/text_utils.h"
     28 
     29 using base::ASCIIToUTF16;
     30 using base::UTF8ToUTF16;
     31 using base::WideToUTF16;
     32 
     33 namespace gfx {
     34 
     35 namespace {
     36 
     37 // Elides a well-formed email address (e.g. username (at) domain.com) to fit into
     38 // |available_pixel_width| using the specified |font_list|.
     39 // This function guarantees that the string returned will contain at least one
     40 // character, other than the ellipses, on either side of the '@'. If it is
     41 // impossible to achieve these requirements: only an ellipsis will be returned.
     42 // If possible: this elides only the username portion of the |email|. Otherwise,
     43 // the domain is elided in the middle so that it splits the available width
     44 // equally with the elided username (should the username be short enough that it
     45 // doesn't need half the available width: the elided domain will occupy that
     46 // extra width).
     47 base::string16 ElideEmail(const base::string16& email,
     48                           const FontList& font_list,
     49                           float available_pixel_width) {
     50   if (GetStringWidthF(email, font_list) <= available_pixel_width)
     51     return email;
     52 
     53   // Split the email into its local-part (username) and domain-part. The email
     54   // spec technically allows for @ symbols in the local-part (username) of the
     55   // email under some special requirements. It is guaranteed that there is no @
     56   // symbol in the domain part of the email however so splitting at the last @
     57   // symbol is safe.
     58   const size_t split_index = email.find_last_of('@');
     59   DCHECK_NE(split_index, base::string16::npos);
     60   base::string16 username = email.substr(0, split_index);
     61   base::string16 domain = email.substr(split_index + 1);
     62   DCHECK(!username.empty());
     63   DCHECK(!domain.empty());
     64 
     65   // Subtract the @ symbol from the available width as it is mandatory.
     66   const base::string16 kAtSignUTF16 = ASCIIToUTF16("@");
     67   available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list);
     68 
     69   // Check whether eliding the domain is necessary: if eliding the username
     70   // is sufficient, the domain will not be elided.
     71   const float full_username_width = GetStringWidthF(username, font_list);
     72   const float available_domain_width =
     73       available_pixel_width -
     74       std::min(full_username_width,
     75                GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16,
     76                                font_list));
     77   if (GetStringWidthF(domain, font_list) > available_domain_width) {
     78     // Elide the domain so that it only takes half of the available width.
     79     // Should the username not need all the width available in its half, the
     80     // domain will occupy the leftover width.
     81     // If |desired_domain_width| is greater than |available_domain_width|: the
     82     // minimal username elision allowed by the specifications will not fit; thus
     83     // |desired_domain_width| must be <= |available_domain_width| at all cost.
     84     const float desired_domain_width =
     85         std::min(available_domain_width,
     86                  std::max(available_pixel_width - full_username_width,
     87                           available_pixel_width / 2));
     88     domain = ElideText(domain, font_list, desired_domain_width, ELIDE_MIDDLE);
     89     // Failing to elide the domain such that at least one character remains
     90     // (other than the ellipsis itself) remains: return a single ellipsis.
     91     if (domain.length() <= 1U)
     92       return base::string16(kEllipsisUTF16);
     93   }
     94 
     95   // Fit the username in the remaining width (at this point the elided username
     96   // is guaranteed to fit with at least one character remaining given all the
     97   // precautions taken earlier).
     98   available_pixel_width -= GetStringWidthF(domain, font_list);
     99   username = ElideText(username, font_list, available_pixel_width, ELIDE_TAIL);
    100   return username + kAtSignUTF16 + domain;
    101 }
    102 
    103 }  // namespace
    104 
    105 // U+2026 in utf8
    106 const char kEllipsis[] = "\xE2\x80\xA6";
    107 const base::char16 kEllipsisUTF16[] = { 0x2026, 0 };
    108 const base::char16 kForwardSlash = '/';
    109 
    110 StringSlicer::StringSlicer(const base::string16& text,
    111                            const base::string16& ellipsis,
    112                            bool elide_in_middle,
    113                            bool elide_at_beginning)
    114     : text_(text),
    115       ellipsis_(ellipsis),
    116       elide_in_middle_(elide_in_middle),
    117       elide_at_beginning_(elide_at_beginning) {
    118 }
    119 
    120 base::string16 StringSlicer::CutString(size_t length, bool insert_ellipsis) {
    121   const base::string16 ellipsis_text = insert_ellipsis ? ellipsis_
    122                                                        : base::string16();
    123 
    124   if (elide_at_beginning_)
    125     return ellipsis_text +
    126            text_.substr(FindValidBoundaryBefore(text_.length() - length));
    127 
    128   if (!elide_in_middle_)
    129     return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text;
    130 
    131   // We put the extra character, if any, before the cut.
    132   const size_t half_length = length / 2;
    133   const size_t prefix_length = FindValidBoundaryBefore(length - half_length);
    134   const size_t suffix_start_guess = text_.length() - half_length;
    135   const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess);
    136   const size_t suffix_length =
    137       half_length - (suffix_start_guess - suffix_start);
    138   return text_.substr(0, prefix_length) + ellipsis_text +
    139          text_.substr(suffix_start, suffix_length);
    140 }
    141 
    142 size_t StringSlicer::FindValidBoundaryBefore(size_t index) const {
    143   DCHECK_LE(index, text_.length());
    144   if (index != text_.length())
    145     U16_SET_CP_START(text_.data(), 0, index);
    146   return index;
    147 }
    148 
    149 size_t StringSlicer::FindValidBoundaryAfter(size_t index) const {
    150   DCHECK_LE(index, text_.length());
    151   if (index != text_.length())
    152     U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length());
    153   return index;
    154 }
    155 
    156 base::string16 ElideFilename(const base::FilePath& filename,
    157                              const FontList& font_list,
    158                              float available_pixel_width) {
    159 #if defined(OS_WIN)
    160   base::string16 filename_utf16 = filename.value();
    161   base::string16 extension = filename.Extension();
    162   base::string16 rootname = filename.BaseName().RemoveExtension().value();
    163 #elif defined(OS_POSIX)
    164   base::string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide(
    165       filename.value()));
    166   base::string16 extension = WideToUTF16(base::SysNativeMBToWide(
    167       filename.Extension()));
    168   base::string16 rootname = WideToUTF16(base::SysNativeMBToWide(
    169       filename.BaseName().RemoveExtension().value()));
    170 #endif
    171 
    172   const float full_width = GetStringWidthF(filename_utf16, font_list);
    173   if (full_width <= available_pixel_width)
    174     return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16);
    175 
    176   if (rootname.empty() || extension.empty()) {
    177     const base::string16 elided_name =
    178         ElideText(filename_utf16, font_list, available_pixel_width, ELIDE_TAIL);
    179     return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
    180   }
    181 
    182   const float ext_width = GetStringWidthF(extension, font_list);
    183   const float root_width = GetStringWidthF(rootname, font_list);
    184 
    185   // We may have trimmed the path.
    186   if (root_width + ext_width <= available_pixel_width) {
    187     const base::string16 elided_name = rootname + extension;
    188     return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
    189   }
    190 
    191   if (ext_width >= available_pixel_width) {
    192     const base::string16 elided_name = ElideText(
    193         rootname + extension, font_list, available_pixel_width, ELIDE_MIDDLE);
    194     return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
    195   }
    196 
    197   float available_root_width = available_pixel_width - ext_width;
    198   base::string16 elided_name =
    199       ElideText(rootname, font_list, available_root_width, ELIDE_TAIL);
    200   elided_name += extension;
    201   return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
    202 }
    203 
    204 base::string16 ElideText(const base::string16& text,
    205                          const FontList& font_list,
    206                          float available_pixel_width,
    207                          ElideBehavior behavior) {
    208   DCHECK_NE(behavior, FADE_TAIL);
    209   if (text.empty() || behavior == FADE_TAIL)
    210     return text;
    211   if (behavior == ELIDE_EMAIL)
    212     return ElideEmail(text, font_list, available_pixel_width);
    213 
    214   const float current_text_pixel_width = GetStringWidthF(text, font_list);
    215   const bool elide_in_middle = (behavior == ELIDE_MIDDLE);
    216   const bool elide_at_beginning = (behavior == ELIDE_HEAD);
    217   const bool insert_ellipsis = (behavior != TRUNCATE);
    218   const base::string16 ellipsis = base::string16(kEllipsisUTF16);
    219   StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning);
    220 
    221   // Pango will return 0 width for absurdly long strings. Cut the string in
    222   // half and try again.
    223   // This is caused by an int overflow in Pango (specifically, in
    224   // pango_glyph_string_extents_range). It's actually more subtle than just
    225   // returning 0, since on super absurdly long strings, the int can wrap and
    226   // return positive numbers again. Detecting that is probably not worth it
    227   // (eliding way too much from a ridiculous string is probably still
    228   // ridiculous), but we should check other widths for bogus values as well.
    229   if (current_text_pixel_width <= 0) {
    230     const base::string16 cut =
    231       slicer.CutString(text.length() / 2, insert_ellipsis);
    232     return ElideText(cut, font_list, available_pixel_width, behavior);
    233   }
    234 
    235   if (current_text_pixel_width <= available_pixel_width)
    236     return text;
    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 if we are on loop terminating condition, and guess is wider
    258       // than available.
    259       if (hi < lo)
    260         lo = hi;
    261     } else {
    262       lo = guess + 1;
    263     }
    264   }
    265 
    266   return slicer.CutString(guess, insert_ellipsis);
    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, size_t length) {
    764   if (string.size() <= length)
    765     // String fits, return it.
    766     return string;
    767 
    768   if (length == 0)
    769     // No room for the elide string, return an empty string.
    770     return base::string16();
    771 
    772   size_t max = length - 1;
    773 
    774   // Added to the end of strings that are too big.
    775   static const base::char16 kElideString[] = { 0x2026, 0 };
    776 
    777   if (max == 0)
    778     // Just enough room for the elide string.
    779     return kElideString;
    780 
    781   // Use a line iterator to find the first boundary.
    782   UErrorCode status = U_ZERO_ERROR;
    783   scoped_ptr<icu::RuleBasedBreakIterator> bi(
    784       static_cast<icu::RuleBasedBreakIterator*>(
    785           icu::RuleBasedBreakIterator::createLineInstance(
    786               icu::Locale::getDefault(), status)));
    787   if (U_FAILURE(status))
    788     return string.substr(0, max) + kElideString;
    789   bi->setText(string.c_str());
    790   int32_t index = bi->preceding(static_cast<int32_t>(max));
    791   if (index == icu::BreakIterator::DONE) {
    792     index = static_cast<int32_t>(max);
    793   } else {
    794     // Found a valid break (may be the beginning of the string). Now use
    795     // a character iterator to find the previous non-whitespace character.
    796     icu::StringCharacterIterator char_iterator(string.c_str());
    797     if (index == 0) {
    798       // No valid line breaks. Start at the end again. This ensures we break
    799       // on a valid character boundary.
    800       index = static_cast<int32_t>(max);
    801     }
    802     char_iterator.setIndex(index);
    803     while (char_iterator.hasPrevious()) {
    804       char_iterator.previous();
    805       if (!(u_isspace(char_iterator.current()) ||
    806             u_charType(char_iterator.current()) == U_CONTROL_CHAR ||
    807             u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) {
    808         // Not a whitespace character. Advance the iterator so that we
    809         // include the current character in the truncated string.
    810         char_iterator.next();
    811         break;
    812       }
    813     }
    814     if (char_iterator.hasPrevious()) {
    815       // Found a valid break point.
    816       index = char_iterator.getIndex();
    817     } else {
    818       // String has leading whitespace, return the elide string.
    819       return kElideString;
    820     }
    821   }
    822   return string.substr(0, index) + kElideString;
    823 }
    824 
    825 }  // namespace gfx
    826