blob: 471fa8110168bb80180a3268f7c0dc83150ee549 [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.com4c1ff322009-07-13 00:54:12 +00003 * Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009 Apple Inc. All right reserved.
kociendabb0c24b2001-08-24 14:24:40 +00004 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
ddkilzerc8eccec2007-09-26 02:29:57 +000017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
kociendabb0c24b2001-08-24 14:24:40 +000019 *
kociendabb0c24b2001-08-24 14:24:40 +000020 */
darinbe4c67d2005-12-19 19:53:12 +000021
mjsb64c50a2005-10-03 21:13:12 +000022#include "config.h"
darin36d11362006-04-11 16:30:21 +000023
mitz@apple.com4c1ff322009-07-13 00:54:12 +000024#include "BidiResolver.h"
darinf9e5d6c2007-01-09 14:54:26 +000025#include "CharacterNames.h"
eseidel3a6d1322006-01-09 03:14:50 +000026#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000027#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000028#include "RenderArena.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000029#include "RenderInline.h"
mjsd26b2972007-02-13 13:09:04 +000030#include "RenderListMarker.h"
hyattd8048342006-05-31 01:48:18 +000031#include "RenderView.h"
darin36d11362006-04-11 16:30:21 +000032#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000033#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000034#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000035#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000036#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000037
darin7bd70952006-04-13 07:07:34 +000038using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000039using namespace WTF;
40using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000041
darinb9481ed2006-03-20 02:57:59 +000042namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000043
hyatt1d5d87b2007-04-24 04:55:54 +000044// We don't let our line box tree for a single line get any deeper than this.
45const unsigned cMaxLineDepth = 200;
46
mitz@apple.com15035e62008-07-05 20:44:44 +000047class InlineIterator {
pewtermoosecf72e2d2007-07-20 19:01:55 +000048public:
mitz@apple.com15035e62008-07-05 20:44:44 +000049 InlineIterator()
pewtermoosecf72e2d2007-07-20 19:01:55 +000050 : block(0)
51 , obj(0)
52 , pos(0)
mitz@apple.comd17dc392008-09-15 18:48:20 +000053 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000054 {
55 }
56
mitz@apple.com15035e62008-07-05 20:44:44 +000057 InlineIterator(RenderBlock* b, RenderObject* o, unsigned p)
pewtermoosecf72e2d2007-07-20 19:01:55 +000058 : block(b)
59 , obj(o)
60 , pos(p)
mitz@apple.comd17dc392008-09-15 18:48:20 +000061 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000062 {
63 }
64
mitz@apple.com15035e62008-07-05 20:44:44 +000065 void increment(InlineBidiResolver* resolver = 0);
mjsfe301d72003-10-02 18:46:18 +000066 bool atEnd() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000067
darin7ab31092006-05-10 04:59:57 +000068 UChar current() const;
mitz@apple.com4c1ff322009-07-13 00:54:12 +000069 Direction direction() const;
hyatt275d0702005-11-03 23:53:57 +000070
71 RenderBlock* block;
72 RenderObject* obj;
mitz@apple.com1a301772008-03-11 18:30:36 +000073 unsigned pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +000074 int nextBreakablePosition;
mjsfe301d72003-10-02 18:46:18 +000075};
76
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000077static int getBorderPaddingMargin(RenderBoxModelObject* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000078{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000079 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
80 if (leftSide)
81 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
82 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000083}
84
85static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
86{
hyatt1d5d87b2007-04-24 04:55:54 +000087 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000088 int extraWidth = 0;
89 RenderObject* parent = child->parent();
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000090 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyatt@apple.com40232f82009-02-04 04:26:08 +000091 if (start && !child->previousSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000092 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), false);
hyatt@apple.com40232f82009-02-04 04:26:08 +000093 if (end && !child->nextSibling())
hyatt@apple.com0d4818f2009-02-08 05:39:22 +000094 extraWidth += getBorderPaddingMargin(toRenderBoxModelObject(parent), true);
hyattffe78712003-02-11 01:59:29 +000095 child = parent;
96 parent = child->parent();
97 }
98 return extraWidth;
99}
100
mitz@apple.com4c1ff322009-07-13 00:54:12 +0000101struct BidiRun : BidiCharacterRun {
102 BidiRun(int start, int stop, RenderObject* object, BidiContext* context, Direction dir)
103 : BidiCharacterRun(start, stop, context, dir)
104 , m_object(object)
105 , m_box(0)
106 {
107 }
108
109 void destroy();
110
111 // Overloaded new operator.
112 void* operator new(size_t, RenderArena*) throw();
113
114 // Overridden to prevent the normal delete from being called.
115 void operator delete(void*, size_t);
116
117 BidiRun* next() { return static_cast<BidiRun*>(m_next); }
118
119private:
120 // The normal operator new is disallowed.
121 void* operator new(size_t) throw();
122
123public:
124 RenderObject* m_object;
125 InlineBox* m_box;
126};
127
darin35355e52002-12-20 09:19:00 +0000128#ifndef NDEBUG
mitz@apple.com4c1ff322009-07-13 00:54:12 +0000129static RefCountedLeakCounter bidiRunCounter("BidiRun");
ggarenec11e5b2007-02-25 02:14:54 +0000130
harrison0012ced2005-10-06 18:37:42 +0000131static bool inBidiRunDestroy;
hyattffe78712003-02-11 01:59:29 +0000132#endif
133
mitz@apple.come1364202008-02-28 01:06:41 +0000134void BidiRun::destroy()
hyattffe78712003-02-11 01:59:29 +0000135{
136#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000137 inBidiRunDestroy = true;
hyattffe78712003-02-11 01:59:29 +0000138#endif
mitz@apple.come1364202008-02-28 01:06:41 +0000139 RenderArena* renderArena = m_object->renderArena();
hyattffe78712003-02-11 01:59:29 +0000140 delete this;
141#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000142 inBidiRunDestroy = false;
hyattffe78712003-02-11 01:59:29 +0000143#endif
144
145 // Recover the size left there for us by operator delete and free the memory.
mitz@apple.come1364202008-02-28 01:06:41 +0000146 renderArena->free(*reinterpret_cast<size_t*>(this), this);
hyattffe78712003-02-11 01:59:29 +0000147}
148
149void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
150{
ggarenec11e5b2007-02-25 02:14:54 +0000151#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000152 bidiRunCounter.increment();
ggarenec11e5b2007-02-25 02:14:54 +0000153#endif
hyattffe78712003-02-11 01:59:29 +0000154 return renderArena->allocate(sz);
155}
156
157void BidiRun::operator delete(void* ptr, size_t sz)
158{
ggarenec11e5b2007-02-25 02:14:54 +0000159#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000160 bidiRunCounter.decrement();
ggarenec11e5b2007-02-25 02:14:54 +0000161#endif
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000162 ASSERT(inBidiRunDestroy);
hyattffe78712003-02-11 01:59:29 +0000163
harrisone8363b42005-10-06 00:54:06 +0000164 // Stash size where destroy() can find it.
hyattffe78712003-02-11 01:59:29 +0000165 *(size_t*)ptr = sz;
166}
167
kociendabb0c24b2001-08-24 14:24:40 +0000168// ---------------------------------------------------------------------
169
mitz@apple.com15035e62008-07-05 20:44:44 +0000170inline bool operator==(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000171{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000172 return it1.pos == it2.pos && it1.obj == it2.obj;
darinb70665a2002-04-15 23:43:21 +0000173}
174
mitz@apple.com15035e62008-07-05 20:44:44 +0000175inline bool operator!=(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000176{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000177 return it1.pos != it2.pos || it1.obj != it2.obj;
darinb70665a2002-04-15 23:43:21 +0000178}
179
mitz@apple.com15035e62008-07-05 20:44:44 +0000180static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, InlineBidiResolver* resolver = 0, bool skipInlines = true, bool* endOfInlinePtr = 0)
mjs6f821c82002-03-22 00:31:57 +0000181{
hyatt275d0702005-11-03 23:53:57 +0000182 RenderObject* next = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +0000183 bool oldEndOfInline = endOfInlinePtr ? *endOfInlinePtr : false;
184 bool endOfInline = false;
hyattffe78712003-02-11 01:59:29 +0000185
hyatt275d0702005-11-03 23:53:57 +0000186 while (current) {
sullivanabd4d032007-02-09 22:51:41 +0000187 next = 0;
hyatt@apple.com40232f82009-02-04 04:26:08 +0000188 if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned() && !current->isText()) {
hyattffe78712003-02-11 01:59:29 +0000189 next = current->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000190 if (next && resolver && next->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000191 EUnicodeBidi ub = next->style()->unicodeBidi();
hyatt275d0702005-11-03 23:53:57 +0000192 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000193 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000194 Direction d = (ub == Embed
195 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
196 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000197 resolver->embed(d);
hyattffe78712003-02-11 01:59:29 +0000198 }
199 }
200 }
hyatt275d0702005-11-03 23:53:57 +0000201
hyattffe78712003-02-11 01:59:29 +0000202 if (!next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000203 if (!skipInlines && !oldEndOfInline && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000204 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000205 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000206 break;
207 }
mjs6f821c82002-03-22 00:31:57 +0000208
hyatt275d0702005-11-03 23:53:57 +0000209 while (current && current != block) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000210 if (resolver && current->isRenderInline() && current->style()->unicodeBidi() != UBNormal)
mitz@apple.com15035e62008-07-05 20:44:44 +0000211 resolver->embed(PopDirectionalFormat);
hyatt275d0702005-11-03 23:53:57 +0000212
darindde01502005-12-18 22:55:35 +0000213 next = current->nextSibling();
214 if (next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000215 if (resolver && next->isRenderInline()) {
darindde01502005-12-18 22:55:35 +0000216 EUnicodeBidi ub = next->style()->unicodeBidi();
217 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000218 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000219 Direction d = (ub == Embed
220 ? (dir == RTL ? RightToLeftEmbedding: LeftToRightEmbedding)
221 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000222 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000223 }
224 }
225 break;
226 }
227
hyattffe78712003-02-11 01:59:29 +0000228 current = current->parent();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000229 if (!skipInlines && current && current != block && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000230 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000231 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000232 break;
233 }
234 }
235 }
mjs6f821c82002-03-22 00:31:57 +0000236
hyatt275d0702005-11-03 23:53:57 +0000237 if (!next)
238 break;
hyattffe78712003-02-11 01:59:29 +0000239
mitz@apple.combfdc9112008-02-21 19:59:40 +0000240 if (next->isText() || next->isFloating() || next->isReplaced() || next->isPositioned()
hyattffe78712003-02-11 01:59:29 +0000241 || ((!skipInlines || !next->firstChild()) // Always return EMPTY inlines.
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000242 && next->isRenderInline()))
mjs6f821c82002-03-22 00:31:57 +0000243 break;
244 current = next;
245 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000246
247 if (endOfInlinePtr)
248 *endOfInlinePtr = endOfInline;
249
mjs6f821c82002-03-22 00:31:57 +0000250 return next;
251}
252
mitz@apple.com15035e62008-07-05 20:44:44 +0000253static RenderObject* bidiFirst(RenderBlock* block, InlineBidiResolver* resolver, bool skipInlines = true)
mjs6f821c82002-03-22 00:31:57 +0000254{
hyatt275d0702005-11-03 23:53:57 +0000255 if (!block->firstChild())
256 return 0;
257
258 RenderObject* o = block->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000259 if (o->isRenderInline()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000260 if (resolver) {
darindde01502005-12-18 22:55:35 +0000261 EUnicodeBidi ub = o->style()->unicodeBidi();
262 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000263 TextDirection dir = o->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000264 Direction d = (ub == Embed
265 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
266 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000267 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000268 }
269 }
hyattffe78712003-02-11 01:59:29 +0000270 if (skipInlines && o->firstChild())
mitz@apple.com15035e62008-07-05 20:44:44 +0000271 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000272 else {
273 // Never skip empty inlines.
274 if (resolver)
275 resolver->commitExplicitEmbedding();
276 return o;
277 }
hyattffe78712003-02-11 01:59:29 +0000278 }
hyattc5334f12003-08-08 22:26:08 +0000279
mitz@apple.combfdc9112008-02-21 19:59:40 +0000280 if (o && !o->isText() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
mitz@apple.com15035e62008-07-05 20:44:44 +0000281 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000282
283 if (resolver)
284 resolver->commitExplicitEmbedding();
mjs6f821c82002-03-22 00:31:57 +0000285 return o;
286}
287
mitz@apple.com15035e62008-07-05 20:44:44 +0000288inline void InlineIterator::increment(InlineBidiResolver* resolver)
kociendabb0c24b2001-08-24 14:24:40 +0000289{
hyatt275d0702005-11-03 23:53:57 +0000290 if (!obj)
291 return;
292 if (obj->isText()) {
kociendabb0c24b2001-08-24 14:24:40 +0000293 pos++;
darin@apple.com36744d62009-01-25 20:23:04 +0000294 if (pos >= toRenderText(obj)->textLength()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000295 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000296 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000297 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000298 }
299 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +0000300 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000301 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000302 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000303 }
304}
305
mitz@apple.com1a301772008-03-11 18:30:36 +0000306template<>
mitz@apple.com15035e62008-07-05 20:44:44 +0000307inline void InlineBidiResolver::increment()
mitz@apple.com1a301772008-03-11 18:30:36 +0000308{
309 current.increment(this);
310}
311
mitz@apple.com15035e62008-07-05 20:44:44 +0000312inline bool InlineIterator::atEnd() const
kociendabb0c24b2001-08-24 14:24:40 +0000313{
hyatt275d0702005-11-03 23:53:57 +0000314 return !obj;
kociendabb0c24b2001-08-24 14:24:40 +0000315}
316
mitz@apple.com15035e62008-07-05 20:44:44 +0000317inline UChar InlineIterator::current() const
kociendabb0c24b2001-08-24 14:24:40 +0000318{
hyatt30586b42003-12-02 23:19:11 +0000319 if (!obj || !obj->isText())
darin7ab31092006-05-10 04:59:57 +0000320 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000321
darin@apple.com36744d62009-01-25 20:23:04 +0000322 RenderText* text = toRenderText(obj);
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000323 if (pos >= text->textLength())
darin7ab31092006-05-10 04:59:57 +0000324 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000325
darin42563ac52007-01-22 17:28:57 +0000326 return text->characters()[pos];
kociendabb0c24b2001-08-24 14:24:40 +0000327}
328
mitz@apple.com15035e62008-07-05 20:44:44 +0000329ALWAYS_INLINE Direction InlineIterator::direction() const
kociendabb0c24b2001-08-24 14:24:40 +0000330{
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000331 if (UChar c = current())
332 return Unicode::direction(c);
333
334 if (obj && obj->isListMarker())
darinf9e5d6c2007-01-09 14:54:26 +0000335 return obj->style()->direction() == LTR ? LeftToRight : RightToLeft;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000336
337 return OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000338}
339
kociendabb0c24b2001-08-24 14:24:40 +0000340// -------------------------------------------------------------------------------------------------
341
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000342static void chopMidpointsAt(LineMidpointState& lineMidpointState, RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +0000343{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000344 if (!lineMidpointState.numMidpoints)
hyatt275d0702005-11-03 23:53:57 +0000345 return;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000346 InlineIterator* midpoints = lineMidpointState.midpoints.data();
347 for (int i = lineMidpointState.numMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000348 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +0000349 if (point.obj == obj && point.pos == pos) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000350 lineMidpointState.numMidpoints = i;
hyatt78b85132004-03-29 20:07:45 +0000351 break;
352 }
353 }
354}
355
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000356static void checkMidpoints(LineMidpointState& lineMidpointState, InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000357{
358 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000359 // shave it off the list, and shave off a trailing space if the previous end point doesn't
360 // preserve whitespace.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000361 if (lBreak.obj && lineMidpointState.numMidpoints && !(lineMidpointState.numMidpoints % 2)) {
362 InlineIterator* midpoints = lineMidpointState.midpoints.data();
363 InlineIterator& endpoint = midpoints[lineMidpointState.numMidpoints - 2];
364 const InlineIterator& startpoint = midpoints[lineMidpointState.numMidpoints - 1];
mitz@apple.com15035e62008-07-05 20:44:44 +0000365 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000366 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000367 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000368 if (currpoint == lBreak) {
369 // We hit the line break before the start point. Shave off the start point.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000370 lineMidpointState.numMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000371 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000372 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000373 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000374 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000375 if (endpoint.pos + 1 < textObj->textLength()) {
376 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000377 return;
378 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000379 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000380 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000381 return;
382 }
hyattee1bcae2004-03-29 21:36:04 +0000383 }
hyattfe99c872003-07-31 22:25:29 +0000384 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000385 }
hyattfe99c872003-07-31 22:25:29 +0000386 }
387 }
388}
389
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000390static void addMidpoint(LineMidpointState& lineMidpointState, const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000391{
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000392 if (lineMidpointState.midpoints.size() <= lineMidpointState.numMidpoints)
393 lineMidpointState.midpoints.grow(lineMidpointState.numMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000394
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000395 InlineIterator* midpoints = lineMidpointState.midpoints.data();
396 midpoints[lineMidpointState.numMidpoints++] = midpoint;
hyatt85586af2003-02-19 23:22:42 +0000397}
398
mitz@apple.com15035e62008-07-05 20:44:44 +0000399static void appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000400{
hyatt98ee7e42003-05-14 01:39:15 +0000401 if (start > end || obj->isFloating() ||
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000402 (obj->isPositioned() && !obj->style()->hasStaticX() && !obj->style()->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000403 return;
hyatt85586af2003-02-19 23:22:42 +0000404
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000405 LineMidpointState& lineMidpointState = resolver.midpointState();
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000406 bool haveNextMidpoint = (lineMidpointState.currentMidpoint < lineMidpointState.numMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000407 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000408 if (haveNextMidpoint)
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000409 nextMidpoint = lineMidpointState.midpoints[lineMidpointState.currentMidpoint];
410 if (lineMidpointState.betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000411 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000412 return;
413 // This is a new start point. Stop ignoring objects and
414 // adjust our start.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000415 lineMidpointState.betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000416 start = nextMidpoint.pos;
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000417 lineMidpointState.currentMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000418 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000419 return appendRunsForObject(start, end, obj, resolver);
420 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000421 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000422 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000423 return;
424 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000425
hyatt78b85132004-03-29 20:07:45 +0000426 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000427 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000428 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +0000429 lineMidpointState.betweenMidpoints = true;
430 lineMidpointState.currentMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000431 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 +0000432 if (static_cast<int>(nextMidpoint.pos + 1) > start)
433 resolver.addRun(new (obj->renderArena())
434 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
435 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000436 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000437 } else
438 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000439 }
440}
441
pewtermoosecf72e2d2007-07-20 19:01:55 +0000442template <>
mitz@apple.com15035e62008-07-05 20:44:44 +0000443void InlineBidiResolver::appendRun()
kociendabb0c24b2001-08-24 14:24:40 +0000444{
mitz@apple.com6970fb72008-07-03 21:37:53 +0000445 if (!emptyRun && !eor.atEnd()) {
446 int start = sor.pos;
447 RenderObject *obj = sor.obj;
448 while (obj && obj != eor.obj && obj != endOfLine.obj) {
449 appendRunsForObject(start, obj->length(), obj, *this);
450 start = 0;
451 obj = bidiNext(sor.block, obj);
eseidel789896f2005-11-27 22:52:09 +0000452 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000453 if (obj) {
454 unsigned pos = obj == eor.obj ? eor.pos : UINT_MAX;
455 if (obj == endOfLine.obj && endOfLine.pos <= pos) {
456 reachedEndOfLine = true;
457 pos = endOfLine.pos;
458 }
459 // It's OK to add runs for zero-length RenderObjects, just don't make the run larger than it should be
460 int end = obj->length() ? pos+1 : 0;
461 appendRunsForObject(start, end, obj, *this);
462 }
463
464 eor.increment();
465 sor = eor;
justing5b0e0422005-08-01 03:20:49 +0000466 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000467
pewtermoosecf72e2d2007-07-20 19:01:55 +0000468 m_direction = OtherNeutral;
469 m_status.eor = OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000470}
471
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000472static inline InlineBox* createInlineBoxForRenderer(RenderObject* obj, bool isRootLineBox, bool isOnlyRun = false)
473{
474 if (isRootLineBox)
eric@webkit.org49b9d952009-07-03 01:29:07 +0000475 return toRenderBlock(obj)->createAndAppendRootInlineBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000476
477 if (obj->isText()) {
478 InlineTextBox* textBox = toRenderText(obj)->createInlineTextBox();
479 // We only treat a box as text for a <br> if we are on a line by ourself or in strict mode
480 // (Note the use of strict mode. In "almost strict" mode, we don't treat the box for <br> as text.)
481 if (obj->isBR())
482 textBox->setIsText(isOnlyRun || obj->document()->inStrictMode());
483 return textBox;
484 }
485
486 if (obj->isBox())
487 return toRenderBox(obj)->createInlineBox();
488
eric@webkit.org49b9d952009-07-03 01:29:07 +0000489 return toRenderInline(obj)->createAndAppendInlineFlowBox();
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000490}
491
492static inline void dirtyLineBoxesForRenderer(RenderObject* o, bool fullLayout)
493{
494 if (o->isText()) {
495 if (o->prefWidthsDirty() && o->isCounter())
496 toRenderText(o)->calcPrefWidths(0); // FIXME: Counters depend on this hack. No clue why. Should be investigated and removed.
497 toRenderText(o)->dirtyLineBoxes(fullLayout);
498 } else
499 toRenderInline(o)->dirtyLineBoxes(fullLayout);
500}
501
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000502InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000503{
504 // See if we have an unconstructed line box for this object that is also
505 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000506 unsigned lineDepth = 1;
507 InlineFlowBox* childBox = 0;
508 InlineFlowBox* parentBox = 0;
509 InlineFlowBox* result = 0;
510 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000511 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000512
hyatt1d5d87b2007-04-24 04:55:54 +0000513 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000514 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000515
hyatt1d5d87b2007-04-24 04:55:54 +0000516 // If this box is constructed then it is from a previous line, and we need
517 // to make a new box for our line. If this box is unconstructed but it has
518 // something following it on the line, then we know we have to make a new box
519 // as well. In this situation our inline has actually been split in two on
520 // the same line (this can happen with very fancy language mixtures).
521 bool constructedNewBox = false;
522 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
523 // We need to make a new box for this render object. Once
524 // made, we need to place it at the end of the current line.
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000525 InlineBox* newBox = createInlineBoxForRenderer(obj, obj == this);
hyatt1d5d87b2007-04-24 04:55:54 +0000526 ASSERT(newBox->isInlineFlowBox());
527 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000528 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000529 constructedNewBox = true;
530 }
mitz@apple.come1364202008-02-28 01:06:41 +0000531
hyatt1d5d87b2007-04-24 04:55:54 +0000532 if (!result)
533 result = parentBox;
534
535 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000536 // inline box for the line, and it doesn't have to be appended to any parent
537 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000538 if (childBox)
539 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000540
hyatt1d5d87b2007-04-24 04:55:54 +0000541 if (!constructedNewBox || obj == this)
542 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000543
hyatt1d5d87b2007-04-24 04:55:54 +0000544 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000545
hyatt1d5d87b2007-04-24 04:55:54 +0000546 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
547 // intermediate inline flows.
548 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000549
hyatt1d5d87b2007-04-24 04:55:54 +0000550 } while (true);
551
552 return result;
hyattffe78712003-02-11 01:59:29 +0000553}
554
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000555RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000556{
mitz@apple.com887f3592008-02-25 22:03:08 +0000557 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000558
559 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000560 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000561 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000562 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000563 if (runCount == 2 && !r->m_object->isListMarker())
564 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000565
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000566 InlineBox* box = createInlineBoxForRenderer(r->m_object, false, isOnlyRun);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000567 r->m_box = box;
568
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000569 ASSERT(box);
570 if (!box)
571 continue;
hyattffe78712003-02-11 01:59:29 +0000572
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000573 // If we have no parent box yet, or if the run is not simply a sibling,
574 // then we need to construct inline boxes as necessary to properly enclose the
575 // run's inline box.
576 if (!parentBox || parentBox->renderer() != r->m_object->parent())
577 // Create new inline boxes all the way back to the appropriate insertion point.
578 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000579
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000580 // Append the inline box to this line.
581 parentBox->addToLine(box);
mitz@apple.com576e84e2008-04-24 19:09:48 +0000582
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000583 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
584 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
585
586 if (box->isInlineTextBox()) {
587 InlineTextBox* text = static_cast<InlineTextBox*>(box);
588 text->setStart(r->m_start);
589 text->setLen(r->m_stop - r->m_start);
590 text->m_dirOverride = r->dirOverride(visuallyOrdered);
hyatt0c3a9862004-02-23 21:26:26 +0000591 }
hyattffe78712003-02-11 01:59:29 +0000592 }
593
594 // We should have a root inline box. It should be unconstructed and
595 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000596 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000597
598 // Set bits on our inline flow boxes that indicate which sides should
599 // paint borders/margins/padding. This knowledge will ultimately be used when
600 // we determine the horizontal positions and widths of all the inline boxes on
601 // the line.
hyattffe78712003-02-11 01:59:29 +0000602 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
603
604 // Now mark the line boxes as being constructed.
605 lastLineBox()->setConstructed();
606
607 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000608 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000609}
610
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000611void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd)
hyattffe78712003-02-11 01:59:29 +0000612{
613 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000614 int availableWidth = lineWidth(height(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000615 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000616 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000617 unsigned numSpaces = 0;
618 ETextAlign textAlign = style()->textAlign();
619
mitz@apple.come1364202008-02-28 01:06:41 +0000620 for (BidiRun* r = firstRun; r; r = r->next()) {
621 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000622 continue; // Positioned objects are only participating to figure out their
623 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000624 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000625 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000626 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000627
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000628 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000629 const UChar* characters = rt->characters();
630 for (int i = r->m_start; i < r->m_stop; i++) {
631 UChar c = characters[i];
632 if (c == ' ' || c == '\n' || c == '\t')
633 numSpaces++;
634 }
635 }
636
mitz@apple.come1364202008-02-28 01:06:41 +0000637 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000638 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000639 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000640 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000641 }
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000642 HashSet<const SimpleFontData*> fallbackFonts;
643 r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, firstLine, &fallbackFonts));
644 if (!fallbackFonts.isEmpty()
645#if ENABLE(SVG)
646 && !isSVGText()
647#endif
648 ) {
649 ASSERT(r->m_box->isText());
650 static_cast<InlineTextBox*>(r->m_box)->setFallbackFonts(fallbackFonts);
651 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000652 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000653 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000654 renderBox->calcWidth();
655 r->m_box->setWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000656 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000657 }
hyatt4b381692003-03-10 21:11:59 +0000658
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000659 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000660 }
661
662 // Armed with the total width of the line (without justification),
663 // we now examine our text-align property in order to determine where to position the
664 // objects horizontally. The total width of the line can be increased if we end up
665 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000666 int x = leftOffset(height(), firstLine);
ddkilzer@apple.comdfdc3fd2009-07-08 13:55:55 +0000667 switch (textAlign) {
hyattffe78712003-02-11 01:59:29 +0000668 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000669 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000670 // The direction of the block should determine what happens with wide lines. In
671 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000672 if (style()->direction() == LTR) {
673 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000674 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000675 } else {
676 if (trailingSpaceRun)
677 trailingSpaceRun->m_box->setWidth(0);
678 else if (totWidth > availableWidth)
679 x -= (totWidth - availableWidth);
680 }
hyattffe78712003-02-11 01:59:29 +0000681 break;
682 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000683 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
684 if (trailingSpaceRun) {
685 totWidth -= trailingSpaceRun->m_box->width();
686 trailingSpaceRun->m_box->setWidth(0);
687 }
hyattffe78712003-02-11 01:59:29 +0000688 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000689 }
hyattffe78712003-02-11 01:59:29 +0000690 // fall through
691 case TAAUTO:
692 numSpaces = 0;
693 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000694 if (style()->direction() == LTR) {
695 if (totWidth > availableWidth && trailingSpaceRun)
mitz@apple.comcbf51272009-04-07 17:08:18 +0000696 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
hyattffe78712003-02-11 01:59:29 +0000697 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000698 }
hyattffe78712003-02-11 01:59:29 +0000699 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000700 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000701 // Wide lines spill out of the block based off direction.
702 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
703 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000704 if (style()->direction() == LTR) {
705 if (trailingSpaceRun) {
706 totWidth -= trailingSpaceRun->m_box->width();
707 trailingSpaceRun->m_box->setWidth(0);
708 }
709 if (totWidth < availableWidth)
710 x += availableWidth - totWidth;
711 } else {
712 if (totWidth > availableWidth && trailingSpaceRun) {
mitz@apple.comcbf51272009-04-07 17:08:18 +0000713 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceRun->m_box->width() - totWidth + availableWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000714 totWidth -= trailingSpaceRun->m_box->width();
715 } else
716 x += availableWidth - totWidth;
717 }
hyattffe78712003-02-11 01:59:29 +0000718 break;
719 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000720 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000721 int trailingSpaceWidth = 0;
722 if (trailingSpaceRun) {
723 totWidth -= trailingSpaceRun->m_box->width();
724 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
mitz@apple.comcbf51272009-04-07 17:08:18 +0000725 trailingSpaceRun->m_box->setWidth(max(0, trailingSpaceWidth));
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000726 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000727 if (style()->direction() == LTR)
728 x += max((availableWidth - totWidth) / 2, 0);
729 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000730 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000731 break;
732 }
733
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000734 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000735 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000736 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000737 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000738
hyattacbb0d42003-04-25 01:32:49 +0000739 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000740 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000741 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000742 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000743 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000744 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000745 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000746 spaces++;
darina3c48282003-10-07 15:49:30 +0000747 }
hyatt870bdda2003-05-21 23:37:33 +0000748
darinb53ebdc2006-07-09 15:10:21 +0000749 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000750
hyattdca76e92005-11-02 08:52:50 +0000751 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000752 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000753 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000754 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000755 totWidth += spaceAdd;
756 }
hyattacbb0d42003-04-25 01:32:49 +0000757 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000758 if (!numSpaces)
759 break;
hyattacbb0d42003-04-25 01:32:49 +0000760 }
hyattffe78712003-02-11 01:59:29 +0000761 }
hyattffe78712003-02-11 01:59:29 +0000762 }
mitz@apple.come1364202008-02-28 01:06:41 +0000763
hyattffe78712003-02-11 01:59:29 +0000764 // The widths of all runs are now known. We can now place every inline box (and
765 // compute accurate widths for the inline flow boxes).
darin06dcb9c2005-08-15 04:31:09 +0000766 needsWordSpacing = false;
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000767 lineBox->placeBoxesHorizontally(x, needsWordSpacing);
hyattffe78712003-02-11 01:59:29 +0000768}
769
mitz@apple.com887f3592008-02-25 22:03:08 +0000770void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun)
hyattffe78712003-02-11 01:59:29 +0000771{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000772 setHeight(lineBox->verticallyAlignBoxes(height()));
773 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000774
775 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000776 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comaa6ce3d2009-04-10 01:00:20 +0000777 ASSERT(r->m_box);
mitz@apple.come1364202008-02-28 01:06:41 +0000778 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000779 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000780
hyatt98ee7e42003-05-14 01:39:15 +0000781 // Align positioned boxes with the top of the line box. This is
782 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000783 if (r->m_object->isPositioned())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000784 r->m_box->setY(height());
hyatt98ee7e42003-05-14 01:39:15 +0000785
786 // Position is used to properly position both replaced elements and
787 // to update the static normal flow x/y of positioned elements.
hyatt@apple.com6a551ad2009-02-11 22:43:12 +0000788 if (r->m_object->isText())
789 toRenderText(r->m_object)->positionLineBox(r->m_box);
790 else if (r->m_object->isBox())
791 toRenderBox(r->m_object)->positionLineBox(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000792 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000793 // Positioned objects and zero-length text nodes destroy their boxes in
794 // position(), which unnecessarily dirties the line.
795 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000796}
kociendabb0c24b2001-08-24 14:24:40 +0000797
798// collects one line of the paragraph and transforms it to visual order
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000799void RenderBlock::bidiReorderLine(InlineBidiResolver& resolver, const InlineIterator& end, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +0000800{
mitz@apple.com1a301772008-03-11 18:30:36 +0000801 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
hyatt4b381692003-03-10 21:11:59 +0000802}
kociendabb0c24b2001-08-24 14:24:40 +0000803
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000804static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
805{
806 if (character == ' ' || character == '\t' || character == softHyphen)
807 return true;
808 if (character == '\n')
809 return !renderer->style()->preserveNewline();
810 if (character == noBreakSpace)
811 return renderer->style()->nbspMode() == SPACE;
812 return false;
813}
814
apddd2ff42007-03-31 08:26:24 +0000815void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
kociendabb0c24b2001-08-24 14:24:40 +0000816{
apddd2ff42007-03-31 08:26:24 +0000817 bool useRepaintBounds = false;
hyatt7b41b9d2007-05-30 05:32:40 +0000818
hyatt@apple.com5dc5a312009-08-18 19:15:19 +0000819 m_overflow.clear();
hyatt7b41b9d2007-05-30 05:32:40 +0000820
hyatt@apple.comd885df72009-01-22 02:31:52 +0000821 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000822 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000823
hyatt0c3a9862004-02-23 21:26:26 +0000824 // Figure out if we should clear out our line boxes.
825 // FIXME: Handle resize eventually!
eric@webkit.orgc0249422010-01-06 00:07:56 +0000826 bool fullLayout = !firstLineBox() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000827 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000828 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000829
hyatted77ad82004-06-15 07:21:23 +0000830 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
831 // clip.
832 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
833 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
834 // anyway, so we won't worry about following the draft here.
835 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000836
hyatted77ad82004-06-15 07:21:23 +0000837 // Walk all the lines and delete our ellipsis line boxes if they exist.
838 if (hasTextOverflow)
839 deleteEllipsisLineBoxes();
840
hyattffe78712003-02-11 01:59:29 +0000841 if (firstChild()) {
kociendabb0c24b2001-08-24 14:24:40 +0000842 // layout replaced elements
hyattffe78712003-02-11 01:59:29 +0000843 bool endOfInline = false;
mitz@apple.com1a301772008-03-11 18:30:36 +0000844 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000845 Vector<FloatWithRect> floats;
hyatt0c3a9862004-02-23 21:26:26 +0000846 while (o) {
847 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000848 RenderBox* box = toRenderBox(o);
849
hyatt89377f12002-12-13 21:24:40 +0000850 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
hyattadbb2ef2003-11-18 09:21:44 +0000851 o->setChildNeedsLayout(true, false);
hyatt567533d2007-04-25 21:46:21 +0000852
853 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
854 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
855 o->setPrefWidthsDirty(true, false);
856
hyatt3ac01352003-03-22 01:37:33 +0000857 if (o->isPositioned())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000858 o->containingBlock()->insertPositionedObject(box);
hyatt0c3a9862004-02-23 21:26:26 +0000859 else {
860 if (o->isFloating())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000861 floats.append(FloatWithRect(box));
hyatt0c3a9862004-02-23 21:26:26 +0000862 else if (fullLayout || o->needsLayout()) // Replaced elements
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000863 toRenderBox(o)->dirtyLineBoxes(fullLayout);
mitz@apple.com40547b32008-03-18 04:04:34 +0000864
hyatt390427a2003-05-03 18:33:42 +0000865 o->layoutIfNeeded();
hyatt0c3a9862004-02-23 21:26:26 +0000866 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000867 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
hyatt0c3a9862004-02-23 21:26:26 +0000868 if (fullLayout || o->selfNeedsLayout())
hyatt@apple.comc92b7352009-02-12 01:35:08 +0000869 dirtyLineBoxesForRenderer(o, fullLayout);
hyattf4fe67f2003-10-07 04:43:23 +0000870 o->setNeedsLayout(false);
hyatt@apple.com4f81f3e2009-02-10 23:00:37 +0000871 if (!o->isText())
872 toRenderInline(o)->invalidateVerticalPosition(); // FIXME: Should do better here and not always invalidate everything.
hyattf4fe67f2003-10-07 04:43:23 +0000873 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000874 o = bidiNext(this, o, 0, false, &endOfInline);
kociendabb0c24b2001-08-24 14:24:40 +0000875 }
876
mitz@apple.com40547b32008-03-18 04:04:34 +0000877 // We want to skip ahead to the first dirty line
mitz@apple.com15035e62008-07-05 20:44:44 +0000878 InlineBidiResolver resolver;
mitz@apple.com40547b32008-03-18 04:04:34 +0000879 unsigned floatIndex;
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000880 bool firstLine = true;
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000881 bool previousLineBrokeCleanly = true;
882 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, previousLineBrokeCleanly, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000883
hyatt837eb362004-05-21 22:17:10 +0000884 if (fullLayout && !selfNeedsLayout()) {
hyatt0c3a9862004-02-23 21:26:26 +0000885 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
886 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000887 RenderView* v = view();
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000888 if (v && !v->doingFullRepaint() && hasLayer()) {
hyatt837eb362004-05-21 22:17:10 +0000889 // Because we waited until we were already inside layout to discover
890 // that the block really needed a full layout, we missed our chance to repaint the layer
891 // before layout started. Luckily the layer has cached the repaint rect for its original
892 // position and size, and so we can use that to make a repaint happen now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +0000893 repaintUsingContainer(containerForRepaint(), layer()->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000894 }
895 }
hyatt0c3a9862004-02-23 21:26:26 +0000896
mitz@apple.com40547b32008-03-18 04:04:34 +0000897 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
898
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000899 LineMidpointState& lineMidpointState = resolver.midpointState();
mitz@apple.come1364202008-02-28 01:06:41 +0000900
hyatt0c3a9862004-02-23 21:26:26 +0000901 // We also find the first clean line and extract these lines. We will add them back
902 // if we determine that we're able to synchronize after handling all our dirty lines.
mitz@apple.com15035e62008-07-05 20:44:44 +0000903 InlineIterator cleanLineStart;
eseidel789896f2005-11-27 22:52:09 +0000904 BidiStatus cleanLineBidiStatus;
aroben201afd12007-07-01 04:53:01 +0000905 int endLineYPos = 0;
hyatt0c3a9862004-02-23 21:26:26 +0000906 RootInlineBox* endLine = (fullLayout || !startLine) ?
pewtermoosecf72e2d2007-07-20 19:01:55 +0000907 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
908
hyatt0c3a9862004-02-23 21:26:26 +0000909 if (startLine) {
apddd2ff42007-03-31 08:26:24 +0000910 useRepaintBounds = true;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000911 repaintTop = height();
912 repaintBottom = height();
hyatt0c3a9862004-02-23 21:26:26 +0000913 RenderArena* arena = renderArena();
914 RootInlineBox* box = startLine;
915 while (box) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +0000916 repaintTop = min(repaintTop, box->topVisibleOverflow());
917 repaintBottom = max(repaintBottom, box->bottomVisibleOverflow());
hyatt0c3a9862004-02-23 21:26:26 +0000918 RootInlineBox* next = box->nextRootBox();
919 box->deleteLine(arena);
920 box = next;
921 }
hyatt0c3a9862004-02-23 21:26:26 +0000922 }
mitz@apple.come1364202008-02-28 01:06:41 +0000923
mitz@apple.com15035e62008-07-05 20:44:44 +0000924 InlineIterator end = resolver.position();
hyatt0c3a9862004-02-23 21:26:26 +0000925
mitz@apple.come3602dd2008-04-08 20:58:36 +0000926 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
927 // If the last line before the start line ends with a line break that clear floats,
928 // adjust the height accordingly.
929 // 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 +0000930 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
hyatt@apple.com5631f112009-02-11 01:36:45 +0000931 RenderObject* lastObject = lastLeafChild->renderer();
mitz@apple.comc7bc5992008-04-18 23:19:19 +0000932 if (!lastObject->isBR())
hyatt@apple.com5631f112009-02-11 01:36:45 +0000933 lastObject = lastRootBox()->firstLeafChild()->renderer();
mitz@apple.comc7bc5992008-04-18 23:19:19 +0000934 if (lastObject->isBR()) {
935 EClear clear = lastObject->style()->clear();
936 if (clear != CNONE)
937 newLine(clear);
938 }
mitz@apple.come3602dd2008-04-08 20:58:36 +0000939 }
940 }
mitz@apple.com40547b32008-03-18 04:04:34 +0000941
hyatt0c3a9862004-02-23 21:26:26 +0000942 bool endLineMatched = false;
mitz@apple.comeb9c42c2008-03-26 20:53:07 +0000943 bool checkForEndLineMatch = endLine;
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000944 bool checkForFloatsFromLastLine = false;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000945 int lastHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000946
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000947 bool isLineEmpty = true;
948
hyatt0c3a9862004-02-23 21:26:26 +0000949 while (!end.atEnd()) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000950 // 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 +0000951 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
hyatt0c3a9862004-02-23 21:26:26 +0000952 break;
953
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000954 lineMidpointState.reset();
955
hyatt33f8d492002-11-12 21:44:52 +0000956 isLineEmpty = true;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000957
mitz@apple.com71e30842008-03-18 16:13:31 +0000958 EClear clear = CNONE;
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000959 end = findNextLineBreak(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly, &clear);
mitz@apple.com15035e62008-07-05 20:44:44 +0000960 if (resolver.position().atEnd()) {
961 resolver.deleteRuns();
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000962 checkForFloatsFromLastLine = true;
eseidel789896f2005-11-27 22:52:09 +0000963 break;
ggarenec11e5b2007-02-25 02:14:54 +0000964 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000965 ASSERT(end != resolver.position());
mitz@apple.com1a301772008-03-11 18:30:36 +0000966
hyatt33f8d492002-11-12 21:44:52 +0000967 if (!isLineEmpty) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +0000968 bidiReorderLine(resolver, end, previousLineBrokeCleanly);
mitz@apple.com15035e62008-07-05 20:44:44 +0000969 ASSERT(resolver.position() == end);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000970
971 BidiRun* trailingSpaceRun = 0;
mitz@apple.comcbf51272009-04-07 17:08:18 +0000972 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()
973 && resolver.logicallyLastRun()->m_object->style()->autoWrap()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000974 trailingSpaceRun = resolver.logicallyLastRun();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000975 RenderObject* lastObject = trailingSpaceRun->m_object;
976 if (lastObject->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000977 RenderText* lastText = toRenderText(lastObject);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000978 const UChar* characters = lastText->characters();
979 int firstSpace = trailingSpaceRun->stop();
980 while (firstSpace > trailingSpaceRun->start()) {
981 UChar current = characters[firstSpace - 1];
982 if (!isCollapsibleSpace(current, lastText))
983 break;
984 firstSpace--;
985 }
986 if (firstSpace == trailingSpaceRun->stop())
987 trailingSpaceRun = 0;
mitz@apple.com3a43c712008-04-20 04:26:28 +0000988 else {
989 TextDirection direction = style()->direction();
mitz@apple.com15035e62008-07-05 20:44:44 +0000990 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
mitz@apple.com3a43c712008-04-20 04:26:28 +0000991 if (firstSpace != trailingSpaceRun->start()) {
bdakin@apple.com01af3022009-03-11 00:05:24 +0000992 BidiContext* baseContext = resolver.context();
993 while (BidiContext* parent = baseContext->parent())
994 baseContext = parent;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000995
bdakin@apple.com01af3022009-03-11 00:05:24 +0000996 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
997 trailingSpaceRun->m_stop = firstSpace;
998 if (direction == LTR)
999 resolver.addRun(newTrailingRun);
1000 else
1001 resolver.prependRun(newTrailingRun);
1002 trailingSpaceRun = newTrailingRun;
1003 shouldReorder = false;
mitz@apple.com3a43c712008-04-20 04:26:28 +00001004 }
1005 if (shouldReorder) {
mitz@apple.com8e05a142008-04-22 20:42:02 +00001006 if (direction == LTR) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001007 resolver.moveRunToEnd(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +00001008 trailingSpaceRun->m_level = 0;
1009 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +00001010 resolver.moveRunToBeginning(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +00001011 trailingSpaceRun->m_level = 1;
1012 }
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001013 }
1014 }
1015 } else
1016 trailingSpaceRun = 0;
1017 }
hyatt4b381692003-03-10 21:11:59 +00001018
1019 // Now that the runs have been ordered, we create the line boxes.
1020 // At the same time we figure out where border/padding/margin should be applied for
1021 // inline flow boxes.
hyatt4b381692003-03-10 21:11:59 +00001022
hyatt0c3a9862004-02-23 21:26:26 +00001023 RootInlineBox* lineBox = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +00001024 if (resolver.runCount()) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001025 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
hyattdfb09052003-03-11 02:58:59 +00001026 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +00001027 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
pewtermoosecf72e2d2007-07-20 19:01:55 +00001028
hyattdfb09052003-03-11 02:58:59 +00001029 // Now we position all of our text runs horizontally.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001030 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd());
mitz@apple.come1364202008-02-28 01:06:41 +00001031
hyattdfb09052003-03-11 02:58:59 +00001032 // Now position our text runs vertically.
mitz@apple.com15035e62008-07-05 20:44:44 +00001033 computeVerticalPositionsForLine(lineBox, resolver.firstRun());
hyattbddcc612006-07-24 23:16:15 +00001034
oliver98d52382007-10-12 10:47:18 +00001035#if ENABLE(SVG)
1036 // Special SVG text layout code
1037 lineBox->computePerCharacterLayoutInformation();
1038#endif
1039
hyattbddcc612006-07-24 23:16:15 +00001040#if PLATFORM(MAC)
1041 // Highlight acts as an overflow inflation.
1042 if (style()->highlight() != nullAtom)
1043 lineBox->addHighlightOverflow();
1044#endif
hyattdfb09052003-03-11 02:58:59 +00001045 }
1046 }
ggarenec11e5b2007-02-25 02:14:54 +00001047
mitz@apple.com15035e62008-07-05 20:44:44 +00001048 resolver.deleteRuns();
hyatt35a85e52003-02-14 19:43:07 +00001049
ap9059f6f2006-07-24 16:55:02 +00001050 if (lineBox) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001051 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
apddd2ff42007-03-31 08:26:24 +00001052 if (useRepaintBounds) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001053 repaintTop = min(repaintTop, lineBox->topVisibleOverflow());
1054 repaintBottom = max(repaintBottom, lineBox->bottomVisibleOverflow());
ap9059f6f2006-07-24 16:55:02 +00001055 }
1056 }
mitz@apple.come1364202008-02-28 01:06:41 +00001057
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001058 firstLine = false;
mitz@apple.com71e30842008-03-18 16:13:31 +00001059 newLine(clear);
darinb70665a2002-04-15 23:43:21 +00001060 }
mitz@apple.come1364202008-02-28 01:06:41 +00001061
mitz@apple.com7b089842008-03-22 06:57:28 +00001062 if (m_floatingObjects && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001063 if (lastFloat) {
1064 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1065 }
1066 m_floatingObjects->next();
1067 } else
1068 m_floatingObjects->first();
1069 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001070 if (f->m_bottom > lastHeight)
1071 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com93526592008-03-18 04:36:51 +00001072 ASSERT(f->m_renderer == floats[floatIndex].object);
mitz@apple.com40547b32008-03-18 04:04:34 +00001073 // If a float's geometry has changed, give up on syncing with clean lines.
mitz@apple.com93526592008-03-18 04:36:51 +00001074 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 +00001075 checkForEndLineMatch = false;
mitz@apple.com40547b32008-03-18 04:04:34 +00001076 floatIndex++;
1077 }
1078 lastFloat = m_floatingObjects->last();
1079 }
1080
hyatt@apple.comd885df72009-01-22 02:31:52 +00001081 lastHeight = height();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001082 lineMidpointState.reset();
mitz@apple.com15035e62008-07-05 20:44:44 +00001083 resolver.setPosition(end);
kociendabb0c24b2001-08-24 14:24:40 +00001084 }
mitz@apple.come1364202008-02-28 01:06:41 +00001085
hyatt0c3a9862004-02-23 21:26:26 +00001086 if (endLine) {
1087 if (endLineMatched) {
1088 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001089 int delta = height() - endLineYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001090 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1091 line->attachLine();
1092 if (delta) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001093 repaintTop = min(repaintTop, line->topVisibleOverflow() + min(delta, 0));
1094 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow() + max(delta, 0));
hyattda77c4b2004-06-17 18:09:49 +00001095 line->adjustPosition(0, delta);
ap9059f6f2006-07-24 16:55:02 +00001096 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001097 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1098 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1099 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1100 int floatTop = (*f)->y() - (*f)->marginTop();
mitz@apple.com40547b32008-03-18 04:04:34 +00001101 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001102 setHeight(floatTop + delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001103 positionNewFloats();
1104 }
1105 }
harrison208ea792005-07-29 23:42:59 +00001106 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001107 setHeight(lastRootBox()->blockHeight());
ap9059f6f2006-07-24 16:55:02 +00001108 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001109 // Delete all the remaining lines.
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001110 RootInlineBox* line = endLine;
hyatt0c3a9862004-02-23 21:26:26 +00001111 RenderArena* arena = renderArena();
1112 while (line) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001113 repaintTop = min(repaintTop, line->topVisibleOverflow());
1114 repaintBottom = max(repaintBottom, line->bottomVisibleOverflow());
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00001115 RootInlineBox* next = line->nextRootBox();
hyatt0c3a9862004-02-23 21:26:26 +00001116 line->deleteLine(arena);
1117 line = next;
1118 }
1119 }
1120 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001121 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001122 // In case we have a float on the last line, it might not be positioned up to now.
1123 // This has to be done before adding in the bottom border/padding, or the float will
1124 // include the padding incorrectly. -dwh
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001125 if (lastFloat) {
1126 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001127 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001128 m_floatingObjects->next();
1129 } else
1130 m_floatingObjects->first();
1131 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
1132 if (f->m_bottom > lastHeight)
1133 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com40547b32008-03-18 04:04:34 +00001134 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001135 lastFloat = m_floatingObjects->last();
mitz@apple.com40547b32008-03-18 04:04:34 +00001136 }
mitz@apple.com356e07c2009-07-07 21:36:21 +00001137 size_t floatCount = floats.size();
1138 // Floats that did not have layout did not repaint when we laid them out. They would have
1139 // painted by now if they had moved, but if they stayed at (0, 0), they still need to be
1140 // painted.
1141 for (size_t i = 0; i < floatCount; ++i) {
1142 if (!floats[i].everHadLayout) {
1143 RenderBox* f = floats[i].object;
1144 if (!f->x() && !f->y() && f->checkForRepaintDuringLayout())
1145 f->repaint();
1146 }
1147 }
kociendabb0c24b2001-08-24 14:24:40 +00001148 }
hyatt85586af2003-02-19 23:22:42 +00001149
hyatta70560a2002-11-20 01:53:20 +00001150 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001151 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +00001152
adele7a470a72006-04-20 22:22:14 +00001153 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +00001154 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +00001155
1156 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1157 // truncate text.
1158 if (hasTextOverflow)
1159 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001160}
1161
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001162RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, bool& previousLineBrokeCleanly,
1163 InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +00001164{
1165 RootInlineBox* curr = 0;
1166 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001167
mitz@apple.com40547b32008-03-18 04:04:34 +00001168 bool dirtiedByFloat = false;
1169 if (!fullLayout) {
1170 size_t floatIndex = 0;
1171 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001172 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
1173 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1174 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
1175 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +00001176 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
1177 ASSERT(floatIndex < floats.size());
1178 if (floats[floatIndex].object != f) {
1179 // A new float has been inserted before this line or before its last known float.
1180 // Just do a full layout.
1181 fullLayout = true;
1182 break;
1183 }
1184 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +00001185 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +00001186 curr->markDirty();
1187 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()));
1188 floats[floatIndex].rect.setSize(newSize);
1189 dirtiedByFloat = true;
1190 }
1191 floatIndex++;
1192 }
1193 }
1194 if (dirtiedByFloat || fullLayout)
1195 break;
1196 }
1197 // Check if a new float has been inserted after the last known float.
1198 if (!curr && floatIndex < floats.size())
1199 fullLayout = true;
1200 }
1201
hyatt0c3a9862004-02-23 21:26:26 +00001202 if (fullLayout) {
1203 // Nuke all our lines.
1204 if (firstRootBox()) {
1205 RenderArena* arena = renderArena();
1206 curr = firstRootBox();
1207 while (curr) {
1208 RootInlineBox* next = curr->nextRootBox();
1209 curr->deleteLine(arena);
1210 curr = next;
1211 }
darinec375482007-01-06 01:36:24 +00001212 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001213 }
eseidel789896f2005-11-27 22:52:09 +00001214 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001215 if (curr) {
1216 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001217 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001218 // We have a previous line.
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001219 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || (prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength())))
mjs9f78dd92007-02-12 04:06:07 +00001220 // The previous line didn't break cleanly or broke at a newline
1221 // that has been deleted, so treat it as dirty too.
1222 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001223 }
eseidel789896f2005-11-27 22:52:09 +00001224 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001225 // No dirty lines were found.
1226 // If the last line didn't break cleanly, treat it as dirty.
1227 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1228 curr = lastRootBox();
1229 }
mitz@apple.come1364202008-02-28 01:06:41 +00001230
hyatt0c3a9862004-02-23 21:26:26 +00001231 // If we have no dirty lines, then last is just the last root box.
1232 last = curr ? curr->prevRootBox() : lastRootBox();
1233 }
mitz@apple.come1364202008-02-28 01:06:41 +00001234
mitz@apple.com40547b32008-03-18 04:04:34 +00001235 numCleanFloats = 0;
1236 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001237 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +00001238 // Restore floats from clean lines.
1239 RootInlineBox* line = firstRootBox();
1240 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001241 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1242 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1243 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001244 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001245 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +00001246 positionNewFloats();
1247 ASSERT(floats[numCleanFloats].object == *f);
1248 numCleanFloats++;
1249 }
1250 }
1251 line = line->nextRootBox();
1252 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001253 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001254 }
1255
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001256 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001257 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001258
1259 RenderObject* startObj;
1260 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001261 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001262 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001263 startObj = last->lineBreakObj();
1264 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001265 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001266 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001267 bool ltr = style()->direction() == LTR
1268 #if ENABLE(SVG)
1269 || (style()->unicodeBidi() == UBNormal && isSVGText())
1270 #endif
1271 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001272
mitz@apple.com45d9e102009-04-27 16:24:55 +00001273 Direction direction = ltr ? LeftToRight : RightToLeft;
1274 resolver.setLastStrongDir(direction);
1275 resolver.setLastDir(direction);
1276 resolver.setEorDir(direction);
1277 resolver.setContext(BidiContext::create(ltr ? 0 : 1, direction, style()->unicodeBidi() == Override));
mitz@apple.com1a301772008-03-11 18:30:36 +00001278
mitz@apple.com15035e62008-07-05 20:44:44 +00001279 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001280 }
mitz@apple.come1364202008-02-28 01:06:41 +00001281
mitz@apple.com15035e62008-07-05 20:44:44 +00001282 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001283
hyatt0c3a9862004-02-23 21:26:26 +00001284 return curr;
1285}
1286
mitz@apple.com15035e62008-07-05 20:44:44 +00001287RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001288{
1289 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001290 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001291 last = 0;
1292 else {
hyatt04420ca2004-07-16 00:05:42 +00001293 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1294 if (curr->isDirty())
1295 last = 0;
1296 else if (!last)
1297 last = curr;
1298 }
hyatt0c3a9862004-02-23 21:26:26 +00001299 }
mitz@apple.come1364202008-02-28 01:06:41 +00001300
hyatt0c3a9862004-02-23 21:26:26 +00001301 if (!last)
1302 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001303
eseidel789896f2005-11-27 22:52:09 +00001304 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001305 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001306 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001307 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001308
hyatt0c3a9862004-02-23 21:26:26 +00001309 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1310 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1311 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001312
hyatt0c3a9862004-02-23 21:26:26 +00001313 return last;
1314}
1315
mitz@apple.com15035e62008-07-05 20:44:44 +00001316bool 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 +00001317{
mitz@apple.com15035e62008-07-05 20:44:44 +00001318 if (resolver.position() == endLineStart) {
1319 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001320 return false;
1321
hyatt@apple.comd885df72009-01-22 02:31:52 +00001322 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001323 if (!delta || !m_floatingObjects)
1324 return true;
1325
1326 // 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 +00001327 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001328
1329 RootInlineBox* lastLine = endLine;
1330 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1331 lastLine = nextLine;
1332
1333 int bottom = lastLine->blockHeight() + abs(delta);
1334
1335 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001336 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001337 return false;
1338 }
1339
1340 return true;
1341 }
hyatt0c3a9862004-02-23 21:26:26 +00001342
mitz@apple.come1364202008-02-28 01:06:41 +00001343 // The first clean line doesn't match, but we can check a handful of following lines to try
1344 // to match back up.
1345 static int numLines = 8; // The # of lines we're willing to match against.
1346 RootInlineBox* line = endLine;
1347 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001348 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001349 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001350 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001351 return false; // ...but the bidi state doesn't match.
1352 RootInlineBox* result = line->nextRootBox();
1353
1354 // Set our yPos to be the block height of endLine.
1355 if (result)
1356 endYPos = line->blockHeight();
1357
hyatt@apple.comd885df72009-01-22 02:31:52 +00001358 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001359 if (delta && m_floatingObjects) {
1360 // 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 +00001361 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001362
1363 RootInlineBox* lastLine = endLine;
1364 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1365 lastLine = nextLine;
1366
1367 int bottom = lastLine->blockHeight() + abs(delta);
1368
1369 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001370 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001371 return false;
1372 }
1373 }
1374
mitz@apple.come1364202008-02-28 01:06:41 +00001375 // Now delete the lines that we failed to sync.
1376 RootInlineBox* boxToDelete = endLine;
1377 RenderArena* arena = renderArena();
1378 while (boxToDelete && boxToDelete != result) {
hyatt@apple.comc37b4bb2009-08-19 19:26:32 +00001379 repaintTop = min(repaintTop, boxToDelete->topVisibleOverflow());
1380 repaintBottom = max(repaintBottom, boxToDelete->bottomVisibleOverflow());
mitz@apple.come1364202008-02-28 01:06:41 +00001381 RootInlineBox* next = boxToDelete->nextRootBox();
1382 boxToDelete->deleteLine(arena);
1383 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001384 }
mitz@apple.come1364202008-02-28 01:06:41 +00001385
1386 endLine = result;
1387 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001388 }
1389 }
mitz@apple.come1364202008-02-28 01:06:41 +00001390
hyatt0c3a9862004-02-23 21:26:26 +00001391 return false;
1392}
1393
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001394static inline bool skipNonBreakingSpace(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
kocienda98440082004-10-14 23:51:47 +00001395{
darinf9e5d6c2007-01-09 14:54:26 +00001396 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001397 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001398
hyattdca76e92005-11-02 08:52:50 +00001399 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1400 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001401 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001402 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1403 // |true|).
1404 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001405 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001406
kocienda498d1982004-10-15 21:07:24 +00001407 return true;
kocienda98440082004-10-14 23:51:47 +00001408}
1409
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001410static inline bool shouldCollapseWhiteSpace(const RenderStyle* style, bool isLineEmpty, bool previousLineBrokeCleanly)
hyattd9953212005-11-03 21:05:59 +00001411{
1412 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1413}
1414
zimmermannac3781f2007-02-04 01:25:03 +00001415static inline bool shouldPreserveNewline(RenderObject* object)
1416{
mjsd2948ef2007-02-26 19:29:04 +00001417#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001418 if (object->isSVGText())
1419 return false;
1420#endif
1421
1422 return object->style()->preserveNewline();
1423}
1424
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001425static bool inlineFlowRequiresLineBox(RenderInline* flow)
bdakinf876bee2007-10-30 05:27:09 +00001426{
1427 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001428 // We need to fix this, though, because at the very least, inlines containing only
1429 // ignorable whitespace should should also have line boxes.
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001430 return !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001431}
1432
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001433static inline bool requiresLineBox(const InlineIterator& it, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001434{
bdakinf876bee2007-10-30 05:27:09 +00001435 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001436 return false;
bdakinf876bee2007-10-30 05:27:09 +00001437
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001438 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderInline(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001439 return false;
1440
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001441 if (!shouldCollapseWhiteSpace(it.obj->style(), isLineEmpty, previousLineBrokeCleanly) || it.obj->isBR())
bdashccffb432007-07-13 11:51:40 +00001442 return true;
1443
1444 UChar current = it.current();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001445 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj))
1446 && !skipNonBreakingSpace(it, isLineEmpty, previousLineBrokeCleanly);
bdashccffb432007-07-13 11:51:40 +00001447}
1448
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001449bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj, bool isLineEmpty, bool previousLineBrokeCleanly)
bdashccffb432007-07-13 11:51:40 +00001450{
1451 ASSERT(inlineObj->parent() == this);
1452
mitz@apple.com15035e62008-07-05 20:44:44 +00001453 InlineIterator it(this, inlineObj, 0);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001454 while (!it.atEnd() && !requiresLineBox(it, isLineEmpty, previousLineBrokeCleanly))
mitz@apple.com1a301772008-03-11 18:30:36 +00001455 it.increment();
bdashccffb432007-07-13 11:51:40 +00001456
1457 return !it.atEnd();
1458}
1459
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001460// 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 +00001461// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1462// elements quite right. In other words, we need to build this function's work into the normal line
1463// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001464// NB. this function will insert any floating elements that would otherwise
1465// be skipped but it will not position them.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001466void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator, bool isLineEmpty, bool previousLineBrokeCleanly)
kociendabb0c24b2001-08-24 14:24:40 +00001467{
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001468 while (!iterator.atEnd() && !requiresLineBox(iterator, isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001469 RenderObject* object = iterator.obj;
1470 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001471 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001472 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001473 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1474 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001475 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001476 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001477 // A relative positioned inline encloses us. In this case, we also have to determine our
1478 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1479 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001480 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
1481 toRenderInline(c)->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001482 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001483
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001484 RenderBox* box = toRenderBox(object);
1485 if (box->style()->hasStaticX()) {
1486 if (box->style()->isOriginalDisplayInlineType())
1487 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001488 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001489 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001490 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001491
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001492 if (box->style()->hasStaticY())
1493 box->layer()->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001494 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001495 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001496 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001497}
bdashccffb432007-07-13 11:51:40 +00001498
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001499int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine, bool isLineEmpty, bool previousLineBrokeCleanly)
mitz@apple.com1a301772008-03-11 18:30:36 +00001500{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001501 int availableWidth = lineWidth(height(), firstLine);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001502 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position(), isLineEmpty, previousLineBrokeCleanly)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001503 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001504 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001505 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001506 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001507 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001508 } else if (object->isPositioned()) {
1509 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1510 // will work for the common cases
1511 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001512 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001513 // A relative positioned inline encloses us. In this case, we also have to determine our
1514 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1515 // inline so that we can obtain the value later.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001516 toRenderInline(c)->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
1517 toRenderInline(c)->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001518 }
1519
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001520 RenderBox* box = toRenderBox(object);
1521 if (box->style()->hasStaticX()) {
bfulgham@webkit.orga519d8a2009-02-08 02:10:55 +00001522 if (box->style()->isOriginalDisplayInlineType())
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001523 box->layer()->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001524 else
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001525 box->layer()->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
mitz@apple.com1a301772008-03-11 18:30:36 +00001526 }
1527
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001528 if (box->style()->hasStaticY())
1529 box->layer()->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001530 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001531 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001532 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001533 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001534 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001535}
1536
bdakinf876bee2007-10-30 05:27:09 +00001537// This is currently just used for list markers and inline flows that have line boxes. Neither should
1538// have an effect on whitespace at the start of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001539static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o, LineMidpointState& lineMidpointState)
bdakinf876bee2007-10-30 05:27:09 +00001540{
mitz@apple.com1a301772008-03-11 18:30:36 +00001541 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001542 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1543 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001544 UChar nextChar = nextText->characters()[0];
1545 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001546 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001547 return true;
1548 }
1549 }
1550
1551 return false;
1552}
1553
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001554void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001555{
1556 ASSERT(widthToFit > availableWidth);
1557
1558 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001559 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001560 int newLineWidth = availableWidth;
1561 while (true) {
1562 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1563 if (!floatBottom)
1564 break;
1565
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001566 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001567 lastFloatBottom = floatBottom;
1568 if (newLineWidth >= widthToFit)
1569 break;
1570 }
1571
1572 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001573 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001574 availableWidth = newLineWidth;
1575 }
1576}
1577
mitz@apple.com34106442009-02-01 06:23:39 +00001578static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1579{
staikos@webkit.org19d8c5f2009-03-26 14:24:15 +00001580 if (isFixedPitch || (!from && len == text->textLength()))
mitz@apple.com34106442009-02-01 06:23:39 +00001581 return text->width(from, len, font, xPos);
1582 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1583}
1584
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001585InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, bool& isLineEmpty, bool& previousLineBrokeCleanly,
1586 EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001587{
mitz@apple.com15035e62008-07-05 20:44:44 +00001588 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001589
mitz@apple.com15035e62008-07-05 20:44:44 +00001590 bool appliedStartWidth = resolver.position().pos > 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001591 LineMidpointState& lineMidpointState = resolver.midpointState();
1592
1593 int width = skipLeadingWhitespace(resolver, firstLine, isLineEmpty, previousLineBrokeCleanly);
mitz@apple.com1a301772008-03-11 18:30:36 +00001594
kociendae40cb942004-10-05 20:05:38 +00001595 int w = 0;
1596 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001597
mitz@apple.com15035e62008-07-05 20:44:44 +00001598 if (resolver.position().atEnd())
1599 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001600
hyatt33f8d492002-11-12 21:44:52 +00001601 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1602 // or not we are currently ignoring whitespace.
1603 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001604 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001605
1606 // This variable tracks whether the very last character we saw was a space. We use
1607 // this to detect when we encounter a second space so we know we have to terminate
1608 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001609 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001610 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001611 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001612
mitz@apple.com15035e62008-07-05 20:44:44 +00001613 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001614
mitz@apple.com15035e62008-07-05 20:44:44 +00001615 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001616 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001617 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001618 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001619 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001620
hyatt0c3a9862004-02-23 21:26:26 +00001621 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1622 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001623
1624 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001625 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001626
hyatt@apple.com69340902008-01-16 21:24:21 +00001627 // 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 +00001628 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001629 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
1630 bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
1631
hyattb0d9f602007-01-15 01:28:23 +00001632 EWhiteSpace currWS = style()->whiteSpace();
1633 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001634 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001635 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1636 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1637
1638 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001639 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001640
mjsd2948ef2007-02-26 19:29:04 +00001641#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001642 bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
1643#else
hyattb0d9f602007-01-15 01:28:23 +00001644 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001645#endif
1646
hyattb0d9f602007-01-15 01:28:23 +00001647 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1648
hyatt275d0702005-11-03 23:53:57 +00001649 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001650 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001651 lBreak.obj = o;
1652 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001653 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001654 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001655
hyatt33f8d492002-11-12 21:44:52 +00001656 // A <br> always breaks a line, so don't let the line be collapsed
1657 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001658 // get collapsed away. It only does this if the previous line broke
1659 // cleanly. Otherwise the <br> has no effect on whether the line is
1660 // empty or not.
1661 if (prevLineBrokeCleanly)
1662 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001663 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001664 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001665
mitz@apple.com71e30842008-03-18 16:13:31 +00001666 if (!isLineEmpty && clear)
1667 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001668 }
1669 goto end;
1670 }
hyattb0d9f602007-01-15 01:28:23 +00001671
hyatt275d0702005-11-03 23:53:57 +00001672 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001673 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001674 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001675 RenderBox* floatBox = toRenderBox(o);
1676 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001677 // check if it fits in the current line.
1678 // If it does, position it now, otherwise, position
1679 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001680 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001681 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001682 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001683 } else
1684 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001685 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001686 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001687 // go ahead and determine our static x position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001688 RenderBox* box = toRenderBox(o);
1689 bool isInlineType = box->style()->isOriginalDisplayInlineType();
1690 bool needToSetStaticX = box->style()->hasStaticX();
1691 if (box->style()->hasStaticX() && !isInlineType) {
1692 box->layer()->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001693 borderLeft() + paddingLeft() :
1694 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001695 needToSetStaticX = false;
1696 }
1697
1698 // If our original display was an INLINE type, then we can go ahead
1699 // and determine our static y position now.
hyatt@apple.com0de4d642009-02-05 22:26:53 +00001700 bool needToSetStaticY = box->style()->hasStaticY();
1701 if (box->style()->hasStaticY() && isInlineType) {
1702 box->layer()->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001703 needToSetStaticY = false;
1704 }
1705
hyatt853cd7d2004-11-05 02:59:48 +00001706 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1707 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001708 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001709 needToCreateLineBox = true;
1710
hyatt98ee7e42003-05-14 01:39:15 +00001711 // If we're ignoring spaces, we have to stop and include this object and
1712 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001713 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001714 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001715 ignoreStart.obj = o;
1716 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001717 if (ignoringSpaces) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001718 addMidpoint(lineMidpointState, ignoreStart); // Stop ignoring spaces.
1719 addMidpoint(lineMidpointState, ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001720 }
hyatt98b16282004-03-31 18:43:12 +00001721
hyatt851816b2003-07-08 07:54:17 +00001722 }
hyatt98ee7e42003-05-14 01:39:15 +00001723 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001724 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001725 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001726 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001727
hyatt@apple.com0d4818f2009-02-08 05:39:22 +00001728 RenderInline* flowBox = toRenderInline(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001729
bdakinf876bee2007-10-30 05:27:09 +00001730 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1731 // to make sure that we stop to include this object and then start ignoring spaces again.
1732 // If this object is at the start of the line, we need to behave like list markers and
1733 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001734 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001735 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001736 if (ignoringSpaces) {
1737 trailingSpaceObject = 0;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001738 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Stop ignoring spaces.
1739 addMidpoint(lineMidpointState, InlineIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001740 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001741 && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001742 // Like with list markers, we start ignoring spaces to make sure that any
1743 // additional spaces we see will be discarded.
1744 currentCharacterIsSpace = true;
1745 currentCharacterIsWS = true;
1746 ignoringSpaces = true;
1747 }
1748 }
1749
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001750 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1751 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001752 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001753 RenderBox* replacedBox = toRenderBox(o);
1754
hyattde396342003-10-29 08:57:20 +00001755 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001756 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001757 w += tmpW;
1758 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001759 lBreak.obj = o;
1760 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001761 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001762 }
1763
mitz@apple.combfdc9112008-02-21 19:59:40 +00001764 if (ignoringSpaces)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001765 addMidpoint(lineMidpointState, InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001766
hyatt33f8d492002-11-12 21:44:52 +00001767 isLineEmpty = false;
1768 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001769 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001770 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001771 trailingSpaceObject = 0;
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001772
bdakinf876bee2007-10-30 05:27:09 +00001773 // Optimize for a common case. If we can't find whitespace after the list
1774 // item, then this is all moot. -dwh
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001775 if (o->isListMarker()) {
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001776 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o, lineMidpointState)) {
bdakinf876bee2007-10-30 05:27:09 +00001777 // Like with inline flows, we start ignoring spaces to make sure that any
1778 // additional spaces we see will be discarded.
1779 currentCharacterIsSpace = true;
1780 currentCharacterIsWS = true;
1781 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001782 }
hamaji@chromium.org382642b2009-12-01 07:37:14 +00001783 if (toRenderListMarker(o)->isInside())
1784 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
justing244d3a32006-04-13 01:31:24 +00001785 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001786 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001787 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001788 if (!pos)
1789 appliedStartWidth = false;
1790
darin@apple.com36744d62009-01-25 20:23:04 +00001791 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001792
darin42563ac52007-01-22 17:28:57 +00001793 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001794 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001795 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001796
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001797 const Font& f = t->style(firstLine)->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001798 bool isFixedPitch = f.isFixedPitch();
weinigf28a1c32007-02-14 14:10:31 +00001799
hyatt33f8d492002-11-12 21:44:52 +00001800 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001801 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001802 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001803
darin54008922006-01-13 16:39:05 +00001804 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001805 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001806 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1807 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1808 // 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 +00001809 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001810 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001811 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1812
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001813 if (t->isWordBreak()) {
1814 w += tmpW;
1815 tmpW = 0;
1816 lBreak.obj = o;
1817 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001818 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001819 ASSERT(!len);
1820 }
1821
hyatt275d0702005-11-03 23:53:57 +00001822 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001823 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001824 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001825 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001826 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001827
hyattb0d9f602007-01-15 01:28:23 +00001828 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001829 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001830
hyatt78b85132004-03-29 20:07:45 +00001831 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001832 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001833 if (!ignoringSpaces) {
1834 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001835 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001836 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001837 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001838 else
darin@apple.com36744d62009-01-25 20:23:04 +00001839 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001840 // Two consecutive soft hyphens. Avoid overlapping midpoints.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00001841 if (lineMidpointState.numMidpoints && lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].obj == o &&
1842 lineMidpointState.midpoints[lineMidpointState.numMidpoints - 1].pos == pos)
1843 lineMidpointState.numMidpoints--;
bdakin9151ba52005-10-24 22:51:06 +00001844 else
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001845 addMidpoint(lineMidpointState, beforeSoftHyphen);
mitz@apple.combe429562008-03-07 01:09:51 +00001846
hyatt78b85132004-03-29 20:07:45 +00001847 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001848 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001849
hyattdca76e92005-11-02 08:52:50 +00001850 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001851 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001852 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001853 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001854
mitz@apple.com15035e62008-07-05 20:44:44 +00001855 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001856 afterSoftHyphen.increment();
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001857 addMidpoint(lineMidpointState, afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001858 }
mitz@apple.combe429562008-03-07 01:09:51 +00001859
hyatt78b85132004-03-29 20:07:45 +00001860 pos++;
1861 len--;
eseideld13fe532005-11-30 02:40:29 +00001862 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001863 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
1864 continue;
1865 }
1866
hyatt3aff2332003-01-23 01:15:28 +00001867 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001868
darinf9e5d6c2007-01-09 14:54:26 +00001869 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001870
weiniged111c12007-07-13 22:45:11 +00001871 if ((breakAll || breakWords) && !midWordBreak) {
1872 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001873 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001874 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001875 }
darin47ece0d2005-09-04 07:42:31 +00001876
ddkilzere8759ef2007-03-25 06:28:19 +00001877 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001878
weiniged111c12007-07-13 22:45:11 +00001879 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001880 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001881 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001882 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001883 // Stop ignoring spaces and begin at this
1884 // new point.
hyatt48710d62003-08-21 09:17:13 +00001885 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001886 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001887 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001888 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001889 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001890 } else {
hyatt33f8d492002-11-12 21:44:52 +00001891 // Just keep ignoring these spaces.
1892 pos++;
1893 len--;
1894 continue;
1895 }
1896 }
rjwc9c257d2003-01-24 03:46:17 +00001897
mitz@apple.com34106442009-02-01 06:23:39 +00001898 int additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001899 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001900 if (!appliedStartWidth) {
1901 tmpW += inlineWidth(o, true, false);
1902 appliedStartWidth = true;
1903 }
1904
eseideld13fe532005-11-30 02:40:29 +00001905 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001906
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001907 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001908 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001909
hyattb0d9f602007-01-15 01:28:23 +00001910 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001911 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001912 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001913 bool lineWasTooWide = false;
1914 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001915 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001916 // Check if line is too big even without the extra space
1917 // at the end of the line. If it is not, do nothing.
1918 // If the line needs the extra whitespace to be too long,
1919 // then move the line break to the space and skip all
1920 // additional whitespace.
1921 if (w + tmpW + charWidth > width) {
1922 lineWasTooWide = true;
1923 lBreak.obj = o;
1924 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001925 lBreak.nextBreakablePosition = nextBreakable;
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001926 skipTrailingWhitespace(lBreak, isLineEmpty, previousLineBrokeCleanly);
kocienda9dbe9b12004-10-22 20:07:05 +00001927 }
ap932806a2006-10-01 09:06:09 +00001928 }
1929 if (lineWasTooWide || w + tmpW > width) {
eric@webkit.org1cb44402009-12-29 06:25:16 +00001930 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 +00001931 if (!stoppedIgnoringSpaces && pos > 0) {
1932 // We need to stop right before the newline and then start up again.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001933 addMidpoint(lineMidpointState, InlineIterator(0, o, pos - 1)); // Stop
1934 addMidpoint(lineMidpointState, InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001935 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001936 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001937 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001938 }
hyatt78b85132004-03-29 20:07:45 +00001939 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001940 } else {
weiniged111c12007-07-13 22:45:11 +00001941 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001942 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001943 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001944 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001945 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001946 }
rjwc9c257d2003-01-24 03:46:17 +00001947 }
hyatt33f8d492002-11-12 21:44:52 +00001948
hyattb0d9f602007-01-15 01:28:23 +00001949 if (c == '\n' && preserveNewline) {
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 }
hyatta9f48e32003-02-03 22:48:01 +00001955 lBreak.obj = o;
1956 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001957 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001958 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001959 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001960 return lBreak;
1961 }
hyatta9f48e32003-02-03 22:48:01 +00001962
weinigf28a1c32007-02-14 14:10:31 +00001963 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001964 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001965 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001966 tmpW = 0;
1967 lBreak.obj = o;
1968 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001969 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001970 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1971 // opportunity to break after a word.
1972 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001973 }
hyatt33f8d492002-11-12 21:44:52 +00001974
darin54008922006-01-13 16:39:05 +00001975 if (midWordBreak) {
1976 // Remember this as a breakable position in case
1977 // adding the end width forces a break.
1978 lBreak.obj = o;
1979 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001980 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001981 midWordBreak &= (breakWords || breakAll);
1982 }
1983
1984 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001985 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1986 lastSpace = pos;
1987 }
hyatt33f8d492002-11-12 21:44:52 +00001988
hyattdca76e92005-11-02 08:52:50 +00001989 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001990 // If we encounter a newline, or if we encounter a
1991 // second space, we need to go ahead and break up this
1992 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001993 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001994 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001995
hyatt33f8d492002-11-12 21:44:52 +00001996 // We just entered a mode where we are ignoring
1997 // spaces. Create a midpoint to terminate the run
1998 // before the second space.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00001999 addMidpoint(lineMidpointState, ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00002000 }
2001 }
eseidel789896f2005-11-27 22:52:09 +00002002 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002003 // Stop ignoring spaces and begin at this
2004 // new point.
2005 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002006 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00002007 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002008 addMidpoint(lineMidpointState, InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00002009 }
hyatt98b16282004-03-31 18:43:12 +00002010
2011 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
2012 ignoreStart.obj = o;
2013 ignoreStart.pos = pos;
2014 }
harrisone343c412005-01-18 01:07:26 +00002015
2016 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00002017 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00002018 lBreak.obj = o;
2019 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002020 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00002021 }
2022 }
hyatt33f8d492002-11-12 21:44:52 +00002023
hyattb0d9f602007-01-15 01:28:23 +00002024 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00002025 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00002026 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00002027 trailingSpaceObject = 0;
2028
2029 pos++;
2030 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00002031 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00002032 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002033
kociendabb0c24b2001-08-24 14:24:40 +00002034 // IMPORTANT: pos is > length here!
hyatt33f8d492002-11-12 21:44:52 +00002035 if (!ignoringSpaces)
mitz@apple.com34106442009-02-01 06:23:39 +00002036 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
weinigf28a1c32007-02-14 14:10:31 +00002037 tmpW += inlineWidth(o, !appliedStartWidth, true);
kociendabb0c24b2001-08-24 14:24:40 +00002038 } else
weinigf28a1c32007-02-14 14:10:31 +00002039 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00002040
mitz@apple.com1a301772008-03-11 18:30:36 +00002041 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00002042 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00002043 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002044 checkForBreak = true;
2045 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00002046 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002047 if (currentCharacterIsSpace)
2048 checkForBreak = true;
2049 else {
harrison07b5e582005-08-15 23:31:16 +00002050 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00002051 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002052 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00002053 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00002054 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00002055 // If the next item on the line is text, and if we did not end with
2056 // a space, then the next text run continues our word (and so it needs to
2057 // keep adding to |tmpW|. Just update and continue.
2058 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002059 } else if (nextText->isWordBreak())
2060 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002061 bool willFitOnLine = w + tmpW <= width;
2062 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002063 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002064 willFitOnLine = tmpW <= width;
2065 }
ddkilzere8759ef2007-03-25 06:28:19 +00002066 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00002067 if (canPlaceOnLine && checkForBreak) {
2068 w += tmpW;
2069 tmpW = 0;
2070 lBreak.obj = next;
2071 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002072 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00002073 }
2074 }
2075 }
2076 }
2077
darin54008922006-01-13 16:39:05 +00002078 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002079 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002080 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002081 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002082
2083 if (w)
2084 goto end;
2085
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002086 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00002087
hyatta14d1742003-01-02 20:25:46 +00002088 // |width| may have been adjusted because we got shoved down past a float (thus
2089 // giving us more room), so we need to retest, and only jump to
2090 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002091 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002092 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002093 }
hyatt1d9e29b2003-04-10 01:48:53 +00002094
mitz@apple.com1a301772008-03-11 18:30:36 +00002095 if (!o->isFloatingOrPositioned()) {
2096 last = o;
darin@apple.comb6cb2562009-08-05 21:25:09 +00002097 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || toRenderListMarker(last)->isInside())) {
mitz@apple.com1a301772008-03-11 18:30:36 +00002098 w += tmpW;
2099 tmpW = 0;
2100 lBreak.obj = next;
2101 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002102 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002103 }
hyatt711fe232002-11-20 21:25:14 +00002104 }
2105
mitz@apple.com1a301772008-03-11 18:30:36 +00002106 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002107 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002108
hyatta9f48e32003-02-03 22:48:01 +00002109 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2110 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002111 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002112 currentCharacterIsSpace = false;
2113
kociendabb0c24b2001-08-24 14:24:40 +00002114 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002115 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002116 }
2117
hyattb0d9f602007-01-15 01:28:23 +00002118
2119 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00002120 lBreak.obj = 0;
2121 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002122 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002123 }
2124
2125 end:
eric@webkit.orgee76f4f2009-03-25 22:14:46 +00002126 if (lBreak == resolver.position() && (!lBreak.obj || !lBreak.obj->isBR())) {
kociendabb0c24b2001-08-24 14:24:40 +00002127 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002128 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002129 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002130 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002131 lBreak.obj = o;
2132 lBreak.pos = pos - 1;
2133 } else {
2134 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002135 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002136 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002137 }
hyatt275d0702005-11-03 23:53:57 +00002138 } else if (lBreak.obj) {
2139 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002140 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002141 lBreak.obj = o;
2142 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002143 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002144 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002145 // Don't ever break in the middle of a word if we can help it.
2146 // There's no room at all. We just have to be on this line,
2147 // even though we'll spill out.
2148 lBreak.obj = o;
2149 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002150 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002151 }
kociendabb0c24b2001-08-24 14:24:40 +00002152 }
2153 }
2154
2155 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002156 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002157 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002158
hyattfe99c872003-07-31 22:25:29 +00002159 // Sanity check our midpoints.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002160 checkMidpoints(lineMidpointState, lBreak);
hyattfe99c872003-07-31 22:25:29 +00002161
hyatt33f8d492002-11-12 21:44:52 +00002162 if (trailingSpaceObject) {
2163 // This object is either going to be part of the last midpoint, or it is going
2164 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2165 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt@apple.com1a5ffd82009-06-13 17:20:30 +00002166 if (lineMidpointState.numMidpoints % 2) {
2167 InlineIterator* midpoints = lineMidpointState.midpoints.data();
2168 midpoints[lineMidpointState.numMidpoints - 1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002169 }
2170 //else if (lBreak.pos > 0)
2171 // lBreak.pos--;
2172 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002173 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00002174 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002175 unsigned length = text->textLength();
2176 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00002177 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002178 addMidpoint(lineMidpointState, endMid);
hyatt33f8d492002-11-12 21:44:52 +00002179 }
2180 }
rjwc9c257d2003-01-24 03:46:17 +00002181
mjs54b64002003-04-02 02:59:02 +00002182 // We might have made lBreak an iterator that points past the end
2183 // of the object. Do this adjustment to make it point to the start
2184 // of the next object instead to avoid confusing the rest of the
2185 // code.
2186 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002187 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002188 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002189 }
2190
hyatt78b85132004-03-29 20:07:45 +00002191 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2192 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2193 // ignore the hyphen so that it will render at the end of the line.
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002194 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos - 1];
darin42563ac52007-01-22 17:28:57 +00002195 if (c == softHyphen)
hyatt@apple.comb3466af2009-06-13 06:04:40 +00002196 chopMidpointsAt(lineMidpointState, lBreak.obj, lBreak.pos - 2);
hyatt78b85132004-03-29 20:07:45 +00002197 }
2198
kociendabb0c24b2001-08-24 14:24:40 +00002199 return lBreak;
2200}
2201
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002202void RenderBlock::addOverflowFromInlineChildren()
hyattb4b20872004-10-20 21:34:01 +00002203{
hyattb4b20872004-10-20 21:34:01 +00002204 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.com5dc5a312009-08-18 19:15:19 +00002205 addLayoutOverflow(curr->layoutOverflowRect());
2206 if (!hasOverflowClip())
2207 addVisualOverflow(curr->visualOverflowRect());
hyattb4b20872004-10-20 21:34:01 +00002208 }
2209}
2210
hyatted77ad82004-06-15 07:21:23 +00002211void RenderBlock::deleteEllipsisLineBoxes()
2212{
hyatted77ad82004-06-15 07:21:23 +00002213 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002214 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002215}
2216
2217void RenderBlock::checkLinesForTextOverflow()
2218{
2219 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002220 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2221 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002222 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002223 const Font& firstLineFont = firstLineStyle()->font();
2224 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002225 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2226 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002227
2228 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2229 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2230 // check the left edge of the line box to see if it is less
2231 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2232 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002233 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002234 int blockRightEdge = rightOffset(curr->y(), curr == firstRootBox());
2235 int blockLeftEdge = leftOffset(curr->y(), curr == firstRootBox());
hyatt@apple.com5631f112009-02-11 01:36:45 +00002236 int lineBoxEdge = ltr ? curr->x() + curr->width() : curr->x();
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002237 if ((ltr && lineBoxEdge > blockRightEdge) || (!ltr && lineBoxEdge < blockLeftEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002238 // 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 +00002239 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2240 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2241 // space.
2242 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002243 int blockEdge = ltr ? blockRightEdge : blockLeftEdge;
hyatt1a8d2512004-06-17 01:38:30 +00002244 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
dglazkov@chromium.org28434e62009-05-13 22:30:10 +00002245 curr->placeEllipsis(ellipsisStr, ltr, blockLeftEdge, blockRightEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002246 }
2247 }
2248}
2249
hyattffe78712003-02-11 01:59:29 +00002250}