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

Side by Side Diff: ui/gfx/render_text_harfbuzz.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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_harfbuzz.h" 5 #include "ui/gfx/render_text_harfbuzz.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/i18n/bidi_line_iterator.h" 9 #include "base/i18n/bidi_line_iterator.h"
10 #include "base/i18n/break_iterator.h" 10 #include "base/i18n/break_iterator.h"
11 #include "base/i18n/char_iterator.h" 11 #include "base/i18n/char_iterator.h"
12 #include "base/profiler/scoped_tracker.h" 12 #include "base/profiler/scoped_tracker.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/trace_event/trace_event.h"
13 #include "third_party/harfbuzz-ng/src/hb.h" 15 #include "third_party/harfbuzz-ng/src/hb.h"
14 #include "third_party/icu/source/common/unicode/ubidi.h" 16 #include "third_party/icu/source/common/unicode/ubidi.h"
15 #include "third_party/skia/include/core/SkColor.h" 17 #include "third_party/skia/include/core/SkColor.h"
16 #include "third_party/skia/include/core/SkTypeface.h" 18 #include "third_party/skia/include/core/SkTypeface.h"
17 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/font_fallback.h" 20 #include "ui/gfx/font_fallback.h"
19 #include "ui/gfx/font_render_params.h" 21 #include "ui/gfx/font_render_params.h"
20 #include "ui/gfx/harfbuzz_font_skia.h" 22 #include "ui/gfx/harfbuzz_font_skia.h"
21 #include "ui/gfx/utf16_indexing.h" 23 #include "ui/gfx/utf16_indexing.h"
22 24
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 cluster_width * (before + 1) / static_cast<float>(total)); 569 cluster_width * (before + 1) / static_cast<float>(total));
568 return RangeF(preceding_run_widths + grapheme_begin_x, 570 return RangeF(preceding_run_widths + grapheme_begin_x,
569 preceding_run_widths + grapheme_end_x); 571 preceding_run_widths + grapheme_end_x);
570 } 572 }
571 } 573 }
572 574
573 return RangeF(preceding_run_widths + cluster_begin_x, 575 return RangeF(preceding_run_widths + cluster_begin_x,
574 preceding_run_widths + cluster_end_x); 576 preceding_run_widths + cluster_end_x);
575 } 577 }
576 578
579 TextRunList::TextRunList() : width_(0.0f) {}
580
581 TextRunList::~TextRunList() {}
582
583 void TextRunList::Reset() {
584 runs_.clear();
585 width_ = 0.0f;
586 }
587
588 void TextRunList::InitIndexMap() {
589 if (runs_.size() == 1) {
590 visual_to_logical_ = logical_to_visual_ = std::vector<int32_t>(1, 0);
591 return;
592 }
593 const size_t num_runs = runs_.size();
594 std::vector<UBiDiLevel> levels(num_runs);
595 for (size_t i = 0; i < num_runs; ++i)
596 levels[i] = runs_[i]->level;
597 visual_to_logical_.resize(num_runs);
598 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]);
599 logical_to_visual_.resize(num_runs);
600 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]);
601 }
602
603 void TextRunList::ComputePrecedingRunWidths() {
604 // Precalculate run width information.
605 width_ = 0.0f;
606 for (size_t i = 0; i < runs_.size(); ++i) {
607 TextRunHarfBuzz* run = runs_[visual_to_logical_[i]];
608 run->preceding_run_widths = width_;
609 width_ += run->width;
610 }
611 }
612
577 } // namespace internal 613 } // namespace internal
578 614
579 RenderTextHarfBuzz::RenderTextHarfBuzz() 615 RenderTextHarfBuzz::RenderTextHarfBuzz()
580 : RenderText(), 616 : RenderText(),
581 needs_layout_(false), 617 update_layout_run_list_(false),
618 update_display_run_list_(false),
619 update_grapheme_iterator_(false),
620 update_display_text_(false),
582 glyph_width_for_test_(0u) { 621 glyph_width_for_test_(0u) {
583 set_truncate_length(kMaxTextLength); 622 set_truncate_length(kMaxTextLength);
584 } 623 }
585 624
586 RenderTextHarfBuzz::~RenderTextHarfBuzz() {} 625 RenderTextHarfBuzz::~RenderTextHarfBuzz() {}
587 626
588 scoped_ptr<RenderText> RenderTextHarfBuzz::CreateInstanceOfSameType() const { 627 scoped_ptr<RenderText> RenderTextHarfBuzz::CreateInstanceOfSameType() const {
589 return scoped_ptr<RenderTextHarfBuzz>(new RenderTextHarfBuzz); 628 return scoped_ptr<RenderTextHarfBuzz>(new RenderTextHarfBuzz);
590 } 629 }
591 630
631 const base::string16& RenderTextHarfBuzz::GetDisplayText() {
632 // TODO(oshima): Consider supporting eliding multi-line text.
633 // This requires max_line support first.
634 if (multiline() ||
635 elide_behavior() == NO_ELIDE ||
636 elide_behavior() == FADE_TAIL) {
637 // Call UpdateDisplayText to clear |display_text_| and |text_elided_|
638 // on the RenderText class.
639 UpdateDisplayText(0);
640 update_display_text_ = false;
641 display_run_list_.reset();
642 return layout_text();
643 }
644
645 EnsureLayoutRunList();
646 DCHECK(!update_display_text_);
647 return text_elided() ? display_text() : layout_text();
648 }
649
592 Size RenderTextHarfBuzz::GetStringSize() { 650 Size RenderTextHarfBuzz::GetStringSize() {
593 const SizeF size_f = GetStringSizeF(); 651 const SizeF size_f = GetStringSizeF();
594 return Size(std::ceil(size_f.width()), size_f.height()); 652 return Size(std::ceil(size_f.width()), size_f.height());
595 } 653 }
596 654
597 SizeF RenderTextHarfBuzz::GetStringSizeF() { 655 SizeF RenderTextHarfBuzz::GetStringSizeF() {
598 EnsureLayout(); 656 EnsureLayout();
599 return total_size_; 657 return total_size_;
600 } 658 }
601 659
602 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) { 660 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) {
603 EnsureLayout(); 661 EnsureLayout();
604 662
605 int x = ToTextPoint(point).x(); 663 int x = ToTextPoint(point).x();
606 float offset = 0; 664 float offset = 0;
607 size_t run_index = GetRunContainingXCoord(x, &offset); 665 size_t run_index = GetRunContainingXCoord(x, &offset);
608 if (run_index >= runs_.size()) 666
667 internal::TextRunList* run_list = GetRunList();
668 if (run_index >= run_list->size())
609 return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT); 669 return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT);
610 const internal::TextRunHarfBuzz& run = *runs_[run_index]; 670 const internal::TextRunHarfBuzz& run = *run_list->runs()[run_index];
611
612 for (size_t i = 0; i < run.glyph_count; ++i) { 671 for (size_t i = 0; i < run.glyph_count; ++i) {
613 const SkScalar end = 672 const SkScalar end =
614 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); 673 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x();
615 const SkScalar middle = (end + run.positions[i].x()) / 2; 674 const SkScalar middle = (end + run.positions[i].x()) / 2;
616 675
617 if (offset < middle) { 676 if (offset < middle) {
618 return SelectionModel(LayoutIndexToTextIndex( 677 return SelectionModel(DisplayIndexToTextIndex(
619 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)), 678 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)),
620 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD)); 679 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD));
621 } 680 }
622 if (offset < end) { 681 if (offset < end) {
623 return SelectionModel(LayoutIndexToTextIndex( 682 return SelectionModel(DisplayIndexToTextIndex(
624 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)), 683 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)),
625 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD)); 684 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD));
626 } 685 }
627 } 686 }
628 return EdgeSelectionModel(CURSOR_RIGHT); 687 return EdgeSelectionModel(CURSOR_RIGHT);
629 } 688 }
630 689
631 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { 690 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() {
632 EnsureLayout(); 691 EnsureLayout();
633 692
693 internal::TextRunList* run_list = GetRunList();
634 std::vector<RenderText::FontSpan> spans; 694 std::vector<RenderText::FontSpan> spans;
635 for (size_t i = 0; i < runs_.size(); ++i) { 695 for (auto* run : run_list->runs()) {
636 SkString family_name; 696 SkString family_name;
637 runs_[i]->skia_face->getFamilyName(&family_name); 697 run->skia_face->getFamilyName(&family_name);
638 Font font(family_name.c_str(), runs_[i]->font_size); 698 Font font(family_name.c_str(), run->font_size);
639 spans.push_back(RenderText::FontSpan(font, 699 spans.push_back(RenderText::FontSpan(
640 Range(LayoutIndexToTextIndex(runs_[i]->range.start()), 700 font,
641 LayoutIndexToTextIndex(runs_[i]->range.end())))); 701 Range(DisplayIndexToTextIndex(run->range.start()),
702 DisplayIndexToTextIndex(run->range.end()))));
642 } 703 }
643 704
644 return spans; 705 return spans;
645 } 706 }
646 707
647 Range RenderTextHarfBuzz::GetGlyphBounds(size_t index) { 708 Range RenderTextHarfBuzz::GetGlyphBounds(size_t index) {
648 EnsureLayout(); 709 EnsureLayout();
649 const size_t run_index = 710 const size_t run_index =
650 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD)); 711 GetRunContainingCaret(SelectionModel(index, CURSOR_FORWARD));
712 internal::TextRunList* run_list = GetRunList();
651 // Return edge bounds if the index is invalid or beyond the layout text size. 713 // Return edge bounds if the index is invalid or beyond the layout text size.
652 if (run_index >= runs_.size()) 714 if (run_index >= run_list->size())
653 return Range(GetStringSize().width()); 715 return Range(GetStringSize().width());
654 const size_t layout_index = TextIndexToLayoutIndex(index); 716 const size_t layout_index = TextIndexToDisplayIndex(index);
655 internal::TextRunHarfBuzz* run = runs_[run_index]; 717 internal::TextRunHarfBuzz* run = run_list->runs()[run_index];
656 RangeF bounds = 718 RangeF bounds =
657 run->GetGraphemeBounds(grapheme_iterator_.get(), layout_index); 719 run->GetGraphemeBounds(GetGraphemeIterator(), layout_index);
658 // If cursor is enabled, extend the last glyph up to the rightmost cursor 720 // If cursor is enabled, extend the last glyph up to the rightmost cursor
659 // position since clients expect them to be contiguous. 721 // position since clients expect them to be contiguous.
660 if (cursor_enabled() && run_index == runs_.size() - 1 && 722 if (cursor_enabled() && run_index == run_list->size() - 1 &&
661 index == (run->is_rtl ? run->range.start() : run->range.end() - 1)) 723 index == (run->is_rtl ? run->range.start() : run->range.end() - 1))
662 bounds.second = std::ceil(bounds.second); 724 bounds.second = std::ceil(bounds.second);
663 return RoundRangeF(run->is_rtl ? 725 return RoundRangeF(run->is_rtl ?
664 RangeF(bounds.second, bounds.first) : bounds); 726 RangeF(bounds.second, bounds.first) : bounds);
665 } 727 }
666 728
667 int RenderTextHarfBuzz::GetLayoutTextBaseline() { 729 int RenderTextHarfBuzz::GetDisplayTextBaseline() {
668 EnsureLayout(); 730 EnsureLayout();
669 return lines()[0].baseline; 731 return lines()[0].baseline;
670 } 732 }
671 733
672 SelectionModel RenderTextHarfBuzz::AdjacentCharSelectionModel( 734 SelectionModel RenderTextHarfBuzz::AdjacentCharSelectionModel(
673 const SelectionModel& selection, 735 const SelectionModel& selection,
674 VisualCursorDirection direction) { 736 VisualCursorDirection direction) {
675 DCHECK(!needs_layout_); 737 DCHECK(!update_display_run_list_);
738
739 internal::TextRunList* run_list = GetRunList();
676 internal::TextRunHarfBuzz* run; 740 internal::TextRunHarfBuzz* run;
741
677 size_t run_index = GetRunContainingCaret(selection); 742 size_t run_index = GetRunContainingCaret(selection);
678 if (run_index >= runs_.size()) { 743 if (run_index >= run_list->size()) {
679 // The cursor is not in any run: we're at the visual and logical edge. 744 // The cursor is not in any run: we're at the visual and logical edge.
680 SelectionModel edge = EdgeSelectionModel(direction); 745 SelectionModel edge = EdgeSelectionModel(direction);
681 if (edge.caret_pos() == selection.caret_pos()) 746 if (edge.caret_pos() == selection.caret_pos())
682 return edge; 747 return edge;
683 int visual_index = (direction == CURSOR_RIGHT) ? 0 : runs_.size() - 1; 748 int visual_index = (direction == CURSOR_RIGHT) ? 0 : run_list->size() - 1;
684 run = runs_[visual_to_logical_[visual_index]]; 749 run = run_list->runs()[run_list->visual_to_logical(visual_index)];
685 } else { 750 } else {
686 // If the cursor is moving within the current run, just move it by one 751 // If the cursor is moving within the current run, just move it by one
687 // grapheme in the appropriate direction. 752 // grapheme in the appropriate direction.
688 run = runs_[run_index]; 753 run = run_list->runs()[run_index];
689 size_t caret = selection.caret_pos(); 754 size_t caret = selection.caret_pos();
690 bool forward_motion = run->is_rtl == (direction == CURSOR_LEFT); 755 bool forward_motion = run->is_rtl == (direction == CURSOR_LEFT);
691 if (forward_motion) { 756 if (forward_motion) {
692 if (caret < LayoutIndexToTextIndex(run->range.end())) { 757 if (caret < DisplayIndexToTextIndex(run->range.end())) {
693 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD); 758 caret = IndexOfAdjacentGrapheme(caret, CURSOR_FORWARD);
694 return SelectionModel(caret, CURSOR_BACKWARD); 759 return SelectionModel(caret, CURSOR_BACKWARD);
695 } 760 }
696 } else { 761 } else {
697 if (caret > LayoutIndexToTextIndex(run->range.start())) { 762 if (caret > DisplayIndexToTextIndex(run->range.start())) {
698 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD); 763 caret = IndexOfAdjacentGrapheme(caret, CURSOR_BACKWARD);
699 return SelectionModel(caret, CURSOR_FORWARD); 764 return SelectionModel(caret, CURSOR_FORWARD);
700 } 765 }
701 } 766 }
702 // The cursor is at the edge of a run; move to the visually adjacent run. 767 // The cursor is at the edge of a run; move to the visually adjacent run.
703 int visual_index = logical_to_visual_[run_index]; 768 int visual_index = run_list->logical_to_visual(run_index);
704 visual_index += (direction == CURSOR_LEFT) ? -1 : 1; 769 visual_index += (direction == CURSOR_LEFT) ? -1 : 1;
705 if (visual_index < 0 || visual_index >= static_cast<int>(runs_.size())) 770 if (visual_index < 0 || visual_index >= static_cast<int>(run_list->size()))
706 return EdgeSelectionModel(direction); 771 return EdgeSelectionModel(direction);
707 run = runs_[visual_to_logical_[visual_index]]; 772 run = run_list->runs()[run_list->visual_to_logical(visual_index)];
708 } 773 }
709 bool forward_motion = run->is_rtl == (direction == CURSOR_LEFT); 774 bool forward_motion = run->is_rtl == (direction == CURSOR_LEFT);
710 return forward_motion ? FirstSelectionModelInsideRun(run) : 775 return forward_motion ? FirstSelectionModelInsideRun(run) :
711 LastSelectionModelInsideRun(run); 776 LastSelectionModelInsideRun(run);
712 } 777 }
713 778
714 SelectionModel RenderTextHarfBuzz::AdjacentWordSelectionModel( 779 SelectionModel RenderTextHarfBuzz::AdjacentWordSelectionModel(
715 const SelectionModel& selection, 780 const SelectionModel& selection,
716 VisualCursorDirection direction) { 781 VisualCursorDirection direction) {
717 if (obscured()) 782 if (obscured())
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 // Move to the top of current word. 816 // Move to the top of current word.
752 pos = begin; 817 pos = begin;
753 break; 818 break;
754 } 819 }
755 pos = iter.pos() - iter.GetString().length(); 820 pos = iter.pos() - iter.GetString().length();
756 } 821 }
757 } 822 }
758 } 823 }
759 return SelectionModel(pos, CURSOR_FORWARD); 824 return SelectionModel(pos, CURSOR_FORWARD);
760 #else 825 #else
826 internal::TextRunList* run_list = GetRunList();
761 SelectionModel cur(selection); 827 SelectionModel cur(selection);
762 for (;;) { 828 for (;;) {
763 cur = AdjacentCharSelectionModel(cur, direction); 829 cur = AdjacentCharSelectionModel(cur, direction);
764 size_t run = GetRunContainingCaret(cur); 830 size_t run = GetRunContainingCaret(cur);
765 if (run == runs_.size()) 831 if (run == run_list->size())
766 break; 832 break;
767 const bool is_forward = runs_[run]->is_rtl == (direction == CURSOR_LEFT); 833 const bool is_forward =
834 run_list->runs()[run]->is_rtl == (direction == CURSOR_LEFT);
768 size_t cursor = cur.caret_pos(); 835 size_t cursor = cur.caret_pos();
769 if (is_forward ? iter.IsEndOfWord(cursor) : iter.IsStartOfWord(cursor)) 836 if (is_forward ? iter.IsEndOfWord(cursor) : iter.IsStartOfWord(cursor))
770 break; 837 break;
771 } 838 }
772 return cur; 839 return cur;
773 #endif 840 #endif
774 } 841 }
775 842
776 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { 843 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) {
777 DCHECK(!needs_layout_); 844 DCHECK(!update_display_run_list_);
778 DCHECK(Range(0, text().length()).Contains(range)); 845 DCHECK(Range(0, text().length()).Contains(range));
779 Range layout_range(TextIndexToLayoutIndex(range.start()), 846 Range layout_range(TextIndexToDisplayIndex(range.start()),
780 TextIndexToLayoutIndex(range.end())); 847 TextIndexToDisplayIndex(range.end()));
781 DCHECK(Range(0, GetLayoutText().length()).Contains(layout_range)); 848 DCHECK(Range(0, GetDisplayText().length()).Contains(layout_range));
782 849
783 std::vector<Rect> rects; 850 std::vector<Rect> rects;
784 if (layout_range.is_empty()) 851 if (layout_range.is_empty())
785 return rects; 852 return rects;
786 std::vector<Range> bounds; 853 std::vector<Range> bounds;
787 854
855 internal::TextRunList* run_list = GetRunList();
856
788 // Add a Range for each run/selection intersection. 857 // Add a Range for each run/selection intersection.
789 for (size_t i = 0; i < runs_.size(); ++i) { 858 for (size_t i = 0; i < run_list->size(); ++i) {
790 internal::TextRunHarfBuzz* run = runs_[visual_to_logical_[i]]; 859 internal::TextRunHarfBuzz* run =
860 run_list->runs()[run_list->visual_to_logical(i)];
791 Range intersection = run->range.Intersect(layout_range); 861 Range intersection = run->range.Intersect(layout_range);
792 if (!intersection.IsValid()) 862 if (!intersection.IsValid())
793 continue; 863 continue;
794 DCHECK(!intersection.is_reversed()); 864 DCHECK(!intersection.is_reversed());
795 const Range leftmost_character_x = RoundRangeF(run->GetGraphemeBounds( 865 const Range leftmost_character_x = RoundRangeF(run->GetGraphemeBounds(
796 grapheme_iterator_.get(), 866 GetGraphemeIterator(),
797 run->is_rtl ? intersection.end() - 1 : intersection.start())); 867 run->is_rtl ? intersection.end() - 1 : intersection.start()));
798 const Range rightmost_character_x = RoundRangeF(run->GetGraphemeBounds( 868 const Range rightmost_character_x = RoundRangeF(run->GetGraphemeBounds(
799 grapheme_iterator_.get(), 869 GetGraphemeIterator(),
800 run->is_rtl ? intersection.start() : intersection.end() - 1)); 870 run->is_rtl ? intersection.start() : intersection.end() - 1));
801 Range range_x(leftmost_character_x.start(), rightmost_character_x.end()); 871 Range range_x(leftmost_character_x.start(), rightmost_character_x.end());
802 DCHECK(!range_x.is_reversed()); 872 DCHECK(!range_x.is_reversed());
803 if (range_x.is_empty()) 873 if (range_x.is_empty())
804 continue; 874 continue;
805 875
806 // Union this with the last range if they're adjacent. 876 // Union this with the last range if they're adjacent.
807 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin()); 877 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin());
808 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) { 878 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
809 range_x = Range(bounds.back().GetMin(), range_x.GetMax()); 879 range_x = Range(bounds.back().GetMin(), range_x.GetMax());
810 bounds.pop_back(); 880 bounds.pop_back();
811 } 881 }
812 bounds.push_back(range_x); 882 bounds.push_back(range_x);
813 } 883 }
814 for (size_t i = 0; i < bounds.size(); ++i) { 884 for (Range& bound : bounds) {
815 std::vector<Rect> current_rects = TextBoundsToViewBounds(bounds[i]); 885 std::vector<Rect> current_rects = TextBoundsToViewBounds(bound);
816 rects.insert(rects.end(), current_rects.begin(), current_rects.end()); 886 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
817 } 887 }
818 return rects; 888 return rects;
819 } 889 }
820 890
821 size_t RenderTextHarfBuzz::TextIndexToLayoutIndex(size_t index) const { 891 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) {
822 DCHECK_LE(index, text().length()); 892 return TextIndexToGivenTextIndex(GetDisplayText(), index);
823 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index;
824 CHECK_GE(i, 0);
825 // Clamp layout indices to the length of the text actually used for layout.
826 return std::min<size_t>(GetLayoutText().length(), i);
827 } 893 }
828 894
829 size_t RenderTextHarfBuzz::LayoutIndexToTextIndex(size_t index) const { 895 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) {
830 if (!obscured()) 896 if (!obscured())
831 return index; 897 return index;
832
833 DCHECK_LE(index, GetLayoutText().length());
834 const size_t text_index = UTF16OffsetToIndex(text(), 0, index); 898 const size_t text_index = UTF16OffsetToIndex(text(), 0, index);
835 DCHECK_LE(text_index, text().length()); 899 DCHECK_LE(text_index, text().length());
836 return text_index; 900 return text_index;
837 } 901 }
838 902
839 bool RenderTextHarfBuzz::IsValidCursorIndex(size_t index) { 903 bool RenderTextHarfBuzz::IsValidCursorIndex(size_t index) {
840 if (index == 0 || index == text().length()) 904 if (index == 0 || index == text().length())
841 return true; 905 return true;
842 if (!IsValidLogicalIndex(index)) 906 if (!IsValidLogicalIndex(index))
843 return false; 907 return false;
844 EnsureLayout(); 908 base::i18n::BreakIterator* grapheme_iterator = GetGraphemeIterator();
845 return !grapheme_iterator_ || grapheme_iterator_->IsGraphemeBoundary(index); 909 return !grapheme_iterator || grapheme_iterator->IsGraphemeBoundary(index);
846 } 910 }
847 911
848 void RenderTextHarfBuzz::ResetLayout() { 912 void RenderTextHarfBuzz::OnLayoutTextAttributeChanged(bool text_changed) {
849 needs_layout_ = true; 913 update_layout_run_list_ = true;
914 OnDisplayTextAttributeChanged();
915 }
916
917 void RenderTextHarfBuzz::OnDisplayTextAttributeChanged() {
918 update_display_text_ = true;
919 update_grapheme_iterator_ = true;
850 } 920 }
851 921
852 void RenderTextHarfBuzz::EnsureLayout() { 922 void RenderTextHarfBuzz::EnsureLayout() {
853 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 923 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
854 tracked_objects::ScopedTracker tracking_profile( 924 tracked_objects::ScopedTracker tracking_profile(
855 FROM_HERE_WITH_EXPLICIT_FUNCTION( 925 FROM_HERE_WITH_EXPLICIT_FUNCTION(
856 "431326 RenderTextHarfBuzz::EnsureLayout")); 926 "431326 RenderTextHarfBuzz::EnsureLayout"));
857 927
858 if (needs_layout_) { 928 EnsureLayoutRunList();
859 runs_.clear();
860 grapheme_iterator_.reset();
861 929
862 if (!GetLayoutText().empty()) { 930 if (update_display_run_list_) {
931 DCHECK(text_elided());
932 const base::string16& display_text = GetDisplayText();
933 display_run_list_.reset(new internal::TextRunList);
934
935 if (!display_text.empty()) {
936 TRACE_EVENT0("ui", "RenderTextHarfBuzz:EnsureLayout1");
937
863 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is 938 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
864 // fixed. 939 // fixed.
865 tracked_objects::ScopedTracker tracking_profile1( 940 tracked_objects::ScopedTracker tracking_profile1(
866 FROM_HERE_WITH_EXPLICIT_FUNCTION( 941 FROM_HERE_WITH_EXPLICIT_FUNCTION(
867 "431326 RenderTextHarfBuzz::EnsureLayout1")); 942 "431326 RenderTextHarfBuzz::EnsureLayout1"));
868 943 ItemizeTextToRuns(display_text, display_run_list_.get());
869 grapheme_iterator_.reset(new base::i18n::BreakIterator(GetLayoutText(),
870 base::i18n::BreakIterator::BREAK_CHARACTER));
871 if (!grapheme_iterator_->Init())
872 grapheme_iterator_.reset();
873 944
874 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is 945 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
875 // fixed. 946 // fixed.
876 tracked_objects::ScopedTracker tracking_profile11( 947 tracked_objects::ScopedTracker tracking_profile2(
877 FROM_HERE_WITH_EXPLICIT_FUNCTION(
878 "431326 RenderTextHarfBuzz::EnsureLayout11"));
879
880 ItemizeText();
881
882 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
883 // fixed.
884 tracked_objects::ScopedTracker tracking_profile12(
885 FROM_HERE_WITH_EXPLICIT_FUNCTION( 948 FROM_HERE_WITH_EXPLICIT_FUNCTION(
886 "431326 RenderTextHarfBuzz::EnsureLayout12")); 949 "431326 RenderTextHarfBuzz::EnsureLayout12"));
887 950 ShapeRunList(display_text, display_run_list_.get());
888 for (size_t i = 0; i < runs_.size(); ++i)
889 ShapeRun(runs_[i]);
890
891 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
892 // fixed.
893 tracked_objects::ScopedTracker tracking_profile13(
894 FROM_HERE_WITH_EXPLICIT_FUNCTION(
895 "431326 RenderTextHarfBuzz::EnsureLayout13"));
896
897 // Precalculate run width information.
898 float preceding_run_widths = 0.0f;
899 for (size_t i = 0; i < runs_.size(); ++i) {
900 internal::TextRunHarfBuzz* run = runs_[visual_to_logical_[i]];
901 run->preceding_run_widths = preceding_run_widths;
902 preceding_run_widths += run->width;
903 }
904 } 951 }
952 update_display_run_list_ = false;
905 953
906 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is 954 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
907 // fixed. 955 // fixed.
908 tracked_objects::ScopedTracker tracking_profile14( 956 tracked_objects::ScopedTracker tracking_profile14(
909 FROM_HERE_WITH_EXPLICIT_FUNCTION( 957 FROM_HERE_WITH_EXPLICIT_FUNCTION(
910 "431326 RenderTextHarfBuzz::EnsureLayout14")); 958 "431326 RenderTextHarfBuzz::EnsureLayout14"));
911
912 needs_layout_ = false;
913 std::vector<internal::Line> empty_lines; 959 std::vector<internal::Line> empty_lines;
914 set_lines(&empty_lines); 960 set_lines(&empty_lines);
915 } 961 }
916 962
917 if (lines().empty()) { 963 if (lines().empty()) {
918 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 964 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
919 tracked_objects::ScopedTracker tracking_profile2( 965 tracked_objects::ScopedTracker tracking_profile2(
920 FROM_HERE_WITH_EXPLICIT_FUNCTION( 966 FROM_HERE_WITH_EXPLICIT_FUNCTION(
921 "431326 RenderTextHarfBuzz::EnsureLayout2")); 967 "431326 RenderTextHarfBuzz::EnsureLayout2"));
922 968
969 internal::TextRunList* run_list = GetRunList();
923 HarfBuzzLineBreaker line_breaker( 970 HarfBuzzLineBreaker line_breaker(
924 display_rect().width(), font_list().GetBaseline(), 971 display_rect().width(), font_list().GetBaseline(),
925 std::max(font_list().GetHeight(), min_line_height()), multiline(), 972 std::max(font_list().GetHeight(), min_line_height()), multiline(),
926 GetLayoutText(), multiline() ? &GetLineBreaks() : nullptr, runs_); 973 GetDisplayText(), multiline() ? &GetLineBreaks() : nullptr,
974 run_list->runs());
927 975
928 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 976 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
929 tracked_objects::ScopedTracker tracking_profile3( 977 tracked_objects::ScopedTracker tracking_profile3(
930 FROM_HERE_WITH_EXPLICIT_FUNCTION( 978 FROM_HERE_WITH_EXPLICIT_FUNCTION(
931 "431326 RenderTextHarfBuzz::EnsureLayout3")); 979 "431326 RenderTextHarfBuzz::EnsureLayout3"));
932 980
933 for (size_t i = 0; i < runs_.size(); ++i) 981 for (size_t i = 0; i < run_list->size(); ++i)
934 line_breaker.AddRun(visual_to_logical_[i]); 982 line_breaker.AddRun(run_list->visual_to_logical(i));
983
935 std::vector<internal::Line> lines; 984 std::vector<internal::Line> lines;
936 line_breaker.Finalize(&lines, &total_size_); 985 line_breaker.Finalize(&lines, &total_size_);
937 set_lines(&lines); 986 set_lines(&lines);
938 } 987 }
939 } 988 }
940 989
941 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { 990 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) {
942 DCHECK(!needs_layout_); 991 DCHECK(!update_layout_run_list_);
992 DCHECK(!update_display_run_list_);
993 DCHECK(!update_display_text_);
943 if (lines().empty()) 994 if (lines().empty())
944 return; 995 return;
945 996
946 internal::SkiaTextRenderer renderer(canvas); 997 internal::SkiaTextRenderer renderer(canvas);
947 ApplyFadeEffects(&renderer); 998 ApplyFadeEffects(&renderer);
948 ApplyTextShadows(&renderer); 999 ApplyTextShadows(&renderer);
949 ApplyCompositionAndSelectionStyles(); 1000 ApplyCompositionAndSelectionStyles();
950 1001
1002 internal::TextRunList* run_list = GetRunList();
951 for (size_t i = 0; i < lines().size(); ++i) { 1003 for (size_t i = 0; i < lines().size(); ++i) {
952 const internal::Line& line = lines()[i]; 1004 const internal::Line& line = lines()[i];
953 const Vector2d origin = GetLineOffset(i) + Vector2d(0, line.baseline); 1005 const Vector2d origin = GetLineOffset(i) + Vector2d(0, line.baseline);
954 SkScalar preceding_segment_widths = 0; 1006 SkScalar preceding_segment_widths = 0;
955 for (const internal::LineSegment& segment : line.segments) { 1007 for (const internal::LineSegment& segment : line.segments) {
956 const internal::TextRunHarfBuzz& run = *runs_[segment.run]; 1008 const internal::TextRunHarfBuzz& run = *run_list->runs()[segment.run];
957 renderer.SetTypeface(run.skia_face.get()); 1009 renderer.SetTypeface(run.skia_face.get());
958 renderer.SetTextSize(SkIntToScalar(run.font_size)); 1010 renderer.SetTextSize(SkIntToScalar(run.font_size));
959 renderer.SetFontRenderParams(run.render_params, 1011 renderer.SetFontRenderParams(run.render_params,
960 background_is_transparent()); 1012 background_is_transparent());
961 Range glyphs_range = run.CharRangeToGlyphRange(segment.char_range); 1013 Range glyphs_range = run.CharRangeToGlyphRange(segment.char_range);
962 scoped_ptr<SkPoint[]> positions(new SkPoint[glyphs_range.length()]); 1014 scoped_ptr<SkPoint[]> positions(new SkPoint[glyphs_range.length()]);
963 SkScalar offset_x = 1015 SkScalar offset_x =
964 preceding_segment_widths - run.positions[glyphs_range.start()].x(); 1016 preceding_segment_widths - run.positions[glyphs_range.start()].x();
965 for (size_t j = 0; j < glyphs_range.length(); ++j) { 1017 for (size_t j = 0; j < glyphs_range.length(); ++j) {
966 positions[j] = run.positions[(glyphs_range.is_reversed()) ? 1018 positions[j] = run.positions[(glyphs_range.is_reversed()) ?
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 preceding_segment_widths += SkFloatToScalar(segment.width); 1054 preceding_segment_widths += SkFloatToScalar(segment.width);
1003 } 1055 }
1004 } 1056 }
1005 1057
1006 renderer.EndDiagonalStrike(); 1058 renderer.EndDiagonalStrike();
1007 1059
1008 UndoCompositionAndSelectionStyles(); 1060 UndoCompositionAndSelectionStyles();
1009 } 1061 }
1010 1062
1011 size_t RenderTextHarfBuzz::GetRunContainingCaret( 1063 size_t RenderTextHarfBuzz::GetRunContainingCaret(
1012 const SelectionModel& caret) const { 1064 const SelectionModel& caret) {
1013 DCHECK(!needs_layout_); 1065 DCHECK(!update_display_run_list_);
1014 size_t layout_position = TextIndexToLayoutIndex(caret.caret_pos()); 1066 size_t layout_position = TextIndexToDisplayIndex(caret.caret_pos());
1015 LogicalCursorDirection affinity = caret.caret_affinity(); 1067 LogicalCursorDirection affinity = caret.caret_affinity();
1016 for (size_t run = 0; run < runs_.size(); ++run) { 1068 internal::TextRunList* run_list = GetRunList();
1017 if (RangeContainsCaret(runs_[run]->range, layout_position, affinity)) 1069 for (size_t i = 0; i < run_list->size(); ++i) {
1018 return run; 1070 internal::TextRunHarfBuzz* run = run_list->runs()[i];
1071 if (RangeContainsCaret(run->range, layout_position, affinity))
1072 return i;
1019 } 1073 }
1020 return runs_.size(); 1074 return run_list->size();
1021 } 1075 }
1022 1076
1023 size_t RenderTextHarfBuzz::GetRunContainingXCoord(float x, 1077 size_t RenderTextHarfBuzz::GetRunContainingXCoord(float x,
1024 float* offset) const { 1078 float* offset) const {
1025 DCHECK(!needs_layout_); 1079 DCHECK(!update_display_run_list_);
1080 const internal::TextRunList* run_list = GetRunList();
1026 if (x < 0) 1081 if (x < 0)
1027 return runs_.size(); 1082 return run_list->size();
1028 // Find the text run containing the argument point (assumed already offset). 1083 // Find the text run containing the argument point (assumed already offset).
1029 float current_x = 0; 1084 float current_x = 0;
1030 for (size_t i = 0; i < runs_.size(); ++i) { 1085 for (size_t i = 0; i < run_list->size(); ++i) {
1031 size_t run = visual_to_logical_[i]; 1086 size_t run = run_list->visual_to_logical(i);
1032 current_x += runs_[run]->width; 1087 current_x += run_list->runs()[run]->width;
1033 if (x < current_x) { 1088 if (x < current_x) {
1034 *offset = x - (current_x - runs_[run]->width); 1089 *offset = x - (current_x - run_list->runs()[run]->width);
1035 return run; 1090 return run;
1036 } 1091 }
1037 } 1092 }
1038 return runs_.size(); 1093 return run_list->size();
1039 } 1094 }
1040 1095
1041 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( 1096 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun(
1042 const internal::TextRunHarfBuzz* run) { 1097 const internal::TextRunHarfBuzz* run) {
1043 size_t position = LayoutIndexToTextIndex(run->range.start()); 1098 size_t position = DisplayIndexToTextIndex(run->range.start());
1044 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); 1099 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD);
1045 return SelectionModel(position, CURSOR_BACKWARD); 1100 return SelectionModel(position, CURSOR_BACKWARD);
1046 } 1101 }
1047 1102
1048 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( 1103 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun(
1049 const internal::TextRunHarfBuzz* run) { 1104 const internal::TextRunHarfBuzz* run) {
1050 size_t position = LayoutIndexToTextIndex(run->range.end()); 1105 size_t position = DisplayIndexToTextIndex(run->range.end());
1051 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD); 1106 position = IndexOfAdjacentGrapheme(position, CURSOR_BACKWARD);
1052 return SelectionModel(position, CURSOR_FORWARD); 1107 return SelectionModel(position, CURSOR_FORWARD);
1053 } 1108 }
1054 1109
1055 void RenderTextHarfBuzz::ItemizeText() { 1110 void RenderTextHarfBuzz::ItemizeTextToRuns(
1056 const base::string16& text = GetLayoutText(); 1111 const base::string16& text,
1057 const bool is_text_rtl = GetTextDirection() == base::i18n::RIGHT_TO_LEFT; 1112 internal::TextRunList* run_list_out) {
1113 const bool is_text_rtl = GetTextDirection(text) == base::i18n::RIGHT_TO_LEFT;
1058 DCHECK_NE(0U, text.length()); 1114 DCHECK_NE(0U, text.length());
1059 1115
1060 // If ICU fails to itemize the text, we create a run that spans the entire 1116 // If ICU fails to itemize the text, we create a run that spans the entire
1061 // text. This is needed because leaving the runs set empty causes some clients 1117 // text. This is needed because leaving the runs set empty causes some clients
1062 // to misbehave since they expect non-zero text metrics from a non-empty text. 1118 // to misbehave since they expect non-zero text metrics from a non-empty text.
1063 base::i18n::BiDiLineIterator bidi_iterator; 1119 base::i18n::BiDiLineIterator bidi_iterator;
1064 if (!bidi_iterator.Open(text, is_text_rtl, false)) { 1120 if (!bidi_iterator.Open(text, is_text_rtl, false)) {
1065 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; 1121 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz;
1066 run->range = Range(0, text.length()); 1122 run->range = Range(0, text.length());
1067 runs_.push_back(run); 1123 run_list_out->add(run);
1068 visual_to_logical_ = logical_to_visual_ = std::vector<int32_t>(1, 0); 1124 run_list_out->InitIndexMap();
1069 return; 1125 return;
1070 } 1126 }
1071 1127
1072 // Temporarily apply composition underlines and selection colors. 1128 // Temporarily apply composition underlines and selection colors.
1073 ApplyCompositionAndSelectionStyles(); 1129 ApplyCompositionAndSelectionStyles();
1074 1130
1075 // Build the list of runs from the script items and ranged styles. Use an 1131 // Build the list of runs from the script items and ranged styles. Use an
1076 // empty color BreakList to avoid breaking runs at color boundaries. 1132 // empty color BreakList to avoid breaking runs at color boundaries.
1077 BreakList<SkColor> empty_colors; 1133 BreakList<SkColor> empty_colors;
1078 empty_colors.SetMax(text.length()); 1134 empty_colors.SetMax(text.length());
1079 internal::StyleIterator style(empty_colors, styles()); 1135 internal::StyleIterator style(empty_colors, styles());
1080 1136
1081 for (size_t run_break = 0; run_break < text.length();) { 1137 for (size_t run_break = 0; run_break < text.length();) {
1082 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; 1138 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz;
1083 run->range.set_start(run_break); 1139 run->range.set_start(run_break);
1084 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | 1140 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) |
1085 (style.style(ITALIC) ? Font::ITALIC : 0); 1141 (style.style(ITALIC) ? Font::ITALIC : 0);
1086 run->strike = style.style(STRIKE); 1142 run->strike = style.style(STRIKE);
1087 run->diagonal_strike = style.style(DIAGONAL_STRIKE); 1143 run->diagonal_strike = style.style(DIAGONAL_STRIKE);
1088 run->underline = style.style(UNDERLINE); 1144 run->underline = style.style(UNDERLINE);
1089
1090 int32 script_item_break = 0; 1145 int32 script_item_break = 0;
1091 bidi_iterator.GetLogicalRun(run_break, &script_item_break, &run->level); 1146 bidi_iterator.GetLogicalRun(run_break, &script_item_break, &run->level);
1092 // Odd BiDi embedding levels correspond to RTL runs. 1147 // Odd BiDi embedding levels correspond to RTL runs.
1093 run->is_rtl = (run->level % 2) == 1; 1148 run->is_rtl = (run->level % 2) == 1;
1094 // Find the length and script of this script run. 1149 // Find the length and script of this script run.
1095 script_item_break = ScriptInterval(text, run_break, 1150 script_item_break = ScriptInterval(text, run_break,
1096 script_item_break - run_break, &run->script) + run_break; 1151 script_item_break - run_break, &run->script) + run_break;
1097 1152
1098 // Find the next break and advance the iterators as needed. 1153 // Find the next break and advance the iterators as needed.
1099 run_break = std::min(static_cast<size_t>(script_item_break), 1154 run_break = std::min(
1100 TextIndexToLayoutIndex(style.GetRange().end())); 1155 static_cast<size_t>(script_item_break),
1156 TextIndexToGivenTextIndex(text, style.GetRange().end()));
1101 1157
1102 // Break runs at certain characters that need to be rendered separately to 1158 // Break runs at certain characters that need to be rendered separately to
1103 // prevent either an unusual character from forcing a fallback font on the 1159 // prevent either an unusual character from forcing a fallback font on the
1104 // entire run, or brackets from being affected by a fallback font. 1160 // entire run, or brackets from being affected by a fallback font.
1105 // http://crbug.com/278913, http://crbug.com/396776 1161 // http://crbug.com/278913, http://crbug.com/396776
1106 if (run_break > run->range.start()) 1162 if (run_break > run->range.start())
1107 run_break = FindRunBreakingCharacter(text, run->range.start(), run_break); 1163 run_break = FindRunBreakingCharacter(text, run->range.start(), run_break);
1108 1164
1109 DCHECK(IsValidCodePointIndex(text, run_break)); 1165 DCHECK(IsValidCodePointIndex(text, run_break));
1110 style.UpdatePosition(LayoutIndexToTextIndex(run_break)); 1166 style.UpdatePosition(DisplayIndexToTextIndex(run_break));
1111 run->range.set_end(run_break); 1167 run->range.set_end(run_break);
1112 1168
1113 runs_.push_back(run); 1169 run_list_out->add(run);
1114 } 1170 }
1115 1171
1116 // Undo the temporarily applied composition underlines and selection colors. 1172 // Undo the temporarily applied composition underlines and selection colors.
1117 UndoCompositionAndSelectionStyles(); 1173 UndoCompositionAndSelectionStyles();
1118 1174
1119 const size_t num_runs = runs_.size(); 1175 run_list_out->InitIndexMap();
1120 std::vector<UBiDiLevel> levels(num_runs);
1121 for (size_t i = 0; i < num_runs; ++i)
1122 levels[i] = runs_[i]->level;
1123 visual_to_logical_.resize(num_runs);
1124 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]);
1125 logical_to_visual_.resize(num_runs);
1126 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]);
1127 } 1176 }
1128 1177
1129 bool RenderTextHarfBuzz::CompareFamily( 1178 bool RenderTextHarfBuzz::CompareFamily(
1130 internal::TextRunHarfBuzz* run, 1179 const base::string16& text,
1131 const std::string& family, 1180 const std::string& family,
1132 const gfx::FontRenderParams& render_params, 1181 const gfx::FontRenderParams& render_params,
1182 internal::TextRunHarfBuzz* run,
1133 std::string* best_family, 1183 std::string* best_family,
1134 gfx::FontRenderParams* best_render_params, 1184 gfx::FontRenderParams* best_render_params,
1135 size_t* best_missing_glyphs) { 1185 size_t* best_missing_glyphs) {
1136 if (!ShapeRunWithFont(run, family, render_params)) 1186 if (!ShapeRunWithFont(text, family, render_params, run))
1137 return false; 1187 return false;
1138 1188
1139 const size_t missing_glyphs = run->CountMissingGlyphs(); 1189 const size_t missing_glyphs = run->CountMissingGlyphs();
1140 if (missing_glyphs < *best_missing_glyphs) { 1190 if (missing_glyphs < *best_missing_glyphs) {
1141 *best_family = family; 1191 *best_family = family;
1142 *best_render_params = render_params; 1192 *best_render_params = render_params;
1143 *best_missing_glyphs = missing_glyphs; 1193 *best_missing_glyphs = missing_glyphs;
1144 } 1194 }
1145 return missing_glyphs == 0; 1195 return missing_glyphs == 0;
1146 } 1196 }
1147 1197
1148 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { 1198 void RenderTextHarfBuzz::ShapeRunList(const base::string16& text,
1199 internal::TextRunList* run_list) {
1200 for (auto* run : run_list->runs())
1201 ShapeRun(text, run);
1202 run_list->ComputePrecedingRunWidths();
1203 }
1204
1205 void RenderTextHarfBuzz::ShapeRun(const base::string16& text,
1206 internal::TextRunHarfBuzz* run) {
1149 const Font& primary_font = font_list().GetPrimaryFont(); 1207 const Font& primary_font = font_list().GetPrimaryFont();
1150 const std::string primary_family = primary_font.GetFontName(); 1208 const std::string primary_family = primary_font.GetFontName();
1151 run->font_size = primary_font.GetFontSize(); 1209 run->font_size = primary_font.GetFontSize();
1152 1210
1153 std::string best_family; 1211 std::string best_family;
1154 FontRenderParams best_render_params; 1212 FontRenderParams best_render_params;
1155 size_t best_missing_glyphs = std::numeric_limits<size_t>::max(); 1213 size_t best_missing_glyphs = std::numeric_limits<size_t>::max();
1156 1214
1157 for (const Font& font : font_list().GetFonts()) { 1215 for (const Font& font : font_list().GetFonts()) {
1158 if (CompareFamily(run, font.GetFontName(), font.GetFontRenderParams(), 1216 if (CompareFamily(text, font.GetFontName(), font.GetFontRenderParams(),
1159 &best_family, &best_render_params, &best_missing_glyphs)) 1217 run, &best_family, &best_render_params,
1218 &best_missing_glyphs))
1160 return; 1219 return;
1161 } 1220 }
1162 1221
1163 #if defined(OS_WIN) 1222 #if defined(OS_WIN)
1164 Font uniscribe_font; 1223 Font uniscribe_font;
1165 std::string uniscribe_family; 1224 std::string uniscribe_family;
1166 const base::char16* run_text = &(GetLayoutText()[run->range.start()]); 1225 const base::char16* run_text = &(text[run->range.start()]);
1167 if (GetUniscribeFallbackFont(primary_font, run_text, run->range.length(), 1226 if (GetUniscribeFallbackFont(primary_font, run_text, run->range.length(),
1168 &uniscribe_font)) { 1227 &uniscribe_font)) {
1169 uniscribe_family = uniscribe_font.GetFontName(); 1228 uniscribe_family = uniscribe_font.GetFontName();
1170 if (CompareFamily(run, uniscribe_family, 1229 if (CompareFamily(text, uniscribe_family,
1171 uniscribe_font.GetFontRenderParams(), 1230 uniscribe_font.GetFontRenderParams(), run,
1172 &best_family, &best_render_params, &best_missing_glyphs)) 1231 &best_family, &best_render_params, &best_missing_glyphs))
1173 return; 1232 return;
1174 } 1233 }
1175 #endif 1234 #endif
1176 1235
1177 std::vector<std::string> fallback_families = 1236 std::vector<std::string> fallback_families =
1178 GetFallbackFontFamilies(primary_family); 1237 GetFallbackFontFamilies(primary_family);
1179 1238
1180 #if defined(OS_WIN) 1239 #if defined(OS_WIN)
1181 // Append fonts in the fallback list of the Uniscribe font. 1240 // Append fonts in the fallback list of the Uniscribe font.
(...skipping 11 matching lines...) Expand all
1193 continue; 1252 continue;
1194 #if defined(OS_WIN) 1253 #if defined(OS_WIN)
1195 if (family == uniscribe_family) 1254 if (family == uniscribe_family)
1196 continue; 1255 continue;
1197 #endif 1256 #endif
1198 FontRenderParamsQuery query(false); 1257 FontRenderParamsQuery query(false);
1199 query.families.push_back(family); 1258 query.families.push_back(family);
1200 query.pixel_size = run->font_size; 1259 query.pixel_size = run->font_size;
1201 query.style = run->font_style; 1260 query.style = run->font_style;
1202 FontRenderParams fallback_render_params = GetFontRenderParams(query, NULL); 1261 FontRenderParams fallback_render_params = GetFontRenderParams(query, NULL);
1203 if (CompareFamily(run, family, fallback_render_params, &best_family, 1262 if (CompareFamily(text, family, fallback_render_params, run, &best_family,
1204 &best_render_params, &best_missing_glyphs)) 1263 &best_render_params, &best_missing_glyphs))
1205 return; 1264 return;
1206 } 1265 }
1207 1266
1208 if (!best_family.empty() && 1267 if (!best_family.empty() &&
1209 (best_family == run->family || 1268 (best_family == run->family ||
1210 ShapeRunWithFont(run, best_family, best_render_params))) 1269 ShapeRunWithFont(text, best_family, best_render_params, run)))
1211 return; 1270 return;
1212 1271
1213 run->glyph_count = 0; 1272 run->glyph_count = 0;
1214 run->width = 0.0f; 1273 run->width = 0.0f;
1215 } 1274 }
1216 1275
1217 bool RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, 1276 bool RenderTextHarfBuzz::ShapeRunWithFont(const base::string16& text,
1218 const std::string& font_family, 1277 const std::string& font_family,
1219 const FontRenderParams& params) { 1278 const FontRenderParams& params,
1279 internal::TextRunHarfBuzz* run) {
1220 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 1280 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
1221 tracked_objects::ScopedTracker tracking_profile0( 1281 tracked_objects::ScopedTracker tracking_profile0(
1222 FROM_HERE_WITH_EXPLICIT_FUNCTION( 1282 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1223 "431326 RenderTextHarfBuzz::ShapeRunWithFont0")); 1283 "431326 RenderTextHarfBuzz::ShapeRunWithFont0"));
1224 1284
1225 const base::string16& text = GetLayoutText();
1226 skia::RefPtr<SkTypeface> skia_face = 1285 skia::RefPtr<SkTypeface> skia_face =
1227 internal::CreateSkiaTypeface(font_family, run->font_style); 1286 internal::CreateSkiaTypeface(font_family, run->font_style);
1228 if (skia_face == NULL) 1287 if (skia_face == NULL)
1229 return false; 1288 return false;
1230 run->skia_face = skia_face; 1289 run->skia_face = skia_face;
1231 run->family = font_family; 1290 run->family = font_family;
1232 run->render_params = params; 1291 run->render_params = params;
1233 1292
1234 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed. 1293 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is fixed.
1235 tracked_objects::ScopedTracker tracking_profile01( 1294 tracked_objects::ScopedTracker tracking_profile01(
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 // Round run widths if subpixel positioning is off to match native behavior. 1382 // Round run widths if subpixel positioning is off to match native behavior.
1324 if (!run->render_params.subpixel_positioning) 1383 if (!run->render_params.subpixel_positioning)
1325 run->width = std::floor(run->width + 0.5f); 1384 run->width = std::floor(run->width + 0.5f);
1326 } 1385 }
1327 1386
1328 hb_buffer_destroy(buffer); 1387 hb_buffer_destroy(buffer);
1329 hb_font_destroy(harfbuzz_font); 1388 hb_font_destroy(harfbuzz_font);
1330 return true; 1389 return true;
1331 } 1390 }
1332 1391
1392 void RenderTextHarfBuzz::EnsureLayoutRunList() {
1393 if (update_layout_run_list_) {
1394 layout_run_list_.Reset();
1395
1396 const base::string16& text = layout_text();
1397 if (!text.empty()) {
1398 TRACE_EVENT0("ui", "RenderTextHarfBuzz:EnsureLayoutRunList");
1399 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
1400 // fixed.
1401 tracked_objects::ScopedTracker tracking_profile1(
1402 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1403 "431326 RenderTextHarfBuzz::EnsureLayout1"));
1404 ItemizeTextToRuns(text, &layout_run_list_);
1405
1406 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
1407 // fixed.
1408 tracked_objects::ScopedTracker tracking_profile2(
1409 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1410 "431326 RenderTextHarfBuzz::EnsureLayout12"));
1411 ShapeRunList(text, &layout_run_list_);
1412 }
1413
1414 // TODO(vadimt): Remove ScopedTracker below once crbug.com/431326 is
1415 // fixed.
1416 tracked_objects::ScopedTracker tracking_profile14(
1417 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1418 "431326 RenderTextHarfBuzz::EnsureLayout14"));
1419
1420 std::vector<internal::Line> empty_lines;
1421 set_lines(&empty_lines);
1422 display_run_list_.reset();
1423 update_display_text_ = true;
1424 update_layout_run_list_ = false;
1425 }
1426 if (update_display_text_) {
1427 UpdateDisplayText(layout_run_list_.width());
1428 update_display_text_ = false;
1429 update_display_run_list_ = text_elided();
1430 }
1431 }
1432
1433 base::i18n::BreakIterator* RenderTextHarfBuzz::GetGraphemeIterator() {
1434 if (update_grapheme_iterator_) {
1435 update_grapheme_iterator_ = false;
1436 grapheme_iterator_.reset(new base::i18n::BreakIterator(
1437 GetDisplayText(),
1438 base::i18n::BreakIterator::BREAK_CHARACTER));
1439 if (!grapheme_iterator_->Init())
1440 grapheme_iterator_.reset();
1441 }
1442 return grapheme_iterator_.get();
1443 }
1444
1445 size_t RenderTextHarfBuzz::TextIndexToGivenTextIndex(
1446 const base::string16& given_text,
1447 size_t index) {
1448 DCHECK(given_text == layout_text() || given_text == display_text());
1449 DCHECK_LE(index, text().length());
1450 ptrdiff_t i = obscured() ? UTF16IndexToOffset(text(), 0, index) : index;
1451 CHECK_GE(i, 0);
1452 // Clamp indices to the length of the given layout or display text.
1453 return std::min<size_t>(given_text.length(), i);
1454 }
1455
1456 internal::TextRunList* RenderTextHarfBuzz::GetRunList() {
1457 DCHECK(!update_layout_run_list_);
1458 DCHECK(!update_display_run_list_);
1459 return text_elided() ? display_run_list_.get() : &layout_run_list_;
1460 }
1461
1462 const internal::TextRunList* RenderTextHarfBuzz::GetRunList() const {
1463 RenderTextHarfBuzz* that = const_cast<RenderTextHarfBuzz*>(this);
msw 2015/02/14 00:11:26 nit: inline |that| below for: return const_cast<
oshima 2015/02/14 00:40:51 Done.
1464 return that->GetRunList();
1465 }
1466
1333 } // namespace gfx 1467 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698