File: LayoutIntegrationInlineContentBuilder.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 (378 lines) | stat: -rw-r--r-- 18,303 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
/*
* Copyright (C) 2020 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 "LayoutIntegrationInlineContentBuilder.h"

#include "InlineDamage.h"
#include "InlineDisplayBox.h"
#include "LayoutBoxGeometry.h"
#include "LayoutIntegrationInlineContent.h"
#include "LayoutState.h"
#include "RenderBlockFlowInlines.h"
#include "RenderBoxInlines.h"
#include "RenderStyleInlines.h"
#include "StringTruncator.h"

namespace WebCore {
namespace LayoutIntegration {

inline static float endPaddingQuirkValue(const RenderBlockFlow& flow)
{
    // FIXME: It's the copy of the lets-adjust-overflow-for-the-caret behavior from LegacyLineLayout::addOverflowFromInlineChildren.
    auto endPadding = flow.hasNonVisibleOverflow() ? flow.paddingEnd() : 0_lu;
    if (!endPadding)
        endPadding = flow.endPaddingWidthForCaret();
    if (flow.hasNonVisibleOverflow() && !endPadding && flow.element() && flow.element()->isRootEditableElement() && flow.style().isLeftToRightDirection())
        endPadding = 1;
    return endPadding;
}

static std::tuple<float, float> glyphOverflowInInlineDirection(size_t firstTextBoxIndex, size_t lastTextBoxIndex, const InlineDisplay::Boxes& boxes, const FloatRect& inkOverflowRect, bool isLeftToRightDirection)
{
    // FIXME: This should be on the text box level and taking all characters into account (maybe consider utilizing the measuring pass if turns out to be a perf hit)
    if (firstTextBoxIndex >= boxes.size() || lastTextBoxIndex >= boxes.size()) {
        ASSERT_NOT_REACHED();
        return { };
    }

    auto bounds = [&](auto& textBox, bool isLeading) {
        auto textContent = textBox.text().renderedContent();
        if (!textContent.length()) {
            ASSERT_NOT_REACHED();
            return FloatRect { };
        }
        auto character = isLeading ? textContent[0] : textContent[textContent.length() - 1];
        auto& fontCascade = textBox.style().fontCascade();
        auto glyphData = fontCascade.glyphDataForCharacter(character, !isLeftToRightDirection);
        return (glyphData.font ? *glyphData.font : fontCascade.primaryFont().get()).boundsForGlyph(glyphData.glyph);
    };

    auto leadingOverflow = [&] {
        auto& firstTextBox = boxes[firstTextBoxIndex];
        ASSERT(firstTextBox.isText());
        if (downcast<Layout::InlineTextBox>(firstTextBox.layoutBox()).canUseSimpleFontCodePath())
            return 0.f;
        if (auto boundsX = bounds(firstTextBox, true).x(); boundsX < 0)
            return std::max(0.f, inkOverflowRect.x() - (firstTextBox.left() + boundsX));
        return 0.f;
    };

    auto trailingOverflow = [&] {
        auto& lastTextBox = boxes[lastTextBoxIndex];
        ASSERT(lastTextBox.isText());
        if (downcast<Layout::InlineTextBox>(lastTextBox.layoutBox()).canUseSimpleFontCodePath())
            return 0.f;
        if (auto boundsMaxX = bounds(lastTextBox, false).maxX(); boundsMaxX > lastTextBox.width())
            return std::max(0.f, (lastTextBox.left() + boundsMaxX) - inkOverflowRect.maxX());
        return 0.f;
    };
    return { leadingOverflow(), trailingOverflow() };
}

InlineContentBuilder::InlineContentBuilder(const RenderBlockFlow& blockFlow)
    : m_blockFlow(blockFlow)
{
}

FloatRect InlineContentBuilder::build(Layout::InlineLayoutResult&& layoutResult, InlineContent& inlineContent, const Layout::InlineDamage* lineDamage) const
{
    inlineContent.releaseCaches();
    auto damageRect = FloatRect { };

    if (layoutResult.range == Layout::InlineLayoutResult::Range::Full) {
        for (auto& line : inlineContent.displayContent().lines)
            damageRect.unite(line.inkOverflow());

        inlineContent.displayContent().set(WTFMove(layoutResult.displayContent));
        adjustDisplayLines(inlineContent, 0);

        for (auto& line : inlineContent.displayContent().lines)
            damageRect.unite(line.inkOverflow());
    } else
        damageRect = handlePartialDisplayContentUpdate(WTFMove(layoutResult), inlineContent, lineDamage);

    computeIsFirstIsLastBoxAndBidiReorderingForInlineContent(inlineContent.displayContent().boxes);
    return damageRect;
}

void InlineContentBuilder::updateLineOverflow(InlineContent& inlineContent) const
{
    adjustDisplayLines(inlineContent, 0);
}

void InlineContentBuilder::adjustDisplayLines(InlineContent& inlineContent, size_t startIndex) const
{
    auto& lines = inlineContent.displayContent().lines;
    auto& boxes = inlineContent.displayContent().boxes;

    size_t boxIndex = !startIndex ? 0 : lines[startIndex - 1].lastBoxIndex() + 1;
    auto& rootBoxStyle = m_blockFlow.style();
    auto isLeftToRightInlineDirection = rootBoxStyle.isLeftToRightDirection();
    auto isHorizontalWritingMode = rootBoxStyle.writingMode().isHorizontal();

    auto blockScrollableOverflowRect = FloatRect { };
    auto blockInkOverflowRect = FloatRect { };

    for (size_t lineIndex = 0; lineIndex < startIndex; ++lineIndex) {
        auto& line = lines[lineIndex];
        blockScrollableOverflowRect.unite(line.scrollableOverflow());
        blockInkOverflowRect.unite(line.inkOverflow());
    }

    for (size_t lineIndex = startIndex; lineIndex < lines.size(); ++lineIndex) {
        auto& line = lines[lineIndex];
        auto lineScrollableOverflowRect = line.contentOverflow();
        auto adjustOverflowLogicalWidthWithBlockFlowQuirk = [&] {
            auto scrollableOverflowLogicalWidth = isHorizontalWritingMode ? lineScrollableOverflowRect.width() : lineScrollableOverflowRect.height();
            if (!isLeftToRightInlineDirection && line.contentLogicalWidth() > scrollableOverflowLogicalWidth) {
                // The only time when scrollable overflow here could be shorter than
                // the content width is when hanging RTL trailing content is applied (and ignored as scrollable overflow. See LineBoxBuilder::build.
                return;
            }
            auto adjustedOverflowLogicalWidth = line.contentLogicalWidth() + endPaddingQuirkValue(m_blockFlow);
            if (adjustedOverflowLogicalWidth > scrollableOverflowLogicalWidth) {
                auto overflowValue = adjustedOverflowLogicalWidth - scrollableOverflowLogicalWidth;
                if (isHorizontalWritingMode)
                    isLeftToRightInlineDirection ? lineScrollableOverflowRect.shiftMaxXEdgeBy(overflowValue) : lineScrollableOverflowRect.shiftXEdgeBy(-overflowValue);
                else
                    isLeftToRightInlineDirection ? lineScrollableOverflowRect.shiftMaxYEdgeBy(overflowValue) : lineScrollableOverflowRect.shiftYEdgeBy(-overflowValue);
            }
        };
        adjustOverflowLogicalWidthWithBlockFlowQuirk();

        auto firstBoxIndex = boxIndex;
        auto lineInkOverflowRect = lineScrollableOverflowRect;
        // Collect overflow from boxes.
        // Note while we compute ink overflow for all type of boxes including atomic inline level boxes (e.g. <iframe> <img>) as part of constructing
        // display boxes (see InlineDisplayContentBuilder) RenderBlockFlow expects visual overflow.
        // Visual overflow propagation is slightly different from ink overflow when it comes to renderers with self painting layers.
        // -and for now we consult atomic renderers for such visual overflow which is not how we are supposed to do in LFC.
        // (visual overflow is computed during their ::layout() call which we issue right before running inline layout in RenderBlockFlow::layoutModernLines)
        auto firstTextBoxIndex = std::optional<size_t> { };
        auto lastTextBoxIndex = std::optional<size_t> { };
        for (; boxIndex < boxes.size() && boxes[boxIndex].lineIndex() == lineIndex; ++boxIndex) {
            auto& box = boxes[boxIndex];
            if (box.isRootInlineBox() || box.isEllipsis() || box.isLineBreak())
                continue;

            if (box.isText()) {
                lineInkOverflowRect.unite(box.inkOverflow());
                if (box.isVisible() && box.text().renderedContent().length()) {
                    firstTextBoxIndex = firstTextBoxIndex.value_or(boxIndex);
                    lastTextBoxIndex = boxIndex;
                }
                continue;
            }

            if (box.isAtomicInlineBox()) {
                auto& renderer = downcast<RenderBox>(*box.layoutBox().rendererForIntegration());
                if (!renderer.hasSelfPaintingLayer()) {
                    auto childInkOverflow = renderer.logicalVisualOverflowRectForPropagation(renderer.parent()->writingMode());
                    childInkOverflow.move(box.left(), box.top());
                    lineInkOverflowRect.unite(childInkOverflow);
                }
                auto childScrollableOverflow = renderer.layoutOverflowRectForPropagation(renderer.parent()->writingMode());
                childScrollableOverflow.move(box.left(), box.top());
                lineScrollableOverflowRect.unite(childScrollableOverflow);
                continue;
            }

            if (box.isInlineBox()) {
                if (!downcast<RenderElement>(*box.layoutBox().rendererForIntegration()).hasSelfPaintingLayer())
                    lineInkOverflowRect.unite(box.inkOverflow());
            }
        }

        if (firstTextBoxIndex && lastTextBoxIndex) {
            auto [leadingOverflow, trailingOverflow] = glyphOverflowInInlineDirection(*firstTextBoxIndex, *lastTextBoxIndex, boxes, lineInkOverflowRect, line.isLeftToRightInlineDirection());
            lineInkOverflowRect.inflate(leadingOverflow, { }, trailingOverflow, { });
        }

        line.setScrollableOverflow(lineScrollableOverflowRect);
        line.setInkOverflow(lineInkOverflowRect);
        line.setFirstBoxIndex(firstBoxIndex);

        blockScrollableOverflowRect.unite(lineScrollableOverflowRect);
        blockInkOverflowRect.unite(lineInkOverflowRect);

        ASSERT(line.boxCount() == boxIndex - firstBoxIndex);

        if (lineIndex) {
            auto& lastInkOverflow = lines[lineIndex - 1].inkOverflow();
            if (lineInkOverflowRect.y() <= lastInkOverflow.y() || lastInkOverflow.maxY() >= lineInkOverflowRect.maxY())
                inlineContent.setHasMultilinePaintOverlap();
        }
    }
    inlineContent.setScrollableOverflow(blockScrollableOverflowRect);
    inlineContent.setInkOverflow(blockInkOverflowRect);
}

void InlineContentBuilder::computeIsFirstIsLastBoxAndBidiReorderingForInlineContent(InlineDisplay::Boxes& boxes) const
{
    if (boxes.isEmpty()) {
        // Line clamp may produce a completely empty IFC.
        return;
    }

    UncheckedKeyHashMap<const Layout::Box*, size_t> lastDisplayBoxForLayoutBoxIndexes;
    lastDisplayBoxForLayoutBoxIndexes.reserveInitialCapacity(boxes.size() - 1);

    ASSERT(boxes[0].isRootInlineBox());
    boxes[0].setIsFirstForLayoutBox(true);
    size_t lastRootInlineBoxIndex = 0;

    for (size_t index = 1; index < boxes.size(); ++index) {
        auto& displayBox = boxes[index];
        if (displayBox.isRootInlineBox()) {
            lastRootInlineBoxIndex = index;
            continue;
        }
        auto& layoutBox = displayBox.layoutBox();
        if (is<Layout::InlineTextBox>(layoutBox) && displayBox.bidiLevel() != UBIDI_DEFAULT_LTR)
            downcast<RenderText>(*layoutBox.rendererForIntegration()).setNeedsVisualReordering();

        if (lastDisplayBoxForLayoutBoxIndexes.set(&layoutBox, index).isNewEntry)
            displayBox.setIsFirstForLayoutBox(true);
    }
    for (auto index : lastDisplayBoxForLayoutBoxIndexes.values())
        boxes[index].setIsLastForLayoutBox(true);

    boxes[lastRootInlineBoxIndex].setIsLastForLayoutBox(true);
}

FloatRect InlineContentBuilder::handlePartialDisplayContentUpdate(Layout::InlineLayoutResult&& layoutResult, InlineContent& inlineContent, const Layout::InlineDamage* lineDamage) const
{
    auto firstDamagedLineIndex = [&]() -> std::optional<size_t> {
        auto& displayContentFromPreviousLayout = inlineContent.displayContent();
        if (!lineDamage || !lineDamage->layoutStartPosition() || !displayContentFromPreviousLayout.lines.size())
            return { };
        auto canidateLineIndex = lineDamage->layoutStartPosition()->lineIndex;
        if (canidateLineIndex >= displayContentFromPreviousLayout.lines.size()) {
            ASSERT_NOT_REACHED();
            return { };
        }
        if (layoutResult.displayContent.boxes.size() && canidateLineIndex > layoutResult.displayContent.boxes[0].lineIndex()) {
            // We should never generate lines _before_ the damaged line.
            ASSERT_NOT_REACHED();
            return { };
        }
        return { canidateLineIndex };
    }();

    auto firstDamagedBoxIndex = [&]() -> std::optional<size_t> {
        auto& displayContentFromPreviousLayout = inlineContent.displayContent();
        return firstDamagedLineIndex ? std::make_optional(displayContentFromPreviousLayout.lines[*firstDamagedLineIndex].firstBoxIndex()) : std::nullopt;
    }();

    auto numberOfDamagedLines = [&]() -> std::optional<size_t> {
        if (!firstDamagedLineIndex)
            return { };
        auto& displayContentFromPreviousLayout = inlineContent.displayContent();
        ASSERT(layoutResult.range != Layout::InlineLayoutResult::Range::Full);
        auto canidateLineCount = layoutResult.range == Layout::InlineLayoutResult::Range::FullFromDamage
            ? displayContentFromPreviousLayout.lines.size() - *firstDamagedLineIndex
            : layoutResult.displayContent.lines.size();

        if (*firstDamagedLineIndex + canidateLineCount > displayContentFromPreviousLayout.lines.size()) {
            ASSERT_NOT_REACHED();
            return { };
        }
        return { canidateLineCount };
    }();

    auto numberOfDamagedBoxes = [&]() -> std::optional<size_t> {
        if (!firstDamagedLineIndex || !numberOfDamagedLines || !firstDamagedBoxIndex)
            return { };
        auto& displayContentFromPreviousLayout = inlineContent.displayContent();
        ASSERT(*firstDamagedLineIndex + *numberOfDamagedLines <= displayContentFromPreviousLayout.lines.size());
        size_t boxCount = 0;
        for (size_t i = 0; i < *numberOfDamagedLines; ++i)
            boxCount += displayContentFromPreviousLayout.lines[*firstDamagedLineIndex + i].boxCount();
        ASSERT(boxCount);
        return { boxCount };
    }();

    if (!firstDamagedLineIndex || !numberOfDamagedLines || !firstDamagedBoxIndex || !numberOfDamagedBoxes) {
        ASSERT_NOT_REACHED();
        return { };
    }

    auto numberOfNewLines = layoutResult.displayContent.lines.size();
    auto numberOfNewBoxes = layoutResult.displayContent.boxes.size();

    auto damagedRect = FloatRect { };
    auto adjustDamagedRectWithLineRange = [&](size_t firstLineIndex, size_t lineCount) {
        auto& lines = inlineContent.displayContent().lines;
        ASSERT(firstLineIndex + lineCount <= lines.size());
        for (size_t i = 0; i < lineCount; ++i)
            damagedRect.unite(lines[firstLineIndex + i].inkOverflow());
    };

    // Repaint the damaged content boundary.
    adjustDamagedRectWithLineRange(*firstDamagedLineIndex, *numberOfDamagedLines);

    switch (layoutResult.range) {
    case Layout::InlineLayoutResult::Range::FullFromDamage: {
        auto& displayContent = inlineContent.displayContent();
        displayContent.remove(*firstDamagedLineIndex, *numberOfDamagedLines, *firstDamagedBoxIndex, *numberOfDamagedBoxes);
        displayContent.append(WTFMove(layoutResult.displayContent));
        break;
    }
    case Layout::InlineLayoutResult::Range::PartialFromDamage: {
        auto& displayContent = inlineContent.displayContent();
        displayContent.remove(*firstDamagedLineIndex, *numberOfDamagedLines, *firstDamagedBoxIndex, *numberOfDamagedBoxes);
        displayContent.insert(WTFMove(layoutResult.displayContent), *firstDamagedLineIndex, *firstDamagedBoxIndex);

        auto adjustCachedBoxIndexesIfNeeded = [&] {
            if (numberOfNewBoxes == *numberOfDamagedBoxes)
                return;
            auto firstCleanLineIndex = *firstDamagedLineIndex + *numberOfDamagedLines;
            auto offset = numberOfNewBoxes - *numberOfDamagedBoxes;
            auto& lines = displayContent.lines;
            for (size_t cleanLineIndex = firstCleanLineIndex; cleanLineIndex < lines.size(); ++cleanLineIndex) {
                ASSERT(lines[cleanLineIndex].firstBoxIndex() + offset > 0);
                auto adjustedFirstBoxIndex = std::max<size_t>(0, lines[cleanLineIndex].firstBoxIndex() + offset);
                lines[cleanLineIndex].setFirstBoxIndex(adjustedFirstBoxIndex);
            }
        };
        adjustCachedBoxIndexesIfNeeded();
        break;
    }
    case Layout::InlineLayoutResult::Range::Full:
    default:
        ASSERT_NOT_REACHED();
    }

    adjustDisplayLines(inlineContent, *firstDamagedLineIndex);
    // Repaint the new content boundary.
    adjustDamagedRectWithLineRange(*firstDamagedLineIndex, numberOfNewLines);

    return damagedRect;
}

}
}