Skip to content

Commit

Permalink
[IFC] Move line ending policy logic to InlineFormattingContext::creat…
Browse files Browse the repository at this point in the history
…eDisplayContentForLine

https://bugs.webkit.org/show_bug.cgi?id=251992

Reviewed by Antti Koivisto.

Let's try to keep InlineFormattingContext::lineLayout short and clean.

* Source/WebCore/layout/formattingContexts/inline/InlineFormattingContext.cpp:
(WebCore::Layout::InlineFormattingContext::lineLayout):
(WebCore::Layout::lineEndingEllipsisPolicy):
(WebCore::Layout::InlineFormattingContext::createDisplayContentForLine):
* Source/WebCore/layout/formattingContexts/inline/InlineFormattingContext.h:

Canonical link: https://commits.webkit.org/260093@main
  • Loading branch information
alanbaradlay committed Feb 10, 2023
1 parent 9a95ba9 commit 82d4101
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
Expand Up @@ -203,21 +203,6 @@ static size_t indexOfFirstInlineItemForNextLine(const LineBuilder::LineContent&
return lineContentRange.endIndex();
}

static LineEndingEllipsisPolicy lineEndingEllipsisPolicy(const RenderStyle& rootStyle, size_t numberOfLines, std::optional<size_t> maximumNumberOfVisibleLines)
{
// We may have passed the line-clamp line with overflow visible.
if (maximumNumberOfVisibleLines && numberOfLines < *maximumNumberOfVisibleLines) {
// If the next call to layoutInlineContent() won't produce a line with content (e.g. only floats), we'll end up here again.
auto shouldApplyClampWhenApplicable = *maximumNumberOfVisibleLines - numberOfLines == 1;
if (shouldApplyClampWhenApplicable)
return LineEndingEllipsisPolicy::WhenContentOverflowsInBlockDirection;
}
// Truncation is in effect when the block container has overflow other than visible.
if (rootStyle.overflowX() == Overflow::Hidden && rootStyle.textOverflow() == TextOverflow::Ellipsis)
return LineEndingEllipsisPolicy::WhenContentOverflowsInInlineDirection;
return LineEndingEllipsisPolicy::No;
}

