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

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

Issue 2541313002: RenderTextHarfBuzz: Add support for multi line text selection. (Closed)
Patch Set: Tests 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 at least contain some information regarding the
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());
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 = 0;
837 }
838 if (line_index >= static_cast<int>(lines().size())) {
839 if (RenderText::kDragToEndIfOutsideVerticalBounds)
840 return EdgeSelectionModel(GetVisualDirectionOfLogicalEnd());
841 else
842 line_index = lines().size() - 1;
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_index, CURSOR_LEFT);
852 if (segment_index >= static_cast<int>(line.segments.size()))
853 return LineSelectionModel(line_index, CURSOR_RIGHT);
854 const internal::LineSegment& segment = line.segments[segment_index];
855
856 const internal::TextRunHarfBuzz& run = *GetRunList()->runs()[segment.run];
857 const size_t segment_min_glyph_index =
858 run.CharRangeToGlyphRange(segment.char_range).GetMin();
859 const float segment_offset_relative_run =
860 segment_min_glyph_index != 0
861 ? SkScalarToFloat(run.positions[segment_min_glyph_index].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_index, 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/13 03:04:55 nit: I think nesting a DCHECK in an if statement w
karandeepb 2016/12/16 02:58:38 Done.
1025 1066
1026 // Add a Range for each run/selection intersection. 1067 float line_x = 0;
1027 for (size_t i = 0; i < run_list->size(); ++i) { 1068 for (const internal::LineSegment& segment : line.segments) {
1028 internal::TextRunHarfBuzz* run = 1069 const internal::TextRunHarfBuzz& run = *run_list->runs()[segment.run];
1029 run_list->runs()[run_list->visual_to_logical(i)]; 1070 const Range intersection = segment.char_range.Intersect(layout_range);
1030 Range intersection = run->range.Intersect(layout_range); 1071 DCHECK(!intersection.is_reversed());
1031 if (!intersection.IsValid()) 1072 if (!intersection.is_empty()) {
1032 continue; 1073 float width =
1033 DCHECK(!intersection.is_reversed()); 1074 SkScalarToFloat(run.GetGlyphWidthForCharRange(intersection));
1034 const size_t left_index = 1075 float x = line_x;
1035 run->is_rtl ? intersection.end() - 1 : intersection.start(); 1076 if (run.is_rtl) {
1036 const Range leftmost_character_x = 1077 x += SkScalarToFloat(run.GetGlyphWidthForCharRange(
1037 run->GetGraphemeBounds(this, left_index).Round(); 1078 gfx::Range(intersection.end(), segment.char_range.end())));
1038 const size_t right_index = 1079 } else {
1039 run->is_rtl ? intersection.start() : intersection.end() - 1; 1080 x += SkScalarToFloat(run.GetGlyphWidthForCharRange(
1040 const Range rightmost_character_x = 1081 gfx::Range(segment.char_range.start(), intersection.start())));
1041 run->GetGraphemeBounds(this, right_index).Round(); 1082 }
1042 Range range_x(leftmost_character_x.start(), rightmost_character_x.end()); 1083 int end_x = std::ceil(x + width);
1043 DCHECK(!range_x.is_reversed()); 1084 int start_x = std::ceil(x);
1044 if (range_x.is_empty()) 1085 gfx::Rect rect(start_x, 0, end_x - start_x, line.size.height());
1045 continue; 1086 rects.push_back(rect + GetLineOffset(line_index));
1046 1087 }
1047 // Union this with the last range if they're adjacent. 1088 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 } 1089 }
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 } 1090 }
1059 return rects; 1091 return rects;
1060 } 1092 }
1061 1093
1062 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) { 1094 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) {
1063 return TextIndexToGivenTextIndex(GetDisplayText(), index); 1095 return TextIndexToGivenTextIndex(GetDisplayText(), index);
1064 } 1096 }
1065 1097
1066 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) { 1098 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) {
1067 if (!obscured()) 1099 if (!obscured())
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 LogicalCursorDirection affinity = caret.caret_affinity(); 1254 LogicalCursorDirection affinity = caret.caret_affinity();
1223 internal::TextRunList* run_list = GetRunList(); 1255 internal::TextRunList* run_list = GetRunList();
1224 for (size_t i = 0; i < run_list->size(); ++i) { 1256 for (size_t i = 0; i < run_list->size(); ++i) {
1225 internal::TextRunHarfBuzz* run = run_list->runs()[i]; 1257 internal::TextRunHarfBuzz* run = run_list->runs()[i];
1226 if (RangeContainsCaret(run->range, layout_position, affinity)) 1258 if (RangeContainsCaret(run->range, layout_position, affinity))
1227 return i; 1259 return i;
1228 } 1260 }
1229 return run_list->size(); 1261 return run_list->size();
1230 } 1262 }
1231 1263
1232 size_t RenderTextHarfBuzz::GetRunContainingXCoord(float x, 1264 int RenderTextHarfBuzz::GetLineContainingYCoord(float text_y) {
1233 float* offset) const { 1265 if (text_y < 0)
1234 DCHECK(!update_display_run_list_); 1266 return -1;
1235 const internal::TextRunList* run_list = GetRunList(); 1267
1236 if (x < 0) 1268 for (size_t i = 0; i < lines().size(); i++) {
1237 return run_list->size(); 1269 const internal::Line& line = lines()[i];
1238 // Find the text run containing the argument point (assumed already offset). 1270
1239 float current_x = 0; 1271 if (text_y <= line.size.height())
1240 for (size_t i = 0; i < run_list->size(); ++i) { 1272 return i;
1241 size_t run = run_list->visual_to_logical(i); 1273 text_y -= line.size.height();
1242 current_x += run_list->runs()[run]->width; 1274 }
1243 if (x < current_x) { 1275
1244 *offset = x - (current_x - run_list->runs()[run]->width); 1276 return lines().size();
1245 return run; 1277 }
1278
1279 int RenderTextHarfBuzz::GetLineSegmentContainingXCoord(
1280 const internal::Line& line,
1281 float line_x,
1282 float* offset_relative_segment) {
1283 DCHECK(offset_relative_segment);
1284
1285 *offset_relative_segment = 0;
1286 if (line_x < 0)
1287 return -1;
1288 for (size_t i = 0; i < line.segments.size(); i++) {
1289 const internal::LineSegment& segment = line.segments[i];
1290
1291 // segment.x_range is not used because it is in text space.
1292 if (line_x < segment.width()) {
1293 *offset_relative_segment = line_x;
1294 return i;
1246 } 1295 }
1296 line_x -= segment.width();
1247 } 1297 }
1248 return run_list->size(); 1298 return line.segments.size();
1249 } 1299 }
1250 1300
1251 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( 1301 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun(
1252 const internal::TextRunHarfBuzz* run) { 1302 const internal::TextRunHarfBuzz* run) {
1253 size_t position = DisplayIndexToTextIndex(run->range.start()); 1303 size_t position = DisplayIndexToTextIndex(run->range.start());
1254 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); 1304 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD);
1255 return SelectionModel(position, CURSOR_BACKWARD); 1305 return SelectionModel(position, CURSOR_BACKWARD);
1256 } 1306 }
1257 1307
1258 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( 1308 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun(
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
1626 1676
1627 attribute.strike = run.strike; 1677 attribute.strike = run.strike;
1628 attribute.diagonal_strike = run.diagonal_strike; 1678 attribute.diagonal_strike = run.diagonal_strike;
1629 decorated_text->attributes.push_back(attribute); 1679 decorated_text->attributes.push_back(attribute);
1630 } 1680 }
1631 } 1681 }
1632 return true; 1682 return true;
1633 } 1683 }
1634 1684
1635 } // namespace gfx 1685 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698