Commit 6979b134 authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Lift out ExpandRangeToGraphemeBoundary

The lifted function is similar to the ExpandRangeToWordBoundary(...).
It allows to get the enclosing range at grapheme boundaries.

R=pkasting@chromium.org

Bug: 1025561
Change-Id: I4f439b81ac0c1c7e540f67b98c9e3e7f9757b36b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1982714
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727676}
parent 8e86923a
......@@ -1035,7 +1035,7 @@ bool RenderText::IsValidLogicalIndex(size_t index) const {
IsValidCodePointIndex(text(), index));
}
bool RenderText::IsValidCursorIndex(size_t index) {
bool RenderText::IsValidCursorIndex(size_t index) const {
return index == 0 || index == text().length() ||
(IsValidLogicalIndex(index) && IsGraphemeBoundary(index));
}
......@@ -1266,6 +1266,19 @@ base::string16 RenderText::GetTextFromRange(const Range& range) const {
return base::string16();
}
Range RenderText::ExpandRangeToGraphemeBoundary(const Range& range) const {
const auto snap_to_grapheme = [this](auto index, auto direction) {
return IsValidCursorIndex(index)
? index
: IndexOfAdjacentGrapheme(index, direction);
};
const size_t min_index = snap_to_grapheme(range.GetMin(), CURSOR_BACKWARD);
const size_t max_index = snap_to_grapheme(range.GetMax(), CURSOR_FORWARD);
return range.is_reversed() ? Range(max_index, min_index)
: Range(min_index, max_index);
}
bool RenderText::IsNewlineSegment(const internal::LineSegment& segment) const {
return IsNewlineSegment(text_, segment);
}
......
......@@ -488,7 +488,7 @@ class GFX_EXPORT RenderText {
// Returns true if the position is a valid logical index into text(), and is
// also a valid grapheme boundary, which may be used as a cursor position.
bool IsValidCursorIndex(size_t index);
bool IsValidCursorIndex(size_t index) const;
// Get the visual bounds of a cursor at |caret|. These bounds typically
// represent a vertical line if |insert_mode| is true. Pass false for
......@@ -586,6 +586,10 @@ class GFX_EXPORT RenderText {
// line if the |caret| exceeds the text length.
virtual size_t GetLineContainingCaret(const SelectionModel& caret) = 0;
// Expands |range| to its nearest grapheme boundaries and returns the
// resulting range. Maintains directionality of |range|.
Range ExpandRangeToGraphemeBoundary(const Range& range) const;
protected:
RenderText();
......
......@@ -1396,16 +1396,9 @@ std::vector<Rect> RenderTextHarfBuzz::GetSubstringBounds(const Range& range) {
EnsureLayout();
DCHECK(!update_display_run_list_);
DCHECK(Range(0, text().length()).Contains(range));
const size_t start =
IsValidCursorIndex(range.GetMin())
? range.GetMin()
: IndexOfAdjacentGrapheme(range.GetMin(), CURSOR_BACKWARD);
const size_t end =
IsValidCursorIndex(range.GetMax())
? range.GetMax()
: IndexOfAdjacentGrapheme(range.GetMax(), CURSOR_FORWARD);
const Range display_range(TextIndexToDisplayIndex(start),
TextIndexToDisplayIndex(end));
const Range grapheme_range = ExpandRangeToGraphemeBoundary(range);
const Range display_range(TextIndexToDisplayIndex(grapheme_range.start()),
TextIndexToDisplayIndex(grapheme_range.end()));
DCHECK(Range(0, GetDisplayText().length()).Contains(display_range));
std::vector<Rect> rects;
......@@ -1698,17 +1691,10 @@ void RenderTextHarfBuzz::DrawVisualText(internal::SkiaTextRenderer* renderer,
// Apply the selected text color to the [un-reversed] selection range.
BreakList<SkColor> colors = layout_colors();
if (!selection.is_empty()) {
const size_t grapheme_start =
IsGraphemeBoundary(selection.GetMin())
? selection.GetMin()
: IndexOfAdjacentGrapheme(selection.GetMin(), CURSOR_BACKWARD);
const size_t grapheme_end =
IsGraphemeBoundary(selection.GetMax())
? selection.GetMax()
: IndexOfAdjacentGrapheme(selection.GetMax(), CURSOR_FORWARD);
const Range grapheme_range = ExpandRangeToGraphemeBoundary(selection);
colors.ApplyValue(selection_color(),
Range(TextIndexToDisplayIndex(grapheme_start),
TextIndexToDisplayIndex(grapheme_end)));
Range(TextIndexToDisplayIndex(grapheme_range.GetMin()),
TextIndexToDisplayIndex(grapheme_range.GetMax())));
}
internal::TextRunList* run_list = GetRunList();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment