blob: 066ad8dfc8fcc978294e97c9398932a825a291b3 [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"
eseidel3a6d1322006-01-09 03:14:50 +000027#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000028#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000029#include "RenderArena.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000030#include "RenderInline.h"
mjsd26b2972007-02-13 13:09:04 +000031#include "RenderListMarker.h"
hyattd8048342006-05-31 01:48:18 +000032#include "RenderView.h"
mitz@apple.com1d4e9532010-01-18 20:55:25 +000033#include "TrailingFloatsRootInlineBox.h"
darin36d11362006-04-11 16:30:21 +000034#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000035#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000036#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000037#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000038#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000039
darin7bd70952006-04-13 07:07:34 +000040using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000041using namespace WTF;
42using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000043
darinb9481ed2006-03-20 02:57:59 +000044namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000045
hyatt1d5d87b2007-04-24 04:55:54 +000046// We don't let our line box tree for a single line get any deeper than this.
47const unsigned cMaxLineDepth = 200;
48
mitz@apple.com15035e62008-07-05 20:44:44 +000049class InlineIterator {
pewtermoosecf72e2d2007-07-20 19:01:55 +000050public:
mitz@apple.com15035e62008-07-05 20:44:44 +000051 InlineIterator()
pewtermoosecf72e2d2007-07-20 19:01:55 +000052 : block(0)
53 , obj(0)
54 , pos(0)
mitz@apple.comd17dc392008-09-15 18:48:20 +000055 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000056 {
57 }
58
mitz@apple.com15035e62008-07-05 20:44:44 +000059 InlineIterator(RenderBlock* b, RenderObject* o, unsigned p)
pewtermoosecf72e2d2007-07-20 19:01:55 +000060 : block(b)
61 , obj(o)
62 , pos(p)
mitz@apple.comd17dc392008-09-15 18:48:20 +000063 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000064 {
65 }
66
mitz@apple.com15035e62008-07-05 20:44:44 +000067 void increment(InlineBidiResolver* resolver = 0);
mjsfe301d72003-10-02 18:46:18 +000068 bool atEnd() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000069
darin7ab31092006-05-10 04:59:57 +000070 UChar current() const;
mitz@apple.com4c1ff322009-07-13 00:54:12 +000071 Direction direction() const;
hyatt275d0702005-11-03 23:53:57 +000072
73 RenderBlock* block;
74 RenderObject* obj;
mitz@apple.com1a301772008-03-11 18:30:36 +000075 unsigned pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +000076 int nextBreakablePosition;
mjsfe301d72003-10-02 18:46:18 +000077};
78
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000079static int getBorderPaddingMargin(RenderBoxModelObject* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000080{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000081 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
82 if (leftSide)
83 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
84 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000085}
86
87static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
88{
hyatt1d5d87b2007-04-24 04:55:54 +000089 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000090 int extraWidth = 0;
91 RenderObject* parent = child->parent();
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000092 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyatt@apple.com40232f82009-02-04 04:26:08 +000093 if (start && !child->previousSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000094 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), false);
hyatt@apple.com40232f82009-02-04 04:26:08 +000095 if (end && !child->nextSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000096 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), true);
hyattffe78712003-02-11 01:59:29 +000097 child = parent;
98 parent = child->parent();
99 }
100 return extraWidth;
101}
102
mitz@apple.com4c1ff322009-07-13 00:54:12 +0000103struct BidiRun : BidiCharacterRun {
104 BidiRun(int start, int stop, RenderObject* object, BidiContext* context, Direction dir)
105 : BidiCharacterRun(start, stop, context, dir)
106 , m_object(object)
107 , m_box(0)
108 {
109 }
110
111 void destroy();
112
113 // Overloaded new operator.
114 void* operator new(size_t, RenderArena*) throw();
115
116 // Overridden to prevent the normal delete from being called.
117 void operator delete(void*, size_t);
118
119 BidiRun* next() { return static_cast<BidiRun*>(m_next); }
120
121private:
122 // The normal operator new is disallowed.
123 void* operator new(size_t) throw();
124
125public:
126 RenderObject* m_object;
127 InlineBox* m_box;
128};
129
darin35355e52002-12-20 09:19:00 +0000130#ifndef NDEBUG
mitz@apple.com4c1ff322009-07-13 00:54:12 +0000131static RefCountedLeakCounter bidiRunCounter("BidiRun");
ggarenec11e5b2007-02-25 02:14:54 +0000132
harrison0012ced2005-10-06 18:37:42 +0000133static bool inBidiRunDestroy;
hyattffe78712003-02-11 01:59:29 +0000134#endif
135
mitz@apple.come1364202008-02-28 01:06:41 +0000136void BidiRun::destroy()
hyattffe78712003-02-11 01:59:29 +0000137{
138#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000139 inBidiRunDestroy = true;
hyattffe78712003-02-11 01:59:29 +0000140#endif
mitz@apple.come1364202008-02-28 01:06:41 +0000141 RenderArena* renderArena = m_object->renderArena();
hyattffe78712003-02-11 01:59:29 +0000142 delete this;
143#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000144 inBidiRunDestroy = false;
hyattffe78712003-02-11 01:59:29 +0000145#endif
146
147 // Recover the size left there for us by operator delete and free the memory.
mitz@apple.come1364202008-02-28 01:06:41 +0000148 renderArena->free(*reinterpret_cast<size_t*>(this), this);
hyattffe78712003-02-11 01:59:29 +0000149}
150
151void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
152{
ggarenec11e5b2007-02-25 02:14:54 +0000153#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000154 bidiRunCounter.increment();
ggarenec11e5b2007-02-25 02:14:54 +0000155#endif
hyattffe78712003-02-11 01:59:29 +0000156 return renderArena->allocate(sz);
157}
158
159void BidiRun::operator delete(void* ptr, size_t sz)
160{
ggarenec11e5b2007-02-25 02:14:54 +0000161#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000162 bidiRunCounter.decrement();
ggarenec11e5b2007-02-25 02:14:54 +0000163#endif
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000164 ASSERT(inBidiRunDestroy);
hyattffe78712003-02-11 01:59:29 +0000165
harrisone8363b42005-10-06 00:54:06 +0000166 // Stash size where destroy() can find it.
hyattffe78712003-02-11 01:59:29 +0000167 *(size_t*)ptr = sz;
168}
169
kociendabb0c24b2001-08-24 14:24:40 +0000170// ---------------------------------------------------------------------
171
mitz@apple.com15035e62008-07-05 20:44:44 +0000172inline bool operator==(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000173{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000174 return it1.pos == it2.pos && it1.obj == it2.obj;
darinb70665a2002-04-15 23:43:21 +0000175}
176
mitz@apple.com15035e62008-07-05 20:44:44 +0000177inline bool operator!=(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000178{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000179 return it1.pos != it2.pos || it1.obj != it2.obj;
darinb70665a2002-04-15 23:43:21 +0000180}
181
mitz@apple.com15035e62008-07-05 20:44:44 +0000182static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, InlineBidiResolver* resolver = 0, bool skipInlines = true, bool* endOfInlinePtr = 0)
mjs6f821c82002-03-22 00:31:57 +0000183{
hyatt275d0702005-11-03 23:53:57 +0000184 RenderObject* next = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +0000185 bool oldEndOfInline = endOfInlinePtr ? *endOfInlinePtr : false;
186 bool endOfInline = false;
hyattffe78712003-02-11 01:59:29 +0000187
hyatt275d0702005-11-03 23:53:57 +0000188 while (current) {
sullivanabd4d032007-02-09 22:51:41 +0000189 next = 0;
hyatt@apple.com40232f82009-02-04 04:26:08 +0000190 if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned() && !current->isText()) {
hyattffe78712003-02-11 01:59:29 +0000191 next = current->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000192 if (next && resolver && next->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000193 EUnicodeBidi ub = next->style()->unicodeBidi();
hyatt275d0702005-11-03 23:53:57 +0000194 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000195 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000196 Direction d = (ub == Embed
197 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
198 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000199 resolver->embed(d);
hyattffe78712003-02-11 01:59:29 +0000200 }
201 }
202 }
hyatt275d0702005-11-03 23:53:57 +0000203
hyattffe78712003-02-11 01:59:29 +0000204 if (!next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000205 if (!skipInlines && !oldEndOfInline && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000206 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000207 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000208 break;
209 }
mjs6f821c82002-03-22 00:31:57 +0000210
hyatt275d0702005-11-03 23:53:57 +0000211 while (current && current != block) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000212 if (resolver && current->isRenderInline() && current->style()->unicodeBidi() != UBNormal)
mitz@apple.com15035e62008-07-05 20:44:44 +0000213 resolver->embed(PopDirectionalFormat);
hyatt275d0702005-11-03 23:53:57 +0000214
darindde01502005-12-18 22:55:35 +0000215 next = current->nextSibling();
216 if (next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000217 if (resolver && next->isRenderInline()) {
darindde01502005-12-18 22:55:35 +0000218 EUnicodeBidi ub = next->style()->unicodeBidi();
219 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000220 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000221 Direction d = (ub == Embed
222 ? (dir == RTL ? RightToLeftEmbedding: LeftToRightEmbedding)
223 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000224 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000225 }
226 }
227 break;
228 }
229
hyattffe78712003-02-11 01:59:29 +0000230 current = current->parent();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000231 if (!skipInlines && current && current != block && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000232 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000233 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000234 break;
235 }
236 }
237 }
mjs6f821c82002-03-22 00:31:57 +0000238
hyatt275d0702005-11-03 23:53:57 +0000239 if (!next)
240 break;
hyattffe78712003-02-11 01:59:29 +0000241
mitz@apple.combfdc9112008-02-21 19:59:40 +0000242 if (next->isText() || next->isFloating() || next->isReplaced() || next->isPositioned()
hyattffe78712003-02-11 01:59:29 +0000243 || ((!skipInlines || !next->firstChild()) // Always return EMPTY inlines.
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000244 && next->isRenderInline()))
mjs6f821c82002-03-22 00:31:57 +0000245 break;
246 current = next;
247 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000248
249 if (endOfInlinePtr)
250 *endOfInlinePtr = endOfInline;
251
mjs6f821c82002-03-22 00:31:57 +0000252 return next;
253}
254
mitz@apple.com15035e62008-07-05 20:44:44 +0000255static RenderObject* bidiFirst(RenderBlock* block, InlineBidiResolver* resolver, bool skipInlines = true)
mjs6f821c82002-03-22 00:31:57 +0000256{
hyatt275d0702005-11-03 23:53:57 +0000257 if (!block->firstChild())
258 return 0;
259
260 RenderObject* o = block->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000261 if (o->isRenderInline()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000262 if (resolver) {
darindde01502005-12-18 22:55:35 +0000263 EUnicodeBidi ub = o->style()->unicodeBidi();
264 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000265 TextDirection dir = o->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000266 Direction d = (ub == Embed
267 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
268 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000269 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000270 }
271 }
hyattffe78712003-02-11 01:59:29 +0000272 if (skipInlines && o->firstChild())
mitz@apple.com15035e62008-07-05 20:44:44 +0000273 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000274 else {
275 // Never skip empty inlines.
276 if (resolver)
277 resolver->commitExplicitEmbedding();
278 return o;
279 }
hyattffe78712003-02-11 01:59:29 +0000280 }
hyattc5334f12003-08-08 22:26:08 +0000281
mitz@apple.combfdc9112008-02-21 19:59:40 +0000282 if (o && !o->isText() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
mitz@apple.com15035e62008-07-05 20:44:44 +0000283 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000284
285 if (resolver)
286 resolver->commitExplicitEmbedding();
mjs6f821c82002-03-22 00:31:57 +0000287 return o;
288}
289
mitz@apple.com15035e62008-07-05 20:44:44 +0000290inline void InlineIterator::increment(InlineBidiResolver* resolver)
kociendabb0c24b2001-08-24 14:24:40 +0000291{
hyatt275d0702005-11-03 23:53:57 +0000292 if (!obj)
293 return;
294 if (obj->isText()) {
kociendabb0c24b2001-08-24 14:24:40 +0000295 pos++;
darin@apple.com36744d62009-01-25 20:23:04 +0000296 if (pos >= toRenderText(obj)->textLength()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000297 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000298 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000299 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000300 }
301 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +0000302 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000303 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000304 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000305 }
306}
307
mitz@apple.com1a301772008-03-11 18:30:36 +0000308template<>
mitz@apple.com15035e62008-07-05 20:44:44 +0000309inline void InlineBidiResolver::increment()
mitz@apple.com1a301772008-03-11 18:30:36 +0000310{
311 current.increment(this);
312}
313
mitz@apple.com15035e62008-07-05 20:44:44 +0000314inline bool InlineIterator::atEnd() const
kociendabb0c24b2001-08-24 14:24:40 +0000315{
hyatt275d0702005-11-03 23:53:57 +0000316 return !obj;
kociendabb0c24b2001-08-24 14:24:40 +0000317}
318
mitz@apple.com15035e62008-07-05 20:44:44 +0000319inline UChar InlineIterator::current() const
kociendabb0c24b2001-08-24 14:24:40 +0000320{
hyatt30586b42003-12-02 23:19:11 +0000321 if (!obj || !obj->isText())
darin7ab31092006-05-10 04:59:57 +0000322 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000323
darin@apple.com36744d62009-01-25 20:23:04 +0000324 RenderText* text = toRenderText(obj);
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000325 if (pos >= text->textLength())
darin7ab31092006-05-10 04:59:57 +0000326 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000327
darin42563ac52007-01-22 17:28:57 +0000328 return text->characters()[pos];
kociendabb0c24b2001-08-24 14:24:40 +0000329}
330
mitz@apple.com15035e62008-07-05 20:44:44 +0000331ALWAYS_INLINE Direction InlineIterator::direction() const
kociendabb0c24b2001-08-24 14:24:40 +0000332{
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000333 if (UChar c = current())
334 return Unicode::direction(c);
335
336 if (obj && obj->isListMarker())
darinf9e5d6c2007-01-09 14:54:26 +0000337 return obj->style()->direction() == LTR ? LeftToRight : RightToLeft;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000338
339 return OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000340}
341
kociendabb0c24b2001-08-24 14:24:40 +0000342// -------------------------------------------------------------------------------------------------
343
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000344static void chopMidpointsAt(LineMidpointState& lineMidpointState, RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +0000345{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000346 if (!lineMidpointState.numMidpoints)
hyatt275d0702005-11-03 23:53:57 +0000347 return;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000348 InlineIterator* midpoints = lineMidpointState.midpoints.data();
349 for (int i = lineMidpointState.numMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000350 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +0000351 if (point.obj == obj && point.pos == pos) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000352 lineMidpointState.numMidpoints = i;
hyatt78b85132004-03-29 20:07:45 +0000353 break;
354 }
355 }
356}
357
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000358static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000359{
360 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000361 // shave it off the list, and shave off a trailing space if the previous end point doesn't
362 // preserve whitespace.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000363 if (lBreak.obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
364 InlineIterator* midpoints = lineMidpointState.midpoints.data();
365 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
366 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000367 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000368 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000369 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000370 if (currpoint == lBreak) {
371 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000372 lineMidpointState.numMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000373 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000374 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000375 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000376 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000377 if (endpoint.pos + 1 < textObj->textLength()) {
378 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000379 return;
380 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000381 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000382 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000383 return;
384 }
hyattee1bcae2004-03-29 21:36:04 +0000385 }
hyattfe99c872003-07-31 22:25:29 +0000386 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000387 }
hyattfe99c872003-07-31 22:25:29 +0000388 }
389 }
390}
391
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000392static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000393{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000394 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
395 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000396
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000397 InlineIterator* midpoints = lineMidpointState.midpoints.data();
398 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000399}
400
mitz@apple.com15035e62008-07-05 20:44:44 +0000401static void appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000402{
hyatt98ee7e42003-05-14 01:39:15 +0000403 if (start > end || obj->isFloating() ||
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000404 (obj->isPositioned() && !obj->style()->hasStaticX() && !obj->style()->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000405 return;
hyatt85586af2003-02-19 23:22:42 +0000406
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000407 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000408 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000409 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000410 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000411 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
412 if (lineMidpointState.betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000413 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000414 return;
415 // This is a new start point. Stop ignoring objects and
416 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000417 lineMidpointState.betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000418 start = nextMidpoint.pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000419 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000420 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000421 return appendRunsForObject(start, end, obj, resolver);
422 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000423 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000424 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000425 return;
426 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000427
hyatt78b85132004-03-29 20:07:45 +0000428 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000429 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000430 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000431 lineMidpointState.betweenMidpoints = true;
432 lineMidpointState.currentMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000433 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 +0000434 if (static_cast<int>(nextMidpoint.pos + 1) > start)
435 resolver.addRun(new (obj->renderArena())
436 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
437 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000438 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000439 } else
440 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000441 }
442}
443
pewtermoosecf72e2d2007-07-20 19:01:55 +0000444template <>
mitz@apple.com15035e62008-07-05 20:44:44 +0000445void InlineBidiResolver::appendRun()
kociendabb0c24b2001-08-24 14:24:40 +0000446{
mitz@apple.com6970fb72008-07-03 21:37:53 +0000447 if (!emptyRun && !eor.atEnd()) {
448 int start = sor.pos;
449 RenderObject *obj = sor.obj;
450 while (obj && obj != eor.obj && obj != endOfLine.obj) {
451 appendRunsForObject(start, obj->length(), obj, *this);
452 start = 0;
453 obj = bidiNext(sor.block, obj);
eseidel789896f2005-11-27 22:52:09 +0000454 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000455 if (obj) {
456 unsigned pos = obj == eor.obj ? eor.pos : UINT_MAX;
457 if (obj == endOfLine.obj && endOfLine.pos <= pos) {
458 reachedEndOfLine = true;
459 pos = endOfLine.pos;
460 }
461 // It's OK to add runs for zero-length RenderObjects, just don't make the run larger than it should be
462 int end = obj->length() ? pos+1 : 0;
463 appendRunsForObject(start, end, obj, *this);
464 }
465
466 eor.increment();
467 sor = eor;
justing5b0e0422005-08-01 03:20:49 +0000468 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000469
pewtermoosecf72e2d2007-07-20 19:01:55 +0000470 m_direction = OtherNeutral;
471 m_status.eor = OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000472}
473
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000474static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
475{
476 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000477 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000478
479 if (obj->isText()) {
480 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
481 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
482 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
483 if (obj->isBR())
484 textBox->setIsText(isOnlyRun || obj->document()->inStrictMode());
485 return textBox;
486 }
487
488 if (obj->isBox())
489 return toRenderBox(obj)->createInlineBox();
490
eric@webkit.org49b9d952009-07-03 01:29:07 +0000491 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000492}
493
494static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
495{
496 if (o->isText()) {
497 if (o->prefWidthsDirty() && o->isCounter())
498 toRenderText(o)->calcPrefWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
499 toRenderText(o)->dirtyLineBoxes(fullLayout);
500 } else
501 toRenderInline(o)->dirtyLineBoxes(fullLayout);
502}
503
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000504InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000505{
506 // See if we have an unconstructed line box for this object that is also
507 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000508 unsigned lineDepth = 1;
509 InlineFlowBox* childBox = 0;
510 InlineFlowBox* parentBox = 0;
511 InlineFlowBox* result = 0;
512 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000513 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000514
hyatt1d5d87b2007-04-24 04:55:54 +0000515 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000516 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000517
hyatt1d5d87b2007-04-24 04:55:54 +0000518 // If this box is constructed then it is from a previous line, and we need
519 // to make a new box for our line. If this box is unconstructed but it has
520 // something following it on the line, then we know we have to make a new box
521 // as well. In this situation our inline has actually been split in two on
522 // the same line (this can happen with very fancy language mixtures).
523 bool constructedNewBox = false;
524 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
525 // We need to make a new box for this render object. Once
526 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000527 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000528 ASSERT(newBox->isInlineFlowBox());
529 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000530 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000531 constructedNewBox = true;
532 }
mitz@apple.come1364202008-02-28 01:06:41 +0000533
hyatt1d5d87b2007-04-24 04:55:54 +0000534 if (!result)
535 result = parentBox;
536
537 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000538 // inline box for the line, and it doesn't have to be appended to any parent
539 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000540 if (childBox)
541 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000542
hyatt1d5d87b2007-04-24 04:55:54 +0000543 if (!constructedNewBox || obj == this)
544 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000545
hyatt1d5d87b2007-04-24 04:55:54 +0000546 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000547
hyatt1d5d87b2007-04-24 04:55:54 +0000548 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
549 // intermediate inline flows.
550 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000551
hyatt1d5d87b2007-04-24 04:55:54 +0000552 } while (true);
553
554 return result;
hyattffe78712003-02-11 01:59:29 +0000555}
556
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000557RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000558{
mitz@apple.com887f3592008-02-25 22:03:08 +0000559 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000560
561 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000562 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000563 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000564 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000565 if (runCount == 2 && !r->m_object->isListMarker())
566 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000567
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000568 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000569 r->m_box = box;
570
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000571 ASSERT(box);
572 if (!box)
573 continue;
hyattffe78712003-02-11 01:59:29 +0000574
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000575 // If we have no parent box yet, or if the run is not simply a sibling,
576 // then we need to construct inline boxes as necessary to properly enclose the
577 // run's inline box.
578 if (!parentBox || parentBox->renderer() != r->m_object->parent())
579 // Create new inline boxes all the way back to the appropriate insertion point.
580 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000581
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000582 // Append the inline box to this line.
583 parentBox->addToLine(box);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000584
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000585 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
586 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
587
588 if (box->isInlineTextBox()) {
589 InlineTextBox* text = static_cast<InlineTextBox*>(box);
590 text->setStart(r->m_start);
591 text->setLen(r->m_stop - r->m_start);
592 text->m_dirOverride = r->dirOverride(visuallyOrdered);
hyatt0c3a9862004-02-23 21:26:26 +0000593 }
hyattffe78712003-02-11 01:59:29 +0000594 }
595
596 // We should have a root inline box. It should be unconstructed and
597 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000598 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000599
600 // Set bits on our inline flow boxes that indicate which sides should
601 // paint borders/margins/padding. This knowledge will ultimately be used when
602 // we determine the horizontal positions and widths of all the inline boxes on
603 // the line.
hyattffe78712003-02-11 01:59:29 +0000604 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
605
606 // Now mark the line boxes as being constructed.
607 lastLineBox()->setConstructed();
608
609 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000610 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000611}
612
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000613void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd)
hyattffe78712003-02-11 01:59:29 +0000614{
615 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000616 int availableWidth = lineWidth(height(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000617 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000618 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000619 unsigned numSpaces = 0;
620 ETextAlign textAlign = style()->textAlign();
621
mitz@apple.come1364202008-02-28 01:06:41 +0000622 for (BidiRun* r = firstRun; r; r = r->next()) {
623 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000624 continue; // Positioned objects are only participating to figure out their
625 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000626 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000627 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000628 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000629
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000630 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000631 const UChar* characters = rt->characters();
632 for (int i = r->m_start; i < r->m_stop; i++) {
633 UChar c = characters[i];
634 if (c == ' ' || c == '\n' || c == '\t')
635 numSpaces++;
636 }
637 }
638
mitz@apple.come1364202008-02-28 01:06:41 +0000639 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000640 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000641 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000642 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000643 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000644 HashSet<const SimpleFontData*> fallbackFonts;
645 r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, firstLine, &fallbackFonts));
646 if (!fallbackFonts.isEmpty()
647#if ENABLE(SVG)
648 && !isSVGText()
649#endif
650 ) {
651 ASSERT(r->m_box->isText());
652 static_cast<InlineTextBox*>(r->m_box)->setFallbackFonts(fallbackFonts);
653 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000654 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000655 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000656 renderBox->calcWidth();
657 r->m_box->setWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000658 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000659 }
hyatt4b381692003-03-10 21:11:59 +0000660
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000661 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000662 }
663
664 // Armed with the total width of the line (without justification),
665 // we now examine our text-align property in order to determine where to position the
666 // objects horizontally. The total width of the line can be increased if we end up
667 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000668 int x = leftOffset(height(), firstLine);
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000669 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000670 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000671 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000672 // The direction of the block should determine what happens with wide lines. In
673 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000674 if (style()->direction() == LTR) {
675 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000676 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000677 } else {
678 if (trailingSpaceRun)
679 trailingSpaceRun->m_box->setWidth(0);
680 else if (totWidth > availableWidth)
681 x -= (totWidth - availableWidth);
682 }
hyattffe78712003-02-11 01:59:29 +0000683 break;
684 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000685 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
686 if (trailingSpaceRun) {
687 totWidth -= trailingSpaceRun->m_box->width();
688 trailingSpaceRun->m_box->setWidth(0);
689 }
hyattffe78712003-02-11 01:59:29 +0000690 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000691 }
hyattffe78712003-02-11 01:59:29 +0000692 // fall through
693 case TAAUTO:
694 numSpaces = 0;
695 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000696 if (style()->direction() == LTR) {
697 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000698 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
hyattffe78712003-02-11 01:59:29 +0000699 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000700 }
hyattffe78712003-02-11 01:59:29 +0000701 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000702 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000703 // Wide lines spill out of the block based off direction.
704 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
705 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000706 if (style()->direction() == LTR) {
707 if (trailingSpaceRun) {
708 totWidth -= trailingSpaceRun->m_box->width();
709 trailingSpaceRun->m_box->setWidth(0);
710 }
711 if (totWidth < availableWidth)
712 x += availableWidth - totWidth;
713 } else {
714 if (totWidth > availableWidth && trailingSpaceRun) {
mitz@apple.comcbf51272009-04-07 17:08:18 +0000715 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000716 totWidth -= trailingSpaceRun->m_box->width();
717 } else
718 x += availableWidth - totWidth;
719 }
hyattffe78712003-02-11 01:59:29 +0000720 break;
721 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000722 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000723 int trailingSpaceWidth = 0;
724 if (trailingSpaceRun) {
725 totWidth -= trailingSpaceRun->m_box->width();
726 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
mitz@apple.comcbf51272009-04-07 17:08:18 +0000727 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000728 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000729 if (style()->direction() == LTR)
730 x += max((availableWidth - totWidth) / 2, 0);
731 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000732 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000733 break;
734 }
735
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000736 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000737 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000738 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000739 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000740
hyattacbb0d42003-04-25 01:32:49 +0000741 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000742 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000743 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000744 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000745 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000746 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000747 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000748 spaces++;
darina3c48282003-10-07 15:49:30 +0000749 }
hyatt870bdda2003-05-21 23:37:33 +0000750
darinb53ebdc2006-07-09 15:10:21 +0000751 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000752
hyattdca76e92005-11-02 08:52:50 +0000753 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000754 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000755 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000756 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000757 totWidth += spaceAdd;
758 }
hyattacbb0d42003-04-25 01:32:49 +0000759 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000760 if (!numSpaces)
761 break;
hyattacbb0d42003-04-25 01:32:49 +0000762 }
hyattffe78712003-02-11 01:59:29 +0000763 }
hyattffe78712003-02-11 01:59:29 +0000764 }
mitz@apple.come1364202008-02-28 01:06:41 +0000765
hyattffe78712003-02-11 01:59:29 +0000766 // The widths of all runs are now known. We can now place every inline box (and
767 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000768 needsWordSpacing = false;
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000769 lineBox->placeBoxesHorizontally(x, needsWordSpacing);
hyattffe78712003-02-11 01:59:29 +0000770}
771
mitz@apple.com887f3592008-02-25 22:03:08 +0000772void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun)
hyattffe78712003-02-11 01:59:29 +0000773{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000774 setHeight(lineBox->verticallyAlignBoxes(height()));
775 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000776
777 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000778 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000779 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000780 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000781 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000782
hyatt98ee7e42003-05-14 01:39:15 +0000783 // Align positioned boxes with the top of the line box. This is
784 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000785 if (r->m_object->isPositioned())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000786 r->m_box->setY(height());
hyatt98ee7e42003-05-14 01:39:15 +0000787
788 // Position is used to properly position both replaced elements and
789 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000790 if (r->m_object->isText())
791 toRenderText(r->m_object)->positionLineBox(r->m_box);
792 else if (r->m_object->isBox())
793 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000794 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000795 // Positioned objects and zero-length text nodes destroy their boxes in
796 // position(), which unnecessarily dirties the line.
797 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000798}
kociendabb0c24b2001-08-24 14:24:40 +0000799
800// collects one line of the paragraph and transforms it to visual order
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000801void RenderBlock::bidiReorderLine(InlineBidiResolver& resolver, const InlineIterator& end, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +0000802{
mitz@apple.com1a301772008-03-11 18:30:36 +0000803 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
hyatt4b381692003-03-10 21:11:59 +0000804}
kociendabb0c24b2001-08-24 14:24:40 +0000805
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000806static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
807{
808 if (character == ' ' || character == '\t' || character == softHyphen)
809 return true;
810 if (character == '\n')
811 return !renderer->style()->preserveNewline();
812 if (character == noBreakSpace)
813 return renderer->style()->nbspMode() == SPACE;
814 return false;
815}
816
apddd2ff42007-03-31 08:26:24 +0000817void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
kociendabb0c24b2001-08-24 14:24:40 +0000818{
apddd2ff42007-03-31 08:26:24 +0000819 bool useRepaintBounds = false;
hyatt7b41b9d2007-05-30 05:32:40 +0000820
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000821 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000822
hyatt@apple.comd885df72009-01-22 02:31:52 +0000823 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000824 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000825
hyatt0c3a9862004-02-23 21:26:26 +0000826 // Figure out if we should clear out our line boxes.
827 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000828 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000829 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000830 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000831
hyatted77ad82004-06-15 07:21:23 +0000832 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
833 // clip.
834 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
835 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
836 // anyway, so we won't worry about following the draft here.
837 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000838
hyatted77ad82004-06-15 07:21:23 +0000839 // Walk all the lines and delete our ellipsis line boxes if they exist.
840 if (hasTextOverflow)
841 deleteEllipsisLineBoxes();
842
hyattffe78712003-02-11 01:59:29 +0000843 if (firstChild()) {
kociendabb0c24b2001-08-24 14:24:40 +0000844 // layout replaced elements
hyattffe78712003-02-11 01:59:29 +0000845 bool endOfInline = false;
mitz@apple.com1a301772008-03-11 18:30:36 +0000846 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000847 Vector<FloatWithRect> floats;
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000848 bool hasInlineChild = false;
hyatt0c3a9862004-02-23 21:26:26 +0000849 while (o) {
850 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000851 RenderBox* box = toRenderBox(o);
852
hyatt89377f12002-12-13 21:24:40 +0000853 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
hyattadbb2ef2003-11-18 09:21:44 +0000854 o->setChildNeedsLayout(true, false);
hyatt567533d2007-04-25 21:46:21 +0000855
856 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
857 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
858 o->setPrefWidthsDirty(true, false);
859
hyatt3ac01352003-03-22 01:37:33 +0000860 if (o->isPositioned())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000861 o->containingBlock()->insertPositionedObject(box);
hyatt0c3a9862004-02-23 21:26:26 +0000862 else {
863 if (o->isFloating())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000864 floats.append(FloatWithRect(box));
hyatt0c3a9862004-02-23 21:26:26 +0000865 else if (fullLayout || o->needsLayout()) // Replaced elements
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000866 toRenderBox(o)->dirtyLineBoxes(fullLayout);
mitz@apple.com40547b32008-03-18 04:04:34 +0000867
hyatt390427a2003-05-03 18:33:42 +0000868 o->layoutIfNeeded();
hyatt0c3a9862004-02-23 21:26:26 +0000869 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000870 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000871 hasInlineChild = true;
hyatt0c3a9862004-02-23 21:26:26 +0000872 if (fullLayout || o->selfNeedsLayout())
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000873 dirtyLineBoxesForRenderer(o, fullLayout);
hyattf4fe67f2003-10-07 04:43:23 +0000874 o->setNeedsLayout(false);
hyatt@apple.com4f81f3e2009-02-10 23:00:37 +0000875 if (!o->isText())
876 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
hyattf4fe67f2003-10-07 04:43:23 +0000877 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000878 o = bidiNext(this, o, 0, false, &endOfInline);
kociendabb0c24b2001-08-24 14:24:40 +0000879 }
880
mitz@apple.com40547b32008-03-18 04:04:34 +0000881 // We want to skip ahead to the first dirty line
mitz@apple.com15035e62008-07-05 20:44:44 +0000882 InlineBidiResolver resolver;
mitz@apple.com40547b32008-03-18 04:04:34 +0000883 unsigned floatIndex;
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000884 bool firstLine = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000885 bool previousLineBrokeCleanly = true;
886 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000887
eric@webkit.orgddbec0aa2010-01-06 01:13:11 +0000888 if (fullLayout && hasInlineChild && !selfNeedsLayout()) {
hyatt0c3a9862004-02-23 21:26:26 +0000889 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
890 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000891 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000892 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000893 // Because we waited until we were already inside layout to discover
894 // that the block really needed a full layout, we missed our chance to repaint the layer
895 // before layout started. Luckily the layer has cached the repaint rect for its original
896 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000897 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000898 }
899 }
hyatt0c3a9862004-02-23 21:26:26 +0000900
mitz@apple.com40547b32008-03-18 04:04:34 +0000901 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
902
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000903 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.come1364202008-02-28 01:06:41 +0000904
hyatt0c3a9862004-02-23 21:26:26 +0000905 // We also find the first clean line and extract these lines. We will add them back
906 // if we determine that we're able to synchronize after handling all our dirty lines.
mitz@apple.com15035e62008-07-05 20:44:44 +0000907 InlineIterator cleanLineStart;
eseidel789896f2005-11-27 22:52:09 +0000908 BidiStatus cleanLineBidiStatus;
aroben201afd12007-07-01 04:53:01 +0000909 int endLineYPos = 0;
hyatt0c3a9862004-02-23 21:26:26 +0000910 RootInlineBox* endLine = (fullLayout || !startLine) ?
pewtermoosecf72e2d2007-07-20 19:01:55 +0000911 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
912
hyatt0c3a9862004-02-23 21:26:26 +0000913 if (startLine) {
apddd2ff42007-03-31 08:26:24 +0000914 useRepaintBounds = true;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000915 repaintTop = height();
916 repaintBottom = height();
hyatt0c3a9862004-02-23 21:26:26 +0000917 RenderArena* arena = renderArena();
918 RootInlineBox* box = startLine;
919 while (box) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +0000920 repaintTop = min(repaintTop, box->topVisibleOverflow());
921 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
hyatt0c3a9862004-02-23 21:26:26 +0000922 RootInlineBox* next = box->nextRootBox();
923 box->deleteLine(arena);
924 box = next;
925 }
hyatt0c3a9862004-02-23 21:26:26 +0000926 }
mitz@apple.come1364202008-02-28 01:06:41 +0000927
mitz@apple.com15035e62008-07-05 20:44:44 +0000928 InlineIterator end = resolver.position();
hyatt0c3a9862004-02-23 21:26:26 +0000929
mitz@apple.come3602dd2008-04-08 20:58:36 +0000930 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
931 // If the last line before the start line ends with a line break that clear floats,
932 // adjust the height accordingly.
933 // A line break can be either the first or the last object on a line, depending on its direction.
mitz@apple.comc7bc5992008-04-18 23:19:19 +0000934 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
hyatt@apple.com5631f112009-02-11 01:36:45 +0000935 RenderObject* lastObject = lastLeafChild->renderer();
mitz@apple.comc7bc5992008-04-18 23:19:19 +0000936 if (!lastObject->isBR())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000937 lastObject = lastRootBox()->firstLeafChild()->renderer();
mitz@apple.comc7bc5992008-04-18 23:19:19 +0000938 if (lastObject->isBR()) {
939 EClear clear = lastObject->style()->clear();
940 if (clear != CNONE)
941 newLine(clear);
942 }
mitz@apple.come3602dd2008-04-08 20:58:36 +0000943 }
944 }
mitz@apple.com40547b32008-03-18 04:04:34 +0000945
hyatt0c3a9862004-02-23 21:26:26 +0000946 bool endLineMatched = false;
mitz@apple.comeb9c42c2008-03-26 20:53:07 +0000947 bool checkForEndLineMatch = endLine;
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000948 bool checkForFloatsFromLastLine = false;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000949 int lastHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000950
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000951 bool isLineEmpty = true;
952
hyatt0c3a9862004-02-23 21:26:26 +0000953 while (!end.atEnd()) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000954 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
mitz@apple.com15035e62008-07-05 20:44:44 +0000955 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
hyatt0c3a9862004-02-23 21:26:26 +0000956 break;
957
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000958 lineMidpointState.reset();
959
hyatt33f8d492002-11-12 21:44:52 +0000960 isLineEmpty = true;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000961
mitz@apple.com71e30842008-03-18 16:13:31 +0000962 EClear clear = CNONE;
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000963 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, &clear);
mitz@apple.com15035e62008-07-05 20:44:44 +0000964 if (resolver.position().atEnd()) {
965 resolver.deleteRuns();
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000966 checkForFloatsFromLastLine = true;
eseidel789896f2005-11-27 22:52:09 +0000967 break;
ggarenec11e5b2007-02-25 02:14:54 +0000968 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000969 ASSERT(end != resolver.position());
mitz@apple.com1a301772008-03-11 18:30:36 +0000970
hyatt33f8d492002-11-12 21:44:52 +0000971 if (!isLineEmpty) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000972 bidiReorderLine(resolver, end, previousLineBrokeCleanly);
mitz@apple.com15035e62008-07-05 20:44:44 +0000973 ASSERT(resolver.position() == end);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000974
975 BidiRun* trailingSpaceRun = 0;
mitz@apple.comcbf51272009-04-07 17:08:18 +0000976 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
977 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000978 trailingSpaceRun = resolver.logicallyLastRun();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000979 RenderObject* lastObject = trailingSpaceRun->m_object;
980 if (lastObject->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000981 RenderText* lastText = toRenderText(lastObject);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000982 const UChar* characters = lastText->characters();
983 int firstSpace = trailingSpaceRun->stop();
984 while (firstSpace > trailingSpaceRun->start()) {
985 UChar current = characters[firstSpace - 1];
986 if (!isCollapsibleSpace(current, lastText))
987 break;
988 firstSpace--;
989 }
990 if (firstSpace == trailingSpaceRun->stop())
991 trailingSpaceRun = 0;
mitz@apple.com3a43c712008-04-20 04:26:28 +0000992 else {
993 TextDirection direction = style()->direction();
mitz@apple.com15035e62008-07-05 20:44:44 +0000994 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
mitz@apple.com3a43c712008-04-20 04:26:28 +0000995 if (firstSpace != trailingSpaceRun->start()) {
bdakin@apple.com01af3022009-03-11 00:05:24 +0000996 BidiContext* baseContext = resolver.context();
997 while (BidiContext* parent = baseContext->parent())
998 baseContext = parent;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000999
bdakin@apple.com01af3022009-03-11 00:05:24 +00001000 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
1001 trailingSpaceRun->m_stop = firstSpace;
1002 if (direction == LTR)
1003 resolver.addRun(newTrailingRun);
1004 else
1005 resolver.prependRun(newTrailingRun);
1006 trailingSpaceRun = newTrailingRun;
1007 shouldReorder = false;
mitz@apple.com3a43c712008-04-20 04:26:28 +00001008 }
1009 if (shouldReorder) {
mitz@apple.com8e05a142008-04-22 20:42:02 +00001010 if (direction == LTR) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001011 resolver.moveRunToEnd(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +00001012 trailingSpaceRun->m_level = 0;
1013 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +00001014 resolver.moveRunToBeginning(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +00001015 trailingSpaceRun->m_level = 1;
1016 }
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001017 }
1018 }
1019 } else
1020 trailingSpaceRun = 0;
1021 }
hyatt4b381692003-03-10 21:11:59 +00001022
1023 // Now that the runs have been ordered, we create the line boxes.
1024 // At the same time we figure out where border/padding/margin should be applied for
1025 // inline flow boxes.
hyatt4b381692003-03-10 21:11:59 +00001026
hyatt0c3a9862004-02-23 21:26:26 +00001027 RootInlineBox* lineBox = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +00001028 if (resolver.runCount()) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001029 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
hyattdfb09052003-03-11 02:58:59 +00001030 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +00001031 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
pewtermoosecf72e2d2007-07-20 19:01:55 +00001032
hyattdfb09052003-03-11 02:58:59 +00001033 // Now we position all of our text runs horizontally.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001034 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd());
mitz@apple.come1364202008-02-28 01:06:41 +00001035
hyattdfb09052003-03-11 02:58:59 +00001036 // Now position our text runs vertically.
mitz@apple.com15035e62008-07-05 20:44:44 +00001037 computeVerticalPositionsForLine(lineBox, resolver.firstRun());
hyattbddcc612006-07-24 23:16:15 +00001038
oliver98d52382007-10-12 10:47:18 +00001039#if ENABLE(SVG)
1040 // Special SVG text layout code
1041 lineBox->computePerCharacterLayoutInformation();
1042#endif
1043
hyattbddcc612006-07-24 23:16:15 +00001044#if PLATFORM(MAC)
1045 // Highlight acts as an overflow inflation.
1046 if (style()->highlight() != nullAtom)
1047 lineBox->addHighlightOverflow();
1048#endif
hyattdfb09052003-03-11 02:58:59 +00001049 }
1050 }
ggarenec11e5b2007-02-25 02:14:54 +00001051
mitz@apple.com15035e62008-07-05 20:44:44 +00001052 resolver.deleteRuns();
hyatt35a85e52003-02-14 19:43:07 +00001053
ap9059f6f2006-07-24 16:55:02 +00001054 if (lineBox) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001055 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
apddd2ff42007-03-31 08:26:24 +00001056 if (useRepaintBounds) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001057 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
1058 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
ap9059f6f2006-07-24 16:55:02 +00001059 }
1060 }
mitz@apple.come1364202008-02-28 01:06:41 +00001061
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001062 firstLine = false;
mitz@apple.com71e30842008-03-18 16:13:31 +00001063 newLine(clear);
darinb70665a2002-04-15 23:43:21 +00001064 }
mitz@apple.come1364202008-02-28 01:06:41 +00001065
mitz@apple.com7b089842008-03-22 06:57:28 +00001066 if (m_floatingObjects && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001067 if (lastFloat) {
1068 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1069 }
1070 m_floatingObjects->next();
1071 } else
1072 m_floatingObjects->first();
1073 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001074 if (f->m_bottom > lastHeight)
1075 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com93526592008-03-18 04:36:51 +00001076 ASSERT(f->m_renderer == floats[floatIndex].object);
mitz@apple.com40547b32008-03-18 04:04:34 +00001077 // If a float's geometry has changed, give up on syncing with clean lines.
mitz@apple.com93526592008-03-18 04:36:51 +00001078 if (floats[floatIndex].rect != IntRect(f->m_left, f->m_top, f->m_width, f->m_bottom - f->m_top))
mitz@apple.comeb9c42c2008-03-26 20:53:07 +00001079 checkForEndLineMatch = false;
mitz@apple.com40547b32008-03-18 04:04:34 +00001080 floatIndex++;
1081 }
1082 lastFloat = m_floatingObjects->last();
1083 }
1084
hyatt@apple.comd885df72009-01-22 02:31:52 +00001085 lastHeight = height();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001086 lineMidpointState.reset();
mitz@apple.com15035e62008-07-05 20:44:44 +00001087 resolver.setPosition(end);
kociendabb0c24b2001-08-24 14:24:40 +00001088 }
mitz@apple.come1364202008-02-28 01:06:41 +00001089
hyatt0c3a9862004-02-23 21:26:26 +00001090 if (endLine) {
1091 if (endLineMatched) {
1092 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001093 int delta = height() - endLineYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001094 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1095 line->attachLine();
1096 if (delta) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001097 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
1098 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
hyattda77c4b2004-06-17 18:09:49 +00001099 line->adjustPosition(0, delta);
ap9059f6f2006-07-24 16:55:02 +00001100 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001101 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1102 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1103 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1104 int floatTop = (*f)->y() - (*f)->marginTop();
mitz@apple.com40547b32008-03-18 04:04:34 +00001105 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001106 setHeight(floatTop + delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001107 positionNewFloats();
1108 }
1109 }
harrison208ea792005-07-29 23:42:59 +00001110 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001111 setHeight(lastRootBox()->blockHeight());
ap9059f6f2006-07-24 16:55:02 +00001112 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001113 // Delete all the remaining lines.
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001114 RootInlineBox* line = endLine;
hyatt0c3a9862004-02-23 21:26:26 +00001115 RenderArena* arena = renderArena();
1116 while (line) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001117 repaintTop = min(repaintTop, line->topVisibleOverflow());
1118 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001119 RootInlineBox* next = line->nextRootBox();
hyatt0c3a9862004-02-23 21:26:26 +00001120 line->deleteLine(arena);
1121 line = next;
1122 }
1123 }
1124 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001125 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001126 // In case we have a float on the last line, it might not be positioned up to now.
1127 // This has to be done before adding in the bottom border/padding, or the float will
1128 // include the padding incorrectly. -dwh
mitz@apple.com1d4e9532010-01-18 20:55:25 +00001129 if (checkForFloatsFromLastLine) {
1130 TrailingFloatsRootInlineBox* trailingFloatsLineBox = new (renderArena()) TrailingFloatsRootInlineBox(this);
1131 m_lineBoxes.appendLineBox(trailingFloatsLineBox);
1132 trailingFloatsLineBox->setConstructed();
1133 trailingFloatsLineBox->verticallyAlignBoxes(height());
1134 trailingFloatsLineBox->setBlockHeight(height());
1135 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001136 if (lastFloat) {
1137 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001138 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001139 m_floatingObjects->next();
1140 } else
1141 m_floatingObjects->first();
1142 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
1143 if (f->m_bottom > lastHeight)
1144 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com40547b32008-03-18 04:04:34 +00001145 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001146 lastFloat = m_floatingObjects->last();
mitz@apple.com40547b32008-03-18 04:04:34 +00001147 }
mitz@apple.com356e07c2009-07-07 21:36:21 +00001148 size_t floatCount = floats.size();
1149 // Floats that did not have layout did not repaint when we laid them out. They would have
1150 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1151 // painted.
1152 for (size_t i = 0; i < floatCount; ++i) {
1153 if (!floats[i].everHadLayout) {
1154 RenderBox* f = floats[i].object;
1155 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1156 f->repaint();
1157 }
1158 }
kociendabb0c24b2001-08-24 14:24:40 +00001159 }
hyatt85586af2003-02-19 23:22:42 +00001160
hyatta70560a2002-11-20 01:53:20 +00001161 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001162 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +00001163
adele7a470a72006-04-20 22:22:14 +00001164 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +00001165 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +00001166
1167 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1168 // truncate text.
1169 if (hasTextOverflow)
1170 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001171}
1172
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001173RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
1174 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +00001175{
1176 RootInlineBox* curr = 0;
1177 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001178
mitz@apple.com40547b32008-03-18 04:04:34 +00001179 bool dirtiedByFloat = false;
1180 if (!fullLayout) {
1181 size_t floatIndex = 0;
1182 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001183 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
1184 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1185 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
1186 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +00001187 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
1188 ASSERT(floatIndex < floats.size());
1189 if (floats[floatIndex].object != f) {
1190 // A new float has been inserted before this line or before its last known float.
1191 // Just do a full layout.
1192 fullLayout = true;
1193 break;
1194 }
1195 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +00001196 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +00001197 curr->markDirty();
mitz@apple.com1d4e9532010-01-18 20:55:25 +00001198 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()), curr);
mitz@apple.com40547b32008-03-18 04:04:34 +00001199 floats[floatIndex].rect.setSize(newSize);
1200 dirtiedByFloat = true;
1201 }
1202 floatIndex++;
1203 }
1204 }
1205 if (dirtiedByFloat || fullLayout)
1206 break;
1207 }
1208 // Check if a new float has been inserted after the last known float.
1209 if (!curr && floatIndex < floats.size())
1210 fullLayout = true;
1211 }
1212
hyatt0c3a9862004-02-23 21:26:26 +00001213 if (fullLayout) {
1214 // Nuke all our lines.
1215 if (firstRootBox()) {
1216 RenderArena* arena = renderArena();
1217 curr = firstRootBox();
1218 while (curr) {
1219 RootInlineBox* next = curr->nextRootBox();
1220 curr->deleteLine(arena);
1221 curr = next;
1222 }
darinec375482007-01-06 01:36:24 +00001223 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001224 }
eseidel789896f2005-11-27 22:52:09 +00001225 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001226 if (curr) {
1227 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001228 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001229 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001230 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001231 // The previous line didn't break cleanly or broke at a newline
1232 // that has been deleted, so treat it as dirty too.
1233 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001234 }
eseidel789896f2005-11-27 22:52:09 +00001235 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001236 // No dirty lines were found.
1237 // If the last line didn't break cleanly, treat it as dirty.
1238 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1239 curr = lastRootBox();
1240 }
mitz@apple.come1364202008-02-28 01:06:41 +00001241
hyatt0c3a9862004-02-23 21:26:26 +00001242 // If we have no dirty lines, then last is just the last root box.
1243 last = curr ? curr->prevRootBox() : lastRootBox();
1244 }
mitz@apple.come1364202008-02-28 01:06:41 +00001245
mitz@apple.com40547b32008-03-18 04:04:34 +00001246 numCleanFloats = 0;
1247 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001248 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +00001249 // Restore floats from clean lines.
1250 RootInlineBox* line = firstRootBox();
1251 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001252 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1253 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1254 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001255 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001256 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +00001257 positionNewFloats();
1258 ASSERT(floats[numCleanFloats].object == *f);
1259 numCleanFloats++;
1260 }
1261 }
1262 line = line->nextRootBox();
1263 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001264 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001265 }
1266
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001267 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001268 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001269
1270 RenderObject* startObj;
1271 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001272 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001273 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001274 startObj = last->lineBreakObj();
1275 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001276 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001277 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001278 bool ltr = style()->direction() == LTR
1279 #if ENABLE(SVG)
1280 || (style()->unicodeBidi() == UBNormal && isSVGText())
1281 #endif
1282 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001283
mitz@apple.com45d9e102009-04-27 16:24:55 +00001284 Direction direction = ltr ? LeftToRight : RightToLeft;
1285 resolver.setLastStrongDir(direction);
1286 resolver.setLastDir(direction);
1287 resolver.setEorDir(direction);
1288 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001289
mitz@apple.com15035e62008-07-05 20:44:44 +00001290 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001291 }
mitz@apple.come1364202008-02-28 01:06:41 +00001292
mitz@apple.com15035e62008-07-05 20:44:44 +00001293 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001294
hyatt0c3a9862004-02-23 21:26:26 +00001295 return curr;
1296}
1297
mitz@apple.com15035e62008-07-05 20:44:44 +00001298RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001299{
1300 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001301 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001302 last = 0;
1303 else {
hyatt04420ca2004-07-16 00:05:42 +00001304 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1305 if (curr->isDirty())
1306 last = 0;
1307 else if (!last)
1308 last = curr;
1309 }
hyatt0c3a9862004-02-23 21:26:26 +00001310 }
mitz@apple.come1364202008-02-28 01:06:41 +00001311
hyatt0c3a9862004-02-23 21:26:26 +00001312 if (!last)
1313 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001314
eseidel789896f2005-11-27 22:52:09 +00001315 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001316 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001317 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001318 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001319
hyatt0c3a9862004-02-23 21:26:26 +00001320 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1321 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1322 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001323
hyatt0c3a9862004-02-23 21:26:26 +00001324 return last;
1325}
1326
mitz@apple.com15035e62008-07-05 20:44:44 +00001327bool 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 +00001328{
mitz@apple.com15035e62008-07-05 20:44:44 +00001329 if (resolver.position() == endLineStart) {
1330 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001331 return false;
1332
hyatt@apple.comd885df72009-01-22 02:31:52 +00001333 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001334 if (!delta || !m_floatingObjects)
1335 return true;
1336
1337 // 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 +00001338 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001339
1340 RootInlineBox* lastLine = endLine;
1341 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1342 lastLine = nextLine;
1343
1344 int bottom = lastLine->blockHeight() + abs(delta);
1345
1346 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001347 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001348 return false;
1349 }
1350
1351 return true;
1352 }
hyatt0c3a9862004-02-23 21:26:26 +00001353
mitz@apple.come1364202008-02-28 01:06:41 +00001354 // The first clean line doesn't match, but we can check a handful of following lines to try
1355 // to match back up.
1356 static int numLines = 8; // The # of lines we're willing to match against.
1357 RootInlineBox* line = endLine;
1358 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001359 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001360 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001361 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001362 return false; // ...but the bidi state doesn't match.
1363 RootInlineBox* result = line->nextRootBox();
1364
1365 // Set our yPos to be the block height of endLine.
1366 if (result)
1367 endYPos = line->blockHeight();
1368
hyatt@apple.comd885df72009-01-22 02:31:52 +00001369 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001370 if (delta && m_floatingObjects) {
1371 // 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 +00001372 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001373
1374 RootInlineBox* lastLine = endLine;
1375 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1376 lastLine = nextLine;
1377
1378 int bottom = lastLine->blockHeight() + abs(delta);
1379
1380 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001381 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001382 return false;
1383 }
1384 }
1385
mitz@apple.come1364202008-02-28 01:06:41 +00001386 // Now delete the lines that we failed to sync.
1387 RootInlineBox* boxToDelete = endLine;
1388 RenderArena* arena = renderArena();
1389 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001390 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1391 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001392 RootInlineBox* next = boxToDelete->nextRootBox();
1393 boxToDelete->deleteLine(arena);
1394 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001395 }
mitz@apple.come1364202008-02-28 01:06:41 +00001396
1397 endLine = result;
1398 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001399 }
1400 }
mitz@apple.come1364202008-02-28 01:06:41 +00001401
hyatt0c3a9862004-02-23 21:26:26 +00001402 return false;
1403}
1404
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001405static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001406{
darinf9e5d6c2007-01-09 14:54:26 +00001407 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001408 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001409
hyattdca76e92005-11-02 08:52:50 +00001410 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1411 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001412 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001413 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1414 // |true|).
1415 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001416 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001417
kocienda498d1982004-10-15 21:07:24 +00001418 return true;
kocienda98440082004-10-14 23:51:47 +00001419}
1420
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001421static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001422{
1423 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1424}
1425
zimmermannac3781f2007-02-04 01:25:03 +00001426static inline bool shouldPreserveNewline(RenderObject* object)
1427{
mjsd2948ef2007-02-26 19:29:04 +00001428#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001429 if (object->isSVGText())
1430 return false;
1431#endif
1432
1433 return object->style()->preserveNewline();
1434}
1435
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001436static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001437{
1438 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001439 // We need to fix this, though, because at the very least, inlines containing only
1440 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001441 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001442}
1443
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001444static inline bool requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001445{
bdakinf876bee2007-10-30 05:27:09 +00001446 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001447 return false;
bdakinf876bee2007-10-30 05:27:09 +00001448
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001449 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001450 return false;
1451
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001452 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001453 return true;
1454
1455 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001456 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1457 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001458}
1459
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001460bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001461{
1462 ASSERT(inlineObj->parent() == this);
1463
mitz@apple.com15035e62008-07-05 20:44:44 +00001464 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001465 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001466 it.increment();
bdashccffb432007-07-13 11:51:40 +00001467
1468 return !it.atEnd();
1469}
1470
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001471// 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 +00001472// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1473// elements quite right. In other words, we need to build this function's work into the normal line
1474// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001475// NB. this function will insert any floating elements that would otherwise
1476// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001477void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001478{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001479 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001480 RenderObject* object = iterator.obj;
1481 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001482 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001483 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001484 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1485 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001486 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001487 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001488 // A relative positioned inline encloses us. In this case, we also have to determine our
1489 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1490 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001491 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1492 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001493 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001494
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001495 RenderBox* box = toRenderBox(object);
1496 if (box->style()->hasStaticX()) {
1497 if (box->style()->isOriginalDisplayInlineType())
1498 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001499 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001500 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001501 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001502
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001503 if (box->style()->hasStaticY())
1504 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001505 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001506 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001507 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001508}
bdashccffb432007-07-13 11:51:40 +00001509
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001510int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly)
mitz@apple.com1a301772008-03-11 18:30:36 +00001511{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001512 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001513 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001514 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001515 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001516 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001517 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001518 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001519 } else if (object->isPositioned()) {
1520 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1521 // will work for the common cases
1522 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001523 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001524 // A relative positioned inline encloses us. In this case, we also have to determine our
1525 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1526 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001527 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1528 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001529 }
1530
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001531 RenderBox* box = toRenderBox(object);
1532 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001533 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001534 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001535 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001536 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001537 }
1538
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001539 if (box->style()->hasStaticY())
1540 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001541 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001542 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001543 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001544 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001545 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001546}
1547
bdakinf876bee2007-10-30 05:27:09 +00001548// This is currently just used for list markers and inline flows that have line boxes. Neither should
1549// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001550static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001551{
mitz@apple.com1a301772008-03-11 18:30:36 +00001552 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001553 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1554 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001555 UChar nextChar = nextText->characters()[0];
1556 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001557 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001558 return true;
1559 }
1560 }
1561
1562 return false;
1563}
1564
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001565void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001566{
1567 ASSERT(widthToFit > availableWidth);
1568
1569 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001570 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001571 int newLineWidth = availableWidth;
1572 while (true) {
1573 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1574 if (!floatBottom)
1575 break;
1576
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001577 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001578 lastFloatBottom = floatBottom;
1579 if (newLineWidth >= widthToFit)
1580 break;
1581 }
1582
1583 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001584 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001585 availableWidth = newLineWidth;
1586 }
1587}
1588
mitz@apple.com34106442009-02-01 06:23:39 +00001589static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1590{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001591 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001592 return text->width(from, len, font, xPos);
1593 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1594}
1595
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001596InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
1597 EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001598{
mitz@apple.com15035e62008-07-05 20:44:44 +00001599 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001600
mitz@apple.com15035e62008-07-05 20:44:44 +00001601 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001602 LineMidpointState& lineMidpointState = resolver.midpointState();
1603
1604 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly);
mitz@apple.com1a301772008-03-11 18:30:36 +00001605
kociendae40cb942004-10-05 20:05:38 +00001606 int w = 0;
1607 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001608
mitz@apple.com15035e62008-07-05 20:44:44 +00001609 if (resolver.position().atEnd())
1610 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001611
hyatt33f8d492002-11-12 21:44:52 +00001612 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1613 // or not we are currently ignoring whitespace.
1614 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001615 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001616
1617 // This variable tracks whether the very last character we saw was a space. We use
1618 // this to detect when we encounter a second space so we know we have to terminate
1619 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001620 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001621 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001622 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001623
mitz@apple.com15035e62008-07-05 20:44:44 +00001624 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001625
mitz@apple.com15035e62008-07-05 20:44:44 +00001626 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001627 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001628 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001629 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001630 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001631
hyatt0c3a9862004-02-23 21:26:26 +00001632 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1633 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001634
1635 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001636 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001637
hyatt@apple.com69340902008-01-16 21:24:21 +00001638 // 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 +00001639 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001640 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
1641 bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
1642
hyattb0d9f602007-01-15 01:28:23 +00001643 EWhiteSpace currWS = style()->whiteSpace();
1644 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001645 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001646 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1647 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1648
1649 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001650 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001651
mjsd2948ef2007-02-26 19:29:04 +00001652#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001653 bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
1654#else
hyattb0d9f602007-01-15 01:28:23 +00001655 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001656#endif
1657
hyattb0d9f602007-01-15 01:28:23 +00001658 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1659
hyatt275d0702005-11-03 23:53:57 +00001660 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001661 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001662 lBreak.obj = o;
1663 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001664 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001665 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001666
hyatt33f8d492002-11-12 21:44:52 +00001667 // A <br> always breaks a line, so don't let the line be collapsed
1668 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001669 // get collapsed away. It only does this if the previous line broke
1670 // cleanly. Otherwise the <br> has no effect on whether the line is
1671 // empty or not.
1672 if (prevLineBrokeCleanly)
1673 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001674 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001675 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001676
mitz@apple.com71e30842008-03-18 16:13:31 +00001677 if (!isLineEmpty && clear)
1678 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001679 }
1680 goto end;
1681 }
hyattb0d9f602007-01-15 01:28:23 +00001682
hyatt275d0702005-11-03 23:53:57 +00001683 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001684 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001685 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001686 RenderBox* floatBox = toRenderBox(o);
1687 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001688 // check if it fits in the current line.
1689 // If it does, position it now, otherwise, position
1690 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001691 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001692 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001693 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001694 } else
1695 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001696 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001697 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001698 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001699 RenderBox* box = toRenderBox(o);
1700 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1701 bool needToSetStaticX = box->style()->hasStaticX();
1702 if (box->style()->hasStaticX() && !isInlineType) {
1703 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001704 borderLeft() + paddingLeft() :
1705 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001706 needToSetStaticX = false;
1707 }
1708
1709 // If our original display was an INLINE type, then we can go ahead
1710 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001711 bool needToSetStaticY = box->style()->hasStaticY();
1712 if (box->style()->hasStaticY() && isInlineType) {
1713 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001714 needToSetStaticY = false;
1715 }
1716
hyatt853cd7d2004-11-05 02:59:48 +00001717 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1718 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001719 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001720 needToCreateLineBox = true;
1721
hyatt98ee7e42003-05-14 01:39:15 +00001722 // If we're ignoring spaces, we have to stop and include this object and
1723 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001724 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001725 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001726 ignoreStart.obj = o;
1727 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001728 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001729 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1730 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001731 }
hyatt98b16282004-03-31 18:43:12 +00001732
hyatt851816b2003-07-08 07:54:17 +00001733 }
hyatt98ee7e42003-05-14 01:39:15 +00001734 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001735 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001736 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001737 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001738
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001739 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001740
bdakinf876bee2007-10-30 05:27:09 +00001741 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1742 // to make sure that we stop to include this object and then start ignoring spaces again.
1743 // If this object is at the start of the line, we need to behave like list markers and
1744 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001745 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001746 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001747 if (ignoringSpaces) {
1748 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001749 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1750 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001751 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001752 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001753 // Like with list markers, we start ignoring spaces to make sure that any
1754 // additional spaces we see will be discarded.
1755 currentCharacterIsSpace = true;
1756 currentCharacterIsWS = true;
1757 ignoringSpaces = true;
1758 }
1759 }
1760
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001761 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1762 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001763 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001764 RenderBox* replacedBox = toRenderBox(o);
1765
hyattde396342003-10-29 08:57:20 +00001766 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001767 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001768 w += tmpW;
1769 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001770 lBreak.obj = o;
1771 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001772 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001773 }
1774
mitz@apple.combfdc9112008-02-21 19:59:40 +00001775 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001776 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001777
hyatt33f8d492002-11-12 21:44:52 +00001778 isLineEmpty = false;
1779 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001780 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001781 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001782 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001783
bdakinf876bee2007-10-30 05:27:09 +00001784 // Optimize for a common case. If we can't find whitespace after the list
1785 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001786 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001787 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001788 // Like with inline flows, we start ignoring spaces to make sure that any
1789 // additional spaces we see will be discarded.
1790 currentCharacterIsSpace = true;
1791 currentCharacterIsWS = true;
1792 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001793 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001794 if (toRenderListMarker(o)->isInside())
1795 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001796 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001797 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001798 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001799 if (!pos)
1800 appliedStartWidth = false;
1801
darin@apple.com36744d62009-01-25 20:23:04 +00001802 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001803
darin42563ac52007-01-22 17:28:57 +00001804 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001805 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001806 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001807
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001808 const Font& f = t->style(firstLine)->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001809 bool isFixedPitch = f.isFixedPitch();
weinigf28a1c32007-02-14 14:10:31 +00001810
hyatt33f8d492002-11-12 21:44:52 +00001811 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001812 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001813 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001814
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001815 // Non-zero only when kerning is enabled, in which case we measure words with their trailing
1816 // space, then subtract its width.
mitz@apple.com8c85d972010-01-07 18:28:04 +00001817 int wordTrailingSpaceWidth = f.typesettingFeatures() & Kerning ? f.spaceWidth() + wordSpacing : 0;
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001818
darin54008922006-01-13 16:39:05 +00001819 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001820 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001821 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1822 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1823 // 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 +00001824 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001825 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001826 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1827
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001828 if (t->isWordBreak()) {
1829 w += tmpW;
1830 tmpW = 0;
1831 lBreak.obj = o;
1832 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001833 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001834 ASSERT(!len);
1835 }
1836
hyatt275d0702005-11-03 23:53:57 +00001837 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001838 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001839 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001840 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001841 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001842
hyattb0d9f602007-01-15 01:28:23 +00001843 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001844 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001845
hyatt78b85132004-03-29 20:07:45 +00001846 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001847 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001848 if (!ignoringSpaces) {
1849 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001850 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001851 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001852 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001853 else
darin@apple.com36744d62009-01-25 20:23:04 +00001854 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001855 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001856 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1857 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1858 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001859 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001860 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001861
hyatt78b85132004-03-29 20:07:45 +00001862 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001863 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001864
hyattdca76e92005-11-02 08:52:50 +00001865 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001866 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001867 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001868 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001869
mitz@apple.com15035e62008-07-05 20:44:44 +00001870 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001871 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001872 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001873 }
mitz@apple.combe429562008-03-07 01:09:51 +00001874
hyatt78b85132004-03-29 20:07:45 +00001875 pos++;
1876 len--;
eseideld13fe532005-11-30 02:40:29 +00001877 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001878 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
1879 continue;
1880 }
1881
hyatt3aff2332003-01-23 01:15:28 +00001882 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001883
darinf9e5d6c2007-01-09 14:54:26 +00001884 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001885
weiniged111c12007-07-13 22:45:11 +00001886 if ((breakAll || breakWords) && !midWordBreak) {
1887 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001888 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001889 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001890 }
darin47ece0d2005-09-04 07:42:31 +00001891
ddkilzere8759ef2007-03-25 06:28:19 +00001892 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001893
weiniged111c12007-07-13 22:45:11 +00001894 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001895 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001896 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001897 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001898 // Stop ignoring spaces and begin at this
1899 // new point.
hyatt48710d62003-08-21 09:17:13 +00001900 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001901 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001902 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001903 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001904 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001905 } else {
hyatt33f8d492002-11-12 21:44:52 +00001906 // Just keep ignoring these spaces.
1907 pos++;
1908 len--;
1909 continue;
1910 }
1911 }
rjwc9c257d2003-01-24 03:46:17 +00001912
mitz@apple.comfa13fcc2010-01-07 01:41:32 +00001913 int additionalTmpW;
1914 if (wordTrailingSpaceWidth && currentCharacterIsSpace)
1915 additionalTmpW = textWidth(t, lastSpace, pos + 1 - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) - wordTrailingSpaceWidth + lastSpaceWordSpacing;
1916 else
1917 additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001918 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001919 if (!appliedStartWidth) {
1920 tmpW += inlineWidth(o, true, false);
1921 appliedStartWidth = true;
1922 }
1923
eseideld13fe532005-11-30 02:40:29 +00001924 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001925
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001926 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001927 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001928
hyattb0d9f602007-01-15 01:28:23 +00001929 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001930 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001931 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001932 bool lineWasTooWide = false;
1933 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001934 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001935 // Check if line is too big even without the extra space
1936 // at the end of the line. If it is not, do nothing.
1937 // If the line needs the extra whitespace to be too long,
1938 // then move the line break to the space and skip all
1939 // additional whitespace.
1940 if (w + tmpW + charWidth > width) {
1941 lineWasTooWide = true;
1942 lBreak.obj = o;
1943 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001944 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001945 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001946 }
ap932806a2006-10-01 09:06:09 +00001947 }
1948 if (lineWasTooWide || w + tmpW > width) {
eric@webkit.org1cb44402009-12-29 06:25:16 +00001949 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 +00001950 if (!stoppedIgnoringSpaces && pos > 0) {
1951 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001952 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1953 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001954 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001955 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001956 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001957 }
hyatt78b85132004-03-29 20:07:45 +00001958 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001959 } else {
weiniged111c12007-07-13 22:45:11 +00001960 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001961 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001962 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001963 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001964 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001965 }
rjwc9c257d2003-01-24 03:46:17 +00001966 }
hyatt33f8d492002-11-12 21:44:52 +00001967
hyattb0d9f602007-01-15 01:28:23 +00001968 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001969 if (!stoppedIgnoringSpaces && pos > 0) {
1970 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001971 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1972 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001973 }
hyatta9f48e32003-02-03 22:48:01 +00001974 lBreak.obj = o;
1975 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001976 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001977 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001978 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001979 return lBreak;
1980 }
hyatta9f48e32003-02-03 22:48:01 +00001981
weinigf28a1c32007-02-14 14:10:31 +00001982 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001983 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001984 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001985 tmpW = 0;
1986 lBreak.obj = o;
1987 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001988 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001989 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1990 // opportunity to break after a word.
1991 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001992 }
hyatt33f8d492002-11-12 21:44:52 +00001993
darin54008922006-01-13 16:39:05 +00001994 if (midWordBreak) {
1995 // Remember this as a breakable position in case
1996 // adding the end width forces a break.
1997 lBreak.obj = o;
1998 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001999 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00002000 midWordBreak &= (breakWords || breakAll);
2001 }
2002
2003 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00002004 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
2005 lastSpace = pos;
2006 }
hyatt33f8d492002-11-12 21:44:52 +00002007
hyattdca76e92005-11-02 08:52:50 +00002008 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002009 // If we encounter a newline, or if we encounter a
2010 // second space, we need to go ahead and break up this
2011 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002012 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002013 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00002014
hyatt33f8d492002-11-12 21:44:52 +00002015 // We just entered a mode where we are ignoring
2016 // spaces. Create a midpoint to terminate the run
2017 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002018 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00002019 }
2020 }
eseidel789896f2005-11-27 22:52:09 +00002021 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002022 // Stop ignoring spaces and begin at this
2023 // new point.
2024 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002025 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00002026 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002027 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00002028 }
hyatt98b16282004-03-31 18:43:12 +00002029
2030 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
2031 ignoreStart.obj = o;
2032 ignoreStart.pos = pos;
2033 }
harrisone343c412005-01-18 01:07:26 +00002034
2035 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00002036 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00002037 lBreak.obj = o;
2038 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002039 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00002040 }
2041 }
hyatt33f8d492002-11-12 21:44:52 +00002042
hyattb0d9f602007-01-15 01:28:23 +00002043 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00002044 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00002045 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00002046 trailingSpaceObject = 0;
2047
2048 pos++;
2049 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00002050 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002051 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002052
kociendabb0c24b2001-08-24 14:24:40 +00002053 // IMPORTANT: pos is > length here!
hyatt33f8d492002-11-12 21:44:52 +00002054 if (!ignoringSpaces)
mitz@apple.com34106442009-02-01 06:23:39 +00002055 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
weinigf28a1c32007-02-14 14:10:31 +00002056 tmpW += inlineWidth(o, !appliedStartWidth, true);
kociendabb0c24b2001-08-24 14:24:40 +00002057 } else
weinigf28a1c32007-02-14 14:10:31 +00002058 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002059
mitz@apple.com1a301772008-03-11 18:30:36 +00002060 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00002061 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00002062 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002063 checkForBreak = true;
2064 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00002065 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002066 if (currentCharacterIsSpace)
2067 checkForBreak = true;
2068 else {
harrison07b5e582005-08-15 23:31:16 +00002069 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00002070 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002071 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00002072 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00002073 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00002074 // If the next item on the line is text, and if we did not end with
2075 // a space, then the next text run continues our word (and so it needs to
2076 // keep adding to |tmpW|. Just update and continue.
2077 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002078 } else if (nextText->isWordBreak())
2079 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002080 bool willFitOnLine = w + tmpW <= width;
2081 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002082 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002083 willFitOnLine = tmpW <= width;
2084 }
ddkilzere8759ef2007-03-25 06:28:19 +00002085 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00002086 if (canPlaceOnLine && checkForBreak) {
2087 w += tmpW;
2088 tmpW = 0;
2089 lBreak.obj = next;
2090 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002091 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00002092 }
2093 }
2094 }
2095 }
2096
darin54008922006-01-13 16:39:05 +00002097 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002098 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002099 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002100 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002101
2102 if (w)
2103 goto end;
2104
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002105 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00002106
hyatta14d1742003-01-02 20:25:46 +00002107 // |width| may have been adjusted because we got shoved down past a float (thus
2108 // giving us more room), so we need to retest, and only jump to
2109 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002110 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002111 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002112 }
hyatt1d9e29b2003-04-10 01:48:53 +00002113
mitz@apple.com1a301772008-03-11 18:30:36 +00002114 if (!o->isFloatingOrPositioned()) {
2115 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002116 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00002117 w += tmpW;
2118 tmpW = 0;
2119 lBreak.obj = next;
2120 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002121 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002122 }
hyatt711fe232002-11-20 21:25:14 +00002123 }
2124
mitz@apple.com1a301772008-03-11 18:30:36 +00002125 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002126 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002127
hyatta9f48e32003-02-03 22:48:01 +00002128 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2129 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002130 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002131 currentCharacterIsSpace = false;
2132
kociendabb0c24b2001-08-24 14:24:40 +00002133 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002134 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002135 }
2136
hyattb0d9f602007-01-15 01:28:23 +00002137
2138 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00002139 lBreak.obj = 0;
2140 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002141 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002142 }
2143
2144 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00002145 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002146 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002147 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002148 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002149 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002150 lBreak.obj = o;
2151 lBreak.pos = pos - 1;
2152 } else {
2153 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002154 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002155 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002156 }
hyatt275d0702005-11-03 23:53:57 +00002157 } else if (lBreak.obj) {
2158 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002159 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002160 lBreak.obj = o;
2161 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002162 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002163 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002164 // Don't ever break in the middle of a word if we can help it.
2165 // There's no room at all. We just have to be on this line,
2166 // even though we'll spill out.
2167 lBreak.obj = o;
2168 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002169 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002170 }
kociendabb0c24b2001-08-24 14:24:40 +00002171 }
2172 }
2173
2174 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002175 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002176 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002177
hyattfe99c872003-07-31 22:25:29 +00002178 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002179 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00002180
hyatt33f8d492002-11-12 21:44:52 +00002181 if (trailingSpaceObject) {
2182 // This object is either going to be part of the last midpoint, or it is going
2183 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2184 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00002185 if (lineMidpointState.numMidpoints % 2) {
2186 InlineIterator* midpoints = lineMidpointState.midpoints.data();
2187 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002188 }
2189 //else if (lBreak.pos > 0)
2190 // lBreak.pos--;
2191 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002192 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00002193 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002194 unsigned length = text->textLength();
2195 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00002196 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002197 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00002198 }
2199 }
rjwc9c257d2003-01-24 03:46:17 +00002200
mjs54b64002003-04-02 02:59:02 +00002201 // We might have made lBreak an iterator that points past the end
2202 // of the object. Do this adjustment to make it point to the start
2203 // of the next object instead to avoid confusing the rest of the
2204 // code.
2205 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002206 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002207 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002208 }
2209
hyatt78b85132004-03-29 20:07:45 +00002210 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2211 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2212 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002213 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00002214 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002215 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00002216 }
2217
kociendabb0c24b2001-08-24 14:24:40 +00002218 return lBreak;
2219}
2220
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002221void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002222{
hyattb4b20872004-10-20 21:34:01 +00002223 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002224 addLayoutOverflow(curr->layoutOverflowRect());
2225 if (!hasOverflowClip())
2226 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00002227 }
2228}
2229
hyatted77ad82004-06-15 07:21:23 +00002230void RenderBlock::deleteEllipsisLineBoxes()
2231{
hyatted77ad82004-06-15 07:21:23 +00002232 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002233 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002234}
2235
2236void RenderBlock::checkLinesForTextOverflow()
2237{
2238 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002239 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2240 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002241 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002242 const Font& firstLineFont = firstLineStyle()->font();
2243 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002244 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2245 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002246
2247 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2248 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2249 // check the left edge of the line box to see if it is less
2250 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2251 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002252 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002253 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2254 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.com5631f112009-02-11 01:36:45 +00002255 int lineBoxEdge = ltr ? curr->x() + curr->width() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002256 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002257 // 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 +00002258 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2259 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2260 // space.
2261 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002262 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002263 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002264 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002265 }
2266 }
2267}
2268
hyattffe78712003-02-11 01:59:29 +00002269}