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