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