File: InlineDisplayLineBuilder.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (499 lines) | stat: -rw-r--r-- 27,071 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * Copyright (C) 2021 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "InlineDisplayLineBuilder.h"

#include "InlineDisplayContentBuilder.h"
#include "LayoutBoxGeometry.h"
#include "RenderStyleInlines.h"
#include "TextUtil.h"

namespace WebCore {
namespace Layout {

static InlineRect mapLineRectLogicalToVisual(const InlineRect& lineLogicalRect, const LayoutSize containerRenderSize, WritingMode writingMode)
{
    if (writingMode.isHorizontal())
        return lineLogicalRect;
    if (writingMode.isLogicalLeftLineLeft())
        return { lineLogicalRect.left(), lineLogicalRect.top(), lineLogicalRect.height(), lineLogicalRect.width() };

    return {
        containerRenderSize.height() - lineLogicalRect.right(),
        lineLogicalRect.top(),
        lineLogicalRect.height(),
        lineLogicalRect.width()
    };
}

InlineDisplayLineBuilder::InlineDisplayLineBuilder(InlineFormattingContext& inlineFormattingContext, const ConstraintsForInlineContent& constraints)
    : m_inlineFormattingContext(inlineFormattingContext)
    , m_constraints(constraints)
{
}

InlineDisplayLineBuilder::EnclosingLineGeometry InlineDisplayLineBuilder::collectEnclosingLineGeometry(const LineLayoutResult& lineLayoutResult, const LineBox& lineBox, const InlineRect& lineBoxRect) const
{
    auto& rootInlineBox = lineBox.rootInlineBox();
    auto initialEnclosingTopAndBottom = [&]() -> std::tuple<std::optional<InlineLayoutUnit>, std::optional<InlineLayoutUnit>>  {
        if (!lineBox.hasContent() || !rootInlineBox.hasContent())
            return { };
        return {
            lineBoxRect.top() + rootInlineBox.logicalTop() - rootInlineBox.textEmphasisAbove().value_or(0.f),
            lineBoxRect.top() + rootInlineBox.logicalBottom() + rootInlineBox.textEmphasisBelow().value_or(0.f)
        };
    };
    auto [enclosingTop, enclosingBottom] = initialEnclosingTopAndBottom();
    auto contentOverflowRect = [&]() -> InlineRect {
        auto rect = lineBoxRect;
        auto rootInlineBoxWidth = lineBox.logicalRectForRootInlineBox().width();
        auto isLeftToRightDirection = root().writingMode().isBidiLTR();
        if (lineLayoutResult.hangingContent.shouldContributeToScrollableOverflow)
            rect.expandHorizontally(lineLayoutResult.hangingContent.logicalWidth);
        else if (!isLeftToRightDirection) {
            // This is to balance hanging RTL trailing content. See LineBoxBuilder::build.
            rootInlineBoxWidth -= lineLayoutResult.hangingContent.logicalWidth;
        }
        auto rootInlineBoxHorizontalOverflow = rootInlineBoxWidth - rect.width();
        if (rootInlineBoxHorizontalOverflow > 0)
            isLeftToRightDirection ? rect.shiftRightBy(rootInlineBoxHorizontalOverflow) : rect.shiftLeftBy(-rootInlineBoxHorizontalOverflow);
        return rect;
    }();
    for (auto& inlineLevelBox : lineBox.nonRootInlineLevelBoxes()) {
        if (!inlineLevelBox.isAtomicInlineBox() && !inlineLevelBox.isInlineBox() && !inlineLevelBox.isLineBreakBox())
            continue;

        auto& layoutBox = inlineLevelBox.layoutBox();
        auto borderBox = InlineRect { };

        if (inlineLevelBox.isAtomicInlineBox()) {
            borderBox = lineBox.logicalBorderBoxForAtomicInlineBox(layoutBox, formattingContext().geometryForBox(layoutBox));
            borderBox.moveBy(lineBoxRect.topLeft());
        } else if (inlineLevelBox.isInlineBox()) {
            auto& boxGeometry = formattingContext().geometryForBox(layoutBox);
            // In standards mode, inline boxes always start with an imaginary strut.
            auto isContentful = formattingContext().layoutState().inStandardsMode() || inlineLevelBox.hasContent() || boxGeometry.horizontalBorderAndPadding();
            if (!isContentful)
                continue;
            borderBox = lineBox.logicalBorderBoxForInlineBox(layoutBox, boxGeometry);
            borderBox.moveBy(lineBoxRect.topLeft());
            // Collect scrollable overflow from inline boxes. All other inline level boxes (e.g atomic inline level boxes) stretch the line.
            if (lineBox.hasContent()) {
                // Empty lines (e.g. continuation pre/post blocks) don't expect scrollbar overflow.
                contentOverflowRect.expandVerticallyToContain(borderBox);
            }
        } else if (inlineLevelBox.isLineBreakBox()) {
            borderBox = lineBox.logicalBorderBoxForInlineBox(layoutBox, formattingContext().geometryForBox(layoutBox));
            borderBox.moveBy(lineBoxRect.topLeft());
        } else
            ASSERT_NOT_REACHED();

        auto adjustedBorderBoxTop = borderBox.top() - inlineLevelBox.textEmphasisAbove().value_or(0.f);
        auto adjustedBorderBoxBottom = borderBox.bottom() + inlineLevelBox.textEmphasisBelow().value_or(0.f);
        enclosingTop = std::min(enclosingTop.value_or(adjustedBorderBoxTop), adjustedBorderBoxTop);
        enclosingBottom = std::max(enclosingBottom.value_or(adjustedBorderBoxBottom), adjustedBorderBoxBottom);
    }
    return { { enclosingTop.value_or(lineBoxRect.top()), enclosingBottom.value_or(lineBoxRect.top()) }, contentOverflowRect };
}

InlineDisplay::Line InlineDisplayLineBuilder::build(const LineLayoutResult& lineLayoutResult, const LineBox& lineBox, bool lineIsFullyTruncatedInBlockDirection) const
{
    auto& constraints = this->constraints();
    auto isLeftToRightDirection = lineLayoutResult.directionality.inlineBaseDirection == TextDirection::LTR;

    InlineRect lineBoxLogicalRect = lineBox.logicalRect();
    auto lineBoxLineLeftEdge = isLeftToRightDirection
        ? lineBoxLogicalRect.left()
        : InlineLayoutUnit { constraints.visualLeft() + constraints.horizontal().logicalRight()  } - lineBoxLogicalRect.right();
    lineBoxLogicalRect.setLeft(lineBoxLineLeftEdge);
    auto enclosingLineGeometry = collectEnclosingLineGeometry(lineLayoutResult, lineBox, lineBoxLogicalRect);

    auto& rootInlineBox = lineBox.rootInlineBox();
    auto rootInlineBoxRect = lineBox.logicalRectForRootInlineBox();
    auto contentLineLeftEdge = isLeftToRightDirection
        ? rootInlineBoxRect.left()
        : lineBoxLogicalRect.width() - lineLayoutResult.contentGeometry.logicalRightIncludingNegativeMargin; // Note that with hanging content lineLayoutResult.contentGeometry.logicalRight is not the same as rootLineBoxRect.right().

    auto writingMode = root().writingMode();
    return InlineDisplay::Line { lineBoxLogicalRect
        , mapLineRectLogicalToVisual(lineBoxLogicalRect, constraints.containerRenderSize(), writingMode)
        , mapLineRectLogicalToVisual(enclosingLineGeometry.contentOverflowRect, constraints.containerRenderSize(), writingMode)
        , enclosingLineGeometry.enclosingTopAndBottom
        , rootInlineBox.logicalTop() + rootInlineBox.ascent()
        , lineBox.baselineType()
        , rootInlineBoxRect.left()
        , contentLineLeftEdge
        , rootInlineBox.logicalWidth()
        , isLeftToRightDirection
        , rootInlineBox.layoutBox().writingMode().isHorizontal()
        , lineIsFullyTruncatedInBlockDirection
    };
}

static float truncateTextContentWithMismatchingDirection(InlineDisplay::Box& displayBox, float contentWidth, float availableWidthForContent, bool canFullyTruncate)
{
    // While truncation normally starts at the end of the content and progress backwards, with mismatching direction
    // we take a different approach and truncate content the other way around (i.e. ellipsis follows inline direction truncating the beginning of the content).
    // <div dir=rtl>some long content</div>
    // [...ng content]
    auto& inlineTextBox = downcast<InlineTextBox>(displayBox.layoutBox());
    auto& textContent = displayBox.text();

    auto availableWidthForTruncatedContent = contentWidth - availableWidthForContent;
    auto truncatedSide = TextUtil::breakWord(inlineTextBox, textContent.start(), textContent.length(), contentWidth, availableWidthForTruncatedContent, { }, displayBox.style().fontCascade());

    ASSERT(truncatedSide.length < textContent.length());
    auto visibleLength = textContent.length() - truncatedSide.length;
    auto visibleWidth = contentWidth - truncatedSide.logicalWidth;
    // TextUtil::breakWord never returns overflowing content (left side) which means the right side is normally wider (by one character) than what we need.
    auto visibleContentOverflows = truncatedSide.logicalWidth < availableWidthForTruncatedContent;
    if (!visibleContentOverflows) {
        textContent.setPartiallyVisibleContentLength(visibleLength);
        return visibleWidth;
    }

    auto wouldFullyTruncate = visibleLength <= 1;
    if (wouldFullyTruncate && canFullyTruncate) {
        displayBox.setIsFullyTruncated();
        return { };
    }

    visibleLength = !wouldFullyTruncate ? visibleLength - 1 : 1;
    textContent.setPartiallyVisibleContentLength(visibleLength);
    auto visibleStartPosition = textContent.end() - visibleLength;
    return TextUtil::width(inlineTextBox, displayBox.style().fontCascade(), visibleStartPosition, textContent.end(), { }, TextUtil::UseTrailingWhitespaceMeasuringOptimization::No);
}

static float truncate(InlineDisplay::Box& displayBox, float contentWidth, float availableWidthForContent, bool canFullyTruncate)
{
    if (displayBox.isText()) {
        if (!availableWidthForContent && canFullyTruncate) {
            displayBox.setIsFullyTruncated();
            return { };
        }
        auto contentDirection = displayBox.bidiLevel() % 2 ? TextDirection::RTL : TextDirection::LTR;
        if (displayBox.layoutBox().parent().style().writingMode().bidiDirection() != contentDirection)
            return truncateTextContentWithMismatchingDirection(displayBox, contentWidth, availableWidthForContent, canFullyTruncate);

        auto& inlineTextBox = downcast<InlineTextBox>(displayBox.layoutBox());
        auto& textContent = displayBox.text();
        auto visibleSide = TextUtil::breakWord(inlineTextBox, textContent.start(), textContent.length(), contentWidth, availableWidthForContent, { }, displayBox.style().fontCascade());
        if (visibleSide.length) {
            textContent.setPartiallyVisibleContentLength(visibleSide.length);
            return visibleSide.logicalWidth;
        }
        if (canFullyTruncate) {
            displayBox.setIsFullyTruncated();
            return { };
        }
        auto firstCharacterLength = TextUtil::firstUserPerceivedCharacterLength(inlineTextBox, textContent.start(), textContent.length());
        auto firstCharacterWidth = TextUtil::width(inlineTextBox, displayBox.style().fontCascade(), textContent.start(), textContent.start() + firstCharacterLength, { }, TextUtil::UseTrailingWhitespaceMeasuringOptimization::No);
        textContent.setPartiallyVisibleContentLength(firstCharacterLength);
        return firstCharacterWidth;
    }

    if (canFullyTruncate) {
        // Atomic inline level boxes are never partially truncated.
        displayBox.setIsFullyTruncated();
        return { };
    }
    return contentWidth;
}

static float truncateOverflowingDisplayBoxes(InlineDisplay::Boxes& boxes, size_t startIndex, size_t endIndex, float lineBoxVisualLeft, float lineBoxVisualRight, float ellipsisWidth, const RenderStyle& rootStyle, LineEndingTruncationPolicy lineEndingTruncationPolicy)
{
    ASSERT(endIndex && startIndex <= endIndex);
    // We gotta truncate some runs.
    auto isHorizontal = rootStyle.writingMode().isHorizontal();
    auto left = [&] (auto& displayBox) {
        return isHorizontal ? displayBox.left() : displayBox.top();
    };
    auto right = [&] (auto& displayBox) {
        return isHorizontal ? displayBox.right() : displayBox.bottom();
    };
    auto width = [&] (auto& displayBox) {
        return isHorizontal ? displayBox.width() : displayBox.height();
    };
    // The logically first character or atomic inline-level element on a line must be clipped rather than ellipsed.
    auto isFirstContentRun = true;
    if (rootStyle.writingMode().isBidiLTR()) {
        auto visualRightForContentEnd = std::max(0.f, lineBoxVisualRight - ellipsisWidth);
#if USE_FLOAT_AS_INLINE_LAYOUT_UNIT
        if (visualRightForContentEnd)
            visualRightForContentEnd += LayoutUnit::epsilon();
#endif
        auto truncateRight = std::optional<float> { };
        for (auto index = startIndex; index <= endIndex; ++index) {
            auto& displayBox = boxes[index];
            if (displayBox.isInlineBox())
                continue;
            if (right(displayBox) > visualRightForContentEnd) {
                auto visibleWidth = truncate(displayBox, width(displayBox), std::max(0.f, visualRightForContentEnd - left(displayBox)), !isFirstContentRun);
                auto truncatePosition = left(displayBox) + visibleWidth;
                truncateRight = truncateRight.value_or(truncatePosition);
            }
            isFirstContentRun = false;
        }
        ASSERT_UNUSED(lineEndingTruncationPolicy, lineEndingTruncationPolicy != LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection || truncateRight.has_value() || right(boxes.last()) == visualRightForContentEnd || boxes.last().isInlineBox());
        return truncateRight.value_or(right(boxes.last()));
    }

    auto truncateLeft = std::optional<float> { };
    auto visualLeftForContentEnd = std::max(0.f, lineBoxVisualLeft + ellipsisWidth);
#if USE_FLOAT_AS_INLINE_LAYOUT_UNIT
    if (visualLeftForContentEnd)
        visualLeftForContentEnd -= LayoutUnit::epsilon();
#endif
    for (size_t index = endIndex + 1; index-- > startIndex;) {
        auto& displayBox = boxes[index];
        if (displayBox.isInlineBox())
            continue;
        if (left(displayBox) < visualLeftForContentEnd) {
            auto visibleWidth = truncate(displayBox, width(displayBox), std::max(0.f, right(displayBox) - visualLeftForContentEnd), !isFirstContentRun);
            auto truncatePosition = right(displayBox) - visibleWidth;
            truncateLeft = truncateLeft.value_or(truncatePosition);
        }
        isFirstContentRun = false;
    }

    ASSERT_UNUSED(lineEndingTruncationPolicy, lineEndingTruncationPolicy != LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection || truncateLeft.has_value() || left(boxes.first()) == visualLeftForContentEnd || boxes.first().isInlineBox());
    return truncateLeft.value_or(left(boxes.first())) - ellipsisWidth;
}

static std::optional<FloatRect> trailingEllipsisVisualRectAfterTruncation(LineEndingTruncationPolicy lineEndingTruncationPolicy, String ellipsisText, const InlineDisplay::Line& displayLine, InlineDisplay::Boxes& displayBoxes)
{
    ASSERT(lineEndingTruncationPolicy != LineEndingTruncationPolicy::NoTruncation);
    if (displayBoxes.isEmpty())
        return { };

    auto needsEllipsis = [&] {
        if (lineEndingTruncationPolicy == LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection)
            return displayLine.contentLogicalWidth() && displayLine.contentLogicalWidth() > displayLine.lineBoxLogicalRect().width();
        ASSERT(lineEndingTruncationPolicy == LineEndingTruncationPolicy::WhenContentOverflowsInBlockDirection);
        // We consider even the last line as overflow in block direction due to the propagated nature of line-clamp where clamping is shared
        // across sibling IFCs. If this IFC has the last formatted line (in which case the last line needs no ellipsis) the parent BFC reissues a layout with no clamping.
        return true;
    };
    if (!needsEllipsis())
        return { };

    ASSERT(displayBoxes[0].isRootInlineBox());
    auto& rootInlineBox = displayBoxes[0];
    auto& rootStyle = rootInlineBox.style();
    auto ellipsisWidth = std::max(0.f, rootStyle.fontCascade().width(TextRun { ellipsisText }));

    auto contentNeedsTruncation = [&] {
        switch (lineEndingTruncationPolicy) {
        case LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection:
            ASSERT(displayLine.contentLogicalWidth() > displayLine.lineBoxLogicalRect().width());
            return true;
        case LineEndingTruncationPolicy::WhenContentOverflowsInBlockDirection:
            return displayLine.contentLogicalLeft() + displayLine.contentLogicalWidth() + ellipsisWidth > displayLine.lineBoxLogicalRect().maxX();
        default:
            ASSERT_NOT_REACHED();
            return false;
        }
    };

    auto ellipsisStart = 0.f;
    if (!contentNeedsTruncation()) {
        // The content does not overflow the line box. The ellipsis is supposed to be either visually trailing or leading depending on the inline direction.
        if (displayBoxes.size() > 1)
            ellipsisStart = rootStyle.writingMode().isBidiLTR() ? displayBoxes.last().right() : displayBoxes[1].left() - ellipsisWidth;
        else {
            // All we have is the root inline box.
            ellipsisStart = displayBoxes.first().left();
        }
    } else {
        auto lineBoxVisualLeft = rootStyle.writingMode().isHorizontal() ? displayLine.left() : displayLine.top();
        auto lineBoxVisualRight = std::max(rootStyle.writingMode().isHorizontal() ? displayLine.right() : displayLine.bottom(), lineBoxVisualLeft);
        ellipsisStart = truncateOverflowingDisplayBoxes(displayBoxes, 0, displayBoxes.size() - 1, lineBoxVisualLeft, lineBoxVisualRight, ellipsisWidth, rootStyle, lineEndingTruncationPolicy);
    }

    if (rootStyle.writingMode().isHorizontal())
        return FloatRect { ellipsisStart, rootInlineBox.top(), ellipsisWidth, rootInlineBox.height() };
    return FloatRect { rootInlineBox.left(), ellipsisStart, rootInlineBox.width(), ellipsisWidth };
}

static inline bool isEligibleForLinkBoxLineClamp(auto& displayBoxes)
{
    if (displayBoxes.size() < 3) {
        // We need at least 3 display boxes to generate content with link ([root inline box][inline box][content])
        return false;
    }
    auto writingMode = displayBoxes[0].layoutBox().writingMode();
    if (writingMode.isBidiRTL() || writingMode.isVertical())
        return false;
    auto& linkCandidateBox = displayBoxes[displayBoxes.size() - 2];
    if (!linkCandidateBox.isNonRootInlineBox() || !linkCandidateBox.isFirstForLayoutBox()) {
        // Link spanning multiple lines looks odd with line-clamp.
        return false;
    }
    return linkCandidateBox.layoutBox().style().isLink();
}

static constexpr int legacyMatchingLinkBoxOffset = 3;
static inline void makeRoomForLinkBoxOnClampedLineIfNeeded(auto& clampedLine, auto& displayBoxes, auto insertionPosition, auto linkContentWidth)
{
    ASSERT(insertionPosition);
    auto ellipsisBoxRect = clampedLine.ellipsis()->visualRect;
    if (ellipsisBoxRect.maxX() + linkContentWidth <= clampedLine.right())
        return;
    auto& rootStyle = displayBoxes[0].layoutBox().style();
    auto startIndex = [&]() -> size_t {
        ASSERT(insertionPosition);
        for (size_t index = insertionPosition - 1; index--;) {
            if (displayBoxes[index].isRootInlineBox())
                return index + 1;
            return index;
        }
        ASSERT_NOT_REACHED();
        return 0;
    };
    auto endIndex = insertionPosition - 1;
    auto ellispsisPosition = clampedLine.right() - linkContentWidth - legacyMatchingLinkBoxOffset;
    auto ellipsisStart = truncateOverflowingDisplayBoxes(displayBoxes, startIndex(), endIndex, clampedLine.left(), ellispsisPosition, ellipsisBoxRect.width(), rootStyle, LineEndingTruncationPolicy::WhenContentOverflowsInBlockDirection);
    ellipsisBoxRect.setX(ellipsisStart);
    clampedLine.setEllipsis({ InlineDisplay::Line::Ellipsis::Type::Block, ellipsisBoxRect, TextUtil::ellipsisTextInInlineDirection(clampedLine.isHorizontal()) });
}

static inline void moveDisplayBoxToClampedLine(auto& displayLines, auto clampedLineIndex, auto& displayBox, auto horizontalOffset)
{
    auto& clampedLine = displayLines[clampedLineIndex];
    displayBox.setLeft(clampedLine.ellipsis()->visualRect.maxX() + horizontalOffset + legacyMatchingLinkBoxOffset);
    // Assume baseline alignment here.
    displayBox.moveVertically((clampedLine.top() + clampedLine.baseline()) - (displayLines.last().top() + displayLines.last().baseline()));

    auto& originalLine = displayLines[displayBox.lineIndex()];
    originalLine.setBoxCount(originalLine.boxCount() - 1);
    displayBox.moveToLine(clampedLineIndex);
    clampedLine.setBoxCount(clampedLine.boxCount() + 1);
}

void InlineDisplayLineBuilder::addLegacyLineClampTrailingLinkBoxIfApplicable(const InlineFormattingContext& inlineFormattingContext, const InlineLayoutState& inlineLayoutState, InlineDisplay::Content& displayContent)
{
    // This is link-box type of line clamping (-webkit-line-clamp) where we check if the inline content ends in a link
    // and move such link content next to the clamped line's trailing ellispsis. It is meant to produce the following rendering
    //
    // first line
    // second line is clamped... more info
    //
    // where "more info" is a link and it comes from the end of the inline content (normally invisible due to block direction clamping)
    // This is to match legacy line clamping behavior (and not a block-ellispsis: <string> implementation) which was introduced
    // at https://commits.webkit.org/6086@main to support special article rendering with clamped lines.
    // It supports horizontal, left-to-right content only where the link inline box has (non-split) text content and when
    // the link content ('more info') fits the clamped line.
    auto clampedLineIndex = inlineLayoutState.legacyClampedLineIndex();
    if (!clampedLineIndex)
        return;
    auto& displayLines = displayContent.lines;
    if (displayLines.isEmpty() || *clampedLineIndex >= displayLines.size() || !displayLines[*clampedLineIndex].hasEllipsis()) {
        ASSERT_NOT_REACHED();
        return;
    }
    if (*clampedLineIndex == displayLines.size() - 1) {
        // No need to link-box line clamp the last line.
        return;
    }

    if (!isEligibleForLinkBoxLineClamp(displayContent.boxes))
        return;

    auto& displayBoxes = displayContent.boxes;
    auto insertionPosition = [&]() -> std::optional<size_t> {
        for (size_t boxIndex = 0; boxIndex < displayBoxes.size(); ++boxIndex) {
            if (displayBoxes[boxIndex].lineIndex() > *clampedLineIndex)
                return boxIndex;
        }
        return { };
    }();

    if (!insertionPosition || !*insertionPosition || *insertionPosition >= displayBoxes.size() - 1) {
        // Unexpected cases where the insertion point is at the leading/trailing box. They both indicate incorrect line-clamp
        // position and would produce incorrect rendering.
        ASSERT_NOT_REACHED_WITH_SECURITY_IMPLICATION();
        return;
    }
    auto& clampedLine = displayLines[*clampedLineIndex];
    auto linkContentWidth = displayBoxes[displayBoxes.size() - 2].visualRectIgnoringBlockDirection().width();
    if (linkContentWidth >= clampedLine.lineBoxWidth()) {
        // Not enough space for "more info" content.
        return;
    }
    auto linkContentDisplayBox = displayBoxes.takeLast();
    auto linkInlineBoxDisplayBox = displayBoxes.takeLast();

    auto handleTrailingLineBreakIfApplicable = [&] {
        // "more info" gets inserted after the trailing run on the clamped line unless the trailing content is forced line break.
        auto& trailingDisplayBox = displayBoxes[*insertionPosition - 1];
        if (!trailingDisplayBox.isLineBreak())
            return;
        // Move trailing line break after the link box.
        trailingDisplayBox.moveHorizontally(linkContentWidth);
        --*insertionPosition;
    };
    handleTrailingLineBreakIfApplicable();

    makeRoomForLinkBoxOnClampedLineIfNeeded(clampedLine, displayBoxes, *insertionPosition, linkContentWidth);

    // link box content
    moveDisplayBoxToClampedLine(displayLines, *clampedLineIndex, linkContentDisplayBox, inlineFormattingContext.geometryForBox(linkInlineBoxDisplayBox.layoutBox()).marginBorderAndPaddingStart());
    displayBoxes.insert(*insertionPosition, linkContentDisplayBox);
    // Link inline box
    moveDisplayBoxToClampedLine(displayLines, *clampedLineIndex, linkInlineBoxDisplayBox, LayoutUnit { });
    displayBoxes.insert(*insertionPosition, linkInlineBoxDisplayBox);

    clampedLine.setHasContentAfterEllipsisBox();
}

void InlineDisplayLineBuilder::applyEllipsisIfNeeded(LineEndingTruncationPolicy truncationPolicy, InlineDisplay::Line& displayLine, InlineDisplay::Boxes& displayBoxes, bool isLegacyLineClamp)
{
    if (truncationPolicy == LineEndingTruncationPolicy::NoTruncation || !displayBoxes.size())
        return;

    auto ellipsisText = [&] {
        if (truncationPolicy == LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection || isLegacyLineClamp) {
            // Legacy line clamp always uses ...
            return TextUtil::ellipsisTextInInlineDirection(displayLine.isHorizontal());
        }
        auto& blockEllipsis = displayBoxes[0].layoutBox().style().blockEllipsis();
        if (blockEllipsis.type == BlockEllipsis::Type::None)
            return nullAtom();
        if (blockEllipsis.type == BlockEllipsis::Type::Auto)
            return TextUtil::ellipsisTextInInlineDirection(displayLine.isHorizontal());
        return blockEllipsis.string;
    }();

    if (ellipsisText.isNull())
        return;

    if (auto ellipsisRect = trailingEllipsisVisualRectAfterTruncation(truncationPolicy, ellipsisText, displayLine, displayBoxes))
        displayLine.setEllipsis({ truncationPolicy == LineEndingTruncationPolicy::WhenContentOverflowsInInlineDirection ? InlineDisplay::Line::Ellipsis::Type::Inline : InlineDisplay::Line::Ellipsis::Type::Block, *ellipsisRect, ellipsisText });
}

}
}