blob: 19a926916c7dbaac8ad01df154c7aa617c5b8ad4 [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"
tonyg@chromium.org9c384212011-01-19 23:13:26 +000039#include "TextRun.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000040#include "TrailingFloatsRootInlineBox.h"
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +000041#include "VerticalPositionCache.h"
darin36d11362006-04-11 16:30:21 +000042#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000043#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000044#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000045#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000046#include <wtf/Vector.h>
paroga@webkit.org10c9e1b2011-01-29 17:04:51 +000047#include <wtf/unicode/CharacterNames.h>
hyatt8c371e52004-06-16 01:19:26 +000048
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000049#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000050#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000051#include "SVGRootInlineBox.h"
52#endif
53
darin7bd70952006-04-13 07:07:34 +000054using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000055using namespace WTF;
56using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000057
darinb9481ed2006-03-20 02:57:59 +000058namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000059
hyatt1d5d87b2007-04-24 04:55:54 +000060// We don't let our line box tree for a single line get any deeper than this.
61const unsigned cMaxLineDepth = 200;
62
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000063static inline int borderPaddingMarginStart(RenderInline* child)
hyattffe78712003-02-11 01:59:29 +000064{
hyatt@apple.com0415e5d2010-10-07 17:40:25 +000065 return child->marginStart() + child->paddingStart() + child->borderStart();
hyattffe78712003-02-11 01:59:29 +000066}
67
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000068static inline int borderPaddingMarginEnd(RenderInline* child)
rniwa@webkit.org33a346a2011-04-06 17:06:14 +000069{
70 return child->marginEnd() + child->paddingEnd() + child->borderEnd();
71}
72
hyatt@apple.com0415e5d2010-10-07 17:40:25 +000073static int inlineLogicalWidth(RenderObject* child, bool start = true, bool end = true)
hyattffe78712003-02-11 01:59:29 +000074{
hyatt1d5d87b2007-04-24 04:55:54 +000075 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000076 int extraWidth = 0;
77 RenderObject* parent = child->parent();
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000078 while (parent->isRenderInline() && lineDepth++ < cMaxLineDepth) {
79 RenderInline* parentAsRenderInline = toRenderInline(parent);
hyatt@apple.com40232f82009-02-04 04:26:08 +000080 if (start && !child->previousSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000081 extraWidth += borderPaddingMarginStart(parentAsRenderInline);
hyatt@apple.com40232f82009-02-04 04:26:08 +000082 if (end && !child->nextSibling())
rniwa@webkit.org63db8cb2011-04-06 21:08:20 +000083 extraWidth += borderPaddingMarginEnd(parentAsRenderInline);
hyattffe78712003-02-11 01:59:29 +000084 child = parent;
85 parent = child->parent();
86 }
87 return extraWidth;
88}
89
hyatt@apple.comb3466af2009-06-13 06:04:40 +000090static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +000091{
92 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +000093 // shave it off the list, and shave off a trailing space if the previous end point doesn't
94 // preserve whitespace.
eric@webkit.org8c25a592011-03-29 13:18:11 +000095 if (lBreak.m_obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000096 InlineIterator* midpoints = lineMidpointState.midpoints.data();
97 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
98 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +000099 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000100 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000101 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000102 if (currpoint == lBreak) {
103 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000104 lineMidpointState.numMidpoints--;
eric@webkit.org8c25a592011-03-29 13:18:11 +0000105 if (endpoint.m_obj->style()->collapseWhiteSpace())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000106 endpoint.m_pos--;
hyattfe99c872003-07-31 22:25:29 +0000107 }
108 }
109}
110
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000111static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000112{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000113 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
114 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000115
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000116 InlineIterator* midpoints = lineMidpointState.midpoints.data();
117 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000118}
119
hyatt@apple.com71eeb442010-02-11 20:05:51 +0000120void RenderBlock::appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000121{
hyatt98ee7e42003-05-14 01:39:15 +0000122 if (start > end || obj->isFloating() ||
hyatt@apple.come16585a2011-02-28 23:21:36 +0000123 (obj->isPositioned() && !obj->style()->hasAutoLeftAndRight() && !obj->style()->hasAutoTopAndBottom() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000124 return;
hyatt85586af2003-02-19 23:22:42 +0000125
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000126 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000127 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000128 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000129 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000130 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
131 if (lineMidpointState.betweenMidpoints) {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000132 if (!(haveNextMidpoint && nextMidpoint.m_obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000133 return;
134 // This is a new start point. Stop ignoring objects and
135 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000136 lineMidpointState.betweenMidpoints = false;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000137 start = nextMidpoint.m_pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000138 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000139 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000140 return appendRunsForObject(start, end, obj, resolver);
141 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +0000142 if (!haveNextMidpoint || (obj != nextMidpoint.m_obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000143 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000144 return;
145 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000146
hyatt78b85132004-03-29 20:07:45 +0000147 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000148 // need to go ahead and append a run with our endpoint.
eric@webkit.org86a865a2011-03-29 15:30:41 +0000149 if (static_cast<int>(nextMidpoint.m_pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000150 lineMidpointState.betweenMidpoints = true;
151 lineMidpointState.currentMidpoint++;
eric@webkit.org86a865a2011-03-29 15:30:41 +0000152 if (nextMidpoint.m_pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
153 if (static_cast<int>(nextMidpoint.m_pos + 1) > start)
mitz@apple.com15035e62008-07-05 20:44:44 +0000154 resolver.addRun(new (obj->renderArena())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000155 BidiRun(start, nextMidpoint.m_pos + 1, obj, resolver.context(), resolver.dir()));
156 return appendRunsForObject(nextMidpoint.m_pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000157 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000158 } else
159 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000160 }
161}
162
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000163static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
164{
165 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000166 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000167
168 if (obj->isText()) {
169 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
170 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
171 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
172 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000173 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000174 return textBox;
175 }
176
177 if (obj->isBox())
178 return toRenderBox(obj)->createInlineBox();
179
eric@webkit.org49b9d952009-07-03 01:29:07 +0000180 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000181}
182
183static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
184{
185 if (o->isText()) {
carol.szabo@nokia.comeaaf1992011-03-02 19:34:00 +0000186 if (o->preferredLogicalWidthsDirty() && (o->isCounter() || o->isQuote()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000187 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 +0000188 toRenderText(o)->dirtyLineBoxes(fullLayout);
189 } else
190 toRenderInline(o)->dirtyLineBoxes(fullLayout);
191}
192
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000193static bool parentIsConstructedOrHaveNext(InlineFlowBox* parentBox)
194{
195 do {
196 if (parentBox->isConstructed() || parentBox->nextOnLine())
197 return true;
198 parentBox = parentBox->parent();
199 } while (parentBox);
200 return false;
201}
202
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000203InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine, InlineBox* childBox)
hyattffe78712003-02-11 01:59:29 +0000204{
205 // See if we have an unconstructed line box for this object that is also
206 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000207 unsigned lineDepth = 1;
hyatt1d5d87b2007-04-24 04:55:54 +0000208 InlineFlowBox* parentBox = 0;
209 InlineFlowBox* result = 0;
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000210 bool hasDefaultLineBoxContain = style()->lineBoxContain() == RenderStyle::initialLineBoxContain();
hyatt1d5d87b2007-04-24 04:55:54 +0000211 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000212 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000213
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000214 RenderInline* inlineFlow = (obj != this) ? toRenderInline(obj) : 0;
215
hyatt1d5d87b2007-04-24 04:55:54 +0000216 // Get the last box we made for this render object.
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000217 parentBox = inlineFlow ? inlineFlow->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000218
xji@chromium.orgb0ad6eb822011-02-01 20:02:06 +0000219 // If this box or its ancestor is constructed then it is from a previous line, and we need
220 // 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 +0000221 // something following it on the line, then we know we have to make a new box
222 // as well. In this situation our inline has actually been split in two on
223 // the same line (this can happen with very fancy language mixtures).
224 bool constructedNewBox = false;
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000225 bool allowedToConstructNewBox = !hasDefaultLineBoxContain || !inlineFlow || inlineFlow->alwaysCreateLineBoxes();
226 bool canUseExistingParentBox = parentBox && !parentIsConstructedOrHaveNext(parentBox);
227 if (allowedToConstructNewBox && !canUseExistingParentBox) {
hyatt1d5d87b2007-04-24 04:55:54 +0000228 // We need to make a new box for this render object. Once
229 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000230 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000231 ASSERT(newBox->isInlineFlowBox());
232 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000233 parentBox->setFirstLineStyleBit(firstLine);
hyatt@apple.com2a5eb212011-03-22 23:21:54 +0000234 parentBox->setIsHorizontal(isHorizontalWritingMode());
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000235 if (!hasDefaultLineBoxContain)
236 parentBox->clearDescendantsHaveSameLineHeightAndBaseline();
hyatt1d5d87b2007-04-24 04:55:54 +0000237 constructedNewBox = true;
238 }
mitz@apple.come1364202008-02-28 01:06:41 +0000239
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000240 if (constructedNewBox || canUseExistingParentBox) {
241 if (!result)
242 result = parentBox;
hyatt1d5d87b2007-04-24 04:55:54 +0000243
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000244 // If we have hit the block itself, then |box| represents the root
245 // inline box for the line, and it doesn't have to be appended to any parent
246 // inline.
247 if (childBox)
248 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000249
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000250 if (!constructedNewBox || obj == this)
251 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000252
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000253 childBox = parentBox;
254 }
mitz@apple.come1364202008-02-28 01:06:41 +0000255
hyatt1d5d87b2007-04-24 04:55:54 +0000256 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
257 // intermediate inline flows.
258 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000259
hyatt1d5d87b2007-04-24 04:55:54 +0000260 } while (true);
261
262 return result;
hyattffe78712003-02-11 01:59:29 +0000263}
264
yael.aharon@nokia.com52dacd02011-03-30 10:09:29 +0000265RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject, RenderObject* logicallyLastRunRenderer)
hyattffe78712003-02-11 01:59:29 +0000266{
mitz@apple.com887f3592008-02-25 22:03:08 +0000267 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000268
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000269 bool rootHasSelectedChildren = false;
hyattffe78712003-02-11 01:59:29 +0000270 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000271 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000272 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000273 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000274 if (runCount == 2 && !r->m_object->isListMarker())
hyatt@apple.comc0fa1632010-09-30 20:01:33 +0000275 isOnlyRun = (!style()->isLeftToRightDirection() ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000276
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000277 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000278 r->m_box = box;
279
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000280 ASSERT(box);
281 if (!box)
282 continue;
hyattffe78712003-02-11 01:59:29 +0000283
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000284 if (!rootHasSelectedChildren && box->renderer()->selectionState() != RenderObject::SelectionNone)
285 rootHasSelectedChildren = true;
286
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000287 // If we have no parent box yet, or if the run is not simply a sibling,
288 // then we need to construct inline boxes as necessary to properly enclose the
289 // run's inline box.
290 if (!parentBox || parentBox->renderer() != r->m_object->parent())
291 // Create new inline boxes all the way back to the appropriate insertion point.
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000292 parentBox = createLineBoxes(r->m_object->parent(), firstLine, box);
293 else {
294 // Append the inline box to this line.
295 parentBox->addToLine(box);
296 }
mitz@apple.com576e84e2008-04-24 19:09:48 +0000297
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000298 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000299 box->setBidiLevel(r->level());
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000300
301 if (box->isInlineTextBox()) {
302 InlineTextBox* text = static_cast<InlineTextBox*>(box);
303 text->setStart(r->m_start);
304 text->setLen(r->m_stop - r->m_start);
305 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000306 if (r->m_hasHyphen)
307 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000308 }
hyattffe78712003-02-11 01:59:29 +0000309 }
310
311 // We should have a root inline box. It should be unconstructed and
312 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000313 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000314
inferno@chromium.orge29694f2010-10-07 22:00:02 +0000315 // Set the m_selectedChildren flag on the root inline box if one of the leaf inline box
316 // from the bidi runs walk above has a selection state.
317 if (rootHasSelectedChildren)
318 lastLineBox()->root()->setHasSelectedChildren(true);
319
hyattffe78712003-02-11 01:59:29 +0000320 // Set bits on our inline flow boxes that indicate which sides should
321 // paint borders/margins/padding. This knowledge will ultimately be used when
322 // we determine the horizontal positions and widths of all the inline boxes on
323 // the line.
yael.aharon@nokia.com52dacd02011-03-30 10:09:29 +0000324 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject, logicallyLastRunRenderer);
hyattffe78712003-02-11 01:59:29 +0000325
326 // Now mark the line boxes as being constructed.
327 lastLineBox()->setConstructed();
328
329 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000330 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000331}
332
mitz@apple.com390fa322011-02-24 23:07:06 +0000333ETextAlign RenderBlock::textAlignmentForLine(bool endsWithSoftBreak) const
334{
335 ETextAlign alignment = style()->textAlign();
336 if (!endsWithSoftBreak && alignment == JUSTIFY)
337 alignment = TAAUTO;
338
339 return alignment;
340}
341
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000342static void updateLogicalWidthForLeftAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
343{
344 // The direction of the block should determine what happens with wide lines.
345 // In particular with RTL blocks, wide lines should still spill out to the left.
346 if (isLeftToRightDirection) {
347 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
348 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
349 return;
350 }
351
352 if (trailingSpaceRun)
353 trailingSpaceRun->m_box->setLogicalWidth(0);
354 else if (totalLogicalWidth > availableLogicalWidth)
355 logicalLeft -= (totalLogicalWidth - availableLogicalWidth);
356}
357
358static void updateLogicalWidthForRightAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
359{
360 // Wide lines spill out of the block based off direction.
361 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
362 // side of the block.
363 if (isLeftToRightDirection) {
364 if (trailingSpaceRun) {
365 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
366 trailingSpaceRun->m_box->setLogicalWidth(0);
367 }
368 if (totalLogicalWidth < availableLogicalWidth)
369 logicalLeft += availableLogicalWidth - totalLogicalWidth;
370 return;
371 }
372
373 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun) {
374 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
375 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
376 } else
377 logicalLeft += availableLogicalWidth - totalLogicalWidth;
378}
379
380static void updateLogicalWidthForCenterAlignedBlock(bool isLeftToRightDirection, BidiRun* trailingSpaceRun, float& logicalLeft, float& totalLogicalWidth, float availableLogicalWidth)
381{
382 float trailingSpaceWidth = 0;
383 if (trailingSpaceRun) {
384 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
385 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableLogicalWidth - totalLogicalWidth + 1) / 2);
386 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceWidth));
387 }
388 if (isLeftToRightDirection)
389 logicalLeft += max<float>((availableLogicalWidth - totalLogicalWidth) / 2, 0);
390 else
391 logicalLeft += totalLogicalWidth > availableLogicalWidth ? (availableLogicalWidth - totalLogicalWidth) : (availableLogicalWidth - totalLogicalWidth) / 2 - trailingSpaceWidth;
392}
393
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000394void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd,
395 GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000396{
mitz@apple.com390fa322011-02-24 23:07:06 +0000397 ETextAlign textAlign = textAlignmentForLine(!reachedEnd && !lineBox->endsWithBreak());
398 float logicalLeft = logicalLeftOffsetForLine(logicalHeight(), firstLine);
399 float availableLogicalWidth = logicalRightOffsetForLine(logicalHeight(), firstLine) - logicalLeft;
400
darin06dcb9c2005-08-15 04:31:09 +0000401 bool needsWordSpacing = false;
mitz@apple.com390fa322011-02-24 23:07:06 +0000402 float totalLogicalWidth = lineBox->getFlowSpacingLogicalWidth();
mitz@apple.com86470c82011-01-27 01:39:27 +0000403 unsigned expansionOpportunityCount = 0;
404 bool isAfterExpansion = true;
405 Vector<unsigned, 16> expansionOpportunities;
mitz@apple.comddb59872011-04-05 05:21:16 +0000406 RenderObject* previousObject = 0;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000407
mitz@apple.come1364202008-02-28 01:06:41 +0000408 for (BidiRun* r = firstRun; r; r = r->next()) {
409 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000410 continue; // Positioned objects are only participating to figure out their
411 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000412 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000413 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000414 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000415
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000416 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com80968932011-03-26 00:46:26 +0000417 if (!isAfterExpansion)
418 static_cast<InlineTextBox*>(r->m_box)->setCanHaveLeadingExpansion(true);
mitz@apple.com1b0578d2011-01-27 08:23:31 +0000419 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 +0000420 expansionOpportunities.append(opportunitiesInRun);
421 expansionOpportunityCount += opportunitiesInRun;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000422 }
423
mitz@apple.come1364202008-02-28 01:06:41 +0000424 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000425 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.com546a2482010-10-07 21:16:49 +0000426 totalLogicalWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000427 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000428 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000429 HashSet<const SimpleFontData*> fallbackFonts;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000430 GlyphOverflow glyphOverflow;
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000431
432 // Always compute glyph overflow if the block's line-box-contain value is "glyphs".
433 if (lineBox->fitsToGlyphs()) {
434 // If we don't stick out of the root line's font box, then don't bother computing our glyph overflow. This optimization
435 // will keep us from computing glyph bounds in nearly all cases.
436 bool includeRootLine = lineBox->includesRootLineBoxFontOrLeading();
437 int baselineShift = lineBox->verticalPositionForBox(r->m_box, verticalPositionCache);
438 int rootDescent = includeRootLine ? lineBox->renderer()->style(firstLine)->font().fontMetrics().descent() : 0;
439 int rootAscent = includeRootLine ? lineBox->renderer()->style(firstLine)->font().fontMetrics().ascent() : 0;
440 int boxAscent = rt->style(firstLine)->font().fontMetrics().ascent() - baselineShift;
441 int boxDescent = rt->style(firstLine)->font().fontMetrics().descent() + baselineShift;
442 if (boxAscent > rootDescent || boxDescent > rootAscent)
443 glyphOverflow.computeBounds = true;
444 }
445
mitz@apple.comb2107652010-06-21 16:54:52 +0000446 int hyphenWidth = 0;
447 if (static_cast<InlineTextBox*>(r->m_box)->hasHyphen()) {
448 const AtomicString& hyphenString = rt->style()->hyphenString();
449 hyphenWidth = rt->style(firstLine)->font().width(TextRun(hyphenString.characters(), hyphenString.length()));
450 }
hyatt@apple.com546a2482010-10-07 21:16:49 +0000451 r->m_box->setLogicalWidth(rt->width(r->m_start, r->m_stop - r->m_start, totalLogicalWidth, firstLine, &fallbackFonts, &glyphOverflow) + hyphenWidth);
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000452 if (!fallbackFonts.isEmpty()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000453 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000454 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
455 ASSERT(it->second.first.isEmpty());
456 copyToVector(fallbackFonts, it->second.first);
hyatt@apple.com7d4066a2011-03-29 17:56:09 +0000457 r->m_box->parent()->clearDescendantsHaveSameLineHeightAndBaseline();
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000458 }
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000459 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000460 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000461 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
462 it->second.second = glyphOverflow;
hyatt@apple.com61f25322011-03-31 20:40:48 +0000463 r->m_box->clearKnownToHaveNoOverflow();
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000464 }
mitz@apple.com86470c82011-01-27 01:39:27 +0000465 } else {
466 isAfterExpansion = false;
467 if (!r->m_object->isRenderInline()) {
468 RenderBox* renderBox = toRenderBox(r->m_object);
mitz@apple.comddb59872011-04-05 05:21:16 +0000469 if (renderBox->isRubyRun()) {
470 int startOverhang;
471 int endOverhang;
472 RenderObject* nextObject = 0;
473 for (BidiRun* runWithNextObject = r->next(); runWithNextObject; runWithNextObject = runWithNextObject->next()) {
474 if (!runWithNextObject->m_object->isPositioned() && !runWithNextObject->m_box->isLineBreak()) {
475 nextObject = runWithNextObject->m_object;
476 break;
477 }
478 }
479 toRenderRubyRun(renderBox)->getOverhang(firstLine, renderBox->style()->isLeftToRightDirection() ? previousObject : nextObject, renderBox->style()->isLeftToRightDirection() ? nextObject : previousObject, startOverhang, endOverhang);
480 setMarginStartForChild(renderBox, -startOverhang);
481 setMarginEndForChild(renderBox, -endOverhang);
482 }
mitz@apple.com86470c82011-01-27 01:39:27 +0000483 r->m_box->setLogicalWidth(logicalWidthForChild(renderBox));
484 totalLogicalWidth += marginStartForChild(renderBox) + marginEndForChild(renderBox);
485 }
hyattffe78712003-02-11 01:59:29 +0000486 }
hyatt4b381692003-03-10 21:11:59 +0000487
hyatt@apple.com546a2482010-10-07 21:16:49 +0000488 totalLogicalWidth += r->m_box->logicalWidth();
mitz@apple.comddb59872011-04-05 05:21:16 +0000489 previousObject = r->m_object;
hyattffe78712003-02-11 01:59:29 +0000490 }
491
mitz@apple.com86470c82011-01-27 01:39:27 +0000492 if (isAfterExpansion && !expansionOpportunities.isEmpty()) {
493 expansionOpportunities.last()--;
494 expansionOpportunityCount--;
495 }
496
hyattffe78712003-02-11 01:59:29 +0000497 // Armed with the total width of the line (without justification),
498 // we now examine our text-align property in order to determine where to position the
499 // objects horizontally. The total width of the line can be increased if we end up
500 // justifying text.
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000501 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000502 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000503 case WEBKIT_LEFT:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000504 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000505 break;
506 case JUSTIFY:
mitz@apple.coma70af822011-02-25 02:21:20 +0000507 adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, availableLogicalWidth);
mitz@apple.com390fa322011-02-24 23:07:06 +0000508 if (expansionOpportunityCount) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000509 if (trailingSpaceRun) {
hyatt@apple.com546a2482010-10-07 21:16:49 +0000510 totalLogicalWidth -= trailingSpaceRun->m_box->logicalWidth();
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000511 trailingSpaceRun->m_box->setLogicalWidth(0);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000512 }
hyattffe78712003-02-11 01:59:29 +0000513 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000514 }
hyattffe78712003-02-11 01:59:29 +0000515 // fall through
516 case TAAUTO:
hyattffe78712003-02-11 01:59:29 +0000517 // for right to left fall through to right aligned
hyatt@apple.comc0fa1632010-09-30 20:01:33 +0000518 if (style()->isLeftToRightDirection()) {
hyatt@apple.com546a2482010-10-07 21:16:49 +0000519 if (totalLogicalWidth > availableLogicalWidth && trailingSpaceRun)
hyatt@apple.com0acc9352011-02-17 19:19:07 +0000520 trailingSpaceRun->m_box->setLogicalWidth(max<float>(0, trailingSpaceRun->m_box->logicalWidth() - totalLogicalWidth + availableLogicalWidth));
hyattffe78712003-02-11 01:59:29 +0000521 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000522 }
hyattffe78712003-02-11 01:59:29 +0000523 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000524 case WEBKIT_RIGHT:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000525 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000526 break;
527 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000528 case WEBKIT_CENTER:
rniwa@webkit.orgcda6dbd2011-03-28 09:19:50 +0000529 updateLogicalWidthForCenterAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
hyattffe78712003-02-11 01:59:29 +0000530 break;
rniwa@webkit.org0b6f81f2011-03-28 12:04:56 +0000531 case TASTART:
532 if (style()->isLeftToRightDirection())
533 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
534 else
535 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
536 break;
537 case TAEND:
538 if (style()->isLeftToRightDirection())
539 updateLogicalWidthForRightAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
540 else
541 updateLogicalWidthForLeftAlignedBlock(style()->isLeftToRightDirection(), trailingSpaceRun, logicalLeft, totalLogicalWidth, availableLogicalWidth);
542 break;
hyattffe78712003-02-11 01:59:29 +0000543 }
544
mitz@apple.com80968932011-03-26 00:46:26 +0000545 if (expansionOpportunityCount && availableLogicalWidth > totalLogicalWidth) {
mitz@apple.com86470c82011-01-27 01:39:27 +0000546 size_t i = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000547 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000548 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000549 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000550
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000551 if (r->m_object->isText()) {
mitz@apple.com86470c82011-01-27 01:39:27 +0000552 unsigned opportunitiesInRun = expansionOpportunities[i++];
hyatt870bdda2003-05-21 23:37:33 +0000553
mitz@apple.com86470c82011-01-27 01:39:27 +0000554 ASSERT(opportunitiesInRun <= expansionOpportunityCount);
hyatt870bdda2003-05-21 23:37:33 +0000555
hyattdca76e92005-11-02 08:52:50 +0000556 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000557 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com86470c82011-01-27 01:39:27 +0000558 InlineTextBox* textBox = static_cast<InlineTextBox*>(r->m_box);
hyatt@apple.com0acc9352011-02-17 19:19:07 +0000559 float expansion = (availableLogicalWidth - totalLogicalWidth) * opportunitiesInRun / expansionOpportunityCount;
mitz@apple.com86470c82011-01-27 01:39:27 +0000560 textBox->setExpansion(expansion);
561 totalLogicalWidth += expansion;
hyatt870bdda2003-05-21 23:37:33 +0000562 }
mitz@apple.com86470c82011-01-27 01:39:27 +0000563 expansionOpportunityCount -= opportunitiesInRun;
564 if (!expansionOpportunityCount)
mitz@apple.come1364202008-02-28 01:06:41 +0000565 break;
hyattacbb0d42003-04-25 01:32:49 +0000566 }
hyattffe78712003-02-11 01:59:29 +0000567 }
hyattffe78712003-02-11 01:59:29 +0000568 }
mitz@apple.come1364202008-02-28 01:06:41 +0000569
hyattffe78712003-02-11 01:59:29 +0000570 // The widths of all runs are now known. We can now place every inline box (and
571 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000572 needsWordSpacing = false;
hyatt@apple.com546a2482010-10-07 21:16:49 +0000573 lineBox->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000574}
575
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000576void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
577 VerticalPositionCache& verticalPositionCache)
hyattffe78712003-02-11 01:59:29 +0000578{
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000579 setLogicalHeight(lineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache));
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000580 lineBox->setBlockLogicalHeight(logicalHeight());
hyattffe78712003-02-11 01:59:29 +0000581
582 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000583 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000584 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000585 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000586 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000587
hyatt98ee7e42003-05-14 01:39:15 +0000588 // Align positioned boxes with the top of the line box. This is
589 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000590 if (r->m_object->isPositioned())
hyatt@apple.com35d2ad52010-10-20 18:17:36 +0000591 r->m_box->setLogicalTop(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +0000592
593 // Position is used to properly position both replaced elements and
594 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000595 if (r->m_object->isText())
596 toRenderText(r->m_object)->positionLineBox(r->m_box);
597 else if (r->m_object->isBox())
598 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000599 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000600 // Positioned objects and zero-length text nodes destroy their boxes in
601 // position(), which unnecessarily dirties the line.
602 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000603}
kociendabb0c24b2001-08-24 14:24:40 +0000604
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000605static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
606{
607 if (character == ' ' || character == '\t' || character == softHyphen)
608 return true;
609 if (character == '\n')
610 return !renderer->style()->preserveNewline();
611 if (character == noBreakSpace)
612 return renderer->style()->nbspMode() == SPACE;
613 return false;
614}
615
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000616
617static void setStaticPositions(RenderBlock* block, RenderBox* child)
618{
619 // FIXME: The math here is actually not really right. It's a best-guess approximation that
620 // will work for the common cases
621 RenderObject* containerBlock = child->container();
622 int blockHeight = block->logicalHeight();
623 if (containerBlock->isRenderInline()) {
624 // A relative positioned inline encloses us. In this case, we also have to determine our
625 // position as though we were an inline. Set |staticInlinePosition| and |staticBlockPosition| on the relative positioned
626 // inline so that we can obtain the value later.
627 toRenderInline(containerBlock)->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
628 toRenderInline(containerBlock)->layer()->setStaticBlockPosition(blockHeight);
629 }
630
631 if (child->style()->isOriginalDisplayInlineType())
632 child->layer()->setStaticInlinePosition(block->startOffsetForLine(blockHeight, false));
633 else
634 child->layer()->setStaticInlinePosition(block->borderAndPaddingStart());
635 child->layer()->setStaticBlockPosition(blockHeight);
636}
637
yael.aharon@nokia.com52dacd02011-03-30 10:09:29 +0000638static bool reachedEndOfTextRenderer(InlineBidiResolver& resolver)
639{
640 BidiRun* run = resolver.logicallyLastRun();
641 if (!run)
642 return true;
643 unsigned int pos = run->stop();
644 RenderObject* r = run->m_object;
yael.aharon@nokia.comcc45cbf2011-03-30 17:10:25 +0000645 if (!r->isText() || r->isBR())
yael.aharon@nokia.com52dacd02011-03-30 10:09:29 +0000646 return false;
647 RenderText* renderText = toRenderText(r);
648 if (pos >= renderText->textLength())
649 return true;
650
651 while (isASCIISpace(renderText->characters()[pos])) {
652 pos++;
653 if (pos >= renderText->textLength())
654 return true;
655 }
656 return false;
657}
658
eric@webkit.org0894bb82011-04-03 08:29:40 +0000659inline BidiRun* RenderBlock::handleTrailingSpaces(InlineBidiResolver& resolver)
660{
661 if (!resolver.runCount()
662 || !resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
663 || !resolver.logicallyLastRun()->m_object->style()->autoWrap())
664 return 0;
665
666 BidiRun* trailingSpaceRun = resolver.logicallyLastRun();
667 RenderObject* lastObject = trailingSpaceRun->m_object;
668 if (!lastObject->isText())
669 return 0;
670
671 RenderText* lastText = toRenderText(lastObject);
672 const UChar* characters = lastText->characters();
673 int firstSpace = trailingSpaceRun->stop();
674 while (firstSpace > trailingSpaceRun->start()) {
675 UChar current = characters[firstSpace - 1];
676 if (!isCollapsibleSpace(current, lastText))
677 break;
678 firstSpace--;
679 }
680 if (firstSpace == trailingSpaceRun->stop())
681 return 0;
682
683 TextDirection direction = style()->direction();
684 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
685 if (firstSpace != trailingSpaceRun->start()) {
686 BidiContext* baseContext = resolver.context();
687 while (BidiContext* parent = baseContext->parent())
688 baseContext = parent;
689
690 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
691 trailingSpaceRun->m_stop = firstSpace;
692 if (direction == LTR)
693 resolver.addRun(newTrailingRun);
694 else
695 resolver.prependRun(newTrailingRun);
696 trailingSpaceRun = newTrailingRun;
697 return trailingSpaceRun;
698 }
699 if (!shouldReorder)
700 return trailingSpaceRun;
701
702 if (direction == LTR) {
703 resolver.moveRunToEnd(trailingSpaceRun);
704 trailingSpaceRun->m_level = 0;
705 } else {
706 resolver.moveRunToBeginning(trailingSpaceRun);
707 trailingSpaceRun->m_level = 1;
708 }
709 return trailingSpaceRun;
710}
711
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000712void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintLogicalTop, int& repaintLogicalBottom)
jamesr@google.com19548ad2010-04-02 23:21:35 +0000713{
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000714 bool useRepaintBounds = false;
715
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000716 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000717
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000718 setLogicalHeight(borderBefore() + paddingBefore());
mitz@apple.come1364202008-02-28 01:06:41 +0000719
hyatt0c3a9862004-02-23 21:26:26 +0000720 // Figure out if we should clear out our line boxes.
721 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000722 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000723 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000724 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000725
hyatted77ad82004-06-15 07:21:23 +0000726 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
727 // clip.
728 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
729 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
730 // anyway, so we won't worry about following the draft here.
731 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000732
hyatted77ad82004-06-15 07:21:23 +0000733 // Walk all the lines and delete our ellipsis line boxes if they exist.
734 if (hasTextOverflow)
735 deleteEllipsisLineBoxes();
736
hyattffe78712003-02-11 01:59:29 +0000737 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000738 // layout replaced elements
739 bool endOfInline = false;
740 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000741 Vector<FloatWithRect> floats;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000742 bool hasInlineChild = false;
743 while (o) {
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +0000744 if (!hasInlineChild && o->isInline())
745 hasInlineChild = true;
746
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000747 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
748 RenderBox* box = toRenderBox(o);
749
750 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
751 o->setChildNeedsLayout(true, false);
752
753 // 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 +0000754 if (relayoutChildren && (o->style()->paddingStart().isPercent() || o->style()->paddingEnd().isPercent()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000755 o->setPreferredLogicalWidthsDirty(true, false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000756
757 if (o->isPositioned())
758 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000759 else if (o->isFloating())
760 floats.append(FloatWithRect(box));
761 else if (fullLayout || o->needsLayout()) {
762 // Replaced elements
763 toRenderBox(o)->dirtyLineBoxes(fullLayout);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000764 o->layoutIfNeeded();
765 }
766 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
hyatt@apple.coma61b8a32011-04-06 18:20:52 +0000767 if (!o->isText())
768 toRenderInline(o)->updateAlwaysCreateLineBoxes();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000769 if (fullLayout || o->selfNeedsLayout())
770 dirtyLineBoxesForRenderer(o, fullLayout);
771 o->setNeedsLayout(false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000772 }
773 o = bidiNext(this, o, 0, false, &endOfInline);
774 }
775
776 // We want to skip ahead to the first dirty line
777 InlineBidiResolver resolver;
778 unsigned floatIndex;
779 bool firstLine = true;
780 bool previousLineBrokeCleanly = true;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000781 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex,
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000782 useRepaintBounds, repaintLogicalTop, repaintLogicalBottom);
mitz@apple.come1364202008-02-28 01:06:41 +0000783
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000784 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000785 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
786 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000787 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000788 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000789 // Because we waited until we were already inside layout to discover
790 // that the block really needed a full layout, we missed our chance to repaint the layer
791 // before layout started. Luckily the layer has cached the repaint rect for its original
792 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000793 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000794 }
795 }
hyatt0c3a9862004-02-23 21:26:26 +0000796
commit-queue@webkit.org2984df92011-03-04 23:07:15 +0000797 FloatingObject* lastFloat = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000798
799 LineMidpointState& lineMidpointState = resolver.midpointState();
800
801 // We also find the first clean line and extract these lines. We will add them back
802 // if we determine that we're able to synchronize after handling all our dirty lines.
803 InlineIterator cleanLineStart;
804 BidiStatus cleanLineBidiStatus;
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000805 int endLineLogicalTop = 0;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000806 RootInlineBox* endLine = (fullLayout || !startLine) ?
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +0000807 0 : determineEndPosition(startLine, floats, floatIndex, cleanLineStart, cleanLineBidiStatus, endLineLogicalTop);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000808
809 if (startLine) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000810 if (!useRepaintBounds) {
811 useRepaintBounds = true;
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000812 repaintLogicalTop = logicalHeight();
813 repaintLogicalBottom = logicalHeight();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000814 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000815 RenderArena* arena = renderArena();
816 RootInlineBox* box = startLine;
817 while (box) {
hyatt@apple.com61f25322011-03-31 20:40:48 +0000818 repaintLogicalTop = min(repaintLogicalTop, box->logicalTopVisualOverflow());
819 repaintLogicalBottom = max(repaintLogicalBottom, box->logicalBottomVisualOverflow());
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000820 RootInlineBox* next = box->nextRootBox();
821 box->deleteLine(arena);
822 box = next;
823 }
824 }
825
826 InlineIterator end = resolver.position();
827
828 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
829 // If the last line before the start line ends with a line break that clear floats,
830 // adjust the height accordingly.
831 // A line break can be either the first or the last object on a line, depending on its direction.
832 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
833 RenderObject* lastObject = lastLeafChild->renderer();
834 if (!lastObject->isBR())
835 lastObject = lastRootBox()->firstLeafChild()->renderer();
836 if (lastObject->isBR()) {
837 EClear clear = lastObject->style()->clear();
838 if (clear != CNONE)
839 newLine(clear);
840 }
841 }
842 }
843
844 bool endLineMatched = false;
845 bool checkForEndLineMatch = endLine;
846 bool checkForFloatsFromLastLine = false;
847
848 bool isLineEmpty = true;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000849 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000850
mitz@apple.com44fc5132011-02-25 18:34:15 +0000851 LineBreakIteratorInfo lineBreakIteratorInfo;
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000852 VerticalPositionCache verticalPositionCache;
853
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000854 while (!end.atEnd()) {
855 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000856 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineLogicalTop, repaintLogicalBottom, repaintLogicalTop)))
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000857 break;
858
859 lineMidpointState.reset();
860
861 isLineEmpty = true;
862
863 EClear clear = CNONE;
mitz@apple.comb2107652010-06-21 16:54:52 +0000864 bool hyphenated;
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000865 Vector<RenderBox*> positionedObjects;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000866
867 InlineIterator oldEnd = end;
commit-queue@webkit.org2984df92011-03-04 23:07:15 +0000868 FloatingObject* lastFloatFromPreviousLine = (m_floatingObjects && !m_floatingObjects->set().isEmpty()) ? m_floatingObjects->set().last() : 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000869 end = findNextLineBreak(resolver, firstLine, isLineEmpty, lineBreakIteratorInfo, previousLineBrokeCleanly, hyphenated, &clear, lastFloatFromPreviousLine, positionedObjects);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000870 if (resolver.position().atEnd()) {
871 resolver.deleteRuns();
872 checkForFloatsFromLastLine = true;
873 break;
874 }
875 ASSERT(end != resolver.position());
876
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +0000877 if (isLineEmpty) {
878 if (lastRootBox())
eric@webkit.org86a865a2011-03-29 15:30:41 +0000879 lastRootBox()->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +0000880 } else {
xji@chromium.org6b0c0172011-02-14 19:21:12 +0000881 VisualDirectionOverride override = (style()->visuallyOrdered() ? (style()->direction() == LTR ? VisualLeftToRightOverride : VisualRightToLeftOverride) : NoVisualOverride);
882 resolver.createBidiRunsForLine(end, override, previousLineBrokeCleanly);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000883 ASSERT(resolver.position() == end);
884
eric@webkit.org0894bb82011-04-03 08:29:40 +0000885 BidiRun* trailingSpaceRun = !previousLineBrokeCleanly ? handleTrailingSpaces(resolver) : 0;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000886
887 // Now that the runs have been ordered, we create the line boxes.
888 // At the same time we figure out where border/padding/margin should be applied for
889 // inline flow boxes.
890
891 RootInlineBox* lineBox = 0;
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000892 int oldLogicalHeight = logicalHeight();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000893 if (resolver.runCount()) {
mitz@apple.comb2107652010-06-21 16:54:52 +0000894 if (hyphenated)
895 resolver.logicallyLastRun()->m_hasHyphen = true;
yael.aharon@nokia.com52dacd02011-03-30 10:09:29 +0000896 bool lastLine = end.m_obj && end.m_obj->isText() ? reachedEndOfTextRenderer(resolver) : !end.m_obj;
897 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, lastLine, end.m_obj && !end.m_pos ? end.m_obj : 0, resolver.logicallyLastRun()->m_object);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000898 if (lineBox) {
899 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
900
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000901#if ENABLE(SVG)
902 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
903#else
904 bool isSVGRootInlineBox = false;
905#endif
906
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000907 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000908
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000909 // Now we position all of our text runs horizontally.
910 if (!isSVGRootInlineBox)
hyatt@apple.com9bcbad62011-03-22 19:03:50 +0000911 computeInlineDirectionPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap, verticalPositionCache);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000912
913 // Now position our text runs vertically.
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +0000914 computeBlockDirectionPositionsForLine(lineBox, resolver.firstRun(), textBoxDataMap, verticalPositionCache);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000915
916#if ENABLE(SVG)
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000917 // SVG text layout code computes vertical & horizontal positions on its own.
918 // Note that we still need to execute computeVerticalPositionsForLine() as
919 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
920 // contains reversed text or not. If we wouldn't do that editing and thus
921 // text selection in RTL boxes would not work as expected.
922 if (isSVGRootInlineBox) {
923 ASSERT(isSVGText());
924 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
925 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000926#endif
927
hyatt@apple.com592848f2010-12-06 20:03:43 +0000928 // Compute our overflow now.
hyatt@apple.com61f25322011-03-31 20:40:48 +0000929 lineBox->computeOverflow(lineBox->lineTop(), lineBox->lineBottom(), textBoxDataMap);
hyatt@apple.com592848f2010-12-06 20:03:43 +0000930
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000931#if PLATFORM(MAC)
932 // Highlight acts as an overflow inflation.
933 if (style()->highlight() != nullAtom)
934 lineBox->addHighlightOverflow();
935#endif
936 }
937 }
938
939 resolver.deleteRuns();
940
941 if (lineBox) {
eric@webkit.org86a865a2011-03-29 15:30:41 +0000942 lineBox->setLineBreakInfo(end.m_obj, end.m_pos, resolver.status());
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000943 if (useRepaintBounds) {
hyatt@apple.com61f25322011-03-31 20:40:48 +0000944 repaintLogicalTop = min(repaintLogicalTop, lineBox->logicalTopVisualOverflow());
945 repaintLogicalBottom = max(repaintLogicalBottom, lineBox->logicalBottomVisualOverflow());
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000946 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000947
948 if (paginated) {
949 int adjustment = 0;
950 adjustLinePositionForPagination(lineBox, adjustment);
951 if (adjustment) {
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000952 int oldLineWidth = availableLogicalWidthForLine(oldLogicalHeight, firstLine);
hyatt@apple.com61bbedf2011-01-26 23:10:57 +0000953 lineBox->adjustBlockDirectionPosition(adjustment);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000954 if (useRepaintBounds) // This can only be a positive adjustment, so no need to update repaintTop.
hyatt@apple.com61f25322011-03-31 20:40:48 +0000955 repaintLogicalBottom = max(repaintLogicalBottom, lineBox->logicalBottomVisualOverflow());
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000956
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000957 if (availableLogicalWidthForLine(oldLogicalHeight + adjustment, firstLine) != oldLineWidth) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000958 // We have to delete this line, remove all floats that got added, and let line layout re-run.
959 lineBox->deleteLine(renderArena());
hyatt@apple.com81c1d742010-10-06 21:44:02 +0000960 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldLogicalHeight);
961 setLogicalHeight(oldLogicalHeight + adjustment);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000962 resolver.setPosition(oldEnd);
963 end = oldEnd;
964 continue;
965 }
966
hyatt@apple.coma956e332010-10-06 20:35:21 +0000967 setLogicalHeight(lineBox->blockLogicalHeight());
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000968 }
969 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000970 }
971
hyatt@apple.com14e332d2011-03-25 21:57:07 +0000972 for (size_t i = 0; i < positionedObjects.size(); ++i)
973 setStaticPositions(this, positionedObjects[i]);
974
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000975 firstLine = false;
976 newLine(clear);
977 }
978
979 if (m_floatingObjects && lastRootBox()) {
commit-queue@webkit.org2984df92011-03-04 23:07:15 +0000980 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
981 FloatingObjectSetIterator it = floatingObjectSet.begin();
982 FloatingObjectSetIterator end = floatingObjectSet.end();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000983 if (lastFloat) {
commit-queue@webkit.org2984df92011-03-04 23:07:15 +0000984 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(lastFloat);
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +0000985 ASSERT(lastFloatIterator != end);
986 ++lastFloatIterator;
987 it = lastFloatIterator;
988 }
989 for (; it != end; ++it) {
990 FloatingObject* f = *it;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000991 lastRootBox()->floats().append(f->m_renderer);
992 ASSERT(f->m_renderer == floats[floatIndex].object);
993 // If a float's geometry has changed, give up on syncing with clean lines.
hyatt@apple.comf382f2e2010-10-01 21:25:39 +0000994 if (floats[floatIndex].rect != f->frameRect())
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000995 checkForEndLineMatch = false;
996 floatIndex++;
997 }
commit-queue@webkit.org2984df92011-03-04 23:07:15 +0000998 lastFloat = !floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000999 }
1000
1001 lineMidpointState.reset();
1002 resolver.setPosition(end);
1003 }
1004
1005 if (endLine) {
1006 if (endLineMatched) {
1007 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001008 int delta = logicalHeight() - endLineLogicalTop;
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001009 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1010 line->attachLine();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001011 if (paginated) {
1012 delta -= line->paginationStrut();
1013 adjustLinePositionForPagination(line, delta);
1014 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001015 if (delta) {
hyatt@apple.com61f25322011-03-31 20:40:48 +00001016 repaintLogicalTop = min(repaintLogicalTop, line->logicalTopVisualOverflow() + min(delta, 0));
1017 repaintLogicalBottom = max(repaintLogicalBottom, line->logicalBottomVisualOverflow() + max(delta, 0));
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001018 line->adjustBlockDirectionPosition(delta);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001019 }
1020 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1021 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1022 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001023 insertFloatingObject(*f);
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001024 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f) + delta);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001025 positionNewFloats();
1026 }
1027 }
1028 }
hyatt@apple.coma956e332010-10-06 20:35:21 +00001029 setLogicalHeight(lastRootBox()->blockLogicalHeight());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001030 } else {
1031 // Delete all the remaining lines.
1032 RootInlineBox* line = endLine;
1033 RenderArena* arena = renderArena();
1034 while (line) {
hyatt@apple.com61f25322011-03-31 20:40:48 +00001035 repaintLogicalTop = min(repaintLogicalTop, line->logicalTopVisualOverflow());
1036 repaintLogicalBottom = max(repaintLogicalBottom, line->logicalBottomVisualOverflow());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001037 RootInlineBox* next = line->nextRootBox();
1038 line->deleteLine(arena);
1039 line = next;
1040 }
1041 }
1042 }
1043 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
1044 // In case we have a float on the last line, it might not be positioned up to now.
1045 // This has to be done before adding in the bottom border/padding, or the float will
1046 // include the padding incorrectly. -dwh
1047 if (checkForFloatsFromLastLine) {
hyatt@apple.com61f25322011-03-31 20:40:48 +00001048 int bottomVisualOverflow = lastRootBox()->logicalTopLayoutOverflow();
1049 int bottomLayoutOverflow = lastRootBox()->logicalBottomLayoutOverflow();
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +00001050 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001051 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1052 trailingFloatsLineBox->setConstructed();
mitz@apple.com0cc7bed2010-05-17 01:43:49 +00001053 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
hyatt@apple.com4a9c625a2010-11-17 20:55:40 +00001054 VerticalPositionCache verticalPositionCache;
1055 trailingFloatsLineBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
hyatt@apple.com592848f2010-12-06 20:03:43 +00001056 IntRect logicalLayoutOverflow(0, logicalHeight(), 1, bottomLayoutOverflow);
1057 IntRect logicalVisualOverflow(0, logicalHeight(), 1, bottomVisualOverflow);
hyatt@apple.com61f25322011-03-31 20:40:48 +00001058 trailingFloatsLineBox->setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, trailingFloatsLineBox->lineTop(), trailingFloatsLineBox->lineBottom());
hyatt@apple.com81c1d742010-10-06 21:44:02 +00001059 trailingFloatsLineBox->setBlockLogicalHeight(logicalHeight());
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001060 }
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001061
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001062 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1063 FloatingObjectSetIterator it = floatingObjectSet.begin();
1064 FloatingObjectSetIterator end = floatingObjectSet.end();
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001065 if (lastFloat) {
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001066 FloatingObjectSetIterator lastFloatIterator = floatingObjectSet.find(lastFloat);
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001067 ASSERT(lastFloatIterator != end);
1068 ++lastFloatIterator;
1069 it = lastFloatIterator;
1070 }
1071 for (; it != end; ++it)
1072 lastRootBox()->floats().append((*it)->m_renderer);
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001073 lastFloat = !floatingObjectSet.isEmpty() ? floatingObjectSet.last() : 0;
abarth@webkit.org31d23df2010-04-05 21:52:09 +00001074 }
1075 size_t floatCount = floats.size();
1076 // Floats that did not have layout did not repaint when we laid them out. They would have
1077 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1078 // painted.
1079 for (size_t i = 0; i < floatCount; ++i) {
1080 if (!floats[i].everHadLayout) {
1081 RenderBox* f = floats[i].object;
1082 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1083 f->repaint();
1084 }
1085 }
kociendabb0c24b2001-08-24 14:24:40 +00001086 }
hyatt85586af2003-02-19 23:22:42 +00001087
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001088 // Expand the last line to accommodate Ruby and emphasis marks.
1089 int lastLineAnnotationsAdjustment = 0;
1090 if (lastRootBox()) {
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001091 int lowestAllowedPosition = max(lastRootBox()->lineBottom(), logicalHeight() + paddingAfter());
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001092 if (!style()->isFlippedLinesWritingMode())
1093 lastLineAnnotationsAdjustment = lastRootBox()->computeUnderAnnotationAdjustment(lowestAllowedPosition);
1094 else
1095 lastLineAnnotationsAdjustment = lastRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
hyatt@apple.com5e48ff52010-11-19 00:43:29 +00001096 }
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001097
hyatta70560a2002-11-20 01:53:20 +00001098 // Now add in the bottom border/padding.
mitz@apple.com3672d9e2010-12-17 19:31:16 +00001099 setLogicalHeight(logicalHeight() + lastLineAnnotationsAdjustment + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
kociendabb0c24b2001-08-24 14:24:40 +00001100
adele7a470a72006-04-20 22:22:14 +00001101 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001102 setLogicalHeight(logicalHeight() + lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes));
hyatted77ad82004-06-15 07:21:23 +00001103
1104 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1105 // truncate text.
1106 if (hasTextOverflow)
1107 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001108}
1109
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001110void RenderBlock::checkFloatsInCleanLine(RootInlineBox* line, Vector<FloatWithRect>& floats, size_t& floatIndex, bool& encounteredNewFloat, bool& dirtiedByFloat)
1111{
1112 Vector<RenderBox*>* cleanLineFloats = line->floatsPtr();
1113 if (!cleanLineFloats)
1114 return;
1115
1116 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1117 for (Vector<RenderBox*>::iterator it = cleanLineFloats->begin(); it != end; ++it) {
1118 RenderBox* floatingBox = *it;
1119 floatingBox->layoutIfNeeded();
1120 IntSize newSize(floatingBox->width() + floatingBox->marginLeft() + floatingBox->marginRight(), floatingBox->height() + floatingBox->marginTop() + floatingBox->marginBottom());
1121 ASSERT(floatIndex < floats.size());
1122 if (floats[floatIndex].object != floatingBox) {
1123 encounteredNewFloat = true;
1124 return;
1125 }
1126 if (floats[floatIndex].rect.size() != newSize) {
hyatt@apple.com2a5eb212011-03-22 23:21:54 +00001127 int floatTop = isHorizontalWritingMode() ? floats[floatIndex].rect.y() : floats[floatIndex].rect.x();
1128 int floatHeight = isHorizontalWritingMode() ? max(floats[floatIndex].rect.height(), newSize.height())
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001129 : max(floats[floatIndex].rect.width(), newSize.width());
1130 line->markDirty();
1131 markLinesDirtyInBlockRange(line->blockLogicalHeight(), floatTop + floatHeight, line);
1132 floats[floatIndex].rect.setSize(newSize);
1133 dirtiedByFloat = true;
1134 }
1135 floatIndex++;
1136 }
1137}
1138
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001139RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001140 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats,
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001141 bool& useRepaintBounds, int& repaintLogicalTop, int& repaintLogicalBottom)
hyatt0c3a9862004-02-23 21:26:26 +00001142{
1143 RootInlineBox* curr = 0;
1144 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001145
mitz@apple.com40547b32008-03-18 04:04:34 +00001146 bool dirtiedByFloat = false;
1147 if (!fullLayout) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001148 // Paginate all of the clean lines.
1149 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
1150 int paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +00001151 size_t floatIndex = 0;
1152 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001153 if (paginated) {
1154 paginationDelta -= curr->paginationStrut();
1155 adjustLinePositionForPagination(curr, paginationDelta);
1156 if (paginationDelta) {
1157 if (containsFloats() || !floats.isEmpty()) {
1158 // FIXME: Do better eventually. For now if we ever shift because of pagination and floats are present just go to a full layout.
1159 fullLayout = true;
1160 break;
1161 }
1162
1163 if (!useRepaintBounds)
1164 useRepaintBounds = true;
1165
hyatt@apple.com61f25322011-03-31 20:40:48 +00001166 repaintLogicalTop = min(repaintLogicalTop, curr->logicalTopVisualOverflow() + min(paginationDelta, 0));
1167 repaintLogicalBottom = max(repaintLogicalBottom, curr->logicalBottomVisualOverflow() + max(paginationDelta, 0));
hyatt@apple.com61bbedf2011-01-26 23:10:57 +00001168 curr->adjustBlockDirectionPosition(paginationDelta);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001169 }
1170 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001171
1172 // If a new float has been inserted before this line or before its last known float,just do a full layout.
1173 checkFloatsInCleanLine(curr, floats, floatIndex, fullLayout, dirtiedByFloat);
mitz@apple.com40547b32008-03-18 04:04:34 +00001174 if (dirtiedByFloat || fullLayout)
1175 break;
1176 }
1177 // Check if a new float has been inserted after the last known float.
1178 if (!curr && floatIndex < floats.size())
1179 fullLayout = true;
1180 }
1181
hyatt0c3a9862004-02-23 21:26:26 +00001182 if (fullLayout) {
1183 // Nuke all our lines.
1184 if (firstRootBox()) {
1185 RenderArena* arena = renderArena();
1186 curr = firstRootBox();
1187 while (curr) {
1188 RootInlineBox* next = curr->nextRootBox();
1189 curr->deleteLine(arena);
1190 curr = next;
1191 }
darinec375482007-01-06 01:36:24 +00001192 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001193 }
eseidel789896f2005-11-27 22:52:09 +00001194 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001195 if (curr) {
1196 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001197 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001198 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001199 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001200 // The previous line didn't break cleanly or broke at a newline
1201 // that has been deleted, so treat it as dirty too.
1202 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001203 }
eseidel789896f2005-11-27 22:52:09 +00001204 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001205 // No dirty lines were found.
1206 // If the last line didn't break cleanly, treat it as dirty.
1207 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1208 curr = lastRootBox();
1209 }
mitz@apple.come1364202008-02-28 01:06:41 +00001210
hyatt0c3a9862004-02-23 21:26:26 +00001211 // If we have no dirty lines, then last is just the last root box.
1212 last = curr ? curr->prevRootBox() : lastRootBox();
1213 }
mitz@apple.come1364202008-02-28 01:06:41 +00001214
mitz@apple.com40547b32008-03-18 04:04:34 +00001215 numCleanFloats = 0;
1216 if (!floats.isEmpty()) {
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001217 int savedLogicalHeight = logicalHeight();
mitz@apple.com40547b32008-03-18 04:04:34 +00001218 // Restore floats from clean lines.
1219 RootInlineBox* line = firstRootBox();
1220 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001221 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1222 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1223 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001224 insertFloatingObject(*f);
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001225 setLogicalHeight(logicalTopForChild(*f) - marginBeforeForChild(*f));
mitz@apple.com40547b32008-03-18 04:04:34 +00001226 positionNewFloats();
1227 ASSERT(floats[numCleanFloats].object == *f);
1228 numCleanFloats++;
1229 }
1230 }
1231 line = line->nextRootBox();
1232 }
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001233 setLogicalHeight(savedLogicalHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001234 }
1235
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001236 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001237 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001238
1239 RenderObject* startObj;
1240 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001241 if (last) {
hyatt@apple.coma956e332010-10-06 20:35:21 +00001242 setLogicalHeight(last->blockLogicalHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001243 startObj = last->lineBreakObj();
1244 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001245 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001246 } else {
zimmermann@webkit.org7208fe32011-03-15 20:15:17 +00001247 bool ltr = style()->isLeftToRightDirection();
mitz@apple.com45d9e102009-04-27 16:24:55 +00001248 Direction direction = ltr ? LeftToRight : RightToLeft;
1249 resolver.setLastStrongDir(direction);
1250 resolver.setLastDir(direction);
1251 resolver.setEorDir(direction);
leviw@chromium.org1863f2e2011-03-30 12:48:08 +00001252 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override, FromStyleOrDOM));
mitz@apple.com1a301772008-03-11 18:30:36 +00001253
mitz@apple.com15035e62008-07-05 20:44:44 +00001254 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001255 }
mitz@apple.come1364202008-02-28 01:06:41 +00001256
mitz@apple.com15035e62008-07-05 20:44:44 +00001257 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001258
hyatt0c3a9862004-02-23 21:26:26 +00001259 return curr;
1260}
1261
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001262RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, Vector<FloatWithRect>& floats, size_t floatIndex, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& logicalTop)
hyatt0c3a9862004-02-23 21:26:26 +00001263{
1264 RootInlineBox* last = 0;
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001265 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1266 if (!curr->isDirty()) {
1267 bool encounteredNewFloat = false;
1268 bool dirtiedByFloat = false;
1269 checkFloatsInCleanLine(curr, floats, floatIndex, encounteredNewFloat, dirtiedByFloat);
1270 if (encounteredNewFloat)
1271 return 0;
hyatt04420ca2004-07-16 00:05:42 +00001272 }
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001273 if (curr->isDirty())
1274 last = 0;
1275 else if (!last)
1276 last = curr;
hyatt0c3a9862004-02-23 21:26:26 +00001277 }
mitz@apple.come1364202008-02-28 01:06:41 +00001278
hyatt0c3a9862004-02-23 21:26:26 +00001279 if (!last)
1280 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001281
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001282 // At this point, |last| is the first line in a run of clean lines that ends with the last line
1283 // in the block.
1284
eseidel789896f2005-11-27 22:52:09 +00001285 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001286 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001287 cleanLineBidiStatus = prev->lineBreakBidiStatus();
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001288 logicalTop = prev->blockLogicalHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001289
hyatt0c3a9862004-02-23 21:26:26 +00001290 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1291 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1292 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001293
hyatt0c3a9862004-02-23 21:26:26 +00001294 return last;
1295}
1296
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001297bool RenderBlock::matchedEndLine(const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus, RootInlineBox*& endLine,
1298 int& endLogicalTop, int& repaintLogicalBottom, int& repaintLogicalTop)
hyatt0c3a9862004-02-23 21:26:26 +00001299{
mitz@apple.com15035e62008-07-05 20:44:44 +00001300 if (resolver.position() == endLineStart) {
1301 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001302 return false;
1303
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001304 int delta = logicalHeight() - endLogicalTop;
mitz@apple.com40547b32008-03-18 04:04:34 +00001305 if (!delta || !m_floatingObjects)
1306 return true;
1307
1308 // 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 +00001309 int logicalTop = min(logicalHeight(), endLogicalTop);
mitz@apple.com40547b32008-03-18 04:04:34 +00001310
1311 RootInlineBox* lastLine = endLine;
1312 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1313 lastLine = nextLine;
1314
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001315 int logicalBottom = lastLine->blockLogicalHeight() + abs(delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001316
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001317 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1318 FloatingObjectSetIterator end = floatingObjectSet.end();
1319 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001320 FloatingObject* f = *it;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001321 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001322 return false;
1323 }
1324
1325 return true;
1326 }
hyatt0c3a9862004-02-23 21:26:26 +00001327
mitz@apple.come1364202008-02-28 01:06:41 +00001328 // The first clean line doesn't match, but we can check a handful of following lines to try
1329 // to match back up.
1330 static int numLines = 8; // The # of lines we're willing to match against.
1331 RootInlineBox* line = endLine;
1332 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001333 if (line->lineBreakObj() == resolver.position().m_obj && line->lineBreakPos() == resolver.position().m_pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001334 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001335 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001336 return false; // ...but the bidi state doesn't match.
1337 RootInlineBox* result = line->nextRootBox();
1338
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001339 // Set our logical top to be the block height of endLine.
mitz@apple.come1364202008-02-28 01:06:41 +00001340 if (result)
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001341 endLogicalTop = line->blockLogicalHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001342
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001343 int delta = logicalHeight() - endLogicalTop;
mitz@apple.com40547b32008-03-18 04:04:34 +00001344 if (delta && m_floatingObjects) {
1345 // 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 +00001346 int logicalTop = min(logicalHeight(), endLogicalTop);
mitz@apple.com40547b32008-03-18 04:04:34 +00001347
1348 RootInlineBox* lastLine = endLine;
1349 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1350 lastLine = nextLine;
1351
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001352 int logicalBottom = lastLine->blockLogicalHeight() + abs(delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001353
commit-queue@webkit.org2984df92011-03-04 23:07:15 +00001354 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1355 FloatingObjectSetIterator end = floatingObjectSet.end();
1356 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
benjamin.poulain@nokia.coma655ed72011-02-27 15:51:19 +00001357 FloatingObject* f = *it;
hyatt@apple.com9a2e7d22010-10-06 22:13:31 +00001358 if (logicalBottomForFloat(f) >= logicalTop && logicalBottomForFloat(f) < logicalBottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001359 return false;
1360 }
1361 }
1362
mitz@apple.come1364202008-02-28 01:06:41 +00001363 // Now delete the lines that we failed to sync.
1364 RootInlineBox* boxToDelete = endLine;
1365 RenderArena* arena = renderArena();
1366 while (boxToDelete && boxToDelete != result) {
hyatt@apple.com61f25322011-03-31 20:40:48 +00001367 repaintLogicalTop = min(repaintLogicalTop, boxToDelete->logicalTopVisualOverflow());
1368 repaintLogicalBottom = max(repaintLogicalBottom, boxToDelete->logicalBottomVisualOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001369 RootInlineBox* next = boxToDelete->nextRootBox();
1370 boxToDelete->deleteLine(arena);
1371 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001372 }
mitz@apple.come1364202008-02-28 01:06:41 +00001373
1374 endLine = result;
1375 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001376 }
1377 }
mitz@apple.come1364202008-02-28 01:06:41 +00001378
hyatt0c3a9862004-02-23 21:26:26 +00001379 return false;
1380}
1381
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001382static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001383{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001384 if (it.m_obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001385 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001386
hyattdca76e92005-11-02 08:52:50 +00001387 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1388 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001389 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001390 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1391 // |true|).
1392 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001393 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001394
kocienda498d1982004-10-15 21:07:24 +00001395 return true;
kocienda98440082004-10-14 23:51:47 +00001396}
1397
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001398static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001399{
1400 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1401}
1402
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001403static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001404{
1405 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001406 // We need to fix this, though, because at the very least, inlines containing only
1407 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001408 return !flow->firstChild() && flow->hasInlineDirectionBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001409}
1410
hyatt@apple.com71eeb442010-02-11 20:05:51 +00001411bool RenderBlock::requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001412{
eric@webkit.org8c25a592011-03-29 13:18:11 +00001413 if (it.m_obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001414 return false;
bdakinf876bee2007-10-30 05:27:09 +00001415
eric@webkit.org8c25a592011-03-29 13:18:11 +00001416 if (it.m_obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.m_obj)))
bdakinf876bee2007-10-30 05:27:09 +00001417 return false;
1418
eric@webkit.org8c25a592011-03-29 13:18:11 +00001419 if (!shouldCollapseWhiteSpace(it.m_obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.m_obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001420 return true;
1421
1422 UChar current = it.current();
leviw@chromium.orgcee20512011-04-06 12:12:58 +00001423 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || it.m_obj->preservesNewline())
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001424 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001425}
1426
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001427bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001428{
1429 ASSERT(inlineObj->parent() == this);
1430
mitz@apple.com15035e62008-07-05 20:44:44 +00001431 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001432 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001433 it.increment();
bdashccffb432007-07-13 11:51:40 +00001434
1435 return !it.atEnd();
1436}
1437
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001438// FIXME: The entire concept of the skipTrailingWhitespace function is flawed, since we really need to be building
mitz@apple.com1a301772008-03-11 18:30:36 +00001439// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1440// elements quite right. In other words, we need to build this function's work into the normal line
1441// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001442// NB. this function will insert any floating elements that would otherwise
1443// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001444void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001445{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001446 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001447 RenderObject* object = iterator.m_obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001448 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001449 insertFloatingObject(toRenderBox(object));
hyatt@apple.come16585a2011-02-28 23:21:36 +00001450 } else if (object->isPositioned())
1451 setStaticPositions(this, toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001452 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001453 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001454}
bdashccffb432007-07-13 11:51:40 +00001455
hyatt@apple.com95900032011-03-23 03:27:12 +00001456void RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly,
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001457 FloatingObject* lastFloatFromPreviousLine, LineOffsets& lineOffsets)
mitz@apple.com1a301772008-03-11 18:30:36 +00001458{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001459 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001460 RenderObject* object = resolver.position().m_obj;
hyatt@apple.com95900032011-03-23 03:27:12 +00001461 if (object->isFloating())
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001462 positionNewFloatOnLine(insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine, firstLine, lineOffsets);
hyatt@apple.com95900032011-03-23 03:27:12 +00001463 else if (object->isPositioned())
hyatt@apple.come16585a2011-02-28 23:21:36 +00001464 setStaticPositions(this, toRenderBox(object));
mitz@apple.com15035e62008-07-05 20:44:44 +00001465 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001466 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001467 resolver.commitExplicitEmbedding();
kociendae40cb942004-10-05 20:05:38 +00001468}
1469
bdakinf876bee2007-10-30 05:27:09 +00001470// This is currently just used for list markers and inline flows that have line boxes. Neither should
1471// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001472static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001473{
mitz@apple.com1a301772008-03-11 18:30:36 +00001474 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001475 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1476 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001477 UChar nextChar = nextText->characters()[0];
1478 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001479 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001480 return true;
1481 }
1482 }
1483
1484 return false;
1485}
1486
mitz@apple.comddb59872011-04-05 05:21:16 +00001487void RenderBlock::fitBelowFloats(float widthToFit, float totalOverhangWidth, bool firstLine, float& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001488{
1489 ASSERT(widthToFit > availableWidth);
1490
hyatt@apple.com2d76a3b2010-10-02 00:13:00 +00001491 int floatLogicalBottom;
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001492 int lastFloatLogicalBottom = logicalHeight();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001493 float newLineWidth = availableWidth;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001494 while (true) {
hyatt@apple.com2d76a3b2010-10-02 00:13:00 +00001495 floatLogicalBottom = nextFloatLogicalBottomBelow(lastFloatLogicalBottom);
1496 if (!floatLogicalBottom)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001497 break;
1498
hyatt@apple.com2d76a3b2010-10-02 00:13:00 +00001499 newLineWidth = availableLogicalWidthForLine(floatLogicalBottom, firstLine);
1500 lastFloatLogicalBottom = floatLogicalBottom;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001501 if (newLineWidth >= widthToFit)
1502 break;
1503 }
1504
1505 if (newLineWidth > availableWidth) {
hyatt@apple.com2d76a3b2010-10-02 00:13:00 +00001506 setLogicalHeight(lastFloatLogicalBottom);
mitz@apple.comddb59872011-04-05 05:21:16 +00001507 availableWidth = newLineWidth + totalOverhangWidth;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001508 }
1509}
1510
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001511static 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 +00001512{
hyatt@apple.com4d046b72011-01-31 20:39:09 +00001513 if (isFixedPitch || (!from && len == text->textLength()) || text->style()->hasTextCombine())
mitz@apple.com34106442009-02-01 06:23:39 +00001514 return text->width(from, len, font, xPos);
1515 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1516}
1517
mitz@apple.comd56f1082011-03-06 22:44:48 +00001518static 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 +00001519{
mitz@apple.comd56f1082011-03-06 22:44:48 +00001520 // Map 'hyphenate-limit-{before,after}: auto;' to 2.
1521 if (minimumPrefixLength < 0)
1522 minimumPrefixLength = 2;
1523
1524 if (minimumSuffixLength < 0)
1525 minimumSuffixLength = 2;
1526
1527 if (pos - lastSpace <= minimumSuffixLength)
1528 return;
1529
mitz@apple.comb2107652010-06-21 16:54:52 +00001530 const AtomicString& hyphenString = text->style()->hyphenString();
1531 int hyphenWidth = font.width(TextRun(hyphenString.characters(), hyphenString.length()));
1532
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001533 float maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
mitz@apple.com7c67b292010-09-12 23:04:16 +00001534 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1535 // that an hyphenation opportunity exists, so do not bother to look for it.
1536 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1537 return;
1538
1539 unsigned prefixLength = font.offsetForPosition(TextRun(text->characters() + lastSpace, pos - lastSpace, !collapseWhiteSpace, xPos + lastSpaceWordSpacing), maxPrefixWidth, false);
mitz@apple.comd56f1082011-03-06 22:44:48 +00001540 if (prefixLength < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001541 return;
1542
mitz@apple.comd56f1082011-03-06 22:44:48 +00001543 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, min(prefixLength, static_cast<unsigned>(pos - lastSpace - minimumSuffixLength)) + 1, localeIdentifier);
1544 // FIXME: The following assumes that the character at lastSpace is a space (and therefore should not factor
1545 // into hyphenate-limit-before) unless lastSpace is 0. This is wrong in the rare case of hyphenating
1546 // the first word in a text node which has leading whitespace.
mitz@apple.come4bedf32011-03-14 00:54:59 +00001547 if (!prefixLength || prefixLength - (lastSpace ? 1 : 0) < static_cast<unsigned>(minimumPrefixLength))
mitz@apple.comb2107652010-06-21 16:54:52 +00001548 return;
1549
mitz@apple.comd56f1082011-03-06 22:44:48 +00001550 ASSERT(pos - lastSpace - prefixLength >= static_cast<unsigned>(minimumSuffixLength));
1551
mitz@apple.comb2107652010-06-21 16:54:52 +00001552#if !ASSERT_DISABLED
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001553 float prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00001554 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001555#else
1556 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001557#endif
1558
eric@webkit.orgbd143592011-03-29 17:44:41 +00001559 lineBreak.moveTo(text, lastSpace + prefixLength, nextBreakable);
mitz@apple.comb2107652010-06-21 16:54:52 +00001560 hyphenated = true;
1561}
1562
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001563class LineOffsets {
1564public:
1565 LineOffsets(int leftOffset, int rightOffset)
1566 : m_left(leftOffset)
1567 , m_right(rightOffset)
1568 {
1569 }
1570 int width() const { return max(0, m_right - m_left); }
1571 void setLeft(int left) { m_left = left; }
1572 void setRight(int right) { m_right = right; }
1573private:
1574 int m_left;
1575 int m_right;
1576};
1577
mitz@apple.com44fc5132011-02-25 18:34:15 +00001578InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, LineBreakIteratorInfo& lineBreakIteratorInfo, bool& previousLineBrokeCleanly,
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001579 bool& hyphenated, EClear* clear, FloatingObject* lastFloatFromPreviousLine, Vector<RenderBox*>& positionedBoxes)
kociendae40cb942004-10-05 20:05:38 +00001580{
eric@webkit.org307c6702011-04-03 12:37:40 +00001581 ASSERT(resolver.position().root() == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001582
eric@webkit.org86a865a2011-03-29 15:30:41 +00001583 bool appliedStartWidth = resolver.position().m_pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001584 LineMidpointState& lineMidpointState = resolver.midpointState();
1585
hyatt@apple.com95900032011-03-23 03:27:12 +00001586 int blockOffset = logicalHeight();
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001587 LineOffsets lineOffsets(logicalLeftOffsetForLine(blockOffset, firstLine), logicalRightOffsetForLine(blockOffset, firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001588
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001589 skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, lastFloatFromPreviousLine, lineOffsets);
1590
1591 float width = lineOffsets.width();
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001592 float w = 0;
1593 float tmpW = 0;
mitz@apple.comddb59872011-04-05 05:21:16 +00001594 // The amount by which |width| has been inflated to account for possible contraction due to ruby overhang.
1595 float totalOverhangWidth = 0;
kociendae40cb942004-10-05 20:05:38 +00001596
mitz@apple.com15035e62008-07-05 20:44:44 +00001597 if (resolver.position().atEnd())
1598 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001599
hyatt33f8d492002-11-12 21:44:52 +00001600 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1601 // or not we are currently ignoring whitespace.
1602 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001603 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001604
1605 // This variable tracks whether the very last character we saw was a space. We use
1606 // this to detect when we encounter a second space so we know we have to terminate
1607 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001608 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001609 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001610 RenderObject* trailingSpaceObject = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001611 Vector<RenderBox*, 4> trailingPositionedBoxes;
hyatt98b16282004-03-31 18:43:12 +00001612
mitz@apple.com15035e62008-07-05 20:44:44 +00001613 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001614
eric@webkit.orgbd143592011-03-29 17:44:41 +00001615 // FIXME: It is error-prone to split the position object out like this.
1616 // Teach this code to work with objects instead of this split tuple.
eric@webkit.org8c25a592011-03-29 13:18:11 +00001617 RenderObject* o = resolver.position().m_obj;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001618 RenderObject* last = o;
eric@webkit.org86a865a2011-03-29 15:30:41 +00001619 unsigned pos = resolver.position().m_pos;
eric@webkit.org25e0e022011-03-29 14:35:02 +00001620 int nextBreakable = resolver.position().m_nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001621 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001622
hyatt0c3a9862004-02-23 21:26:26 +00001623 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1624 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001625
mitz@apple.comb2107652010-06-21 16:54:52 +00001626 hyphenated = false;
1627
ddkilzer5d01fa22007-01-29 03:10:37 +00001628 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001629 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001630
hyatt@apple.com69340902008-01-16 21:24:21 +00001631 // Firefox and Opera will allow a table cell to grow to fit an image inside it under
mitz@apple.com25beac62008-02-24 18:48:27 +00001632 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001633 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001634 bool allowImagesToBreak = !document()->inQuirksMode() || !isTableCell() || !style()->logicalWidth().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001635
hyattb0d9f602007-01-15 01:28:23 +00001636 EWhiteSpace currWS = style()->whiteSpace();
1637 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001638 while (o) {
mitz@apple.comddb59872011-04-05 05:21:16 +00001639 RenderObject* next = bidiNext(this, o);
1640
hyattb0d9f602007-01-15 01:28:23 +00001641 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1642 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1643
1644 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001645 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001646
mjsd2948ef2007-02-26 19:29:04 +00001647#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001648 bool preserveNewline = o->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001649#else
hyattb0d9f602007-01-15 01:28:23 +00001650 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001651#endif
1652
hyattb0d9f602007-01-15 01:28:23 +00001653 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1654
hyatt275d0702005-11-03 23:53:57 +00001655 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001656 if (w + tmpW <= width) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00001657 lBreak.moveToStartOf(o);
mitz@apple.com1a301772008-03-11 18:30:36 +00001658 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001659
hyatt33f8d492002-11-12 21:44:52 +00001660 // A <br> always breaks a line, so don't let the line be collapsed
1661 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001662 // get collapsed away. It only does this if the previous line broke
1663 // cleanly. Otherwise the <br> has no effect on whether the line is
1664 // empty or not.
1665 if (prevLineBrokeCleanly)
1666 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001667 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001668 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001669
mitz@apple.com71e30842008-03-18 16:13:31 +00001670 if (!isLineEmpty && clear)
1671 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001672 }
1673 goto end;
1674 }
hyattb0d9f602007-01-15 01:28:23 +00001675
hyatt275d0702005-11-03 23:53:57 +00001676 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001677 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001678 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001679 RenderBox* floatBox = toRenderBox(o);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001680 FloatingObject* f = insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001681 // check if it fits in the current line.
1682 // If it does, position it now, otherwise, position
1683 // it after moving to next line (in newLine() func)
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001684 if (floatsFitOnLine && logicalWidthForFloat(f) + w + tmpW <= width) {
rniwa@webkit.org059b1972011-04-06 18:34:11 +00001685 positionNewFloatOnLine(f, lastFloatFromPreviousLine, firstLine, lineOffsets);
1686 width = lineOffsets.width() + totalOverhangWidth;
eric@webkit.org8c25a592011-03-29 13:18:11 +00001687 if (lBreak.m_obj == o) {
eric@webkit.org86a865a2011-03-29 15:30:41 +00001688 ASSERT(!lBreak.m_pos);
mitz@apple.comd9ccfeb2011-03-10 01:56:27 +00001689 lBreak.increment();
1690 }
mitz@apple.com25beac62008-02-24 18:48:27 +00001691 } else
1692 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001693 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001694 // If our original display wasn't an inline type, then we can
hyatt@apple.come16585a2011-02-28 23:21:36 +00001695 // go ahead and determine our static inline position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001696 RenderBox* box = toRenderBox(o);
1697 bool isInlineType = box->style()->isOriginalDisplayInlineType();
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001698 if (!isInlineType)
hyatt@apple.come16585a2011-02-28 23:21:36 +00001699 box->layer()->setStaticInlinePosition(borderAndPaddingStart());
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001700 else {
1701 // If our original display was an INLINE type, then we can go ahead
1702 // and determine our static y position now.
hyatt@apple.come16585a2011-02-28 23:21:36 +00001703 box->layer()->setStaticBlockPosition(logicalHeight());
hyatt98ee7e42003-05-14 01:39:15 +00001704 }
1705
1706 // If we're ignoring spaces, we have to stop and include this object and
1707 // then start ignoring spaces again.
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001708 if (isInlineType || o->container()->isRenderInline()) {
hyattbc7f07f2003-05-27 20:04:26 +00001709 if (ignoringSpaces) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00001710 ignoreStart.m_obj = o;
eric@webkit.org86a865a2011-03-29 15:30:41 +00001711 ignoreStart.m_pos = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001712 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1713 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001714 }
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001715 if (trailingSpaceObject)
1716 trailingPositionedBoxes.append(box);
1717 } else
1718 positionedBoxes.append(box);
hyatt98ee7e42003-05-14 01:39:15 +00001719 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001720 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001721 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001722 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001723
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001724 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001725
bdakinf876bee2007-10-30 05:27:09 +00001726 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1727 // to make sure that we stop to include this object and then start ignoring spaces again.
1728 // If this object is at the start of the line, we need to behave like list markers and
1729 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001730 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001731 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001732 if (ignoringSpaces) {
1733 trailingSpaceObject = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001734 trailingPositionedBoxes.clear();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001735 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1736 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
eric@webkit.org8c25a592011-03-29 13:18:11 +00001737 } else if (style()->collapseWhiteSpace() && resolver.position().m_obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001738 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001739 // Like with list markers, we start ignoring spaces to make sure that any
1740 // additional spaces we see will be discarded.
1741 currentCharacterIsSpace = true;
1742 currentCharacterIsWS = true;
1743 ignoringSpaces = true;
1744 }
1745 }
1746
rniwa@webkit.org33a346a2011-04-06 17:06:14 +00001747 tmpW += borderPaddingMarginStart(flowBox) + borderPaddingMarginEnd(flowBox);
hyatt275d0702005-11-03 23:53:57 +00001748 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001749 RenderBox* replacedBox = toRenderBox(o);
1750
hyattde396342003-10-29 08:57:20 +00001751 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001752 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001753 w += tmpW;
1754 tmpW = 0;
eric@webkit.orgbd143592011-03-29 17:44:41 +00001755 lBreak.moveToStartOf(o);
hyatt711fe232002-11-20 21:25:14 +00001756 }
1757
mitz@apple.combfdc9112008-02-21 19:59:40 +00001758 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001759 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001760
hyatt33f8d492002-11-12 21:44:52 +00001761 isLineEmpty = false;
1762 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001763 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001764 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001765 trailingSpaceObject = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00001766 trailingPositionedBoxes.clear();
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001767
bdakinf876bee2007-10-30 05:27:09 +00001768 // Optimize for a common case. If we can't find whitespace after the list
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001769 // item, then this is all moot.
1770 int replacedLogicalWidth = logicalWidthForChild(replacedBox) + marginStartForChild(replacedBox) + marginEndForChild(replacedBox) + inlineLogicalWidth(o);
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001771 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001772 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001773 // Like with inline flows, we start ignoring spaces to make sure that any
1774 // additional spaces we see will be discarded.
1775 currentCharacterIsSpace = true;
1776 currentCharacterIsWS = true;
1777 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001778 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001779 if (toRenderListMarker(o)->isInside())
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001780 tmpW += replacedLogicalWidth;
justing244d3a32006-04-13 01:31:24 +00001781 } else
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001782 tmpW += replacedLogicalWidth;
mitz@apple.comddb59872011-04-05 05:21:16 +00001783 if (o->isRubyRun()) {
1784 RenderRubyRun* rubyRun = toRenderRubyRun(o);
1785 int startOverhang;
1786 int endOverhang;
1787 rubyRun->getOverhang(firstLine, last, next, startOverhang, endOverhang);
1788 startOverhang = min<int>(startOverhang, w);
1789 totalOverhangWidth += startOverhang;
1790 width += startOverhang;
1791 endOverhang = max(min<int>(endOverhang, width - (w + tmpW)), 0);
1792 totalOverhangWidth += endOverhang;
1793 width += endOverhang;
1794 }
eseidel789896f2005-11-27 22:52:09 +00001795 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001796 if (!pos)
1797 appliedStartWidth = false;
1798
darin@apple.com36744d62009-01-25 20:23:04 +00001799 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001800
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001801#if ENABLE(SVG)
1802 bool isSVGText = t->isSVGInlineText();
1803#endif
1804
hyatt@apple.com4d046b72011-01-31 20:39:09 +00001805 RenderStyle* style = t->style(firstLine);
inferno@chromium.orgc4802fc2011-03-23 19:32:21 +00001806 if (style->hasTextCombine() && o->isCombineText())
hyatt@apple.com4d046b72011-01-31 20:39:09 +00001807 toRenderCombineText(o)->combineText();
1808
darin42563ac52007-01-22 17:28:57 +00001809 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001810 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001811 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001812
mitz@apple.comb2107652010-06-21 16:54:52 +00001813 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001814 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com6ae88612011-03-03 23:09:11 +00001815 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->locale());
weinigf28a1c32007-02-14 14:10:31 +00001816
hyatt33f8d492002-11-12 21:44:52 +00001817 int lastSpace = pos;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001818 float wordSpacing = o->style()->wordSpacing();
1819 float lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001820
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001821 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1822 // space, then subtract its width.
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001823 float wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(TextRun(&space, 1)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001824
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001825 float wrapW = tmpW + inlineLogicalWidth(o, !appliedStartWidth, true);
1826 float charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001827 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1828 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1829 // which is only possible if the word is the first thing on the line, that is, if |w| is zero.
hyattea474f72007-04-20 05:02:19 +00001830 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001831 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001832 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001833 float hyphenWidth = 0;
hyattea474f72007-04-20 05:02:19 +00001834
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001835 if (t->isWordBreak()) {
1836 w += tmpW;
1837 tmpW = 0;
eric@webkit.orgbd143592011-03-29 17:44:41 +00001838 lBreak.moveToStartOf(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001839 ASSERT(!len);
1840 }
1841
hyatt275d0702005-11-03 23:53:57 +00001842 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001843 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001844 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001845 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001846 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001847
hyattb0d9f602007-01-15 01:28:23 +00001848 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001849 isLineEmpty = false;
mitz@apple.combe429562008-03-07 01:09:51 +00001850
mitz@apple.com20e34452010-09-28 19:38:15 +00001851 if (c == softHyphen && autoWrap && !hyphenWidth && style->hyphens() != HyphensNone) {
1852 const AtomicString& hyphenString = style->hyphenString();
1853 hyphenWidth = f.width(TextRun(hyphenString.characters(), hyphenString.length()));
1854 tmpW += hyphenWidth;
hyatt78b85132004-03-29 20:07:45 +00001855 }
mitz@apple.com20e34452010-09-28 19:38:15 +00001856
hyatt3aff2332003-01-23 01:15:28 +00001857 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001858
darinf9e5d6c2007-01-09 14:54:26 +00001859 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001860
weiniged111c12007-07-13 22:45:11 +00001861 if ((breakAll || breakWords) && !midWordBreak) {
1862 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001863 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001864 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001865 }
darin47ece0d2005-09-04 07:42:31 +00001866
mitz@apple.com44fc5132011-02-25 18:34:15 +00001867 if (lineBreakIteratorInfo.first != t) {
1868 lineBreakIteratorInfo.first = t;
1869 lineBreakIteratorInfo.second.reset(str, strlen);
1870 }
1871
1872 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(lineBreakIteratorInfo.second, pos, nextBreakable, breakNBSP) && (style->hyphens() != HyphensNone || (pos && str[pos - 1] != softHyphen)));
mitz@apple.com20e34452010-09-28 19:38:15 +00001873
weiniged111c12007-07-13 22:45:11 +00001874 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001875 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001876 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001877 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001878 // Stop ignoring spaces and begin at this
1879 // new point.
hyatt48710d62003-08-21 09:17:13 +00001880 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001881 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001882 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001883 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001884 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001885 } else {
hyatt33f8d492002-11-12 21:44:52 +00001886 // Just keep ignoring these spaces.
1887 pos++;
1888 len--;
1889 continue;
1890 }
1891 }
rjwc9c257d2003-01-24 03:46:17 +00001892
hyatt@apple.com0acc9352011-02-17 19:19:07 +00001893 float additionalTmpW;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001894 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1895 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1896 else
1897 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001898 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001899 if (!appliedStartWidth) {
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00001900 tmpW += inlineLogicalWidth(o, true, false);
hyattffe78712003-02-11 01:59:29 +00001901 appliedStartWidth = true;
1902 }
1903
eseideld13fe532005-11-30 02:40:29 +00001904 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001905
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001906 if (!w && autoWrap && tmpW > width)
mitz@apple.comddb59872011-04-05 05:21:16 +00001907 fitBelowFloats(tmpW, totalOverhangWidth, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001908
hyattb0d9f602007-01-15 01:28:23 +00001909 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001910 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001911 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001912 bool lineWasTooWide = false;
1913 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001914 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001915 // Check if line is too big even without the extra space
1916 // at the end of the line. If it is not, do nothing.
1917 // If the line needs the extra whitespace to be too long,
1918 // then move the line break to the space and skip all
1919 // additional whitespace.
1920 if (w + tmpW + charWidth > width) {
1921 lineWasTooWide = true;
eric@webkit.orgbd143592011-03-29 17:44:41 +00001922 lBreak.moveTo(o, pos, nextBreakable);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001923 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001924 }
ap932806a2006-10-01 09:06:09 +00001925 }
1926 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001927 if (canHyphenate && w + tmpW > width) {
mitz@apple.comd56f1082011-03-06 22:44:48 +00001928 tryHyphenating(t, f, style->locale(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, pos, w + tmpW - additionalTmpW, width, isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, nextBreakable, hyphenated);
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001929 if (hyphenated)
1930 goto end;
1931 }
leviw@chromium.orgcee20512011-04-06 12:12:58 +00001932 if (lBreak.atTextParagraphSeparator()) {
hyatt0c05e102006-04-14 08:15:00 +00001933 if (!stoppedIgnoringSpaces && pos > 0) {
1934 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001935 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1936 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001937 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001938 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001939 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001940 }
eric@webkit.org86a865a2011-03-29 15:30:41 +00001941 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)
mitz@apple.com20e34452010-09-28 19:38:15 +00001942 hyphenated = true;
hyatt78b85132004-03-29 20:07:45 +00001943 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001944 } else {
weiniged111c12007-07-13 22:45:11 +00001945 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001946 tmpW -= additionalTmpW;
mitz@apple.com20e34452010-09-28 19:38:15 +00001947 if (hyphenWidth) {
darin54008922006-01-13 16:39:05 +00001948 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com20e34452010-09-28 19:38:15 +00001949 tmpW -= hyphenWidth;
1950 hyphenWidth = 0;
1951 }
darin54008922006-01-13 16:39:05 +00001952 }
rjwc9c257d2003-01-24 03:46:17 +00001953 }
hyatt33f8d492002-11-12 21:44:52 +00001954
hyattb0d9f602007-01-15 01:28:23 +00001955 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001956 if (!stoppedIgnoringSpaces && pos > 0) {
1957 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001958 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1959 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001960 }
eric@webkit.orgbd143592011-03-29 17:44:41 +00001961 lBreak.moveTo(o, pos, nextBreakable);
mitz@apple.com1a301772008-03-11 18:30:36 +00001962 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001963 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001964 return lBreak;
1965 }
hyatta9f48e32003-02-03 22:48:01 +00001966
weinigf28a1c32007-02-14 14:10:31 +00001967 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001968 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001969 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001970 tmpW = 0;
eric@webkit.orgbd143592011-03-29 17:44:41 +00001971 lBreak.moveTo(o, pos, nextBreakable);
weinigf28a1c32007-02-14 14:10:31 +00001972 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1973 // opportunity to break after a word.
1974 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001975 }
hyatt33f8d492002-11-12 21:44:52 +00001976
darin54008922006-01-13 16:39:05 +00001977 if (midWordBreak) {
1978 // Remember this as a breakable position in case
1979 // adding the end width forces a break.
eric@webkit.orgbd143592011-03-29 17:44:41 +00001980 lBreak.moveTo(o, pos, nextBreakable);
weiniged111c12007-07-13 22:45:11 +00001981 midWordBreak &= (breakWords || breakAll);
1982 }
1983
1984 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001985 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1986 lastSpace = pos;
1987 }
hyatt33f8d492002-11-12 21:44:52 +00001988
hyattdca76e92005-11-02 08:52:50 +00001989 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001990 // If we encounter a newline, or if we encounter a
1991 // second space, we need to go ahead and break up this
1992 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001993 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001994 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001995
hyatt33f8d492002-11-12 21:44:52 +00001996 // We just entered a mode where we are ignoring
1997 // spaces. Create a midpoint to terminate the run
1998 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001999 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00002000 }
2001 }
eseidel789896f2005-11-27 22:52:09 +00002002 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002003 // Stop ignoring spaces and begin at this
2004 // new point.
2005 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002006 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00002007 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002008 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00002009 }
hyatt98b16282004-03-31 18:43:12 +00002010
zimmermann@webkit.orgcea314662011-04-05 16:38:10 +00002011#if ENABLE(SVG)
2012 if (isSVGText && pos > 0) {
2013 // Force creation of new InlineBoxes for each absolute positioned character (those that start new text chunks).
2014 if (static_cast<RenderSVGInlineText*>(t)->characterStartsNewTextChunk(pos)) {
2015 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1));
2016 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
2017 }
2018 }
2019#endif
2020
hyatt98b16282004-03-31 18:43:12 +00002021 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002022 ignoreStart.m_obj = o;
eric@webkit.org86a865a2011-03-29 15:30:41 +00002023 ignoreStart.m_pos = pos;
hyatt98b16282004-03-31 18:43:12 +00002024 }
harrisone343c412005-01-18 01:07:26 +00002025
2026 if (!currentCharacterIsWS && previousCharacterIsWS) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002027 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace())
2028 lBreak.moveTo(o, pos, nextBreakable);
harrisone343c412005-01-18 01:07:26 +00002029 }
hyatt33f8d492002-11-12 21:44:52 +00002030
hyattb0d9f602007-01-15 01:28:23 +00002031 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00002032 trailingSpaceObject = o;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002033 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002034 trailingSpaceObject = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002035 trailingPositionedBoxes.clear();
2036 }
hyatt33f8d492002-11-12 21:44:52 +00002037
2038 pos++;
2039 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00002040 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002041 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002042
kociendabb0c24b2001-08-24 14:24:40 +00002043 // IMPORTANT: pos is > length here!
hyatt@apple.com0acc9352011-02-17 19:19:07 +00002044 float additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.comb2107652010-06-21 16:54:52 +00002045 tmpW += additionalTmpW;
hyatt@apple.com0415e5d2010-10-07 17:40:25 +00002046 tmpW += inlineLogicalWidth(o, !appliedStartWidth, true);
mitz@apple.comb2107652010-06-21 16:54:52 +00002047
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002048 if (w + tmpW > width) {
2049 if (canHyphenate)
2050 tryHyphenating(t, f, style->locale(), style->hyphenationLimitBefore(), style->hyphenationLimitAfter(), lastSpace, pos, w + tmpW - additionalTmpW, width, isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, nextBreakable, hyphenated);
2051
eric@webkit.org86a865a2011-03-29 15:30:41 +00002052 if (!hyphenated && 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)
mitz@apple.com96cf46d2011-03-14 01:09:26 +00002053 hyphenated = true;
2054
mitz@apple.comb2107652010-06-21 16:54:52 +00002055 if (hyphenated)
2056 goto end;
2057 }
kociendabb0c24b2001-08-24 14:24:40 +00002058 } else
weinigf28a1c32007-02-14 14:10:31 +00002059 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002060
hyattdca76e92005-11-02 08:52:50 +00002061 bool checkForBreak = autoWrap;
eric@webkit.org8c25a592011-03-29 13:18:11 +00002062 if (w && w + tmpW > width && lBreak.m_obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002063 checkForBreak = true;
2064 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00002065 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002066 if (currentCharacterIsSpace)
2067 checkForBreak = true;
2068 else {
harrison07b5e582005-08-15 23:31:16 +00002069 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00002070 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002071 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00002072 UChar c = nextText->characters()[0];
leviw@chromium.orgcee20512011-04-06 12:12:58 +00002073 if (c == ' ' || c == '\t' || (c == '\n' && !next->preservesNewline()))
eseideld13fe532005-11-30 02:40:29 +00002074 // If the next item on the line is text, and if we did not end with
2075 // a space, then the next text run continues our word (and so it needs to
2076 // keep adding to |tmpW|. Just update and continue.
2077 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002078 } else if (nextText->isWordBreak())
2079 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002080 bool willFitOnLine = w + tmpW <= width;
2081 if (!willFitOnLine && !w) {
mitz@apple.comddb59872011-04-05 05:21:16 +00002082 fitBelowFloats(tmpW, totalOverhangWidth, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002083 willFitOnLine = tmpW <= width;
2084 }
ddkilzere8759ef2007-03-25 06:28:19 +00002085 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00002086 if (canPlaceOnLine && checkForBreak) {
2087 w += tmpW;
2088 tmpW = 0;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002089 lBreak.moveToStartOf(next);
hyatta9f48e32003-02-03 22:48:01 +00002090 }
2091 }
2092 }
2093 }
2094
darin54008922006-01-13 16:39:05 +00002095 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002096 // if we have floats, try to get below them.
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002097 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002098 trailingSpaceObject = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002099 trailingPositionedBoxes.clear();
2100 }
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002101
2102 if (w)
2103 goto end;
2104
mitz@apple.comddb59872011-04-05 05:21:16 +00002105 fitBelowFloats(tmpW, totalOverhangWidth, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00002106
hyatta14d1742003-01-02 20:25:46 +00002107 // |width| may have been adjusted because we got shoved down past a float (thus
2108 // giving us more room), so we need to retest, and only jump to
2109 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002110 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002111 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002112 }
hyatt1d9e29b2003-04-10 01:48:53 +00002113
mitz@apple.com1a301772008-03-11 18:30:36 +00002114 if (!o->isFloatingOrPositioned()) {
2115 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002116 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00002117 w += tmpW;
2118 tmpW = 0;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002119 lBreak.moveToStartOf(next);
mitz@apple.com1a301772008-03-11 18:30:36 +00002120 }
hyatt711fe232002-11-20 21:25:14 +00002121 }
2122
mitz@apple.com1a301772008-03-11 18:30:36 +00002123 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002124 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002125
hyatta9f48e32003-02-03 22:48:01 +00002126 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2127 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002128 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002129 currentCharacterIsSpace = false;
2130
kociendabb0c24b2001-08-24 14:24:40 +00002131 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002132 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002133 }
2134
hyattb0d9f602007-01-15 01:28:23 +00002135
eric@webkit.orgbd143592011-03-29 17:44:41 +00002136 if (w + tmpW <= width || lastWS == NOWRAP)
2137 lBreak.clear();
kociendabb0c24b2001-08-24 14:24:40 +00002138
2139 end:
eric@webkit.org8c25a592011-03-29 13:18:11 +00002140 if (lBreak == resolver.position() && (!lBreak.m_obj || !lBreak.m_obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002141 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002142 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002143 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002144 if (pos != 0) {
eric@webkit.orgbd143592011-03-29 17:44:41 +00002145 // FIXME: This should call moveTo which would clear m_nextBreakablePosition
2146 // this code as-is is likely wrong.
eric@webkit.org8c25a592011-03-29 13:18:11 +00002147 lBreak.m_obj = o;
eric@webkit.org86a865a2011-03-29 15:30:41 +00002148 lBreak.m_pos = pos - 1;
eric@webkit.orgbd143592011-03-29 17:44:41 +00002149 } else
2150 lBreak.moveTo(last, last->isText() ? last->length() : 0);
eric@webkit.org8c25a592011-03-29 13:18:11 +00002151 } else if (lBreak.m_obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002152 // Don't ever break in the middle of a word if we can help it.
2153 // There's no room at all. We just have to be on this line,
2154 // even though we'll spill out.
eric@webkit.orgbd143592011-03-29 17:44:41 +00002155 lBreak.moveTo(o, pos);
kociendabb0c24b2001-08-24 14:24:40 +00002156 }
2157 }
2158
2159 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002160 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002161 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002162
hyattfe99c872003-07-31 22:25:29 +00002163 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002164 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00002165
hyatt33f8d492002-11-12 21:44:52 +00002166 if (trailingSpaceObject) {
2167 // This object is either going to be part of the last midpoint, or it is going
2168 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2169 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00002170 if (lineMidpointState.numMidpoints % 2) {
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002171 // Find the trailing space object's midpoint.
2172 int trailingSpaceMidpoint = lineMidpointState.numMidpoints - 1;
eric@webkit.org8c25a592011-03-29 13:18:11 +00002173 for ( ; trailingSpaceMidpoint >= 0 && lineMidpointState.midpoints[trailingSpaceMidpoint].m_obj != trailingSpaceObject; --trailingSpaceMidpoint) { }
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002174 ASSERT(trailingSpaceMidpoint >= 0);
eric@webkit.org86a865a2011-03-29 15:30:41 +00002175 lineMidpointState.midpoints[trailingSpaceMidpoint].m_pos--;
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002176
2177 // Now make sure every single trailingPositionedBox following the trailingSpaceMidpoint properly stops and starts
2178 // ignoring spaces.
2179 size_t currentMidpoint = trailingSpaceMidpoint + 1;
2180 for (size_t i = 0; i < trailingPositionedBoxes.size(); ++i) {
2181 if (currentMidpoint >= lineMidpointState.numMidpoints) {
2182 // We don't have a midpoint for this box yet.
2183 InlineIterator ignoreStart(this, trailingPositionedBoxes[i], 0);
2184 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring.
2185 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2186 } else {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002187 ASSERT(lineMidpointState.midpoints[currentMidpoint].m_obj == trailingPositionedBoxes[i]);
2188 ASSERT(lineMidpointState.midpoints[currentMidpoint + 1].m_obj == trailingPositionedBoxes[i]);
hyatt@apple.com63a8df32011-03-28 19:44:19 +00002189 }
2190 currentMidpoint += 2;
2191 }
eric@webkit.org8c25a592011-03-29 13:18:11 +00002192 } else if (!lBreak.m_obj && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002193 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00002194 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002195 unsigned length = text->textLength();
2196 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00002197 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002198 addMidpoint(lineMidpointState, endMid);
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002199 for (size_t i = 0; i < trailingPositionedBoxes.size(); ++i) {
eric@webkit.org8c25a592011-03-29 13:18:11 +00002200 ignoreStart.m_obj = trailingPositionedBoxes[i];
eric@webkit.org86a865a2011-03-29 15:30:41 +00002201 ignoreStart.m_pos = 0;
hyatt@apple.com14e332d2011-03-25 21:57:07 +00002202 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
2203 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
2204 }
hyatt33f8d492002-11-12 21:44:52 +00002205 }
2206 }
rjwc9c257d2003-01-24 03:46:17 +00002207
mjs54b64002003-04-02 02:59:02 +00002208 // We might have made lBreak an iterator that points past the end
2209 // of the object. Do this adjustment to make it point to the start
2210 // of the next object instead to avoid confusing the rest of the
2211 // code.
eric@webkit.org86a865a2011-03-29 15:30:41 +00002212 if (lBreak.m_pos > 0) {
2213 lBreak.m_pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002214 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002215 }
2216
kociendabb0c24b2001-08-24 14:24:40 +00002217 return lBreak;
2218}
2219
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002220void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002221{
hyatt@apple.com592848f2010-12-06 20:03:43 +00002222 int endPadding = hasOverflowClip() ? paddingEnd() : 0;
2223 // 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 +00002224 if (hasOverflowClip() && !endPadding && node() && node()->rendererIsEditable() && node() == node()->rootEditableElement() && style()->isLeftToRightDirection())
hyatt@apple.com592848f2010-12-06 20:03:43 +00002225 endPadding = 1;
hyattb4b20872004-10-20 21:34:01 +00002226 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com592848f2010-12-06 20:03:43 +00002227 addLayoutOverflow(curr->paddedLayoutOverflowRect(endPadding));
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002228 if (!hasOverflowClip())
hyatt@apple.com61f25322011-03-31 20:40:48 +00002229 addVisualOverflow(curr->visualOverflowRect(curr->lineTop(), curr->lineBottom()));
hyattb4b20872004-10-20 21:34:01 +00002230 }
2231}
2232
hyatted77ad82004-06-15 07:21:23 +00002233void RenderBlock::deleteEllipsisLineBoxes()
2234{
hyatted77ad82004-06-15 07:21:23 +00002235 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002236 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002237}
2238
2239void RenderBlock::checkLinesForTextOverflow()
2240{
2241 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002242 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2243 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002244 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002245 const Font& firstLineFont = firstLineStyle()->font();
2246 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002247 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2248 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002249
2250 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2251 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2252 // check the left edge of the line box to see if it is less
2253 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
hyatt@apple.comc0fa1632010-09-30 20:01:33 +00002254 bool ltr = style()->isLeftToRightDirection();
hyatted77ad82004-06-15 07:21:23 +00002255 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com230b2802010-09-24 19:14:27 +00002256 int blockRightEdge = logicalRightOffsetForLine(curr->y(), curr == firstRootBox());
2257 int blockLeftEdge = logicalLeftOffsetForLine(curr->y(), curr == firstRootBox());
hyatt@apple.comc01df9e2010-09-23 19:17:33 +00002258 int lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002259 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002260 // 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 +00002261 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2262 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2263 // space.
2264 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002265 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
thakis@chromium.orga4fcdf32011-02-22 05:51:51 +00002266 if (curr->lineCanAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002267 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002268 }
2269 }
2270}
2271
rniwa@webkit.org059b1972011-04-06 18:34:11 +00002272bool RenderBlock::positionNewFloatOnLine(FloatingObject* newFloat, FloatingObject* lastFloatFromPreviousLine, bool firstLine, LineOffsets& lineOffsets)
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002273{
2274 bool didPosition = positionNewFloats();
2275 if (!didPosition)
2276 return didPosition;
2277
2278 int blockOffset = logicalHeight();
2279 if (blockOffset >= logicalTopForFloat(newFloat) && blockOffset < logicalBottomForFloat(newFloat)) {
2280 if (newFloat->type() == FloatingObject::FloatLeft)
rniwa@webkit.org059b1972011-04-06 18:34:11 +00002281 lineOffsets.setLeft(logicalRightForFloat(newFloat));
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002282 else
rniwa@webkit.org059b1972011-04-06 18:34:11 +00002283 lineOffsets.setRight(logicalLeftForFloat(newFloat));
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002284 }
2285
2286 if (!newFloat->m_paginationStrut)
2287 return didPosition;
2288
2289 FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2290 ASSERT(floatingObjectSet.last() == newFloat);
2291
2292 int floatLogicalTop = logicalTopForFloat(newFloat);
2293 int paginationStrut = newFloat->m_paginationStrut;
2294
2295 if (floatLogicalTop - paginationStrut != logicalHeight())
2296 return didPosition;
2297
2298 FloatingObjectSetIterator it = floatingObjectSet.end();
2299 --it; // Last float is newFloat, skip that one.
2300 FloatingObjectSetIterator begin = floatingObjectSet.begin();
2301 while (it != begin) {
2302 --it;
2303 FloatingObject* f = *it;
2304 if (f == lastFloatFromPreviousLine)
2305 break;
2306 if (logicalTopForFloat(f) == logicalHeight()) {
2307 ASSERT(!f->m_paginationStrut);
2308 f->m_paginationStrut = paginationStrut;
2309 RenderBox* o = f->m_renderer;
2310 setLogicalTopForChild(o, logicalTopForChild(o) + marginBeforeForChild(o) + paginationStrut);
2311 if (o->isRenderBlock())
2312 toRenderBlock(o)->setChildNeedsLayout(true, false);
2313 o->layoutIfNeeded();
2314 setLogicalTopForFloat(f, logicalTopForFloat(f) + f->m_paginationStrut);
2315 }
2316 }
2317
2318 setLogicalHeight(blockOffset + paginationStrut);
rniwa@webkit.org059b1972011-04-06 18:34:11 +00002319 lineOffsets.setLeft(logicalLeftOffsetForLine(logicalHeight(), firstLine));
2320 lineOffsets.setRight(logicalRightOffsetForLine(logicalHeight(), firstLine));
rniwa@webkit.org73ce42a2011-04-06 14:10:02 +00002321
2322 return didPosition;
2323}
2324
hyattffe78712003-02-11 01:59:29 +00002325}