-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
InlineFormattingContext.cpp
518 lines (456 loc) · 27.8 KB
/
InlineFormattingContext.cpp
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
* Copyright (C) 2018 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 "InlineFormattingContext.h"
#include "BlockLayoutState.h"
#include "FloatingContext.h"
#include "FontCascade.h"
#include "InlineDamage.h"
#include "InlineDisplayBox.h"
#include "InlineDisplayContentBuilder.h"
#include "InlineDisplayLineBuilder.h"
#include "InlineFormattingState.h"
#include "InlineItemsBuilder.h"
#include "InlineLineBox.h"
#include "InlineLineBoxBuilder.h"
#include "InlineLineTypes.h"
#include "InlineTextItem.h"
#include "LayoutBox.h"
#include "LayoutContext.h"
#include "LayoutElementBox.h"
#include "LayoutInitialContainingBlock.h"
#include "LayoutInlineTextBox.h"
#include "LayoutState.h"
#include "Logging.h"
#include "TextUtil.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
namespace Layout {
WTF_MAKE_ISO_ALLOCATED_IMPL(InlineFormattingContext);
InlineFormattingContext::InlineFormattingContext(const ElementBox& formattingContextRoot, InlineFormattingState& formattingState, const InlineDamage* lineDamage)
: FormattingContext(formattingContextRoot, formattingState)
, m_lineDamage(lineDamage)
, m_inlineFormattingGeometry(*this)
, m_inlineFormattingQuirks(*this)
{
}
static inline const Box* nextInlineLevelBoxToLayout(const Box& layoutBox, const ElementBox& stayWithin)
{
// Atomic inline-level boxes and floats are opaque boxes meaning that they are
// responsible for their own content (do not need to descend into their subtrees).
// Only inline boxes may have relevant descendant content.
if (layoutBox.isInlineBox()) {
if (is<ElementBox>(layoutBox) && downcast<ElementBox>(layoutBox).hasInFlowOrFloatingChild()) {
// Anonymous inline text boxes/line breaks can't have descendant content by definition.
ASSERT(!layoutBox.isInlineTextBox() && !layoutBox.isLineBreakBox());
return downcast<ElementBox>(layoutBox).firstInFlowOrFloatingChild();
}
}
for (auto* nextInPreOrder = &layoutBox; nextInPreOrder && nextInPreOrder != &stayWithin; nextInPreOrder = &nextInPreOrder->parent()) {
if (auto* nextSibling = nextInPreOrder->nextInFlowOrFloatingSibling())
return nextSibling;
}
return nullptr;
}
void InlineFormattingContext::layoutInFlowContent(const ConstraintsForInFlowContent& constraints)
{
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Start] -> inline formatting context -> formatting root(" << &root() << ")");
ASSERT(root().hasInFlowOrFloatingChild());
invalidateFormattingState();
auto* layoutBox = root().firstInFlowOrFloatingChild();
// 1. Visit each inline box and partially compute their geometry (margins, padding and borders).
// 2. Collect the inline items (flatten the the layout tree) and place them on lines in bidirectional order.
while (layoutBox) {
ASSERT(layoutBox->isInlineLevelBox() || layoutBox->isFloatingPositioned());
if (layoutBox->isAtomicInlineLevelBox() || layoutBox->isFloatingPositioned()) {
// Inline-blocks, inline-tables and replaced elements (img, video) can be sized but not yet positioned.
if (is<ElementBox>(layoutBox) && layoutBox->establishesFormattingContext()) {
ASSERT(layoutBox->isInlineBlockBox() || layoutBox->isInlineTableBox() || layoutBox->isFloatingPositioned());
auto& formattingRoot = downcast<ElementBox>(*layoutBox);
computeBorderAndPadding(formattingRoot, constraints.horizontal());
computeWidthAndMargin(formattingRoot, constraints.horizontal());
if (formattingRoot.hasChild()) {
auto formattingContext = LayoutContext::createFormattingContext(formattingRoot, layoutState());
if (formattingRoot.hasInFlowOrFloatingChild())
formattingContext->layoutInFlowContent(formattingGeometry().constraintsForInFlowContent(formattingRoot));
computeHeightAndMargin(formattingRoot, constraints.horizontal());
formattingContext->layoutOutOfFlowContent(formattingGeometry().constraintsForOutOfFlowContent(formattingRoot));
} else
computeHeightAndMargin(formattingRoot, constraints.horizontal());
} else {
// Replaced and other type of leaf atomic inline boxes.
computeBorderAndPadding(*layoutBox, constraints.horizontal());
computeWidthAndMargin(*layoutBox, constraints.horizontal());
computeHeightAndMargin(*layoutBox, constraints.horizontal());
}
} else if (layoutBox->isLineBreakBox()) {
auto& boxGeometry = formattingState().boxGeometry(*layoutBox);
boxGeometry.setHorizontalMargin({ });
boxGeometry.setBorder({ });
boxGeometry.setPadding({ });
boxGeometry.setContentBoxWidth({ });
boxGeometry.setVerticalMargin({ });
} else if (layoutBox->isInlineBox()) {
// Text wrapper boxes (anonymous inline level boxes) don't have box geometries (they only generate boxes).
if (!layoutBox->isInlineTextBox()) {
// Inline boxes (<span>) can't get sized/positioned yet. At this point we can only compute their margins, borders and padding.
computeBorderAndPadding(*layoutBox, constraints.horizontal());
computeHorizontalMargin(*layoutBox, constraints.horizontal());
formattingState().boxGeometry(*layoutBox).setVerticalMargin({ });
}
} else
ASSERT_NOT_REACHED();
layoutBox = nextInlineLevelBoxToLayout(*layoutBox, root());
}
collectContentIfNeeded();
auto& inlineItems = formattingState().inlineItems();
// FIXME: Let the caller pass in the block layout state.
auto floatingState = FloatingState { layoutState(), FormattingContext::initialContainingBlock(root()) };
auto blockLayoutState = BlockLayoutState { floatingState, { } };
lineLayout(inlineItems, { 0, inlineItems.size() }, { constraints, { } }, blockLayoutState);
computeStaticPositionForOutOfFlowContent(formattingState().outOfFlowBoxes(), { constraints.horizontal().logicalLeft, constraints.logicalTop() });
InlineDisplayContentBuilder::computeIsFirstIsLastBoxForInlineContent(formattingState().boxes());
LOG_WITH_STREAM(FormattingContextLayout, stream << "[End] -> inline formatting context -> formatting root(" << &root() << ")");
}
void InlineFormattingContext::layoutInFlowContentForIntegration(const ConstraintsForInFlowContent& constraints, BlockLayoutState& blockLayoutState)
{
invalidateFormattingState();
collectContentIfNeeded();
auto& inlineItems = formattingState().inlineItems();
auto inlineConstraints = downcast<ConstraintsForInlineContent>(constraints);
lineLayout(inlineItems, { 0, inlineItems.size() }, inlineConstraints, blockLayoutState);
computeStaticPositionForOutOfFlowContent(formattingState().outOfFlowBoxes(), { inlineConstraints.horizontal().logicalLeft, inlineConstraints.logicalTop() });
InlineDisplayContentBuilder::computeIsFirstIsLastBoxForInlineContent(formattingState().boxes());
}
IntrinsicWidthConstraints InlineFormattingContext::computedIntrinsicWidthConstraintsForIntegration()
{
if (formattingState().intrinsicWidthConstraints())
return *formattingState().intrinsicWidthConstraints();
collectContentIfNeeded();
auto constraints = IntrinsicWidthConstraints {
ceiledLayoutUnit(computedIntrinsicWidthForConstraint(IntrinsicWidthMode::Minimum)),
ceiledLayoutUnit(computedIntrinsicWidthForConstraint(IntrinsicWidthMode::Maximum))
};
formattingState().setIntrinsicWidthConstraints(constraints);
return constraints;
}
LayoutUnit InlineFormattingContext::usedContentHeight() const
{
auto& lines = formattingState().lines();
// Even empty content generates a line.
ASSERT(!lines.isEmpty());
auto top = LayoutUnit { lines.first().top() };
auto bottom = LayoutUnit { lines.last().bottom() + formattingState().clearGapAfterLastLine() };
return bottom - top;
}
static size_t indexOfFirstInlineItemForNextLine(const LineBuilder::LineContent& lineContent, const std::optional<LineBuilder::PreviousLine>& previousLine, std::optional<size_t> previousLineLastInlineItemIndex)
{
auto lineContentRange = lineContent.inlineItemRange;
if (!lineContent.partialOverflowingContent.has_value())
return lineContentRange.endIndex();
// When the trailing content is partial, we need to reuse the last InlineTextItem.
auto lineLayoutHasAdvancedWithPartialContent = !previousLine || (lineContentRange.endIndex() > previousLineLastInlineItemIndex
|| (previousLine->partialOverflowingContent && previousLine->partialOverflowingContent->length > lineContent.partialOverflowingContent->length));
if (lineLayoutHasAdvancedWithPartialContent)
return lineContentRange.endIndex() - 1;
// Move over to the next run if we are stuck on this partial content (when the overflow content length remains the same).
// We certainly lose some content, but we would be busy looping otherwise.
ASSERT_NOT_REACHED();
return lineContentRange.endIndex();
}
void InlineFormattingContext::lineLayout(InlineItems& inlineItems, const InlineItemRange& needsLayoutRange, const ConstraintsForInlineContent& constraints, BlockLayoutState& blockLayoutState)
{
ASSERT(!needsLayoutRange.isEmpty());
auto& formattingState = this->formattingState();
formattingState.boxes().reserveInitialCapacity(formattingState.inlineItems().size());
auto floatingContext = FloatingContext { *this, blockLayoutState.floatingState() };
auto lineLogicalTop = InlineLayoutUnit { constraints.logicalTop() };
auto previousLine = std::optional<LineBuilder::PreviousLine> { };
auto previousLineLastInlineItemIndex = std::optional<size_t> { };
auto firstInlineItemNeedsLayout = needsLayoutRange.startIndex();
auto lineBuilder = LineBuilder { *this, blockLayoutState, constraints.horizontal(), inlineItems };
while (true) {
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 lineLogicalRect = createDisplayContentForLine(lineContent, constraints, blockLayoutState);
if (lineContent.isLastLineWithInlineContent)
formattingState.setClearGapAfterLastLine(formattingGeometry().logicalTopForNextLine(lineContent, lineLogicalRect, floatingContext) - lineLogicalRect.bottom());
firstInlineItemNeedsLayout = indexOfFirstInlineItemForNextLine(lineContent, previousLine, previousLineLastInlineItemIndex);
auto isLastLine = (firstInlineItemNeedsLayout == needsLayoutRange.endIndex() && lineContent.overflowingFloats.isEmpty());
if (isLastLine)
break;
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();
}
}
void InlineFormattingContext::computeStaticPositionForOutOfFlowContent(const FormattingState::OutOfFlowBoxList& outOfFlowBoxes, LayoutPoint contentBoxTopLeft)
{
// This function computes the static position for out-of-flow content inside the inline formatting context.
// As per spec, the static position of an out-of-flow box is computed as if the position was set to static.
// However it does not mean that the out-of-flow box should be involved in the inline layout process.
// Instead we figure out this static position after the inline layout by looking at the previous/next sibling (or parent) box's geometry and
// place the out-of-flow box at the logical right position.
auto& formattingGeometry = this->formattingGeometry();
auto& formattingState = this->formattingState();
for (auto& outOfFlowBox : outOfFlowBoxes) {
if (outOfFlowBox->style().isOriginalDisplayInlineType()) {
formattingState.boxGeometry(outOfFlowBox).setLogicalTopLeft(formattingGeometry.staticPositionForOutOfFlowInlineLevelBox(outOfFlowBox, contentBoxTopLeft));
continue;
}
formattingState.boxGeometry(outOfFlowBox).setLogicalTopLeft(formattingGeometry.staticPositionForOutOfFlowBlockLevelBox(outOfFlowBox, contentBoxTopLeft));
}
}
IntrinsicWidthConstraints InlineFormattingContext::computedIntrinsicWidthConstraints()
{
auto& formattingState = this->formattingState();
if (formattingState.intrinsicWidthConstraints())
return *formattingState.intrinsicWidthConstraints();
if (!root().hasInFlowOrFloatingChild()) {
auto constraints = formattingGeometry().constrainByMinMaxWidth(root(), { });
formattingState.setIntrinsicWidthConstraints(constraints);
return constraints;
}
Vector<const Box*> formattingContextRootList;
auto horizontalConstraints = HorizontalConstraints { 0_lu, 0_lu };
auto* layoutBox = root().firstInFlowOrFloatingChild();
// In order to compute the max/min widths, we need to compute margins, borders and padding for certain inline boxes first.
while (layoutBox) {
if (layoutBox->isInlineTextBox()) {
layoutBox = nextInlineLevelBoxToLayout(*layoutBox, root());
continue;
}
if (layoutBox->isReplacedBox()) {
computeBorderAndPadding(*layoutBox, horizontalConstraints);
computeWidthAndMargin(*layoutBox, horizontalConstraints);
} else if (layoutBox->isFloatingPositioned() || layoutBox->isAtomicInlineLevelBox()) {
ASSERT(layoutBox->establishesFormattingContext());
formattingContextRootList.append(layoutBox);
computeBorderAndPadding(*layoutBox, horizontalConstraints);
computeHorizontalMargin(*layoutBox, horizontalConstraints);
computeIntrinsicWidthForFormattingRoot(*layoutBox);
} else if (layoutBox->isInlineBox()) {
computeBorderAndPadding(*layoutBox, horizontalConstraints);
computeHorizontalMargin(*layoutBox, horizontalConstraints);
} else
ASSERT_NOT_REACHED();
layoutBox = nextInlineLevelBoxToLayout(*layoutBox, root());
}
collectContentIfNeeded();
auto maximumLineWidth = [&](auto intrinsicWidthMode) {
// Switch to the min/max formatting root width values before formatting the lines.
for (auto* formattingRoot : formattingContextRootList) {
auto intrinsicWidths = formattingState.intrinsicWidthConstraintsForBox(*formattingRoot);
auto& boxGeometry = formattingState.boxGeometry(*formattingRoot);
auto contentWidth = (intrinsicWidthMode == IntrinsicWidthMode::Maximum ? intrinsicWidths->maximum : intrinsicWidths->minimum) - boxGeometry.horizontalMarginBorderAndPadding();
boxGeometry.setContentBoxWidth(contentWidth);
}
return computedIntrinsicWidthForConstraint(intrinsicWidthMode);
};
auto minimumContentWidth = ceiledLayoutUnit(maximumLineWidth(IntrinsicWidthMode::Minimum));
auto maximumContentWidth = ceiledLayoutUnit(maximumLineWidth(IntrinsicWidthMode::Maximum));
auto constraints = formattingGeometry().constrainByMinMaxWidth(root(), { minimumContentWidth, maximumContentWidth });
formattingState.setIntrinsicWidthConstraints(constraints);
return constraints;
}
InlineLayoutUnit InlineFormattingContext::computedIntrinsicWidthForConstraint(IntrinsicWidthMode intrinsicWidthMode) const
{
auto& inlineItems = formattingState().inlineItems();
auto lineBuilder = LineBuilder { *this, inlineItems, intrinsicWidthMode };
auto layoutRange = InlineItemRange { 0 , inlineItems.size() };
auto maximumLineWidth = InlineLayoutUnit { };
auto maximumFloatWidth = LayoutUnit { };
auto previousLine = std::optional<LineBuilder::PreviousLine> { };
while (!layoutRange.isEmpty()) {
auto intrinsicContent = lineBuilder.computedIntrinsicWidth(layoutRange, previousLine);
maximumLineWidth = std::max(maximumLineWidth, intrinsicContent.logicalWidth);
layoutRange.start.index = !intrinsicContent.partialOverflowingContent ? intrinsicContent.inlineItemRange.endIndex() : intrinsicContent.inlineItemRange.endIndex() - 1;
previousLine = LineBuilder::PreviousLine { { }, { }, intrinsicContent.partialOverflowingContent, { } };
// FIXME: Add support for clear.
for (auto* inlineFloatItem : intrinsicContent.placedFloats)
maximumFloatWidth += geometryForBox(inlineFloatItem->layoutBox()).marginBoxWidth();
}
return maximumLineWidth + maximumFloatWidth;
}
void InlineFormattingContext::computeIntrinsicWidthForFormattingRoot(const Box& formattingRoot)
{
ASSERT(formattingRoot.establishesFormattingContext());
auto constraints = IntrinsicWidthConstraints { };
if (auto fixedWidth = formattingGeometry().fixedValue(formattingRoot.style().logicalWidth()))
constraints = { *fixedWidth, *fixedWidth };
else {
auto hasInflowOrFloatingContent = is<ElementBox>(formattingRoot) && downcast<ElementBox>(formattingRoot).hasInFlowOrFloatingChild();
// The intrinsic sizes of the size containment box are determined as if the element had no content.
auto shouldIgnoreChildContent = formattingRoot.isSizeContainmentBox();
if (hasInflowOrFloatingContent && !shouldIgnoreChildContent)
constraints = LayoutContext::createFormattingContext(downcast<ElementBox>(formattingRoot), layoutState())->computedIntrinsicWidthConstraints();
}
constraints = formattingGeometry().constrainByMinMaxWidth(formattingRoot, constraints);
constraints.expand(geometryForBox(formattingRoot).horizontalMarginBorderAndPadding());
formattingState().setIntrinsicWidthConstraintsForBox(formattingRoot, constraints);
}
void InlineFormattingContext::computeHorizontalMargin(const Box& layoutBox, const HorizontalConstraints& horizontalConstraints)
{
auto computedHorizontalMargin = formattingGeometry().computedHorizontalMargin(layoutBox, horizontalConstraints);
formattingState().boxGeometry(layoutBox).setHorizontalMargin({ computedHorizontalMargin.start.value_or(0), computedHorizontalMargin.end.value_or(0) });
}
void InlineFormattingContext::computeWidthAndMargin(const Box& layoutBox, const HorizontalConstraints& horizontalConstraints)
{
auto compute = [&](std::optional<LayoutUnit> usedWidth) {
if (layoutBox.isFloatingPositioned())
return formattingGeometry().floatingContentWidthAndMargin(layoutBox, horizontalConstraints, { usedWidth, { } });
if (layoutBox.isInlineBlockBox())
return formattingGeometry().inlineBlockContentWidthAndMargin(layoutBox, horizontalConstraints, { usedWidth, { } });
if (layoutBox.isReplacedBox())
return formattingGeometry().inlineReplacedContentWidthAndMargin(downcast<ElementBox>(layoutBox), horizontalConstraints, { }, { usedWidth, { } });
ASSERT_NOT_REACHED();
return ContentWidthAndMargin { };
};
auto contentWidthAndMargin = compute({ });
auto availableWidth = horizontalConstraints.logicalWidth;
if (auto maxWidth = formattingGeometry().computedMaxWidth(layoutBox, availableWidth)) {
auto maxWidthAndMargin = compute(maxWidth);
if (contentWidthAndMargin.contentWidth > maxWidthAndMargin.contentWidth)
contentWidthAndMargin = maxWidthAndMargin;
}
auto minWidth = formattingGeometry().computedMinWidth(layoutBox, availableWidth).value_or(0);
auto minWidthAndMargin = compute(minWidth);
if (contentWidthAndMargin.contentWidth < minWidthAndMargin.contentWidth)
contentWidthAndMargin = minWidthAndMargin;
auto& boxGeometry = formattingState().boxGeometry(layoutBox);
boxGeometry.setContentBoxWidth(contentWidthAndMargin.contentWidth);
boxGeometry.setHorizontalMargin({ contentWidthAndMargin.usedMargin.start, contentWidthAndMargin.usedMargin.end });
}
void InlineFormattingContext::computeHeightAndMargin(const Box& layoutBox, const HorizontalConstraints& horizontalConstraints)
{
auto compute = [&](std::optional<LayoutUnit> usedHeight) {
if (layoutBox.isFloatingPositioned())
return formattingGeometry().floatingContentHeightAndMargin(layoutBox, horizontalConstraints, { usedHeight });
if (layoutBox.isInlineBlockBox())
return formattingGeometry().inlineBlockContentHeightAndMargin(layoutBox, horizontalConstraints, { usedHeight });
if (layoutBox.isReplacedBox())
return formattingGeometry().inlineReplacedContentHeightAndMargin(downcast<ElementBox>(layoutBox), horizontalConstraints, { }, { usedHeight });
ASSERT_NOT_REACHED();
return ContentHeightAndMargin { };
};
auto contentHeightAndMargin = compute({ });
if (auto maxHeight = formattingGeometry().computedMaxHeight(layoutBox)) {
auto maxHeightAndMargin = compute(maxHeight);
if (contentHeightAndMargin.contentHeight > maxHeightAndMargin.contentHeight)
contentHeightAndMargin = maxHeightAndMargin;
}
if (auto minHeight = formattingGeometry().computedMinHeight(layoutBox)) {
auto minHeightAndMargin = compute(minHeight);
if (contentHeightAndMargin.contentHeight < minHeightAndMargin.contentHeight)
contentHeightAndMargin = minHeightAndMargin;
}
auto& boxGeometry = formattingState().boxGeometry(layoutBox);
boxGeometry.setContentBoxHeight(contentHeightAndMargin.contentHeight);
boxGeometry.setVerticalMargin({ contentHeightAndMargin.nonCollapsedMargin.before, contentHeightAndMargin.nonCollapsedMargin.after });
}
void InlineFormattingContext::collectContentIfNeeded()
{
auto& formattingState = this->formattingState();
if (!formattingState.inlineItems().isEmpty())
return;
auto inlineItemsBuilder = InlineItemsBuilder { root(), formattingState };
formattingState.addInlineItems(inlineItemsBuilder.build());
}
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);
auto ellipsisPolicy = lineEndingEllipsisPolicy(root().style(), currentLineIndex, maximumNumberOfVisibleLinesForThisInlineContent);
if (auto ellipsisRect = InlineDisplayLineBuilder::trailingEllipsisVisualRectAfterTruncation(ellipsisPolicy, displayLine, boxes, lineContent.isLastLineWithInlineContent))
displayLine.setEllipsisVisualRect(*ellipsisRect);
formattingState.addBoxes(WTFMove(boxes));
formattingState.addLineBox(WTFMove(lineBox));
formattingState.addLine(displayLine);
return InlineFormattingGeometry::flipVisualRectToLogicalForWritingMode(formattingState.lines().last().lineBoxRect(), root().style().writingMode());
}
void InlineFormattingContext::resetGeometryForClampedContent(const InlineItemRange& needsDisplayContentRange, const LineBuilder::FloatList& overflowingFloats, LayoutPoint topleft)
{
if (needsDisplayContentRange.isEmpty() && overflowingFloats.isEmpty())
return;
auto& inlineItems = formattingState().inlineItems();
for (size_t index = needsDisplayContentRange.startIndex(); index < needsDisplayContentRange.endIndex(); ++index) {
auto& inlineItem = inlineItems[index];
auto hasBoxGeometry = inlineItem.isBox() || inlineItem.isFloat() || inlineItem.isHardLineBreak() || inlineItem.isInlineBoxStart();
if (!hasBoxGeometry)
continue;
auto& boxGeometry = formattingState().boxGeometry(inlineItem.layoutBox());
boxGeometry.setLogicalTopLeft(topleft);
boxGeometry.setContentBoxHeight({ });
boxGeometry.setContentBoxWidth({ });
}
}
void InlineFormattingContext::invalidateFormattingState()
{
if (!m_lineDamage) {
// Non-empty formatting state with no damage means we are trying to layout a clean tree.
// FIXME: Add ASSERT(formattingState().inlineItems().isEmpty()) when all the codepaths are covered.
formattingState().clearLineAndBoxes();
return;
}
// FIXME: Lines and boxes are moved out to under Integration::InlineContent in the integration codepath, so clearLineAndBoxes is no-op.
switch (m_lineDamage->type()) {
case InlineDamage::Type::NeedsContentUpdateAndLineLayout:
formattingState().clearInlineItems();
FALLTHROUGH;
case InlineDamage::Type::NeedsLineLayout:
formattingState().clearLineAndBoxes();
break;
case InlineDamage::Type::NeedsVerticalAdjustment:
case InlineDamage::Type::NeedsHorizontalAdjustment:
default:
ASSERT_NOT_IMPLEMENTED_YET();
break;
}
}
}
}