blob: da24d9c93ec5ab33ac3aa7f983371dd534ee29f0 [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"
hyatt@apple.comcc1737c2010-09-16 20:20:02 +000036#include "Settings.h"
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +000037#include "TrailingFloatsRootInlineBox.h"
darin36d11362006-04-11 16:30:21 +000038#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000039#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000040#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000041#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000042#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000043
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000044#if ENABLE(SVG)
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +000045#include "RenderSVGInlineText.h"
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +000046#include "SVGRootInlineBox.h"
47#endif
48
darin7bd70952006-04-13 07:07:34 +000049using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000050using namespace WTF;
51using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000052
darinb9481ed2006-03-20 02:57:59 +000053namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000054
hyatt1d5d87b2007-04-24 04:55:54 +000055// We don't let our line box tree for a single line get any deeper than this.
56const unsigned cMaxLineDepth = 200;
57
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000058static int getBorderPaddingMargin(RenderBoxModelObject* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000059{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000060 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
61 if (leftSide)
62 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
63 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000064}
65
66static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
67{
hyatt1d5d87b2007-04-24 04:55:54 +000068 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000069 int extraWidth = 0;
70 RenderObject* parent = child->parent();
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000071 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyatt@apple.com40232f82009-02-04 04:26:08 +000072 if (start && !child->previousSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000073 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), false);
hyatt@apple.com40232f82009-02-04 04:26:08 +000074 if (end && !child->nextSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000075 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), true);
hyattffe78712003-02-11 01:59:29 +000076 child = parent;
77 parent = child->parent();
78 }
79 return extraWidth;
80}
81
hyatt@apple.comb3466af2009-06-13 06:04:40 +000082static void chopMidpointsAt(LineMidpointState& lineMidpointState, RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +000083{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000084 if (!lineMidpointState.numMidpoints)
hyatt275d0702005-11-03 23:53:57 +000085 return;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000086 InlineIterator* midpoints = lineMidpointState.midpoints.data();
87 for (int i = lineMidpointState.numMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +000088 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +000089 if (point.obj == obj && point.pos == pos) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +000090 lineMidpointState.numMidpoints = i;
hyatt78b85132004-03-29 20:07:45 +000091 break;
92 }
93 }
94}
95
hyatt@apple.comb3466af2009-06-13 06:04:40 +000096static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +000097{
98 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +000099 // shave it off the list, and shave off a trailing space if the previous end point doesn't
100 // preserve whitespace.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000101 if (lBreak.obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
102 InlineIterator* midpoints = lineMidpointState.midpoints.data();
103 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
104 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000105 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000106 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000107 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000108 if (currpoint == lBreak) {
109 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000110 lineMidpointState.numMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000111 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000112 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000113 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000114 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000115 if (endpoint.pos + 1 < textObj->textLength()) {
116 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000117 return;
118 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000119 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000120 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000121 return;
122 }
hyattee1bcae2004-03-29 21:36:04 +0000123 }
hyattfe99c872003-07-31 22:25:29 +0000124 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000125 }
hyattfe99c872003-07-31 22:25:29 +0000126 }
127 }
128}
129
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000130static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000131{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000132 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
133 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000134
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000135 InlineIterator* midpoints = lineMidpointState.midpoints.data();
136 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000137}
138
hyatt@apple.com71eeb442010-02-11 20:05:51 +0000139void RenderBlock::appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000140{
hyatt98ee7e42003-05-14 01:39:15 +0000141 if (start > end || obj->isFloating() ||
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000142 (obj->isPositioned() && !obj->style()->hasStaticX() && !obj->style()->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000143 return;
hyatt85586af2003-02-19 23:22:42 +0000144
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000145 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000146 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000147 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000148 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000149 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
150 if (lineMidpointState.betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000151 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000152 return;
153 // This is a new start point. Stop ignoring objects and
154 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000155 lineMidpointState.betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000156 start = nextMidpoint.pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000157 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000158 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000159 return appendRunsForObject(start, end, obj, resolver);
160 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000161 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000162 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000163 return;
164 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000165
hyatt78b85132004-03-29 20:07:45 +0000166 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000167 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000168 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000169 lineMidpointState.betweenMidpoints = true;
170 lineMidpointState.currentMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000171 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 +0000172 if (static_cast<int>(nextMidpoint.pos + 1) > start)
173 resolver.addRun(new (obj->renderArena())
174 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
175 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000176 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000177 } else
178 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000179 }
180}
181
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000182static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
183{
184 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000185 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000186
187 if (obj->isText()) {
188 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
189 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
190 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
191 if (obj->isBR())
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +0000192 textBox->setIsText(isOnlyRun || obj->document()->inNoQuirksMode());
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000193 return textBox;
194 }
195
196 if (obj->isBox())
197 return toRenderBox(obj)->createInlineBox();
198
eric@webkit.org49b9d952009-07-03 01:29:07 +0000199 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000200}
201
202static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
203{
204 if (o->isText()) {
hyatt@apple.com75dad742010-09-24 18:07:44 +0000205 if (o->preferredLogicalWidthsDirty() && o->isCounter())
206 toRenderText(o)->computePreferredLogicalWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000207 toRenderText(o)->dirtyLineBoxes(fullLayout);
208 } else
209 toRenderInline(o)->dirtyLineBoxes(fullLayout);
210}
211
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000212InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000213{
214 // See if we have an unconstructed line box for this object that is also
215 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000216 unsigned lineDepth = 1;
217 InlineFlowBox* childBox = 0;
218 InlineFlowBox* parentBox = 0;
219 InlineFlowBox* result = 0;
220 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000221 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000222
hyatt1d5d87b2007-04-24 04:55:54 +0000223 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000224 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000225
hyatt1d5d87b2007-04-24 04:55:54 +0000226 // If this box is constructed then it is from a previous line, and we need
227 // to make a new box for our line. If this box is unconstructed but it has
228 // something following it on the line, then we know we have to make a new box
229 // as well. In this situation our inline has actually been split in two on
230 // the same line (this can happen with very fancy language mixtures).
231 bool constructedNewBox = false;
232 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
233 // We need to make a new box for this render object. Once
234 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000235 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000236 ASSERT(newBox->isInlineFlowBox());
237 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000238 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000239 constructedNewBox = true;
240 }
mitz@apple.come1364202008-02-28 01:06:41 +0000241
hyatt1d5d87b2007-04-24 04:55:54 +0000242 if (!result)
243 result = parentBox;
244
245 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000246 // inline box for the line, and it doesn't have to be appended to any parent
247 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000248 if (childBox)
249 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000250
hyatt1d5d87b2007-04-24 04:55:54 +0000251 if (!constructedNewBox || obj == this)
252 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000253
hyatt1d5d87b2007-04-24 04:55:54 +0000254 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000255
hyatt1d5d87b2007-04-24 04:55:54 +0000256 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
257 // intermediate inline flows.
258 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000259
hyatt1d5d87b2007-04-24 04:55:54 +0000260 } while (true);
261
262 return result;
hyattffe78712003-02-11 01:59:29 +0000263}
264
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000265RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000266{
mitz@apple.com887f3592008-02-25 22:03:08 +0000267 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000268
269 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000270 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000271 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000272 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000273 if (runCount == 2 && !r->m_object->isListMarker())
274 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000275
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000276 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000277 r->m_box = box;
278
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000279 ASSERT(box);
280 if (!box)
281 continue;
hyattffe78712003-02-11 01:59:29 +0000282
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000283 // If we have no parent box yet, or if the run is not simply a sibling,
284 // then we need to construct inline boxes as necessary to properly enclose the
285 // run's inline box.
286 if (!parentBox || parentBox->renderer() != r->m_object->parent())
287 // Create new inline boxes all the way back to the appropriate insertion point.
288 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000289
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000290 // Append the inline box to this line.
291 parentBox->addToLine(box);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000292
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000293 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
294 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
295
296 if (box->isInlineTextBox()) {
297 InlineTextBox* text = static_cast<InlineTextBox*>(box);
298 text->setStart(r->m_start);
299 text->setLen(r->m_stop - r->m_start);
300 text->m_dirOverride = r->dirOverride(visuallyOrdered);
mitz@apple.comb2107652010-06-21 16:54:52 +0000301 if (r->m_hasHyphen)
302 text->setHasHyphen(true);
hyatt0c3a9862004-02-23 21:26:26 +0000303 }
hyattffe78712003-02-11 01:59:29 +0000304 }
305
306 // We should have a root inline box. It should be unconstructed and
307 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000308 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000309
310 // Set bits on our inline flow boxes that indicate which sides should
311 // paint borders/margins/padding. This knowledge will ultimately be used when
312 // we determine the horizontal positions and widths of all the inline boxes on
313 // the line.
hyattffe78712003-02-11 01:59:29 +0000314 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
315
316 // Now mark the line boxes as being constructed.
317 lastLineBox()->setConstructed();
318
319 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000320 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000321}
322
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000323void RenderBlock::computeInlineDirectionPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000324{
325 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000326 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.com39cee962010-09-23 21:47:13 +0000327 int totWidth = lineBox->getFlowSpacingLogicalWidth();
darin06dcb9c2005-08-15 04:31:09 +0000328 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000329 unsigned numSpaces = 0;
330 ETextAlign textAlign = style()->textAlign();
331
mitz@apple.come1364202008-02-28 01:06:41 +0000332 for (BidiRun* r = firstRun; r; r = r->next()) {
333 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000334 continue; // Positioned objects are only participating to figure out their
335 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000336 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000337 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000338 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000339
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000340 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000341 const UChar* characters = rt->characters();
342 for (int i = r->m_start; i < r->m_stop; i++) {
343 UChar c = characters[i];
344 if (c == ' ' || c == '\n' || c == '\t')
345 numSpaces++;
346 }
347 }
348
mitz@apple.come1364202008-02-28 01:06:41 +0000349 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000350 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000351 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000352 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000353 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000354 HashSet<const SimpleFontData*> fallbackFonts;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000355 GlyphOverflow glyphOverflow;
mitz@apple.comb2107652010-06-21 16:54:52 +0000356 int hyphenWidth = 0;
357 if (static_cast<InlineTextBox*>(r->m_box)->hasHyphen()) {
358 const AtomicString& hyphenString = rt->style()->hyphenString();
359 hyphenWidth = rt->style(firstLine)->font().width(TextRun(hyphenString.characters(), hyphenString.length()));
360 }
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000361 r->m_box->setLogicalWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, firstLine, &fallbackFonts, &glyphOverflow) + hyphenWidth);
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000362 if (!fallbackFonts.isEmpty()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000363 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000364 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
365 ASSERT(it->second.first.isEmpty());
366 copyToVector(fallbackFonts, it->second.first);
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000367 }
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000368 if ((glyphOverflow.top || glyphOverflow.bottom || glyphOverflow.left || glyphOverflow.right)) {
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000369 ASSERT(r->m_box->isText());
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000370 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.add(static_cast<InlineTextBox*>(r->m_box), make_pair(Vector<const SimpleFontData*>(), GlyphOverflow())).first;
371 it->second.second = glyphOverflow;
enrica@apple.comb9050ed2010-04-07 17:01:52 +0000372 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000373 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000374 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com5f4fae02010-09-24 06:45:16 +0000375 renderBox->computeLogicalWidth();
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000376 r->m_box->setLogicalWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000377 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000378 }
hyatt4b381692003-03-10 21:11:59 +0000379
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000380 totWidth += r->m_box->logicalWidth();
hyattffe78712003-02-11 01:59:29 +0000381 }
382
383 // Armed with the total width of the line (without justification),
384 // we now examine our text-align property in order to determine where to position the
385 // objects horizontally. The total width of the line can be increased if we end up
386 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000387 int x = leftOffset(height(), firstLine);
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000388 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000389 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000390 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000391 // The direction of the block should determine what happens with wide lines. In
392 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000393 if (style()->direction() == LTR) {
394 if (totWidth > availableWidth && trailingSpaceRun)
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000395 trailingSpaceRun->m_box->setLogicalWidth(max(0, trailingSpaceRun->m_box->logicalWidth() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000396 } else {
397 if (trailingSpaceRun)
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000398 trailingSpaceRun->m_box->setLogicalWidth(0);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000399 else if (totWidth > availableWidth)
400 x -= (totWidth - availableWidth);
401 }
hyattffe78712003-02-11 01:59:29 +0000402 break;
403 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000404 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
405 if (trailingSpaceRun) {
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000406 totWidth -= trailingSpaceRun->m_box->logicalWidth();
407 trailingSpaceRun->m_box->setLogicalWidth(0);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000408 }
hyattffe78712003-02-11 01:59:29 +0000409 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000410 }
hyattffe78712003-02-11 01:59:29 +0000411 // fall through
412 case TAAUTO:
413 numSpaces = 0;
414 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000415 if (style()->direction() == LTR) {
416 if (totWidth > availableWidth && trailingSpaceRun)
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000417 trailingSpaceRun->m_box->setLogicalWidth(max(0, trailingSpaceRun->m_box->logicalWidth() - totWidth + availableWidth));
hyattffe78712003-02-11 01:59:29 +0000418 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000419 }
hyattffe78712003-02-11 01:59:29 +0000420 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000421 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000422 // Wide lines spill out of the block based off direction.
423 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
424 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000425 if (style()->direction() == LTR) {
426 if (trailingSpaceRun) {
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000427 totWidth -= trailingSpaceRun->m_box->logicalWidth();
428 trailingSpaceRun->m_box->setLogicalWidth(0);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000429 }
430 if (totWidth < availableWidth)
431 x += availableWidth - totWidth;
432 } else {
433 if (totWidth > availableWidth && trailingSpaceRun) {
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000434 trailingSpaceRun->m_box->setLogicalWidth(max(0, trailingSpaceRun->m_box->logicalWidth() - totWidth + availableWidth));
435 totWidth -= trailingSpaceRun->m_box->logicalWidth();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000436 } else
437 x += availableWidth - totWidth;
438 }
hyattffe78712003-02-11 01:59:29 +0000439 break;
440 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000441 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000442 int trailingSpaceWidth = 0;
443 if (trailingSpaceRun) {
hyatt@apple.comc01df9e2010-09-23 19:17:33 +0000444 totWidth -= trailingSpaceRun->m_box->logicalWidth();
445 trailingSpaceWidth = min(trailingSpaceRun->m_box->logicalWidth(), (availableWidth - totWidth + 1) / 2);
446 trailingSpaceRun->m_box->setLogicalWidth(max(0, trailingSpaceWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000447 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000448 if (style()->direction() == LTR)
449 x += max((availableWidth - totWidth) / 2, 0);
450 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000451 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000452 break;
453 }
454
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000455 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000456 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000457 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000458 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000459
hyattacbb0d42003-04-25 01:32:49 +0000460 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000461 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000462 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000463 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000464 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000465 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000466 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000467 spaces++;
darina3c48282003-10-07 15:49:30 +0000468 }
hyatt870bdda2003-05-21 23:37:33 +0000469
darinb53ebdc2006-07-09 15:10:21 +0000470 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000471
hyattdca76e92005-11-02 08:52:50 +0000472 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000473 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000474 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000475 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000476 totWidth += spaceAdd;
477 }
hyattacbb0d42003-04-25 01:32:49 +0000478 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000479 if (!numSpaces)
480 break;
hyattacbb0d42003-04-25 01:32:49 +0000481 }
hyattffe78712003-02-11 01:59:29 +0000482 }
hyattffe78712003-02-11 01:59:29 +0000483 }
mitz@apple.come1364202008-02-28 01:06:41 +0000484
hyattffe78712003-02-11 01:59:29 +0000485 // The widths of all runs are now known. We can now place every inline box (and
486 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000487 needsWordSpacing = false;
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000488 lineBox->placeBoxesInInlineDirection(x, needsWordSpacing, textBoxDataMap);
hyattffe78712003-02-11 01:59:29 +0000489}
490
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000491void RenderBlock::computeBlockDirectionPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
hyattffe78712003-02-11 01:59:29 +0000492{
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000493 setHeight(lineBox->alignBoxesInBlockDirection(height(), textBoxDataMap));
hyatt@apple.comd885df72009-01-22 02:31:52 +0000494 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000495
496 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000497 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000498 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000499 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000500 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000501
hyatt98ee7e42003-05-14 01:39:15 +0000502 // Align positioned boxes with the top of the line box. This is
503 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000504 if (r->m_object->isPositioned())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000505 r->m_box->setY(height());
hyatt98ee7e42003-05-14 01:39:15 +0000506
507 // Position is used to properly position both replaced elements and
508 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000509 if (r->m_object->isText())
510 toRenderText(r->m_object)->positionLineBox(r->m_box);
511 else if (r->m_object->isBox())
512 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000513 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000514 // Positioned objects and zero-length text nodes destroy their boxes in
515 // position(), which unnecessarily dirties the line.
516 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000517}
kociendabb0c24b2001-08-24 14:24:40 +0000518
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000519static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
520{
521 if (character == ' ' || character == '\t' || character == softHyphen)
522 return true;
523 if (character == '\n')
524 return !renderer->style()->preserveNewline();
525 if (character == noBreakSpace)
526 return renderer->style()->nbspMode() == SPACE;
527 return false;
528}
529
jamesr@google.com19548ad2010-04-02 23:21:35 +0000530void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
531{
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000532 bool useRepaintBounds = false;
533
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000534 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000535
hyatt@apple.comd885df72009-01-22 02:31:52 +0000536 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000537 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000538
hyatt0c3a9862004-02-23 21:26:26 +0000539 // Figure out if we should clear out our line boxes.
540 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000541 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000542 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000543 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000544
hyatted77ad82004-06-15 07:21:23 +0000545 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
546 // clip.
547 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
548 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
549 // anyway, so we won't worry about following the draft here.
550 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000551
hyatted77ad82004-06-15 07:21:23 +0000552 // Walk all the lines and delete our ellipsis line boxes if they exist.
553 if (hasTextOverflow)
554 deleteEllipsisLineBoxes();
555
hyattffe78712003-02-11 01:59:29 +0000556 if (firstChild()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000557 // layout replaced elements
558 bool endOfInline = false;
559 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000560 Vector<FloatWithRect> floats;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000561 bool hasInlineChild = false;
562 while (o) {
commit-queue@webkit.orgcf45df92010-09-14 13:25:06 +0000563 if (!hasInlineChild && o->isInline())
564 hasInlineChild = true;
565
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000566 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
567 RenderBox* box = toRenderBox(o);
568
569 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
570 o->setChildNeedsLayout(true, false);
571
572 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
573 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
hyatt@apple.com75dad742010-09-24 18:07:44 +0000574 o->setPreferredLogicalWidthsDirty(true, false);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000575
576 if (o->isPositioned())
577 o->containingBlock()->insertPositionedObject(box);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000578 else if (o->isFloating())
579 floats.append(FloatWithRect(box));
580 else if (fullLayout || o->needsLayout()) {
581 // Replaced elements
582 toRenderBox(o)->dirtyLineBoxes(fullLayout);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000583 o->layoutIfNeeded();
584 }
585 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000586 if (fullLayout || o->selfNeedsLayout())
587 dirtyLineBoxesForRenderer(o, fullLayout);
588 o->setNeedsLayout(false);
589 if (!o->isText())
590 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
591 }
592 o = bidiNext(this, o, 0, false, &endOfInline);
593 }
594
595 // We want to skip ahead to the first dirty line
596 InlineBidiResolver resolver;
597 unsigned floatIndex;
598 bool firstLine = true;
599 bool previousLineBrokeCleanly = true;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000600 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex,
601 useRepaintBounds, repaintTop, repaintBottom);
mitz@apple.come1364202008-02-28 01:06:41 +0000602
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000603 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000604 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
605 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000606 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000607 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000608 // Because we waited until we were already inside layout to discover
609 // that the block really needed a full layout, we missed our chance to repaint the layer
610 // before layout started. Luckily the layer has cached the repaint rect for its original
611 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000612 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000613 }
614 }
hyatt0c3a9862004-02-23 21:26:26 +0000615
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000616 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
617
618 LineMidpointState& lineMidpointState = resolver.midpointState();
619
620 // We also find the first clean line and extract these lines. We will add them back
621 // if we determine that we're able to synchronize after handling all our dirty lines.
622 InlineIterator cleanLineStart;
623 BidiStatus cleanLineBidiStatus;
624 int endLineYPos = 0;
625 RootInlineBox* endLine = (fullLayout || !startLine) ?
626 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
627
628 if (startLine) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000629 if (!useRepaintBounds) {
630 useRepaintBounds = true;
631 repaintTop = height();
632 repaintBottom = height();
633 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000634 RenderArena* arena = renderArena();
635 RootInlineBox* box = startLine;
636 while (box) {
637 repaintTop = min(repaintTop, box->topVisibleOverflow());
638 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
639 RootInlineBox* next = box->nextRootBox();
640 box->deleteLine(arena);
641 box = next;
642 }
643 }
644
645 InlineIterator end = resolver.position();
646
647 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
648 // If the last line before the start line ends with a line break that clear floats,
649 // adjust the height accordingly.
650 // A line break can be either the first or the last object on a line, depending on its direction.
651 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
652 RenderObject* lastObject = lastLeafChild->renderer();
653 if (!lastObject->isBR())
654 lastObject = lastRootBox()->firstLeafChild()->renderer();
655 if (lastObject->isBR()) {
656 EClear clear = lastObject->style()->clear();
657 if (clear != CNONE)
658 newLine(clear);
659 }
660 }
661 }
662
663 bool endLineMatched = false;
664 bool checkForEndLineMatch = endLine;
665 bool checkForFloatsFromLastLine = false;
666
667 bool isLineEmpty = true;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000668 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000669
670 while (!end.atEnd()) {
671 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
672 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
673 break;
674
675 lineMidpointState.reset();
676
677 isLineEmpty = true;
678
679 EClear clear = CNONE;
mitz@apple.comb2107652010-06-21 16:54:52 +0000680 bool hyphenated;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000681
682 InlineIterator oldEnd = end;
683 FloatingObject* lastFloatFromPreviousLine = m_floatingObjects ? m_floatingObjects->last() : 0;
684 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, hyphenated, &clear, lastFloatFromPreviousLine);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000685 if (resolver.position().atEnd()) {
686 resolver.deleteRuns();
687 checkForFloatsFromLastLine = true;
688 break;
689 }
690 ASSERT(end != resolver.position());
691
692 if (!isLineEmpty) {
mitz@apple.com62d5bbd2010-06-25 18:07:01 +0000693 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000694 ASSERT(resolver.position() == end);
695
696 BidiRun* trailingSpaceRun = 0;
697 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
698 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
699 trailingSpaceRun = resolver.logicallyLastRun();
700 RenderObject* lastObject = trailingSpaceRun->m_object;
701 if (lastObject->isText()) {
702 RenderText* lastText = toRenderText(lastObject);
703 const UChar* characters = lastText->characters();
704 int firstSpace = trailingSpaceRun->stop();
705 while (firstSpace > trailingSpaceRun->start()) {
706 UChar current = characters[firstSpace - 1];
707 if (!isCollapsibleSpace(current, lastText))
708 break;
709 firstSpace--;
710 }
711 if (firstSpace == trailingSpaceRun->stop())
712 trailingSpaceRun = 0;
713 else {
714 TextDirection direction = style()->direction();
715 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
716 if (firstSpace != trailingSpaceRun->start()) {
717 BidiContext* baseContext = resolver.context();
718 while (BidiContext* parent = baseContext->parent())
719 baseContext = parent;
720
721 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
722 trailingSpaceRun->m_stop = firstSpace;
723 if (direction == LTR)
724 resolver.addRun(newTrailingRun);
725 else
726 resolver.prependRun(newTrailingRun);
727 trailingSpaceRun = newTrailingRun;
728 shouldReorder = false;
729 }
730 if (shouldReorder) {
731 if (direction == LTR) {
732 resolver.moveRunToEnd(trailingSpaceRun);
733 trailingSpaceRun->m_level = 0;
734 } else {
735 resolver.moveRunToBeginning(trailingSpaceRun);
736 trailingSpaceRun->m_level = 1;
737 }
738 }
739 }
740 } else
741 trailingSpaceRun = 0;
742 }
743
744 // Now that the runs have been ordered, we create the line boxes.
745 // At the same time we figure out where border/padding/margin should be applied for
746 // inline flow boxes.
747
748 RootInlineBox* lineBox = 0;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000749 int oldHeight = height();
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000750 if (resolver.runCount()) {
mitz@apple.comb2107652010-06-21 16:54:52 +0000751 if (hyphenated)
752 resolver.logicallyLastRun()->m_hasHyphen = true;
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000753 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
754 if (lineBox) {
755 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
756
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000757#if ENABLE(SVG)
758 bool isSVGRootInlineBox = lineBox->isSVGRootInlineBox();
759#else
760 bool isSVGRootInlineBox = false;
761#endif
762
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000763 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000764
765 // Now we position all of our text runs horizontally.
766 if (!isSVGRootInlineBox)
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000767 computeInlineDirectionPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000768
769 // Now position our text runs vertically.
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000770 computeBlockDirectionPositionsForLine(lineBox, resolver.firstRun(), textBoxDataMap);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000771
772#if ENABLE(SVG)
zimmermann@webkit.orgb40815c2010-06-18 11:18:44 +0000773 // SVG text layout code computes vertical & horizontal positions on its own.
774 // Note that we still need to execute computeVerticalPositionsForLine() as
775 // it calls InlineTextBox::positionLineBox(), which tracks whether the box
776 // contains reversed text or not. If we wouldn't do that editing and thus
777 // text selection in RTL boxes would not work as expected.
778 if (isSVGRootInlineBox) {
779 ASSERT(isSVGText());
780 static_cast<SVGRootInlineBox*>(lineBox)->computePerCharacterLayoutInformation();
781 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000782#endif
783
784#if PLATFORM(MAC)
785 // Highlight acts as an overflow inflation.
786 if (style()->highlight() != nullAtom)
787 lineBox->addHighlightOverflow();
788#endif
789 }
790 }
791
792 resolver.deleteRuns();
793
794 if (lineBox) {
795 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
796 if (useRepaintBounds) {
797 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
798 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
799 }
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000800
801 if (paginated) {
802 int adjustment = 0;
803 adjustLinePositionForPagination(lineBox, adjustment);
804 if (adjustment) {
805 int oldLineWidth = lineWidth(oldHeight, firstLine);
806 lineBox->adjustPosition(0, adjustment);
807 if (useRepaintBounds) // This can only be a positive adjustment, so no need to update repaintTop.
808 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
809
810 if (lineWidth(oldHeight + adjustment, firstLine) != oldLineWidth) {
811 // We have to delete this line, remove all floats that got added, and let line layout re-run.
812 lineBox->deleteLine(renderArena());
813 removeFloatingObjectsBelow(lastFloatFromPreviousLine, oldHeight);
814 setHeight(oldHeight + adjustment);
815 resolver.setPosition(oldEnd);
816 end = oldEnd;
817 continue;
818 }
819
820 setHeight(lineBox->blockHeight());
821 }
822 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000823 }
824
825 firstLine = false;
826 newLine(clear);
827 }
828
829 if (m_floatingObjects && lastRootBox()) {
830 if (lastFloat) {
831 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
832 }
833 m_floatingObjects->next();
834 } else
835 m_floatingObjects->first();
836 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
837 lastRootBox()->floats().append(f->m_renderer);
838 ASSERT(f->m_renderer == floats[floatIndex].object);
839 // If a float's geometry has changed, give up on syncing with clean lines.
840 if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
841 checkForEndLineMatch = false;
842 floatIndex++;
843 }
844 lastFloat = m_floatingObjects->last();
845 }
846
847 lineMidpointState.reset();
848 resolver.setPosition(end);
849 }
850
851 if (endLine) {
852 if (endLineMatched) {
853 // Attach all the remaining lines, and then adjust their y-positions as needed.
854 int delta = height() - endLineYPos;
855 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
856 line->attachLine();
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000857 if (paginated) {
858 delta -= line->paginationStrut();
859 adjustLinePositionForPagination(line, delta);
860 }
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000861 if (delta) {
862 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
863 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
864 line->adjustPosition(0, delta);
865 }
866 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
867 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
868 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
869 int floatTop = (*f)->y() - (*f)->marginTop();
870 insertFloatingObject(*f);
871 setHeight(floatTop + delta);
872 positionNewFloats();
873 }
874 }
875 }
876 setHeight(lastRootBox()->blockHeight());
877 } else {
878 // Delete all the remaining lines.
879 RootInlineBox* line = endLine;
880 RenderArena* arena = renderArena();
881 while (line) {
882 repaintTop = min(repaintTop, line->topVisibleOverflow());
883 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
884 RootInlineBox* next = line->nextRootBox();
885 line->deleteLine(arena);
886 line = next;
887 }
888 }
889 }
890 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
891 // In case we have a float on the last line, it might not be positioned up to now.
892 // This has to be done before adding in the bottom border/padding, or the float will
893 // include the padding incorrectly. -dwh
894 if (checkForFloatsFromLastLine) {
895 int bottomVisualOverflow = lastRootBox()->bottomVisualOverflow();
896 int bottomLayoutOverflow = lastRootBox()->bottomLayoutOverflow();
dbates@webkit.orgf6f05a92010-05-17 04:58:25 +0000897 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000898 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
899 trailingFloatsLineBox->setConstructed();
mitz@apple.com0cc7bed2010-05-17 01:43:49 +0000900 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
hyatt@apple.com44e83c02010-09-23 22:21:30 +0000901 trailingFloatsLineBox->alignBoxesInBlockDirection(height(), textBoxDataMap);
902 trailingFloatsLineBox->setBlockDirectionOverflowPositions(height(), bottomLayoutOverflow, height(), bottomVisualOverflow, 0);
abarth@webkit.org31d23df2010-04-05 21:52:09 +0000903 trailingFloatsLineBox->setBlockHeight(height());
904 }
905 if (lastFloat) {
906 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
907 }
908 m_floatingObjects->next();
909 } else
910 m_floatingObjects->first();
911 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
912 lastRootBox()->floats().append(f->m_renderer);
913 lastFloat = m_floatingObjects->last();
914 }
915 size_t floatCount = floats.size();
916 // Floats that did not have layout did not repaint when we laid them out. They would have
917 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
918 // painted.
919 for (size_t i = 0; i < floatCount; ++i) {
920 if (!floats[i].everHadLayout) {
921 RenderBox* f = floats[i].object;
922 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
923 f->repaint();
924 }
925 }
kociendabb0c24b2001-08-24 14:24:40 +0000926 }
hyatt85586af2003-02-19 23:22:42 +0000927
hyatta70560a2002-11-20 01:53:20 +0000928 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +0000929 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +0000930
adele7a470a72006-04-20 22:22:14 +0000931 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000932 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +0000933
934 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
935 // truncate text.
936 if (hasTextOverflow)
937 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +0000938}
939
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000940RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000941 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats,
942 bool& useRepaintBounds, int& repaintTop, int& repaintBottom)
hyatt0c3a9862004-02-23 21:26:26 +0000943{
944 RootInlineBox* curr = 0;
945 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000946
mitz@apple.com40547b32008-03-18 04:04:34 +0000947 bool dirtiedByFloat = false;
948 if (!fullLayout) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000949 // Paginate all of the clean lines.
950 bool paginated = view()->layoutState() && view()->layoutState()->isPaginated();
951 int paginationDelta = 0;
mitz@apple.com40547b32008-03-18 04:04:34 +0000952 size_t floatIndex = 0;
953 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000954 if (paginated) {
955 paginationDelta -= curr->paginationStrut();
956 adjustLinePositionForPagination(curr, paginationDelta);
957 if (paginationDelta) {
958 if (containsFloats() || !floats.isEmpty()) {
959 // FIXME: Do better eventually. For now if we ever shift because of pagination and floats are present just go to a full layout.
960 fullLayout = true;
961 break;
962 }
963
964 if (!useRepaintBounds)
965 useRepaintBounds = true;
966
967 repaintTop = min(repaintTop, curr->topVisibleOverflow() + min(paginationDelta, 0));
968 repaintBottom = max(repaintBottom, curr->bottomVisibleOverflow() + max(paginationDelta, 0));
969 curr->adjustPosition(0, paginationDelta);
970 }
971 }
972
hyatt@apple.comd885df72009-01-22 02:31:52 +0000973 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
974 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
975 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
976 RenderBox* f = *o;
hyatt@apple.comcc1737c2010-09-16 20:20:02 +0000977 f->layoutIfNeeded();
978 IntSize newSize(f->width() + f->marginLeft() + f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
mitz@apple.com40547b32008-03-18 04:04:34 +0000979 ASSERT(floatIndex < floats.size());
980 if (floats[floatIndex].object != f) {
981 // A new float has been inserted before this line or before its last known float.
982 // Just do a full layout.
983 fullLayout = true;
984 break;
985 }
986 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +0000987 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +0000988 curr->markDirty();
mitz@apple.com1d4e9532010-01-18 20:55:25 +0000989 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()), curr);
mitz@apple.com40547b32008-03-18 04:04:34 +0000990 floats[floatIndex].rect.setSize(newSize);
991 dirtiedByFloat = true;
992 }
993 floatIndex++;
994 }
995 }
996 if (dirtiedByFloat || fullLayout)
997 break;
998 }
999 // Check if a new float has been inserted after the last known float.
1000 if (!curr && floatIndex < floats.size())
1001 fullLayout = true;
1002 }
1003
hyatt0c3a9862004-02-23 21:26:26 +00001004 if (fullLayout) {
1005 // Nuke all our lines.
1006 if (firstRootBox()) {
1007 RenderArena* arena = renderArena();
1008 curr = firstRootBox();
1009 while (curr) {
1010 RootInlineBox* next = curr->nextRootBox();
1011 curr->deleteLine(arena);
1012 curr = next;
1013 }
darinec375482007-01-06 01:36:24 +00001014 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001015 }
eseidel789896f2005-11-27 22:52:09 +00001016 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001017 if (curr) {
1018 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001019 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001020 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001021 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001022 // The previous line didn't break cleanly or broke at a newline
1023 // that has been deleted, so treat it as dirty too.
1024 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001025 }
eseidel789896f2005-11-27 22:52:09 +00001026 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001027 // No dirty lines were found.
1028 // If the last line didn't break cleanly, treat it as dirty.
1029 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1030 curr = lastRootBox();
1031 }
mitz@apple.come1364202008-02-28 01:06:41 +00001032
hyatt0c3a9862004-02-23 21:26:26 +00001033 // If we have no dirty lines, then last is just the last root box.
1034 last = curr ? curr->prevRootBox() : lastRootBox();
1035 }
mitz@apple.come1364202008-02-28 01:06:41 +00001036
mitz@apple.com40547b32008-03-18 04:04:34 +00001037 numCleanFloats = 0;
1038 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001039 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +00001040 // Restore floats from clean lines.
1041 RootInlineBox* line = firstRootBox();
1042 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001043 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1044 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1045 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001046 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001047 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +00001048 positionNewFloats();
1049 ASSERT(floats[numCleanFloats].object == *f);
1050 numCleanFloats++;
1051 }
1052 }
1053 line = line->nextRootBox();
1054 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001055 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001056 }
1057
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001058 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001059 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001060
1061 RenderObject* startObj;
1062 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001063 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001064 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001065 startObj = last->lineBreakObj();
1066 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001067 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001068 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001069 bool ltr = style()->direction() == LTR
1070 #if ENABLE(SVG)
1071 || (style()->unicodeBidi() == UBNormal && isSVGText())
1072 #endif
1073 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001074
mitz@apple.com45d9e102009-04-27 16:24:55 +00001075 Direction direction = ltr ? LeftToRight : RightToLeft;
1076 resolver.setLastStrongDir(direction);
1077 resolver.setLastDir(direction);
1078 resolver.setEorDir(direction);
1079 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001080
mitz@apple.com15035e62008-07-05 20:44:44 +00001081 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001082 }
mitz@apple.come1364202008-02-28 01:06:41 +00001083
mitz@apple.com15035e62008-07-05 20:44:44 +00001084 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001085
hyatt0c3a9862004-02-23 21:26:26 +00001086 return curr;
1087}
1088
mitz@apple.com15035e62008-07-05 20:44:44 +00001089RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001090{
1091 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001092 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001093 last = 0;
1094 else {
hyatt04420ca2004-07-16 00:05:42 +00001095 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1096 if (curr->isDirty())
1097 last = 0;
1098 else if (!last)
1099 last = curr;
1100 }
hyatt0c3a9862004-02-23 21:26:26 +00001101 }
mitz@apple.come1364202008-02-28 01:06:41 +00001102
hyatt0c3a9862004-02-23 21:26:26 +00001103 if (!last)
1104 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001105
eseidel789896f2005-11-27 22:52:09 +00001106 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001107 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001108 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001109 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001110
hyatt0c3a9862004-02-23 21:26:26 +00001111 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1112 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1113 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001114
hyatt0c3a9862004-02-23 21:26:26 +00001115 return last;
1116}
1117
mitz@apple.com15035e62008-07-05 20:44:44 +00001118bool 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 +00001119{
mitz@apple.com15035e62008-07-05 20:44:44 +00001120 if (resolver.position() == endLineStart) {
1121 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001122 return false;
1123
hyatt@apple.comd885df72009-01-22 02:31:52 +00001124 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001125 if (!delta || !m_floatingObjects)
1126 return true;
1127
1128 // 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 +00001129 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001130
1131 RootInlineBox* lastLine = endLine;
1132 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1133 lastLine = nextLine;
1134
1135 int bottom = lastLine->blockHeight() + abs(delta);
1136
1137 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001138 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001139 return false;
1140 }
1141
1142 return true;
1143 }
hyatt0c3a9862004-02-23 21:26:26 +00001144
mitz@apple.come1364202008-02-28 01:06:41 +00001145 // The first clean line doesn't match, but we can check a handful of following lines to try
1146 // to match back up.
1147 static int numLines = 8; // The # of lines we're willing to match against.
1148 RootInlineBox* line = endLine;
1149 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001150 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001151 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001152 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001153 return false; // ...but the bidi state doesn't match.
1154 RootInlineBox* result = line->nextRootBox();
1155
1156 // Set our yPos to be the block height of endLine.
1157 if (result)
1158 endYPos = line->blockHeight();
1159
hyatt@apple.comd885df72009-01-22 02:31:52 +00001160 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001161 if (delta && m_floatingObjects) {
1162 // 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 +00001163 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001164
1165 RootInlineBox* lastLine = endLine;
1166 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1167 lastLine = nextLine;
1168
1169 int bottom = lastLine->blockHeight() + abs(delta);
1170
1171 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001172 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001173 return false;
1174 }
1175 }
1176
mitz@apple.come1364202008-02-28 01:06:41 +00001177 // Now delete the lines that we failed to sync.
1178 RootInlineBox* boxToDelete = endLine;
1179 RenderArena* arena = renderArena();
1180 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001181 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1182 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001183 RootInlineBox* next = boxToDelete->nextRootBox();
1184 boxToDelete->deleteLine(arena);
1185 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001186 }
mitz@apple.come1364202008-02-28 01:06:41 +00001187
1188 endLine = result;
1189 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001190 }
1191 }
mitz@apple.come1364202008-02-28 01:06:41 +00001192
hyatt0c3a9862004-02-23 21:26:26 +00001193 return false;
1194}
1195
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001196static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001197{
darinf9e5d6c2007-01-09 14:54:26 +00001198 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001199 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001200
hyattdca76e92005-11-02 08:52:50 +00001201 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1202 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001203 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001204 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1205 // |true|).
1206 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001207 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001208
kocienda498d1982004-10-15 21:07:24 +00001209 return true;
kocienda98440082004-10-14 23:51:47 +00001210}
1211
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001212static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001213{
1214 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1215}
1216
zimmermannac3781f2007-02-04 01:25:03 +00001217static inline bool shouldPreserveNewline(RenderObject* object)
1218{
mjsd2948ef2007-02-26 19:29:04 +00001219#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001220 if (object->isSVGInlineText())
zimmermannac3781f2007-02-04 01:25:03 +00001221 return false;
1222#endif
1223
1224 return object->style()->preserveNewline();
1225}
1226
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001227static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001228{
1229 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001230 // We need to fix this, though, because at the very least, inlines containing only
1231 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001232 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001233}
1234
hyatt@apple.com71eeb442010-02-11 20:05:51 +00001235bool RenderBlock::requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001236{
bdakinf876bee2007-10-30 05:27:09 +00001237 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001238 return false;
bdakinf876bee2007-10-30 05:27:09 +00001239
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001240 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001241 return false;
1242
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001243 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001244 return true;
1245
1246 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001247 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1248 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001249}
1250
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001251bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001252{
1253 ASSERT(inlineObj->parent() == this);
1254
mitz@apple.com15035e62008-07-05 20:44:44 +00001255 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001256 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001257 it.increment();
bdashccffb432007-07-13 11:51:40 +00001258
1259 return !it.atEnd();
1260}
1261
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001262// 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 +00001263// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1264// elements quite right. In other words, we need to build this function's work into the normal line
1265// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001266// NB. this function will insert any floating elements that would otherwise
1267// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001268void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001269{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001270 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001271 RenderObject* object = iterator.obj;
1272 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001273 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001274 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001275 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1276 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001277 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001278 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001279 // A relative positioned inline encloses us. In this case, we also have to determine our
1280 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1281 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001282 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1283 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001284 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001285
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001286 RenderBox* box = toRenderBox(object);
1287 if (box->style()->hasStaticX()) {
1288 if (box->style()->isOriginalDisplayInlineType())
1289 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001290 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001291 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001292 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001293
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001294 if (box->style()->hasStaticY())
1295 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001296 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001297 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001298 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001299}
bdashccffb432007-07-13 11:51:40 +00001300
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001301int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly,
1302 FloatingObject* lastFloatFromPreviousLine)
mitz@apple.com1a301772008-03-11 18:30:36 +00001303{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001304 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001305 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001306 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001307 if (object->isFloating()) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001308 positionNewFloatOnLine(insertFloatingObject(toRenderBox(object)), lastFloatFromPreviousLine);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001309 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001310 } else if (object->isPositioned()) {
1311 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1312 // will work for the common cases
1313 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001314 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001315 // A relative positioned inline encloses us. In this case, we also have to determine our
1316 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1317 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001318 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1319 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001320 }
1321
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001322 RenderBox* box = toRenderBox(object);
1323 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001324 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001325 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001326 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001327 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001328 }
1329
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001330 if (box->style()->hasStaticY())
1331 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001332 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001333 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001334 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001335 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001336 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001337}
1338
bdakinf876bee2007-10-30 05:27:09 +00001339// This is currently just used for list markers and inline flows that have line boxes. Neither should
1340// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001341static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001342{
mitz@apple.com1a301772008-03-11 18:30:36 +00001343 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001344 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1345 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001346 UChar nextChar = nextText->characters()[0];
1347 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001348 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001349 return true;
1350 }
1351 }
1352
1353 return false;
1354}
1355
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001356void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001357{
1358 ASSERT(widthToFit > availableWidth);
1359
1360 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001361 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001362 int newLineWidth = availableWidth;
1363 while (true) {
1364 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1365 if (!floatBottom)
1366 break;
1367
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001368 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001369 lastFloatBottom = floatBottom;
1370 if (newLineWidth >= widthToFit)
1371 break;
1372 }
1373
1374 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001375 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001376 availableWidth = newLineWidth;
1377 }
1378}
1379
mitz@apple.com34106442009-02-01 06:23:39 +00001380static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1381{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001382 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001383 return text->width(from, len, font, xPos);
1384 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1385}
1386
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001387static 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 +00001388{
1389 const AtomicString& hyphenString = text->style()->hyphenString();
1390 int hyphenWidth = font.width(TextRun(hyphenString.characters(), hyphenString.length()));
1391
mitz@apple.com7c67b292010-09-12 23:04:16 +00001392 int maxPrefixWidth = availableWidth - xPos - hyphenWidth - lastSpaceWordSpacing;
1393 // If the maximum width available for the prefix before the hyphen is small, then it is very unlikely
1394 // that an hyphenation opportunity exists, so do not bother to look for it.
1395 if (maxPrefixWidth <= font.pixelSize() * 5 / 4)
1396 return;
1397
1398 unsigned prefixLength = font.offsetForPosition(TextRun(text->characters() + lastSpace, pos - lastSpace, !collapseWhiteSpace, xPos + lastSpaceWordSpacing), maxPrefixWidth, false);
mitz@apple.comb2107652010-06-21 16:54:52 +00001399 if (!prefixLength)
1400 return;
1401
mitz@apple.com2ef44912010-08-07 21:41:06 +00001402 prefixLength = lastHyphenLocation(text->characters() + lastSpace, pos - lastSpace, prefixLength + 1, localeIdentifier);
1403 if (!prefixLength)
mitz@apple.comb2107652010-06-21 16:54:52 +00001404 return;
1405
1406#if !ASSERT_DISABLED
1407 int prefixWidth = hyphenWidth + textWidth(text, lastSpace, prefixLength, font, xPos, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1408 ASSERT(xPos + prefixWidth <= availableWidth);
mitz@apple.com34b43c72010-06-21 17:21:22 +00001409#else
1410 UNUSED_PARAM(isFixedPitch);
mitz@apple.comb2107652010-06-21 16:54:52 +00001411#endif
1412
1413 lineBreak.obj = text;
1414 lineBreak.pos = lastSpace + prefixLength;
1415 lineBreak.nextBreakablePosition = nextBreakable;
1416 hyphenated = true;
1417}
1418
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001419InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001420 bool& hyphenated, EClear* clear, FloatingObject* lastFloatFromPreviousLine)
kociendae40cb942004-10-05 20:05:38 +00001421{
mitz@apple.com15035e62008-07-05 20:44:44 +00001422 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001423
mitz@apple.com15035e62008-07-05 20:44:44 +00001424 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001425 LineMidpointState& lineMidpointState = resolver.midpointState();
1426
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001427 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, lastFloatFromPreviousLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001428
kociendae40cb942004-10-05 20:05:38 +00001429 int w = 0;
1430 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001431
mitz@apple.com15035e62008-07-05 20:44:44 +00001432 if (resolver.position().atEnd())
1433 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001434
hyatt33f8d492002-11-12 21:44:52 +00001435 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1436 // or not we are currently ignoring whitespace.
1437 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001438 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001439
1440 // This variable tracks whether the very last character we saw was a space. We use
1441 // this to detect when we encounter a second space so we know we have to terminate
1442 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001443 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001444 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001445 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001446
mitz@apple.com15035e62008-07-05 20:44:44 +00001447 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001448
mitz@apple.com15035e62008-07-05 20:44:44 +00001449 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001450 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001451 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001452 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001453 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001454
hyatt0c3a9862004-02-23 21:26:26 +00001455 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1456 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001457
mitz@apple.comb2107652010-06-21 16:54:52 +00001458 hyphenated = false;
1459
ddkilzer5d01fa22007-01-29 03:10:37 +00001460 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001461 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001462
hyatt@apple.com69340902008-01-16 21:24:21 +00001463 // 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 +00001464 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001465 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
hyatt@apple.comce8ee2a2010-08-27 20:29:34 +00001466 bool allowImagesToBreak = !document()->inQuirksMode() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
hyatt@apple.com69340902008-01-16 21:24:21 +00001467
hyattb0d9f602007-01-15 01:28:23 +00001468 EWhiteSpace currWS = style()->whiteSpace();
1469 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001470 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001471 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1472 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1473
1474 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001475 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001476
mjsd2948ef2007-02-26 19:29:04 +00001477#if ENABLE(SVG)
zimmermann@webkit.orge943aaa2010-06-25 10:03:33 +00001478 bool preserveNewline = o->isSVGInlineText() ? false : RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001479#else
hyattb0d9f602007-01-15 01:28:23 +00001480 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001481#endif
1482
hyattb0d9f602007-01-15 01:28:23 +00001483 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1484
hyatt275d0702005-11-03 23:53:57 +00001485 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001486 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001487 lBreak.obj = o;
1488 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001489 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001490 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001491
hyatt33f8d492002-11-12 21:44:52 +00001492 // A <br> always breaks a line, so don't let the line be collapsed
1493 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001494 // get collapsed away. It only does this if the previous line broke
1495 // cleanly. Otherwise the <br> has no effect on whether the line is
1496 // empty or not.
1497 if (prevLineBrokeCleanly)
1498 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001499 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001500 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001501
mitz@apple.com71e30842008-03-18 16:13:31 +00001502 if (!isLineEmpty && clear)
1503 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001504 }
1505 goto end;
1506 }
hyattb0d9f602007-01-15 01:28:23 +00001507
hyatt275d0702005-11-03 23:53:57 +00001508 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001509 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001510 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001511 RenderBox* floatBox = toRenderBox(o);
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001512 FloatingObject* f = insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001513 // check if it fits in the current line.
1514 // If it does, position it now, otherwise, position
1515 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001516 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt@apple.comcc1737c2010-09-16 20:20:02 +00001517 positionNewFloatOnLine(f, lastFloatFromPreviousLine);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001518 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001519 } else
1520 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001521 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001522 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001523 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001524 RenderBox* box = toRenderBox(o);
1525 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1526 bool needToSetStaticX = box->style()->hasStaticX();
1527 if (box->style()->hasStaticX() && !isInlineType) {
1528 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001529 borderLeft() + paddingLeft() :
1530 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001531 needToSetStaticX = false;
1532 }
1533
1534 // If our original display was an INLINE type, then we can go ahead
1535 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001536 bool needToSetStaticY = box->style()->hasStaticY();
1537 if (box->style()->hasStaticY() && isInlineType) {
1538 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001539 needToSetStaticY = false;
1540 }
1541
hyatt853cd7d2004-11-05 02:59:48 +00001542 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1543 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001544 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001545 needToCreateLineBox = true;
1546
hyatt98ee7e42003-05-14 01:39:15 +00001547 // If we're ignoring spaces, we have to stop and include this object and
1548 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001549 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001550 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001551 ignoreStart.obj = o;
1552 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001553 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001554 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1555 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001556 }
hyatt98b16282004-03-31 18:43:12 +00001557
hyatt851816b2003-07-08 07:54:17 +00001558 }
hyatt98ee7e42003-05-14 01:39:15 +00001559 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001560 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001561 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001562 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001563
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001564 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001565
bdakinf876bee2007-10-30 05:27:09 +00001566 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1567 // to make sure that we stop to include this object and then start ignoring spaces again.
1568 // If this object is at the start of the line, we need to behave like list markers and
1569 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001570 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001571 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001572 if (ignoringSpaces) {
1573 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001574 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1575 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001576 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001577 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001578 // Like with list markers, we start ignoring spaces to make sure that any
1579 // additional spaces we see will be discarded.
1580 currentCharacterIsSpace = true;
1581 currentCharacterIsWS = true;
1582 ignoringSpaces = true;
1583 }
1584 }
1585
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001586 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1587 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001588 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001589 RenderBox* replacedBox = toRenderBox(o);
1590
hyattde396342003-10-29 08:57:20 +00001591 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001592 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001593 w += tmpW;
1594 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001595 lBreak.obj = o;
1596 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001597 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001598 }
1599
mitz@apple.combfdc9112008-02-21 19:59:40 +00001600 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001601 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001602
hyatt33f8d492002-11-12 21:44:52 +00001603 isLineEmpty = false;
1604 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001605 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001606 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001607 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001608
bdakinf876bee2007-10-30 05:27:09 +00001609 // Optimize for a common case. If we can't find whitespace after the list
1610 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001611 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001612 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001613 // Like with inline flows, we start ignoring spaces to make sure that any
1614 // additional spaces we see will be discarded.
1615 currentCharacterIsSpace = true;
1616 currentCharacterIsWS = true;
1617 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001618 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001619 if (toRenderListMarker(o)->isInside())
1620 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001621 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001622 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001623 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001624 if (!pos)
1625 appliedStartWidth = false;
1626
darin@apple.com36744d62009-01-25 20:23:04 +00001627 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001628
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001629#if ENABLE(SVG)
1630 bool isSVGText = t->isSVGInlineText();
1631#endif
1632
darin42563ac52007-01-22 17:28:57 +00001633 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001634 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001635 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001636
mitz@apple.comb2107652010-06-21 16:54:52 +00001637 RenderStyle* style = t->style(firstLine);
1638 const Font& f = style->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001639 bool isFixedPitch = f.isFixedPitch();
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001640 bool canHyphenate = style->hyphens() == HyphensAuto && WebCore::canHyphenate(style->hyphenationLocale());
weinigf28a1c32007-02-14 14:10:31 +00001641
hyatt33f8d492002-11-12 21:44:52 +00001642 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001643 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001644 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001645
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001646 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1647 // space, then subtract its width.
mitz@apple.com6ff116d2010-07-06 00:11:08 +00001648 int wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.width(TextRun(&space, 1)) + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001649
darin54008922006-01-13 16:39:05 +00001650 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001651 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001652 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1653 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1654 // 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 +00001655 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001656 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001657 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1658
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001659 if (t->isWordBreak()) {
1660 w += tmpW;
1661 tmpW = 0;
1662 lBreak.obj = o;
1663 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001664 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001665 ASSERT(!len);
1666 }
1667
hyatt275d0702005-11-03 23:53:57 +00001668 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001669 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001670 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001671 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001672 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001673
hyattb0d9f602007-01-15 01:28:23 +00001674 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001675 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001676
hyatt78b85132004-03-29 20:07:45 +00001677 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001678 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001679 if (!ignoringSpaces) {
1680 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001681 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001682 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001683 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001684 else
darin@apple.com36744d62009-01-25 20:23:04 +00001685 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001686 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001687 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1688 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1689 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001690 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001691 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001692
hyatt78b85132004-03-29 20:07:45 +00001693 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001694 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001695
hyattdca76e92005-11-02 08:52:50 +00001696 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001697 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001698 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001699 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001700
mitz@apple.com15035e62008-07-05 20:44:44 +00001701 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001702 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001703 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001704 }
mitz@apple.combe429562008-03-07 01:09:51 +00001705
hyatt78b85132004-03-29 20:07:45 +00001706 pos++;
1707 len--;
eseideld13fe532005-11-30 02:40:29 +00001708 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001709 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
mitz@apple.comb2107652010-06-21 16:54:52 +00001710 if (style->hyphens() == HyphensNone) {
1711 // Prevent a line break at the soft hyphen by ensuring that betweenWords is false
1712 // in the next iteration.
1713 atStart = true;
1714 }
hyatt78b85132004-03-29 20:07:45 +00001715 continue;
1716 }
zimmermann@webkit.org6e96afd2010-09-10 15:35:50 +00001717
1718#if ENABLE(SVG)
1719 if (isSVGText) {
1720 RenderSVGInlineText* svgInlineText = static_cast<RenderSVGInlineText*>(t);
1721 if (pos > 0) {
1722 if (svgInlineText->characterStartsNewTextChunk(pos)) {
1723 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1));
1724 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
1725 }
1726 }
1727 }
1728#endif
1729
hyatt3aff2332003-01-23 01:15:28 +00001730 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001731
darinf9e5d6c2007-01-09 14:54:26 +00001732 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001733
weiniged111c12007-07-13 22:45:11 +00001734 if ((breakAll || breakWords) && !midWordBreak) {
1735 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001736 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001737 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001738 }
darin47ece0d2005-09-04 07:42:31 +00001739
ddkilzere8759ef2007-03-25 06:28:19 +00001740 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001741
weiniged111c12007-07-13 22:45:11 +00001742 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001743 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001744 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001745 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001746 // Stop ignoring spaces and begin at this
1747 // new point.
hyatt48710d62003-08-21 09:17:13 +00001748 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001749 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001750 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001751 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001752 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001753 } else {
hyatt33f8d492002-11-12 21:44:52 +00001754 // Just keep ignoring these spaces.
1755 pos++;
1756 len--;
1757 continue;
1758 }
1759 }
rjwc9c257d2003-01-24 03:46:17 +00001760
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001761 int additionalTmpW;
1762 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1763 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1764 else
1765 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001766 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001767 if (!appliedStartWidth) {
1768 tmpW += inlineWidth(o, true, false);
1769 appliedStartWidth = true;
1770 }
1771
eseideld13fe532005-11-30 02:40:29 +00001772 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001773
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001774 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001775 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001776
hyattb0d9f602007-01-15 01:28:23 +00001777 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001778 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001779 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001780 bool lineWasTooWide = false;
1781 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001782 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001783 // Check if line is too big even without the extra space
1784 // at the end of the line. If it is not, do nothing.
1785 // If the line needs the extra whitespace to be too long,
1786 // then move the line break to the space and skip all
1787 // additional whitespace.
1788 if (w + tmpW + charWidth > width) {
1789 lineWasTooWide = true;
1790 lBreak.obj = o;
1791 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001792 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001793 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001794 }
ap932806a2006-10-01 09:06:09 +00001795 }
1796 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com67ed70a2010-06-22 22:10:10 +00001797 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001798 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 +00001799 if (hyphenated)
1800 goto end;
1801 }
eric@webkit.org1cb44402009-12-29 06:25:16 +00001802 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 +00001803 if (!stoppedIgnoringSpaces && pos > 0) {
1804 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001805 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1806 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001807 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001808 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001809 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001810 }
hyatt78b85132004-03-29 20:07:45 +00001811 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001812 } else {
weiniged111c12007-07-13 22:45:11 +00001813 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001814 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001815 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001816 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001817 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001818 }
rjwc9c257d2003-01-24 03:46:17 +00001819 }
hyatt33f8d492002-11-12 21:44:52 +00001820
hyattb0d9f602007-01-15 01:28:23 +00001821 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001822 if (!stoppedIgnoringSpaces && pos > 0) {
1823 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001824 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1825 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001826 }
hyatta9f48e32003-02-03 22:48:01 +00001827 lBreak.obj = o;
1828 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001829 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001830 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001831 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001832 return lBreak;
1833 }
hyatta9f48e32003-02-03 22:48:01 +00001834
weinigf28a1c32007-02-14 14:10:31 +00001835 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001836 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001837 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001838 tmpW = 0;
1839 lBreak.obj = o;
1840 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001841 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001842 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1843 // opportunity to break after a word.
1844 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001845 }
hyatt33f8d492002-11-12 21:44:52 +00001846
darin54008922006-01-13 16:39:05 +00001847 if (midWordBreak) {
1848 // Remember this as a breakable position in case
1849 // adding the end width forces a break.
1850 lBreak.obj = o;
1851 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001852 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001853 midWordBreak &= (breakWords || breakAll);
1854 }
1855
1856 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001857 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1858 lastSpace = pos;
1859 }
hyatt33f8d492002-11-12 21:44:52 +00001860
hyattdca76e92005-11-02 08:52:50 +00001861 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001862 // If we encounter a newline, or if we encounter a
1863 // second space, we need to go ahead and break up this
1864 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001865 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001866 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001867
hyatt33f8d492002-11-12 21:44:52 +00001868 // We just entered a mode where we are ignoring
1869 // spaces. Create a midpoint to terminate the run
1870 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001871 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001872 }
1873 }
eseidel789896f2005-11-27 22:52:09 +00001874 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001875 // Stop ignoring spaces and begin at this
1876 // new point.
1877 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001878 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001879 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001880 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001881 }
hyatt98b16282004-03-31 18:43:12 +00001882
1883 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1884 ignoreStart.obj = o;
1885 ignoreStart.pos = pos;
1886 }
harrisone343c412005-01-18 01:07:26 +00001887
1888 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001889 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001890 lBreak.obj = o;
1891 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001892 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00001893 }
1894 }
hyatt33f8d492002-11-12 21:44:52 +00001895
hyattb0d9f602007-01-15 01:28:23 +00001896 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001897 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001898 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001899 trailingSpaceObject = 0;
1900
1901 pos++;
1902 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001903 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001904 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001905
kociendabb0c24b2001-08-24 14:24:40 +00001906 // IMPORTANT: pos is > length here!
mitz@apple.comb2107652010-06-21 16:54:52 +00001907 int additionalTmpW = ignoringSpaces ? 0 : textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
1908 tmpW += additionalTmpW;
weinigf28a1c32007-02-14 14:10:31 +00001909 tmpW += inlineWidth(o, !appliedStartWidth, true);
mitz@apple.comb2107652010-06-21 16:54:52 +00001910
1911 if (canHyphenate && w + tmpW > width) {
mitz@apple.com73eb8c92010-08-04 21:22:24 +00001912 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 +00001913 if (hyphenated)
1914 goto end;
1915 }
kociendabb0c24b2001-08-24 14:24:40 +00001916 } else
weinigf28a1c32007-02-14 14:10:31 +00001917 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001918
mitz@apple.com1a301772008-03-11 18:30:36 +00001919 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001920 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001921 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00001922 checkForBreak = true;
1923 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00001924 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00001925 if (currentCharacterIsSpace)
1926 checkForBreak = true;
1927 else {
harrison07b5e582005-08-15 23:31:16 +00001928 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00001929 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001930 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00001931 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00001932 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00001933 // If the next item on the line is text, and if we did not end with
1934 // a space, then the next text run continues our word (and so it needs to
1935 // keep adding to |tmpW|. Just update and continue.
1936 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001937 } else if (nextText->isWordBreak())
1938 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001939 bool willFitOnLine = w + tmpW <= width;
1940 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001941 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001942 willFitOnLine = tmpW <= width;
1943 }
ddkilzere8759ef2007-03-25 06:28:19 +00001944 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00001945 if (canPlaceOnLine && checkForBreak) {
1946 w += tmpW;
1947 tmpW = 0;
1948 lBreak.obj = next;
1949 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001950 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00001951 }
1952 }
1953 }
1954 }
1955
darin54008922006-01-13 16:39:05 +00001956 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00001957 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00001958 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00001959 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001960
1961 if (w)
1962 goto end;
1963
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001964 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00001965
hyatta14d1742003-01-02 20:25:46 +00001966 // |width| may have been adjusted because we got shoved down past a float (thus
1967 // giving us more room), so we need to retest, and only jump to
1968 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00001969 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00001970 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00001971 }
hyatt1d9e29b2003-04-10 01:48:53 +00001972
mitz@apple.com1a301772008-03-11 18:30:36 +00001973 if (!o->isFloatingOrPositioned()) {
1974 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00001975 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001976 w += tmpW;
1977 tmpW = 0;
1978 lBreak.obj = next;
1979 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001980 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001981 }
hyatt711fe232002-11-20 21:25:14 +00001982 }
1983
mitz@apple.com1a301772008-03-11 18:30:36 +00001984 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001985 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001986
hyatta9f48e32003-02-03 22:48:01 +00001987 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
1988 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00001989 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00001990 currentCharacterIsSpace = false;
1991
kociendabb0c24b2001-08-24 14:24:40 +00001992 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00001993 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00001994 }
1995
hyattb0d9f602007-01-15 01:28:23 +00001996
1997 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00001998 lBreak.obj = 0;
1999 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002000 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002001 }
2002
2003 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00002004 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002005 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002006 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002007 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002008 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002009 lBreak.obj = o;
2010 lBreak.pos = pos - 1;
2011 } else {
2012 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002013 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002014 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002015 }
hyatt275d0702005-11-03 23:53:57 +00002016 } else if (lBreak.obj) {
yuzo@google.comc25f62f2010-02-09 09:16:36 +00002017 // Don't ever break in the middle of a word if we can help it.
2018 // There's no room at all. We just have to be on this line,
2019 // even though we'll spill out.
2020 lBreak.obj = o;
2021 lBreak.pos = pos;
2022 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002023 }
2024 }
2025
2026 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002027 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002028 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002029
hyattfe99c872003-07-31 22:25:29 +00002030 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002031 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00002032
hyatt33f8d492002-11-12 21:44:52 +00002033 if (trailingSpaceObject) {
2034 // This object is either going to be part of the last midpoint, or it is going
2035 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2036 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00002037 if (lineMidpointState.numMidpoints % 2) {
2038 InlineIterator* midpoints = lineMidpointState.midpoints.data();
2039 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002040 }
2041 //else if (lBreak.pos > 0)
2042 // lBreak.pos--;
2043 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002044 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00002045 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002046 unsigned length = text->textLength();
2047 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00002048 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002049 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00002050 }
2051 }
rjwc9c257d2003-01-24 03:46:17 +00002052
mjs54b64002003-04-02 02:59:02 +00002053 // We might have made lBreak an iterator that points past the end
2054 // of the object. Do this adjustment to make it point to the start
2055 // of the next object instead to avoid confusing the rest of the
2056 // code.
2057 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002058 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002059 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002060 }
2061
hyatt78b85132004-03-29 20:07:45 +00002062 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2063 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2064 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002065 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00002066 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002067 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00002068 }
2069
kociendabb0c24b2001-08-24 14:24:40 +00002070 return lBreak;
2071}
2072
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002073void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002074{
hyattb4b20872004-10-20 21:34:01 +00002075 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002076 addLayoutOverflow(curr->layoutOverflowRect());
2077 if (!hasOverflowClip())
2078 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00002079 }
2080}
2081
hyatted77ad82004-06-15 07:21:23 +00002082void RenderBlock::deleteEllipsisLineBoxes()
2083{
hyatted77ad82004-06-15 07:21:23 +00002084 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002085 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002086}
2087
2088void RenderBlock::checkLinesForTextOverflow()
2089{
2090 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002091 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2092 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002093 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002094 const Font& firstLineFont = firstLineStyle()->font();
2095 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002096 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2097 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002098
2099 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2100 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2101 // check the left edge of the line box to see if it is less
2102 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2103 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002104 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002105 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2106 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.comc01df9e2010-09-23 19:17:33 +00002107 int lineBoxEdge = ltr ? curr->x() + curr->logicalWidth() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002108 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002109 // 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 +00002110 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2111 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2112 // space.
2113 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002114 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002115 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002116 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002117 }
2118 }
2119}
2120
hyattffe78712003-02-11 01:59:29 +00002121}