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

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

Issue 916203002: Reduce the number of text reshaping in RenderText (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_harfbuzz.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "ui/gfx/render_text.h" 5 #include "ui/gfx/render_text.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <climits> 8 #include <climits>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/i18n/break_iterator.h" 11 #include "base/i18n/break_iterator.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/trace_event/trace_event.h"
16 #include "third_party/icu/source/common/unicode/rbbi.h" 17 #include "third_party/icu/source/common/unicode/rbbi.h"
17 #include "third_party/icu/source/common/unicode/utf16.h" 18 #include "third_party/icu/source/common/unicode/utf16.h"
18 #include "third_party/skia/include/core/SkTypeface.h" 19 #include "third_party/skia/include/core/SkTypeface.h"
19 #include "third_party/skia/include/effects/SkGradientShader.h" 20 #include "third_party/skia/include/effects/SkGradientShader.h"
20 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/geometry/insets.h" 22 #include "ui/gfx/geometry/insets.h"
22 #include "ui/gfx/geometry/safe_integer_conversions.h" 23 #include "ui/gfx/geometry/safe_integer_conversions.h"
23 #include "ui/gfx/render_text_harfbuzz.h" 24 #include "ui/gfx/render_text_harfbuzz.h"
24 #include "ui/gfx/scoped_canvas.h" 25 #include "ui/gfx/scoped_canvas.h"
25 #include "ui/gfx/skia_util.h" 26 #include "ui/gfx/skia_util.h"
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 438
438 // Reset selection model. SetText should always followed by SetSelectionModel 439 // Reset selection model. SetText should always followed by SetSelectionModel
439 // or SetCursorPosition in upper layer. 440 // or SetCursorPosition in upper layer.
440 SetSelectionModel(SelectionModel()); 441 SetSelectionModel(SelectionModel());
441 442
442 // Invalidate the cached text direction if it depends on the text contents. 443 // Invalidate the cached text direction if it depends on the text contents.
443 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) 444 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT)
444 text_direction_ = base::i18n::UNKNOWN_DIRECTION; 445 text_direction_ = base::i18n::UNKNOWN_DIRECTION;
445 446
446 obscured_reveal_index_ = -1; 447 obscured_reveal_index_ = -1;
447 UpdateLayoutText(); 448 OnTextAttributeChanged();
448 } 449 }
449 450
450 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { 451 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
451 if (horizontal_alignment_ != alignment) { 452 if (horizontal_alignment_ != alignment) {
452 horizontal_alignment_ = alignment; 453 horizontal_alignment_ = alignment;
453 display_offset_ = Vector2d(); 454 display_offset_ = Vector2d();
454 cached_bounds_and_offset_valid_ = false; 455 cached_bounds_and_offset_valid_ = false;
455 } 456 }
456 } 457 }
457 458
458 void RenderText::SetFontList(const FontList& font_list) { 459 void RenderText::SetFontList(const FontList& font_list) {
459 font_list_ = font_list; 460 font_list_ = font_list;
460 const int font_style = font_list.GetFontStyle(); 461 const int font_style = font_list.GetFontStyle();
461 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0); 462 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0);
462 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0); 463 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0);
463 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0); 464 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0);
464 baseline_ = kInvalidBaseline; 465 baseline_ = kInvalidBaseline;
465 cached_bounds_and_offset_valid_ = false; 466 cached_bounds_and_offset_valid_ = false;
466 ResetLayout(); 467 OnLayoutTextAttributeChanged(false);
467 } 468 }
468 469
469 void RenderText::SetCursorEnabled(bool cursor_enabled) { 470 void RenderText::SetCursorEnabled(bool cursor_enabled) {
470 cursor_enabled_ = cursor_enabled; 471 cursor_enabled_ = cursor_enabled;
471 cached_bounds_and_offset_valid_ = false; 472 cached_bounds_and_offset_valid_ = false;
472 } 473 }
473 474
474 void RenderText::ToggleInsertMode() { 475 void RenderText::ToggleInsertMode() {
475 insert_mode_ = !insert_mode_; 476 insert_mode_ = !insert_mode_;
476 cached_bounds_and_offset_valid_ = false; 477 cached_bounds_and_offset_valid_ = false;
477 } 478 }
478 479
479 void RenderText::SetObscured(bool obscured) { 480 void RenderText::SetObscured(bool obscured) {
480 if (obscured != obscured_) { 481 if (obscured != obscured_) {
481 obscured_ = obscured; 482 obscured_ = obscured;
482 obscured_reveal_index_ = -1; 483 obscured_reveal_index_ = -1;
483 cached_bounds_and_offset_valid_ = false; 484 cached_bounds_and_offset_valid_ = false;
484 UpdateLayoutText(); 485 OnTextAttributeChanged();
485 } 486 }
486 } 487 }
487 488
488 void RenderText::SetObscuredRevealIndex(int index) { 489 void RenderText::SetObscuredRevealIndex(int index) {
489 if (obscured_reveal_index_ == index) 490 if (obscured_reveal_index_ == index)
490 return; 491 return;
491 492
492 obscured_reveal_index_ = index; 493 obscured_reveal_index_ = index;
493 cached_bounds_and_offset_valid_ = false; 494 cached_bounds_and_offset_valid_ = false;
494 UpdateLayoutText(); 495 OnTextAttributeChanged();
495 }
496
497 void RenderText::SetReplaceNewlineCharsWithSymbols(bool replace) {
498 replace_newline_chars_with_symbols_ = replace;
499 cached_bounds_and_offset_valid_ = false;
500 UpdateLayoutText();
501 } 496 }
502 497
503 void RenderText::SetMultiline(bool multiline) { 498 void RenderText::SetMultiline(bool multiline) {
504 if (multiline != multiline_) { 499 if (multiline != multiline_) {
505 multiline_ = multiline; 500 multiline_ = multiline;
506 cached_bounds_and_offset_valid_ = false; 501 cached_bounds_and_offset_valid_ = false;
507 lines_.clear(); 502 lines_.clear();
503 OnDisplayTextAttributeChanged();
508 } 504 }
509 } 505 }
510 506
511 void RenderText::SetMinLineHeight(int line_height) { 507 void RenderText::SetMinLineHeight(int line_height) {
512 if (min_line_height_ == line_height) 508 if (min_line_height_ == line_height)
513 return; 509 return;
514 min_line_height_ = line_height; 510 min_line_height_ = line_height;
515 cached_bounds_and_offset_valid_ = false; 511 cached_bounds_and_offset_valid_ = false;
516 lines_.clear(); 512 lines_.clear();
513 OnDisplayTextAttributeChanged();
517 } 514 }
518 515
519 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) { 516 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) {
520 // TODO(skanuj) : Add a test for triggering layout change. 517 // TODO(skanuj) : Add a test for triggering layout change.
521 if (elide_behavior_ != elide_behavior) { 518 if (elide_behavior_ != elide_behavior) {
522 elide_behavior_ = elide_behavior; 519 elide_behavior_ = elide_behavior;
523 UpdateLayoutText(); 520 OnDisplayTextAttributeChanged();
524 } 521 }
525 } 522 }
526 523
527 void RenderText::SetDisplayRect(const Rect& r) { 524 void RenderText::SetDisplayRect(const Rect& r) {
528 if (r != display_rect_) { 525 if (r != display_rect_) {
529 display_rect_ = r; 526 display_rect_ = r;
530 baseline_ = kInvalidBaseline; 527 baseline_ = kInvalidBaseline;
531 cached_bounds_and_offset_valid_ = false; 528 cached_bounds_and_offset_valid_ = false;
532 lines_.clear(); 529 lines_.clear();
533 if (elide_behavior_ != NO_ELIDE) 530 if (elide_behavior_ != NO_ELIDE &&
534 UpdateLayoutText(); 531 elide_behavior_ != FADE_TAIL) {
532 OnDisplayTextAttributeChanged();
533 }
535 } 534 }
536 } 535 }
537 536
538 void RenderText::SetCursorPosition(size_t position) { 537 void RenderText::SetCursorPosition(size_t position) {
539 MoveCursorTo(position, false); 538 MoveCursorTo(position, false);
540 } 539 }
541 540
542 void RenderText::MoveCursor(BreakType break_type, 541 void RenderText::MoveCursor(BreakType break_type,
543 VisualCursorDirection direction, 542 VisualCursorDirection direction,
544 bool select) { 543 bool select) {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 649
651 const Range& RenderText::GetCompositionRange() const { 650 const Range& RenderText::GetCompositionRange() const {
652 return composition_range_; 651 return composition_range_;
653 } 652 }
654 653
655 void RenderText::SetCompositionRange(const Range& composition_range) { 654 void RenderText::SetCompositionRange(const Range& composition_range) {
656 CHECK(!composition_range.IsValid() || 655 CHECK(!composition_range.IsValid() ||
657 Range(0, text_.length()).Contains(composition_range)); 656 Range(0, text_.length()).Contains(composition_range));
658 composition_range_.set_end(composition_range.end()); 657 composition_range_.set_end(composition_range.end());
659 composition_range_.set_start(composition_range.start()); 658 composition_range_.set_start(composition_range.start());
660 ResetLayout(); 659 // TODO(oshima|msw): Altering composition underlines shouldn't
660 // require layout changes. It's currently necessary because
661 // RenderTextHarfBuzz paints text decorations by run, and
662 // RenderTextMac applies all styles during layout.
663 OnLayoutTextAttributeChanged(false);
661 } 664 }
662 665
663 void RenderText::SetColor(SkColor value) { 666 void RenderText::SetColor(SkColor value) {
664 colors_.SetValue(value); 667 colors_.SetValue(value);
665 } 668 }
666 669
667 void RenderText::ApplyColor(SkColor value, const Range& range) { 670 void RenderText::ApplyColor(SkColor value, const Range& range) {
668 colors_.ApplyValue(value, range); 671 colors_.ApplyValue(value, range);
669 } 672 }
670 673
671 void RenderText::SetStyle(TextStyle style, bool value) { 674 void RenderText::SetStyle(TextStyle style, bool value) {
672 styles_[style].SetValue(value); 675 styles_[style].SetValue(value);
673 676
674 cached_bounds_and_offset_valid_ = false; 677 cached_bounds_and_offset_valid_ = false;
675 ResetLayout(); 678 // TODO(oshima|msw): Not all style change requires layout changes.
679 // Consider optimizing based on the type of change.
680 OnLayoutTextAttributeChanged(false);
676 } 681 }
677 682
678 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) { 683 void RenderText::ApplyStyle(TextStyle style, bool value, const Range& range) {
679 // Do not change styles mid-grapheme to avoid breaking ligatures. 684 // Do not change styles mid-grapheme to avoid breaking ligatures.
680 const size_t start = IsValidCursorIndex(range.start()) ? range.start() : 685 const size_t start = IsValidCursorIndex(range.start()) ? range.start() :
681 IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD); 686 IndexOfAdjacentGrapheme(range.start(), CURSOR_BACKWARD);
682 const size_t end = IsValidCursorIndex(range.end()) ? range.end() : 687 const size_t end = IsValidCursorIndex(range.end()) ? range.end() :
683 IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD); 688 IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD);
684 styles_[style].ApplyValue(value, Range(start, end)); 689 styles_[style].ApplyValue(value, Range(start, end));
685 690
686 cached_bounds_and_offset_valid_ = false; 691 cached_bounds_and_offset_valid_ = false;
687 ResetLayout(); 692 // TODO(oshima|msw): Not all style change requires layout changes.
693 // Consider optimizing based on the type of change.
694 OnLayoutTextAttributeChanged(false);
688 } 695 }
689 696
690 bool RenderText::GetStyle(TextStyle style) const { 697 bool RenderText::GetStyle(TextStyle style) const {
691 return (styles_[style].breaks().size() == 1) && 698 return (styles_[style].breaks().size() == 1) &&
692 styles_[style].breaks().front().second; 699 styles_[style].breaks().front().second;
693 } 700 }
694 701
695 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { 702 void RenderText::SetDirectionalityMode(DirectionalityMode mode) {
696 if (mode == directionality_mode_) 703 if (mode == directionality_mode_)
697 return; 704 return;
698 705
699 directionality_mode_ = mode; 706 directionality_mode_ = mode;
700 text_direction_ = base::i18n::UNKNOWN_DIRECTION; 707 text_direction_ = base::i18n::UNKNOWN_DIRECTION;
701 cached_bounds_and_offset_valid_ = false; 708 cached_bounds_and_offset_valid_ = false;
702 ResetLayout(); 709 OnLayoutTextAttributeChanged(false);
703 } 710 }
704 711
705 base::i18n::TextDirection RenderText::GetTextDirection() { 712 base::i18n::TextDirection RenderText::GetDisplayTextDirection() {
706 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { 713 return GetTextDirection(GetDisplayText());
707 switch (directionality_mode_) {
708 case DIRECTIONALITY_FROM_TEXT:
709 // Derive the direction from the display text, which differs from text()
710 // in the case of obscured (password) textfields.
711 text_direction_ =
712 base::i18n::GetFirstStrongCharacterDirection(GetLayoutText());
713 break;
714 case DIRECTIONALITY_FROM_UI:
715 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT :
716 base::i18n::LEFT_TO_RIGHT;
717 break;
718 case DIRECTIONALITY_FORCE_LTR:
719 text_direction_ = base::i18n::LEFT_TO_RIGHT;
720 break;
721 case DIRECTIONALITY_FORCE_RTL:
722 text_direction_ = base::i18n::RIGHT_TO_LEFT;
723 break;
724 default:
725 NOTREACHED();
726 }
727 }
728
729 return text_direction_;
730 } 714 }
731 715
732 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { 716 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() {
733 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? 717 return GetDisplayTextDirection() == base::i18n::LEFT_TO_RIGHT ?
734 CURSOR_RIGHT : CURSOR_LEFT; 718 CURSOR_RIGHT : CURSOR_LEFT;
735 } 719 }
736 720
737 SizeF RenderText::GetStringSizeF() { 721 SizeF RenderText::GetStringSizeF() {
738 return GetStringSize(); 722 return GetStringSize();
739 } 723 }
740 724
741 float RenderText::GetContentWidthF() { 725 float RenderText::GetContentWidthF() {
742 const float string_size = GetStringSizeF().width(); 726 const float string_size = GetStringSizeF().width();
743 // The cursor is drawn one pixel beyond the int-enclosed text bounds. 727 // The cursor is drawn one pixel beyond the int-enclosed text bounds.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 } 764 }
781 765
782 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) { 766 void RenderText::DrawCursor(Canvas* canvas, const SelectionModel& position) {
783 // Paint cursor. Replace cursor is drawn as rectangle for now. 767 // Paint cursor. Replace cursor is drawn as rectangle for now.
784 // TODO(msw): Draw a better cursor with a better indication of association. 768 // TODO(msw): Draw a better cursor with a better indication of association.
785 canvas->FillRect(GetCursorBounds(position, true), cursor_color_); 769 canvas->FillRect(GetCursorBounds(position, true), cursor_color_);
786 } 770 }
787 771
788 bool RenderText::IsValidLogicalIndex(size_t index) { 772 bool RenderText::IsValidLogicalIndex(size_t index) {
789 // Check that the index is at a valid code point (not mid-surrgate-pair) and 773 // Check that the index is at a valid code point (not mid-surrgate-pair) and
790 // that it's not truncated from the layout text (its glyph may be shown). 774 // that it's not truncated from the display text (its glyph may be shown).
791 // 775 //
792 // Indices within truncated text are disallowed so users can easily interact 776 // Indices within truncated text are disallowed so users can easily interact
793 // with the underlying truncated text using the ellipsis as a proxy. This lets 777 // with the underlying truncated text using the ellipsis as a proxy. This lets
794 // users select all text, select the truncated text, and transition from the 778 // users select all text, select the truncated text, and transition from the
795 // last rendered glyph to the end of the text without getting invisible cursor 779 // last rendered glyph to the end of the text without getting invisible cursor
796 // positions nor needing unbounded arrow key presses to traverse the ellipsis. 780 // positions nor needing unbounded arrow key presses to traverse the ellipsis.
797 return index == 0 || index == text().length() || 781 return index == 0 || index == text().length() ||
798 (index < text().length() && 782 (index < text().length() &&
799 (truncate_length_ == 0 || index < truncate_length_) && 783 (truncate_length_ == 0 || index < truncate_length_) &&
800 IsValidCodePointIndex(text(), index)); 784 IsValidCodePointIndex(text(), index));
(...skipping 10 matching lines...) Expand all
811 DCHECK(IsValidLogicalIndex(caret_pos)); 795 DCHECK(IsValidLogicalIndex(caret_pos));
812 // In overtype mode, ignore the affinity and always indicate that we will 796 // In overtype mode, ignore the affinity and always indicate that we will
813 // overtype the next character. 797 // overtype the next character.
814 LogicalCursorDirection caret_affinity = 798 LogicalCursorDirection caret_affinity =
815 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD; 799 insert_mode ? caret.caret_affinity() : CURSOR_FORWARD;
816 int x = 0, width = 1; 800 int x = 0, width = 1;
817 Size size = GetStringSize(); 801 Size size = GetStringSize();
818 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) { 802 if (caret_pos == (caret_affinity == CURSOR_BACKWARD ? 0 : text().length())) {
819 // The caret is attached to the boundary. Always return a 1-dip width caret, 803 // The caret is attached to the boundary. Always return a 1-dip width caret,
820 // since there is nothing to overtype. 804 // since there is nothing to overtype.
821 if ((GetTextDirection() == base::i18n::RIGHT_TO_LEFT) == (caret_pos == 0)) 805 if ((GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT)
806 == (caret_pos == 0)) {
822 x = size.width(); 807 x = size.width();
808 }
823 } else { 809 } else {
824 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ? 810 size_t grapheme_start = (caret_affinity == CURSOR_FORWARD) ?
825 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD); 811 caret_pos : IndexOfAdjacentGrapheme(caret_pos, CURSOR_BACKWARD);
826 Range xspan(GetGlyphBounds(grapheme_start)); 812 Range xspan(GetGlyphBounds(grapheme_start));
827 if (insert_mode) { 813 if (insert_mode) {
828 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start(); 814 x = (caret_affinity == CURSOR_BACKWARD) ? xspan.end() : xspan.start();
829 } else { // overtype mode 815 } else { // overtype mode
830 x = xspan.GetMin(); 816 x = xspan.GetMin();
831 width = xspan.length(); 817 width = xspan.length();
832 } 818 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 selection_background_focused_color_(kDefaultSelectionBackgroundColor), 910 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
925 focused_(false), 911 focused_(false),
926 composition_range_(Range::InvalidRange()), 912 composition_range_(Range::InvalidRange()),
927 colors_(kDefaultColor), 913 colors_(kDefaultColor),
928 styles_(NUM_TEXT_STYLES), 914 styles_(NUM_TEXT_STYLES),
929 composition_and_selection_styles_applied_(false), 915 composition_and_selection_styles_applied_(false),
930 obscured_(false), 916 obscured_(false),
931 obscured_reveal_index_(-1), 917 obscured_reveal_index_(-1),
932 truncate_length_(0), 918 truncate_length_(0),
933 elide_behavior_(NO_ELIDE), 919 elide_behavior_(NO_ELIDE),
934 replace_newline_chars_with_symbols_(true), 920 text_elided_(false),
935 min_line_height_(0), 921 min_line_height_(0),
936 multiline_(false), 922 multiline_(false),
937 background_is_transparent_(false), 923 background_is_transparent_(false),
938 clip_to_display_rect_(true), 924 clip_to_display_rect_(true),
939 baseline_(kInvalidBaseline), 925 baseline_(kInvalidBaseline),
940 cached_bounds_and_offset_valid_(false) { 926 cached_bounds_and_offset_valid_(false) {
941 } 927 }
942 928
943 SelectionModel RenderText::GetAdjacentSelectionModel( 929 SelectionModel RenderText::GetAdjacentSelectionModel(
944 const SelectionModel& current, 930 const SelectionModel& current,
(...skipping 15 matching lines...) Expand all
960 return SelectionModel(text().length(), CURSOR_FORWARD); 946 return SelectionModel(text().length(), CURSOR_FORWARD);
961 return SelectionModel(0, CURSOR_BACKWARD); 947 return SelectionModel(0, CURSOR_BACKWARD);
962 } 948 }
963 949
964 void RenderText::SetSelectionModel(const SelectionModel& model) { 950 void RenderText::SetSelectionModel(const SelectionModel& model) {
965 DCHECK_LE(model.selection().GetMax(), text().length()); 951 DCHECK_LE(model.selection().GetMax(), text().length());
966 selection_model_ = model; 952 selection_model_ = model;
967 cached_bounds_and_offset_valid_ = false; 953 cached_bounds_and_offset_valid_ = false;
968 } 954 }
969 955
970 const base::string16& RenderText::GetLayoutText() const { 956 void RenderText::UpdateDisplayText(float text_width) {
971 return layout_text_; 957 // TODO(oshima): Consider support eliding for multi-line text.
958 // This requires max_line support first.
959 if (multiline_ ||
960 elide_behavior() == NO_ELIDE ||
961 elide_behavior() == FADE_TAIL ||
962 text_width < display_rect_.width() ||
963 layout_text_.empty()) {
964 text_elided_ = false;
965 display_text_.clear();
966 return;
967 }
968
969 // This doesn't trim styles so ellipsis may get rendered as a different
970 // style than the preceding text. See crbug.com/327850.
971 display_text_.assign(Elide(layout_text_,
972 text_width,
973 static_cast<float>(display_rect_.width()),
974 elide_behavior_));
975
976 text_elided_ = display_text_ != layout_text_;
977 if (!text_elided_)
978 display_text_.clear();
972 } 979 }
973 980
974 const BreakList<size_t>& RenderText::GetLineBreaks() { 981 const BreakList<size_t>& RenderText::GetLineBreaks() {
975 if (line_breaks_.max() != 0) 982 if (line_breaks_.max() != 0)
976 return line_breaks_; 983 return line_breaks_;
977 984
978 const base::string16& layout_text = GetLayoutText(); 985 const base::string16& layout_text = GetDisplayText();
979 const size_t text_length = layout_text.length(); 986 const size_t text_length = layout_text.length();
980 line_breaks_.SetValue(0); 987 line_breaks_.SetValue(0);
981 line_breaks_.SetMax(text_length); 988 line_breaks_.SetMax(text_length);
982 base::i18n::BreakIterator iter(layout_text, 989 base::i18n::BreakIterator iter(layout_text,
983 base::i18n::BreakIterator::BREAK_LINE); 990 base::i18n::BreakIterator::BREAK_LINE);
984 const bool success = iter.Init(); 991 const bool success = iter.Init();
985 DCHECK(success); 992 DCHECK(success);
986 if (success) { 993 if (success) {
987 do { 994 do {
988 line_breaks_.ApplyValue(iter.pos(), Range(iter.pos(), text_length)); 995 line_breaks_.ApplyValue(iter.pos(), Range(iter.pos(), text_length));
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 line_x += segment->x_range.length(); 1081 line_x += segment->x_range.length();
1075 } 1082 }
1076 } 1083 }
1077 1084
1078 return rects; 1085 return rects;
1079 } 1086 }
1080 1087
1081 HorizontalAlignment RenderText::GetCurrentHorizontalAlignment() { 1088 HorizontalAlignment RenderText::GetCurrentHorizontalAlignment() {
1082 if (horizontal_alignment_ != ALIGN_TO_HEAD) 1089 if (horizontal_alignment_ != ALIGN_TO_HEAD)
1083 return horizontal_alignment_; 1090 return horizontal_alignment_;
1084 return GetTextDirection() == base::i18n::RIGHT_TO_LEFT ? ALIGN_RIGHT 1091 return GetDisplayTextDirection() == base::i18n::RIGHT_TO_LEFT ?
1085 : ALIGN_LEFT; 1092 ALIGN_RIGHT : ALIGN_LEFT;
1086 } 1093 }
1087 1094
1088 Vector2d RenderText::GetAlignmentOffset(size_t line_number) { 1095 Vector2d RenderText::GetAlignmentOffset(size_t line_number) {
1089 // TODO(ckocagil): Enable |lines_| usage in other platforms. 1096 // TODO(ckocagil): Enable |lines_| usage in other platforms.
1090 #if defined(OS_WIN) 1097 #if defined(OS_WIN)
1091 DCHECK_LT(line_number, lines_.size()); 1098 DCHECK_LT(line_number, lines_.size());
1092 #endif 1099 #endif
1093 Vector2d offset; 1100 Vector2d offset;
1094 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment(); 1101 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment();
1095 if (horizontal_alignment != ALIGN_LEFT) { 1102 if (horizontal_alignment != ALIGN_LEFT) {
1096 #if defined(OS_WIN) 1103 #if defined(OS_WIN)
1097 const int width = std::ceil(lines_[line_number].size.width()) + 1104 const int width = std::ceil(lines_[line_number].size.width()) +
1098 (cursor_enabled_ ? 1 : 0); 1105 (cursor_enabled_ ? 1 : 0);
1099 #else 1106 #else
1100 const int width = GetContentWidth(); 1107 const int width = GetContentWidth();
1101 #endif 1108 #endif
1102 offset.set_x(display_rect().width() - width); 1109 offset.set_x(display_rect().width() - width);
1103 // Put any extra margin pixel on the left to match legacy behavior. 1110 // Put any extra margin pixel on the left to match legacy behavior.
1104 if (horizontal_alignment == ALIGN_CENTER) 1111 if (horizontal_alignment == ALIGN_CENTER)
1105 offset.set_x((offset.x() + 1) / 2); 1112 offset.set_x((offset.x() + 1) / 2);
1106 } 1113 }
1107 1114
1108 // Vertically center the text. 1115 // Vertically center the text.
1109 if (multiline_) { 1116 if (multiline_) {
1110 const int text_height = lines_.back().preceding_heights + 1117 const int text_height = lines_.back().preceding_heights +
1111 lines_.back().size.height(); 1118 lines_.back().size.height();
1112 offset.set_y((display_rect_.height() - text_height) / 2); 1119 offset.set_y((display_rect_.height() - text_height) / 2);
1113 } else { 1120 } else {
1114 offset.set_y(GetBaseline() - GetLayoutTextBaseline()); 1121 offset.set_y(GetBaseline() - GetDisplayTextBaseline());
1115 } 1122 }
1116 1123
1117 return offset; 1124 return offset;
1118 } 1125 }
1119 1126
1120 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { 1127 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
1121 const int width = display_rect().width(); 1128 const int width = display_rect().width();
1122 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width) 1129 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width)
1123 return; 1130 return;
1124 1131
(...skipping 24 matching lines...) Expand all
1149 text_rect, left_part, right_part, colors_.breaks().front().second); 1156 text_rect, left_part, right_part, colors_.breaks().front().second);
1150 if (shader) 1157 if (shader)
1151 renderer->SetShader(shader.get()); 1158 renderer->SetShader(shader.get());
1152 } 1159 }
1153 1160
1154 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 1161 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
1155 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); 1162 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_);
1156 renderer->SetDrawLooper(looper.get()); 1163 renderer->SetDrawLooper(looper.get());
1157 } 1164 }
1158 1165
1166 base::i18n::TextDirection RenderText::GetTextDirection(
1167 const base::string16& text) {
1168 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) {
1169 switch (directionality_mode_) {
1170 case DIRECTIONALITY_FROM_TEXT:
1171 // Derive the direction from the display text, which differs from text()
1172 // in the case of obscured (password) textfields.
1173 text_direction_ =
1174 base::i18n::GetFirstStrongCharacterDirection(text);
1175 break;
1176 case DIRECTIONALITY_FROM_UI:
1177 text_direction_ = base::i18n::IsRTL() ? base::i18n::RIGHT_TO_LEFT :
1178 base::i18n::LEFT_TO_RIGHT;
1179 break;
1180 case DIRECTIONALITY_FORCE_LTR:
1181 text_direction_ = base::i18n::LEFT_TO_RIGHT;
1182 break;
1183 case DIRECTIONALITY_FORCE_RTL:
1184 text_direction_ = base::i18n::RIGHT_TO_LEFT;
1185 break;
1186 default:
1187 NOTREACHED();
1188 }
1189 }
1190
1191 return text_direction_;
1192 }
1193
1159 // static 1194 // static
1160 bool RenderText::RangeContainsCaret(const Range& range, 1195 bool RenderText::RangeContainsCaret(const Range& range,
1161 size_t caret_pos, 1196 size_t caret_pos,
1162 LogicalCursorDirection caret_affinity) { 1197 LogicalCursorDirection caret_affinity) {
1163 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). 1198 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9).
1164 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? 1199 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ?
1165 caret_pos - 1 : caret_pos + 1; 1200 caret_pos - 1 : caret_pos + 1;
1166 return range.Contains(Range(caret_pos, adjacent)); 1201 return range.Contains(Range(caret_pos, adjacent));
1167 } 1202 }
1168 1203
1169 void RenderText::MoveCursorTo(size_t position, bool select) { 1204 void RenderText::MoveCursorTo(size_t position, bool select) {
1170 size_t cursor = std::min(position, text().length()); 1205 size_t cursor = std::min(position, text().length());
1171 if (IsValidCursorIndex(cursor)) 1206 if (IsValidCursorIndex(cursor))
1172 SetSelectionModel(SelectionModel( 1207 SetSelectionModel(SelectionModel(
1173 Range(select ? selection().start() : cursor, cursor), 1208 Range(select ? selection().start() : cursor, cursor),
1174 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); 1209 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD));
1175 } 1210 }
1176 1211
1177 void RenderText::UpdateLayoutText() { 1212 void RenderText::OnTextAttributeChanged() {
1178 layout_text_.clear(); 1213 layout_text_.clear();
1214 display_text_.clear();
1179 line_breaks_.SetMax(0); 1215 line_breaks_.SetMax(0);
1180 1216
1181 if (obscured_) { 1217 if (obscured_) {
1182 size_t obscured_text_length = 1218 size_t obscured_text_length =
1183 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length())); 1219 static_cast<size_t>(UTF16IndexToOffset(text_, 0, text_.length()));
1184 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); 1220 layout_text_.assign(obscured_text_length, kPasswordReplacementChar);
1185 1221
1186 if (obscured_reveal_index_ >= 0 && 1222 if (obscured_reveal_index_ >= 0 &&
1187 obscured_reveal_index_ < static_cast<int>(text_.length())) { 1223 obscured_reveal_index_ < static_cast<int>(text_.length())) {
1188 // Gets the index range in |text_| to be revealed. 1224 // Gets the index range in |text_| to be revealed.
(...skipping 27 matching lines...) Expand all
1216 iter.setIndex32(text.length() - (truncate_length_ / 2)); 1252 iter.setIndex32(text.length() - (truncate_length_ / 2));
1217 const size_t ellipsis_end = iter.getIndex(); 1253 const size_t ellipsis_end = iter.getIndex();
1218 DCHECK_LE(ellipsis_start, ellipsis_end); 1254 DCHECK_LE(ellipsis_start, ellipsis_end);
1219 layout_text_.assign(text.substr(0, ellipsis_start) + kEllipsisUTF16 + 1255 layout_text_.assign(text.substr(0, ellipsis_start) + kEllipsisUTF16 +
1220 text.substr(ellipsis_end)); 1256 text.substr(ellipsis_end));
1221 } else { 1257 } else {
1222 iter.setIndex32(truncate_length_ - 1); 1258 iter.setIndex32(truncate_length_ - 1);
1223 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16); 1259 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16);
1224 } 1260 }
1225 } 1261 }
1226
1227 if (elide_behavior_ != NO_ELIDE &&
1228 elide_behavior_ != FADE_TAIL &&
1229 !layout_text_.empty() &&
1230 GetContentWidth() > display_rect_.width()) {
1231 // This doesn't trim styles so ellipsis may get rendered as a different
1232 // style than the preceding text. See crbug.com/327850.
1233 layout_text_.assign(Elide(layout_text_,
1234 static_cast<float>(display_rect_.width()),
1235 elide_behavior_));
1236 }
1237
1238 // Replace the newline character with a newline symbol in single line mode.
1239 static const base::char16 kNewline[] = { '\n', 0 }; 1262 static const base::char16 kNewline[] = { '\n', 0 };
1240 static const base::char16 kNewlineSymbol[] = { 0x2424, 0 }; 1263 static const base::char16 kNewlineSymbol[] = { 0x2424, 0 };
1241 if (!multiline_ && replace_newline_chars_with_symbols_) 1264 if (!multiline_)
1242 base::ReplaceChars(layout_text_, kNewline, kNewlineSymbol, &layout_text_); 1265 base::ReplaceChars(layout_text_, kNewline, kNewlineSymbol, &layout_text_);
1243 1266
1244 ResetLayout(); 1267 OnLayoutTextAttributeChanged(true);
1245 } 1268 }
1246 1269
1247 base::string16 RenderText::Elide(const base::string16& text, 1270 base::string16 RenderText::Elide(const base::string16& text,
1271 float text_width,
1248 float available_width, 1272 float available_width,
1249 ElideBehavior behavior) { 1273 ElideBehavior behavior) {
1250 if (available_width <= 0 || text.empty()) 1274 if (available_width <= 0 || text.empty())
1251 return base::string16(); 1275 return base::string16();
1252 if (behavior == ELIDE_EMAIL) 1276 if (behavior == ELIDE_EMAIL)
1253 return ElideEmail(text, available_width); 1277 return ElideEmail(text, available_width);
1278 if (text_width > 0 && text_width < available_width)
1279 return text;
1280
1281 TRACE_EVENT0("ui", "RenderText::Elide");
1254 1282
1255 // Create a RenderText copy with attributes that affect the rendering width. 1283 // Create a RenderText copy with attributes that affect the rendering width.
1256 scoped_ptr<RenderText> render_text = CreateInstanceOfSameType(); 1284 scoped_ptr<RenderText> render_text = CreateInstanceOfSameType();
1257 render_text->SetFontList(font_list_); 1285 render_text->SetFontList(font_list_);
1258 render_text->SetDirectionalityMode(directionality_mode_); 1286 render_text->SetDirectionalityMode(directionality_mode_);
1259 render_text->SetCursorEnabled(cursor_enabled_); 1287 render_text->SetCursorEnabled(cursor_enabled_);
1260 render_text->set_truncate_length(truncate_length_); 1288 render_text->set_truncate_length(truncate_length_);
1261 render_text->styles_ = styles_; 1289 render_text->styles_ = styles_;
1262 render_text->colors_ = colors_; 1290 render_text->colors_ = colors_;
1263 render_text->SetText(text); 1291 if (text_width == 0) {
1264 if (render_text->GetContentWidthF() <= available_width) 1292 render_text->SetText(text);
1293 text_width = render_text->GetContentWidthF();
1294 }
1295 if (text_width <= available_width)
1265 return text; 1296 return text;
1266 1297
1267 const base::string16 ellipsis = base::string16(kEllipsisUTF16); 1298 const base::string16 ellipsis = base::string16(kEllipsisUTF16);
1268 const bool insert_ellipsis = (behavior != TRUNCATE); 1299 const bool insert_ellipsis = (behavior != TRUNCATE);
1269 const bool elide_in_middle = (behavior == ELIDE_MIDDLE); 1300 const bool elide_in_middle = (behavior == ELIDE_MIDDLE);
1270 const bool elide_at_beginning = (behavior == ELIDE_HEAD); 1301 const bool elide_at_beginning = (behavior == ELIDE_HEAD);
1302
1303 if (insert_ellipsis) {
1304 render_text->SetText(ellipsis);
1305 const float ellipsis_width = render_text->GetContentWidthF();
1306 if (ellipsis_width > available_width)
1307 return base::string16();
1308 }
1309
1271 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning); 1310 StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning);
1272 1311
1273 render_text->SetText(ellipsis);
1274 const float ellipsis_width = render_text->GetContentWidthF();
1275
1276 if (insert_ellipsis && (ellipsis_width > available_width))
1277 return base::string16();
1278
1279 // Use binary search to compute the elided text. 1312 // Use binary search to compute the elided text.
1280 size_t lo = 0; 1313 size_t lo = 0;
1281 size_t hi = text.length() - 1; 1314 size_t hi = text.length() - 1;
1282 const base::i18n::TextDirection text_direction = GetTextDirection(); 1315 const base::i18n::TextDirection text_direction = GetTextDirection(text);
1283 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { 1316 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) {
1284 // Restore colors. They will be truncated to size by SetText. 1317 // Restore colors. They will be truncated to size by SetText.
1285 render_text->colors_ = colors_; 1318 render_text->colors_ = colors_;
1286 base::string16 new_text = 1319 base::string16 new_text =
1287 slicer.CutString(guess, insert_ellipsis && behavior != ELIDE_TAIL); 1320 slicer.CutString(guess, insert_ellipsis && behavior != ELIDE_TAIL);
1288 render_text->SetText(new_text); 1321 render_text->SetText(new_text);
1289 1322
1290 // This has to be an additional step so that the ellipsis is rendered with 1323 // This has to be an additional step so that the ellipsis is rendered with
1291 // same style as trailing part of the text. 1324 // same style as trailing part of the text.
1292 if (insert_ellipsis && behavior == ELIDE_TAIL) { 1325 if (insert_ellipsis && behavior == ELIDE_TAIL) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 // Elide the domain so that it only takes half of the available width. 1410 // Elide the domain so that it only takes half of the available width.
1378 // Should the username not need all the width available in its half, the 1411 // Should the username not need all the width available in its half, the
1379 // domain will occupy the leftover width. 1412 // domain will occupy the leftover width.
1380 // If |desired_domain_width| is greater than |available_domain_width|: the 1413 // If |desired_domain_width| is greater than |available_domain_width|: the
1381 // minimal username elision allowed by the specifications will not fit; thus 1414 // minimal username elision allowed by the specifications will not fit; thus
1382 // |desired_domain_width| must be <= |available_domain_width| at all cost. 1415 // |desired_domain_width| must be <= |available_domain_width| at all cost.
1383 const float desired_domain_width = 1416 const float desired_domain_width =
1384 std::min<float>(available_domain_width, 1417 std::min<float>(available_domain_width,
1385 std::max<float>(available_width - full_username_width, 1418 std::max<float>(available_width - full_username_width,
1386 available_width / 2)); 1419 available_width / 2));
1387 domain = Elide(domain, desired_domain_width, ELIDE_MIDDLE); 1420 domain = Elide(domain, 0, desired_domain_width, ELIDE_MIDDLE);
1388 // Failing to elide the domain such that at least one character remains 1421 // Failing to elide the domain such that at least one character remains
1389 // (other than the ellipsis itself) remains: return a single ellipsis. 1422 // (other than the ellipsis itself) remains: return a single ellipsis.
1390 if (domain.length() <= 1U) 1423 if (domain.length() <= 1U)
1391 return base::string16(kEllipsisUTF16); 1424 return base::string16(kEllipsisUTF16);
1392 } 1425 }
1393 1426
1394 // Fit the username in the remaining width (at this point the elided username 1427 // Fit the username in the remaining width (at this point the elided username
1395 // is guaranteed to fit with at least one character remaining given all the 1428 // is guaranteed to fit with at least one character remaining given all the
1396 // precautions taken earlier). 1429 // precautions taken earlier).
1397 available_width -= GetStringWidthF(domain, font_list()); 1430 available_width -= GetStringWidthF(domain, font_list());
1398 username = Elide(username, available_width, ELIDE_TAIL); 1431 username = Elide(username, 0, available_width, ELIDE_TAIL);
1399 return username + kAtSignUTF16 + domain; 1432 return username + kAtSignUTF16 + domain;
1400 } 1433 }
1401 1434
1402 void RenderText::UpdateCachedBoundsAndOffset() { 1435 void RenderText::UpdateCachedBoundsAndOffset() {
1403 if (cached_bounds_and_offset_valid_) 1436 if (cached_bounds_and_offset_valid_)
1404 return; 1437 return;
1405 1438
1406 // TODO(ckocagil): Add support for scrolling multiline text. 1439 // TODO(ckocagil): Add support for scrolling multiline text.
1407 1440
1408 int delta_x = 0; 1441 int delta_x = 0;
(...skipping 10 matching lines...) Expand all
1419 if (cursor_bounds_.right() > display_rect_.right()) 1452 if (cursor_bounds_.right() > display_rect_.right())
1420 delta_x = display_rect_.right() - cursor_bounds_.right(); 1453 delta_x = display_rect_.right() - cursor_bounds_.right();
1421 else if (cursor_bounds_.x() < display_rect_.x()) 1454 else if (cursor_bounds_.x() < display_rect_.x())
1422 delta_x = display_rect_.x() - cursor_bounds_.x(); 1455 delta_x = display_rect_.x() - cursor_bounds_.x();
1423 } 1456 }
1424 1457
1425 SetDisplayOffset(display_offset_.x() + delta_x); 1458 SetDisplayOffset(display_offset_.x() + delta_x);
1426 } 1459 }
1427 1460
1428 void RenderText::DrawSelection(Canvas* canvas) { 1461 void RenderText::DrawSelection(Canvas* canvas) {
1429 const std::vector<Rect> sel = GetSubstringBounds(selection()); 1462 for (const Rect& s : GetSubstringBounds(selection()))
1430 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 1463 canvas->FillRect(s, selection_background_focused_color_);
1431 canvas->FillRect(*i, selection_background_focused_color_);
1432 } 1464 }
1433 1465
1434 } // namespace gfx 1466 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_harfbuzz.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698