blob: c2d21146254c2606322d61ea1473bf2b5a5f69e4 [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)
44#include "SVGRootInlineBox.h"
45#endif
46
darin7bd70952006-04-13 07:07:34 +000047using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000048using namespace WTF;
49using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000050
darinb9481ed2006-03-20 02:57:59 +000051namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000052
hyatt1d5d87b2007-04-24 04:55:54 +000053// We don't let our line box tree for a single line get any deeper than this.
54const unsigned cMaxLineDepth = 200;
55
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000056static int getBorderPaddingMargin(RenderBoxModelObject* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000057{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000058 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
59 if (leftSide)
60 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
61 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000062}
63
64static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
65{
hyatt1d5d87b2007-04-24 04:55:54 +000066 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000067 int extraWidth = 0;
68 RenderObject* parent = child->parent();
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000069 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyatt@apple.com40232f82009-02-04 04:26:08 +000070 if (start && !child->previousSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000071 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), false);
hyatt@apple.com40232f82009-02-04 04:26:08 +000072 if (end && !child->nextSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000073 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), true);
hyattffe78712003-02-11 01:59:29 +000074 child = parent;
75 parent = child->parent();
76 }
77 return extraWidth;
78}
79
hyatt@apple.comb3466af2009-06-13 06:04:40 +000080static void chopMidpointsAt(LineMidpointState& lineMidpointState, RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +000081{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000082 if (!lineMidpointState.numMidpoints)
hyatt275d0702005-11-03 23:53:57 +000083 return;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000084 InlineIterator* midpoints = lineMidpointState.midpoints.data();
85 for (int i = lineMidpointState.numMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +000086 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +000087 if (point.obj == obj && point.pos == pos) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000088 lineMidpointState.numMidpoints = i;
hyatt78b85132004-03-29 20:07:45 +000089 break;
90 }
91 }
92}
93
hyatt@apple.comb3466af2009-06-13 06:04:40 +000094static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +000095{
96 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +000097 // shave it off the list, and shave off a trailing space if the previous end point doesn't
98 // preserve whitespace.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000099 if (lBreak.obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
100 InlineIterator* midpoints = lineMidpointState.midpoints.data();
101 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
102 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000103 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000104 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000105 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000106 if (currpoint == lBreak) {
107 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000108 lineMidpointState.numMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000109 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000110 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000111 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000112 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000113 if (endpoint.pos + 1 < textObj->textLength()) {
114 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000115 return;
116 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000117 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000118 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000119 return;
120 }
hyattee1bcae2004-03-29 21:36:04 +0000121 }
hyattfe99c872003-07-31 22:25:29 +0000122 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000123 }
hyattfe99c872003-07-31 22:25:29 +0000124 }
125 }
126}
127
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000128static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000129{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000130 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
131 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000132
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000133 InlineIterator* midpoints = lineMidpointState.midpoints.data();
134 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000135}
136
hyatt@apple.com71eeb442010-02-11 20:05:51 +0000137void RenderBlock::appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000138{
hyatt98ee7e42003-05-14 01:39:15 +0000139 if (start > end || obj->isFloating() ||
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000140 (obj->isPositioned() && !obj->style()->hasStaticX() && !obj->style()->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000141 return;
hyatt85586af2003-02-19 23:22:42 +0000142
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000143 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000144 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000145 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000146 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000147 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
148 if (lineMidpointState.betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000149 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000150 return;
151 // This is a new start point. Stop ignoring objects and
152 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000153 lineMidpointState.betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000154 start = nextMidpoint.pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000155 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000156 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000157 return appendRunsForObject(start, end, obj, resolver);
158 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000159 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000160 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000161 return;
162 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000163
hyatt78b85132004-03-29 20:07:45 +0000164 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000165 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000166 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000167 lineMidpointState.betweenMidpoints = true;
168 lineMidpointState.currentMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000169 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 +0000170 if (static_cast<int>(nextMidpoint.pos + 1) > start)
171 resolver.addRun(new (obj->renderArena())
172 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
173 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000174 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000175 } else
176 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000177 }
178}
179
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000180static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
181{
182 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000183 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000184
185 if (obj->isText()) {
186 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
187 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
188 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
189 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000190 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000191 return textBox;
192 }
193
194 if (obj->isBox())
195 return toRenderBox(obj)->createInlineBox();
196
eric@webkit.org49b9d952009-07-03 01:29:07 +0000197 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000198}
199
200static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
201{
202 if (o->isText()) {
203 if (o->prefWidthsDirty() && o->isCounter())
204 toRenderText(o)->calcPrefWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
205 toRenderText(o)->dirtyLineBoxes(fullLayout);
206 } else
207 toRenderInline(o)->dirtyLineBoxes(fullLayout);
208}
209
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000210InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000211{
212 // See if we have an unconstructed line box for this object that is also
213 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000214 unsigned lineDepth = 1;
215 InlineFlowBox* childBox = 0;
216 InlineFlowBox* parentBox = 0;
217 InlineFlowBox* result = 0;
218 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000219 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000220
hyatt1d5d87b2007-04-24 04:55:54 +0000221 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000222 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000223
hyatt1d5d87b2007-04-24 04:55:54 +0000224 // If this box is constructed then it is from a previous line, and we need
225 // to make a new box for our line. If this box is unconstructed but it has
226 // something following it on the line, then we know we have to make a new box
227 // as well. In this situation our inline has actually been split in two on
228 // the same line (this can happen with very fancy language mixtures).
229 bool constructedNewBox = false;
230 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
231 // We need to make a new box for this render object. Once
232 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000233 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000234 ASSERT(newBox->isInlineFlowBox());
235 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000236 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000237 constructedNewBox = true;
238 }
mitz@apple.come1364202008-02-28 01:06:41 +0000239
hyatt1d5d87b2007-04-24 04:55:54 +0000240 if (!result)
241 result = parentBox;
242
243 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000244 // inline box for the line, and it doesn't have to be appended to any parent
245 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000246 if (childBox)
247 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000248
hyatt1d5d87b2007-04-24 04:55:54 +0000249 if (!constructedNewBox || obj == this)
250 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000251
hyatt1d5d87b2007-04-24 04:55:54 +0000252 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000253
hyatt1d5d87b2007-04-24 04:55:54 +0000254 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
255 // intermediate inline flows.
256 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000257
hyatt1d5d87b2007-04-24 04:55:54 +0000258 } while (true);
259
260 return result;
hyattffe78712003-02-11 01:59:29 +0000261}
262
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000263RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000264{
mitz@apple.com887f3592008-02-25 22:03:08 +0000265 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000266
267 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000268 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000269 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000270 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000271 if (runCount == 2 && !r->m_object->isListMarker())
272 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000273
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000274 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000275 r->m_box = box;
276
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000277 ASSERT(box);
278 if (!box)
279 continue;
hyattffe78712003-02-11 01:59:29 +0000280
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000281 // If we have no parent box yet, or if the run is not simply a sibling,
282 // then we need to construct inline boxes as necessary to properly enclose the
283 // run's inline box.
284 if (!parentBox || parentBox->renderer() != r->m_object->parent())
285 // Create new inline boxes all the way back to the appropriate insertion point.
286 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000287
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000288 // Append the inline box to this line.
289 parentBox->addToLine(box);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000290
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000291 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
292 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
293
294 if (box->isInlineTextBox()) {
295 InlineTextBox* text = static_cast<InlineTextBox*>(box);
296 text->setStart(r->m_start);
297 text->setLen(r->m_stop - r->m_start);
298 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000299 if (r->m_hasHyphen)
300 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000301 }
hyattffe78712003-02-11 01:59:29 +0000302 }
303
304 // We should have a root inline box. It should be unconstructed and
305 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000306 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000307
308 // Set bits on our inline flow boxes that indicate which sides should
309 // paint borders/margins/padding. This knowledge will ultimately be used when
310 // we determine the horizontal positions and widths of all the inline boxes on
311 // the line.
hyattffe78712003-02-11 01:59:29 +0000312 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
313
314 // Now mark the line boxes as being constructed.
315 lastLineBox()->setConstructed();
316
317 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000318 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000319}
320
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000321void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000322{
323 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000324 int availableWidth = lineWidth(height(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000325 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000326 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000327 unsigned numSpaces = 0;
328 ETextAlign textAlign = style()->textAlign();
329
mitz@apple.come1364202008-02-28 01:06:41 +0000330 for (BidiRun* r = firstRun; r; r = r->next()) {
331 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000332 continue; // Positioned objects are only participating to figure out their
333 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000334 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000335 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000336 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000337
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000338 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000339 const UChar* characters = rt->characters();
340 for (int i = r->m_start; i < r->m_stop; i++) {
341 UChar c = characters[i];
342 if (c == ' ' || c == '\n' || c == '\t')
343 numSpaces++;
344 }
345 }
346
mitz@apple.come1364202008-02-28 01:06:41 +0000347 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000348 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000349 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000350 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000351 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000352 HashSet<const SimpleFontData*> fallbackFonts;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000353 GlyphOverflow glyphOverflow;
mitz@apple.comb2107652010-06-21 16:54:52 +0000354 int hyphenWidth = 0;
355 if (static_cast<InlineTextBox*>(r->m_box)->hasHyphen()) {
356 const AtomicString& hyphenString = rt->style()->hyphenString();
357 hyphenWidth = rt->style(firstLine)->font().width(TextRun(hyphenString.characters(), hyphenString.length()));
358 }
359 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 +0000360 if (!fallbackFonts.isEmpty()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000361 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000362 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
363 ASSERT(it->second.first.isEmpty());
364 copyToVector(fallbackFonts, it->second.first);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000365 }
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000366 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000367 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000368 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
369 it->second.second = glyphOverflow;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000370 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000371 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000372 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000373 renderBox->calcWidth();
374 r->m_box->setWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000375 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000376 }
hyatt4b381692003-03-10 21:11:59 +0000377
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000378 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000379 }
380
381 // Armed with the total width of the line (without justification),
382 // we now examine our text-align property in order to determine where to position the
383 // objects horizontally. The total width of the line can be increased if we end up
384 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000385 int x = leftOffset(height(), firstLine);
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000386 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000387 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000388 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000389 // The direction of the block should determine what happens with wide lines. In
390 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000391 if (style()->direction() == LTR) {
392 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000393 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000394 } else {
395 if (trailingSpaceRun)
396 trailingSpaceRun->m_box->setWidth(0);
397 else if (totWidth > availableWidth)
398 x -= (totWidth - availableWidth);
399 }
hyattffe78712003-02-11 01:59:29 +0000400 break;
401 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000402 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
403 if (trailingSpaceRun) {
404 totWidth -= trailingSpaceRun->m_box->width();
405 trailingSpaceRun->m_box->setWidth(0);
406 }
hyattffe78712003-02-11 01:59:29 +0000407 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000408 }
hyattffe78712003-02-11 01:59:29 +0000409 // fall through
410 case TAAUTO:
411 numSpaces = 0;
412 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000413 if (style()->direction() == LTR) {
414 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000415 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
hyattffe78712003-02-11 01:59:29 +0000416 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000417 }
hyattffe78712003-02-11 01:59:29 +0000418 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000419 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000420 // Wide lines spill out of the block based off direction.
421 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
422 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000423 if (style()->direction() == LTR) {
424 if (trailingSpaceRun) {
425 totWidth -= trailingSpaceRun->m_box->width();
426 trailingSpaceRun->m_box->setWidth(0);
427 }
428 if (totWidth < availableWidth)
429 x += availableWidth - totWidth;
430 } else {
431 if (totWidth > availableWidth && trailingSpaceRun) {
mitz@apple.comcbf51272009-04-07 17:08:18 +0000432 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000433 totWidth -= trailingSpaceRun->m_box->width();
434 } else
435 x += availableWidth - totWidth;
436 }
hyattffe78712003-02-11 01:59:29 +0000437 break;
438 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000439 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000440 int trailingSpaceWidth = 0;
441 if (trailingSpaceRun) {
442 totWidth -= trailingSpaceRun->m_box->width();
443 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
mitz@apple.comcbf51272009-04-07 17:08:18 +0000444 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000445 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000446 if (style()->direction() == LTR)
447 x += max((availableWidth - totWidth) / 2, 0);
448 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000449 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000450 break;
451 }
452
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000453 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000454 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000455 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000456 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000457
hyattacbb0d42003-04-25 01:32:49 +0000458 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000459 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000460 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000461 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000462 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000463 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000464 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000465 spaces++;
darina3c48282003-10-07 15:49:30 +0000466 }
hyatt870bdda2003-05-21 23:37:33 +0000467
darinb53ebdc2006-07-09 15:10:21 +0000468 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000469
hyattdca76e92005-11-02 08:52:50 +0000470 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000471 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000472 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000473 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000474 totWidth += spaceAdd;
475 }
hyattacbb0d42003-04-25 01:32:49 +0000476 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000477 if (!numSpaces)
478 break;
hyattacbb0d42003-04-25 01:32:49 +0000479 }
hyattffe78712003-02-11 01:59:29 +0000480 }
hyattffe78712003-02-11 01:59:29 +0000481 }
mitz@apple.come1364202008-02-28 01:06:41 +0000482
hyattffe78712003-02-11 01:59:29 +0000483 // The widths of all runs are now known. We can now place every inline box (and
484 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000485 needsWordSpacing = false;
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000486 lineBox->placeBoxesHorizontally(x, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000487}
488
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000489void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000490{
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000491 setHeight(lineBox->verticallyAlignBoxes(height(), textBoxDataMap));
hyatt@apple.comd885df72009-01-22 02:31:52 +0000492 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000493
494 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000495 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000496 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000497 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000498 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000499
hyatt98ee7e42003-05-14 01:39:15 +0000500 // Align positioned boxes with the top of the line box. This is
501 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000502 if (r->m_object->isPositioned())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000503 r->m_box->setY(height());
hyatt98ee7e42003-05-14 01:39:15 +0000504
505 // Position is used to properly position both replaced elements and
506 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000507 if (r->m_object->isText())
508 toRenderText(r->m_object)->positionLineBox(r->m_box);
509 else if (r->m_object->isBox())
510 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000511 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000512 // Positioned objects and zero-length text nodes destroy their boxes in
513 // position(), which unnecessarily dirties the line.
514 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000515}
kociendabb0c24b2001-08-24 14:24:40 +0000516
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000517static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
518{
519 if (character == ' ' || character == '\t' || character == softHyphen)
520 return true;
521 if (character == '\n')
522 return !renderer->style()->preserveNewline();
523 if (character == noBreakSpace)
524 return renderer->style()->nbspMode() == SPACE;
525 return false;
526}
527
jamesr@google.com19548ad2010-04-02 23:21:35 +0000528void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
529{
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000530 bool useRepaintBounds = false;
531
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000532 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000533
hyatt@apple.comd885df72009-01-22 02:31:52 +0000534 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000535 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000536
hyatt0c3a9862004-02-23 21:26:26 +0000537 // Figure out if we should clear out our line boxes.
538 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000539 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000540 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000541 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000542
hyatted77ad82004-06-15 07:21:23 +0000543 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
544 // clip.
545 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
546 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
547 // anyway, so we won't worry about following the draft here.
548 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000549
hyatted77ad82004-06-15 07:21:23 +0000550 // Walk all the lines and delete our ellipsis line boxes if they exist.
551 if (hasTextOverflow)
552 deleteEllipsisLineBoxes();
553
hyattffe78712003-02-11 01:59:29 +0000554 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000555 // layout replaced elements
556 bool endOfInline = false;
557 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000558 Vector<FloatWithRect> floats;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000559 bool hasInlineChild = false;
560 while (o) {
561 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
562 RenderBox* box = toRenderBox(o);
563
564 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
565 o->setChildNeedsLayout(true, false);
566
567 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
568 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
569 o->setPrefWidthsDirty(true, false);
570
571 if (o->isPositioned())
572 o->containingBlock()->insertPositionedObject(box);
573 else {
574 if (o->isFloating())
575 floats.append(FloatWithRect(box));
576 else if (fullLayout || o->needsLayout()) // Replaced elements
577 toRenderBox(o)->dirtyLineBoxes(fullLayout);
578
579 o->layoutIfNeeded();
580 }
581 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
582 hasInlineChild = true;
583 if (fullLayout || o->selfNeedsLayout())
584 dirtyLineBoxesForRenderer(o, fullLayout);
585 o->setNeedsLayout(false);
586 if (!o->isText())
587 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
588 }
589 o = bidiNext(this, o, 0, false, &endOfInline);
590 }
591
592 // We want to skip ahead to the first dirty line
593 InlineBidiResolver resolver;
594 unsigned floatIndex;
595 bool firstLine = true;
596 bool previousLineBrokeCleanly = true;
597 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000598
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000599 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000600 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
601 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000602 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000603 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000604 // Because we waited until we were already inside layout to discover
605 // that the block really needed a full layout, we missed our chance to repaint the layer
606 // before layout started. Luckily the layer has cached the repaint rect for its original
607 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000608 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000609 }
610 }
hyatt0c3a9862004-02-23 21:26:26 +0000611
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000612 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
613
614 LineMidpointState& lineMidpointState = resolver.midpointState();
615
616 // We also find the first clean line and extract these lines. We will add them back
617 // if we determine that we're able to synchronize after handling all our dirty lines.
618 InlineIterator cleanLineStart;
619 BidiStatus cleanLineBidiStatus;
620 int endLineYPos = 0;
621 RootInlineBox* endLine = (fullLayout || !startLine) ?
622 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
623
624 if (startLine) {
625 useRepaintBounds = true;
626 repaintTop = height();
627 repaintBottom = height();
628 RenderArena* arena = renderArena();
629 RootInlineBox* box = startLine;
630 while (box) {
631 repaintTop = min(repaintTop, box->topVisibleOverflow());
632 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
633 RootInlineBox* next = box->nextRootBox();
634 box->deleteLine(arena);
635 box = next;
636 }
637 }
638
639 InlineIterator end = resolver.position();
640
641 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
642 // If the last line before the start line ends with a line break that clear floats,
643 // adjust the height accordingly.
644 // A line break can be either the first or the last object on a line, depending on its direction.
645 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
646 RenderObject* lastObject = lastLeafChild->renderer();
647 if (!lastObject->isBR())
648 lastObject = lastRootBox()->firstLeafChild()->renderer();
649 if (lastObject->isBR()) {
650 EClear clear = lastObject->style()->clear();
651 if (clear != CNONE)
652 newLine(clear);
653 }
654 }
655 }
656
657 bool endLineMatched = false;
658 bool checkForEndLineMatch = endLine;
659 bool checkForFloatsFromLastLine = false;
660
661 bool isLineEmpty = true;
662
663 while (!end.atEnd()) {
664 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
665 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
666 break;
667
668 lineMidpointState.reset();
669
670 isLineEmpty = true;
671
672 EClear clear = CNONE;
mitz@apple.comb2107652010-06-21 16:54:52 +0000673 bool hyphenated;
674 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, hyphenated, &clear);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000675 if (resolver.position().atEnd()) {
676 resolver.deleteRuns();
677 checkForFloatsFromLastLine = true;
678 break;
679 }
680 ASSERT(end != resolver.position());
681
682 if (!isLineEmpty) {
mitz@apple.com62d5bbd2010-06-25 18:07:01 +0000683 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000684 ASSERT(resolver.position() == end);
685
686 BidiRun* trailingSpaceRun = 0;
687 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
688 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
689 trailingSpaceRun = resolver.logicallyLastRun();
690 RenderObject* lastObject = trailingSpaceRun->m_object;
691 if (lastObject->isText()) {
692 RenderText* lastText = toRenderText(lastObject);
693 const UChar* characters = lastText->characters();
694 int firstSpace = trailingSpaceRun->stop();
695 while (firstSpace > trailingSpaceRun->start()) {
696 UChar current = characters[firstSpace - 1];
697 if (!isCollapsibleSpace(current, lastText))
698 break;
699 firstSpace--;
700 }
701 if (firstSpace == trailingSpaceRun->stop())
702 trailingSpaceRun = 0;
703 else {
704 TextDirection direction = style()->direction();
705 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
706 if (firstSpace != trailingSpaceRun->start()) {
707 BidiContext* baseContext = resolver.context();
708 while (BidiContext* parent = baseContext->parent())
709 baseContext = parent;
710
711 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
712 trailingSpaceRun->m_stop = firstSpace;
713 if (direction == LTR)
714 resolver.addRun(newTrailingRun);
715 else
716 resolver.prependRun(newTrailingRun);
717 trailingSpaceRun = newTrailingRun;
718 shouldReorder = false;
719 }
720 if (shouldReorder) {
721 if (direction == LTR) {
722 resolver.moveRunToEnd(trailingSpaceRun);
723 trailingSpaceRun->m_level = 0;
724 } else {
725 resolver.moveRunToBeginning(trailingSpaceRun);
726 trailingSpaceRun->m_level = 1;
727 }
728 }
729 }
730 } else
731 trailingSpaceRun = 0;
732 }
733
734 // Now that the runs have been ordered, we create the line boxes.
735 // At the same time we figure out where border/padding/margin should be applied for
736 // inline flow boxes.
737
738 RootInlineBox* lineBox = 0;
739 if (resolver.runCount()) {
mitz@apple.comb2107652010-06-21 16:54:52 +0000740 if (hyphenated)
741 resolver.logicallyLastRun()->m_hasHyphen = true;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000742 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
743 if (lineBox) {
744 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
745
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000746#if ENABLE(SVG)
747 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
748#else
749 bool isSVGRootInlineBox = false;
750#endif
751
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000752 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000753
754 // Now we position all of our text runs horizontally.
755 if (!isSVGRootInlineBox)
756 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000757
758 // Now position our text runs vertically.
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000759 computeVerticalPositionsForLine(lineBox, resolver.firstRun(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000760
761#if ENABLE(SVG)
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000762 // SVG text layout code computes vertical & horizontal positions on its own.
763 // Note that we still need to execute computeVerticalPositionsForLine() as
764 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
765 // contains reversed text or not. If we wouldn't do that editing and thus
766 // text selection in RTL boxes would not work as expected.
767 if (isSVGRootInlineBox) {
768 ASSERT(isSVGText());
769 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
770 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000771#endif
772
773#if PLATFORM(MAC)
774 // Highlight acts as an overflow inflation.
775 if (style()->highlight() != nullAtom)
776 lineBox->addHighlightOverflow();
777#endif
778 }
779 }
780
781 resolver.deleteRuns();
782
783 if (lineBox) {
784 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
785 if (useRepaintBounds) {
786 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
787 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
788 }
789 }
790
791 firstLine = false;
792 newLine(clear);
793 }
794
795 if (m_floatingObjects && lastRootBox()) {
796 if (lastFloat) {
797 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
798 }
799 m_floatingObjects->next();
800 } else
801 m_floatingObjects->first();
802 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
803 lastRootBox()->floats().append(f->m_renderer);
804 ASSERT(f->m_renderer == floats[floatIndex].object);
805 // If a float's geometry has changed, give up on syncing with clean lines.
806 if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
807 checkForEndLineMatch = false;
808 floatIndex++;
809 }
810 lastFloat = m_floatingObjects->last();
811 }
812
813 lineMidpointState.reset();
814 resolver.setPosition(end);
815 }
816
817 if (endLine) {
818 if (endLineMatched) {
819 // Attach all the remaining lines, and then adjust their y-positions as needed.
820 int delta = height() - endLineYPos;
821 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
822 line->attachLine();
823 if (delta) {
824 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
825 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
826 line->adjustPosition(0, delta);
827 }
828 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
829 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
830 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
831 int floatTop = (*f)->y() - (*f)->marginTop();
832 insertFloatingObject(*f);
833 setHeight(floatTop + delta);
834 positionNewFloats();
835 }
836 }
837 }
838 setHeight(lastRootBox()->blockHeight());
839 } else {
840 // Delete all the remaining lines.
841 RootInlineBox* line = endLine;
842 RenderArena* arena = renderArena();
843 while (line) {
844 repaintTop = min(repaintTop, line->topVisibleOverflow());
845 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
846 RootInlineBox* next = line->nextRootBox();
847 line->deleteLine(arena);
848 line = next;
849 }
850 }
851 }
852 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
853 // In case we have a float on the last line, it might not be positioned up to now.
854 // This has to be done before adding in the bottom border/padding, or the float will
855 // include the padding incorrectly. -dwh
856 if (checkForFloatsFromLastLine) {
857 int bottomVisualOverflow = lastRootBox()->bottomVisualOverflow();
858 int bottomLayoutOverflow = lastRootBox()->bottomLayoutOverflow();
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000859 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000860 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
861 trailingFloatsLineBox->setConstructed();
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000862 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
863 trailingFloatsLineBox->verticallyAlignBoxes(height(), textBoxDataMap);
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000864 trailingFloatsLineBox->setVerticalOverflowPositions(height(), bottomLayoutOverflow, height(), bottomVisualOverflow, 0);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000865 trailingFloatsLineBox->setBlockHeight(height());
866 }
867 if (lastFloat) {
868 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
869 }
870 m_floatingObjects->next();
871 } else
872 m_floatingObjects->first();
873 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
874 lastRootBox()->floats().append(f->m_renderer);
875 lastFloat = m_floatingObjects->last();
876 }
877 size_t floatCount = floats.size();
878 // Floats that did not have layout did not repaint when we laid them out. They would have
879 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
880 // painted.
881 for (size_t i = 0; i < floatCount; ++i) {
882 if (!floats[i].everHadLayout) {
883 RenderBox* f = floats[i].object;
884 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
885 f->repaint();
886 }
887 }
kociendabb0c24b2001-08-24 14:24:40 +0000888 }
hyatt85586af2003-02-19 23:22:42 +0000889
hyatta70560a2002-11-20 01:53:20 +0000890 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000891 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +0000892
adele7a470a72006-04-20 22:22:14 +0000893 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000894 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +0000895
896 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
897 // truncate text.
898 if (hasTextOverflow)
899 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +0000900}
901
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000902RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
903 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +0000904{
905 RootInlineBox* curr = 0;
906 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000907
mitz@apple.com40547b32008-03-18 04:04:34 +0000908 bool dirtiedByFloat = false;
909 if (!fullLayout) {
910 size_t floatIndex = 0;
911 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000912 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
913 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
914 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
915 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +0000916 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
917 ASSERT(floatIndex < floats.size());
918 if (floats[floatIndex].object != f) {
919 // A new float has been inserted before this line or before its last known float.
920 // Just do a full layout.
921 fullLayout = true;
922 break;
923 }
924 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +0000925 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +0000926 curr->markDirty();
mitz@apple.com1d4e9532010-01-18 20:55:25 +0000927 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()), curr);
mitz@apple.com40547b32008-03-18 04:04:34 +0000928 floats[floatIndex].rect.setSize(newSize);
929 dirtiedByFloat = true;
930 }
931 floatIndex++;
932 }
933 }
934 if (dirtiedByFloat || fullLayout)
935 break;
936 }
937 // Check if a new float has been inserted after the last known float.
938 if (!curr && floatIndex < floats.size())
939 fullLayout = true;
940 }
941
hyatt0c3a9862004-02-23 21:26:26 +0000942 if (fullLayout) {
943 // Nuke all our lines.
944 if (firstRootBox()) {
945 RenderArena* arena = renderArena();
946 curr = firstRootBox();
947 while (curr) {
948 RootInlineBox* next = curr->nextRootBox();
949 curr->deleteLine(arena);
950 curr = next;
951 }
darinec375482007-01-06 01:36:24 +0000952 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +0000953 }
eseidel789896f2005-11-27 22:52:09 +0000954 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000955 if (curr) {
956 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +0000957 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +0000958 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +0000959 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +0000960 // The previous line didn't break cleanly or broke at a newline
961 // that has been deleted, so treat it as dirty too.
962 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +0000963 }
eseidel789896f2005-11-27 22:52:09 +0000964 } else {
hyatt0c3a9862004-02-23 21:26:26 +0000965 // No dirty lines were found.
966 // If the last line didn't break cleanly, treat it as dirty.
967 if (lastRootBox() && !lastRootBox()->endsWithBreak())
968 curr = lastRootBox();
969 }
mitz@apple.come1364202008-02-28 01:06:41 +0000970
hyatt0c3a9862004-02-23 21:26:26 +0000971 // If we have no dirty lines, then last is just the last root box.
972 last = curr ? curr->prevRootBox() : lastRootBox();
973 }
mitz@apple.come1364202008-02-28 01:06:41 +0000974
mitz@apple.com40547b32008-03-18 04:04:34 +0000975 numCleanFloats = 0;
976 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000977 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000978 // Restore floats from clean lines.
979 RootInlineBox* line = firstRootBox();
980 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000981 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
982 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
983 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +0000984 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +0000985 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +0000986 positionNewFloats();
987 ASSERT(floats[numCleanFloats].object == *f);
988 numCleanFloats++;
989 }
990 }
991 line = line->nextRootBox();
992 }
hyatt@apple.comd885df72009-01-22 02:31:52 +0000993 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +0000994 }
995
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000996 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +0000997 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +0000998
999 RenderObject* startObj;
1000 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001001 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001002 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001003 startObj = last->lineBreakObj();
1004 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001005 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001006 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001007 bool ltr = style()->direction() == LTR
1008 #if ENABLE(SVG)
1009 || (style()->unicodeBidi() == UBNormal && isSVGText())
1010 #endif
1011 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001012
mitz@apple.com45d9e102009-04-27 16:24:55 +00001013 Direction direction = ltr ? LeftToRight : RightToLeft;
1014 resolver.setLastStrongDir(direction);
1015 resolver.setLastDir(direction);
1016 resolver.setEorDir(direction);
1017 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001018
mitz@apple.com15035e62008-07-05 20:44:44 +00001019 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001020 }
mitz@apple.come1364202008-02-28 01:06:41 +00001021
mitz@apple.com15035e62008-07-05 20:44:44 +00001022 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001023
hyatt0c3a9862004-02-23 21:26:26 +00001024 return curr;
1025}
1026
mitz@apple.com15035e62008-07-05 20:44:44 +00001027RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001028{
1029 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001030 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001031 last = 0;
1032 else {
hyatt04420ca2004-07-16 00:05:42 +00001033 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1034 if (curr->isDirty())
1035 last = 0;
1036 else if (!last)
1037 last = curr;
1038 }
hyatt0c3a9862004-02-23 21:26:26 +00001039 }
mitz@apple.come1364202008-02-28 01:06:41 +00001040
hyatt0c3a9862004-02-23 21:26:26 +00001041 if (!last)
1042 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001043
eseidel789896f2005-11-27 22:52:09 +00001044 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001045 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001046 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001047 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001048
hyatt0c3a9862004-02-23 21:26:26 +00001049 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1050 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1051 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001052
hyatt0c3a9862004-02-23 21:26:26 +00001053 return last;
1054}
1055
mitz@apple.com15035e62008-07-05 20:44:44 +00001056bool 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 +00001057{
mitz@apple.com15035e62008-07-05 20:44:44 +00001058 if (resolver.position() == endLineStart) {
1059 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001060 return false;
1061
hyatt@apple.comd885df72009-01-22 02:31:52 +00001062 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001063 if (!delta || !m_floatingObjects)
1064 return true;
1065
1066 // 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 +00001067 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001068
1069 RootInlineBox* lastLine = endLine;
1070 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1071 lastLine = nextLine;
1072
1073 int bottom = lastLine->blockHeight() + abs(delta);
1074
1075 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001076 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001077 return false;
1078 }
1079
1080 return true;
1081 }
hyatt0c3a9862004-02-23 21:26:26 +00001082
mitz@apple.come1364202008-02-28 01:06:41 +00001083 // The first clean line doesn't match, but we can check a handful of following lines to try
1084 // to match back up.
1085 static int numLines = 8; // The # of lines we're willing to match against.
1086 RootInlineBox* line = endLine;
1087 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001088 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001089 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001090 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001091 return false; // ...but the bidi state doesn't match.
1092 RootInlineBox* result = line->nextRootBox();
1093
1094 // Set our yPos to be the block height of endLine.
1095 if (result)
1096 endYPos = line->blockHeight();
1097
hyatt@apple.comd885df72009-01-22 02:31:52 +00001098 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001099 if (delta && m_floatingObjects) {
1100 // 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 +00001101 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001102
1103 RootInlineBox* lastLine = endLine;
1104 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1105 lastLine = nextLine;
1106
1107 int bottom = lastLine->blockHeight() + abs(delta);
1108
1109 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001110 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001111 return false;
1112 }
1113 }
1114
mitz@apple.come1364202008-02-28 01:06:41 +00001115 // Now delete the lines that we failed to sync.
1116 RootInlineBox* boxToDelete = endLine;
1117 RenderArena* arena = renderArena();
1118 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001119 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1120 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001121 RootInlineBox* next = boxToDelete->nextRootBox();
1122 boxToDelete->deleteLine(arena);
1123 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001124 }
mitz@apple.come1364202008-02-28 01:06:41 +00001125
1126 endLine = result;
1127 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001128 }
1129 }
mitz@apple.come1364202008-02-28 01:06:41 +00001130
hyatt0c3a9862004-02-23 21:26:26 +00001131 return false;
1132}
1133
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001134static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001135{
darinf9e5d6c2007-01-09 14:54:26 +00001136 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001137 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001138
hyattdca76e92005-11-02 08:52:50 +00001139 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1140 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001141 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001142 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1143 // |true|).
1144 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001145 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001146
kocienda498d1982004-10-15 21:07:24 +00001147 return true;
kocienda98440082004-10-14 23:51:47 +00001148}
1149
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001150static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001151{
1152 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1153}
1154
zimmermannac3781f2007-02-04 01:25:03 +00001155static inline bool shouldPreserveNewline(RenderObject* object)
1156{
mjsd2948ef2007-02-26 19:29:04 +00001157#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001158 if (object->isSVGInlineText())
zimmermannac3781f2007-02-04 01:25:03 +00001159 return false;
1160#endif
1161
1162 return object->style()->preserveNewline();
1163}
1164
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001165static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001166{
1167 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001168 // We need to fix this, though, because at the very least, inlines containing only
1169 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001170 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001171}
1172
hyatt@apple.com71eeb442010-02-11 20:05:51 +00001173bool RenderBlock::requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001174{
bdakinf876bee2007-10-30 05:27:09 +00001175 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001176 return false;
bdakinf876bee2007-10-30 05:27:09 +00001177
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001178 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001179 return false;
1180
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001181 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001182 return true;
1183
1184 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001185 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1186 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001187}
1188
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001189bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001190{
1191 ASSERT(inlineObj->parent() == this);
1192
mitz@apple.com15035e62008-07-05 20:44:44 +00001193 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001194 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001195 it.increment();
bdashccffb432007-07-13 11:51:40 +00001196
1197 return !it.atEnd();
1198}
1199
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001200// 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 +00001201// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1202// elements quite right. In other words, we need to build this function's work into the normal line
1203// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001204// NB. this function will insert any floating elements that would otherwise
1205// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001206void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001207{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001208 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001209 RenderObject* object = iterator.obj;
1210 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001211 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001212 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001213 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1214 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001215 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001216 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001217 // A relative positioned inline encloses us. In this case, we also have to determine our
1218 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1219 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001220 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1221 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001222 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001223
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001224 RenderBox* box = toRenderBox(object);
1225 if (box->style()->hasStaticX()) {
1226 if (box->style()->isOriginalDisplayInlineType())
1227 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001228 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001229 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001230 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001231
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001232 if (box->style()->hasStaticY())
1233 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001234 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001235 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001236 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001237}
bdashccffb432007-07-13 11:51:40 +00001238
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001239int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly)
mitz@apple.com1a301772008-03-11 18:30:36 +00001240{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001241 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001242 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001243 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001244 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001245 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001246 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001247 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001248 } else if (object->isPositioned()) {
1249 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1250 // will work for the common cases
1251 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001252 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001253 // A relative positioned inline encloses us. In this case, we also have to determine our
1254 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1255 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001256 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1257 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001258 }
1259
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001260 RenderBox* box = toRenderBox(object);
1261 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001262 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001263 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001264 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001265 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001266 }
1267
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001268 if (box->style()->hasStaticY())
1269 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001270 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001271 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001272 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001273 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001274 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001275}
1276
bdakinf876bee2007-10-30 05:27:09 +00001277// This is currently just used for list markers and inline flows that have line boxes. Neither should
1278// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001279static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001280{
mitz@apple.com1a301772008-03-11 18:30:36 +00001281 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001282 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1283 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001284 UChar nextChar = nextText->characters()[0];
1285 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001286 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001287 return true;
1288 }
1289 }
1290
1291 return false;
1292}
1293
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001294void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001295{
1296 ASSERT(widthToFit > availableWidth);
1297
1298 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001299 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001300 int newLineWidth = availableWidth;
1301 while (true) {
1302 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1303 if (!floatBottom)
1304 break;
1305
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001306 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001307 lastFloatBottom = floatBottom;
1308 if (newLineWidth >= widthToFit)
1309 break;
1310 }
1311
1312 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001313 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001314 availableWidth = newLineWidth;
1315 }
1316}
1317
mitz@apple.com34106442009-02-01 06:23:39 +00001318static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1319{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001320 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001321 return text->width(from, len, font, xPos);
1322 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1323}
1324
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001325static 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 +00001326{
1327 const AtomicString& hyphenString = text->style()->hyphenString();
1328 int hyphenWidth = font.width(TextRun(hyphenString.characters(), hyphenString.length()));
1329
1330 unsigned prefixLength = font.offsetForPosition(TextRun(text->characters() + lastSpace, pos - lastSpace, !collapseWhiteSpace, xPos + lastSpaceWordSpacing), availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing, false);
1331 if (!prefixLength)
1332 return;
1333
mitz@apple.com2ef44912010-08-07 21:41:06 +00001334 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, prefixLength + 1, localeIdentifier);
1335 if (!prefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00001336 return;
1337
1338#if !ASSERT_DISABLED
1339 int prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1340 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001341#else
1342 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001343#endif
1344
1345 lineBreak.obj = text;
1346 lineBreak.pos = lastSpace + prefixLength;
1347 lineBreak.nextBreakablePosition = nextBreakable;
1348 hyphenated = true;
1349}
1350
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001351InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
mitz@apple.comb2107652010-06-21 16:54:52 +00001352 bool& hyphenated, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001353{
mitz@apple.com15035e62008-07-05 20:44:44 +00001354 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001355
mitz@apple.com15035e62008-07-05 20:44:44 +00001356 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001357 LineMidpointState& lineMidpointState = resolver.midpointState();
1358
1359 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly);
mitz@apple.com1a301772008-03-11 18:30:36 +00001360
kociendae40cb942004-10-05 20:05:38 +00001361 int w = 0;
1362 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001363
mitz@apple.com15035e62008-07-05 20:44:44 +00001364 if (resolver.position().atEnd())
1365 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001366
hyatt33f8d492002-11-12 21:44:52 +00001367 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1368 // or not we are currently ignoring whitespace.
1369 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001370 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001371
1372 // This variable tracks whether the very last character we saw was a space. We use
1373 // this to detect when we encounter a second space so we know we have to terminate
1374 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001375 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001376 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001377 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001378
mitz@apple.com15035e62008-07-05 20:44:44 +00001379 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001380
mitz@apple.com15035e62008-07-05 20:44:44 +00001381 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001382 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001383 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001384 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001385 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001386
hyatt0c3a9862004-02-23 21:26:26 +00001387 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1388 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001389
mitz@apple.comb2107652010-06-21 16:54:52 +00001390 hyphenated = false;
1391
ddkilzer5d01fa22007-01-29 03:10:37 +00001392 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001393 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001394
hyatt@apple.com69340902008-01-16 21:24:21 +00001395 // 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 +00001396 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001397 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +00001398 bool allowImagesToBreak = !document()->inQuirksMode() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001399
hyattb0d9f602007-01-15 01:28:23 +00001400 EWhiteSpace currWS = style()->whiteSpace();
1401 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001402 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001403 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1404 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1405
1406 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001407 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001408
mjsd2948ef2007-02-26 19:29:04 +00001409#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001410 bool preserveNewline = o->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001411#else
hyattb0d9f602007-01-15 01:28:23 +00001412 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001413#endif
1414
hyattb0d9f602007-01-15 01:28:23 +00001415 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1416
hyatt275d0702005-11-03 23:53:57 +00001417 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001418 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001419 lBreak.obj = o;
1420 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001421 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001422 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001423
hyatt33f8d492002-11-12 21:44:52 +00001424 // A <br> always breaks a line, so don't let the line be collapsed
1425 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001426 // get collapsed away. It only does this if the previous line broke
1427 // cleanly. Otherwise the <br> has no effect on whether the line is
1428 // empty or not.
1429 if (prevLineBrokeCleanly)
1430 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001431 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001432 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001433
mitz@apple.com71e30842008-03-18 16:13:31 +00001434 if (!isLineEmpty && clear)
1435 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001436 }
1437 goto end;
1438 }
hyattb0d9f602007-01-15 01:28:23 +00001439
hyatt275d0702005-11-03 23:53:57 +00001440 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001441 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001442 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001443 RenderBox* floatBox = toRenderBox(o);
1444 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001445 // check if it fits in the current line.
1446 // If it does, position it now, otherwise, position
1447 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001448 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001449 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001450 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001451 } else
1452 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001453 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001454 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001455 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001456 RenderBox* box = toRenderBox(o);
1457 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1458 bool needToSetStaticX = box->style()->hasStaticX();
1459 if (box->style()->hasStaticX() && !isInlineType) {
1460 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001461 borderLeft() + paddingLeft() :
1462 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001463 needToSetStaticX = false;
1464 }
1465
1466 // If our original display was an INLINE type, then we can go ahead
1467 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001468 bool needToSetStaticY = box->style()->hasStaticY();
1469 if (box->style()->hasStaticY() && isInlineType) {
1470 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001471 needToSetStaticY = false;
1472 }
1473
hyatt853cd7d2004-11-05 02:59:48 +00001474 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1475 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001476 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001477 needToCreateLineBox = true;
1478
hyatt98ee7e42003-05-14 01:39:15 +00001479 // If we're ignoring spaces, we have to stop and include this object and
1480 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001481 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001482 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001483 ignoreStart.obj = o;
1484 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001485 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001486 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1487 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001488 }
hyatt98b16282004-03-31 18:43:12 +00001489
hyatt851816b2003-07-08 07:54:17 +00001490 }
hyatt98ee7e42003-05-14 01:39:15 +00001491 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001492 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001493 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001494 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001495
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001496 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001497
bdakinf876bee2007-10-30 05:27:09 +00001498 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1499 // to make sure that we stop to include this object and then start ignoring spaces again.
1500 // If this object is at the start of the line, we need to behave like list markers and
1501 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001502 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001503 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001504 if (ignoringSpaces) {
1505 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001506 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1507 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001508 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001509 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001510 // Like with list markers, we start ignoring spaces to make sure that any
1511 // additional spaces we see will be discarded.
1512 currentCharacterIsSpace = true;
1513 currentCharacterIsWS = true;
1514 ignoringSpaces = true;
1515 }
1516 }
1517
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001518 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1519 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001520 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001521 RenderBox* replacedBox = toRenderBox(o);
1522
hyattde396342003-10-29 08:57:20 +00001523 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001524 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001525 w += tmpW;
1526 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001527 lBreak.obj = o;
1528 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001529 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001530 }
1531
mitz@apple.combfdc9112008-02-21 19:59:40 +00001532 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001533 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001534
hyatt33f8d492002-11-12 21:44:52 +00001535 isLineEmpty = false;
1536 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001537 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001538 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001539 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001540
bdakinf876bee2007-10-30 05:27:09 +00001541 // Optimize for a common case. If we can't find whitespace after the list
1542 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001543 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001544 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001545 // Like with inline flows, we start ignoring spaces to make sure that any
1546 // additional spaces we see will be discarded.
1547 currentCharacterIsSpace = true;
1548 currentCharacterIsWS = true;
1549 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001550 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001551 if (toRenderListMarker(o)->isInside())
1552 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001553 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001554 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001555 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001556 if (!pos)
1557 appliedStartWidth = false;
1558
darin@apple.com36744d62009-01-25 20:23:04 +00001559 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001560
darin42563ac52007-01-22 17:28:57 +00001561 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001562 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001563 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001564
mitz@apple.comb2107652010-06-21 16:54:52 +00001565 RenderStyle* style = t->style(firstLine);
1566 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001567 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001568 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->hyphenationLocale());
weinigf28a1c32007-02-14 14:10:31 +00001569
hyatt33f8d492002-11-12 21:44:52 +00001570 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001571 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001572 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001573
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001574 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1575 // space, then subtract its width.
mitz@apple.com6ff116d2010-07-06 00:11:08 +00001576 int wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(TextRun(&space, 1)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001577
darin54008922006-01-13 16:39:05 +00001578 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001579 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001580 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1581 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1582 // 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 +00001583 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001584 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001585 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1586
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001587 if (t->isWordBreak()) {
1588 w += tmpW;
1589 tmpW = 0;
1590 lBreak.obj = o;
1591 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001592 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001593 ASSERT(!len);
1594 }
1595
hyatt275d0702005-11-03 23:53:57 +00001596 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001597 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001598 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001599 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001600 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001601
hyattb0d9f602007-01-15 01:28:23 +00001602 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001603 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001604
hyatt78b85132004-03-29 20:07:45 +00001605 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001606 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001607 if (!ignoringSpaces) {
1608 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001609 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001610 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001611 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001612 else
darin@apple.com36744d62009-01-25 20:23:04 +00001613 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001614 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001615 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1616 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1617 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001618 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001619 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001620
hyatt78b85132004-03-29 20:07:45 +00001621 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001622 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001623
hyattdca76e92005-11-02 08:52:50 +00001624 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001625 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001626 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001627 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001628
mitz@apple.com15035e62008-07-05 20:44:44 +00001629 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001630 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001631 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001632 }
mitz@apple.combe429562008-03-07 01:09:51 +00001633
hyatt78b85132004-03-29 20:07:45 +00001634 pos++;
1635 len--;
eseideld13fe532005-11-30 02:40:29 +00001636 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001637 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
mitz@apple.comb2107652010-06-21 16:54:52 +00001638 if (style->hyphens() == HyphensNone) {
1639 // Prevent a line break at the soft hyphen by ensuring that betweenWords is false
1640 // in the next iteration.
1641 atStart = true;
1642 }
hyatt78b85132004-03-29 20:07:45 +00001643 continue;
1644 }
1645
hyatt3aff2332003-01-23 01:15:28 +00001646 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001647
darinf9e5d6c2007-01-09 14:54:26 +00001648 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001649
weiniged111c12007-07-13 22:45:11 +00001650 if ((breakAll || breakWords) && !midWordBreak) {
1651 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001652 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001653 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001654 }
darin47ece0d2005-09-04 07:42:31 +00001655
ddkilzere8759ef2007-03-25 06:28:19 +00001656 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001657
weiniged111c12007-07-13 22:45:11 +00001658 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001659 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001660 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001661 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001662 // Stop ignoring spaces and begin at this
1663 // new point.
hyatt48710d62003-08-21 09:17:13 +00001664 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001665 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001666 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001667 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001668 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001669 } else {
hyatt33f8d492002-11-12 21:44:52 +00001670 // Just keep ignoring these spaces.
1671 pos++;
1672 len--;
1673 continue;
1674 }
1675 }
rjwc9c257d2003-01-24 03:46:17 +00001676
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001677 int additionalTmpW;
1678 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1679 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1680 else
1681 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001682 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001683 if (!appliedStartWidth) {
1684 tmpW += inlineWidth(o, true, false);
1685 appliedStartWidth = true;
1686 }
1687
eseideld13fe532005-11-30 02:40:29 +00001688 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001689
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001690 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001691 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001692
hyattb0d9f602007-01-15 01:28:23 +00001693 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001694 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001695 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001696 bool lineWasTooWide = false;
1697 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001698 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001699 // Check if line is too big even without the extra space
1700 // at the end of the line. If it is not, do nothing.
1701 // If the line needs the extra whitespace to be too long,
1702 // then move the line break to the space and skip all
1703 // additional whitespace.
1704 if (w + tmpW + charWidth > width) {
1705 lineWasTooWide = true;
1706 lBreak.obj = o;
1707 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001708 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001709 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001710 }
ap932806a2006-10-01 09:06:09 +00001711 }
1712 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001713 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001714 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 +00001715 if (hyphenated)
1716 goto end;
1717 }
eric@webkit.org1cb44402009-12-29 06:25:16 +00001718 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 +00001719 if (!stoppedIgnoringSpaces && pos > 0) {
1720 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001721 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1722 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001723 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001724 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001725 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001726 }
hyatt78b85132004-03-29 20:07:45 +00001727 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001728 } else {
weiniged111c12007-07-13 22:45:11 +00001729 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001730 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001731 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001732 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001733 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001734 }
rjwc9c257d2003-01-24 03:46:17 +00001735 }
hyatt33f8d492002-11-12 21:44:52 +00001736
hyattb0d9f602007-01-15 01:28:23 +00001737 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001738 if (!stoppedIgnoringSpaces && pos > 0) {
1739 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001740 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1741 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001742 }
hyatta9f48e32003-02-03 22:48:01 +00001743 lBreak.obj = o;
1744 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001745 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001746 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001747 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001748 return lBreak;
1749 }
hyatta9f48e32003-02-03 22:48:01 +00001750
weinigf28a1c32007-02-14 14:10:31 +00001751 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001752 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001753 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001754 tmpW = 0;
1755 lBreak.obj = o;
1756 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001757 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001758 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1759 // opportunity to break after a word.
1760 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001761 }
hyatt33f8d492002-11-12 21:44:52 +00001762
darin54008922006-01-13 16:39:05 +00001763 if (midWordBreak) {
1764 // Remember this as a breakable position in case
1765 // adding the end width forces a break.
1766 lBreak.obj = o;
1767 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001768 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001769 midWordBreak &= (breakWords || breakAll);
1770 }
1771
1772 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001773 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1774 lastSpace = pos;
1775 }
hyatt33f8d492002-11-12 21:44:52 +00001776
hyattdca76e92005-11-02 08:52:50 +00001777 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001778 // If we encounter a newline, or if we encounter a
1779 // second space, we need to go ahead and break up this
1780 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001781 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001782 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001783
hyatt33f8d492002-11-12 21:44:52 +00001784 // We just entered a mode where we are ignoring
1785 // spaces. Create a midpoint to terminate the run
1786 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001787 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001788 }
1789 }
eseidel789896f2005-11-27 22:52:09 +00001790 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001791 // Stop ignoring spaces and begin at this
1792 // new point.
1793 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001794 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001795 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001796 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001797 }
hyatt98b16282004-03-31 18:43:12 +00001798
1799 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1800 ignoreStart.obj = o;
1801 ignoreStart.pos = pos;
1802 }
harrisone343c412005-01-18 01:07:26 +00001803
1804 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001805 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001806 lBreak.obj = o;
1807 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001808 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00001809 }
1810 }
hyatt33f8d492002-11-12 21:44:52 +00001811
hyattb0d9f602007-01-15 01:28:23 +00001812 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001813 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001814 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001815 trailingSpaceObject = 0;
1816
1817 pos++;
1818 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001819 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001820 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001821
kociendabb0c24b2001-08-24 14:24:40 +00001822 // IMPORTANT: pos is > length here!
mitz@apple.comb2107652010-06-21 16:54:52 +00001823 int additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1824 tmpW += additionalTmpW;
weinigf28a1c32007-02-14 14:10:31 +00001825 tmpW += inlineWidth(o, !appliedStartWidth, true);
mitz@apple.comb2107652010-06-21 16:54:52 +00001826
1827 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001828 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 +00001829 if (hyphenated)
1830 goto end;
1831 }
kociendabb0c24b2001-08-24 14:24:40 +00001832 } else
weinigf28a1c32007-02-14 14:10:31 +00001833 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001834
mitz@apple.com1a301772008-03-11 18:30:36 +00001835 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001836 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001837 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00001838 checkForBreak = true;
1839 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00001840 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00001841 if (currentCharacterIsSpace)
1842 checkForBreak = true;
1843 else {
harrison07b5e582005-08-15 23:31:16 +00001844 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00001845 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001846 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00001847 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00001848 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00001849 // If the next item on the line is text, and if we did not end with
1850 // a space, then the next text run continues our word (and so it needs to
1851 // keep adding to |tmpW|. Just update and continue.
1852 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001853 } else if (nextText->isWordBreak())
1854 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001855 bool willFitOnLine = w + tmpW <= width;
1856 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001857 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001858 willFitOnLine = tmpW <= width;
1859 }
ddkilzere8759ef2007-03-25 06:28:19 +00001860 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00001861 if (canPlaceOnLine && checkForBreak) {
1862 w += tmpW;
1863 tmpW = 0;
1864 lBreak.obj = next;
1865 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001866 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00001867 }
1868 }
1869 }
1870 }
1871
darin54008922006-01-13 16:39:05 +00001872 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00001873 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00001874 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00001875 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001876
1877 if (w)
1878 goto end;
1879
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001880 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00001881
hyatta14d1742003-01-02 20:25:46 +00001882 // |width| may have been adjusted because we got shoved down past a float (thus
1883 // giving us more room), so we need to retest, and only jump to
1884 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00001885 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00001886 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00001887 }
hyatt1d9e29b2003-04-10 01:48:53 +00001888
mitz@apple.com1a301772008-03-11 18:30:36 +00001889 if (!o->isFloatingOrPositioned()) {
1890 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00001891 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001892 w += tmpW;
1893 tmpW = 0;
1894 lBreak.obj = next;
1895 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001896 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001897 }
hyatt711fe232002-11-20 21:25:14 +00001898 }
1899
mitz@apple.com1a301772008-03-11 18:30:36 +00001900 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001901 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001902
hyatta9f48e32003-02-03 22:48:01 +00001903 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
1904 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00001905 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00001906 currentCharacterIsSpace = false;
1907
kociendabb0c24b2001-08-24 14:24:40 +00001908 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00001909 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00001910 }
1911
hyattb0d9f602007-01-15 01:28:23 +00001912
1913 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00001914 lBreak.obj = 0;
1915 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001916 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001917 }
1918
1919 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00001920 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00001921 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00001922 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00001923 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00001924 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00001925 lBreak.obj = o;
1926 lBreak.pos = pos - 1;
1927 } else {
1928 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00001929 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001930 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001931 }
hyatt275d0702005-11-03 23:53:57 +00001932 } else if (lBreak.obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00001933 // Don't ever break in the middle of a word if we can help it.
1934 // There's no room at all. We just have to be on this line,
1935 // even though we'll spill out.
1936 lBreak.obj = o;
1937 lBreak.pos = pos;
1938 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00001939 }
1940 }
1941
1942 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00001943 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00001944 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00001945
hyattfe99c872003-07-31 22:25:29 +00001946 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001947 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00001948
hyatt33f8d492002-11-12 21:44:52 +00001949 if (trailingSpaceObject) {
1950 // This object is either going to be part of the last midpoint, or it is going
1951 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
1952 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001953 if (lineMidpointState.numMidpoints % 2) {
1954 InlineIterator* midpoints = lineMidpointState.midpoints.data();
1955 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00001956 }
1957 //else if (lBreak.pos > 0)
1958 // lBreak.pos--;
1959 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00001960 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00001961 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00001962 unsigned length = text->textLength();
1963 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00001964 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001965 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00001966 }
1967 }
rjwc9c257d2003-01-24 03:46:17 +00001968
mjs54b64002003-04-02 02:59:02 +00001969 // We might have made lBreak an iterator that points past the end
1970 // of the object. Do this adjustment to make it point to the start
1971 // of the next object instead to avoid confusing the rest of the
1972 // code.
1973 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00001974 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00001975 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00001976 }
1977
hyatt78b85132004-03-29 20:07:45 +00001978 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
1979 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
1980 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001981 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00001982 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001983 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00001984 }
1985
kociendabb0c24b2001-08-24 14:24:40 +00001986 return lBreak;
1987}
1988
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001989void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00001990{
hyattb4b20872004-10-20 21:34:01 +00001991 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001992 addLayoutOverflow(curr->layoutOverflowRect());
1993 if (!hasOverflowClip())
1994 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00001995 }
1996}
1997
hyatted77ad82004-06-15 07:21:23 +00001998void RenderBlock::deleteEllipsisLineBoxes()
1999{
hyatted77ad82004-06-15 07:21:23 +00002000 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002001 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002002}
2003
2004void RenderBlock::checkLinesForTextOverflow()
2005{
2006 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002007 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2008 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002009 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002010 const Font& firstLineFont = firstLineStyle()->font();
2011 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002012 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2013 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002014
2015 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2016 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2017 // check the left edge of the line box to see if it is less
2018 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2019 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002020 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002021 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2022 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.com5631f112009-02-11 01:36:45 +00002023 int lineBoxEdge = ltr ? curr->x() + curr->width() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002024 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002025 // 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 +00002026 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2027 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2028 // space.
2029 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002030 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002031 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002032 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002033 }
2034 }
2035}
2036
hyattffe78712003-02-11 01:59:29 +00002037}