blob: d79a93a68feb8b6a389c770cb06cbd1fd3422175 [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"
darinec375482007-01-06 01:36:24 +000032#include "RenderLayer.h"
mjsd26b2972007-02-13 13:09:04 +000033#include "RenderListMarker.h"
hyattd8048342006-05-31 01:48:18 +000034#include "RenderView.h"
darin36d11362006-04-11 16:30:21 +000035#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000036#include <wtf/AlwaysInline.h>
darin91298e52006-06-12 01:10:17 +000037#include <wtf/Vector.h>
hyatt8c371e52004-06-16 01:19:26 +000038
darin7bd70952006-04-13 07:07:34 +000039using namespace std;
darinf9e5d6c2007-01-09 14:54:26 +000040using namespace WTF;
41using namespace Unicode;
darin7bd70952006-04-13 07:07:34 +000042
darinb9481ed2006-03-20 02:57:59 +000043namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000044
hyatt1d5d87b2007-04-24 04:55:54 +000045// We don't let our line box tree for a single line get any deeper than this.
46const unsigned cMaxLineDepth = 200;
47
pewtermoosecf72e2d2007-07-20 19:01:55 +000048class BidiIterator {
49public:
50 BidiIterator()
51 : block(0)
52 , obj(0)
53 , pos(0)
54 {
55 }
56
57 BidiIterator(RenderBlock* b, RenderObject* o, unsigned p)
58 : block(b)
59 , obj(o)
60 , pos(p)
61 {
62 }
63
mitz@apple.com1a301772008-03-11 18:30:36 +000064 void increment(BidiState* resolver = 0);
mjsfe301d72003-10-02 18:46:18 +000065 bool atEnd() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000066
darin7ab31092006-05-10 04:59:57 +000067 UChar current() const;
pewtermoosecf72e2d2007-07-20 19:01:55 +000068 WTF::Unicode::Direction direction() const;
hyatt275d0702005-11-03 23:53:57 +000069
70 RenderBlock* block;
71 RenderObject* obj;
mitz@apple.com1a301772008-03-11 18:30:36 +000072 unsigned pos;
mjsfe301d72003-10-02 18:46:18 +000073};
74
hyatt85586af2003-02-19 23:22:42 +000075// Midpoint globals. The goal is not to do any allocation when dealing with
76// these midpoints, so we just keep an array around and never clear it. We track
77// the number of items and position using the two other variables.
darin91298e52006-06-12 01:10:17 +000078static Vector<BidiIterator>* smidpoints;
darinb9481ed2006-03-20 02:57:59 +000079static unsigned sNumMidpoints;
80static unsigned sCurrMidpoint;
mjsfe301d72003-10-02 18:46:18 +000081static bool betweenMidpoints;
hyatt85586af2003-02-19 23:22:42 +000082
hyatt33f8d492002-11-12 21:44:52 +000083static bool isLineEmpty = true;
hyatt0c3a9862004-02-23 21:26:26 +000084static bool previousLineBrokeCleanly = true;
mjs6f821c82002-03-22 00:31:57 +000085
hyattffe78712003-02-11 01:59:29 +000086static int getBorderPaddingMargin(RenderObject* child, bool endOfInline)
87{
hyatt@apple.com21cc37a2008-02-26 23:17:03 +000088 bool leftSide = (child->style()->direction() == LTR) ? !endOfInline : endOfInline;
89 if (leftSide)
90 return child->marginLeft() + child->paddingLeft() + child->borderLeft();
91 return child->marginRight() + child->paddingRight() + child->borderRight();
hyattffe78712003-02-11 01:59:29 +000092}
93
94static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
95{
hyatt1d5d87b2007-04-24 04:55:54 +000096 unsigned lineDepth = 1;
hyattffe78712003-02-11 01:59:29 +000097 int extraWidth = 0;
98 RenderObject* parent = child->parent();
hyatt1d5d87b2007-04-24 04:55:54 +000099 while (parent->isInline() && !parent->isInlineBlockOrInlineTable() && lineDepth++ < cMaxLineDepth) {
hyattffe78712003-02-11 01:59:29 +0000100 if (start && parent->firstChild() == child)
101 extraWidth += getBorderPaddingMargin(parent, false);
102 if (end && parent->lastChild() == child)
103 extraWidth += getBorderPaddingMargin(parent, true);
104 child = parent;
105 parent = child->parent();
106 }
107 return extraWidth;
108}
109
darin35355e52002-12-20 09:19:00 +0000110#ifndef NDEBUG
ggarenec11e5b2007-02-25 02:14:54 +0000111WTFLogChannel LogWebCoreBidiRunLeaks = { 0x00000000, "", WTFLogChannelOn };
112
113struct BidiRunCounter {
114 static int count;
115 ~BidiRunCounter()
116 {
117 if (count)
118 LOG(WebCoreBidiRunLeaks, "LEAK: %d BidiRun\n", count);
119 }
120};
121int BidiRunCounter::count = 0;
122static BidiRunCounter bidiRunCounter;
123
harrison0012ced2005-10-06 18:37:42 +0000124static bool inBidiRunDestroy;
hyattffe78712003-02-11 01:59:29 +0000125#endif
126
mitz@apple.come1364202008-02-28 01:06:41 +0000127void BidiRun::destroy()
hyattffe78712003-02-11 01:59:29 +0000128{
129#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000130 inBidiRunDestroy = true;
hyattffe78712003-02-11 01:59:29 +0000131#endif
mitz@apple.come1364202008-02-28 01:06:41 +0000132 RenderArena* renderArena = m_object->renderArena();
hyattffe78712003-02-11 01:59:29 +0000133 delete this;
134#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000135 inBidiRunDestroy = false;
hyattffe78712003-02-11 01:59:29 +0000136#endif
137
138 // Recover the size left there for us by operator delete and free the memory.
mitz@apple.come1364202008-02-28 01:06:41 +0000139 renderArena->free(*reinterpret_cast<size_t*>(this), this);
hyattffe78712003-02-11 01:59:29 +0000140}
141
142void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
143{
ggarenec11e5b2007-02-25 02:14:54 +0000144#ifndef NDEBUG
145 ++BidiRunCounter::count;
146#endif
hyattffe78712003-02-11 01:59:29 +0000147 return renderArena->allocate(sz);
148}
149
150void BidiRun::operator delete(void* ptr, size_t sz)
151{
ggarenec11e5b2007-02-25 02:14:54 +0000152#ifndef NDEBUG
153 --BidiRunCounter::count;
154#endif
ggarenf9f32ae2007-03-26 20:08:53 +0000155 ASSERT(inBidiRunDestroy);
hyattffe78712003-02-11 01:59:29 +0000156
harrisone8363b42005-10-06 00:54:06 +0000157 // Stash size where destroy() can find it.
hyattffe78712003-02-11 01:59:29 +0000158 *(size_t*)ptr = sz;
159}
160
kociendabb0c24b2001-08-24 14:24:40 +0000161// ---------------------------------------------------------------------
162
hyatt275d0702005-11-03 23:53:57 +0000163inline bool operator==(const BidiIterator& it1, const BidiIterator& 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
hyatt275d0702005-11-03 23:53:57 +0000168inline bool operator!=(const BidiIterator& it1, const BidiIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000169{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000170 return it1.pos != it2.pos || it1.obj != it2.obj;
darinb70665a2002-04-15 23:43:21 +0000171}
172
mitz@apple.com1a301772008-03-11 18:30:36 +0000173static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, BidiState* bidi = 0, bool skipInlines = true, bool* endOfInlinePtr = 0)
mjs6f821c82002-03-22 00:31:57 +0000174{
hyatt275d0702005-11-03 23:53:57 +0000175 RenderObject* next = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +0000176 bool oldEndOfInline = endOfInlinePtr ? *endOfInlinePtr : false;
177 bool endOfInline = false;
hyattffe78712003-02-11 01:59:29 +0000178
hyatt275d0702005-11-03 23:53:57 +0000179 while (current) {
sullivanabd4d032007-02-09 22:51:41 +0000180 next = 0;
hyattffe78712003-02-11 01:59:29 +0000181 if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned()) {
182 next = current->firstChild();
mitz@apple.com1a301772008-03-11 18:30:36 +0000183 if (next && bidi && next->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000184 EUnicodeBidi ub = next->style()->unicodeBidi();
hyatt275d0702005-11-03 23:53:57 +0000185 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000186 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000187 Direction d = (ub == Embed
188 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
189 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com1a301772008-03-11 18:30:36 +0000190 bidi->embed(d);
hyattffe78712003-02-11 01:59:29 +0000191 }
192 }
193 }
hyatt275d0702005-11-03 23:53:57 +0000194
hyattffe78712003-02-11 01:59:29 +0000195 if (!next) {
hyatt275d0702005-11-03 23:53:57 +0000196 if (!skipInlines && !oldEndOfInline && current->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000197 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000198 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000199 break;
200 }
mjs6f821c82002-03-22 00:31:57 +0000201
hyatt275d0702005-11-03 23:53:57 +0000202 while (current && current != block) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000203 if (bidi && current->isInlineFlow() && current->style()->unicodeBidi() != UBNormal)
204 bidi->embed(PopDirectionalFormat);
hyatt275d0702005-11-03 23:53:57 +0000205
darindde01502005-12-18 22:55:35 +0000206 next = current->nextSibling();
207 if (next) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000208 if (bidi && next->isInlineFlow()) {
darindde01502005-12-18 22:55:35 +0000209 EUnicodeBidi ub = next->style()->unicodeBidi();
210 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000211 TextDirection dir = next->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000212 Direction d = (ub == Embed
213 ? (dir == RTL ? RightToLeftEmbedding: LeftToRightEmbedding)
214 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com1a301772008-03-11 18:30:36 +0000215 bidi->embed(d);
darindde01502005-12-18 22:55:35 +0000216 }
217 }
218 break;
219 }
220
hyattffe78712003-02-11 01:59:29 +0000221 current = current->parent();
hyatt275d0702005-11-03 23:53:57 +0000222 if (!skipInlines && current && current != block && current->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000223 next = current;
mitz@apple.com1a301772008-03-11 18:30:36 +0000224 endOfInline = true;
hyattffe78712003-02-11 01:59:29 +0000225 break;
226 }
227 }
228 }
mjs6f821c82002-03-22 00:31:57 +0000229
hyatt275d0702005-11-03 23:53:57 +0000230 if (!next)
231 break;
hyattffe78712003-02-11 01:59:29 +0000232
mitz@apple.combfdc9112008-02-21 19:59:40 +0000233 if (next->isText() || next->isFloating() || next->isReplaced() || next->isPositioned()
hyattffe78712003-02-11 01:59:29 +0000234 || ((!skipInlines || !next->firstChild()) // Always return EMPTY inlines.
235 && next->isInlineFlow()))
mjs6f821c82002-03-22 00:31:57 +0000236 break;
237 current = next;
238 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000239
240 if (endOfInlinePtr)
241 *endOfInlinePtr = endOfInline;
242
mjs6f821c82002-03-22 00:31:57 +0000243 return next;
244}
245
mitz@apple.com1a301772008-03-11 18:30:36 +0000246static RenderObject* bidiFirst(RenderBlock* block, BidiState* bidi, bool skipInlines = true)
mjs6f821c82002-03-22 00:31:57 +0000247{
hyatt275d0702005-11-03 23:53:57 +0000248 if (!block->firstChild())
249 return 0;
250
251 RenderObject* o = block->firstChild();
hyattffe78712003-02-11 01:59:29 +0000252 if (o->isInlineFlow()) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000253 if (bidi) {
darindde01502005-12-18 22:55:35 +0000254 EUnicodeBidi ub = o->style()->unicodeBidi();
255 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000256 TextDirection dir = o->style()->direction();
darinf9e5d6c2007-01-09 14:54:26 +0000257 Direction d = (ub == Embed
258 ? (dir == RTL ? RightToLeftEmbedding : LeftToRightEmbedding)
259 : (dir == RTL ? RightToLeftOverride : LeftToRightOverride));
mitz@apple.com1a301772008-03-11 18:30:36 +0000260 bidi->embed(d);
darindde01502005-12-18 22:55:35 +0000261 }
262 }
hyattffe78712003-02-11 01:59:29 +0000263 if (skipInlines && o->firstChild())
hyatt275d0702005-11-03 23:53:57 +0000264 o = bidiNext(block, o, bidi, skipInlines);
hyattffe78712003-02-11 01:59:29 +0000265 else
266 return o; // Never skip empty inlines.
267 }
hyattc5334f12003-08-08 22:26:08 +0000268
mitz@apple.combfdc9112008-02-21 19:59:40 +0000269 if (o && !o->isText() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
hyatt275d0702005-11-03 23:53:57 +0000270 o = bidiNext(block, o, bidi, skipInlines);
mjs6f821c82002-03-22 00:31:57 +0000271 return o;
272}
273
mitz@apple.com1a301772008-03-11 18:30:36 +0000274inline void BidiIterator::increment(BidiState* bidi)
kociendabb0c24b2001-08-24 14:24:40 +0000275{
hyatt275d0702005-11-03 23:53:57 +0000276 if (!obj)
277 return;
278 if (obj->isText()) {
kociendabb0c24b2001-08-24 14:24:40 +0000279 pos++;
mitz@apple.com1a301772008-03-11 18:30:36 +0000280 if (pos >= static_cast<RenderText*>(obj)->textLength()) {
hyatt275d0702005-11-03 23:53:57 +0000281 obj = bidiNext(block, obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000282 pos = 0;
283 }
284 } else {
hyatt275d0702005-11-03 23:53:57 +0000285 obj = bidiNext(block, obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000286 pos = 0;
287 }
288}
289
mitz@apple.com1a301772008-03-11 18:30:36 +0000290template<>
291inline void BidiState::increment()
292{
293 current.increment(this);
294}
295
mjs6f821c82002-03-22 00:31:57 +0000296inline bool BidiIterator::atEnd() const
kociendabb0c24b2001-08-24 14:24:40 +0000297{
hyatt275d0702005-11-03 23:53:57 +0000298 return !obj;
kociendabb0c24b2001-08-24 14:24:40 +0000299}
300
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000301inline UChar BidiIterator::current() const
kociendabb0c24b2001-08-24 14:24:40 +0000302{
hyatt30586b42003-12-02 23:19:11 +0000303 if (!obj || !obj->isText())
darin7ab31092006-05-10 04:59:57 +0000304 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000305
hyatt30586b42003-12-02 23:19:11 +0000306 RenderText* text = static_cast<RenderText*>(obj);
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000307 if (pos >= text->textLength())
darin7ab31092006-05-10 04:59:57 +0000308 return 0;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000309
darin42563ac52007-01-22 17:28:57 +0000310 return text->characters()[pos];
kociendabb0c24b2001-08-24 14:24:40 +0000311}
312
darinf9e5d6c2007-01-09 14:54:26 +0000313ALWAYS_INLINE Direction BidiIterator::direction() const
kociendabb0c24b2001-08-24 14:24:40 +0000314{
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000315 if (UChar c = current())
316 return Unicode::direction(c);
317
318 if (obj && obj->isListMarker())
darinf9e5d6c2007-01-09 14:54:26 +0000319 return obj->style()->direction() == LTR ? LeftToRight : RightToLeft;
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000320
321 return OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000322}
323
kociendabb0c24b2001-08-24 14:24:40 +0000324// -------------------------------------------------------------------------------------------------
325
darinb9481ed2006-03-20 02:57:59 +0000326static void chopMidpointsAt(RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +0000327{
hyatt275d0702005-11-03 23:53:57 +0000328 if (!sNumMidpoints)
329 return;
hyatt78b85132004-03-29 20:07:45 +0000330 BidiIterator* midpoints = smidpoints->data();
mitz@apple.comc92d1282008-02-28 05:38:52 +0000331 for (int i = sNumMidpoints - 1; i >= 0; i--) {
hyatt78b85132004-03-29 20:07:45 +0000332 const BidiIterator& point = midpoints[i];
333 if (point.obj == obj && point.pos == pos) {
334 sNumMidpoints = i;
335 break;
336 }
337 }
338}
339
mitz@apple.com1a301772008-03-11 18:30:36 +0000340static void checkMidpoints(BidiIterator& lBreak)
hyattfe99c872003-07-31 22:25:29 +0000341{
342 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000343 // shave it off the list, and shave off a trailing space if the previous end point doesn't
344 // preserve whitespace.
mitz@apple.combe429562008-03-07 01:09:51 +0000345 if (lBreak.obj && sNumMidpoints && sNumMidpoints % 2 == 0) {
hyattfe99c872003-07-31 22:25:29 +0000346 BidiIterator* midpoints = smidpoints->data();
347 BidiIterator& endpoint = midpoints[sNumMidpoints-2];
348 const BidiIterator& startpoint = midpoints[sNumMidpoints-1];
349 BidiIterator currpoint = endpoint;
350 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
mitz@apple.com1a301772008-03-11 18:30:36 +0000351 currpoint.increment();
hyattfe99c872003-07-31 22:25:29 +0000352 if (currpoint == lBreak) {
353 // We hit the line break before the start point. Shave off the start point.
354 sNumMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000355 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000356 if (endpoint.obj->isText()) {
harrisona4d6cdc2006-10-02 15:32:17 +0000357 // Don't shave a character off the endpoint if it was from a soft hyphen.
hyattee1bcae2004-03-29 21:36:04 +0000358 RenderText* textObj = static_cast<RenderText*>(endpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000359 if (endpoint.pos + 1 < textObj->textLength()) {
360 if (textObj->characters()[endpoint.pos+1] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000361 return;
362 } else if (startpoint.obj->isText()) {
363 RenderText *startText = static_cast<RenderText*>(startpoint.obj);
darin42563ac52007-01-22 17:28:57 +0000364 if (startText->textLength() && startText->characters()[0] == softHyphen)
bdakin9151ba52005-10-24 22:51:06 +0000365 return;
366 }
hyattee1bcae2004-03-29 21:36:04 +0000367 }
hyattfe99c872003-07-31 22:25:29 +0000368 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000369 }
hyattfe99c872003-07-31 22:25:29 +0000370 }
371 }
372}
373
hyatt85586af2003-02-19 23:22:42 +0000374static void addMidpoint(const BidiIterator& midpoint)
375{
hyatt85586af2003-02-19 23:22:42 +0000376 if (smidpoints->size() <= sNumMidpoints)
darin@apple.com83833152008-01-14 17:51:10 +0000377 smidpoints->grow(sNumMidpoints + 10);
hyatt85586af2003-02-19 23:22:42 +0000378
379 BidiIterator* midpoints = smidpoints->data();
380 midpoints[sNumMidpoints++] = midpoint;
381}
382
pewtermoosecf72e2d2007-07-20 19:01:55 +0000383static void appendRunsForObject(int start, int end, RenderObject* obj, BidiState& bidi)
hyatt33f8d492002-11-12 21:44:52 +0000384{
hyatt98ee7e42003-05-14 01:39:15 +0000385 if (start > end || obj->isFloating() ||
hyatt853cd7d2004-11-05 02:59:48 +0000386 (obj->isPositioned() && !obj->hasStaticX() && !obj->hasStaticY() && !obj->container()->isInlineFlow()))
hyatteb003b82002-11-15 22:35:10 +0000387 return;
hyatt85586af2003-02-19 23:22:42 +0000388
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000389 bool haveNextMidpoint = (sCurrMidpoint < sNumMidpoints);
mjsfe301d72003-10-02 18:46:18 +0000390 BidiIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000391 if (haveNextMidpoint)
392 nextMidpoint = smidpoints->at(sCurrMidpoint);
hyatt33f8d492002-11-12 21:44:52 +0000393 if (betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000394 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000395 return;
396 // This is a new start point. Stop ignoring objects and
397 // adjust our start.
398 betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000399 start = nextMidpoint.pos;
400 sCurrMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000401 if (start < end)
mjsfe301d72003-10-02 18:46:18 +0000402 return appendRunsForObject(start, end, obj, bidi);
hyatt33f8d492002-11-12 21:44:52 +0000403 }
404 else {
mitz@apple.com5c2e0e02008-03-13 00:25:55 +0000405 if (!haveNextMidpoint || (obj != nextMidpoint.obj)) {
pewtermoosecf72e2d2007-07-20 19:01:55 +0000406 bidi.addRun(new (obj->renderArena()) BidiRun(start, end, obj, bidi.context(), bidi.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000407 return;
408 }
409
hyatt78b85132004-03-29 20:07:45 +0000410 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000411 // need to go ahead and append a run with our endpoint.
hyatt85586af2003-02-19 23:22:42 +0000412 if (int(nextMidpoint.pos+1) <= end) {
hyatt26179432002-11-17 01:57:27 +0000413 betweenMidpoints = true;
hyatt85586af2003-02-19 23:22:42 +0000414 sCurrMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000415 if (nextMidpoint.pos != UINT_MAX) { // UINT_MAX means stop at the object and don't include any of it.
darinef0c09f2005-10-09 01:46:17 +0000416 if (int(nextMidpoint.pos+1) > start)
pewtermoosecf72e2d2007-07-20 19:01:55 +0000417 bidi.addRun(new (obj->renderArena())
418 BidiRun(start, nextMidpoint.pos+1, obj, bidi.context(), bidi.dir()));
mjsfe301d72003-10-02 18:46:18 +0000419 return appendRunsForObject(nextMidpoint.pos+1, end, obj, bidi);
hyattc64f9fc2003-03-14 01:25:59 +0000420 }
hyatt26179432002-11-17 01:57:27 +0000421 }
422 else
pewtermoosecf72e2d2007-07-20 19:01:55 +0000423 bidi.addRun(new (obj->renderArena()) BidiRun(start, end, obj, bidi.context(), bidi.dir()));
hyatt33f8d492002-11-12 21:44:52 +0000424 }
425}
426
pewtermoosecf72e2d2007-07-20 19:01:55 +0000427template <>
428void BidiState::appendRun()
kociendabb0c24b2001-08-24 14:24:40 +0000429{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000430 if (emptyRun || eor.atEnd())
ggarenef026b22005-07-06 00:21:53 +0000431 return;
darinf028f812002-06-10 20:08:04 +0000432
pewtermoosecf72e2d2007-07-20 19:01:55 +0000433 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);
kociendabb0c24b2001-08-24 14:24:40 +0000437 start = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +0000438 obj = bidiNext(sor.block, obj);
kociendabb0c24b2001-08-24 14:24:40 +0000439 }
justing5b0e0422005-08-01 03:20:49 +0000440 if (obj) {
pewtermoosecf72e2d2007-07-20 19:01:55 +0000441 unsigned pos = obj == eor.obj ? eor.pos : UINT_MAX;
442 if (obj == endOfLine.obj && endOfLine.pos <= pos) {
443 reachedEndOfLine = true;
444 pos = endOfLine.pos;
eseidel789896f2005-11-27 22:52:09 +0000445 }
justing5b0e0422005-08-01 03:20:49 +0000446 // It's OK to add runs for zero-length RenderObjects, just don't make the run larger than it should be
eseidel789896f2005-11-27 22:52:09 +0000447 int end = obj->length() ? pos+1 : 0;
pewtermoosecf72e2d2007-07-20 19:01:55 +0000448 appendRunsForObject(start, end, obj, *this);
justing5b0e0422005-08-01 03:20:49 +0000449 }
hyatt33f8d492002-11-12 21:44:52 +0000450
mitz@apple.com1a301772008-03-11 18:30:36 +0000451 eor.increment();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000452 sor = eor;
453 m_direction = OtherNeutral;
454 m_status.eor = OtherNeutral;
kociendabb0c24b2001-08-24 14:24:40 +0000455}
456
hyattffe78712003-02-11 01:59:29 +0000457InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj)
458{
459 // See if we have an unconstructed line box for this object that is also
460 // the last item on the line.
hyatt1d5d87b2007-04-24 04:55:54 +0000461 unsigned lineDepth = 1;
462 InlineFlowBox* childBox = 0;
463 InlineFlowBox* parentBox = 0;
464 InlineFlowBox* result = 0;
465 do {
466 ASSERT(obj->isInlineFlow() || obj == this);
467 RenderFlow* flow = static_cast<RenderFlow*>(obj);
hyattffe78712003-02-11 01:59:29 +0000468
hyatt1d5d87b2007-04-24 04:55:54 +0000469 // Get the last box we made for this render object.
470 parentBox = flow->lastLineBox();
hyattffe78712003-02-11 01:59:29 +0000471
hyatt1d5d87b2007-04-24 04:55:54 +0000472 // If this box is constructed then it is from a previous line, and we need
473 // to make a new box for our line. If this box is unconstructed but it has
474 // something following it on the line, then we know we have to make a new box
475 // as well. In this situation our inline has actually been split in two on
476 // the same line (this can happen with very fancy language mixtures).
477 bool constructedNewBox = false;
478 if (!parentBox || parentBox->isConstructed() || parentBox->nextOnLine()) {
479 // We need to make a new box for this render object. Once
480 // made, we need to place it at the end of the current line.
481 InlineBox* newBox = obj->createInlineBox(false, obj == this);
482 ASSERT(newBox->isInlineFlowBox());
483 parentBox = static_cast<InlineFlowBox*>(newBox);
484 parentBox->setFirstLineStyleBit(m_firstLine);
485 constructedNewBox = true;
486 }
mitz@apple.come1364202008-02-28 01:06:41 +0000487
hyatt1d5d87b2007-04-24 04:55:54 +0000488 if (!result)
489 result = parentBox;
490
491 // If we have hit the block itself, then |box| represents the root
hyattffe78712003-02-11 01:59:29 +0000492 // inline box for the line, and it doesn't have to be appended to any parent
493 // inline.
hyatt1d5d87b2007-04-24 04:55:54 +0000494 if (childBox)
495 parentBox->addToLine(childBox);
mitz@apple.come1364202008-02-28 01:06:41 +0000496
hyatt1d5d87b2007-04-24 04:55:54 +0000497 if (!constructedNewBox || obj == this)
498 break;
mitz@apple.come1364202008-02-28 01:06:41 +0000499
hyatt1d5d87b2007-04-24 04:55:54 +0000500 childBox = parentBox;
mitz@apple.come1364202008-02-28 01:06:41 +0000501
hyatt1d5d87b2007-04-24 04:55:54 +0000502 // If we've exceeded our line depth, then jump straight to the root and skip all the remaining
503 // intermediate inline flows.
504 obj = (++lineDepth >= cMaxLineDepth) ? this : obj->parent();
hyattffe78712003-02-11 01:59:29 +0000505
hyatt1d5d87b2007-04-24 04:55:54 +0000506 } while (true);
507
508 return result;
hyattffe78712003-02-11 01:59:29 +0000509}
510
mitz@apple.com887f3592008-02-25 22:03:08 +0000511RootInlineBox* RenderBlock::constructLine(unsigned runCount, BidiRun* firstRun, BidiRun* lastRun, bool lastLine, RenderObject* endObject)
hyattffe78712003-02-11 01:59:29 +0000512{
mitz@apple.com887f3592008-02-25 22:03:08 +0000513 ASSERT(firstRun);
hyattffe78712003-02-11 01:59:29 +0000514
515 InlineFlowBox* parentBox = 0;
mitz@apple.com887f3592008-02-25 22:03:08 +0000516 for (BidiRun* r = firstRun; r; r = r->next()) {
hyattffe78712003-02-11 01:59:29 +0000517 // Create a box for our object.
mitz@apple.com887f3592008-02-25 22:03:08 +0000518 bool isOnlyRun = (runCount == 1);
mitz@apple.come1364202008-02-28 01:06:41 +0000519 if (runCount == 2 && !r->m_object->isListMarker())
520 isOnlyRun = ((style()->direction() == RTL) ? lastRun : firstRun)->m_object->isListMarker();
521 r->m_box = r->m_object->createInlineBox(r->m_object->isPositioned(), false, isOnlyRun);
522
523 if (r->m_box) {
hyatt0c3a9862004-02-23 21:26:26 +0000524 // If we have no parent box yet, or if the run is not simply a sibling,
525 // then we need to construct inline boxes as necessary to properly enclose the
526 // run's inline box.
mitz@apple.come1364202008-02-28 01:06:41 +0000527 if (!parentBox || parentBox->object() != r->m_object->parent())
hyatt0c3a9862004-02-23 21:26:26 +0000528 // Create new inline boxes all the way back to the appropriate insertion point.
mitz@apple.come1364202008-02-28 01:06:41 +0000529 parentBox = createLineBoxes(r->m_object->parent());
hyattffe78712003-02-11 01:59:29 +0000530
hyatt0c3a9862004-02-23 21:26:26 +0000531 // Append the inline box to this line.
mitz@apple.come1364202008-02-28 01:06:41 +0000532 parentBox->addToLine(r->m_box);
darin06dcb9c2005-08-15 04:31:09 +0000533
mitz@apple.come1364202008-02-28 01:06:41 +0000534 if (r->m_box->isInlineTextBox()) {
535 InlineTextBox* text = static_cast<InlineTextBox*>(r->m_box);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000536 text->setStart(r->m_start);
537 text->setLen(r->m_stop - r->m_start);
mitz@apple.come1364202008-02-28 01:06:41 +0000538 bool visuallyOrdered = r->m_object->style()->visuallyOrdered();
adele68afa0b2007-02-05 23:25:44 +0000539 text->m_reversed = r->reversed(visuallyOrdered);
540 text->m_dirOverride = r->dirOverride(visuallyOrdered);
darin06dcb9c2005-08-15 04:31:09 +0000541 }
hyatt0c3a9862004-02-23 21:26:26 +0000542 }
hyattffe78712003-02-11 01:59:29 +0000543 }
544
545 // We should have a root inline box. It should be unconstructed and
546 // be the last continuation of our line list.
ggarenf9f32ae2007-03-26 20:08:53 +0000547 ASSERT(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000548
549 // Set bits on our inline flow boxes that indicate which sides should
550 // paint borders/margins/padding. This knowledge will ultimately be used when
551 // we determine the horizontal positions and widths of all the inline boxes on
552 // the line.
hyattffe78712003-02-11 01:59:29 +0000553 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
554
555 // Now mark the line boxes as being constructed.
556 lastLineBox()->setConstructed();
557
558 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000559 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000560}
561
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000562void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun, BidiRun* trailingSpaceRun, bool reachedEnd)
hyattffe78712003-02-11 01:59:29 +0000563{
564 // First determine our total width.
kociendae40cb942004-10-05 20:05:38 +0000565 int availableWidth = lineWidth(m_height);
hyattffe78712003-02-11 01:59:29 +0000566 int totWidth = lineBox->getFlowSpacingWidth();
darin06dcb9c2005-08-15 04:31:09 +0000567 bool needsWordSpacing = false;
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000568 unsigned numSpaces = 0;
569 ETextAlign textAlign = style()->textAlign();
570
mitz@apple.come1364202008-02-28 01:06:41 +0000571 for (BidiRun* r = firstRun; r; r = r->next()) {
572 if (!r->m_box || r->m_object->isPositioned() || r->m_box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000573 continue; // Positioned objects are only participating to figure out their
574 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000575 // Similarly, line break boxes have no effect on the width.
mitz@apple.come1364202008-02-28 01:06:41 +0000576 if (r->m_object->isText()) {
577 RenderText* rt = static_cast<RenderText*>(r->m_object);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000578
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000579 if (textAlign == JUSTIFY && r != trailingSpaceRun) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000580 const UChar* characters = rt->characters();
581 for (int i = r->m_start; i < r->m_stop; i++) {
582 UChar c = characters[i];
583 if (c == ' ' || c == '\n' || c == '\t')
584 numSpaces++;
585 }
586 }
587
mitz@apple.come1364202008-02-28 01:06:41 +0000588 if (int length = rt->textLength()) {
589 if (!r->m_compact && !r->m_start && needsWordSpacing && isSpaceOrNewline(rt->characters()[r->m_start]))
mitzf297b072007-10-21 07:10:55 +0000590 totWidth += rt->style(m_firstLine)->font().wordSpacing();
mitz@apple.come1364202008-02-28 01:06:41 +0000591 needsWordSpacing = !isSpaceOrNewline(rt->characters()[r->m_stop - 1]) && r->m_stop == length;
darin06dcb9c2005-08-15 04:31:09 +0000592 }
mitz@apple.come1364202008-02-28 01:06:41 +0000593 r->m_box->setWidth(rt->width(r->m_start, r->m_stop - r->m_start, totWidth, m_firstLine));
594 } else if (!r->m_object->isInlineFlow()) {
595 r->m_object->calcWidth();
596 r->m_box->setWidth(r->m_object->width());
597 if (!r->m_compact)
598 totWidth += r->m_object->marginLeft() + r->m_object->marginRight();
hyattffe78712003-02-11 01:59:29 +0000599 }
hyatt4b381692003-03-10 21:11:59 +0000600
601 // Compacts don't contribute to the width of the line, since they are placed in the margin.
mitz@apple.come1364202008-02-28 01:06:41 +0000602 if (!r->m_compact)
603 totWidth += r->m_box->width();
hyattffe78712003-02-11 01:59:29 +0000604 }
605
606 // Armed with the total width of the line (without justification),
607 // we now examine our text-align property in order to determine where to position the
608 // objects horizontally. The total width of the line can be increased if we end up
609 // justifying text.
610 int x = leftOffset(m_height);
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000611 switch(textAlign) {
hyattffe78712003-02-11 01:59:29 +0000612 case LEFT:
ddkilzer95d2be02007-04-14 01:26:07 +0000613 case WEBKIT_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000614 // The direction of the block should determine what happens with wide lines. In
615 // particular with RTL blocks, wide lines should still spill out to the left.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000616 if (style()->direction() == LTR) {
617 if (totWidth > availableWidth && trailingSpaceRun)
618 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
619 } else {
620 if (trailingSpaceRun)
621 trailingSpaceRun->m_box->setWidth(0);
622 else if (totWidth > availableWidth)
623 x -= (totWidth - availableWidth);
624 }
hyattffe78712003-02-11 01:59:29 +0000625 break;
626 case JUSTIFY:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000627 if (numSpaces && !reachedEnd && !lineBox->endsWithBreak()) {
628 if (trailingSpaceRun) {
629 totWidth -= trailingSpaceRun->m_box->width();
630 trailingSpaceRun->m_box->setWidth(0);
631 }
hyattffe78712003-02-11 01:59:29 +0000632 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000633 }
hyattffe78712003-02-11 01:59:29 +0000634 // fall through
635 case TAAUTO:
636 numSpaces = 0;
637 // for right to left fall through to right aligned
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000638 if (style()->direction() == LTR) {
639 if (totWidth > availableWidth && trailingSpaceRun)
640 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
hyattffe78712003-02-11 01:59:29 +0000641 break;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000642 }
hyattffe78712003-02-11 01:59:29 +0000643 case RIGHT:
ddkilzer95d2be02007-04-14 01:26:07 +0000644 case WEBKIT_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000645 // Wide lines spill out of the block based off direction.
646 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
647 // side of the block.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000648 if (style()->direction() == LTR) {
649 if (trailingSpaceRun) {
650 totWidth -= trailingSpaceRun->m_box->width();
651 trailingSpaceRun->m_box->setWidth(0);
652 }
653 if (totWidth < availableWidth)
654 x += availableWidth - totWidth;
655 } else {
656 if (totWidth > availableWidth && trailingSpaceRun) {
657 trailingSpaceRun->m_box->setWidth(trailingSpaceRun->m_box->width() - totWidth + availableWidth);
658 totWidth -= trailingSpaceRun->m_box->width();
659 } else
660 x += availableWidth - totWidth;
661 }
hyattffe78712003-02-11 01:59:29 +0000662 break;
663 case CENTER:
ddkilzer95d2be02007-04-14 01:26:07 +0000664 case WEBKIT_CENTER:
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000665 int trailingSpaceWidth = 0;
666 if (trailingSpaceRun) {
667 totWidth -= trailingSpaceRun->m_box->width();
668 trailingSpaceWidth = min(trailingSpaceRun->m_box->width(), (availableWidth - totWidth + 1) / 2);
669 trailingSpaceRun->m_box->setWidth(trailingSpaceWidth);
670 }
mitz@apple.com37717da2008-02-28 02:23:22 +0000671 if (style()->direction() == LTR)
672 x += max((availableWidth - totWidth) / 2, 0);
673 else
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000674 x += totWidth > availableWidth ? (availableWidth - totWidth) : (availableWidth - totWidth) / 2 - trailingSpaceWidth;
hyattffe78712003-02-11 01:59:29 +0000675 break;
676 }
677
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000678 if (numSpaces) {
mitz@apple.come1364202008-02-28 01:06:41 +0000679 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000680 if (!r->m_box || r == trailingSpaceRun)
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000681 continue;
hyatt0c3a9862004-02-23 21:26:26 +0000682
hyattacbb0d42003-04-25 01:32:49 +0000683 int spaceAdd = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000684 if (r->m_object->isText() && !r->m_compact) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000685 unsigned spaces = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000686 const UChar* characters = static_cast<RenderText*>(r->m_object)->characters();
pewtermoosecf72e2d2007-07-20 19:01:55 +0000687 for (int i = r->m_start; i < r->m_stop; i++) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000688 UChar c = characters[i];
harrison208ea792005-07-29 23:42:59 +0000689 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000690 spaces++;
darina3c48282003-10-07 15:49:30 +0000691 }
hyatt870bdda2003-05-21 23:37:33 +0000692
darinb53ebdc2006-07-09 15:10:21 +0000693 ASSERT(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000694
hyattdca76e92005-11-02 08:52:50 +0000695 // Only justify text if whitespace is collapsed.
mitz@apple.come1364202008-02-28 01:06:41 +0000696 if (r->m_object->style()->collapseWhiteSpace()) {
mitz@apple.com815ef2f2008-02-25 17:11:56 +0000697 spaceAdd = (availableWidth - totWidth) * spaces / numSpaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000698 static_cast<InlineTextBox*>(r->m_box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000699 totWidth += spaceAdd;
700 }
hyattacbb0d42003-04-25 01:32:49 +0000701 numSpaces -= spaces;
mitz@apple.come1364202008-02-28 01:06:41 +0000702 if (!numSpaces)
703 break;
hyattacbb0d42003-04-25 01:32:49 +0000704 }
hyattffe78712003-02-11 01:59:29 +0000705 }
hyattffe78712003-02-11 01:59:29 +0000706 }
mitz@apple.come1364202008-02-28 01:06:41 +0000707
hyattffe78712003-02-11 01:59:29 +0000708 // The widths of all runs are now known. We can now place every inline box (and
709 // compute accurate widths for the inline flow boxes).
hyatt1f14a612004-12-07 00:34:02 +0000710 int leftPosition = x;
711 int rightPosition = x;
darin06dcb9c2005-08-15 04:31:09 +0000712 needsWordSpacing = false;
713 lineBox->placeBoxesHorizontally(x, leftPosition, rightPosition, needsWordSpacing);
hyatt1f14a612004-12-07 00:34:02 +0000714 lineBox->setHorizontalOverflowPositions(leftPosition, rightPosition);
hyattffe78712003-02-11 01:59:29 +0000715}
716
mitz@apple.com887f3592008-02-25 22:03:08 +0000717void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox, BidiRun* firstRun)
hyattffe78712003-02-11 01:59:29 +0000718{
719 lineBox->verticallyAlignBoxes(m_height);
hyatt0c3a9862004-02-23 21:26:26 +0000720 lineBox->setBlockHeight(m_height);
hyattffe78712003-02-11 01:59:29 +0000721
hyatt35a85e52003-02-14 19:43:07 +0000722 // See if the line spilled out. If so set overflow height accordingly.
723 int bottomOfLine = lineBox->bottomOverflow();
724 if (bottomOfLine > m_height && bottomOfLine > m_overflowHeight)
725 m_overflowHeight = bottomOfLine;
mitz@apple.come1364202008-02-28 01:06:41 +0000726
hyattffe78712003-02-11 01:59:29 +0000727 // Now make sure we place replaced render objects correctly.
mitz@apple.com887f3592008-02-25 22:03:08 +0000728 for (BidiRun* r = firstRun; r; r = r->next()) {
mitz@apple.come1364202008-02-28 01:06:41 +0000729 if (!r->m_box)
eseidel789896f2005-11-27 22:52:09 +0000730 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000731
hyatt98ee7e42003-05-14 01:39:15 +0000732 // Align positioned boxes with the top of the line box. This is
733 // a reasonable approximation of an appropriate y position.
mitz@apple.come1364202008-02-28 01:06:41 +0000734 if (r->m_object->isPositioned())
735 r->m_box->setYPos(m_height);
hyatt98ee7e42003-05-14 01:39:15 +0000736
737 // Position is used to properly position both replaced elements and
738 // to update the static normal flow x/y of positioned elements.
mitz@apple.come1364202008-02-28 01:06:41 +0000739 r->m_object->position(r->m_box);
hyatt98ee7e42003-05-14 01:39:15 +0000740 }
mitz@apple.coma927be62008-03-21 05:30:19 +0000741 // Positioned objects and zero-length text nodes destroy their boxes in
742 // position(), which unnecessarily dirties the line.
743 lineBox->markDirty(false);
hyattffe78712003-02-11 01:59:29 +0000744}
kociendabb0c24b2001-08-24 14:24:40 +0000745
746// collects one line of the paragraph and transforms it to visual order
mitz@apple.com1a301772008-03-11 18:30:36 +0000747void RenderBlock::bidiReorderLine(BidiState& resolver, const BidiIterator& end)
kociendabb0c24b2001-08-24 14:24:40 +0000748{
mitz@apple.com1a301772008-03-11 18:30:36 +0000749 resolver.createBidiRunsForLine(end, style()->visuallyOrdered(), previousLineBrokeCleanly);
hyatt4b381692003-03-10 21:11:59 +0000750}
kociendabb0c24b2001-08-24 14:24:40 +0000751
hyatt275d0702005-11-03 23:53:57 +0000752static void buildCompactRuns(RenderObject* compactObj, BidiState& bidi)
hyatt4b381692003-03-10 21:11:59 +0000753{
pewtermoosecf72e2d2007-07-20 19:01:55 +0000754 ASSERT(compactObj->isRenderBlock());
755 ASSERT(!bidi.firstRun());
hyatt275d0702005-11-03 23:53:57 +0000756
pewtermoosecf72e2d2007-07-20 19:01:55 +0000757 // Format the compact like it is its own single line. We build up all the runs for
758 // the little compact and then reorder them for bidi.
759 RenderBlock* compactBlock = static_cast<RenderBlock*>(compactObj);
mitz@apple.com1a301772008-03-11 18:30:36 +0000760
761 BidiIterator start(compactBlock, bidiFirst(compactBlock, &bidi), 0);
762 bidi.setPosition(start);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000763
764 betweenMidpoints = false;
765 isLineEmpty = true;
766 previousLineBrokeCleanly = true;
mitz@apple.come1364202008-02-28 01:06:41 +0000767
mitz@apple.com1a301772008-03-11 18:30:36 +0000768 BidiIterator end = compactBlock->findNextLineBreak(bidi);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000769 if (!isLineEmpty)
mitz@apple.com1a301772008-03-11 18:30:36 +0000770 compactBlock->bidiReorderLine(bidi, end);
pewtermoosecf72e2d2007-07-20 19:01:55 +0000771
772 for (BidiRun* run = bidi.firstRun(); run; run = run->next())
mitz@apple.come1364202008-02-28 01:06:41 +0000773 run->m_compact = true;
774
hyatt4b381692003-03-10 21:11:59 +0000775 sNumMidpoints = 0;
776 sCurrMidpoint = 0;
777 betweenMidpoints = false;
kociendabb0c24b2001-08-24 14:24:40 +0000778}
779
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000780static inline bool isCollapsibleSpace(UChar character, RenderText* renderer)
781{
782 if (character == ' ' || character == '\t' || character == softHyphen)
783 return true;
784 if (character == '\n')
785 return !renderer->style()->preserveNewline();
786 if (character == noBreakSpace)
787 return renderer->style()->nbspMode() == SPACE;
788 return false;
789}
790
apddd2ff42007-03-31 08:26:24 +0000791void RenderBlock::layoutInlineChildren(bool relayoutChildren, int& repaintTop, int& repaintBottom)
kociendabb0c24b2001-08-24 14:24:40 +0000792{
apddd2ff42007-03-31 08:26:24 +0000793 bool useRepaintBounds = false;
hyatt0c3a9862004-02-23 21:26:26 +0000794
hyatt7b41b9d2007-05-30 05:32:40 +0000795 invalidateVerticalPosition();
796
hyatta70560a2002-11-20 01:53:20 +0000797 m_overflowHeight = 0;
hyatt7b41b9d2007-05-30 05:32:40 +0000798
hyattdd7dfe92003-11-16 20:48:12 +0000799 m_height = borderTop() + paddingTop();
hyattf9f247b2007-01-12 22:53:40 +0000800 int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
mitz@apple.come1364202008-02-28 01:06:41 +0000801
hyatt0c3a9862004-02-23 21:26:26 +0000802 // Figure out if we should clear out our line boxes.
803 // FIXME: Handle resize eventually!
804 // FIXME: Do something better when floats are present.
mitz@apple.com40547b32008-03-18 04:04:34 +0000805 bool fullLayout = !firstLineBox() || !firstChild() || selfNeedsLayout() || relayoutChildren;
hyatt0c3a9862004-02-23 21:26:26 +0000806 if (fullLayout)
807 deleteLineBoxes();
mitz@apple.come1364202008-02-28 01:06:41 +0000808
hyatted77ad82004-06-15 07:21:23 +0000809 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
810 // clip.
811 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
812 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
813 // anyway, so we won't worry about following the draft here.
814 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
mitz@apple.come1364202008-02-28 01:06:41 +0000815
hyatted77ad82004-06-15 07:21:23 +0000816 // Walk all the lines and delete our ellipsis line boxes if they exist.
817 if (hasTextOverflow)
818 deleteEllipsisLineBoxes();
819
hyattffe78712003-02-11 01:59:29 +0000820 if (firstChild()) {
kociendabb0c24b2001-08-24 14:24:40 +0000821 // layout replaced elements
hyattffe78712003-02-11 01:59:29 +0000822 bool endOfInline = false;
mitz@apple.com1a301772008-03-11 18:30:36 +0000823 RenderObject* o = bidiFirst(this, 0, false);
mitz@apple.com40547b32008-03-18 04:04:34 +0000824 Vector<FloatWithRect> floats;
hyatt@apple.com21cc37a2008-02-26 23:17:03 +0000825 int containerWidth = max(0, containingBlockWidth());
hyatt0c3a9862004-02-23 21:26:26 +0000826 while (o) {
hyatt7b41b9d2007-05-30 05:32:40 +0000827 o->invalidateVerticalPosition();
hyatt0c3a9862004-02-23 21:26:26 +0000828 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
hyatt89377f12002-12-13 21:24:40 +0000829 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
hyattadbb2ef2003-11-18 09:21:44 +0000830 o->setChildNeedsLayout(true, false);
hyatt567533d2007-04-25 21:46:21 +0000831
832 // If relayoutChildren is set and we have percentage padding, we also need to invalidate the child's pref widths.
833 if (relayoutChildren && (o->style()->paddingLeft().isPercent() || o->style()->paddingRight().isPercent()))
834 o->setPrefWidthsDirty(true, false);
835
hyatt3ac01352003-03-22 01:37:33 +0000836 if (o->isPositioned())
837 o->containingBlock()->insertPositionedObject(o);
hyatt0c3a9862004-02-23 21:26:26 +0000838 else {
839 if (o->isFloating())
mitz@apple.com40547b32008-03-18 04:04:34 +0000840 floats.append(FloatWithRect(o));
hyatt0c3a9862004-02-23 21:26:26 +0000841 else if (fullLayout || o->needsLayout()) // Replaced elements
842 o->dirtyLineBoxes(fullLayout);
mitz@apple.com40547b32008-03-18 04:04:34 +0000843
hyatt390427a2003-05-03 18:33:42 +0000844 o->layoutIfNeeded();
hyatt0c3a9862004-02-23 21:26:26 +0000845 }
mitz@apple.com40547b32008-03-18 04:04:34 +0000846 } else if (o->isText() || (o->isInlineFlow() && !endOfInline)) {
hyatt0c3a9862004-02-23 21:26:26 +0000847 if (fullLayout || o->selfNeedsLayout())
848 o->dirtyLineBoxes(fullLayout);
hyatt@apple.com21cc37a2008-02-26 23:17:03 +0000849
850 // Calculate margins of inline flows so that they can be used later by line layout.
851 if (o->isInlineFlow())
852 static_cast<RenderFlow*>(o)->calcMargins(containerWidth);
hyattf4fe67f2003-10-07 04:43:23 +0000853 o->setNeedsLayout(false);
hyattf4fe67f2003-10-07 04:43:23 +0000854 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000855 o = bidiNext(this, o, 0, false, &endOfInline);
kociendabb0c24b2001-08-24 14:24:40 +0000856 }
857
mitz@apple.com40547b32008-03-18 04:04:34 +0000858 // We want to skip ahead to the first dirty line
859 BidiState start;
860 unsigned floatIndex;
861 RootInlineBox* startLine = determineStartPosition(fullLayout, start, floats, floatIndex);
mitz@apple.come1364202008-02-28 01:06:41 +0000862
hyatt837eb362004-05-21 22:17:10 +0000863 if (fullLayout && !selfNeedsLayout()) {
hyatt0c3a9862004-02-23 21:26:26 +0000864 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
865 // we're supposed to.
hyatt837eb362004-05-21 22:17:10 +0000866 if (!document()->view()->needsFullRepaint() && m_layer) {
867 // Because we waited until we were already inside layout to discover
868 // that the block really needed a full layout, we missed our chance to repaint the layer
869 // before layout started. Luckily the layer has cached the repaint rect for its original
870 // position and size, and so we can use that to make a repaint happen now.
hyattd8048342006-05-31 01:48:18 +0000871 RenderView* c = view();
sfalken6f98c4d2007-01-11 01:39:58 +0000872 if (c && !c->printing())
hyatt837eb362004-05-21 22:17:10 +0000873 c->repaintViewRectangle(m_layer->repaintRect());
874 }
875 }
hyatt0c3a9862004-02-23 21:26:26 +0000876
mitz@apple.com40547b32008-03-18 04:04:34 +0000877 FloatingObject* lastFloat = m_floatingObjects ? m_floatingObjects->last() : 0;
878
hyatt85586af2003-02-19 23:22:42 +0000879 if (!smidpoints)
darin91298e52006-06-12 01:10:17 +0000880 smidpoints = new Vector<BidiIterator>();
mitz@apple.come1364202008-02-28 01:06:41 +0000881
hyatt85586af2003-02-19 23:22:42 +0000882 sNumMidpoints = 0;
883 sCurrMidpoint = 0;
mitz@apple.come1364202008-02-28 01:06:41 +0000884
hyatt0c3a9862004-02-23 21:26:26 +0000885 // We also find the first clean line and extract these lines. We will add them back
886 // if we determine that we're able to synchronize after handling all our dirty lines.
887 BidiIterator cleanLineStart;
eseidel789896f2005-11-27 22:52:09 +0000888 BidiStatus cleanLineBidiStatus;
aroben201afd12007-07-01 04:53:01 +0000889 int endLineYPos = 0;
hyatt0c3a9862004-02-23 21:26:26 +0000890 RootInlineBox* endLine = (fullLayout || !startLine) ?
pewtermoosecf72e2d2007-07-20 19:01:55 +0000891 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, endLineYPos);
892
hyatt0c3a9862004-02-23 21:26:26 +0000893 if (startLine) {
apddd2ff42007-03-31 08:26:24 +0000894 useRepaintBounds = true;
ap9059f6f2006-07-24 16:55:02 +0000895 repaintTop = m_height;
896 repaintBottom = m_height;
hyatt0c3a9862004-02-23 21:26:26 +0000897 RenderArena* arena = renderArena();
898 RootInlineBox* box = startLine;
899 while (box) {
ap9059f6f2006-07-24 16:55:02 +0000900 repaintTop = min(repaintTop, box->topOverflow());
901 repaintBottom = max(repaintBottom, box->bottomOverflow());
hyatt0c3a9862004-02-23 21:26:26 +0000902 RootInlineBox* next = box->nextRootBox();
903 box->deleteLine(arena);
904 box = next;
905 }
hyatt0c3a9862004-02-23 21:26:26 +0000906 }
mitz@apple.come1364202008-02-28 01:06:41 +0000907
mitz@apple.com1a301772008-03-11 18:30:36 +0000908 BidiIterator end = start.position();
hyatt0c3a9862004-02-23 21:26:26 +0000909
mitz@apple.come3602dd2008-04-08 20:58:36 +0000910 if (!fullLayout && lastRootBox() && lastRootBox()->endsWithBreak()) {
911 // If the last line before the start line ends with a line break that clear floats,
912 // adjust the height accordingly.
913 // 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 +0000914 if (InlineBox* lastLeafChild = lastRootBox()->lastLeafChild()) {
915 RenderObject* lastObject = lastLeafChild->object();
916 if (!lastObject->isBR())
917 lastObject = lastRootBox()->firstLeafChild()->object();
918 if (lastObject->isBR()) {
919 EClear clear = lastObject->style()->clear();
920 if (clear != CNONE)
921 newLine(clear);
922 }
mitz@apple.come3602dd2008-04-08 20:58:36 +0000923 }
924 }
mitz@apple.com40547b32008-03-18 04:04:34 +0000925
hyatt0c3a9862004-02-23 21:26:26 +0000926 bool endLineMatched = false;
mitz@apple.comeb9c42c2008-03-26 20:53:07 +0000927 bool checkForEndLineMatch = endLine;
mitz@apple.com40547b32008-03-18 04:04:34 +0000928
hyatt0c3a9862004-02-23 21:26:26 +0000929 while (!end.atEnd()) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000930 // FIXME: Is this check necessary before the first iteration or can it be moved to the end?
mitz@apple.comeb9c42c2008-03-26 20:53:07 +0000931 if (checkForEndLineMatch && (endLineMatched = matchedEndLine(start, cleanLineStart, cleanLineBidiStatus, endLine, endLineYPos, repaintBottom, repaintTop)))
hyatt0c3a9862004-02-23 21:26:26 +0000932 break;
933
hyatt33f8d492002-11-12 21:44:52 +0000934 betweenMidpoints = false;
935 isLineEmpty = true;
mitz@apple.com1a301772008-03-11 18:30:36 +0000936 if (m_firstLine && firstChild()->isCompact() && firstChild()->isRenderBlock()) {
937 buildCompactRuns(firstChild(), start);
938 start.setPosition(BidiIterator(this, firstChild()->nextSibling(), 0));
hyatt4b381692003-03-10 21:11:59 +0000939 }
mitz@apple.com71e30842008-03-18 16:13:31 +0000940 EClear clear = CNONE;
941 end = findNextLineBreak(start, &clear);
mitz@apple.com1a301772008-03-11 18:30:36 +0000942 if (start.position().atEnd()) {
943 start.deleteRuns();
eseidel789896f2005-11-27 22:52:09 +0000944 break;
ggarenec11e5b2007-02-25 02:14:54 +0000945 }
mitz@apple.com1a301772008-03-11 18:30:36 +0000946 ASSERT(end != start.position());
947
hyatt33f8d492002-11-12 21:44:52 +0000948 if (!isLineEmpty) {
mitz@apple.com1a301772008-03-11 18:30:36 +0000949 bidiReorderLine(start, end);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000950 ASSERT(start.position() == end);
951
952 BidiRun* trailingSpaceRun = 0;
953 if (!previousLineBrokeCleanly && start.runCount() && start.logicallyLastRun()->m_object->style()->breakOnlyAfterWhiteSpace()) {
954 trailingSpaceRun = start.logicallyLastRun();
955 RenderObject* lastObject = trailingSpaceRun->m_object;
956 if (lastObject->isText()) {
957 RenderText* lastText = static_cast<RenderText*>(lastObject);
958 const UChar* characters = lastText->characters();
959 int firstSpace = trailingSpaceRun->stop();
960 while (firstSpace > trailingSpaceRun->start()) {
961 UChar current = characters[firstSpace - 1];
962 if (!isCollapsibleSpace(current, lastText))
963 break;
964 firstSpace--;
965 }
966 if (firstSpace == trailingSpaceRun->stop())
967 trailingSpaceRun = 0;
mitz@apple.com3a43c712008-04-20 04:26:28 +0000968 else {
969 TextDirection direction = style()->direction();
970 bool shouldReorder = trailingSpaceRun != (direction == LTR ? start.lastRun() : start.firstRun());
971 if (firstSpace != trailingSpaceRun->start()) {
972 ETextAlign textAlign = style()->textAlign();
973 // If the trailing white space is at the right hand side of a left-aligned line, then computeHorizontalPositionsForLine()
974 // does not care if trailingSpaceRun includes non-spaces at the beginning. In all other cases, trailingSpaceRun has to
975 // contain only the spaces, either because we re-order them or because computeHorizontalPositionsForLine() needs to know
976 // their width.
977 bool shouldSeparateSpaces = textAlign != LEFT && textAlign != WEBKIT_LEFT && textAlign != TAAUTO || trailingSpaceRun->m_level % 2 || direction == RTL || shouldReorder;
978 if (shouldSeparateSpaces) {
979 BidiContext* baseContext = start.context();
980 while (BidiContext* parent = baseContext->parent())
981 baseContext = parent;
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000982
mitz@apple.com3a43c712008-04-20 04:26:28 +0000983 BidiRun* newTrailingRun = new (renderArena()) BidiRun(firstSpace, trailingSpaceRun->m_stop, trailingSpaceRun->m_object, baseContext, OtherNeutral);
984 trailingSpaceRun->m_stop = firstSpace;
985 if (direction == LTR)
986 start.addRun(newTrailingRun);
987 else
988 start.prependRun(newTrailingRun);
989 trailingSpaceRun = newTrailingRun;
990 shouldReorder = false;
991 }
992 }
993 if (shouldReorder) {
994 if (direction == LTR)
995 start.moveRunToEnd(trailingSpaceRun);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000996 else
mitz@apple.com3a43c712008-04-20 04:26:28 +0000997 start.moveRunToBeginning(trailingSpaceRun);
mitz@apple.comc13ea5f2008-04-18 21:18:26 +0000998 }
999 }
1000 } else
1001 trailingSpaceRun = 0;
1002 }
hyatt4b381692003-03-10 21:11:59 +00001003
1004 // Now that the runs have been ordered, we create the line boxes.
1005 // At the same time we figure out where border/padding/margin should be applied for
1006 // inline flow boxes.
hyatt4b381692003-03-10 21:11:59 +00001007
hyatt0c3a9862004-02-23 21:26:26 +00001008 RootInlineBox* lineBox = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001009 if (start.runCount()) {
1010 lineBox = constructLine(start.runCount(), start.firstRun(), start.lastRun(), !end.obj, end.obj && !end.pos ? end.obj : 0);
hyattdfb09052003-03-11 02:58:59 +00001011 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +00001012 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
pewtermoosecf72e2d2007-07-20 19:01:55 +00001013
hyattdfb09052003-03-11 02:58:59 +00001014 // Now we position all of our text runs horizontally.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001015 computeHorizontalPositionsForLine(lineBox, start.firstRun(), trailingSpaceRun, end.atEnd());
mitz@apple.come1364202008-02-28 01:06:41 +00001016
hyattdfb09052003-03-11 02:58:59 +00001017 // Now position our text runs vertically.
mitz@apple.com1a301772008-03-11 18:30:36 +00001018 computeVerticalPositionsForLine(lineBox, start.firstRun());
hyattbddcc612006-07-24 23:16:15 +00001019
oliver98d52382007-10-12 10:47:18 +00001020#if ENABLE(SVG)
1021 // Special SVG text layout code
1022 lineBox->computePerCharacterLayoutInformation();
1023#endif
1024
hyattbddcc612006-07-24 23:16:15 +00001025#if PLATFORM(MAC)
1026 // Highlight acts as an overflow inflation.
1027 if (style()->highlight() != nullAtom)
1028 lineBox->addHighlightOverflow();
1029#endif
hyattdfb09052003-03-11 02:58:59 +00001030 }
1031 }
ggarenec11e5b2007-02-25 02:14:54 +00001032
mitz@apple.com1a301772008-03-11 18:30:36 +00001033 start.deleteRuns();
hyatt35a85e52003-02-14 19:43:07 +00001034
ap9059f6f2006-07-24 16:55:02 +00001035 if (lineBox) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001036 lineBox->setLineBreakInfo(end.obj, end.pos, start.status());
apddd2ff42007-03-31 08:26:24 +00001037 if (useRepaintBounds) {
ap9059f6f2006-07-24 16:55:02 +00001038 repaintTop = min(repaintTop, lineBox->topOverflow());
1039 repaintBottom = max(repaintBottom, lineBox->bottomOverflow());
1040 }
1041 }
mitz@apple.come1364202008-02-28 01:06:41 +00001042
hyatt35a85e52003-02-14 19:43:07 +00001043 m_firstLine = false;
mitz@apple.com71e30842008-03-18 16:13:31 +00001044 newLine(clear);
darinb70665a2002-04-15 23:43:21 +00001045 }
mitz@apple.come1364202008-02-28 01:06:41 +00001046
mitz@apple.com7b089842008-03-22 06:57:28 +00001047 if (m_floatingObjects && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001048 if (lastFloat) {
1049 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1050 }
1051 m_floatingObjects->next();
1052 } else
1053 m_floatingObjects->first();
1054 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001055 lastRootBox()->floats().append(f->m_renderer);
1056 ASSERT(f->m_renderer == floats[floatIndex].object);
mitz@apple.com40547b32008-03-18 04:04:34 +00001057 // If a float's geometry has changed, give up on syncing with clean lines.
mitz@apple.com93526592008-03-18 04:36:51 +00001058 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 +00001059 checkForEndLineMatch = false;
mitz@apple.com40547b32008-03-18 04:04:34 +00001060 floatIndex++;
1061 }
1062 lastFloat = m_floatingObjects->last();
1063 }
1064
hyatt85586af2003-02-19 23:22:42 +00001065 sNumMidpoints = 0;
1066 sCurrMidpoint = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001067 start.setPosition(end);
kociendabb0c24b2001-08-24 14:24:40 +00001068 }
mitz@apple.come1364202008-02-28 01:06:41 +00001069
hyatt0c3a9862004-02-23 21:26:26 +00001070 if (endLine) {
1071 if (endLineMatched) {
1072 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt0c3a9862004-02-23 21:26:26 +00001073 int delta = m_height - endLineYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001074 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1075 line->attachLine();
1076 if (delta) {
mitz@apple.come1364202008-02-28 01:06:41 +00001077 repaintTop = min(repaintTop, line->topOverflow() + min(delta, 0));
1078 repaintBottom = max(repaintBottom, line->bottomOverflow() + max(delta, 0));
hyattda77c4b2004-06-17 18:09:49 +00001079 line->adjustPosition(0, delta);
ap9059f6f2006-07-24 16:55:02 +00001080 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001081 if (Vector<RenderObject*>* cleanLineFloats = line->floatsPtr()) {
1082 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1083 for (Vector<RenderObject*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1084 int floatTop = (*f)->yPos() - (*f)->marginTop();
1085 insertFloatingObject(*f);
1086 m_height = floatTop + delta;
1087 positionNewFloats();
1088 }
1089 }
harrison208ea792005-07-29 23:42:59 +00001090 }
hyatt0c3a9862004-02-23 21:26:26 +00001091 m_height = lastRootBox()->blockHeight();
ap9059f6f2006-07-24 16:55:02 +00001092 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001093 // Delete all the remaining lines.
hyatt0c3a9862004-02-23 21:26:26 +00001094 InlineRunBox* line = endLine;
1095 RenderArena* arena = renderArena();
1096 while (line) {
ap9059f6f2006-07-24 16:55:02 +00001097 repaintTop = min(repaintTop, line->topOverflow());
1098 repaintBottom = max(repaintBottom, line->bottomOverflow());
hyatt0c3a9862004-02-23 21:26:26 +00001099 InlineRunBox* next = line->nextLineBox();
hyatt0c3a9862004-02-23 21:26:26 +00001100 line->deleteLine(arena);
1101 line = next;
1102 }
1103 }
1104 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001105 if (m_floatingObjects) {
1106 // In case we have a float on the last line, it might not be positioned up to now.
1107 // This has to be done before adding in the bottom border/padding, or the float will
1108 // include the padding incorrectly. -dwh
1109 if (positionNewFloats() && lastRootBox()) {
1110 if (lastFloat) {
1111 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1112 }
1113 m_floatingObjects->next();
1114 } else
1115 m_floatingObjects->first();
1116 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
mitz@apple.com93526592008-03-18 04:36:51 +00001117 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com40547b32008-03-18 04:04:34 +00001118 lastFloat = m_floatingObjects->last();
1119 }
1120 }
1121
kociendabb0c24b2001-08-24 14:24:40 +00001122 }
hyatt85586af2003-02-19 23:22:42 +00001123
1124 sNumMidpoints = 0;
1125 sCurrMidpoint = 0;
hyatta70560a2002-11-20 01:53:20 +00001126
1127 // Now add in the bottom border/padding.
kociendabb0c24b2001-08-24 14:24:40 +00001128 m_height += toAdd;
1129
hyatta70560a2002-11-20 01:53:20 +00001130 // Always make sure this is at least our height.
darin7bd70952006-04-13 07:07:34 +00001131 m_overflowHeight = max(m_height, m_overflowHeight);
mitz@apple.come1364202008-02-28 01:06:41 +00001132
hyattb4b20872004-10-20 21:34:01 +00001133 // See if any lines spill out of the block. If so, we need to update our overflow width.
1134 checkLinesForOverflow();
1135
adele7a470a72006-04-20 22:22:14 +00001136 if (!firstLineBox() && hasLineIfEmpty())
adele05abee32006-08-25 23:44:05 +00001137 m_height += lineHeight(true, true);
hyatted77ad82004-06-15 07:21:23 +00001138
1139 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1140 // truncate text.
1141 if (hasTextOverflow)
1142 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001143}
1144
mitz@apple.com40547b32008-03-18 04:04:34 +00001145RootInlineBox* RenderBlock::determineStartPosition(bool& fullLayout, BidiState& start, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +00001146{
1147 RootInlineBox* curr = 0;
1148 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001149
mitz@apple.com40547b32008-03-18 04:04:34 +00001150 bool dirtiedByFloat = false;
1151 if (!fullLayout) {
1152 size_t floatIndex = 0;
1153 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
1154 if (Vector<RenderObject*>* cleanLineFloats = curr->floatsPtr()) {
1155 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1156 for (Vector<RenderObject*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
1157 RenderObject* f = *o;
1158 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
1159 ASSERT(floatIndex < floats.size());
1160 if (floats[floatIndex].object != f) {
1161 // A new float has been inserted before this line or before its last known float.
1162 // Just do a full layout.
1163 fullLayout = true;
1164 break;
1165 }
1166 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +00001167 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +00001168 curr->markDirty();
1169 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()));
1170 floats[floatIndex].rect.setSize(newSize);
1171 dirtiedByFloat = true;
1172 }
1173 floatIndex++;
1174 }
1175 }
1176 if (dirtiedByFloat || fullLayout)
1177 break;
1178 }
1179 // Check if a new float has been inserted after the last known float.
1180 if (!curr && floatIndex < floats.size())
1181 fullLayout = true;
1182 }
1183
hyatt0c3a9862004-02-23 21:26:26 +00001184 if (fullLayout) {
1185 // Nuke all our lines.
1186 if (firstRootBox()) {
1187 RenderArena* arena = renderArena();
1188 curr = firstRootBox();
1189 while (curr) {
1190 RootInlineBox* next = curr->nextRootBox();
1191 curr->deleteLine(arena);
1192 curr = next;
1193 }
darinec375482007-01-06 01:36:24 +00001194 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001195 }
eseidel789896f2005-11-27 22:52:09 +00001196 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001197 if (curr) {
1198 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001199 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001200 // We have a previous line.
mitz@apple.com40547b32008-03-18 04:04:34 +00001201 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= static_cast<RenderText*>(prevRootBox->lineBreakObj())->textLength()))
mjs9f78dd92007-02-12 04:06:07 +00001202 // The previous line didn't break cleanly or broke at a newline
1203 // that has been deleted, so treat it as dirty too.
1204 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001205 }
eseidel789896f2005-11-27 22:52:09 +00001206 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001207 // No dirty lines were found.
1208 // If the last line didn't break cleanly, treat it as dirty.
1209 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1210 curr = lastRootBox();
1211 }
mitz@apple.come1364202008-02-28 01:06:41 +00001212
hyatt0c3a9862004-02-23 21:26:26 +00001213 // If we have no dirty lines, then last is just the last root box.
1214 last = curr ? curr->prevRootBox() : lastRootBox();
1215 }
mitz@apple.come1364202008-02-28 01:06:41 +00001216
mitz@apple.com40547b32008-03-18 04:04:34 +00001217 numCleanFloats = 0;
1218 if (!floats.isEmpty()) {
1219 int savedHeight = m_height;
1220 // Restore floats from clean lines.
1221 RootInlineBox* line = firstRootBox();
1222 while (line != curr) {
1223 if (Vector<RenderObject*>* cleanLineFloats = line->floatsPtr()) {
1224 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1225 for (Vector<RenderObject*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1226 insertFloatingObject(*f);
1227 m_height = (*f)->yPos() - (*f)->marginTop();
1228 positionNewFloats();
1229 ASSERT(floats[numCleanFloats].object == *f);
1230 numCleanFloats++;
1231 }
1232 }
1233 line = line->nextRootBox();
1234 }
1235 m_height = savedHeight;
1236 }
1237
hyatt0c3a9862004-02-23 21:26:26 +00001238 m_firstLine = !last;
1239 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001240
1241 RenderObject* startObj;
1242 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001243 if (last) {
1244 m_height = last->blockHeight();
hyatt0c3a9862004-02-23 21:26:26 +00001245 startObj = last->lineBreakObj();
1246 pos = last->lineBreakPos();
mitz@apple.com1a301772008-03-11 18:30:36 +00001247 start.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001248 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001249 bool ltr = style()->direction() == LTR
1250 #if ENABLE(SVG)
1251 || (style()->unicodeBidi() == UBNormal && isSVGText())
1252 #endif
1253 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001254
mitz@apple.com1a301772008-03-11 18:30:36 +00001255 BidiContext* context = new BidiContext(ltr ? 0 : 1, ltr ? LeftToRight : RightToLeft, style()->unicodeBidi() == Override);
1256
1257 start.setLastStrongDir(context->dir());
1258 start.setLastDir(context->dir());
1259 start.setEorDir(context->dir());
1260 start.setContext(context);
1261 startObj = bidiFirst(this, &start);
darindde01502005-12-18 22:55:35 +00001262 }
mitz@apple.come1364202008-02-28 01:06:41 +00001263
mitz@apple.com1a301772008-03-11 18:30:36 +00001264 start.setPosition(BidiIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001265
hyatt0c3a9862004-02-23 21:26:26 +00001266 return curr;
1267}
1268
pewtermoosecf72e2d2007-07-20 19:01:55 +00001269RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, BidiIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001270{
1271 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001272 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001273 last = 0;
1274 else {
hyatt04420ca2004-07-16 00:05:42 +00001275 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1276 if (curr->isDirty())
1277 last = 0;
1278 else if (!last)
1279 last = curr;
1280 }
hyatt0c3a9862004-02-23 21:26:26 +00001281 }
mitz@apple.come1364202008-02-28 01:06:41 +00001282
hyatt0c3a9862004-02-23 21:26:26 +00001283 if (!last)
1284 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001285
eseidel789896f2005-11-27 22:52:09 +00001286 RootInlineBox* prev = last->prevRootBox();
1287 cleanLineStart = BidiIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
1288 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001289 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001290
hyatt0c3a9862004-02-23 21:26:26 +00001291 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1292 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1293 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001294
hyatt0c3a9862004-02-23 21:26:26 +00001295 return last;
1296}
1297
mitz@apple.com1a301772008-03-11 18:30:36 +00001298bool RenderBlock::matchedEndLine(const BidiState& start, const BidiIterator& endLineStart, const BidiStatus& endLineStatus, RootInlineBox*& endLine, int& endYPos, int& repaintBottom, int& repaintTop)
hyatt0c3a9862004-02-23 21:26:26 +00001299{
mitz@apple.com40547b32008-03-18 04:04:34 +00001300 if (start.position() == endLineStart) {
1301 if (start.status() != endLineStatus)
1302 return false;
1303
1304 int delta = m_height - endYPos;
1305 if (!delta || !m_floatingObjects)
1306 return true;
1307
1308 // See if any floats end in the range along which we want to shift the lines vertically.
1309 int top = min(m_height, endYPos);
1310
1311 RootInlineBox* lastLine = endLine;
1312 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1313 lastLine = nextLine;
1314
1315 int bottom = lastLine->blockHeight() + abs(delta);
1316
1317 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001318 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001319 return false;
1320 }
1321
1322 return true;
1323 }
hyatt0c3a9862004-02-23 21:26:26 +00001324
mitz@apple.come1364202008-02-28 01:06:41 +00001325 // The first clean line doesn't match, but we can check a handful of following lines to try
1326 // to match back up.
1327 static int numLines = 8; // The # of lines we're willing to match against.
1328 RootInlineBox* line = endLine;
1329 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001330 if (line->lineBreakObj() == start.position().obj && line->lineBreakPos() == start.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001331 // We have a match.
mitz@apple.com1a301772008-03-11 18:30:36 +00001332 if (line->lineBreakBidiStatus() != start.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001333 return false; // ...but the bidi state doesn't match.
1334 RootInlineBox* result = line->nextRootBox();
1335
1336 // Set our yPos to be the block height of endLine.
1337 if (result)
1338 endYPos = line->blockHeight();
1339
mitz@apple.com40547b32008-03-18 04:04:34 +00001340 int delta = m_height - endYPos;
1341 if (delta && m_floatingObjects) {
1342 // See if any floats end in the range along which we want to shift the lines vertically.
1343 int top = min(m_height, endYPos);
1344
1345 RootInlineBox* lastLine = endLine;
1346 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1347 lastLine = nextLine;
1348
1349 int bottom = lastLine->blockHeight() + abs(delta);
1350
1351 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001352 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001353 return false;
1354 }
1355 }
1356
mitz@apple.come1364202008-02-28 01:06:41 +00001357 // Now delete the lines that we failed to sync.
1358 RootInlineBox* boxToDelete = endLine;
1359 RenderArena* arena = renderArena();
1360 while (boxToDelete && boxToDelete != result) {
1361 repaintTop = min(repaintTop, boxToDelete->topOverflow());
1362 repaintBottom = max(repaintBottom, boxToDelete->bottomOverflow());
1363 RootInlineBox* next = boxToDelete->nextRootBox();
1364 boxToDelete->deleteLine(arena);
1365 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001366 }
mitz@apple.come1364202008-02-28 01:06:41 +00001367
1368 endLine = result;
1369 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001370 }
1371 }
mitz@apple.come1364202008-02-28 01:06:41 +00001372
hyatt0c3a9862004-02-23 21:26:26 +00001373 return false;
1374}
1375
mitz@apple.com1a301772008-03-11 18:30:36 +00001376static inline bool skipNonBreakingSpace(const BidiIterator& it)
kocienda98440082004-10-14 23:51:47 +00001377{
darinf9e5d6c2007-01-09 14:54:26 +00001378 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001379 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001380
hyattdca76e92005-11-02 08:52:50 +00001381 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1382 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001383 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001384 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1385 // |true|).
1386 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001387 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001388
kocienda498d1982004-10-15 21:07:24 +00001389 return true;
kocienda98440082004-10-14 23:51:47 +00001390}
1391
hyattd9953212005-11-03 21:05:59 +00001392static inline bool shouldCollapseWhiteSpace(const RenderStyle* style)
1393{
1394 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1395}
1396
zimmermannac3781f2007-02-04 01:25:03 +00001397static inline bool shouldPreserveNewline(RenderObject* object)
1398{
mjsd2948ef2007-02-26 19:29:04 +00001399#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001400 if (object->isSVGText())
1401 return false;
1402#endif
1403
1404 return object->style()->preserveNewline();
1405}
1406
bdakinf876bee2007-10-30 05:27:09 +00001407static bool inlineFlowRequiresLineBox(RenderObject* flow)
1408{
1409 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001410 // We need to fix this, though, because at the very least, inlines containing only
1411 // ignorable whitespace should should also have line boxes.
hyatt@apple.com21cc37a2008-02-26 23:17:03 +00001412 return flow->isInlineFlow() && !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001413}
1414
mitz@apple.com1a301772008-03-11 18:30:36 +00001415static inline bool requiresLineBox(const BidiIterator& it)
bdashccffb432007-07-13 11:51:40 +00001416{
bdakinf876bee2007-10-30 05:27:09 +00001417 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001418 return false;
bdakinf876bee2007-10-30 05:27:09 +00001419
1420 if (it.obj->isInlineFlow() && !inlineFlowRequiresLineBox(it.obj))
1421 return false;
1422
bdashccffb432007-07-13 11:51:40 +00001423 if (!shouldCollapseWhiteSpace(it.obj->style()) || it.obj->isBR())
1424 return true;
1425
1426 UChar current = it.current();
1427 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj)) && !skipNonBreakingSpace(it);
1428}
1429
1430bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
1431{
1432 ASSERT(inlineObj->parent() == this);
1433
1434 BidiIterator it(this, inlineObj, 0);
bdashccffb432007-07-13 11:51:40 +00001435 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001436 it.increment();
bdashccffb432007-07-13 11:51:40 +00001437
1438 return !it.atEnd();
1439}
1440
mitz@apple.com1a301772008-03-11 18:30:36 +00001441// FIXME: The entire concept of the skipWhitespace function is flawed, since we really need to be building
1442// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1443// elements quite right. In other words, we need to build this function's work into the normal line
1444// object iteration process.
1445int RenderBlock::skipWhitespace(BidiIterator& iterator)
kociendabb0c24b2001-08-24 14:24:40 +00001446{
mitz@apple.com1a301772008-03-11 18:30:36 +00001447 int availableWidth = lineWidth(m_height);
1448 while (!iterator.atEnd() && !requiresLineBox(iterator)) {
1449 RenderObject* object = iterator.obj;
1450 if (object->isFloating()) {
1451 insertFloatingObject(object);
mitz@apple.come1364202008-02-28 01:06:41 +00001452 positionNewFloats();
mitz@apple.com1a301772008-03-11 18:30:36 +00001453 availableWidth = lineWidth(m_height);
1454 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001455 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1456 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001457 RenderObject* c = object->container();
mitz@apple.come1364202008-02-28 01:06:41 +00001458 if (c->isInlineFlow()) {
1459 // A relative positioned inline encloses us. In this case, we also have to determine our
1460 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1461 // inline so that we can obtain the value later.
1462 c->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : rightOffset(m_height));
1463 c->setStaticY(m_height);
hyatt33f8d492002-11-12 21:44:52 +00001464 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001465
1466 if (object->hasStaticX()) {
1467 if (object->style()->isOriginalDisplayInlineType())
1468 object->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : width() - rightOffset(m_height));
mitz@apple.come1364202008-02-28 01:06:41 +00001469 else
mitz@apple.com1a301772008-03-11 18:30:36 +00001470 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001471 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001472
1473 if (object->hasStaticY())
1474 object->setStaticY(m_height);
hyatt33f8d492002-11-12 21:44:52 +00001475 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001476 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001477 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001478 return availableWidth;
1479}
bdashccffb432007-07-13 11:51:40 +00001480
mitz@apple.com1a301772008-03-11 18:30:36 +00001481int RenderBlock::skipWhitespace(BidiState& iterator)
1482{
1483 int availableWidth = lineWidth(m_height);
1484 while (!iterator.position().atEnd() && !requiresLineBox(iterator.position())) {
1485 RenderObject* object = iterator.position().obj;
1486 if (object->isFloating()) {
1487 insertFloatingObject(object);
1488 positionNewFloats();
1489 availableWidth = lineWidth(m_height);
1490 } else if (object->isPositioned()) {
1491 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1492 // will work for the common cases
1493 RenderObject* c = object->container();
1494 if (c->isInlineFlow()) {
1495 // A relative positioned inline encloses us. In this case, we also have to determine our
1496 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1497 // inline so that we can obtain the value later.
1498 c->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : rightOffset(m_height));
1499 c->setStaticY(m_height);
1500 }
1501
1502 if (object->hasStaticX()) {
1503 if (object->style()->isOriginalDisplayInlineType())
1504 object->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : width() - rightOffset(m_height));
1505 else
1506 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
1507 }
1508
1509 if (object->hasStaticY())
1510 object->setStaticY(m_height);
1511 }
1512 iterator.increment();
1513 }
1514 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001515}
1516
bdakinf876bee2007-10-30 05:27:09 +00001517// This is currently just used for list markers and inline flows that have line boxes. Neither should
1518// have an effect on whitespace at the start of the line.
mitz@apple.com1a301772008-03-11 18:30:36 +00001519static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o)
bdakinf876bee2007-10-30 05:27:09 +00001520{
mitz@apple.com1a301772008-03-11 18:30:36 +00001521 RenderObject* next = bidiNext(block, o);
bdakinf876bee2007-10-30 05:27:09 +00001522 if (next && !next->isBR() && next->isText() && static_cast<RenderText*>(next)->textLength() > 0) {
1523 RenderText* nextText = static_cast<RenderText*>(next);
1524 UChar nextChar = nextText->characters()[0];
1525 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
mitz@apple.combfdc9112008-02-21 19:59:40 +00001526 addMidpoint(BidiIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001527 return true;
1528 }
1529 }
1530
1531 return false;
1532}
1533
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001534void RenderBlock::fitBelowFloats(int widthToFit, int& availableWidth)
1535{
1536 ASSERT(widthToFit > availableWidth);
1537
1538 int floatBottom;
1539 int lastFloatBottom = m_height;
1540 int newLineWidth = availableWidth;
1541 while (true) {
1542 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1543 if (!floatBottom)
1544 break;
1545
1546 newLineWidth = lineWidth(floatBottom);
1547 lastFloatBottom = floatBottom;
1548 if (newLineWidth >= widthToFit)
1549 break;
1550 }
1551
1552 if (newLineWidth > availableWidth) {
1553 m_height = lastFloatBottom;
1554 availableWidth = newLineWidth;
1555 }
1556}
1557
mitz@apple.com71e30842008-03-18 16:13:31 +00001558BidiIterator RenderBlock::findNextLineBreak(BidiState& start, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001559{
mitz@apple.com1a301772008-03-11 18:30:36 +00001560 ASSERT(start.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001561
mitz@apple.com1a301772008-03-11 18:30:36 +00001562 bool appliedStartWidth = start.position().pos > 0;
1563
1564 int width = skipWhitespace(start);
1565
kociendae40cb942004-10-05 20:05:38 +00001566 int w = 0;
1567 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001568
mitz@apple.com1a301772008-03-11 18:30:36 +00001569 if (start.position().atEnd())
1570 return start.position();
mjs6f821c82002-03-22 00:31:57 +00001571
hyatt33f8d492002-11-12 21:44:52 +00001572 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1573 // or not we are currently ignoring whitespace.
1574 bool ignoringSpaces = false;
hyatt98b16282004-03-31 18:43:12 +00001575 BidiIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001576
1577 // This variable tracks whether the very last character we saw was a space. We use
1578 // this to detect when we encounter a second space so we know we have to terminate
1579 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001580 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001581 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001582 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001583
mitz@apple.com1a301772008-03-11 18:30:36 +00001584 BidiIterator lBreak = start.position();
mjs6f821c82002-03-22 00:31:57 +00001585
mitz@apple.com1a301772008-03-11 18:30:36 +00001586 RenderObject *o = start.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001587 RenderObject *last = o;
mitz@apple.com1a301772008-03-11 18:30:36 +00001588 unsigned pos = start.position().pos;
ddkilzere8759ef2007-03-25 06:28:19 +00001589 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001590
hyatt0c3a9862004-02-23 21:26:26 +00001591 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1592 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001593
1594 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001595 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001596
hyatt@apple.com69340902008-01-16 21:24:21 +00001597 // 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 +00001598 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001599 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
1600 bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
1601
hyattb0d9f602007-01-15 01:28:23 +00001602 EWhiteSpace currWS = style()->whiteSpace();
1603 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001604 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001605 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1606 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1607
1608 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001609 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001610
mjsd2948ef2007-02-26 19:29:04 +00001611#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001612 bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
1613#else
hyattb0d9f602007-01-15 01:28:23 +00001614 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001615#endif
1616
hyattb0d9f602007-01-15 01:28:23 +00001617 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1618
hyatt275d0702005-11-03 23:53:57 +00001619 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001620 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001621 lBreak.obj = o;
1622 lBreak.pos = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001623 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001624
hyatt33f8d492002-11-12 21:44:52 +00001625 // A <br> always breaks a line, so don't let the line be collapsed
1626 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001627 // get collapsed away. It only does this if the previous line broke
1628 // cleanly. Otherwise the <br> has no effect on whether the line is
1629 // empty or not.
1630 if (prevLineBrokeCleanly)
1631 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001632 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001633 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001634
mitz@apple.com71e30842008-03-18 16:13:31 +00001635 if (!isLineEmpty && clear)
1636 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001637 }
1638 goto end;
1639 }
hyattb0d9f602007-01-15 01:28:23 +00001640
hyatt275d0702005-11-03 23:53:57 +00001641 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001642 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001643 if (o->isFloating()) {
hyatt3ac01352003-03-22 01:37:33 +00001644 insertFloatingObject(o);
hyatt33f8d492002-11-12 21:44:52 +00001645 // check if it fits in the current line.
1646 // If it does, position it now, otherwise, position
1647 // it after moving to next line (in newLine() func)
mitz@apple.com25beac62008-02-24 18:48:27 +00001648 if (floatsFitOnLine && o->width() + o->marginLeft() + o->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001649 positionNewFloats();
1650 width = lineWidth(m_height);
mitz@apple.com25beac62008-02-24 18:48:27 +00001651 } else
1652 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001653 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001654 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001655 // go ahead and determine our static x position now.
hyatt851816b2003-07-08 07:54:17 +00001656 bool isInlineType = o->style()->isOriginalDisplayInlineType();
hyatt98ee7e42003-05-14 01:39:15 +00001657 bool needToSetStaticX = o->hasStaticX();
1658 if (o->hasStaticX() && !isInlineType) {
1659 o->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001660 borderLeft() + paddingLeft() :
1661 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001662 needToSetStaticX = false;
1663 }
1664
1665 // If our original display was an INLINE type, then we can go ahead
1666 // and determine our static y position now.
1667 bool needToSetStaticY = o->hasStaticY();
1668 if (o->hasStaticY() && isInlineType) {
1669 o->setStaticY(m_height);
1670 needToSetStaticY = false;
1671 }
1672
hyatt853cd7d2004-11-05 02:59:48 +00001673 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1674 RenderObject* c = o->container();
1675 if (c->isInlineFlow() && (!needToSetStaticX || !needToSetStaticY))
1676 needToCreateLineBox = true;
1677
hyatt98ee7e42003-05-14 01:39:15 +00001678 // If we're ignoring spaces, we have to stop and include this object and
1679 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001680 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001681 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001682 ignoreStart.obj = o;
1683 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001684 if (ignoringSpaces) {
hyatt98b16282004-03-31 18:43:12 +00001685 addMidpoint(ignoreStart); // Stop ignoring spaces.
1686 addMidpoint(ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001687 }
hyatt98b16282004-03-31 18:43:12 +00001688
hyatt851816b2003-07-08 07:54:17 +00001689 }
hyatt98ee7e42003-05-14 01:39:15 +00001690 }
hyattffe78712003-02-11 01:59:29 +00001691 } else if (o->isInlineFlow()) {
bdakinf876bee2007-10-30 05:27:09 +00001692 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001693 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001694
1695 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1696 // to make sure that we stop to include this object and then start ignoring spaces again.
1697 // If this object is at the start of the line, we need to behave like list markers and
1698 // start ignoring spaces.
1699 if (inlineFlowRequiresLineBox(o)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001700 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001701 if (ignoringSpaces) {
1702 trailingSpaceObject = 0;
1703 addMidpoint(BidiIterator(0, o, 0)); // Stop ignoring spaces.
1704 addMidpoint(BidiIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com1a301772008-03-11 18:30:36 +00001705 } else if (style()->collapseWhiteSpace() && start.position().obj == o
1706 && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001707 // Like with list markers, we start ignoring spaces to make sure that any
1708 // additional spaces we see will be discarded.
1709 currentCharacterIsSpace = true;
1710 currentCharacterIsWS = true;
1711 ignoringSpaces = true;
1712 }
1713 }
1714
hyattb0d9f602007-01-15 01:28:23 +00001715 tmpW += o->marginLeft() + o->borderLeft() + o->paddingLeft() +
1716 o->marginRight() + o->borderRight() + o->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001717 } else if (o->isReplaced()) {
hyattde396342003-10-29 08:57:20 +00001718 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001719 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001720 w += tmpW;
1721 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001722 lBreak.obj = o;
1723 lBreak.pos = 0;
hyatt711fe232002-11-20 21:25:14 +00001724 }
1725
mitz@apple.combfdc9112008-02-21 19:59:40 +00001726 if (ignoringSpaces)
1727 addMidpoint(BidiIterator(0, o, 0));
1728
hyatt33f8d492002-11-12 21:44:52 +00001729 isLineEmpty = false;
1730 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001731 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001732 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001733 trailingSpaceObject = 0;
hyatte85e4a72002-12-08 02:06:16 +00001734
bdakinf876bee2007-10-30 05:27:09 +00001735 // Optimize for a common case. If we can't find whitespace after the list
1736 // item, then this is all moot. -dwh
mjsd26b2972007-02-13 13:09:04 +00001737 if (o->isListMarker() && !static_cast<RenderListMarker*>(o)->isInside()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001738 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001739 // Like with inline flows, we start ignoring spaces to make sure that any
1740 // additional spaces we see will be discarded.
1741 currentCharacterIsSpace = true;
1742 currentCharacterIsWS = true;
1743 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001744 }
justing244d3a32006-04-13 01:31:24 +00001745 } else
hyattb0d9f602007-01-15 01:28:23 +00001746 tmpW += o->width() + o->marginLeft() + o->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001747 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001748 if (!pos)
1749 appliedStartWidth = false;
1750
weinigf28a1c32007-02-14 14:10:31 +00001751 RenderText* t = static_cast<RenderText*>(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001752
darin42563ac52007-01-22 17:28:57 +00001753 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001754 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001755 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001756
darin42563ac52007-01-22 17:28:57 +00001757 const Font& f = t->style(m_firstLine)->font();
weinigf28a1c32007-02-14 14:10:31 +00001758
hyatt33f8d492002-11-12 21:44:52 +00001759 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001760 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001761 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001762
darin54008922006-01-13 16:39:05 +00001763 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001764 int charWidth = 0;
darin47ece0d2005-09-04 07:42:31 +00001765 int nextBreakable = -1;
weinigf28a1c32007-02-14 14:10:31 +00001766 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1767 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1768 // 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 +00001769 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001770 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001771 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1772
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001773 if (t->isWordBreak()) {
1774 w += tmpW;
1775 tmpW = 0;
1776 lBreak.obj = o;
1777 lBreak.pos = 0;
1778 ASSERT(!len);
1779 }
1780
hyatt275d0702005-11-03 23:53:57 +00001781 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001782 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001783 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001784 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001785 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001786
hyattb0d9f602007-01-15 01:28:23 +00001787 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001788 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001789
hyatt78b85132004-03-29 20:07:45 +00001790 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001791 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001792 if (!ignoringSpaces) {
1793 // Ignore soft hyphens
mitz@apple.combe429562008-03-07 01:09:51 +00001794 BidiIterator beforeSoftHyphen;
1795 if (pos)
1796 beforeSoftHyphen = BidiIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001797 else
mitz@apple.com1a301772008-03-11 18:30:36 +00001798 beforeSoftHyphen = BidiIterator(0, last, last->isText() ? static_cast<RenderText*>(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001799 // Two consecutive soft hyphens. Avoid overlapping midpoints.
mitz@apple.combe429562008-03-07 01:09:51 +00001800 if (sNumMidpoints && smidpoints->at(sNumMidpoints - 1).obj == o && smidpoints->at(sNumMidpoints - 1).pos == pos)
bdakin9151ba52005-10-24 22:51:06 +00001801 sNumMidpoints--;
1802 else
mitz@apple.combe429562008-03-07 01:09:51 +00001803 addMidpoint(beforeSoftHyphen);
1804
hyatt78b85132004-03-29 20:07:45 +00001805 // Add the width up to but not including the hyphen.
weinigf28a1c32007-02-14 14:10:31 +00001806 tmpW += t->width(lastSpace, pos - lastSpace, f, w + tmpW) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001807
hyattdca76e92005-11-02 08:52:50 +00001808 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001809 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001810 if (autoWrap)
weinigf28a1c32007-02-14 14:10:31 +00001811 tmpW += t->width(pos, 1, f, w + tmpW);
mitz@apple.combe429562008-03-07 01:09:51 +00001812
1813 BidiIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001814 afterSoftHyphen.increment();
mitz@apple.combe429562008-03-07 01:09:51 +00001815 addMidpoint(afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001816 }
mitz@apple.combe429562008-03-07 01:09:51 +00001817
hyatt78b85132004-03-29 20:07:45 +00001818 pos++;
1819 len--;
eseideld13fe532005-11-30 02:40:29 +00001820 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001821 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
1822 continue;
1823 }
1824
hyatt3aff2332003-01-23 01:15:28 +00001825 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001826
darinf9e5d6c2007-01-09 14:54:26 +00001827 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001828
weiniged111c12007-07-13 22:45:11 +00001829 if ((breakAll || breakWords) && !midWordBreak) {
1830 wrapW += charWidth;
1831 charWidth = t->width(pos, 1, f, w + wrapW);
1832 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001833 }
darin47ece0d2005-09-04 07:42:31 +00001834
ddkilzere8759ef2007-03-25 06:28:19 +00001835 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001836
weiniged111c12007-07-13 22:45:11 +00001837 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001838 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001839 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001840 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001841 // Stop ignoring spaces and begin at this
1842 // new point.
hyatt48710d62003-08-21 09:17:13 +00001843 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001844 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001845 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001846 addMidpoint(BidiIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001847 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001848 } else {
hyatt33f8d492002-11-12 21:44:52 +00001849 // Just keep ignoring these spaces.
1850 pos++;
1851 len--;
1852 continue;
1853 }
1854 }
rjwc9c257d2003-01-24 03:46:17 +00001855
darin54008922006-01-13 16:39:05 +00001856 int additionalTmpW = t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
1857 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001858 if (!appliedStartWidth) {
1859 tmpW += inlineWidth(o, true, false);
1860 appliedStartWidth = true;
1861 }
1862
eseideld13fe532005-11-30 02:40:29 +00001863 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001864
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001865 if (!w && autoWrap && tmpW > width)
1866 fitBelowFloats(tmpW, width);
1867
hyattb0d9f602007-01-15 01:28:23 +00001868 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001869 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001870 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001871 bool lineWasTooWide = false;
1872 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
1873 int charWidth = t->width(pos, 1, f, w + tmpW) + (applyWordSpacing ? wordSpacing : 0);
1874 // Check if line is too big even without the extra space
1875 // at the end of the line. If it is not, do nothing.
1876 // If the line needs the extra whitespace to be too long,
1877 // then move the line break to the space and skip all
1878 // additional whitespace.
1879 if (w + tmpW + charWidth > width) {
1880 lineWasTooWide = true;
1881 lBreak.obj = o;
1882 lBreak.pos = pos;
mitz@apple.com1a301772008-03-11 18:30:36 +00001883 skipWhitespace(lBreak);
kocienda9dbe9b12004-10-22 20:07:05 +00001884 }
ap932806a2006-10-01 09:06:09 +00001885 }
1886 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com3d3d92b2008-02-21 05:00:10 +00001887 if (lBreak.obj && shouldPreserveNewline(lBreak.obj) && lBreak.obj->isText() && !static_cast<RenderText*>(lBreak.obj)->isWordBreak() && static_cast<RenderText*>(lBreak.obj)->characters()[lBreak.pos] == '\n') {
hyatt0c05e102006-04-14 08:15:00 +00001888 if (!stoppedIgnoringSpaces && pos > 0) {
1889 // We need to stop right before the newline and then start up again.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001890 addMidpoint(BidiIterator(0, o, pos - 1)); // Stop
hyatt0c05e102006-04-14 08:15:00 +00001891 addMidpoint(BidiIterator(0, o, pos)); // Start
1892 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001893 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001894 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001895 }
hyatt78b85132004-03-29 20:07:45 +00001896 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001897 } else {
weiniged111c12007-07-13 22:45:11 +00001898 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001899 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001900 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001901 // Subtract the width of the soft hyphen out since we fit on a line.
1902 tmpW -= t->width(pos-1, 1, f, w+tmpW);
1903 }
rjwc9c257d2003-01-24 03:46:17 +00001904 }
hyatt33f8d492002-11-12 21:44:52 +00001905
hyattb0d9f602007-01-15 01:28:23 +00001906 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001907 if (!stoppedIgnoringSpaces && pos > 0) {
1908 // We need to stop right before the newline and then start up again.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001909 addMidpoint(BidiIterator(0, o, pos - 1)); // Stop
hyatt0c05e102006-04-14 08:15:00 +00001910 addMidpoint(BidiIterator(0, o, pos)); // Start
1911 }
hyatta9f48e32003-02-03 22:48:01 +00001912 lBreak.obj = o;
1913 lBreak.pos = pos;
mitz@apple.com1a301772008-03-11 18:30:36 +00001914 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001915 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001916 return lBreak;
1917 }
hyatta9f48e32003-02-03 22:48:01 +00001918
weinigf28a1c32007-02-14 14:10:31 +00001919 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001920 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001921 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001922 tmpW = 0;
1923 lBreak.obj = o;
1924 lBreak.pos = pos;
weinigf28a1c32007-02-14 14:10:31 +00001925 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1926 // opportunity to break after a word.
1927 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001928 }
hyatt33f8d492002-11-12 21:44:52 +00001929
darin54008922006-01-13 16:39:05 +00001930 if (midWordBreak) {
1931 // Remember this as a breakable position in case
1932 // adding the end width forces a break.
1933 lBreak.obj = o;
1934 lBreak.pos = pos;
weiniged111c12007-07-13 22:45:11 +00001935 midWordBreak &= (breakWords || breakAll);
1936 }
1937
1938 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001939 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1940 lastSpace = pos;
1941 }
hyatt33f8d492002-11-12 21:44:52 +00001942
hyattdca76e92005-11-02 08:52:50 +00001943 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001944 // If we encounter a newline, or if we encounter a
1945 // second space, we need to go ahead and break up this
1946 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001947 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001948 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001949
hyatt33f8d492002-11-12 21:44:52 +00001950 // We just entered a mode where we are ignoring
1951 // spaces. Create a midpoint to terminate the run
1952 // before the second space.
hyatt98b16282004-03-31 18:43:12 +00001953 addMidpoint(ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001954 }
1955 }
eseidel789896f2005-11-27 22:52:09 +00001956 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001957 // Stop ignoring spaces and begin at this
1958 // new point.
1959 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001960 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001961 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001962 addMidpoint(BidiIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001963 }
hyatt98b16282004-03-31 18:43:12 +00001964
1965 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1966 ignoreStart.obj = o;
1967 ignoreStart.pos = pos;
1968 }
harrisone343c412005-01-18 01:07:26 +00001969
1970 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001971 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001972 lBreak.obj = o;
1973 lBreak.pos = pos;
1974 }
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)
eseideld13fe532005-11-30 02:40:29 +00001989 tmpW += t->width(lastSpace, pos - lastSpace, f, w+tmpW) + 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;
hyatta9f48e32003-02-03 22:48:01 +00002004 RenderText* nextText = static_cast<RenderText*>(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) {
2016 fitBelowFloats(tmpW, width);
2017 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;
2025 }
2026 }
2027 }
2028 }
2029
darin54008922006-01-13 16:39:05 +00002030 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002031 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002032 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002033 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002034
2035 if (w)
2036 goto end;
2037
2038 fitBelowFloats(tmpW, width);
hyattf14a4a32002-11-21 22:06:32 +00002039
hyatta14d1742003-01-02 20:25:46 +00002040 // |width| may have been adjusted because we got shoved down past a float (thus
2041 // giving us more room), so we need to retest, and only jump to
2042 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002043 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002044 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002045 }
hyatt1d9e29b2003-04-10 01:48:53 +00002046
mitz@apple.com1a301772008-03-11 18:30:36 +00002047 if (!o->isFloatingOrPositioned()) {
2048 last = o;
2049 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || static_cast<RenderListMarker*>(last)->isInside())) {
2050 w += tmpW;
2051 tmpW = 0;
2052 lBreak.obj = next;
2053 lBreak.pos = 0;
2054 }
hyatt711fe232002-11-20 21:25:14 +00002055 }
2056
mitz@apple.com1a301772008-03-11 18:30:36 +00002057 o = next;
2058
hyatta9f48e32003-02-03 22:48:01 +00002059 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2060 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002061 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002062 currentCharacterIsSpace = false;
2063
kociendabb0c24b2001-08-24 14:24:40 +00002064 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002065 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002066 }
2067
hyattb0d9f602007-01-15 01:28:23 +00002068
2069 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00002070 lBreak.obj = 0;
2071 lBreak.pos = 0;
2072 }
2073
2074 end:
rjwc9c257d2003-01-24 03:46:17 +00002075
mitz@apple.com1a301772008-03-11 18:30:36 +00002076 if (lBreak == start.position() && !lBreak.obj->isBR()) {
kociendabb0c24b2001-08-24 14:24:40 +00002077 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002078 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002079 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002080 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002081 lBreak.obj = o;
2082 lBreak.pos = pos - 1;
2083 } else {
2084 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002085 lBreak.pos = last->isText() ? last->length() : 0;
kociendabb0c24b2001-08-24 14:24:40 +00002086 }
hyatt275d0702005-11-03 23:53:57 +00002087 } else if (lBreak.obj) {
2088 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002089 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002090 lBreak.obj = o;
2091 lBreak.pos = 0;
2092 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002093 // Don't ever break in the middle of a word if we can help it.
2094 // There's no room at all. We just have to be on this line,
2095 // even though we'll spill out.
2096 lBreak.obj = o;
2097 lBreak.pos = pos;
hyatt33f8d492002-11-12 21:44:52 +00002098 }
kociendabb0c24b2001-08-24 14:24:40 +00002099 }
2100 }
2101
2102 // make sure we consume at least one char/object.
mitz@apple.com1a301772008-03-11 18:30:36 +00002103 if (lBreak == start.position())
2104 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002105
hyattfe99c872003-07-31 22:25:29 +00002106 // Sanity check our midpoints.
mitz@apple.com1a301772008-03-11 18:30:36 +00002107 checkMidpoints(lBreak);
hyattfe99c872003-07-31 22:25:29 +00002108
hyatt33f8d492002-11-12 21:44:52 +00002109 if (trailingSpaceObject) {
2110 // This object is either going to be part of the last midpoint, or it is going
2111 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2112 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt85586af2003-02-19 23:22:42 +00002113 if (sNumMidpoints%2==1) {
2114 BidiIterator* midpoints = smidpoints->data();
2115 midpoints[sNumMidpoints-1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002116 }
2117 //else if (lBreak.pos > 0)
2118 // lBreak.pos--;
2119 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002120 // Add a new end midpoint that stops right at the very end.
hyattd20075d2002-11-16 02:23:32 +00002121 RenderText* text = static_cast<RenderText *>(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002122 unsigned length = text->textLength();
2123 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
eseidel789896f2005-11-27 22:52:09 +00002124 BidiIterator endMid(0, trailingSpaceObject, pos);
hyatt85586af2003-02-19 23:22:42 +00002125 addMidpoint(endMid);
hyatt33f8d492002-11-12 21:44:52 +00002126 }
2127 }
rjwc9c257d2003-01-24 03:46:17 +00002128
mjs54b64002003-04-02 02:59:02 +00002129 // We might have made lBreak an iterator that points past the end
2130 // of the object. Do this adjustment to make it point to the start
2131 // of the next object instead to avoid confusing the rest of the
2132 // code.
2133 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002134 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002135 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002136 }
2137
hyatt78b85132004-03-29 20:07:45 +00002138 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2139 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2140 // ignore the hyphen so that it will render at the end of the line.
darin42563ac52007-01-22 17:28:57 +00002141 UChar c = static_cast<RenderText*>(lBreak.obj)->characters()[lBreak.pos-1];
2142 if (c == softHyphen)
hyatt78b85132004-03-29 20:07:45 +00002143 chopMidpointsAt(lBreak.obj, lBreak.pos-2);
2144 }
2145
kociendabb0c24b2001-08-24 14:24:40 +00002146 return lBreak;
2147}
2148
hyattb4b20872004-10-20 21:34:01 +00002149void RenderBlock::checkLinesForOverflow()
2150{
hyattb4b20872004-10-20 21:34:01 +00002151 m_overflowWidth = m_width;
2152 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
darin7bd70952006-04-13 07:07:34 +00002153 m_overflowLeft = min(curr->leftOverflow(), m_overflowLeft);
2154 m_overflowTop = min(curr->topOverflow(), m_overflowTop);
2155 m_overflowWidth = max(curr->rightOverflow(), m_overflowWidth);
2156 m_overflowHeight = max(curr->bottomOverflow(), m_overflowHeight);
hyattb4b20872004-10-20 21:34:01 +00002157 }
2158}
2159
hyatted77ad82004-06-15 07:21:23 +00002160void RenderBlock::deleteEllipsisLineBoxes()
2161{
hyatted77ad82004-06-15 07:21:23 +00002162 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002163 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002164}
2165
2166void RenderBlock::checkLinesForTextOverflow()
2167{
2168 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002169 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2170 TextRun ellipsisRun(&horizontalEllipsis, 1);
2171 static AtomicString ellipsisStr(&horizontalEllipsis, 1);
hyatt3e99d1c2006-02-24 21:13:08 +00002172 const Font& firstLineFont = firstLineStyle()->font();
2173 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002174 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2175 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002176
2177 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2178 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2179 // check the left edge of the line box to see if it is less
2180 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2181 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002182 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyattda77c4b2004-06-17 18:09:49 +00002183 int blockEdge = ltr ? rightOffset(curr->yPos()) : leftOffset(curr->yPos());
hyatt1a8d2512004-06-17 01:38:30 +00002184 int lineBoxEdge = ltr ? curr->xPos() + curr->width() : curr->xPos();
hyatted77ad82004-06-15 07:21:23 +00002185 if ((ltr && lineBoxEdge > blockEdge) || (!ltr && lineBoxEdge < blockEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002186 // 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 +00002187 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2188 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2189 // space.
2190 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
hyatt1a8d2512004-06-17 01:38:30 +00002191 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
2192 curr->placeEllipsis(ellipsisStr, ltr, blockEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002193 }
2194 }
2195}
2196
hyattffe78712003-02-11 01:59:29 +00002197}