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