Commit b5205c1e authored by Koji Ishii's avatar Koji Ishii Committed by Commit Bot

[LayoutNG] Refactor the usage of GetCharacterRange in NGCaretRect

This patch refactors inefficient usage of GetCharacterRange
as discussed in CL:979732.

Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I1d1b135eaf8d7a12f0fd918316428d5bf30cbcc0
Reviewed-on: https://chromium-review.googlesource.com/981848Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Commit-Queue: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547079}
parent 6ccb0369
......@@ -264,17 +264,9 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
is_horizontal ? fragment.Size().height : fragment.Size().width;
LayoutUnit caret_top;
LayoutUnit caret_left;
if (!fragment.IsLineBreak()) {
unsigned offset_in_fragment = offset - fragment.StartOffset();
const ShapeResult* shape_result = fragment.TextShapeResult();
DCHECK(shape_result);
CharacterRange character_range =
shape_result->GetCharacterRange(offset_in_fragment, offset_in_fragment);
LayoutUnit caret_center = LayoutUnit(character_range.start);
caret_left = caret_center - caret_width / 2;
}
LayoutUnit caret_left = fragment.InlinePositionForOffset(offset);
if (!fragment.IsLineBreak())
caret_left -= caret_width / 2;
if (!is_horizontal) {
std::swap(caret_top, caret_left);
......
......@@ -15,6 +15,24 @@
namespace blink {
LayoutUnit NGPhysicalTextFragment::InlinePositionForOffset(
unsigned offset) const {
DCHECK_GE(offset, start_offset_);
DCHECK_LE(offset, end_offset_);
offset -= start_offset_;
if (shape_result_) {
return LayoutUnit::FromFloatRound(shape_result_->PositionForOffset(offset));
}
// All non-flow controls have ShapeResult.
DCHECK(IsFlowControl());
DCHECK_EQ(1u, Length());
if (!offset || UNLIKELY(IsRtl(Style().Direction())))
return LayoutUnit();
return IsHorizontal() ? Size().width : Size().height;
}
NGPhysicalOffsetRect NGPhysicalTextFragment::LocalRect(
unsigned start_offset,
unsigned end_offset) const {
......
......@@ -105,6 +105,10 @@ class CORE_EXPORT NGPhysicalTextFragment final : public NGPhysicalFragment {
return IsHorizontal() ? kAlphabeticBaseline : kIdeographicBaseline;
}
// Compute the inline position from text offset, in logical coordinate
// relative to this fragment.
LayoutUnit InlinePositionForOffset(unsigned offset) const;
// The layout box of text in (start, end) range in local coordinate.
// Start and end offsets must be between StartOffset() and EndOffset().
NGPhysicalOffsetRect LocalRect(unsigned start_offset,
......
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