blob: f4d2d5f40de1fff93d1d5c7a2e3944429ec33750 [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)
antti@apple.com50b36fde2019-08-11 11:02:01 +00003 * Copyright (C) 2003-2019 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"
antti@apple.com50b36fde2019-08-11 11:02:01 +000026#include "ComplexLineLayout.h"
darin36d11362006-04-11 16:30:21 +000027
weinig@apple.comcef4e1e2013-10-19 03:14:44 +000028#include "AXObjectCache.h"
mitz@apple.com4c1ff322009-07-13 00:54:12 +000029#include "BidiResolver.h"
mmaxfield@apple.com167a0e92015-03-06 19:06:30 +000030#include "BreakingContext.h"
bjonesbe@adobe.com67478092013-09-09 22:18:17 +000031#include "FloatingObjects.h"
darin@apple.com5917cb12017-11-23 17:32:42 +000032#include "HTMLParserIdioms.h"
akling@apple.comd3ec5ef2013-11-07 03:30:11 +000033#include "InlineElementBox.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000034#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000035#include "InlineTextBox.h"
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +000036#include "InlineTextBoxStyle.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.comf41183e2018-12-07 20:10:14 +000042#include "RenderLayoutState.h"
antti@apple.com8d8ae712013-09-18 18:04:32 +000043#include "RenderLineBreak.h"
mmaxfield@apple.com8301e832014-10-09 01:14:30 +000044#include "RenderRubyBase.h"
45#include "RenderRubyText.h"
antti@apple.com50b36fde2019-08-11 11:02:01 +000046#include "RenderSVGText.h"
hyattd8048342006-05-31 01:48:18 +000047#include "RenderView.h"
ossy@webkit.org66d8c0a2014-02-05 11:42:35 +000048#include "SVGRootInlineBox.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000049#include "Settings.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000050#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000051#include "VerticalPositionCache.h"
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000052#include <wtf/StdLibExtras.h>
commit-queue@webkit.orgb3540512012-08-24 18:48:49 +000053
darinb9481ed2006-03-20 02:57:59 +000054namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000055
antti@apple.com50b36fde2019-08-11 11:02:01 +000056ComplexLineLayout::ComplexLineLayout(RenderBlockFlow& flow)
57 : m_flow(flow)
58{
59}
60
61ComplexLineLayout::~ComplexLineLayout() = default;
62
leviw@chromium.orge7812f32012-02-07 23:46:40 +000063static void determineDirectionality(TextDirection& dir, InlineIterator iter)
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000064{
65 while (!iter.atEnd()) {
66 if (iter.atParagraphSeparator())
67 return;
68 if (UChar current = iter.current()) {
darin@apple.com2eb5f4d2013-10-12 04:16:42 +000069 UCharDirection charDirection = u_charDirection(current);
70 if (charDirection == U_LEFT_TO_RIGHT) {
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +000071 dir = TextDirection::LTR;
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000072 return;
73 }
darin@apple.com2eb5f4d2013-10-12 04:16:42 +000074 if (charDirection == U_RIGHT_TO_LEFT || charDirection == U_RIGHT_TO_LEFT_ARABIC) {
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +000075 dir = TextDirection::RTL;
leviw@chromium.org7781b6a2011-06-27 22:01:38 +000076 return;
77 }
78 }
79 iter.increment();
80 }
81}
82
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +000083inline std::unique_ptr<BidiRun> createRun(int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
eric@webkit.org5bee2942011-04-08 02:12:31 +000084{
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +000085 return makeUnique<BidiRun>(start, end, obj, resolver.context(), resolver.dir());
eric@webkit.org5bee2942011-04-08 02:12:31 +000086}
87
antti@apple.com50b36fde2019-08-11 11:02:01 +000088void ComplexLineLayout::appendRunsForObject(BidiRunList<BidiRun>* runs, int start, int end, RenderObject& obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +000089{
antti@apple.com50b36fde2019-08-11 11:02:01 +000090 if (start > end || RenderBlock::shouldSkipCreatingRunsForObject(obj))
hyatteb003b82002-11-15 22:35:10 +000091 return;
hyatt85586af2003-02-19 23:22:42 +000092
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +000093 LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
94 bool haveNextTransition = (lineWhitespaceCollapsingState.currentTransition() < lineWhitespaceCollapsingState.numTransitions());
95 InlineIterator nextTransition;
96 if (haveNextTransition)
97 nextTransition = lineWhitespaceCollapsingState.transitions()[lineWhitespaceCollapsingState.currentTransition()];
98 if (lineWhitespaceCollapsingState.betweenTransitions()) {
99 if (!haveNextTransition || (&obj != nextTransition.renderer()))
hyatt33f8d492002-11-12 21:44:52 +0000100 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000101 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000102 // adjust our start.
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000103 start = nextTransition.offset();
104 lineWhitespaceCollapsingState.incrementCurrentTransition();
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +0000105 if (start < end) {
106 appendRunsForObject(runs, start, end, obj, resolver);
107 return;
108 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000109 } else {
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000110 if (!haveNextTransition || (&obj != nextTransition.renderer())) {
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +0000111 if (runs)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +0000112 runs->appendRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000113 return;
114 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000115
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000116 // An end transition has been encountered within our object. We need to append a run with our endpoint.
117 if (static_cast<int>(nextTransition.offset() + 1) <= end) {
118 lineWhitespaceCollapsingState.incrementCurrentTransition();
mmaxfield@apple.com0682d512014-03-25 20:15:14 +0000119 // The end of the line is before the object we're inspecting. Skip everything and return
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000120 if (nextTransition.refersToEndOfPreviousNode())
mmaxfield@apple.com0682d512014-03-25 20:15:14 +0000121 return;
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +0000122 if (static_cast<int>(nextTransition.offset() + 1) > start && runs)
123 runs->appendRun(createRun(start, nextTransition.offset() + 1, obj, resolver));
124 appendRunsForObject(runs, nextTransition.offset() + 1, end, obj, resolver);
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +0000125 } else if (runs)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +0000126 runs->appendRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000127 }
128}
129
antti@apple.com50b36fde2019-08-11 11:02:01 +0000130std::unique_ptr<RootInlineBox> ComplexLineLayout::createRootInlineBox()
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000131{
antti@apple.com50b36fde2019-08-11 11:02:01 +0000132 if (is<RenderSVGText>(m_flow)) {
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000133 auto box = makeUnique<SVGRootInlineBox>(downcast<RenderSVGText>(m_flow));
antti@apple.com50b36fde2019-08-11 11:02:01 +0000134 box->setHasVirtualLogicalHeight();
135 return box;
136 }
137
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000138 return makeUnique<RootInlineBox>(m_flow);
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000139}
140
antti@apple.com50b36fde2019-08-11 11:02:01 +0000141RootInlineBox* ComplexLineLayout::createAndAppendRootInlineBox()
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000142{
akling@apple.comb5f24642013-11-06 04:47:12 +0000143 auto newRootBox = createRootInlineBox();
144 RootInlineBox* rootBox = newRootBox.get();
aestes@apple.com13aae082016-01-02 08:03:08 +0000145 m_lineBoxes.appendLineBox(WTFMove(newRootBox));
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000146
akling@apple.comee3c8df2013-11-06 08:09:44 +0000147 if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && firstRootBox() == rootBox) {
antti@apple.com50b36fde2019-08-11 11:02:01 +0000148 if (AXObjectCache* cache = m_flow.document().existingAXObjectCache())
149 cache->deferRecomputeIsIgnored(m_flow.element());
weinig@apple.comcef4e1e2013-10-19 03:14:44 +0000150 }
151
152 return rootBox;
153}
154
antti@apple.comc6a9c732019-08-12 15:31:34 +0000155InlineBox* ComplexLineLayout::createInlineBoxForRenderer(RenderObject* renderer, bool isOnlyRun)
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000156{
antti@apple.comc6a9c732019-08-12 15:31:34 +0000157 if (renderer == &m_flow)
158 return createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000159
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000160 if (is<RenderText>(*renderer))
161 return downcast<RenderText>(*renderer).createInlineTextBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000162
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000163 if (is<RenderBox>(*renderer)) {
akling@apple.comb5f24642013-11-06 04:47:12 +0000164 // FIXME: This is terrible. This branch returns an *owned* pointer!
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000165 return downcast<RenderBox>(*renderer).createInlineBox().release();
akling@apple.comb5f24642013-11-06 04:47:12 +0000166 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000167
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000168 if (is<RenderLineBreak>(*renderer)) {
akling@apple.comb5f24642013-11-06 04:47:12 +0000169 // FIXME: This is terrible. This branch returns an *owned* pointer!
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000170 auto inlineBox = downcast<RenderLineBreak>(*renderer).createInlineBox().release();
antti@apple.com9d8157e2013-09-17 15:13:37 +0000171 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
172 // (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 +0000173 inlineBox->setBehavesLikeText(isOnlyRun || renderer->document().inNoQuirksMode() || renderer->isLineBreakOpportunity());
antti@apple.com9d8157e2013-09-17 15:13:37 +0000174 return inlineBox;
175 }
176
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000177 return downcast<RenderInline>(*renderer).createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000178}
179
weinig@apple.com12840dc2013-10-22 23:59:08 +0000180static inline void dirtyLineBoxesForRenderer(RenderObject& renderer, bool fullLayout)
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000181{
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000182 if (is<RenderText>(renderer)) {
183 RenderText& renderText = downcast<RenderText>(renderer);
esprehn@chromium.org7745ffb2013-02-19 21:51:19 +0000184 updateCounterIfNeeded(renderText);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000185 renderText.dirtyLineBoxes(fullLayout);
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000186 } else if (is<RenderLineBreak>(renderer))
187 downcast<RenderLineBreak>(renderer).dirtyLineBoxes(fullLayout);
antti@apple.com9d8157e2013-09-17 15:13:37 +0000188 else
cdumez@apple.com35094bd2014-10-07 19:33:53 +0000189 downcast<RenderInline>(renderer).dirtyLineBoxes(fullLayout);
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000190}
191
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000192static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
193{
194 do {
195 if (parentBox->isConstructed() || parentBox->nextOnLine())
196 return true;
197 parentBox = parentBox->parent();
198 } while (parentBox);
199 return false;
200}
201
antti@apple.com50b36fde2019-08-11 11:02:01 +0000202InlineFlowBox* ComplexLineLayout::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000203{
204 // See if we have an unconstructed line box for this object that is also
205 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000206 unsigned lineDepth = 1;
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000207 InlineFlowBox* parentBox = nullptr;
208 InlineFlowBox* result = nullptr;
akling@apple.com827be9c2013-10-29 02:58:43 +0000209 bool hasDefaultLineBoxContain = style().lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000210 do {
antti@apple.com50b36fde2019-08-11 11:02:01 +0000211 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(is<RenderInline>(*obj) || obj == &m_flow);
eric@webkit.org060caf62011-05-03 22:11:39 +0000212
antti@apple.com50b36fde2019-08-11 11:02:01 +0000213 RenderInline* inlineFlow = obj != &m_flow ? downcast<RenderInline>(obj) : nullptr;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000214
hyatt1d5d87b2007-04-24 04:55:54 +0000215 // Get the last box we made for this render object.
cdumez@apple.com34e77ab2014-10-09 16:17:06 +0000216 parentBox = inlineFlow ? inlineFlow->lastLineBox() : downcast<RenderBlockFlow>(*obj).lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000217
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000218 // If this box or its ancestor is constructed then it is from a previous line, and we need
antti@apple.com50b36fde2019-08-11 11:02:01 +0000219 // 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 +0000220 // something following it on the line, then we know we have to make a new box
antti@apple.com50b36fde2019-08-11 11:02:01 +0000221 // as well. In this situation our inline has actually been split in two on
hyatt1d5d87b2007-04-24 04:55:54 +0000222 // the same line (this can happen with very fancy language mixtures).
223 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000224 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000225 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000226 if (allowedToConstructNewBox && !canUseExistingParentBox) {
antti@apple.com50b36fde2019-08-11 11:02:01 +0000227 // We need to make a new box for this render object. Once
hyatt1d5d87b2007-04-24 04:55:54 +0000228 // made, we need to place it at the end of the current line.
antti@apple.comc6a9c732019-08-12 15:31:34 +0000229 InlineBox* newBox = createInlineBoxForRenderer(obj);
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000230 parentBox = downcast<InlineFlowBox>(newBox);
antti@apple.comb0608f62013-09-28 18:30:16 +0000231 parentBox->setIsFirstLine(lineInfo.isFirstLine());
antti@apple.com50b36fde2019-08-11 11:02:01 +0000232 parentBox->setIsHorizontal(m_flow.isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000233 if (!hasDefaultLineBoxContain)
234 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000235 constructedNewBox = true;
236 }
mitz@apple.come1364202008-02-28 01:06:41 +0000237
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000238 if (constructedNewBox || canUseExistingParentBox) {
239 if (!result)
240 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000241
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000242 // If we have hit the block itself, then |box| represents the root
243 // inline box for the line, and it doesn't have to be appended to any parent
244 // inline.
245 if (childBox)
246 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000247
antti@apple.com50b36fde2019-08-11 11:02:01 +0000248 if (!constructedNewBox || obj == &m_flow)
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000249 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000250
eric@webkit.org060caf62011-05-03 22:11:39 +0000251 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000252 }
mitz@apple.come1364202008-02-28 01:06:41 +0000253
hyatt1d5d87b2007-04-24 04:55:54 +0000254 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
255 // intermediate inline flows.
antti@apple.com50b36fde2019-08-11 11:02:01 +0000256 obj = (++lineDepth >= cMaxLineDepth) ? &m_flow : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000257
hyatt1d5d87b2007-04-24 04:55:54 +0000258 } while (true);
259
260 return result;
hyattffe78712003-02-11 01:59:29 +0000261}
262
darin@apple.com5917cb12017-11-23 17:32:42 +0000263template<typename CharacterType> static inline bool endsWithHTMLSpaces(const CharacterType* characters, unsigned position, unsigned end)
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000264{
darin@apple.com5917cb12017-11-23 17:32:42 +0000265 for (unsigned i = position; i < end; ++i) {
266 if (!isHTMLSpace(characters[i]))
267 return false;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000268 }
darin@apple.com5917cb12017-11-23 17:32:42 +0000269 return true;
msaboff@apple.com776c286c72012-10-15 16:56:29 +0000270}
271
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000272static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000273{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000274 BidiRun* run = bidiRuns.logicallyLastRun();
275 if (!run)
276 return true;
darin@apple.com5917cb12017-11-23 17:32:42 +0000277 if (!is<RenderText>(run->renderer()))
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000278 return false;
darin@apple.com5917cb12017-11-23 17:32:42 +0000279 auto& text = downcast<RenderText>(run->renderer()).text();
280 unsigned position = run->stop();
281 unsigned length = text.length();
282 if (text.is8Bit())
283 return endsWithHTMLSpaces(text.characters8(), position, length);
284 return endsWithHTMLSpaces(text.characters16(), position, length);
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000285}
286
antti@apple.com50b36fde2019-08-11 11:02:01 +0000287RootInlineBox* ComplexLineLayout::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000288{
289 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000290
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000291 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000292 InlineFlowBox* parentBox = 0;
robert@webkit.org8cbab142011-12-30 20:58:29 +0000293 int runCount = bidiRuns.runCount() - lineInfo.runsFromLeadingWhitespace();
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000294
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000295 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000296 // Create a box for our object.
robert@webkit.org8cbab142011-12-30 20:58:29 +0000297 bool isOnlyRun = (runCount == 1);
akling@apple.com7506fda2013-11-07 10:12:36 +0000298 if (runCount == 2 && !r->renderer().isListMarker())
299 isOnlyRun = (!style().isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->renderer().isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000300
robert@webkit.org56e5a9f2012-02-17 21:10:42 +0000301 if (lineInfo.isEmpty())
302 continue;
303
antti@apple.comc6a9c732019-08-12 15:31:34 +0000304 InlineBox* box = createInlineBoxForRenderer(&r->renderer(), isOnlyRun);
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000305 r->setBox(box);
hyattffe78712003-02-11 01:59:29 +0000306
akling@apple.com0b8172b72013-08-31 18:34:23 +0000307 if (!rootHasSelectedChildren && box->renderer().selectionState() != RenderObject::SelectionNone)
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000308 rootHasSelectedChildren = true;
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000309
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000310 // If we have no parent box yet, or if the run is not simply a sibling,
311 // then we need to construct inline boxes as necessary to properly enclose the
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000312 // run's inline box. Segments can only be siblings at the root level, as
313 // they are positioned separately.
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000314 if (!parentBox || &parentBox->renderer() != r->renderer().parent()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000315 // Create new inline boxes all the way back to the appropriate insertion point.
hyatt@apple.com8e8cd252015-04-02 18:34:57 +0000316 RenderObject* parentToUse = r->renderer().parent();
hyatt@apple.comdafe5972015-03-31 17:42:24 +0000317 parentBox = createLineBoxes(parentToUse, lineInfo, box);
bjonesbe@adobe.comfb6ceac2014-04-15 00:27:13 +0000318 } else {
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000319 // Append the inline box to this line.
320 parentBox->addToLine(box);
321 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000322
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000323 bool visuallyOrdered = r->renderer().style().rtlOrdering() == Order::Visual;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000324 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000325
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000326 if (is<InlineTextBox>(*box)) {
327 auto& textBox = downcast<InlineTextBox>(*box);
328 textBox.setStart(r->m_start);
329 textBox.setLen(r->m_stop - r->m_start);
330 textBox.setDirOverride(r->dirOverride(visuallyOrdered));
mitz@apple.comb2107652010-06-21 16:54:52 +0000331 if (r->m_hasHyphen)
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000332 textBox.setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000333 }
hyattffe78712003-02-11 01:59:29 +0000334 }
335
antti@apple.com50b36fde2019-08-11 11:02:01 +0000336 // We should have a root inline box. It should be unconstructed and
hyattffe78712003-02-11 01:59:29 +0000337 // be the last continuation of our line list.
akling@apple.comee3c8df2013-11-06 08:09:44 +0000338 ASSERT(lastRootBox() && !lastRootBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000339
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000340 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
341 // from the bidi runs walk above has a selection state.
342 if (rootHasSelectedChildren)
akling@apple.comee3c8df2013-11-06 08:09:44 +0000343 lastRootBox()->root().setHasSelectedChildren(true);
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000344
hyattffe78712003-02-11 01:59:29 +0000345 // Set bits on our inline flow boxes that indicate which sides should
antti@apple.com50b36fde2019-08-11 11:02:01 +0000346 // paint borders/margins/padding. This knowledge will ultimately be used when
hyattffe78712003-02-11 01:59:29 +0000347 // we determine the horizontal positions and widths of all the inline boxes on
348 // the line.
hyatt@apple.com3ab5f732016-03-25 19:25:05 +0000349 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->renderer().isText() ? !reachedEndOfTextRenderer(bidiRuns) : !is<RenderInline>(bidiRuns.logicallyLastRun()->renderer());
akling@apple.com7506fda2013-11-07 10:12:36 +0000350 lastRootBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, &bidiRuns.logicallyLastRun()->renderer());
hyattffe78712003-02-11 01:59:29 +0000351
352 // Now mark the line boxes as being constructed.
akling@apple.comee3c8df2013-11-06 08:09:44 +0000353 lastRootBox()->setConstructed();
hyattffe78712003-02-11 01:59:29 +0000354
355 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000356 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000357}
358
antti@apple.com50b36fde2019-08-11 11:02:01 +0000359TextAlignMode ComplexLineLayout::textAlignmentForLine(bool endsWithSoftBreak) const
mitz@apple.com390fa322011-02-24 23:07:06 +0000360{
antti@apple.com50b36fde2019-08-11 11:02:01 +0000361 if (auto overrideAlignment = m_flow.overrideTextAlignmentForLine(endsWithSoftBreak))
362 return *overrideAlignment;
363
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000364 TextAlignMode alignment = style().textAlign();
zoltan@webkit.orgcb4f0e42014-08-13 18:00:27 +0000365#if ENABLE(CSS3_TEXT)
366 TextJustify textJustify = style().textJustify();
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000367 if (alignment == TextAlignMode::Justify && textJustify == TextJustify::None)
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +0000368 return style().direction() == TextDirection::LTR ? TextAlignMode::Left : TextAlignMode::Right;
zoltan@webkit.orgcb4f0e42014-08-13 18:00:27 +0000369#endif
370
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000371 if (endsWithSoftBreak)
372 return alignment;
mitz@apple.com390fa322011-02-24 23:07:06 +0000373
zoltan@webkit.org14989c32014-08-14 21:36:40 +0000374#if !ENABLE(CSS3_TEXT)
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000375 return (alignment == TextAlignMode::Justify) ? TextAlignMode::Start : alignment;
zoltan@webkit.org14989c32014-08-14 21:36:40 +0000376#else
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000377 if (alignment != TextAlignMode::Justify)
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000378 return alignment;
379
380 TextAlignLast alignmentLast = style().textAlignLast();
381 switch (alignmentLast) {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000382 case TextAlignLast::Start:
383 return TextAlignMode::Start;
384 case TextAlignLast::End:
385 return TextAlignMode::End;
386 case TextAlignLast::Left:
387 return TextAlignMode::Left;
388 case TextAlignLast::Right:
389 return TextAlignMode::Right;
390 case TextAlignLast::Center:
391 return TextAlignMode::Center;
392 case TextAlignLast::Justify:
393 return TextAlignMode::Justify;
394 case TextAlignLast::Auto:
395 if (textJustify == TextJustify::Distribute)
396 return TextAlignMode::Justify;
397 return TextAlignMode::Start;
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000398 }
mitz@apple.com390fa322011-02-24 23:07:06 +0000399 return alignment;
zoltan@webkit.orgcb7f93f2014-01-17 20:33:04 +0000400#endif
mitz@apple.com390fa322011-02-24 23:07:06 +0000401}
402
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000403static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
404{
405 // The direction of the block should determine what happens with wide lines.
406 // In particular with RTL blocks, wide lines should still spill out to the left.
407 if (isLeftToRightDirection) {
408 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
andersca@apple.com86298632013-11-10 19:32:33 +0000409 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000410 return;
411 }
412
413 if (trailingSpaceRun)
akling@apple.com7506fda2013-11-07 10:12:36 +0000414 trailingSpaceRun->box()->setLogicalWidth(0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000415 else if (totalLogicalWidth > availableLogicalWidth)
416 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
417}
418
419static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
420{
421 // Wide lines spill out of the block based off direction.
422 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
423 // side of the block.
424 if (isLeftToRightDirection) {
425 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000426 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
427 trailingSpaceRun->box()->setLogicalWidth(0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000428 }
mmaxfield@apple.come6da30e2015-07-25 03:51:06 +0000429 logicalLeft += std::max(0.f, availableLogicalWidth - totalLogicalWidth);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000430 return;
431 }
432
433 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
andersca@apple.com86298632013-11-10 19:32:33 +0000434 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceRun->box()->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
akling@apple.com7506fda2013-11-07 10:12:36 +0000435 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000436 } else
437 logicalLeft += availableLogicalWidth - totalLogicalWidth;
438}
439
440static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
441{
442 float trailingSpaceWidth = 0;
443 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000444 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
andersca@apple.com86298632013-11-10 19:32:33 +0000445 trailingSpaceWidth = std::min(trailingSpaceRun->box()->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
446 trailingSpaceRun->box()->setLogicalWidth(std::max<float>(0, trailingSpaceWidth));
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000447 }
448 if (isLeftToRightDirection)
andersca@apple.com86298632013-11-10 19:32:33 +0000449 logicalLeft += std::max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000450 else
451 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
452}
453
antti@apple.com50b36fde2019-08-11 11:02:01 +0000454void ComplexLineLayout::setMarginsForRubyRun(BidiRun* run, RenderRubyRun& renderer, RenderObject* previousObject, const LineInfo& lineInfo)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000455{
mmaxfield@apple.com3273fd72014-12-16 22:53:19 +0000456 float startOverhang;
457 float endOverhang;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000458 RenderObject* nextObject = 0;
459 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000460 if (!runWithNextObject->renderer().isOutOfFlowPositioned() && !runWithNextObject->box()->isLineBreak()) {
461 nextObject = &runWithNextObject->renderer();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000462 break;
463 }
464 }
akling@apple.com827be9c2013-10-29 02:58:43 +0000465 renderer.getOverhang(lineInfo.isFirstLine(), renderer.style().isLeftToRightDirection() ? previousObject : nextObject, renderer.style().isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
antti@apple.com50b36fde2019-08-11 11:02:01 +0000466 m_flow.setMarginStartForChild(renderer, LayoutUnit(-startOverhang));
467 m_flow.setMarginEndForChild(renderer, LayoutUnit(-endOverhang));
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000468}
469
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000470static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText& renderer, float xPos, const LineInfo& lineInfo,
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000471 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000472{
antti@apple.com5a8f7942015-01-22 21:57:04 +0000473 HashSet<const Font*> fallbackFonts;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000474 GlyphOverflow glyphOverflow;
antti@apple.comb0608f62013-09-28 18:30:16 +0000475
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000476 const FontCascade& font = lineStyle(*renderer.parent(), lineInfo).fontCascade();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000477 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
478 if (lineBox->fitsToGlyphs()) {
479 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
480 // will keep us from computing glyph bounds in nearly all cases.
481 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
akling@apple.com7506fda2013-11-07 10:12:36 +0000482 int baselineShift = lineBox->verticalPositionForBox(run->box(), verticalPositionCache);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000483 int rootDescent = includeRootLine ? font.fontMetrics().descent() : 0;
484 int rootAscent = includeRootLine ? font.fontMetrics().ascent() : 0;
485 int boxAscent = font.fontMetrics().ascent() - baselineShift;
486 int boxDescent = font.fontMetrics().descent() + baselineShift;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000487 if (boxAscent > rootDescent || boxDescent > rootAscent)
488 glyphOverflow.computeBounds = true;
489 }
490
ross.kirsling@sony.combd744282018-11-18 03:14:31 +0000491 LayoutUnit hyphenWidth;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000492 if (downcast<InlineTextBox>(*run->box()).hasHyphen())
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000493 hyphenWidth = measureHyphenWidth(renderer, font, &fallbackFonts);
antti@apple.comb0608f62013-09-28 18:30:16 +0000494
enrica@apple.com885c84d2012-10-10 05:48:51 +0000495 float measuredWidth = 0;
496
mmaxfield@apple.com2ef57ea2015-10-13 23:40:38 +0000497 bool kerningIsEnabled = font.enableKerning();
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000498 bool canUseSimpleFontCodePath = renderer.canUseSimpleFontCodePath();
enrica@apple.com885c84d2012-10-10 05:48:51 +0000499
500 // Since we don't cache glyph overflows, we need to re-measure the run if
501 // the style is linebox-contain: glyph.
leviw@chromium.orgd70f4cc2013-03-28 21:03:44 +0000502 if (!lineBox->fitsToGlyphs() && canUseSimpleFontCodePath) {
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +0000503 unsigned lastEndOffset = run->m_start;
zalan@apple.com9cc58512016-12-16 06:32:36 +0000504 bool atFirstWordMeasurement = true;
enrica@apple.com885c84d2012-10-10 05:48:51 +0000505 for (size_t i = 0, size = wordMeasurements.size(); i < size && lastEndOffset < run->m_stop; ++i) {
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000506 WordMeasurement& wordMeasurement = wordMeasurements[i];
507 if (wordMeasurement.width <= 0 || wordMeasurement.startOffset == wordMeasurement.endOffset)
enrica@apple.com885c84d2012-10-10 05:48:51 +0000508 continue;
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000509 if (wordMeasurement.renderer != &renderer || wordMeasurement.startOffset != lastEndOffset || wordMeasurement.endOffset > run->m_stop)
enrica@apple.com885c84d2012-10-10 05:48:51 +0000510 continue;
511
512 lastEndOffset = wordMeasurement.endOffset;
513 if (kerningIsEnabled && lastEndOffset == run->m_stop) {
dino@apple.com7c50e7c2013-03-18 18:01:05 +0000514 int wordLength = lastEndOffset - wordMeasurement.startOffset;
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000515 GlyphOverflow overflow;
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000516 measuredWidth += renderer.width(wordMeasurement.startOffset, wordLength, xPos + measuredWidth, lineInfo.isFirstLine(),
rniwa@webkit.orgfe811f22013-06-07 17:59:01 +0000517 &wordMeasurement.fallbackFonts, &overflow);
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000518 UChar c = renderer.characterAt(wordMeasurement.startOffset);
zalan@apple.com9cc58512016-12-16 06:32:36 +0000519 // 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 +0000520 if (!atFirstWordMeasurement && FontCascade::treatAsSpace(c))
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000521 measuredWidth += renderer.style().fontCascade().wordSpacing();
enrica@apple.com885c84d2012-10-10 05:48:51 +0000522 } else
523 measuredWidth += wordMeasurement.width;
zalan@apple.com9cc58512016-12-16 06:32:36 +0000524 atFirstWordMeasurement = false;
525
enrica@apple.com885c84d2012-10-10 05:48:51 +0000526 if (!wordMeasurement.fallbackFonts.isEmpty()) {
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +0000527 HashSet<const Font*>::const_iterator end = wordMeasurement.fallbackFonts.end();
528 for (HashSet<const Font*>::const_iterator it = wordMeasurement.fallbackFonts.begin(); it != end; ++it)
529 fallbackFonts.add(*it);
enrica@apple.com885c84d2012-10-10 05:48:51 +0000530 }
531 }
532 if (measuredWidth && lastEndOffset != run->m_stop) {
533 // If we don't have enough cached data, we'll measure the run again.
534 measuredWidth = 0;
535 fallbackFonts.clear();
536 }
537 }
enrica@apple.com885c84d2012-10-10 05:48:51 +0000538
539 if (!measuredWidth)
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000540 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 +0000541
akling@apple.com7506fda2013-11-07 10:12:36 +0000542 run->box()->setLogicalWidth(measuredWidth + hyphenWidth);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000543 if (!fallbackFonts.isEmpty()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000544 ASSERT(run->box()->behavesLikeText());
antti@apple.com5a8f7942015-01-22 21:57:04 +0000545 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 +0000546 ASSERT(it->value.first.isEmpty());
weinig@apple.comb6e19c52017-10-12 15:38:42 +0000547 it->value.first = copyToVector(fallbackFonts);
akling@apple.com7506fda2013-11-07 10:12:36 +0000548 run->box()->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000549 }
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +0000550
551 // Include text decoration visual overflow as part of the glyph overflow.
commit-queue@webkit.org02cb0002018-05-28 00:49:21 +0000552 if (!renderer.style().textDecorationsInEffect().isEmpty())
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000553 glyphOverflow.extendTo(visualOverflowForDecorations(run->box()->lineStyle(), downcast<InlineTextBox>(run->box())));
mmaxfield@apple.comf28245e2014-05-20 00:45:52 +0000554
555 if (!glyphOverflow.isEmpty()) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000556 ASSERT(run->box()->behavesLikeText());
antti@apple.com5a8f7942015-01-22 21:57:04 +0000557 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 +0000558 it->value.second = glyphOverflow;
akling@apple.com7506fda2013-11-07 10:12:36 +0000559 run->box()->clearKnownToHaveNoOverflow();
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000560 }
561}
562
antti@apple.com50b36fde2019-08-11 11:02:01 +0000563void ComplexLineLayout::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 +0000564{
565 if (!rubyRun.rubyBase() || !rubyRun.rubyBase()->firstRootBox() || rubyRun.rubyBase()->firstRootBox()->nextRootBox() || !r.renderer().style().collapseWhiteSpace())
566 return;
567
568 auto& rubyBase = *rubyRun.rubyBase();
569 auto& rootBox = *rubyBase.firstRootBox();
570
571 float totalExpansion = 0;
572 unsigned totalOpportunitiesInRun = 0;
antti@apple.com7ac7f602019-09-25 14:23:03 +0000573 for (auto* leafChild = rootBox.firstLeafDescendant(); leafChild; leafChild = leafChild->nextLeafOnLine()) {
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000574 if (!leafChild->isInlineTextBox())
575 continue;
576
577 unsigned opportunitiesInRun = expansionOpportunities[i++];
578 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
579 auto expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
580 totalExpansion += expansion;
581 totalOpportunitiesInRun += opportunitiesInRun;
582 }
583
rego@igalia.com8bac95d2018-05-14 15:20:47 +0000584 ASSERT(!rubyRun.hasOverrideContentLogicalWidth());
antti@apple.com50b36fde2019-08-11 11:02:01 +0000585 float newBaseWidth = rubyRun.logicalWidth() + totalExpansion + m_flow.marginStartForChild(rubyRun) + m_flow.marginEndForChild(rubyRun);
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000586 float newRubyRunWidth = rubyRun.logicalWidth() + totalExpansion;
587 rubyBase.setInitialOffset((newRubyRunWidth - newBaseWidth) / 2);
ross.kirsling@sony.com80414652019-05-21 01:36:11 +0000588 rubyRun.setOverrideContentLogicalWidth(LayoutUnit(newRubyRunWidth));
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000589 rubyRun.setNeedsLayout(MarkOnlyThis);
590 rootBox.markDirty();
591 if (RenderRubyText* rubyText = rubyRun.rubyText()) {
592 if (RootInlineBox* textRootBox = rubyText->firstRootBox())
593 textRootBox->markDirty();
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000594 }
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000595 rubyRun.layoutBlock(true);
rego@igalia.com8bac95d2018-05-14 15:20:47 +0000596 rubyRun.clearOverrideContentLogicalWidth();
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000597 r.box()->setExpansion(newRubyRunWidth - r.box()->logicalWidth());
598
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000599 totalLogicalWidth += totalExpansion;
600 expansionOpportunityCount -= totalOpportunitiesInRun;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000601}
602
antti@apple.com50b36fde2019-08-11 11:02:01 +0000603void ComplexLineLayout::computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, const Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float totalLogicalWidth, float availableLogicalWidth)
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000604{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000605 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
606 return;
607
608 size_t i = 0;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000609 for (BidiRun* run = firstRun; run; run = run->next()) {
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000610 if (!run->box() || run == trailingSpaceRun)
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000611 continue;
612
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000613 if (is<RenderText>(run->renderer())) {
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000614 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000615
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000616 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
617
618 // Only justify text if whitespace is collapsed.
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000619 if (run->renderer().style().collapseWhiteSpace()) {
620 InlineTextBox& textBox = downcast<InlineTextBox>(*run->box());
mmaxfield@apple.coma9f58d02014-10-02 22:25:50 +0000621 float expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000622 textBox.setExpansion(expansion);
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000623 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000624 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000625 expansionOpportunityCount -= opportunitiesInRun;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000626 } else if (is<RenderRubyRun>(run->renderer()))
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000627 updateRubyForJustifiedText(downcast<RenderRubyRun>(run->renderer()), *run, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth, i);
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000628
629 if (!expansionOpportunityCount)
630 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000631 }
632}
633
antti@apple.comc6a9c732019-08-12 15:31:34 +0000634void ComplexLineLayout::updateLogicalWidthForAlignment(RenderBlockFlow& flow, const TextAlignMode& textAlign, const RootInlineBox* rootInlineBox, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float& availableLogicalWidth, int expansionOpportunityCount)
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000635{
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000636 TextDirection direction;
antti@apple.comc6a9c732019-08-12 15:31:34 +0000637 if (rootInlineBox && flow.style().unicodeBidi() == Plaintext)
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000638 direction = rootInlineBox->direction();
639 else
antti@apple.comc6a9c732019-08-12 15:31:34 +0000640 direction = flow.style().direction();
641
642 bool isLeftToRightDirection = flow.style().isLeftToRightDirection();
mario.prada@samsung.com79397522014-02-28 18:12:52 +0000643
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000644 // Armed with the total width of the line (without justification),
645 // we now examine our text-align property in order to determine where to position the
646 // objects horizontally. The total width of the line can be increased if we end up
647 // justifying text.
648 switch (textAlign) {
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000649 case TextAlignMode::Left:
650 case TextAlignMode::WebKitLeft:
antti@apple.comc6a9c732019-08-12 15:31:34 +0000651 updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000652 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000653 case TextAlignMode::Right:
654 case TextAlignMode::WebKitRight:
antti@apple.comc6a9c732019-08-12 15:31:34 +0000655 updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000656 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000657 case TextAlignMode::Center:
658 case TextAlignMode::WebKitCenter:
antti@apple.comc6a9c732019-08-12 15:31:34 +0000659 updateLogicalWidthForCenterAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
rniwa@webkit.orgaf7f7a42012-06-15 21:54:44 +0000660 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000661 case TextAlignMode::Justify:
antti@apple.comc6a9c732019-08-12 15:31:34 +0000662 flow.adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000663 if (expansionOpportunityCount) {
664 if (trailingSpaceRun) {
akling@apple.com7506fda2013-11-07 10:12:36 +0000665 totalLogicalWidth -= trailingSpaceRun->box()->logicalWidth();
666 trailingSpaceRun->box()->setLogicalWidth(0);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000667 }
668 break;
669 }
joepeck@webkit.orgaa676ee52014-01-28 04:04:52 +0000670 FALLTHROUGH;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000671 case TextAlignMode::Start:
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +0000672 if (direction == TextDirection::LTR)
antti@apple.comc6a9c732019-08-12 15:31:34 +0000673 updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000674 else
antti@apple.comc6a9c732019-08-12 15:31:34 +0000675 updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000676 break;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000677 case TextAlignMode::End:
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +0000678 if (direction == TextDirection::LTR)
antti@apple.comc6a9c732019-08-12 15:31:34 +0000679 updateLogicalWidthForRightAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000680 else
antti@apple.comc6a9c732019-08-12 15:31:34 +0000681 updateLogicalWidthForLeftAlignedBlock(isLeftToRightDirection, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
robert@webkit.orgfc7763c2011-09-03 18:28:57 +0000682 break;
683 }
684}
685
zalan@apple.com64761fe2016-03-02 21:42:22 +0000686static void updateLogicalInlinePositions(RenderBlockFlow& block, float& lineLogicalLeft, float& lineLogicalRight, float& availableLogicalWidth, bool firstLine,
antti@apple.comae85e112017-08-31 23:27:02 +0000687 IndentTextOrNot shouldIndentText, LayoutUnit boxLogicalHeight)
commit-queue@webkit.orgab781b02013-04-03 01:32:24 +0000688{
weinig@apple.come9621c32014-01-04 20:53:51 +0000689 LayoutUnit lineLogicalHeight = block.minLineHeightForReplacedRenderer(firstLine, boxLogicalHeight);
antti@apple.comae85e112017-08-31 23:27:02 +0000690 lineLogicalLeft = block.logicalLeftOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
691 lineLogicalRight = block.logicalRightOffsetForLine(block.logicalHeight(), shouldIndentText, lineLogicalHeight);
robert@webkit.org0903cf52012-12-11 18:14:16 +0000692 availableLogicalWidth = lineLogicalRight - lineLogicalLeft;
693}
694
antti@apple.com50b36fde2019-08-11 11:02:01 +0000695void ComplexLineLayout::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache, WordMeasurements& wordMeasurements)
hyattffe78712003-02-11 01:59:29 +0000696{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000697 TextAlignMode textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
robert@webkit.org4f76df92012-07-03 17:41:35 +0000698
robert@webkit.org328ecd02012-08-09 21:12:44 +0000699 // 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
700 // box is only affected if it is the first child of its parent element."
commit-queue@webkit.orgab781b02013-04-03 01:32:24 +0000701 // CSS3 "text-indent", "-webkit-each-line" affects the first line of the block container as well as each line after a forced line break,
702 // but does not affect lines after a soft wrap break.
antti@apple.com50b36fde2019-08-11 11:02:01 +0000703 bool isFirstLine = lineInfo.isFirstLine() && !(m_flow.isAnonymousBlock() && m_flow.parent()->firstChild() != &m_flow);
commit-queue@webkit.orgab781b02013-04-03 01:32:24 +0000704 bool isAfterHardLineBreak = lineBox->prevRootBox() && lineBox->prevRootBox()->endsWithBreak();
akling@apple.com827be9c2013-10-29 02:58:43 +0000705 IndentTextOrNot shouldIndentText = requiresIndent(isFirstLine, isAfterHardLineBreak, style());
robert@webkit.org0903cf52012-12-11 18:14:16 +0000706 float lineLogicalLeft;
707 float lineLogicalRight;
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000708 float availableLogicalWidth;
antti@apple.com50b36fde2019-08-11 11:02:01 +0000709 updateLogicalInlinePositions(m_flow, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, 0);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000710 bool needsWordSpacing;
robert@webkit.org0903cf52012-12-11 18:14:16 +0000711
akling@apple.com7506fda2013-11-07 10:12:36 +0000712 if (firstRun && firstRun->renderer().isReplaced()) {
cdumez@apple.com0abff8b2014-10-17 21:25:10 +0000713 RenderBox& renderBox = downcast<RenderBox>(firstRun->renderer());
antti@apple.com50b36fde2019-08-11 11:02:01 +0000714 updateLogicalInlinePositions(m_flow, lineLogicalLeft, lineLogicalRight, availableLogicalWidth, isFirstLine, shouldIndentText, renderBox.logicalHeight());
robert@webkit.org0903cf52012-12-11 18:14:16 +0000715 }
716
717 computeInlineDirectionPositionsForSegment(lineBox, lineInfo, textAlign, lineLogicalLeft, availableLogicalWidth, firstRun, trailingSpaceRun, textBoxDataMap, verticalPositionCache, wordMeasurements);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000718 // The widths of all runs are now known. We can now place every inline box (and
719 // compute accurate widths for the inline flow boxes).
720 needsWordSpacing = false;
mmaxfield@apple.comb0a4a3f2014-04-30 00:36:21 +0000721 lineBox->placeBoxesInInlineDirection(lineLogicalLeft, needsWordSpacing);
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000722}
mitz@apple.com390fa322011-02-24 23:07:06 +0000723
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000724static inline ExpansionBehavior expansionBehaviorForInlineTextBox(RenderBlockFlow& block, InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, TextAlignMode textAlign, bool isAfterExpansion)
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000725{
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000726 // 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 +0000727 if (textBox.renderer().style().textCombine() == TextCombine::Horizontal)
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000728 return ForbidLeadingExpansion | ForbidTrailingExpansion;
729
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000730 ExpansionBehavior result = 0;
731 bool setLeadingExpansion = false;
732 bool setTrailingExpansion = false;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000733 if (textAlign == TextAlignMode::Justify) {
mmaxfield@apple.com5a572b12015-11-06 23:20:12 +0000734 // 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 +0000735 if (nextRun && is<RenderRubyRun>(nextRun->renderer()) && downcast<RenderRubyRun>(nextRun->renderer()).rubyBase() && nextRun->renderer().style().collapseWhiteSpace()) {
736 auto& rubyBase = *downcast<RenderRubyRun>(nextRun->renderer()).rubyBase();
737 if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
antti@apple.com7ac7f602019-09-25 14:23:03 +0000738 if (auto* leafChild = rubyBase.firstRootBox()->firstLeafDescendant()) {
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000739 if (is<InlineTextBox>(*leafChild)) {
740 // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
741 if (FontCascade::leadingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
742 setTrailingExpansion = true;
743 result |= ForceTrailingExpansion;
744 }
745 }
746 }
747 }
748 }
749 // Same thing, except if we're following a ruby
750 if (previousRun && is<RenderRubyRun>(previousRun->renderer()) && downcast<RenderRubyRun>(previousRun->renderer()).rubyBase() && previousRun->renderer().style().collapseWhiteSpace()) {
751 auto& rubyBase = *downcast<RenderRubyRun>(previousRun->renderer()).rubyBase();
752 if (rubyBase.firstRootBox() && !rubyBase.firstRootBox()->nextRootBox()) {
antti@apple.com7ac7f602019-09-25 14:23:03 +0000753 if (auto* leafChild = rubyBase.firstRootBox()->lastLeafDescendant()) {
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000754 if (is<InlineTextBox>(*leafChild)) {
755 // FIXME: This leadingExpansionOpportunity doesn't actually work because it doesn't perform the UBA
756 if (FontCascade::trailingExpansionOpportunity(downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction())) {
757 setLeadingExpansion = true;
758 result |= ForceLeadingExpansion;
759 }
760 }
761 }
762 }
763 }
764 // If we're the first box inside a ruby base, forbid a leading expansion, and vice-versa
765 if (is<RenderRubyBase>(block)) {
766 RenderRubyBase& rubyBase = downcast<RenderRubyBase>(block);
antti@apple.com7ac7f602019-09-25 14:23:03 +0000767 if (&textBox == rubyBase.firstRootBox()->firstLeafDescendant()) {
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000768 setLeadingExpansion = true;
769 result |= ForbidLeadingExpansion;
antti@apple.com7ac7f602019-09-25 14:23:03 +0000770 } if (&textBox == rubyBase.firstRootBox()->lastLeafDescendant()) {
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000771 setTrailingExpansion = true;
772 result |= ForbidTrailingExpansion;
773 }
774 }
775 }
776 if (!setLeadingExpansion)
777 result |= isAfterExpansion ? ForbidLeadingExpansion : AllowLeadingExpansion;
778 if (!setTrailingExpansion)
779 result |= AllowTrailingExpansion;
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000780 return result;
781}
782
783static inline void applyExpansionBehavior(InlineTextBox& textBox, ExpansionBehavior expansionBehavior)
784{
785 switch (expansionBehavior & LeadingExpansionMask) {
786 case ForceLeadingExpansion:
787 textBox.setForceLeadingExpansion();
788 break;
789 case ForbidLeadingExpansion:
790 textBox.setCanHaveLeadingExpansion(false);
791 break;
792 case AllowLeadingExpansion:
793 textBox.setCanHaveLeadingExpansion(true);
794 break;
795 default:
796 ASSERT_NOT_REACHED();
797 break;
798 }
799 switch (expansionBehavior & TrailingExpansionMask) {
800 case ForceTrailingExpansion:
801 textBox.setForceTrailingExpansion();
802 break;
803 case ForbidTrailingExpansion:
804 textBox.setCanHaveTrailingExpansion(false);
805 break;
806 case AllowTrailingExpansion:
807 textBox.setCanHaveTrailingExpansion(true);
808 break;
809 default:
810 ASSERT_NOT_REACHED();
811 break;
812 }
813}
814
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000815static bool inlineAncestorHasStartBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
816{
817 bool isLTR = block.style().isLeftToRightDirection();
818 for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
819 if ((isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0)
820 || (!isLTR && currentBox->marginBorderPaddingLogicalRight() > 0))
821 return true;
822 }
823 return false;
824}
825
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000826static bool inlineAncestorHasEndBorderPaddingOrMargin(const RenderBlockFlow& block, const InlineBox& box)
827{
828 bool isLTR = block.style().isLeftToRightDirection();
829 for (auto* currentBox = box.parent(); currentBox; currentBox = currentBox->parent()) {
830 if ((isLTR && currentBox->marginBorderPaddingLogicalRight() > 0)
831 || (!isLTR && currentBox->marginBorderPaddingLogicalLeft() > 0))
832 return true;
833 }
834 return false;
835}
836
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000837static bool isLastInFlowRun(BidiRun& runToCheck)
838{
839 for (auto* run = runToCheck.next(); run; run = run->next()) {
840 if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak())
841 continue;
842 return false;
843 }
844 return true;
845}
846
antti@apple.com50b36fde2019-08-11 11:02:01 +0000847BidiRun* ComplexLineLayout::computeInlineDirectionPositionsForSegment(RootInlineBox* lineBox, const LineInfo& lineInfo, TextAlignMode textAlign, float& logicalLeft,
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +0000848 float& availableLogicalWidth, BidiRun* firstRun, BidiRun* trailingSpaceRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache,
849 WordMeasurements& wordMeasurements)
850{
darin06dcb9c2005-08-15 04:31:09 +0000851 bool needsWordSpacing = false;
commit-queue@webkit.org02cb0002018-05-28 00:49:21 +0000852 bool canHangPunctuationAtStart = style().hangingPunctuation().contains(HangingPunctuation::First);
853 bool canHangPunctuationAtEnd = style().hangingPunctuation().contains(HangingPunctuation::Last);
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000854 bool isLTR = style().isLeftToRightDirection();
mitz@apple.com390fa322011-02-24 23:07:06 +0000855 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000856 unsigned expansionOpportunityCount = 0;
antti@apple.com50b36fde2019-08-11 11:02:01 +0000857 bool isAfterExpansion = is<RenderRubyBase>(m_flow) ? downcast<RenderRubyBase>(m_flow).isAfterExpansion() : true;
mitz@apple.com86470c82011-01-27 01:39:27 +0000858 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000859
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000860 BidiRun* run = firstRun;
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000861 BidiRun* previousRun = nullptr;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000862 for (; run; run = run->next()) {
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000863 auto computeExpansionOpportunities = [&expansionOpportunities, &expansionOpportunityCount, textAlign, &isAfterExpansion] (RenderBlockFlow& block,
864 InlineTextBox& textBox, BidiRun* previousRun, BidiRun* nextRun, const StringView& stringView, TextDirection direction)
865 {
866 if (stringView.isEmpty()) {
867 // Empty runs should still produce an entry in expansionOpportunities list so that the number of items matches the number of runs.
868 expansionOpportunities.append(0);
869 return;
870 }
871 ExpansionBehavior expansionBehavior = expansionBehaviorForInlineTextBox(block, textBox, previousRun, nextRun, textAlign, isAfterExpansion);
872 applyExpansionBehavior(textBox, expansionBehavior);
873 unsigned opportunitiesInRun;
874 std::tie(opportunitiesInRun, isAfterExpansion) = FontCascade::expansionOpportunityCount(stringView, direction, expansionBehavior);
875 expansionOpportunities.append(opportunitiesInRun);
876 expansionOpportunityCount += opportunitiesInRun;
877 };
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000878 if (!run->box() || run->renderer().isOutOfFlowPositioned() || run->box()->isLineBreak()) {
antti@apple.com50b36fde2019-08-11 11:02:01 +0000879 // Positioned objects are only participating to figure out their correct static x position.
880 // They have no effect on the width. Similarly, line break boxes have no effect on the width.
881 continue;
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000882 }
883 if (is<RenderText>(run->renderer())) {
884 auto& renderText = downcast<RenderText>(run->renderer());
mmaxfield@apple.com43761a72015-04-01 16:58:20 +0000885 auto& textBox = downcast<InlineTextBox>(*run->box());
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000886 if (canHangPunctuationAtStart && lineInfo.isFirstLine() && (isLTR || isLastInFlowRun(*run))
antti@apple.com50b36fde2019-08-11 11:02:01 +0000887 && !inlineAncestorHasStartBorderPaddingOrMargin(m_flow, *run->box())) {
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000888 float hangStartWidth = renderText.hangablePunctuationStartWidth(run->m_start);
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000889 availableLogicalWidth += hangStartWidth;
890 if (style().isLeftToRightDirection())
891 logicalLeft -= hangStartWidth;
892 canHangPunctuationAtStart = false;
893 }
894
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000895 if (canHangPunctuationAtEnd && lineInfo.isLastLine() && run->m_stop > 0 && (!isLTR || isLastInFlowRun(*run))
antti@apple.com50b36fde2019-08-11 11:02:01 +0000896 && !inlineAncestorHasEndBorderPaddingOrMargin(m_flow, *run->box())) {
hyatt@apple.com6b422a72016-03-03 21:49:32 +0000897 float hangEndWidth = renderText.hangablePunctuationEndWidth(run->m_stop - 1);
898 availableLogicalWidth += hangEndWidth;
899 if (!style().isLeftToRightDirection())
900 logicalLeft -= hangEndWidth;
901 canHangPunctuationAtEnd = false;
902 }
903
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000904 if (textAlign == TextAlignMode::Justify && run != trailingSpaceRun)
antti@apple.com50b36fde2019-08-11 11:02:01 +0000905 computeExpansionOpportunities(m_flow, textBox, previousRun, run->next(), renderText.stringView(run->m_start, run->m_stop), run->box()->direction());
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000906
darin@apple.com5917cb12017-11-23 17:32:42 +0000907 if (unsigned length = renderText.text().length()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000908 if (!run->m_start && needsWordSpacing && isSpaceOrNewline(renderText.characterAt(run->m_start)))
antti@apple.comc54cbc92015-01-15 14:19:56 +0000909 totalLogicalWidth += lineStyle(*renderText.parent(), lineInfo).fontCascade().wordSpacing();
mmaxfield@apple.comfde7b6a2017-01-11 23:06:13 +0000910 // run->m_start == run->m_stop should only be true iff the run is a replaced run for bidi: isolate.
911 ASSERT(run->m_stop > 0 || run->m_start == run->m_stop);
912 needsWordSpacing = run->m_stop == length && !isSpaceOrNewline(renderText.characterAt(run->m_stop - 1));
darin06dcb9c2005-08-15 04:31:09 +0000913 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000914
mmaxfield@apple.com16fc17b2015-03-05 19:51:05 +0000915 setLogicalWidthForTextRun(lineBox, run, renderText, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache, wordMeasurements);
mitz@apple.com86470c82011-01-27 01:39:27 +0000916 } else {
hyatt@apple.com41c12c92016-03-02 22:29:26 +0000917 canHangPunctuationAtStart = false;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000918 bool encounteredJustifiedRuby = false;
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000919 if (is<RenderRubyRun>(run->renderer()) && textAlign == TextAlignMode::Justify && run != trailingSpaceRun && downcast<RenderRubyRun>(run->renderer()).rubyBase()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000920 auto* rubyBase = downcast<RenderRubyRun>(run->renderer()).rubyBase();
921 if (rubyBase->firstRootBox() && !rubyBase->firstRootBox()->nextRootBox() && run->renderer().style().collapseWhiteSpace()) {
commit-queue@webkit.org01ad5842014-12-16 19:28:57 +0000922 rubyBase->setIsAfterExpansion(isAfterExpansion);
antti@apple.com7ac7f602019-09-25 14:23:03 +0000923 for (auto* leafChild = rubyBase->firstRootBox()->firstLeafDescendant(); leafChild; leafChild = leafChild->nextLeafOnLine()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000924 if (!is<InlineTextBox>(*leafChild))
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000925 continue;
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000926 encounteredJustifiedRuby = true;
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000927 computeExpansionOpportunities(*rubyBase, downcast<InlineTextBox>(*leafChild), nullptr, nullptr,
928 downcast<RenderText>(leafChild->renderer()).stringView(), leafChild->direction());
mmaxfield@apple.com8301e832014-10-09 01:14:30 +0000929 }
930 }
931 }
932
933 if (!encounteredJustifiedRuby)
934 isAfterExpansion = false;
935
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000936 if (!is<RenderInline>(run->renderer())) {
937 auto& renderBox = downcast<RenderBox>(run->renderer());
938 if (is<RenderRubyRun>(renderBox))
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000939 setMarginsForRubyRun(run, downcast<RenderRubyRun>(renderBox), previousRun ? &previousRun->renderer() : nullptr, lineInfo);
antti@apple.com50b36fde2019-08-11 11:02:01 +0000940 run->box()->setLogicalWidth(m_flow.logicalWidthForChild(renderBox));
941 totalLogicalWidth += m_flow.marginStartForChild(renderBox) + m_flow.marginEndForChild(renderBox);
mitz@apple.com86470c82011-01-27 01:39:27 +0000942 }
hyattffe78712003-02-11 01:59:29 +0000943 }
hyatt4b381692003-03-10 21:11:59 +0000944
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000945 totalLogicalWidth += run->box()->logicalWidth();
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000946 previousRun = run;
hyattffe78712003-02-11 01:59:29 +0000947 }
948
mitz@apple.com86470c82011-01-27 01:39:27 +0000949 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
zalan@apple.coma45efeb2016-08-30 17:57:43 +0000950 // FIXME: see <webkit.org/b/139393#c11>
951 int lastValidExpansionOpportunitiesIndex = expansionOpportunities.size() - 1;
952 while (lastValidExpansionOpportunitiesIndex >= 0 && !expansionOpportunities.at(lastValidExpansionOpportunitiesIndex))
953 --lastValidExpansionOpportunitiesIndex;
954 if (lastValidExpansionOpportunitiesIndex >= 0) {
955 ASSERT(expansionOpportunities.at(lastValidExpansionOpportunitiesIndex));
956 expansionOpportunities.at(lastValidExpansionOpportunitiesIndex)--;
957 expansionOpportunityCount--;
958 }
mitz@apple.com86470c82011-01-27 01:39:27 +0000959 }
960
antti@apple.com50b36fde2019-08-11 11:02:01 +0000961 if (is<RenderRubyBase>(m_flow) && !expansionOpportunityCount)
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +0000962 textAlign = TextAlignMode::Center;
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +0000963
antti@apple.comc6a9c732019-08-12 15:31:34 +0000964 updateLogicalWidthForAlignment(m_flow, textAlign, lineBox, trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth, expansionOpportunityCount);
hyattffe78712003-02-11 01:59:29 +0000965
mmaxfield@apple.comb2c6d862015-02-18 19:39:17 +0000966 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000967
cdumez@apple.com57d544c2014-10-16 00:05:37 +0000968 return run;
hyattffe78712003-02-11 01:59:29 +0000969}
970
antti@apple.com50b36fde2019-08-11 11:02:01 +0000971void ComplexLineLayout::removeInlineBox(BidiRun& run, const RootInlineBox& rootLineBox) const
zalan@apple.com2d2b32d2016-04-29 01:11:17 +0000972{
973 auto* inlineBox = run.box();
974#if !ASSERT_DISABLED
975 auto* inlineParent = inlineBox->parent();
976 while (inlineParent && inlineParent != &rootLineBox) {
977 ASSERT(!inlineParent->isDirty());
978 inlineParent = inlineParent->parent();
979 }
980 ASSERT(!rootLineBox.isDirty());
981#endif
982 auto* parent = inlineBox->parent();
983 inlineBox->removeFromParent();
984
985 auto& renderer = run.renderer();
986 if (is<RenderText>(renderer))
987 downcast<RenderText>(renderer).removeTextBox(downcast<InlineTextBox>(*inlineBox));
988 delete inlineBox;
989 run.setBox(nullptr);
990 // removeFromParent() unnecessarily dirties the ancestor subtree.
991 auto* ancestor = parent;
992 while (ancestor) {
993 ancestor->markDirty(false);
994 if (ancestor == &rootLineBox)
995 break;
996 ancestor = ancestor->parent();
997 }
998}
999
antti@apple.com50b36fde2019-08-11 11:02:01 +00001000void ComplexLineLayout::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +00001001{
antti@apple.com50b36fde2019-08-11 11:02:01 +00001002 m_flow.setLogicalHeight(lineBox->alignBoxesInBlockDirection(m_flow.logicalHeight(), textBoxDataMap, verticalPositionCache));
hyattffe78712003-02-11 01:59:29 +00001003
1004 // Now make sure we place replaced render objects correctly.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +00001005 for (auto* run = firstRun; run; run = run->next()) {
cdumez@apple.com57d544c2014-10-16 00:05:37 +00001006 ASSERT(run->box());
1007 if (!run->box())
eseidel789896f2005-11-27 22:52:09 +00001008 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +00001009
antti@apple.com50b36fde2019-08-11 11:02:01 +00001010 // Align positioned boxes with the top of the line box. This is
hyatt98ee7e42003-05-14 01:39:15 +00001011 // a reasonable approximation of an appropriate y position.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +00001012 auto& renderer = run->renderer();
1013 if (renderer.isOutOfFlowPositioned())
antti@apple.com50b36fde2019-08-11 11:02:01 +00001014 run->box()->setLogicalTop(m_flow.logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00001015
1016 // Position is used to properly position both replaced elements and
1017 // to update the static normal flow x/y of positioned elements.
zalan@apple.com2d2b32d2016-04-29 01:11:17 +00001018 bool inlineBoxIsRedundant = false;
1019 if (is<RenderText>(renderer)) {
1020 auto& inlineTextBox = downcast<InlineTextBox>(*run->box());
1021 downcast<RenderText>(renderer).positionLineBox(inlineTextBox);
dbates@webkit.org1dc77ff2018-09-04 17:02:33 +00001022 inlineBoxIsRedundant = !inlineTextBox.hasTextContent();
zalan@apple.com2d2b32d2016-04-29 01:11:17 +00001023 } else if (is<RenderBox>(renderer)) {
1024 downcast<RenderBox>(renderer).positionLineBox(downcast<InlineElementBox>(*run->box()));
1025 inlineBoxIsRedundant = renderer.isOutOfFlowPositioned();
1026 } else if (is<RenderLineBreak>(renderer))
1027 downcast<RenderLineBreak>(renderer).replaceInlineBoxWrapper(downcast<InlineElementBox>(*run->box()));
1028 // Check if we need to keep this box on the line at all.
1029 if (inlineBoxIsRedundant)
1030 removeInlineBox(*run, *lineBox);
hyatt98ee7e42003-05-14 01:39:15 +00001031 }
hyattffe78712003-02-11 01:59:29 +00001032}
kociendabb0c24b2001-08-24 14:24:40 +00001033
akling@apple.com7506fda2013-11-07 10:12:36 +00001034static inline bool isCollapsibleSpace(UChar character, const RenderText& renderer)
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001035{
1036 if (character == ' ' || character == '\t' || character == softHyphen)
1037 return true;
1038 if (character == '\n')
akling@apple.com7506fda2013-11-07 10:12:36 +00001039 return !renderer.style().preserveNewline();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001040 if (character == noBreakSpace)
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +00001041 return renderer.style().nbspMode() == NBSPMode::Space;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001042 return false;
1043}
1044
msaboff@apple.com142fc202012-10-18 18:03:14 +00001045template <typename CharacterType>
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001046static inline unsigned findFirstTrailingSpace(const RenderText& lastText, const CharacterType* characters, unsigned start, unsigned stop)
msaboff@apple.com142fc202012-10-18 18:03:14 +00001047{
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001048 unsigned firstSpace = stop;
msaboff@apple.com142fc202012-10-18 18:03:14 +00001049 while (firstSpace > start) {
1050 UChar current = characters[firstSpace - 1];
1051 if (!isCollapsibleSpace(current, lastText))
1052 break;
1053 firstSpace--;
1054 }
1055
1056 return firstSpace;
1057}
1058
antti@apple.com50b36fde2019-08-11 11:02:01 +00001059inline BidiRun* ComplexLineLayout::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +00001060{
eric@webkit.org5bee2942011-04-08 02:12:31 +00001061 if (!bidiRuns.runCount()
akling@apple.com7506fda2013-11-07 10:12:36 +00001062 || !bidiRuns.logicallyLastRun()->renderer().style().breakOnlyAfterWhiteSpace()
1063 || !bidiRuns.logicallyLastRun()->renderer().style().autoWrap())
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001064 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001065
eric@webkit.org5bee2942011-04-08 02:12:31 +00001066 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
akling@apple.com7506fda2013-11-07 10:12:36 +00001067 const RenderObject& lastObject = trailingSpaceRun->renderer();
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001068 if (!is<RenderText>(lastObject))
1069 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001070
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001071 const RenderText& lastText = downcast<RenderText>(lastObject);
mmaxfield@apple.coma030c5a2016-08-19 21:47:43 +00001072 unsigned firstSpace;
darin@apple.com5917cb12017-11-23 17:32:42 +00001073 if (lastText.text().is8Bit())
1074 firstSpace = findFirstTrailingSpace(lastText, lastText.text().characters8(), trailingSpaceRun->start(), trailingSpaceRun->stop());
msaboff@apple.com142fc202012-10-18 18:03:14 +00001075 else
darin@apple.com5917cb12017-11-23 17:32:42 +00001076 firstSpace = findFirstTrailingSpace(lastText, lastText.text().characters16(), trailingSpaceRun->start(), trailingSpaceRun->stop());
msaboff@apple.com142fc202012-10-18 18:03:14 +00001077
eric@webkit.org0894bb82011-04-03 08:29:40 +00001078 if (firstSpace == trailingSpaceRun->stop())
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001079 return nullptr;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001080
akling@apple.com827be9c2013-10-29 02:58:43 +00001081 TextDirection direction = style().direction();
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +00001082 bool shouldReorder = trailingSpaceRun != (direction == TextDirection::LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +00001083 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001084 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +00001085 while (BidiContext* parent = baseContext->parent())
1086 baseContext = parent;
1087
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +00001088 std::unique_ptr<BidiRun> newTrailingRun = makeUnique<BidiRun>(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->renderer(), baseContext, U_OTHER_NEUTRAL);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001089 trailingSpaceRun->m_stop = firstSpace;
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001090 trailingSpaceRun = newTrailingRun.get();
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +00001091 if (direction == TextDirection::LTR)
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001092 bidiRuns.appendRun(WTFMove(newTrailingRun));
eric@webkit.org0894bb82011-04-03 08:29:40 +00001093 else
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001094 bidiRuns.prependRun(WTFMove(newTrailingRun));
eric@webkit.org0894bb82011-04-03 08:29:40 +00001095 return trailingSpaceRun;
1096 }
1097 if (!shouldReorder)
1098 return trailingSpaceRun;
1099
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +00001100 if (direction == TextDirection::LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001101 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001102 trailingSpaceRun->m_level = 0;
1103 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +00001104 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +00001105 trailingSpaceRun->m_level = 1;
1106 }
1107 return trailingSpaceRun;
1108}
1109
antti@apple.com50b36fde2019-08-11 11:02:01 +00001110void ComplexLineLayout::appendFloatingObjectToLastLine(FloatingObject& floatingObject)
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001111{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001112 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject.originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001113 ASSERT(lastRootBox());
1114 floatingObject.setOriginatingLine(*lastRootBox());
zalan@apple.com4ed97602016-10-12 16:45:55 +00001115 lastRootBox()->appendFloat(floatingObject.renderer());
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001116}
1117
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001118static inline void notifyResolverToResumeInIsolate(InlineBidiResolver& resolver, RenderObject* root, RenderObject* startObject)
mmaxfield@apple.com18782a22014-01-28 21:53:22 +00001119{
1120 if (root != startObject) {
1121 RenderObject* parent = startObject->parent();
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001122 notifyResolverToResumeInIsolate(resolver, root, parent);
mmaxfield@apple.com18782a22014-01-28 21:53:22 +00001123 notifyObserverEnteredObject(&resolver, startObject);
1124 }
1125}
1126
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001127static inline void setUpResolverToResumeInIsolate(InlineBidiResolver& resolver, InlineBidiResolver& topResolver, BidiRun& isolatedRun, RenderObject* root, RenderObject* startObject)
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001128{
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001129 // Set up m_whitespaceCollapsingState
1130 resolver.whitespaceCollapsingState() = topResolver.whitespaceCollapsingState();
1131 resolver.whitespaceCollapsingState().setCurrentTransition(topResolver.whitespaceCollapsingTransitionForIsolatedRun(isolatedRun));
mmaxfield@apple.comff5b7ac2015-07-10 20:39:16 +00001132
1133 // Set up m_nestedIsolateCount
1134 notifyResolverToResumeInIsolate(resolver, root, startObject);
1135}
1136
eric@webkit.orga26de042011-09-08 18:46:01 +00001137// FIXME: BidiResolver should have this logic.
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001138static inline void constructBidiRunsForSegment(InlineBidiResolver& topResolver, BidiRunList<BidiRun>& bidiRuns, const InlineIterator& endOfRuns, VisualDirectionOverride override, bool previousLineBrokeCleanly)
eric@webkit.orga26de042011-09-08 18:46:01 +00001139{
1140 // FIXME: We should pass a BidiRunList into createBidiRunsForLine instead
1141 // of the resolver owning the runs.
1142 ASSERT(&topResolver.runs() == &bidiRuns);
betravis@adobe.com9e5e8242013-04-19 23:28:26 +00001143 ASSERT(topResolver.position() != endOfRuns);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001144 RenderObject* currentRoot = topResolver.position().root();
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001145 topResolver.createBidiRunsForLine(endOfRuns, override, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001146
1147 while (!topResolver.isolatedRuns().isEmpty()) {
1148 // 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 +00001149 auto isolatedRun = WTFMove(topResolver.isolatedRuns().last());
eric@webkit.orga26de042011-09-08 18:46:01 +00001150 topResolver.isolatedRuns().removeLast();
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001151 currentRoot = &isolatedRun.root;
eric@webkit.orga26de042011-09-08 18:46:01 +00001152
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001153 RenderObject& startObject = isolatedRun.object;
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001154
eric@webkit.orga26de042011-09-08 18:46:01 +00001155 // Only inlines make sense with unicode-bidi: isolate (blocks are already isolated).
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001156 // FIXME: Because enterIsolate is not passed a RenderObject, we have to crawl up the
1157 // tree to see which parent inline is the isolate. We could change enterIsolate
1158 // to take a RenderObject and do this logic there, but that would be a layering
1159 // violation for BidiResolver (which knows nothing about RenderObject).
cdumez@apple.comf8022152014-10-15 00:29:51 +00001160 RenderInline* isolatedInline = downcast<RenderInline>(highestContainingIsolateWithinRoot(startObject, currentRoot));
ddkilzer@apple.comad6ad362014-04-02 17:21:09 +00001161 ASSERT(isolatedInline);
1162
eric@webkit.orga26de042011-09-08 18:46:01 +00001163 InlineBidiResolver isolatedResolver;
akling@apple.com827be9c2013-10-29 02:58:43 +00001164 EUnicodeBidi unicodeBidi = isolatedInline->style().unicodeBidi();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001165 TextDirection direction;
1166 if (unicodeBidi == Plaintext)
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001167 determineDirectionality(direction, InlineIterator(isolatedInline, &isolatedRun.object, 0));
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001168 else {
rniwa@webkit.org4d4bd332012-08-20 21:34:11 +00001169 ASSERT(unicodeBidi == Isolate || unicodeBidi == IsolateOverride);
akling@apple.com827be9c2013-10-29 02:58:43 +00001170 direction = isolatedInline->style().direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001171 }
zoltan@webkit.orgd1a97402013-12-08 05:53:33 +00001172 isolatedResolver.setStatus(BidiStatus(direction, isOverride(unicodeBidi)));
eric@webkit.orga26de042011-09-08 18:46:01 +00001173
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001174 setUpResolverToResumeInIsolate(isolatedResolver, topResolver, isolatedRun.runToReplace, isolatedInline, &startObject);
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001175
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001176 // The starting position is the beginning of the first run within the isolate that was identified
1177 // during the earlier call to createBidiRunsForLine. This can be but is not necessarily the
1178 // first run within the isolate.
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001179 InlineIterator iter = InlineIterator(isolatedInline, &startObject, isolatedRun.position);
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001180 isolatedResolver.setPositionIgnoringNestedIsolates(iter);
eric@webkit.orga26de042011-09-08 18:46:01 +00001181
commit-queue@webkit.org30cc2912011-12-15 04:19:24 +00001182 // 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 +00001183 // FIXME: What should end and previousLineBrokeCleanly be?
1184 // rniwa says previousLineBrokeCleanly is just a WinIE hack and could always be false here?
commit-queue@webkit.orgccad9242012-12-05 20:17:30 +00001185 isolatedResolver.createBidiRunsForLine(endOfRuns, NoVisualOverride, previousLineBrokeCleanly);
eric@webkit.orga26de042011-09-08 18:46:01 +00001186 // Note that we do not delete the runs from the resolver.
rniwa@webkit.orgd0ad8882012-05-23 07:37:07 +00001187 // 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 +00001188 // itself to be turned into an InlineBox. We can't remove it here without potentially losing track of
1189 // the logically last run.
1190 if (isolatedResolver.runs().runCount())
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001191 bidiRuns.replaceRunWithRuns(&isolatedRun.runToReplace, isolatedResolver.runs());
eric@webkit.orga26de042011-09-08 18:46:01 +00001192
1193 // If we encountered any nested isolate runs, just move them
1194 // to the top resolver's list for later processing.
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001195 while (!isolatedResolver.isolatedRuns().isEmpty()) {
aestes@apple.com13aae082016-01-02 08:03:08 +00001196 auto runWithContext = WTFMove(isolatedResolver.isolatedRuns().last());
mmaxfield@apple.com0fba19a2015-09-16 00:30:38 +00001197 isolatedResolver.isolatedRuns().removeLast();
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001198 topResolver.setWhitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace, isolatedResolver.whitespaceCollapsingTransitionForIsolatedRun(runWithContext.runToReplace));
aestes@apple.com13aae082016-01-02 08:03:08 +00001199 topResolver.isolatedRuns().append(WTFMove(runWithContext));
eric@webkit.orga26de042011-09-08 18:46:01 +00001200 }
1201 }
1202}
1203
eric@webkit.org45e33a52011-05-04 11:51:09 +00001204// This function constructs line boxes for all of the text runs in the resolver and computes their position.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001205RootInlineBox* ComplexLineLayout::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 +00001206{
1207 if (!bidiRuns.runCount())
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001208 return nullptr;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001209
1210 // FIXME: Why is this only done when we had runs?
gyuyoung.kim@samsung.com113fb8b2013-11-28 23:25:19 +00001211 lineInfo.setLastLine(!end.renderer());
eric@webkit.org45e33a52011-05-04 11:51:09 +00001212
1213 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
1214 if (!lineBox)
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001215 return nullptr;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001216
mario.prada@samsung.com79397522014-02-28 18:12:52 +00001217 lineBox->setBidiLevel(bidiLevel);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001218 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
1219
cdumez@apple.com57d544c2014-10-16 00:05:37 +00001220 bool isSVGRootInlineBox = is<SVGRootInlineBox>(*lineBox);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001221
1222 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1223
1224 // Now we position all of our text runs horizontally.
1225 if (!isSVGRootInlineBox)
enrica@apple.com885c84d2012-10-10 05:48:51 +00001226 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache, wordMeasurements);
eric@webkit.org45e33a52011-05-04 11:51:09 +00001227
1228 // Now position our text runs vertically.
1229 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
1230
eric@webkit.org45e33a52011-05-04 11:51:09 +00001231 // SVG text layout code computes vertical & horizontal positions on its own.
1232 // Note that we still need to execute computeVerticalPositionsForLine() as
1233 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
1234 // contains reversed text or not. If we wouldn't do that editing and thus
1235 // text selection in RTL boxes would not work as expected.
1236 if (isSVGRootInlineBox) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001237 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(m_flow.isSVGText());
cdumez@apple.com57d544c2014-10-16 00:05:37 +00001238 downcast<SVGRootInlineBox>(*lineBox).computePerCharacterLayoutInformation();
eric@webkit.org45e33a52011-05-04 11:51:09 +00001239 }
eric@webkit.org45e33a52011-05-04 11:51:09 +00001240
1241 // Compute our overflow now.
1242 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
1243
eric@webkit.org45e33a52011-05-04 11:51:09 +00001244 return lineBox;
1245}
1246
akling@apple.com31dd4f42013-10-30 22:27:59 +00001247static void deleteLineRange(LineLayoutState& layoutState, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +00001248{
1249 RootInlineBox* boxToDelete = startLine;
1250 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001251 layoutState.updateRepaintRangeFromBox(boxToDelete);
akling@apple.com31dd4f42013-10-30 22:27:59 +00001252 // Note: deleteLineRange(firstRootBox()) is not identical to deleteLineBoxTree().
eric@webkit.orge2532d92011-05-16 23:10:49 +00001253 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +00001254 RootInlineBox* next = boxToDelete->nextRootBox();
akling@apple.com31dd4f42013-10-30 22:27:59 +00001255 boxToDelete->deleteLine();
eric@webkit.org455d90e2011-05-09 22:27:27 +00001256 boxToDelete = next;
1257 }
1258}
1259
zalan@apple.com4ed97602016-10-12 16:45:55 +00001260static void repaintDirtyFloats(LineLayoutState::FloatList& floats)
1261{
1262 // Floats that did not have layout did not repaint when we laid them out. They would have
1263 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1264 // painted.
1265 for (auto& floatBox : floats) {
1266 if (floatBox->everHadLayout())
1267 continue;
1268 auto& box = floatBox->renderer();
1269 if (!box.x() && !box.y() && box.checkForRepaintDuringLayout())
1270 box.repaint();
1271 }
1272}
1273
antti@apple.com50b36fde2019-08-11 11:02:01 +00001274void ComplexLineLayout::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild)
eric@webkit.org060caf62011-05-03 22:11:39 +00001275{
1276 // We want to skip ahead to the first dirty line
1277 InlineBidiResolver resolver;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001278 RootInlineBox* startLine = determineStartPosition(layoutState, resolver);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001279
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001280 unsigned consecutiveHyphenatedLines = 0;
1281 if (startLine) {
1282 for (RootInlineBox* line = startLine->prevRootBox(); line && line->isHyphenated(); line = line->prevRootBox())
1283 consecutiveHyphenatedLines++;
1284 }
1285
eric@webkit.org060caf62011-05-03 22:11:39 +00001286 // FIXME: This would make more sense outside of this function, but since
1287 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
1288 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001289 if (layoutState.isFullLayout() && hasInlineChild && !m_flow.selfNeedsLayout()) {
1290 m_flow.setNeedsLayout(MarkOnlyThis); // Mark as needing a full layout to force us to repaint.
1291 if (!layoutContext().needsFullRepaint() && m_flow.hasSelfPaintingLayer() && m_flow.hasRepaintLayoutRects()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001292 // Because we waited until we were already inside layout to discover
1293 // that the block really needed a full layout, we missed our chance to repaint the layer
antti@apple.com50b36fde2019-08-11 11:02:01 +00001294 // before layout started. Luckily the layer has cached the repaint rect for its original
eric@webkit.org060caf62011-05-03 22:11:39 +00001295 // position and size, and so we can use that to make a repaint happen now.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001296 m_flow.repaintUsingContainer(m_flow.containerForRepaint(), m_flow.repaintLayoutRects().m_repaintRect);
eric@webkit.org060caf62011-05-03 22:11:39 +00001297 }
1298 }
1299
antti@apple.com50b36fde2019-08-11 11:02:01 +00001300 if (m_flow.containsFloats())
1301 layoutState.floatList().setLastFloat(m_flow.floatingObjects()->set().last().get());
eric@webkit.org060caf62011-05-03 22:11:39 +00001302
antti@apple.com50b36fde2019-08-11 11:02:01 +00001303 // We also find the first clean line and extract these lines. We will add them back
eric@webkit.org060caf62011-05-03 22:11:39 +00001304 // if we determine that we're able to synchronize after handling all our dirty lines.
1305 InlineIterator cleanLineStart;
1306 BidiStatus cleanLineBidiStatus;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001307 if (!layoutState.isFullLayout() && startLine)
1308 determineEndPosition(layoutState, startLine, cleanLineStart, cleanLineBidiStatus);
eric@webkit.org060caf62011-05-03 22:11:39 +00001309
1310 if (startLine) {
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001311 if (!layoutState.usesRepaintBounds())
antti@apple.com50b36fde2019-08-11 11:02:01 +00001312 layoutState.setRepaintRange(m_flow.logicalHeight());
akling@apple.com31dd4f42013-10-30 22:27:59 +00001313 deleteLineRange(layoutState, startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001314 }
1315
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001316 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001317 // If the last line before the start line ends with a line break that clear floats,
1318 // adjust the height accordingly.
1319 // A line break can be either the first or the last object on a line, depending on its direction.
antti@apple.com7ac7f602019-09-25 14:23:03 +00001320 if (InlineBox* lastLeafDescendant = lastRootBox()->lastLeafDescendant()) {
1321 RenderObject* lastObject = &lastLeafDescendant->renderer();
eric@webkit.org060caf62011-05-03 22:11:39 +00001322 if (!lastObject->isBR())
antti@apple.com7ac7f602019-09-25 14:23:03 +00001323 lastObject = &lastRootBox()->firstLeafDescendant()->renderer();
eric@webkit.org060caf62011-05-03 22:11:39 +00001324 if (lastObject->isBR()) {
commit-queue@webkit.org1a4e6672018-05-21 16:55:45 +00001325 Clear clear = lastObject->style().clear();
1326 if (clear != Clear::None)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001327 m_flow.clearFloats(clear);
eric@webkit.org060caf62011-05-03 22:11:39 +00001328 }
1329 }
1330 }
1331
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001332 layoutRunsAndFloatsInRange(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, consecutiveHyphenatedLines);
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001333 linkToEndLineIfNeeded(layoutState);
zalan@apple.com4ed97602016-10-12 16:45:55 +00001334 repaintDirtyFloats(layoutState.floatList());
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001335}
eric@webkit.org060caf62011-05-03 22:11:39 +00001336
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001337// Before restarting the layout loop with a new logicalHeight, remove all floats that were added and reset the resolver.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001338inline const InlineIterator& ComplexLineLayout::restartLayoutRunsAndFloatsInRange(LayoutUnit oldLogicalHeight, LayoutUnit newLogicalHeight, FloatingObject* lastFloatFromPreviousLine, InlineBidiResolver& resolver, const InlineIterator& oldEnd)
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001339{
antti@apple.com50b36fde2019-08-11 11:02:01 +00001340 m_flow.removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1341 m_flow.setLogicalHeight(newLogicalHeight);
commit-queue@webkit.org4055b2e2012-12-06 19:05:15 +00001342 resolver.setPositionIgnoringNestedIsolates(oldEnd);
1343 return oldEnd;
1344}
1345
antti@apple.com50b36fde2019-08-11 11:02:01 +00001346void ComplexLineLayout::layoutRunsAndFloatsInRange(LineLayoutState& layoutState, InlineBidiResolver& resolver, const InlineIterator& cleanLineStart, const BidiStatus& cleanLineBidiStatus, unsigned consecutiveHyphenatedLines)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001347{
akling@apple.com827be9c2013-10-29 02:58:43 +00001348 const RenderStyle& styleToUse = style();
antti@apple.com50b36fde2019-08-11 11:02:01 +00001349 bool paginated = layoutContext().layoutState() && layoutContext().layoutState()->isPaginated();
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001350 LineWhitespaceCollapsingState& lineWhitespaceCollapsingState = resolver.whitespaceCollapsingState();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001351 InlineIterator end = resolver.position();
1352 bool checkForEndLineMatch = layoutState.endLine();
mitz@apple.com6a859602012-08-27 15:31:56 +00001353 RenderTextInfo renderTextInfo;
eric@webkit.org060caf62011-05-03 22:11:39 +00001354 VerticalPositionCache verticalPositionCache;
1355
antti@apple.com50b36fde2019-08-11 11:02:01 +00001356 LineBreaker lineBreaker(m_flow);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001357
eric@webkit.org060caf62011-05-03 22:11:39 +00001358 while (!end.atEnd()) {
1359 // 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 +00001360 if (checkForEndLineMatch) {
1361 layoutState.setEndLineMatched(matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus));
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001362 if (layoutState.endLineMatched()) {
1363 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001364 layoutState.marginInfo().clearMargin();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001365 break;
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001366 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001367 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001368
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001369 lineWhitespaceCollapsingState.reset();
eric@webkit.org060caf62011-05-03 22:11:39 +00001370
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001371 layoutState.lineInfo().setEmpty(true);
robert@webkit.org8cbab142011-12-30 20:58:29 +00001372 layoutState.lineInfo().resetRunsFromLeadingWhitespace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001373
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001374 const InlineIterator oldEnd = end;
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001375 bool isNewUBAParagraph = layoutState.lineInfo().previousLineBrokeCleanly();
antti@apple.com50b36fde2019-08-11 11:02:01 +00001376 FloatingObject* lastFloatFromPreviousLine = (m_flow.containsFloats()) ? m_flow.floatingObjects()->set().last().get() : nullptr;
zoltan@webkit.org3bd77f52013-04-23 18:23:36 +00001377
enrica@apple.com885c84d2012-10-10 05:48:51 +00001378 WordMeasurements wordMeasurements;
antti@apple.comae85e112017-08-31 23:27:02 +00001379 end = lineBreaker.nextLineBreak(resolver, layoutState.lineInfo(), renderTextInfo, lastFloatFromPreviousLine, consecutiveHyphenatedLines, wordMeasurements);
antti@apple.com50b36fde2019-08-11 11:02:01 +00001380 m_flow.cachePriorCharactersIfNeeded(renderTextInfo.lineBreakIterator);
darin@apple.com3d1d5fc2015-04-23 05:20:23 +00001381 renderTextInfo.lineBreakIterator.resetPriorContext();
eric@webkit.org060caf62011-05-03 22:11:39 +00001382 if (resolver.position().atEnd()) {
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001383 // FIXME: We shouldn't be creating any runs in nextLineBreak to begin with!
eric@webkit.org060caf62011-05-03 22:11:39 +00001384 // Once BidiRunList is separated from BidiResolver this will not be needed.
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001385 resolver.runs().clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001386 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001387 layoutState.setCheckForFloatsFromLastLine(true);
rniwa@webkit.org7daa12d2011-12-02 02:39:14 +00001388 resolver.setPosition(InlineIterator(resolver.position().root(), 0, 0), 0);
eric@webkit.org060caf62011-05-03 22:11:39 +00001389 break;
1390 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001391
betravis@adobe.com9e5e8242013-04-19 23:28:26 +00001392 ASSERT(end != resolver.position());
1393
eric@webkit.org45e33a52011-05-04 11:51:09 +00001394 // This is a short-cut for empty lines.
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001395 if (layoutState.lineInfo().isEmpty()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001396 if (lastRootBox())
gyuyoung.kim@samsung.com044376f2013-12-26 07:32:04 +00001397 lastRootBox()->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
eric@webkit.org060caf62011-05-03 22:11:39 +00001398 } else {
commit-queue@webkit.orgdf99a392018-08-13 18:19:03 +00001399 VisualDirectionOverride override = (styleToUse.rtlOrdering() == Order::Visual ? (styleToUse.direction() == TextDirection::LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001400
akling@apple.com827be9c2013-10-29 02:58:43 +00001401 if (isNewUBAParagraph && styleToUse.unicodeBidi() == Plaintext && !resolver.context()->parent()) {
1402 TextDirection direction = styleToUse.direction();
leviw@chromium.orge7812f32012-02-07 23:46:40 +00001403 determineDirectionality(direction, resolver.position());
akling@apple.com827be9c2013-10-29 02:58:43 +00001404 resolver.setStatus(BidiStatus(direction, isOverride(styleToUse.unicodeBidi())));
leviw@chromium.org7781b6a2011-06-27 22:01:38 +00001405 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001406 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
1407 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
zoltan@webkit.org7d4f8cc2014-03-26 18:20:15 +00001408 constructBidiRunsForSegment(resolver, bidiRuns, end, override, layoutState.lineInfo().previousLineBrokeCleanly());
eric@webkit.org060caf62011-05-03 22:11:39 +00001409 ASSERT(resolver.position() == end);
1410
mmaxfield@apple.com08b380d2015-04-02 20:29:23 +00001411 BidiRun* trailingSpaceRun = !layoutState.lineInfo().previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : nullptr;
eric@webkit.org060caf62011-05-03 22:11:39 +00001412
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001413 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated()) {
eric@webkit.org45e33a52011-05-04 11:51:09 +00001414 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
mitz@apple.com10ed3cb2011-09-07 20:59:39 +00001415 consecutiveHyphenatedLines++;
1416 } else
1417 consecutiveHyphenatedLines = 0;
eric@webkit.org45e33a52011-05-04 11:51:09 +00001418
eric@webkit.org060caf62011-05-03 22:11:39 +00001419 // Now that the runs have been ordered, we create the line boxes.
1420 // At the same time we figure out where border/padding/margin should be applied for
1421 // inline flow boxes.
1422
antti@apple.com50b36fde2019-08-11 11:02:01 +00001423 LayoutUnit oldLogicalHeight = m_flow.logicalHeight();
mario.prada@samsung.com79397522014-02-28 18:12:52 +00001424 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(resolver.status().context->level(), bidiRuns, end, layoutState.lineInfo(), verticalPositionCache, trailingSpaceRun, wordMeasurements);
eric@webkit.org060caf62011-05-03 22:11:39 +00001425
mmaxfield@apple.com9ccce672016-04-02 07:01:43 +00001426 bidiRuns.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001427 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
1428
1429 if (lineBox) {
gyuyoung.kim@samsung.com044376f2013-12-26 07:32:04 +00001430 lineBox->setLineBreakInfo(end.renderer(), end.offset(), resolver.status());
commit-queue@webkit.org49ad4732011-07-15 07:23:01 +00001431 if (layoutState.usesRepaintBounds())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001432 layoutState.updateRepaintRangeFromBox(lineBox);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001433
ross.kirsling@sony.combd744282018-11-18 03:14:31 +00001434 LayoutUnit adjustment;
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001435 bool overflowsFragment = false;
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001436
antti@apple.comae85e112017-08-31 23:27:02 +00001437 layoutState.marginInfo().setAtBeforeSideOfBlock(false);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001438
1439 if (paginated)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001440 m_flow.adjustLinePositionForPagination(lineBox, adjustment, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001441 if (adjustment) {
zalan@apple.com64761fe2016-03-02 21:42:22 +00001442 IndentTextOrNot shouldIndentText = layoutState.lineInfo().isFirstLine() ? IndentText : DoNotIndentText;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001443 LayoutUnit oldLineWidth = m_flow.availableLogicalWidthForLine(oldLogicalHeight, shouldIndentText);
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001444 lineBox->adjustBlockDirectionPosition(adjustment);
1445 if (layoutState.usesRepaintBounds())
1446 layoutState.updateRepaintRangeFromBox(lineBox);
1447
antti@apple.com50b36fde2019-08-11 11:02:01 +00001448 if (m_flow.availableLogicalWidthForLine(oldLogicalHeight + adjustment, shouldIndentText) != oldLineWidth) {
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001449 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1450 lineBox->deleteLine();
1451 end = restartLayoutRunsAndFloatsInRange(oldLogicalHeight, oldLogicalHeight + adjustment, lastFloatFromPreviousLine, resolver, oldEnd);
1452 continue;
1453 }
1454
antti@apple.com50b36fde2019-08-11 11:02:01 +00001455 m_flow.setLogicalHeight(lineBox->lineBottomWithLeading());
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001456 }
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001457
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001458 if (paginated) {
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001459 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001460 updateFragmentForLine(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001461 }
1462 }
robert@webkit.org402c1512013-02-07 18:48:39 +00001463 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001464
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00001465 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001466 setStaticPositions(m_flow, *lineBreaker.positionedObjects()[i], DoNotIndentText);
eric@webkit.org060caf62011-05-03 22:11:39 +00001467
robert@webkit.org402c1512013-02-07 18:48:39 +00001468 if (!layoutState.lineInfo().isEmpty()) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001469 layoutState.lineInfo().setFirstLine(false);
antti@apple.com50b36fde2019-08-11 11:02:01 +00001470 m_flow.clearFloats(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001471 }
1472
antti@apple.com50b36fde2019-08-11 11:02:01 +00001473 if (m_flow.floatingObjects() && lastRootBox()) {
1474 const FloatingObjectSet& floatingObjectSet = m_flow.floatingObjects()->set();
darin@apple.com7cad7042013-09-24 05:53:55 +00001475 auto it = floatingObjectSet.begin();
1476 auto end = floatingObjectSet.end();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001477 if (auto* lastFloat = layoutState.floatList().lastFloat()) {
commit-queue@webkit.orgc27f5ca2018-11-26 19:29:23 +00001478 auto lastFloatIterator = floatingObjectSet.find(lastFloat);
eric@webkit.org060caf62011-05-03 22:11:39 +00001479 ASSERT(lastFloatIterator != end);
1480 ++lastFloatIterator;
1481 it = lastFloatIterator;
1482 }
1483 for (; it != end; ++it) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001484 auto& floatingObject = *it;
1485 appendFloatingObjectToLastLine(*floatingObject);
eric@webkit.org060caf62011-05-03 22:11:39 +00001486 // If a float's geometry has changed, give up on syncing with clean lines.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001487 auto* floatWithRect = layoutState.floatList().floatWithRect(floatingObject->renderer());
1488 if (!floatWithRect || floatWithRect->rect() != floatingObject->frameRect())
eric@webkit.org060caf62011-05-03 22:11:39 +00001489 checkForEndLineMatch = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00001490 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001491 layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
eric@webkit.org060caf62011-05-03 22:11:39 +00001492 }
1493
mmaxfield@apple.comfd1aa5c2016-04-07 04:59:55 +00001494 lineWhitespaceCollapsingState.reset();
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001495 resolver.setPosition(end, numberOfIsolateAncestors(end));
eric@webkit.org060caf62011-05-03 22:11:39 +00001496 }
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001497
abucur@adobe.comfc497132013-10-04 08:49:21 +00001498 // In case we already adjusted the line positions during this layout to avoid widows
1499 // then we need to ignore the possibility of having a new widows situation.
1500 // Otherwise, we risk leaving empty containers which is against the block fragmentation principles.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001501 if (paginated && !style().hasAutoWidows() && !m_flow.didBreakAtLineToAvoidWidow()) {
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001502 // Check the line boxes to make sure we didn't create unacceptable widows.
1503 // However, we'll prioritize orphans - so nothing we do here should create
1504 // a new orphan.
1505
1506 RootInlineBox* lineBox = lastRootBox();
1507
1508 // Count from the end of the block backwards, to see how many hanging
1509 // lines we have.
1510 RootInlineBox* firstLineInBlock = firstRootBox();
1511 int numLinesHanging = 1;
1512 while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1513 ++numLinesHanging;
1514 lineBox = lineBox->prevRootBox();
1515 }
1516
1517 // If there were no breaks in the block, we didn't create any widows.
jchaffraix@webkit.org0f225142013-01-28 22:28:21 +00001518 if (!lineBox || !lineBox->isFirstAfterPageBreak() || lineBox == firstLineInBlock)
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001519 return;
1520
akling@apple.com827be9c2013-10-29 02:58:43 +00001521 if (numLinesHanging < style().widows()) {
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001522 // We have detected a widow. Now we need to work out how many
1523 // lines there are on the previous page, and how many we need
1524 // to steal.
akling@apple.com827be9c2013-10-29 02:58:43 +00001525 int numLinesNeeded = style().widows() - numLinesHanging;
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001526 RootInlineBox* currentFirstLineOfNewPage = lineBox;
1527
1528 // Count the number of lines in the previous page.
1529 lineBox = lineBox->prevRootBox();
1530 int numLinesInPreviousPage = 1;
1531 while (lineBox && lineBox != firstLineInBlock && !lineBox->isFirstAfterPageBreak()) {
1532 ++numLinesInPreviousPage;
1533 lineBox = lineBox->prevRootBox();
1534 }
1535
1536 // If there was an explicit value for orphans, respect that. If not, we still
1537 // shouldn't create a situation where we make an orphan bigger than the initial value.
1538 // This means that setting widows implies we also care about orphans, but given
1539 // the specification says the initial orphan value is non-zero, this is ok. The
1540 // author is always free to set orphans explicitly as well.
akling@apple.com827be9c2013-10-29 02:58:43 +00001541 int orphans = style().hasAutoOrphans() ? style().initialOrphans() : style().orphans();
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001542 int numLinesAvailable = numLinesInPreviousPage - orphans;
1543 if (numLinesAvailable <= 0)
1544 return;
1545
andersca@apple.com86298632013-11-10 19:32:33 +00001546 int numLinesToTake = std::min(numLinesAvailable, numLinesNeeded);
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001547 // Wind back from our first widowed line.
1548 lineBox = currentFirstLineOfNewPage;
1549 for (int i = 0; i < numLinesToTake; ++i)
1550 lineBox = lineBox->prevRootBox();
1551
1552 // We now want to break at this line. Remember for next layout and trigger relayout.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001553 m_flow.setBreakAtLineToAvoidWidow(m_flow.lineCount(lineBox));
1554 m_flow.markLinesDirtyInBlockRange(lastRootBox()->lineBottomWithLeading(), lineBox->lineBottomWithLeading(), lineBox);
dino@apple.comfde5fdf2012-12-10 21:22:44 +00001555 }
1556 }
antti@apple.com50b36fde2019-08-11 11:02:01 +00001557 m_flow.clearDidBreakAtLineToAvoidWidow();
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001558}
eric@webkit.org060caf62011-05-03 22:11:39 +00001559
antti@apple.com50b36fde2019-08-11 11:02:01 +00001560void ComplexLineLayout::reattachCleanLineFloats(RootInlineBox& cleanLine, LayoutUnit delta, bool isFirstCleanLine)
zalan@apple.com6f66a672016-04-06 15:56:06 +00001561{
1562 auto* cleanLineFloats = cleanLine.floatsPtr();
1563 if (!cleanLineFloats)
1564 return;
1565
zalan@apple.com9201b992017-10-06 22:14:05 +00001566 for (auto& floatingBox : *cleanLineFloats) {
1567 if (!floatingBox)
1568 continue;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001569 auto* floatingObject = m_flow.insertFloatingObject(*floatingBox);
zalan@apple.com6f66a672016-04-06 15:56:06 +00001570 if (isFirstCleanLine && floatingObject->originatingLine()) {
1571 // Float box does not belong to this line anymore.
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001572 ASSERT_WITH_SECURITY_IMPLICATION(cleanLine.prevRootBox() == floatingObject->originatingLine());
zalan@apple.com6f66a672016-04-06 15:56:06 +00001573 cleanLine.removeFloat(*floatingBox);
1574 continue;
1575 }
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001576 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001577 floatingObject->setOriginatingLine(cleanLine);
antti@apple.com50b36fde2019-08-11 11:02:01 +00001578 m_flow.setLogicalHeight(m_flow.logicalTopForChild(*floatingBox) - m_flow.marginBeforeForChild(*floatingBox) + delta);
1579 m_flow.positionNewFloats();
zalan@apple.com6f66a672016-04-06 15:56:06 +00001580 }
1581}
1582
antti@apple.com50b36fde2019-08-11 11:02:01 +00001583void ComplexLineLayout::linkToEndLineIfNeeded(LineLayoutState& layoutState)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001584{
zalan@apple.com6f66a672016-04-06 15:56:06 +00001585 auto* firstCleanLine = layoutState.endLine();
1586 if (firstCleanLine) {
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001587 if (layoutState.endLineMatched()) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001588 bool paginated = layoutContext().layoutState() && layoutContext().layoutState()->isPaginated();
eric@webkit.org060caf62011-05-03 22:11:39 +00001589 // Attach all the remaining lines, and then adjust their y-positions as needed.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001590 LayoutUnit delta = m_flow.logicalHeight() - layoutState.endLineLogicalTop();
zalan@apple.com6f66a672016-04-06 15:56:06 +00001591 for (auto* line = firstCleanLine; line; line = line->nextRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001592 line->attachLine();
1593 if (paginated) {
1594 delta -= line->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001595 bool overflowsFragment;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001596 m_flow.adjustLinePositionForPagination(line, delta, overflowsFragment, layoutState.fragmentedFlow());
eric@webkit.org060caf62011-05-03 22:11:39 +00001597 }
1598 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001599 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001600 line->adjustBlockDirectionPosition(delta);
1601 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001602 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001603 updateFragmentForLine(line);
zalan@apple.com6f66a672016-04-06 15:56:06 +00001604 reattachCleanLineFloats(*line, delta, line == firstCleanLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001605 }
antti@apple.com50b36fde2019-08-11 11:02:01 +00001606 m_flow.setLogicalHeight(lastRootBox()->lineBottomWithLeading());
eric@webkit.org060caf62011-05-03 22:11:39 +00001607 } else {
1608 // Delete all the remaining lines.
akling@apple.com31dd4f42013-10-30 22:27:59 +00001609 deleteLineRange(layoutState, layoutState.endLine());
eric@webkit.org060caf62011-05-03 22:11:39 +00001610 }
1611 }
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001612
antti@apple.com50b36fde2019-08-11 11:02:01 +00001613 if (m_flow.floatingObjects() && (layoutState.checkForFloatsFromLastLine() || m_flow.positionNewFloats()) && lastRootBox()) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001614 // In case we have a float on the last line, it might not be positioned up to now.
1615 // This has to be done before adding in the bottom border/padding, or the float will
1616 // include the padding incorrectly. -dwh
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001617 if (layoutState.checkForFloatsFromLastLine()) {
leviw@chromium.orgd32486e2012-03-16 10:52:56 +00001618 LayoutUnit bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1619 LayoutUnit bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +00001620 auto newLineBox = makeUnique<TrailingFloatsRootInlineBox>(m_flow);
akling@apple.comb5f24642013-11-06 04:47:12 +00001621 auto trailingFloatsLineBox = newLineBox.get();
aestes@apple.com13aae082016-01-02 08:03:08 +00001622 m_lineBoxes.appendLineBox(WTFMove(newLineBox));
eric@webkit.org060caf62011-05-03 22:11:39 +00001623 trailingFloatsLineBox->setConstructed();
1624 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1625 VerticalPositionCache verticalPositionCache;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001626 LayoutUnit blockLogicalHeight = m_flow.logicalHeight();
hyatt@apple.coma8b5b822011-09-07 18:48:07 +00001627 trailingFloatsLineBox->alignBoxesInBlockDirection(blockLogicalHeight, textBoxDataMap, verticalPositionCache);
1628 trailingFloatsLineBox->setLineTopBottomPositions(blockLogicalHeight, blockLogicalHeight, blockLogicalHeight, blockLogicalHeight);
antti@apple.com50b36fde2019-08-11 11:02:01 +00001629 trailingFloatsLineBox->setPaginatedLineWidth(m_flow.availableLogicalWidthForContent(blockLogicalHeight));
ross.kirsling@sony.coma10d10c2018-11-23 20:47:11 +00001630 LayoutRect logicalLayoutOverflow(0_lu, blockLogicalHeight, 1_lu, bottomLayoutOverflow - blockLogicalHeight);
1631 LayoutRect logicalVisualOverflow(0_lu, blockLogicalHeight, 1_lu, bottomVisualOverflow - blockLogicalHeight);
eric@webkit.org060caf62011-05-03 22:11:39 +00001632 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001633 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001634 updateFragmentForLine(trailingFloatsLineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +00001635 }
1636
antti@apple.com50b36fde2019-08-11 11:02:01 +00001637 const FloatingObjectSet& floatingObjectSet = m_flow.floatingObjects()->set();
darin@apple.com7cad7042013-09-24 05:53:55 +00001638 auto it = floatingObjectSet.begin();
1639 auto end = floatingObjectSet.end();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001640 if (auto* lastFloat = layoutState.floatList().lastFloat()) {
commit-queue@webkit.orgc27f5ca2018-11-26 19:29:23 +00001641 auto lastFloatIterator = floatingObjectSet.find(lastFloat);
eric@webkit.org060caf62011-05-03 22:11:39 +00001642 ASSERT(lastFloatIterator != end);
1643 ++lastFloatIterator;
1644 it = lastFloatIterator;
1645 }
1646 for (; it != end; ++it)
zalan@apple.com4ed97602016-10-12 16:45:55 +00001647 appendFloatingObjectToLastLine(**it);
1648 layoutState.floatList().setLastFloat(!floatingObjectSet.isEmpty() ? floatingObjectSet.last().get() : nullptr);
eric@webkit.org060caf62011-05-03 22:11:39 +00001649 }
1650}
1651
antti@apple.com50b36fde2019-08-11 11:02:01 +00001652void ComplexLineLayout::layoutLineBoxes(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001653{
antti@apple.com50b36fde2019-08-11 11:02:01 +00001654 ASSERT(!m_flow.simpleLineLayout());
antti@apple.com940f5872013-10-24 20:31:11 +00001655
antti@apple.com50b36fde2019-08-11 11:02:01 +00001656 m_flow.setLogicalHeight(m_flow.borderAndPaddingBefore());
hyatt@apple.comee7af1d2012-01-17 19:16:24 +00001657
1658 // Lay out our hypothetical grid line as though it occurs at the top of the block.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001659 if (layoutContext().layoutState() && layoutContext().layoutState()->lineGrid() == &m_flow)
1660 m_flow.layoutLineGridBox();
mitz@apple.come1364202008-02-28 01:06:41 +00001661
antti@apple.com50b36fde2019-08-11 11:02:01 +00001662 RenderFragmentedFlow* fragmentedFlow = m_flow.enclosingFragmentedFlow();
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001663 bool clearLinesForPagination = firstRootBox() && fragmentedFlow && !fragmentedFlow->hasFragments();
commit-queue@webkit.org93cdd632012-12-07 00:33:56 +00001664
hyatt0c3a9862004-02-23 21:26:26 +00001665 // Figure out if we should clear out our line boxes.
1666 // FIXME: Handle resize eventually!
antti@apple.com50b36fde2019-08-11 11:02:01 +00001667 bool isFullLayout = !firstRootBox() || m_flow.selfNeedsLayout() || relayoutChildren || clearLinesForPagination;
1668 LineLayoutState layoutState(m_flow, isFullLayout, repaintLogicalTop, repaintLogicalBottom, fragmentedFlow);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001669
1670 if (isFullLayout)
akling@apple.com31dd4f42013-10-30 22:27:59 +00001671 lineBoxes().deleteLineBoxes();
mitz@apple.come1364202008-02-28 01:06:41 +00001672
commit-queue@webkit.org07797312013-02-22 18:58:19 +00001673 // Text truncation kicks in in two cases:
1674 // 1) If your overflow isn't visible and your text-overflow-mode isn't clip.
1675 // 2) If you're an anonymous block with a block parent that satisfies #1.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001676 // 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 +00001677 // difficult to figure out in general (especially in the middle of doing layout), so we only handle the
1678 // simple case of an anonymous block truncating when it's parent is clipped.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001679 auto* parent = m_flow.parent();
1680 bool hasTextOverflow = (style().textOverflow() == TextOverflow::Ellipsis && m_flow.hasOverflowClip())
1681 || (m_flow.isAnonymousBlock() && parent && parent->isRenderBlock() && parent->style().textOverflow() == TextOverflow::Ellipsis && parent->hasOverflowClip());
mitz@apple.come1364202008-02-28 01:06:41 +00001682
hyatted77ad82004-06-15 07:21:23 +00001683 // Walk all the lines and delete our ellipsis line boxes if they exist.
1684 if (hasTextOverflow)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001685 deleteEllipsisLineBoxes();
hyatted77ad82004-06-15 07:21:23 +00001686
antti@apple.com50b36fde2019-08-11 11:02:01 +00001687 if (m_flow.firstChild()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001688 // In full layout mode, clear the line boxes of children upfront. Otherwise,
1689 // siblings can run into stale root lineboxes during layout. Then layout
1690 // the replaced elements later. In partial layout mode, line boxes are not
1691 // deleted and only dirtied. In that case, we can layout the replaced
1692 // elements at the same time.
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001693 bool hasInlineChild = false;
inferno@chromium.org13563122012-08-16 20:50:05 +00001694 Vector<RenderBox*> replacedChildren;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001695 for (InlineWalker walker(m_flow); !walker.atEnd(); walker.advance()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001696 RenderObject& o = *walker.current();
abucur@adobe.com33159da2013-08-13 07:44:32 +00001697
weinig@apple.com12840dc2013-10-22 23:59:08 +00001698 if (!hasInlineChild && o.isInline())
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001699 hasInlineChild = true;
1700
weinig@apple.com12840dc2013-10-22 23:59:08 +00001701 if (o.isReplaced() || o.isFloating() || o.isOutOfFlowPositioned()) {
cdumez@apple.com0abff8b2014-10-17 21:25:10 +00001702 RenderBox& box = downcast<RenderBox>(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001703
weinig@apple.com12840dc2013-10-22 23:59:08 +00001704 if (relayoutChildren || box.hasRelativeDimensions())
1705 box.setChildNeedsLayout(MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001706
zimmermann@webkit.orgac68af42011-06-15 08:02:37 +00001707 // 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 +00001708 if (relayoutChildren && box.needsPreferredWidthsRecalculation())
1709 box.setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
eric@webkit.org060caf62011-05-03 22:11:39 +00001710
weinig@apple.com12840dc2013-10-22 23:59:08 +00001711 if (box.isOutOfFlowPositioned())
1712 box.containingBlock()->insertPositionedObject(box);
1713 else if (box.isFloating())
zalan@apple.com4ed97602016-10-12 16:45:55 +00001714 layoutState.floatList().append(FloatWithRect::create(box));
weinig@apple.com12840dc2013-10-22 23:59:08 +00001715 else if (isFullLayout || box.needsLayout()) {
inferno@chromium.org13563122012-08-16 20:50:05 +00001716 // Replaced element.
zalan@apple.comf2d2fb52017-07-06 05:49:50 +00001717 if (isFullLayout && is<RenderRubyRun>(box)) {
1718 // FIXME: This resets the overhanging margins that we set during line layout (see computeInlineDirectionPositionsForSegment)
1719 // Find a more suitable place for this.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001720 m_flow.setMarginStartForChild(box, 0);
1721 m_flow.setMarginEndForChild(box, 0);
zalan@apple.comf2d2fb52017-07-06 05:49:50 +00001722 }
weinig@apple.com12840dc2013-10-22 23:59:08 +00001723 box.dirtyLineBoxes(isFullLayout);
antti@apple.comae85e112017-08-31 23:27:02 +00001724 if (isFullLayout)
1725 replacedChildren.append(&box);
1726 else
1727 box.layoutIfNeeded();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001728 }
cdumez@apple.comf8022152014-10-15 00:29:51 +00001729 } else if (o.isTextOrLineBreak() || (is<RenderInline>(o) && !walker.atEndOfInline())) {
1730 if (is<RenderInline>(o))
1731 downcast<RenderInline>(o).updateAlwaysCreateLineBoxes(layoutState.isFullLayout());
weinig@apple.com12840dc2013-10-22 23:59:08 +00001732 if (layoutState.isFullLayout() || o.selfNeedsLayout())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001733 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
weinig@apple.com12840dc2013-10-22 23:59:08 +00001734 o.clearNeedsLayout();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001735 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001736 }
1737
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00001738 for (size_t i = 0; i < replacedChildren.size(); i++)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001739 replacedChildren[i]->layoutIfNeeded();
inferno@chromium.org13563122012-08-16 20:50:05 +00001740
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001741 layoutRunsAndFloats(layoutState, hasInlineChild);
kociendabb0c24b2001-08-24 14:24:40 +00001742 }
hyatt85586af2003-02-19 23:22:42 +00001743
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001744 // Expand the last line to accommodate Ruby and emphasis marks.
1745 int lastLineAnnotationsAdjustment = 0;
1746 if (lastRootBox()) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001747 LayoutUnit lowestAllowedPosition = std::max(lastRootBox()->lineBottom(), m_flow.logicalHeight() + m_flow.paddingAfter());
akling@apple.com827be9c2013-10-29 02:58:43 +00001748 if (!style().isFlippedLinesWritingMode())
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001749 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1750 else
1751 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001752 }
hyatt@apple.com9a79c622015-09-15 18:38:18 +00001753
1754 // Now do the handling of the bottom of the block, adding in our bottom border/padding and
1755 // determining the correct collapsed bottom margin information. This collapse is only necessary
1756 // if our last child was an anonymous inline block that might need to propagate margin information out to
1757 // us.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001758 LayoutUnit afterEdge = m_flow.borderAndPaddingAfter() + m_flow.scrollbarLogicalHeight() + lastLineAnnotationsAdjustment;
1759 m_flow.setLogicalHeight(m_flow.logicalHeight() + afterEdge);
kociendabb0c24b2001-08-24 14:24:40 +00001760
antti@apple.com50b36fde2019-08-11 11:02:01 +00001761 if (!firstRootBox() && m_flow.hasLineIfEmpty())
1762 m_flow.setLogicalHeight(m_flow.logicalHeight() + m_flow.lineHeight(true, m_flow.isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001763
antti@apple.com50b36fde2019-08-11 11:02:01 +00001764 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
hyatted77ad82004-06-15 07:21:23 +00001765 // truncate text.
1766 if (hasTextOverflow)
1767 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001768}
1769
antti@apple.com50b36fde2019-08-11 11:02:01 +00001770void ComplexLineLayout::checkFloatInCleanLine(RootInlineBox& cleanLine, RenderBox& floatBoxOnCleanLine, FloatWithRect& matchingFloatWithRect,
zalan@apple.com4ed97602016-10-12 16:45:55 +00001771 bool& encounteredNewFloat, bool& dirtiedByFloat)
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001772{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001773 ASSERT_WITH_SECURITY_IMPLICATION(!floatBoxOnCleanLine.style().deletionHasBegun());
1774 if (&matchingFloatWithRect.renderer() != &floatBoxOnCleanLine) {
stavila@adobe.comf4ef0972014-03-28 21:55:46 +00001775 encounteredNewFloat = true;
1776 return;
1777 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001778 floatBoxOnCleanLine.layoutIfNeeded();
1779 LayoutRect originalFloatRect = matchingFloatWithRect.rect();
1780 LayoutSize newSize(
1781 floatBoxOnCleanLine.width() + floatBoxOnCleanLine.horizontalMarginExtent(),
1782 floatBoxOnCleanLine.height() + floatBoxOnCleanLine.verticalMarginExtent());
hyatt@apple.comc2e15522014-09-03 19:26:38 +00001783
zalan@apple.com4ed97602016-10-12 16:45:55 +00001784 // 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 +00001785 if (originalFloatRect.size() == newSize && (floatBoxOnCleanLine.style().styleType() != PseudoId::FirstLetter || !floatBoxOnCleanLine.style().initialLetterDrop()))
zalan@apple.com4ed97602016-10-12 16:45:55 +00001786 return;
1787
antti@apple.com50b36fde2019-08-11 11:02:01 +00001788 LayoutUnit floatTop = m_flow.isHorizontalWritingMode() ? originalFloatRect.y() : originalFloatRect.x();
1789 LayoutUnit floatHeight = m_flow.isHorizontalWritingMode() ? std::max(originalFloatRect.height(), newSize.height())
zalan@apple.com4ed97602016-10-12 16:45:55 +00001790 : std::max(originalFloatRect.width(), newSize.width());
1791 floatHeight = std::min(floatHeight, LayoutUnit::max() - floatTop);
1792 cleanLine.markDirty();
antti@apple.com50b36fde2019-08-11 11:02:01 +00001793 m_flow.markLinesDirtyInBlockRange(cleanLine.lineBottomWithLeading(), floatTop + floatHeight, &cleanLine);
zalan@apple.com4ed97602016-10-12 16:45:55 +00001794 LayoutRect newFloatRect = originalFloatRect;
1795 newFloatRect.setSize(newSize);
1796 matchingFloatWithRect.adjustRect(newFloatRect);
1797 dirtiedByFloat = true;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001798}
1799
antti@apple.com50b36fde2019-08-11 11:02:01 +00001800RootInlineBox* ComplexLineLayout::determineStartPosition(LineLayoutState& layoutState, InlineBidiResolver& resolver)
hyatt0c3a9862004-02-23 21:26:26 +00001801{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001802 RootInlineBox* currentLine = nullptr;
1803 RootInlineBox* lastLine = nullptr;
mitz@apple.come1364202008-02-28 01:06:41 +00001804
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001805 // FIXME: This entire float-checking block needs to be broken into a new function.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001806 auto& floats = layoutState.floatList();
mitz@apple.com40547b32008-03-18 04:04:34 +00001807 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001808 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001809 // Paginate all of the clean lines.
antti@apple.com50b36fde2019-08-11 11:02:01 +00001810 bool paginated = layoutContext().layoutState() && layoutContext().layoutState()->isPaginated();
ross.kirsling@sony.combd744282018-11-18 03:14:31 +00001811 LayoutUnit paginationDelta;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001812 auto floatsIterator = floats.begin();
1813 auto end = floats.end();
1814 for (currentLine = firstRootBox(); currentLine && !currentLine->isDirty(); currentLine = currentLine->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001815 if (paginated) {
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001816 if (lineWidthForPaginatedLineChanged(currentLine, 0, layoutState.fragmentedFlow())) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001817 currentLine->markDirty();
hyatt@apple.com0c6cd7a2011-09-22 20:50:41 +00001818 break;
1819 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001820 paginationDelta -= currentLine->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001821 bool overflowsFragment;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001822 m_flow.adjustLinePositionForPagination(currentLine, paginationDelta, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001823 if (paginationDelta) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001824 if (m_flow.containsFloats() || !floats.isEmpty()) {
1825 // 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 +00001826 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001827 break;
1828 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001829
zalan@apple.com4ed97602016-10-12 16:45:55 +00001830 layoutState.updateRepaintRangeFromBox(currentLine, paginationDelta);
1831 currentLine->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001832 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001833 if (layoutState.fragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00001834 updateFragmentForLine(currentLine);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001835 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001836
zalan@apple.com4ed97602016-10-12 16:45:55 +00001837 if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1838 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1839 bool encounteredNewFloat = false;
zalan@apple.com9201b992017-10-06 22:14:05 +00001840 for (auto& floatBoxOnCleanLine : *cleanLineFloats) {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001841 ASSERT(floatsIterator != end);
zalan@apple.com9201b992017-10-06 22:14:05 +00001842 if (!floatBoxOnCleanLine)
1843 continue;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001844 checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1845 ++floatsIterator;
1846 if (floatsIterator == end || encounteredNewFloat) {
1847 layoutState.markForFullLayout();
1848 break;
1849 }
1850 }
1851 if (dirtiedByFloat || encounteredNewFloat)
1852 break;
1853 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001854 }
1855 // Check if a new float has been inserted after the last known float.
zalan@apple.com280ebf62018-07-12 14:23:45 +00001856 if (floatsIterator != end) {
1857 if (!currentLine)
1858 layoutState.markForFullLayout();
1859 else {
1860 for (; floatsIterator != end; ++floatsIterator) {
1861 auto& floatWithRect = *floatsIterator;
1862 if (!floatWithRect->renderer().needsLayout())
1863 continue;
1864 layoutState.markForFullLayout();
1865 break;
1866 }
1867 }
1868 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001869 }
1870
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001871 if (layoutState.isFullLayout()) {
akling@apple.com31dd4f42013-10-30 22:27:59 +00001872 m_lineBoxes.deleteLineBoxTree();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001873 currentLine = nullptr;
akling@apple.comee3c8df2013-11-06 08:09:44 +00001874 ASSERT(!firstRootBox() && !lastRootBox());
eseidel789896f2005-11-27 22:52:09 +00001875 } else {
zalan@apple.com4ed97602016-10-12 16:45:55 +00001876 if (currentLine) {
hyatt0c3a9862004-02-23 21:26:26 +00001877 // We have a dirty line.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001878 if (RootInlineBox* prevRootBox = currentLine->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001879 // We have a previous line.
antti@apple.comae85e112017-08-31 23:27:02 +00001880 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak()
zalan@apple.com4ed97602016-10-12 16:45:55 +00001881 || !prevRootBox->lineBreakObj()
1882 || (is<RenderText>(*prevRootBox->lineBreakObj())
darin@apple.com5917cb12017-11-23 17:32:42 +00001883 && prevRootBox->lineBreakPos() >= downcast<RenderText>(*prevRootBox->lineBreakObj()).text().length()))) {
mjs9f78dd92007-02-12 04:06:07 +00001884 // The previous line didn't break cleanly or broke at a newline
1885 // that has been deleted, so treat it as dirty too.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001886 currentLine = prevRootBox;
cdumez@apple.com35094bd2014-10-07 19:33:53 +00001887 }
hyatt0c3a9862004-02-23 21:26:26 +00001888 }
hyatt0c3a9862004-02-23 21:26:26 +00001889 }
hyatt0c3a9862004-02-23 21:26:26 +00001890 // If we have no dirty lines, then last is just the last root box.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001891 lastLine = currentLine ? currentLine->prevRootBox() : lastRootBox();
hyatt0c3a9862004-02-23 21:26:26 +00001892 }
mitz@apple.come1364202008-02-28 01:06:41 +00001893
zalan@apple.com4ed97602016-10-12 16:45:55 +00001894 if (!floats.isEmpty()) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001895 LayoutUnit savedLogicalHeight = m_flow.logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001896 // Restore floats from clean lines.
1897 RootInlineBox* line = firstRootBox();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001898 while (line != currentLine) {
zalan@apple.com9201b992017-10-06 22:14:05 +00001899 if (auto* cleanLineFloats = line->floatsPtr()) {
1900 for (auto& floatingBox : *cleanLineFloats) {
1901 if (!floatingBox)
1902 continue;
antti@apple.com50b36fde2019-08-11 11:02:01 +00001903 auto* floatingObject = m_flow.insertFloatingObject(*floatingBox);
zalan@apple.com4a4a6d52016-04-06 20:03:58 +00001904 ASSERT_WITH_SECURITY_IMPLICATION(!floatingObject->originatingLine());
zalan@apple.comd2acead2017-09-20 22:03:06 +00001905 floatingObject->setOriginatingLine(*line);
antti@apple.com50b36fde2019-08-11 11:02:01 +00001906 m_flow.setLogicalHeight(m_flow.logicalTopForChild(*floatingBox) - m_flow.marginBeforeForChild(*floatingBox));
1907 m_flow.positionNewFloats();
zalan@apple.com4ed97602016-10-12 16:45:55 +00001908 floats.setLastCleanFloat(*floatingBox);
mitz@apple.com40547b32008-03-18 04:04:34 +00001909 }
1910 }
1911 line = line->nextRootBox();
1912 }
antti@apple.com50b36fde2019-08-11 11:02:01 +00001913 m_flow.setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001914 }
1915
zalan@apple.com4ed97602016-10-12 16:45:55 +00001916 layoutState.lineInfo().setFirstLine(!lastLine);
1917 layoutState.lineInfo().setPreviousLineBrokeCleanly(!lastLine || lastLine->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001918
zalan@apple.com4ed97602016-10-12 16:45:55 +00001919 if (lastLine) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00001920 m_flow.setLogicalHeight(lastLine->lineBottomWithLeading());
1921 InlineIterator iter = InlineIterator(&m_flow, lastLine->lineBreakObj(), lastLine->lineBreakPos());
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001922 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
zalan@apple.com4ed97602016-10-12 16:45:55 +00001923 resolver.setStatus(lastLine->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001924 } else {
akling@apple.com827be9c2013-10-29 02:58:43 +00001925 TextDirection direction = style().direction();
1926 if (style().unicodeBidi() == Plaintext)
antti@apple.com50b36fde2019-08-11 11:02:01 +00001927 determineDirectionality(direction, InlineIterator(&m_flow, bidiFirstSkippingEmptyInlines(m_flow), 0));
akling@apple.com827be9c2013-10-29 02:58:43 +00001928 resolver.setStatus(BidiStatus(direction, isOverride(style().unicodeBidi())));
antti@apple.com50b36fde2019-08-11 11:02:01 +00001929 InlineIterator iter = InlineIterator(&m_flow, bidiFirstSkippingEmptyInlines(m_flow, &resolver), 0);
rniwa@webkit.org53d106b2011-11-30 22:33:20 +00001930 resolver.setPosition(iter, numberOfIsolateAncestors(iter));
darindde01502005-12-18 22:55:35 +00001931 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001932 return currentLine;
hyatt0c3a9862004-02-23 21:26:26 +00001933}
1934
antti@apple.com50b36fde2019-08-11 11:02:01 +00001935void ComplexLineLayout::determineEndPosition(LineLayoutState& layoutState, RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus)
hyatt0c3a9862004-02-23 21:26:26 +00001936{
zalan@apple.com4ed97602016-10-12 16:45:55 +00001937 auto iteratorForFirstDirtyFloat = [](LineLayoutState::FloatList& floats) {
1938 auto lastCleanFloat = floats.lastCleanFloat();
1939 if (!lastCleanFloat)
1940 return floats.begin();
1941 auto* lastCleanFloatWithRect = floats.floatWithRect(*lastCleanFloat);
1942 ASSERT(lastCleanFloatWithRect);
1943 return ++floats.find(*lastCleanFloatWithRect);
1944 };
1945
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001946 ASSERT(!layoutState.endLine());
zalan@apple.com4ed97602016-10-12 16:45:55 +00001947 auto floatsIterator = iteratorForFirstDirtyFloat(layoutState.floatList());
1948 auto end = layoutState.floatList().end();
1949 RootInlineBox* lastLine = nullptr;
1950 for (RootInlineBox* currentLine = startLine->nextRootBox(); currentLine; currentLine = currentLine->nextRootBox()) {
1951 if (!currentLine->isDirty()) {
1952 if (auto* cleanLineFloats = currentLine->floatsPtr()) {
1953 bool encounteredNewFloat = false;
1954 bool dirtiedByFloat = false;
zalan@apple.com9201b992017-10-06 22:14:05 +00001955 for (auto& floatBoxOnCleanLine : *cleanLineFloats) {
1956 if (!floatBoxOnCleanLine)
1957 continue;
zalan@apple.com4ed97602016-10-12 16:45:55 +00001958 ASSERT(floatsIterator != end);
1959 checkFloatInCleanLine(*currentLine, *floatBoxOnCleanLine, *floatsIterator, encounteredNewFloat, dirtiedByFloat);
1960 ++floatsIterator;
1961 if (floatsIterator == end || encounteredNewFloat)
1962 return;
1963 }
1964 }
hyatt04420ca2004-07-16 00:05:42 +00001965 }
zalan@apple.com4ed97602016-10-12 16:45:55 +00001966 if (currentLine->isDirty())
1967 lastLine = nullptr;
1968 else if (!lastLine)
1969 lastLine = currentLine;
hyatt0c3a9862004-02-23 21:26:26 +00001970 }
mitz@apple.come1364202008-02-28 01:06:41 +00001971
zalan@apple.com4ed97602016-10-12 16:45:55 +00001972 if (!lastLine)
commit-queue@webkit.orgc979afb2011-08-02 18:07:32 +00001973 return;
mitz@apple.come1364202008-02-28 01:06:41 +00001974
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001975 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1976 // in the block.
zalan@apple.com4ed97602016-10-12 16:45:55 +00001977 RootInlineBox* previousLine = lastLine->prevRootBox();
antti@apple.com50b36fde2019-08-11 11:02:01 +00001978 cleanLineStart = InlineIterator(&m_flow, previousLine->lineBreakObj(), previousLine->lineBreakPos());
zalan@apple.com4ed97602016-10-12 16:45:55 +00001979 cleanLineBidiStatus = previousLine->lineBreakBidiStatus();
1980 layoutState.setEndLineLogicalTop(previousLine->lineBottomWithLeading());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001981
zalan@apple.com4ed97602016-10-12 16:45:55 +00001982 for (RootInlineBox* line = lastLine; line; line = line->nextRootBox()) {
1983 // Disconnect all line boxes from their render objects while preserving their connections to one another.
1984 line->extractLine();
1985 }
1986 layoutState.setEndLine(lastLine);
hyatt0c3a9862004-02-23 21:26:26 +00001987}
1988
antti@apple.com50b36fde2019-08-11 11:02:01 +00001989bool ComplexLineLayout::checkPaginationAndFloatsAtEndLine(LineLayoutState& layoutState)
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001990{
antti@apple.com50b36fde2019-08-11 11:02:01 +00001991 LayoutUnit lineDelta = m_flow.logicalHeight() - layoutState.endLineLogicalTop();
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001992
antti@apple.com50b36fde2019-08-11 11:02:01 +00001993 bool paginated = layoutContext().layoutState() && layoutContext().layoutState()->isPaginated();
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00001994 if (paginated && layoutState.fragmentedFlow()) {
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00001995 // Check all lines from here to the end, and see if the hypothetical new position for the lines will result
1996 // in a different available line width.
1997 for (RootInlineBox* lineBox = layoutState.endLine(); lineBox; lineBox = lineBox->nextRootBox()) {
1998 if (paginated) {
1999 // This isn't the real move we're going to do, so don't update the line box's pagination
2000 // strut yet.
2001 LayoutUnit oldPaginationStrut = lineBox->paginationStrut();
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002002 bool overflowsFragment;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002003 lineDelta -= oldPaginationStrut;
antti@apple.com50b36fde2019-08-11 11:02:01 +00002004 m_flow.adjustLinePositionForPagination(lineBox, lineDelta, overflowsFragment, layoutState.fragmentedFlow());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002005 lineBox->setPaginationStrut(oldPaginationStrut);
2006 }
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002007 if (lineWidthForPaginatedLineChanged(lineBox, lineDelta, layoutState.fragmentedFlow()))
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002008 return false;
2009 }
2010 }
2011
antti@apple.com50b36fde2019-08-11 11:02:01 +00002012 if (!lineDelta || !m_flow.floatingObjects())
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002013 return true;
2014
2015 // See if any floats end in the range along which we want to shift the lines vertically.
antti@apple.com50b36fde2019-08-11 11:02:01 +00002016 LayoutUnit logicalTop = std::min(m_flow.logicalHeight(), layoutState.endLineLogicalTop());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002017
2018 RootInlineBox* lastLine = layoutState.endLine();
2019 while (RootInlineBox* nextLine = lastLine->nextRootBox())
2020 lastLine = nextLine;
2021
leviw@chromium.org3957b452012-05-01 00:06:37 +00002022 LayoutUnit logicalBottom = lastLine->lineBottomWithLeading() + absoluteValue(lineDelta);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002023
antti@apple.com50b36fde2019-08-11 11:02:01 +00002024 const FloatingObjectSet& floatingObjectSet = m_flow.floatingObjects()->set();
commit-queue@webkit.org6ad70c22015-06-12 18:43:01 +00002025 auto end = floatingObjectSet.end();
2026 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002027 const auto& floatingObject = *it->get();
antti@apple.com50b36fde2019-08-11 11:02:01 +00002028 if (m_flow.logicalBottomForFloat(floatingObject) >= logicalTop && m_flow.logicalBottomForFloat(floatingObject) < logicalBottom)
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002029 return false;
2030 }
2031
2032 return true;
2033}
2034
antti@apple.com50b36fde2019-08-11 11:02:01 +00002035bool ComplexLineLayout::lineWidthForPaginatedLineChanged(RootInlineBox* rootBox, LayoutUnit lineDelta, RenderFragmentedFlow* fragmentedFlow) const
weinig@apple.com31324fd2013-10-28 19:22:51 +00002036{
hyatt@apple.com4e0bf862017-09-27 20:54:17 +00002037 if (!fragmentedFlow)
weinig@apple.com31324fd2013-10-28 19:22:51 +00002038 return false;
2039
antti@apple.com50b36fde2019-08-11 11:02:01 +00002040 RenderFragmentContainer* currentFragment = m_flow.fragmentAtBlockOffset(rootBox->lineTopWithLeading() + lineDelta);
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002041 // Just bail if the fragment didn't change.
2042 if (rootBox->containingFragment() == currentFragment)
weinig@apple.com31324fd2013-10-28 19:22:51 +00002043 return false;
antti@apple.com50b36fde2019-08-11 11:02:01 +00002044 return rootBox->paginatedLineWidth() != m_flow.availableLogicalWidthForContent(currentFragment);
weinig@apple.com31324fd2013-10-28 19:22:51 +00002045}
2046
antti@apple.com50b36fde2019-08-11 11:02:01 +00002047bool ComplexLineLayout::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus)
hyatt0c3a9862004-02-23 21:26:26 +00002048{
mitz@apple.com15035e62008-07-05 20:44:44 +00002049 if (resolver.position() == endLineStart) {
2050 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00002051 return false;
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002052 return checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002053 }
hyatt0c3a9862004-02-23 21:26:26 +00002054
mitz@apple.come1364202008-02-28 01:06:41 +00002055 // The first clean line doesn't match, but we can check a handful of following lines to try
2056 // to match back up.
cdumez@apple.com4bf983a2015-05-19 07:22:36 +00002057 static const int numLines = 8; // The # of lines we're willing to match against.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002058 RootInlineBox* originalEndLine = layoutState.endLine();
2059 RootInlineBox* line = originalEndLine;
mitz@apple.come1364202008-02-28 01:06:41 +00002060 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
antti@apple.comae85e112017-08-31 23:27:02 +00002061 if (line->lineBreakObj() == resolver.position().renderer() && line->lineBreakPos() == resolver.position().offset()) {
mitz@apple.come1364202008-02-28 01:06:41 +00002062 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00002063 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00002064 return false; // ...but the bidi state doesn't match.
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002065
2066 bool matched = false;
mitz@apple.come1364202008-02-28 01:06:41 +00002067 RootInlineBox* result = line->nextRootBox();
hyatt@apple.com1fb7d582011-09-23 20:25:11 +00002068 layoutState.setEndLine(result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002069 if (result) {
hyatt@apple.com7ce0d422011-08-30 16:57:37 +00002070 layoutState.setEndLineLogicalTop(line->lineBottomWithLeading());
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002071 matched = checkPaginationAndFloatsAtEndLine(layoutState);
mitz@apple.com40547b32008-03-18 04:04:34 +00002072 }
2073
mitz@apple.come1364202008-02-28 01:06:41 +00002074 // Now delete the lines that we failed to sync.
akling@apple.com31dd4f42013-10-30 22:27:59 +00002075 deleteLineRange(layoutState, originalEndLine, result);
hyatt@apple.coma10d30e2011-09-22 22:28:21 +00002076 return matched;
hyatt0c3a9862004-02-23 21:26:26 +00002077 }
2078 }
mitz@apple.come1364202008-02-28 01:06:41 +00002079
hyatt0c3a9862004-02-23 21:26:26 +00002080 return false;
2081}
2082
antti@apple.com50b36fde2019-08-11 11:02:01 +00002083void ComplexLineLayout::addOverflowFromInlineChildren()
bdashccffb432007-07-13 11:51:40 +00002084{
antti@apple.com50b36fde2019-08-11 11:02:01 +00002085 ASSERT(!m_flow.simpleLineLayout());
bdashccffb432007-07-13 11:51:40 +00002086
antti@apple.com50b36fde2019-08-11 11:02:01 +00002087 LayoutUnit endPadding = m_flow.hasOverflowClip() ? m_flow.paddingEnd() : 0_lu;
hyatt@apple.com592848f2010-12-06 20:03:43 +00002088 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
antti@apple.com50b36fde2019-08-11 11:02:01 +00002089 if (m_flow.hasOverflowClip() && !endPadding && m_flow.element() && m_flow.element()->isRootEditableElement() && style().isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002090 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002091 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00002092 m_flow.addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
2093 RenderFragmentContainer* fragment = m_flow.enclosingFragmentedFlow() ? curr->containingFragment() : nullptr;
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002094 if (fragment)
antti@apple.com50b36fde2019-08-11 11:02:01 +00002095 fragment->addLayoutOverflowForBox(&m_flow, curr->paddedLayoutOverflowRect(endPadding));
2096 if (!m_flow.hasOverflowClip()) {
zalan@apple.comd423bcc2016-02-06 23:07:54 +00002097 LayoutRect childVisualOverflowRect = curr->visualOverflowRect(curr->lineTop(), curr->lineBottom());
antti@apple.com50b36fde2019-08-11 11:02:01 +00002098 m_flow.addVisualOverflow(childVisualOverflowRect);
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002099 if (fragment)
antti@apple.com50b36fde2019-08-11 11:02:01 +00002100 fragment->addVisualOverflowForBox(&m_flow, childVisualOverflowRect);
abucur@adobe.com6585d012013-09-04 08:26:41 +00002101 }
hyattb4b20872004-10-20 21:34:01 +00002102 }
2103}
2104
antti@apple.com50b36fde2019-08-11 11:02:01 +00002105void ComplexLineLayout::deleteEllipsisLineBoxes()
hyatted77ad82004-06-15 07:21:23 +00002106{
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002107 TextAlignMode textAlign = style().textAlign();
akling@apple.com827be9c2013-10-29 02:58:43 +00002108 bool ltr = style().isLeftToRightDirection();
zalan@apple.com64761fe2016-03-02 21:42:22 +00002109 IndentTextOrNot shouldIndentText = IndentText;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002110 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
2111 if (curr->hasEllipsisBox()) {
2112 curr->clearTruncation();
2113
2114 // Shift the line back where it belongs if we cannot accomodate an ellipsis.
antti@apple.com50b36fde2019-08-11 11:02:01 +00002115 float logicalLeft = m_flow.logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
2116 float availableLogicalWidth = m_flow.logicalRightOffsetForLine(curr->lineTop(), DoNotIndentText) - logicalLeft;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002117 float totalLogicalWidth = curr->logicalWidth();
antti@apple.comc6a9c732019-08-12 15:31:34 +00002118 updateLogicalWidthForAlignment(m_flow, textAlign, curr, 0, logicalLeft, totalLogicalWidth, availableLogicalWidth, 0);
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002119
2120 if (ltr)
2121 curr->adjustLogicalPosition((logicalLeft - curr->logicalLeft()), 0);
2122 else
2123 curr->adjustLogicalPosition(-(curr->logicalLeft() - logicalLeft), 0);
2124 }
zalan@apple.com64761fe2016-03-02 21:42:22 +00002125 shouldIndentText = DoNotIndentText;
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002126 }
hyatted77ad82004-06-15 07:21:23 +00002127}
2128
antti@apple.com50b36fde2019-08-11 11:02:01 +00002129void ComplexLineLayout::checkLinesForTextOverflow()
hyatted77ad82004-06-15 07:21:23 +00002130{
2131 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002132 // 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 +00002133 const FontCascade& font = style().fontCascade();
darin@apple.com0ce67df2019-06-17 01:48:13 +00002134 static NeverDestroyed<AtomString> ellipsisStr(&horizontalEllipsis, 1);
antti@apple.com50b36fde2019-08-11 11:02:01 +00002135 const FontCascade& firstLineFont = m_flow.firstLineStyle().fontCascade();
2136 float firstLineEllipsisWidth = firstLineFont.width(m_flow.constructTextRun(&horizontalEllipsis, 1, m_flow.firstLineStyle()));
2137 float ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(m_flow.constructTextRun(&horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002138
2139 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
antti@apple.com50b36fde2019-08-11 11:02:01 +00002140 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
hyatted77ad82004-06-15 07:21:23 +00002141 // check the left edge of the line box to see if it is less
2142 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
akling@apple.com827be9c2013-10-29 02:58:43 +00002143 bool ltr = style().isLeftToRightDirection();
commit-queue@webkit.orgeea2d6a2018-05-25 01:42:36 +00002144 TextAlignMode textAlign = style().textAlign();
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002145 bool firstLine = true;
hyatted77ad82004-06-15 07:21:23 +00002146 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
zalan@apple.com64761fe2016-03-02 21:42:22 +00002147 IndentTextOrNot shouldIndentText = firstLine ? IndentText : DoNotIndentText;
antti@apple.com50b36fde2019-08-11 11:02:01 +00002148 LayoutUnit blockRightEdge = m_flow.logicalRightOffsetForLine(curr->lineTop(), shouldIndentText);
2149 LayoutUnit blockLeftEdge = m_flow.logicalLeftOffsetForLine(curr->lineTop(), shouldIndentText);
ross.kirsling@sony.com80414652019-05-21 01:36:11 +00002150 LayoutUnit lineBoxEdge { ltr ? curr->x() + curr->logicalWidth() : curr->x() };
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002151 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
antti@apple.com50b36fde2019-08-11 11:02:01 +00002152 // This line spills out of our box in the appropriate direction. Now we need to see if the line
2153 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
hyatted77ad82004-06-15 07:21:23 +00002154 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2155 // space.
ross.kirsling@sony.com80414652019-05-21 01:36:11 +00002156 LayoutUnit width { firstLine ? firstLineEllipsisWidth : ellipsisWidth };
2157 LayoutUnit blockEdge { ltr ? blockRightEdge : blockLeftEdge };
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002158 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width)) {
2159 float totalLogicalWidth = curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
2160
akling@apple.comf294cf02013-07-17 18:46:39 +00002161 float logicalLeft = 0; // We are only interested in the delta from the base position.
antti@apple.com50b36fde2019-08-11 11:02:01 +00002162 float truncatedWidth = m_flow.availableLogicalWidthForLine(curr->lineTop(), shouldIndentText);
antti@apple.comc6a9c732019-08-12 15:31:34 +00002163 updateLogicalWidthForAlignment(m_flow, textAlign, curr, nullptr, logicalLeft, totalLogicalWidth, truncatedWidth, 0);
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002164 if (ltr)
2165 curr->adjustLogicalPosition(logicalLeft, 0);
2166 else
2167 curr->adjustLogicalPosition(-(truncatedWidth - (logicalLeft + totalLogicalWidth)), 0);
2168 }
hyatted77ad82004-06-15 07:21:23 +00002169 }
benjamin@webkit.orgf68b1be2012-06-23 02:02:28 +00002170 firstLine = false;
hyatted77ad82004-06-15 07:21:23 +00002171 }
2172}
2173
antti@apple.com50b36fde2019-08-11 11:02:01 +00002174bool ComplexLineLayout::positionNewFloatOnLine(const FloatingObject& newFloat, FloatingObject* lastFloatFromPreviousLine, LineInfo& lineInfo, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002175{
antti@apple.com50b36fde2019-08-11 11:02:01 +00002176 if (!m_flow.positionNewFloats())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002177 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002178
rniwa@webkit.org44424752011-04-14 00:58:40 +00002179 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002180
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002181 // We only connect floats to lines for pagination purposes if the floats occur at the start of
2182 // the line and the previous line had a hard break (so this line is either the first in the block
2183 // or follows a <br>).
zalan@apple.com48ea2832015-10-16 19:53:04 +00002184 if (!newFloat.paginationStrut() || !lineInfo.previousLineBrokeCleanly() || !lineInfo.isEmpty())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002185 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002186
antti@apple.com50b36fde2019-08-11 11:02:01 +00002187 const FloatingObjectSet& floatingObjectSet = m_flow.floatingObjects()->set();
zalan@apple.com48ea2832015-10-16 19:53:04 +00002188 ASSERT(floatingObjectSet.last().get() == &newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002189
antti@apple.com50b36fde2019-08-11 11:02:01 +00002190 LayoutUnit floatLogicalTop = m_flow.logicalTopForFloat(newFloat);
zalan@apple.com48ea2832015-10-16 19:53:04 +00002191 LayoutUnit paginationStrut = newFloat.paginationStrut();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002192
antti@apple.com50b36fde2019-08-11 11:02:01 +00002193 if (floatLogicalTop - paginationStrut != m_flow.logicalHeight() + lineInfo.floatPaginationStrut())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002194 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002195
darin@apple.com7cad7042013-09-24 05:53:55 +00002196 auto it = floatingObjectSet.end();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002197 --it; // Last float is newFloat, skip that one.
darin@apple.com7cad7042013-09-24 05:53:55 +00002198 auto begin = floatingObjectSet.begin();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002199 while (it != begin) {
2200 --it;
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002201 auto& floatingObject = *it->get();
2202 if (&floatingObject == lastFloatFromPreviousLine)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002203 break;
antti@apple.com50b36fde2019-08-11 11:02:01 +00002204 if (m_flow.logicalTopForFloat(floatingObject) == m_flow.logicalHeight() + lineInfo.floatPaginationStrut()) {
zalan@apple.com84ccfa12015-10-17 03:36:56 +00002205 floatingObject.setPaginationStrut(paginationStrut + floatingObject.paginationStrut());
2206 RenderBox& floatBox = floatingObject.renderer();
antti@apple.com50b36fde2019-08-11 11:02:01 +00002207 m_flow.setLogicalTopForChild(floatBox, m_flow.logicalTopForChild(floatBox) + m_flow.marginBeforeForChild(floatBox) + paginationStrut);
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002208
antti@apple.com50b36fde2019-08-11 11:02:01 +00002209 if (m_flow.updateFragmentRangeForBoxChild(floatBox))
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002210 floatBox.setNeedsLayout(MarkOnlyThis);
cdumez@apple.come9437792014-10-08 23:33:43 +00002211 else if (is<RenderBlock>(floatBox))
2212 downcast<RenderBlock>(floatBox).setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002213 floatBox.layoutIfNeeded();
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00002214
hyatt@apple.com46c65b32011-08-09 19:13:45 +00002215 // Save the old logical top before calling removePlacedObject which will set
2216 // isPlaced to false. Otherwise it will trigger an assert in logicalTopForFloat.
antti@apple.com50b36fde2019-08-11 11:02:01 +00002217 LayoutUnit oldLogicalTop = m_flow.logicalTopForFloat(floatingObject);
2218 m_flow.floatingObjects()->removePlacedObject(&floatingObject);
2219 m_flow.setLogicalTopForFloat(floatingObject, oldLogicalTop + paginationStrut);
2220 m_flow.floatingObjects()->addPlacedObject(&floatingObject);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002221 }
2222 }
2223
hyatt@apple.comdd78df82011-09-27 22:11:41 +00002224 // Just update the line info's pagination strut without altering our logical height yet. If the line ends up containing
2225 // no content, then we don't want to improperly grow the height of the block.
2226 lineInfo.setFloatPaginationStrut(lineInfo.floatPaginationStrut() + paginationStrut);
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002227 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002228}
2229
antti@apple.com50b36fde2019-08-11 11:02:01 +00002230void ComplexLineLayout::updateFragmentForLine(RootInlineBox* lineBox) const
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002231{
2232 ASSERT(lineBox);
akling@apple.combdf247b2014-01-13 22:33:08 +00002233
antti@apple.com50b36fde2019-08-11 11:02:01 +00002234 if (!m_flow.hasFragmentRangeInFragmentedFlow())
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002235 lineBox->clearContainingFragment();
abucur@adobe.comc18af352014-05-14 06:33:48 +00002236 else {
antti@apple.com50b36fde2019-08-11 11:02:01 +00002237 if (auto containingFragment = m_flow.fragmentAtBlockOffset(lineBox->lineTopWithLeading()))
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002238 lineBox->setContainingFragment(*containingFragment);
abucur@adobe.comc18af352014-05-14 06:33:48 +00002239 else
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002240 lineBox->clearContainingFragment();
abucur@adobe.comc18af352014-05-14 06:33:48 +00002241 }
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002242
2243 RootInlineBox* prevLineBox = lineBox->prevRootBox();
2244 if (!prevLineBox)
2245 return;
2246
2247 // This check is more accurate than the one in |adjustLinePositionForPagination| because it takes into
2248 // account just the container changes between lines. The before mentioned function doesn't set the flag
2249 // correctly if the line is positioned at the top of the last fragment container.
hyatt@apple.come0d2e0f2017-09-27 16:19:08 +00002250 if (lineBox->containingFragment() != prevLineBox->containingFragment())
abucur@adobe.comd40287b2013-10-08 17:33:05 +00002251 lineBox->setIsFirstAfterPageBreak(true);
2252}
2253
antti@apple.com50b36fde2019-08-11 11:02:01 +00002254const RenderStyle& ComplexLineLayout::style() const
2255{
2256 return m_flow.style();
2257}
2258
2259const FrameViewLayoutContext& ComplexLineLayout::layoutContext() const
2260{
2261 return m_flow.view().frameView().layoutContext();
2262}
2263
2264
hyattffe78712003-02-11 01:59:29 +00002265}