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