void InlineFormattingContext::lineLayout(InlineItems& inlineItems, const InlineItemRange& needsLayoutRange, const ConstraintsForInlineContent& constraints, BlockLayoutState& blockLayoutState)
{
ASSERT(!needsLayoutRange.isEmpty());
Expand All @@ -226,14 +211,6 @@ void InlineFormattingContext::lineLayout(InlineItems& inlineItems, const InlineI
formattingState.boxes().reserveInitialCapacity(formattingState.inlineItems().size());

auto floatingContext = FloatingContext { *this, blockLayoutState.floatingState() };
auto& rootStyle = root().style();

auto maximumNumberOfVisibleLinesForThisInlineContent = [&] () -> std::optional<size_t> {
if (auto lineClamp = blockLayoutState.lineClamp())
return lineClamp->maximumNumberOfLines - lineClamp->numberOfVisibleLines;
return { };
}();
size_t numberOfLines = 0;

auto lineLogicalTop = InlineLayoutUnit { constraints.logicalTop() };
auto previousLine = std::optional<LineBuilder::PreviousLine> { };
Expand All @@ -246,8 +223,7 @@ void InlineFormattingContext::lineLayout(InlineItems& inlineItems, const InlineI
auto lineInitialRect = InlineRect { lineLogicalTop, constraints.horizontal().logicalLeft, constraints.horizontal().logicalWidth, formattingGeometry().initialLineHeight(!previousLine.has_value()) };
auto lineContent = lineBuilder.layoutInlineContent({ { firstInlineItemNeedsLayout, needsLayoutRange.endIndex() }, lineInitialRect }, previousLine);

auto ellipsisPolicy = lineEndingEllipsisPolicy(rootStyle, numberOfLines, maximumNumberOfVisibleLinesForThisInlineContent);
auto lineLogicalRect = createDisplayContentForLine(lineContent, constraints, ellipsisPolicy, blockLayoutState);
auto lineLogicalRect = createDisplayContentForLine(lineContent, constraints, blockLayoutState);
if (lineContent.isLastLineWithInlineContent)
formattingState.setClearGapAfterLastLine(formattingGeometry().logicalTopForNextLine(lineContent, lineLogicalRect, floatingContext) - lineLogicalRect.bottom());

Expand All @@ -256,8 +232,6 @@ void InlineFormattingContext::lineLayout(InlineItems& inlineItems, const InlineI
if (isLastLine)
break;

if (!lineContent.runs.isEmpty() && lineContent.contentLogicalWidth)
++numberOfLines;
lineLogicalTop = formattingGeometry().logicalTopForNextLine(lineContent, lineLogicalRect, floatingContext);
previousLine = LineBuilder::PreviousLine { !lineContent.runs.isEmpty() && lineContent.runs.last().isLineBreak(), lineContent.inlineBaseDirection, lineContent.partialOverflowingContent, WTFMove(lineContent.overflowingFloats), lineContent.trailingOverflowingContentWidth };
previousLineLastInlineItemIndex = lineContent.inlineItemRange.endIndex();
Expand Down Expand Up @@ -458,15 +432,36 @@ void InlineFormattingContext::collectContentIfNeeded()
formattingState.addInlineItems(inlineItemsBuilder.build());
}

InlineRect InlineFormattingContext::createDisplayContentForLine(const LineBuilder::LineContent& lineContent, const ConstraintsForInlineContent& constraints, LineEndingEllipsisPolicy lineEndingEllipsisPolicy, const BlockLayoutState& blockLayoutState)
static LineEndingEllipsisPolicy lineEndingEllipsisPolicy(const RenderStyle& rootStyle, size_t numberOfLines, std::optional<size_t> maximumNumberOfVisibleLines)
{
// We may have passed the line-clamp line with overflow visible.
if (maximumNumberOfVisibleLines && numberOfLines < *maximumNumberOfVisibleLines) {
// If the next call to layoutInlineContent() won't produce a line with content (e.g. only floats), we'll end up here again.
auto shouldApplyClampWhenApplicable = *maximumNumberOfVisibleLines - numberOfLines == 1;
if (shouldApplyClampWhenApplicable)
return LineEndingEllipsisPolicy::WhenContentOverflowsInBlockDirection;
}
// Truncation is in effect when the block container has overflow other than visible.
if (rootStyle.overflowX() == Overflow::Hidden && rootStyle.textOverflow() == TextOverflow::Ellipsis)
return LineEndingEllipsisPolicy::WhenContentOverflowsInInlineDirection;
return LineEndingEllipsisPolicy::No;
}

InlineRect InlineFormattingContext::createDisplayContentForLine(const LineBuilder::LineContent& lineContent, const ConstraintsForInlineContent& constraints, const BlockLayoutState& blockLayoutState)
{
auto& formattingState = this->formattingState();
auto currentLineIndex = formattingState.lines().size();
auto maximumNumberOfVisibleLinesForThisInlineContent = [&] () -> std::optional<size_t> {
if (auto lineClamp = blockLayoutState.lineClamp())
return lineClamp->maximumNumberOfLines - lineClamp->numberOfVisibleLines;
return { };
}();

auto lineBox = LineBoxBuilder { *this, lineContent, blockLayoutState }.build(currentLineIndex);
auto displayLine = InlineDisplayLineBuilder { *this }.build(lineContent, lineBox, constraints);
auto boxes = InlineDisplayContentBuilder { *this, formattingState }.build(lineContent, lineBox, displayLine, currentLineIndex);
if (auto ellipsisRect = InlineDisplayLineBuilder::trailingEllipsisVisualRectAfterTruncation(lineEndingEllipsisPolicy, displayLine, boxes, lineContent.isLastLineWithInlineContent))
auto ellipsisPolicy = lineEndingEllipsisPolicy(root().style(), currentLineIndex, maximumNumberOfVisibleLinesForThisInlineContent);
if (auto ellipsisRect = InlineDisplayLineBuilder::trailingEllipsisVisualRectAfterTruncation(ellipsisPolicy, displayLine, boxes, lineContent.isLastLineWithInlineContent))
displayLine.setEllipsisVisualRect(*ellipsisRect);

formattingState.addBoxes(WTFMove(boxes));
Expand Down
Expand Up @@ -72,7 +72,7 @@ class InlineFormattingContext final : public FormattingContext {
void computeWidthAndMargin(const Box&, const HorizontalConstraints&);

void collectContentIfNeeded();
InlineRect createDisplayContentForLine(const LineBuilder::LineContent&, const ConstraintsForInlineContent&, LineEndingEllipsisPolicy, const BlockLayoutState&);
InlineRect createDisplayContentForLine(const LineBuilder::LineContent&, const ConstraintsForInlineContent&, const BlockLayoutState&);
void resetGeometryForClampedContent(const InlineItemRange& needsDisplayContentRange, const LineBuilder::FloatList& overflowingFloats, LayoutPoint topleft);

void invalidateFormattingState();
Expand Down

0 comments on commit 82d4101

Please sign in to comment.