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