blob: e57f28e8f83e7b6b2bb5a4455f1bd837bb269639 [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.com1d4e9532010-01-18 20:55:25 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2010 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"
darinf9e5d6c2007-01-09 14:54:26 +000026#include "CharacterNames.h"
mitz@apple.comb2107652010-06-21 16:54:52 +000027#include "Hyphenation.h"
hyatt@apple.com71eeb442010-02-11 20:05:51 +000028#include "InlineIterator.h"
eseidel3a6d1322006-01-09 03:14:50 +000029#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000030#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000031#include "RenderArena.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"
hyattd8048342006-05-31 01:48:18 +000035#include "RenderView.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000036#include "TrailingFloatsRootInlineBox.h"
darin36d11362006-04-11 16:30:21 +000037#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000038#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000039#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000040#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000041#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000042
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000043#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000044#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000045#include "SVGRootInlineBox.h"
46#endif
47
darin7bd70952006-04-13 07:07:34 +000048using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000049using namespace WTF;
50using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000051
darinb9481ed2006-03-20 02:57:59 +000052namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000053
hyatt1d5d87b2007-04-24 04:55:54 +000054// We don't let our line box tree for a single line get any deeper than this.
55const unsigned cMaxLineDepth = 200;
56
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000057static int getBorderPaddingMargin(RenderBoxModelObject* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000058{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000059 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
60 if (leftSide)
61 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
62 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000063}
64
65static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
66{
hyatt1d5d87b2007-04-24 04:55:54 +000067 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000068 int extraWidth = 0;
69 RenderObject* parent = child->parent();
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000070 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyatt@apple.com40232f82009-02-04 04:26:08 +000071 if (start && !child->previousSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000072 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), false);
hyatt@apple.com40232f82009-02-04 04:26:08 +000073 if (end && !child->nextSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000074 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), true);
hyattffe78712003-02-11 01:59:29 +000075 child = parent;
76 parent = child->parent();
77 }
78 return extraWidth;
79}
80
hyatt@apple.comb3466af2009-06-13 06:04:40 +000081static void chopMidpointsAt(LineMidpointState& lineMidpointState, RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +000082{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000083 if (!lineMidpointState.numMidpoints)
hyatt275d0702005-11-03 23:53:57 +000084 return;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000085 InlineIterator* midpoints = lineMidpointState.midpoints.data();
86 for (int i = lineMidpointState.numMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +000087 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +000088 if (point.obj == obj && point.pos == pos) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000089 lineMidpointState.numMidpoints = i;
hyatt78b85132004-03-29 20:07:45 +000090 break;
91 }
92 }
93}
94
hyatt@apple.comb3466af2009-06-13 06:04:40 +000095static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +000096{
97 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +000098 // shave it off the list, and shave off a trailing space if the previous end point doesn't
99 // preserve whitespace.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000100 if (lBreak.obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
101 InlineIterator* midpoints = lineMidpointState.midpoints.data();
102 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
103 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000104 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000105 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000106 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000107 if (currpoint == lBreak) {
108 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000109 lineMidpointState.numMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000110 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000111 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000112 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000113 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000114 if (endpoint.pos + 1 < textObj->textLength()) {
115 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000116 return;
117 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000118 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000119 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000120 return;
121 }
hyattee1bcae2004-03-29 21:36:04 +0000122 }
hyattfe99c872003-07-31 22:25:29 +0000123 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000124 }
hyattfe99c872003-07-31 22:25:29 +0000125 }
126 }
127}
128
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000129static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000130{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000131 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
132 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000133
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000134 InlineIterator* midpoints = lineMidpointState.midpoints.data();
135 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000136}
137
hyatt@apple.com71eeb442010-02-11 20:05:51 +0000138void RenderBlock::appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000139{
hyatt98ee7e42003-05-14 01:39:15 +0000140 if (start > end || obj->isFloating() ||
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000141 (obj->isPositioned() && !obj->style()->hasStaticX() && !obj->style()->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000142 return;
hyatt85586af2003-02-19 23:22:42 +0000143
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000144 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000145 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000146 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000147 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000148 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
149 if (lineMidpointState.betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000150 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000151 return;
152 // This is a new start point. Stop ignoring objects and
153 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000154 lineMidpointState.betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000155 start = nextMidpoint.pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000156 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000157 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000158 return appendRunsForObject(start, end, obj, resolver);
159 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000160 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000161 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000162 return;
163 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000164
hyatt78b85132004-03-29 20:07:45 +0000165 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000166 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000167 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000168 lineMidpointState.betweenMidpoints = true;
169 lineMidpointState.currentMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000170 if (nextMidpoint.pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
mitz@apple.com15035e62008-07-05 20:44:44 +0000171 if (static_cast<int>(nextMidpoint.pos + 1) > start)
172 resolver.addRun(new (obj->renderArena())
173 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
174 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000175 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000176 } else
177 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000178 }
179}
180
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000181static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
182{
183 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000184 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000185
186 if (obj->isText()) {
187 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
188 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
189 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
190 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000191 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000192 return textBox;
193 }
194
195 if (obj->isBox())
196 return toRenderBox(obj)->createInlineBox();
197
eric@webkit.org49b9d952009-07-03 01:29:07 +0000198 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000199}
200
201static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
202{
203 if (o->isText()) {
204 if (o->prefWidthsDirty() && o->isCounter())
205 toRenderText(o)->calcPrefWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
206 toRenderText(o)->dirtyLineBoxes(fullLayout);
207 } else
208 toRenderInline(o)->dirtyLineBoxes(fullLayout);
209}
210
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000211InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000212{
213 // See if we have an unconstructed line box for this object that is also
214 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000215 unsigned lineDepth = 1;
216 InlineFlowBox* childBox = 0;
217 InlineFlowBox* parentBox = 0;
218 InlineFlowBox* result = 0;
219 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000220 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000221
hyatt1d5d87b2007-04-24 04:55:54 +0000222 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000223 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000224
hyatt1d5d87b2007-04-24 04:55:54 +0000225 // If this box is constructed then it is from a previous line, and we need
226 // to make a new box for our line. If this box is unconstructed but it has
227 // something following it on the line, then we know we have to make a new box
228 // as well. In this situation our inline has actually been split in two on
229 // the same line (this can happen with very fancy language mixtures).
230 bool constructedNewBox = false;
231 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
232 // We need to make a new box for this render object. Once
233 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000234 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000235 ASSERT(newBox->isInlineFlowBox());
236 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000237 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000238 constructedNewBox = true;
239 }
mitz@apple.come1364202008-02-28 01:06:41 +0000240
hyatt1d5d87b2007-04-24 04:55:54 +0000241 if (!result)
242 result = parentBox;
243
244 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000245 // inline box for the line, and it doesn't have to be appended to any parent
246 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000247 if (childBox)
248 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000249
hyatt1d5d87b2007-04-24 04:55:54 +0000250 if (!constructedNewBox || obj == this)
251 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000252
hyatt1d5d87b2007-04-24 04:55:54 +0000253 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000254
hyatt1d5d87b2007-04-24 04:55:54 +0000255 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
256 // intermediate inline flows.
257 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000258
hyatt1d5d87b2007-04-24 04:55:54 +0000259 } while (true);
260
261 return result;
hyattffe78712003-02-11 01:59:29 +0000262}
263
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000264RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000265{
mitz@apple.com887f3592008-02-25 22:03:08 +0000266 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000267
268 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000269 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000270 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000271 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000272 if (runCount == 2 && !r->m_object->isListMarker())
273 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000274
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000275 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000276 r->m_box = box;
277
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000278 ASSERT(box);
279 if (!box)
280 continue;
hyattffe78712003-02-11 01:59:29 +0000281
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000282 // If we have no parent box yet, or if the run is not simply a sibling,
283 // then we need to construct inline boxes as necessary to properly enclose the
284 // run's inline box.
285 if (!parentBox || parentBox->renderer() != r->m_object->parent())
286 // Create new inline boxes all the way back to the appropriate insertion point.
287 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000288
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000289 // Append the inline box to this line.
290 parentBox->addToLine(box);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000291
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000292 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
293 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
294
295 if (box->isInlineTextBox()) {
296 InlineTextBox* text = static_cast<InlineTextBox*>(box);
297 text->setStart(r->m_start);
298 text->setLen(r->m_stop - r->m_start);
299 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000300 if (r->m_hasHyphen)
301 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000302 }
hyattffe78712003-02-11 01:59:29 +0000303 }
304
305 // We should have a root inline box. It should be unconstructed and
306 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000307 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000308
309 // Set bits on our inline flow boxes that indicate which sides should
310 // paint borders/margins/padding. This knowledge will ultimately be used when
311 // we determine the horizontal positions and widths of all the inline boxes on
312 // the line.
hyattffe78712003-02-11 01:59:29 +0000313 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
314
315 // Now mark the line boxes as being constructed.
316 lastLineBox()->setConstructed();
317
318 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000319 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000320}
321
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000322void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000323{
324 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000325 int availableWidth = lineWidth(height(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000326 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000327 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000328 unsigned numSpaces = 0;
329 ETextAlign textAlign = style()->textAlign();
330
mitz@apple.come1364202008-02-28 01:06:41 +0000331 for (BidiRun* r = firstRun; r; r = r->next()) {
332 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000333 continue; // Positioned objects are only participating to figure out their
334 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000335 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000336 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000337 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000338
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000339 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000340 const UChar* characters = rt->characters();
341 for (int i = r->m_start; i < r->m_stop; i++) {
342 UChar c = characters[i];
343 if (c == ' ' || c == '\n' || c == '\t')
344 numSpaces++;
345 }
346 }
347
mitz@apple.come1364202008-02-28 01:06:41 +0000348 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000349 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000350 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000351 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000352 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000353 HashSet<const SimpleFontData*> fallbackFonts;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000354 GlyphOverflow glyphOverflow;
mitz@apple.comb2107652010-06-21 16:54:52 +0000355 int hyphenWidth = 0;
356 if (static_cast<InlineTextBox*>(r->m_box)->hasHyphen()) {
357 const AtomicString& hyphenString = rt->style()->hyphenString();
358 hyphenWidth = rt->style(firstLine)->font().width(TextRun(hyphenString.characters(), hyphenString.length()));
359 }
360 r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, firstLine, &fallbackFonts, &glyphOverflow) + hyphenWidth);
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000361 if (!fallbackFonts.isEmpty()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000362 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000363 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
364 ASSERT(it->second.first.isEmpty());
365 copyToVector(fallbackFonts, it->second.first);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000366 }
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000367 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000368 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000369 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
370 it->second.second = glyphOverflow;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000371 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000372 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000373 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000374 renderBox->calcWidth();
375 r->m_box->setWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000376 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000377 }
hyatt4b381692003-03-10 21:11:59 +0000378
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000379 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000380 }
381
382 // Armed with the total width of the line (without justification),
383 // we now examine our text-align property in order to determine where to position the
384 // objects horizontally. The total width of the line can be increased if we end up
385 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000386 int x = leftOffset(height(), firstLine);
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000387 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000388 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000389 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000390 // The direction of the block should determine what happens with wide lines. In
391 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000392 if (style()->direction() == LTR) {
393 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000394 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000395 } else {
396 if (trailingSpaceRun)
397 trailingSpaceRun->m_box->setWidth(0);
398 else if (totWidth > availableWidth)
399 x -= (totWidth - availableWidth);
400 }
hyattffe78712003-02-11 01:59:29 +0000401 break;
402 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000403 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
404 if (trailingSpaceRun) {
405 totWidth -= trailingSpaceRun->m_box->width();
406 trailingSpaceRun->m_box->setWidth(0);
407 }
hyattffe78712003-02-11 01:59:29 +0000408 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000409 }
hyattffe78712003-02-11 01:59:29 +0000410 // fall through
411 case TAAUTO:
412 numSpaces = 0;
413 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000414 if (style()->direction() == LTR) {
415 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000416 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
hyattffe78712003-02-11 01:59:29 +0000417 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000418 }
hyattffe78712003-02-11 01:59:29 +0000419 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000420 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000421 // Wide lines spill out of the block based off direction.
422 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
423 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000424 if (style()->direction() == LTR) {
425 if (trailingSpaceRun) {
426 totWidth -= trailingSpaceRun->m_box->width();
427 trailingSpaceRun->m_box->setWidth(0);
428 }
429 if (totWidth < availableWidth)
430 x += availableWidth - totWidth;
431 } else {
432 if (totWidth > availableWidth && trailingSpaceRun) {
mitz@apple.comcbf51272009-04-07 17:08:18 +0000433 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000434 totWidth -= trailingSpaceRun->m_box->width();
435 } else
436 x += availableWidth - totWidth;
437 }
hyattffe78712003-02-11 01:59:29 +0000438 break;
439 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000440 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000441 int trailingSpaceWidth = 0;
442 if (trailingSpaceRun) {
443 totWidth -= trailingSpaceRun->m_box->width();
444 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
mitz@apple.comcbf51272009-04-07 17:08:18 +0000445 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000446 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000447 if (style()->direction() == LTR)
448 x += max((availableWidth - totWidth) / 2, 0);
449 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000450 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000451 break;
452 }
453
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000454 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000455 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000456 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000457 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000458
hyattacbb0d42003-04-25 01:32:49 +0000459 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000460 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000461 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000462 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000463 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000464 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000465 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000466 spaces++;
darina3c48282003-10-07 15:49:30 +0000467 }
hyatt870bdda2003-05-21 23:37:33 +0000468
darinb53ebdc2006-07-09 15:10:21 +0000469 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000470
hyattdca76e92005-11-02 08:52:50 +0000471 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000472 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000473 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000474 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000475 totWidth += spaceAdd;
476 }
hyattacbb0d42003-04-25 01:32:49 +0000477 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000478 if (!numSpaces)
479 break;
hyattacbb0d42003-04-25 01:32:49 +0000480 }
hyattffe78712003-02-11 01:59:29 +0000481 }
hyattffe78712003-02-11 01:59:29 +0000482 }
mitz@apple.come1364202008-02-28 01:06:41 +0000483
hyattffe78712003-02-11 01:59:29 +0000484 // The widths of all runs are now known. We can now place every inline box (and
485 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000486 needsWordSpacing = false;
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000487 lineBox->placeBoxesHorizontally(x, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000488}
489
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000490void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000491{
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000492 setHeight(lineBox->verticallyAlignBoxes(height(), textBoxDataMap));
hyatt@apple.comd885df72009-01-22 02:31:52 +0000493 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000494
495 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000496 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000497 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000498 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000499 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000500
hyatt98ee7e42003-05-14 01:39:15 +0000501 // Align positioned boxes with the top of the line box. This is
502 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000503 if (r->m_object->isPositioned())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000504 r->m_box->setY(height());
hyatt98ee7e42003-05-14 01:39:15 +0000505
506 // Position is used to properly position both replaced elements and
507 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000508 if (r->m_object->isText())
509 toRenderText(r->m_object)->positionLineBox(r->m_box);
510 else if (r->m_object->isBox())
511 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000512 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000513 // Positioned objects and zero-length text nodes destroy their boxes in
514 // position(), which unnecessarily dirties the line.
515 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000516}
kociendabb0c24b2001-08-24 14:24:40 +0000517
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000518static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
519{
520 if (character == ' ' || character == '\t' || character == softHyphen)
521 return true;
522 if (character == '\n')
523 return !renderer->style()->preserveNewline();
524 if (character == noBreakSpace)
525 return renderer->style()->nbspMode() == SPACE;
526 return false;
527}
528
jamesr@google.com19548ad2010-04-02 23:21:35 +0000529void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
530{
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000531 bool useRepaintBounds = false;
532
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000533 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000534
hyatt@apple.comd885df72009-01-22 02:31:52 +0000535 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000536 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000537
hyatt0c3a9862004-02-23 21:26:26 +0000538 // Figure out if we should clear out our line boxes.
539 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000540 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000541 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000542 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000543
hyatted77ad82004-06-15 07:21:23 +0000544 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
545 // clip.
546 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
547 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
548 // anyway, so we won't worry about following the draft here.
549 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000550
hyatted77ad82004-06-15 07:21:23 +0000551 // Walk all the lines and delete our ellipsis line boxes if they exist.
552 if (hasTextOverflow)
553 deleteEllipsisLineBoxes();
554
hyattffe78712003-02-11 01:59:29 +0000555 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000556 // layout replaced elements
557 bool endOfInline = false;
558 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000559 Vector<FloatWithRect> floats;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000560 bool hasInlineChild = false;
561 while (o) {
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +0000562 if (!hasInlineChild && o->isInline())
563 hasInlineChild = true;
564
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000565 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
566 RenderBox* box = toRenderBox(o);
567
568 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
569 o->setChildNeedsLayout(true, false);
570
571 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
572 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
573 o->setPrefWidthsDirty(true, false);
574
575 if (o->isPositioned())
576 o->containingBlock()->insertPositionedObject(box);
577 else {
578 if (o->isFloating())
579 floats.append(FloatWithRect(box));
580 else if (fullLayout || o->needsLayout()) // Replaced elements
581 toRenderBox(o)->dirtyLineBoxes(fullLayout);
582
583 o->layoutIfNeeded();
584 }
585 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000586 if (fullLayout || o->selfNeedsLayout())
587 dirtyLineBoxesForRenderer(o, fullLayout);
588 o->setNeedsLayout(false);
589 if (!o->isText())
590 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
591 }
592 o = bidiNext(this, o, 0, false, &endOfInline);
593 }
594
595 // We want to skip ahead to the first dirty line
596 InlineBidiResolver resolver;
597 unsigned floatIndex;
598 bool firstLine = true;
599 bool previousLineBrokeCleanly = true;
600 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000601
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000602 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000603 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
604 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000605 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000606 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000607 // Because we waited until we were already inside layout to discover
608 // that the block really needed a full layout, we missed our chance to repaint the layer
609 // before layout started. Luckily the layer has cached the repaint rect for its original
610 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000611 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000612 }
613 }
hyatt0c3a9862004-02-23 21:26:26 +0000614
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000615 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
616
617 LineMidpointState& lineMidpointState = resolver.midpointState();
618
619 // We also find the first clean line and extract these lines. We will add them back
620 // if we determine that we're able to synchronize after handling all our dirty lines.
621 InlineIterator cleanLineStart;
622 BidiStatus cleanLineBidiStatus;
623 int endLineYPos = 0;
624 RootInlineBox* endLine = (fullLayout || !startLine) ?
625 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
626
627 if (startLine) {
628 useRepaintBounds = true;
629 repaintTop = height();
630 repaintBottom = height();
631 RenderArena* arena = renderArena();
632 RootInlineBox* box = startLine;
633 while (box) {
634 repaintTop = min(repaintTop, box->topVisibleOverflow());
635 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
636 RootInlineBox* next = box->nextRootBox();
637 box->deleteLine(arena);
638 box = next;
639 }
640 }
641
642 InlineIterator end = resolver.position();
643
644 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
645 // If the last line before the start line ends with a line break that clear floats,
646 // adjust the height accordingly.
647 // A line break can be either the first or the last object on a line, depending on its direction.
648 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
649 RenderObject* lastObject = lastLeafChild->renderer();
650 if (!lastObject->isBR())
651 lastObject = lastRootBox()->firstLeafChild()->renderer();
652 if (lastObject->isBR()) {
653 EClear clear = lastObject->style()->clear();
654 if (clear != CNONE)
655 newLine(clear);
656 }
657 }
658 }
659
660 bool endLineMatched = false;
661 bool checkForEndLineMatch = endLine;
662 bool checkForFloatsFromLastLine = false;
663
664 bool isLineEmpty = true;
665
666 while (!end.atEnd()) {
667 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
668 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
669 break;
670
671 lineMidpointState.reset();
672
673 isLineEmpty = true;
674
675 EClear clear = CNONE;
mitz@apple.comb2107652010-06-21 16:54:52 +0000676 bool hyphenated;
677 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, hyphenated, &clear);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000678 if (resolver.position().atEnd()) {
679 resolver.deleteRuns();
680 checkForFloatsFromLastLine = true;
681 break;
682 }
683 ASSERT(end != resolver.position());
684
685 if (!isLineEmpty) {
mitz@apple.com62d5bbd2010-06-25 18:07:01 +0000686 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000687 ASSERT(resolver.position() == end);
688
689 BidiRun* trailingSpaceRun = 0;
690 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
691 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
692 trailingSpaceRun = resolver.logicallyLastRun();
693 RenderObject* lastObject = trailingSpaceRun->m_object;
694 if (lastObject->isText()) {
695 RenderText* lastText = toRenderText(lastObject);
696 const UChar* characters = lastText->characters();
697 int firstSpace = trailingSpaceRun->stop();
698 while (firstSpace > trailingSpaceRun->start()) {
699 UChar current = characters[firstSpace - 1];
700 if (!isCollapsibleSpace(current, lastText))
701 break;
702 firstSpace--;
703 }
704 if (firstSpace == trailingSpaceRun->stop())
705 trailingSpaceRun = 0;
706 else {
707 TextDirection direction = style()->direction();
708 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
709 if (firstSpace != trailingSpaceRun->start()) {
710 BidiContext* baseContext = resolver.context();
711 while (BidiContext* parent = baseContext->parent())
712 baseContext = parent;
713
714 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
715 trailingSpaceRun->m_stop = firstSpace;
716 if (direction == LTR)
717 resolver.addRun(newTrailingRun);
718 else
719 resolver.prependRun(newTrailingRun);
720 trailingSpaceRun = newTrailingRun;
721 shouldReorder = false;
722 }
723 if (shouldReorder) {
724 if (direction == LTR) {
725 resolver.moveRunToEnd(trailingSpaceRun);
726 trailingSpaceRun->m_level = 0;
727 } else {
728 resolver.moveRunToBeginning(trailingSpaceRun);
729 trailingSpaceRun->m_level = 1;
730 }
731 }
732 }
733 } else
734 trailingSpaceRun = 0;
735 }
736
737 // Now that the runs have been ordered, we create the line boxes.
738 // At the same time we figure out where border/padding/margin should be applied for
739 // inline flow boxes.
740
741 RootInlineBox* lineBox = 0;
742 if (resolver.runCount()) {
mitz@apple.comb2107652010-06-21 16:54:52 +0000743 if (hyphenated)
744 resolver.logicallyLastRun()->m_hasHyphen = true;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000745 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
746 if (lineBox) {
747 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
748
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000749#if ENABLE(SVG)
750 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
751#else
752 bool isSVGRootInlineBox = false;
753#endif
754
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000755 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000756
757 // Now we position all of our text runs horizontally.
758 if (!isSVGRootInlineBox)
759 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000760
761 // Now position our text runs vertically.
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000762 computeVerticalPositionsForLine(lineBox, resolver.firstRun(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000763
764#if ENABLE(SVG)
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000765 // SVG text layout code computes vertical & horizontal positions on its own.
766 // Note that we still need to execute computeVerticalPositionsForLine() as
767 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
768 // contains reversed text or not. If we wouldn't do that editing and thus
769 // text selection in RTL boxes would not work as expected.
770 if (isSVGRootInlineBox) {
771 ASSERT(isSVGText());
772 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
773 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000774#endif
775
776#if PLATFORM(MAC)
777 // Highlight acts as an overflow inflation.
778 if (style()->highlight() != nullAtom)
779 lineBox->addHighlightOverflow();
780#endif
781 }
782 }
783
784 resolver.deleteRuns();
785
786 if (lineBox) {
787 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
788 if (useRepaintBounds) {
789 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
790 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
791 }
792 }
793
794 firstLine = false;
795 newLine(clear);
796 }
797
798 if (m_floatingObjects && lastRootBox()) {
799 if (lastFloat) {
800 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
801 }
802 m_floatingObjects->next();
803 } else
804 m_floatingObjects->first();
805 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
806 lastRootBox()->floats().append(f->m_renderer);
807 ASSERT(f->m_renderer == floats[floatIndex].object);
808 // If a float's geometry has changed, give up on syncing with clean lines.
809 if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
810 checkForEndLineMatch = false;
811 floatIndex++;
812 }
813 lastFloat = m_floatingObjects->last();
814 }
815
816 lineMidpointState.reset();
817 resolver.setPosition(end);
818 }
819
820 if (endLine) {
821 if (endLineMatched) {
822 // Attach all the remaining lines, and then adjust their y-positions as needed.
823 int delta = height() - endLineYPos;
824 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
825 line->attachLine();
826 if (delta) {
827 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
828 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
829 line->adjustPosition(0, delta);
830 }
831 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
832 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
833 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
834 int floatTop = (*f)->y() - (*f)->marginTop();
835 insertFloatingObject(*f);
836 setHeight(floatTop + delta);
837 positionNewFloats();
838 }
839 }
840 }
841 setHeight(lastRootBox()->blockHeight());
842 } else {
843 // Delete all the remaining lines.
844 RootInlineBox* line = endLine;
845 RenderArena* arena = renderArena();
846 while (line) {
847 repaintTop = min(repaintTop, line->topVisibleOverflow());
848 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
849 RootInlineBox* next = line->nextRootBox();
850 line->deleteLine(arena);
851 line = next;
852 }
853 }
854 }
855 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
856 // In case we have a float on the last line, it might not be positioned up to now.
857 // This has to be done before adding in the bottom border/padding, or the float will
858 // include the padding incorrectly. -dwh
859 if (checkForFloatsFromLastLine) {
860 int bottomVisualOverflow = lastRootBox()->bottomVisualOverflow();
861 int bottomLayoutOverflow = lastRootBox()->bottomLayoutOverflow();
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000862 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000863 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
864 trailingFloatsLineBox->setConstructed();
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000865 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
866 trailingFloatsLineBox->verticallyAlignBoxes(height(), textBoxDataMap);
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000867 trailingFloatsLineBox->setVerticalOverflowPositions(height(), bottomLayoutOverflow, height(), bottomVisualOverflow, 0);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000868 trailingFloatsLineBox->setBlockHeight(height());
869 }
870 if (lastFloat) {
871 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
872 }
873 m_floatingObjects->next();
874 } else
875 m_floatingObjects->first();
876 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
877 lastRootBox()->floats().append(f->m_renderer);
878 lastFloat = m_floatingObjects->last();
879 }
880 size_t floatCount = floats.size();
881 // Floats that did not have layout did not repaint when we laid them out. They would have
882 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
883 // painted.
884 for (size_t i = 0; i < floatCount; ++i) {
885 if (!floats[i].everHadLayout) {
886 RenderBox* f = floats[i].object;
887 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
888 f->repaint();
889 }
890 }
kociendabb0c24b2001-08-24 14:24:40 +0000891 }
hyatt85586af2003-02-19 23:22:42 +0000892
hyatta70560a2002-11-20 01:53:20 +0000893 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000894 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +0000895
adele7a470a72006-04-20 22:22:14 +0000896 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000897 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +0000898
899 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
900 // truncate text.
901 if (hasTextOverflow)
902 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +0000903}
904
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000905RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
906 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +0000907{
908 RootInlineBox* curr = 0;
909 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000910
mitz@apple.com40547b32008-03-18 04:04:34 +0000911 bool dirtiedByFloat = false;
912 if (!fullLayout) {
913 size_t floatIndex = 0;
914 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000915 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
916 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
917 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
918 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +0000919 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
920 ASSERT(floatIndex < floats.size());
921 if (floats[floatIndex].object != f) {
922 // A new float has been inserted before this line or before its last known float.
923 // Just do a full layout.
924 fullLayout = true;
925 break;
926 }
927 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +0000928 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +0000929 curr->markDirty();
mitz@apple.com1d4e9532010-01-18 20:55:25 +0000930 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()), curr);
mitz@apple.com40547b32008-03-18 04:04:34 +0000931 floats[floatIndex].rect.setSize(newSize);
932 dirtiedByFloat = true;
933 }
934 floatIndex++;
935 }
936 }
937 if (dirtiedByFloat || fullLayout)
938 break;
939 }
940 // Check if a new float has been inserted after the last known float.
941 if (!curr && floatIndex < floats.size())
942 fullLayout = true;
943 }
944
hyatt0c3a9862004-02-23 21:26:26 +0000945 if (fullLayout) {
946 // Nuke all our lines.
947 if (firstRootBox()) {
948 RenderArena* arena = renderArena();
949 curr = firstRootBox();
950 while (curr) {
951 RootInlineBox* next = curr->nextRootBox();
952 curr->deleteLine(arena);
953 curr = next;
954 }
darinec375482007-01-06 01:36:24 +0000955 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +0000956 }
eseidel789896f2005-11-27 22:52:09 +0000957 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000958 if (curr) {
959 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +0000960 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +0000961 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +0000962 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +0000963 // The previous line didn't break cleanly or broke at a newline
964 // that has been deleted, so treat it as dirty too.
965 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +0000966 }
eseidel789896f2005-11-27 22:52:09 +0000967 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000968 // No dirty lines were found.
969 // If the last line didn't break cleanly, treat it as dirty.
970 if (lastRootBox() && !lastRootBox()->endsWithBreak())
971 curr = lastRootBox();
972 }
mitz@apple.come1364202008-02-28 01:06:41 +0000973
hyatt0c3a9862004-02-23 21:26:26 +0000974 // If we have no dirty lines, then last is just the last root box.
975 last = curr ? curr->prevRootBox() : lastRootBox();
976 }
mitz@apple.come1364202008-02-28 01:06:41 +0000977
mitz@apple.com40547b32008-03-18 04:04:34 +0000978 numCleanFloats = 0;
979 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000980 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000981 // Restore floats from clean lines.
982 RootInlineBox* line = firstRootBox();
983 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000984 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
985 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
986 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +0000987 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000988 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +0000989 positionNewFloats();
990 ASSERT(floats[numCleanFloats].object == *f);
991 numCleanFloats++;
992 }
993 }
994 line = line->nextRootBox();
995 }
hyatt@apple.comd885df72009-01-22 02:31:52 +0000996 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +0000997 }
998
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000999 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001000 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001001
1002 RenderObject* startObj;
1003 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001004 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001005 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001006 startObj = last->lineBreakObj();
1007 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001008 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001009 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001010 bool ltr = style()->direction() == LTR
1011 #if ENABLE(SVG)
1012 || (style()->unicodeBidi() == UBNormal && isSVGText())
1013 #endif
1014 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001015
mitz@apple.com45d9e102009-04-27 16:24:55 +00001016 Direction direction = ltr ? LeftToRight : RightToLeft;
1017 resolver.setLastStrongDir(direction);
1018 resolver.setLastDir(direction);
1019 resolver.setEorDir(direction);
1020 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001021
mitz@apple.com15035e62008-07-05 20:44:44 +00001022 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001023 }
mitz@apple.come1364202008-02-28 01:06:41 +00001024
mitz@apple.com15035e62008-07-05 20:44:44 +00001025 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001026
hyatt0c3a9862004-02-23 21:26:26 +00001027 return curr;
1028}
1029
mitz@apple.com15035e62008-07-05 20:44:44 +00001030RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001031{
1032 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001033 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001034 last = 0;
1035 else {
hyatt04420ca2004-07-16 00:05:42 +00001036 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1037 if (curr->isDirty())
1038 last = 0;
1039 else if (!last)
1040 last = curr;
1041 }
hyatt0c3a9862004-02-23 21:26:26 +00001042 }
mitz@apple.come1364202008-02-28 01:06:41 +00001043
hyatt0c3a9862004-02-23 21:26:26 +00001044 if (!last)
1045 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001046
eseidel789896f2005-11-27 22:52:09 +00001047 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001048 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001049 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001050 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001051
hyatt0c3a9862004-02-23 21:26:26 +00001052 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1053 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1054 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001055
hyatt0c3a9862004-02-23 21:26:26 +00001056 return last;
1057}
1058
mitz@apple.com15035e62008-07-05 20:44:44 +00001059bool RenderBlock::matchedEndLine(const InlineBidiResolver& resolver, const InlineIterator& endLineStart, const BidiStatus& endLineStatus, RootInlineBox*& endLine, int& endYPos, int& repaintBottom, int& repaintTop)
hyatt0c3a9862004-02-23 21:26:26 +00001060{
mitz@apple.com15035e62008-07-05 20:44:44 +00001061 if (resolver.position() == endLineStart) {
1062 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001063 return false;
1064
hyatt@apple.comd885df72009-01-22 02:31:52 +00001065 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001066 if (!delta || !m_floatingObjects)
1067 return true;
1068
1069 // See if any floats end in the range along which we want to shift the lines vertically.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001070 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001071
1072 RootInlineBox* lastLine = endLine;
1073 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1074 lastLine = nextLine;
1075
1076 int bottom = lastLine->blockHeight() + abs(delta);
1077
1078 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001079 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001080 return false;
1081 }
1082
1083 return true;
1084 }
hyatt0c3a9862004-02-23 21:26:26 +00001085
mitz@apple.come1364202008-02-28 01:06:41 +00001086 // The first clean line doesn't match, but we can check a handful of following lines to try
1087 // to match back up.
1088 static int numLines = 8; // The # of lines we're willing to match against.
1089 RootInlineBox* line = endLine;
1090 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001091 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001092 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001093 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001094 return false; // ...but the bidi state doesn't match.
1095 RootInlineBox* result = line->nextRootBox();
1096
1097 // Set our yPos to be the block height of endLine.
1098 if (result)
1099 endYPos = line->blockHeight();
1100
hyatt@apple.comd885df72009-01-22 02:31:52 +00001101 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001102 if (delta && m_floatingObjects) {
1103 // See if any floats end in the range along which we want to shift the lines vertically.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001104 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001105
1106 RootInlineBox* lastLine = endLine;
1107 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1108 lastLine = nextLine;
1109
1110 int bottom = lastLine->blockHeight() + abs(delta);
1111
1112 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001113 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001114 return false;
1115 }
1116 }
1117
mitz@apple.come1364202008-02-28 01:06:41 +00001118 // Now delete the lines that we failed to sync.
1119 RootInlineBox* boxToDelete = endLine;
1120 RenderArena* arena = renderArena();
1121 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001122 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1123 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001124 RootInlineBox* next = boxToDelete->nextRootBox();
1125 boxToDelete->deleteLine(arena);
1126 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001127 }
mitz@apple.come1364202008-02-28 01:06:41 +00001128
1129 endLine = result;
1130 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001131 }
1132 }
mitz@apple.come1364202008-02-28 01:06:41 +00001133
hyatt0c3a9862004-02-23 21:26:26 +00001134 return false;
1135}
1136
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001137static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001138{
darinf9e5d6c2007-01-09 14:54:26 +00001139 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001140 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001141
hyattdca76e92005-11-02 08:52:50 +00001142 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1143 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001144 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001145 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1146 // |true|).
1147 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001148 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001149
kocienda498d1982004-10-15 21:07:24 +00001150 return true;
kocienda98440082004-10-14 23:51:47 +00001151}
1152
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001153static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001154{
1155 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1156}
1157
zimmermannac3781f2007-02-04 01:25:03 +00001158static inline bool shouldPreserveNewline(RenderObject* object)
1159{
mjsd2948ef2007-02-26 19:29:04 +00001160#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001161 if (object->isSVGInlineText())
zimmermannac3781f2007-02-04 01:25:03 +00001162 return false;
1163#endif
1164
1165 return object->style()->preserveNewline();
1166}
1167
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001168static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001169{
1170 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001171 // We need to fix this, though, because at the very least, inlines containing only
1172 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001173 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001174}
1175
hyatt@apple.com71eeb442010-02-11 20:05:51 +00001176bool RenderBlock::requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001177{
bdakinf876bee2007-10-30 05:27:09 +00001178 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001179 return false;
bdakinf876bee2007-10-30 05:27:09 +00001180
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001181 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001182 return false;
1183
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001184 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001185 return true;
1186
1187 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001188 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1189 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001190}
1191
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001192bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001193{
1194 ASSERT(inlineObj->parent() == this);
1195
mitz@apple.com15035e62008-07-05 20:44:44 +00001196 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001197 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001198 it.increment();
bdashccffb432007-07-13 11:51:40 +00001199
1200 return !it.atEnd();
1201}
1202
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001203// 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 +00001204// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1205// elements quite right. In other words, we need to build this function's work into the normal line
1206// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001207// NB. this function will insert any floating elements that would otherwise
1208// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001209void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001210{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001211 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001212 RenderObject* object = iterator.obj;
1213 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001214 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001215 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001216 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1217 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001218 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001219 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001220 // A relative positioned inline encloses us. In this case, we also have to determine our
1221 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1222 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001223 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1224 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001225 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001226
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001227 RenderBox* box = toRenderBox(object);
1228 if (box->style()->hasStaticX()) {
1229 if (box->style()->isOriginalDisplayInlineType())
1230 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001231 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001232 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001233 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001234
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001235 if (box->style()->hasStaticY())
1236 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001237 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001238 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001239 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001240}
bdashccffb432007-07-13 11:51:40 +00001241
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001242int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly)
mitz@apple.com1a301772008-03-11 18:30:36 +00001243{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001244 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001245 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001246 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001247 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001248 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001249 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001250 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001251 } else if (object->isPositioned()) {
1252 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1253 // will work for the common cases
1254 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001255 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001256 // A relative positioned inline encloses us. In this case, we also have to determine our
1257 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1258 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001259 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1260 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001261 }
1262
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001263 RenderBox* box = toRenderBox(object);
1264 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001265 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001266 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001267 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001268 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001269 }
1270
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001271 if (box->style()->hasStaticY())
1272 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001273 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001274 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001275 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001276 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001277 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001278}
1279
bdakinf876bee2007-10-30 05:27:09 +00001280// This is currently just used for list markers and inline flows that have line boxes. Neither should
1281// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001282static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001283{
mitz@apple.com1a301772008-03-11 18:30:36 +00001284 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001285 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1286 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001287 UChar nextChar = nextText->characters()[0];
1288 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001289 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001290 return true;
1291 }
1292 }
1293
1294 return false;
1295}
1296
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001297void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001298{
1299 ASSERT(widthToFit > availableWidth);
1300
1301 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001302 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001303 int newLineWidth = availableWidth;
1304 while (true) {
1305 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1306 if (!floatBottom)
1307 break;
1308
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001309 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001310 lastFloatBottom = floatBottom;
1311 if (newLineWidth >= widthToFit)
1312 break;
1313 }
1314
1315 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001316 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001317 availableWidth = newLineWidth;
1318 }
1319}
1320
mitz@apple.com34106442009-02-01 06:23:39 +00001321static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1322{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001323 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001324 return text->width(from, len, font, xPos);
1325 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1326}
1327
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001328static void tryHyphenating(RenderText* text, const Font& font, const AtomicString& localeIdentifier, int lastSpace, int pos, int xPos, int availableWidth, bool isFixedPitch, bool collapseWhiteSpace, int lastSpaceWordSpacing, InlineIterator& lineBreak, int nextBreakable, bool& hyphenated)
mitz@apple.comb2107652010-06-21 16:54:52 +00001329{
1330 const AtomicString& hyphenString = text->style()->hyphenString();
1331 int hyphenWidth = font.width(TextRun(hyphenString.characters(), hyphenString.length()));
1332
mitz@apple.com7c67b292010-09-12 23:04:16 +00001333 int maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
1334 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1335 // that an hyphenation opportunity exists, so do not bother to look for it.
1336 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1337 return;
1338
1339 unsigned prefixLength = font.offsetForPosition(TextRun(text->characters() + lastSpace, pos - lastSpace, !collapseWhiteSpace, xPos + lastSpaceWordSpacing), maxPrefixWidth, false);
mitz@apple.comb2107652010-06-21 16:54:52 +00001340 if (!prefixLength)
1341 return;
1342
mitz@apple.com2ef44912010-08-07 21:41:06 +00001343 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, prefixLength + 1, localeIdentifier);
1344 if (!prefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00001345 return;
1346
1347#if !ASSERT_DISABLED
1348 int prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1349 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001350#else
1351 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001352#endif
1353
1354 lineBreak.obj = text;
1355 lineBreak.pos = lastSpace + prefixLength;
1356 lineBreak.nextBreakablePosition = nextBreakable;
1357 hyphenated = true;
1358}
1359
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001360InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
mitz@apple.comb2107652010-06-21 16:54:52 +00001361 bool& hyphenated, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001362{
mitz@apple.com15035e62008-07-05 20:44:44 +00001363 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001364
mitz@apple.com15035e62008-07-05 20:44:44 +00001365 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001366 LineMidpointState& lineMidpointState = resolver.midpointState();
1367
1368 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly);
mitz@apple.com1a301772008-03-11 18:30:36 +00001369
kociendae40cb942004-10-05 20:05:38 +00001370 int w = 0;
1371 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001372
mitz@apple.com15035e62008-07-05 20:44:44 +00001373 if (resolver.position().atEnd())
1374 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001375
hyatt33f8d492002-11-12 21:44:52 +00001376 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1377 // or not we are currently ignoring whitespace.
1378 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001379 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001380
1381 // This variable tracks whether the very last character we saw was a space. We use
1382 // this to detect when we encounter a second space so we know we have to terminate
1383 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001384 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001385 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001386 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001387
mitz@apple.com15035e62008-07-05 20:44:44 +00001388 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001389
mitz@apple.com15035e62008-07-05 20:44:44 +00001390 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001391 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001392 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001393 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001394 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001395
hyatt0c3a9862004-02-23 21:26:26 +00001396 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1397 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001398
mitz@apple.comb2107652010-06-21 16:54:52 +00001399 hyphenated = false;
1400
ddkilzer5d01fa22007-01-29 03:10:37 +00001401 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001402 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001403
hyatt@apple.com69340902008-01-16 21:24:21 +00001404 // 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 +00001405 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001406 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +00001407 bool allowImagesToBreak = !document()->inQuirksMode() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001408
hyattb0d9f602007-01-15 01:28:23 +00001409 EWhiteSpace currWS = style()->whiteSpace();
1410 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001411 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001412 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1413 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1414
1415 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001416 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001417
mjsd2948ef2007-02-26 19:29:04 +00001418#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001419 bool preserveNewline = o->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001420#else
hyattb0d9f602007-01-15 01:28:23 +00001421 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001422#endif
1423
hyattb0d9f602007-01-15 01:28:23 +00001424 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1425
hyatt275d0702005-11-03 23:53:57 +00001426 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001427 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001428 lBreak.obj = o;
1429 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001430 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001431 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001432
hyatt33f8d492002-11-12 21:44:52 +00001433 // A <br> always breaks a line, so don't let the line be collapsed
1434 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001435 // get collapsed away. It only does this if the previous line broke
1436 // cleanly. Otherwise the <br> has no effect on whether the line is
1437 // empty or not.
1438 if (prevLineBrokeCleanly)
1439 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001440 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001441 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001442
mitz@apple.com71e30842008-03-18 16:13:31 +00001443 if (!isLineEmpty && clear)
1444 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001445 }
1446 goto end;
1447 }
hyattb0d9f602007-01-15 01:28:23 +00001448
hyatt275d0702005-11-03 23:53:57 +00001449 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001450 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001451 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001452 RenderBox* floatBox = toRenderBox(o);
1453 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001454 // check if it fits in the current line.
1455 // If it does, position it now, otherwise, position
1456 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001457 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001458 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001459 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001460 } else
1461 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001462 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001463 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001464 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001465 RenderBox* box = toRenderBox(o);
1466 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1467 bool needToSetStaticX = box->style()->hasStaticX();
1468 if (box->style()->hasStaticX() && !isInlineType) {
1469 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001470 borderLeft() + paddingLeft() :
1471 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001472 needToSetStaticX = false;
1473 }
1474
1475 // If our original display was an INLINE type, then we can go ahead
1476 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001477 bool needToSetStaticY = box->style()->hasStaticY();
1478 if (box->style()->hasStaticY() && isInlineType) {
1479 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001480 needToSetStaticY = false;
1481 }
1482
hyatt853cd7d2004-11-05 02:59:48 +00001483 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1484 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001485 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001486 needToCreateLineBox = true;
1487
hyatt98ee7e42003-05-14 01:39:15 +00001488 // If we're ignoring spaces, we have to stop and include this object and
1489 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001490 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001491 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001492 ignoreStart.obj = o;
1493 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001494 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001495 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1496 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001497 }
hyatt98b16282004-03-31 18:43:12 +00001498
hyatt851816b2003-07-08 07:54:17 +00001499 }
hyatt98ee7e42003-05-14 01:39:15 +00001500 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001501 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001502 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001503 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001504
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001505 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001506
bdakinf876bee2007-10-30 05:27:09 +00001507 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1508 // to make sure that we stop to include this object and then start ignoring spaces again.
1509 // If this object is at the start of the line, we need to behave like list markers and
1510 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001511 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001512 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001513 if (ignoringSpaces) {
1514 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001515 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1516 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001517 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001518 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001519 // Like with list markers, we start ignoring spaces to make sure that any
1520 // additional spaces we see will be discarded.
1521 currentCharacterIsSpace = true;
1522 currentCharacterIsWS = true;
1523 ignoringSpaces = true;
1524 }
1525 }
1526
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001527 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1528 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001529 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001530 RenderBox* replacedBox = toRenderBox(o);
1531
hyattde396342003-10-29 08:57:20 +00001532 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001533 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001534 w += tmpW;
1535 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001536 lBreak.obj = o;
1537 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001538 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001539 }
1540
mitz@apple.combfdc9112008-02-21 19:59:40 +00001541 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001542 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001543
hyatt33f8d492002-11-12 21:44:52 +00001544 isLineEmpty = false;
1545 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001546 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001547 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001548 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001549
bdakinf876bee2007-10-30 05:27:09 +00001550 // Optimize for a common case. If we can't find whitespace after the list
1551 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001552 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001553 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001554 // Like with inline flows, we start ignoring spaces to make sure that any
1555 // additional spaces we see will be discarded.
1556 currentCharacterIsSpace = true;
1557 currentCharacterIsWS = true;
1558 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001559 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001560 if (toRenderListMarker(o)->isInside())
1561 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001562 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001563 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001564 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001565 if (!pos)
1566 appliedStartWidth = false;
1567
darin@apple.com36744d62009-01-25 20:23:04 +00001568 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001569
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001570#if ENABLE(SVG)
1571 bool isSVGText = t->isSVGInlineText();
1572#endif
1573
darin42563ac52007-01-22 17:28:57 +00001574 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001575 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001576 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001577
mitz@apple.comb2107652010-06-21 16:54:52 +00001578 RenderStyle* style = t->style(firstLine);
1579 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001580 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001581 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->hyphenationLocale());
weinigf28a1c32007-02-14 14:10:31 +00001582
hyatt33f8d492002-11-12 21:44:52 +00001583 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001584 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001585 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001586
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001587 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1588 // space, then subtract its width.
mitz@apple.com6ff116d2010-07-06 00:11:08 +00001589 int wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(TextRun(&space, 1)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001590
darin54008922006-01-13 16:39:05 +00001591 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001592 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001593 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1594 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1595 // 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 +00001596 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001597 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001598 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1599
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001600 if (t->isWordBreak()) {
1601 w += tmpW;
1602 tmpW = 0;
1603 lBreak.obj = o;
1604 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001605 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001606 ASSERT(!len);
1607 }
1608
hyatt275d0702005-11-03 23:53:57 +00001609 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001610 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001611 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001612 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001613 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001614
hyattb0d9f602007-01-15 01:28:23 +00001615 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001616 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001617
hyatt78b85132004-03-29 20:07:45 +00001618 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001619 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001620 if (!ignoringSpaces) {
1621 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001622 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001623 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001624 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001625 else
darin@apple.com36744d62009-01-25 20:23:04 +00001626 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001627 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001628 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1629 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1630 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001631 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001632 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001633
hyatt78b85132004-03-29 20:07:45 +00001634 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001635 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001636
hyattdca76e92005-11-02 08:52:50 +00001637 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001638 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001639 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001640 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001641
mitz@apple.com15035e62008-07-05 20:44:44 +00001642 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001643 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001644 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001645 }
mitz@apple.combe429562008-03-07 01:09:51 +00001646
hyatt78b85132004-03-29 20:07:45 +00001647 pos++;
1648 len--;
eseideld13fe532005-11-30 02:40:29 +00001649 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001650 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
mitz@apple.comb2107652010-06-21 16:54:52 +00001651 if (style->hyphens() == HyphensNone) {
1652 // Prevent a line break at the soft hyphen by ensuring that betweenWords is false
1653 // in the next iteration.
1654 atStart = true;
1655 }
hyatt78b85132004-03-29 20:07:45 +00001656 continue;
1657 }
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001658
1659#if ENABLE(SVG)
1660 if (isSVGText) {
1661 RenderSVGInlineText* svgInlineText = static_cast<RenderSVGInlineText*>(t);
1662 if (pos > 0) {
1663 if (svgInlineText->characterStartsNewTextChunk(pos)) {
1664 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1));
1665 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
1666 }
1667 }
1668 }
1669#endif
1670
hyatt3aff2332003-01-23 01:15:28 +00001671 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001672
darinf9e5d6c2007-01-09 14:54:26 +00001673 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001674
weiniged111c12007-07-13 22:45:11 +00001675 if ((breakAll || breakWords) && !midWordBreak) {
1676 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001677 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001678 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001679 }
darin47ece0d2005-09-04 07:42:31 +00001680
ddkilzere8759ef2007-03-25 06:28:19 +00001681 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001682
weiniged111c12007-07-13 22:45:11 +00001683 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001684 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001685 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001686 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001687 // Stop ignoring spaces and begin at this
1688 // new point.
hyatt48710d62003-08-21 09:17:13 +00001689 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001690 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001691 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001692 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001693 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001694 } else {
hyatt33f8d492002-11-12 21:44:52 +00001695 // Just keep ignoring these spaces.
1696 pos++;
1697 len--;
1698 continue;
1699 }
1700 }
rjwc9c257d2003-01-24 03:46:17 +00001701
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001702 int additionalTmpW;
1703 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1704 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1705 else
1706 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001707 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001708 if (!appliedStartWidth) {
1709 tmpW += inlineWidth(o, true, false);
1710 appliedStartWidth = true;
1711 }
1712
eseideld13fe532005-11-30 02:40:29 +00001713 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001714
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001715 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001716 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001717
hyattb0d9f602007-01-15 01:28:23 +00001718 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001719 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001720 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001721 bool lineWasTooWide = false;
1722 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001723 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001724 // Check if line is too big even without the extra space
1725 // at the end of the line. If it is not, do nothing.
1726 // If the line needs the extra whitespace to be too long,
1727 // then move the line break to the space and skip all
1728 // additional whitespace.
1729 if (w + tmpW + charWidth > width) {
1730 lineWasTooWide = true;
1731 lBreak.obj = o;
1732 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001733 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001734 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001735 }
ap932806a2006-10-01 09:06:09 +00001736 }
1737 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001738 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001739 tryHyphenating(t, f, style->hyphenationLocale(), lastSpace, pos, w + tmpW - additionalTmpW, width, isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, nextBreakable, hyphenated);
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001740 if (hyphenated)
1741 goto end;
1742 }
eric@webkit.org1cb44402009-12-29 06:25:16 +00001743 if (lBreak.obj && shouldPreserveNewline(lBreak.obj) && lBreak.obj->isText() && toRenderText(lBreak.obj)->textLength() && !toRenderText(lBreak.obj)->isWordBreak() && toRenderText(lBreak.obj)->characters()[lBreak.pos] == '\n') {
hyatt0c05e102006-04-14 08:15:00 +00001744 if (!stoppedIgnoringSpaces && pos > 0) {
1745 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001746 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1747 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001748 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001749 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001750 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001751 }
hyatt78b85132004-03-29 20:07:45 +00001752 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001753 } else {
weiniged111c12007-07-13 22:45:11 +00001754 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001755 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001756 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001757 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001758 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001759 }
rjwc9c257d2003-01-24 03:46:17 +00001760 }
hyatt33f8d492002-11-12 21:44:52 +00001761
hyattb0d9f602007-01-15 01:28:23 +00001762 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001763 if (!stoppedIgnoringSpaces && pos > 0) {
1764 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001765 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1766 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001767 }
hyatta9f48e32003-02-03 22:48:01 +00001768 lBreak.obj = o;
1769 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001770 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001771 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001772 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001773 return lBreak;
1774 }
hyatta9f48e32003-02-03 22:48:01 +00001775
weinigf28a1c32007-02-14 14:10:31 +00001776 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001777 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001778 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001779 tmpW = 0;
1780 lBreak.obj = o;
1781 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001782 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001783 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1784 // opportunity to break after a word.
1785 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001786 }
hyatt33f8d492002-11-12 21:44:52 +00001787
darin54008922006-01-13 16:39:05 +00001788 if (midWordBreak) {
1789 // Remember this as a breakable position in case
1790 // adding the end width forces a break.
1791 lBreak.obj = o;
1792 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001793 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001794 midWordBreak &= (breakWords || breakAll);
1795 }
1796
1797 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001798 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1799 lastSpace = pos;
1800 }
hyatt33f8d492002-11-12 21:44:52 +00001801
hyattdca76e92005-11-02 08:52:50 +00001802 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001803 // If we encounter a newline, or if we encounter a
1804 // second space, we need to go ahead and break up this
1805 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001806 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001807 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001808
hyatt33f8d492002-11-12 21:44:52 +00001809 // We just entered a mode where we are ignoring
1810 // spaces. Create a midpoint to terminate the run
1811 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001812 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001813 }
1814 }
eseidel789896f2005-11-27 22:52:09 +00001815 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001816 // Stop ignoring spaces and begin at this
1817 // new point.
1818 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001819 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001820 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001821 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001822 }
hyatt98b16282004-03-31 18:43:12 +00001823
1824 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1825 ignoreStart.obj = o;
1826 ignoreStart.pos = pos;
1827 }
harrisone343c412005-01-18 01:07:26 +00001828
1829 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001830 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001831 lBreak.obj = o;
1832 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001833 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00001834 }
1835 }
hyatt33f8d492002-11-12 21:44:52 +00001836
hyattb0d9f602007-01-15 01:28:23 +00001837 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001838 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001839 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001840 trailingSpaceObject = 0;
1841
1842 pos++;
1843 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001844 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001845 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001846
kociendabb0c24b2001-08-24 14:24:40 +00001847 // IMPORTANT: pos is > length here!
mitz@apple.comb2107652010-06-21 16:54:52 +00001848 int additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1849 tmpW += additionalTmpW;
weinigf28a1c32007-02-14 14:10:31 +00001850 tmpW += inlineWidth(o, !appliedStartWidth, true);
mitz@apple.comb2107652010-06-21 16:54:52 +00001851
1852 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001853 tryHyphenating(t, f, style->hyphenationLocale(), lastSpace, pos, w + tmpW - additionalTmpW, width, isFixedPitch, collapseWhiteSpace, lastSpaceWordSpacing, lBreak, nextBreakable, hyphenated);
mitz@apple.comb2107652010-06-21 16:54:52 +00001854 if (hyphenated)
1855 goto end;
1856 }
kociendabb0c24b2001-08-24 14:24:40 +00001857 } else
weinigf28a1c32007-02-14 14:10:31 +00001858 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001859
mitz@apple.com1a301772008-03-11 18:30:36 +00001860 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001861 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001862 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00001863 checkForBreak = true;
1864 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00001865 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00001866 if (currentCharacterIsSpace)
1867 checkForBreak = true;
1868 else {
harrison07b5e582005-08-15 23:31:16 +00001869 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00001870 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001871 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00001872 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00001873 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00001874 // If the next item on the line is text, and if we did not end with
1875 // a space, then the next text run continues our word (and so it needs to
1876 // keep adding to |tmpW|. Just update and continue.
1877 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001878 } else if (nextText->isWordBreak())
1879 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001880 bool willFitOnLine = w + tmpW <= width;
1881 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001882 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001883 willFitOnLine = tmpW <= width;
1884 }
ddkilzere8759ef2007-03-25 06:28:19 +00001885 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00001886 if (canPlaceOnLine && checkForBreak) {
1887 w += tmpW;
1888 tmpW = 0;
1889 lBreak.obj = next;
1890 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001891 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00001892 }
1893 }
1894 }
1895 }
1896
darin54008922006-01-13 16:39:05 +00001897 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00001898 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00001899 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00001900 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001901
1902 if (w)
1903 goto end;
1904
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001905 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00001906
hyatta14d1742003-01-02 20:25:46 +00001907 // |width| may have been adjusted because we got shoved down past a float (thus
1908 // giving us more room), so we need to retest, and only jump to
1909 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00001910 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00001911 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00001912 }
hyatt1d9e29b2003-04-10 01:48:53 +00001913
mitz@apple.com1a301772008-03-11 18:30:36 +00001914 if (!o->isFloatingOrPositioned()) {
1915 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00001916 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001917 w += tmpW;
1918 tmpW = 0;
1919 lBreak.obj = next;
1920 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001921 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001922 }
hyatt711fe232002-11-20 21:25:14 +00001923 }
1924
mitz@apple.com1a301772008-03-11 18:30:36 +00001925 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001926 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001927
hyatta9f48e32003-02-03 22:48:01 +00001928 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
1929 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00001930 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00001931 currentCharacterIsSpace = false;
1932
kociendabb0c24b2001-08-24 14:24:40 +00001933 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00001934 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00001935 }
1936
hyattb0d9f602007-01-15 01:28:23 +00001937
1938 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00001939 lBreak.obj = 0;
1940 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001941 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001942 }
1943
1944 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00001945 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00001946 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00001947 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00001948 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00001949 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00001950 lBreak.obj = o;
1951 lBreak.pos = pos - 1;
1952 } else {
1953 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00001954 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001955 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001956 }
hyatt275d0702005-11-03 23:53:57 +00001957 } else if (lBreak.obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00001958 // Don't ever break in the middle of a word if we can help it.
1959 // There's no room at all. We just have to be on this line,
1960 // even though we'll spill out.
1961 lBreak.obj = o;
1962 lBreak.pos = pos;
1963 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001964 }
1965 }
1966
1967 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00001968 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00001969 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00001970
hyattfe99c872003-07-31 22:25:29 +00001971 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001972 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00001973
hyatt33f8d492002-11-12 21:44:52 +00001974 if (trailingSpaceObject) {
1975 // This object is either going to be part of the last midpoint, or it is going
1976 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
1977 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001978 if (lineMidpointState.numMidpoints % 2) {
1979 InlineIterator* midpoints = lineMidpointState.midpoints.data();
1980 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00001981 }
1982 //else if (lBreak.pos > 0)
1983 // lBreak.pos--;
1984 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00001985 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00001986 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00001987 unsigned length = text->textLength();
1988 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00001989 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001990 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00001991 }
1992 }
rjwc9c257d2003-01-24 03:46:17 +00001993
mjs54b64002003-04-02 02:59:02 +00001994 // We might have made lBreak an iterator that points past the end
1995 // of the object. Do this adjustment to make it point to the start
1996 // of the next object instead to avoid confusing the rest of the
1997 // code.
1998 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00001999 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002000 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002001 }
2002
hyatt78b85132004-03-29 20:07:45 +00002003 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2004 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2005 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002006 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00002007 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002008 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00002009 }
2010
kociendabb0c24b2001-08-24 14:24:40 +00002011 return lBreak;
2012}
2013
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002014void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002015{
hyattb4b20872004-10-20 21:34:01 +00002016 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002017 addLayoutOverflow(curr->layoutOverflowRect());
2018 if (!hasOverflowClip())
2019 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00002020 }
2021}
2022
hyatted77ad82004-06-15 07:21:23 +00002023void RenderBlock::deleteEllipsisLineBoxes()
2024{
hyatted77ad82004-06-15 07:21:23 +00002025 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002026 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002027}
2028
2029void RenderBlock::checkLinesForTextOverflow()
2030{
2031 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002032 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2033 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002034 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002035 const Font& firstLineFont = firstLineStyle()->font();
2036 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002037 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2038 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002039
2040 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2041 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2042 // check the left edge of the line box to see if it is less
2043 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2044 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002045 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002046 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2047 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.com5631f112009-02-11 01:36:45 +00002048 int lineBoxEdge = ltr ? curr->x() + curr->width() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002049 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002050 // 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 +00002051 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2052 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2053 // space.
2054 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002055 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002056 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002057 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002058 }
2059 }
2060}
2061
hyattffe78712003-02-11 01:59:29 +00002062}