blob: a92c6c7d11c67d6823bed7a73e2c1a6816c72464 [file] [log] [blame]
darin55ae73e2007-05-11 15:47:28 +00001/*
kociendabb0c24b2001-08-24 14:24:40 +00002 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
mitz@apple.com86470c82011-01-27 01:39:27 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All right reserved.
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +00004 * Copyright (C) 2010 Google Inc. All rights reserved.
kociendabb0c24b2001-08-24 14:24:40 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000020 *
kociendabb0c24b2001-08-24 14:24:40 +000021 */
darinbe4c67d2005-12-19 19:53:12 +000022
mjsb64c50a2005-10-03 21:13:12 +000023#include "config.h"
darin36d11362006-04-11 16:30:21 +000024
mitz@apple.com4c1ff322009-07-13 00:54:12 +000025#include "BidiResolver.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000026#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000027#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000028#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000029#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000030#include "RenderArena.h"
hyatt@apple.com4d046b72011-01-31 20:39:09 +000031#include "RenderCombineText.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000032#include "RenderInline.h"
eric@webkit.orgb35848a2010-06-10 08:33:22 +000033#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000034#include "RenderListMarker.h"
mitz@apple.comddb59872011-04-05 05:21:16 +000035#include "RenderRubyRun.h"
hyattd8048342006-05-31 01:48:18 +000036#include "RenderView.h"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000037#include "Settings.h"
mitz@apple.com44fc5132011-02-25 18:34:15 +000038#include "TextBreakIterator.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000039#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000040#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000041#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000042#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000043#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000044#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000045#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000046#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000047
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000048#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000049#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000050#include "SVGRootInlineBox.h"
51#endif
52
darin7bd70952006-04-13 07:07:34 +000053using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000054using namespace WTF;
55using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000056
darinb9481ed2006-03-20 02:57:59 +000057namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000058
hyatt1d5d87b2007-04-24 04:55:54 +000059// We don't let our line box tree for a single line get any deeper than this.
60const unsigned cMaxLineDepth = 200;
eric@webkit.org060caf62011-05-03 22:11:39 +000061
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +000062class LineInfo {
63public:
64 LineInfo()
65 : m_isFirstLine(true)
66 , m_isLastLine(false)
67 , m_isEmpty(true)
68 , m_previousLineBrokeCleanly(true)
69 { }
70
71 bool isFirstLine() const { return m_isFirstLine; }
72 bool isLastLine() const { return m_isLastLine; }
73 bool isEmpty() const { return m_isEmpty; }
74 bool previousLineBrokeCleanly() const { return m_previousLineBrokeCleanly; }
75
76 void setFirstLine(bool firstLine) { m_isFirstLine = firstLine; }
77 void setLastLine(bool lastLine) { m_isLastLine = lastLine; }
78 void setEmpty(bool empty) { m_isEmpty = empty; }
79 void setPreviousLineBrokeCleanly(bool previousLineBrokeCleanly) { m_previousLineBrokeCleanly = previousLineBrokeCleanly; }
80
81private:
82 bool m_isFirstLine;
83 bool m_isLastLine;
84 bool m_isEmpty;
85 bool m_previousLineBrokeCleanly;
86};
hyatt1d5d87b2007-04-24 04:55:54 +000087
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000088static inline int borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +000089{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +000090 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +000091}
92
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000093static inline int borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +000094{
95 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
96}
97
hyatt@apple.com0415e5d2010-10-07 17:40:25 +000098static int inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +000099{
hyatt1d5d87b2007-04-24 04:55:54 +0000100 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +0000101 int extraWidth = 0;
102 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000103 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
104 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000105 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000106 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +0000107 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +0000108 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +0000109 child = parent;
110 parent = child->parent();
111 }
112 return extraWidth;
113}
114
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000115static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000116{
117 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000118 // shave it off the list, and shave off a trailing space if the previous end point doesn't
119 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +0000120 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000121 InlineIterator* midpoints = lineMidpointState.midpoints.data();
122 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
123 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000124 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000125 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000126 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000127 if (currpoint == lBreak) {
128 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000129 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000130 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000131 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000132 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000133 }
hyattfe99c872003-07-31 22:25:29 +0000134}
135
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000136static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000137{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000138 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
139 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000140
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000141 InlineIterator* midpoints = lineMidpointState.midpoints.data();
142 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000143}
144
eric@webkit.org5bee2942011-04-08 02:12:31 +0000145static inline BidiRun* createRun(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
146{
147 return new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir());
148}
149
150void RenderBlock::appendRunsForObject(BidiRunList<BidiRun>& runs, int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000151{
hyatt98ee7e42003-05-14 01:39:15 +0000152 if (start > end || obj->isFloating() ||
hyatt@apple.comaa956102011-04-12 20:31:02 +0000153 (obj->isPositioned() && !obj->style()->isOriginalDisplayInlineType() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000154 return;
hyatt85586af2003-02-19 23:22:42 +0000155
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000156 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000157 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000158 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000159 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000160 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
161 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000162 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000163 return;
eric@webkit.org060caf62011-05-03 22:11:39 +0000164 // This is a new start point. Stop ignoring objects and
hyatt33f8d492002-11-12 21:44:52 +0000165 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000166 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000167 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000168 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000169 if (start < end)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000170 return appendRunsForObject(runs, start, end, obj, resolver);
mitz@apple.com15035e62008-07-05 20:44:44 +0000171 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000172 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000173 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000174 return;
175 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000176
hyatt78b85132004-03-29 20:07:45 +0000177 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000178 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000179 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000180 lineMidpointState.betweenMidpoints = true;
181 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000182 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
183 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000184 runs.addRun(createRun(start, nextMidpoint.m_pos + 1, obj, resolver));
185 return appendRunsForObject(runs, nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000186 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000187 } else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000188 runs.addRun(createRun(start, end, obj, resolver));
hyatt33f8d492002-11-12 21:44:52 +0000189 }
190}
191
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000192static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
193{
194 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000195 return toRenderBlock(obj)->createAndAppendRootInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000196
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000197 if (obj->isText()) {
198 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
199 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
200 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
201 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000202 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000203 return textBox;
204 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000205
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000206 if (obj->isBox())
207 return toRenderBox(obj)->createInlineBox();
eric@webkit.org060caf62011-05-03 22:11:39 +0000208
eric@webkit.org49b9d952009-07-03 01:29:07 +0000209 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000210}
211
212static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
213{
214 if (o->isText()) {
carol.szabo@nokia.comeaaf1992011-03-02 19:34:00 +0000215 if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000216 toRenderText(o)->computePreferredLogicalWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000217 toRenderText(o)->dirtyLineBoxes(fullLayout);
218 } else
219 toRenderInline(o)->dirtyLineBoxes(fullLayout);
220}
221
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000222static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
223{
224 do {
225 if (parentBox->isConstructed() || parentBox->nextOnLine())
226 return true;
227 parentBox = parentBox->parent();
228 } while (parentBox);
229 return false;
230}
231
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000232InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, const LineInfo& lineInfo, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000233{
234 // See if we have an unconstructed line box for this object that is also
235 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000236 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000237 InlineFlowBox* parentBox = 0;
238 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000239 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000240 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000241 ASSERT(obj->isRenderInline() || obj == this);
eric@webkit.org060caf62011-05-03 22:11:39 +0000242
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000243 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
244
hyatt1d5d87b2007-04-24 04:55:54 +0000245 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000246 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000247
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000248 // If this box or its ancestor is constructed then it is from a previous line, and we need
249 // 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 +0000250 // something following it on the line, then we know we have to make a new box
251 // as well. In this situation our inline has actually been split in two on
252 // the same line (this can happen with very fancy language mixtures).
253 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000254 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
255 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
256 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000257 // We need to make a new box for this render object. Once
258 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000259 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000260 ASSERT(newBox->isInlineFlowBox());
261 parentBox = static_cast<InlineFlowBox*>(newBox);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000262 parentBox->setFirstLineStyleBit(lineInfo.isFirstLine());
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000263 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000264 if (!hasDefaultLineBoxContain)
265 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000266 constructedNewBox = true;
267 }
mitz@apple.come1364202008-02-28 01:06:41 +0000268
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000269 if (constructedNewBox || canUseExistingParentBox) {
270 if (!result)
271 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000272
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000273 // If we have hit the block itself, then |box| represents the root
274 // inline box for the line, and it doesn't have to be appended to any parent
275 // inline.
276 if (childBox)
277 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000278
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000279 if (!constructedNewBox || obj == this)
280 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000281
eric@webkit.org060caf62011-05-03 22:11:39 +0000282 childBox = parentBox;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000283 }
mitz@apple.come1364202008-02-28 01:06:41 +0000284
hyatt1d5d87b2007-04-24 04:55:54 +0000285 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
286 // intermediate inline flows.
287 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000288
hyatt1d5d87b2007-04-24 04:55:54 +0000289 } while (true);
290
291 return result;
hyattffe78712003-02-11 01:59:29 +0000292}
293
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000294static bool reachedEndOfTextRenderer(const BidiRunList<BidiRun>& bidiRuns)
hyattffe78712003-02-11 01:59:29 +0000295{
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000296 BidiRun* run = bidiRuns.logicallyLastRun();
297 if (!run)
298 return true;
299 unsigned int pos = run->stop();
300 RenderObject* r = run->m_object;
301 if (!r->isText() || r->isBR())
302 return false;
303 RenderText* renderText = toRenderText(r);
304 if (pos >= renderText->textLength())
305 return true;
306
307 while (isASCIISpace(renderText->characters()[pos])) {
308 pos++;
309 if (pos >= renderText->textLength())
310 return true;
311 }
312 return false;
313}
314
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000315RootInlineBox* RenderBlock::constructLine(BidiRunList<BidiRun>& bidiRuns, const LineInfo& lineInfo)
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000316{
317 ASSERT(bidiRuns.firstRun());
hyattffe78712003-02-11 01:59:29 +0000318
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000319 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000320 InlineFlowBox* parentBox = 0;
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000321 for (BidiRun* r = bidiRuns.firstRun(); r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000322 // Create a box for our object.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000323 bool isOnlyRun = (bidiRuns.runCount() == 1);
324 if (bidiRuns.runCount() == 2 && !r->m_object->isListMarker())
325 isOnlyRun = (!style()->isLeftToRightDirection() ? bidiRuns.lastRun() : bidiRuns.firstRun())->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000326
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000327 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000328 r->m_box = box;
329
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000330 ASSERT(box);
331 if (!box)
332 continue;
hyattffe78712003-02-11 01:59:29 +0000333
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000334 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
335 rootHasSelectedChildren = true;
336
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000337 // If we have no parent box yet, or if the run is not simply a sibling,
338 // then we need to construct inline boxes as necessary to properly enclose the
339 // run's inline box.
340 if (!parentBox || parentBox->renderer() != r->m_object->parent())
341 // Create new inline boxes all the way back to the appropriate insertion point.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000342 parentBox = createLineBoxes(r->m_object->parent(), lineInfo, box);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000343 else {
344 // Append the inline box to this line.
345 parentBox->addToLine(box);
346 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000347
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000348 bool visuallyOrdered = r->m_object->style()->rtlOrdering() == VisualOrder;
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000349 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000350
351 if (box->isInlineTextBox()) {
352 InlineTextBox* text = static_cast<InlineTextBox*>(box);
353 text->setStart(r->m_start);
354 text->setLen(r->m_stop - r->m_start);
355 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000356 if (r->m_hasHyphen)
357 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000358 }
hyattffe78712003-02-11 01:59:29 +0000359 }
360
361 // We should have a root inline box. It should be unconstructed and
362 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000363 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000364
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000365 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
366 // from the bidi runs walk above has a selection state.
367 if (rootHasSelectedChildren)
368 lastLineBox()->root()->setHasSelectedChildren(true);
369
hyattffe78712003-02-11 01:59:29 +0000370 // Set bits on our inline flow boxes that indicate which sides should
371 // paint borders/margins/padding. This knowledge will ultimately be used when
372 // we determine the horizontal positions and widths of all the inline boxes on
373 // the line.
yael.aharon@nokia.com15c605d2011-04-14 05:35:54 +0000374 bool isLogicallyLastRunWrapped = bidiRuns.logicallyLastRun()->m_object && bidiRuns.logicallyLastRun()->m_object->isText() ? !reachedEndOfTextRenderer(bidiRuns) : true;
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000375 lastLineBox()->determineSpacingForFlowBoxes(lineInfo.isLastLine(), isLogicallyLastRunWrapped, bidiRuns.logicallyLastRun()->m_object);
hyattffe78712003-02-11 01:59:29 +0000376
377 // Now mark the line boxes as being constructed.
378 lastLineBox()->setConstructed();
379
380 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000381 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000382}
383
mitz@apple.com390fa322011-02-24 23:07:06 +0000384ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
385{
386 ETextAlign alignment = style()->textAlign();
387 if (!endsWithSoftBreak && alignment == JUSTIFY)
388 alignment = TAAUTO;
389
390 return alignment;
391}
392
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000393static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
394{
395 // The direction of the block should determine what happens with wide lines.
396 // In particular with RTL blocks, wide lines should still spill out to the left.
397 if (isLeftToRightDirection) {
398 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
399 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
400 return;
401 }
402
403 if (trailingSpaceRun)
404 trailingSpaceRun->m_box->setLogicalWidth(0);
405 else if (totalLogicalWidth > availableLogicalWidth)
406 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
407}
408
409static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
410{
411 // Wide lines spill out of the block based off direction.
412 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
413 // side of the block.
414 if (isLeftToRightDirection) {
415 if (trailingSpaceRun) {
416 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
417 trailingSpaceRun->m_box->setLogicalWidth(0);
418 }
419 if (totalLogicalWidth < availableLogicalWidth)
420 logicalLeft += availableLogicalWidth - totalLogicalWidth;
421 return;
422 }
423
424 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
425 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
426 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
427 } else
428 logicalLeft += availableLogicalWidth - totalLogicalWidth;
429}
430
431static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
432{
433 float trailingSpaceWidth = 0;
434 if (trailingSpaceRun) {
435 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
436 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
437 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
438 }
439 if (isLeftToRightDirection)
440 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
441 else
442 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
443}
444
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000445void RenderBlock::setMarginsForRubyRun(BidiRun* run, RenderRubyRun* renderer, RenderObject* previousObject, const LineInfo& lineInfo)
446{
447 int startOverhang;
448 int endOverhang;
449 RenderObject* nextObject = 0;
450 for (BidiRun* runWithNextObject = run->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
451 if (!runWithNextObject->m_object->isPositioned() && !runWithNextObject->m_box->isLineBreak()) {
452 nextObject = runWithNextObject->m_object;
453 break;
454 }
455 }
456 renderer->getOverhang(lineInfo.isFirstLine(), renderer->style()->isLeftToRightDirection() ? previousObject : nextObject, renderer->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
457 setMarginStartForChild(renderer, -startOverhang);
458 setMarginEndForChild(renderer, -endOverhang);
459}
460
461static inline void setLogicalWidthForTextRun(RootInlineBox* lineBox, BidiRun* run, RenderText* renderer, float xPos, const LineInfo& lineInfo,
462 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
463{
464 HashSet<const SimpleFontData*> fallbackFonts;
465 GlyphOverflow glyphOverflow;
466
467 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
468 if (lineBox->fitsToGlyphs()) {
469 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
470 // will keep us from computing glyph bounds in nearly all cases.
471 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
472 int baselineShift = lineBox->verticalPositionForBox(run->m_box, verticalPositionCache);
473 int rootDescent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().descent() : 0;
474 int rootAscent = includeRootLine ? lineBox->renderer()->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() : 0;
475 int boxAscent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().ascent() - baselineShift;
476 int boxDescent = renderer->style(lineInfo.isFirstLine())->font().fontMetrics().descent() + baselineShift;
477 if (boxAscent > rootDescent || boxDescent > rootAscent)
478 glyphOverflow.computeBounds = true;
479 }
480
481 int hyphenWidth = 0;
482 if (static_cast<InlineTextBox*>(run->m_box)->hasHyphen()) {
483 const AtomicString& hyphenString = renderer->style()->hyphenString();
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +0000484 const Font& font = renderer->style(lineInfo.isFirstLine())->font();
485 hyphenWidth = font.width(RenderBlock::constructTextRun(renderer, font, hyphenString.string(), renderer->style()));
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000486 }
487 run->m_box->setLogicalWidth(renderer->width(run->m_start, run->m_stop - run->m_start, xPos, lineInfo.isFirstLine(), &fallbackFonts, &glyphOverflow) + hyphenWidth);
488 if (!fallbackFonts.isEmpty()) {
489 ASSERT(run->m_box->isText());
490 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
491 ASSERT(it->second.first.isEmpty());
492 copyToVector(fallbackFonts, it->second.first);
493 run->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
494 }
495 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
496 ASSERT(run->m_box->isText());
497 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(run->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
498 it->second.second = glyphOverflow;
499 run->m_box->clearKnownToHaveNoOverflow();
500 }
501}
502
503static inline void computeExpansionForJustifiedText(BidiRun* firstRun, BidiRun* trailingSpaceRun, Vector<unsigned, 16>& expansionOpportunities, unsigned expansionOpportunityCount, float& totalLogicalWidth, float availableLogicalWidth)
504{
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000505 if (!expansionOpportunityCount || availableLogicalWidth <= totalLogicalWidth)
506 return;
507
508 size_t i = 0;
509 for (BidiRun* r = firstRun; r; r = r->next()) {
510 if (!r->m_box || r == trailingSpaceRun)
511 continue;
512
513 if (r->m_object->isText()) {
514 unsigned opportunitiesInRun = expansionOpportunities[i++];
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000515
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000516 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
517
518 // Only justify text if whitespace is collapsed.
519 if (r->m_object->style()->collapseWhiteSpace()) {
520 InlineTextBox* textBox = static_cast<InlineTextBox*>(r->m_box);
521 int expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
522 textBox->setExpansion(expansion);
523 totalLogicalWidth += expansion;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000524 }
mitz@apple.comc7a1a512011-05-08 16:27:31 +0000525 expansionOpportunityCount -= opportunitiesInRun;
526 if (!expansionOpportunityCount)
527 break;
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000528 }
529 }
530}
531
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000532void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, const LineInfo& lineInfo, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000533 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000534{
mitz@apple.com390fa322011-02-24 23:07:06 +0000535 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000536 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), lineInfo.isFirstLine());
537 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), lineInfo.isFirstLine()) - logicalLeft;
mitz@apple.com390fa322011-02-24 23:07:06 +0000538
darin06dcb9c2005-08-15 04:31:09 +0000539 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000540 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000541 unsigned expansionOpportunityCount = 0;
542 bool isAfterExpansion = true;
543 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000544 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000545
mitz@apple.come1364202008-02-28 01:06:41 +0000546 for (BidiRun* r = firstRun; r; r = r->next()) {
547 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000548 continue; // Positioned objects are only participating to figure out their
549 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000550 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000551 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000552 RenderText* rt = toRenderText(r->m_object);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000553 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000554 if (!isAfterExpansion)
555 static_cast<InlineTextBox*>(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000556 unsigned opportunitiesInRun = Font::expansionOpportunityCount(rt->characters() + r->m_start, r->m_stop - r->m_start, r->m_box->direction(), isAfterExpansion);
mitz@apple.com86470c82011-01-27 01:39:27 +0000557 expansionOpportunities.append(opportunitiesInRun);
558 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000559 }
560
mitz@apple.come1364202008-02-28 01:06:41 +0000561 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000562 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +0000563 totalLogicalWidth += rt->style(lineInfo.isFirstLine())->font().wordSpacing();
eric@webkit.org060caf62011-05-03 22:11:39 +0000564 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000565 }
eric@webkit.org060caf62011-05-03 22:11:39 +0000566
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000567 setLogicalWidthForTextRun(lineBox, r, rt, totalLogicalWidth, lineInfo, textBoxDataMap, verticalPositionCache);
mitz@apple.com86470c82011-01-27 01:39:27 +0000568 } else {
569 isAfterExpansion = false;
570 if (!r->m_object->isRenderInline()) {
571 RenderBox* renderBox = toRenderBox(r->m_object);
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000572 if (renderBox->isRubyRun())
573 setMarginsForRubyRun(r, toRenderRubyRun(renderBox), previousObject, lineInfo);
mitz@apple.com86470c82011-01-27 01:39:27 +0000574 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
575 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
576 }
hyattffe78712003-02-11 01:59:29 +0000577 }
hyatt4b381692003-03-10 21:11:59 +0000578
hyatt@apple.com546a2482010-10-07 21:16:49 +0000579 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000580 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000581 }
582
mitz@apple.com86470c82011-01-27 01:39:27 +0000583 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
584 expansionOpportunities.last()--;
585 expansionOpportunityCount--;
586 }
587
hyattffe78712003-02-11 01:59:29 +0000588 // Armed with the total width of the line (without justification),
589 // we now examine our text-align property in order to determine where to position the
590 // objects horizontally. The total width of the line can be increased if we end up
591 // justifying text.
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000592 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000593 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000594 case WEBKIT_LEFT:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000595 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000596 break;
597 case JUSTIFY:
mitz@apple.coma70af822011-02-25 02:21:20 +0000598 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
mitz@apple.com390fa322011-02-24 23:07:06 +0000599 if (expansionOpportunityCount) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000600 if (trailingSpaceRun) {
hyatt@apple.com546a2482010-10-07 21:16:49 +0000601 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000602 trailingSpaceRun->m_box->setLogicalWidth(0);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000603 }
hyattffe78712003-02-11 01:59:29 +0000604 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000605 }
hyattffe78712003-02-11 01:59:29 +0000606 // fall through
607 case TAAUTO:
hyattffe78712003-02-11 01:59:29 +0000608 // for right to left fall through to right aligned
hyatt@apple.comc0fa1632010-09-30 20:01:33 +0000609 if (style()->isLeftToRightDirection()) {
hyatt@apple.com546a2482010-10-07 21:16:49 +0000610 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
hyatt@apple.com0acc9352011-02-17 19:19:07 +0000611 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
hyattffe78712003-02-11 01:59:29 +0000612 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000613 }
hyattffe78712003-02-11 01:59:29 +0000614 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000615 case WEBKIT_RIGHT:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000616 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000617 break;
618 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000619 case WEBKIT_CENTER:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000620 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000621 break;
rniwa@webkit.org0b6f81f2011-03-28 12:04:56 +0000622 case TASTART:
623 if (style()->isLeftToRightDirection())
624 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
625 else
626 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
627 break;
628 case TAEND:
629 if (style()->isLeftToRightDirection())
630 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
631 else
632 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
633 break;
hyattffe78712003-02-11 01:59:29 +0000634 }
635
leviw@chromium.orgd70a0072011-05-03 23:28:11 +0000636 computeExpansionForJustifiedText(firstRun, trailingSpaceRun, expansionOpportunities, expansionOpportunityCount, totalLogicalWidth, availableLogicalWidth);
mitz@apple.come1364202008-02-28 01:06:41 +0000637
hyattffe78712003-02-11 01:59:29 +0000638 // The widths of all runs are now known. We can now place every inline box (and
639 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000640 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000641 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000642}
643
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000644void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
645 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000646{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000647 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000648 lineBox->setBlockLogicalHeight(logicalHeight());
hyattffe78712003-02-11 01:59:29 +0000649
650 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000651 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000652 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000653 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000654 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000655
hyatt98ee7e42003-05-14 01:39:15 +0000656 // Align positioned boxes with the top of the line box. This is
657 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000658 if (r->m_object->isPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000659 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000660
661 // Position is used to properly position both replaced elements and
662 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000663 if (r->m_object->isText())
664 toRenderText(r->m_object)->positionLineBox(r->m_box);
665 else if (r->m_object->isBox())
666 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000667 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000668 // Positioned objects and zero-length text nodes destroy their boxes in
669 // position(), which unnecessarily dirties the line.
670 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000671}
kociendabb0c24b2001-08-24 14:24:40 +0000672
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000673static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
674{
675 if (character == ' ' || character == '\t' || character == softHyphen)
676 return true;
677 if (character == '\n')
678 return !renderer->style()->preserveNewline();
679 if (character == noBreakSpace)
680 return renderer->style()->nbspMode() == SPACE;
681 return false;
682}
683
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000684
685static void setStaticPositions(RenderBlock* block, RenderBox* child)
686{
687 // FIXME: The math here is actually not really right. It's a best-guess approximation that
688 // will work for the common cases
689 RenderObject* containerBlock = child->container();
690 int blockHeight = block->logicalHeight();
691 if (containerBlock->isRenderInline()) {
692 // A relative positioned inline encloses us. In this case, we also have to determine our
693 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
694 // inline so that we can obtain the value later.
695 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
696 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
697 }
698
699 if (child->style()->isOriginalDisplayInlineType())
700 child->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
701 else
702 child->layer()->setStaticInlinePosition(block->borderAndPaddingStart());
703 child->layer()->setStaticBlockPosition(blockHeight);
704}
705
eric@webkit.org5bee2942011-04-08 02:12:31 +0000706inline BidiRun* RenderBlock::handleTrailingSpaces(BidiRunList<BidiRun>& bidiRuns, BidiContext* currentContext)
eric@webkit.org0894bb82011-04-03 08:29:40 +0000707{
eric@webkit.org5bee2942011-04-08 02:12:31 +0000708 if (!bidiRuns.runCount()
709 || !bidiRuns.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
710 || !bidiRuns.logicallyLastRun()->m_object->style()->autoWrap())
eric@webkit.org0894bb82011-04-03 08:29:40 +0000711 return 0;
712
eric@webkit.org5bee2942011-04-08 02:12:31 +0000713 BidiRun* trailingSpaceRun = bidiRuns.logicallyLastRun();
eric@webkit.org0894bb82011-04-03 08:29:40 +0000714 RenderObject* lastObject = trailingSpaceRun->m_object;
715 if (!lastObject->isText())
716 return 0;
717
718 RenderText* lastText = toRenderText(lastObject);
719 const UChar* characters = lastText->characters();
720 int firstSpace = trailingSpaceRun->stop();
721 while (firstSpace > trailingSpaceRun->start()) {
722 UChar current = characters[firstSpace - 1];
723 if (!isCollapsibleSpace(current, lastText))
724 break;
725 firstSpace--;
726 }
727 if (firstSpace == trailingSpaceRun->stop())
728 return 0;
729
730 TextDirection direction = style()->direction();
eric@webkit.org5bee2942011-04-08 02:12:31 +0000731 bool shouldReorder = trailingSpaceRun != (direction == LTR ? bidiRuns.lastRun() : bidiRuns.firstRun());
eric@webkit.org0894bb82011-04-03 08:29:40 +0000732 if (firstSpace != trailingSpaceRun->start()) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000733 BidiContext* baseContext = currentContext;
eric@webkit.org0894bb82011-04-03 08:29:40 +0000734 while (BidiContext* parent = baseContext->parent())
735 baseContext = parent;
736
737 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
738 trailingSpaceRun->m_stop = firstSpace;
739 if (direction == LTR)
eric@webkit.org5bee2942011-04-08 02:12:31 +0000740 bidiRuns.addRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000741 else
eric@webkit.org5bee2942011-04-08 02:12:31 +0000742 bidiRuns.prependRun(newTrailingRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000743 trailingSpaceRun = newTrailingRun;
744 return trailingSpaceRun;
745 }
746 if (!shouldReorder)
747 return trailingSpaceRun;
748
749 if (direction == LTR) {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000750 bidiRuns.moveRunToEnd(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000751 trailingSpaceRun->m_level = 0;
752 } else {
eric@webkit.org5bee2942011-04-08 02:12:31 +0000753 bidiRuns.moveRunToBeginning(trailingSpaceRun);
eric@webkit.org0894bb82011-04-03 08:29:40 +0000754 trailingSpaceRun->m_level = 1;
755 }
756 return trailingSpaceRun;
757}
758
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000759void RenderBlock::appendFloatingObjectToLastLine(FloatingObject* floatingObject)
760{
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +0000761 ASSERT(!floatingObject->m_originatingLine);
762 floatingObject->m_originatingLine = lastRootBox();
mitz@apple.comd17e8c02011-04-16 21:59:36 +0000763 lastRootBox()->appendFloat(floatingObject->renderer());
764}
765
eric@webkit.org45e33a52011-05-04 11:51:09 +0000766// This function constructs line boxes for all of the text runs in the resolver and computes their position.
767RootInlineBox* RenderBlock::createLineBoxesFromBidiRuns(BidiRunList<BidiRun>& bidiRuns, const InlineIterator& end, LineInfo& lineInfo, VerticalPositionCache& verticalPositionCache, BidiRun* trailingSpaceRun)
768{
769 if (!bidiRuns.runCount())
770 return 0;
771
772 // FIXME: Why is this only done when we had runs?
773 lineInfo.setLastLine(!end.m_obj);
774
775 RootInlineBox* lineBox = constructLine(bidiRuns, lineInfo);
776 if (!lineBox)
777 return 0;
778
779 lineBox->setEndsWithBreak(lineInfo.previousLineBrokeCleanly());
780
781#if ENABLE(SVG)
782 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
783#else
784 bool isSVGRootInlineBox = false;
785#endif
786
787 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
788
789 // Now we position all of our text runs horizontally.
790 if (!isSVGRootInlineBox)
791 computeInlineDirectionPositionsForLine(lineBox, lineInfo, bidiRuns.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
792
793 // Now position our text runs vertically.
794 computeBlockDirectionPositionsForLine(lineBox, bidiRuns.firstRun(), textBoxDataMap, verticalPositionCache);
795
796#if ENABLE(SVG)
797 // SVG text layout code computes vertical & horizontal positions on its own.
798 // Note that we still need to execute computeVerticalPositionsForLine() as
799 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
800 // contains reversed text or not. If we wouldn't do that editing and thus
801 // text selection in RTL boxes would not work as expected.
802 if (isSVGRootInlineBox) {
803 ASSERT(isSVGText());
804 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
805 }
806#endif
807
808 // Compute our overflow now.
809 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
810
811#if PLATFORM(MAC)
812 // Highlight acts as an overflow inflation.
813 if (style()->highlight() != nullAtom)
814 lineBox->addHighlightOverflow();
815#endif
816 return lineBox;
817}
818
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000819// Like LayoutState for layout(), LineLayoutState keeps track of global information
820// during an entire linebox tree layout pass (aka layoutInlineChildren).
821class LineLayoutState {
822public:
823 LineLayoutState(bool fullLayout, int& repaintLogicalTop, int& repaintLogicalBottom)
824 : m_isFullLayout(fullLayout)
825 , m_repaintLogicalTop(repaintLogicalTop)
826 , m_repaintLogicalBottom(repaintLogicalBottom)
827 { }
828
829 void markForFullLayout() { m_isFullLayout = true; }
830 bool isFullLayout() const { return m_isFullLayout; }
831
832 void setRepaintRange(int logicalHeight) { m_repaintLogicalTop = m_repaintLogicalBottom = logicalHeight; }
833 void updateRepaintRangeFromBox(RootInlineBox* box, int paginationDelta = 0)
834 {
835 m_repaintLogicalTop = min(m_repaintLogicalTop, box->logicalTopVisualOverflow() + min(paginationDelta, 0));
836 m_repaintLogicalBottom = max(m_repaintLogicalBottom, box->logicalBottomVisualOverflow() + max(paginationDelta, 0));
837 }
838
839private:
840 bool m_isFullLayout;
841
842 // FIXME: Should this be a range object instead of two ints?
843 int& m_repaintLogicalTop;
844 int& m_repaintLogicalBottom;
845};
846
847static void deleteLineRange(LineLayoutState& layoutState, RenderArena* arena, RootInlineBox* startLine, RootInlineBox* stopLine = 0)
eric@webkit.org455d90e2011-05-09 22:27:27 +0000848{
849 RootInlineBox* boxToDelete = startLine;
850 while (boxToDelete && boxToDelete != stopLine) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000851 layoutState.updateRepaintRangeFromBox(boxToDelete);
eric@webkit.orge2532d92011-05-16 23:10:49 +0000852 // Note: deleteLineRange(renderArena(), firstRootBox()) is not identical to deleteLineBoxTree().
853 // deleteLineBoxTree uses nextLineBox() instead of nextRootBox() when traversing.
eric@webkit.org455d90e2011-05-09 22:27:27 +0000854 RootInlineBox* next = boxToDelete->nextRootBox();
855 boxToDelete->deleteLine(arena);
856 boxToDelete = next;
857 }
858}
859
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000860void RenderBlock::layoutRunsAndFloats(LineLayoutState& layoutState, bool hasInlineChild, Vector<FloatWithRect>& floats)
eric@webkit.org060caf62011-05-03 22:11:39 +0000861{
862 // We want to skip ahead to the first dirty line
863 InlineBidiResolver resolver;
864 unsigned floatIndex;
865 LineInfo lineInfo;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000866 // FIXME: Should useRepaintBounds be on the LineLayoutState?
867 // It appears to be used to track the case where we're only repainting a subset of our lines.
eric@webkit.org060caf62011-05-03 22:11:39 +0000868 bool useRepaintBounds = false;
869
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000870 RootInlineBox* startLine = determineStartPosition(layoutState, lineInfo, resolver, floats, floatIndex, useRepaintBounds);
eric@webkit.org060caf62011-05-03 22:11:39 +0000871
872 // FIXME: This would make more sense outside of this function, but since
873 // determineStartPosition can change the fullLayout flag we have to do this here. Failure to call
874 // determineStartPosition first will break fast/repaint/line-flow-with-floats-9.html.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000875 if (layoutState.isFullLayout() && hasInlineChild && !selfNeedsLayout()) {
eric@webkit.org060caf62011-05-03 22:11:39 +0000876 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
877 // we're supposed to.
878 RenderView* v = view();
879 if (v && !v->doingFullRepaint() && hasLayer()) {
880 // Because we waited until we were already inside layout to discover
881 // that the block really needed a full layout, we missed our chance to repaint the layer
882 // before layout started. Luckily the layer has cached the repaint rect for its original
883 // position and size, and so we can use that to make a repaint happen now.
884 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
885 }
886 }
887
888 FloatingObject* lastFloat = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
889
890 LineMidpointState& lineMidpointState = resolver.midpointState();
891
892 // We also find the first clean line and extract these lines. We will add them back
893 // if we determine that we're able to synchronize after handling all our dirty lines.
894 InlineIterator cleanLineStart;
895 BidiStatus cleanLineBidiStatus;
896 int endLineLogicalTop = 0;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000897 RootInlineBox* endLine = (layoutState.isFullLayout() || !startLine) ?
eric@webkit.org060caf62011-05-03 22:11:39 +0000898 0 : determineEndPosition(startLine, floats, floatIndex, cleanLineStart, cleanLineBidiStatus, endLineLogicalTop);
899
900 if (startLine) {
901 if (!useRepaintBounds) {
902 useRepaintBounds = true;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000903 layoutState.setRepaintRange(logicalHeight());
eric@webkit.org060caf62011-05-03 22:11:39 +0000904 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000905 deleteLineRange(layoutState, renderArena(), startLine);
eric@webkit.org060caf62011-05-03 22:11:39 +0000906 }
907
908 InlineIterator end = resolver.position();
909
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000910 if (!layoutState.isFullLayout() && lastRootBox() && lastRootBox()->endsWithBreak()) {
eric@webkit.org060caf62011-05-03 22:11:39 +0000911 // If the last line before the start line ends with a line break that clear floats,
912 // adjust the height accordingly.
913 // A line break can be either the first or the last object on a line, depending on its direction.
914 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
915 RenderObject* lastObject = lastLeafChild->renderer();
916 if (!lastObject->isBR())
917 lastObject = lastRootBox()->firstLeafChild()->renderer();
918 if (lastObject->isBR()) {
919 EClear clear = lastObject->style()->clear();
920 if (clear != CNONE)
921 newLine(clear);
922 }
923 }
924 }
925
926 bool endLineMatched = false;
927 bool checkForEndLineMatch = endLine;
928 bool checkForFloatsFromLastLine = false;
929
930 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
931
932 LineBreakIteratorInfo lineBreakIteratorInfo;
933 VerticalPositionCache verticalPositionCache;
934
leviw@chromium.org1a508692011-05-05 00:01:11 +0000935 LineBreaker lineBreaker(this);
936
eric@webkit.org060caf62011-05-03 22:11:39 +0000937 while (!end.atEnd()) {
938 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000939 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(layoutState, resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineLogicalTop)))
eric@webkit.org060caf62011-05-03 22:11:39 +0000940 break;
941
942 lineMidpointState.reset();
943
944 lineInfo.setEmpty(true);
945
eric@webkit.org060caf62011-05-03 22:11:39 +0000946 InlineIterator oldEnd = end;
947 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
leviw@chromium.org1a508692011-05-05 00:01:11 +0000948 end = lineBreaker.nextLineBreak(resolver, lineInfo, lineBreakIteratorInfo, lastFloatFromPreviousLine);
eric@webkit.org060caf62011-05-03 22:11:39 +0000949 if (resolver.position().atEnd()) {
950 // FIXME: We shouldn't be creating any runs in findNextLineBreak to begin with!
951 // Once BidiRunList is separated from BidiResolver this will not be needed.
952 resolver.runs().deleteRuns();
953 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
954 checkForFloatsFromLastLine = true;
955 break;
956 }
957 ASSERT(end != resolver.position());
958
eric@webkit.org45e33a52011-05-04 11:51:09 +0000959 // This is a short-cut for empty lines.
eric@webkit.org060caf62011-05-03 22:11:39 +0000960 if (lineInfo.isEmpty()) {
961 if (lastRootBox())
962 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
963 } else {
macpherson@chromium.org258babf2011-06-10 06:20:58 +0000964 VisualDirectionOverride override = (style()->rtlOrdering() == VisualOrder ? (style()->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
eric@webkit.org060caf62011-05-03 22:11:39 +0000965 // FIXME: This ownership is reversed. We should own the BidiRunList and pass it to createBidiRunsForLine.
966 BidiRunList<BidiRun>& bidiRuns = resolver.runs();
967 resolver.createBidiRunsForLine(end, override, lineInfo.previousLineBrokeCleanly());
968 ASSERT(resolver.position() == end);
969
970 BidiRun* trailingSpaceRun = !lineInfo.previousLineBrokeCleanly() ? handleTrailingSpaces(bidiRuns, resolver.context()) : 0;
971
leviw@chromium.org1a508692011-05-05 00:01:11 +0000972 if (bidiRuns.runCount() && lineBreaker.lineWasHyphenated())
eric@webkit.org45e33a52011-05-04 11:51:09 +0000973 bidiRuns.logicallyLastRun()->m_hasHyphen = true;
974
eric@webkit.org060caf62011-05-03 22:11:39 +0000975 // Now that the runs have been ordered, we create the line boxes.
976 // At the same time we figure out where border/padding/margin should be applied for
977 // inline flow boxes.
978
eric@webkit.org060caf62011-05-03 22:11:39 +0000979 int oldLogicalHeight = logicalHeight();
eric@webkit.org45e33a52011-05-04 11:51:09 +0000980 RootInlineBox* lineBox = createLineBoxesFromBidiRuns(bidiRuns, end, lineInfo, verticalPositionCache, trailingSpaceRun);
eric@webkit.org060caf62011-05-03 22:11:39 +0000981
982 bidiRuns.deleteRuns();
983 resolver.markCurrentRunEmpty(); // FIXME: This can probably be replaced by an ASSERT (or just removed).
984
985 if (lineBox) {
986 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000987 if (useRepaintBounds)
988 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +0000989
990 if (paginated) {
991 int adjustment = 0;
992 adjustLinePositionForPagination(lineBox, adjustment);
993 if (adjustment) {
994 int oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, lineInfo.isFirstLine());
995 lineBox->adjustBlockDirectionPosition(adjustment);
eric@webkit.org59c3c1e2011-05-17 19:45:24 +0000996 if (useRepaintBounds)
997 layoutState.updateRepaintRangeFromBox(lineBox);
eric@webkit.org060caf62011-05-03 22:11:39 +0000998
999 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, lineInfo.isFirstLine()) != oldLineWidth) {
1000 // We have to delete this line, remove all floats that got added, and let line layout re-run.
1001 lineBox->deleteLine(renderArena());
1002 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
1003 setLogicalHeight(oldLogicalHeight + adjustment);
1004 resolver.setPosition(oldEnd);
1005 end = oldEnd;
1006 continue;
1007 }
1008
1009 setLogicalHeight(lineBox->blockLogicalHeight());
1010 }
1011 }
1012 }
1013
leviw@chromium.org1a508692011-05-05 00:01:11 +00001014 for (size_t i = 0; i < lineBreaker.positionedObjects().size(); ++i)
1015 setStaticPositions(this, lineBreaker.positionedObjects()[i]);
eric@webkit.org060caf62011-05-03 22:11:39 +00001016
1017 lineInfo.setFirstLine(false);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001018 newLine(lineBreaker.clear());
eric@webkit.org060caf62011-05-03 22:11:39 +00001019 }
1020
1021 if (m_floatingObjects && lastRootBox()) {
1022 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1023 FloatingObjectSetIterator it = floatingObjectSet.begin();
1024 FloatingObjectSetIterator end = floatingObjectSet.end();
1025 if (lastFloat) {
1026 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(lastFloat);
1027 ASSERT(lastFloatIterator != end);
1028 ++lastFloatIterator;
1029 it = lastFloatIterator;
1030 }
1031 for (; it != end; ++it) {
1032 FloatingObject* f = *it;
1033 appendFloatingObjectToLastLine(f);
1034 ASSERT(f->m_renderer == floats[floatIndex].object);
1035 // If a float's geometry has changed, give up on syncing with clean lines.
1036 if (floats[floatIndex].rect != f->frameRect())
1037 checkForEndLineMatch = false;
1038 floatIndex++;
1039 }
1040 lastFloat = !floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0;
1041 }
1042
1043 lineMidpointState.reset();
1044 resolver.setPosition(end);
1045 }
1046
1047 if (endLine) {
1048 if (endLineMatched) {
1049 // Attach all the remaining lines, and then adjust their y-positions as needed.
1050 int delta = logicalHeight() - endLineLogicalTop;
1051 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1052 line->attachLine();
1053 if (paginated) {
1054 delta -= line->paginationStrut();
1055 adjustLinePositionForPagination(line, delta);
1056 }
1057 if (delta) {
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001058 layoutState.updateRepaintRangeFromBox(line, delta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001059 line->adjustBlockDirectionPosition(delta);
1060 }
1061 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1062 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1063 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001064 FloatingObject* floatingObject = insertFloatingObject(*f);
1065 ASSERT(!floatingObject->m_originatingLine);
1066 floatingObject->m_originatingLine = line;
eric@webkit.org060caf62011-05-03 22:11:39 +00001067 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
1068 positionNewFloats();
1069 }
1070 }
1071 }
1072 setLogicalHeight(lastRootBox()->blockLogicalHeight());
1073 } else {
1074 // Delete all the remaining lines.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001075 deleteLineRange(layoutState, renderArena(), endLine);
eric@webkit.org060caf62011-05-03 22:11:39 +00001076 }
1077 }
1078 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
1079 // In case we have a float on the last line, it might not be positioned up to now.
1080 // This has to be done before adding in the bottom border/padding, or the float will
1081 // include the padding incorrectly. -dwh
1082 if (checkForFloatsFromLastLine) {
1083 int bottomVisualOverflow = lastRootBox()->logicalBottomVisualOverflow();
1084 int bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
1085 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1086 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1087 trailingFloatsLineBox->setConstructed();
1088 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1089 VerticalPositionCache verticalPositionCache;
1090 trailingFloatsLineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
1091 int blockLogicalHeight = logicalHeight();
1092 IntRect logicalLayoutOverflow(0, blockLogicalHeight, 1, bottomLayoutOverflow - blockLogicalHeight);
1093 IntRect logicalVisualOverflow(0, blockLogicalHeight, 1, bottomVisualOverflow - blockLogicalHeight);
1094 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
1095 trailingFloatsLineBox->setBlockLogicalHeight(logicalHeight());
1096 }
1097
1098 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1099 FloatingObjectSetIterator it = floatingObjectSet.begin();
1100 FloatingObjectSetIterator end = floatingObjectSet.end();
1101 if (lastFloat) {
1102 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(lastFloat);
1103 ASSERT(lastFloatIterator != end);
1104 ++lastFloatIterator;
1105 it = lastFloatIterator;
1106 }
1107 for (; it != end; ++it)
1108 appendFloatingObjectToLastLine(*it);
1109 lastFloat = !floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0;
1110 }
1111 size_t floatCount = floats.size();
1112 // Floats that did not have layout did not repaint when we laid them out. They would have
1113 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1114 // painted.
1115 for (size_t i = 0; i < floatCount; ++i) {
1116 if (!floats[i].everHadLayout) {
1117 RenderBox* f = floats[i].object;
1118 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1119 f->repaint();
1120 }
1121 }
1122}
1123
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001124void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintLogicalTop, int& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +00001125{
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001126 m_overflow.clear();
eric@webkit.org060caf62011-05-03 22:11:39 +00001127
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001128 setLogicalHeight(borderBefore() + paddingBefore());
mitz@apple.come1364202008-02-28 01:06:41 +00001129
hyatt0c3a9862004-02-23 21:26:26 +00001130 // Figure out if we should clear out our line boxes.
1131 // FIXME: Handle resize eventually!
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001132 bool isFullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
1133 LineLayoutState layoutState(isFullLayout, repaintLogicalTop, repaintLogicalBottom);
1134
1135 if (isFullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +00001136 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +00001137
hyatted77ad82004-06-15 07:21:23 +00001138 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1139 // clip.
1140 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1141 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1142 // anyway, so we won't worry about following the draft here.
1143 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +00001144
hyatted77ad82004-06-15 07:21:23 +00001145 // Walk all the lines and delete our ellipsis line boxes if they exist.
1146 if (hasTextOverflow)
1147 deleteEllipsisLineBoxes();
1148
hyattffe78712003-02-11 01:59:29 +00001149 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001150 // layout replaced elements
mitz@apple.com40547b32008-03-18 04:04:34 +00001151 Vector<FloatWithRect> floats;
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001152 bool hasInlineChild = false;
eric@webkit.org33510472011-06-04 19:34:29 +00001153 for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1154 RenderObject* o = walker.current();
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +00001155 if (!hasInlineChild && o->isInline())
1156 hasInlineChild = true;
1157
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001158 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
1159 RenderBox* box = toRenderBox(o);
eric@webkit.org060caf62011-05-03 22:11:39 +00001160
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001161 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
1162 o->setChildNeedsLayout(true, false);
eric@webkit.org060caf62011-05-03 22:11:39 +00001163
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001164 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001165 if (relayoutChildren && (o->style()->paddingStart().isPercent() || o->style()->paddingEnd().isPercent()))
hyatt@apple.com75dad742010-09-24 18:07:44 +00001166 o->setPreferredLogicalWidthsDirty(true, false);
eric@webkit.org060caf62011-05-03 22:11:39 +00001167
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001168 if (o->isPositioned())
1169 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001170 else if (o->isFloating())
1171 floats.append(FloatWithRect(box));
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001172 else if (layoutState.isFullLayout() || o->needsLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001173 // Replaced elements
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001174 toRenderBox(o)->dirtyLineBoxes(layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001175 o->layoutIfNeeded();
1176 }
eric@webkit.org33510472011-06-04 19:34:29 +00001177 } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline())) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +00001178 if (!o->isText())
1179 toRenderInline(o)->updateAlwaysCreateLineBoxes();
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001180 if (layoutState.isFullLayout() || o->selfNeedsLayout())
1181 dirtyLineBoxesForRenderer(o, layoutState.isFullLayout());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001182 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001183 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001184 }
1185
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001186 layoutRunsAndFloats(layoutState, hasInlineChild, floats);
kociendabb0c24b2001-08-24 14:24:40 +00001187 }
hyatt85586af2003-02-19 23:22:42 +00001188
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001189 // Expand the last line to accommodate Ruby and emphasis marks.
1190 int lastLineAnnotationsAdjustment = 0;
1191 if (lastRootBox()) {
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001192 int lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001193 if (!style()->isFlippedLinesWritingMode())
1194 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1195 else
1196 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001197 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001198
hyatta70560a2002-11-20 01:53:20 +00001199 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001200 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001201
adele7a470a72006-04-20 22:22:14 +00001202 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001203 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001204
1205 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1206 // truncate text.
1207 if (hasTextOverflow)
1208 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001209}
1210
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001211void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1212{
1213 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1214 if (!cleanLineFloats)
1215 return;
1216
1217 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1218 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1219 RenderBox* floatingBox = *it;
1220 floatingBox->layoutIfNeeded();
1221 IntSize newSize(floatingBox->width() + floatingBox->marginLeft() + floatingBox->marginRight(), floatingBox->height() + floatingBox->marginTop() + floatingBox->marginBottom());
1222 ASSERT(floatIndex < floats.size());
1223 if (floats[floatIndex].object != floatingBox) {
1224 encounteredNewFloat = true;
1225 return;
1226 }
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001227
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001228 if (floats[floatIndex].rect.size() != newSize) {
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001229 int floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
eric@webkit.org060caf62011-05-03 22:11:39 +00001230 int floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001231 : max(floats[floatIndex].rect.width(), newSize.width());
mitz@apple.comd17e8c02011-04-16 21:59:36 +00001232 floatHeight = min(floatHeight, numeric_limits<int>::max() - floatTop);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001233 line->markDirty();
1234 markLinesDirtyInBlockRange(line->blockLogicalHeight(), floatTop + floatHeight, line);
1235 floats[floatIndex].rect.setSize(newSize);
1236 dirtiedByFloat = true;
1237 }
1238 floatIndex++;
1239 }
1240}
1241
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001242RootInlineBox* RenderBlock::determineStartPosition(LineLayoutState& layoutState, LineInfo& lineInfo, InlineBidiResolver& resolver, Vector<FloatWithRect>& floats,
1243 unsigned& numCleanFloats, bool& useRepaintBounds)
hyatt0c3a9862004-02-23 21:26:26 +00001244{
1245 RootInlineBox* curr = 0;
1246 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001247
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001248 // FIXME: This entire float-checking block needs to be broken into a new function.
mitz@apple.com40547b32008-03-18 04:04:34 +00001249 bool dirtiedByFloat = false;
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001250 if (!layoutState.isFullLayout()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001251 // Paginate all of the clean lines.
1252 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
1253 int paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001254 size_t floatIndex = 0;
1255 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001256 if (paginated) {
1257 paginationDelta -= curr->paginationStrut();
1258 adjustLinePositionForPagination(curr, paginationDelta);
1259 if (paginationDelta) {
1260 if (containsFloats() || !floats.isEmpty()) {
1261 // 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 +00001262 layoutState.markForFullLayout();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001263 break;
1264 }
eric@webkit.org060caf62011-05-03 22:11:39 +00001265
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001266 if (!useRepaintBounds)
1267 useRepaintBounds = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00001268
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001269 layoutState.updateRepaintRangeFromBox(curr, paginationDelta);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001270 curr->adjustBlockDirectionPosition(paginationDelta);
eric@webkit.org060caf62011-05-03 22:11:39 +00001271 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001272 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001273
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001274 // If a new float has been inserted before this line or before its last known float, just do a full layout.
1275 bool encounteredNewFloat = false;
1276 checkFloatsInCleanLine(curr, floats, floatIndex, encounteredNewFloat, dirtiedByFloat);
1277 if (encounteredNewFloat)
1278 layoutState.markForFullLayout();
1279
1280 if (dirtiedByFloat || layoutState.isFullLayout())
mitz@apple.com40547b32008-03-18 04:04:34 +00001281 break;
1282 }
1283 // Check if a new float has been inserted after the last known float.
1284 if (!curr && floatIndex < floats.size())
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001285 layoutState.markForFullLayout();
mitz@apple.com40547b32008-03-18 04:04:34 +00001286 }
1287
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001288 if (layoutState.isFullLayout()) {
eric@webkit.orge2532d92011-05-16 23:10:49 +00001289 // FIXME: This should just call deleteLineBoxTree, but that causes
1290 // crashes for fast/repaint tests.
1291 RenderArena* arena = renderArena();
1292 curr = firstRootBox();
1293 while (curr) {
1294 // Note: This uses nextRootBox() insted of nextLineBox() like deleteLineBoxTree does.
1295 RootInlineBox* next = curr->nextRootBox();
1296 curr->deleteLine(arena);
1297 curr = next;
hyatt0c3a9862004-02-23 21:26:26 +00001298 }
eric@webkit.orge2532d92011-05-16 23:10:49 +00001299 ASSERT(!firstLineBox() && !lastLineBox());
eseidel789896f2005-11-27 22:52:09 +00001300 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001301 if (curr) {
1302 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001303 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001304 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001305 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001306 // The previous line didn't break cleanly or broke at a newline
1307 // that has been deleted, so treat it as dirty too.
1308 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001309 }
eseidel789896f2005-11-27 22:52:09 +00001310 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001311 // No dirty lines were found.
1312 // If the last line didn't break cleanly, treat it as dirty.
1313 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1314 curr = lastRootBox();
1315 }
mitz@apple.come1364202008-02-28 01:06:41 +00001316
hyatt0c3a9862004-02-23 21:26:26 +00001317 // If we have no dirty lines, then last is just the last root box.
1318 last = curr ? curr->prevRootBox() : lastRootBox();
1319 }
mitz@apple.come1364202008-02-28 01:06:41 +00001320
mitz@apple.com40547b32008-03-18 04:04:34 +00001321 numCleanFloats = 0;
1322 if (!floats.isEmpty()) {
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001323 int savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001324 // Restore floats from clean lines.
1325 RootInlineBox* line = firstRootBox();
1326 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001327 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1328 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1329 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com0c4ce9f2011-05-04 02:20:02 +00001330 FloatingObject* floatingObject = insertFloatingObject(*f);
1331 ASSERT(!floatingObject->m_originatingLine);
1332 floatingObject->m_originatingLine = line;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001333 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001334 positionNewFloats();
1335 ASSERT(floats[numCleanFloats].object == *f);
1336 numCleanFloats++;
1337 }
1338 }
1339 line = line->nextRootBox();
1340 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001341 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001342 }
1343
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001344 lineInfo.setFirstLine(!last);
1345 lineInfo.setPreviousLineBrokeCleanly(!last || last->endsWithBreak());
mitz@apple.com1a301772008-03-11 18:30:36 +00001346
hyatt0c3a9862004-02-23 21:26:26 +00001347 if (last) {
hyatt@apple.coma956e332010-10-06 20:35:21 +00001348 setLogicalHeight(last->blockLogicalHeight());
eric@webkit.org6a555502011-04-28 03:20:54 +00001349 resolver.setPosition(InlineIterator(this, last->lineBreakObj(), last->lineBreakPos()));
mitz@apple.com15035e62008-07-05 20:44:44 +00001350 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001351 } else {
eric@webkit.org6a555502011-04-28 03:20:54 +00001352 resolver.setStatus(BidiStatus(style()->direction(), style()->unicodeBidi() == Override));
1353 resolver.setPosition(InlineIterator(this, bidiFirstSkippingInlines(this, &resolver), 0));
darindde01502005-12-18 22:55:35 +00001354 }
hyatt0c3a9862004-02-23 21:26:26 +00001355 return curr;
1356}
1357
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001358RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, Vector<FloatWithRect>& floats, size_t floatIndex, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& logicalTop)
hyatt0c3a9862004-02-23 21:26:26 +00001359{
1360 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001361 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1362 if (!curr->isDirty()) {
1363 bool encounteredNewFloat = false;
1364 bool dirtiedByFloat = false;
1365 checkFloatsInCleanLine(curr, floats, floatIndex, encounteredNewFloat, dirtiedByFloat);
1366 if (encounteredNewFloat)
1367 return 0;
hyatt04420ca2004-07-16 00:05:42 +00001368 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001369 if (curr->isDirty())
1370 last = 0;
1371 else if (!last)
1372 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001373 }
mitz@apple.come1364202008-02-28 01:06:41 +00001374
hyatt0c3a9862004-02-23 21:26:26 +00001375 if (!last)
1376 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001377
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001378 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1379 // in the block.
1380
eseidel789896f2005-11-27 22:52:09 +00001381 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001382 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001383 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001384 logicalTop = prev->blockLogicalHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001385
hyatt0c3a9862004-02-23 21:26:26 +00001386 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1387 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1388 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001389
hyatt0c3a9862004-02-23 21:26:26 +00001390 return last;
1391}
1392
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001393bool RenderBlock::matchedEndLine(LineLayoutState& layoutState, const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus, RootInlineBox*& endLine, int& endLogicalTop)
hyatt0c3a9862004-02-23 21:26:26 +00001394{
mitz@apple.com15035e62008-07-05 20:44:44 +00001395 if (resolver.position() == endLineStart) {
1396 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001397 return false;
1398
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001399 int delta = logicalHeight() - endLogicalTop;
mitz@apple.com40547b32008-03-18 04:04:34 +00001400 if (!delta || !m_floatingObjects)
1401 return true;
1402
1403 // See if any floats end in the range along which we want to shift the lines vertically.
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001404 int logicalTop = min(logicalHeight(), endLogicalTop);
mitz@apple.com40547b32008-03-18 04:04:34 +00001405
1406 RootInlineBox* lastLine = endLine;
1407 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1408 lastLine = nextLine;
1409
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001410 int logicalBottom = lastLine->blockLogicalHeight() + abs(delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001411
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001412 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1413 FloatingObjectSetIterator end = floatingObjectSet.end();
1414 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001415 FloatingObject* f = *it;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001416 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001417 return false;
1418 }
1419
1420 return true;
1421 }
hyatt0c3a9862004-02-23 21:26:26 +00001422
mitz@apple.come1364202008-02-28 01:06:41 +00001423 // The first clean line doesn't match, but we can check a handful of following lines to try
1424 // to match back up.
1425 static int numLines = 8; // The # of lines we're willing to match against.
1426 RootInlineBox* line = endLine;
1427 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001428 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001429 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001430 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001431 return false; // ...but the bidi state doesn't match.
1432 RootInlineBox* result = line->nextRootBox();
1433
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001434 // Set our logical top to be the block height of endLine.
mitz@apple.come1364202008-02-28 01:06:41 +00001435 if (result)
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001436 endLogicalTop = line->blockLogicalHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001437
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001438 int delta = logicalHeight() - endLogicalTop;
mitz@apple.com40547b32008-03-18 04:04:34 +00001439 if (delta && m_floatingObjects) {
1440 // See if any floats end in the range along which we want to shift the lines vertically.
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001441 int logicalTop = min(logicalHeight(), endLogicalTop);
mitz@apple.com40547b32008-03-18 04:04:34 +00001442
1443 RootInlineBox* lastLine = endLine;
1444 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1445 lastLine = nextLine;
1446
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001447 int logicalBottom = lastLine->blockLogicalHeight() + abs(delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001448
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001449 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1450 FloatingObjectSetIterator end = floatingObjectSet.end();
1451 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001452 FloatingObject* f = *it;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001453 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001454 return false;
1455 }
1456 }
1457
mitz@apple.come1364202008-02-28 01:06:41 +00001458 // Now delete the lines that we failed to sync.
eric@webkit.org59c3c1e2011-05-17 19:45:24 +00001459 deleteLineRange(layoutState, renderArena(), endLine, result);
mitz@apple.come1364202008-02-28 01:06:41 +00001460 endLine = result;
1461 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001462 }
1463 }
mitz@apple.come1364202008-02-28 01:06:41 +00001464
hyatt0c3a9862004-02-23 21:26:26 +00001465 return false;
1466}
1467
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001468static inline bool skipNonBreakingSpace(const InlineIterator& it, const LineInfo& lineInfo)
kocienda98440082004-10-14 23:51:47 +00001469{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001470 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001471 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001472
hyattdca76e92005-11-02 08:52:50 +00001473 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1474 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001475 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001476 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1477 // |true|).
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001478 if (lineInfo.isEmpty() && lineInfo.previousLineBrokeCleanly())
kocienda498d1982004-10-15 21:07:24 +00001479 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001480
kocienda498d1982004-10-15 21:07:24 +00001481 return true;
kocienda98440082004-10-14 23:51:47 +00001482}
1483
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001484static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, const LineInfo& lineInfo)
hyattd9953212005-11-03 21:05:59 +00001485{
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001486 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!lineInfo.isEmpty() || !lineInfo.previousLineBrokeCleanly()));
hyattd9953212005-11-03 21:05:59 +00001487}
1488
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001489static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001490{
1491 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001492 // We need to fix this, though, because at the very least, inlines containing only
eric@webkit.org060caf62011-05-03 22:11:39 +00001493 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001494 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001495}
1496
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001497static bool requiresLineBox(const InlineIterator& it, const LineInfo& lineInfo = LineInfo())
bdashccffb432007-07-13 11:51:40 +00001498{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001499 if (it.m_obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001500 return false;
bdakinf876bee2007-10-30 05:27:09 +00001501
eric@webkit.org8c25a592011-03-29 13:18:11 +00001502 if (it.m_obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.m_obj)))
bdakinf876bee2007-10-30 05:27:09 +00001503 return false;
1504
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001505 if (!shouldCollapseWhiteSpace(it.m_obj->style(), lineInfo) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001506 return true;
1507
1508 UChar current = it.current();
eric@webkit.org060caf62011-05-03 22:11:39 +00001509 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001510 && !skipNonBreakingSpace(it, lineInfo);
bdashccffb432007-07-13 11:51:40 +00001511}
1512
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001513bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
bdashccffb432007-07-13 11:51:40 +00001514{
1515 ASSERT(inlineObj->parent() == this);
1516
mitz@apple.com15035e62008-07-05 20:44:44 +00001517 InlineIterator it(this, inlineObj, 0);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001518 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001519 it.increment();
bdashccffb432007-07-13 11:51:40 +00001520
1521 return !it.atEnd();
1522}
1523
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001524// FIXME: The entire concept of the skipTrailingWhitespace function is flawed, since we really need to be building
leviw@chromium.org1a508692011-05-05 00:01:11 +00001525// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1526// elements quite right. In other words, we need to build this function's work into the normal line
mitz@apple.com1a301772008-03-11 18:30:36 +00001527// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001528// NB. this function will insert any floating elements that would otherwise
1529// be skipped but it will not position them.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001530void RenderBlock::LineBreaker::skipTrailingWhitespace(InlineIterator& iterator, const LineInfo& lineInfo)
kociendabb0c24b2001-08-24 14:24:40 +00001531{
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001532 while (!iterator.atEnd() && !requiresLineBox(iterator, lineInfo)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001533 RenderObject* object = iterator.m_obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001534 if (object->isFloating()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00001535 m_block->insertFloatingObject(toRenderBox(object));
hyatt@apple.come16585a2011-02-28 23:21:36 +00001536 } else if (object->isPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001537 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001538 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001539 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001540}
bdashccffb432007-07-13 11:51:40 +00001541
leviw@chromium.org1a508692011-05-05 00:01:11 +00001542void RenderBlock::LineBreaker::skipLeadingWhitespace(InlineBidiResolver& resolver, const LineInfo& lineInfo,
1543 FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
mitz@apple.com1a301772008-03-11 18:30:36 +00001544{
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001545 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), lineInfo)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001546 RenderObject* object = resolver.position().m_obj;
hyatt@apple.com95900032011-03-23 03:27:12 +00001547 if (object->isFloating())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001548 m_block->positionNewFloatOnLine(m_block->insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, width);
hyatt@apple.com95900032011-03-23 03:27:12 +00001549 else if (object->isPositioned())
leviw@chromium.org1a508692011-05-05 00:01:11 +00001550 setStaticPositions(m_block, toRenderBox(object));
mitz@apple.com15035e62008-07-05 20:44:44 +00001551 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001552 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001553 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00001554}
1555
eric@webkit.org060caf62011-05-03 22:11:39 +00001556// This is currently just used for list markers and inline flows that have line boxes. Neither should
1557// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001558static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001559{
mitz@apple.com1a301772008-03-11 18:30:36 +00001560 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001561 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1562 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001563 UChar nextChar = nextText->characters()[0];
1564 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001565 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001566 return true;
1567 }
1568 }
1569
1570 return false;
1571}
1572
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001573static inline float textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, float xPos, bool isFixedPitch, bool collapseWhiteSpace)
mitz@apple.com34106442009-02-01 06:23:39 +00001574{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00001575 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00001576 return text->width(from, len, font, xPos);
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001577 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + from, len, text->style());
1578 run.setAllowTabs(!collapseWhiteSpace);
1579 run.setXPos(xPos);
1580 return font.width(run);
mitz@apple.com34106442009-02-01 06:23:39 +00001581}
1582
mitz@apple.comd56f1082011-03-06 22:44:48 +00001583static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, int minimumPrefixLength, int minimumSuffixLength, int lastSpace, int pos, float xPos, int availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, int nextBreakable, bool& hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00001584{
mitz@apple.comd56f1082011-03-06 22:44:48 +00001585 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
1586 if (minimumPrefixLength < 0)
1587 minimumPrefixLength = 2;
1588
1589 if (minimumSuffixLength < 0)
1590 minimumSuffixLength = 2;
1591
1592 if (pos - lastSpace <= minimumSuffixLength)
1593 return;
1594
mitz@apple.comb2107652010-06-21 16:54:52 +00001595 const AtomicString& hyphenString = text->style()->hyphenString();
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001596 int hyphenWidth = font.width(RenderBlock::constructTextRun(text, font, hyphenString.string(), text->style()));
mitz@apple.comb2107652010-06-21 16:54:52 +00001597
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001598 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00001599 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1600 // that an hyphenation opportunity exists, so do not bother to look for it.
1601 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1602 return;
1603
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00001604 TextRun run = RenderBlock::constructTextRun(text, font, text->characters() + lastSpace, pos - lastSpace, text->style());
1605 run.setAllowTabs(!collapseWhiteSpace);
1606 run.setXPos(xPos + lastSpaceWordSpacing);
1607
1608 unsigned prefixLength = font.offsetForPosition(run, maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00001609 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001610 return;
1611
mitz@apple.comd56f1082011-03-06 22:44:48 +00001612 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, static_cast<unsigned>(pos - lastSpace - minimumSuffixLength)) + 1, localeIdentifier);
1613 // FIXME: The following assumes that the character at lastSpace is a space (and therefore should not factor
1614 // into hyphenate-limit-before) unless lastSpace is 0. This is wrong in the rare case of hyphenating
1615 // the first word in a text node which has leading whitespace.
mitz@apple.come4bedf32011-03-14 00:54:59 +00001616 if (!prefixLength || prefixLength - (lastSpace ? 1 : 0) < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001617 return;
1618
mitz@apple.comd56f1082011-03-06 22:44:48 +00001619 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
1620
mitz@apple.comb2107652010-06-21 16:54:52 +00001621#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001622 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00001623 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001624#else
1625 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001626#endif
1627
eric@webkit.orgbd143592011-03-29 17:44:41 +00001628 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00001629 hyphenated = true;
1630}
1631
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001632class LineWidth {
1633public:
rniwa@webkit.org44424752011-04-14 00:58:40 +00001634 LineWidth(RenderBlock* block, bool isFirstLine)
1635 : m_block(block)
1636 , m_uncommittedWidth(0)
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001637 , m_committedWidth(0)
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001638 , m_overhangWidth(0)
rniwa@webkit.org44424752011-04-14 00:58:40 +00001639 , m_left(0)
1640 , m_right(0)
1641 , m_availableWidth(0)
1642 , m_isFirstLine(isFirstLine)
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001643 {
rniwa@webkit.org44424752011-04-14 00:58:40 +00001644 ASSERT(block);
1645 updateAvailableWidth();
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001646 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001647 bool fitsOnLine() const { return currentWidth() <= m_availableWidth; }
1648 bool fitsOnLine(float extra) const { return currentWidth() + extra <= m_availableWidth; }
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001649 float currentWidth() const { return m_committedWidth + m_uncommittedWidth; }
1650
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001651 // FIXME: We should eventually replace these three functions by ones that work on a higher abstraction.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001652 float uncommittedWidth() const { return m_uncommittedWidth; }
1653 float committedWidth() const { return m_committedWidth; }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001654 float availableWidth() const { return m_availableWidth; }
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001655
rniwa@webkit.org44424752011-04-14 00:58:40 +00001656 void updateAvailableWidth();
1657 void shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject*);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001658 void addUncommittedWidth(float delta) { m_uncommittedWidth += delta; }
1659 void commit()
1660 {
1661 m_committedWidth += m_uncommittedWidth;
1662 m_uncommittedWidth = 0;
1663 }
rniwa@webkit.org44424752011-04-14 00:58:40 +00001664 void applyOverhang(RenderRubyRun*, RenderObject* startRenderer, RenderObject* endRenderer);
1665 void fitBelowFloats();
1666
1667private:
1668 void computeAvailableWidthFromLeftAndRight()
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001669 {
rniwa@webkit.org44424752011-04-14 00:58:40 +00001670 m_availableWidth = max(0, m_right - m_left) + m_overhangWidth;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001671 }
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001672
1673private:
rniwa@webkit.org44424752011-04-14 00:58:40 +00001674 RenderBlock* m_block;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001675 float m_uncommittedWidth;
1676 float m_committedWidth;
rniwa@webkit.org44424752011-04-14 00:58:40 +00001677 float m_overhangWidth; // The amount by which |m_availableWidth| has been inflated to account for possible contraction due to ruby overhang.
1678 int m_left;
1679 int m_right;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001680 float m_availableWidth;
rniwa@webkit.org44424752011-04-14 00:58:40 +00001681 bool m_isFirstLine;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001682};
1683
rniwa@webkit.org44424752011-04-14 00:58:40 +00001684inline void LineWidth::updateAvailableWidth()
1685{
1686 int height = m_block->logicalHeight();
1687 m_left = m_block->logicalLeftOffsetForLine(height, m_isFirstLine);
1688 m_right = m_block->logicalRightOffsetForLine(height, m_isFirstLine);
1689
1690 computeAvailableWidthFromLeftAndRight();
1691}
1692
1693inline void LineWidth::shrinkAvailableWidthForNewFloatIfNeeded(RenderBlock::FloatingObject* newFloat)
1694{
1695 int height = m_block->logicalHeight();
1696 if (height < m_block->logicalTopForFloat(newFloat) || height >= m_block->logicalBottomForFloat(newFloat))
1697 return;
1698
1699 if (newFloat->type() == RenderBlock::FloatingObject::FloatLeft)
1700 m_left = m_block->logicalRightForFloat(newFloat);
1701 else
1702 m_right = m_block->logicalLeftForFloat(newFloat);
1703
1704 computeAvailableWidthFromLeftAndRight();
1705}
1706
1707void LineWidth::applyOverhang(RenderRubyRun* rubyRun, RenderObject* startRenderer, RenderObject* endRenderer)
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001708{
1709 int startOverhang;
1710 int endOverhang;
rniwa@webkit.org44424752011-04-14 00:58:40 +00001711 rubyRun->getOverhang(m_isFirstLine, startRenderer, endRenderer, startOverhang, endOverhang);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001712
1713 startOverhang = min<int>(startOverhang, m_committedWidth);
1714 m_availableWidth += startOverhang;
1715
1716 endOverhang = max(min<int>(endOverhang, m_availableWidth - currentWidth()), 0);
1717 m_availableWidth += endOverhang;
1718 m_overhangWidth += startOverhang + endOverhang;
1719}
1720
rniwa@webkit.org44424752011-04-14 00:58:40 +00001721void LineWidth::fitBelowFloats()
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001722{
1723 ASSERT(!m_committedWidth);
1724 ASSERT(!fitsOnLine());
1725
1726 int floatLogicalBottom;
rniwa@webkit.org44424752011-04-14 00:58:40 +00001727 int lastFloatLogicalBottom = m_block->logicalHeight();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001728 float newLineWidth = m_availableWidth;
1729 while (true) {
rniwa@webkit.org44424752011-04-14 00:58:40 +00001730 floatLogicalBottom = m_block->nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001731 if (!floatLogicalBottom)
1732 break;
1733
rniwa@webkit.org44424752011-04-14 00:58:40 +00001734 newLineWidth = m_block->availableLogicalWidthForLine(floatLogicalBottom, m_isFirstLine);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001735 lastFloatLogicalBottom = floatLogicalBottom;
1736 if (newLineWidth >= m_uncommittedWidth)
1737 break;
1738 }
1739
1740 if (newLineWidth > m_availableWidth) {
rniwa@webkit.org44424752011-04-14 00:58:40 +00001741 m_block->setLogicalHeight(lastFloatLogicalBottom);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001742 m_availableWidth = newLineWidth + m_overhangWidth;
1743 }
1744}
1745
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001746class TrailingObjects {
1747public:
1748 TrailingObjects();
1749 void setTrailingWhitespace(RenderText*);
1750 void clear();
1751 void appendBoxIfNeeded(RenderBox*);
mitz@apple.come98acc92011-05-22 04:44:27 +00001752
1753 enum CollapseFirstSpaceOrNot { DoNotCollapseFirstSpace, CollapseFirstSpace };
1754
1755 void updateMidpointsForTrailingBoxes(LineMidpointState&, const InlineIterator& lBreak, CollapseFirstSpaceOrNot);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001756
1757private:
1758 RenderText* m_whitespace;
1759 Vector<RenderBox*, 4> m_boxes;
1760};
1761
1762TrailingObjects::TrailingObjects()
1763 : m_whitespace(0)
1764{
1765}
1766
1767inline void TrailingObjects::setTrailingWhitespace(RenderText* whitespace)
1768{
1769 ASSERT(whitespace);
1770 m_whitespace = whitespace;
1771}
1772
1773inline void TrailingObjects::clear()
1774{
1775 m_whitespace = 0;
1776 m_boxes.clear();
1777}
1778
1779inline void TrailingObjects::appendBoxIfNeeded(RenderBox* box)
1780{
1781 if (m_whitespace)
1782 m_boxes.append(box);
1783}
1784
mitz@apple.come98acc92011-05-22 04:44:27 +00001785void TrailingObjects::updateMidpointsForTrailingBoxes(LineMidpointState& lineMidpointState, const InlineIterator& lBreak, CollapseFirstSpaceOrNot collapseFirstSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001786{
1787 if (!m_whitespace)
1788 return;
1789
1790 // This object is either going to be part of the last midpoint, or it is going to be the actual endpoint.
1791 // In both cases we just decrease our pos by 1 level to exclude the space, allowing it to - in effect - collapse into the newline.
1792 if (lineMidpointState.numMidpoints % 2) {
1793 // Find the trailing space object's midpoint.
1794 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
1795 for ( ; trailingSpaceMidpoint >= 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != m_whitespace; --trailingSpaceMidpoint) { }
1796 ASSERT(trailingSpaceMidpoint >= 0);
mitz@apple.come98acc92011-05-22 04:44:27 +00001797 if (collapseFirstSpace == CollapseFirstSpace)
1798 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001799
eric@webkit.org060caf62011-05-03 22:11:39 +00001800 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001801 // ignoring spaces.
1802 size_t currentMidpoint = trailingSpaceMidpoint + 1;
1803 for (size_t i = 0; i < m_boxes.size(); ++i) {
1804 if (currentMidpoint >= lineMidpointState.numMidpoints) {
1805 // We don't have a midpoint for this box yet.
1806 InlineIterator ignoreStart(0, m_boxes[i], 0);
1807 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
1808 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
1809 } else {
1810 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == m_boxes[i]);
1811 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == m_boxes[i]);
1812 }
1813 currentMidpoint += 2;
1814 }
1815 } else if (!lBreak.m_obj) {
1816 ASSERT(m_whitespace->isText());
mitz@apple.come98acc92011-05-22 04:44:27 +00001817 ASSERT(collapseFirstSpace == CollapseFirstSpace);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001818 // Add a new end midpoint that stops right at the very end.
1819 unsigned length = m_whitespace->textLength();
1820 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
1821 InlineIterator endMid(0, m_whitespace, pos);
1822 addMidpoint(lineMidpointState, endMid);
1823 for (size_t i = 0; i < m_boxes.size(); ++i) {
1824 InlineIterator ignoreStart(0, m_boxes[i], 0);
1825 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1826 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
1827 }
1828 }
1829}
1830
leviw@chromium.org1a508692011-05-05 00:01:11 +00001831void RenderBlock::LineBreaker::reset()
kociendae40cb942004-10-05 20:05:38 +00001832{
leviw@chromium.org1a508692011-05-05 00:01:11 +00001833 m_positionedObjects.clear();
1834 m_hyphenated = false;
1835 m_clear = CNONE;
1836}
1837
1838InlineIterator RenderBlock::LineBreaker::nextLineBreak(InlineBidiResolver& resolver, LineInfo& lineInfo,
1839 LineBreakIteratorInfo& lineBreakIteratorInfo, FloatingObject* lastFloatFromPreviousLine)
1840{
1841 reset();
1842
1843 ASSERT(resolver.position().root() == m_block);
mitz@apple.com51017322008-02-26 06:47:43 +00001844
eric@webkit.org86a865a2011-03-29 15:30:41 +00001845 bool appliedStartWidth = resolver.position().m_pos > 0;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00001846 bool includeEndWidth = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001847 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.com1a301772008-03-11 18:30:36 +00001848
leviw@chromium.org1a508692011-05-05 00:01:11 +00001849 LineWidth width(m_block, lineInfo.isFirstLine());
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00001850
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001851 skipLeadingWhitespace(resolver, lineInfo, lastFloatFromPreviousLine, width);
kociendae40cb942004-10-05 20:05:38 +00001852
mitz@apple.com15035e62008-07-05 20:44:44 +00001853 if (resolver.position().atEnd())
1854 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001855
hyatt33f8d492002-11-12 21:44:52 +00001856 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1857 // or not we are currently ignoring whitespace.
1858 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001859 InlineIterator ignoreStart;
eric@webkit.org060caf62011-05-03 22:11:39 +00001860
hyatt33f8d492002-11-12 21:44:52 +00001861 // This variable tracks whether the very last character we saw was a space. We use
1862 // this to detect when we encounter a second space so we know we have to terminate
1863 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001864 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001865 bool currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001866 TrailingObjects trailingObjects;
hyatt98b16282004-03-31 18:43:12 +00001867
mitz@apple.com15035e62008-07-05 20:44:44 +00001868 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001869
eric@webkit.orgbd143592011-03-29 17:44:41 +00001870 // FIXME: It is error-prone to split the position object out like this.
1871 // Teach this code to work with objects instead of this split tuple.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001872 InlineIterator current = resolver.position();
1873 RenderObject* last = current.m_obj;
ddkilzere8759ef2007-03-25 06:28:19 +00001874 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001875
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001876 bool startingNewParagraph = lineInfo.previousLineBrokeCleanly();
1877 lineInfo.setPreviousLineBrokeCleanly(false);
ddkilzer5d01fa22007-01-29 03:10:37 +00001878
1879 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001880 bool floatsFitOnLine = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00001881
hyatt@apple.com69340902008-01-16 21:24:21 +00001882 // Firefox and Opera will allow a table cell to grow to fit an image inside it under
eric@webkit.org060caf62011-05-03 22:11:39 +00001883 // very specific circumstances (in order to match common WinIE renderings).
1884 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
leviw@chromium.org1a508692011-05-05 00:01:11 +00001885 bool allowImagesToBreak = !m_block->document()->inQuirksMode() || !m_block->isTableCell() || !m_block->style()->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001886
leviw@chromium.org1a508692011-05-05 00:01:11 +00001887 EWhiteSpace currWS = m_block->style()->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00001888 EWhiteSpace lastWS = currWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001889 while (current.m_obj) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00001890 RenderObject* next = bidiNext(m_block, current.m_obj);
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00001891 if (next && next->parent() && !next->parent()->isDescendantOf(current.m_obj->parent()))
1892 includeEndWidth = true;
mitz@apple.comddb59872011-04-05 05:21:16 +00001893
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001894 currWS = current.m_obj->isReplaced() ? current.m_obj->parent()->style()->whiteSpace() : current.m_obj->style()->whiteSpace();
hyattb0d9f602007-01-15 01:28:23 +00001895 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
eric@webkit.org060caf62011-05-03 22:11:39 +00001896
hyattb0d9f602007-01-15 01:28:23 +00001897 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001898 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001899
mjsd2948ef2007-02-26 19:29:04 +00001900#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001901 bool preserveNewline = current.m_obj->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001902#else
hyattb0d9f602007-01-15 01:28:23 +00001903 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001904#endif
1905
hyattb0d9f602007-01-15 01:28:23 +00001906 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
eric@webkit.org060caf62011-05-03 22:11:39 +00001907
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001908 if (current.m_obj->isBR()) {
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00001909 if (width.fitsOnLine()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001910 lBreak.moveToStartOf(current.m_obj);
mitz@apple.com1a301772008-03-11 18:30:36 +00001911 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001912
hyatt33f8d492002-11-12 21:44:52 +00001913 // A <br> always breaks a line, so don't let the line be collapsed
1914 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001915 // get collapsed away. It only does this if the previous line broke
1916 // cleanly. Otherwise the <br> has no effect on whether the line is
1917 // empty or not.
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001918 if (startingNewParagraph)
1919 lineInfo.setEmpty(false);
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001920 trailingObjects.clear();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001921 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt74eec4d2003-03-23 08:02:47 +00001922
leviw@chromium.org1a508692011-05-05 00:01:11 +00001923 if (!lineInfo.isEmpty())
1924 m_clear = current.m_obj->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001925 }
1926 goto end;
1927 }
hyattb0d9f602007-01-15 01:28:23 +00001928
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001929 if (current.m_obj->isFloating()) {
1930 RenderBox* floatBox = toRenderBox(current.m_obj);
leviw@chromium.org1a508692011-05-05 00:01:11 +00001931 FloatingObject* f = m_block->insertFloatingObject(floatBox);
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001932 // check if it fits in the current line.
1933 // If it does, position it now, otherwise, position
1934 // it after moving to next line (in newLine() func)
leviw@chromium.org1a508692011-05-05 00:01:11 +00001935 if (floatsFitOnLine && width.fitsOnLine(m_block->logicalWidthForFloat(f))) {
1936 m_block->positionNewFloatOnLine(f, lastFloatFromPreviousLine, width);
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001937 if (lBreak.m_obj == current.m_obj) {
1938 ASSERT(!lBreak.m_pos);
1939 lBreak.increment();
hyatt98ee7e42003-05-14 01:39:15 +00001940 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001941 } else
1942 floatsFitOnLine = false;
1943 } else if (current.m_obj->isPositioned()) {
1944 // If our original display wasn't an inline type, then we can
1945 // go ahead and determine our static inline position now.
1946 RenderBox* box = toRenderBox(current.m_obj);
1947 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1948 if (!isInlineType)
leviw@chromium.org1a508692011-05-05 00:01:11 +00001949 box->layer()->setStaticInlinePosition(m_block->borderAndPaddingStart());
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001950 else {
1951 // If our original display was an INLINE type, then we can go ahead
1952 // and determine our static y position now.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001953 box->layer()->setStaticBlockPosition(m_block->logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00001954 }
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00001955
1956 // If we're ignoring spaces, we have to stop and include this object and
1957 // then start ignoring spaces again.
1958 if (isInlineType || current.m_obj->container()->isRenderInline()) {
1959 if (ignoringSpaces) {
1960 ignoreStart.m_obj = current.m_obj;
1961 ignoreStart.m_pos = 0;
1962 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1963 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
1964 }
1965 trailingObjects.appendBoxIfNeeded(box);
1966 } else
leviw@chromium.org1a508692011-05-05 00:01:11 +00001967 m_positionedObjects.append(box);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001968 } else if (current.m_obj->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001969 // Right now, we should only encounter empty inlines here.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001970 ASSERT(!current.m_obj->firstChild());
eric@webkit.org060caf62011-05-03 22:11:39 +00001971
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001972 RenderInline* flowBox = toRenderInline(current.m_obj);
eric@webkit.org060caf62011-05-03 22:11:39 +00001973
1974 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1975 // to make sure that we stop to include this object and then start ignoring spaces again.
1976 // If this object is at the start of the line, we need to behave like list markers and
bdakinf876bee2007-10-30 05:27:09 +00001977 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001978 if (inlineFlowRequiresLineBox(flowBox)) {
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00001979 lineInfo.setEmpty(false);
bdakinf876bee2007-10-30 05:27:09 +00001980 if (ignoringSpaces) {
rniwa@webkit.org105350c2011-05-03 20:33:25 +00001981 trailingObjects.clear();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001982 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Stop ignoring spaces.
1983 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0)); // Start ignoring again.
leviw@chromium.org1a508692011-05-05 00:01:11 +00001984 } else if (m_block->style()->collapseWhiteSpace() && resolver.position().m_obj == current.m_obj
1985 && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00001986 // Like with list markers, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00001987 // additional spaces we see will be discarded.
1988 currentCharacterIsSpace = true;
1989 currentCharacterIsWS = true;
1990 ignoringSpaces = true;
1991 }
1992 }
1993
rniwa@webkit.org58925e82011-04-12 21:32:58 +00001994 width.addUncommittedWidth(borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox));
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001995 } else if (current.m_obj->isReplaced()) {
1996 RenderBox* replacedBox = toRenderBox(current.m_obj);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001997
hyattde396342003-10-29 08:57:20 +00001998 // Break on replaced elements if either has normal white-space.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00001999 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!current.m_obj->isImage() || allowImagesToBreak)) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002000 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002001 lBreak.moveToStartOf(current.m_obj);
hyatt711fe232002-11-20 21:25:14 +00002002 }
2003
mitz@apple.combfdc9112008-02-21 19:59:40 +00002004 if (ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002005 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00002006
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002007 lineInfo.setEmpty(false);
hyatt33f8d492002-11-12 21:44:52 +00002008 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002009 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002010 currentCharacterIsWS = false;
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002011 trailingObjects.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00002012
bdakinf876bee2007-10-30 05:27:09 +00002013 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002014 // item, then this is all moot.
leviw@chromium.org1a508692011-05-05 00:01:11 +00002015 int replacedLogicalWidth = m_block->logicalWidthForChild(replacedBox) + m_block->marginStartForChild(replacedBox) + m_block->marginEndForChild(replacedBox) + inlineLogicalWidth(current.m_obj);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002016 if (current.m_obj->isListMarker()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002017 if (m_block->style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(m_block, current.m_obj, lineMidpointState)) {
eric@webkit.org060caf62011-05-03 22:11:39 +00002018 // Like with inline flows, we start ignoring spaces to make sure that any
bdakinf876bee2007-10-30 05:27:09 +00002019 // additional spaces we see will be discarded.
2020 currentCharacterIsSpace = true;
2021 currentCharacterIsWS = true;
2022 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00002023 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002024 if (toRenderListMarker(current.m_obj)->isInside())
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002025 width.addUncommittedWidth(replacedLogicalWidth);
justing244d3a32006-04-13 01:31:24 +00002026 } else
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002027 width.addUncommittedWidth(replacedLogicalWidth);
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002028 if (current.m_obj->isRubyRun())
2029 width.applyOverhang(toRenderRubyRun(current.m_obj), last, next);
2030 } else if (current.m_obj->isText()) {
2031 if (!current.m_pos)
mitz@apple.com51017322008-02-26 06:47:43 +00002032 appliedStartWidth = false;
2033
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002034 RenderText* t = toRenderText(current.m_obj);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002035
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00002036#if ENABLE(SVG)
2037 bool isSVGText = t->isSVGInlineText();
2038#endif
2039
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002040 RenderStyle* style = t->style(lineInfo.isFirstLine());
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002041 if (style->hasTextCombine() && current.m_obj->isCombineText())
2042 toRenderCombineText(current.m_obj)->combineText();
kociendabb0c24b2001-08-24 14:24:40 +00002043
mitz@apple.comb2107652010-06-21 16:54:52 +00002044 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00002045 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00002046 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00002047
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002048 int lastSpace = current.m_pos;
2049 float wordSpacing = current.m_obj->style()->wordSpacing();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002050 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002051
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002052 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
2053 // space, then subtract its width.
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002054 float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(constructTextRun(t, f, &space, 1, style)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002055
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002056 float wrapW = width.uncommittedWidth() + inlineLogicalWidth(current.m_obj, !appliedStartWidth, true);
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002057 float charWidth = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002058 bool breakNBSP = autoWrap && current.m_obj->style()->nbspMode() == SPACE;
weinigf28a1c32007-02-14 14:10:31 +00002059 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
2060 // which is only possible if the word is the first thing on the line, that is, if |w| is zero.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002061 bool breakWords = current.m_obj->style()->breakWords() && ((autoWrap && !width.committedWidth()) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00002062 bool midWordBreak = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002063 bool breakAll = current.m_obj->style()->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002064 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00002065
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002066 if (t->isWordBreak()) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002067 width.commit();
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002068 lBreak.moveToStartOf(current.m_obj);
2069 ASSERT(current.m_pos == t->textLength());
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002070 }
2071
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002072 for (; current.m_pos < t->textLength(); current.fastIncrementInTextNode()) {
rjwc9c257d2003-01-24 03:46:17 +00002073 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002074 bool previousCharacterIsWS = currentCharacterIsWS;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002075 UChar c = current.current();
hyattb0d9f602007-01-15 01:28:23 +00002076 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002077
hyattb0d9f602007-01-15 01:28:23 +00002078 if (!collapseWhiteSpace || !currentCharacterIsSpace)
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002079 lineInfo.setEmpty(false);
mitz@apple.combe429562008-03-07 01:09:51 +00002080
mitz@apple.com20e34452010-09-28 19:38:15 +00002081 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
2082 const AtomicString& hyphenString = style->hyphenString();
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002083 hyphenWidth = f.width(constructTextRun(t, f, hyphenString.string(), current.m_obj->style()));
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002084 width.addUncommittedWidth(hyphenWidth);
hyatt78b85132004-03-29 20:07:45 +00002085 }
mitz@apple.com20e34452010-09-28 19:38:15 +00002086
hyatt3aff2332003-01-23 01:15:28 +00002087 bool applyWordSpacing = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002088
darinf9e5d6c2007-01-09 14:54:26 +00002089 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00002090
weiniged111c12007-07-13 22:45:11 +00002091 if ((breakAll || breakWords) && !midWordBreak) {
2092 wrapW += charWidth;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002093 charWidth = textWidth(t, current.m_pos, 1, f, width.committedWidth() + wrapW, isFixedPitch, collapseWhiteSpace);
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002094 midWordBreak = width.committedWidth() + wrapW + charWidth > width.availableWidth();
weinigf28a1c32007-02-14 14:10:31 +00002095 }
darin47ece0d2005-09-04 07:42:31 +00002096
mitz@apple.com44fc5132011-02-25 18:34:15 +00002097 if (lineBreakIteratorInfo.first != t) {
2098 lineBreakIteratorInfo.first = t;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002099 lineBreakIteratorInfo.second.reset(t->characters(), t->textLength());
mitz@apple.com44fc5132011-02-25 18:34:15 +00002100 }
2101
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002102 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(lineBreakIteratorInfo.second, current.m_pos, current.m_nextBreakablePosition, breakNBSP)
2103 && (style->hyphens() != HyphensNone || (current.previousInSameNode() != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00002104
weiniged111c12007-07-13 22:45:11 +00002105 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002106 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002107 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002108 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002109 // Stop ignoring spaces and begin at this
2110 // new point.
hyatt48710d62003-08-21 09:17:13 +00002111 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002112 lastSpaceWordSpacing = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002113 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2114 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt0c05e102006-04-14 08:15:00 +00002115 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002116 } else {
hyatt33f8d492002-11-12 21:44:52 +00002117 // Just keep ignoring these spaces.
hyatt33f8d492002-11-12 21:44:52 +00002118 continue;
2119 }
2120 }
rjwc9c257d2003-01-24 03:46:17 +00002121
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002122 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002123 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002124 additionalTmpW = textWidth(t, lastSpace, current.m_pos + 1 - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00002125 else
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002126 additionalTmpW = textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002127 width.addUncommittedWidth(additionalTmpW);
hyattffe78712003-02-11 01:59:29 +00002128 if (!appliedStartWidth) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002129 width.addUncommittedWidth(inlineLogicalWidth(current.m_obj, true, false));
hyattffe78712003-02-11 01:59:29 +00002130 appliedStartWidth = true;
2131 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002132
eseideld13fe532005-11-30 02:40:29 +00002133 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002134
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002135 if (!width.committedWidth() && autoWrap && !width.fitsOnLine())
rniwa@webkit.org44424752011-04-14 00:58:40 +00002136 width.fitBelowFloats();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002137
hyattb0d9f602007-01-15 01:28:23 +00002138 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00002139 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002140 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00002141 bool lineWasTooWide = false;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002142 if (width.fitsOnLine() && currentCharacterIsWS && current.m_obj->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
2143 float charWidth = textWidth(t, current.m_pos, 1, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00002144 // Check if line is too big even without the extra space
eric@webkit.org060caf62011-05-03 22:11:39 +00002145 // at the end of the line. If it is not, do nothing.
2146 // If the line needs the extra whitespace to be too long,
2147 // then move the line break to the space and skip all
ap932806a2006-10-01 09:06:09 +00002148 // additional whitespace.
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002149 if (!width.fitsOnLine(charWidth)) {
ap932806a2006-10-01 09:06:09 +00002150 lineWasTooWide = true;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002151 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002152 skipTrailingWhitespace(lBreak, lineInfo);
kocienda9dbe9b12004-10-22 20:07:05 +00002153 }
ap932806a2006-10-01 09:06:09 +00002154 }
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002155 if (lineWasTooWide || !width.fitsOnLine()) {
2156 if (canHyphenate && !width.fitsOnLine()) {
leviw@chromium.org1a508692011-05-05 00:01:11 +00002157 tryHyphenating(t, f, style->locale(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
2158 if (m_hyphenated)
mitz@apple.com67ed70a2010-06-22 22:10:10 +00002159 goto end;
2160 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002161 if (lBreak.atTextParagraphSeparator()) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002162 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002163 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002164 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2165 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002166 }
mitz@apple.com1a301772008-03-11 18:30:36 +00002167 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002168 lineInfo.setPreviousLineBrokeCleanly(true);
adele7fc3e832006-02-17 09:31:35 +00002169 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00002170 if (lBreak.m_obj && lBreak.m_pos && lBreak.m_obj->isText() && toRenderText(lBreak.m_obj)->textLength() && toRenderText(lBreak.m_obj)->characters()[lBreak.m_pos - 1] == softHyphen && style->hyphens() != HyphensNone)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002171 m_hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00002172 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002173 } else {
weiniged111c12007-07-13 22:45:11 +00002174 if (!betweenWords || (midWordBreak && !autoWrap))
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002175 width.addUncommittedWidth(-additionalTmpW);
mitz@apple.com20e34452010-09-28 19:38:15 +00002176 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00002177 // Subtract the width of the soft hyphen out since we fit on a line.
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002178 width.addUncommittedWidth(-hyphenWidth);
mitz@apple.com20e34452010-09-28 19:38:15 +00002179 hyphenWidth = 0;
2180 }
darin54008922006-01-13 16:39:05 +00002181 }
rjwc9c257d2003-01-24 03:46:17 +00002182 }
hyatt33f8d492002-11-12 21:44:52 +00002183
hyattb0d9f602007-01-15 01:28:23 +00002184 if (c == '\n' && preserveNewline) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002185 if (!stoppedIgnoringSpaces && current.m_pos > 0) {
hyatt0c05e102006-04-14 08:15:00 +00002186 // We need to stop right before the newline and then start up again.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002187 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1)); // Stop
2188 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00002189 }
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002190 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
mitz@apple.com1a301772008-03-11 18:30:36 +00002191 lBreak.increment();
leviw@chromium.orgf009c8e2011-05-03 20:49:15 +00002192 lineInfo.setPreviousLineBrokeCleanly(true);
hyatt33f8d492002-11-12 21:44:52 +00002193 return lBreak;
2194 }
hyatta9f48e32003-02-03 22:48:01 +00002195
weinigf28a1c32007-02-14 14:10:31 +00002196 if (autoWrap && betweenWords) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002197 width.commit();
weiniged111c12007-07-13 22:45:11 +00002198 wrapW = 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002199 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weinigf28a1c32007-02-14 14:10:31 +00002200 // Auto-wrapping text should not wrap in the middle of a word once it has had an
2201 // opportunity to break after a word.
2202 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00002203 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002204
darin54008922006-01-13 16:39:05 +00002205 if (midWordBreak) {
2206 // Remember this as a breakable position in case
2207 // adding the end width forces a break.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002208 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
weiniged111c12007-07-13 22:45:11 +00002209 midWordBreak &= (breakWords || breakAll);
2210 }
2211
2212 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002213 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002214 lastSpace = current.m_pos;
darin54008922006-01-13 16:39:05 +00002215 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002216
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002217 if (!ignoringSpaces && current.m_obj->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002218 // If we encounter a newline, or if we encounter a
2219 // second space, we need to go ahead and break up this
2220 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002221 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002222 ignoringSpaces = true;
eric@webkit.org060caf62011-05-03 22:11:39 +00002223
hyatt33f8d492002-11-12 21:44:52 +00002224 // We just entered a mode where we are ignoring
2225 // spaces. Create a midpoint to terminate the run
eric@webkit.org060caf62011-05-03 22:11:39 +00002226 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002227 addMidpoint(lineMidpointState, ignoreStart);
mitz@apple.come98acc92011-05-22 04:44:27 +00002228 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, InlineIterator(), TrailingObjects::DoNotCollapseFirstSpace);
hyatt33f8d492002-11-12 21:44:52 +00002229 }
2230 }
eseidel789896f2005-11-27 22:52:09 +00002231 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002232 // Stop ignoring spaces and begin at this
2233 // new point.
2234 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002235 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002236 lastSpace = current.m_pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
2237 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
hyatt33f8d492002-11-12 21:44:52 +00002238 }
hyatt98b16282004-03-31 18:43:12 +00002239
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002240#if ENABLE(SVG)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002241 if (isSVGText && current.m_pos > 0) {
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002242 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002243 if (static_cast<RenderSVGInlineText*>(t)->characterStartsNewTextChunk(current.m_pos)) {
2244 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos - 1));
2245 addMidpoint(lineMidpointState, InlineIterator(0, current.m_obj, current.m_pos));
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002246 }
2247 }
2248#endif
2249
hyatt98b16282004-03-31 18:43:12 +00002250 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002251 ignoreStart.m_obj = current.m_obj;
2252 ignoreStart.m_pos = current.m_pos;
hyatt98b16282004-03-31 18:43:12 +00002253 }
harrisone343c412005-01-18 01:07:26 +00002254
2255 if (!currentCharacterIsWS && previousCharacterIsWS) {
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002256 if (autoWrap && current.m_obj->style()->breakOnlyAfterWhiteSpace())
2257 lBreak.moveTo(current.m_obj, current.m_pos, current.m_nextBreakablePosition);
harrisone343c412005-01-18 01:07:26 +00002258 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002259
hyattb0d9f602007-01-15 01:28:23 +00002260 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002261 trailingObjects.setTrailingWhitespace(static_cast<RenderText*>(current.m_obj));
2262 else if (!current.m_obj->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002263 trailingObjects.clear();
2264
ddkilzere8759ef2007-03-25 06:28:19 +00002265 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002266 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002267
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002268 // IMPORTANT: current.m_pos is > length here!
2269 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, current.m_pos - lastSpace, f, width.currentWidth(), isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
yael.aharon@nokia.com1cfbceb2011-06-06 18:32:54 +00002270 width.addUncommittedWidth(additionalTmpW + inlineLogicalWidth(current.m_obj, !appliedStartWidth, includeEndWidth));
2271 includeEndWidth = false;
mitz@apple.comb2107652010-06-21 16:54:52 +00002272
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002273 if (!width.fitsOnLine()) {
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002274 if (canHyphenate)
leviw@chromium.org1a508692011-05-05 00:01:11 +00002275 tryHyphenating(t, f, style->locale(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, current.m_pos, width.currentWidth() - additionalTmpW, width.availableWidth(), isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, current.m_nextBreakablePosition, m_hyphenated);
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002276
leviw@chromium.org1a508692011-05-05 00:01:11 +00002277 if (!m_hyphenated && lBreak.previousInSameNode() == softHyphen && style->hyphens() != HyphensNone)
2278 m_hyphenated = true;
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002279
leviw@chromium.org1a508692011-05-05 00:01:11 +00002280 if (m_hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00002281 goto end;
2282 }
kociendabb0c24b2001-08-24 14:24:40 +00002283 } else
weinigf28a1c32007-02-14 14:10:31 +00002284 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002285
hyattdca76e92005-11-02 08:52:50 +00002286 bool checkForBreak = autoWrap;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002287 if (width.committedWidth() && !width.fitsOnLine() && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002288 checkForBreak = true;
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002289 else if (next && current.m_obj->isText() && next->isText() && !next->isBR() && (autoWrap || (next->style()->autoWrap()))) {
2290 if (currentCharacterIsSpace)
2291 checkForBreak = true;
2292 else {
2293 RenderText* nextText = toRenderText(next);
2294 if (nextText->textLength()) {
2295 UChar c = nextText->characters()[0];
2296 checkForBreak = (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()));
2297 // If the next item on the line is text, and if we did not end with
2298 // a space, then the next text run continues our word (and so it needs to
2299 // keep adding to |tmpW|. Just update and continue.
2300 } else if (nextText->isWordBreak())
hyatta9f48e32003-02-03 22:48:01 +00002301 checkForBreak = true;
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002302
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002303 if (!width.fitsOnLine() && !width.committedWidth())
2304 width.fitBelowFloats();
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002305
rniwa@webkit.org76aa5222011-05-04 13:03:32 +00002306 bool canPlaceOnLine = width.fitsOnLine() || !autoWrapWasEverTrueOnLine;
2307 if (canPlaceOnLine && checkForBreak) {
2308 width.commit();
2309 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002310 }
2311 }
2312 }
2313
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002314 if (checkForBreak && !width.fitsOnLine()) {
kociendabb0c24b2001-08-24 14:24:40 +00002315 // if we have floats, try to get below them.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002316 if (currentCharacterIsSpace && !ignoringSpaces && current.m_obj->style()->collapseWhiteSpace())
rniwa@webkit.org105350c2011-05-03 20:33:25 +00002317 trailingObjects.clear();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002318
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002319 if (width.committedWidth())
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002320 goto end;
2321
rniwa@webkit.org44424752011-04-14 00:58:40 +00002322 width.fitBelowFloats();
hyattf14a4a32002-11-21 22:06:32 +00002323
hyatta14d1742003-01-02 20:25:46 +00002324 // |width| may have been adjusted because we got shoved down past a float (thus
2325 // giving us more room), so we need to retest, and only jump to
2326 // the end label if we still don't fit on the line. -dwh
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002327 if (!width.fitsOnLine())
hyatta14d1742003-01-02 20:25:46 +00002328 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002329 }
hyatt1d9e29b2003-04-10 01:48:53 +00002330
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002331 if (!current.m_obj->isFloatingOrPositioned()) {
2332 last = current.m_obj;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002333 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
rniwa@webkit.org58925e82011-04-12 21:32:58 +00002334 width.commit();
eric@webkit.orgbd143592011-03-29 17:44:41 +00002335 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002336 }
hyatt711fe232002-11-20 21:25:14 +00002337 }
2338
hyatta9f48e32003-02-03 22:48:01 +00002339 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2340 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002341 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002342 currentCharacterIsSpace = false;
eric@webkit.org060caf62011-05-03 22:11:39 +00002343
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002344 current.moveToStartOf(next);
ddkilzere8759ef2007-03-25 06:28:19 +00002345 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002346 }
eric@webkit.org060caf62011-05-03 22:11:39 +00002347
rniwa@webkit.org5eb8d162011-04-13 02:13:33 +00002348 if (width.fitsOnLine() || lastWS == NOWRAP)
eric@webkit.orgbd143592011-03-29 17:44:41 +00002349 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002350
2351 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002352 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002353 // we just add as much as possible
leviw@chromium.org1a508692011-05-05 00:01:11 +00002354 if (m_block->style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002355 // FIXME: Don't really understand this case.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002356 if (current.m_pos) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002357 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2358 // this code as-is is likely wrong.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002359 lBreak.m_obj = current.m_obj;
2360 lBreak.m_pos = current.m_pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002361 } else
2362 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002363 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002364 // Don't ever break in the middle of a word if we can help it.
2365 // There's no room at all. We just have to be on this line,
2366 // even though we'll spill out.
rniwa@webkit.org15b91522011-05-04 00:38:05 +00002367 lBreak.moveTo(current.m_obj, current.m_pos);
kociendabb0c24b2001-08-24 14:24:40 +00002368 }
2369 }
2370
2371 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002372 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002373 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002374
hyattfe99c872003-07-31 22:25:29 +00002375 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002376 checkMidpoints(lineMidpointState, lBreak);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002377
mitz@apple.come98acc92011-05-22 04:44:27 +00002378 trailingObjects.updateMidpointsForTrailingBoxes(lineMidpointState, lBreak, TrailingObjects::CollapseFirstSpace);
rjwc9c257d2003-01-24 03:46:17 +00002379
mjs54b64002003-04-02 02:59:02 +00002380 // We might have made lBreak an iterator that points past the end
2381 // of the object. Do this adjustment to make it point to the start
2382 // of the next object instead to avoid confusing the rest of the
2383 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002384 if (lBreak.m_pos > 0) {
2385 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002386 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002387 }
2388
kociendabb0c24b2001-08-24 14:24:40 +00002389 return lBreak;
2390}
2391
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002392void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002393{
hyatt@apple.com592848f2010-12-06 20:03:43 +00002394 int endPadding = hasOverflowClip() ? paddingEnd() : 0;
2395 // FIXME: Need to find another way to do this, since scrollbars could show when we don't want them to.
commit-queue@webkit.org595681f2011-03-25 16:21:30 +00002396 if (hasOverflowClip() && !endPadding && node() && node()->rendererIsEditable() && node() == node()->rootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002397 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002398 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002399 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002400 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002401 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002402 }
2403}
2404
hyatted77ad82004-06-15 07:21:23 +00002405void RenderBlock::deleteEllipsisLineBoxes()
2406{
hyatted77ad82004-06-15 07:21:23 +00002407 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002408 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002409}
2410
2411void RenderBlock::checkLinesForTextOverflow()
2412{
2413 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002414 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002415 const Font& font = style()->font();
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002416 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002417 const Font& firstLineFont = firstLineStyle()->font();
zimmermann@webkit.org8a7e7ff2011-05-24 15:27:36 +00002418 int firstLineEllipsisWidth = firstLineFont.width(constructTextRun(this, firstLineFont, &horizontalEllipsis, 1, firstLineStyle()));
2419 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(constructTextRun(this, font, &horizontalEllipsis, 1, style()));
hyatted77ad82004-06-15 07:21:23 +00002420
2421 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2422 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2423 // check the left edge of the line box to see if it is less
2424 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002425 bool ltr = style()->isLeftToRightDirection();
hyatted77ad82004-06-15 07:21:23 +00002426 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com230b2802010-09-24 19:14:27 +00002427 int blockRightEdge = logicalRightOffsetForLine(curr->y(), curr == firstRootBox());
2428 int blockLeftEdge = logicalLeftOffsetForLine(curr->y(), curr == firstRootBox());
hyatt@apple.comc01df9e2010-09-23 19:17:33 +00002429 int lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002430 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002431 // This line spills out of our box in the appropriate direction. Now we need to see if the line
hyatted77ad82004-06-15 07:21:23 +00002432 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2433 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2434 // space.
2435 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002436 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
thakis@chromium.orga4fcdf32011-02-22 05:51:51 +00002437 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002438 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002439 }
2440 }
2441}
2442
rniwa@webkit.org44424752011-04-14 00:58:40 +00002443bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, LineWidth& width)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002444{
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002445 if (!positionNewFloats())
2446 return false;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002447
rniwa@webkit.org44424752011-04-14 00:58:40 +00002448 width.shrinkAvailableWidthForNewFloatIfNeeded(newFloat);
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002449
2450 if (!newFloat->m_paginationStrut)
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002451 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002452
2453 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2454 ASSERT(floatingObjectSet.last() == newFloat);
2455
2456 int floatLogicalTop = logicalTopForFloat(newFloat);
2457 int paginationStrut = newFloat->m_paginationStrut;
2458
2459 if (floatLogicalTop - paginationStrut != logicalHeight())
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002460 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002461
2462 FloatingObjectSetIterator it = floatingObjectSet.end();
2463 --it; // Last float is newFloat, skip that one.
2464 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2465 while (it != begin) {
2466 --it;
2467 FloatingObject* f = *it;
2468 if (f == lastFloatFromPreviousLine)
2469 break;
2470 if (logicalTopForFloat(f) == logicalHeight()) {
2471 ASSERT(!f->m_paginationStrut);
2472 f->m_paginationStrut = paginationStrut;
2473 RenderBox* o = f->m_renderer;
2474 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2475 if (o->isRenderBlock())
2476 toRenderBlock(o)->setChildNeedsLayout(true, false);
2477 o->layoutIfNeeded();
2478 setLogicalTopForFloat(f, logicalTopForFloat(f) + f->m_paginationStrut);
2479 }
2480 }
2481
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002482 setLogicalHeight(logicalHeight() + paginationStrut);
rniwa@webkit.org44424752011-04-14 00:58:40 +00002483 width.updateAvailableWidth();
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002484
rniwa@webkit.org7881ad02011-04-07 13:05:35 +00002485 return true;
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002486}
2487
hyattffe78712003-02-11 01:59:29 +00002488}