OLD | NEW |
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 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 } | 573 } |
574 } else { | 574 } else { |
575 NOTREACHED() << "BreakIterator (lines) init failed"; | 575 NOTREACHED() << "BreakIterator (lines) init failed"; |
576 } | 576 } |
577 } | 577 } |
578 | 578 |
579 int RectangleText::Finalize() { | 579 int RectangleText::Finalize() { |
580 // Remove trailing whitespace from the last line or remove the last line | 580 // Remove trailing whitespace from the last line or remove the last line |
581 // completely, if it's just whitespace. | 581 // completely, if it's just whitespace. |
582 if (!insufficient_height_ && !lines_->empty()) { | 582 if (!insufficient_height_ && !lines_->empty()) { |
583 TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back()); | 583 base::TrimWhitespace(lines_->back(), base::TRIM_TRAILING, &lines_->back()); |
584 if (lines_->back().empty() && !last_line_ended_in_lf_) | 584 if (lines_->back().empty() && !last_line_ended_in_lf_) |
585 lines_->pop_back(); | 585 lines_->pop_back(); |
586 } | 586 } |
587 if (last_line_ended_in_lf_) | 587 if (last_line_ended_in_lf_) |
588 lines_->push_back(base::string16()); | 588 lines_->push_back(base::string16()); |
589 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | | 589 return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | |
590 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); | 590 (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); |
591 } | 591 } |
592 | 592 |
593 void RectangleText::AddLine(const base::string16& line) { | 593 void RectangleText::AddLine(const base::string16& line) { |
594 const float line_width = GetStringWidthF(line, font_list_); | 594 const float line_width = GetStringWidthF(line, font_list_); |
595 if (line_width <= available_pixel_width_) { | 595 if (line_width <= available_pixel_width_) { |
596 AddToCurrentLineWithWidth(line, line_width); | 596 AddToCurrentLineWithWidth(line, line_width); |
597 } else { | 597 } else { |
598 // Iterate over positions that are valid to break the line at. In general, | 598 // Iterate over positions that are valid to break the line at. In general, |
599 // these are word boundaries but after any punctuation following the word. | 599 // these are word boundaries but after any punctuation following the word. |
600 base::i18n::BreakIterator words(line, | 600 base::i18n::BreakIterator words(line, |
601 base::i18n::BreakIterator::BREAK_LINE); | 601 base::i18n::BreakIterator::BREAK_LINE); |
602 if (words.Init()) { | 602 if (words.Init()) { |
603 while (words.Advance()) { | 603 while (words.Advance()) { |
604 const bool truncate = !current_line_.empty(); | 604 const bool truncate = !current_line_.empty(); |
605 const base::string16& word = words.GetString(); | 605 const base::string16& word = words.GetString(); |
606 const int lines_added = AddWord(word); | 606 const int lines_added = AddWord(word); |
607 if (lines_added) { | 607 if (lines_added) { |
608 if (truncate) { | 608 if (truncate) { |
609 // Trim trailing whitespace from the line that was added. | 609 // Trim trailing whitespace from the line that was added. |
610 const int line = lines_->size() - lines_added; | 610 const int line = lines_->size() - lines_added; |
611 TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line)); | 611 base::TrimWhitespace(lines_->at(line), base::TRIM_TRAILING, |
| 612 &lines_->at(line)); |
612 } | 613 } |
613 if (ContainsOnlyWhitespace(word)) { | 614 if (ContainsOnlyWhitespace(word)) { |
614 // Skip the first space if the previous line was carried over. | 615 // Skip the first space if the previous line was carried over. |
615 current_width_ = 0; | 616 current_width_ = 0; |
616 current_line_.clear(); | 617 current_line_.clear(); |
617 } | 618 } |
618 } | 619 } |
619 } | 620 } |
620 } else { | 621 } else { |
621 NOTREACHED() << "BreakIterator (words) init failed"; | 622 NOTREACHED() << "BreakIterator (words) init failed"; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 AddToCurrentLine(elided_word); | 671 AddToCurrentLine(elided_word); |
671 insufficient_width_ = true; | 672 insufficient_width_ = true; |
672 } | 673 } |
673 | 674 |
674 return lines_added; | 675 return lines_added; |
675 } | 676 } |
676 | 677 |
677 int RectangleText::AddWord(const base::string16& word) { | 678 int RectangleText::AddWord(const base::string16& word) { |
678 int lines_added = 0; | 679 int lines_added = 0; |
679 base::string16 trimmed; | 680 base::string16 trimmed; |
680 TrimWhitespace(word, TRIM_TRAILING, &trimmed); | 681 base::TrimWhitespace(word, base::TRIM_TRAILING, &trimmed); |
681 const float trimmed_width = GetStringWidthF(trimmed, font_list_); | 682 const float trimmed_width = GetStringWidthF(trimmed, font_list_); |
682 if (trimmed_width <= available_pixel_width_) { | 683 if (trimmed_width <= available_pixel_width_) { |
683 // Word can be made to fit, no need to fragment it. | 684 // Word can be made to fit, no need to fragment it. |
684 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) | 685 if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) |
685 lines_added++; | 686 lines_added++; |
686 // Append the non-trimmed word, in case more words are added after. | 687 // Append the non-trimmed word, in case more words are added after. |
687 AddToCurrentLine(word); | 688 AddToCurrentLine(word); |
688 } else { | 689 } else { |
689 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? | 690 lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? |
690 trimmed : word); | 691 trimmed : word); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 index = char_iterator.getIndex(); | 804 index = char_iterator.getIndex(); |
804 } else { | 805 } else { |
805 // String has leading whitespace, return the elide string. | 806 // String has leading whitespace, return the elide string. |
806 return kElideString; | 807 return kElideString; |
807 } | 808 } |
808 } | 809 } |
809 return string.substr(0, index) + kElideString; | 810 return string.substr(0, index) + kElideString; |
810 } | 811 } |
811 | 812 |
812 } // namespace gfx | 813 } // namespace gfx |
OLD | NEW |