blob: 3f9cd73726464e48d1474d49615f20706f64fec2 [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)
darin@apple.com83833152008-01-14 17:51:10 +00003 * Copyright (C) 2004, 2006, 2007, 2008 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"
kociendabb0c24b2001-08-24 14:24:40 +000023#include "bidi.h"
darin36d11362006-04-11 16:30:21 +000024
darinf9e5d6c2007-01-09 14:54:26 +000025#include "CharacterNames.h"
darinb9481ed2006-03-20 02:57:59 +000026#include "Document.h"
eseidel40eb1b92006-03-25 22:20:36 +000027#include "Element.h"
mjsd4145d12006-01-11 09:36:47 +000028#include "FrameView.h"
eseidel3a6d1322006-01-09 03:14:50 +000029#include "InlineTextBox.h"
ggarenec11e5b2007-02-25 02:14:54 +000030#include "Logging.h"
darin36d11362006-04-11 16:30:21 +000031#include "RenderArena.h"
hyatt@apple.comc3c7e902009-01-28 21:48:33 +000032#include "RenderInline.h"
darinec375482007-01-06 01:36:24 +000033#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000034#include "RenderListMarker.h"
hyattd8048342006-05-31 01:48:18 +000035#include "RenderView.h"
darin36d11362006-04-11 16:30:21 +000036#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000037#include <wtf/AlwaysInline.h>
slewis@apple.coma7615ca2008-07-12 05:51:33 +000038#include <wtf/RefCountedLeakCounter.h>
bolsinga@apple.com97e42c42008-11-15 04:47:20 +000039#include <wtf/StdLibExtras.h>
darin91298e52006-06-12 01:10:17 +000040#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000041
darin7bd70952006-04-13 07:07:34 +000042using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000043using namespace WTF;
44using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000045
darinb9481ed2006-03-20 02:57:59 +000046namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000047
hyatt1d5d87b2007-04-24 04:55:54 +000048// We don't let our line box tree for a single line get any deeper than this.
49const unsigned cMaxLineDepth = 200;
50
mitz@apple.com15035e62008-07-05 20:44:44 +000051class InlineIterator {
pewtermoosecf72e2d2007-07-20 19:01:55 +000052public:
mitz@apple.com15035e62008-07-05 20:44:44 +000053 InlineIterator()
pewtermoosecf72e2d2007-07-20 19:01:55 +000054 : block(0)
55 , obj(0)
56 , pos(0)
mitz@apple.comd17dc392008-09-15 18:48:20 +000057 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000058 {
59 }
60
mitz@apple.com15035e62008-07-05 20:44:44 +000061 InlineIterator(RenderBlock* b, RenderObject* o, unsigned p)
pewtermoosecf72e2d2007-07-20 19:01:55 +000062 : block(b)
63 , obj(o)
64 , pos(p)
mitz@apple.comd17dc392008-09-15 18:48:20 +000065 , nextBreakablePosition(-1)
pewtermoosecf72e2d2007-07-20 19:01:55 +000066 {
67 }
68
mitz@apple.com15035e62008-07-05 20:44:44 +000069 void increment(InlineBidiResolver* resolver = 0);
mjsfe301d72003-10-02 18:46:18 +000070 bool atEnd() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000071
darin7ab31092006-05-10 04:59:57 +000072 UChar current() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000073 WTF::Unicode::Direction direction() const;
hyatt275d0702005-11-03 23:53:57 +000074
75 RenderBlock* block;
76 RenderObject* obj;
mitz@apple.com1a301772008-03-11 18:30:36 +000077 unsigned pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +000078 int nextBreakablePosition;
mjsfe301d72003-10-02 18:46:18 +000079};
80
hyatt85586af2003-02-19 23:22:42 +000081// Midpoint globals. The goal is not to do any allocation when dealing with
82// these midpoints, so we just keep an array around and never clear it. We track
83// the number of items and position using the two other variables.
mitz@apple.com15035e62008-07-05 20:44:44 +000084static Vector<InlineIterator>* smidpoints;
darinb9481ed2006-03-20 02:57:59 +000085static unsigned sNumMidpoints;
86static unsigned sCurrMidpoint;
mjsfe301d72003-10-02 18:46:18 +000087static bool betweenMidpoints;
hyatt85586af2003-02-19 23:22:42 +000088
hyatt33f8d492002-11-12 21:44:52 +000089static bool isLineEmpty = true;
hyatt0c3a9862004-02-23 21:26:26 +000090static bool previousLineBrokeCleanly = true;
mjs6f821c82002-03-22 00:31:57 +000091
hyatt@apple.com774bbed2009-01-23 05:13:22 +000092static int getBorderPaddingMargin(RenderBox* child, bool endOfInline)
hyattffe78712003-02-11 01:59:29 +000093{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000094 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
95 if (leftSide)
96 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
97 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000098}
99
100static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
101{
hyatt1d5d87b2007-04-24 04:55:54 +0000102 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +0000103 int extraWidth = 0;
104 RenderObject* parent = child->parent();
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000105 while (parent->isBox() && parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyattffe78712003-02-11 01:59:29 +0000106 if (start && parent->firstChild() == child)
hyatt@apple.com0c305632009-01-23 23:25:07 +0000107 extraWidth += getBorderPaddingMargin(toRenderBox(parent), false);
hyattffe78712003-02-11 01:59:29 +0000108 if (end && parent->lastChild() == child)
hyatt@apple.com0c305632009-01-23 23:25:07 +0000109 extraWidth += getBorderPaddingMargin(toRenderBox(parent), true);
hyattffe78712003-02-11 01:59:29 +0000110 child = parent;
111 parent = child->parent();
112 }
113 return extraWidth;
114}
115
darin35355e52002-12-20 09:19:00 +0000116#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000117static WTF::RefCountedLeakCounter bidiRunCounter("BidiRun");
ggarenec11e5b2007-02-25 02:14:54 +0000118
harrison0012ced2005-10-06 18:37:42 +0000119static bool inBidiRunDestroy;
hyattffe78712003-02-11 01:59:29 +0000120#endif
121
mitz@apple.come1364202008-02-28 01:06:41 +0000122void BidiRun::destroy()
hyattffe78712003-02-11 01:59:29 +0000123{
124#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000125 inBidiRunDestroy = true;
hyattffe78712003-02-11 01:59:29 +0000126#endif
mitz@apple.come1364202008-02-28 01:06:41 +0000127 RenderArena* renderArena = m_object->renderArena();
hyattffe78712003-02-11 01:59:29 +0000128 delete this;
129#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000130 inBidiRunDestroy = false;
hyattffe78712003-02-11 01:59:29 +0000131#endif
132
133 // Recover the size left there for us by operator delete and free the memory.
mitz@apple.come1364202008-02-28 01:06:41 +0000134 renderArena->free(*reinterpret_cast<size_t*>(this), this);
hyattffe78712003-02-11 01:59:29 +0000135}
136
137void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
138{
ggarenec11e5b2007-02-25 02:14:54 +0000139#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000140 bidiRunCounter.increment();
ggarenec11e5b2007-02-25 02:14:54 +0000141#endif
hyattffe78712003-02-11 01:59:29 +0000142 return renderArena->allocate(sz);
143}
144
145void BidiRun::operator delete(void* ptr, size_t sz)
146{
ggarenec11e5b2007-02-25 02:14:54 +0000147#ifndef NDEBUG
slewis@apple.coma7615ca2008-07-12 05:51:33 +0000148 bidiRunCounter.decrement();
ggarenec11e5b2007-02-25 02:14:54 +0000149#endif
ggarenf9f32ae2007-03-26 20:08:53 +0000150 ASSERT(inBidiRunDestroy);
hyattffe78712003-02-11 01:59:29 +0000151
harrisone8363b42005-10-06 00:54:06 +0000152 // Stash size where destroy() can find it.
hyattffe78712003-02-11 01:59:29 +0000153 *(size_t*)ptr = sz;
154}
155
kociendabb0c24b2001-08-24 14:24:40 +0000156// ---------------------------------------------------------------------
157
mitz@apple.com15035e62008-07-05 20:44:44 +0000158inline bool operator==(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000159{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000160 return it1.pos == it2.pos && it1.obj == it2.obj;
darinb70665a2002-04-15 23:43:21 +0000161}
162
mitz@apple.com15035e62008-07-05 20:44:44 +0000163inline bool operator!=(const InlineIterator& it1, const InlineIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000164{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000165 return it1.pos != it2.pos || it1.obj != it2.obj;
darinb70665a2002-04-15 23:43:21 +0000166}
167
mitz@apple.com15035e62008-07-05 20:44:44 +0000168static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, InlineBidiResolver* resolver = 0, bool skipInlines = true, bool* endOfInlinePtr = 0)
mjs6f821c82002-03-22 00:31:57 +0000169{
hyatt275d0702005-11-03 23:53:57 +0000170 RenderObject* next = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +0000171 bool oldEndOfInline = endOfInlinePtr ? *endOfInlinePtr : false;
172 bool endOfInline = false;
hyattffe78712003-02-11 01:59:29 +0000173
hyatt275d0702005-11-03 23:53:57 +0000174 while (current) {
sullivanabd4d032007-02-09 22:51:41 +0000175 next = 0;
hyattffe78712003-02-11 01:59:29 +0000176 if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned()) {
177 next = current->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000178 if (next && resolver && next->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000179 EUnicodeBidi ub = next->style()->unicodeBidi();
hyatt275d0702005-11-03 23:53:57 +0000180 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000181 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000182 Direction d = (ub == Embed
183 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
184 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000185 resolver->embed(d);
hyattffe78712003-02-11 01:59:29 +0000186 }
187 }
188 }
hyatt275d0702005-11-03 23:53:57 +0000189
hyattffe78712003-02-11 01:59:29 +0000190 if (!next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000191 if (!skipInlines && !oldEndOfInline && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000192 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000193 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000194 break;
195 }
mjs6f821c82002-03-22 00:31:57 +0000196
hyatt275d0702005-11-03 23:53:57 +0000197 while (current && current != block) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000198 if (resolver && current->isRenderInline() && current->style()->unicodeBidi() != UBNormal)
mitz@apple.com15035e62008-07-05 20:44:44 +0000199 resolver->embed(PopDirectionalFormat);
hyatt275d0702005-11-03 23:53:57 +0000200
darindde01502005-12-18 22:55:35 +0000201 next = current->nextSibling();
202 if (next) {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000203 if (resolver && next->isRenderInline()) {
darindde01502005-12-18 22:55:35 +0000204 EUnicodeBidi ub = next->style()->unicodeBidi();
205 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000206 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000207 Direction d = (ub == Embed
208 ? (dir == RTL ? RightToLeftEmbedding: LeftToRightEmbedding)
209 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000210 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000211 }
212 }
213 break;
214 }
215
hyattffe78712003-02-11 01:59:29 +0000216 current = current->parent();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000217 if (!skipInlines && current && current != block && current->isRenderInline()) {
hyattffe78712003-02-11 01:59:29 +0000218 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000219 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000220 break;
221 }
222 }
223 }
mjs6f821c82002-03-22 00:31:57 +0000224
hyatt275d0702005-11-03 23:53:57 +0000225 if (!next)
226 break;
hyattffe78712003-02-11 01:59:29 +0000227
mitz@apple.combfdc9112008-02-21 19:59:40 +0000228 if (next->isText() || next->isFloating() || next->isReplaced() || next->isPositioned()
hyattffe78712003-02-11 01:59:29 +0000229 || ((!skipInlines || !next->firstChild()) // Always return EMPTY inlines.
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000230 && next->isRenderInline()))
mjs6f821c82002-03-22 00:31:57 +0000231 break;
232 current = next;
233 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000234
235 if (endOfInlinePtr)
236 *endOfInlinePtr = endOfInline;
237
mjs6f821c82002-03-22 00:31:57 +0000238 return next;
239}
240
mitz@apple.com15035e62008-07-05 20:44:44 +0000241static RenderObject* bidiFirst(RenderBlock* block, InlineBidiResolver* resolver, bool skipInlines = true)
mjs6f821c82002-03-22 00:31:57 +0000242{
hyatt275d0702005-11-03 23:53:57 +0000243 if (!block->firstChild())
244 return 0;
245
246 RenderObject* o = block->firstChild();
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000247 if (o->isRenderInline()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000248 if (resolver) {
darindde01502005-12-18 22:55:35 +0000249 EUnicodeBidi ub = o->style()->unicodeBidi();
250 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000251 TextDirection dir = o->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000252 Direction d = (ub == Embed
253 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
254 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com15035e62008-07-05 20:44:44 +0000255 resolver->embed(d);
darindde01502005-12-18 22:55:35 +0000256 }
257 }
hyattffe78712003-02-11 01:59:29 +0000258 if (skipInlines && o->firstChild())
mitz@apple.com15035e62008-07-05 20:44:44 +0000259 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000260 else {
261 // Never skip empty inlines.
262 if (resolver)
263 resolver->commitExplicitEmbedding();
264 return o;
265 }
hyattffe78712003-02-11 01:59:29 +0000266 }
hyattc5334f12003-08-08 22:26:08 +0000267
mitz@apple.combfdc9112008-02-21 19:59:40 +0000268 if (o && !o->isText() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
mitz@apple.com15035e62008-07-05 20:44:44 +0000269 o = bidiNext(block, o, resolver, skipInlines);
mitz@apple.com83d2e872008-10-23 21:56:03 +0000270
271 if (resolver)
272 resolver->commitExplicitEmbedding();
mjs6f821c82002-03-22 00:31:57 +0000273 return o;
274}
275
mitz@apple.com15035e62008-07-05 20:44:44 +0000276inline void InlineIterator::increment(InlineBidiResolver* resolver)
kociendabb0c24b2001-08-24 14:24:40 +0000277{
hyatt275d0702005-11-03 23:53:57 +0000278 if (!obj)
279 return;
280 if (obj->isText()) {
kociendabb0c24b2001-08-24 14:24:40 +0000281 pos++;
darin@apple.com36744d62009-01-25 20:23:04 +0000282 if (pos >= toRenderText(obj)->textLength()) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000283 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000284 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000285 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000286 }
287 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +0000288 obj = bidiNext(block, obj, resolver);
kociendabb0c24b2001-08-24 14:24:40 +0000289 pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +0000290 nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +0000291 }
292}
293
mitz@apple.com1a301772008-03-11 18:30:36 +0000294template<>
mitz@apple.com15035e62008-07-05 20:44:44 +0000295inline void InlineBidiResolver::increment()
mitz@apple.com1a301772008-03-11 18:30:36 +0000296{
297 current.increment(this);
298}
299
mitz@apple.com15035e62008-07-05 20:44:44 +0000300inline bool InlineIterator::atEnd() const
kociendabb0c24b2001-08-24 14:24:40 +0000301{
hyatt275d0702005-11-03 23:53:57 +0000302 return !obj;
kociendabb0c24b2001-08-24 14:24:40 +0000303}
304
mitz@apple.com15035e62008-07-05 20:44:44 +0000305inline UChar InlineIterator::current() const
kociendabb0c24b2001-08-24 14:24:40 +0000306{
hyatt30586b42003-12-02 23:19:11 +0000307 if (!obj || !obj->isText())
darin7ab31092006-05-10 04:59:57 +0000308 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000309
darin@apple.com36744d62009-01-25 20:23:04 +0000310 RenderText* text = toRenderText(obj);
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000311 if (pos >= text->textLength())
darin7ab31092006-05-10 04:59:57 +0000312 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000313
darin42563ac52007-01-22 17:28:57 +0000314 return text->characters()[pos];
kociendabb0c24b2001-08-24 14:24:40 +0000315}
316
mitz@apple.com15035e62008-07-05 20:44:44 +0000317ALWAYS_INLINE Direction InlineIterator::direction() const
kociendabb0c24b2001-08-24 14:24:40 +0000318{
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000319 if (UChar c = current())
320 return Unicode::direction(c);
321
322 if (obj && obj->isListMarker())
darinf9e5d6c2007-01-09 14:54:26 +0000323 return obj->style()->direction() == LTR ? LeftToRight : RightToLeft;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000324
325 return OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000326}
327
kociendabb0c24b2001-08-24 14:24:40 +0000328// -------------------------------------------------------------------------------------------------
329
darinb9481ed2006-03-20 02:57:59 +0000330static void chopMidpointsAt(RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +0000331{
hyatt275d0702005-11-03 23:53:57 +0000332 if (!sNumMidpoints)
333 return;
mitz@apple.com15035e62008-07-05 20:44:44 +0000334 InlineIterator* midpoints = smidpoints->data();
mitz@apple.comc92d1282008-02-28 05:38:52 +0000335 for (int i = sNumMidpoints - 1; i >= 0; i--) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000336 const InlineIterator& point = midpoints[i];
hyatt78b85132004-03-29 20:07:45 +0000337 if (point.obj == obj && point.pos == pos) {
338 sNumMidpoints = i;
339 break;
340 }
341 }
342}
343
mitz@apple.com15035e62008-07-05 20:44:44 +0000344static void checkMidpoints(InlineIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000345{
346 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000347 // shave it off the list, and shave off a trailing space if the previous end point doesn't
348 // preserve whitespace.
mitz@apple.combe429562008-03-07 01:09:51 +0000349 if (lBreak.obj && sNumMidpoints && sNumMidpoints % 2 == 0) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000350 InlineIterator* midpoints = smidpoints->data();
351 InlineIterator& endpoint = midpoints[sNumMidpoints-2];
352 const InlineIterator& startpoint = midpoints[sNumMidpoints-1];
353 InlineIterator currpoint = endpoint;
hyattfe99c872003-07-31 22:25:29 +0000354 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000355 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000356 if (currpoint == lBreak) {
357 // We hit the line break before the start point. Shave off the start point.
358 sNumMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000359 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000360 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000361 // Don't shave a character off the endpoint if it was from a soft hyphen.
darin@apple.com36744d62009-01-25 20:23:04 +0000362 RenderText* textObj = toRenderText(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000363 if (endpoint.pos + 1 < textObj->textLength()) {
364 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000365 return;
366 } else if (startpoint.obj->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000367 RenderText *startText = toRenderText(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000368 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000369 return;
370 }
hyattee1bcae2004-03-29 21:36:04 +0000371 }
hyattfe99c872003-07-31 22:25:29 +0000372 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000373 }
hyattfe99c872003-07-31 22:25:29 +0000374 }
375 }
376}
377
mitz@apple.com15035e62008-07-05 20:44:44 +0000378static void addMidpoint(const InlineIterator& midpoint)
hyatt85586af2003-02-19 23:22:42 +0000379{
hyatt85586af2003-02-19 23:22:42 +0000380 if (smidpoints->size() <= sNumMidpoints)
darin@apple.com83833152008-01-14 17:51:10 +0000381 smidpoints->grow(sNumMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000382
mitz@apple.com15035e62008-07-05 20:44:44 +0000383 InlineIterator* midpoints = smidpoints->data();
hyatt85586af2003-02-19 23:22:42 +0000384 midpoints[sNumMidpoints++] = midpoint;
385}
386
mitz@apple.com15035e62008-07-05 20:44:44 +0000387static void appendRunsForObject(int start, int end, RenderObject* obj, InlineBidiResolver& resolver)
hyatt33f8d492002-11-12 21:44:52 +0000388{
hyatt98ee7e42003-05-14 01:39:15 +0000389 if (start > end || obj->isFloating() ||
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000390 (obj->isPositioned() && !obj->hasStaticX() && !obj->hasStaticY() && !obj->container()->isRenderInline()))
hyatteb003b82002-11-15 22:35:10 +0000391 return;
hyatt85586af2003-02-19 23:22:42 +0000392
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000393 bool haveNextMidpoint = (sCurrMidpoint < sNumMidpoints);
mitz@apple.com15035e62008-07-05 20:44:44 +0000394 InlineIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000395 if (haveNextMidpoint)
396 nextMidpoint = smidpoints->at(sCurrMidpoint);
hyatt33f8d492002-11-12 21:44:52 +0000397 if (betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000398 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000399 return;
400 // This is a new start point. Stop ignoring objects and
401 // adjust our start.
402 betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000403 start = nextMidpoint.pos;
404 sCurrMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000405 if (start < end)
mitz@apple.com15035e62008-07-05 20:44:44 +0000406 return appendRunsForObject(start, end, obj, resolver);
407 } else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000408 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000409 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000410 return;
411 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000412
hyatt78b85132004-03-29 20:07:45 +0000413 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000414 // need to go ahead and append a run with our endpoint.
mitz@apple.com15035e62008-07-05 20:44:44 +0000415 if (static_cast<int>(nextMidpoint.pos + 1) <= end) {
hyatt26179432002-11-17 01:57:27 +0000416 betweenMidpoints = true;
hyatt85586af2003-02-19 23:22:42 +0000417 sCurrMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000418 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 +0000419 if (static_cast<int>(nextMidpoint.pos + 1) > start)
420 resolver.addRun(new (obj->renderArena())
421 BidiRun(start, nextMidpoint.pos + 1, obj, resolver.context(), resolver.dir()));
422 return appendRunsForObject(nextMidpoint.pos + 1, end, obj, resolver);
hyattc64f9fc2003-03-14 01:25:59 +0000423 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000424 } else
425 resolver.addRun(new (obj->renderArena()) BidiRun(start, end, obj, resolver.context(), resolver.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000426 }
427}
428
pewtermoosecf72e2d2007-07-20 19:01:55 +0000429template <>
mitz@apple.com15035e62008-07-05 20:44:44 +0000430void InlineBidiResolver::appendRun()
kociendabb0c24b2001-08-24 14:24:40 +0000431{
mitz@apple.com6970fb72008-07-03 21:37:53 +0000432 if (!emptyRun && !eor.atEnd()) {
433 int start = sor.pos;
434 RenderObject *obj = sor.obj;
435 while (obj && obj != eor.obj && obj != endOfLine.obj) {
436 appendRunsForObject(start, obj->length(), obj, *this);
437 start = 0;
438 obj = bidiNext(sor.block, obj);
eseidel789896f2005-11-27 22:52:09 +0000439 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000440 if (obj) {
441 unsigned pos = obj == eor.obj ? eor.pos : UINT_MAX;
442 if (obj == endOfLine.obj && endOfLine.pos <= pos) {
443 reachedEndOfLine = true;
444 pos = endOfLine.pos;
445 }
446 // It's OK to add runs for zero-length RenderObjects, just don't make the run larger than it should be
447 int end = obj->length() ? pos+1 : 0;
448 appendRunsForObject(start, end, obj, *this);
449 }
450
451 eor.increment();
452 sor = eor;
justing5b0e0422005-08-01 03:20:49 +0000453 }
mitz@apple.com6970fb72008-07-03 21:37:53 +0000454
pewtermoosecf72e2d2007-07-20 19:01:55 +0000455 m_direction = OtherNeutral;
456 m_status.eor = OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000457}
458
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000459InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj, bool firstLine)
hyattffe78712003-02-11 01:59:29 +0000460{
461 // See if we have an unconstructed line box for this object that is also
462 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000463 unsigned lineDepth = 1;
464 InlineFlowBox* childBox = 0;
465 InlineFlowBox* parentBox = 0;
466 InlineFlowBox* result = 0;
467 do {
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000468 ASSERT(obj->isRenderInline() || obj == this);
hyatt@apple.com76dbdb52009-01-29 22:49:13 +0000469
hyatt1d5d87b2007-04-24 04:55:54 +0000470 // Get the last box we made for this render object.
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000471 parentBox = obj->isRenderInline() ? toRenderInline(obj)->lastLineBox() : toRenderBlock(obj)->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000472
hyatt1d5d87b2007-04-24 04:55:54 +0000473 // If this box is constructed then it is from a previous line, and we need
474 // to make a new box for our line. If this box is unconstructed but it has
475 // something following it on the line, then we know we have to make a new box
476 // as well. In this situation our inline has actually been split in two on
477 // the same line (this can happen with very fancy language mixtures).
478 bool constructedNewBox = false;
479 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
480 // We need to make a new box for this render object. Once
481 // made, we need to place it at the end of the current line.
482 InlineBox* newBox = obj->createInlineBox(false, obj == this);
483 ASSERT(newBox->isInlineFlowBox());
484 parentBox = static_cast<InlineFlowBox*>(newBox);
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000485 parentBox->setFirstLineStyleBit(firstLine);
hyatt1d5d87b2007-04-24 04:55:54 +0000486 constructedNewBox = true;
487 }
mitz@apple.come1364202008-02-28 01:06:41 +0000488
hyatt1d5d87b2007-04-24 04:55:54 +0000489 if (!result)
490 result = parentBox;
491
492 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000493 // inline box for the line, and it doesn't have to be appended to any parent
494 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000495 if (childBox)
496 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000497
hyatt1d5d87b2007-04-24 04:55:54 +0000498 if (!constructedNewBox || obj == this)
499 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000500
hyatt1d5d87b2007-04-24 04:55:54 +0000501 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000502
hyatt1d5d87b2007-04-24 04:55:54 +0000503 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
504 // intermediate inline flows.
505 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000506
hyatt1d5d87b2007-04-24 04:55:54 +0000507 } while (true);
508
509 return result;
hyattffe78712003-02-11 01:59:29 +0000510}
511
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000512RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool firstLine, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000513{
mitz@apple.com887f3592008-02-25 22:03:08 +0000514 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000515
516 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000517 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000518 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000519 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000520 if (runCount == 2 && !r->m_object->isListMarker())
521 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
mitz@apple.come1364202008-02-28 01:06:41 +0000522
mitz@apple.com576e84e2008-04-24 19:09:48 +0000523 InlineBox* box = r->m_object->createInlineBox(r->m_object->isPositioned(), false, isOnlyRun);
524 r->m_box = box;
525
526 if (box) {
hyatt0c3a9862004-02-23 21:26:26 +0000527 // If we have no parent box yet, or if the run is not simply a sibling,
528 // then we need to construct inline boxes as necessary to properly enclose the
529 // run's inline box.
mitz@apple.come1364202008-02-28 01:06:41 +0000530 if (!parentBox || parentBox->object() != r->m_object->parent())
hyatt0c3a9862004-02-23 21:26:26 +0000531 // Create new inline boxes all the way back to the appropriate insertion point.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000532 parentBox = createLineBoxes(r->m_object->parent(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000533
hyatt0c3a9862004-02-23 21:26:26 +0000534 // Append the inline box to this line.
mitz@apple.com576e84e2008-04-24 19:09:48 +0000535 parentBox->addToLine(box);
536
537 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
538 box->setBidiLevel(visuallyOrdered ? 0 : r->level());
539
540 if (box->isInlineTextBox()) {
541 InlineTextBox* text = static_cast<InlineTextBox*>(box);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000542 text->setStart(r->m_start);
543 text->setLen(r->m_stop - r->m_start);
adele68afa0b2007-02-05 23:25:44 +0000544 text->m_dirOverride = r->dirOverride(visuallyOrdered);
darin06dcb9c2005-08-15 04:31:09 +0000545 }
hyatt0c3a9862004-02-23 21:26:26 +0000546 }
hyattffe78712003-02-11 01:59:29 +0000547 }
548
549 // We should have a root inline box. It should be unconstructed and
550 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000551 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000552
553 // Set bits on our inline flow boxes that indicate which sides should
554 // paint borders/margins/padding. This knowledge will ultimately be used when
555 // we determine the horizontal positions and widths of all the inline boxes on
556 // the line.
hyattffe78712003-02-11 01:59:29 +0000557 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
558
559 // Now mark the line boxes as being constructed.
560 lastLineBox()->setConstructed();
561
562 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000563 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000564}
565
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000566void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, bool firstLine, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd)
hyattffe78712003-02-11 01:59:29 +0000567{
568 // First determine our total width.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000569 int availableWidth = lineWidth(height(), firstLine);
hyattffe78712003-02-11 01:59:29 +0000570 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000571 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000572 unsigned numSpaces = 0;
573 ETextAlign textAlign = style()->textAlign();
574
mitz@apple.come1364202008-02-28 01:06:41 +0000575 for (BidiRun* r = firstRun; r; r = r->next()) {
576 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000577 continue; // Positioned objects are only participating to figure out their
578 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000579 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000580 if (r->m_object->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000581 RenderText* rt = toRenderText(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000582
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000583 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000584 const UChar* characters = rt->characters();
585 for (int i = r->m_start; i < r->m_stop; i++) {
586 UChar c = characters[i];
587 if (c == ' ' || c == '\n' || c == '\t')
588 numSpaces++;
589 }
590 }
591
mitz@apple.come1364202008-02-28 01:06:41 +0000592 if (int length = rt->textLength()) {
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000593 if (!r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000594 totWidth += rt->style(firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000595 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000596 }
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000597 r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, firstLine));
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000598 } else if (!r->m_object->isRenderInline()) {
hyatt@apple.com0c305632009-01-23 23:25:07 +0000599 RenderBox* renderBox = toRenderBox(r->m_object);
hyatt@apple.com774bbed2009-01-23 05:13:22 +0000600 renderBox->calcWidth();
601 r->m_box->setWidth(renderBox->width());
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000602 totWidth += renderBox->marginLeft() + renderBox->marginRight();
hyattffe78712003-02-11 01:59:29 +0000603 }
hyatt4b381692003-03-10 21:11:59 +0000604
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000605 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000606 }
607
608 // Armed with the total width of the line (without justification),
609 // we now examine our text-align property in order to determine where to position the
610 // objects horizontally. The total width of the line can be increased if we end up
611 // justifying text.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000612 int x = leftOffset(height(), firstLine);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000613 switch(textAlign) {
hyattffe78712003-02-11 01:59:29 +0000614 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000615 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000616 // The direction of the block should determine what happens with wide lines. In
617 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000618 if (style()->direction() == LTR) {
619 if (totWidth > availableWidth && trailingSpaceRun)
620 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
621 } else {
622 if (trailingSpaceRun)
623 trailingSpaceRun->m_box->setWidth(0);
624 else if (totWidth > availableWidth)
625 x -= (totWidth - availableWidth);
626 }
hyattffe78712003-02-11 01:59:29 +0000627 break;
628 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000629 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
630 if (trailingSpaceRun) {
631 totWidth -= trailingSpaceRun->m_box->width();
632 trailingSpaceRun->m_box->setWidth(0);
633 }
hyattffe78712003-02-11 01:59:29 +0000634 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000635 }
hyattffe78712003-02-11 01:59:29 +0000636 // fall through
637 case TAAUTO:
638 numSpaces = 0;
639 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000640 if (style()->direction() == LTR) {
641 if (totWidth > availableWidth && trailingSpaceRun)
642 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
hyattffe78712003-02-11 01:59:29 +0000643 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000644 }
hyattffe78712003-02-11 01:59:29 +0000645 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000646 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000647 // Wide lines spill out of the block based off direction.
648 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
649 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000650 if (style()->direction() == LTR) {
651 if (trailingSpaceRun) {
652 totWidth -= trailingSpaceRun->m_box->width();
653 trailingSpaceRun->m_box->setWidth(0);
654 }
655 if (totWidth < availableWidth)
656 x += availableWidth - totWidth;
657 } else {
658 if (totWidth > availableWidth && trailingSpaceRun) {
659 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
660 totWidth -= trailingSpaceRun->m_box->width();
661 } else
662 x += availableWidth - totWidth;
663 }
hyattffe78712003-02-11 01:59:29 +0000664 break;
665 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000666 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000667 int trailingSpaceWidth = 0;
668 if (trailingSpaceRun) {
669 totWidth -= trailingSpaceRun->m_box->width();
670 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
671 trailingSpaceRun->m_box->setWidth(trailingSpaceWidth);
672 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000673 if (style()->direction() == LTR)
674 x += max((availableWidth - totWidth) / 2, 0);
675 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000676 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000677 break;
678 }
679
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000680 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000681 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000682 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000683 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000684
hyattacbb0d42003-04-25 01:32:49 +0000685 int spaceAdd = 0;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000686 if (r->m_object->isText()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000687 unsigned spaces = 0;
darin@apple.com36744d62009-01-25 20:23:04 +0000688 const UChar* characters = toRenderText(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000689 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000690 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000691 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000692 spaces++;
darina3c48282003-10-07 15:49:30 +0000693 }
hyatt870bdda2003-05-21 23:37:33 +0000694
darinb53ebdc2006-07-09 15:10:21 +0000695 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000696
hyattdca76e92005-11-02 08:52:50 +0000697 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000698 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000699 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000700 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000701 totWidth += spaceAdd;
702 }
hyattacbb0d42003-04-25 01:32:49 +0000703 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000704 if (!numSpaces)
705 break;
hyattacbb0d42003-04-25 01:32:49 +0000706 }
hyattffe78712003-02-11 01:59:29 +0000707 }
hyattffe78712003-02-11 01:59:29 +0000708 }
mitz@apple.come1364202008-02-28 01:06:41 +0000709
hyattffe78712003-02-11 01:59:29 +0000710 // The widths of all runs are now known. We can now place every inline box (and
711 // compute accurate widths for the inline flow boxes).
hyatt1f14a612004-12-07 00:34:02 +0000712 int leftPosition = x;
713 int rightPosition = x;
darin06dcb9c2005-08-15 04:31:09 +0000714 needsWordSpacing = false;
715 lineBox->placeBoxesHorizontally(x, leftPosition, rightPosition, needsWordSpacing);
hyatt1f14a612004-12-07 00:34:02 +0000716 lineBox->setHorizontalOverflowPositions(leftPosition, rightPosition);
hyattffe78712003-02-11 01:59:29 +0000717}
718
mitz@apple.com887f3592008-02-25 22:03:08 +0000719void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun)
hyattffe78712003-02-11 01:59:29 +0000720{
hyatt@apple.comd885df72009-01-22 02:31:52 +0000721 setHeight(lineBox->verticallyAlignBoxes(height()));
722 lineBox->setBlockHeight(height());
hyattffe78712003-02-11 01:59:29 +0000723
hyatt35a85e52003-02-14 19:43:07 +0000724 // See if the line spilled out. If so set overflow height accordingly.
725 int bottomOfLine = lineBox->bottomOverflow();
hyatt@apple.comd885df72009-01-22 02:31:52 +0000726 if (bottomOfLine > height() && bottomOfLine > m_overflowHeight)
hyatt35a85e52003-02-14 19:43:07 +0000727 m_overflowHeight = bottomOfLine;
mitz@apple.come1364202008-02-28 01:06:41 +0000728
hyattffe78712003-02-11 01:59:29 +0000729 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000730 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.come1364202008-02-28 01:06:41 +0000731 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000732 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000733
hyatt98ee7e42003-05-14 01:39:15 +0000734 // Align positioned boxes with the top of the line box. This is
735 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000736 if (r->m_object->isPositioned())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000737 r->m_box->setYPos(height());
hyatt98ee7e42003-05-14 01:39:15 +0000738
739 // Position is used to properly position both replaced elements and
740 // to update the static normal flow x/y of positioned elements.
mitz@apple.come1364202008-02-28 01:06:41 +0000741 r->m_object->position(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000742 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000743 // Positioned objects and zero-length text nodes destroy their boxes in
744 // position(), which unnecessarily dirties the line.
745 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000746}
kociendabb0c24b2001-08-24 14:24:40 +0000747
748// collects one line of the paragraph and transforms it to visual order
mitz@apple.com15035e62008-07-05 20:44:44 +0000749void RenderBlock::bidiReorderLine(InlineBidiResolver& resolver, const InlineIterator& end)
kociendabb0c24b2001-08-24 14:24:40 +0000750{
mitz@apple.com1a301772008-03-11 18:30:36 +0000751 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
hyatt4b381692003-03-10 21:11:59 +0000752}
kociendabb0c24b2001-08-24 14:24:40 +0000753
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000754static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
755{
756 if (character == ' ' || character == '\t' || character == softHyphen)
757 return true;
758 if (character == '\n')
759 return !renderer->style()->preserveNewline();
760 if (character == noBreakSpace)
761 return renderer->style()->nbspMode() == SPACE;
762 return false;
763}
764
apddd2ff42007-03-31 08:26:24 +0000765void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
kociendabb0c24b2001-08-24 14:24:40 +0000766{
apddd2ff42007-03-31 08:26:24 +0000767 bool useRepaintBounds = false;
hyatt0c3a9862004-02-23 21:26:26 +0000768
hyatt7b41b9d2007-05-30 05:32:40 +0000769 invalidateVerticalPosition();
770
hyatta70560a2002-11-20 01:53:20 +0000771 m_overflowHeight = 0;
hyatt7b41b9d2007-05-30 05:32:40 +0000772
hyatt@apple.comd885df72009-01-22 02:31:52 +0000773 setHeight(borderTop() + paddingTop());
hyattf9f247b2007-01-12 22:53:40 +0000774 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000775
hyatt0c3a9862004-02-23 21:26:26 +0000776 // Figure out if we should clear out our line boxes.
777 // FIXME: Handle resize eventually!
778 // FIXME: Do something better when floats are present.
mitz@apple.com40547b32008-03-18 04:04:34 +0000779 bool fullLayout = !firstLineBox() || !firstChild() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000780 if (fullLayout)
hyatt@apple.comb83de652009-01-28 20:48:04 +0000781 lineBoxes()->deleteLineBoxes(renderArena());
mitz@apple.come1364202008-02-28 01:06:41 +0000782
hyatted77ad82004-06-15 07:21:23 +0000783 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
784 // clip.
785 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
786 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
787 // anyway, so we won't worry about following the draft here.
788 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000789
hyatted77ad82004-06-15 07:21:23 +0000790 // Walk all the lines and delete our ellipsis line boxes if they exist.
791 if (hasTextOverflow)
792 deleteEllipsisLineBoxes();
793
hyattffe78712003-02-11 01:59:29 +0000794 if (firstChild()) {
kociendabb0c24b2001-08-24 14:24:40 +0000795 // layout replaced elements
hyattffe78712003-02-11 01:59:29 +0000796 bool endOfInline = false;
mitz@apple.com1a301772008-03-11 18:30:36 +0000797 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000798 Vector<FloatWithRect> floats;
hyatt@apple.com21cc37a2008-02-26 23:17:03 +0000799 int containerWidth = max(0, containingBlockWidth());
hyatt0c3a9862004-02-23 21:26:26 +0000800 while (o) {
hyatt7b41b9d2007-05-30 05:32:40 +0000801 o->invalidateVerticalPosition();
hyatt0c3a9862004-02-23 21:26:26 +0000802 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +0000803 RenderBox* box = toRenderBox(o);
804
hyatt89377f12002-12-13 21:24:40 +0000805 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
hyattadbb2ef2003-11-18 09:21:44 +0000806 o->setChildNeedsLayout(true, false);
hyatt567533d2007-04-25 21:46:21 +0000807
808 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
809 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
810 o->setPrefWidthsDirty(true, false);
811
hyatt3ac01352003-03-22 01:37:33 +0000812 if (o->isPositioned())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000813 o->containingBlock()->insertPositionedObject(box);
hyatt0c3a9862004-02-23 21:26:26 +0000814 else {
815 if (o->isFloating())
hyatt@apple.comd885df72009-01-22 02:31:52 +0000816 floats.append(FloatWithRect(box));
hyatt0c3a9862004-02-23 21:26:26 +0000817 else if (fullLayout || o->needsLayout()) // Replaced elements
818 o->dirtyLineBoxes(fullLayout);
mitz@apple.com40547b32008-03-18 04:04:34 +0000819
hyatt390427a2003-05-03 18:33:42 +0000820 o->layoutIfNeeded();
hyatt0c3a9862004-02-23 21:26:26 +0000821 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000822 } else if (o->isText() || (o->isRenderInline() && !endOfInline)) {
hyatt0c3a9862004-02-23 21:26:26 +0000823 if (fullLayout || o->selfNeedsLayout())
824 o->dirtyLineBoxes(fullLayout);
hyatt@apple.com21cc37a2008-02-26 23:17:03 +0000825
826 // Calculate margins of inline flows so that they can be used later by line layout.
hyatt@apple.com415d8de2009-01-26 22:29:19 +0000827 if (o->isRenderInline())
weinig@apple.com7c50a3b2009-01-30 19:55:45 +0000828 toRenderInline(o)->calcMargins(containerWidth);
hyattf4fe67f2003-10-07 04:43:23 +0000829 o->setNeedsLayout(false);
hyattf4fe67f2003-10-07 04:43:23 +0000830 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000831 o = bidiNext(this, o, 0, false, &endOfInline);
kociendabb0c24b2001-08-24 14:24:40 +0000832 }
833
mitz@apple.com40547b32008-03-18 04:04:34 +0000834 // We want to skip ahead to the first dirty line
mitz@apple.com15035e62008-07-05 20:44:44 +0000835 InlineBidiResolver resolver;
mitz@apple.com40547b32008-03-18 04:04:34 +0000836 unsigned floatIndex;
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000837 bool firstLine = true;
838 RootInlineBox* startLine = determineStartPosition(firstLine, fullLayout, resolver, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000839
hyatt837eb362004-05-21 22:17:10 +0000840 if (fullLayout && !selfNeedsLayout()) {
hyatt0c3a9862004-02-23 21:26:26 +0000841 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
842 // we're supposed to.
simon.fraser@apple.com6528b502009-01-12 21:42:25 +0000843 RenderView* v = view();
844 if (v && !v->doingFullRepaint() && m_layer) {
hyatt837eb362004-05-21 22:17:10 +0000845 // Because we waited until we were already inside layout to discover
846 // that the block really needed a full layout, we missed our chance to repaint the layer
847 // before layout started. Luckily the layer has cached the repaint rect for its original
848 // position and size, and so we can use that to make a repaint happen now.
simon.fraser@apple.comeba39c02009-01-27 23:05:57 +0000849 repaintUsingContainer(containerForRepaint(), m_layer->repaintRect());
hyatt837eb362004-05-21 22:17:10 +0000850 }
851 }
hyatt0c3a9862004-02-23 21:26:26 +0000852
mitz@apple.com40547b32008-03-18 04:04:34 +0000853 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
854
hyatt85586af2003-02-19 23:22:42 +0000855 if (!smidpoints)
mitz@apple.com15035e62008-07-05 20:44:44 +0000856 smidpoints = new Vector<InlineIterator>();
mitz@apple.come1364202008-02-28 01:06:41 +0000857
hyatt85586af2003-02-19 23:22:42 +0000858 sNumMidpoints = 0;
859 sCurrMidpoint = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000860
hyatt0c3a9862004-02-23 21:26:26 +0000861 // We also find the first clean line and extract these lines. We will add them back
862 // if we determine that we're able to synchronize after handling all our dirty lines.
mitz@apple.com15035e62008-07-05 20:44:44 +0000863 InlineIterator cleanLineStart;
eseidel789896f2005-11-27 22:52:09 +0000864 BidiStatus cleanLineBidiStatus;
aroben201afd12007-07-01 04:53:01 +0000865 int endLineYPos = 0;
hyatt0c3a9862004-02-23 21:26:26 +0000866 RootInlineBox* endLine = (fullLayout || !startLine) ?
pewtermoosecf72e2d2007-07-20 19:01:55 +0000867 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
868
hyatt0c3a9862004-02-23 21:26:26 +0000869 if (startLine) {
apddd2ff42007-03-31 08:26:24 +0000870 useRepaintBounds = true;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000871 repaintTop = height();
872 repaintBottom = height();
hyatt0c3a9862004-02-23 21:26:26 +0000873 RenderArena* arena = renderArena();
874 RootInlineBox* box = startLine;
875 while (box) {
ap9059f6f2006-07-24 16:55:02 +0000876 repaintTop = min(repaintTop, box->topOverflow());
877 repaintBottom = max(repaintBottom, box->bottomOverflow());
hyatt0c3a9862004-02-23 21:26:26 +0000878 RootInlineBox* next = box->nextRootBox();
879 box->deleteLine(arena);
880 box = next;
881 }
hyatt0c3a9862004-02-23 21:26:26 +0000882 }
mitz@apple.come1364202008-02-28 01:06:41 +0000883
mitz@apple.com15035e62008-07-05 20:44:44 +0000884 InlineIterator end = resolver.position();
hyatt0c3a9862004-02-23 21:26:26 +0000885
mitz@apple.come3602dd2008-04-08 20:58:36 +0000886 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
887 // If the last line before the start line ends with a line break that clear floats,
888 // adjust the height accordingly.
889 // 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 +0000890 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
891 RenderObject* lastObject = lastLeafChild->object();
892 if (!lastObject->isBR())
893 lastObject = lastRootBox()->firstLeafChild()->object();
894 if (lastObject->isBR()) {
895 EClear clear = lastObject->style()->clear();
896 if (clear != CNONE)
897 newLine(clear);
898 }
mitz@apple.come3602dd2008-04-08 20:58:36 +0000899 }
900 }
mitz@apple.com40547b32008-03-18 04:04:34 +0000901
hyatt0c3a9862004-02-23 21:26:26 +0000902 bool endLineMatched = false;
mitz@apple.comeb9c42c2008-03-26 20:53:07 +0000903 bool checkForEndLineMatch = endLine;
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000904 bool checkForFloatsFromLastLine = false;
hyatt@apple.comd885df72009-01-22 02:31:52 +0000905 int lastHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +0000906
hyatt0c3a9862004-02-23 21:26:26 +0000907 while (!end.atEnd()) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000908 // 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 +0000909 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(resolver, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
hyatt0c3a9862004-02-23 21:26:26 +0000910 break;
911
hyatt33f8d492002-11-12 21:44:52 +0000912 betweenMidpoints = false;
913 isLineEmpty = true;
hyatt@apple.comd0301a52009-01-26 21:50:57 +0000914
mitz@apple.com71e30842008-03-18 16:13:31 +0000915 EClear clear = CNONE;
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000916 end = findNextLineBreak(resolver, firstLine, &clear);
mitz@apple.com15035e62008-07-05 20:44:44 +0000917 if (resolver.position().atEnd()) {
918 resolver.deleteRuns();
mitz@apple.com07bff3a2008-05-31 06:28:21 +0000919 checkForFloatsFromLastLine = true;
eseidel789896f2005-11-27 22:52:09 +0000920 break;
ggarenec11e5b2007-02-25 02:14:54 +0000921 }
mitz@apple.com15035e62008-07-05 20:44:44 +0000922 ASSERT(end != resolver.position());
mitz@apple.com1a301772008-03-11 18:30:36 +0000923
hyatt33f8d492002-11-12 21:44:52 +0000924 if (!isLineEmpty) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000925 bidiReorderLine(resolver, end);
926 ASSERT(resolver.position() == end);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000927
928 BidiRun* trailingSpaceRun = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +0000929 if (!previousLineBrokeCleanly && resolver.runCount() && resolver.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()) {
930 trailingSpaceRun = resolver.logicallyLastRun();
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000931 RenderObject* lastObject = trailingSpaceRun->m_object;
932 if (lastObject->isText()) {
darin@apple.com36744d62009-01-25 20:23:04 +0000933 RenderText* lastText = toRenderText(lastObject);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000934 const UChar* characters = lastText->characters();
935 int firstSpace = trailingSpaceRun->stop();
936 while (firstSpace > trailingSpaceRun->start()) {
937 UChar current = characters[firstSpace - 1];
938 if (!isCollapsibleSpace(current, lastText))
939 break;
940 firstSpace--;
941 }
942 if (firstSpace == trailingSpaceRun->stop())
943 trailingSpaceRun = 0;
mitz@apple.com3a43c712008-04-20 04:26:28 +0000944 else {
945 TextDirection direction = style()->direction();
mitz@apple.com15035e62008-07-05 20:44:44 +0000946 bool shouldReorder = trailingSpaceRun != (direction == LTR ? resolver.lastRun() : resolver.firstRun());
mitz@apple.com3a43c712008-04-20 04:26:28 +0000947 if (firstSpace != trailingSpaceRun->start()) {
948 ETextAlign textAlign = style()->textAlign();
949 // If the trailing white space is at the right hand side of a left-aligned line, then computeHorizontalPositionsForLine()
950 // does not care if trailingSpaceRun includes non-spaces at the beginning. In all other cases, trailingSpaceRun has to
951 // contain only the spaces, either because we re-order them or because computeHorizontalPositionsForLine() needs to know
952 // their width.
953 bool shouldSeparateSpaces = textAlign != LEFT && textAlign != WEBKIT_LEFT && textAlign != TAAUTO || trailingSpaceRun->m_level % 2 || direction == RTL || shouldReorder;
954 if (shouldSeparateSpaces) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000955 BidiContext* baseContext = resolver.context();
mitz@apple.com3a43c712008-04-20 04:26:28 +0000956 while (BidiContext* parent = baseContext->parent())
957 baseContext = parent;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000958
mitz@apple.com3a43c712008-04-20 04:26:28 +0000959 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
960 trailingSpaceRun->m_stop = firstSpace;
961 if (direction == LTR)
mitz@apple.com15035e62008-07-05 20:44:44 +0000962 resolver.addRun(newTrailingRun);
mitz@apple.com3a43c712008-04-20 04:26:28 +0000963 else
mitz@apple.com15035e62008-07-05 20:44:44 +0000964 resolver.prependRun(newTrailingRun);
mitz@apple.com3a43c712008-04-20 04:26:28 +0000965 trailingSpaceRun = newTrailingRun;
966 shouldReorder = false;
967 }
968 }
969 if (shouldReorder) {
mitz@apple.com8e05a142008-04-22 20:42:02 +0000970 if (direction == LTR) {
mitz@apple.com15035e62008-07-05 20:44:44 +0000971 resolver.moveRunToEnd(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +0000972 trailingSpaceRun->m_level = 0;
973 } else {
mitz@apple.com15035e62008-07-05 20:44:44 +0000974 resolver.moveRunToBeginning(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +0000975 trailingSpaceRun->m_level = 1;
976 }
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000977 }
978 }
979 } else
980 trailingSpaceRun = 0;
981 }
hyatt4b381692003-03-10 21:11:59 +0000982
983 // Now that the runs have been ordered, we create the line boxes.
984 // At the same time we figure out where border/padding/margin should be applied for
985 // inline flow boxes.
hyatt4b381692003-03-10 21:11:59 +0000986
hyatt0c3a9862004-02-23 21:26:26 +0000987 RootInlineBox* lineBox = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +0000988 if (resolver.runCount()) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000989 lineBox = constructLine(resolver.runCount(), resolver.firstRun(), resolver.lastRun(), firstLine, !end.obj, end.obj && !end.pos ? end.obj : 0);
hyattdfb09052003-03-11 02:58:59 +0000990 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +0000991 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000992
hyattdfb09052003-03-11 02:58:59 +0000993 // Now we position all of our text runs horizontally.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +0000994 computeHorizontalPositionsForLine(lineBox, firstLine, resolver.firstRun(), trailingSpaceRun, end.atEnd());
mitz@apple.come1364202008-02-28 01:06:41 +0000995
hyattdfb09052003-03-11 02:58:59 +0000996 // Now position our text runs vertically.
mitz@apple.com15035e62008-07-05 20:44:44 +0000997 computeVerticalPositionsForLine(lineBox, resolver.firstRun());
hyattbddcc612006-07-24 23:16:15 +0000998
oliver98d52382007-10-12 10:47:18 +0000999#if ENABLE(SVG)
1000 // Special SVG text layout code
1001 lineBox->computePerCharacterLayoutInformation();
1002#endif
1003
hyattbddcc612006-07-24 23:16:15 +00001004#if PLATFORM(MAC)
1005 // Highlight acts as an overflow inflation.
1006 if (style()->highlight() != nullAtom)
1007 lineBox->addHighlightOverflow();
1008#endif
hyattdfb09052003-03-11 02:58:59 +00001009 }
1010 }
ggarenec11e5b2007-02-25 02:14:54 +00001011
mitz@apple.com15035e62008-07-05 20:44:44 +00001012 resolver.deleteRuns();
hyatt35a85e52003-02-14 19:43:07 +00001013
ap9059f6f2006-07-24 16:55:02 +00001014 if (lineBox) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001015 lineBox->setLineBreakInfo(end.obj, end.pos, resolver.status());
apddd2ff42007-03-31 08:26:24 +00001016 if (useRepaintBounds) {
ap9059f6f2006-07-24 16:55:02 +00001017 repaintTop = min(repaintTop, lineBox->topOverflow());
1018 repaintBottom = max(repaintBottom, lineBox->bottomOverflow());
1019 }
1020 }
mitz@apple.come1364202008-02-28 01:06:41 +00001021
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001022 firstLine = false;
mitz@apple.com71e30842008-03-18 16:13:31 +00001023 newLine(clear);
darinb70665a2002-04-15 23:43:21 +00001024 }
mitz@apple.come1364202008-02-28 01:06:41 +00001025
mitz@apple.com7b089842008-03-22 06:57:28 +00001026 if (m_floatingObjects && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001027 if (lastFloat) {
1028 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1029 }
1030 m_floatingObjects->next();
1031 } else
1032 m_floatingObjects->first();
1033 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001034 if (f->m_bottom > lastHeight)
1035 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com93526592008-03-18 04:36:51 +00001036 ASSERT(f->m_renderer == floats[floatIndex].object);
mitz@apple.com40547b32008-03-18 04:04:34 +00001037 // If a float's geometry has changed, give up on syncing with clean lines.
mitz@apple.com93526592008-03-18 04:36:51 +00001038 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 +00001039 checkForEndLineMatch = false;
mitz@apple.com40547b32008-03-18 04:04:34 +00001040 floatIndex++;
1041 }
1042 lastFloat = m_floatingObjects->last();
1043 }
1044
hyatt@apple.comd885df72009-01-22 02:31:52 +00001045 lastHeight = height();
hyatt85586af2003-02-19 23:22:42 +00001046 sNumMidpoints = 0;
1047 sCurrMidpoint = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +00001048 resolver.setPosition(end);
kociendabb0c24b2001-08-24 14:24:40 +00001049 }
mitz@apple.come1364202008-02-28 01:06:41 +00001050
hyatt0c3a9862004-02-23 21:26:26 +00001051 if (endLine) {
1052 if (endLineMatched) {
1053 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001054 int delta = height() - endLineYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001055 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1056 line->attachLine();
1057 if (delta) {
mitz@apple.come1364202008-02-28 01:06:41 +00001058 repaintTop = min(repaintTop, line->topOverflow() + min(delta, 0));
1059 repaintBottom = max(repaintBottom, line->bottomOverflow() + max(delta, 0));
hyattda77c4b2004-06-17 18:09:49 +00001060 line->adjustPosition(0, delta);
ap9059f6f2006-07-24 16:55:02 +00001061 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001062 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1063 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1064 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1065 int floatTop = (*f)->y() - (*f)->marginTop();
mitz@apple.com40547b32008-03-18 04:04:34 +00001066 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001067 setHeight(floatTop + delta);
mitz@apple.com40547b32008-03-18 04:04:34 +00001068 positionNewFloats();
1069 }
1070 }
harrison208ea792005-07-29 23:42:59 +00001071 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001072 setHeight(lastRootBox()->blockHeight());
ap9059f6f2006-07-24 16:55:02 +00001073 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001074 // Delete all the remaining lines.
hyatt0c3a9862004-02-23 21:26:26 +00001075 InlineRunBox* line = endLine;
1076 RenderArena* arena = renderArena();
1077 while (line) {
ap9059f6f2006-07-24 16:55:02 +00001078 repaintTop = min(repaintTop, line->topOverflow());
1079 repaintBottom = max(repaintBottom, line->bottomOverflow());
hyatt0c3a9862004-02-23 21:26:26 +00001080 InlineRunBox* next = line->nextLineBox();
hyatt0c3a9862004-02-23 21:26:26 +00001081 line->deleteLine(arena);
1082 line = next;
1083 }
1084 }
1085 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001086 if (m_floatingObjects && (checkForFloatsFromLastLine || positionNewFloats()) && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001087 // In case we have a float on the last line, it might not be positioned up to now.
1088 // This has to be done before adding in the bottom border/padding, or the float will
1089 // include the padding incorrectly. -dwh
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001090 if (lastFloat) {
1091 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
mitz@apple.com727b9102008-04-25 04:04:46 +00001092 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001093 m_floatingObjects->next();
1094 } else
1095 m_floatingObjects->first();
1096 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
1097 if (f->m_bottom > lastHeight)
1098 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com40547b32008-03-18 04:04:34 +00001099 }
mitz@apple.com07bff3a2008-05-31 06:28:21 +00001100 lastFloat = m_floatingObjects->last();
mitz@apple.com40547b32008-03-18 04:04:34 +00001101 }
kociendabb0c24b2001-08-24 14:24:40 +00001102 }
hyatt85586af2003-02-19 23:22:42 +00001103
1104 sNumMidpoints = 0;
1105 sCurrMidpoint = 0;
hyatta70560a2002-11-20 01:53:20 +00001106
1107 // Now add in the bottom border/padding.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001108 setHeight(height() + toAdd);
kociendabb0c24b2001-08-24 14:24:40 +00001109
hyatta70560a2002-11-20 01:53:20 +00001110 // Always make sure this is at least our height.
hyatt@apple.comd885df72009-01-22 02:31:52 +00001111 m_overflowHeight = max(height(), m_overflowHeight);
mitz@apple.come1364202008-02-28 01:06:41 +00001112
hyattb4b20872004-10-20 21:34:01 +00001113 // See if any lines spill out of the block. If so, we need to update our overflow width.
1114 checkLinesForOverflow();
1115
adele7a470a72006-04-20 22:22:14 +00001116 if (!firstLineBox() && hasLineIfEmpty())
hyatt@apple.comd885df72009-01-22 02:31:52 +00001117 setHeight(height() + lineHeight(true, true));
hyatted77ad82004-06-15 07:21:23 +00001118
1119 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1120 // truncate text.
1121 if (hasTextOverflow)
1122 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001123}
1124
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001125RootInlineBox* RenderBlock::determineStartPosition(bool& firstLine, bool& fullLayout, InlineBidiResolver& resolver, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +00001126{
1127 RootInlineBox* curr = 0;
1128 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001129
mitz@apple.com40547b32008-03-18 04:04:34 +00001130 bool dirtiedByFloat = false;
1131 if (!fullLayout) {
1132 size_t floatIndex = 0;
1133 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001134 if (Vector<RenderBox*>* cleanLineFloats = curr->floatsPtr()) {
1135 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1136 for (Vector<RenderBox*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
1137 RenderBox* f = *o;
mitz@apple.com40547b32008-03-18 04:04:34 +00001138 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
1139 ASSERT(floatIndex < floats.size());
1140 if (floats[floatIndex].object != f) {
1141 // A new float has been inserted before this line or before its last known float.
1142 // Just do a full layout.
1143 fullLayout = true;
1144 break;
1145 }
1146 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +00001147 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +00001148 curr->markDirty();
1149 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()));
1150 floats[floatIndex].rect.setSize(newSize);
1151 dirtiedByFloat = true;
1152 }
1153 floatIndex++;
1154 }
1155 }
1156 if (dirtiedByFloat || fullLayout)
1157 break;
1158 }
1159 // Check if a new float has been inserted after the last known float.
1160 if (!curr && floatIndex < floats.size())
1161 fullLayout = true;
1162 }
1163
hyatt0c3a9862004-02-23 21:26:26 +00001164 if (fullLayout) {
1165 // Nuke all our lines.
1166 if (firstRootBox()) {
1167 RenderArena* arena = renderArena();
1168 curr = firstRootBox();
1169 while (curr) {
1170 RootInlineBox* next = curr->nextRootBox();
1171 curr->deleteLine(arena);
1172 curr = next;
1173 }
darinec375482007-01-06 01:36:24 +00001174 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001175 }
eseidel789896f2005-11-27 22:52:09 +00001176 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001177 if (curr) {
1178 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001179 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001180 // We have a previous line.
darin@apple.com36744d62009-01-25 20:23:04 +00001181 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= toRenderText(prevRootBox->lineBreakObj())->textLength()))
mjs9f78dd92007-02-12 04:06:07 +00001182 // The previous line didn't break cleanly or broke at a newline
1183 // that has been deleted, so treat it as dirty too.
1184 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001185 }
eseidel789896f2005-11-27 22:52:09 +00001186 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001187 // No dirty lines were found.
1188 // If the last line didn't break cleanly, treat it as dirty.
1189 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1190 curr = lastRootBox();
1191 }
mitz@apple.come1364202008-02-28 01:06:41 +00001192
hyatt0c3a9862004-02-23 21:26:26 +00001193 // If we have no dirty lines, then last is just the last root box.
1194 last = curr ? curr->prevRootBox() : lastRootBox();
1195 }
mitz@apple.come1364202008-02-28 01:06:41 +00001196
mitz@apple.com40547b32008-03-18 04:04:34 +00001197 numCleanFloats = 0;
1198 if (!floats.isEmpty()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001199 int savedHeight = height();
mitz@apple.com40547b32008-03-18 04:04:34 +00001200 // Restore floats from clean lines.
1201 RootInlineBox* line = firstRootBox();
1202 while (line != curr) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001203 if (Vector<RenderBox*>* cleanLineFloats = line->floatsPtr()) {
1204 Vector<RenderBox*>::iterator end = cleanLineFloats->end();
1205 for (Vector<RenderBox*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001206 insertFloatingObject(*f);
hyatt@apple.comd885df72009-01-22 02:31:52 +00001207 setHeight((*f)->y() - (*f)->marginTop());
mitz@apple.com40547b32008-03-18 04:04:34 +00001208 positionNewFloats();
1209 ASSERT(floats[numCleanFloats].object == *f);
1210 numCleanFloats++;
1211 }
1212 }
1213 line = line->nextRootBox();
1214 }
hyatt@apple.comd885df72009-01-22 02:31:52 +00001215 setHeight(savedHeight);
mitz@apple.com40547b32008-03-18 04:04:34 +00001216 }
1217
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001218 firstLine = !last;
hyatt0c3a9862004-02-23 21:26:26 +00001219 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001220
1221 RenderObject* startObj;
1222 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001223 if (last) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001224 setHeight(last->blockHeight());
hyatt0c3a9862004-02-23 21:26:26 +00001225 startObj = last->lineBreakObj();
1226 pos = last->lineBreakPos();
mitz@apple.com15035e62008-07-05 20:44:44 +00001227 resolver.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001228 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001229 bool ltr = style()->direction() == LTR
1230 #if ENABLE(SVG)
1231 || (style()->unicodeBidi() == UBNormal && isSVGText())
1232 #endif
1233 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001234
mitz@apple.com1a301772008-03-11 18:30:36 +00001235 BidiContext* context = new BidiContext(ltr ? 0 : 1, ltr ? LeftToRight : RightToLeft, style()->unicodeBidi() == Override);
1236
mitz@apple.com15035e62008-07-05 20:44:44 +00001237 resolver.setLastStrongDir(context->dir());
1238 resolver.setLastDir(context->dir());
1239 resolver.setEorDir(context->dir());
1240 resolver.setContext(context);
1241 startObj = bidiFirst(this, &resolver);
darindde01502005-12-18 22:55:35 +00001242 }
mitz@apple.come1364202008-02-28 01:06:41 +00001243
mitz@apple.com15035e62008-07-05 20:44:44 +00001244 resolver.setPosition(InlineIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001245
hyatt0c3a9862004-02-23 21:26:26 +00001246 return curr;
1247}
1248
mitz@apple.com15035e62008-07-05 20:44:44 +00001249RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, InlineIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001250{
1251 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001252 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001253 last = 0;
1254 else {
hyatt04420ca2004-07-16 00:05:42 +00001255 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1256 if (curr->isDirty())
1257 last = 0;
1258 else if (!last)
1259 last = curr;
1260 }
hyatt0c3a9862004-02-23 21:26:26 +00001261 }
mitz@apple.come1364202008-02-28 01:06:41 +00001262
hyatt0c3a9862004-02-23 21:26:26 +00001263 if (!last)
1264 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001265
eseidel789896f2005-11-27 22:52:09 +00001266 RootInlineBox* prev = last->prevRootBox();
mitz@apple.com15035e62008-07-05 20:44:44 +00001267 cleanLineStart = InlineIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
eseidel789896f2005-11-27 22:52:09 +00001268 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001269 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001270
hyatt0c3a9862004-02-23 21:26:26 +00001271 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1272 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1273 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001274
hyatt0c3a9862004-02-23 21:26:26 +00001275 return last;
1276}
1277
mitz@apple.com15035e62008-07-05 20:44:44 +00001278bool 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 +00001279{
mitz@apple.com15035e62008-07-05 20:44:44 +00001280 if (resolver.position() == endLineStart) {
1281 if (resolver.status() != endLineStatus)
mitz@apple.com40547b32008-03-18 04:04:34 +00001282 return false;
1283
hyatt@apple.comd885df72009-01-22 02:31:52 +00001284 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001285 if (!delta || !m_floatingObjects)
1286 return true;
1287
1288 // 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 +00001289 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001290
1291 RootInlineBox* lastLine = endLine;
1292 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1293 lastLine = nextLine;
1294
1295 int bottom = lastLine->blockHeight() + abs(delta);
1296
1297 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001298 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001299 return false;
1300 }
1301
1302 return true;
1303 }
hyatt0c3a9862004-02-23 21:26:26 +00001304
mitz@apple.come1364202008-02-28 01:06:41 +00001305 // The first clean line doesn't match, but we can check a handful of following lines to try
1306 // to match back up.
1307 static int numLines = 8; // The # of lines we're willing to match against.
1308 RootInlineBox* line = endLine;
1309 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001310 if (line->lineBreakObj() == resolver.position().obj && line->lineBreakPos() == resolver.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001311 // We have a match.
mitz@apple.com15035e62008-07-05 20:44:44 +00001312 if (line->lineBreakBidiStatus() != resolver.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001313 return false; // ...but the bidi state doesn't match.
1314 RootInlineBox* result = line->nextRootBox();
1315
1316 // Set our yPos to be the block height of endLine.
1317 if (result)
1318 endYPos = line->blockHeight();
1319
hyatt@apple.comd885df72009-01-22 02:31:52 +00001320 int delta = height() - endYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001321 if (delta && m_floatingObjects) {
1322 // 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 +00001323 int top = min(height(), endYPos);
mitz@apple.com40547b32008-03-18 04:04:34 +00001324
1325 RootInlineBox* lastLine = endLine;
1326 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1327 lastLine = nextLine;
1328
1329 int bottom = lastLine->blockHeight() + abs(delta);
1330
1331 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001332 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001333 return false;
1334 }
1335 }
1336
mitz@apple.come1364202008-02-28 01:06:41 +00001337 // Now delete the lines that we failed to sync.
1338 RootInlineBox* boxToDelete = endLine;
1339 RenderArena* arena = renderArena();
1340 while (boxToDelete && boxToDelete != result) {
1341 repaintTop = min(repaintTop, boxToDelete->topOverflow());
1342 repaintBottom = max(repaintBottom, boxToDelete->bottomOverflow());
1343 RootInlineBox* next = boxToDelete->nextRootBox();
1344 boxToDelete->deleteLine(arena);
1345 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001346 }
mitz@apple.come1364202008-02-28 01:06:41 +00001347
1348 endLine = result;
1349 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001350 }
1351 }
mitz@apple.come1364202008-02-28 01:06:41 +00001352
hyatt0c3a9862004-02-23 21:26:26 +00001353 return false;
1354}
1355
mitz@apple.com15035e62008-07-05 20:44:44 +00001356static inline bool skipNonBreakingSpace(const InlineIterator& it)
kocienda98440082004-10-14 23:51:47 +00001357{
darinf9e5d6c2007-01-09 14:54:26 +00001358 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001359 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001360
hyattdca76e92005-11-02 08:52:50 +00001361 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1362 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001363 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001364 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1365 // |true|).
1366 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001367 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001368
kocienda498d1982004-10-15 21:07:24 +00001369 return true;
kocienda98440082004-10-14 23:51:47 +00001370}
1371
hyattd9953212005-11-03 21:05:59 +00001372static inline bool shouldCollapseWhiteSpace(const RenderStyle* style)
1373{
1374 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1375}
1376
zimmermannac3781f2007-02-04 01:25:03 +00001377static inline bool shouldPreserveNewline(RenderObject* object)
1378{
mjsd2948ef2007-02-26 19:29:04 +00001379#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001380 if (object->isSVGText())
1381 return false;
1382#endif
1383
1384 return object->style()->preserveNewline();
1385}
1386
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001387static bool inlineFlowRequiresLineBox(RenderBox* flow)
bdakinf876bee2007-10-30 05:27:09 +00001388{
1389 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001390 // We need to fix this, though, because at the very least, inlines containing only
1391 // ignorable whitespace should should also have line boxes.
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001392 return flow->isRenderInline() && !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001393}
1394
mitz@apple.com15035e62008-07-05 20:44:44 +00001395static inline bool requiresLineBox(const InlineIterator& it)
bdashccffb432007-07-13 11:51:40 +00001396{
bdakinf876bee2007-10-30 05:27:09 +00001397 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001398 return false;
bdakinf876bee2007-10-30 05:27:09 +00001399
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001400 if (it.obj->isRenderInline() && !inlineFlowRequiresLineBox(toRenderBox(it.obj)))
bdakinf876bee2007-10-30 05:27:09 +00001401 return false;
1402
bdashccffb432007-07-13 11:51:40 +00001403 if (!shouldCollapseWhiteSpace(it.obj->style()) || it.obj->isBR())
1404 return true;
1405
1406 UChar current = it.current();
1407 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj)) && !skipNonBreakingSpace(it);
1408}
1409
1410bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
1411{
1412 ASSERT(inlineObj->parent() == this);
1413
mitz@apple.com15035e62008-07-05 20:44:44 +00001414 InlineIterator it(this, inlineObj, 0);
bdashccffb432007-07-13 11:51:40 +00001415 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001416 it.increment();
bdashccffb432007-07-13 11:51:40 +00001417
1418 return !it.atEnd();
1419}
1420
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001421// 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 +00001422// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1423// elements quite right. In other words, we need to build this function's work into the normal line
1424// object iteration process.
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001425// NB. this function will insert any floating elements that would otherwise
1426// be skipped but it will not position them.
1427void RenderBlock::skipTrailingWhitespace(InlineIterator& iterator)
kociendabb0c24b2001-08-24 14:24:40 +00001428{
mitz@apple.com1a301772008-03-11 18:30:36 +00001429 while (!iterator.atEnd() && !requiresLineBox(iterator)) {
1430 RenderObject* object = iterator.obj;
1431 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001432 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001433 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001434 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1435 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001436 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001437 if (c->isRenderInline()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001438 // A relative positioned inline encloses us. In this case, we also have to determine our
1439 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1440 // inline so that we can obtain the value later.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001441 c->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : rightOffset(height(), false));
hyatt@apple.comd885df72009-01-22 02:31:52 +00001442 c->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001443 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001444
1445 if (object->hasStaticX()) {
1446 if (object->style()->isOriginalDisplayInlineType())
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001447 object->setStaticX(style()->direction() == LTR ? leftOffset(height(), false) : width() - rightOffset(height(), false));
mitz@apple.come1364202008-02-28 01:06:41 +00001448 else
mitz@apple.com1a301772008-03-11 18:30:36 +00001449 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001450 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001451
1452 if (object->hasStaticY())
hyatt@apple.comd885df72009-01-22 02:31:52 +00001453 object->setStaticY(height());
hyatt33f8d492002-11-12 21:44:52 +00001454 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001455 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001456 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001457}
bdashccffb432007-07-13 11:51:40 +00001458
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001459int RenderBlock::skipLeadingWhitespace(InlineBidiResolver& resolver, bool firstLine)
mitz@apple.com1a301772008-03-11 18:30:36 +00001460{
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001461 int availableWidth = lineWidth(height(), firstLine);
mitz@apple.com15035e62008-07-05 20:44:44 +00001462 while (!resolver.position().atEnd() && !requiresLineBox(resolver.position())) {
1463 RenderObject* object = resolver.position().obj;
mitz@apple.com1a301772008-03-11 18:30:36 +00001464 if (object->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001465 insertFloatingObject(toRenderBox(object));
mitz@apple.com1a301772008-03-11 18:30:36 +00001466 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001467 availableWidth = lineWidth(height(), firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001468 } else if (object->isPositioned()) {
1469 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1470 // will work for the common cases
1471 RenderObject* c = object->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001472 if (c->isRenderInline()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001473 // A relative positioned inline encloses us. In this case, we also have to determine our
1474 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1475 // inline so that we can obtain the value later.
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001476 c->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : rightOffset(height(), firstLine));
hyatt@apple.comd885df72009-01-22 02:31:52 +00001477 c->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001478 }
1479
1480 if (object->hasStaticX()) {
1481 if (object->style()->isOriginalDisplayInlineType())
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001482 object->setStaticX(style()->direction() == LTR ? leftOffset(height(), firstLine) : width() - rightOffset(height(), firstLine));
mitz@apple.com1a301772008-03-11 18:30:36 +00001483 else
1484 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
1485 }
1486
1487 if (object->hasStaticY())
hyatt@apple.comd885df72009-01-22 02:31:52 +00001488 object->setStaticY(height());
mitz@apple.com1a301772008-03-11 18:30:36 +00001489 }
mitz@apple.com15035e62008-07-05 20:44:44 +00001490 resolver.increment();
mitz@apple.com1a301772008-03-11 18:30:36 +00001491 }
mitz@apple.com83d2e872008-10-23 21:56:03 +00001492 resolver.commitExplicitEmbedding();
mitz@apple.com1a301772008-03-11 18:30:36 +00001493 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001494}
1495
bdakinf876bee2007-10-30 05:27:09 +00001496// This is currently just used for list markers and inline flows that have line boxes. Neither should
1497// have an effect on whitespace at the start of the line.
mitz@apple.com1a301772008-03-11 18:30:36 +00001498static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o)
bdakinf876bee2007-10-30 05:27:09 +00001499{
mitz@apple.com1a301772008-03-11 18:30:36 +00001500 RenderObject* next = bidiNext(block, o);
darin@apple.com36744d62009-01-25 20:23:04 +00001501 if (next && !next->isBR() && next->isText() && toRenderText(next)->textLength() > 0) {
1502 RenderText* nextText = toRenderText(next);
bdakinf876bee2007-10-30 05:27:09 +00001503 UChar nextChar = nextText->characters()[0];
1504 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
mitz@apple.com15035e62008-07-05 20:44:44 +00001505 addMidpoint(InlineIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001506 return true;
1507 }
1508 }
1509
1510 return false;
1511}
1512
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001513void RenderBlock::fitBelowFloats(int widthToFit, bool firstLine, int& availableWidth)
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001514{
1515 ASSERT(widthToFit > availableWidth);
1516
1517 int floatBottom;
hyatt@apple.comd885df72009-01-22 02:31:52 +00001518 int lastFloatBottom = height();
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001519 int newLineWidth = availableWidth;
1520 while (true) {
1521 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1522 if (!floatBottom)
1523 break;
1524
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001525 newLineWidth = lineWidth(floatBottom, firstLine);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001526 lastFloatBottom = floatBottom;
1527 if (newLineWidth >= widthToFit)
1528 break;
1529 }
1530
1531 if (newLineWidth > availableWidth) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001532 setHeight(lastFloatBottom);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001533 availableWidth = newLineWidth;
1534 }
1535}
1536
mitz@apple.com34106442009-02-01 06:23:39 +00001537static inline unsigned textWidth(RenderText* text, unsigned from, unsigned len, const Font& font, int xPos, bool isFixedPitch, bool collapseWhiteSpace)
1538{
1539 if (isFixedPitch || !from && len == text->textLength())
1540 return text->width(from, len, font, xPos);
1541 return font.width(TextRun(text->characters() + from, len, !collapseWhiteSpace, xPos));
1542}
1543
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001544InlineIterator RenderBlock::findNextLineBreak(InlineBidiResolver& resolver, bool firstLine, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001545{
mitz@apple.com15035e62008-07-05 20:44:44 +00001546 ASSERT(resolver.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001547
mitz@apple.com15035e62008-07-05 20:44:44 +00001548 bool appliedStartWidth = resolver.position().pos > 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001549
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001550 int width = skipLeadingWhitespace(resolver, firstLine);
mitz@apple.com1a301772008-03-11 18:30:36 +00001551
kociendae40cb942004-10-05 20:05:38 +00001552 int w = 0;
1553 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001554
mitz@apple.com15035e62008-07-05 20:44:44 +00001555 if (resolver.position().atEnd())
1556 return resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001557
hyatt33f8d492002-11-12 21:44:52 +00001558 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1559 // or not we are currently ignoring whitespace.
1560 bool ignoringSpaces = false;
mitz@apple.com15035e62008-07-05 20:44:44 +00001561 InlineIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001562
1563 // This variable tracks whether the very last character we saw was a space. We use
1564 // this to detect when we encounter a second space so we know we have to terminate
1565 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001566 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001567 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001568 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001569
mitz@apple.com15035e62008-07-05 20:44:44 +00001570 InlineIterator lBreak = resolver.position();
mjs6f821c82002-03-22 00:31:57 +00001571
mitz@apple.com15035e62008-07-05 20:44:44 +00001572 RenderObject *o = resolver.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001573 RenderObject *last = o;
mitz@apple.com15035e62008-07-05 20:44:44 +00001574 unsigned pos = resolver.position().pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001575 int nextBreakable = resolver.position().nextBreakablePosition;
ddkilzere8759ef2007-03-25 06:28:19 +00001576 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001577
hyatt0c3a9862004-02-23 21:26:26 +00001578 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1579 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001580
1581 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001582 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001583
hyatt@apple.com69340902008-01-16 21:24:21 +00001584 // 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 +00001585 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001586 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
1587 bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
1588
hyattb0d9f602007-01-15 01:28:23 +00001589 EWhiteSpace currWS = style()->whiteSpace();
1590 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001591 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001592 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1593 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1594
1595 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001596 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001597
mjsd2948ef2007-02-26 19:29:04 +00001598#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001599 bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
1600#else
hyattb0d9f602007-01-15 01:28:23 +00001601 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001602#endif
1603
hyattb0d9f602007-01-15 01:28:23 +00001604 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1605
hyatt275d0702005-11-03 23:53:57 +00001606 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001607 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001608 lBreak.obj = o;
1609 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001610 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00001611 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001612
hyatt33f8d492002-11-12 21:44:52 +00001613 // A <br> always breaks a line, so don't let the line be collapsed
1614 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001615 // get collapsed away. It only does this if the previous line broke
1616 // cleanly. Otherwise the <br> has no effect on whether the line is
1617 // empty or not.
1618 if (prevLineBrokeCleanly)
1619 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001620 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001621 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001622
mitz@apple.com71e30842008-03-18 16:13:31 +00001623 if (!isLineEmpty && clear)
1624 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001625 }
1626 goto end;
1627 }
hyattb0d9f602007-01-15 01:28:23 +00001628
hyatt275d0702005-11-03 23:53:57 +00001629 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001630 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001631 if (o->isFloating()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001632 RenderBox* floatBox = toRenderBox(o);
1633 insertFloatingObject(floatBox);
hyatt33f8d492002-11-12 21:44:52 +00001634 // check if it fits in the current line.
1635 // If it does, position it now, otherwise, position
1636 // it after moving to next line (in newLine() func)
hyatt@apple.comd885df72009-01-22 02:31:52 +00001637 if (floatsFitOnLine && floatBox->width() + floatBox->marginLeft() + floatBox->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001638 positionNewFloats();
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001639 width = lineWidth(height(), firstLine);
mitz@apple.com25beac62008-02-24 18:48:27 +00001640 } else
1641 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001642 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001643 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001644 // go ahead and determine our static x position now.
hyatt851816b2003-07-08 07:54:17 +00001645 bool isInlineType = o->style()->isOriginalDisplayInlineType();
hyatt98ee7e42003-05-14 01:39:15 +00001646 bool needToSetStaticX = o->hasStaticX();
1647 if (o->hasStaticX() && !isInlineType) {
1648 o->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001649 borderLeft() + paddingLeft() :
1650 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001651 needToSetStaticX = false;
1652 }
1653
1654 // If our original display was an INLINE type, then we can go ahead
1655 // and determine our static y position now.
1656 bool needToSetStaticY = o->hasStaticY();
1657 if (o->hasStaticY() && isInlineType) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001658 o->setStaticY(height());
hyatt98ee7e42003-05-14 01:39:15 +00001659 needToSetStaticY = false;
1660 }
1661
hyatt853cd7d2004-11-05 02:59:48 +00001662 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1663 RenderObject* c = o->container();
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001664 if (c->isRenderInline() && (!needToSetStaticX || !needToSetStaticY))
hyatt853cd7d2004-11-05 02:59:48 +00001665 needToCreateLineBox = true;
1666
hyatt98ee7e42003-05-14 01:39:15 +00001667 // If we're ignoring spaces, we have to stop and include this object and
1668 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001669 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001670 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001671 ignoreStart.obj = o;
1672 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001673 if (ignoringSpaces) {
hyatt98b16282004-03-31 18:43:12 +00001674 addMidpoint(ignoreStart); // Stop ignoring spaces.
1675 addMidpoint(ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001676 }
hyatt98b16282004-03-31 18:43:12 +00001677
hyatt851816b2003-07-08 07:54:17 +00001678 }
hyatt98ee7e42003-05-14 01:39:15 +00001679 }
hyatt@apple.com415d8de2009-01-26 22:29:19 +00001680 } else if (o->isRenderInline()) {
bdakinf876bee2007-10-30 05:27:09 +00001681 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001682 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001683
hyatt@apple.com0c305632009-01-23 23:25:07 +00001684 RenderBox* flowBox = toRenderBox(o);
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001685
bdakinf876bee2007-10-30 05:27:09 +00001686 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1687 // to make sure that we stop to include this object and then start ignoring spaces again.
1688 // If this object is at the start of the line, we need to behave like list markers and
1689 // start ignoring spaces.
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001690 if (inlineFlowRequiresLineBox(flowBox)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001691 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001692 if (ignoringSpaces) {
1693 trailingSpaceObject = 0;
mitz@apple.com15035e62008-07-05 20:44:44 +00001694 addMidpoint(InlineIterator(0, o, 0)); // Stop ignoring spaces.
1695 addMidpoint(InlineIterator(0, o, 0)); // Start ignoring again.
1696 } else if (style()->collapseWhiteSpace() && resolver.position().obj == o
mitz@apple.com1a301772008-03-11 18:30:36 +00001697 && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001698 // Like with list markers, we start ignoring spaces to make sure that any
1699 // additional spaces we see will be discarded.
1700 currentCharacterIsSpace = true;
1701 currentCharacterIsWS = true;
1702 ignoringSpaces = true;
1703 }
1704 }
1705
hyatt@apple.com774bbed2009-01-23 05:13:22 +00001706 tmpW += flowBox->marginLeft() + flowBox->borderLeft() + flowBox->paddingLeft() +
1707 flowBox->marginRight() + flowBox->borderRight() + flowBox->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001708 } else if (o->isReplaced()) {
hyatt@apple.comd885df72009-01-22 02:31:52 +00001709 RenderBox* replacedBox = toRenderBox(o);
1710
hyattde396342003-10-29 08:57:20 +00001711 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001712 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001713 w += tmpW;
1714 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001715 lBreak.obj = o;
1716 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001717 lBreak.nextBreakablePosition = -1;
hyatt711fe232002-11-20 21:25:14 +00001718 }
1719
mitz@apple.combfdc9112008-02-21 19:59:40 +00001720 if (ignoringSpaces)
mitz@apple.com15035e62008-07-05 20:44:44 +00001721 addMidpoint(InlineIterator(0, o, 0));
mitz@apple.combfdc9112008-02-21 19:59:40 +00001722
hyatt33f8d492002-11-12 21:44:52 +00001723 isLineEmpty = false;
1724 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001725 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001726 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001727 trailingSpaceObject = 0;
hyatte85e4a72002-12-08 02:06:16 +00001728
bdakinf876bee2007-10-30 05:27:09 +00001729 // Optimize for a common case. If we can't find whitespace after the list
1730 // item, then this is all moot. -dwh
mjsd26b2972007-02-13 13:09:04 +00001731 if (o->isListMarker() && !static_cast<RenderListMarker*>(o)->isInside()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001732 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001733 // Like with inline flows, we start ignoring spaces to make sure that any
1734 // additional spaces we see will be discarded.
1735 currentCharacterIsSpace = true;
1736 currentCharacterIsWS = true;
1737 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001738 }
justing244d3a32006-04-13 01:31:24 +00001739 } else
hyatt@apple.comd885df72009-01-22 02:31:52 +00001740 tmpW += replacedBox->width() + replacedBox->marginLeft() + replacedBox->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001741 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001742 if (!pos)
1743 appliedStartWidth = false;
1744
darin@apple.com36744d62009-01-25 20:23:04 +00001745 RenderText* t = toRenderText(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001746
darin42563ac52007-01-22 17:28:57 +00001747 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001748 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001749 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001750
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001751 const Font& f = t->style(firstLine)->font();
mitz@apple.com34106442009-02-01 06:23:39 +00001752 bool isFixedPitch = f.isFixedPitch();
weinigf28a1c32007-02-14 14:10:31 +00001753
hyatt33f8d492002-11-12 21:44:52 +00001754 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001755 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001756 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001757
darin54008922006-01-13 16:39:05 +00001758 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001759 int charWidth = 0;
weinigf28a1c32007-02-14 14:10:31 +00001760 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1761 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1762 // 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 +00001763 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001764 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001765 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1766
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001767 if (t->isWordBreak()) {
1768 w += tmpW;
1769 tmpW = 0;
1770 lBreak.obj = o;
1771 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001772 lBreak.nextBreakablePosition = -1;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001773 ASSERT(!len);
1774 }
1775
hyatt275d0702005-11-03 23:53:57 +00001776 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001777 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001778 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001779 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001780 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001781
hyattb0d9f602007-01-15 01:28:23 +00001782 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001783 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001784
hyatt78b85132004-03-29 20:07:45 +00001785 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001786 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001787 if (!ignoringSpaces) {
1788 // Ignore soft hyphens
mitz@apple.com15035e62008-07-05 20:44:44 +00001789 InlineIterator beforeSoftHyphen;
mitz@apple.combe429562008-03-07 01:09:51 +00001790 if (pos)
mitz@apple.com15035e62008-07-05 20:44:44 +00001791 beforeSoftHyphen = InlineIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001792 else
darin@apple.com36744d62009-01-25 20:23:04 +00001793 beforeSoftHyphen = InlineIterator(0, last, last->isText() ? toRenderText(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001794 // Two consecutive soft hyphens. Avoid overlapping midpoints.
mitz@apple.combe429562008-03-07 01:09:51 +00001795 if (sNumMidpoints && smidpoints->at(sNumMidpoints - 1).obj == o && smidpoints->at(sNumMidpoints - 1).pos == pos)
bdakin9151ba52005-10-24 22:51:06 +00001796 sNumMidpoints--;
1797 else
mitz@apple.combe429562008-03-07 01:09:51 +00001798 addMidpoint(beforeSoftHyphen);
1799
hyatt78b85132004-03-29 20:07:45 +00001800 // Add the width up to but not including the hyphen.
mitz@apple.com34106442009-02-01 06:23:39 +00001801 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001802
hyattdca76e92005-11-02 08:52:50 +00001803 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001804 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001805 if (autoWrap)
mitz@apple.com34106442009-02-01 06:23:39 +00001806 tmpW += textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
mitz@apple.combe429562008-03-07 01:09:51 +00001807
mitz@apple.com15035e62008-07-05 20:44:44 +00001808 InlineIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001809 afterSoftHyphen.increment();
mitz@apple.combe429562008-03-07 01:09:51 +00001810 addMidpoint(afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001811 }
mitz@apple.combe429562008-03-07 01:09:51 +00001812
hyatt78b85132004-03-29 20:07:45 +00001813 pos++;
1814 len--;
eseideld13fe532005-11-30 02:40:29 +00001815 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001816 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
1817 continue;
1818 }
1819
hyatt3aff2332003-01-23 01:15:28 +00001820 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001821
darinf9e5d6c2007-01-09 14:54:26 +00001822 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001823
weiniged111c12007-07-13 22:45:11 +00001824 if ((breakAll || breakWords) && !midWordBreak) {
1825 wrapW += charWidth;
mitz@apple.com34106442009-02-01 06:23:39 +00001826 charWidth = textWidth(t, pos, 1, f, w + wrapW, isFixedPitch, collapseWhiteSpace);
weiniged111c12007-07-13 22:45:11 +00001827 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001828 }
darin47ece0d2005-09-04 07:42:31 +00001829
ddkilzere8759ef2007-03-25 06:28:19 +00001830 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001831
weiniged111c12007-07-13 22:45:11 +00001832 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001833 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001834 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001835 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001836 // Stop ignoring spaces and begin at this
1837 // new point.
hyatt48710d62003-08-21 09:17:13 +00001838 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001839 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001840 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.com15035e62008-07-05 20:44:44 +00001841 addMidpoint(InlineIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001842 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001843 } else {
hyatt33f8d492002-11-12 21:44:52 +00001844 // Just keep ignoring these spaces.
1845 pos++;
1846 len--;
1847 continue;
1848 }
1849 }
rjwc9c257d2003-01-24 03:46:17 +00001850
mitz@apple.com34106442009-02-01 06:23:39 +00001851 int additionalTmpW = textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
darin54008922006-01-13 16:39:05 +00001852 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001853 if (!appliedStartWidth) {
1854 tmpW += inlineWidth(o, true, false);
1855 appliedStartWidth = true;
1856 }
1857
eseideld13fe532005-11-30 02:40:29 +00001858 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001859
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001860 if (!w && autoWrap && tmpW > width)
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00001861 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001862
hyattb0d9f602007-01-15 01:28:23 +00001863 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001864 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001865 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001866 bool lineWasTooWide = false;
1867 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
mitz@apple.com34106442009-02-01 06:23:39 +00001868 int charWidth = textWidth(t, pos, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + (applyWordSpacing ? wordSpacing : 0);
ap932806a2006-10-01 09:06:09 +00001869 // Check if line is too big even without the extra space
1870 // at the end of the line. If it is not, do nothing.
1871 // If the line needs the extra whitespace to be too long,
1872 // then move the line break to the space and skip all
1873 // additional whitespace.
1874 if (w + tmpW + charWidth > width) {
1875 lineWasTooWide = true;
1876 lBreak.obj = o;
1877 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001878 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.combf6e8d32008-07-25 20:21:06 +00001879 skipTrailingWhitespace(lBreak);
kocienda9dbe9b12004-10-22 20:07:05 +00001880 }
ap932806a2006-10-01 09:06:09 +00001881 }
1882 if (lineWasTooWide || w + tmpW > width) {
darin@apple.com36744d62009-01-25 20:23:04 +00001883 if (lBreak.obj && shouldPreserveNewline(lBreak.obj) && lBreak.obj->isText() && !toRenderText(lBreak.obj)->isWordBreak() && toRenderText(lBreak.obj)->characters()[lBreak.pos] == '\n') {
hyatt0c05e102006-04-14 08:15:00 +00001884 if (!stoppedIgnoringSpaces && pos > 0) {
1885 // We need to stop right before the newline and then start up again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001886 addMidpoint(InlineIterator(0, o, pos - 1)); // Stop
1887 addMidpoint(InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001888 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001889 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001890 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001891 }
hyatt78b85132004-03-29 20:07:45 +00001892 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001893 } else {
weiniged111c12007-07-13 22:45:11 +00001894 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001895 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001896 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001897 // Subtract the width of the soft hyphen out since we fit on a line.
mitz@apple.com34106442009-02-01 06:23:39 +00001898 tmpW -= textWidth(t, pos - 1, 1, f, w + tmpW, isFixedPitch, collapseWhiteSpace);
darin54008922006-01-13 16:39:05 +00001899 }
rjwc9c257d2003-01-24 03:46:17 +00001900 }
hyatt33f8d492002-11-12 21:44:52 +00001901
hyattb0d9f602007-01-15 01:28:23 +00001902 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001903 if (!stoppedIgnoringSpaces && pos > 0) {
1904 // We need to stop right before the newline and then start up again.
mitz@apple.com15035e62008-07-05 20:44:44 +00001905 addMidpoint(InlineIterator(0, o, pos - 1)); // Stop
1906 addMidpoint(InlineIterator(0, o, pos)); // Start
hyatt0c05e102006-04-14 08:15:00 +00001907 }
hyatta9f48e32003-02-03 22:48:01 +00001908 lBreak.obj = o;
1909 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001910 lBreak.nextBreakablePosition = nextBreakable;
mitz@apple.com1a301772008-03-11 18:30:36 +00001911 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001912 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001913 return lBreak;
1914 }
hyatta9f48e32003-02-03 22:48:01 +00001915
weinigf28a1c32007-02-14 14:10:31 +00001916 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001917 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001918 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001919 tmpW = 0;
1920 lBreak.obj = o;
1921 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001922 lBreak.nextBreakablePosition = nextBreakable;
weinigf28a1c32007-02-14 14:10:31 +00001923 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1924 // opportunity to break after a word.
1925 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001926 }
hyatt33f8d492002-11-12 21:44:52 +00001927
darin54008922006-01-13 16:39:05 +00001928 if (midWordBreak) {
1929 // Remember this as a breakable position in case
1930 // adding the end width forces a break.
1931 lBreak.obj = o;
1932 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001933 lBreak.nextBreakablePosition = nextBreakable;
weiniged111c12007-07-13 22:45:11 +00001934 midWordBreak &= (breakWords || breakAll);
1935 }
1936
1937 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001938 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1939 lastSpace = pos;
1940 }
hyatt33f8d492002-11-12 21:44:52 +00001941
hyattdca76e92005-11-02 08:52:50 +00001942 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001943 // If we encounter a newline, or if we encounter a
1944 // second space, we need to go ahead and break up this
1945 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001946 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001947 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001948
hyatt33f8d492002-11-12 21:44:52 +00001949 // We just entered a mode where we are ignoring
1950 // spaces. Create a midpoint to terminate the run
1951 // before the second space.
hyatt98b16282004-03-31 18:43:12 +00001952 addMidpoint(ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001953 }
1954 }
eseidel789896f2005-11-27 22:52:09 +00001955 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001956 // Stop ignoring spaces and begin at this
1957 // new point.
1958 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001959 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001960 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.com15035e62008-07-05 20:44:44 +00001961 addMidpoint(InlineIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001962 }
hyatt98b16282004-03-31 18:43:12 +00001963
1964 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1965 ignoreStart.obj = o;
1966 ignoreStart.pos = pos;
1967 }
harrisone343c412005-01-18 01:07:26 +00001968
1969 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001970 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001971 lBreak.obj = o;
1972 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00001973 lBreak.nextBreakablePosition = nextBreakable;
harrisone343c412005-01-18 01:07:26 +00001974 }
1975 }
hyatt33f8d492002-11-12 21:44:52 +00001976
hyattb0d9f602007-01-15 01:28:23 +00001977 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001978 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001979 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001980 trailingSpaceObject = 0;
1981
1982 pos++;
1983 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001984 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001985 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001986
kociendabb0c24b2001-08-24 14:24:40 +00001987 // IMPORTANT: pos is > length here!
hyatt33f8d492002-11-12 21:44:52 +00001988 if (!ignoringSpaces)
mitz@apple.com34106442009-02-01 06:23:39 +00001989 tmpW += textWidth(t, lastSpace, pos - lastSpace, f, w + tmpW, isFixedPitch, collapseWhiteSpace) + lastSpaceWordSpacing;
weinigf28a1c32007-02-14 14:10:31 +00001990 tmpW += inlineWidth(o, !appliedStartWidth, true);
kociendabb0c24b2001-08-24 14:24:40 +00001991 } else
weinigf28a1c32007-02-14 14:10:31 +00001992 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001993
mitz@apple.com1a301772008-03-11 18:30:36 +00001994 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001995 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001996 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00001997 checkForBreak = true;
1998 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00001999 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002000 if (currentCharacterIsSpace)
2001 checkForBreak = true;
2002 else {
harrison07b5e582005-08-15 23:31:16 +00002003 checkForBreak = false;
darin@apple.com36744d62009-01-25 20:23:04 +00002004 RenderText* nextText = toRenderText(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002005 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00002006 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00002007 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00002008 // If the next item on the line is text, and if we did not end with
2009 // a space, then the next text run continues our word (and so it needs to
2010 // keep adding to |tmpW|. Just update and continue.
2011 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002012 } else if (nextText->isWordBreak())
2013 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002014 bool willFitOnLine = w + tmpW <= width;
2015 if (!willFitOnLine && !w) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002016 fitBelowFloats(tmpW, firstLine, width);
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002017 willFitOnLine = tmpW <= width;
2018 }
ddkilzere8759ef2007-03-25 06:28:19 +00002019 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00002020 if (canPlaceOnLine && checkForBreak) {
2021 w += tmpW;
2022 tmpW = 0;
2023 lBreak.obj = next;
2024 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002025 lBreak.nextBreakablePosition = -1;
hyatta9f48e32003-02-03 22:48:01 +00002026 }
2027 }
2028 }
2029 }
2030
darin54008922006-01-13 16:39:05 +00002031 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002032 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002033 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002034 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002035
2036 if (w)
2037 goto end;
2038
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002039 fitBelowFloats(tmpW, firstLine, width);
hyattf14a4a32002-11-21 22:06:32 +00002040
hyatta14d1742003-01-02 20:25:46 +00002041 // |width| may have been adjusted because we got shoved down past a float (thus
2042 // giving us more room), so we need to retest, and only jump to
2043 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002044 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002045 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002046 }
hyatt1d9e29b2003-04-10 01:48:53 +00002047
mitz@apple.com1a301772008-03-11 18:30:36 +00002048 if (!o->isFloatingOrPositioned()) {
2049 last = o;
2050 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || static_cast<RenderListMarker*>(last)->isInside())) {
2051 w += tmpW;
2052 tmpW = 0;
2053 lBreak.obj = next;
2054 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002055 lBreak.nextBreakablePosition = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002056 }
hyatt711fe232002-11-20 21:25:14 +00002057 }
2058
mitz@apple.com1a301772008-03-11 18:30:36 +00002059 o = next;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002060 nextBreakable = -1;
mitz@apple.com1a301772008-03-11 18:30:36 +00002061
hyatta9f48e32003-02-03 22:48:01 +00002062 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2063 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002064 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002065 currentCharacterIsSpace = false;
2066
kociendabb0c24b2001-08-24 14:24:40 +00002067 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002068 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002069 }
2070
hyattb0d9f602007-01-15 01:28:23 +00002071
2072 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00002073 lBreak.obj = 0;
2074 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002075 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002076 }
2077
2078 end:
rjwc9c257d2003-01-24 03:46:17 +00002079
mitz@apple.com15035e62008-07-05 20:44:44 +00002080 if (lBreak == resolver.position() && !lBreak.obj->isBR()) {
kociendabb0c24b2001-08-24 14:24:40 +00002081 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002082 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002083 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002084 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002085 lBreak.obj = o;
2086 lBreak.pos = pos - 1;
2087 } else {
2088 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002089 lBreak.pos = last->isText() ? last->length() : 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002090 lBreak.nextBreakablePosition = -1;
kociendabb0c24b2001-08-24 14:24:40 +00002091 }
hyatt275d0702005-11-03 23:53:57 +00002092 } else if (lBreak.obj) {
2093 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002094 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002095 lBreak.obj = o;
2096 lBreak.pos = 0;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002097 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002098 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002099 // Don't ever break in the middle of a word if we can help it.
2100 // There's no room at all. We just have to be on this line,
2101 // even though we'll spill out.
2102 lBreak.obj = o;
2103 lBreak.pos = pos;
mitz@apple.comd17dc392008-09-15 18:48:20 +00002104 lBreak.nextBreakablePosition = -1;
hyatt33f8d492002-11-12 21:44:52 +00002105 }
kociendabb0c24b2001-08-24 14:24:40 +00002106 }
2107 }
2108
2109 // make sure we consume at least one char/object.
mitz@apple.com15035e62008-07-05 20:44:44 +00002110 if (lBreak == resolver.position())
mitz@apple.com1a301772008-03-11 18:30:36 +00002111 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002112
hyattfe99c872003-07-31 22:25:29 +00002113 // Sanity check our midpoints.
mitz@apple.com1a301772008-03-11 18:30:36 +00002114 checkMidpoints(lBreak);
hyattfe99c872003-07-31 22:25:29 +00002115
hyatt33f8d492002-11-12 21:44:52 +00002116 if (trailingSpaceObject) {
2117 // This object is either going to be part of the last midpoint, or it is going
2118 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2119 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt85586af2003-02-19 23:22:42 +00002120 if (sNumMidpoints%2==1) {
mitz@apple.com15035e62008-07-05 20:44:44 +00002121 InlineIterator* midpoints = smidpoints->data();
hyatt85586af2003-02-19 23:22:42 +00002122 midpoints[sNumMidpoints-1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002123 }
2124 //else if (lBreak.pos > 0)
2125 // lBreak.pos--;
2126 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002127 // Add a new end midpoint that stops right at the very end.
darin@apple.com36744d62009-01-25 20:23:04 +00002128 RenderText* text = toRenderText(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002129 unsigned length = text->textLength();
2130 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
mitz@apple.com15035e62008-07-05 20:44:44 +00002131 InlineIterator endMid(0, trailingSpaceObject, pos);
hyatt85586af2003-02-19 23:22:42 +00002132 addMidpoint(endMid);
hyatt33f8d492002-11-12 21:44:52 +00002133 }
2134 }
rjwc9c257d2003-01-24 03:46:17 +00002135
mjs54b64002003-04-02 02:59:02 +00002136 // We might have made lBreak an iterator that points past the end
2137 // of the object. Do this adjustment to make it point to the start
2138 // of the next object instead to avoid confusing the rest of the
2139 // code.
2140 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002141 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002142 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002143 }
2144
hyatt78b85132004-03-29 20:07:45 +00002145 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2146 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2147 // ignore the hyphen so that it will render at the end of the line.
darin@apple.com36744d62009-01-25 20:23:04 +00002148 UChar c = toRenderText(lBreak.obj)->characters()[lBreak.pos-1];
darin42563ac52007-01-22 17:28:57 +00002149 if (c == softHyphen)
hyatt78b85132004-03-29 20:07:45 +00002150 chopMidpointsAt(lBreak.obj, lBreak.pos-2);
2151 }
2152
kociendabb0c24b2001-08-24 14:24:40 +00002153 return lBreak;
2154}
2155
hyattb4b20872004-10-20 21:34:01 +00002156void RenderBlock::checkLinesForOverflow()
2157{
hyatt@apple.comd885df72009-01-22 02:31:52 +00002158 m_overflowWidth = width();
hyattb4b20872004-10-20 21:34:01 +00002159 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
darin7bd70952006-04-13 07:07:34 +00002160 m_overflowLeft = min(curr->leftOverflow(), m_overflowLeft);
2161 m_overflowTop = min(curr->topOverflow(), m_overflowTop);
2162 m_overflowWidth = max(curr->rightOverflow(), m_overflowWidth);
2163 m_overflowHeight = max(curr->bottomOverflow(), m_overflowHeight);
hyattb4b20872004-10-20 21:34:01 +00002164 }
2165}
2166
hyatted77ad82004-06-15 07:21:23 +00002167void RenderBlock::deleteEllipsisLineBoxes()
2168{
hyatted77ad82004-06-15 07:21:23 +00002169 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002170 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002171}
2172
2173void RenderBlock::checkLinesForTextOverflow()
2174{
2175 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002176 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2177 TextRun ellipsisRun(&horizontalEllipsis, 1);
bolsinga@apple.com97e42c42008-11-15 04:47:20 +00002178 DEFINE_STATIC_LOCAL(AtomicString, ellipsisStr, (&horizontalEllipsis, 1));
hyatt3e99d1c2006-02-24 21:13:08 +00002179 const Font& firstLineFont = firstLineStyle()->font();
2180 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002181 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2182 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002183
2184 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2185 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2186 // check the left edge of the line box to see if it is less
2187 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2188 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002189 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyatt@apple.comcd6f8952009-01-28 17:30:26 +00002190 int blockEdge = ltr ? rightOffset(curr->yPos(), curr == firstRootBox()) : leftOffset(curr->yPos(), curr == firstRootBox());
hyatt1a8d2512004-06-17 01:38:30 +00002191 int lineBoxEdge = ltr ? curr->xPos() + curr->width() : curr->xPos();
hyatted77ad82004-06-15 07:21:23 +00002192 if ((ltr && lineBoxEdge > blockEdge) || (!ltr && lineBoxEdge < blockEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002193 // 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 +00002194 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2195 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2196 // space.
2197 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
hyatt1a8d2512004-06-17 01:38:30 +00002198 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
2199 curr->placeEllipsis(ellipsisStr, ltr, blockEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002200 }
2201 }
2202}
2203
hyattffe78712003-02-11 01:59:29 +00002204}