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