Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: ui/gfx/text_elider.cc

Issue 614103007: Error in popup on Link (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after review Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This file implements utility functions for eliding and formatting UI text. 5 // This file implements utility functions for eliding and formatting UI text.
6 // 6 //
7 // Note that several of the functions declared in text_elider.h are implemented 7 // Note that several of the functions declared in text_elider.h are implemented
8 // in this file using helper classes in an unnamed namespace. 8 // in this file using helper classes in an unnamed namespace.
9 9
10 #include "ui/gfx/text_elider.h" 10 #include "ui/gfx/text_elider.h"
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 : font_list_(font_list), 475 : font_list_(font_list),
476 line_height_(font_list.GetHeight()), 476 line_height_(font_list.GetHeight()),
477 available_pixel_width_(available_pixel_width), 477 available_pixel_width_(available_pixel_width),
478 available_pixel_height_(available_pixel_height), 478 available_pixel_height_(available_pixel_height),
479 wrap_behavior_(wrap_behavior), 479 wrap_behavior_(wrap_behavior),
480 current_width_(0), 480 current_width_(0),
481 current_height_(0), 481 current_height_(0),
482 last_line_ended_in_lf_(false), 482 last_line_ended_in_lf_(false),
483 lines_(lines), 483 lines_(lines),
484 insufficient_width_(false), 484 insufficient_width_(false),
485 insufficient_height_(false) {} 485 insufficient_height_(false),
486 max_broken_pixel_width_(0) {}
486 487
487 // Perform deferred initializions following creation. Must be called 488 // Perform deferred initializions following creation. Must be called
488 // before any input can be added via AddString(). 489 // before any input can be added via AddString().
489 void Init() { lines_->clear(); } 490 void Init() { lines_->clear(); }
490 491
491 // Add an input string, reformatting to fit the desired dimensions. 492 // Add an input string, reformatting to fit the desired dimensions.
492 // AddString() may be called multiple times to concatenate together 493 // AddString() may be called multiple times to concatenate together
493 // multiple strings into the region (the current caller doesn't do 494 // multiple strings into the region (the current caller doesn't do
494 // this, however). 495 // this, however).
495 void AddString(const base::string16& input); 496 void AddString(const base::string16& input);
496 497
497 // Perform any deferred output processing. Must be called after the last 498 // Perform any deferred output processing. Must be called after the last
498 // AddString() call has occured. Returns a combination of 499 // AddString() call has occured. Returns a combination of
499 // |ReformattingResultFlags| indicating whether the given width or height was 500 // |ReformattingResultFlags| indicating whether the given width or height was
500 // insufficient, leading to elision or truncation. 501 // insufficient, leading to elision or truncation.
501 int Finalize(); 502 int Finalize();
502 503
504 // Returns maximum width of those lines which were broken after some words to
505 // fit |available_pixel_width_|.
506 float GetMaxBrokenPixelWidth() const { return max_broken_pixel_width_; }
Alexei Svitkine (slow) 2014/10/06 17:59:58 Nit: This should just be max_broken_pixel_width()
Roman Sorokin (ftl) 2014/10/07 09:21:53 Done.
507
503 private: 508 private:
504 // Add a line to the rectangular region at the current position, 509 // Add a line to the rectangular region at the current position,
505 // either by itself or by breaking it into words. 510 // either by itself or by breaking it into words.
506 void AddLine(const base::string16& line); 511 void AddLine(const base::string16& line);
507 512
508 // Wrap the specified word across multiple lines. 513 // Wrap the specified word across multiple lines.
509 int WrapWord(const base::string16& word); 514 int WrapWord(const base::string16& word);
510 515
511 // Add a long word - wrapping, eliding or truncating per the wrap behavior. 516 // Add a long word - wrapping, eliding or truncating per the wrap behavior.
512 int AddWordOverflow(const base::string16& word); 517 int AddWordOverflow(const base::string16& word);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 // The output vector of lines. 560 // The output vector of lines.
556 std::vector<base::string16>* lines_; 561 std::vector<base::string16>* lines_;
557 562
558 // Indicates whether a word was so long that it had to be truncated or elided 563 // Indicates whether a word was so long that it had to be truncated or elided
559 // to fit the available width. 564 // to fit the available width.
560 bool insufficient_width_; 565 bool insufficient_width_;
561 566
562 // Indicates whether there were too many lines for the available height. 567 // Indicates whether there were too many lines for the available height.
563 bool insufficient_height_; 568 bool insufficient_height_;
564 569
570 // Maximum width of those lines which were broken after some words to
571 // fit |available_pixel_width_|.
572 float max_broken_pixel_width_;
573
565 DISALLOW_COPY_AND_ASSIGN(RectangleText); 574 DISALLOW_COPY_AND_ASSIGN(RectangleText);
566 }; 575 };
567 576
568 void RectangleText::AddString(const base::string16& input) { 577 void RectangleText::AddString(const base::string16& input) {
569 base::i18n::BreakIterator lines(input, 578 base::i18n::BreakIterator lines(input,
570 base::i18n::BreakIterator::BREAK_NEWLINE); 579 base::i18n::BreakIterator::BREAK_NEWLINE);
571 if (lines.Init()) { 580 if (lines.Init()) {
572 while (!insufficient_height_ && lines.Advance()) { 581 while (!insufficient_height_ && lines.Advance()) {
573 base::string16 line = lines.GetString(); 582 base::string16 line = lines.GetString();
574 // The BREAK_NEWLINE iterator will keep the trailing newline character, 583 // The BREAK_NEWLINE iterator will keep the trailing newline character,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 693
685 int RectangleText::AddWord(const base::string16& word) { 694 int RectangleText::AddWord(const base::string16& word) {
686 int lines_added = 0; 695 int lines_added = 0;
687 base::string16 trimmed; 696 base::string16 trimmed;
688 base::TrimWhitespace(word, base::TRIM_TRAILING, &trimmed); 697 base::TrimWhitespace(word, base::TRIM_TRAILING, &trimmed);
689 const float trimmed_width = GetStringWidthF(trimmed, font_list_); 698 const float trimmed_width = GetStringWidthF(trimmed, font_list_);
690 if (trimmed_width <= available_pixel_width_) { 699 if (trimmed_width <= available_pixel_width_) {
691 // Word can be made to fit, no need to fragment it. 700 // Word can be made to fit, no need to fragment it.
692 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) 701 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine())
693 lines_added++; 702 lines_added++;
703 max_broken_pixel_width_ =
704 std::max(max_broken_pixel_width_, current_width_ + trimmed_width);
694 // Append the non-trimmed word, in case more words are added after. 705 // Append the non-trimmed word, in case more words are added after.
695 AddToCurrentLine(word); 706 AddToCurrentLine(word);
696 } else { 707 } else {
697 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? 708 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ?
698 trimmed : word); 709 trimmed : word);
699 } 710 }
700 return lines_added; 711 return lines_added;
701 } 712 }
702 713
703 void RectangleText::AddToCurrentLine(const base::string16& text) { 714 void RectangleText::AddToCurrentLine(const base::string16& text) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 rect.Init(); 748 rect.Init();
738 rect.AddString(input); 749 rect.AddString(input);
739 return rect.Finalize(); 750 return rect.Finalize();
740 } 751 }
741 752
742 int ElideRectangleText(const base::string16& input, 753 int ElideRectangleText(const base::string16& input,
743 const FontList& font_list, 754 const FontList& font_list,
744 float available_pixel_width, 755 float available_pixel_width,
745 int available_pixel_height, 756 int available_pixel_height,
746 WordWrapBehavior wrap_behavior, 757 WordWrapBehavior wrap_behavior,
747 std::vector<base::string16>* lines) { 758 std::vector<base::string16>* lines,
759 float* max_broken_pixel_width) {
748 RectangleText rect(font_list, 760 RectangleText rect(font_list,
749 available_pixel_width, 761 available_pixel_width,
750 available_pixel_height, 762 available_pixel_height,
751 wrap_behavior, 763 wrap_behavior,
752 lines); 764 lines);
753 rect.Init(); 765 rect.Init();
754 rect.AddString(input); 766 rect.AddString(input);
767 if (max_broken_pixel_width)
768 *max_broken_pixel_width = rect.GetMaxBrokenPixelWidth();
755 return rect.Finalize(); 769 return rect.Finalize();
756 } 770 }
757 771
758 base::string16 TruncateString(const base::string16& string, 772 base::string16 TruncateString(const base::string16& string,
759 size_t length, 773 size_t length,
760 BreakType break_type) { 774 BreakType break_type) {
761 DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK); 775 DCHECK(break_type == CHARACTER_BREAK || break_type == WORD_BREAK);
762 776
763 if (string.size() <= length) 777 if (string.size() <= length)
764 // String fits, return it. 778 // String fits, return it.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 index = char_iterator.getIndex(); 829 index = char_iterator.getIndex();
816 } else { 830 } else {
817 // String has leading whitespace, return the elide string. 831 // String has leading whitespace, return the elide string.
818 return kElideString; 832 return kElideString;
819 } 833 }
820 834
821 return string.substr(0, index) + kElideString; 835 return string.substr(0, index) + kElideString;
822 } 836 }
823 837
824 } // namespace gfx 838 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698