Commit 069ed69f authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Move NGCaretNavigator::GetItem() to NGInlineItemsData

The function is a general function for probing NGInlineItemsData. Hence,
this patch moves it to NGInlineItemsData, where it better resides.

This patch follows from kojii's comments in crrev.com/c/1436486

Bug: 894651
Change-Id: Icdc625c0dbddb219d484e987f4ea02076045a4c8
Reviewed-on: https://chromium-review.googlesource.com/c/1440501
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Auto-Submit: Xiaocheng Hu <xiaochengh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626790}
parent 904e1792
......@@ -33,26 +33,11 @@ bool NGCaretNavigator::IsBidiEnabled() const {
return GetData().IsBidiEnabled();
}
const NGInlineItem& NGCaretNavigator::GetItem(unsigned index) const {
const auto& items = GetData().items;
const NGInlineItem* item =
std::lower_bound(items.begin(), items.end(), index,
[](const NGInlineItem& item, unsigned index) {
if (item.StartOffset() > index)
return false;
return item.EndOffset() <= index;
});
DCHECK_NE(item, items.end());
DCHECK_LE(item->StartOffset(), index);
DCHECK_LT(index, item->EndOffset());
return *item;
}
UBiDiLevel NGCaretNavigator::BidiLevelAt(unsigned index) const {
DCHECK_LT(index, GetText().length());
if (!IsBidiEnabled())
return 0;
return GetItem(index).BidiLevel();
return GetData().FindItemForTextOffset(index).BidiLevel();
}
TextDirection NGCaretNavigator::TextDirectionAt(unsigned index) const {
......
......@@ -275,4 +275,20 @@ void NGInlineItem::SetEndCollapseType(NGCollapseType type, bool is_newline) {
is_end_collapsible_newline_ = is_newline;
}
const NGInlineItem& NGInlineItemsData::FindItemForTextOffset(
unsigned offset) const {
DCHECK_LT(offset, text_content.length());
const NGInlineItem* item =
std::lower_bound(items.begin(), items.end(), offset,
[](const NGInlineItem& item, unsigned offset) {
if (item.StartOffset() > offset)
return false;
return item.EndOffset() <= offset;
});
DCHECK_NE(item, items.end());
DCHECK_LE(item->StartOffset(), offset);
DCHECK_LT(offset, item->EndOffset());
return *item;
}
} // namespace blink
......@@ -224,6 +224,11 @@ struct CORE_EXPORT NGInlineItemsData {
void AssertEndOffset(unsigned index, unsigned offset) const {
items[index].AssertEndOffset(offset);
}
// Returns the non-zero-length inline item whose |StartOffset() <= offset| and
// |EndOffset() > offset|, namely, contains the character at |offset|.
// Note: This function is not a trivial getter, but does a binary search.
const NGInlineItem& FindItemForTextOffset(unsigned offset) const;
};
} // namespace blink
......
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