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

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

Issue 16867016: Windows implementation of multiline RenderText (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: comments addressed Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #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
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() : width(0), height(0), preceding_heights(0), baseline(0) {}
305
306 Line::Line(Line& other) {
307 *this = other;
308 }
309
310 Line& Line::operator=(Line& other) {
311 width = other.width;
312 height = other.height;
313 preceding_heights = other.preceding_heights;
314 baseline = other.baseline;
315 segments.swap(other.segments);
316 return *this;
317 }
318
304 } // namespace internal 319 } // namespace internal
305 320
306 RenderText::~RenderText() { 321 RenderText::~RenderText() {
307 } 322 }
308 323
309 void RenderText::SetText(const base::string16& text) { 324 void RenderText::SetText(const base::string16& text) {
310 DCHECK(!composition_range_.IsValid()); 325 DCHECK(!composition_range_.IsValid());
326 if (text_ == text)
327 return;
311 text_ = text; 328 text_ = text;
312 329
313 // Adjust ranged styles and colors to accommodate a new text length. 330 // Adjust ranged styles and colors to accommodate a new text length.
314 const size_t text_length = text_.length(); 331 const size_t text_length = text_.length();
315 colors_.SetMax(text_length); 332 colors_.SetMax(text_length);
316 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) 333 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
317 styles_[style].SetMax(text_length); 334 styles_[style].SetMax(text_length);
318 cached_bounds_and_offset_valid_ = false; 335 cached_bounds_and_offset_valid_ = false;
319 336
320 // Reset selection model. SetText should always followed by SetSelectionModel 337 // Reset selection model. SetText should always followed by SetSelectionModel
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 void RenderText::SetObscured(bool obscured) { 394 void RenderText::SetObscured(bool obscured) {
378 if (obscured != obscured_) { 395 if (obscured != obscured_) {
379 obscured_ = obscured; 396 obscured_ = obscured;
380 obscured_reveal_index_ = -1; 397 obscured_reveal_index_ = -1;
381 cached_bounds_and_offset_valid_ = false; 398 cached_bounds_and_offset_valid_ = false;
382 UpdateLayoutText(); 399 UpdateLayoutText();
383 ResetLayout(); 400 ResetLayout();
384 } 401 }
385 } 402 }
386 403
404 void RenderText::SetMultiline(bool multiline) {
405 if (multiline != multiline_) {
406 multiline_ = multiline;
407 cached_bounds_and_offset_valid_ = false;
408 ResetLayout();
409 }
410 }
411
387 void RenderText::SetObscuredRevealIndex(int index) { 412 void RenderText::SetObscuredRevealIndex(int index) {
388 if (obscured_reveal_index_ == index) 413 if (obscured_reveal_index_ == index)
389 return; 414 return;
390 415
391 obscured_reveal_index_ = index; 416 obscured_reveal_index_ = index;
392 cached_bounds_and_offset_valid_ = false; 417 cached_bounds_and_offset_valid_ = false;
393 UpdateLayoutText(); 418 UpdateLayoutText();
394 ResetLayout(); 419 ResetLayout();
395 } 420 }
396 421
397 void RenderText::SetDisplayRect(const Rect& r) { 422 void RenderText::SetDisplayRect(const Rect& r) {
398 display_rect_ = r; 423 display_rect_ = r;
399 cached_bounds_and_offset_valid_ = false; 424 cached_bounds_and_offset_valid_ = false;
425 lines_.clear();
400 } 426 }
401 427
402 void RenderText::SetCursorPosition(size_t position) { 428 void RenderText::SetCursorPosition(size_t position) {
403 MoveCursorTo(position, false); 429 MoveCursorTo(position, false);
404 } 430 }
405 431
406 void RenderText::MoveCursor(BreakType break_type, 432 void RenderText::MoveCursor(BreakType break_type,
407 VisualCursorDirection direction, 433 VisualCursorDirection direction,
408 bool select) { 434 bool select) {
409 SelectionModel position(cursor_position(), selection_model_.caret_affinity()); 435 SelectionModel position(cursor_position(), selection_model_.caret_affinity());
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 selection_color_(kDefaultColor), 794 selection_color_(kDefaultColor),
769 selection_background_focused_color_(kDefaultSelectionBackgroundColor), 795 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
770 focused_(false), 796 focused_(false),
771 composition_range_(ui::Range::InvalidRange()), 797 composition_range_(ui::Range::InvalidRange()),
772 colors_(kDefaultColor), 798 colors_(kDefaultColor),
773 styles_(NUM_TEXT_STYLES), 799 styles_(NUM_TEXT_STYLES),
774 composition_and_selection_styles_applied_(false), 800 composition_and_selection_styles_applied_(false),
775 obscured_(false), 801 obscured_(false),
776 obscured_reveal_index_(-1), 802 obscured_reveal_index_(-1),
777 truncate_length_(0), 803 truncate_length_(0),
804 multiline_(false),
778 fade_head_(false), 805 fade_head_(false),
779 fade_tail_(false), 806 fade_tail_(false),
780 background_is_transparent_(false), 807 background_is_transparent_(false),
781 clip_to_display_rect_(true), 808 clip_to_display_rect_(true),
782 cached_bounds_and_offset_valid_(false) { 809 cached_bounds_and_offset_valid_(false) {
783 } 810 }
784 811
785 const Vector2d& RenderText::GetUpdatedDisplayOffset() { 812 const Vector2d& RenderText::GetUpdatedDisplayOffset() {
786 UpdateCachedBoundsAndOffset(); 813 UpdateCachedBoundsAndOffset();
787 return display_offset_; 814 return display_offset_;
(...skipping 23 matching lines...) Expand all
811 void RenderText::SetSelectionModel(const SelectionModel& model) { 838 void RenderText::SetSelectionModel(const SelectionModel& model) {
812 DCHECK_LE(model.selection().GetMax(), text().length()); 839 DCHECK_LE(model.selection().GetMax(), text().length());
813 selection_model_ = model; 840 selection_model_ = model;
814 cached_bounds_and_offset_valid_ = false; 841 cached_bounds_and_offset_valid_ = false;
815 } 842 }
816 843
817 const base::string16& RenderText::GetLayoutText() const { 844 const base::string16& RenderText::GetLayoutText() const {
818 return layout_text_.empty() ? text_ : layout_text_; 845 return layout_text_.empty() ? text_ : layout_text_;
819 } 846 }
820 847
848 const BreakList<size_t>& RenderText::GetLineBreaks() {
849 if (line_breaks_.max() == 0) {
850 const string16& layout_text = GetLayoutText();
851 line_breaks_.SetValue(0);
852 line_breaks_.SetMax(layout_text.length());
853 base::i18n::BreakIterator iter(layout_text,
854 base::i18n::BreakIterator::BREAK_LINE);
855 bool success = iter.Init();
856 CHECK(success);
857 do {
858 line_breaks_.ApplyValue(iter.pos(), ui::Range(iter.pos(),
859 layout_text.length()));
860 } while (iter.Advance());
861 }
862 return line_breaks_;
863 }
864
821 void RenderText::ApplyCompositionAndSelectionStyles() { 865 void RenderText::ApplyCompositionAndSelectionStyles() {
822 // Save the underline and color breaks to undo the temporary styles later. 866 // Save the underline and color breaks to undo the temporary styles later.
823 DCHECK(!composition_and_selection_styles_applied_); 867 DCHECK(!composition_and_selection_styles_applied_);
824 saved_colors_ = colors_; 868 saved_colors_ = colors_;
825 saved_underlines_ = styles_[UNDERLINE]; 869 saved_underlines_ = styles_[UNDERLINE];
826 870
827 // Apply an underline to the composition range in |underlines|. 871 // Apply an underline to the composition range in |underlines|.
828 if (composition_range_.IsValid() && !composition_range_.is_empty()) 872 if (composition_range_.IsValid() && !composition_range_.is_empty())
829 styles_[UNDERLINE].ApplyValue(true, composition_range_); 873 styles_[UNDERLINE].ApplyValue(true, composition_range_);
830 874
831 // Apply the selected text color to the [un-reversed] selection range. 875 // Apply the selected text color to the [un-reversed] selection range.
832 if (!selection().is_empty()) { 876 if (!selection().is_empty()) {
833 const ui::Range range(selection().GetMin(), selection().GetMax()); 877 const ui::Range range(selection().GetMin(), selection().GetMax());
834 colors_.ApplyValue(selection_color_, range); 878 colors_.ApplyValue(selection_color_, range);
835 } 879 }
836 composition_and_selection_styles_applied_ = true; 880 composition_and_selection_styles_applied_ = true;
837 } 881 }
838 882
839 void RenderText::UndoCompositionAndSelectionStyles() { 883 void RenderText::UndoCompositionAndSelectionStyles() {
840 // Restore the underline and color breaks to undo the temporary styles. 884 // Restore the underline and color breaks to undo the temporary styles.
841 DCHECK(composition_and_selection_styles_applied_); 885 DCHECK(composition_and_selection_styles_applied_);
842 colors_ = saved_colors_; 886 colors_ = saved_colors_;
843 styles_[UNDERLINE] = saved_underlines_; 887 styles_[UNDERLINE] = saved_underlines_;
844 composition_and_selection_styles_applied_ = false; 888 composition_and_selection_styles_applied_ = false;
845 } 889 }
846 890
847 Vector2d RenderText::GetTextOffset() { 891 Vector2d RenderText::GetLineOffset(size_t line_number) {
848 Vector2d offset = display_rect().OffsetFromOrigin(); 892 Vector2d offset = display_rect().OffsetFromOrigin();
849 offset.Add(GetUpdatedDisplayOffset()); 893 if (!multiline())
850 offset.Add(GetAlignmentOffset()); 894 offset.Add(GetUpdatedDisplayOffset());
895 else
896 offset.Add(Vector2d(0, lines()[line_number].preceding_heights));
897 offset.Add(GetAlignmentOffset(line_number));
851 return offset; 898 return offset;
852 } 899 }
853 900
854 Point RenderText::ToTextPoint(const Point& point) { 901 Point RenderText::ToTextPoint(const Point& point) {
855 return point - GetTextOffset(); 902 return point - GetLineOffset(0);
903 // TODO(ckocagil): implement multiline.
856 } 904 }
857 905
858 Point RenderText::ToViewPoint(const Point& point) { 906 Point RenderText::ToViewPoint(const Point& point) {
859 return point + GetTextOffset(); 907 if (!multiline())
908 return point + GetLineOffset(0);
909
910 // TODO(ckocagil): refactor this to traverse all line segments to support rtl.
911 DCHECK(!lines_.empty());
912 int x = point.x();
913 unsigned int line = 0;
914 while (line < lines_.size() && x > lines()[line].width) {
915 x -= lines()[line].width;
916 ++line;
917 }
918 return Point(x, point.y()) + GetLineOffset(line);
860 } 919 }
861 920
862 Vector2d RenderText::GetAlignmentOffset() { 921 std::vector<Rect> RenderText::TextBoundsToViewBounds(const ui::Range& x) {
922 std::vector<Rect> rects;
923
924 if (!multiline()) {
925 rects.push_back(Rect(ToViewPoint(Point(x.GetMin(), 0)),
926 Size(x.length(), GetStringSize().height())));
927 return rects;
928 }
929
930 EnsureLayout();
931
932 // Each line segments keeps its position in text coordinates. Traverse all
933 // line segments and if the segment intersects with the given range, add the
934 // view rect corresponding to the intersection to |rects|.
935 for (size_t line = 0; line < lines().size(); ++line) {
936 int line_x = 0;
937 Vector2d offset = GetLineOffset(line);
938 for (size_t i = 0; i < lines()[line].segments.size(); ++i) {
939 const internal::LineSegment* segment = &lines()[line].segments[i];
940 ui::Range intersection = segment->x_pos.Intersect(x);
941 if (!intersection.is_empty()) {
942 Rect rect(line_x + intersection.start() - segment->x_pos.start(),
943 0, intersection.length(), lines()[line].height);
944 rect += offset;
945 rects.push_back(rect);
946 }
947 line_x += segment->x_pos.length();
948 }
949 }
950
951 return rects;
952 }
953
954
955 Vector2d RenderText::GetAlignmentOffset(size_t line_number) {
956 DCHECK(!lines_.empty());
957 const int width = lines()[line_number].width + (cursor_enabled_ ? 1 : 0);
Alexei Svitkine (slow) 2013/08/16 18:32:34 Nit: lines_
ckocagil 2013/08/20 00:09:06 Done (throughout the file).
863 Vector2d offset; 958 Vector2d offset;
864 if (horizontal_alignment_ != ALIGN_LEFT) { 959 if (horizontal_alignment_ != ALIGN_LEFT) {
865 offset.set_x(display_rect().width() - GetContentWidth()); 960 offset.set_x(display_rect().width() - width);
866 if (horizontal_alignment_ == ALIGN_CENTER) 961 if (horizontal_alignment_ == ALIGN_CENTER)
867 offset.set_x(offset.x() / 2); 962 offset.set_x(offset.x() / 2);
868 } 963 }
869 if (vertical_alignment_ != ALIGN_TOP) { 964 if (vertical_alignment_ != ALIGN_TOP) {
870 offset.set_y(display_rect().height() - GetStringSize().height()); 965 offset.set_y(display_rect().height() - GetMultilineTextSize().height());
871 if (vertical_alignment_ == ALIGN_VCENTER) 966 if (vertical_alignment_ == ALIGN_VCENTER)
872 offset.set_y(offset.y() / 2); 967 offset.set_y(offset.y() / 2);
873 } 968 }
874 return offset; 969 return offset;
875 } 970 }
876 971
877 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { 972 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
878 if (!fade_head() && !fade_tail()) 973 if (multiline() || !fade_head() && !fade_tail())
879 return; 974 return;
880 975
881 const int text_width = GetStringSize().width();
882 const int display_width = display_rect().width(); 976 const int display_width = display_rect().width();
883 977
884 // If the text fits as-is, no need to fade. 978 // If the text fits as-is, no need to fade.
885 if (text_width <= display_width) 979 if (GetStringSize().width() <= display_width)
886 return; 980 return;
887 981
888 int gradient_width = CalculateFadeGradientWidth(GetPrimaryFont(), 982 int gradient_width = CalculateFadeGradientWidth(GetPrimaryFont(),
889 display_width); 983 display_width);
890 if (gradient_width == 0) 984 if (gradient_width == 0)
891 return; 985 return;
892 986
893 bool fade_left = fade_head(); 987 bool fade_left = fade_head();
894 bool fade_right = fade_tail(); 988 bool fade_right = fade_tail();
895 // Under RTL, |fade_right| == |fade_head|. 989 // Under RTL, |fade_right| == |fade_head|.
(...skipping 11 matching lines...) Expand all
907 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); 1001 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0);
908 solid_part.Inset(gradient_width, 0, 0, 0); 1002 solid_part.Inset(gradient_width, 0, 0, 0);
909 } 1003 }
910 if (fade_right) { 1004 if (fade_right) {
911 right_part = solid_part; 1005 right_part = solid_part;
912 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); 1006 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
913 solid_part.Inset(0, 0, gradient_width, 0); 1007 solid_part.Inset(0, 0, gradient_width, 0);
914 } 1008 }
915 1009
916 Rect text_rect = display_rect(); 1010 Rect text_rect = display_rect();
917 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0); 1011 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0);
918 1012
919 // TODO(msw): Use the actual text colors corresponding to each faded part. 1013 // TODO(msw): Use the actual text colors corresponding to each faded part.
920 skia::RefPtr<SkShader> shader = CreateFadeShader( 1014 skia::RefPtr<SkShader> shader = CreateFadeShader(
921 text_rect, left_part, right_part, colors_.breaks().front().second); 1015 text_rect, left_part, right_part, colors_.breaks().front().second);
922 if (shader) 1016 if (shader)
923 renderer->SetShader(shader.get(), display_rect()); 1017 renderer->SetShader(shader.get(), display_rect());
924 } 1018 }
925 1019
926 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 1020 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
927 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_); 1021 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 } 1065 }
972 } 1066 }
973 1067
974 const base::string16& text = obscured_ ? layout_text_ : text_; 1068 const base::string16& text = obscured_ ? layout_text_ : text_;
975 if (truncate_length_ > 0 && truncate_length_ < text.length()) { 1069 if (truncate_length_ > 0 && truncate_length_ < text.length()) {
976 // Truncate the text at a valid character break and append an ellipsis. 1070 // Truncate the text at a valid character break and append an ellipsis.
977 icu::StringCharacterIterator iter(text.c_str()); 1071 icu::StringCharacterIterator iter(text.c_str());
978 iter.setIndex32(truncate_length_ - 1); 1072 iter.setIndex32(truncate_length_ - 1);
979 layout_text_.assign(text.substr(0, iter.getIndex()) + ui::kEllipsisUTF16); 1073 layout_text_.assign(text.substr(0, iter.getIndex()) + ui::kEllipsisUTF16);
980 } 1074 }
1075
1076 line_breaks_.SetMax(0);
981 } 1077 }
982 1078
983 void RenderText::UpdateCachedBoundsAndOffset() { 1079 void RenderText::UpdateCachedBoundsAndOffset() {
984 if (cached_bounds_and_offset_valid_) 1080 if (cached_bounds_and_offset_valid_)
985 return; 1081 return;
986 1082
1083 // TODO(ckocagil): Add vertical offset support for scrolling multi-line text.
1084
987 // First, set the valid flag true to calculate the current cursor bounds using 1085 // 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 1086 // the stale |display_offset_|. Applying |delta_offset| at the end of this
989 // function will set |cursor_bounds_| and |display_offset_| to correct values. 1087 // function will set |cursor_bounds_| and |display_offset_| to correct values.
990 cached_bounds_and_offset_valid_ = true; 1088 cached_bounds_and_offset_valid_ = true;
991 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); 1089 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_);
992 1090
993 // Update |display_offset_| to ensure the current cursor is visible. 1091 // Update |display_offset_| to ensure the current cursor is visible.
994 const int display_width = display_rect_.width(); 1092 const int display_width = display_rect_.width();
995 const int content_width = GetContentWidth(); 1093 const int content_width = GetContentWidth();
996 1094
(...skipping 30 matching lines...) Expand all
1027 cursor_bounds_ += delta_offset; 1125 cursor_bounds_ += delta_offset;
1028 } 1126 }
1029 1127
1030 void RenderText::DrawSelection(Canvas* canvas) { 1128 void RenderText::DrawSelection(Canvas* canvas) {
1031 const std::vector<Rect> sel = GetSubstringBounds(selection()); 1129 const std::vector<Rect> sel = GetSubstringBounds(selection());
1032 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 1130 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
1033 canvas->FillRect(*i, selection_background_focused_color_); 1131 canvas->FillRect(*i, selection_background_focused_color_);
1034 } 1132 }
1035 1133
1036 } // namespace gfx 1134 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698