OLD | NEW |
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 Loading... |
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 |
| 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 Loading... |
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++; |
| 837 } |
| 838 if (line_index >= (int)lines().size()) { |
| 839 if (RenderText::kDragToEndIfOutsideVerticalBounds) |
| 840 return EdgeSelectionModel(GetVisualDirectionOfLogicalEnd()); |
| 841 else |
| 842 line_index--; |
| 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()) |
| 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 = |
| 858 run.CharRangeToGlyphRange(segment.char_range); |
| 859 const float segment_offset_relative_run = |
| 860 segment_glyph_range.GetMin() != 0 |
| 861 ? 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 SkScalar end = |
831 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); | 868 i + 1 == run.glyph_count ? run.width : run.positions[i + 1].x(); |
832 const SkScalar middle = (end + run.positions[i].x()) / 2; | 869 const SkScalar middle = (end + run.positions[i].x()) / 2; |
833 | 870 |
834 if (offset < middle) { | 871 if (point_offset_relative_run < middle) { |
835 return SelectionModel(DisplayIndexToTextIndex( | 872 return SelectionModel(DisplayIndexToTextIndex( |
836 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)), | 873 run.glyph_to_char[i] + (run.is_rtl ? 1 : 0)), |
837 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD)); | 874 (run.is_rtl ? CURSOR_BACKWARD : CURSOR_FORWARD)); |
838 } | 875 } |
839 if (offset < end) { | 876 if (point_offset_relative_run < end) { |
840 return SelectionModel(DisplayIndexToTextIndex( | 877 return SelectionModel(DisplayIndexToTextIndex( |
841 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)), | 878 run.glyph_to_char[i] + (run.is_rtl ? 0 : 1)), |
842 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD)); | 879 (run.is_rtl ? CURSOR_FORWARD : CURSOR_BACKWARD)); |
843 } | 880 } |
844 } | 881 } |
845 return EdgeSelectionModel(CURSOR_RIGHT); | 882 |
| 883 return LineSelectionModel(line, CURSOR_RIGHT); |
846 } | 884 } |
847 | 885 |
848 bool RenderTextHarfBuzz::IsSelectionSupported() const { | 886 bool RenderTextHarfBuzz::IsSelectionSupported() const { |
849 // TODO(karandeepb): Support multi-line text selection. | 887 return true; |
850 return !multiline(); | |
851 } | 888 } |
852 | 889 |
853 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { | 890 std::vector<RenderText::FontSpan> RenderTextHarfBuzz::GetFontSpansForTesting() { |
854 EnsureLayout(); | 891 EnsureLayout(); |
855 | 892 |
856 internal::TextRunList* run_list = GetRunList(); | 893 internal::TextRunList* run_list = GetRunList(); |
857 std::vector<RenderText::FontSpan> spans; | 894 std::vector<RenderText::FontSpan> spans; |
858 for (auto* run : run_list->runs()) { | 895 for (auto* run : run_list->runs()) { |
859 spans.push_back(RenderText::FontSpan( | 896 spans.push_back(RenderText::FontSpan( |
860 run->font, Range(DisplayIndexToTextIndex(run->range.start()), | 897 run->font, Range(DisplayIndexToTextIndex(run->range.start()), |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1012 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { | 1049 std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) { |
1013 DCHECK(!update_display_run_list_); | 1050 DCHECK(!update_display_run_list_); |
1014 DCHECK(Range(0, text().length()).Contains(range)); | 1051 DCHECK(Range(0, text().length()).Contains(range)); |
1015 Range layout_range(TextIndexToDisplayIndex(range.start()), | 1052 Range layout_range(TextIndexToDisplayIndex(range.start()), |
1016 TextIndexToDisplayIndex(range.end())); | 1053 TextIndexToDisplayIndex(range.end())); |
1017 DCHECK(Range(0, GetDisplayText().length()).Contains(layout_range)); | 1054 DCHECK(Range(0, GetDisplayText().length()).Contains(layout_range)); |
1018 | 1055 |
1019 std::vector<Rect> rects; | 1056 std::vector<Rect> rects; |
1020 if (layout_range.is_empty()) | 1057 if (layout_range.is_empty()) |
1021 return rects; | 1058 return rects; |
1022 std::vector<Range> bounds; | |
1023 | 1059 |
1024 internal::TextRunList* run_list = GetRunList(); | 1060 internal::TextRunList* run_list = GetRunList(); |
| 1061 for (size_t line_index = 0; line_index < lines().size(); ++line_index) { |
| 1062 const internal::Line& line = lines()[line_index]; |
| 1063 if (line.segments.empty()) { |
| 1064 DCHECK_EQ(lines().size() - 1, line_index); |
| 1065 continue; |
| 1066 } |
1025 | 1067 |
1026 // Add a Range for each run/selection intersection. | 1068 float line_x = 0; |
1027 for (size_t i = 0; i < run_list->size(); ++i) { | 1069 for (const internal::LineSegment& segment : line.segments) { |
1028 internal::TextRunHarfBuzz* run = | 1070 const internal::TextRunHarfBuzz& run = *run_list->runs()[segment.run]; |
1029 run_list->runs()[run_list->visual_to_logical(i)]; | 1071 const Range intersection = segment.char_range.Intersect(layout_range); |
1030 Range intersection = run->range.Intersect(layout_range); | 1072 DCHECK(!intersection.is_reversed()); |
1031 if (!intersection.IsValid()) | 1073 if (!intersection.is_empty()) { |
1032 continue; | 1074 float width = |
1033 DCHECK(!intersection.is_reversed()); | 1075 SkScalarToFloat(run.GetGlyphWidthForCharRange(intersection)); |
1034 const size_t left_index = | 1076 float x; |
1035 run->is_rtl ? intersection.end() - 1 : intersection.start(); | 1077 if (run.is_rtl) { |
1036 const Range leftmost_character_x = | 1078 x = line_x + SkScalarToFloat(run.GetGlyphWidthForCharRange(gfx::Range( |
1037 run->GetGraphemeBounds(this, left_index).Round(); | 1079 intersection.end(), segment.char_range.end()))); |
1038 const size_t right_index = | 1080 } else { |
1039 run->is_rtl ? intersection.start() : intersection.end() - 1; | 1081 x = line_x + SkScalarToFloat(run.GetGlyphWidthForCharRange(gfx::Range( |
1040 const Range rightmost_character_x = | 1082 segment.char_range.start(), intersection.start()))); |
1041 run->GetGraphemeBounds(this, right_index).Round(); | 1083 } |
1042 Range range_x(leftmost_character_x.start(), rightmost_character_x.end()); | 1084 int end_x = std::ceil(x + width); |
1043 DCHECK(!range_x.is_reversed()); | 1085 int start_x = std::ceil(x); |
1044 if (range_x.is_empty()) | 1086 gfx::Rect rect(start_x, 0, end_x - start_x, line.size.height()); |
1045 continue; | 1087 rects.push_back(rect + GetLineOffset(line_index)); |
1046 | 1088 } |
1047 // Union this with the last range if they're adjacent. | 1089 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 } | 1090 } |
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 } | 1091 } |
1059 return rects; | 1092 return rects; |
1060 } | 1093 } |
1061 | 1094 |
1062 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) { | 1095 size_t RenderTextHarfBuzz::TextIndexToDisplayIndex(size_t index) { |
1063 return TextIndexToGivenTextIndex(GetDisplayText(), index); | 1096 return TextIndexToGivenTextIndex(GetDisplayText(), index); |
1064 } | 1097 } |
1065 | 1098 |
1066 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) { | 1099 size_t RenderTextHarfBuzz::DisplayIndexToTextIndex(size_t index) { |
1067 if (!obscured()) | 1100 if (!obscured()) |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1222 LogicalCursorDirection affinity = caret.caret_affinity(); | 1255 LogicalCursorDirection affinity = caret.caret_affinity(); |
1223 internal::TextRunList* run_list = GetRunList(); | 1256 internal::TextRunList* run_list = GetRunList(); |
1224 for (size_t i = 0; i < run_list->size(); ++i) { | 1257 for (size_t i = 0; i < run_list->size(); ++i) { |
1225 internal::TextRunHarfBuzz* run = run_list->runs()[i]; | 1258 internal::TextRunHarfBuzz* run = run_list->runs()[i]; |
1226 if (RangeContainsCaret(run->range, layout_position, affinity)) | 1259 if (RangeContainsCaret(run->range, layout_position, affinity)) |
1227 return i; | 1260 return i; |
1228 } | 1261 } |
1229 return run_list->size(); | 1262 return run_list->size(); |
1230 } | 1263 } |
1231 | 1264 |
1232 size_t RenderTextHarfBuzz::GetRunContainingXCoord(float x, | 1265 int RenderTextHarfBuzz::GetLineContainingYCoord(float text_y) { |
1233 float* offset) const { | 1266 if (text_y < 0) |
1234 DCHECK(!update_display_run_list_); | 1267 return -1; |
1235 const internal::TextRunList* run_list = GetRunList(); | 1268 |
1236 if (x < 0) | 1269 for (size_t i = 0; i < lines().size(); i++) { |
1237 return run_list->size(); | 1270 const internal::Line& line = lines()[i]; |
1238 // Find the text run containing the argument point (assumed already offset). | 1271 |
1239 float current_x = 0; | 1272 if (text_y <= line.size.height()) |
1240 for (size_t i = 0; i < run_list->size(); ++i) { | 1273 return i; |
1241 size_t run = run_list->visual_to_logical(i); | 1274 text_y -= line.size.height(); |
1242 current_x += run_list->runs()[run]->width; | 1275 } |
1243 if (x < current_x) { | 1276 |
1244 *offset = x - (current_x - run_list->runs()[run]->width); | 1277 return lines().size(); |
1245 return run; | 1278 } |
| 1279 |
| 1280 int RenderTextHarfBuzz::GetLineSegmentContainingXCoord( |
| 1281 const internal::Line& line, |
| 1282 float line_x, |
| 1283 float* offset_relative_segment) { |
| 1284 DCHECK(offset_relative_segment); |
| 1285 |
| 1286 *offset_relative_segment = 0; |
| 1287 if (line_x < 0) |
| 1288 return -1; |
| 1289 for (size_t i = 0; i < line.segments.size(); i++) { |
| 1290 const internal::LineSegment& segment = line.segments[i]; |
| 1291 |
| 1292 // segment.x_range is not used because it is text space. |
| 1293 if (line_x <= segment.width()) { |
| 1294 *offset_relative_segment = line_x; |
| 1295 return i; |
1246 } | 1296 } |
| 1297 line_x -= segment.width(); |
1247 } | 1298 } |
1248 return run_list->size(); | 1299 return line.segments.size(); |
1249 } | 1300 } |
1250 | 1301 |
1251 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( | 1302 SelectionModel RenderTextHarfBuzz::FirstSelectionModelInsideRun( |
1252 const internal::TextRunHarfBuzz* run) { | 1303 const internal::TextRunHarfBuzz* run) { |
1253 size_t position = DisplayIndexToTextIndex(run->range.start()); | 1304 size_t position = DisplayIndexToTextIndex(run->range.start()); |
1254 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); | 1305 position = IndexOfAdjacentGrapheme(position, CURSOR_FORWARD); |
1255 return SelectionModel(position, CURSOR_BACKWARD); | 1306 return SelectionModel(position, CURSOR_BACKWARD); |
1256 } | 1307 } |
1257 | 1308 |
1258 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( | 1309 SelectionModel RenderTextHarfBuzz::LastSelectionModelInsideRun( |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1626 | 1677 |
1627 attribute.strike = run.strike; | 1678 attribute.strike = run.strike; |
1628 attribute.diagonal_strike = run.diagonal_strike; | 1679 attribute.diagonal_strike = run.diagonal_strike; |
1629 decorated_text->attributes.push_back(attribute); | 1680 decorated_text->attributes.push_back(attribute); |
1630 } | 1681 } |
1631 } | 1682 } |
1632 return true; | 1683 return true; |
1633 } | 1684 } |
1634 | 1685 |
1635 } // namespace gfx | 1686 } // namespace gfx |
OLD | NEW |