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

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

Issue 2541313002: RenderTextHarfBuzz: Add support for multi line text selection. (Closed)
Patch Set: --- Created 4 years 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 #include <set> 8 #include <set>
9 9
10 #include "base/i18n/bidi_line_iterator.h" 10 #include "base/i18n/bidi_line_iterator.h"
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 const Range word_range = words_->GetRange(iter); 284 const Range word_range = words_->GetRange(iter);
285 std::vector<internal::LineSegment> word_segments; 285 std::vector<internal::LineSegment> word_segments;
286 SkScalar word_width = GetWordWidth(word_range, &word_segments); 286 SkScalar word_width = GetWordWidth(word_range, &word_segments);
287 287
288 // If the last word is '\n', we should advance a new line after adding 288 // If the last word is '\n', we should advance a new line after adding
289 // the word to the current line. 289 // the word to the current line.
290 bool new_line = false; 290 bool new_line = false;
291 if (!word_segments.empty() && 291 if (!word_segments.empty() &&
292 text_[word_segments.back().char_range.start()] == '\n') { 292 text_[word_segments.back().char_range.start()] == '\n') {
293 new_line = true; 293 new_line = true;
294 word_width -= word_segments.back().width(); 294
295 word_segments.pop_back(); 295 // Since the line should atleast contain some information regarding the
msw 2016/12/07 20:20:35 nit: 'at least'
karandeepb 2016/12/12 10:36:18 Done.
296 // text range it corresponds to, don't pop the newline segment, if it's
297 // the only segment in the line. This ensures that every line has a non-
298 // empty segments vector (except the last in some cases).
299 if (word_segments.size() != 1u || available_width_ != max_width_) {
300 word_width -= word_segments.back().width();
301 word_segments.pop_back();
302 }
296 } 303 }
297 304
298 // If the word is not the first word in the line and it can't fit into 305 // If the word is not the first word in the line and it can't fit into
299 // the current line, advance a new line. 306 // the current line, advance a new line.
300 if (word_width > available_width_ && available_width_ != max_width_) 307 if (word_width > available_width_ && available_width_ != max_width_)
301 AdvanceLine(); 308 AdvanceLine();
302 if (!word_segments.empty()) 309 if (!word_segments.empty())
303 AddWordToLine(word_segments); 310 AddWordToLine(word_segments);
304 if (new_line) 311 if (new_line)
305 AdvanceLine(); 312 AdvanceLine();
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 Size RenderTextHarfBuzz::GetStringSize() { 815 Size RenderTextHarfBuzz::GetStringSize() {
809 const SizeF size_f = GetStringSizeF(); 816 const SizeF size_f = GetStringSizeF();
810 return Size(std::ceil(size_f.width()), size_f.height()); 817 return Size(std::ceil(size_f.width()), size_f.height());
811 } 818 }
812 819
813 SizeF RenderTextHarfBuzz::GetStringSizeF() { 820 SizeF RenderTextHarfBuzz::GetStringSizeF() {
814 EnsureLayout(); 821 EnsureLayout();
815 return total_size_; 822 return total_size_;
816 } 823 }
817 824
818 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& point) { 825 SelectionModel RenderTextHarfBuzz::FindCursorPosition(const Point& view_point) {
819 EnsureLayout(); 826 EnsureLayout();
827 DCHECK(!lines().empty());
msw 2016/12/07 20:20:35 Is lines() empty after layout for non-multiline in
karandeepb 2016/12/12 10:36:18 No it isn't. HarfBuzzLineBreaker constructor alway
820 828
821 int x = ToTextPoint(point).x(); 829 int line_index = GetLineContainingYCoord((view_point - GetLineOffset(0)).y());
822 float offset = 0; 830 // Clip line index to a valid value in case kDragToEndIfOutsideVerticalBounds
823 size_t run_index = GetRunContainingXCoord(x, &offset); 831 // is false. Else, drag to end.
832 if (line_index < 0) {
833 if (RenderText::kDragToEndIfOutsideVerticalBounds)
834 return EdgeSelectionModel(GetVisualDirectionOfLogicalBeginning());
835 else
836 line_index++;
msw 2016/12/07 20:20:35 nit: line_index = 0;
karandeepb 2016/12/12 10:36:19 Done.
837 }
838 if (line_index >= (int)lines().size()) {
msw 2016/12/07 20:20:35 nit: use static_cast, or checked_cast from base/nu
karandeepb 2016/12/12 10:36:19 Done.
839 if (RenderText::kDragToEndIfOutsideVerticalBounds)
840 return EdgeSelectionModel(GetVisualDirectionOfLogicalEnd());
841 else
842 line_index--;
msw 2016/12/07 20:20:35 nit: line_index = lines().size() - 1; (with cast l
karandeepb 2016/12/12 10:36:18 Done.
843 }
844 const internal::Line& line = lines()[line_index];
824 845
825 internal::TextRunList* run_list = GetRunList(); 846 float point_offset_relative_segment = 0;
826 if (run_index >= run_list->size()) 847 const int segment_index = GetLineSegmentContainingXCoord(
827 return EdgeSelectionModel((x < 0) ? CURSOR_LEFT : CURSOR_RIGHT); 848 line, (view_point - GetLineOffset(line_index)).x(),
828 const internal::TextRunHarfBuzz& run = *run_list->runs()[run_index]; 849 &point_offset_relative_segment);
850 if (segment_index < 0)
851 return LineSelectionModel(line, CURSOR_LEFT);
852 if (segment_index >= (int)line.segments.size())
msw 2016/12/07 20:20:35 ditto nit: casting
karandeepb 2016/12/12 10:36:19 Done.
853 return LineSelectionModel(line, CURSOR_RIGHT);
854 const internal::LineSegment& segment = line.segments[segment_index];
855
856 const internal::TextRunHarfBuzz& run = *GetRunList()->runs()[segment.run];
857 const Range segment_glyph_range =
msw 2016/12/07 20:20:35 optional nit: just cache the min value instead
karandeepb 2016/12/12 10:36:18 Done.
858 run.CharRangeToGlyphRange(segment.char_range);
859 const float segment_offset_relative_run =
860 segment_glyph_range.GetMin() != 0
861 ? SkScalarToFloat(run.positions[segment_glyph_range.GetMin()].x())
862 : 0;
863 const float point_offset_relative_run =
864 point_offset_relative_segment + segment_offset_relative_run;
865
829 for (size_t i = 0; i < run.glyph_count; ++i) { 866 for (size_t i = 0; i < run.glyph_count; ++i) {
830 const SkScalar end = 867 const float end = i + 1 == run.glyph_count
831 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); 868 ? run.width
832 const SkScalar middle = (end + run.positions[i].x()) / 2; 869 : SkScalarToFloat(run.positions[i + 1].x());
870 const float middle = (end + SkScalarToFloat(run.positions[i].x())) / 2;
833 871
834 if (offset < middle) { 872 if (point_offset_relative_run < middle) {
835 return SelectionModel(DisplayIndexToTextIndex( 873 return SelectionModel(DisplayIndexToTextIndex(
836 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)), 874 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)),
837 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD)); 875 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD));
838 } 876 }
839 if (offset < end) { 877 if (point_offset_relative_run < end) {
840 return SelectionModel(DisplayIndexToTextIndex( 878 return SelectionModel(DisplayIndexToTextIndex(
841 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)), 879 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)),
842 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD)); 880 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD));
843 } 881 }
844 } 882 }
845 return EdgeSelectionModel(CURSOR_RIGHT); 883
884 return LineSelectionModel(line, CURSOR_RIGHT);
846 } 885 }
847 886
848 bool RenderTextHarfBuzz::IsSelectionSupported() const { 887 bool RenderTextHarfBuzz::IsSelectionSupported() const {
849 // TODO(karandeepb): Support multi-line text selection. 888 return true;
850 return !multiline();
851 } 889 }
852 890
853 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { 891 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() {
854 EnsureLayout(); 892 EnsureLayout();
855 893
856 internal::TextRunList* run_list = GetRunList(); 894 internal::TextRunList* run_list = GetRunList();
857 std::vector<RenderText::FontSpan> spans; 895 std::vector<RenderText::FontSpan> spans;
858 for (auto* run : run_list->runs()) { 896 for (auto* run : run_list->runs()) {
859 spans.push_back(RenderText::FontSpan( 897 spans.push_back(RenderText::FontSpan(
860 run->font, Range(DisplayIndexToTextIndex(run->range.start()), 898 run->font, Range(DisplayIndexToTextIndex(run->range.start()),
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { 1050 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) {
1013 DCHECK(!update_display_run_list_); 1051 DCHECK(!update_display_run_list_);
1014 DCHECK(Range(0, text().length()).Contains(range)); 1052 DCHECK(Range(0, text().length()).Contains(range));
1015 Range layout_range(TextIndexToDisplayIndex(range.start()), 1053 Range layout_range(TextIndexToDisplayIndex(range.start()),
1016 TextIndexToDisplayIndex(range.end())); 1054 TextIndexToDisplayIndex(range.end()));
1017 DCHECK(Range(0, GetDisplayText().length()).Contains(layout_range)); 1055 DCHECK(Range(0, GetDisplayText().length()).Contains(layout_range));
1018 1056
1019 std::vector<Rect> rects; 1057 std::vector<Rect> rects;
1020 if (layout_range.is_empty()) 1058 if (layout_range.is_empty())
1021 return rects; 1059 return rects;
1022 std::vector<Range> bounds;
1023 1060
1024 internal::TextRunList* run_list = GetRunList(); 1061 internal::TextRunList* run_list = GetRunList();
1062 for (size_t line_index = 0; line_index < lines().size(); ++line_index) {
1063 const internal::Line& line = lines()[line_index];
1064 if (line.segments.empty()) {
1065 DCHECK_EQ(lines().size() - 1, line_index);
msw 2016/12/07 20:20:35 ditto nit: check for lines.back() instead, if poss
karandeepb 2016/12/12 10:36:18 Would require overloading ==.
1066 continue;
msw 2016/12/07 20:20:35 nit: continue isn't necessary, given the segments
karandeepb 2016/12/12 10:36:19 Done.
1067 }
1025 1068
1026 // Add a Range for each run/selection intersection. 1069 float line_x = 0;
1027 for (size_t i = 0; i < run_list->size(); ++i) { 1070 for (const internal::LineSegment& segment : line.segments) {
1028 internal::TextRunHarfBuzz* run = 1071 const internal::TextRunHarfBuzz& run = *run_list->runs()[segment.run];
1029 run_list->runs()[run_list->visual_to_logical(i)]; 1072 const Range intersection = segment.char_range.Intersect(layout_range);
1030 Range intersection = run->range.Intersect(layout_range); 1073 DCHECK(!intersection.is_reversed());
1031 if (!intersection.IsValid()) 1074 if (!intersection.is_empty()) {
1032 continue; 1075 float width =
1033 DCHECK(!intersection.is_reversed()); 1076 SkScalarToFloat(run.GetGlyphWidthForCharRange(intersection));
1034 const size_t left_index = 1077 float x;
msw 2016/12/07 20:20:35 nit: explicitly init to 0 (or line_x and nix that
karandeepb 2016/12/12 10:36:19 Done.
1035 run->is_rtl ? intersection.end() - 1 : intersection.start(); 1078 if (run.is_rtl) {
1036 const Range leftmost_character_x = 1079 x = line_x + SkScalarToFloat(run.GetGlyphWidthForCharRange(gfx::Range(
1037 run->GetGraphemeBounds(this, left_index).Round(); 1080 intersection.end(), segment.char_range.end())));
1038 const size_t right_index = 1081 } else {
1039 run->is_rtl ? intersection.start() : intersection.end() - 1; 1082 x = line_x + SkScalarToFloat(run.GetGlyphWidthForCharRange(gfx::Range(
1040 const Range rightmost_character_x = 1083 segment.char_range.start(), intersection.start())));
1041 run->GetGraphemeBounds(this, right_index).Round(); 1084 }
1042 Range range_x(leftmost_character_x.start(), rightmost_character_x.end()); 1085 int end_x = std::ceil(x + width);
1043 DCHECK(!range_x.is_reversed()); 1086 int start_x = std::ceil(x);
1044 if (range_x.is_empty()) 1087 gfx::Rect rect(start_x, 0, end_x - start_x, line.size.height());
1045 continue; 1088 rects.push_back(rect + GetLineOffset(line_index));
1046 1089 }
1047 // Union this with the last range if they're adjacent. 1090 line_x += segment.width();
1048 DCHECK(bounds.empty() || bounds.back().GetMax() <= range_x.GetMin());
1049 if (!bounds.empty() && bounds.back().GetMax() == range_x.GetMin()) {
1050 range_x = Range(bounds.back().GetMin(), range_x.GetMax());
1051 bounds.pop_back();
1052 } 1091 }
1053 bounds.push_back(range_x);
1054 }
1055 for (Range& bound : bounds) {
1056 std::vector<Rect> current_rects = TextBoundsToViewBounds(bound);
1057 rects.insert(rects.end(), current_rects.begin(), current_rects.end());
1058 } 1092 }
1059 return rects; 1093 return rects;
1060 } 1094 }
1061 1095
1062 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) { 1096 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) {
1063 return TextIndexToGivenTextIndex(GetDisplayText(), index); 1097 return TextIndexToGivenTextIndex(GetDisplayText(), index);
1064 } 1098 }
1065 1099
1066 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) { 1100 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) {
1067 if (!obscured()) 1101 if (!obscured())
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 LogicalCursorDirection affinity = caret.caret_affinity(); 1256 LogicalCursorDirection affinity = caret.caret_affinity();
1223 internal::TextRunList* run_list = GetRunList(); 1257 internal::TextRunList* run_list = GetRunList();
1224 for (size_t i = 0; i < run_list->size(); ++i) { 1258 for (size_t i = 0; i < run_list->size(); ++i) {
1225 internal::TextRunHarfBuzz* run = run_list->runs()[i]; 1259 internal::TextRunHarfBuzz* run = run_list->runs()[i];
1226 if (RangeContainsCaret(run->range, layout_position, affinity)) 1260 if (RangeContainsCaret(run->range, layout_position, affinity))
1227 return i; 1261 return i;
1228 } 1262 }
1229 return run_list->size(); 1263 return run_list->size();
1230 } 1264 }
1231 1265
1232 size_t RenderTextHarfBuzz::GetRunContainingXCoord(float x, 1266 int RenderTextHarfBuzz::GetLineContainingYCoord(float text_y) {
1233 float* offset) const { 1267 if (text_y < 0)
1234 DCHECK(!update_display_run_list_); 1268 return -1;
1235 const internal::TextRunList* run_list = GetRunList(); 1269
1236 if (x < 0) 1270 for (size_t i = 0; i < lines().size(); i++) {
1237 return run_list->size(); 1271 const internal::Line& line = lines()[i];
1238 // Find the text run containing the argument point (assumed already offset). 1272
1239 float current_x = 0; 1273 if (text_y <= line.size.height())
1240 for (size_t i = 0; i < run_list->size(); ++i) { 1274 return i;
1241 size_t run = run_list->visual_to_logical(i); 1275 text_y -= line.size.height();
1242 current_x += run_list->runs()[run]->width; 1276 }
1243 if (x < current_x) { 1277
1244 *offset = x - (current_x - run_list->runs()[run]->width); 1278 return lines().size();
1245 return run; 1279 }
1280
1281 int RenderTextHarfBuzz::GetLineSegmentContainingXCoord(
1282 const internal::Line& line,
1283 float line_x,
1284 float* offset_relative_segment) {
1285 DCHECK(offset_relative_segment);
1286
1287 *offset_relative_segment = 0;
1288 if (line_x < 0)
1289 return -1;
1290 for (size_t i = 0; i < line.segments.size(); i++) {
1291 const internal::LineSegment& segment = line.segments[i];
1292
1293 // segment.x_range is not used because it is text space.
msw 2016/12/07 20:20:35 q: Isn't line_x also in text space?
karandeepb 2016/12/12 10:36:18 Not really. line_x is relative to the beginning of
msw 2016/12/13 03:04:55 Acknowledged.
1294 if (line_x < segment.width()) {
1295 *offset_relative_segment = line_x;
1296 return i;
1246 } 1297 }
1298 line_x -= segment.width();
1247 } 1299 }
1248 return run_list->size(); 1300 return line.segments.size();
1249 } 1301 }
1250 1302
1251 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( 1303 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun(
1252 const internal::TextRunHarfBuzz* run) { 1304 const internal::TextRunHarfBuzz* run) {
1253 size_t position = DisplayIndexToTextIndex(run->range.start()); 1305 size_t position = DisplayIndexToTextIndex(run->range.start());
1254 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); 1306 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD);
1255 return SelectionModel(position, CURSOR_BACKWARD); 1307 return SelectionModel(position, CURSOR_BACKWARD);
1256 } 1308 }
1257 1309
1258 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( 1310 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun(
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
1626 1678
1627 attribute.strike = run.strike; 1679 attribute.strike = run.strike;
1628 attribute.diagonal_strike = run.diagonal_strike; 1680 attribute.diagonal_strike = run.diagonal_strike;
1629 decorated_text->attributes.push_back(attribute); 1681 decorated_text->attributes.push_back(attribute);
1630 } 1682 }
1631 } 1683 }
1632 return true; 1684 return true;
1633 } 1685 }
1634 1686
1635 } // namespace gfx 1687 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698