blob: 937a1e8a13962080fe5c0ad5132fec7b7410ed5b [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
commit-queue@webkit.org17761402013-04-17 18:48:56 +00005 * Copyright (C) 2013 ChangSeok Oh <shivamidow@gmail.com>
zoltan@webkit.org64be1222013-11-15 21:40:59 +00006 * Copyright (C) 2013 Adobe Systems Inc. All right reserved.
kociendabb0c24b2001-08-24 14:24:40 +00007 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000022 *
kociendabb0c24b2001-08-24 14:24:40 +000023 */
darinbe4c67d2005-12-19 19:53:12 +000024
mjsb64c50a2005-10-03 21:13:12 +000025#include "config.h"
darin36d11362006-04-11 16:30:21 +000026
weinig@apple.comcef4e1e2013-10-19 03:14:44 +000027#include "AXObjectCache.h"
mitz@apple.com4c1ff322009-07-13 00:54:12 +000028#include "BidiResolver.h"
mmaxfield@apple.com167a0e92015-03-06 19:06:30 +000029#include "BreakingContext.h"
bjonesbe@adobe.com67478092013-09-09 22:18:17 +000030#include "FloatingObjects.h"
darin@apple.com5917cb12017-11-23 17:32:42 +000031#include "HTMLParserIdioms.h"
akling@apple.comd3ec5ef2013-11-07 03:30:11 +000032#include "InlineElementBox.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000033#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000034#include "InlineTextBox.h"
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +000035#include "InlineTextBoxStyle.h"
jfernandez@igalia.com06053022017-11-22 23:46:15 +000036#include "LayoutState.h"
zoltan@webkit.org4c74e8d2013-09-13 17:59:12 +000037#include "LineLayoutState.h"
ggarenec11e5b2007-02-25 02:14:54 +000038#include "Logging.h"
bjonesbe@adobe.com24199752013-10-08 23:20:42 +000039#include "RenderBlockFlow.h"
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +000040#include "RenderFragmentContainer.h"
hyatt@apple.com4e0bf862017-09-27 20:54:17 +000041#include "RenderFragmentedFlow.h"
antti@apple.com8d8ae712013-09-18 18:04:32 +000042#include "RenderLineBreak.h"
mmaxfield@apple.com8301e832014-10-09 01:14:30 +000043#include "RenderRubyBase.h"
44#include "RenderRubyText.h"
hyattd8048342006-05-31 01:48:18 +000045#include "RenderView.h"
ossy@webkit.org66d8c0a2014-02-05 11:42:35 +000046#include "SVGRootInlineBox.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000047#include "Settings.h"
antti@apple.com940f5872013-10-24 20:31:11 +000048#include "SimpleLineLayoutFunctions.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000049#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000050#include "VerticalPositionCache.h"
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000051#include <wtf/StdLibExtras.h>
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000052
darinb9481ed2006-03-20 02:57:59 +000053namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000054
leviw@chromium.orge7812f32012-02-07 23:46:40 +000055static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000056{
57 while (!iter.atEnd()) {
58 if (iter.atParagraphSeparator())
59 return;
60 if (UChar current = iter.current()) {
darin@apple.com2eb5f4d2013-10-12 04:16:42 +000061 UCharDirection charDirection = u_charDirection(current);
62 if (charDirection == U_LEFT_TO_RIGHT) {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000063 dir = LTR;
64 return;
65 }
darin@apple.com2eb5f4d2013-10-12 04:16:42 +000066 if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000067 dir = RTL;
68 return;
69 }
70 }
71 iter.increment();
72 }
73}
74
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +000075inline std::unique_ptr<BidiRun> createRun(int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
eric@webkit.org5bee2942011-04-08 02:12:31 +000076{
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +000077 return std::make_unique<BidiRun>(start, end, obj, resolver.context(), resolver.dir());
eric@webkit.org5bee2942011-04-08 02:12:31 +000078}
79
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +000080void RenderBlockFlow::appendRunsForObject(BidiRunList<BidiRun>* runs, int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +000081{
leviw@chromium.orgd8df17d2012-05-24 21:47:47 +000082 if (start > end || shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +000083 return;
hyatt85586af2003-02-19 23:22:42 +000084
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +000085 LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
86 bool haveNextTransition = (lineWhitespaceCollapsingState.currentTransition() < lineWhitespaceCollapsingState.numTransitions());
87 InlineIterator nextTransition;
88 if (haveNextTransition)
89 nextTransition = lineWhitespaceCollapsingState.transitions()[lineWhitespaceCollapsingState.currentTransition()];
90 if (lineWhitespaceCollapsingState.betweenTransitions()) {
91 if (!haveNextTransition || (&obj != nextTransition.renderer()))
hyatt33f8d492002-11-12 21:44:52 +000092 return;
eric@webkit.org060caf62011-05-03 22:11:39 +000093 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +000094 // adjust our start.
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +000095 start = nextTransition.offset();
96 lineWhitespaceCollapsingState.incrementCurrentTransition();
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +000097 if (start < end) {
98 appendRunsForObject(runs, start, end, obj, resolver);
99 return;
100 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000101 } else {
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000102 if (!haveNextTransition || (&obj != nextTransition.renderer())) {
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +0000103 if (runs)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +0000104 runs->appendRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000105 return;
106 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000107
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000108 // An end transition has been encountered within our object. We need to append a run with our endpoint.
109 if (static_cast<int>(nextTransition.offset() + 1) <= end) {
110 lineWhitespaceCollapsingState.incrementCurrentTransition();
mmaxfield@apple.com0682d512014-03-25 20:15:14 +0000111 // The end of the line is before the object we're inspecting. Skip everything and return
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000112 if (nextTransition.refersToEndOfPreviousNode())
mmaxfield@apple.com0682d512014-03-25 20:15:14 +0000113 return;
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000114 if (static_cast<int>(nextTransition.offset() + 1) > start && runs)
115 runs->appendRun(createRun(start, nextTransition.offset() + 1, obj, resolver));
116 appendRunsForObject(runs, nextTransition.offset() + 1, end, obj, resolver);
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +0000117 } else if (runs)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +0000118 runs->appendRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000119 }
120}
121
akling@apple.comb5f24642013-11-06 04:47:12 +0000122std::unique_ptr<RootInlineBox> RenderBlockFlow::createRootInlineBox()
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000123{
akling@apple.comb5f24642013-11-06 04:47:12 +0000124 return std::make_unique<RootInlineBox>(*this);
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000125}
126
127RootInlineBox* RenderBlockFlow::createAndAppendRootInlineBox()
128{
akling@apple.comb5f24642013-11-06 04:47:12 +0000129 auto newRootBox = createRootInlineBox();
130 RootInlineBox* rootBox = newRootBox.get();
aestes@apple.com13aae082016-01-02 08:03:08 +0000131 m_lineBoxes.appendLineBox(WTFMove(newRootBox));
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000132
akling@apple.comee3c8df2013-11-06 08:09:44 +0000133 if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && firstRootBox() == rootBox) {
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000134 if (AXObjectCache* cache = document().existingAXObjectCache())
zalan@apple.com11b9d412017-05-12 03:18:04 +0000135 cache->deferRecomputeIsIgnored(element());
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000136 }
137
138 return rootBox;
139}
140
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000141static inline InlineBox* createInlineBoxForRenderer(RenderObject* renderer, bool isRootLineBox, bool isOnlyRun = false)
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000142{
143 if (isRootLineBox)
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000144 return downcast<RenderBlockFlow>(*renderer).createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000145
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000146 if (is<RenderText>(*renderer))
147 return downcast<RenderText>(*renderer).createInlineTextBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000148
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000149 if (is<RenderBox>(*renderer)) {
akling@apple.comb5f24642013-11-06 04:47:12 +0000150 // FIXME: This is terrible. This branch returns an *owned* pointer!
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000151 return downcast<RenderBox>(*renderer).createInlineBox().release();
akling@apple.comb5f24642013-11-06 04:47:12 +0000152 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000153
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000154 if (is<RenderLineBreak>(*renderer)) {
akling@apple.comb5f24642013-11-06 04:47:12 +0000155 // FIXME: This is terrible. This branch returns an *owned* pointer!
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000156 auto inlineBox = downcast<RenderLineBreak>(*renderer).createInlineBox().release();
antti@apple.com9d8157e2013-09-17 15:13:37 +0000157 // 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.)
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000159 inlineBox->setBehavesLikeText(isOnlyRun || renderer->document().inNoQuirksMode() || renderer->isLineBreakOpportunity());
antti@apple.com9d8157e2013-09-17 15:13:37 +0000160 return inlineBox;
161 }
162
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000163 return downcast<RenderInline>(*renderer).createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000164}
165
weinig@apple.com12840dc2013-10-22 23:59:08 +0000166static inline void dirtyLineBoxesForRenderer(RenderObject& renderer, bool fullLayout)
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000167{
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000168 if (is<RenderText>(renderer)) {
169 RenderText& renderText = downcast<RenderText>(renderer);
esprehn@chromium.org7745ffb2013-02-19 21:51:19 +0000170 updateCounterIfNeeded(renderText);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000171 renderText.dirtyLineBoxes(fullLayout);
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000172 } else if (is<RenderLineBreak>(renderer))
173 downcast<RenderLineBreak>(renderer).dirtyLineBoxes(fullLayout);
antti@apple.com9d8157e2013-09-17 15:13:37 +0000174 else
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000175 downcast<RenderInline>(renderer).dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000176}
177
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000178static 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
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000188InlineFlowBox* RenderBlockFlow::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000189{
190 // See if we have an unconstructed line box for this object that is also
191 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000192 unsigned lineDepth = 1;
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000193 InlineFlowBox* parentBox = nullptr;
194 InlineFlowBox* result = nullptr;
akling@apple.com827be9c2013-10-29 02:58:43 +0000195 bool hasDefaultLineBoxContain = style().lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000196 do {
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000197 ASSERT_WITH_SECURITY_IMPLICATION(is<RenderInline>(*obj) || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000198
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000199 RenderInline* inlineFlow = obj != this ? downcast<RenderInline>(obj) : nullptr;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000200
hyatt1d5d87b2007-04-24 04:55:54 +0000201 // Get the last box we made for this render object.
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000202 parentBox = inlineFlow ? inlineFlow->lastLineBox() : downcast<RenderBlockFlow>(*obj).lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000203
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000204 // If this box or its ancestor is constructed then it is from a previous line, and we need
205 // to make a new box for our line. If this box or its ancestor is unconstructed but it has
hyatt1d5d87b2007-04-24 04:55:54 +0000206 // something following it on the line, then we know we have to make a new box
207 // as well. In this situation our inline has actually been split in two on
208 // the same line (this can happen with very fancy language mixtures).
209 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000210 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000211 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000212 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000213 // We need to make a new box for this render object. Once
214 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000215 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000216 parentBox = downcast<InlineFlowBox>(newBox);
antti@apple.comb0608f62013-09-28 18:30:16 +0000217 parentBox->setIsFirstLine(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000218 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000219 if (!hasDefaultLineBoxContain)
220 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000221 constructedNewBox = true;
222 }
mitz@apple.come1364202008-02-28 01:06:41 +0000223
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000224 if (constructedNewBox || canUseExistingParentBox) {
225 if (!result)
226 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000227
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000228 // If we have hit the block itself, then |box| represents the root
229 // inline box for the line, and it doesn't have to be appended to any parent
230 // inline.
231 if (childBox)
232 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000233
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000234 if (!constructedNewBox || obj == this)
235 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000236
eric@webkit.org060caf62011-05-03 22:11:39 +0000237 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000238 }
mitz@apple.come1364202008-02-28 01:06:41 +0000239
hyatt1d5d87b2007-04-24 04:55:54 +0000240 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
241 // intermediate inline flows.
242 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000243
hyatt1d5d87b2007-04-24 04:55:54 +0000244 } while (true);
245
246 return result;
hyattffe78712003-02-11 01:59:29 +0000247}
248
darin@apple.com5917cb12017-11-23 17:32:42 +0000249template<typename CharacterType> static inline bool endsWithHTMLSpaces(const CharacterType* characters, unsigned position, unsigned end)
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000250{
darin@apple.com5917cb12017-11-23 17:32:42 +0000251 for (unsigned i = position; i < end; ++i) {
252 if (!isHTMLSpace(characters[i]))
253 return false;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000254 }
darin@apple.com5917cb12017-11-23 17:32:42 +0000255 return true;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000256}
257
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000258static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000259{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000260 BidiRun* run = bidiRuns.logicallyLastRun();
261 if (!run)
262 return true;
darin@apple.com5917cb12017-11-23 17:32:42 +0000263 if (!is<RenderText>(run->renderer()))
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000264 return false;
darin@apple.com5917cb12017-11-23 17:32:42 +0000265 auto& text = downcast<RenderText>(run->renderer()).text();
266 unsigned position = run->stop();
267 unsigned length = text.length();
268 if (text.is8Bit())
269 return endsWithHTMLSpaces(text.characters8(), position, length);
270 return endsWithHTMLSpaces(text.characters16(), position, length);
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000271}
272
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000273RootInlineBox* RenderBlockFlow::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000274{
275 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000276
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000277 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000278 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000279 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000280
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000281 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000282 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000283 bool isOnlyRun = (runCount == 1);
akling@apple.com7506fda2013-11-07 10:12:36 +0000284 if (runCount == 2 && !r->renderer().isListMarker())
285 isOnlyRun = (!style().isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->renderer().isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000286
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000287 if (lineInfo.isEmpty())
288 continue;
289
akling@apple.com7506fda2013-11-07 10:12:36 +0000290 InlineBox* box = createInlineBoxForRenderer(&r->renderer(), false, isOnlyRun);
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000291 r->setBox(box);
hyattffe78712003-02-11 01:59:29 +0000292
akling@apple.com0b8172b72013-08-31 18:34:23 +0000293 if (!rootHasSelectedChildren && box->renderer().selectionState() != RenderObject::SelectionNone)
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000294 rootHasSelectedChildren = true;
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000295
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000296 // If we have no parent box yet, or if the run is not simply a sibling,
297 // then we need to construct inline boxes as necessary to properly enclose the
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000298 // run's inline box. Segments can only be siblings at the root level, as
299 // they are positioned separately.
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000300 if (!parentBox || &parentBox->renderer() != r->renderer().parent()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000301 // Create new inline boxes all the way back to the appropriate insertion point.
hyatt@apple.com8e8cd252015-04-02 18:34:57 +0000302 RenderObject* parentToUse = r->renderer().parent();
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000303 parentBox = createLineBoxes(parentToUse, lineInfo, box);
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000304 } else {
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000305 // Append the inline box to this line.
306 parentBox->addToLine(box);
307 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000308
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000309 bool visuallyOrdered = r->renderer().style().rtlOrdering() == Order::Visual;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000310 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000311
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000312 if (is<InlineTextBox>(*box)) {
313 auto& textBox = downcast<InlineTextBox>(*box);
314 textBox.setStart(r->m_start);
315 textBox.setLen(r->m_stop - r->m_start);
316 textBox.setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000317 if (r->m_hasHyphen)
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000318 textBox.setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000319 }
hyattffe78712003-02-11 01:59:29 +0000320 }
321
322 // We should have a root inline box. It should be unconstructed and
323 // be the last continuation of our line list.
akling@apple.comee3c8df2013-11-06 08:09:44 +0000324 ASSERT(lastRootBox() && !lastRootBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000325
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000326 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
327 // from the bidi runs walk above has a selection state.
328 if (rootHasSelectedChildren)
akling@apple.comee3c8df2013-11-06 08:09:44 +0000329 lastRootBox()->root().setHasSelectedChildren(true);
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000330
hyattffe78712003-02-11 01:59:29 +0000331 // Set bits on our inline flow boxes that indicate which sides should
332 // paint borders/margins/padding. This knowledge will ultimately be used when
333 // we determine the horizontal positions and widths of all the inline boxes on
334 // the line.
hyatt@apple.com3ab5f732016-03-25 19:25:05 +0000335 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->renderer().isText() ? !reachedEndOfTextRenderer(bidiRuns) : !is<RenderInline>(bidiRuns.logicallyLastRun()->renderer());
akling@apple.com7506fda2013-11-07 10:12:36 +0000336 lastRootBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, &bidiRuns.logicallyLastRun()->renderer());
hyattffe78712003-02-11 01:59:29 +0000337
338 // Now mark the line boxes as being constructed.
akling@apple.comee3c8df2013-11-06 08:09:44 +0000339 lastRootBox()->setConstructed();
hyattffe78712003-02-11 01:59:29 +0000340
341 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000342 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000343}
344
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000345TextAlignMode RenderBlockFlow::textAlignmentForLine(bool endsWithSoftBreak) const
mitz@apple.com390fa322011-02-24 23:07:06 +0000346{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000347 TextAlignMode alignment = style().textAlign();
zoltan@webkit.orgcb4f0e42014-08-13 18:00:27 +0000348#if ENABLE(CSS3_TEXT)
349 TextJustify textJustify = style().textJustify();
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000350 if (alignment == TextAlignMode::Justify && textJustify == TextJustify::None)
351 return style().direction() == LTR ? TextAlignMode::Left : TextAlignMode::Right;
zoltan@webkit.orgcb4f0e42014-08-13 18:00:27 +0000352#endif
353
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000354 if (endsWithSoftBreak)
355 return alignment;
mitz@apple.com390fa322011-02-24 23:07:06 +0000356
zoltan@webkit.org14989c32014-08-14 21:36:40 +0000357#if !ENABLE(CSS3_TEXT)
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000358 return (alignment == TextAlignMode::Justify) ? TextAlignMode::Start : alignment;
zoltan@webkit.org14989c32014-08-14 21:36:40 +0000359#else
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000360 if (alignment != TextAlignMode::Justify)
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000361 return alignment;
362
363 TextAlignLast alignmentLast = style().textAlignLast();
364 switch (alignmentLast) {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000365 case TextAlignLast::Start:
366 return TextAlignMode::Start;
367 case TextAlignLast::End:
368 return TextAlignMode::End;
369 case TextAlignLast::Left:
370 return TextAlignMode::Left;
371 case TextAlignLast::Right:
372 return TextAlignMode::Right;
373 case TextAlignLast::Center:
374 return TextAlignMode::Center;
375 case TextAlignLast::Justify:
376 return TextAlignMode::Justify;
377 case TextAlignLast::Auto:
378 if (textJustify == TextJustify::Distribute)
379 return TextAlignMode::Justify;
380 return TextAlignMode::Start;
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000381 }
mitz@apple.com390fa322011-02-24 23:07:06 +0000382 return alignment;
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000383#endif
mitz@apple.com390fa322011-02-24 23:07:06 +0000384}
385
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000386static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
387{
388 // The direction of the block should determine what happens with wide lines.
389 // In particular with RTL blocks, wide lines should still spill out to the left.
390 if (isLeftToRightDirection) {
391 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
andersca@apple.com86298632013-11-10 19:32:33 +0000392 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000393 return;
394 }
395
396 if (trailingSpaceRun)
akling@apple.com7506fda2013-11-07 10:12:36 +0000397 trailingSpaceRun->box()->setLogicalWidth(0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000398 else if (totalLogicalWidth > availableLogicalWidth)
399 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
400}
401
402static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
403{
404 // Wide lines spill out of the block based off direction.
405 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
406 // side of the block.
407 if (isLeftToRightDirection) {
408 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000409 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
410 trailingSpaceRun->box()->setLogicalWidth(0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000411 }
mmaxfield@apple.come6da30e2015-07-25 03:51:06 +0000412 logicalLeft += std::max(0.f, availableLogicalWidth - totalLogicalWidth);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000413 return;
414 }
415
416 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
andersca@apple.com86298632013-11-10 19:32:33 +0000417 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
akling@apple.com7506fda2013-11-07 10:12:36 +0000418 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000419 } else
420 logicalLeft += availableLogicalWidth - totalLogicalWidth;
421}
422
423static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
424{
425 float trailingSpaceWidth = 0;
426 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000427 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
andersca@apple.com86298632013-11-10 19:32:33 +0000428 trailingSpaceWidth = std::min(trailingSpaceRun->box()->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
429 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceWidth));
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000430 }
431 if (isLeftToRightDirection)
andersca@apple.com86298632013-11-10 19:32:33 +0000432 logicalLeft += std::max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000433 else
434 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
435}
436
weinig@apple.com12840dc2013-10-22 23:59:08 +0000437void RenderBlockFlow::setMarginsForRubyRun(BidiRun* run, RenderRubyRun& renderer, RenderObject* previousObject, const LineInfo& lineInfo)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000438{
mmaxfield@apple.com3273fd72014-12-16 22:53:19 +0000439 float startOverhang;
440 float endOverhang;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000441 RenderObject* nextObject = 0;
442 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000443 if (!runWithNextObject->renderer().isOutOfFlowPositioned() && !runWithNextObject->box()->isLineBreak()) {
444 nextObject = &runWithNextObject->renderer();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000445 break;
446 }
447 }
akling@apple.com827be9c2013-10-29 02:58:43 +0000448 renderer.getOverhang(lineInfo.isFirstLine(), renderer.style().isLeftToRightDirection() ? previousObject : nextObject, renderer.style().isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000449 setMarginStartForChild(renderer, -startOverhang);
450 setMarginEndForChild(renderer, -endOverhang);
451}
452
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000453static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText& renderer, float xPos, const LineInfo& lineInfo,
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000454 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000455{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000456 HashSet<const Font*> fallbackFonts;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000457 GlyphOverflow glyphOverflow;
antti@apple.comb0608f62013-09-28 18:30:16 +0000458
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000459 const FontCascade& font = lineStyle(*renderer.parent(), lineInfo).fontCascade();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000460 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
461 if (lineBox->fitsToGlyphs()) {
462 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
463 // will keep us from computing glyph bounds in nearly all cases.
464 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
akling@apple.com7506fda2013-11-07 10:12:36 +0000465 int baselineShift = lineBox->verticalPositionForBox(run->box(), verticalPositionCache);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000466 int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
467 int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
468 int boxAscent = font.fontMetrics().ascent() - baselineShift;
469 int boxDescent = font.fontMetrics().descent() + baselineShift;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000470 if (boxAscent > rootDescent || boxDescent > rootAscent)
471 glyphOverflow.computeBounds = true;
472 }
473
leviw@chromium.orgd32486e2012-03-16 10:52:56 +0000474 LayoutUnit hyphenWidth = 0;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000475 if (downcast<InlineTextBox>(*run->box()).hasHyphen())
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000476 hyphenWidth = measureHyphenWidth(renderer, font, &fallbackFonts);
antti@apple.comb0608f62013-09-28 18:30:16 +0000477
enrica@apple.com885c84d2012-10-10 05:48:51 +0000478 float measuredWidth = 0;
479
mmaxfield@apple.com2ef57ea2015-10-13 23:40:38 +0000480 bool kerningIsEnabled = font.enableKerning();
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000481 bool canUseSimpleFontCodePath = renderer.canUseSimpleFontCodePath();
enrica@apple.com885c84d2012-10-10 05:48:51 +0000482
483 // Since we don't cache glyph overflows, we need to re-measure the run if
484 // the style is linebox-contain: glyph.
leviw@chromium.orgd70f4cc2013-03-28 21:03:44 +0000485 if (!lineBox->fitsToGlyphs() && canUseSimpleFontCodePath) {
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +0000486 unsigned lastEndOffset = run->m_start;
zalan@apple.com9cc58512016-12-16 06:32:36 +0000487 bool atFirstWordMeasurement = true;
enrica@apple.com885c84d2012-10-10 05:48:51 +0000488 for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000489 WordMeasurement& wordMeasurement = wordMeasurements[i];
490 if (wordMeasurement.width <= 0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
enrica@apple.com885c84d2012-10-10 05:48:51 +0000491 continue;
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000492 if (wordMeasurement.renderer != &renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
enrica@apple.com885c84d2012-10-10 05:48:51 +0000493 continue;
494
495 lastEndOffset = wordMeasurement.endOffset;
496 if (kerningIsEnabled && lastEndOffset == run->m_stop) {
dino@apple.com7c50e7c2013-03-18 18:01:05 +0000497 int wordLength = lastEndOffset - wordMeasurement.startOffset;
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000498 GlyphOverflow overflow;
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000499 measuredWidth += renderer.width(wordMeasurement.startOffset, wordLength, xPos + measuredWidth, lineInfo.isFirstLine(),
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000500 &wordMeasurement.fallbackFonts, &overflow);
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000501 UChar c = renderer.characterAt(wordMeasurement.startOffset);
zalan@apple.com9cc58512016-12-16 06:32:36 +0000502 // renderer.width() omits word-spacing value for leading whitespace, so let's just add it back here.
commit-queue@webkit.org7c53b232017-09-28 06:57:08 +0000503 if (!atFirstWordMeasurement && FontCascade::treatAsSpace(c))
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000504 measuredWidth += renderer.style().fontCascade().wordSpacing();
enrica@apple.com885c84d2012-10-10 05:48:51 +0000505 } else
506 measuredWidth += wordMeasurement.width;
zalan@apple.com9cc58512016-12-16 06:32:36 +0000507 atFirstWordMeasurement = false;
508
enrica@apple.com885c84d2012-10-10 05:48:51 +0000509 if (!wordMeasurement.fallbackFonts.isEmpty()) {
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +0000510 HashSet<const Font*>::const_iterator end = wordMeasurement.fallbackFonts.end();
511 for (HashSet<const Font*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
512 fallbackFonts.add(*it);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000513 }
514 }
515 if (measuredWidth && lastEndOffset != run->m_stop) {
516 // If we don't have enough cached data, we'll measure the run again.
517 measuredWidth = 0;
518 fallbackFonts.clear();
519 }
520 }
enrica@apple.com885c84d2012-10-10 05:48:51 +0000521
522 if (!measuredWidth)
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000523 measuredWidth = renderer.width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000524
akling@apple.com7506fda2013-11-07 10:12:36 +0000525 run->box()->setLogicalWidth(measuredWidth + hyphenWidth);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000526 if (!fallbackFonts.isEmpty()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000527 ASSERT(run->box()->behavesLikeText());
antti@apple.com5a8f7942015-01-22 21:57:04 +0000528 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<InlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000529 ASSERT(it->value.first.isEmpty());
weinig@apple.comb6e19c52017-10-12 15:38:42 +0000530 it->value.first = copyToVector(fallbackFonts);
akling@apple.com7506fda2013-11-07 10:12:36 +0000531 run->box()->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000532 }
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +0000533
534 // Include text decoration visual overflow as part of the glyph overflow.
commit-queue@webkit.org02cb0002018-05-28 00:49:21 +0000535 if (!renderer.style().textDecorationsInEffect().isEmpty())
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000536 glyphOverflow.extendTo(visualOverflowForDecorations(run->box()->lineStyle(), downcast<InlineTextBox>(run->box())));
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +0000537
538 if (!glyphOverflow.isEmpty()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000539 ASSERT(run->box()->behavesLikeText());
antti@apple.com5a8f7942015-01-22 21:57:04 +0000540 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(downcast<InlineTextBox>(run->box()), std::make_pair(Vector<const Font*>(), GlyphOverflow())).iterator;
benjamin@webkit.orgee554052012-10-07 23:12:07 +0000541 it->value.second = glyphOverflow;
akling@apple.com7506fda2013-11-07 10:12:36 +0000542 run->box()->clearKnownToHaveNoOverflow();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000543 }
544}
545
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000546void RenderBlockFlow::updateRubyForJustifiedText(RenderRubyRun& rubyRun, BidiRun& r, const Vector<unsigned, 16>& expansionOpportunities, unsigned& expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth, size_t& i)
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000547{
548 if (!rubyRun.rubyBase() || !rubyRun.rubyBase()->firstRootBox() || rubyRun.rubyBase()->firstRootBox()->nextRootBox() || !r.renderer().style().collapseWhiteSpace())
549 return;
550
551 auto& rubyBase = *rubyRun.rubyBase();
552 auto& rootBox = *rubyBase.firstRootBox();
553
554 float totalExpansion = 0;
555 unsigned totalOpportunitiesInRun = 0;
556 for (auto* leafChild = rootBox.firstLeafChild(); leafChild; leafChild = leafChild->nextLeafChild()) {
557 if (!leafChild->isInlineTextBox())
558 continue;
559
560 unsigned opportunitiesInRun = expansionOpportunities[i++];
561 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
562 auto expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
563 totalExpansion += expansion;
564 totalOpportunitiesInRun += opportunitiesInRun;
565 }
566
rego@igalia.com8bac95d2018-05-14 15:20:47 +0000567 ASSERT(!rubyRun.hasOverrideContentLogicalWidth());
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000568 float newBaseWidth = rubyRun.logicalWidth() + totalExpansion + marginStartForChild(rubyRun) + marginEndForChild(rubyRun);
569 float newRubyRunWidth = rubyRun.logicalWidth() + totalExpansion;
570 rubyBase.setInitialOffset((newRubyRunWidth - newBaseWidth) / 2);
rego@igalia.com8bac95d2018-05-14 15:20:47 +0000571 rubyRun.setOverrideContentLogicalWidth(newRubyRunWidth);
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000572 rubyRun.setNeedsLayout(MarkOnlyThis);
573 rootBox.markDirty();
574 if (RenderRubyText* rubyText = rubyRun.rubyText()) {
575 if (RootInlineBox* textRootBox = rubyText->firstRootBox())
576 textRootBox->markDirty();
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000577 }
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000578 rubyRun.layoutBlock(true);
rego@igalia.com8bac95d2018-05-14 15:20:47 +0000579 rubyRun.clearOverrideContentLogicalWidth();
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000580 r.box()->setExpansion(newRubyRunWidth - r.box()->logicalWidth());
581
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000582 totalLogicalWidth += totalExpansion;
583 expansionOpportunityCount -= totalOpportunitiesInRun;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000584}
585
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000586void RenderBlockFlow::computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, const Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float totalLogicalWidth, float availableLogicalWidth)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000587{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000588 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
589 return;
590
591 size_t i = 0;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000592 for (BidiRun* run = firstRun; run; run = run->next()) {
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000593 if (!run->box() || run == trailingSpaceRun)
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000594 continue;
595
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000596 if (is<RenderText>(run->renderer())) {
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000597 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000598
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000599 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
600
601 // Only justify text if whitespace is collapsed.
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000602 if (run->renderer().style().collapseWhiteSpace()) {
603 InlineTextBox& textBox = downcast<InlineTextBox>(*run->box());
mmaxfield@apple.coma9f58d02014-10-02 22:25:50 +0000604 float expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000605 textBox.setExpansion(expansion);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000606 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000607 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000608 expansionOpportunityCount -= opportunitiesInRun;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000609 } else if (is<RenderRubyRun>(run->renderer()))
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000610 updateRubyForJustifiedText(downcast<RenderRubyRun>(run->renderer()), *run, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth, i);
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000611
612 if (!expansionOpportunityCount)
613 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000614 }
615}
616
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000617void RenderBlockFlow::updateLogicalWidthForAlignment(const TextAlignMode& textAlign, const RootInlineBox* rootInlineBox, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000618{
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000619 TextDirection direction;
620 if (rootInlineBox && style().unicodeBidi() == Plaintext)
621 direction = rootInlineBox->direction();
622 else
623 direction = style().direction();
624
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000625 // Armed with the total width of the line (without justification),
626 // we now examine our text-align property in order to determine where to position the
627 // objects horizontally. The total width of the line can be increased if we end up
628 // justifying text.
629 switch (textAlign) {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000630 case TextAlignMode::Left:
631 case TextAlignMode::WebKitLeft:
akling@apple.com827be9c2013-10-29 02:58:43 +0000632 updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000633 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000634 case TextAlignMode::Right:
635 case TextAlignMode::WebKitRight:
akling@apple.com827be9c2013-10-29 02:58:43 +0000636 updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000637 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000638 case TextAlignMode::Center:
639 case TextAlignMode::WebKitCenter:
akling@apple.com827be9c2013-10-29 02:58:43 +0000640 updateLogicalWidthForCenterAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000641 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000642 case TextAlignMode::Justify:
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000643 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
644 if (expansionOpportunityCount) {
645 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000646 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
647 trailingSpaceRun->box()->setLogicalWidth(0);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000648 }
649 break;
650 }
joepeck@webkit.orgaa676ee52014-01-28 04:04:52 +0000651 FALLTHROUGH;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000652 case TextAlignMode::Start:
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000653 if (direction == LTR)
akling@apple.com827be9c2013-10-29 02:58:43 +0000654 updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000655 else
akling@apple.com827be9c2013-10-29 02:58:43 +0000656 updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000657 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000658 case TextAlignMode::End:
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000659 if (direction == LTR)
akling@apple.com827be9c2013-10-29 02:58:43 +0000660 updateLogicalWidthForRightAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000661 else
akling@apple.com827be9c2013-10-29 02:58:43 +0000662 updateLogicalWidthForLeftAlignedBlock(style().isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000663 break;
664 }
665}
666
zalan@apple.com64761fe2016-03-02 21:42:22 +0000667static void updateLogicalInlinePositions(RenderBlockFlow& block, float& lineLogicalLeft, float& lineLogicalRight, float& availableLogicalWidth, bool firstLine,
antti@apple.comae85e112017-08-31 23:27:02 +0000668 IndentTextOrNot shouldIndentText, LayoutUnit boxLogicalHeight)
commit-queue@webkit.orgab781b02013-04-03 01:32:24 +0000669{
weinig@apple.come9621c32014-01-04 20:53:51 +0000670 LayoutUnit lineLogicalHeight = block.minLineHeightForReplacedRenderer(firstLine, boxLogicalHeight);
antti@apple.comae85e112017-08-31 23:27:02 +0000671 lineLogicalLeft = block.logicalLeftOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
672 lineLogicalRight = block.logicalRightOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
robert@webkit.org0903cf52012-12-11 18:14:16 +0000673 availableLogicalWidth = lineLogicalRight - lineLogicalLeft;
674}
675
weinig@apple.come9621c32014-01-04 20:53:51 +0000676void RenderBlockFlow::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
hyattffe78712003-02-11 01:59:29 +0000677{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000678 TextAlignMode textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000679
robert@webkit.org328ecd02012-08-09 21:12:44 +0000680 // 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
681 // box is only affected if it is the first child of its parent element."
commit-queue@webkit.orgab781b02013-04-03 01:32:24 +0000682 // CSS3 "text-indent", "-webkit-each-line" affects the first line of the block container as well as each line after a forced line break,
683 // but does not affect lines after a soft wrap break.
684 bool isFirstLine = lineInfo.isFirstLine() && !(isAnonymousBlock() && parent()->firstChild() != this);
685 bool isAfterHardLineBreak = lineBox->prevRootBox() && lineBox->prevRootBox()->endsWithBreak();
akling@apple.com827be9c2013-10-29 02:58:43 +0000686 IndentTextOrNot shouldIndentText = requiresIndent(isFirstLine, isAfterHardLineBreak, style());
robert@webkit.org0903cf52012-12-11 18:14:16 +0000687 float lineLogicalLeft;
688 float lineLogicalRight;
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000689 float availableLogicalWidth;
antti@apple.comae85e112017-08-31 23:27:02 +0000690 updateLogicalInlinePositions(*this, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, 0);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000691 bool needsWordSpacing;
robert@webkit.org0903cf52012-12-11 18:14:16 +0000692
akling@apple.com7506fda2013-11-07 10:12:36 +0000693 if (firstRun && firstRun->renderer().isReplaced()) {
cdumez@apple.com0abff8b2014-10-17 21:25:10 +0000694 RenderBox& renderBox = downcast<RenderBox>(firstRun->renderer());
antti@apple.comae85e112017-08-31 23:27:02 +0000695 updateLogicalInlinePositions(*this, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, renderBox.logicalHeight());
robert@webkit.org0903cf52012-12-11 18:14:16 +0000696 }
697
698 computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, lineLogicalLeft, availableLogicalWidth, firstRun, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000699 // The widths of all runs are now known. We can now place every inline box (and
700 // compute accurate widths for the inline flow boxes).
701 needsWordSpacing = false;
mmaxfield@apple.comb0a4a3f2014-04-30 00:36:21 +0000702 lineBox->placeBoxesInInlineDirection(lineLogicalLeft, needsWordSpacing);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000703}
mitz@apple.com390fa322011-02-24 23:07:06 +0000704
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000705static inline ExpansionBehavior expansionBehaviorForInlineTextBox(RenderBlockFlow& block, InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, TextAlignMode textAlign, bool isAfterExpansion)
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000706{
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000707 // Tatechuyoko is modeled as the Object Replacement Character (U+FFFC), which can never have expansion opportunities inside nor intrinsically adjacent to it.
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +0000708 if (textBox.renderer().style().textCombine() == TextCombine::Horizontal)
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000709 return ForbidLeadingExpansion | ForbidTrailingExpansion;
710
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000711 ExpansionBehavior result = 0;
712 bool setLeadingExpansion = false;
713 bool setTrailingExpansion = false;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000714 if (textAlign == TextAlignMode::Justify) {
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000715 // If the next box is ruby, and we're justifying, and the first box in the ruby base has a leading expansion, and we are a text box, then force a trailing expansion.
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000716 if (nextRun && is<RenderRubyRun>(nextRun->renderer()) && downcast<RenderRubyRun>(nextRun->renderer()).rubyBase() && nextRun->renderer().style().collapseWhiteSpace()) {
717 auto& rubyBase = *downcast<RenderRubyRun>(nextRun->renderer()).rubyBase();
718 if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
719 if (auto* leafChild = rubyBase.firstRootBox()->firstLeafChild()) {
720 if (is<InlineTextBox>(*leafChild)) {
721 // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
722 if (FontCascade::leadingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
723 setTrailingExpansion = true;
724 result |= ForceTrailingExpansion;
725 }
726 }
727 }
728 }
729 }
730 // Same thing, except if we're following a ruby
731 if (previousRun && is<RenderRubyRun>(previousRun->renderer()) && downcast<RenderRubyRun>(previousRun->renderer()).rubyBase() && previousRun->renderer().style().collapseWhiteSpace()) {
732 auto& rubyBase = *downcast<RenderRubyRun>(previousRun->renderer()).rubyBase();
733 if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
734 if (auto* leafChild = rubyBase.firstRootBox()->lastLeafChild()) {
735 if (is<InlineTextBox>(*leafChild)) {
736 // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
737 if (FontCascade::trailingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
738 setLeadingExpansion = true;
739 result |= ForceLeadingExpansion;
740 }
741 }
742 }
743 }
744 }
745 // If we're the first box inside a ruby base, forbid a leading expansion, and vice-versa
746 if (is<RenderRubyBase>(block)) {
747 RenderRubyBase& rubyBase = downcast<RenderRubyBase>(block);
748 if (&textBox == rubyBase.firstRootBox()->firstLeafChild()) {
749 setLeadingExpansion = true;
750 result |= ForbidLeadingExpansion;
751 } if (&textBox == rubyBase.firstRootBox()->lastLeafChild()) {
752 setTrailingExpansion = true;
753 result |= ForbidTrailingExpansion;
754 }
755 }
756 }
757 if (!setLeadingExpansion)
758 result |= isAfterExpansion ? ForbidLeadingExpansion : AllowLeadingExpansion;
759 if (!setTrailingExpansion)
760 result |= AllowTrailingExpansion;
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000761 return result;
762}
763
764static inline void applyExpansionBehavior(InlineTextBox& textBox, ExpansionBehavior expansionBehavior)
765{
766 switch (expansionBehavior & LeadingExpansionMask) {
767 case ForceLeadingExpansion:
768 textBox.setForceLeadingExpansion();
769 break;
770 case ForbidLeadingExpansion:
771 textBox.setCanHaveLeadingExpansion(false);
772 break;
773 case AllowLeadingExpansion:
774 textBox.setCanHaveLeadingExpansion(true);
775 break;
776 default:
777 ASSERT_NOT_REACHED();
778 break;
779 }
780 switch (expansionBehavior & TrailingExpansionMask) {
781 case ForceTrailingExpansion:
782 textBox.setForceTrailingExpansion();
783 break;
784 case ForbidTrailingExpansion:
785 textBox.setCanHaveTrailingExpansion(false);
786 break;
787 case AllowTrailingExpansion:
788 textBox.setCanHaveTrailingExpansion(true);
789 break;
790 default:
791 ASSERT_NOT_REACHED();
792 break;
793 }
794}
795
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000796static bool inlineAncestorHasStartBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
797{
798 bool isLTR = block.style().isLeftToRightDirection();
799 for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
800 if ((isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0)
801 || (!isLTR && currentBox->marginBorderPaddingLogicalRight() > 0))
802 return true;
803 }
804 return false;
805}
806
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000807static bool inlineAncestorHasEndBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
808{
809 bool isLTR = block.style().isLeftToRightDirection();
810 for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
811 if ((isLTR && currentBox->marginBorderPaddingLogicalRight() > 0)
812 || (!isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0))
813 return true;
814 }
815 return false;
816}
817
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000818static bool isLastInFlowRun(BidiRun& runToCheck)
819{
820 for (auto* run = runToCheck.next(); run; run = run->next()) {
821 if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak())
822 continue;
823 return false;
824 }
825 return true;
826}
827
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000828BidiRun* RenderBlockFlow::computeInlineDirectionPositionsForSegment(RootInlineBox* lineBox, const LineInfo& lineInfo, TextAlignMode textAlign, float& logicalLeft,
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000829 float& availableLogicalWidth, BidiRun* firstRun, BidiRun* trailingSpaceRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache,
830 WordMeasurements& wordMeasurements)
831{
darin06dcb9c2005-08-15 04:31:09 +0000832 bool needsWordSpacing = false;
commit-queue@webkit.org02cb0002018-05-28 00:49:21 +0000833 bool canHangPunctuationAtStart = style().hangingPunctuation().contains(HangingPunctuation::First);
834 bool canHangPunctuationAtEnd = style().hangingPunctuation().contains(HangingPunctuation::Last);
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000835 bool isLTR = style().isLeftToRightDirection();
mitz@apple.com390fa322011-02-24 23:07:06 +0000836 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000837 unsigned expansionOpportunityCount = 0;
commit-queue@webkit.org01ad5842014-12-16 19:28:57 +0000838 bool isAfterExpansion = is<RenderRubyBase>(*this) ? downcast<RenderRubyBase>(*this).isAfterExpansion() : true;
mitz@apple.com86470c82011-01-27 01:39:27 +0000839 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000840
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000841 BidiRun* run = firstRun;
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000842 BidiRun* previousRun = nullptr;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000843 for (; run; run = run->next()) {
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000844 auto computeExpansionOpportunities = [&expansionOpportunities, &expansionOpportunityCount, textAlign, &isAfterExpansion] (RenderBlockFlow& block,
845 InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, const StringView& stringView, TextDirection direction)
846 {
847 if (stringView.isEmpty()) {
848 // Empty runs should still produce an entry in expansionOpportunities list so that the number of items matches the number of runs.
849 expansionOpportunities.append(0);
850 return;
851 }
852 ExpansionBehavior expansionBehavior = expansionBehaviorForInlineTextBox(block, textBox, previousRun, nextRun, textAlign, isAfterExpansion);
853 applyExpansionBehavior(textBox, expansionBehavior);
854 unsigned opportunitiesInRun;
855 std::tie(opportunitiesInRun, isAfterExpansion) = FontCascade::expansionOpportunityCount(stringView, direction, expansionBehavior);
856 expansionOpportunities.append(opportunitiesInRun);
857 expansionOpportunityCount += opportunitiesInRun;
858 };
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000859 if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak()) {
hyatt98ee7e42003-05-14 01:39:15 +0000860 continue; // Positioned objects are only participating to figure out their
861 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000862 // Similarly, line break boxes have no effect on the width.
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000863 }
864 if (is<RenderText>(run->renderer())) {
865 auto& renderText = downcast<RenderText>(run->renderer());
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000866 auto& textBox = downcast<InlineTextBox>(*run->box());
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000867 if (canHangPunctuationAtStart && lineInfo.isFirstLine() && (isLTR || isLastInFlowRun(*run))
868 && !inlineAncestorHasStartBorderPaddingOrMargin(*this, *run->box())) {
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000869 float hangStartWidth = renderText.hangablePunctuationStartWidth(run->m_start);
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000870 availableLogicalWidth += hangStartWidth;
871 if (style().isLeftToRightDirection())
872 logicalLeft -= hangStartWidth;
873 canHangPunctuationAtStart = false;
874 }
875
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000876 if (canHangPunctuationAtEnd && lineInfo.isLastLine() && run->m_stop > 0 && (!isLTR || isLastInFlowRun(*run))
877 && !inlineAncestorHasEndBorderPaddingOrMargin(*this, *run->box())) {
878 float hangEndWidth = renderText.hangablePunctuationEndWidth(run->m_stop - 1);
879 availableLogicalWidth += hangEndWidth;
880 if (!style().isLeftToRightDirection())
881 logicalLeft -= hangEndWidth;
882 canHangPunctuationAtEnd = false;
883 }
884
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000885 if (textAlign == TextAlignMode::Justify && run != trailingSpaceRun)
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000886 computeExpansionOpportunities(*this, textBox, previousRun, run->next(), renderText.stringView(run->m_start, run->m_stop), run->box()->direction());
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000887
darin@apple.com5917cb12017-11-23 17:32:42 +0000888 if (unsigned length = renderText.text().length()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000889 if (!run->m_start && needsWordSpacing && isSpaceOrNewline(renderText.characterAt(run->m_start)))
antti@apple.comc54cbc92015-01-15 14:19:56 +0000890 totalLogicalWidth += lineStyle(*renderText.parent(), lineInfo).fontCascade().wordSpacing();
mmaxfield@apple.comfde7b6a2017-01-11 23:06:13 +0000891 // run->m_start == run->m_stop should only be true iff the run is a replaced run for bidi: isolate.
892 ASSERT(run->m_stop > 0 || run->m_start == run->m_stop);
893 needsWordSpacing = run->m_stop == length && !isSpaceOrNewline(renderText.characterAt(run->m_stop - 1));
darin06dcb9c2005-08-15 04:31:09 +0000894 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000895
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000896 setLogicalWidthForTextRun(lineBox, run, renderText, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
mitz@apple.com86470c82011-01-27 01:39:27 +0000897 } else {
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000898 canHangPunctuationAtStart = false;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000899 bool encounteredJustifiedRuby = false;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000900 if (is<RenderRubyRun>(run->renderer()) && textAlign == TextAlignMode::Justify && run != trailingSpaceRun && downcast<RenderRubyRun>(run->renderer()).rubyBase()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000901 auto* rubyBase = downcast<RenderRubyRun>(run->renderer()).rubyBase();
902 if (rubyBase->firstRootBox() && !rubyBase->firstRootBox()->nextRootBox() && run->renderer().style().collapseWhiteSpace()) {
commit-queue@webkit.org01ad5842014-12-16 19:28:57 +0000903 rubyBase->setIsAfterExpansion(isAfterExpansion);
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000904 for (auto* leafChild = rubyBase->firstRootBox()->firstLeafChild(); leafChild; leafChild = leafChild->nextLeafChild()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000905 if (!is<InlineTextBox>(*leafChild))
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000906 continue;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000907 encounteredJustifiedRuby = true;
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000908 computeExpansionOpportunities(*rubyBase, downcast<InlineTextBox>(*leafChild), nullptr, nullptr,
909 downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction());
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000910 }
911 }
912 }
913
914 if (!encounteredJustifiedRuby)
915 isAfterExpansion = false;
916
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000917 if (!is<RenderInline>(run->renderer())) {
918 auto& renderBox = downcast<RenderBox>(run->renderer());
919 if (is<RenderRubyRun>(renderBox))
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000920 setMarginsForRubyRun(run, downcast<RenderRubyRun>(renderBox), previousRun ? &previousRun->renderer() : nullptr, lineInfo);
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000921 run->box()->setLogicalWidth(logicalWidthForChild(renderBox));
mitz@apple.com86470c82011-01-27 01:39:27 +0000922 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
923 }
hyattffe78712003-02-11 01:59:29 +0000924 }
hyatt4b381692003-03-10 21:11:59 +0000925
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000926 totalLogicalWidth += run->box()->logicalWidth();
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000927 previousRun = run;
hyattffe78712003-02-11 01:59:29 +0000928 }
929
mitz@apple.com86470c82011-01-27 01:39:27 +0000930 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000931 // FIXME: see <webkit.org/b/139393#c11>
932 int lastValidExpansionOpportunitiesIndex = expansionOpportunities.size() - 1;
933 while (lastValidExpansionOpportunitiesIndex >= 0 && !expansionOpportunities.at(lastValidExpansionOpportunitiesIndex))
934 --lastValidExpansionOpportunitiesIndex;
935 if (lastValidExpansionOpportunitiesIndex >= 0) {
936 ASSERT(expansionOpportunities.at(lastValidExpansionOpportunitiesIndex));
937 expansionOpportunities.at(lastValidExpansionOpportunitiesIndex)--;
938 expansionOpportunityCount--;
939 }
mitz@apple.com86470c82011-01-27 01:39:27 +0000940 }
941
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000942 if (is<RenderRubyBase>(*this) && !expansionOpportunityCount)
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000943 textAlign = TextAlignMode::Center;
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000944
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000945 updateLogicalWidthForAlignment(textAlign, lineBox, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000946
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000947 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000948
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000949 return run;
hyattffe78712003-02-11 01:59:29 +0000950}
951
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000952void RenderBlockFlow::removeInlineBox(BidiRun& run, const RootInlineBox& rootLineBox) const
953{
954 auto* inlineBox = run.box();
955#if !ASSERT_DISABLED
956 auto* inlineParent = inlineBox->parent();
957 while (inlineParent && inlineParent != &rootLineBox) {
958 ASSERT(!inlineParent->isDirty());
959 inlineParent = inlineParent->parent();
960 }
961 ASSERT(!rootLineBox.isDirty());
962#endif
963 auto* parent = inlineBox->parent();
964 inlineBox->removeFromParent();
965
966 auto& renderer = run.renderer();
967 if (is<RenderText>(renderer))
968 downcast<RenderText>(renderer).removeTextBox(downcast<InlineTextBox>(*inlineBox));
969 delete inlineBox;
970 run.setBox(nullptr);
971 // removeFromParent() unnecessarily dirties the ancestor subtree.
972 auto* ancestor = parent;
973 while (ancestor) {
974 ancestor->markDirty(false);
975 if (ancestor == &rootLineBox)
976 break;
977 ancestor = ancestor->parent();
978 }
979}
980
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000981void RenderBlockFlow::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000982 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000983{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000984 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +0000985
986 // Now make sure we place replaced render objects correctly.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000987 for (auto* run = firstRun; run; run = run->next()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000988 ASSERT(run->box());
989 if (!run->box())
eseidel789896f2005-11-27 22:52:09 +0000990 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000991
hyatt98ee7e42003-05-14 01:39:15 +0000992 // Align positioned boxes with the top of the line box. This is
993 // a reasonable approximation of an appropriate y position.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000994 auto& renderer = run->renderer();
995 if (renderer.isOutOfFlowPositioned())
996 run->box()->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000997
998 // Position is used to properly position both replaced elements and
999 // to update the static normal flow x/y of positioned elements.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +00001000 bool inlineBoxIsRedundant = false;
1001 if (is<RenderText>(renderer)) {
1002 auto& inlineTextBox = downcast<InlineTextBox>(*run->box());
1003 downcast<RenderText>(renderer).positionLineBox(inlineTextBox);
1004 inlineBoxIsRedundant = !inlineTextBox.len();
1005 } else if (is<RenderBox>(renderer)) {
1006 downcast<RenderBox>(renderer).positionLineBox(downcast<InlineElementBox>(*run->box()));
1007 inlineBoxIsRedundant = renderer.isOutOfFlowPositioned();
1008 } else if (is<RenderLineBreak>(renderer))
1009 downcast<RenderLineBreak>(renderer).replaceInlineBoxWrapper(downcast<InlineElementBox>(*run->box()));
1010 // Check if we need to keep this box on the line at all.
1011 if (inlineBoxIsRedundant)
1012 removeInlineBox(*run, *lineBox);
hyatt98ee7e42003-05-14 01:39:15 +00001013 }
hyattffe78712003-02-11 01:59:29 +00001014}
kociendabb0c24b2001-08-24 14:24:40 +00001015
akling@apple.com7506fda2013-11-07 10:12:36 +00001016static inline bool isCollapsibleSpace(UChar character, const RenderText& renderer)
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001017{
1018 if (character == ' ' || character == '\t' || character == softHyphen)
1019 return true;
1020 if (character == '\n')
akling@apple.com7506fda2013-11-07 10:12:36 +00001021 return !renderer.style().preserveNewline();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001022 if (character == noBreakSpace)
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +00001023 return renderer.style().nbspMode() == NBSPMode::Space;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001024 return false;
1025}
1026
msaboff@apple.com142fc202012-10-18 18:03:14 +00001027template <typename CharacterType>
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001028static inline unsigned findFirstTrailingSpace(const RenderText& lastText, const CharacterType* characters, unsigned start, unsigned stop)
msaboff@apple.com142fc202012-10-18 18:03:14 +00001029{
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001030 unsigned firstSpace = stop;
msaboff@apple.com142fc202012-10-18 18:03:14 +00001031 while (firstSpace > start) {
1032 UChar current = characters[firstSpace - 1];
1033 if (!isCollapsibleSpace(current, lastText))
1034 break;
1035 firstSpace--;
1036 }
1037
1038 return firstSpace;
1039}
1040
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001041inline BidiRun* RenderBlockFlow::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +00001042{
eric@webkit.org5bee2942011-04-08 02:12:31 +00001043 if (!bidiRuns.runCount()
akling@apple.com7506fda2013-11-07 10:12:36 +00001044 || !bidiRuns.logicallyLastRun()->renderer().style().breakOnlyAfterWhiteSpace()
1045 || !bidiRuns.logicallyLastRun()->renderer().style().autoWrap())
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001046 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001047
eric@webkit.org5bee2942011-04-08 02:12:31 +00001048 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
akling@apple.com7506fda2013-11-07 10:12:36 +00001049 const RenderObject& lastObject = trailingSpaceRun->renderer();
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001050 if (!is<RenderText>(lastObject))
1051 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001052
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001053 const RenderText& lastText = downcast<RenderText>(lastObject);
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001054 unsigned firstSpace;
darin@apple.com5917cb12017-11-23 17:32:42 +00001055 if (lastText.text().is8Bit())
1056 firstSpace = findFirstTrailingSpace(lastText, lastText.text().characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
msaboff@apple.com142fc202012-10-18 18:03:14 +00001057 else
darin@apple.com5917cb12017-11-23 17:32:42 +00001058 firstSpace = findFirstTrailingSpace(lastText, lastText.text().characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
msaboff@apple.com142fc202012-10-18 18:03:14 +00001059
eric@webkit.org0894bb82011-04-03 08:29:40 +00001060 if (firstSpace == trailingSpaceRun->stop())
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001061 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001062
akling@apple.com827be9c2013-10-29 02:58:43 +00001063 TextDirection direction = style().direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +00001064 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +00001065 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001066 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001067 while (BidiContext* parent = baseContext->parent())
1068 baseContext = parent;
1069
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001070 std::unique_ptr<BidiRun> newTrailingRun = std::make_unique<BidiRun>(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->renderer(), baseContext, U_OTHER_NEUTRAL);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001071 trailingSpaceRun->m_stop = firstSpace;
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001072 trailingSpaceRun = newTrailingRun.get();
eric@webkit.org0894bb82011-04-03 08:29:40 +00001073 if (direction == LTR)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001074 bidiRuns.appendRun(WTFMove(newTrailingRun));
eric@webkit.org0894bb82011-04-03 08:29:40 +00001075 else
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001076 bidiRuns.prependRun(WTFMove(newTrailingRun));
eric@webkit.org0894bb82011-04-03 08:29:40 +00001077 return trailingSpaceRun;
1078 }
1079 if (!shouldReorder)
1080 return trailingSpaceRun;
1081
1082 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001083 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001084 trailingSpaceRun->m_level = 0;
1085 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001086 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001087 trailingSpaceRun->m_level = 1;
1088 }
1089 return trailingSpaceRun;
1090}
1091
zalan@apple.com4ed97602016-10-12 16:45:55 +00001092void RenderBlockFlow::appendFloatingObjectToLastLine(FloatingObject& floatingObject)
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001093{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001094 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject.originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001095 ASSERT(lastRootBox());
1096 floatingObject.setOriginatingLine(*lastRootBox());
zalan@apple.com4ed97602016-10-12 16:45:55 +00001097 lastRootBox()->appendFloat(floatingObject.renderer());
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001098}
1099
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001100static inline void notifyResolverToResumeInIsolate(InlineBidiResolver& resolver, RenderObject* root, RenderObject* startObject)
mmaxfield@apple.com18782a22014-01-28 21:53:22 +00001101{
1102 if (root != startObject) {
1103 RenderObject* parent = startObject->parent();
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001104 notifyResolverToResumeInIsolate(resolver, root, parent);
mmaxfield@apple.com18782a22014-01-28 21:53:22 +00001105 notifyObserverEnteredObject(&resolver, startObject);
1106 }
1107}
1108
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001109static inline void setUpResolverToResumeInIsolate(InlineBidiResolver& resolver, InlineBidiResolver& topResolver, BidiRun& isolatedRun, RenderObject* root, RenderObject* startObject)
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001110{
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001111 // Set up m_whitespaceCollapsingState
1112 resolver.whitespaceCollapsingState() = topResolver.whitespaceCollapsingState();
1113 resolver.whitespaceCollapsingState().setCurrentTransition(topResolver.whitespaceCollapsingTransitionForIsolatedRun(isolatedRun));
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001114
1115 // Set up m_nestedIsolateCount
1116 notifyResolverToResumeInIsolate(resolver, root, startObject);
1117}
1118
eric@webkit.orga26de042011-09-08 18:46:01 +00001119// FIXME: BidiResolver should have this logic.
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001120static inline void constructBidiRunsForSegment(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfRuns, VisualDirectionOverride override, bool previousLineBrokeCleanly)
eric@webkit.orga26de042011-09-08 18:46:01 +00001121{
1122 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1123 // of the resolver owning the runs.
1124 ASSERT(&topResolver.runs() == &bidiRuns);
betravis@adobe.com9e5e8242013-04-19 23:28:26 +00001125 ASSERT(topResolver.position() != endOfRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001126 RenderObject* currentRoot = topResolver.position().root();
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001127 topResolver.createBidiRunsForLine(endOfRuns, override, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001128
1129 while (!topResolver.isolatedRuns().isEmpty()) {
1130 // It does not matter which order we resolve the runs as long as we resolve them all.
aestes@apple.com13aae082016-01-02 08:03:08 +00001131 auto isolatedRun = WTFMove(topResolver.isolatedRuns().last());
eric@webkit.orga26de042011-09-08 18:46:01 +00001132 topResolver.isolatedRuns().removeLast();
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001133 currentRoot = &isolatedRun.root;
eric@webkit.orga26de042011-09-08 18:46:01 +00001134
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001135 RenderObject& startObject = isolatedRun.object;
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001136
eric@webkit.orga26de042011-09-08 18:46:01 +00001137 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001138 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1139 // tree to see which parent inline is the isolate. We could change enterIsolate
1140 // to take a RenderObject and do this logic there, but that would be a layering
1141 // violation for BidiResolver (which knows nothing about RenderObject).
cdumez@apple.comf8022152014-10-15 00:29:51 +00001142 RenderInline* isolatedInline = downcast<RenderInline>(highestContainingIsolateWithinRoot(startObject, currentRoot));
ddkilzer@apple.comad6ad362014-04-02 17:21:09 +00001143 ASSERT(isolatedInline);
1144
eric@webkit.orga26de042011-09-08 18:46:01 +00001145 InlineBidiResolver isolatedResolver;
akling@apple.com827be9c2013-10-29 02:58:43 +00001146 EUnicodeBidi unicodeBidi = isolatedInline->style().unicodeBidi();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001147 TextDirection direction;
1148 if (unicodeBidi == Plaintext)
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001149 determineDirectionality(direction, InlineIterator(isolatedInline, &isolatedRun.object, 0));
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001150 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001151 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
akling@apple.com827be9c2013-10-29 02:58:43 +00001152 direction = isolatedInline->style().direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001153 }
zoltan@webkit.orgd1a97402013-12-08 05:53:33 +00001154 isolatedResolver.setStatus(BidiStatus(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001155
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001156 setUpResolverToResumeInIsolate(isolatedResolver, topResolver, isolatedRun.runToReplace, isolatedInline, &startObject);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001157
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001158 // The starting position is the beginning of the first run within the isolate that was identified
1159 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1160 // first run within the isolate.
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001161 InlineIterator iter = InlineIterator(isolatedInline, &startObject, isolatedRun.position);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001162 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001163
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001164 // We stop at the next end of line; we may re-enter this isolate in the next call to constructBidiRuns().
eric@webkit.orga26de042011-09-08 18:46:01 +00001165 // FIXME: What should end and previousLineBrokeCleanly be?
1166 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001167 isolatedResolver.createBidiRunsForLine(endOfRuns, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001168 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001169 // We're not guaranteed to get any BidiRuns in the previous step. If we don't, we allow the placeholder
leviw@chromium.orgf0d6f362012-05-23 04:00:47 +00001170 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1171 // the logically last run.
1172 if (isolatedResolver.runs().runCount())
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001173 bidiRuns.replaceRunWithRuns(&isolatedRun.runToReplace, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001174
1175 // If we encountered any nested isolate runs, just move them
1176 // to the top resolver's list for later processing.
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001177 while (!isolatedResolver.isolatedRuns().isEmpty()) {
aestes@apple.com13aae082016-01-02 08:03:08 +00001178 auto runWithContext = WTFMove(isolatedResolver.isolatedRuns().last());
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001179 isolatedResolver.isolatedRuns().removeLast();
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001180 topResolver.setWhitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace, isolatedResolver.whitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace));
aestes@apple.com13aae082016-01-02 08:03:08 +00001181 topResolver.isolatedRuns().append(WTFMove(runWithContext));
eric@webkit.orga26de042011-09-08 18:46:01 +00001182 }
1183 }
1184}
1185
eric@webkit.org45e33a52011-05-04 11:51:09 +00001186// This function constructs line boxes for all of the text runs in the resolver and computes their position.
mario.prada@samsung.com79397522014-02-28 18:12:52 +00001187RootInlineBox* RenderBlockFlow::createLineBoxesFromBidiRuns(unsigned bidiLevel, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun, WordMeasurements& wordMeasurements)
eric@webkit.org45e33a52011-05-04 11:51:09 +00001188{
1189 if (!bidiRuns.runCount())
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001190 return nullptr;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001191
1192 // FIXME: Why is this only done when we had runs?
gyuyoung.kim@samsung.com113fb8b2013-11-28 23:25:19 +00001193 lineInfo.setLastLine(!end.renderer());
eric@webkit.org45e33a52011-05-04 11:51:09 +00001194
1195 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1196 if (!lineBox)
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001197 return nullptr;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001198
mario.prada@samsung.com79397522014-02-28 18:12:52 +00001199 lineBox->setBidiLevel(bidiLevel);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001200 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1201
cdumez@apple.com57d544c2014-10-16 00:05:37 +00001202 bool isSVGRootInlineBox = is<SVGRootInlineBox>(*lineBox);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001203
1204 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1205
1206 // Now we position all of our text runs horizontally.
1207 if (!isSVGRootInlineBox)
enrica@apple.com885c84d2012-10-10 05:48:51 +00001208 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001209
1210 // Now position our text runs vertically.
1211 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1212
eric@webkit.org45e33a52011-05-04 11:51:09 +00001213 // SVG text layout code computes vertical & horizontal positions on its own.
1214 // Note that we still need to execute computeVerticalPositionsForLine() as
1215 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1216 // contains reversed text or not. If we wouldn't do that editing and thus
1217 // text selection in RTL boxes would not work as expected.
1218 if (isSVGRootInlineBox) {
akling@apple.com670fea12013-10-12 18:16:42 +00001219 ASSERT_WITH_SECURITY_IMPLICATION(isSVGText());
cdumez@apple.com57d544c2014-10-16 00:05:37 +00001220 downcast<SVGRootInlineBox>(*lineBox).computePerCharacterLayoutInformation();
eric@webkit.org45e33a52011-05-04 11:51:09 +00001221 }
eric@webkit.org45e33a52011-05-04 11:51:09 +00001222
1223 // Compute our overflow now.
1224 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1225
eric@webkit.org45e33a52011-05-04 11:51:09 +00001226 return lineBox;
1227}
1228
akling@apple.com31dd4f42013-10-30 22:27:59 +00001229static void deleteLineRange(LineLayoutState& layoutState, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001230{
1231 RootInlineBox* boxToDelete = startLine;
1232 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001233 layoutState.updateRepaintRangeFromBox(boxToDelete);
akling@apple.com31dd4f42013-10-30 22:27:59 +00001234 // Note: deleteLineRange(firstRootBox()) is not identical to deleteLineBoxTree().
eric@webkit.orge2532d92011-05-16 23:10:49 +00001235 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001236 RootInlineBox* next = boxToDelete->nextRootBox();
akling@apple.com31dd4f42013-10-30 22:27:59 +00001237 boxToDelete->deleteLine();
eric@webkit.org455d90e2011-05-09 22:27:27 +00001238 boxToDelete = next;
1239 }
1240}
1241
zalan@apple.com4ed97602016-10-12 16:45:55 +00001242static void repaintDirtyFloats(LineLayoutState::FloatList& floats)
1243{
1244 // Floats that did not have layout did not repaint when we laid them out. They would have
1245 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1246 // painted.
1247 for (auto& floatBox : floats) {
1248 if (floatBox->everHadLayout())
1249 continue;
1250 auto& box = floatBox->renderer();
1251 if (!box.x() && !box.y() && box.checkForRepaintDuringLayout())
1252 box.repaint();
1253 }
1254}
1255
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001256void RenderBlockFlow::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001257{
1258 // We want to skip ahead to the first dirty line
1259 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001260 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001261
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001262 unsigned consecutiveHyphenatedLines = 0;
1263 if (startLine) {
1264 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1265 consecutiveHyphenatedLines++;
1266 }
1267
eric@webkit.org060caf62011-05-03 22:11:39 +00001268 // FIXME: This would make more sense outside of this function, but since
1269 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1270 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001271 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
antti@apple.comca2a8ff2013-10-04 04:04:35 +00001272 setNeedsLayout(MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
zalan@apple.com4de96c52017-11-07 04:09:59 +00001273 if (!view().frameView().layoutContext().needsFullRepaint() && hasSelfPaintingLayer() && hasRepaintLayoutRects()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001274 // Because we waited until we were already inside layout to discover
1275 // that the block really needed a full layout, we missed our chance to repaint the layer
1276 // before layout started. Luckily the layer has cached the repaint rect for its original
1277 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com8459a882017-08-09 21:03:03 +00001278 repaintUsingContainer(containerForRepaint(), repaintLayoutRects().m_repaintRect);
eric@webkit.org060caf62011-05-03 22:11:39 +00001279 }
1280 }
1281
mihnea@adobe.com99467792013-03-19 09:09:04 +00001282 if (containsFloats())
zalan@apple.com4ed97602016-10-12 16:45:55 +00001283 layoutState.floatList().setLastFloat(m_floatingObjects->set().last().get());
eric@webkit.org060caf62011-05-03 22:11:39 +00001284
1285 // We also find the first clean line and extract these lines. We will add them back
1286 // if we determine that we're able to synchronize after handling all our dirty lines.
1287 InlineIterator cleanLineStart;
1288 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001289 if (!layoutState.isFullLayout() && startLine)
1290 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001291
1292 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001293 if (!layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001294 layoutState.setRepaintRange(logicalHeight());
akling@apple.com31dd4f42013-10-30 22:27:59 +00001295 deleteLineRange(layoutState, startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001296 }
1297
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001298 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001299 // If the last line before the start line ends with a line break that clear floats,
1300 // adjust the height accordingly.
1301 // A line break can be either the first or the last object on a line, depending on its direction.
1302 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
akling@apple.com0b8172b72013-08-31 18:34:23 +00001303 RenderObject* lastObject = &lastLeafChild->renderer();
eric@webkit.org060caf62011-05-03 22:11:39 +00001304 if (!lastObject->isBR())
akling@apple.com0b8172b72013-08-31 18:34:23 +00001305 lastObject = &lastRootBox()->firstLeafChild()->renderer();
eric@webkit.org060caf62011-05-03 22:11:39 +00001306 if (lastObject->isBR()) {
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +00001307 Clear clear = lastObject->style().clear();
1308 if (clear != Clear::None)
bjonesbe@adobe.comf9f10402014-02-20 19:40:28 +00001309 clearFloats(clear);
eric@webkit.org060caf62011-05-03 22:11:39 +00001310 }
1311 }
1312 }
1313
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001314 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001315 linkToEndLineIfNeeded(layoutState);
zalan@apple.com4ed97602016-10-12 16:45:55 +00001316 repaintDirtyFloats(layoutState.floatList());
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001317}
eric@webkit.org060caf62011-05-03 22:11:39 +00001318
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001319// Before restarting the layout loop with a new logicalHeight, remove all floats that were added and reset the resolver.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001320inline const InlineIterator& RenderBlockFlow::restartLayoutRunsAndFloatsInRange(LayoutUnit oldLogicalHeight, LayoutUnit newLogicalHeight, FloatingObject* lastFloatFromPreviousLine, InlineBidiResolver& resolver, const InlineIterator& oldEnd)
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001321{
1322 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1323 setLogicalHeight(newLogicalHeight);
1324 resolver.setPositionIgnoringNestedIsolates(oldEnd);
1325 return oldEnd;
1326}
1327
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001328void RenderBlockFlow::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001329{
akling@apple.com827be9c2013-10-29 02:58:43 +00001330 const RenderStyle& styleToUse = style();
zalan@apple.com4de96c52017-11-07 04:09:59 +00001331 bool paginated = view().frameView().layoutContext().layoutState() && view().frameView().layoutContext().layoutState()->isPaginated();
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001332 LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001333 InlineIterator end = resolver.position();
1334 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001335 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001336 VerticalPositionCache verticalPositionCache;
1337
weinig@apple.com12840dc2013-10-22 23:59:08 +00001338 LineBreaker lineBreaker(*this);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001339
eric@webkit.org060caf62011-05-03 22:11:39 +00001340 while (!end.atEnd()) {
1341 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001342 if (checkForEndLineMatch) {
1343 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001344 if (layoutState.endLineMatched()) {
1345 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001346 layoutState.marginInfo().clearMargin();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001347 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001348 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001349 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001350
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001351 lineWhitespaceCollapsingState.reset();
eric@webkit.org060caf62011-05-03 22:11:39 +00001352
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001353 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001354 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001355
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001356 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001357 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
zalan@apple.com84ccfa12015-10-17 03:36:56 +00001358 FloatingObject* lastFloatFromPreviousLine = (containsFloats()) ? m_floatingObjects->set().last().get() : nullptr;
zoltan@webkit.org3bd77f52013-04-23 18:23:36 +00001359
enrica@apple.com885c84d2012-10-10 05:48:51 +00001360 WordMeasurements wordMeasurements;
antti@apple.comae85e112017-08-31 23:27:02 +00001361 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
darin@apple.com3d1d5fc2015-04-23 05:20:23 +00001362 cachePriorCharactersIfNeeded(renderTextInfo.lineBreakIterator);
1363 renderTextInfo.lineBreakIterator.resetPriorContext();
eric@webkit.org060caf62011-05-03 22:11:39 +00001364 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001365 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001366 // Once BidiRunList is separated from BidiResolver this will not be needed.
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001367 resolver.runs().clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001368 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001369 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001370 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001371 break;
1372 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001373
betravis@adobe.com9e5e8242013-04-19 23:28:26 +00001374 ASSERT(end != resolver.position());
1375
eric@webkit.org45e33a52011-05-04 11:51:09 +00001376 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001377 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001378 if (lastRootBox())
gyuyoung.kim@samsung.com044376f2013-12-26 07:32:04 +00001379 lastRootBox()->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
eric@webkit.org060caf62011-05-03 22:11:39 +00001380 } else {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00001381 VisualDirectionOverride override = (styleToUse.rtlOrdering() == Order::Visual ? (styleToUse.direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001382
akling@apple.com827be9c2013-10-29 02:58:43 +00001383 if (isNewUBAParagraph && styleToUse.unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1384 TextDirection direction = styleToUse.direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001385 determineDirectionality(direction, resolver.position());
akling@apple.com827be9c2013-10-29 02:58:43 +00001386 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse.unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001387 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001388 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1389 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
zoltan@webkit.org7d4f8cc2014-03-26 18:20:15 +00001390 constructBidiRunsForSegment(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001391 ASSERT(resolver.position() == end);
1392
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001393 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : nullptr;
eric@webkit.org060caf62011-05-03 22:11:39 +00001394
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001395 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001396 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001397 consecutiveHyphenatedLines++;
1398 } else
1399 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001400
eric@webkit.org060caf62011-05-03 22:11:39 +00001401 // Now that the runs have been ordered, we create the line boxes.
1402 // At the same time we figure out where border/padding/margin should be applied for
1403 // inline flow boxes.
1404
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001405 LayoutUnit oldLogicalHeight = logicalHeight();
mario.prada@samsung.com79397522014-02-28 18:12:52 +00001406 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(resolver.status().context->level(), bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001407
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001408 bidiRuns.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001409 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1410
1411 if (lineBox) {
gyuyoung.kim@samsung.com044376f2013-12-26 07:32:04 +00001412 lineBox->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001413 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001414 layoutState.updateRepaintRangeFromBox(lineBox);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001415
1416 LayoutUnit adjustment = 0;
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001417 bool overflowsFragment = false;
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001418
antti@apple.comae85e112017-08-31 23:27:02 +00001419 layoutState.marginInfo().setAtBeforeSideOfBlock(false);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001420
1421 if (paginated)
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001422 adjustLinePositionForPagination(lineBox, adjustment, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001423 if (adjustment) {
zalan@apple.com64761fe2016-03-02 21:42:22 +00001424 IndentTextOrNot shouldIndentText = layoutState.lineInfo().isFirstLine() ? IndentText : DoNotIndentText;
1425 LayoutUnit oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, shouldIndentText);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001426 lineBox->adjustBlockDirectionPosition(adjustment);
1427 if (layoutState.usesRepaintBounds())
1428 layoutState.updateRepaintRangeFromBox(lineBox);
1429
zalan@apple.com64761fe2016-03-02 21:42:22 +00001430 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, shouldIndentText) != oldLineWidth) {
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001431 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1432 lineBox->deleteLine();
1433 end = restartLayoutRunsAndFloatsInRange(oldLogicalHeight, oldLogicalHeight + adjustment, lastFloatFromPreviousLine, resolver, oldEnd);
1434 continue;
1435 }
1436
1437 setLogicalHeight(lineBox->lineBottomWithLeading());
1438 }
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001439
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001440 if (paginated) {
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001441 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001442 updateFragmentForLine(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001443 }
1444 }
robert@webkit.org402c1512013-02-07 18:48:39 +00001445 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001446
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00001447 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
zalan@apple.com4d97a002016-02-24 17:13:33 +00001448 setStaticPositions(*this, *lineBreaker.positionedObjects()[i], DoNotIndentText);
eric@webkit.org060caf62011-05-03 22:11:39 +00001449
robert@webkit.org402c1512013-02-07 18:48:39 +00001450 if (!layoutState.lineInfo().isEmpty()) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001451 layoutState.lineInfo().setFirstLine(false);
bjonesbe@adobe.comf9f10402014-02-20 19:40:28 +00001452 clearFloats(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001453 }
1454
1455 if (m_floatingObjects && lastRootBox()) {
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001456 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +00001457 auto it = floatingObjectSet.begin();
1458 auto end = floatingObjectSet.end();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001459 if (auto* lastFloat = layoutState.floatList().lastFloat()) {
1460 auto lastFloatIterator = floatingObjectSet.find<FloatingObject&, FloatingObjectHashTranslator>(*lastFloat);
eric@webkit.org060caf62011-05-03 22:11:39 +00001461 ASSERT(lastFloatIterator != end);
1462 ++lastFloatIterator;
1463 it = lastFloatIterator;
1464 }
1465 for (; it != end; ++it) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001466 auto& floatingObject = *it;
1467 appendFloatingObjectToLastLine(*floatingObject);
eric@webkit.org060caf62011-05-03 22:11:39 +00001468 // If a float's geometry has changed, give up on syncing with clean lines.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001469 auto* floatWithRect = layoutState.floatList().floatWithRect(floatingObject->renderer());
1470 if (!floatWithRect || floatWithRect->rect() != floatingObject->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001471 checkForEndLineMatch = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00001472 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001473 layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
eric@webkit.org060caf62011-05-03 22:11:39 +00001474 }
1475
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001476 lineWhitespaceCollapsingState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001477 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001478 }
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001479
abucur@adobe.comfc497132013-10-04 08:49:21 +00001480 // In case we already adjusted the line positions during this layout to avoid widows
1481 // then we need to ignore the possibility of having a new widows situation.
1482 // Otherwise, we risk leaving empty containers which is against the block fragmentation principles.
akling@apple.com827be9c2013-10-29 02:58:43 +00001483 if (paginated && !style().hasAutoWidows() && !didBreakAtLineToAvoidWidow()) {
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001484 // Check the line boxes to make sure we didn't create unacceptable widows.
1485 // However, we'll prioritize orphans - so nothing we do here should create
1486 // a new orphan.
1487
1488 RootInlineBox* lineBox = lastRootBox();
1489
1490 // Count from the end of the block backwards, to see how many hanging
1491 // lines we have.
1492 RootInlineBox* firstLineInBlock = firstRootBox();
1493 int numLinesHanging = 1;
1494 while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1495 ++numLinesHanging;
1496 lineBox = lineBox->prevRootBox();
1497 }
1498
1499 // If there were no breaks in the block, we didn't create any widows.
jchaffraix@webkit.org0f225142013-01-28 22:28:21 +00001500 if (!lineBox || !lineBox->isFirstAfterPageBreak() || lineBox == firstLineInBlock)
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001501 return;
1502
akling@apple.com827be9c2013-10-29 02:58:43 +00001503 if (numLinesHanging < style().widows()) {
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001504 // We have detected a widow. Now we need to work out how many
1505 // lines there are on the previous page, and how many we need
1506 // to steal.
akling@apple.com827be9c2013-10-29 02:58:43 +00001507 int numLinesNeeded = style().widows() - numLinesHanging;
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001508 RootInlineBox* currentFirstLineOfNewPage = lineBox;
1509
1510 // Count the number of lines in the previous page.
1511 lineBox = lineBox->prevRootBox();
1512 int numLinesInPreviousPage = 1;
1513 while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1514 ++numLinesInPreviousPage;
1515 lineBox = lineBox->prevRootBox();
1516 }
1517
1518 // If there was an explicit value for orphans, respect that. If not, we still
1519 // shouldn't create a situation where we make an orphan bigger than the initial value.
1520 // This means that setting widows implies we also care about orphans, but given
1521 // the specification says the initial orphan value is non-zero, this is ok. The
1522 // author is always free to set orphans explicitly as well.
akling@apple.com827be9c2013-10-29 02:58:43 +00001523 int orphans = style().hasAutoOrphans() ? style().initialOrphans() : style().orphans();
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001524 int numLinesAvailable = numLinesInPreviousPage - orphans;
1525 if (numLinesAvailable <= 0)
1526 return;
1527
andersca@apple.com86298632013-11-10 19:32:33 +00001528 int numLinesToTake = std::min(numLinesAvailable, numLinesNeeded);
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001529 // Wind back from our first widowed line.
1530 lineBox = currentFirstLineOfNewPage;
1531 for (int i = 0; i < numLinesToTake; ++i)
1532 lineBox = lineBox->prevRootBox();
1533
1534 // We now want to break at this line. Remember for next layout and trigger relayout.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001535 setBreakAtLineToAvoidWidow(lineCount(lineBox));
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001536 markLinesDirtyInBlockRange(lastRootBox()->lineBottomWithLeading(), lineBox->lineBottomWithLeading(), lineBox);
1537 }
1538 }
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001539 clearDidBreakAtLineToAvoidWidow();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001540}
eric@webkit.org060caf62011-05-03 22:11:39 +00001541
zalan@apple.com6f66a672016-04-06 15:56:06 +00001542void RenderBlockFlow::reattachCleanLineFloats(RootInlineBox& cleanLine, LayoutUnit delta, bool isFirstCleanLine)
1543{
1544 auto* cleanLineFloats = cleanLine.floatsPtr();
1545 if (!cleanLineFloats)
1546 return;
1547
zalan@apple.com9201b992017-10-06 22:14:05 +00001548 for (auto& floatingBox : *cleanLineFloats) {
1549 if (!floatingBox)
1550 continue;
zalan@apple.com6f66a672016-04-06 15:56:06 +00001551 auto* floatingObject = insertFloatingObject(*floatingBox);
1552 if (isFirstCleanLine && floatingObject->originatingLine()) {
1553 // Float box does not belong to this line anymore.
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001554 ASSERT_WITH_SECURITY_IMPLICATION(cleanLine.prevRootBox() == floatingObject->originatingLine());
zalan@apple.com6f66a672016-04-06 15:56:06 +00001555 cleanLine.removeFloat(*floatingBox);
1556 continue;
1557 }
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001558 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001559 floatingObject->setOriginatingLine(cleanLine);
zalan@apple.com6f66a672016-04-06 15:56:06 +00001560 setLogicalHeight(logicalTopForChild(*floatingBox) - marginBeforeForChild(*floatingBox) + delta);
1561 positionNewFloats();
1562 }
1563}
1564
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001565void RenderBlockFlow::linkToEndLineIfNeeded(LineLayoutState& layoutState)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001566{
zalan@apple.com6f66a672016-04-06 15:56:06 +00001567 auto* firstCleanLine = layoutState.endLine();
1568 if (firstCleanLine) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001569 if (layoutState.endLineMatched()) {
zalan@apple.com4de96c52017-11-07 04:09:59 +00001570 bool paginated = view().frameView().layoutContext().layoutState() && view().frameView().layoutContext().layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001571 // Attach all the remaining lines, and then adjust their y-positions as needed.
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001572 LayoutUnit delta = logicalHeight() - layoutState.endLineLogicalTop();
zalan@apple.com6f66a672016-04-06 15:56:06 +00001573 for (auto* line = firstCleanLine; line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001574 line->attachLine();
1575 if (paginated) {
1576 delta -= line->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001577 bool overflowsFragment;
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001578 adjustLinePositionForPagination(line, delta, overflowsFragment, layoutState.fragmentedFlow());
eric@webkit.org060caf62011-05-03 22:11:39 +00001579 }
1580 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001581 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001582 line->adjustBlockDirectionPosition(delta);
1583 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001584 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001585 updateFragmentForLine(line);
zalan@apple.com6f66a672016-04-06 15:56:06 +00001586 reattachCleanLineFloats(*line, delta, line == firstCleanLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001587 }
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00001588 setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001589 } else {
1590 // Delete all the remaining lines.
akling@apple.com31dd4f42013-10-30 22:27:59 +00001591 deleteLineRange(layoutState, layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001592 }
1593 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001594
1595 if (m_floatingObjects && (layoutState.checkForFloatsFromLastLine() || positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001596 // In case we have a float on the last line, it might not be positioned up to now.
1597 // This has to be done before adding in the bottom border/padding, or the float will
1598 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001599 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001600 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1601 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
akling@apple.comb5f24642013-11-06 04:47:12 +00001602 auto newLineBox = std::make_unique<TrailingFloatsRootInlineBox>(*this);
1603 auto trailingFloatsLineBox = newLineBox.get();
aestes@apple.com13aae082016-01-02 08:03:08 +00001604 m_lineBoxes.appendLineBox(WTFMove(newLineBox));
eric@webkit.org060caf62011-05-03 22:11:39 +00001605 trailingFloatsLineBox->setConstructed();
1606 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1607 VerticalPositionCache verticalPositionCache;
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001608 LayoutUnit blockLogicalHeight = logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001609 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1610 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001611 trailingFloatsLineBox->setPaginatedLineWidth(availableLogicalWidthForContent(blockLogicalHeight));
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001612 LayoutRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1613 LayoutRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001614 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001615 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001616 updateFragmentForLine(trailingFloatsLineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001617 }
1618
hyatt@apple.com46c65b32011-08-09 19:13:45 +00001619 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +00001620 auto it = floatingObjectSet.begin();
1621 auto end = floatingObjectSet.end();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001622 if (auto* lastFloat = layoutState.floatList().lastFloat()) {
1623 auto lastFloatIterator = floatingObjectSet.find<FloatingObject&, FloatingObjectHashTranslator>(*lastFloat);
eric@webkit.org060caf62011-05-03 22:11:39 +00001624 ASSERT(lastFloatIterator != end);
1625 ++lastFloatIterator;
1626 it = lastFloatIterator;
1627 }
1628 for (; it != end; ++it)
zalan@apple.com4ed97602016-10-12 16:45:55 +00001629 appendFloatingObjectToLastLine(**it);
1630 layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
eric@webkit.org060caf62011-05-03 22:11:39 +00001631 }
1632}
1633
antti@apple.com940f5872013-10-24 20:31:11 +00001634void RenderBlockFlow::layoutLineBoxes(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001635{
antti@apple.comfea51992013-10-28 13:39:23 +00001636 ASSERT(!m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00001637
zoltan@webkit.org8e79cc22013-06-14 00:39:36 +00001638 setLogicalHeight(borderAndPaddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001639
1640 // Lay out our hypothetical grid line as though it occurs at the top of the block.
zalan@apple.com4de96c52017-11-07 04:09:59 +00001641 if (view().frameView().layoutContext().layoutState() && view().frameView().layoutContext().layoutState()->lineGrid() == this)
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001642 layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001643
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001644 RenderFragmentedFlow* fragmentedFlow = enclosingFragmentedFlow();
1645 bool clearLinesForPagination = firstRootBox() && fragmentedFlow && !fragmentedFlow->hasFragments();
commit-queue@webkit.org93cdd632012-12-07 00:33:56 +00001646
hyatt0c3a9862004-02-23 21:26:26 +00001647 // Figure out if we should clear out our line boxes.
1648 // FIXME: Handle resize eventually!
akling@apple.comee3c8df2013-11-06 08:09:44 +00001649 bool isFullLayout = !firstRootBox() || selfNeedsLayout() || relayoutChildren || clearLinesForPagination;
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001650 LineLayoutState layoutState(*this, isFullLayout, repaintLogicalTop, repaintLogicalBottom, fragmentedFlow);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001651
1652 if (isFullLayout)
akling@apple.com31dd4f42013-10-30 22:27:59 +00001653 lineBoxes().deleteLineBoxes();
mitz@apple.come1364202008-02-28 01:06:41 +00001654
commit-queue@webkit.org07797312013-02-22 18:58:19 +00001655 // Text truncation kicks in in two cases:
1656 // 1) If your overflow isn't visible and your text-overflow-mode isn't clip.
1657 // 2) If you're an anonymous block with a block parent that satisfies #1.
hyatted77ad82004-06-15 07:21:23 +00001658 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
commit-queue@webkit.org07797312013-02-22 18:58:19 +00001659 // difficult to figure out in general (especially in the middle of doing layout), so we only handle the
1660 // simple case of an anonymous block truncating when it's parent is clipped.
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +00001661 bool hasTextOverflow = (style().textOverflow() == TextOverflow::Ellipsis && hasOverflowClip())
1662 || (isAnonymousBlock() && parent() && parent()->isRenderBlock() && parent()->style().textOverflow() == TextOverflow::Ellipsis && parent()->hasOverflowClip());
mitz@apple.come1364202008-02-28 01:06:41 +00001663
hyatted77ad82004-06-15 07:21:23 +00001664 // Walk all the lines and delete our ellipsis line boxes if they exist.
1665 if (hasTextOverflow)
1666 deleteEllipsisLineBoxes();
1667
hyattffe78712003-02-11 01:59:29 +00001668 if (firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001669 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1670 // siblings can run into stale root lineboxes during layout. Then layout
1671 // the replaced elements later. In partial layout mode, line boxes are not
1672 // deleted and only dirtied. In that case, we can layout the replaced
1673 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001674 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001675 Vector<RenderBox*> replacedChildren;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001676 for (InlineWalker walker(*this); !walker.atEnd(); walker.advance()) {
1677 RenderObject& o = *walker.current();
abucur@adobe.com33159da2013-08-13 07:44:32 +00001678
weinig@apple.com12840dc2013-10-22 23:59:08 +00001679 if (!hasInlineChild && o.isInline())
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001680 hasInlineChild = true;
1681
weinig@apple.com12840dc2013-10-22 23:59:08 +00001682 if (o.isReplaced() || o.isFloating() || o.isOutOfFlowPositioned()) {
cdumez@apple.com0abff8b2014-10-17 21:25:10 +00001683 RenderBox& box = downcast<RenderBox>(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001684
weinig@apple.com12840dc2013-10-22 23:59:08 +00001685 if (relayoutChildren || box.hasRelativeDimensions())
1686 box.setChildNeedsLayout(MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001687
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001688 // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001689 if (relayoutChildren && box.needsPreferredWidthsRecalculation())
1690 box.setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001691
weinig@apple.com12840dc2013-10-22 23:59:08 +00001692 if (box.isOutOfFlowPositioned())
1693 box.containingBlock()->insertPositionedObject(box);
1694 else if (box.isFloating())
zalan@apple.com4ed97602016-10-12 16:45:55 +00001695 layoutState.floatList().append(FloatWithRect::create(box));
weinig@apple.com12840dc2013-10-22 23:59:08 +00001696 else if (isFullLayout || box.needsLayout()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001697 // Replaced element.
zalan@apple.comf2d2fb52017-07-06 05:49:50 +00001698 if (isFullLayout && is<RenderRubyRun>(box)) {
1699 // FIXME: This resets the overhanging margins that we set during line layout (see computeInlineDirectionPositionsForSegment)
1700 // Find a more suitable place for this.
1701 setMarginStartForChild(box, 0);
1702 setMarginEndForChild(box, 0);
1703 }
weinig@apple.com12840dc2013-10-22 23:59:08 +00001704 box.dirtyLineBoxes(isFullLayout);
antti@apple.comae85e112017-08-31 23:27:02 +00001705 if (isFullLayout)
1706 replacedChildren.append(&box);
1707 else
1708 box.layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001709 }
cdumez@apple.comf8022152014-10-15 00:29:51 +00001710 } else if (o.isTextOrLineBreak() || (is<RenderInline>(o) && !walker.atEndOfInline())) {
1711 if (is<RenderInline>(o))
1712 downcast<RenderInline>(o).updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
weinig@apple.com12840dc2013-10-22 23:59:08 +00001713 if (layoutState.isFullLayout() || o.selfNeedsLayout())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001714 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
weinig@apple.com12840dc2013-10-22 23:59:08 +00001715 o.clearNeedsLayout();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001716 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001717 }
1718
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00001719 for (size_t i = 0; i < replacedChildren.size(); i++)
1720 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001721
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001722 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001723 }
hyatt85586af2003-02-19 23:22:42 +00001724
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001725 // Expand the last line to accommodate Ruby and emphasis marks.
1726 int lastLineAnnotationsAdjustment = 0;
1727 if (lastRootBox()) {
andersca@apple.com86298632013-11-10 19:32:33 +00001728 LayoutUnit lowestAllowedPosition = std::max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
akling@apple.com827be9c2013-10-29 02:58:43 +00001729 if (!style().isFlippedLinesWritingMode())
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001730 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1731 else
1732 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001733 }
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001734
1735 // Now do the handling of the bottom of the block, adding in our bottom border/padding and
1736 // determining the correct collapsed bottom margin information. This collapse is only necessary
1737 // if our last child was an anonymous inline block that might need to propagate margin information out to
1738 // us.
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001739 LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight() + lastLineAnnotationsAdjustment;
antti@apple.comae85e112017-08-31 23:27:02 +00001740 setLogicalHeight(logicalHeight() + afterEdge);
kociendabb0c24b2001-08-24 14:24:40 +00001741
akling@apple.comee3c8df2013-11-06 08:09:44 +00001742 if (!firstRootBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001743 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001744
1745 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1746 // truncate text.
1747 if (hasTextOverflow)
1748 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001749}
1750
zalan@apple.com4ed97602016-10-12 16:45:55 +00001751void RenderBlockFlow::checkFloatInCleanLine(RootInlineBox& cleanLine, RenderBox& floatBoxOnCleanLine, FloatWithRect& matchingFloatWithRect,
1752 bool& encounteredNewFloat, bool& dirtiedByFloat)
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001753{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001754 ASSERT_WITH_SECURITY_IMPLICATION(!floatBoxOnCleanLine.style().deletionHasBegun());
1755 if (&matchingFloatWithRect.renderer() != &floatBoxOnCleanLine) {
stavila@adobe.comf4ef0972014-03-28 21:55:46 +00001756 encounteredNewFloat = true;
1757 return;
1758 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001759 floatBoxOnCleanLine.layoutIfNeeded();
1760 LayoutRect originalFloatRect = matchingFloatWithRect.rect();
1761 LayoutSize newSize(
1762 floatBoxOnCleanLine.width() + floatBoxOnCleanLine.horizontalMarginExtent(),
1763 floatBoxOnCleanLine.height() + floatBoxOnCleanLine.verticalMarginExtent());
hyatt@apple.comc2e15522014-09-03 19:26:38 +00001764
zalan@apple.com4ed97602016-10-12 16:45:55 +00001765 // We have to reset the cap-height alignment done by the first-letter floats when initial-letter is set, so just always treat first-letter floats as dirty.
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00001766 if (originalFloatRect.size() == newSize && (floatBoxOnCleanLine.style().styleType() != PseudoId::FirstLetter || !floatBoxOnCleanLine.style().initialLetterDrop()))
zalan@apple.com4ed97602016-10-12 16:45:55 +00001767 return;
1768
1769 LayoutUnit floatTop = isHorizontalWritingMode() ? originalFloatRect.y() : originalFloatRect.x();
1770 LayoutUnit floatHeight = isHorizontalWritingMode() ? std::max(originalFloatRect.height(), newSize.height())
1771 : std::max(originalFloatRect.width(), newSize.width());
1772 floatHeight = std::min(floatHeight, LayoutUnit::max() - floatTop);
1773 cleanLine.markDirty();
1774 markLinesDirtyInBlockRange(cleanLine.lineBottomWithLeading(), floatTop + floatHeight, &cleanLine);
1775 LayoutRect newFloatRect = originalFloatRect;
1776 newFloatRect.setSize(newSize);
1777 matchingFloatWithRect.adjustRect(newFloatRect);
1778 dirtiedByFloat = true;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001779}
1780
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001781RootInlineBox* RenderBlockFlow::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001782{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001783 RootInlineBox* currentLine = nullptr;
1784 RootInlineBox* lastLine = nullptr;
mitz@apple.come1364202008-02-28 01:06:41 +00001785
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001786 // FIXME: This entire float-checking block needs to be broken into a new function.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001787 auto& floats = layoutState.floatList();
mitz@apple.com40547b32008-03-18 04:04:34 +00001788 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001789 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001790 // Paginate all of the clean lines.
zalan@apple.com4de96c52017-11-07 04:09:59 +00001791 bool paginated = view().frameView().layoutContext().layoutState() && view().frameView().layoutContext().layoutState()->isPaginated();
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001792 LayoutUnit paginationDelta = 0;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001793 auto floatsIterator = floats.begin();
1794 auto end = floats.end();
1795 for (currentLine = firstRootBox(); currentLine && !currentLine->isDirty(); currentLine = currentLine->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001796 if (paginated) {
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001797 if (lineWidthForPaginatedLineChanged(currentLine, 0, layoutState.fragmentedFlow())) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001798 currentLine->markDirty();
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001799 break;
1800 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001801 paginationDelta -= currentLine->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001802 bool overflowsFragment;
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001803 adjustLinePositionForPagination(currentLine, paginationDelta, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001804 if (paginationDelta) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001805 if (containsFloats() || !floats.isEmpty()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001806 // FIXME: Do better eventually. For now if we ever shift because of pagination and floats are present just go to a full layout.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001807 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001808 break;
1809 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001810
zalan@apple.com4ed97602016-10-12 16:45:55 +00001811 layoutState.updateRepaintRangeFromBox(currentLine, paginationDelta);
1812 currentLine->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001813 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001814 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001815 updateFragmentForLine(currentLine);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001816 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001817
zalan@apple.com4ed97602016-10-12 16:45:55 +00001818 if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1819 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1820 bool encounteredNewFloat = false;
zalan@apple.com9201b992017-10-06 22:14:05 +00001821 for (auto& floatBoxOnCleanLine : *cleanLineFloats) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001822 ASSERT(floatsIterator != end);
zalan@apple.com9201b992017-10-06 22:14:05 +00001823 if (!floatBoxOnCleanLine)
1824 continue;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001825 checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1826 ++floatsIterator;
1827 if (floatsIterator == end || encounteredNewFloat) {
1828 layoutState.markForFullLayout();
1829 break;
1830 }
1831 }
1832 if (dirtiedByFloat || encounteredNewFloat)
1833 break;
1834 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001835 }
1836 // Check if a new float has been inserted after the last known float.
zalan@apple.com280ebf62018-07-12 14:23:45 +00001837 if (floatsIterator != end) {
1838 if (!currentLine)
1839 layoutState.markForFullLayout();
1840 else {
1841 for (; floatsIterator != end; ++floatsIterator) {
1842 auto& floatWithRect = *floatsIterator;
1843 if (!floatWithRect->renderer().needsLayout())
1844 continue;
1845 layoutState.markForFullLayout();
1846 break;
1847 }
1848 }
1849 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001850 }
1851
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001852 if (layoutState.isFullLayout()) {
akling@apple.com31dd4f42013-10-30 22:27:59 +00001853 m_lineBoxes.deleteLineBoxTree();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001854 currentLine = nullptr;
akling@apple.comee3c8df2013-11-06 08:09:44 +00001855 ASSERT(!firstRootBox() && !lastRootBox());
eseidel789896f2005-11-27 22:52:09 +00001856 } else {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001857 if (currentLine) {
hyatt0c3a9862004-02-23 21:26:26 +00001858 // We have a dirty line.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001859 if (RootInlineBox* prevRootBox = currentLine->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001860 // We have a previous line.
antti@apple.comae85e112017-08-31 23:27:02 +00001861 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak()
zalan@apple.com4ed97602016-10-12 16:45:55 +00001862 || !prevRootBox->lineBreakObj()
1863 || (is<RenderText>(*prevRootBox->lineBreakObj())
darin@apple.com5917cb12017-11-23 17:32:42 +00001864 && prevRootBox->lineBreakPos() >= downcast<RenderText>(*prevRootBox->lineBreakObj()).text().length()))) {
mjs9f78dd92007-02-12 04:06:07 +00001865 // The previous line didn't break cleanly or broke at a newline
1866 // that has been deleted, so treat it as dirty too.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001867 currentLine = prevRootBox;
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001868 }
hyatt0c3a9862004-02-23 21:26:26 +00001869 }
hyatt0c3a9862004-02-23 21:26:26 +00001870 }
hyatt0c3a9862004-02-23 21:26:26 +00001871 // If we have no dirty lines, then last is just the last root box.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001872 lastLine = currentLine ? currentLine->prevRootBox() : lastRootBox();
hyatt0c3a9862004-02-23 21:26:26 +00001873 }
mitz@apple.come1364202008-02-28 01:06:41 +00001874
zalan@apple.com4ed97602016-10-12 16:45:55 +00001875 if (!floats.isEmpty()) {
eae@chromium.orgee8613e2011-11-12 01:12:58 +00001876 LayoutUnit savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001877 // Restore floats from clean lines.
1878 RootInlineBox* line = firstRootBox();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001879 while (line != currentLine) {
zalan@apple.com9201b992017-10-06 22:14:05 +00001880 if (auto* cleanLineFloats = line->floatsPtr()) {
1881 for (auto& floatingBox : *cleanLineFloats) {
1882 if (!floatingBox)
1883 continue;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001884 auto* floatingObject = insertFloatingObject(*floatingBox);
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001885 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001886 floatingObject->setOriginatingLine(*line);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001887 setLogicalHeight(logicalTopForChild(*floatingBox) - marginBeforeForChild(*floatingBox));
mitz@apple.com40547b32008-03-18 04:04:34 +00001888 positionNewFloats();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001889 floats.setLastCleanFloat(*floatingBox);
mitz@apple.com40547b32008-03-18 04:04:34 +00001890 }
1891 }
1892 line = line->nextRootBox();
1893 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001894 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001895 }
1896
zalan@apple.com4ed97602016-10-12 16:45:55 +00001897 layoutState.lineInfo().setFirstLine(!lastLine);
1898 layoutState.lineInfo().setPreviousLineBrokeCleanly(!lastLine || lastLine->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001899
zalan@apple.com4ed97602016-10-12 16:45:55 +00001900 if (lastLine) {
1901 setLogicalHeight(lastLine->lineBottomWithLeading());
1902 InlineIterator iter = InlineIterator(this, lastLine->lineBreakObj(), lastLine->lineBreakPos());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001903 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
zalan@apple.com4ed97602016-10-12 16:45:55 +00001904 resolver.setStatus(lastLine->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001905 } else {
akling@apple.com827be9c2013-10-29 02:58:43 +00001906 TextDirection direction = style().direction();
1907 if (style().unicodeBidi() == Plaintext)
weinig@apple.com12840dc2013-10-22 23:59:08 +00001908 determineDirectionality(direction, InlineIterator(this, bidiFirstSkippingEmptyInlines(*this), 0));
akling@apple.com827be9c2013-10-29 02:58:43 +00001909 resolver.setStatus(BidiStatus(direction, isOverride(style().unicodeBidi())));
weinig@apple.com12840dc2013-10-22 23:59:08 +00001910 InlineIterator iter = InlineIterator(this, bidiFirstSkippingEmptyInlines(*this, &resolver), 0);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001911 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001912 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001913 return currentLine;
hyatt0c3a9862004-02-23 21:26:26 +00001914}
1915
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001916void RenderBlockFlow::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001917{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001918 auto iteratorForFirstDirtyFloat = [](LineLayoutState::FloatList& floats) {
1919 auto lastCleanFloat = floats.lastCleanFloat();
1920 if (!lastCleanFloat)
1921 return floats.begin();
1922 auto* lastCleanFloatWithRect = floats.floatWithRect(*lastCleanFloat);
1923 ASSERT(lastCleanFloatWithRect);
1924 return ++floats.find(*lastCleanFloatWithRect);
1925 };
1926
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001927 ASSERT(!layoutState.endLine());
zalan@apple.com4ed97602016-10-12 16:45:55 +00001928 auto floatsIterator = iteratorForFirstDirtyFloat(layoutState.floatList());
1929 auto end = layoutState.floatList().end();
1930 RootInlineBox* lastLine = nullptr;
1931 for (RootInlineBox* currentLine = startLine->nextRootBox(); currentLine; currentLine = currentLine->nextRootBox()) {
1932 if (!currentLine->isDirty()) {
1933 if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1934 bool encounteredNewFloat = false;
1935 bool dirtiedByFloat = false;
zalan@apple.com9201b992017-10-06 22:14:05 +00001936 for (auto& floatBoxOnCleanLine : *cleanLineFloats) {
1937 if (!floatBoxOnCleanLine)
1938 continue;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001939 ASSERT(floatsIterator != end);
1940 checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1941 ++floatsIterator;
1942 if (floatsIterator == end || encounteredNewFloat)
1943 return;
1944 }
1945 }
hyatt04420ca2004-07-16 00:05:42 +00001946 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001947 if (currentLine->isDirty())
1948 lastLine = nullptr;
1949 else if (!lastLine)
1950 lastLine = currentLine;
hyatt0c3a9862004-02-23 21:26:26 +00001951 }
mitz@apple.come1364202008-02-28 01:06:41 +00001952
zalan@apple.com4ed97602016-10-12 16:45:55 +00001953 if (!lastLine)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001954 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001955
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001956 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1957 // in the block.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001958 RootInlineBox* previousLine = lastLine->prevRootBox();
1959 cleanLineStart = InlineIterator(this, previousLine->lineBreakObj(), previousLine->lineBreakPos());
1960 cleanLineBidiStatus = previousLine->lineBreakBidiStatus();
1961 layoutState.setEndLineLogicalTop(previousLine->lineBottomWithLeading());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001962
zalan@apple.com4ed97602016-10-12 16:45:55 +00001963 for (RootInlineBox* line = lastLine; line; line = line->nextRootBox()) {
1964 // Disconnect all line boxes from their render objects while preserving their connections to one another.
1965 line->extractLine();
1966 }
1967 layoutState.setEndLine(lastLine);
hyatt0c3a9862004-02-23 21:26:26 +00001968}
1969
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001970bool RenderBlockFlow::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001971{
1972 LayoutUnit lineDelta = logicalHeight() - layoutState.endLineLogicalTop();
1973
zalan@apple.com4de96c52017-11-07 04:09:59 +00001974 bool paginated = view().frameView().layoutContext().layoutState() && view().frameView().layoutContext().layoutState()->isPaginated();
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001975 if (paginated && layoutState.fragmentedFlow()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001976 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1977 // in a different available line width.
1978 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1979 if (paginated) {
1980 // This isn't the real move we're going to do, so don't update the line box's pagination
1981 // strut yet.
1982 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001983 bool overflowsFragment;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001984 lineDelta -= oldPaginationStrut;
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001985 adjustLinePositionForPagination(lineBox, lineDelta, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001986 lineBox->setPaginationStrut(oldPaginationStrut);
1987 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001988 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta, layoutState.fragmentedFlow()))
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001989 return false;
1990 }
1991 }
1992
1993 if (!lineDelta || !m_floatingObjects)
1994 return true;
1995
1996 // See if any floats end in the range along which we want to shift the lines vertically.
andersca@apple.com86298632013-11-10 19:32:33 +00001997 LayoutUnit logicalTop = std::min(logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001998
1999 RootInlineBox* lastLine = layoutState.endLine();
2000 while (RootInlineBox* nextLine = lastLine->nextRootBox())
2001 lastLine = nextLine;
2002
leviw@chromium.org3957b452012-05-01 00:06:37 +00002003 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002004
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00002005 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2006 auto end = floatingObjectSet.end();
2007 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002008 const auto& floatingObject = *it->get();
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00002009 if (logicalBottomForFloat(floatingObject) >= logicalTop && logicalBottomForFloat(floatingObject) < logicalBottom)
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002010 return false;
2011 }
2012
2013 return true;
2014}
2015
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002016bool RenderBlockFlow::lineWidthForPaginatedLineChanged(RootInlineBox* rootBox, LayoutUnit lineDelta, RenderFragmentedFlow* fragmentedFlow) const
weinig@apple.com31324fd2013-10-28 19:22:51 +00002017{
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002018 if (!fragmentedFlow)
weinig@apple.com31324fd2013-10-28 19:22:51 +00002019 return false;
2020
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002021 RenderFragmentContainer* currentFragment = fragmentAtBlockOffset(rootBox->lineTopWithLeading() + lineDelta);
2022 // Just bail if the fragment didn't change.
2023 if (rootBox->containingFragment() == currentFragment)
weinig@apple.com31324fd2013-10-28 19:22:51 +00002024 return false;
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002025 return rootBox->paginatedLineWidth() != availableLogicalWidthForContent(currentFragment);
weinig@apple.com31324fd2013-10-28 19:22:51 +00002026}
2027
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002028bool RenderBlockFlow::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00002029{
mitz@apple.com15035e62008-07-05 20:44:44 +00002030 if (resolver.position() == endLineStart) {
2031 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00002032 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002033 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002034 }
hyatt0c3a9862004-02-23 21:26:26 +00002035
mitz@apple.come1364202008-02-28 01:06:41 +00002036 // The first clean line doesn't match, but we can check a handful of following lines to try
2037 // to match back up.
cdumez@apple.com4bf983a2015-05-19 07:22:36 +00002038 static const int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002039 RootInlineBox* originalEndLine = layoutState.endLine();
2040 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00002041 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
antti@apple.comae85e112017-08-31 23:27:02 +00002042 if (line->lineBreakObj() == resolver.position().renderer() && line->lineBreakPos() == resolver.position().offset()) {
mitz@apple.come1364202008-02-28 01:06:41 +00002043 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00002044 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00002045 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002046
2047 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00002048 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00002049 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002050 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00002051 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002052 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002053 }
2054
mitz@apple.come1364202008-02-28 01:06:41 +00002055 // Now delete the lines that we failed to sync.
akling@apple.com31dd4f42013-10-30 22:27:59 +00002056 deleteLineRange(layoutState, originalEndLine, result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002057 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00002058 }
2059 }
mitz@apple.come1364202008-02-28 01:06:41 +00002060
hyatt0c3a9862004-02-23 21:26:26 +00002061 return false;
2062}
2063
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002064bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00002065{
2066 ASSERT(inlineObj->parent() == this);
2067
mitz@apple.com15035e62008-07-05 20:44:44 +00002068 InlineIterator it(this, inlineObj, 0);
rniwa@webkit.org40248422011-06-15 00:19:39 +00002069 // FIXME: We should pass correct value for WhitespacePosition.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002070 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00002071 it.increment();
bdashccffb432007-07-13 11:51:40 +00002072
2073 return !it.atEnd();
2074}
2075
weinig@apple.com611b9292013-10-20 22:57:54 +00002076void RenderBlockFlow::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002077{
antti@apple.comfea51992013-10-28 13:39:23 +00002078 if (auto layout = simpleLineLayout()) {
antti@apple.com940f5872013-10-24 20:31:11 +00002079 ASSERT(!hasOverflowClip());
antti@apple.comfea51992013-10-28 13:39:23 +00002080 SimpleLineLayout::collectFlowOverflow(*this, *layout);
antti@apple.com940f5872013-10-24 20:31:11 +00002081 return;
2082 }
eae@chromium.org9717cd82012-11-07 18:33:44 +00002083 LayoutUnit endPadding = hasOverflowClip() ? paddingEnd() : LayoutUnit();
hyatt@apple.com592848f2010-12-06 20:03:43 +00002084 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
akling@apple.com827be9c2013-10-29 02:58:43 +00002085 if (hasOverflowClip() && !endPadding && element() && element()->isRootEditableElement() && style().isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002086 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002087 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002088 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002089 RenderFragmentContainer* fragment = enclosingFragmentedFlow() ? curr->containingFragment() : nullptr;
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002090 if (fragment)
2091 fragment->addLayoutOverflowForBox(this, curr->paddedLayoutOverflowRect(endPadding));
abucur@adobe.com6585d012013-09-04 08:26:41 +00002092 if (!hasOverflowClip()) {
zalan@apple.comd423bcc2016-02-06 23:07:54 +00002093 LayoutRect childVisualOverflowRect = curr->visualOverflowRect(curr->lineTop(), curr->lineBottom());
2094 addVisualOverflow(childVisualOverflowRect);
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002095 if (fragment)
2096 fragment->addVisualOverflowForBox(this, childVisualOverflowRect);
abucur@adobe.com6585d012013-09-04 08:26:41 +00002097 }
hyattb4b20872004-10-20 21:34:01 +00002098 }
2099}
2100
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002101void RenderBlockFlow::deleteEllipsisLineBoxes()
hyatted77ad82004-06-15 07:21:23 +00002102{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002103 TextAlignMode textAlign = style().textAlign();
akling@apple.com827be9c2013-10-29 02:58:43 +00002104 bool ltr = style().isLeftToRightDirection();
zalan@apple.com64761fe2016-03-02 21:42:22 +00002105 IndentTextOrNot shouldIndentText = IndentText;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002106 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2107 if (curr->hasEllipsisBox()) {
2108 curr->clearTruncation();
2109
2110 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
zalan@apple.com64761fe2016-03-02 21:42:22 +00002111 float logicalLeft = logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
2112 float availableLogicalWidth = logicalRightOffsetForLine(curr->lineTop(), DoNotIndentText) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002113 float totalLogicalWidth = curr->logicalWidth();
mario.prada@samsung.com79397522014-02-28 18:12:52 +00002114 updateLogicalWidthForAlignment(textAlign, curr, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002115
2116 if (ltr)
2117 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2118 else
2119 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2120 }
zalan@apple.com64761fe2016-03-02 21:42:22 +00002121 shouldIndentText = DoNotIndentText;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002122 }
hyatted77ad82004-06-15 07:21:23 +00002123}
2124
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002125void RenderBlockFlow::checkLinesForTextOverflow()
hyatted77ad82004-06-15 07:21:23 +00002126{
2127 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002128 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
antti@apple.comc54cbc92015-01-15 14:19:56 +00002129 const FontCascade& font = style().fontCascade();
mmaxfield@apple.come6da30e2015-07-25 03:51:06 +00002130 static NeverDestroyed<AtomicString> ellipsisStr(&horizontalEllipsis, 1);
antti@apple.comc54cbc92015-01-15 14:19:56 +00002131 const FontCascade& firstLineFont = firstLineStyle().fontCascade();
mmaxfield@apple.com21a4dcb2016-03-13 00:36:59 +00002132 float firstLineEllipsisWidth = firstLineFont.width(constructTextRun(&horizontalEllipsis, 1, firstLineStyle()));
2133 float ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(&horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002134
2135 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2136 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2137 // check the left edge of the line box to see if it is less
2138 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
akling@apple.com827be9c2013-10-29 02:58:43 +00002139 bool ltr = style().isLeftToRightDirection();
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002140 TextAlignMode textAlign = style().textAlign();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002141 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002142 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
zalan@apple.com64761fe2016-03-02 21:42:22 +00002143 IndentTextOrNot shouldIndentText = firstLine ? IndentText : DoNotIndentText;
2144 LayoutUnit blockRightEdge = logicalRightOffsetForLine(curr->lineTop(), shouldIndentText);
2145 LayoutUnit blockLeftEdge = logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
zalan@apple.combfa11952014-05-19 17:37:22 +00002146 LayoutUnit lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002147 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002148 // This line spills out of our box in the appropriate direction. Now we need to see if the line
hyatted77ad82004-06-15 07:21:23 +00002149 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2150 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2151 // space.
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002152 LayoutUnit width = firstLine ? firstLineEllipsisWidth : ellipsisWidth;
eae@chromium.orgee8613e2011-11-12 01:12:58 +00002153 LayoutUnit blockEdge = ltr ? blockRightEdge : blockLeftEdge;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002154 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2155 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2156
akling@apple.comf294cf02013-07-17 18:46:39 +00002157 float logicalLeft = 0; // We are only interested in the delta from the base position.
zalan@apple.com64761fe2016-03-02 21:42:22 +00002158 float truncatedWidth = availableLogicalWidthForLine(curr->lineTop(), shouldIndentText);
mmaxfield@apple.come6da30e2015-07-25 03:51:06 +00002159 updateLogicalWidthForAlignment(textAlign, curr, nullptr, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002160 if (ltr)
2161 curr->adjustLogicalPosition(logicalLeft, 0);
2162 else
2163 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2164 }
hyatted77ad82004-06-15 07:21:23 +00002165 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002166 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002167 }
2168}
2169
zalan@apple.com48ea2832015-10-16 19:53:04 +00002170bool RenderBlockFlow::positionNewFloatOnLine(const FloatingObject& newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002171{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002172 if (!positionNewFloats())
2173 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002174
rniwa@webkit.org44424752011-04-14 00:58:40 +00002175 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002176
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002177 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2178 // the line and the previous line had a hard break (so this line is either the first in the block
2179 // or follows a <br>).
zalan@apple.com48ea2832015-10-16 19:53:04 +00002180 if (!newFloat.paginationStrut() || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002181 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002182
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002183 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
zalan@apple.com48ea2832015-10-16 19:53:04 +00002184 ASSERT(floatingObjectSet.last().get() == &newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002185
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002186 LayoutUnit floatLogicalTop = logicalTopForFloat(newFloat);
zalan@apple.com48ea2832015-10-16 19:53:04 +00002187 LayoutUnit paginationStrut = newFloat.paginationStrut();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002188
hyatt@apple.com5950bd42011-09-27 20:39:57 +00002189 if (floatLogicalTop - paginationStrut != logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002190 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002191
darin@apple.com7cad7042013-09-24 05:53:55 +00002192 auto it = floatingObjectSet.end();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002193 --it; // Last float is newFloat, skip that one.
darin@apple.com7cad7042013-09-24 05:53:55 +00002194 auto begin = floatingObjectSet.begin();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002195 while (it != begin) {
2196 --it;
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002197 auto& floatingObject = *it->get();
2198 if (&floatingObject == lastFloatFromPreviousLine)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002199 break;
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002200 if (logicalTopForFloat(floatingObject) == logicalHeight() + lineInfo.floatPaginationStrut()) {
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002201 floatingObject.setPaginationStrut(paginationStrut + floatingObject.paginationStrut());
2202 RenderBox& floatBox = floatingObject.renderer();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002203 setLogicalTopForChild(floatBox, logicalTopForChild(floatBox) + marginBeforeForChild(floatBox) + paginationStrut);
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002204
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002205 if (updateFragmentRangeForBoxChild(floatBox))
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002206 floatBox.setNeedsLayout(MarkOnlyThis);
cdumez@apple.come9437792014-10-08 23:33:43 +00002207 else if (is<RenderBlock>(floatBox))
2208 downcast<RenderBlock>(floatBox).setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002209 floatBox.layoutIfNeeded();
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002210
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002211 // Save the old logical top before calling removePlacedObject which will set
2212 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002213 LayoutUnit oldLogicalTop = logicalTopForFloat(floatingObject);
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002214 m_floatingObjects->removePlacedObject(&floatingObject);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002215 setLogicalTopForFloat(floatingObject, oldLogicalTop + paginationStrut);
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002216 m_floatingObjects->addPlacedObject(&floatingObject);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002217 }
2218 }
2219
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002220 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2221 // no content, then we don't want to improperly grow the height of the block.
2222 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002223 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002224}
2225
zalan@apple.com4d97a002016-02-24 17:13:33 +00002226LayoutUnit RenderBlockFlow::startAlignedOffsetForLine(LayoutUnit position, IndentTextOrNot shouldIndentText)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002227{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002228 TextAlignMode textAlign = style().textAlign();
zalan@apple.com4d97a002016-02-24 17:13:33 +00002229 bool shouldApplyIndentText = false;
2230 switch (textAlign) {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002231 case TextAlignMode::Left:
2232 case TextAlignMode::WebKitLeft:
zalan@apple.com4d97a002016-02-24 17:13:33 +00002233 shouldApplyIndentText = style().isLeftToRightDirection();
2234 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002235 case TextAlignMode::Right:
2236 case TextAlignMode::WebKitRight:
zalan@apple.com4d97a002016-02-24 17:13:33 +00002237 shouldApplyIndentText = !style().isLeftToRightDirection();
2238 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002239 case TextAlignMode::Start:
zalan@apple.com4d97a002016-02-24 17:13:33 +00002240 shouldApplyIndentText = true;
2241 break;
2242 default:
2243 shouldApplyIndentText = false;
2244 }
hyatt@apple.coma5626692013-11-18 23:08:30 +00002245 // <rdar://problem/15427571>
2246 // https://bugs.webkit.org/show_bug.cgi?id=124522
2247 // This quirk is for legacy content that doesn't work properly with the center positioning scheme
2248 // being honored (e.g., epubs).
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002249 if (shouldApplyIndentText || settings().useLegacyTextAlignPositionedElementBehavior()) // FIXME: Handle TextAlignMode::End here
zalan@apple.com64761fe2016-03-02 21:42:22 +00002250 return startOffsetForLine(position, shouldIndentText);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002251
2252 // updateLogicalWidthForAlignment() handles the direction of the block so no need to consider it here
robert@webkit.org82903f42012-08-28 19:18:40 +00002253 float totalLogicalWidth = 0;
zalan@apple.com64761fe2016-03-02 21:42:22 +00002254 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), DoNotIndentText);
2255 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), DoNotIndentText) - logicalLeft;
mario.prada@samsung.com79397522014-02-28 18:12:52 +00002256
2257 // FIXME: Bug 129311: We need to pass a valid RootInlineBox here, considering the bidi level used to construct the line.
2258 updateLogicalWidthForAlignment(textAlign, 0, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
robert@webkit.org7861a102011-09-22 17:16:47 +00002259
akling@apple.com827be9c2013-10-29 02:58:43 +00002260 if (!style().isLeftToRightDirection())
robert@webkit.org82903f42012-08-28 19:18:40 +00002261 return logicalWidth() - logicalLeft;
robert@webkit.orgfc7763c2011-09-03 18:28:57 +00002262 return logicalLeft;
2263}
2264
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002265void RenderBlockFlow::updateFragmentForLine(RootInlineBox* lineBox) const
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002266{
2267 ASSERT(lineBox);
akling@apple.combdf247b2014-01-13 22:33:08 +00002268
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002269 if (!hasFragmentRangeInFragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002270 lineBox->clearContainingFragment();
abucur@adobe.comc18af352014-05-14 06:33:48 +00002271 else {
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002272 if (auto containingFragment = fragmentAtBlockOffset(lineBox->lineTopWithLeading()))
2273 lineBox->setContainingFragment(*containingFragment);
abucur@adobe.comc18af352014-05-14 06:33:48 +00002274 else
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002275 lineBox->clearContainingFragment();
abucur@adobe.comc18af352014-05-14 06:33:48 +00002276 }
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002277
2278 RootInlineBox* prevLineBox = lineBox->prevRootBox();
2279 if (!prevLineBox)
2280 return;
2281
2282 // This check is more accurate than the one in |adjustLinePositionForPagination| because it takes into
2283 // account just the container changes between lines. The before mentioned function doesn't set the flag
2284 // correctly if the line is positioned at the top of the last fragment container.
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002285 if (lineBox->containingFragment() != prevLineBox->containingFragment())
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002286 lineBox->setIsFirstAfterPageBreak(true);
2287}
2288
hyattffe78712003-02-11 01:59:29 +00002289}