blob: 6785eb24942a68c7f75544381b13128b327c3908 [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) {
562 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
563 RenderBox* box = toRenderBox(o);
564
565 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
566 o->setChildNeedsLayout(true, false);
567
568 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
569 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
570 o->setPrefWidthsDirty(true, false);
571
572 if (o->isPositioned())
573 o->containingBlock()->insertPositionedObject(box);
574 else {
575 if (o->isFloating())
576 floats.append(FloatWithRect(box));
577 else if (fullLayout || o->needsLayout()) // Replaced elements
578 toRenderBox(o)->dirtyLineBoxes(fullLayout);
579
580 o->layoutIfNeeded();
581 }
582 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
583 hasInlineChild = true;
584 if (fullLayout || o->selfNeedsLayout())
585 dirtyLineBoxesForRenderer(o, fullLayout);
586 o->setNeedsLayout(false);
587 if (!o->isText())
588 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
589 }
590 o = bidiNext(this, o, 0, false, &endOfInline);
591 }
592
593 // We want to skip ahead to the first dirty line
594 InlineBidiResolver resolver;
595 unsigned floatIndex;
596 bool firstLine = true;
597 bool previousLineBrokeCleanly = true;
598 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000599
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000600 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000601 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
602 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000603 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000604 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000605 // Because we waited until we were already inside layout to discover
606 // that the block really needed a full layout, we missed our chance to repaint the layer
607 // before layout started. Luckily the layer has cached the repaint rect for its original
608 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000609 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000610 }
611 }
hyatt0c3a9862004-02-23 21:26:26 +0000612
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000613 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
614
615 LineMidpointState& lineMidpointState = resolver.midpointState();
616
617 // We also find the first clean line and extract these lines. We will add them back
618 // if we determine that we're able to synchronize after handling all our dirty lines.
619 InlineIterator cleanLineStart;
620 BidiStatus cleanLineBidiStatus;
621 int endLineYPos = 0;
622 RootInlineBox* endLine = (fullLayout || !startLine) ?
623 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
624
625 if (startLine) {
626 useRepaintBounds = true;
627 repaintTop = height();
628 repaintBottom = height();
629 RenderArena* arena = renderArena();
630 RootInlineBox* box = startLine;
631 while (box) {
632 repaintTop = min(repaintTop, box->topVisibleOverflow());
633 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
634 RootInlineBox* next = box->nextRootBox();
635 box->deleteLine(arena);
636 box = next;
637 }
638 }
639
640 InlineIterator end = resolver.position();
641
642 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
643 // If the last line before the start line ends with a line break that clear floats,
644 // adjust the height accordingly.
645 // A line break can be either the first or the last object on a line, depending on its direction.
646 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
647 RenderObject* lastObject = lastLeafChild->renderer();
648 if (!lastObject->isBR())
649 lastObject = lastRootBox()->firstLeafChild()->renderer();
650 if (lastObject->isBR()) {
651 EClear clear = lastObject->style()->clear();
652 if (clear != CNONE)
653 newLine(clear);
654 }
655 }
656 }
657
658 bool endLineMatched = false;
659 bool checkForEndLineMatch = endLine;
660 bool checkForFloatsFromLastLine = false;
661
662 bool isLineEmpty = true;
663
664 while (!end.atEnd()) {
665 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
666 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
667 break;
668
669 lineMidpointState.reset();
670
671 isLineEmpty = true;
672
673 EClear clear = CNONE;
mitz@apple.comb2107652010-06-21 16:54:52 +0000674 bool hyphenated;
675 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, hyphenated, &clear);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000676 if (resolver.position().atEnd()) {
677 resolver.deleteRuns();
678 checkForFloatsFromLastLine = true;
679 break;
680 }
681 ASSERT(end != resolver.position());
682
683 if (!isLineEmpty) {
mitz@apple.com62d5bbd2010-06-25 18:07:01 +0000684 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000685 ASSERT(resolver.position() == end);
686
687 BidiRun* trailingSpaceRun = 0;
688 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
689 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
690 trailingSpaceRun = resolver.logicallyLastRun();
691 RenderObject* lastObject = trailingSpaceRun->m_object;
692 if (lastObject->isText()) {
693 RenderText* lastText = toRenderText(lastObject);
694 const UChar* characters = lastText->characters();
695 int firstSpace = trailingSpaceRun->stop();
696 while (firstSpace > trailingSpaceRun->start()) {
697 UChar current = characters[firstSpace - 1];
698 if (!isCollapsibleSpace(current, lastText))
699 break;
700 firstSpace--;
701 }
702 if (firstSpace == trailingSpaceRun->stop())
703 trailingSpaceRun = 0;
704 else {
705 TextDirection direction = style()->direction();
706 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
707 if (firstSpace != trailingSpaceRun->start()) {
708 BidiContext* baseContext = resolver.context();
709 while (BidiContext* parent = baseContext->parent())
710 baseContext = parent;
711
712 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
713 trailingSpaceRun->m_stop = firstSpace;
714 if (direction == LTR)
715 resolver.addRun(newTrailingRun);
716 else
717 resolver.prependRun(newTrailingRun);
718 trailingSpaceRun = newTrailingRun;
719 shouldReorder = false;
720 }
721 if (shouldReorder) {
722 if (direction == LTR) {
723 resolver.moveRunToEnd(trailingSpaceRun);
724 trailingSpaceRun->m_level = 0;
725 } else {
726 resolver.moveRunToBeginning(trailingSpaceRun);
727 trailingSpaceRun->m_level = 1;
728 }
729 }
730 }
731 } else
732 trailingSpaceRun = 0;
733 }
734
735 // Now that the runs have been ordered, we create the line boxes.
736 // At the same time we figure out where border/padding/margin should be applied for
737 // inline flow boxes.
738
739 RootInlineBox* lineBox = 0;
740 if (resolver.runCount()) {
mitz@apple.comb2107652010-06-21 16:54:52 +0000741 if (hyphenated)
742 resolver.logicallyLastRun()->m_hasHyphen = true;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000743 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
744 if (lineBox) {
745 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
746
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000747#if ENABLE(SVG)
748 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
749#else
750 bool isSVGRootInlineBox = false;
751#endif
752
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000753 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000754
755 // Now we position all of our text runs horizontally.
756 if (!isSVGRootInlineBox)
757 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000758
759 // Now position our text runs vertically.
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000760 computeVerticalPositionsForLine(lineBox, resolver.firstRun(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000761
762#if ENABLE(SVG)
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000763 // SVG text layout code computes vertical & horizontal positions on its own.
764 // Note that we still need to execute computeVerticalPositionsForLine() as
765 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
766 // contains reversed text or not. If we wouldn't do that editing and thus
767 // text selection in RTL boxes would not work as expected.
768 if (isSVGRootInlineBox) {
769 ASSERT(isSVGText());
770 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
771 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000772#endif
773
774#if PLATFORM(MAC)
775 // Highlight acts as an overflow inflation.
776 if (style()->highlight() != nullAtom)
777 lineBox->addHighlightOverflow();
778#endif
779 }
780 }
781
782 resolver.deleteRuns();
783
784 if (lineBox) {
785 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
786 if (useRepaintBounds) {
787 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
788 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
789 }
790 }
791
792 firstLine = false;
793 newLine(clear);
794 }
795
796 if (m_floatingObjects && lastRootBox()) {
797 if (lastFloat) {
798 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
799 }
800 m_floatingObjects->next();
801 } else
802 m_floatingObjects->first();
803 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
804 lastRootBox()->floats().append(f->m_renderer);
805 ASSERT(f->m_renderer == floats[floatIndex].object);
806 // If a float's geometry has changed, give up on syncing with clean lines.
807 if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
808 checkForEndLineMatch = false;
809 floatIndex++;
810 }
811 lastFloat = m_floatingObjects->last();
812 }
813
814 lineMidpointState.reset();
815 resolver.setPosition(end);
816 }
817
818 if (endLine) {
819 if (endLineMatched) {
820 // Attach all the remaining lines, and then adjust their y-positions as needed.
821 int delta = height() - endLineYPos;
822 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
823 line->attachLine();
824 if (delta) {
825 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
826 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
827 line->adjustPosition(0, delta);
828 }
829 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
830 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
831 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
832 int floatTop = (*f)->y() - (*f)->marginTop();
833 insertFloatingObject(*f);
834 setHeight(floatTop + delta);
835 positionNewFloats();
836 }
837 }
838 }
839 setHeight(lastRootBox()->blockHeight());
840 } else {
841 // Delete all the remaining lines.
842 RootInlineBox* line = endLine;
843 RenderArena* arena = renderArena();
844 while (line) {
845 repaintTop = min(repaintTop, line->topVisibleOverflow());
846 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
847 RootInlineBox* next = line->nextRootBox();
848 line->deleteLine(arena);
849 line = next;
850 }
851 }
852 }
853 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
854 // In case we have a float on the last line, it might not be positioned up to now.
855 // This has to be done before adding in the bottom border/padding, or the float will
856 // include the padding incorrectly. -dwh
857 if (checkForFloatsFromLastLine) {
858 int bottomVisualOverflow = lastRootBox()->bottomVisualOverflow();
859 int bottomLayoutOverflow = lastRootBox()->bottomLayoutOverflow();
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000860 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000861 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
862 trailingFloatsLineBox->setConstructed();
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000863 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
864 trailingFloatsLineBox->verticallyAlignBoxes(height(), textBoxDataMap);
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000865 trailingFloatsLineBox->setVerticalOverflowPositions(height(), bottomLayoutOverflow, height(), bottomVisualOverflow, 0);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000866 trailingFloatsLineBox->setBlockHeight(height());
867 }
868 if (lastFloat) {
869 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
870 }
871 m_floatingObjects->next();
872 } else
873 m_floatingObjects->first();
874 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
875 lastRootBox()->floats().append(f->m_renderer);
876 lastFloat = m_floatingObjects->last();
877 }
878 size_t floatCount = floats.size();
879 // Floats that did not have layout did not repaint when we laid them out. They would have
880 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
881 // painted.
882 for (size_t i = 0; i < floatCount; ++i) {
883 if (!floats[i].everHadLayout) {
884 RenderBox* f = floats[i].object;
885 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
886 f->repaint();
887 }
888 }
kociendabb0c24b2001-08-24 14:24:40 +0000889 }
hyatt85586af2003-02-19 23:22:42 +0000890
hyatta70560a2002-11-20 01:53:20 +0000891 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000892 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +0000893
adele7a470a72006-04-20 22:22:14 +0000894 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000895 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +0000896
897 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
898 // truncate text.
899 if (hasTextOverflow)
900 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +0000901}
902
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000903RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
904 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +0000905{
906 RootInlineBox* curr = 0;
907 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000908
mitz@apple.com40547b32008-03-18 04:04:34 +0000909 bool dirtiedByFloat = false;
910 if (!fullLayout) {
911 size_t floatIndex = 0;
912 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000913 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
914 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
915 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
916 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +0000917 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
918 ASSERT(floatIndex < floats.size());
919 if (floats[floatIndex].object != f) {
920 // A new float has been inserted before this line or before its last known float.
921 // Just do a full layout.
922 fullLayout = true;
923 break;
924 }
925 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +0000926 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +0000927 curr->markDirty();
mitz@apple.com1d4e9532010-01-18 20:55:25 +0000928 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()), curr);
mitz@apple.com40547b32008-03-18 04:04:34 +0000929 floats[floatIndex].rect.setSize(newSize);
930 dirtiedByFloat = true;
931 }
932 floatIndex++;
933 }
934 }
935 if (dirtiedByFloat || fullLayout)
936 break;
937 }
938 // Check if a new float has been inserted after the last known float.
939 if (!curr && floatIndex < floats.size())
940 fullLayout = true;
941 }
942
hyatt0c3a9862004-02-23 21:26:26 +0000943 if (fullLayout) {
944 // Nuke all our lines.
945 if (firstRootBox()) {
946 RenderArena* arena = renderArena();
947 curr = firstRootBox();
948 while (curr) {
949 RootInlineBox* next = curr->nextRootBox();
950 curr->deleteLine(arena);
951 curr = next;
952 }
darinec375482007-01-06 01:36:24 +0000953 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +0000954 }
eseidel789896f2005-11-27 22:52:09 +0000955 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000956 if (curr) {
957 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +0000958 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +0000959 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +0000960 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +0000961 // The previous line didn't break cleanly or broke at a newline
962 // that has been deleted, so treat it as dirty too.
963 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +0000964 }
eseidel789896f2005-11-27 22:52:09 +0000965 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000966 // No dirty lines were found.
967 // If the last line didn't break cleanly, treat it as dirty.
968 if (lastRootBox() && !lastRootBox()->endsWithBreak())
969 curr = lastRootBox();
970 }
mitz@apple.come1364202008-02-28 01:06:41 +0000971
hyatt0c3a9862004-02-23 21:26:26 +0000972 // If we have no dirty lines, then last is just the last root box.
973 last = curr ? curr->prevRootBox() : lastRootBox();
974 }
mitz@apple.come1364202008-02-28 01:06:41 +0000975
mitz@apple.com40547b32008-03-18 04:04:34 +0000976 numCleanFloats = 0;
977 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000978 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000979 // Restore floats from clean lines.
980 RootInlineBox* line = firstRootBox();
981 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000982 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
983 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
984 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +0000985 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000986 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +0000987 positionNewFloats();
988 ASSERT(floats[numCleanFloats].object == *f);
989 numCleanFloats++;
990 }
991 }
992 line = line->nextRootBox();
993 }
hyatt@apple.comd885df72009-01-22 02:31:52 +0000994 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +0000995 }
996
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000997 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +0000998 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +0000999
1000 RenderObject* startObj;
1001 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001002 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001003 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001004 startObj = last->lineBreakObj();
1005 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001006 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001007 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001008 bool ltr = style()->direction() == LTR
1009 #if ENABLE(SVG)
1010 || (style()->unicodeBidi() == UBNormal && isSVGText())
1011 #endif
1012 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001013
mitz@apple.com45d9e102009-04-27 16:24:55 +00001014 Direction direction = ltr ? LeftToRight : RightToLeft;
1015 resolver.setLastStrongDir(direction);
1016 resolver.setLastDir(direction);
1017 resolver.setEorDir(direction);
1018 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001019
mitz@apple.com15035e62008-07-05 20:44:44 +00001020 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001021 }
mitz@apple.come1364202008-02-28 01:06:41 +00001022
mitz@apple.com15035e62008-07-05 20:44:44 +00001023 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001024
hyatt0c3a9862004-02-23 21:26:26 +00001025 return curr;
1026}
1027
mitz@apple.com15035e62008-07-05 20:44:44 +00001028RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001029{
1030 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001031 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001032 last = 0;
1033 else {
hyatt04420ca2004-07-16 00:05:42 +00001034 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1035 if (curr->isDirty())
1036 last = 0;
1037 else if (!last)
1038 last = curr;
1039 }
hyatt0c3a9862004-02-23 21:26:26 +00001040 }
mitz@apple.come1364202008-02-28 01:06:41 +00001041
hyatt0c3a9862004-02-23 21:26:26 +00001042 if (!last)
1043 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001044
eseidel789896f2005-11-27 22:52:09 +00001045 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001046 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001047 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001048 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001049
hyatt0c3a9862004-02-23 21:26:26 +00001050 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1051 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1052 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001053
hyatt0c3a9862004-02-23 21:26:26 +00001054 return last;
1055}
1056
mitz@apple.com15035e62008-07-05 20:44:44 +00001057bool 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 +00001058{
mitz@apple.com15035e62008-07-05 20:44:44 +00001059 if (resolver.position() == endLineStart) {
1060 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001061 return false;
1062
hyatt@apple.comd885df72009-01-22 02:31:52 +00001063 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001064 if (!delta || !m_floatingObjects)
1065 return true;
1066
1067 // 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 +00001068 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001069
1070 RootInlineBox* lastLine = endLine;
1071 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1072 lastLine = nextLine;
1073
1074 int bottom = lastLine->blockHeight() + abs(delta);
1075
1076 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001077 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001078 return false;
1079 }
1080
1081 return true;
1082 }
hyatt0c3a9862004-02-23 21:26:26 +00001083
mitz@apple.come1364202008-02-28 01:06:41 +00001084 // The first clean line doesn't match, but we can check a handful of following lines to try
1085 // to match back up.
1086 static int numLines = 8; // The # of lines we're willing to match against.
1087 RootInlineBox* line = endLine;
1088 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001089 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001090 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001091 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001092 return false; // ...but the bidi state doesn't match.
1093 RootInlineBox* result = line->nextRootBox();
1094
1095 // Set our yPos to be the block height of endLine.
1096 if (result)
1097 endYPos = line->blockHeight();
1098
hyatt@apple.comd885df72009-01-22 02:31:52 +00001099 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001100 if (delta && m_floatingObjects) {
1101 // 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 +00001102 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001103
1104 RootInlineBox* lastLine = endLine;
1105 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1106 lastLine = nextLine;
1107
1108 int bottom = lastLine->blockHeight() + abs(delta);
1109
1110 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001111 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001112 return false;
1113 }
1114 }
1115
mitz@apple.come1364202008-02-28 01:06:41 +00001116 // Now delete the lines that we failed to sync.
1117 RootInlineBox* boxToDelete = endLine;
1118 RenderArena* arena = renderArena();
1119 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001120 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1121 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001122 RootInlineBox* next = boxToDelete->nextRootBox();
1123 boxToDelete->deleteLine(arena);
1124 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001125 }
mitz@apple.come1364202008-02-28 01:06:41 +00001126
1127 endLine = result;
1128 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001129 }
1130 }
mitz@apple.come1364202008-02-28 01:06:41 +00001131
hyatt0c3a9862004-02-23 21:26:26 +00001132 return false;
1133}
1134
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001135static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001136{
darinf9e5d6c2007-01-09 14:54:26 +00001137 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001138 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001139
hyattdca76e92005-11-02 08:52:50 +00001140 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1141 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001142 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001143 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1144 // |true|).
1145 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001146 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001147
kocienda498d1982004-10-15 21:07:24 +00001148 return true;
kocienda98440082004-10-14 23:51:47 +00001149}
1150
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001151static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001152{
1153 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1154}
1155
zimmermannac3781f2007-02-04 01:25:03 +00001156static inline bool shouldPreserveNewline(RenderObject* object)
1157{
mjsd2948ef2007-02-26 19:29:04 +00001158#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001159 if (object->isSVGInlineText())
zimmermannac3781f2007-02-04 01:25:03 +00001160 return false;
1161#endif
1162
1163 return object->style()->preserveNewline();
1164}
1165
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001166static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001167{
1168 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001169 // We need to fix this, though, because at the very least, inlines containing only
1170 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001171 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001172}
1173
hyatt@apple.com71eeb442010-02-11 20:05:51 +00001174bool RenderBlock::requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001175{
bdakinf876bee2007-10-30 05:27:09 +00001176 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001177 return false;
bdakinf876bee2007-10-30 05:27:09 +00001178
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001179 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001180 return false;
1181
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001182 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001183 return true;
1184
1185 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001186 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1187 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001188}
1189
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001190bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001191{
1192 ASSERT(inlineObj->parent() == this);
1193
mitz@apple.com15035e62008-07-05 20:44:44 +00001194 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001195 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001196 it.increment();
bdashccffb432007-07-13 11:51:40 +00001197
1198 return !it.atEnd();
1199}
1200
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001201// 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 +00001202// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1203// elements quite right. In other words, we need to build this function's work into the normal line
1204// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001205// NB. this function will insert any floating elements that would otherwise
1206// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001207void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001208{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001209 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001210 RenderObject* object = iterator.obj;
1211 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001212 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001213 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001214 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1215 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001216 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001217 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001218 // A relative positioned inline encloses us. In this case, we also have to determine our
1219 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1220 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001221 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1222 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001223 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001224
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001225 RenderBox* box = toRenderBox(object);
1226 if (box->style()->hasStaticX()) {
1227 if (box->style()->isOriginalDisplayInlineType())
1228 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001229 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001230 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001231 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001232
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001233 if (box->style()->hasStaticY())
1234 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001235 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001236 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001237 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001238}
bdashccffb432007-07-13 11:51:40 +00001239
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001240int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly)
mitz@apple.com1a301772008-03-11 18:30:36 +00001241{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001242 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001243 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001244 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001245 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001246 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001247 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001248 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001249 } else if (object->isPositioned()) {
1250 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1251 // will work for the common cases
1252 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001253 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001254 // A relative positioned inline encloses us. In this case, we also have to determine our
1255 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1256 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001257 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1258 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001259 }
1260
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001261 RenderBox* box = toRenderBox(object);
1262 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001263 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001264 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001265 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001266 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001267 }
1268
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001269 if (box->style()->hasStaticY())
1270 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001271 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001272 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001273 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001274 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001275 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001276}
1277
bdakinf876bee2007-10-30 05:27:09 +00001278// This is currently just used for list markers and inline flows that have line boxes. Neither should
1279// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001280static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001281{
mitz@apple.com1a301772008-03-11 18:30:36 +00001282 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001283 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1284 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001285 UChar nextChar = nextText->characters()[0];
1286 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001287 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001288 return true;
1289 }
1290 }
1291
1292 return false;
1293}
1294
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001295void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001296{
1297 ASSERT(widthToFit > availableWidth);
1298
1299 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001300 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001301 int newLineWidth = availableWidth;
1302 while (true) {
1303 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1304 if (!floatBottom)
1305 break;
1306
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001307 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001308 lastFloatBottom = floatBottom;
1309 if (newLineWidth >= widthToFit)
1310 break;
1311 }
1312
1313 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001314 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001315 availableWidth = newLineWidth;
1316 }
1317}
1318
mitz@apple.com34106442009-02-01 06:23:39 +00001319static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1320{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001321 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001322 return text->width(from, len, font, xPos);
1323 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1324}
1325
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001326static 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 +00001327{
1328 const AtomicString& hyphenString = text->style()->hyphenString();
1329 int hyphenWidth = font.width(TextRun(hyphenString.characters(), hyphenString.length()));
1330
mitz@apple.com7c67b292010-09-12 23:04:16 +00001331 int maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
1332 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1333 // that an hyphenation opportunity exists, so do not bother to look for it.
1334 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1335 return;
1336
1337 unsigned prefixLength = font.offsetForPosition(TextRun(text->characters() + lastSpace, pos - lastSpace, !collapseWhiteSpace, xPos + lastSpaceWordSpacing), maxPrefixWidth, false);
mitz@apple.comb2107652010-06-21 16:54:52 +00001338 if (!prefixLength)
1339 return;
1340
mitz@apple.com2ef44912010-08-07 21:41:06 +00001341 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, prefixLength + 1, localeIdentifier);
1342 if (!prefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00001343 return;
1344
1345#if !ASSERT_DISABLED
1346 int prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1347 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001348#else
1349 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001350#endif
1351
1352 lineBreak.obj = text;
1353 lineBreak.pos = lastSpace + prefixLength;
1354 lineBreak.nextBreakablePosition = nextBreakable;
1355 hyphenated = true;
1356}
1357
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001358InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
mitz@apple.comb2107652010-06-21 16:54:52 +00001359 bool& hyphenated, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001360{
mitz@apple.com15035e62008-07-05 20:44:44 +00001361 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001362
mitz@apple.com15035e62008-07-05 20:44:44 +00001363 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001364 LineMidpointState& lineMidpointState = resolver.midpointState();
1365
1366 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly);
mitz@apple.com1a301772008-03-11 18:30:36 +00001367
kociendae40cb942004-10-05 20:05:38 +00001368 int w = 0;
1369 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001370
mitz@apple.com15035e62008-07-05 20:44:44 +00001371 if (resolver.position().atEnd())
1372 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001373
hyatt33f8d492002-11-12 21:44:52 +00001374 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1375 // or not we are currently ignoring whitespace.
1376 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001377 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001378
1379 // This variable tracks whether the very last character we saw was a space. We use
1380 // this to detect when we encounter a second space so we know we have to terminate
1381 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001382 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001383 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001384 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001385
mitz@apple.com15035e62008-07-05 20:44:44 +00001386 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001387
mitz@apple.com15035e62008-07-05 20:44:44 +00001388 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001389 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001390 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001391 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001392 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001393
hyatt0c3a9862004-02-23 21:26:26 +00001394 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1395 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001396
mitz@apple.comb2107652010-06-21 16:54:52 +00001397 hyphenated = false;
1398
ddkilzer5d01fa22007-01-29 03:10:37 +00001399 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001400 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001401
hyatt@apple.com69340902008-01-16 21:24:21 +00001402 // 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 +00001403 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001404 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +00001405 bool allowImagesToBreak = !document()->inQuirksMode() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001406
hyattb0d9f602007-01-15 01:28:23 +00001407 EWhiteSpace currWS = style()->whiteSpace();
1408 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001409 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001410 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1411 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1412
1413 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001414 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001415
mjsd2948ef2007-02-26 19:29:04 +00001416#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001417 bool preserveNewline = o->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001418#else
hyattb0d9f602007-01-15 01:28:23 +00001419 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001420#endif
1421
hyattb0d9f602007-01-15 01:28:23 +00001422 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1423
hyatt275d0702005-11-03 23:53:57 +00001424 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001425 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001426 lBreak.obj = o;
1427 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001428 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001429 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001430
hyatt33f8d492002-11-12 21:44:52 +00001431 // A <br> always breaks a line, so don't let the line be collapsed
1432 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001433 // get collapsed away. It only does this if the previous line broke
1434 // cleanly. Otherwise the <br> has no effect on whether the line is
1435 // empty or not.
1436 if (prevLineBrokeCleanly)
1437 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001438 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001439 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001440
mitz@apple.com71e30842008-03-18 16:13:31 +00001441 if (!isLineEmpty && clear)
1442 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001443 }
1444 goto end;
1445 }
hyattb0d9f602007-01-15 01:28:23 +00001446
hyatt275d0702005-11-03 23:53:57 +00001447 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001448 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001449 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001450 RenderBox* floatBox = toRenderBox(o);
1451 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001452 // check if it fits in the current line.
1453 // If it does, position it now, otherwise, position
1454 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001455 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001456 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001457 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001458 } else
1459 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001460 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001461 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001462 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001463 RenderBox* box = toRenderBox(o);
1464 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1465 bool needToSetStaticX = box->style()->hasStaticX();
1466 if (box->style()->hasStaticX() && !isInlineType) {
1467 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001468 borderLeft() + paddingLeft() :
1469 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001470 needToSetStaticX = false;
1471 }
1472
1473 // If our original display was an INLINE type, then we can go ahead
1474 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001475 bool needToSetStaticY = box->style()->hasStaticY();
1476 if (box->style()->hasStaticY() && isInlineType) {
1477 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001478 needToSetStaticY = false;
1479 }
1480
hyatt853cd7d2004-11-05 02:59:48 +00001481 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1482 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001483 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001484 needToCreateLineBox = true;
1485
hyatt98ee7e42003-05-14 01:39:15 +00001486 // If we're ignoring spaces, we have to stop and include this object and
1487 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001488 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001489 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001490 ignoreStart.obj = o;
1491 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001492 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001493 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1494 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001495 }
hyatt98b16282004-03-31 18:43:12 +00001496
hyatt851816b2003-07-08 07:54:17 +00001497 }
hyatt98ee7e42003-05-14 01:39:15 +00001498 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001499 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001500 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001501 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001502
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001503 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001504
bdakinf876bee2007-10-30 05:27:09 +00001505 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1506 // to make sure that we stop to include this object and then start ignoring spaces again.
1507 // If this object is at the start of the line, we need to behave like list markers and
1508 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001509 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001510 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001511 if (ignoringSpaces) {
1512 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001513 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1514 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001515 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001516 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001517 // Like with list markers, we start ignoring spaces to make sure that any
1518 // additional spaces we see will be discarded.
1519 currentCharacterIsSpace = true;
1520 currentCharacterIsWS = true;
1521 ignoringSpaces = true;
1522 }
1523 }
1524
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001525 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1526 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001527 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001528 RenderBox* replacedBox = toRenderBox(o);
1529
hyattde396342003-10-29 08:57:20 +00001530 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001531 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001532 w += tmpW;
1533 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001534 lBreak.obj = o;
1535 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001536 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001537 }
1538
mitz@apple.combfdc9112008-02-21 19:59:40 +00001539 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001540 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001541
hyatt33f8d492002-11-12 21:44:52 +00001542 isLineEmpty = false;
1543 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001544 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001545 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001546 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001547
bdakinf876bee2007-10-30 05:27:09 +00001548 // Optimize for a common case. If we can't find whitespace after the list
1549 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001550 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001551 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001552 // Like with inline flows, we start ignoring spaces to make sure that any
1553 // additional spaces we see will be discarded.
1554 currentCharacterIsSpace = true;
1555 currentCharacterIsWS = true;
1556 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001557 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001558 if (toRenderListMarker(o)->isInside())
1559 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001560 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001561 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001562 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001563 if (!pos)
1564 appliedStartWidth = false;
1565
darin@apple.com36744d62009-01-25 20:23:04 +00001566 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001567
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001568#if ENABLE(SVG)
1569 bool isSVGText = t->isSVGInlineText();
1570#endif
1571
darin42563ac52007-01-22 17:28:57 +00001572 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001573 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001574 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001575
mitz@apple.comb2107652010-06-21 16:54:52 +00001576 RenderStyle* style = t->style(firstLine);
1577 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001578 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001579 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->hyphenationLocale());
weinigf28a1c32007-02-14 14:10:31 +00001580
hyatt33f8d492002-11-12 21:44:52 +00001581 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001582 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001583 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001584
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001585 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1586 // space, then subtract its width.
mitz@apple.com6ff116d2010-07-06 00:11:08 +00001587 int wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(TextRun(&space, 1)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001588
darin54008922006-01-13 16:39:05 +00001589 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001590 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001591 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1592 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1593 // 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 +00001594 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001595 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001596 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1597
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001598 if (t->isWordBreak()) {
1599 w += tmpW;
1600 tmpW = 0;
1601 lBreak.obj = o;
1602 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001603 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001604 ASSERT(!len);
1605 }
1606
hyatt275d0702005-11-03 23:53:57 +00001607 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001608 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001609 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001610 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001611 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001612
hyattb0d9f602007-01-15 01:28:23 +00001613 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001614 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001615
hyatt78b85132004-03-29 20:07:45 +00001616 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001617 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001618 if (!ignoringSpaces) {
1619 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001620 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001621 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001622 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001623 else
darin@apple.com36744d62009-01-25 20:23:04 +00001624 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001625 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001626 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1627 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1628 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001629 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001630 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001631
hyatt78b85132004-03-29 20:07:45 +00001632 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001633 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001634
hyattdca76e92005-11-02 08:52:50 +00001635 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001636 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001637 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001638 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001639
mitz@apple.com15035e62008-07-05 20:44:44 +00001640 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001641 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001642 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001643 }
mitz@apple.combe429562008-03-07 01:09:51 +00001644
hyatt78b85132004-03-29 20:07:45 +00001645 pos++;
1646 len--;
eseideld13fe532005-11-30 02:40:29 +00001647 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001648 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
mitz@apple.comb2107652010-06-21 16:54:52 +00001649 if (style->hyphens() == HyphensNone) {
1650 // Prevent a line break at the soft hyphen by ensuring that betweenWords is false
1651 // in the next iteration.
1652 atStart = true;
1653 }
hyatt78b85132004-03-29 20:07:45 +00001654 continue;
1655 }
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001656
1657#if ENABLE(SVG)
1658 if (isSVGText) {
1659 RenderSVGInlineText* svgInlineText = static_cast<RenderSVGInlineText*>(t);
1660 if (pos > 0) {
1661 if (svgInlineText->characterStartsNewTextChunk(pos)) {
1662 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1));
1663 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
1664 }
1665 }
1666 }
1667#endif
1668
hyatt3aff2332003-01-23 01:15:28 +00001669 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001670
darinf9e5d6c2007-01-09 14:54:26 +00001671 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001672
weiniged111c12007-07-13 22:45:11 +00001673 if ((breakAll || breakWords) && !midWordBreak) {
1674 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001675 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001676 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001677 }
darin47ece0d2005-09-04 07:42:31 +00001678
ddkilzere8759ef2007-03-25 06:28:19 +00001679 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001680
weiniged111c12007-07-13 22:45:11 +00001681 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001682 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001683 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001684 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001685 // Stop ignoring spaces and begin at this
1686 // new point.
hyatt48710d62003-08-21 09:17:13 +00001687 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001688 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001689 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001690 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001691 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001692 } else {
hyatt33f8d492002-11-12 21:44:52 +00001693 // Just keep ignoring these spaces.
1694 pos++;
1695 len--;
1696 continue;
1697 }
1698 }
rjwc9c257d2003-01-24 03:46:17 +00001699
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001700 int additionalTmpW;
1701 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1702 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1703 else
1704 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001705 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001706 if (!appliedStartWidth) {
1707 tmpW += inlineWidth(o, true, false);
1708 appliedStartWidth = true;
1709 }
1710
eseideld13fe532005-11-30 02:40:29 +00001711 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001712
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001713 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001714 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001715
hyattb0d9f602007-01-15 01:28:23 +00001716 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001717 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001718 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001719 bool lineWasTooWide = false;
1720 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001721 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001722 // Check if line is too big even without the extra space
1723 // at the end of the line. If it is not, do nothing.
1724 // If the line needs the extra whitespace to be too long,
1725 // then move the line break to the space and skip all
1726 // additional whitespace.
1727 if (w + tmpW + charWidth > width) {
1728 lineWasTooWide = true;
1729 lBreak.obj = o;
1730 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001731 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001732 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001733 }
ap932806a2006-10-01 09:06:09 +00001734 }
1735 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001736 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001737 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 +00001738 if (hyphenated)
1739 goto end;
1740 }
eric@webkit.org1cb44402009-12-29 06:25:16 +00001741 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 +00001742 if (!stoppedIgnoringSpaces && pos > 0) {
1743 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001744 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1745 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001746 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001747 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001748 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001749 }
hyatt78b85132004-03-29 20:07:45 +00001750 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001751 } else {
weiniged111c12007-07-13 22:45:11 +00001752 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001753 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001754 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001755 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001756 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001757 }
rjwc9c257d2003-01-24 03:46:17 +00001758 }
hyatt33f8d492002-11-12 21:44:52 +00001759
hyattb0d9f602007-01-15 01:28:23 +00001760 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001761 if (!stoppedIgnoringSpaces && pos > 0) {
1762 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001763 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1764 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001765 }
hyatta9f48e32003-02-03 22:48:01 +00001766 lBreak.obj = o;
1767 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001768 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001769 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001770 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001771 return lBreak;
1772 }
hyatta9f48e32003-02-03 22:48:01 +00001773
weinigf28a1c32007-02-14 14:10:31 +00001774 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001775 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001776 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001777 tmpW = 0;
1778 lBreak.obj = o;
1779 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001780 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001781 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1782 // opportunity to break after a word.
1783 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001784 }
hyatt33f8d492002-11-12 21:44:52 +00001785
darin54008922006-01-13 16:39:05 +00001786 if (midWordBreak) {
1787 // Remember this as a breakable position in case
1788 // adding the end width forces a break.
1789 lBreak.obj = o;
1790 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001791 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001792 midWordBreak &= (breakWords || breakAll);
1793 }
1794
1795 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001796 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1797 lastSpace = pos;
1798 }
hyatt33f8d492002-11-12 21:44:52 +00001799
hyattdca76e92005-11-02 08:52:50 +00001800 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001801 // If we encounter a newline, or if we encounter a
1802 // second space, we need to go ahead and break up this
1803 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001804 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001805 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001806
hyatt33f8d492002-11-12 21:44:52 +00001807 // We just entered a mode where we are ignoring
1808 // spaces. Create a midpoint to terminate the run
1809 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001810 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001811 }
1812 }
eseidel789896f2005-11-27 22:52:09 +00001813 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001814 // Stop ignoring spaces and begin at this
1815 // new point.
1816 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001817 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001818 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001819 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001820 }
hyatt98b16282004-03-31 18:43:12 +00001821
1822 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1823 ignoreStart.obj = o;
1824 ignoreStart.pos = pos;
1825 }
harrisone343c412005-01-18 01:07:26 +00001826
1827 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001828 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001829 lBreak.obj = o;
1830 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001831 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00001832 }
1833 }
hyatt33f8d492002-11-12 21:44:52 +00001834
hyattb0d9f602007-01-15 01:28:23 +00001835 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001836 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001837 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001838 trailingSpaceObject = 0;
1839
1840 pos++;
1841 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001842 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001843 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001844
kociendabb0c24b2001-08-24 14:24:40 +00001845 // IMPORTANT: pos is > length here!
mitz@apple.comb2107652010-06-21 16:54:52 +00001846 int additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1847 tmpW += additionalTmpW;
weinigf28a1c32007-02-14 14:10:31 +00001848 tmpW += inlineWidth(o, !appliedStartWidth, true);
mitz@apple.comb2107652010-06-21 16:54:52 +00001849
1850 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001851 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 +00001852 if (hyphenated)
1853 goto end;
1854 }
kociendabb0c24b2001-08-24 14:24:40 +00001855 } else
weinigf28a1c32007-02-14 14:10:31 +00001856 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001857
mitz@apple.com1a301772008-03-11 18:30:36 +00001858 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001859 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001860 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00001861 checkForBreak = true;
1862 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00001863 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00001864 if (currentCharacterIsSpace)
1865 checkForBreak = true;
1866 else {
harrison07b5e582005-08-15 23:31:16 +00001867 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00001868 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001869 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00001870 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00001871 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00001872 // If the next item on the line is text, and if we did not end with
1873 // a space, then the next text run continues our word (and so it needs to
1874 // keep adding to |tmpW|. Just update and continue.
1875 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001876 } else if (nextText->isWordBreak())
1877 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001878 bool willFitOnLine = w + tmpW <= width;
1879 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001880 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001881 willFitOnLine = tmpW <= width;
1882 }
ddkilzere8759ef2007-03-25 06:28:19 +00001883 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00001884 if (canPlaceOnLine && checkForBreak) {
1885 w += tmpW;
1886 tmpW = 0;
1887 lBreak.obj = next;
1888 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001889 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00001890 }
1891 }
1892 }
1893 }
1894
darin54008922006-01-13 16:39:05 +00001895 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00001896 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00001897 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00001898 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001899
1900 if (w)
1901 goto end;
1902
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001903 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00001904
hyatta14d1742003-01-02 20:25:46 +00001905 // |width| may have been adjusted because we got shoved down past a float (thus
1906 // giving us more room), so we need to retest, and only jump to
1907 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00001908 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00001909 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00001910 }
hyatt1d9e29b2003-04-10 01:48:53 +00001911
mitz@apple.com1a301772008-03-11 18:30:36 +00001912 if (!o->isFloatingOrPositioned()) {
1913 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00001914 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001915 w += tmpW;
1916 tmpW = 0;
1917 lBreak.obj = next;
1918 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001919 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001920 }
hyatt711fe232002-11-20 21:25:14 +00001921 }
1922
mitz@apple.com1a301772008-03-11 18:30:36 +00001923 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001924 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001925
hyatta9f48e32003-02-03 22:48:01 +00001926 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
1927 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00001928 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00001929 currentCharacterIsSpace = false;
1930
kociendabb0c24b2001-08-24 14:24:40 +00001931 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00001932 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00001933 }
1934
hyattb0d9f602007-01-15 01:28:23 +00001935
1936 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00001937 lBreak.obj = 0;
1938 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001939 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001940 }
1941
1942 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00001943 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00001944 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00001945 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00001946 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00001947 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00001948 lBreak.obj = o;
1949 lBreak.pos = pos - 1;
1950 } else {
1951 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00001952 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001953 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001954 }
hyatt275d0702005-11-03 23:53:57 +00001955 } else if (lBreak.obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00001956 // Don't ever break in the middle of a word if we can help it.
1957 // There's no room at all. We just have to be on this line,
1958 // even though we'll spill out.
1959 lBreak.obj = o;
1960 lBreak.pos = pos;
1961 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001962 }
1963 }
1964
1965 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00001966 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00001967 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00001968
hyattfe99c872003-07-31 22:25:29 +00001969 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001970 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00001971
hyatt33f8d492002-11-12 21:44:52 +00001972 if (trailingSpaceObject) {
1973 // This object is either going to be part of the last midpoint, or it is going
1974 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
1975 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001976 if (lineMidpointState.numMidpoints % 2) {
1977 InlineIterator* midpoints = lineMidpointState.midpoints.data();
1978 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00001979 }
1980 //else if (lBreak.pos > 0)
1981 // lBreak.pos--;
1982 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00001983 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00001984 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00001985 unsigned length = text->textLength();
1986 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00001987 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001988 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00001989 }
1990 }
rjwc9c257d2003-01-24 03:46:17 +00001991
mjs54b64002003-04-02 02:59:02 +00001992 // We might have made lBreak an iterator that points past the end
1993 // of the object. Do this adjustment to make it point to the start
1994 // of the next object instead to avoid confusing the rest of the
1995 // code.
1996 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00001997 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00001998 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00001999 }
2000
hyatt78b85132004-03-29 20:07:45 +00002001 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2002 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2003 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002004 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00002005 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002006 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00002007 }
2008
kociendabb0c24b2001-08-24 14:24:40 +00002009 return lBreak;
2010}
2011
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002012void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002013{
hyattb4b20872004-10-20 21:34:01 +00002014 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002015 addLayoutOverflow(curr->layoutOverflowRect());
2016 if (!hasOverflowClip())
2017 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00002018 }
2019}
2020
hyatted77ad82004-06-15 07:21:23 +00002021void RenderBlock::deleteEllipsisLineBoxes()
2022{
hyatted77ad82004-06-15 07:21:23 +00002023 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002024 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002025}
2026
2027void RenderBlock::checkLinesForTextOverflow()
2028{
2029 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002030 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2031 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002032 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002033 const Font& firstLineFont = firstLineStyle()->font();
2034 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002035 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2036 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002037
2038 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2039 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2040 // check the left edge of the line box to see if it is less
2041 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2042 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002043 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002044 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2045 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.com5631f112009-02-11 01:36:45 +00002046 int lineBoxEdge = ltr ? curr->x() + curr->width() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002047 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002048 // 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 +00002049 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2050 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2051 // space.
2052 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002053 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002054 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002055 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002056 }
2057 }
2058}
2059
hyattffe78712003-02-11 01:59:29 +00002060}