e61dba2ec80df04a4190a325005533aa4dac65d0
[WebKit-https.git] / Source / WebCore / rendering / RenderBlockLineLayout.cpp
1 /*
2  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
4  * Copyright (C) 2010 Google Inc. All rights reserved.
5  * Copyright (C) 2013 ChangSeok Oh <shivamidow@gmail.com>
6  * Copyright (C) 2013 Adobe Systems Inc. All right reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #include "config.h"
26
27 #include "AXObjectCache.h"
28 #include "BidiResolver.h"
29 #include "BreakingContext.h"
30 #include "FloatingObjects.h"
31 #include "InlineElementBox.h"
32 #include "InlineIterator.h"
33 #include "InlineTextBox.h"
34 #include "InlineTextBoxStyle.h"
35 #include "LineLayoutState.h"
36 #include "Logging.h"
37 #include "RenderBlockFlow.h"
38 #include "RenderFlowThread.h"
39 #include "RenderLineBreak.h"
40 #include "RenderRegion.h"
41 #include "RenderRubyBase.h"
42 #include "RenderRubyText.h"
43 #include "RenderView.h"
44 #include "SVGRootInlineBox.h"
45 #include "Settings.h"
46 #include "SimpleLineLayoutFunctions.h"
47 #include "TrailingFloatsRootInlineBox.h"
48 #include "VerticalPositionCache.h"
49 #include <wtf/StdLibExtras.h>
50
51 namespace WebCore {
52
53 static void determineDirectionality(TextDirection& dir, InlineIterator iter)
54 {
55     while (!iter.atEnd()) {
56         if (iter.atParagraphSeparator())
57             return;
58         if (UChar current = iter.current()) {
59             UCharDirection charDirection = u_charDirection(current);
60             if (charDirection == U_LEFT_TO_RIGHT) {
61                 dir = LTR;
62                 return;
63             }
64             if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
65                 dir = RTL;
66                 return;
67             }
68         }
69         iter.increment();
70     }
71 }
72
73 inline std::unique_ptr<BidiRun> createRun(int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
74 {
75     return std::make_unique<BidiRun>(start, end, obj, resolver.context(), resolver.dir());
76 }
77
78 void RenderBlockFlow::appendRunsForObject(BidiRunList<BidiRun>* runs, int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
79 {
80     if (start > end || shouldSkipCreatingRunsForObject(obj))
81         return;
82
83     LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
84     bool haveNextTransition = (lineWhitespaceCollapsingState.currentTransition() < lineWhitespaceCollapsingState.numTransitions());
85     InlineIterator nextTransition;
86     if (haveNextTransition)
87         nextTransition = lineWhitespaceCollapsingState.transitions()[lineWhitespaceCollapsingState.currentTransition()];
88     if (lineWhitespaceCollapsingState.betweenTransitions()) {
89         if (!haveNextTransition || (&obj != nextTransition.renderer()))
90             return;
91         // This is a new start point. Stop ignoring objects and
92         // adjust our start.
93         start = nextTransition.offset();
94         lineWhitespaceCollapsingState.incrementCurrentTransition();
95         if (start < end) {
96             appendRunsForObject(runs, start, end, obj, resolver);
97             return;
98         }
99     } else {
100         if (!haveNextTransition || (&obj != nextTransition.renderer())) {
101             if (runs)
102                 runs->appendRun(createRun(start, end, obj, resolver));
103             return;
104         }
105
106         // An end transition has been encountered within our object. We need to append a run with our endpoint.
107         if (static_cast<int>(nextTransition.offset() + 1) <= end) {
108             lineWhitespaceCollapsingState.incrementCurrentTransition();
109             // The end of the line is before the object we're inspecting. Skip everything and return
110             if (nextTransition.refersToEndOfPreviousNode())
111                 return;
112             if (static_cast<int>(nextTransition.offset() + 1) > start && runs)
113                 runs->appendRun(createRun(start, nextTransition.offset() + 1, obj, resolver));
114             appendRunsForObject(runs, nextTransition.offset() + 1, end, obj, resolver);
115         } else if (runs)
116             runs->appendRun(createRun(start, end, obj, resolver));
117     }
118 }
119
120 std::unique_ptr<RootInlineBox> RenderBlockFlow::createRootInlineBox()
121 {
122     return std::make_unique<RootInlineBox>(*this);
123 }
124
125 RootInlineBox* RenderBlockFlow::createAndAppendRootInlineBox()
126 {
127     auto newRootBox = createRootInlineBox();
128     RootInlineBox* rootBox = newRootBox.get();
129     m_lineBoxes.appendLineBox(WTFMove(newRootBox));
130
131     if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && firstRootBox() == rootBox) {
132         if (AXObjectCache* cache = document().existingAXObjectCache())
133             cache->deferRecomputeIsIgnored(element());
134     }
135
136     return rootBox;
137 }
138
139 static inline InlineBox* createInlineBoxForRenderer(RenderObject* renderer, bool isRootLineBox, bool isOnlyRun = false)
140 {
141     if (isRootLineBox)
142         return downcast<RenderBlockFlow>(*renderer).createAndAppendRootInlineBox();
143
144     if (is<RenderText>(*renderer))
145         return downcast<RenderText>(*renderer).createInlineTextBox();
146
147     if (is<RenderBox>(*renderer)) {
148         // FIXME: This is terrible. This branch returns an *owned* pointer!
149         return downcast<RenderBox>(*renderer).createInlineBox().release();
150     }
151
152     if (is<RenderLineBreak>(*renderer)) {
153         // FIXME: This is terrible. This branch returns an *owned* pointer!
154         auto inlineBox = downcast<RenderLineBreak>(*renderer).createInlineBox().release();
155         // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
156         // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
157         inlineBox->setBehavesLikeText(isOnlyRun || renderer->document().inNoQuirksMode() || renderer->isLineBreakOpportunity());
158         return inlineBox;
159     }
160
161     return downcast<RenderInline>(*renderer).createAndAppendInlineFlowBox();
162 }
163
164 static inline void dirtyLineBoxesForRenderer(RenderObject& renderer, bool fullLayout)
165 {
166     if (is<RenderText>(renderer)) {
167         RenderText& renderText = downcast<RenderText>(renderer);
168         updateCounterIfNeeded(renderText);
169         renderText.dirtyLineBoxes(fullLayout);
170     } else if (is<RenderLineBreak>(renderer))
171         downcast<RenderLineBreak>(renderer).dirtyLineBoxes(fullLayout);
172     else
173         downcast<RenderInline>(renderer).dirtyLineBoxes(fullLayout);
174 }
175
176 static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
177 {
178     do {
179         if (parentBox->isConstructed() || parentBox->nextOnLine())
180             return true;
181         parentBox = parentBox->parent();
182     } while (parentBox);
183     return false;
184 }
185
186 InlineFlowBox* RenderBlockFlow::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
187 {
188     // See if we have an unconstructed line box for this object that is also
189     // the last item on the line.
190     unsigned lineDepth = 1;
191     InlineFlowBox* parentBox = nullptr;
192     InlineFlowBox* result = nullptr;
193     bool hasDefaultLineBoxContain = style().lineBoxContain() == RenderStyle::initialLineBoxContain();
194     do {
195         ASSERT_WITH_SECURITY_IMPLICATION(is<RenderInline>(*obj) || obj == this);
196
197         RenderInline* inlineFlow = obj != this ? downcast<RenderInline>(obj) : nullptr;
198
199         // Get the last box we made for this render object.
200         parentBox = inlineFlow ? inlineFlow->lastLineBox() : downcast<RenderBlockFlow>(*obj).lastRootBox();
201
202         // If this box or its ancestor is constructed then it is from a previous line, and we need
203         // to make a new box for our line.  If this box or its ancestor is unconstructed but it has
204         // something following it on the line, then we know we have to make a new box
205         // as well.  In this situation our inline has actually been split in two on
206         // the same line (this can happen with very fancy language mixtures).
207         bool constructedNewBox = false;
208         bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
209         bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
210         if (allowedToConstructNewBox && !canUseExistingParentBox) {
211             // We need to make a new box for this render object.  Once
212             // made, we need to place it at the end of the current line.
213             InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
214             parentBox = downcast<InlineFlowBox>(newBox);
215             parentBox->setIsFirstLine(lineInfo.isFirstLine());
216             parentBox->setIsHorizontal(isHorizontalWritingMode());
217             if (!hasDefaultLineBoxContain)
218                 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
219             constructedNewBox = true;
220         }
221
222         if (constructedNewBox || canUseExistingParentBox) {
223             if (!result)
224                 result = parentBox;
225
226             // If we have hit the block itself, then |box| represents the root
227             // inline box for the line, and it doesn't have to be appended to any parent
228             // inline.
229             if (childBox)
230                 parentBox->addToLine(childBox);
231
232             if (!constructedNewBox || obj == this)
233                 break;
234
235             childBox = parentBox;
236         }
237
238         // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
239         // intermediate inline flows.
240         obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
241
242     } while (true);
243
244     return result;
245 }
246
247 template <typename CharacterType>
248 static inline bool endsWithASCIISpaces(const CharacterType* characters, unsigned pos, unsigned end)
249 {
250     while (isASCIISpace(characters[pos])) {
251         pos++;
252         if (pos >= end)
253             return true;
254     }
255     return false;
256 }
257
258 static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
259 {
260     BidiRun* run = bidiRuns.logicallyLastRun();
261     if (!run)
262         return true;
263     unsigned pos = run->stop();
264     const RenderObject& renderer = run->renderer();
265     if (!is<RenderText>(renderer))
266         return false;
267     const RenderText& renderText = downcast<RenderText>(renderer);
268     unsigned length = renderText.textLength();
269     if (pos >= length)
270         return true;
271
272     if (renderText.is8Bit())
273         return endsWithASCIISpaces(renderText.characters8(), pos, length);
274     return endsWithASCIISpaces(renderText.characters16(), pos, length);
275 }
276
277 RootInlineBox* RenderBlockFlow::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
278 {
279     ASSERT(bidiRuns.firstRun());
280
281     bool rootHasSelectedChildren = false;
282     InlineFlowBox* parentBox = 0;
283     int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
284     
285     for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
286         // Create a box for our object.
287         bool isOnlyRun = (runCount == 1);
288         if (runCount == 2 && !r->renderer().isListMarker())
289             isOnlyRun = (!style().isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->renderer().isListMarker();
290
291         if (lineInfo.isEmpty())
292             continue;
293
294         InlineBox* box = createInlineBoxForRenderer(&r->renderer(), false, isOnlyRun);
295         r->setBox(box);
296
297         if (!rootHasSelectedChildren && box->renderer().selectionState() != RenderObject::SelectionNone)
298             rootHasSelectedChildren = true;
299         
300         // If we have no parent box yet, or if the run is not simply a sibling,
301         // then we need to construct inline boxes as necessary to properly enclose the
302         // run's inline box. Segments can only be siblings at the root level, as
303         // they are positioned separately.
304         if (!parentBox || &parentBox->renderer() != r->renderer().parent()) {
305             // Create new inline boxes all the way back to the appropriate insertion point.
306             RenderObject* parentToUse = r->renderer().parent();
307             parentBox = createLineBoxes(parentToUse, lineInfo, box);
308         } else {
309             // Append the inline box to this line.
310             parentBox->addToLine(box);
311         }
312
313         bool visuallyOrdered = r->renderer().style().rtlOrdering() == VisualOrder;
314         box->setBidiLevel(r->level());
315
316         if (is<InlineTextBox>(*box)) {
317             auto& textBox = downcast<InlineTextBox>(*box);
318             textBox.setStart(r->m_start);
319             textBox.setLen(r->m_stop - r->m_start);
320             textBox.setDirOverride(r->dirOverride(visuallyOrdered));
321             if (r->m_hasHyphen)
322                 textBox.setHasHyphen(true);
323         }
324     }
325
326     // We should have a root inline box.  It should be unconstructed and
327     // be the last continuation of our line list.
328     ASSERT(lastRootBox() && !lastRootBox()->isConstructed());
329
330     // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
331     // from the bidi runs walk above has a selection state.
332     if (rootHasSelectedChildren)
333         lastRootBox()->root().setHasSelectedChildren(true);
334
335     // Set bits on our inline flow boxes that indicate which sides should
336     // paint borders/margins/padding.  This knowledge will ultimately be used when
337     // we determine the horizontal positions and widths of all the inline boxes on
338     // the line.
339     bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->renderer().isText() ? !reachedEndOfTextRenderer(bidiRuns) : !is<RenderInline>(bidiRuns.logicallyLastRun()->renderer());
340     lastRootBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, &bidiRuns.logicallyLastRun()->renderer());
341
342     // Now mark the line boxes as being constructed.
343     lastRootBox()->setConstructed();
344
345     // Return the last line.
346     return lastRootBox();
347 }
348
349 ETextAlign RenderBlockFlow::textAlignmentForLine(bool endsWithSoftBreak) const
350 {
351     ETextAlign alignment = style().textAlign();
352 #if ENABLE(CSS3_TEXT)
353     TextJustify textJustify = style().textJustify();
354     if (alignment == JUSTIFY && textJustify == TextJustifyNone)
355         return style().direction() == LTR ? LEFT : RIGHT;
356 #endif
357
358     if (endsWithSoftBreak)
359         return alignment;
360
361 #if !ENABLE(CSS3_TEXT)
362     return (alignment == JUSTIFY) ? TASTART : alignment;
363 #else
364     if (alignment != JUSTIFY)
365         return alignment;
366
367     TextAlignLast alignmentLast = style().textAlignLast();
368     switch (alignmentLast) {
369     case TextAlignLastStart:
370         return TASTART;
371     case TextAlignLastEnd:
372         return TAEND;
373     case TextAlignLastLeft:
374         return LEFT;
375     case TextAlignLastRight:
376         return RIGHT;
377     case TextAlignLastCenter:
378         return CENTER;
379     case TextAlignLastJustify:
380         return JUSTIFY;
381     case TextAlignLastAuto:
382         if (textJustify == TextJustifyDistribute)
383             return JUSTIFY;
384         return TASTART;
385     }
386     return alignment;
387 #endif
388 }
389
390 static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
391 {
392     // The direction of the block should determine what happens with wide lines.
393     // In particular with RTL blocks, wide lines should still spill out to the left.
394     if (isLeftToRightDirection) {
395         if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
396             trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
397         return;
398     }
399
400     if (trailingSpaceRun)
401         trailingSpaceRun->box()->setLogicalWidth(0);
402     else if (totalLogicalWidth > availableLogicalWidth)
403         logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
404 }
405
406 static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
407 {
408     // Wide lines spill out of the block based off direction.
409     // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
410     // side of the block.
411     if (isLeftToRightDirection) {
412         if (trailingSpaceRun) {
413             totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
414             trailingSpaceRun->box()->setLogicalWidth(0);
415         }
416         logicalLeft += std::max(0.f, availableLogicalWidth - totalLogicalWidth);
417         return;
418     }
419
420     if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
421         trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
422         totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
423     } else
424         logicalLeft += availableLogicalWidth - totalLogicalWidth;
425 }
426
427 static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
428 {
429     float trailingSpaceWidth = 0;
430     if (trailingSpaceRun) {
431         totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
432         trailingSpaceWidth = std::min(trailingSpaceRun->box()->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
433         trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceWidth));
434     }
435     if (isLeftToRightDirection)
436         logicalLeft += std::max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
437     else
438         logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
439 }
440
441 void RenderBlockFlow::setMarginsForRubyRun(BidiRun* run, RenderRubyRun& renderer, RenderObject* previousObject, const LineInfo& lineInfo)
442 {
443     float startOverhang;
444     float endOverhang;
445     RenderObject* nextObject = 0;
446     for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
447         if (!runWithNextObject->renderer().isOutOfFlowPositioned() && !runWithNextObject->box()->isLineBreak()) {
448             nextObject = &runWithNextObject->renderer();
449             break;
450         }
451     }
452     renderer.getOverhang(lineInfo.isFirstLine(), renderer.style().isLeftToRightDirection() ? previousObject : nextObject, renderer.style().isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
453     setMarginStartForChild(renderer, -startOverhang);
454     setMarginEndForChild(renderer, -endOverhang);
455 }
456
457 static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText& renderer, float xPos, const LineInfo& lineInfo,
458     GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
459 {
460     HashSet<const Font*> fallbackFonts;
461     GlyphOverflow glyphOverflow;
462
463     const FontCascade& font = lineStyle(*renderer.parent(), lineInfo).fontCascade();
464     // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
465     if (lineBox->fitsToGlyphs()) {
466         // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
467         // will keep us from computing glyph bounds in nearly all cases.
468         bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
469         int baselineShift = lineBox->verticalPositionForBox(run->box(), verticalPositionCache);
470         int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
471         int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
472         int boxAscent = font.fontMetrics().ascent() - baselineShift;
473         int boxDescent = font.fontMetrics().descent() + baselineShift;
474         if (boxAscent > rootDescent ||  boxDescent > rootAscent)
475             glyphOverflow.computeBounds = true; 
476     }
477     
478     LayoutUnit hyphenWidth = 0;
479     if (downcast<InlineTextBox>(*run->box()).hasHyphen())
480         hyphenWidth = measureHyphenWidth(renderer, font, &fallbackFonts);
481
482     float measuredWidth = 0;
483
484     bool kerningIsEnabled = font.enableKerning();
485     bool canUseSimpleFontCodePath = renderer.canUseSimpleFontCodePath();
486     
487     // Since we don't cache glyph overflows, we need to re-measure the run if
488     // the style is linebox-contain: glyph.
489     if (!lineBox->fitsToGlyphs() && canUseSimpleFontCodePath) {
490         unsigned lastEndOffset = run->m_start;
491         bool atFirstWordMeasurement = true;
492         for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
493             WordMeasurement& wordMeasurement = wordMeasurements[i];
494             if (wordMeasurement.width <= 0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
495                 continue;
496             if (wordMeasurement.renderer != &renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
497                 continue;
498
499             lastEndOffset = wordMeasurement.endOffset;
500             if (kerningIsEnabled && lastEndOffset == run->m_stop) {
501                 int wordLength = lastEndOffset - wordMeasurement.startOffset;
502                 GlyphOverflow overflow;
503                 measuredWidth += renderer.width(wordMeasurement.startOffset, wordLength, xPos + measuredWidth, lineInfo.isFirstLine(),
504                     &wordMeasurement.fallbackFonts, &overflow);
505                 UChar c = renderer.characterAt(wordMeasurement.startOffset);
506                 // renderer.width() omits word-spacing value for leading whitespace, so let's just add it back here.
507                 if (!atFirstWordMeasurement && (c == ' ' || c == '\t'))
508                     measuredWidth += renderer.style().fontCascade().wordSpacing();
509             } else
510                 measuredWidth += wordMeasurement.width;
511             atFirstWordMeasurement = false;
512
513             if (!wordMeasurement.fallbackFonts.isEmpty()) {
514                 HashSet<const Font*>::const_iterator end = wordMeasurement.fallbackFonts.end();
515                 for (HashSet<const Font*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
516                     fallbackFonts.add(*it);
517             }
518         }
519         if (measuredWidth && lastEndOffset != run->m_stop) {
520             // If we don't have enough cached data, we'll measure the run again.
521             measuredWidth = 0;
522             fallbackFonts.clear();
523         }
524     }
525
526     if (!measuredWidth)
527         measuredWidth = renderer.width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
528
529     run->box()->setLogicalWidth(measuredWidth + hyphenWidth);
530     if (!fallbackFonts.isEmpty()) {
531         ASSERT(run->box()->behavesLikeText());
532         GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<InlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
533         ASSERT(it->value.first.isEmpty());
534         copyToVector(fallbackFonts, it->value.first);
535         run->box()->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
536     }
537
538     // Include text decoration visual overflow as part of the glyph overflow.
539     if (renderer.style().textDecorationsInEffect() != TextDecorationNone)
540         glyphOverflow.extendTo(visualOverflowForDecorations(run->box()->lineStyle(), downcast<InlineTextBox>(run->box())));
541
542     if (!glyphOverflow.isEmpty()) {
543         ASSERT(run->box()->behavesLikeText());
544         GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<InlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
545         it->value.second = glyphOverflow;
546         run->box()->clearKnownToHaveNoOverflow();
547     }
548 }
549
550 void RenderBlockFlow::updateRubyForJustifiedText(RenderRubyRun& rubyRun, BidiRun& r, const Vector<unsigned, 16>& expansionOpportunities, unsigned& expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth, size_t& i)
551 {
552     if (!rubyRun.rubyBase() || !rubyRun.rubyBase()->firstRootBox() || rubyRun.rubyBase()->firstRootBox()->nextRootBox() || !r.renderer().style().collapseWhiteSpace())
553         return;
554
555     auto& rubyBase = *rubyRun.rubyBase();
556     auto& rootBox = *rubyBase.firstRootBox();
557
558     float totalExpansion = 0;
559     unsigned totalOpportunitiesInRun = 0;
560     for (auto* leafChild = rootBox.firstLeafChild(); leafChild; leafChild = leafChild->nextLeafChild()) {
561         if (!leafChild->isInlineTextBox())
562             continue;
563
564         unsigned opportunitiesInRun = expansionOpportunities[i++];
565         ASSERT(opportunitiesInRun <= expansionOpportunityCount);
566         auto expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
567         totalExpansion += expansion;
568         totalOpportunitiesInRun += opportunitiesInRun;
569     }
570
571     ASSERT(!rubyRun.hasOverrideLogicalContentWidth());
572     float newBaseWidth = rubyRun.logicalWidth() + totalExpansion + marginStartForChild(rubyRun) + marginEndForChild(rubyRun);
573     float newRubyRunWidth = rubyRun.logicalWidth() + totalExpansion;
574     rubyBase.setInitialOffset((newRubyRunWidth - newBaseWidth) / 2);
575     rubyRun.setOverrideLogicalContentWidth(newRubyRunWidth);
576     rubyRun.setNeedsLayout(MarkOnlyThis);
577     rootBox.markDirty();
578     if (RenderRubyText* rubyText = rubyRun.rubyText()) {
579         if (RootInlineBox* textRootBox = rubyText->firstRootBox())
580             textRootBox->markDirty();
581     }
582     rubyRun.layoutBlock(true);
583     rubyRun.clearOverrideLogicalContentWidth();
584     r.box()->setExpansion(newRubyRunWidth - r.box()->logicalWidth());
585
586     totalLogicalWidth += totalExpansion;
587     expansionOpportunityCount -= totalOpportunitiesInRun;
588 }
589
590 void RenderBlockFlow::computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, const Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float totalLogicalWidth, float availableLogicalWidth)
591 {
592     if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
593         return;
594
595     size_t i = 0;
596     for (BidiRun* run = firstRun; run; run = run->next()) {
597         if (!run->box() || run == trailingSpaceRun)
598             continue;
599         
600         if (is<RenderText>(run->renderer())) {
601             unsigned opportunitiesInRun = expansionOpportunities[i++];
602             
603             ASSERT(opportunitiesInRun <= expansionOpportunityCount);
604             
605             // Only justify text if whitespace is collapsed.
606             if (run->renderer().style().collapseWhiteSpace()) {
607                 InlineTextBox& textBox = downcast<InlineTextBox>(*run->box());
608                 float expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
609                 textBox.setExpansion(expansion);
610                 totalLogicalWidth += expansion;
611             }
612             expansionOpportunityCount -= opportunitiesInRun;
613         } else if (is<RenderRubyRun>(run->renderer()))
614             updateRubyForJustifiedText(downcast<RenderRubyRun>(run->renderer()), *run, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth, i);
615
616         if (!expansionOpportunityCount)
617             break;
618     }
619 }
620
621 void RenderBlockFlow::updateLogicalWidthForAlignment(const ETextAlign& textAlign, const RootInlineBox* rootInlineBox, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
622 {
623     TextDirection direction;
624     if (rootInlineBox && style().unicodeBidi() == Plaintext)
625         direction = rootInlineBox->direction();
626     else
627         direction = style().direction();
628
629     // Armed with the total width of the line (without justification),
630     // we now examine our text-align property in order to determine where to position the
631     // objects horizontally. The total width of the line can be increased if we end up
632     // justifying text.
633     switch (textAlign) {
634     case LEFT:
635     case WEBKIT_LEFT:
636         updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
637         break;
638     case RIGHT:
639     case WEBKIT_RIGHT:
640         updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
641         break;
642     case CENTER:
643     case WEBKIT_CENTER:
644         updateLogicalWidthForCenterAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
645         break;
646     case JUSTIFY:
647         adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
648         if (expansionOpportunityCount) {
649             if (trailingSpaceRun) {
650                 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
651                 trailingSpaceRun->box()->setLogicalWidth(0);
652             }
653             break;
654         }
655         FALLTHROUGH;
656     case TASTART:
657         if (direction == LTR)
658             updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
659         else
660             updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
661         break;
662     case TAEND:
663         if (direction == LTR)
664             updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
665         else
666             updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
667         break;
668     }
669 }
670
671 static void updateLogicalInlinePositions(RenderBlockFlow& block, float& lineLogicalLeft, float& lineLogicalRight, float& availableLogicalWidth, bool firstLine,
672     IndentTextOrNot shouldIndentText, LayoutUnit boxLogicalHeight)
673 {
674     LayoutUnit lineLogicalHeight = block.minLineHeightForReplacedRenderer(firstLine, boxLogicalHeight);
675     lineLogicalLeft = block.logicalLeftOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
676     lineLogicalRight = block.logicalRightOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
677     availableLogicalWidth = lineLogicalRight - lineLogicalLeft;
678 }
679
680 void RenderBlockFlow::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
681 {
682     ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
683     
684     // CSS 2.1: "'Text-indent' only affects a line if it is the first formatted line of an element. For example, the first line of an anonymous block 
685     // box is only affected if it is the first child of its parent element."
686     // CSS3 "text-indent", "-webkit-each-line" affects the first line of the block container as well as each line after a forced line break,
687     // but does not affect lines after a soft wrap break.
688     bool isFirstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
689     bool isAfterHardLineBreak = lineBox->prevRootBox() && lineBox->prevRootBox()->endsWithBreak();
690     IndentTextOrNot shouldIndentText = requiresIndent(isFirstLine, isAfterHardLineBreak, style());
691     float lineLogicalLeft;
692     float lineLogicalRight;
693     float availableLogicalWidth;
694     updateLogicalInlinePositions(*this, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, 0);
695     bool needsWordSpacing;
696
697     if (firstRun && firstRun->renderer().isReplaced()) {
698         RenderBox& renderBox = downcast<RenderBox>(firstRun->renderer());
699         updateLogicalInlinePositions(*this, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, renderBox.logicalHeight());
700     }
701
702     computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, lineLogicalLeft, availableLogicalWidth, firstRun, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
703     // The widths of all runs are now known. We can now place every inline box (and
704     // compute accurate widths for the inline flow boxes).
705     needsWordSpacing = false;
706     lineBox->placeBoxesInInlineDirection(lineLogicalLeft, needsWordSpacing);
707 }
708
709 static inline ExpansionBehavior expansionBehaviorForInlineTextBox(RenderBlockFlow& block, InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, ETextAlign textAlign, bool isAfterExpansion)
710 {
711     // Tatechuyoko is modeled as the Object Replacement Character (U+FFFC), which can never have expansion opportunities inside nor intrinsically adjacent to it.
712     if (textBox.renderer().style().textCombine() == TextCombineHorizontal)
713         return ForbidLeadingExpansion | ForbidTrailingExpansion;
714
715     ExpansionBehavior result = 0;
716     bool setLeadingExpansion = false;
717     bool setTrailingExpansion = false;
718     if (textAlign == JUSTIFY) {
719         // If the next box is ruby, and we're justifying, and the first box in the ruby base has a leading expansion, and we are a text box, then force a trailing expansion.
720         if (nextRun && is<RenderRubyRun>(nextRun->renderer()) && downcast<RenderRubyRun>(nextRun->renderer()).rubyBase() && nextRun->renderer().style().collapseWhiteSpace()) {
721             auto& rubyBase = *downcast<RenderRubyRun>(nextRun->renderer()).rubyBase();
722             if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
723                 if (auto* leafChild = rubyBase.firstRootBox()->firstLeafChild()) {
724                     if (is<InlineTextBox>(*leafChild)) {
725                         // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
726                         if (FontCascade::leadingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
727                             setTrailingExpansion = true;
728                             result |= ForceTrailingExpansion;
729                         }
730                     }
731                 }
732             }
733         }
734         // Same thing, except if we're following a ruby
735         if (previousRun && is<RenderRubyRun>(previousRun->renderer()) && downcast<RenderRubyRun>(previousRun->renderer()).rubyBase() && previousRun->renderer().style().collapseWhiteSpace()) {
736             auto& rubyBase = *downcast<RenderRubyRun>(previousRun->renderer()).rubyBase();
737             if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
738                 if (auto* leafChild = rubyBase.firstRootBox()->lastLeafChild()) {
739                     if (is<InlineTextBox>(*leafChild)) {
740                         // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
741                         if (FontCascade::trailingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
742                             setLeadingExpansion = true;
743                             result |= ForceLeadingExpansion;
744                         }
745                     }
746                 }
747             }
748         }
749         // If we're the first box inside a ruby base, forbid a leading expansion, and vice-versa
750         if (is<RenderRubyBase>(block)) {
751             RenderRubyBase& rubyBase = downcast<RenderRubyBase>(block);
752             if (&textBox == rubyBase.firstRootBox()->firstLeafChild()) {
753                 setLeadingExpansion = true;
754                 result |= ForbidLeadingExpansion;
755             } if (&textBox == rubyBase.firstRootBox()->lastLeafChild()) {
756                 setTrailingExpansion = true;
757                 result |= ForbidTrailingExpansion;
758             }
759         }
760     }
761     if (!setLeadingExpansion)
762         result |= isAfterExpansion ? ForbidLeadingExpansion : AllowLeadingExpansion;
763     if (!setTrailingExpansion)
764         result |= AllowTrailingExpansion;
765     return result;
766 }
767
768 static inline void applyExpansionBehavior(InlineTextBox& textBox, ExpansionBehavior expansionBehavior)
769 {
770     switch (expansionBehavior & LeadingExpansionMask) {
771     case ForceLeadingExpansion:
772         textBox.setForceLeadingExpansion();
773         break;
774     case ForbidLeadingExpansion:
775         textBox.setCanHaveLeadingExpansion(false);
776         break;
777     case AllowLeadingExpansion:
778         textBox.setCanHaveLeadingExpansion(true);
779         break;
780     default:
781         ASSERT_NOT_REACHED();
782         break;
783     }
784     switch (expansionBehavior & TrailingExpansionMask) {
785     case ForceTrailingExpansion:
786         textBox.setForceTrailingExpansion();
787         break;
788     case ForbidTrailingExpansion:
789         textBox.setCanHaveTrailingExpansion(false);
790         break;
791     case AllowTrailingExpansion:
792         textBox.setCanHaveTrailingExpansion(true);
793         break;
794     default:
795         ASSERT_NOT_REACHED();
796         break;
797     }
798 }
799
800 static bool inlineAncestorHasStartBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
801 {
802     bool isLTR = block.style().isLeftToRightDirection();
803     for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
804         if ((isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0)
805             || (!isLTR && currentBox->marginBorderPaddingLogicalRight() > 0))
806             return true;
807     }
808     return false;
809 }
810
811 static bool inlineAncestorHasEndBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
812 {
813     bool isLTR = block.style().isLeftToRightDirection();
814     for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
815         if ((isLTR && currentBox->marginBorderPaddingLogicalRight() > 0)
816             || (!isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0))
817             return true;
818     }
819     return false;
820 }
821     
822 static bool isLastInFlowRun(BidiRun& runToCheck)
823 {
824     for (auto* run = runToCheck.next(); run; run = run->next()) {
825         if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak())
826             continue;
827         return false;
828     }
829     return true;
830 }
831     
832 BidiRun* RenderBlockFlow::computeInlineDirectionPositionsForSegment(RootInlineBox* lineBox, const LineInfo& lineInfo, ETextAlign textAlign, float& logicalLeft, 
833     float& availableLogicalWidth, BidiRun* firstRun, BidiRun* trailingSpaceRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache,
834     WordMeasurements& wordMeasurements)
835 {
836     bool needsWordSpacing = false;
837     bool canHangPunctuationAtStart = style().hangingPunctuation() & FirstHangingPunctuation;
838     bool canHangPunctuationAtEnd = style().hangingPunctuation() & LastHangingPunctuation;
839     bool isLTR = style().isLeftToRightDirection();
840     float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
841     unsigned expansionOpportunityCount = 0;
842     bool isAfterExpansion = is<RenderRubyBase>(*this) ? downcast<RenderRubyBase>(*this).isAfterExpansion() : true;
843     Vector<unsigned, 16> expansionOpportunities;
844
845     BidiRun* run = firstRun;
846     BidiRun* previousRun = nullptr;
847     for (; run; run = run->next()) {
848         auto computeExpansionOpportunities = [&expansionOpportunities, &expansionOpportunityCount, textAlign, &isAfterExpansion] (RenderBlockFlow& block,
849             InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, const StringView& stringView, TextDirection direction)
850         {
851             if (stringView.isEmpty()) {
852                 // Empty runs should still produce an entry in expansionOpportunities list so that the number of items matches the number of runs.
853                 expansionOpportunities.append(0);
854                 return;
855             }
856             ExpansionBehavior expansionBehavior = expansionBehaviorForInlineTextBox(block, textBox, previousRun, nextRun, textAlign, isAfterExpansion);
857             applyExpansionBehavior(textBox, expansionBehavior);
858             unsigned opportunitiesInRun;
859             std::tie(opportunitiesInRun, isAfterExpansion) = FontCascade::expansionOpportunityCount(stringView, direction, expansionBehavior);
860             expansionOpportunities.append(opportunitiesInRun);
861             expansionOpportunityCount += opportunitiesInRun;
862         };
863         if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak()) {
864             continue; // Positioned objects are only participating to figure out their
865                       // correct static x position.  They have no effect on the width.
866                       // Similarly, line break boxes have no effect on the width.
867         }
868         if (is<RenderText>(run->renderer())) {
869             auto& renderText = downcast<RenderText>(run->renderer());
870             auto& textBox = downcast<InlineTextBox>(*run->box());
871             if (canHangPunctuationAtStart && lineInfo.isFirstLine() && (isLTR || isLastInFlowRun(*run))
872                 && !inlineAncestorHasStartBorderPaddingOrMargin(*this, *run->box())) {
873                 float hangStartWidth = renderText.hangablePunctuationStartWidth(run->m_start);
874                 availableLogicalWidth += hangStartWidth;
875                 if (style().isLeftToRightDirection())
876                     logicalLeft -= hangStartWidth;
877                 canHangPunctuationAtStart = false;
878             }
879             
880             if (canHangPunctuationAtEnd && lineInfo.isLastLine() && run->m_stop > 0 && (!isLTR || isLastInFlowRun(*run))
881                 && !inlineAncestorHasEndBorderPaddingOrMargin(*this, *run->box())) {
882                 float hangEndWidth = renderText.hangablePunctuationEndWidth(run->m_stop - 1);
883                 availableLogicalWidth += hangEndWidth;
884                 if (!style().isLeftToRightDirection())
885                     logicalLeft -= hangEndWidth;
886                 canHangPunctuationAtEnd = false;
887             }
888             
889             if (textAlign == JUSTIFY && run != trailingSpaceRun)
890                 computeExpansionOpportunities(*this, textBox, previousRun, run->next(), renderText.stringView(run->m_start, run->m_stop), run->box()->direction());
891
892             if (unsigned length = renderText.textLength()) {
893                 if (!run->m_start && needsWordSpacing && isSpaceOrNewline(renderText.characterAt(run->m_start)))
894                     totalLogicalWidth += lineStyle(*renderText.parent(), lineInfo).fontCascade().wordSpacing();
895                 // run->m_start == run->m_stop should only be true iff the run is a replaced run for bidi: isolate.
896                 ASSERT(run->m_stop > 0 || run->m_start == run->m_stop);
897                 needsWordSpacing = run->m_stop == length && !isSpaceOrNewline(renderText.characterAt(run->m_stop - 1));
898             }
899
900             setLogicalWidthForTextRun(lineBox, run, renderText, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
901         } else {
902             canHangPunctuationAtStart = false;
903             bool encounteredJustifiedRuby = false;
904             if (is<RenderRubyRun>(run->renderer()) && textAlign == JUSTIFY && run != trailingSpaceRun && downcast<RenderRubyRun>(run->renderer()).rubyBase()) {
905                 auto* rubyBase = downcast<RenderRubyRun>(run->renderer()).rubyBase();
906                 if (rubyBase->firstRootBox() && !rubyBase->firstRootBox()->nextRootBox() && run->renderer().style().collapseWhiteSpace()) {
907                     rubyBase->setIsAfterExpansion(isAfterExpansion);
908                     for (auto* leafChild = rubyBase->firstRootBox()->firstLeafChild(); leafChild; leafChild = leafChild->nextLeafChild()) {
909                         if (!is<InlineTextBox>(*leafChild))
910                             continue;
911                         encounteredJustifiedRuby = true;
912                         computeExpansionOpportunities(*rubyBase, downcast<InlineTextBox>(*leafChild), nullptr, nullptr,
913                             downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction());
914                     }
915                 }
916             }
917
918             if (!encounteredJustifiedRuby)
919                 isAfterExpansion = false;
920
921             if (!is<RenderInline>(run->renderer())) {
922                 auto& renderBox = downcast<RenderBox>(run->renderer());
923                 if (is<RenderRubyRun>(renderBox))
924                     setMarginsForRubyRun(run, downcast<RenderRubyRun>(renderBox), previousRun ? &previousRun->renderer() : nullptr, lineInfo);
925                 run->box()->setLogicalWidth(logicalWidthForChild(renderBox));
926                 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
927             }
928         }
929
930         totalLogicalWidth += run->box()->logicalWidth();
931         previousRun = run;
932     }
933
934     if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
935         // FIXME: see <webkit.org/b/139393#c11>
936         int lastValidExpansionOpportunitiesIndex = expansionOpportunities.size() - 1;
937         while (lastValidExpansionOpportunitiesIndex >= 0 && !expansionOpportunities.at(lastValidExpansionOpportunitiesIndex))
938             --lastValidExpansionOpportunitiesIndex;
939         if (lastValidExpansionOpportunitiesIndex >= 0) {
940             ASSERT(expansionOpportunities.at(lastValidExpansionOpportunitiesIndex));
941             expansionOpportunities.at(lastValidExpansionOpportunitiesIndex)--;
942             expansionOpportunityCount--;
943         }
944     }
945
946     if (is<RenderRubyBase>(*this) && !expansionOpportunityCount)
947         textAlign = CENTER;
948
949     updateLogicalWidthForAlignment(textAlign, lineBox, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
950
951     computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
952
953     return run;
954 }
955
956 void RenderBlockFlow::removeInlineBox(BidiRun& run, const RootInlineBox& rootLineBox) const
957 {
958     auto* inlineBox = run.box();
959 #if !ASSERT_DISABLED
960     auto* inlineParent = inlineBox->parent();
961     while (inlineParent && inlineParent != &rootLineBox) {
962         ASSERT(!inlineParent->isDirty());
963         inlineParent = inlineParent->parent();
964     }
965     ASSERT(!rootLineBox.isDirty());
966 #endif
967     auto* parent = inlineBox->parent();
968     inlineBox->removeFromParent();
969
970     auto& renderer = run.renderer();
971     if (is<RenderText>(renderer))
972         downcast<RenderText>(renderer).removeTextBox(downcast<InlineTextBox>(*inlineBox));
973     delete inlineBox;
974     run.setBox(nullptr);
975     // removeFromParent() unnecessarily dirties the ancestor subtree.
976     auto* ancestor = parent;
977     while (ancestor) {
978         ancestor->markDirty(false);
979         if (ancestor == &rootLineBox)
980             break;
981         ancestor = ancestor->parent();
982     }
983 }
984
985 void RenderBlockFlow::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
986                                                         VerticalPositionCache& verticalPositionCache)
987 {
988     setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
989
990     // Now make sure we place replaced render objects correctly.
991     for (auto* run = firstRun; run; run = run->next()) {
992         ASSERT(run->box());
993         if (!run->box())
994             continue; // Skip runs with no line boxes.
995
996         // Align positioned boxes with the top of the line box.  This is
997         // a reasonable approximation of an appropriate y position.
998         auto& renderer = run->renderer();
999         if (renderer.isOutOfFlowPositioned())
1000             run->box()->setLogicalTop(logicalHeight());
1001
1002         // Position is used to properly position both replaced elements and
1003         // to update the static normal flow x/y of positioned elements.
1004         bool inlineBoxIsRedundant = false;
1005         if (is<RenderText>(renderer)) {
1006             auto& inlineTextBox = downcast<InlineTextBox>(*run->box());
1007             downcast<RenderText>(renderer).positionLineBox(inlineTextBox);
1008             inlineBoxIsRedundant = !inlineTextBox.len();
1009         } else if (is<RenderBox>(renderer)) {
1010             downcast<RenderBox>(renderer).positionLineBox(downcast<InlineElementBox>(*run->box()));
1011             inlineBoxIsRedundant = renderer.isOutOfFlowPositioned();
1012         } else if (is<RenderLineBreak>(renderer))
1013             downcast<RenderLineBreak>(renderer).replaceInlineBoxWrapper(downcast<InlineElementBox>(*run->box()));
1014         // Check if we need to keep this box on the line at all.
1015         if (inlineBoxIsRedundant)
1016             removeInlineBox(*run, *lineBox);
1017     }
1018 }
1019
1020 static inline bool isCollapsibleSpace(UChar character, const RenderText& renderer)
1021 {
1022     if (character == ' ' || character == '\t' || character == softHyphen)
1023         return true;
1024     if (character == '\n')
1025         return !renderer.style().preserveNewline();
1026     if (character == noBreakSpace)
1027         return renderer.style().nbspMode() == SPACE;
1028     return false;
1029 }
1030
1031 template <typename CharacterType>
1032 static inline unsigned findFirstTrailingSpace(const RenderText& lastText, const CharacterType* characters, unsigned start, unsigned stop)
1033 {
1034     unsigned firstSpace = stop;
1035     while (firstSpace > start) {
1036         UChar current = characters[firstSpace - 1];
1037         if (!isCollapsibleSpace(current, lastText))
1038             break;
1039         firstSpace--;
1040     }
1041
1042     return firstSpace;
1043 }
1044
1045 inline BidiRun* RenderBlockFlow::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
1046 {
1047     if (!bidiRuns.runCount()
1048         || !bidiRuns.logicallyLastRun()->renderer().style().breakOnlyAfterWhiteSpace()
1049         || !bidiRuns.logicallyLastRun()->renderer().style().autoWrap())
1050         return nullptr;
1051
1052     BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
1053     const RenderObject& lastObject = trailingSpaceRun->renderer();
1054     if (!is<RenderText>(lastObject))
1055         return nullptr;
1056
1057     const RenderText& lastText = downcast<RenderText>(lastObject);
1058     unsigned firstSpace;
1059     if (lastText.is8Bit())
1060         firstSpace = findFirstTrailingSpace(lastText, lastText.characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1061     else
1062         firstSpace = findFirstTrailingSpace(lastText, lastText.characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
1063
1064     if (firstSpace == trailingSpaceRun->stop())
1065         return nullptr;
1066
1067     TextDirection direction = style().direction();
1068     bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
1069     if (firstSpace != trailingSpaceRun->start()) {
1070         BidiContext* baseContext = currentContext;
1071         while (BidiContext* parent = baseContext->parent())
1072             baseContext = parent;
1073
1074         std::unique_ptr<BidiRun> newTrailingRun = std::make_unique<BidiRun>(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->renderer(), baseContext, U_OTHER_NEUTRAL);
1075         trailingSpaceRun->m_stop = firstSpace;
1076         trailingSpaceRun = newTrailingRun.get();
1077         if (direction == LTR)
1078             bidiRuns.appendRun(WTFMove(newTrailingRun));
1079         else
1080             bidiRuns.prependRun(WTFMove(newTrailingRun));
1081         return trailingSpaceRun;
1082     }
1083     if (!shouldReorder)
1084         return trailingSpaceRun;
1085
1086     if (direction == LTR) {
1087         bidiRuns.moveRunToEnd(trailingSpaceRun);
1088         trailingSpaceRun->m_level = 0;
1089     } else {
1090         bidiRuns.moveRunToBeginning(trailingSpaceRun);
1091         trailingSpaceRun->m_level = 1;
1092     }
1093     return trailingSpaceRun;
1094 }
1095
1096 void RenderBlockFlow::appendFloatingObjectToLastLine(FloatingObject& floatingObject)
1097 {
1098     ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject.originatingLine());
1099     floatingObject.setOriginatingLine(lastRootBox());
1100     lastRootBox()->appendFloat(floatingObject.renderer());
1101 }
1102
1103 static inline void notifyResolverToResumeInIsolate(InlineBidiResolver& resolver, RenderObject* root, RenderObject* startObject)
1104 {
1105     if (root != startObject) {
1106         RenderObject* parent = startObject->parent();
1107         notifyResolverToResumeInIsolate(resolver, root, parent);
1108         notifyObserverEnteredObject(&resolver, startObject);
1109     }
1110 }
1111
1112 static inline void setUpResolverToResumeInIsolate(InlineBidiResolver& resolver, InlineBidiResolver& topResolver, BidiRun& isolatedRun, RenderObject* root, RenderObject* startObject)
1113 {
1114     // Set up m_whitespaceCollapsingState
1115     resolver.whitespaceCollapsingState() = topResolver.whitespaceCollapsingState();
1116     resolver.whitespaceCollapsingState().setCurrentTransition(topResolver.whitespaceCollapsingTransitionForIsolatedRun(isolatedRun));
1117
1118     // Set up m_nestedIsolateCount
1119     notifyResolverToResumeInIsolate(resolver, root, startObject);
1120 }
1121
1122 // FIXME: BidiResolver should have this logic.
1123 static inline void constructBidiRunsForSegment(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfRuns, VisualDirectionOverride override, bool previousLineBrokeCleanly)
1124 {
1125     // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1126     // of the resolver owning the runs.
1127     ASSERT(&topResolver.runs() == &bidiRuns);
1128     ASSERT(topResolver.position() != endOfRuns);
1129     RenderObject* currentRoot = topResolver.position().root();
1130     topResolver.createBidiRunsForLine(endOfRuns, override, previousLineBrokeCleanly);
1131
1132     while (!topResolver.isolatedRuns().isEmpty()) {
1133         // It does not matter which order we resolve the runs as long as we resolve them all.
1134         auto isolatedRun = WTFMove(topResolver.isolatedRuns().last());
1135         topResolver.isolatedRuns().removeLast();
1136         currentRoot = &isolatedRun.root;
1137
1138         RenderObject& startObject = isolatedRun.object;
1139
1140         // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
1141         // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1142         // tree to see which parent inline is the isolate. We could change enterIsolate
1143         // to take a RenderObject and do this logic there, but that would be a layering
1144         // violation for BidiResolver (which knows nothing about RenderObject).
1145         RenderInline* isolatedInline = downcast<RenderInline>(highestContainingIsolateWithinRoot(startObject, currentRoot));
1146         ASSERT(isolatedInline);
1147
1148         InlineBidiResolver isolatedResolver;
1149         EUnicodeBidi unicodeBidi = isolatedInline->style().unicodeBidi();
1150         TextDirection direction;
1151         if (unicodeBidi == Plaintext)
1152             determineDirectionality(direction, InlineIterator(isolatedInline, &isolatedRun.object, 0));
1153         else {
1154             ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
1155             direction = isolatedInline->style().direction();
1156         }
1157         isolatedResolver.setStatus(BidiStatus(direction, isOverride(unicodeBidi)));
1158
1159         setUpResolverToResumeInIsolate(isolatedResolver, topResolver, isolatedRun.runToReplace, isolatedInline, &startObject);
1160
1161         // The starting position is the beginning of the first run within the isolate that was identified
1162         // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1163         // first run within the isolate.
1164         InlineIterator iter = InlineIterator(isolatedInline, &startObject, isolatedRun.position);
1165         isolatedResolver.setPositionIgnoringNestedIsolates(iter);
1166
1167         // We stop at the next end of line; we may re-enter this isolate in the next call to constructBidiRuns().
1168         // FIXME: What should end and previousLineBrokeCleanly be?
1169         // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
1170         isolatedResolver.createBidiRunsForLine(endOfRuns, NoVisualOverride, previousLineBrokeCleanly);
1171         // Note that we do not delete the runs from the resolver.
1172         // We're not guaranteed to get any BidiRuns in the previous step. If we don't, we allow the placeholder
1173         // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1174         // the logically last run.
1175         if (isolatedResolver.runs().runCount())
1176             bidiRuns.replaceRunWithRuns(&isolatedRun.runToReplace, isolatedResolver.runs());
1177
1178         // If we encountered any nested isolate runs, just move them
1179         // to the top resolver's list for later processing.
1180         while (!isolatedResolver.isolatedRuns().isEmpty()) {
1181             auto runWithContext = WTFMove(isolatedResolver.isolatedRuns().last());
1182             isolatedResolver.isolatedRuns().removeLast();
1183             topResolver.setWhitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace, isolatedResolver.whitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace));
1184             topResolver.isolatedRuns().append(WTFMove(runWithContext));
1185         }
1186     }
1187 }
1188
1189 // This function constructs line boxes for all of the text runs in the resolver and computes their position.
1190 RootInlineBox* RenderBlockFlow::createLineBoxesFromBidiRuns(unsigned bidiLevel, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun, WordMeasurements& wordMeasurements)
1191 {
1192     if (!bidiRuns.runCount())
1193         return nullptr;
1194
1195     // FIXME: Why is this only done when we had runs?
1196     lineInfo.setLastLine(!end.renderer());
1197
1198     RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1199     if (!lineBox)
1200         return nullptr;
1201
1202     lineBox->setBidiLevel(bidiLevel);
1203     lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1204     
1205     bool isSVGRootInlineBox = is<SVGRootInlineBox>(*lineBox);
1206     
1207     GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1208     
1209     // Now we position all of our text runs horizontally.
1210     if (!isSVGRootInlineBox)
1211         computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
1212     
1213     // Now position our text runs vertically.
1214     computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1215     
1216     // SVG text layout code computes vertical & horizontal positions on its own.
1217     // Note that we still need to execute computeVerticalPositionsForLine() as
1218     // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1219     // contains reversed text or not. If we wouldn't do that editing and thus
1220     // text selection in RTL boxes would not work as expected.
1221     if (isSVGRootInlineBox) {
1222         ASSERT_WITH_SECURITY_IMPLICATION(isSVGText());
1223         downcast<SVGRootInlineBox>(*lineBox).computePerCharacterLayoutInformation();
1224     }
1225     
1226     // Compute our overflow now.
1227     lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1228     
1229     return lineBox;
1230 }
1231
1232 static void deleteLineRange(LineLayoutState& layoutState, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
1233 {
1234     RootInlineBox* boxToDelete = startLine;
1235     while (boxToDelete && boxToDelete != stopLine) {
1236         layoutState.updateRepaintRangeFromBox(boxToDelete);
1237         // Note: deleteLineRange(firstRootBox()) is not identical to deleteLineBoxTree().
1238         // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
1239         RootInlineBox* next = boxToDelete->nextRootBox();
1240         boxToDelete->deleteLine();
1241         boxToDelete = next;
1242     }
1243 }
1244
1245 static void repaintDirtyFloats(LineLayoutState::FloatList& floats)
1246 {
1247     // Floats that did not have layout did not repaint when we laid them out. They would have
1248     // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1249     // painted.
1250     for (auto& floatBox : floats) {
1251         if (floatBox->everHadLayout())
1252             continue;
1253         auto& box = floatBox->renderer();
1254         if (!box.x() && !box.y() && box.checkForRepaintDuringLayout())
1255             box.repaint();
1256     }
1257 }
1258
1259 void RenderBlockFlow::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
1260 {
1261     // We want to skip ahead to the first dirty line
1262     InlineBidiResolver resolver;
1263     RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
1264     
1265     unsigned consecutiveHyphenatedLines = 0;
1266     if (startLine) {
1267         for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1268             consecutiveHyphenatedLines++;
1269     }
1270
1271     // FIXME: This would make more sense outside of this function, but since
1272     // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1273     // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
1274     if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
1275         setNeedsLayout(MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
1276         if (!view().doingFullRepaint() && hasSelfPaintingLayer() && hasRepaintLayoutRects()) {
1277             // Because we waited until we were already inside layout to discover
1278             // that the block really needed a full layout, we missed our chance to repaint the layer
1279             // before layout started.  Luckily the layer has cached the repaint rect for its original
1280             // position and size, and so we can use that to make a repaint happen now.
1281             repaintUsingContainer(containerForRepaint(), repaintLayoutRects().m_repaintRect);
1282         }
1283     }
1284
1285     if (containsFloats())
1286         layoutState.floatList().setLastFloat(m_floatingObjects->set().last().get());
1287
1288     // We also find the first clean line and extract these lines.  We will add them back
1289     // if we determine that we're able to synchronize after handling all our dirty lines.
1290     InlineIterator cleanLineStart;
1291     BidiStatus cleanLineBidiStatus;
1292     if (!layoutState.isFullLayout() && startLine)
1293         determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
1294
1295     if (startLine) {
1296         if (!layoutState.usesRepaintBounds())
1297             layoutState.setRepaintRange(logicalHeight());
1298         deleteLineRange(layoutState, startLine);
1299     }
1300
1301     if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
1302         // If the last line before the start line ends with a line break that clear floats,
1303         // adjust the height accordingly.
1304         // A line break can be either the first or the last object on a line, depending on its direction.
1305         if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
1306             RenderObject* lastObject = &lastLeafChild->renderer();
1307             if (!lastObject->isBR())
1308                 lastObject = &lastRootBox()->firstLeafChild()->renderer();
1309             if (lastObject->isBR()) {
1310                 EClear clear = lastObject->style().clear();
1311                 if (clear != CNONE)
1312                     clearFloats(clear);
1313             }
1314         }
1315     }
1316
1317     layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
1318     linkToEndLineIfNeeded(layoutState);
1319     repaintDirtyFloats(layoutState.floatList());
1320 }
1321
1322 // Before restarting the layout loop with a new logicalHeight, remove all floats that were added and reset the resolver.
1323 inline const InlineIterator& RenderBlockFlow::restartLayoutRunsAndFloatsInRange(LayoutUnit oldLogicalHeight, LayoutUnit newLogicalHeight,  FloatingObject* lastFloatFromPreviousLine, InlineBidiResolver& resolver,  const InlineIterator& oldEnd)
1324 {
1325     removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1326     setLogicalHeight(newLogicalHeight);
1327     resolver.setPositionIgnoringNestedIsolates(oldEnd);
1328     return oldEnd;
1329 }
1330
1331 void RenderBlockFlow::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
1332 {
1333     const RenderStyle& styleToUse = style();
1334     bool paginated = view().layoutState() && view().layoutState()->isPaginated();
1335     LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
1336     InlineIterator end = resolver.position();
1337     bool checkForEndLineMatch = layoutState.endLine();
1338     RenderTextInfo renderTextInfo;
1339     VerticalPositionCache verticalPositionCache;
1340
1341     LineBreaker lineBreaker(*this);
1342
1343     while (!end.atEnd()) {
1344         // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
1345         if (checkForEndLineMatch) {
1346             layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
1347             if (layoutState.endLineMatched()) {
1348                 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
1349                 layoutState.marginInfo().clearMargin();
1350                 break;
1351             }
1352         }
1353
1354         lineWhitespaceCollapsingState.reset();
1355
1356         layoutState.lineInfo().setEmpty(true);
1357         layoutState.lineInfo().resetRunsFromLeadingWhitespace();
1358
1359         const InlineIterator oldEnd = end;
1360         bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
1361         FloatingObject* lastFloatFromPreviousLine = (containsFloats()) ? m_floatingObjects->set().last().get() : nullptr;
1362
1363         WordMeasurements wordMeasurements;
1364         end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
1365         cachePriorCharactersIfNeeded(renderTextInfo.lineBreakIterator);
1366         renderTextInfo.lineBreakIterator.resetPriorContext();
1367         if (resolver.position().atEnd()) {
1368             // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
1369             // Once BidiRunList is separated from BidiResolver this will not be needed.
1370             resolver.runs().clear();
1371             resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1372             layoutState.setCheckForFloatsFromLastLine(true);
1373             resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
1374             break;
1375         }
1376
1377         ASSERT(end != resolver.position());
1378
1379         // This is a short-cut for empty lines.
1380         if (layoutState.lineInfo().isEmpty()) {
1381             if (lastRootBox())
1382                 lastRootBox()->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
1383         } else {
1384             VisualDirectionOverride override = (styleToUse.rtlOrdering() == VisualOrder ? (styleToUse.direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
1385
1386             if (isNewUBAParagraph && styleToUse.unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1387                 TextDirection direction = styleToUse.direction();
1388                 determineDirectionality(direction, resolver.position());
1389                 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse.unicodeBidi())));
1390             }
1391             // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1392             BidiRunList<BidiRun>& bidiRuns = resolver.runs();
1393             constructBidiRunsForSegment(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
1394             ASSERT(resolver.position() == end);
1395
1396             BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : nullptr;
1397
1398             if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
1399                 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
1400                 consecutiveHyphenatedLines++;
1401             } else
1402                 consecutiveHyphenatedLines = 0;
1403
1404             // Now that the runs have been ordered, we create the line boxes.
1405             // At the same time we figure out where border/padding/margin should be applied for
1406             // inline flow boxes.
1407
1408             LayoutUnit oldLogicalHeight = logicalHeight();
1409             RootInlineBox* lineBox = createLineBoxesFromBidiRuns(resolver.status().context->level(), bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
1410
1411             bidiRuns.clear();
1412             resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1413
1414             if (lineBox) {
1415                 lineBox->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
1416                 if (layoutState.usesRepaintBounds())
1417                     layoutState.updateRepaintRangeFromBox(lineBox);
1418                 
1419                 LayoutUnit adjustment = 0;
1420                 bool overflowsRegion = false;
1421                 
1422                 layoutState.marginInfo().setAtBeforeSideOfBlock(false);
1423
1424                 if (paginated)
1425                     adjustLinePositionForPagination(lineBox, adjustment, overflowsRegion, layoutState.flowThread());
1426                 if (adjustment) {
1427                     IndentTextOrNot shouldIndentText = layoutState.lineInfo().isFirstLine() ? IndentText : DoNotIndentText;
1428                     LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, shouldIndentText);
1429                     lineBox->adjustBlockDirectionPosition(adjustment);
1430                     if (layoutState.usesRepaintBounds())
1431                         layoutState.updateRepaintRangeFromBox(lineBox);
1432
1433                     if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, shouldIndentText) != oldLineWidth) {
1434                         // We have to delete this line, remove all floats that got added, and let line layout re-run.
1435                         lineBox->deleteLine();
1436                         end = restartLayoutRunsAndFloatsInRange(oldLogicalHeight, oldLogicalHeight + adjustment, lastFloatFromPreviousLine, resolver, oldEnd);
1437                         continue;
1438                     }
1439
1440                     setLogicalHeight(lineBox->lineBottomWithLeading());
1441                 }
1442                     
1443                 if (paginated) {
1444                     if (layoutState.flowThread())
1445                         updateRegionForLine(lineBox);
1446                 }
1447             }
1448         }
1449
1450         for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1451             setStaticPositions(*this, *lineBreaker.positionedObjects()[i], DoNotIndentText);
1452
1453         if (!layoutState.lineInfo().isEmpty()) {
1454             layoutState.lineInfo().setFirstLine(false);
1455             clearFloats(lineBreaker.clear());
1456         }
1457
1458         if (m_floatingObjects && lastRootBox()) {
1459             const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1460             auto it = floatingObjectSet.begin();
1461             auto end = floatingObjectSet.end();
1462             if (auto* lastFloat = layoutState.floatList().lastFloat()) {
1463                 auto lastFloatIterator = floatingObjectSet.find<FloatingObject&, FloatingObjectHashTranslator>(*lastFloat);
1464                 ASSERT(lastFloatIterator != end);
1465                 ++lastFloatIterator;
1466                 it = lastFloatIterator;
1467             }
1468             for (; it != end; ++it) {
1469                 auto& floatingObject = *it;
1470                 appendFloatingObjectToLastLine(*floatingObject);
1471                 // If a float's geometry has changed, give up on syncing with clean lines.
1472                 auto* floatWithRect = layoutState.floatList().floatWithRect(floatingObject->renderer());
1473                 if (!floatWithRect || floatWithRect->rect() != floatingObject->frameRect())
1474                     checkForEndLineMatch = false;
1475             }
1476             layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
1477         }
1478
1479         lineWhitespaceCollapsingState.reset();
1480         resolver.setPosition(end, numberOfIsolateAncestors(end));
1481     }
1482
1483     // In case we already adjusted the line positions during this layout to avoid widows
1484     // then we need to ignore the possibility of having a new widows situation.
1485     // Otherwise, we risk leaving empty containers which is against the block fragmentation principles.
1486     if (paginated && !style().hasAutoWidows() && !didBreakAtLineToAvoidWidow()) {
1487         // Check the line boxes to make sure we didn't create unacceptable widows.
1488         // However, we'll prioritize orphans - so nothing we do here should create
1489         // a new orphan.
1490
1491         RootInlineBox* lineBox = lastRootBox();
1492
1493         // Count from the end of the block backwards, to see how many hanging
1494         // lines we have.
1495         RootInlineBox* firstLineInBlock = firstRootBox();
1496         int numLinesHanging = 1;
1497         while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1498             ++numLinesHanging;
1499             lineBox = lineBox->prevRootBox();
1500         }
1501
1502         // If there were no breaks in the block, we didn't create any widows.
1503         if (!lineBox || !lineBox->isFirstAfterPageBreak() || lineBox == firstLineInBlock)
1504             return;
1505
1506         if (numLinesHanging < style().widows()) {
1507             // We have detected a widow. Now we need to work out how many
1508             // lines there are on the previous page, and how many we need
1509             // to steal.
1510             int numLinesNeeded = style().widows() - numLinesHanging;
1511             RootInlineBox* currentFirstLineOfNewPage = lineBox;
1512
1513             // Count the number of lines in the previous page.
1514             lineBox = lineBox->prevRootBox();
1515             int numLinesInPreviousPage = 1;
1516             while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1517                 ++numLinesInPreviousPage;
1518                 lineBox = lineBox->prevRootBox();
1519             }
1520
1521             // If there was an explicit value for orphans, respect that. If not, we still
1522             // shouldn't create a situation where we make an orphan bigger than the initial value.
1523             // This means that setting widows implies we also care about orphans, but given
1524             // the specification says the initial orphan value is non-zero, this is ok. The
1525             // author is always free to set orphans explicitly as well.
1526             int orphans = style().hasAutoOrphans() ? style().initialOrphans() : style().orphans();
1527             int numLinesAvailable = numLinesInPreviousPage - orphans;
1528             if (numLinesAvailable <= 0)
1529                 return;
1530
1531             int numLinesToTake = std::min(numLinesAvailable, numLinesNeeded);
1532             // Wind back from our first widowed line.
1533             lineBox = currentFirstLineOfNewPage;
1534             for (int i = 0; i < numLinesToTake; ++i)
1535                 lineBox = lineBox->prevRootBox();
1536
1537             // We now want to break at this line. Remember for next layout and trigger relayout.
1538             setBreakAtLineToAvoidWidow(lineCount(lineBox));
1539             markLinesDirtyInBlockRange(lastRootBox()->lineBottomWithLeading(), lineBox->lineBottomWithLeading(), lineBox);
1540         }
1541     }
1542     clearDidBreakAtLineToAvoidWidow();
1543 }
1544
1545 void RenderBlockFlow::reattachCleanLineFloats(RootInlineBox& cleanLine, LayoutUnit delta, bool isFirstCleanLine)
1546 {
1547     auto* cleanLineFloats = cleanLine.floatsPtr();
1548     if (!cleanLineFloats)
1549         return;
1550
1551     for (auto* floatingBox : *cleanLineFloats) {
1552         auto* floatingObject = insertFloatingObject(*floatingBox);
1553         if (isFirstCleanLine && floatingObject->originatingLine()) {
1554             // Float box does not belong to this line anymore.
1555             ASSERT_WITH_SECURITY_IMPLICATION(cleanLine.prevRootBox() == floatingObject->originatingLine());
1556             cleanLine.removeFloat(*floatingBox);
1557             continue;
1558         }
1559         ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
1560         floatingObject->setOriginatingLine(&cleanLine);
1561         setLogicalHeight(logicalTopForChild(*floatingBox) - marginBeforeForChild(*floatingBox) + delta);
1562         positionNewFloats();
1563     }
1564 }
1565
1566 void RenderBlockFlow::linkToEndLineIfNeeded(LineLayoutState& layoutState)
1567 {
1568     auto* firstCleanLine = layoutState.endLine();
1569     if (firstCleanLine) {
1570         if (layoutState.endLineMatched()) {
1571             bool paginated = view().layoutState() && view().layoutState()->isPaginated();
1572             // Attach all the remaining lines, and then adjust their y-positions as needed.
1573             LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
1574             for (auto* line = firstCleanLine; line; line = line->nextRootBox()) {
1575                 line->attachLine();
1576                 if (paginated) {
1577                     delta -= line->paginationStrut();
1578                     bool overflowsRegion;
1579                     adjustLinePositionForPagination(line, delta, overflowsRegion, layoutState.flowThread());
1580                 }
1581                 if (delta) {
1582                     layoutState.updateRepaintRangeFromBox(line, delta);
1583                     line->adjustBlockDirectionPosition(delta);
1584                 }
1585                 if (layoutState.flowThread())
1586                     updateRegionForLine(line);
1587                 reattachCleanLineFloats(*line, delta, line == firstCleanLine);
1588             }
1589             setLogicalHeight(lastRootBox()->lineBottomWithLeading());
1590         } else {
1591             // Delete all the remaining lines.
1592             deleteLineRange(layoutState, layoutState.endLine());
1593         }
1594     }
1595     
1596     if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
1597         // In case we have a float on the last line, it might not be positioned up to now.
1598         // This has to be done before adding in the bottom border/padding, or the float will
1599         // include the padding incorrectly. -dwh
1600         if (layoutState.checkForFloatsFromLastLine()) {
1601             LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1602             LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
1603             auto newLineBox = std::make_unique<TrailingFloatsRootInlineBox>(*this);
1604             auto trailingFloatsLineBox = newLineBox.get();
1605             m_lineBoxes.appendLineBox(WTFMove(newLineBox));
1606             trailingFloatsLineBox->setConstructed();
1607             GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1608             VerticalPositionCache verticalPositionCache;
1609             LayoutUnit blockLogicalHeight = logicalHeight();
1610             trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1611             trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
1612             trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
1613             LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1614             LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
1615             trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
1616             if (layoutState.flowThread())
1617                 updateRegionForLine(trailingFloatsLineBox);
1618         }
1619
1620         const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1621         auto it = floatingObjectSet.begin();
1622         auto end = floatingObjectSet.end();
1623         if (auto* lastFloat = layoutState.floatList().lastFloat()) {
1624             auto lastFloatIterator = floatingObjectSet.find<FloatingObject&, FloatingObjectHashTranslator>(*lastFloat);
1625             ASSERT(lastFloatIterator != end);
1626             ++lastFloatIterator;
1627             it = lastFloatIterator;
1628         }
1629         for (; it != end; ++it)
1630             appendFloatingObjectToLastLine(**it);
1631         layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
1632     }
1633 }
1634
1635 void RenderBlockFlow::layoutLineBoxes(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
1636 {
1637     ASSERT(!m_simpleLineLayout);
1638
1639     setLogicalHeight(borderAndPaddingBefore());
1640     
1641     // Lay out our hypothetical grid line as though it occurs at the top of the block.
1642     if (view().layoutState() && view().layoutState()->lineGrid() == this)
1643         layoutLineGridBox();
1644
1645     RenderFlowThread* flowThread = flowThreadContainingBlock();
1646     bool clearLinesForPagination = firstRootBox() && flowThread && !flowThread->hasRegions();
1647
1648     // Figure out if we should clear out our line boxes.
1649     // FIXME: Handle resize eventually!
1650     bool isFullLayout = !firstRootBox() || selfNeedsLayout() || relayoutChildren || clearLinesForPagination;
1651     LineLayoutState layoutState(*this, isFullLayout, repaintLogicalTop, repaintLogicalBottom, flowThread);
1652
1653     if (isFullLayout)
1654         lineBoxes().deleteLineBoxes();
1655
1656     // Text truncation kicks in in two cases:
1657     //     1) If your overflow isn't visible and your text-overflow-mode isn't clip.
1658     //     2) If you're an anonymous block with a block parent that satisfies #1.
1659     // FIXME: CSS3 says that descendants that are clipped must also know how to truncate.  This is insanely
1660     // difficult to figure out in general (especially in the middle of doing layout), so we only handle the
1661     // simple case of an anonymous block truncating when it's parent is clipped.
1662     bool hasTextOverflow = (style().textOverflow() && hasOverflowClip())
1663         || (isAnonymousBlock() && parent() && parent()->isRenderBlock() && parent()->style().textOverflow() && parent()->hasOverflowClip());
1664
1665     // Walk all the lines and delete our ellipsis line boxes if they exist.
1666     if (hasTextOverflow)
1667          deleteEllipsisLineBoxes();
1668
1669     if (firstChild()) {
1670         // In full layout mode, clear the line boxes of children upfront. Otherwise,
1671         // siblings can run into stale root lineboxes during layout. Then layout
1672         // the replaced elements later. In partial layout mode, line boxes are not
1673         // deleted and only dirtied. In that case, we can layout the replaced
1674         // elements at the same time.
1675         bool hasInlineChild = false;
1676         Vector<RenderBox*> replacedChildren;
1677         for (InlineWalker walker(*this); !walker.atEnd(); walker.advance()) {
1678             RenderObject& o = *walker.current();
1679
1680             if (!hasInlineChild && o.isInline())
1681                 hasInlineChild = true;
1682
1683             if (o.isReplaced() || o.isFloating() || o.isOutOfFlowPositioned()) {
1684                 RenderBox& box = downcast<RenderBox>(o);
1685
1686                 if (relayoutChildren || box.hasRelativeDimensions())
1687                     box.setChildNeedsLayout(MarkOnlyThis);
1688
1689                 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1690                 if (relayoutChildren && box.needsPreferredWidthsRecalculation())
1691                     box.setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
1692
1693                 if (box.isOutOfFlowPositioned())
1694                     box.containingBlock()->insertPositionedObject(box);
1695                 else if (box.isFloating())
1696                     layoutState.floatList().append(FloatWithRect::create(box));
1697                 else if (isFullLayout || box.needsLayout()) {
1698                     // Replaced element.
1699                     if (isFullLayout && is<RenderRubyRun>(box)) {
1700                         // FIXME: This resets the overhanging margins that we set during line layout (see computeInlineDirectionPositionsForSegment)
1701                         // Find a more suitable place for this.
1702                         setMarginStartForChild(box, 0);
1703                         setMarginEndForChild(box, 0);
1704                     }
1705                     box.dirtyLineBoxes(isFullLayout);
1706                     if (isFullLayout)
1707                         replacedChildren.append(&box);
1708                     else
1709                         box.layoutIfNeeded();
1710                 }
1711             } else if (o.isTextOrLineBreak() || (is<RenderInline>(o) && !walker.atEndOfInline())) {
1712                 if (is<RenderInline>(o))
1713                     downcast<RenderInline>(o).updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
1714                 if (layoutState.isFullLayout() || o.selfNeedsLayout())
1715                     dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
1716                 o.clearNeedsLayout();
1717             }
1718         }
1719
1720         for (size_t i = 0; i < replacedChildren.size(); i++)
1721              replacedChildren[i]->layoutIfNeeded();
1722
1723         layoutRunsAndFloats(layoutState, hasInlineChild);
1724     }
1725
1726     // Expand the last line to accommodate Ruby and emphasis marks.
1727     int lastLineAnnotationsAdjustment = 0;
1728     if (lastRootBox()) {
1729         LayoutUnit lowestAllowedPosition = std::max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
1730         if (!style().isFlippedLinesWritingMode())
1731             lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1732         else
1733             lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
1734     }
1735     
1736     // Now do the handling of the bottom of the block, adding in our bottom border/padding and
1737     // determining the correct collapsed bottom margin information. This collapse is only necessary
1738     // if our last child was an anonymous inline block that might need to propagate margin information out to
1739     // us.
1740     LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight() + lastLineAnnotationsAdjustment;
1741     setLogicalHeight(logicalHeight() + afterEdge);
1742
1743     if (!firstRootBox() && hasLineIfEmpty())
1744         setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
1745
1746     // See if we have any lines that spill out of our block.  If we do, then we will possibly need to
1747     // truncate text.
1748     if (hasTextOverflow)
1749         checkLinesForTextOverflow();
1750 }
1751
1752 void RenderBlockFlow::checkFloatInCleanLine(RootInlineBox& cleanLine, RenderBox& floatBoxOnCleanLine, FloatWithRect& matchingFloatWithRect,
1753     bool& encounteredNewFloat, bool& dirtiedByFloat)
1754 {
1755     ASSERT_WITH_SECURITY_IMPLICATION(!floatBoxOnCleanLine.style().deletionHasBegun());
1756     if (&matchingFloatWithRect.renderer() != &floatBoxOnCleanLine) {
1757         encounteredNewFloat = true;
1758         return;
1759     }
1760     floatBoxOnCleanLine.layoutIfNeeded();
1761     LayoutRect originalFloatRect = matchingFloatWithRect.rect();
1762     LayoutSize newSize(
1763         floatBoxOnCleanLine.width() + floatBoxOnCleanLine.horizontalMarginExtent(),
1764         floatBoxOnCleanLine.height() + floatBoxOnCleanLine.verticalMarginExtent());
1765     
1766     // We have to reset the cap-height alignment done by the first-letter floats when initial-letter is set, so just always treat first-letter floats as dirty.
1767     if (originalFloatRect.size() == newSize && (floatBoxOnCleanLine.style().styleType() != FIRST_LETTER || !floatBoxOnCleanLine.style().initialLetterDrop()))
1768         return;
1769
1770     LayoutUnit floatTop = isHorizontalWritingMode() ? originalFloatRect.y() : originalFloatRect.x();
1771     LayoutUnit floatHeight = isHorizontalWritingMode() ? std::max(originalFloatRect.height(), newSize.height())
1772         : std::max(originalFloatRect.width(), newSize.width());
1773     floatHeight = std::min(floatHeight, LayoutUnit::max() - floatTop);
1774     cleanLine.markDirty();
1775     markLinesDirtyInBlockRange(cleanLine.lineBottomWithLeading(), floatTop + floatHeight, &cleanLine);
1776     LayoutRect newFloatRect = originalFloatRect;
1777     newFloatRect.setSize(newSize);
1778     matchingFloatWithRect.adjustRect(newFloatRect);
1779     dirtiedByFloat = true;
1780 }
1781
1782 RootInlineBox* RenderBlockFlow::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
1783 {
1784     RootInlineBox* currentLine = nullptr;
1785     RootInlineBox* lastLine = nullptr;
1786
1787     // FIXME: This entire float-checking block needs to be broken into a new function.
1788     auto& floats = layoutState.floatList();
1789     bool dirtiedByFloat = false;
1790     if (!layoutState.isFullLayout()) {
1791         // Paginate all of the clean lines.
1792         bool paginated = view().layoutState() && view().layoutState()->isPaginated();
1793         LayoutUnit paginationDelta = 0;
1794         auto floatsIterator = floats.begin();
1795         auto end = floats.end();
1796         for (currentLine = firstRootBox(); currentLine && !currentLine->isDirty(); currentLine = currentLine->nextRootBox()) {
1797             if (paginated) {
1798                 if (lineWidthForPaginatedLineChanged(currentLine, 0, layoutState.flowThread())) {
1799                     currentLine->markDirty();
1800                     break;
1801                 }
1802                 paginationDelta -= currentLine->paginationStrut();
1803                 bool overflowsRegion;
1804                 adjustLinePositionForPagination(currentLine, paginationDelta, overflowsRegion, layoutState.flowThread());
1805                 if (paginationDelta) {
1806                     if (containsFloats() || !floats.isEmpty()) {
1807                         // FIXME: Do better eventually.  For now if we ever shift because of pagination and floats are present just go to a full layout.
1808                         layoutState.markForFullLayout();
1809                         break;
1810                     }
1811
1812                     layoutState.updateRepaintRangeFromBox(currentLine, paginationDelta);
1813                     currentLine->adjustBlockDirectionPosition(paginationDelta);
1814                 }
1815                 if (layoutState.flowThread())
1816                     updateRegionForLine(currentLine);
1817             }
1818
1819             if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1820                 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1821                 bool encounteredNewFloat = false;
1822                 for (auto* floatBoxOnCleanLine : *cleanLineFloats) {
1823                     ASSERT(floatsIterator != end);
1824                     checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1825                     ++floatsIterator;
1826                     if (floatsIterator == end || encounteredNewFloat) {
1827                         layoutState.markForFullLayout();
1828                         break;
1829                     }
1830                 }
1831                 if (dirtiedByFloat || encounteredNewFloat)
1832                     break;
1833             }
1834         }
1835         // Check if a new float has been inserted after the last known float.
1836         if (!currentLine && floatsIterator != end)
1837             layoutState.markForFullLayout();
1838     }
1839
1840     if (layoutState.isFullLayout()) {
1841         m_lineBoxes.deleteLineBoxTree();
1842         currentLine = nullptr;
1843         ASSERT(!firstRootBox() && !lastRootBox());
1844     } else {
1845         if (currentLine) {
1846             // We have a dirty line.
1847             if (RootInlineBox* prevRootBox = currentLine->prevRootBox()) {
1848                 // We have a previous line.
1849                 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak()
1850                     || !prevRootBox->lineBreakObj()
1851                     || (is<RenderText>(*prevRootBox->lineBreakObj())
1852                     && prevRootBox->lineBreakPos() >= downcast<RenderText>(*prevRootBox->lineBreakObj()).textLength()))) {
1853                     // The previous line didn't break cleanly or broke at a newline
1854                     // that has been deleted, so treat it as dirty too.
1855                     currentLine = prevRootBox;
1856                 }
1857             }
1858         }
1859         // If we have no dirty lines, then last is just the last root box.
1860         lastLine = currentLine ? currentLine->prevRootBox() : lastRootBox();
1861     }
1862
1863     if (!floats.isEmpty()) {
1864         LayoutUnit savedLogicalHeight = logicalHeight();
1865         // Restore floats from clean lines.
1866         RootInlineBox* line = firstRootBox();
1867         while (line != currentLine) {
1868             if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1869                 for (auto it = cleanLineFloats->begin(), end = cleanLineFloats->end(); it != end; ++it) {
1870                     auto* floatingBox = *it;
1871                     auto* floatingObject = insertFloatingObject(*floatingBox);
1872                     ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
1873                     floatingObject->setOriginatingLine(line);
1874                     setLogicalHeight(logicalTopForChild(*floatingBox) - marginBeforeForChild(*floatingBox));
1875                     positionNewFloats();
1876                     floats.setLastCleanFloat(*floatingBox);
1877                 }
1878             }
1879             line = line->nextRootBox();
1880         }
1881         setLogicalHeight(savedLogicalHeight);
1882     }
1883
1884     layoutState.lineInfo().setFirstLine(!lastLine);
1885     layoutState.lineInfo().setPreviousLineBrokeCleanly(!lastLine || lastLine->endsWithBreak());
1886
1887     if (lastLine) {
1888         setLogicalHeight(lastLine->lineBottomWithLeading());
1889         InlineIterator iter = InlineIterator(this, lastLine->lineBreakObj(), lastLine->lineBreakPos());
1890         resolver.setPosition(iter, numberOfIsolateAncestors(iter));
1891         resolver.setStatus(lastLine->lineBreakBidiStatus());
1892     } else {
1893         TextDirection direction = style().direction();
1894         if (style().unicodeBidi() == Plaintext)
1895             determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(*this), 0));
1896         resolver.setStatus(BidiStatus(direction, isOverride(style().unicodeBidi())));
1897         InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(*this, &resolver), 0);
1898         resolver.setPosition(iter, numberOfIsolateAncestors(iter));
1899     }
1900     return currentLine;
1901 }
1902
1903 void RenderBlockFlow::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
1904 {
1905     auto iteratorForFirstDirtyFloat = [](LineLayoutState::FloatList& floats) {
1906         auto lastCleanFloat = floats.lastCleanFloat();
1907         if (!lastCleanFloat)
1908             return floats.begin();
1909         auto* lastCleanFloatWithRect = floats.floatWithRect(*lastCleanFloat);
1910         ASSERT(lastCleanFloatWithRect);
1911         return ++floats.find(*lastCleanFloatWithRect);
1912     };
1913
1914     ASSERT(!layoutState.endLine());
1915     auto floatsIterator = iteratorForFirstDirtyFloat(layoutState.floatList());
1916     auto end = layoutState.floatList().end();
1917     RootInlineBox* lastLine = nullptr;
1918     for (RootInlineBox* currentLine = startLine->nextRootBox(); currentLine; currentLine = currentLine->nextRootBox()) {
1919         if (!currentLine->isDirty()) {
1920             if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1921                 bool encounteredNewFloat = false;
1922                 bool dirtiedByFloat = false;
1923                 for (auto* floatBoxOnCleanLine : *cleanLineFloats) {
1924                     ASSERT(floatsIterator != end);
1925                     checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1926                     ++floatsIterator;
1927                     if (floatsIterator == end || encounteredNewFloat)
1928                         return;
1929                 }
1930             }
1931         }
1932         if (currentLine->isDirty())
1933             lastLine = nullptr;
1934         else if (!lastLine)
1935             lastLine = currentLine;
1936     }
1937
1938     if (!lastLine)
1939         return;
1940
1941     // At this point, |last| is the first line in a run of clean lines that ends with the last line
1942     // in the block.
1943     RootInlineBox* previousLine = lastLine->prevRootBox();
1944     cleanLineStart = InlineIterator(this, previousLine->lineBreakObj(), previousLine->lineBreakPos());
1945     cleanLineBidiStatus = previousLine->lineBreakBidiStatus();
1946     layoutState.setEndLineLogicalTop(previousLine->lineBottomWithLeading());
1947
1948     for (RootInlineBox* line = lastLine; line; line = line->nextRootBox()) {
1949         // Disconnect all line boxes from their render objects while preserving their connections to one another.
1950         line->extractLine();
1951     }
1952     layoutState.setEndLine(lastLine);
1953 }
1954
1955 bool RenderBlockFlow::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
1956 {
1957     LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1958
1959     bool paginated = view().layoutState() && view().layoutState()->isPaginated();
1960     if (paginated && layoutState.flowThread()) {
1961         // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1962         // in a different available line width.
1963         for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1964             if (paginated) {
1965                 // This isn't the real move we're going to do, so don't update the line box's pagination
1966                 // strut yet.
1967                 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
1968                 bool overflowsRegion;
1969                 lineDelta -= oldPaginationStrut;
1970                 adjustLinePositionForPagination(lineBox, lineDelta, overflowsRegion, layoutState.flowThread());
1971                 lineBox->setPaginationStrut(oldPaginationStrut);
1972             }
1973             if (lineWidthForPaginatedLineChanged(lineBox, lineDelta, layoutState.flowThread()))
1974                 return false;
1975         }
1976     }
1977     
1978     if (!lineDelta || !m_floatingObjects)
1979         return true;
1980     
1981     // See if any floats end in the range along which we want to shift the lines vertically.
1982     LayoutUnit logicalTop = std::min(logicalHeight(), layoutState.endLineLogicalTop());
1983
1984     RootInlineBox* lastLine = layoutState.endLine();
1985     while (RootInlineBox* nextLine = lastLine->nextRootBox())
1986         lastLine = nextLine;
1987
1988     LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
1989
1990     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1991     auto end = floatingObjectSet.end();
1992     for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1993         const auto& floatingObject = *it->get();
1994         if (logicalBottomForFloat(floatingObject) >= logicalTop && logicalBottomForFloat(floatingObject) < logicalBottom)
1995             return false;
1996     }
1997
1998     return true;
1999 }
2000
2001 bool RenderBlockFlow::lineWidthForPaginatedLineChanged(RootInlineBox* rootBox, LayoutUnit lineDelta, RenderFlowThread* flowThread) const
2002 {
2003     if (!flowThread)
2004         return false;
2005
2006     RenderRegion* currentRegion = regionAtBlockOffset(rootBox->lineTopWithLeading() + lineDelta);
2007     // Just bail if the region didn't change.
2008     if (rootBox->containingRegion() == currentRegion)
2009         return false;
2010     return rootBox->paginatedLineWidth() != availableLogicalWidthForContent(currentRegion);
2011 }
2012
2013 bool RenderBlockFlow::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
2014 {
2015     if (resolver.position() == endLineStart) {
2016         if (resolver.status() != endLineStatus)
2017             return false;
2018         return checkPaginationAndFloatsAtEndLine(layoutState);
2019     }
2020
2021     // The first clean line doesn't match, but we can check a handful of following lines to try
2022     // to match back up.
2023     static const int numLines = 8; // The # of lines we're willing to match against.
2024     RootInlineBox* originalEndLine = layoutState.endLine();
2025     RootInlineBox* line = originalEndLine;
2026     for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
2027         if (line->lineBreakObj() == resolver.position().renderer() && line->lineBreakPos() == resolver.position().offset()) {
2028             // We have a match.
2029             if (line->lineBreakBidiStatus() != resolver.status())
2030                 return false; // ...but the bidi state doesn't match.
2031             
2032             bool matched = false;
2033             RootInlineBox* result = line->nextRootBox();
2034             layoutState.setEndLine(result);
2035             if (result) {
2036                 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
2037                 matched = checkPaginationAndFloatsAtEndLine(layoutState);
2038             }
2039
2040             // Now delete the lines that we failed to sync.
2041             deleteLineRange(layoutState, originalEndLine, result);
2042             return matched;
2043         }
2044     }
2045
2046     return false;
2047 }
2048
2049 bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
2050 {
2051     ASSERT(inlineObj->parent() == this);
2052
2053     InlineIterator it(this, inlineObj, 0);
2054     // FIXME: We should pass correct value for WhitespacePosition.
2055     while (!it.atEnd() && !requiresLineBox(it))
2056         it.increment();
2057
2058     return !it.atEnd();
2059 }
2060
2061 void RenderBlockFlow::addOverflowFromInlineChildren()
2062 {
2063     if (auto layout = simpleLineLayout()) {
2064         ASSERT(!hasOverflowClip());
2065         SimpleLineLayout::collectFlowOverflow(*this, *layout);
2066         return;
2067     }
2068     LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : LayoutUnit();
2069     // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
2070     if (hasOverflowClip() && !endPadding && element() && element()->isRootEditableElement() && style().isLeftToRightDirection())
2071         endPadding = 1;
2072     for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2073         addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
2074         RenderRegion* region = flowThreadContainingBlock() ? curr->containingRegion() : nullptr;
2075         if (region)
2076             region->addLayoutOverflowForBox(this, curr->paddedLayoutOverflowRect(endPadding));
2077         if (!hasOverflowClip()) {
2078             LayoutRect childVisualOverflowRect = curr->visualOverflowRect(curr->lineTop(), curr->lineBottom());
2079             addVisualOverflow(childVisualOverflowRect);
2080             if (region)
2081                 region->addVisualOverflowForBox(this, childVisualOverflowRect);
2082         }
2083     }
2084 }
2085
2086 void RenderBlockFlow::deleteEllipsisLineBoxes()
2087 {
2088     ETextAlign textAlign = style().textAlign();
2089     bool ltr = style().isLeftToRightDirection();
2090     IndentTextOrNot shouldIndentText = IndentText;
2091     for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2092         if (curr->hasEllipsisBox()) {
2093             curr->clearTruncation();
2094
2095             // Shift the line back where it belongs if we cannot accomodate an ellipsis.
2096             float logicalLeft = logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
2097             float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), DoNotIndentText) - logicalLeft;
2098             float totalLogicalWidth = curr->logicalWidth();
2099             updateLogicalWidthForAlignment(textAlign, curr, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2100
2101             if (ltr)
2102                 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2103             else
2104                 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2105         }
2106         shouldIndentText = DoNotIndentText;
2107     }
2108 }
2109
2110 void RenderBlockFlow::checkLinesForTextOverflow()
2111 {
2112     // Determine the width of the ellipsis using the current font.
2113     // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2114     const FontCascade& font = style().fontCascade();
2115     static NeverDestroyed<AtomicString> ellipsisStr(&horizontalEllipsis, 1);
2116     const FontCascade& firstLineFont = firstLineStyle().fontCascade();
2117     float firstLineEllipsisWidth = firstLineFont.width(constructTextRun(&horizontalEllipsis, 1, firstLineStyle()));
2118     float ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(&horizontalEllipsis, 1, style()));
2119
2120     // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2121     // if the right edge of a line box exceeds that.  For RTL, we use the left edge of the padding box and
2122     // check the left edge of the line box to see if it is less
2123     // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2124     bool ltr = style().isLeftToRightDirection();
2125     ETextAlign textAlign = style().textAlign();
2126     bool firstLine = true;
2127     for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2128         IndentTextOrNot shouldIndentText = firstLine ? IndentText : DoNotIndentText;
2129         LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), shouldIndentText);
2130         LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
2131         LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
2132         if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
2133             // This line spills out of our box in the appropriate direction.  Now we need to see if the line
2134             // can be truncated.  In order for truncation to be possible, the line must have sufficient space to
2135             // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2136             // space.
2137             LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
2138             LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
2139             if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2140                 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2141
2142                 float logicalLeft = 0; // We are only interested in the delta from the base position.
2143                 float truncatedWidth = availableLogicalWidthForLine(curr->lineTop(), shouldIndentText);
2144                 updateLogicalWidthForAlignment(textAlign, curr, nullptr, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
2145                 if (ltr)
2146                     curr->adjustLogicalPosition(logicalLeft, 0);
2147                 else
2148                     curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2149             }
2150         }
2151         firstLine = false;
2152     }
2153 }
2154
2155 bool RenderBlockFlow::positionNewFloatOnLine(const FloatingObject& newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
2156 {
2157     if (!positionNewFloats())
2158         return false;
2159
2160     width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
2161
2162     // We only connect floats to lines for pagination purposes if the floats occur at the start of
2163     // the line and the previous line had a hard break (so this line is either the first in the block
2164     // or follows a <br>).
2165     if (!newFloat.paginationStrut() || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
2166         return true;
2167
2168     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2169     ASSERT(floatingObjectSet.last().get() == &newFloat);
2170
2171     LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
2172     LayoutUnit paginationStrut = newFloat.paginationStrut();
2173
2174     if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
2175         return true;
2176
2177     auto it = floatingObjectSet.end();
2178     --it; // Last float is newFloat, skip that one.
2179     auto begin = floatingObjectSet.begin();
2180     while (it != begin) {
2181         --it;
2182         auto& floatingObject = *it->get();
2183         if (&floatingObject == lastFloatFromPreviousLine)
2184             break;
2185         if (logicalTopForFloat(floatingObject) == logicalHeight() + lineInfo.floatPaginationStrut()) {
2186             floatingObject.setPaginationStrut(paginationStrut + floatingObject.paginationStrut());
2187             RenderBox& floatBox = floatingObject.renderer();
2188             setLogicalTopForChild(floatBox, logicalTopForChild(floatBox) + marginBeforeForChild(floatBox) + paginationStrut);
2189
2190             if (updateRegionRangeForBoxChild(floatBox))
2191                 floatBox.setNeedsLayout(MarkOnlyThis);
2192             else if (is<RenderBlock>(floatBox))
2193                 downcast<RenderBlock>(floatBox).setChildNeedsLayout(MarkOnlyThis);
2194             floatBox.layoutIfNeeded();
2195
2196             // Save the old logical top before calling removePlacedObject which will set
2197             // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
2198             LayoutUnit oldLogicalTop = logicalTopForFloat(floatingObject);
2199             m_floatingObjects->removePlacedObject(&floatingObject);
2200             setLogicalTopForFloat(floatingObject, oldLogicalTop + paginationStrut);
2201             m_floatingObjects->addPlacedObject(&floatingObject);
2202         }
2203     }
2204
2205     // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2206     // no content, then we don't want to improperly grow the height of the block.
2207     lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
2208     return true;
2209 }
2210
2211 LayoutUnit RenderBlockFlow::startAlignedOffsetForLine(LayoutUnit position, IndentTextOrNot shouldIndentText)
2212 {
2213     ETextAlign textAlign = style().textAlign();
2214     bool shouldApplyIndentText = false;
2215     switch (textAlign) {
2216     case LEFT:
2217     case WEBKIT_LEFT:
2218         shouldApplyIndentText = style().isLeftToRightDirection();
2219         break;
2220     case RIGHT:
2221     case WEBKIT_RIGHT:
2222         shouldApplyIndentText = !style().isLeftToRightDirection();
2223         break;
2224     case TASTART:
2225         shouldApplyIndentText = true;
2226         break;
2227     default:
2228         shouldApplyIndentText = false;
2229     }
2230     // <rdar://problem/15427571>
2231     // https://bugs.webkit.org/show_bug.cgi?id=124522
2232     // This quirk is for legacy content that doesn't work properly with the center positioning scheme
2233     // being honored (e.g., epubs).
2234     if (shouldApplyIndentText || settings().useLegacyTextAlignPositionedElementBehavior()) // FIXME: Handle TAEND here
2235         return startOffsetForLine(position, shouldIndentText);
2236
2237     // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
2238     float totalLogicalWidth = 0;
2239     float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), DoNotIndentText);
2240     float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), DoNotIndentText) - logicalLeft;
2241
2242     // FIXME: Bug 129311: We need to pass a valid RootInlineBox here, considering the bidi level used to construct the line.
2243     updateLogicalWidthForAlignment(textAlign, 0, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
2244
2245     if (!style().isLeftToRightDirection())
2246         return logicalWidth() - logicalLeft;
2247     return logicalLeft;
2248 }
2249
2250 void RenderBlockFlow::updateRegionForLine(RootInlineBox* lineBox) const
2251 {
2252     ASSERT(lineBox);
2253
2254     if (!hasRegionRangeInFlowThread())
2255         lineBox->clearContainingRegion();
2256     else {
2257         if (auto containingRegion = regionAtBlockOffset(lineBox->lineTopWithLeading()))
2258             lineBox->setContainingRegion(*containingRegion);
2259         else
2260             lineBox->clearContainingRegion();
2261     }
2262
2263     RootInlineBox* prevLineBox = lineBox->prevRootBox();
2264     if (!prevLineBox)
2265         return;
2266
2267     // This check is more accurate than the one in |adjustLinePositionForPagination| because it takes into
2268     // account just the container changes between lines. The before mentioned function doesn't set the flag
2269     // correctly if the line is positioned at the top of the last fragment container.
2270     if (lineBox->containingRegion() != prevLineBox->containingRegion())
2271         lineBox->setIsFirstAfterPageBreak(true);
2272 }
2273
2274 }