Commit 0720948f authored by Xiaocheng Hu's avatar Xiaocheng Hu Committed by Commit Bot

[LayoutNG] Refactor ng_caret_rect.h/cc

This patch refactors functions in ng_caret_rect.h/cc so that:

1. Functions stop passing around the containing NG inline formatting context.

2. Part of ComputeNGLocalCaretRect() is wrapped into a new overload of
   ComputeNGCaretPosition() taking PositionWithAffinity parameter.

This is a preparation patch for implementing NG line-related functions
in crrev.com/c/1008631

Bug: 822575
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_layout_tests_layout_ng
Change-Id: I9ee8d90b327fec9684c0bdbf52bee6dad6fafb7f
Reviewed-on: https://chromium-review.googlesource.com/1011217
Commit-Queue: Xiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551109}
parent f1c92daa
......@@ -106,9 +106,8 @@ LocalCaretRect LocalCaretRectOfPositionTemplate(
ComputeInlineAdjustedPosition(position);
if (adjusted.IsNotNull()) {
if (const LayoutBlockFlow* context =
NGInlineFormattingContextOf(adjusted.GetPosition()))
return ComputeNGLocalCaretRect(*context, adjusted);
if (NGInlineFormattingContextOf(adjusted.GetPosition()))
return ComputeNGLocalCaretRect(adjusted);
// TODO(editing-dev): This DCHECK is for ensuring the correctness of
// breaking |ComputeInlineBoxPosition| into |ComputeInlineAdjustedPosition|
......@@ -158,12 +157,11 @@ LocalCaretRect LocalSelectionRectOfPositionTemplate(
if (adjusted.IsNull())
return LocalCaretRect();
if (const LayoutBlockFlow* context =
NGInlineFormattingContextOf(adjusted.GetPosition())) {
if (NGInlineFormattingContextOf(adjusted.GetPosition())) {
// TODO(editing-dev): Use selection height instead of caret height, or
// decide if we need to keep the distinction between caret height and
// selection height in NG.
return ComputeNGLocalCaretRect(*context, adjusted);
return ComputeNGLocalCaretRect(adjusted);
}
// TODO(editing-dev): This DCHECK is for ensuring the correctness of
......
......@@ -18,10 +18,8 @@ const LayoutBlockFlow* NGInlineFormattingContextOf(
}
LocalCaretRect ComputeNGLocalCaretRect(
const LayoutBlockFlow& context,
const PositionInFlatTreeWithAffinity& position) {
return ComputeNGLocalCaretRect(context,
ToPositionInDOMTreeWithAffinity(position));
return ComputeNGLocalCaretRect(ToPositionInDOMTreeWithAffinity(position));
}
} // namespace blink
......@@ -18,8 +18,7 @@ struct LocalCaretRect;
const LayoutBlockFlow* NGInlineFormattingContextOf(const PositionInFlatTree&);
LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow&,
const PositionInFlatTreeWithAffinity&);
LocalCaretRect ComputeNGLocalCaretRect(const PositionInFlatTreeWithAffinity&);
} // namespace blink
......
......@@ -185,7 +185,6 @@ CaretPositionResolution TryResolveCaretPositionWithFragment(
// Helpers for converting NGCaretPositions to caret rects.
NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide(
const LayoutBlockFlow& context,
const NGPaintFragment& fragment,
NGCaretPositionType position_type) {
const bool is_horizontal = fragment.Style().IsHorizontalWritingMode();
......@@ -223,7 +222,6 @@ NGPhysicalOffsetRect ComputeLocalCaretRectByBoxSide(
}
NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
const LayoutBlockFlow& context,
const NGPaintFragment& paint_fragment,
unsigned offset) {
const NGPhysicalTextFragment& fragment =
......@@ -255,7 +253,8 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
paint_fragment.InlineOffsetToContainerBox();
NGPhysicalSize caret_size(caret_width, caret_height);
const NGPhysicalBoxFragment& context_fragment = *context.CurrentFragment();
const NGPaintFragment& context_fragment =
*NGPaintFragment::GetForInlineContainer(fragment.GetLayoutObject());
const NGPaintFragment* line_box = paint_fragment.ContainerLineBox();
const NGPhysicalOffset line_box_offset =
line_box->InlineOffsetToContainerBox();
......@@ -284,8 +283,7 @@ NGPhysicalOffsetRect ComputeLocalCaretRectAtTextOffset(
return NGPhysicalOffsetRect(caret_location, caret_size);
}
LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context,
const NGCaretPosition& caret_position) {
LocalCaretRect ComputeLocalCaretRect(const NGCaretPosition& caret_position) {
if (caret_position.IsNull())
return LocalCaretRect();
......@@ -294,7 +292,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context,
case NGCaretPositionType::kAfterBox: {
DCHECK(caret_position.fragment->PhysicalFragment().IsBox());
const NGPhysicalOffsetRect fragment_local_rect =
ComputeLocalCaretRectByBoxSide(context, *caret_position.fragment,
ComputeLocalCaretRectByBoxSide(*caret_position.fragment,
caret_position.position_type);
return {caret_position.fragment->GetLayoutObject(),
fragment_local_rect.ToLayoutRect()};
......@@ -303,7 +301,7 @@ LocalCaretRect ComputeLocalCaretRect(const LayoutBlockFlow& context,
DCHECK(caret_position.fragment->PhysicalFragment().IsText());
DCHECK(caret_position.text_offset.has_value());
const NGPhysicalOffsetRect caret_rect = ComputeLocalCaretRectAtTextOffset(
context, *caret_position.fragment, *caret_position.text_offset);
*caret_position.fragment, *caret_position.text_offset);
return {caret_position.fragment->GetLayoutObject(),
caret_rect.ToLayoutRect()};
......@@ -361,24 +359,30 @@ NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow& context,
return candidate;
}
LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow& context,
const PositionWithAffinity& position) {
NGCaretPosition ComputeNGCaretPosition(const PositionWithAffinity& position) {
AssertValidPositionForCaretRectComputation(position);
DCHECK_EQ(&context, NGInlineFormattingContextOf(position.GetPosition()));
const NGOffsetMapping* mapping = NGOffsetMapping::GetFor(&context);
const LayoutBlockFlow* context =
NGInlineFormattingContextOf(position.GetPosition());
if (!context)
return NGCaretPosition();
const NGOffsetMapping* mapping = NGOffsetMapping::GetFor(context);
DCHECK(mapping);
const Optional<unsigned> maybe_offset =
mapping->GetTextContentOffset(position.GetPosition());
if (!maybe_offset.has_value()) {
// TODO(xiaochengh): Investigate if we reach here.
NOTREACHED();
return LocalCaretRect();
return NGCaretPosition();
}
const unsigned offset = maybe_offset.value();
const TextAffinity affinity = position.Affinity();
return ComputeLocalCaretRect(
context, ComputeNGCaretPosition(context, offset, affinity));
return ComputeNGCaretPosition(*context, offset, affinity);
}
LocalCaretRect ComputeNGLocalCaretRect(const PositionWithAffinity& position) {
return ComputeLocalCaretRect(ComputeNGCaretPosition(position));
}
} // namespace blink
......@@ -18,11 +18,11 @@ class NGPaintFragment;
class LayoutBlockFlow;
struct LocalCaretRect;
// Given an inline formatting context and a position in the context, returns the
// caret rect if a caret should be placed at the position, with the given
// affinity. The caret rect location is local to the given formatting context.
CORE_EXPORT LocalCaretRect ComputeNGLocalCaretRect(const LayoutBlockFlow&,
const PositionWithAffinity&);
// Given a position with affinity, returns the caret rect if the position is
// laid out with LayoutNG, and a caret can be placed at the position with the
// given affinity. The caret rect location is local to the containing inline
// formatting context.
CORE_EXPORT LocalCaretRect ComputeNGLocalCaretRect(const PositionWithAffinity&);
// An NGCaretPosition indicates a caret position relative to an inline
// NGPaintFragment:
......@@ -53,6 +53,10 @@ CORE_EXPORT NGCaretPosition ComputeNGCaretPosition(const LayoutBlockFlow&,
unsigned,
TextAffinity);
// Shorthand of the above when the input is a position instead of a
// (context, offset) pair.
NGCaretPosition ComputeNGCaretPosition(const PositionWithAffinity&);
} // namespace blink
#endif // NGCaretRect_h
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