OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 range = range.Intersect(styles_[i].GetRange(style_[i])); | 294 range = range.Intersect(styles_[i].GetRange(style_[i])); |
295 return range; | 295 return range; |
296 } | 296 } |
297 | 297 |
298 void StyleIterator::UpdatePosition(size_t position) { | 298 void StyleIterator::UpdatePosition(size_t position) { |
299 color_ = colors_.GetBreak(position); | 299 color_ = colors_.GetBreak(position); |
300 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) | 300 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
301 style_[i] = styles_[i].GetBreak(position); | 301 style_[i] = styles_[i].GetBreak(position); |
302 } | 302 } |
303 | 303 |
304 Line::Line() : preceding_heights(0), baseline(0) {} | |
305 | |
304 } // namespace internal | 306 } // namespace internal |
305 | 307 |
306 RenderText::~RenderText() { | 308 RenderText::~RenderText() { |
307 } | 309 } |
308 | 310 |
309 void RenderText::SetText(const base::string16& text) { | 311 void RenderText::SetText(const base::string16& text) { |
310 DCHECK(!composition_range_.IsValid()); | 312 DCHECK(!composition_range_.IsValid()); |
313 if (text_ == text) | |
314 return; | |
311 text_ = text; | 315 text_ = text; |
312 | 316 |
313 // Adjust ranged styles and colors to accommodate a new text length. | 317 // Adjust ranged styles and colors to accommodate a new text length. |
314 const size_t text_length = text_.length(); | 318 const size_t text_length = text_.length(); |
315 colors_.SetMax(text_length); | 319 colors_.SetMax(text_length); |
316 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) | 320 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
317 styles_[style].SetMax(text_length); | 321 styles_[style].SetMax(text_length); |
318 cached_bounds_and_offset_valid_ = false; | 322 cached_bounds_and_offset_valid_ = false; |
319 | 323 |
320 // Reset selection model. SetText should always followed by SetSelectionModel | 324 // Reset selection model. SetText should always followed by SetSelectionModel |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 void RenderText::SetObscuredRevealIndex(int index) { | 391 void RenderText::SetObscuredRevealIndex(int index) { |
388 if (obscured_reveal_index_ == index) | 392 if (obscured_reveal_index_ == index) |
389 return; | 393 return; |
390 | 394 |
391 obscured_reveal_index_ = index; | 395 obscured_reveal_index_ = index; |
392 cached_bounds_and_offset_valid_ = false; | 396 cached_bounds_and_offset_valid_ = false; |
393 UpdateLayoutText(); | 397 UpdateLayoutText(); |
394 ResetLayout(); | 398 ResetLayout(); |
395 } | 399 } |
396 | 400 |
401 void RenderText::SetMultiline(bool multiline) { | |
402 if (multiline != multiline_) { | |
403 multiline_ = multiline; | |
404 cached_bounds_and_offset_valid_ = false; | |
405 ResetLayout(); | |
406 } | |
407 } | |
408 | |
397 void RenderText::SetDisplayRect(const Rect& r) { | 409 void RenderText::SetDisplayRect(const Rect& r) { |
398 display_rect_ = r; | 410 display_rect_ = r; |
399 cached_bounds_and_offset_valid_ = false; | 411 cached_bounds_and_offset_valid_ = false; |
412 lines_.clear(); | |
400 } | 413 } |
401 | 414 |
402 void RenderText::SetCursorPosition(size_t position) { | 415 void RenderText::SetCursorPosition(size_t position) { |
403 MoveCursorTo(position, false); | 416 MoveCursorTo(position, false); |
404 } | 417 } |
405 | 418 |
406 void RenderText::MoveCursor(BreakType break_type, | 419 void RenderText::MoveCursor(BreakType break_type, |
407 VisualCursorDirection direction, | 420 VisualCursorDirection direction, |
408 bool select) { | 421 bool select) { |
409 SelectionModel position(cursor_position(), selection_model_.caret_affinity()); | 422 SelectionModel position(cursor_position(), selection_model_.caret_affinity()); |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
768 selection_color_(kDefaultColor), | 781 selection_color_(kDefaultColor), |
769 selection_background_focused_color_(kDefaultSelectionBackgroundColor), | 782 selection_background_focused_color_(kDefaultSelectionBackgroundColor), |
770 focused_(false), | 783 focused_(false), |
771 composition_range_(ui::Range::InvalidRange()), | 784 composition_range_(ui::Range::InvalidRange()), |
772 colors_(kDefaultColor), | 785 colors_(kDefaultColor), |
773 styles_(NUM_TEXT_STYLES), | 786 styles_(NUM_TEXT_STYLES), |
774 composition_and_selection_styles_applied_(false), | 787 composition_and_selection_styles_applied_(false), |
775 obscured_(false), | 788 obscured_(false), |
776 obscured_reveal_index_(-1), | 789 obscured_reveal_index_(-1), |
777 truncate_length_(0), | 790 truncate_length_(0), |
791 multiline_(false), | |
778 fade_head_(false), | 792 fade_head_(false), |
779 fade_tail_(false), | 793 fade_tail_(false), |
780 background_is_transparent_(false), | 794 background_is_transparent_(false), |
781 clip_to_display_rect_(true), | 795 clip_to_display_rect_(true), |
782 cached_bounds_and_offset_valid_(false) { | 796 cached_bounds_and_offset_valid_(false) { |
783 } | 797 } |
784 | 798 |
785 const Vector2d& RenderText::GetUpdatedDisplayOffset() { | 799 const Vector2d& RenderText::GetUpdatedDisplayOffset() { |
786 UpdateCachedBoundsAndOffset(); | 800 UpdateCachedBoundsAndOffset(); |
787 return display_offset_; | 801 return display_offset_; |
(...skipping 23 matching lines...) Expand all Loading... | |
811 void RenderText::SetSelectionModel(const SelectionModel& model) { | 825 void RenderText::SetSelectionModel(const SelectionModel& model) { |
812 DCHECK_LE(model.selection().GetMax(), text().length()); | 826 DCHECK_LE(model.selection().GetMax(), text().length()); |
813 selection_model_ = model; | 827 selection_model_ = model; |
814 cached_bounds_and_offset_valid_ = false; | 828 cached_bounds_and_offset_valid_ = false; |
815 } | 829 } |
816 | 830 |
817 const base::string16& RenderText::GetLayoutText() const { | 831 const base::string16& RenderText::GetLayoutText() const { |
818 return layout_text_.empty() ? text_ : layout_text_; | 832 return layout_text_.empty() ? text_ : layout_text_; |
819 } | 833 } |
820 | 834 |
835 const BreakList<size_t>& RenderText::GetLineBreaks() { | |
836 const string16& layout_text = GetLayoutText(); | |
837 const size_t text_length = layout_text.length(); | |
838 if (line_breaks_.max() == 0) { | |
Alexei Svitkine (slow)
2013/08/26 21:34:41
Nit: Make this an early return and put the above t
ckocagil
2013/08/28 18:01:45
Done.
| |
839 line_breaks_.SetValue(0); | |
840 line_breaks_.SetMax(text_length); | |
841 base::i18n::BreakIterator iter(layout_text, | |
842 base::i18n::BreakIterator::BREAK_LINE); | |
843 bool success = iter.Init(); | |
844 DCHECK(success); | |
845 if (success) { | |
846 do { | |
847 line_breaks_.ApplyValue(iter.pos(), ui::Range(iter.pos(), text_length)); | |
848 } while (iter.Advance()); | |
849 } | |
850 } | |
851 return line_breaks_; | |
852 } | |
853 | |
821 void RenderText::ApplyCompositionAndSelectionStyles() { | 854 void RenderText::ApplyCompositionAndSelectionStyles() { |
822 // Save the underline and color breaks to undo the temporary styles later. | 855 // Save the underline and color breaks to undo the temporary styles later. |
823 DCHECK(!composition_and_selection_styles_applied_); | 856 DCHECK(!composition_and_selection_styles_applied_); |
824 saved_colors_ = colors_; | 857 saved_colors_ = colors_; |
825 saved_underlines_ = styles_[UNDERLINE]; | 858 saved_underlines_ = styles_[UNDERLINE]; |
826 | 859 |
827 // Apply an underline to the composition range in |underlines|. | 860 // Apply an underline to the composition range in |underlines|. |
828 if (composition_range_.IsValid() && !composition_range_.is_empty()) | 861 if (composition_range_.IsValid() && !composition_range_.is_empty()) |
829 styles_[UNDERLINE].ApplyValue(true, composition_range_); | 862 styles_[UNDERLINE].ApplyValue(true, composition_range_); |
830 | 863 |
831 // Apply the selected text color to the [un-reversed] selection range. | 864 // Apply the selected text color to the [un-reversed] selection range. |
832 if (!selection().is_empty()) { | 865 if (!selection().is_empty()) { |
833 const ui::Range range(selection().GetMin(), selection().GetMax()); | 866 const ui::Range range(selection().GetMin(), selection().GetMax()); |
834 colors_.ApplyValue(selection_color_, range); | 867 colors_.ApplyValue(selection_color_, range); |
835 } | 868 } |
836 composition_and_selection_styles_applied_ = true; | 869 composition_and_selection_styles_applied_ = true; |
837 } | 870 } |
838 | 871 |
839 void RenderText::UndoCompositionAndSelectionStyles() { | 872 void RenderText::UndoCompositionAndSelectionStyles() { |
840 // Restore the underline and color breaks to undo the temporary styles. | 873 // Restore the underline and color breaks to undo the temporary styles. |
841 DCHECK(composition_and_selection_styles_applied_); | 874 DCHECK(composition_and_selection_styles_applied_); |
842 colors_ = saved_colors_; | 875 colors_ = saved_colors_; |
843 styles_[UNDERLINE] = saved_underlines_; | 876 styles_[UNDERLINE] = saved_underlines_; |
844 composition_and_selection_styles_applied_ = false; | 877 composition_and_selection_styles_applied_ = false; |
845 } | 878 } |
846 | 879 |
847 Vector2d RenderText::GetTextOffset() { | 880 Vector2d RenderText::GetLineOffset(size_t line_number) { |
848 Vector2d offset = display_rect().OffsetFromOrigin(); | 881 Vector2d offset = display_rect().OffsetFromOrigin(); |
849 offset.Add(GetUpdatedDisplayOffset()); | 882 if (!multiline()) |
850 offset.Add(GetAlignmentOffset()); | 883 offset.Add(GetUpdatedDisplayOffset()); |
884 else | |
885 offset.Add(Vector2d(0, lines_[line_number].preceding_heights)); | |
886 offset.Add(GetAlignmentOffset(line_number)); | |
851 return offset; | 887 return offset; |
852 } | 888 } |
853 | 889 |
854 Point RenderText::ToTextPoint(const Point& point) { | 890 Point RenderText::ToTextPoint(const Point& point) { |
855 return point - GetTextOffset(); | 891 return point - GetLineOffset(0); |
892 // TODO(ckocagil): implement multiline. | |
856 } | 893 } |
857 | 894 |
858 Point RenderText::ToViewPoint(const Point& point) { | 895 Point RenderText::ToViewPoint(const Point& point) { |
859 return point + GetTextOffset(); | 896 if (!multiline()) |
897 return point + GetLineOffset(0); | |
898 | |
899 // TODO(ckocagil): refactor this to traverse all line segments to support rtl. | |
900 DCHECK(!lines_.empty()); | |
901 int x = point.x(); | |
902 unsigned int line = 0; | |
Alexei Svitkine (slow)
2013/08/27 15:40:38
size_t?
ckocagil
2013/08/28 19:16:26
Done.
| |
903 while (line < lines_.size() && x > lines_[line].size.width()) { | |
904 x -= lines_[line].size.width(); | |
905 ++line; | |
906 } | |
907 return Point(x, point.y()) + GetLineOffset(line); | |
860 } | 908 } |
861 | 909 |
862 Vector2d RenderText::GetAlignmentOffset() { | 910 std::vector<Rect> RenderText::TextBoundsToViewBounds(const ui::Range& x) { |
911 std::vector<Rect> rects; | |
912 | |
913 if (!multiline()) { | |
914 rects.push_back(Rect(ToViewPoint(Point(x.GetMin(), 0)), | |
915 Size(x.length(), GetStringSize().height()))); | |
916 return rects; | |
917 } | |
918 | |
919 EnsureLayout(); | |
920 | |
921 // Each line segment keeps its position in text coordinates. Traverse all line | |
922 // segments and if the segment intersects with the given range, add the view | |
923 // rect corresponding to the intersection to |rects|. | |
924 for (size_t line = 0; line < lines_.size(); ++line) { | |
925 int line_x = 0; | |
926 Vector2d offset = GetLineOffset(line); | |
927 for (size_t i = 0; i < lines_[line].segments.size(); ++i) { | |
928 const internal::LineSegment* segment = &lines_[line].segments[i]; | |
929 const ui::Range intersection = segment->x_range.Intersect(x); | |
930 if (!intersection.is_empty()) { | |
931 Rect rect(line_x + intersection.start() - segment->x_range.start(), | |
932 0, intersection.length(), lines_[line].size.height()); | |
933 rects.push_back(rect + offset); | |
934 } | |
935 line_x += segment->x_range.length(); | |
936 } | |
937 } | |
938 | |
939 return rects; | |
940 } | |
941 | |
942 Vector2d RenderText::GetAlignmentOffset(size_t line_number) { | |
943 DCHECK_LT(line_number, lines_.size()); | |
863 Vector2d offset; | 944 Vector2d offset; |
864 if (horizontal_alignment_ != ALIGN_LEFT) { | 945 if (horizontal_alignment_ != ALIGN_LEFT) { |
865 offset.set_x(display_rect().width() - GetContentWidth()); | 946 const int width = lines_[line_number].size.width() + |
947 (cursor_enabled_ ? 1 : 0); | |
948 offset.set_x(display_rect().width() - width); | |
866 if (horizontal_alignment_ == ALIGN_CENTER) | 949 if (horizontal_alignment_ == ALIGN_CENTER) |
867 offset.set_x(offset.x() / 2); | 950 offset.set_x(offset.x() / 2); |
868 } | 951 } |
869 if (vertical_alignment_ != ALIGN_TOP) { | 952 if (vertical_alignment_ != ALIGN_TOP) { |
870 offset.set_y(display_rect().height() - GetStringSize().height()); | 953 offset.set_y(display_rect().height() - GetMultilineTextSize().height()); |
871 if (vertical_alignment_ == ALIGN_VCENTER) | 954 if (vertical_alignment_ == ALIGN_VCENTER) |
872 offset.set_y(offset.y() / 2); | 955 offset.set_y(offset.y() / 2); |
873 } | 956 } |
874 return offset; | 957 return offset; |
875 } | 958 } |
876 | 959 |
877 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { | 960 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { |
878 if (!fade_head() && !fade_tail()) | 961 if (multiline() || !fade_head() && !fade_tail()) |
879 return; | 962 return; |
880 | 963 |
881 const int text_width = GetStringSize().width(); | |
882 const int display_width = display_rect().width(); | 964 const int display_width = display_rect().width(); |
883 | 965 |
884 // If the text fits as-is, no need to fade. | 966 // If the text fits as-is, no need to fade. |
885 if (text_width <= display_width) | 967 if (GetStringSize().width() <= display_width) |
886 return; | 968 return; |
887 | 969 |
888 int gradient_width = CalculateFadeGradientWidth(GetPrimaryFont(), | 970 int gradient_width = CalculateFadeGradientWidth(GetPrimaryFont(), |
889 display_width); | 971 display_width); |
890 if (gradient_width == 0) | 972 if (gradient_width == 0) |
891 return; | 973 return; |
892 | 974 |
893 bool fade_left = fade_head(); | 975 bool fade_left = fade_head(); |
894 bool fade_right = fade_tail(); | 976 bool fade_right = fade_tail(); |
895 // Under RTL, |fade_right| == |fade_head|. | 977 // Under RTL, |fade_right| == |fade_head|. |
(...skipping 11 matching lines...) Expand all Loading... | |
907 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); | 989 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); |
908 solid_part.Inset(gradient_width, 0, 0, 0); | 990 solid_part.Inset(gradient_width, 0, 0, 0); |
909 } | 991 } |
910 if (fade_right) { | 992 if (fade_right) { |
911 right_part = solid_part; | 993 right_part = solid_part; |
912 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); | 994 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); |
913 solid_part.Inset(0, 0, gradient_width, 0); | 995 solid_part.Inset(0, 0, gradient_width, 0); |
914 } | 996 } |
915 | 997 |
916 Rect text_rect = display_rect(); | 998 Rect text_rect = display_rect(); |
917 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0); | 999 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
918 | 1000 |
919 // TODO(msw): Use the actual text colors corresponding to each faded part. | 1001 // TODO(msw): Use the actual text colors corresponding to each faded part. |
920 skia::RefPtr<SkShader> shader = CreateFadeShader( | 1002 skia::RefPtr<SkShader> shader = CreateFadeShader( |
921 text_rect, left_part, right_part, colors_.breaks().front().second); | 1003 text_rect, left_part, right_part, colors_.breaks().front().second); |
922 if (shader) | 1004 if (shader) |
923 renderer->SetShader(shader.get(), display_rect()); | 1005 renderer->SetShader(shader.get(), display_rect()); |
924 } | 1006 } |
925 | 1007 |
926 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1008 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
927 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_); | 1009 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_); |
(...skipping 13 matching lines...) Expand all Loading... | |
941 void RenderText::MoveCursorTo(size_t position, bool select) { | 1023 void RenderText::MoveCursorTo(size_t position, bool select) { |
942 size_t cursor = std::min(position, text().length()); | 1024 size_t cursor = std::min(position, text().length()); |
943 if (IsCursorablePosition(cursor)) | 1025 if (IsCursorablePosition(cursor)) |
944 SetSelectionModel(SelectionModel( | 1026 SetSelectionModel(SelectionModel( |
945 ui::Range(select ? selection().start() : cursor, cursor), | 1027 ui::Range(select ? selection().start() : cursor, cursor), |
946 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); | 1028 (cursor == 0) ? CURSOR_FORWARD : CURSOR_BACKWARD)); |
947 } | 1029 } |
948 | 1030 |
949 void RenderText::UpdateLayoutText() { | 1031 void RenderText::UpdateLayoutText() { |
950 layout_text_.clear(); | 1032 layout_text_.clear(); |
1033 line_breaks_.SetMax(0); | |
951 | 1034 |
952 if (obscured_) { | 1035 if (obscured_) { |
953 size_t obscured_text_length = | 1036 size_t obscured_text_length = |
954 static_cast<size_t>(ui::UTF16IndexToOffset(text_, 0, text_.length())); | 1037 static_cast<size_t>(ui::UTF16IndexToOffset(text_, 0, text_.length())); |
955 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); | 1038 layout_text_.assign(obscured_text_length, kPasswordReplacementChar); |
956 | 1039 |
957 if (obscured_reveal_index_ >= 0 && | 1040 if (obscured_reveal_index_ >= 0 && |
958 obscured_reveal_index_ < static_cast<int>(text_.length())) { | 1041 obscured_reveal_index_ < static_cast<int>(text_.length())) { |
959 // Gets the index range in |text_| to be revealed. | 1042 // Gets the index range in |text_| to be revealed. |
960 size_t start = obscured_reveal_index_; | 1043 size_t start = obscured_reveal_index_; |
(...skipping 16 matching lines...) Expand all Loading... | |
977 icu::StringCharacterIterator iter(text.c_str()); | 1060 icu::StringCharacterIterator iter(text.c_str()); |
978 iter.setIndex32(truncate_length_ - 1); | 1061 iter.setIndex32(truncate_length_ - 1); |
979 layout_text_.assign(text.substr(0, iter.getIndex()) + ui::kEllipsisUTF16); | 1062 layout_text_.assign(text.substr(0, iter.getIndex()) + ui::kEllipsisUTF16); |
980 } | 1063 } |
981 } | 1064 } |
982 | 1065 |
983 void RenderText::UpdateCachedBoundsAndOffset() { | 1066 void RenderText::UpdateCachedBoundsAndOffset() { |
984 if (cached_bounds_and_offset_valid_) | 1067 if (cached_bounds_and_offset_valid_) |
985 return; | 1068 return; |
986 | 1069 |
1070 // TODO(ckocagil): Add vertical offset support for scrolling multi-line text. | |
1071 | |
987 // First, set the valid flag true to calculate the current cursor bounds using | 1072 // First, set the valid flag true to calculate the current cursor bounds using |
988 // the stale |display_offset_|. Applying |delta_offset| at the end of this | 1073 // the stale |display_offset_|. Applying |delta_offset| at the end of this |
989 // function will set |cursor_bounds_| and |display_offset_| to correct values. | 1074 // function will set |cursor_bounds_| and |display_offset_| to correct values. |
990 cached_bounds_and_offset_valid_ = true; | 1075 cached_bounds_and_offset_valid_ = true; |
991 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); | 1076 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); |
992 | 1077 |
993 // Update |display_offset_| to ensure the current cursor is visible. | 1078 // Update |display_offset_| to ensure the current cursor is visible. |
994 const int display_width = display_rect_.width(); | 1079 const int display_width = display_rect_.width(); |
995 const int content_width = GetContentWidth(); | 1080 const int content_width = GetContentWidth(); |
996 | 1081 |
(...skipping 30 matching lines...) Expand all Loading... | |
1027 cursor_bounds_ += delta_offset; | 1112 cursor_bounds_ += delta_offset; |
1028 } | 1113 } |
1029 | 1114 |
1030 void RenderText::DrawSelection(Canvas* canvas) { | 1115 void RenderText::DrawSelection(Canvas* canvas) { |
1031 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1116 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
1032 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1117 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
1033 canvas->FillRect(*i, selection_background_focused_color_); | 1118 canvas->FillRect(*i, selection_background_focused_color_); |
1034 } | 1119 } |
1035 | 1120 |
1036 } // namespace gfx | 1121 } // namespace gfx |
OLD | NEW |