blob: f14ec7069e8f4733e5ddfad1fffcea3a6bfd27b1 [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) {
mitz@apple.com8e05a142008-04-22 20:42:02 +0000994 if (direction == LTR) {
mitz@apple.com3a43c712008-04-20 04:26:28 +0000995 start.moveRunToEnd(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +0000996 trailingSpaceRun->m_level = 0;
997 } else {
mitz@apple.com3a43c712008-04-20 04:26:28 +0000998 start.moveRunToBeginning(trailingSpaceRun);
mitz@apple.com8e05a142008-04-22 20:42:02 +0000999 trailingSpaceRun->m_level = 1;
1000 }
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001001 }
1002 }
1003 } else
1004 trailingSpaceRun = 0;
1005 }
hyatt4b381692003-03-10 21:11:59 +00001006
1007 // Now that the runs have been ordered, we create the line boxes.
1008 // At the same time we figure out where border/padding/margin should be applied for
1009 // inline flow boxes.
hyatt4b381692003-03-10 21:11:59 +00001010
hyatt0c3a9862004-02-23 21:26:26 +00001011 RootInlineBox* lineBox = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001012 if (start.runCount()) {
1013 lineBox = constructLine(start.runCount(), start.firstRun(), start.lastRun(), !end.obj, end.obj && !end.pos ? end.obj : 0);
hyattdfb09052003-03-11 02:58:59 +00001014 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +00001015 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
pewtermoosecf72e2d2007-07-20 19:01:55 +00001016
hyattdfb09052003-03-11 02:58:59 +00001017 // Now we position all of our text runs horizontally.
mitz@apple.comc13ea5f2008-04-18 21:18:26 +00001018 computeHorizontalPositionsForLine(lineBox, start.firstRun(), trailingSpaceRun, end.atEnd());
mitz@apple.come1364202008-02-28 01:06:41 +00001019
hyattdfb09052003-03-11 02:58:59 +00001020 // Now position our text runs vertically.
mitz@apple.com1a301772008-03-11 18:30:36 +00001021 computeVerticalPositionsForLine(lineBox, start.firstRun());
hyattbddcc612006-07-24 23:16:15 +00001022
oliver98d52382007-10-12 10:47:18 +00001023#if ENABLE(SVG)
1024 // Special SVG text layout code
1025 lineBox->computePerCharacterLayoutInformation();
1026#endif
1027
hyattbddcc612006-07-24 23:16:15 +00001028#if PLATFORM(MAC)
1029 // Highlight acts as an overflow inflation.
1030 if (style()->highlight() != nullAtom)
1031 lineBox->addHighlightOverflow();
1032#endif
hyattdfb09052003-03-11 02:58:59 +00001033 }
1034 }
ggarenec11e5b2007-02-25 02:14:54 +00001035
mitz@apple.com1a301772008-03-11 18:30:36 +00001036 start.deleteRuns();
hyatt35a85e52003-02-14 19:43:07 +00001037
ap9059f6f2006-07-24 16:55:02 +00001038 if (lineBox) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001039 lineBox->setLineBreakInfo(end.obj, end.pos, start.status());
apddd2ff42007-03-31 08:26:24 +00001040 if (useRepaintBounds) {
ap9059f6f2006-07-24 16:55:02 +00001041 repaintTop = min(repaintTop, lineBox->topOverflow());
1042 repaintBottom = max(repaintBottom, lineBox->bottomOverflow());
1043 }
1044 }
mitz@apple.come1364202008-02-28 01:06:41 +00001045
hyatt35a85e52003-02-14 19:43:07 +00001046 m_firstLine = false;
mitz@apple.com71e30842008-03-18 16:13:31 +00001047 newLine(clear);
darinb70665a2002-04-15 23:43:21 +00001048 }
mitz@apple.come1364202008-02-28 01:06:41 +00001049
mitz@apple.com7b089842008-03-22 06:57:28 +00001050 if (m_floatingObjects && lastRootBox()) {
mitz@apple.com40547b32008-03-18 04:04:34 +00001051 if (lastFloat) {
1052 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1053 }
1054 m_floatingObjects->next();
1055 } else
1056 m_floatingObjects->first();
1057 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001058 lastRootBox()->floats().append(f->m_renderer);
1059 ASSERT(f->m_renderer == floats[floatIndex].object);
mitz@apple.com40547b32008-03-18 04:04:34 +00001060 // If a float's geometry has changed, give up on syncing with clean lines.
mitz@apple.com93526592008-03-18 04:36:51 +00001061 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 +00001062 checkForEndLineMatch = false;
mitz@apple.com40547b32008-03-18 04:04:34 +00001063 floatIndex++;
1064 }
1065 lastFloat = m_floatingObjects->last();
1066 }
1067
hyatt85586af2003-02-19 23:22:42 +00001068 sNumMidpoints = 0;
1069 sCurrMidpoint = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001070 start.setPosition(end);
kociendabb0c24b2001-08-24 14:24:40 +00001071 }
mitz@apple.come1364202008-02-28 01:06:41 +00001072
hyatt0c3a9862004-02-23 21:26:26 +00001073 if (endLine) {
1074 if (endLineMatched) {
1075 // Attach all the remaining lines, and then adjust their y-positions as needed.
hyatt0c3a9862004-02-23 21:26:26 +00001076 int delta = m_height - endLineYPos;
mitz@apple.com40547b32008-03-18 04:04:34 +00001077 for (RootInlineBox* line = endLine; line; line = line->nextRootBox()) {
1078 line->attachLine();
1079 if (delta) {
mitz@apple.come1364202008-02-28 01:06:41 +00001080 repaintTop = min(repaintTop, line->topOverflow() + min(delta, 0));
1081 repaintBottom = max(repaintBottom, line->bottomOverflow() + max(delta, 0));
hyattda77c4b2004-06-17 18:09:49 +00001082 line->adjustPosition(0, delta);
ap9059f6f2006-07-24 16:55:02 +00001083 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001084 if (Vector<RenderObject*>* cleanLineFloats = line->floatsPtr()) {
1085 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1086 for (Vector<RenderObject*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1087 int floatTop = (*f)->yPos() - (*f)->marginTop();
1088 insertFloatingObject(*f);
1089 m_height = floatTop + delta;
1090 positionNewFloats();
1091 }
1092 }
harrison208ea792005-07-29 23:42:59 +00001093 }
hyatt0c3a9862004-02-23 21:26:26 +00001094 m_height = lastRootBox()->blockHeight();
ap9059f6f2006-07-24 16:55:02 +00001095 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001096 // Delete all the remaining lines.
hyatt0c3a9862004-02-23 21:26:26 +00001097 InlineRunBox* line = endLine;
1098 RenderArena* arena = renderArena();
1099 while (line) {
ap9059f6f2006-07-24 16:55:02 +00001100 repaintTop = min(repaintTop, line->topOverflow());
1101 repaintBottom = max(repaintBottom, line->bottomOverflow());
hyatt0c3a9862004-02-23 21:26:26 +00001102 InlineRunBox* next = line->nextLineBox();
hyatt0c3a9862004-02-23 21:26:26 +00001103 line->deleteLine(arena);
1104 line = next;
1105 }
1106 }
1107 }
mitz@apple.com40547b32008-03-18 04:04:34 +00001108 if (m_floatingObjects) {
1109 // In case we have a float on the last line, it might not be positioned up to now.
1110 // This has to be done before adding in the bottom border/padding, or the float will
1111 // include the padding incorrectly. -dwh
1112 if (positionNewFloats() && lastRootBox()) {
1113 if (lastFloat) {
1114 for (FloatingObject* f = m_floatingObjects->last(); f != lastFloat; f = m_floatingObjects->prev()) {
1115 }
1116 m_floatingObjects->next();
1117 } else
1118 m_floatingObjects->first();
1119 for (FloatingObject* f = m_floatingObjects->current(); f; f = m_floatingObjects->next())
mitz@apple.com93526592008-03-18 04:36:51 +00001120 lastRootBox()->floats().append(f->m_renderer);
mitz@apple.com40547b32008-03-18 04:04:34 +00001121 lastFloat = m_floatingObjects->last();
1122 }
1123 }
1124
kociendabb0c24b2001-08-24 14:24:40 +00001125 }
hyatt85586af2003-02-19 23:22:42 +00001126
1127 sNumMidpoints = 0;
1128 sCurrMidpoint = 0;
hyatta70560a2002-11-20 01:53:20 +00001129
1130 // Now add in the bottom border/padding.
kociendabb0c24b2001-08-24 14:24:40 +00001131 m_height += toAdd;
1132
hyatta70560a2002-11-20 01:53:20 +00001133 // Always make sure this is at least our height.
darin7bd70952006-04-13 07:07:34 +00001134 m_overflowHeight = max(m_height, m_overflowHeight);
mitz@apple.come1364202008-02-28 01:06:41 +00001135
hyattb4b20872004-10-20 21:34:01 +00001136 // See if any lines spill out of the block. If so, we need to update our overflow width.
1137 checkLinesForOverflow();
1138
adele7a470a72006-04-20 22:22:14 +00001139 if (!firstLineBox() && hasLineIfEmpty())
adele05abee32006-08-25 23:44:05 +00001140 m_height += lineHeight(true, true);
hyatted77ad82004-06-15 07:21:23 +00001141
1142 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1143 // truncate text.
1144 if (hasTextOverflow)
1145 checkLinesForTextOverflow();
kociendabb0c24b2001-08-24 14:24:40 +00001146}
1147
mitz@apple.com40547b32008-03-18 04:04:34 +00001148RootInlineBox* RenderBlock::determineStartPosition(bool& fullLayout, BidiState& start, Vector<FloatWithRect>& floats, unsigned& numCleanFloats)
hyatt0c3a9862004-02-23 21:26:26 +00001149{
1150 RootInlineBox* curr = 0;
1151 RootInlineBox* last = 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001152
mitz@apple.com40547b32008-03-18 04:04:34 +00001153 bool dirtiedByFloat = false;
1154 if (!fullLayout) {
1155 size_t floatIndex = 0;
1156 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox()) {
1157 if (Vector<RenderObject*>* cleanLineFloats = curr->floatsPtr()) {
1158 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1159 for (Vector<RenderObject*>::iterator o = cleanLineFloats->begin(); o != end; ++o) {
1160 RenderObject* f = *o;
1161 IntSize newSize(f->width() + f->marginLeft() +f->marginRight(), f->height() + f->marginTop() + f->marginBottom());
1162 ASSERT(floatIndex < floats.size());
1163 if (floats[floatIndex].object != f) {
1164 // A new float has been inserted before this line or before its last known float.
1165 // Just do a full layout.
1166 fullLayout = true;
1167 break;
1168 }
1169 if (floats[floatIndex].rect.size() != newSize) {
mitz@apple.com070a7de2008-03-19 18:40:52 +00001170 int floatTop = floats[floatIndex].rect.y();
mitz@apple.com40547b32008-03-18 04:04:34 +00001171 curr->markDirty();
1172 markLinesDirtyInVerticalRange(curr->blockHeight(), floatTop + max(floats[floatIndex].rect.height(), newSize.height()));
1173 floats[floatIndex].rect.setSize(newSize);
1174 dirtiedByFloat = true;
1175 }
1176 floatIndex++;
1177 }
1178 }
1179 if (dirtiedByFloat || fullLayout)
1180 break;
1181 }
1182 // Check if a new float has been inserted after the last known float.
1183 if (!curr && floatIndex < floats.size())
1184 fullLayout = true;
1185 }
1186
hyatt0c3a9862004-02-23 21:26:26 +00001187 if (fullLayout) {
1188 // Nuke all our lines.
1189 if (firstRootBox()) {
1190 RenderArena* arena = renderArena();
1191 curr = firstRootBox();
1192 while (curr) {
1193 RootInlineBox* next = curr->nextRootBox();
1194 curr->deleteLine(arena);
1195 curr = next;
1196 }
darinec375482007-01-06 01:36:24 +00001197 ASSERT(!firstLineBox() && !lastLineBox());
hyatt0c3a9862004-02-23 21:26:26 +00001198 }
eseidel789896f2005-11-27 22:52:09 +00001199 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001200 if (curr) {
1201 // We have a dirty line.
mjs9f78dd92007-02-12 04:06:07 +00001202 if (RootInlineBox* prevRootBox = curr->prevRootBox()) {
hyatt0c3a9862004-02-23 21:26:26 +00001203 // We have a previous line.
mitz@apple.com40547b32008-03-18 04:04:34 +00001204 if (!dirtiedByFloat && (!prevRootBox->endsWithBreak() || prevRootBox->lineBreakObj()->isText() && prevRootBox->lineBreakPos() >= static_cast<RenderText*>(prevRootBox->lineBreakObj())->textLength()))
mjs9f78dd92007-02-12 04:06:07 +00001205 // The previous line didn't break cleanly or broke at a newline
1206 // that has been deleted, so treat it as dirty too.
1207 curr = prevRootBox;
hyatt0c3a9862004-02-23 21:26:26 +00001208 }
eseidel789896f2005-11-27 22:52:09 +00001209 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001210 // No dirty lines were found.
1211 // If the last line didn't break cleanly, treat it as dirty.
1212 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1213 curr = lastRootBox();
1214 }
mitz@apple.come1364202008-02-28 01:06:41 +00001215
hyatt0c3a9862004-02-23 21:26:26 +00001216 // If we have no dirty lines, then last is just the last root box.
1217 last = curr ? curr->prevRootBox() : lastRootBox();
1218 }
mitz@apple.come1364202008-02-28 01:06:41 +00001219
mitz@apple.com40547b32008-03-18 04:04:34 +00001220 numCleanFloats = 0;
1221 if (!floats.isEmpty()) {
1222 int savedHeight = m_height;
1223 // Restore floats from clean lines.
1224 RootInlineBox* line = firstRootBox();
1225 while (line != curr) {
1226 if (Vector<RenderObject*>* cleanLineFloats = line->floatsPtr()) {
1227 Vector<RenderObject*>::iterator end = cleanLineFloats->end();
1228 for (Vector<RenderObject*>::iterator f = cleanLineFloats->begin(); f != end; ++f) {
1229 insertFloatingObject(*f);
1230 m_height = (*f)->yPos() - (*f)->marginTop();
1231 positionNewFloats();
1232 ASSERT(floats[numCleanFloats].object == *f);
1233 numCleanFloats++;
1234 }
1235 }
1236 line = line->nextRootBox();
1237 }
1238 m_height = savedHeight;
1239 }
1240
hyatt0c3a9862004-02-23 21:26:26 +00001241 m_firstLine = !last;
1242 previousLineBrokeCleanly = !last || last->endsWithBreak();
mitz@apple.com1a301772008-03-11 18:30:36 +00001243
1244 RenderObject* startObj;
1245 int pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001246 if (last) {
1247 m_height = last->blockHeight();
hyatt0c3a9862004-02-23 21:26:26 +00001248 startObj = last->lineBreakObj();
1249 pos = last->lineBreakPos();
mitz@apple.com1a301772008-03-11 18:30:36 +00001250 start.setStatus(last->lineBreakBidiStatus());
darindde01502005-12-18 22:55:35 +00001251 } else {
mitz@apple.com1a301772008-03-11 18:30:36 +00001252 bool ltr = style()->direction() == LTR
1253 #if ENABLE(SVG)
1254 || (style()->unicodeBidi() == UBNormal && isSVGText())
1255 #endif
1256 ;
mitz@apple.com40547b32008-03-18 04:04:34 +00001257
mitz@apple.com1a301772008-03-11 18:30:36 +00001258 BidiContext* context = new BidiContext(ltr ? 0 : 1, ltr ? LeftToRight : RightToLeft, style()->unicodeBidi() == Override);
1259
1260 start.setLastStrongDir(context->dir());
1261 start.setLastDir(context->dir());
1262 start.setEorDir(context->dir());
1263 start.setContext(context);
1264 startObj = bidiFirst(this, &start);
darindde01502005-12-18 22:55:35 +00001265 }
mitz@apple.come1364202008-02-28 01:06:41 +00001266
mitz@apple.com1a301772008-03-11 18:30:36 +00001267 start.setPosition(BidiIterator(this, startObj, pos));
mitz@apple.come1364202008-02-28 01:06:41 +00001268
hyatt0c3a9862004-02-23 21:26:26 +00001269 return curr;
1270}
1271
pewtermoosecf72e2d2007-07-20 19:01:55 +00001272RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, BidiIterator& cleanLineStart, BidiStatus& cleanLineBidiStatus, int& yPos)
hyatt0c3a9862004-02-23 21:26:26 +00001273{
1274 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001275 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001276 last = 0;
1277 else {
hyatt04420ca2004-07-16 00:05:42 +00001278 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1279 if (curr->isDirty())
1280 last = 0;
1281 else if (!last)
1282 last = curr;
1283 }
hyatt0c3a9862004-02-23 21:26:26 +00001284 }
mitz@apple.come1364202008-02-28 01:06:41 +00001285
hyatt0c3a9862004-02-23 21:26:26 +00001286 if (!last)
1287 return 0;
mitz@apple.come1364202008-02-28 01:06:41 +00001288
eseidel789896f2005-11-27 22:52:09 +00001289 RootInlineBox* prev = last->prevRootBox();
1290 cleanLineStart = BidiIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
1291 cleanLineBidiStatus = prev->lineBreakBidiStatus();
ap9059f6f2006-07-24 16:55:02 +00001292 yPos = prev->blockHeight();
mitz@apple.come1364202008-02-28 01:06:41 +00001293
hyatt0c3a9862004-02-23 21:26:26 +00001294 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1295 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1296 // their connections to one another.
mitz@apple.come1364202008-02-28 01:06:41 +00001297
hyatt0c3a9862004-02-23 21:26:26 +00001298 return last;
1299}
1300
mitz@apple.com1a301772008-03-11 18:30:36 +00001301bool 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 +00001302{
mitz@apple.com40547b32008-03-18 04:04:34 +00001303 if (start.position() == endLineStart) {
1304 if (start.status() != endLineStatus)
1305 return false;
1306
1307 int delta = m_height - endYPos;
1308 if (!delta || !m_floatingObjects)
1309 return true;
1310
1311 // See if any floats end in the range along which we want to shift the lines vertically.
1312 int top = min(m_height, endYPos);
1313
1314 RootInlineBox* lastLine = endLine;
1315 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1316 lastLine = nextLine;
1317
1318 int bottom = lastLine->blockHeight() + abs(delta);
1319
1320 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001321 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001322 return false;
1323 }
1324
1325 return true;
1326 }
hyatt0c3a9862004-02-23 21:26:26 +00001327
mitz@apple.come1364202008-02-28 01:06:41 +00001328 // The first clean line doesn't match, but we can check a handful of following lines to try
1329 // to match back up.
1330 static int numLines = 8; // The # of lines we're willing to match against.
1331 RootInlineBox* line = endLine;
1332 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001333 if (line->lineBreakObj() == start.position().obj && line->lineBreakPos() == start.position().pos) {
mitz@apple.come1364202008-02-28 01:06:41 +00001334 // We have a match.
mitz@apple.com1a301772008-03-11 18:30:36 +00001335 if (line->lineBreakBidiStatus() != start.status())
mitz@apple.come1364202008-02-28 01:06:41 +00001336 return false; // ...but the bidi state doesn't match.
1337 RootInlineBox* result = line->nextRootBox();
1338
1339 // Set our yPos to be the block height of endLine.
1340 if (result)
1341 endYPos = line->blockHeight();
1342
mitz@apple.com40547b32008-03-18 04:04:34 +00001343 int delta = m_height - endYPos;
1344 if (delta && m_floatingObjects) {
1345 // See if any floats end in the range along which we want to shift the lines vertically.
1346 int top = min(m_height, endYPos);
1347
1348 RootInlineBox* lastLine = endLine;
1349 while (RootInlineBox* nextLine = lastLine->nextRootBox())
1350 lastLine = nextLine;
1351
1352 int bottom = lastLine->blockHeight() + abs(delta);
1353
1354 for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
mitz@apple.com93526592008-03-18 04:36:51 +00001355 if (f->m_bottom >= top && f->m_bottom < bottom)
mitz@apple.com40547b32008-03-18 04:04:34 +00001356 return false;
1357 }
1358 }
1359
mitz@apple.come1364202008-02-28 01:06:41 +00001360 // Now delete the lines that we failed to sync.
1361 RootInlineBox* boxToDelete = endLine;
1362 RenderArena* arena = renderArena();
1363 while (boxToDelete && boxToDelete != result) {
1364 repaintTop = min(repaintTop, boxToDelete->topOverflow());
1365 repaintBottom = max(repaintBottom, boxToDelete->bottomOverflow());
1366 RootInlineBox* next = boxToDelete->nextRootBox();
1367 boxToDelete->deleteLine(arena);
1368 boxToDelete = next;
hyatt0c3a9862004-02-23 21:26:26 +00001369 }
mitz@apple.come1364202008-02-28 01:06:41 +00001370
1371 endLine = result;
1372 return result;
hyatt0c3a9862004-02-23 21:26:26 +00001373 }
1374 }
mitz@apple.come1364202008-02-28 01:06:41 +00001375
hyatt0c3a9862004-02-23 21:26:26 +00001376 return false;
1377}
1378
mitz@apple.com1a301772008-03-11 18:30:36 +00001379static inline bool skipNonBreakingSpace(const BidiIterator& it)
kocienda98440082004-10-14 23:51:47 +00001380{
darinf9e5d6c2007-01-09 14:54:26 +00001381 if (it.obj->style()->nbspMode() != SPACE || it.current() != noBreakSpace)
kocienda98440082004-10-14 23:51:47 +00001382 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001383
hyattdca76e92005-11-02 08:52:50 +00001384 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1385 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001386 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001387 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1388 // |true|).
1389 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001390 return false;
mitz@apple.come1364202008-02-28 01:06:41 +00001391
kocienda498d1982004-10-15 21:07:24 +00001392 return true;
kocienda98440082004-10-14 23:51:47 +00001393}
1394
hyattd9953212005-11-03 21:05:59 +00001395static inline bool shouldCollapseWhiteSpace(const RenderStyle* style)
1396{
1397 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1398}
1399
zimmermannac3781f2007-02-04 01:25:03 +00001400static inline bool shouldPreserveNewline(RenderObject* object)
1401{
mjsd2948ef2007-02-26 19:29:04 +00001402#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001403 if (object->isSVGText())
1404 return false;
1405#endif
1406
1407 return object->style()->preserveNewline();
1408}
1409
bdakinf876bee2007-10-30 05:27:09 +00001410static bool inlineFlowRequiresLineBox(RenderObject* flow)
1411{
1412 // FIXME: Right now, we only allow line boxes for inlines that are truly empty.
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001413 // We need to fix this, though, because at the very least, inlines containing only
1414 // ignorable whitespace should should also have line boxes.
hyatt@apple.com21cc37a2008-02-26 23:17:03 +00001415 return flow->isInlineFlow() && !flow->firstChild() && flow->hasHorizontalBordersPaddingOrMargin();
bdakinf876bee2007-10-30 05:27:09 +00001416}
1417
mitz@apple.com1a301772008-03-11 18:30:36 +00001418static inline bool requiresLineBox(const BidiIterator& it)
bdashccffb432007-07-13 11:51:40 +00001419{
bdakinf876bee2007-10-30 05:27:09 +00001420 if (it.obj->isFloatingOrPositioned())
bdashccffb432007-07-13 11:51:40 +00001421 return false;
bdakinf876bee2007-10-30 05:27:09 +00001422
1423 if (it.obj->isInlineFlow() && !inlineFlowRequiresLineBox(it.obj))
1424 return false;
1425
bdashccffb432007-07-13 11:51:40 +00001426 if (!shouldCollapseWhiteSpace(it.obj->style()) || it.obj->isBR())
1427 return true;
1428
1429 UChar current = it.current();
1430 return current != ' ' && current != '\t' && current != softHyphen && (current != '\n' || shouldPreserveNewline(it.obj)) && !skipNonBreakingSpace(it);
1431}
1432
1433bool RenderBlock::generatesLineBoxesForInlineChild(RenderObject* inlineObj)
1434{
1435 ASSERT(inlineObj->parent() == this);
1436
1437 BidiIterator it(this, inlineObj, 0);
bdashccffb432007-07-13 11:51:40 +00001438 while (!it.atEnd() && !requiresLineBox(it))
mitz@apple.com1a301772008-03-11 18:30:36 +00001439 it.increment();
bdashccffb432007-07-13 11:51:40 +00001440
1441 return !it.atEnd();
1442}
1443
mitz@apple.com1a301772008-03-11 18:30:36 +00001444// FIXME: The entire concept of the skipWhitespace function is flawed, since we really need to be building
1445// line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1446// elements quite right. In other words, we need to build this function's work into the normal line
1447// object iteration process.
1448int RenderBlock::skipWhitespace(BidiIterator& iterator)
kociendabb0c24b2001-08-24 14:24:40 +00001449{
mitz@apple.com1a301772008-03-11 18:30:36 +00001450 int availableWidth = lineWidth(m_height);
1451 while (!iterator.atEnd() && !requiresLineBox(iterator)) {
1452 RenderObject* object = iterator.obj;
1453 if (object->isFloating()) {
1454 insertFloatingObject(object);
mitz@apple.come1364202008-02-28 01:06:41 +00001455 positionNewFloats();
mitz@apple.com1a301772008-03-11 18:30:36 +00001456 availableWidth = lineWidth(m_height);
1457 } else if (object->isPositioned()) {
mitz@apple.come1364202008-02-28 01:06:41 +00001458 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1459 // will work for the common cases
mitz@apple.com1a301772008-03-11 18:30:36 +00001460 RenderObject* c = object->container();
mitz@apple.come1364202008-02-28 01:06:41 +00001461 if (c->isInlineFlow()) {
1462 // A relative positioned inline encloses us. In this case, we also have to determine our
1463 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1464 // inline so that we can obtain the value later.
1465 c->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : rightOffset(m_height));
1466 c->setStaticY(m_height);
hyatt33f8d492002-11-12 21:44:52 +00001467 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001468
1469 if (object->hasStaticX()) {
1470 if (object->style()->isOriginalDisplayInlineType())
1471 object->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : width() - rightOffset(m_height));
mitz@apple.come1364202008-02-28 01:06:41 +00001472 else
mitz@apple.com1a301772008-03-11 18:30:36 +00001473 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001474 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001475
1476 if (object->hasStaticY())
1477 object->setStaticY(m_height);
hyatt33f8d492002-11-12 21:44:52 +00001478 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001479 iterator.increment();
mjs6f821c82002-03-22 00:31:57 +00001480 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001481 return availableWidth;
1482}
bdashccffb432007-07-13 11:51:40 +00001483
mitz@apple.com1a301772008-03-11 18:30:36 +00001484int RenderBlock::skipWhitespace(BidiState& iterator)
1485{
1486 int availableWidth = lineWidth(m_height);
1487 while (!iterator.position().atEnd() && !requiresLineBox(iterator.position())) {
1488 RenderObject* object = iterator.position().obj;
1489 if (object->isFloating()) {
1490 insertFloatingObject(object);
1491 positionNewFloats();
1492 availableWidth = lineWidth(m_height);
1493 } else if (object->isPositioned()) {
1494 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1495 // will work for the common cases
1496 RenderObject* c = object->container();
1497 if (c->isInlineFlow()) {
1498 // A relative positioned inline encloses us. In this case, we also have to determine our
1499 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1500 // inline so that we can obtain the value later.
1501 c->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : rightOffset(m_height));
1502 c->setStaticY(m_height);
1503 }
1504
1505 if (object->hasStaticX()) {
1506 if (object->style()->isOriginalDisplayInlineType())
1507 object->setStaticX(style()->direction() == LTR ? leftOffset(m_height) : width() - rightOffset(m_height));
1508 else
1509 object->setStaticX(style()->direction() == LTR ? borderLeft() + paddingLeft() : borderRight() + paddingRight());
1510 }
1511
1512 if (object->hasStaticY())
1513 object->setStaticY(m_height);
1514 }
1515 iterator.increment();
1516 }
1517 return availableWidth;
kociendae40cb942004-10-05 20:05:38 +00001518}
1519
bdakinf876bee2007-10-30 05:27:09 +00001520// This is currently just used for list markers and inline flows that have line boxes. Neither should
1521// have an effect on whitespace at the start of the line.
mitz@apple.com1a301772008-03-11 18:30:36 +00001522static bool shouldSkipWhitespaceAfterStartObject(RenderBlock* block, RenderObject* o)
bdakinf876bee2007-10-30 05:27:09 +00001523{
mitz@apple.com1a301772008-03-11 18:30:36 +00001524 RenderObject* next = bidiNext(block, o);
bdakinf876bee2007-10-30 05:27:09 +00001525 if (next && !next->isBR() && next->isText() && static_cast<RenderText*>(next)->textLength() > 0) {
1526 RenderText* nextText = static_cast<RenderText*>(next);
1527 UChar nextChar = nextText->characters()[0];
1528 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
mitz@apple.combfdc9112008-02-21 19:59:40 +00001529 addMidpoint(BidiIterator(0, o, 0));
bdakinf876bee2007-10-30 05:27:09 +00001530 return true;
1531 }
1532 }
1533
1534 return false;
1535}
1536
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001537void RenderBlock::fitBelowFloats(int widthToFit, int& availableWidth)
1538{
1539 ASSERT(widthToFit > availableWidth);
1540
1541 int floatBottom;
1542 int lastFloatBottom = m_height;
1543 int newLineWidth = availableWidth;
1544 while (true) {
1545 floatBottom = nextFloatBottomBelow(lastFloatBottom);
1546 if (!floatBottom)
1547 break;
1548
1549 newLineWidth = lineWidth(floatBottom);
1550 lastFloatBottom = floatBottom;
1551 if (newLineWidth >= widthToFit)
1552 break;
1553 }
1554
1555 if (newLineWidth > availableWidth) {
1556 m_height = lastFloatBottom;
1557 availableWidth = newLineWidth;
1558 }
1559}
1560
mitz@apple.com71e30842008-03-18 16:13:31 +00001561BidiIterator RenderBlock::findNextLineBreak(BidiState& start, EClear* clear)
kociendae40cb942004-10-05 20:05:38 +00001562{
mitz@apple.com1a301772008-03-11 18:30:36 +00001563 ASSERT(start.position().block == this);
mitz@apple.com51017322008-02-26 06:47:43 +00001564
mitz@apple.com1a301772008-03-11 18:30:36 +00001565 bool appliedStartWidth = start.position().pos > 0;
1566
1567 int width = skipWhitespace(start);
1568
kociendae40cb942004-10-05 20:05:38 +00001569 int w = 0;
1570 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001571
mitz@apple.com1a301772008-03-11 18:30:36 +00001572 if (start.position().atEnd())
1573 return start.position();
mjs6f821c82002-03-22 00:31:57 +00001574
hyatt33f8d492002-11-12 21:44:52 +00001575 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1576 // or not we are currently ignoring whitespace.
1577 bool ignoringSpaces = false;
hyatt98b16282004-03-31 18:43:12 +00001578 BidiIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001579
1580 // This variable tracks whether the very last character we saw was a space. We use
1581 // this to detect when we encounter a second space so we know we have to terminate
1582 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001583 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001584 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001585 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001586
mitz@apple.com1a301772008-03-11 18:30:36 +00001587 BidiIterator lBreak = start.position();
mjs6f821c82002-03-22 00:31:57 +00001588
mitz@apple.com1a301772008-03-11 18:30:36 +00001589 RenderObject *o = start.position().obj;
kociendabb0c24b2001-08-24 14:24:40 +00001590 RenderObject *last = o;
mitz@apple.com1a301772008-03-11 18:30:36 +00001591 unsigned pos = start.position().pos;
ddkilzere8759ef2007-03-25 06:28:19 +00001592 bool atStart = true;
kociendabb0c24b2001-08-24 14:24:40 +00001593
hyatt0c3a9862004-02-23 21:26:26 +00001594 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
1595 previousLineBrokeCleanly = false;
ddkilzer5d01fa22007-01-29 03:10:37 +00001596
1597 bool autoWrapWasEverTrueOnLine = false;
mitz@apple.com25beac62008-02-24 18:48:27 +00001598 bool floatsFitOnLine = true;
hyatt01eff982003-03-14 20:13:23 +00001599
hyatt@apple.com69340902008-01-16 21:24:21 +00001600 // 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 +00001601 // very specific circumstances (in order to match common WinIE renderings).
hyatt@apple.com69340902008-01-16 21:24:21 +00001602 // Not supporting the quirk has caused us to mis-render some real sites. (See Bugzilla 10517.)
1603 bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
1604
hyattb0d9f602007-01-15 01:28:23 +00001605 EWhiteSpace currWS = style()->whiteSpace();
1606 EWhiteSpace lastWS = currWS;
hyatt275d0702005-11-03 23:53:57 +00001607 while (o) {
hyattb0d9f602007-01-15 01:28:23 +00001608 currWS = o->isReplaced() ? o->parent()->style()->whiteSpace() : o->style()->whiteSpace();
1609 lastWS = last->isReplaced() ? last->parent()->style()->whiteSpace() : last->style()->whiteSpace();
1610
1611 bool autoWrap = RenderStyle::autoWrap(currWS);
ddkilzer5d01fa22007-01-29 03:10:37 +00001612 autoWrapWasEverTrueOnLine = autoWrapWasEverTrueOnLine || autoWrap;
zimmermannac3781f2007-02-04 01:25:03 +00001613
mjsd2948ef2007-02-26 19:29:04 +00001614#if ENABLE(SVG)
zimmermannac3781f2007-02-04 01:25:03 +00001615 bool preserveNewline = o->isSVGText() ? false : RenderStyle::preserveNewline(currWS);
1616#else
hyattb0d9f602007-01-15 01:28:23 +00001617 bool preserveNewline = RenderStyle::preserveNewline(currWS);
zimmermannac3781f2007-02-04 01:25:03 +00001618#endif
1619
hyattb0d9f602007-01-15 01:28:23 +00001620 bool collapseWhiteSpace = RenderStyle::collapseWhiteSpace(currWS);
1621
hyatt275d0702005-11-03 23:53:57 +00001622 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00001623 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00001624 lBreak.obj = o;
1625 lBreak.pos = 0;
mitz@apple.com1a301772008-03-11 18:30:36 +00001626 lBreak.increment();
hyatt0c3a9862004-02-23 21:26:26 +00001627
hyatt33f8d492002-11-12 21:44:52 +00001628 // A <br> always breaks a line, so don't let the line be collapsed
1629 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00001630 // get collapsed away. It only does this if the previous line broke
1631 // cleanly. Otherwise the <br> has no effect on whether the line is
1632 // empty or not.
1633 if (prevLineBrokeCleanly)
1634 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00001635 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00001636 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00001637
mitz@apple.com71e30842008-03-18 16:13:31 +00001638 if (!isLineEmpty && clear)
1639 *clear = o->style()->clear();
kociendabb0c24b2001-08-24 14:24:40 +00001640 }
1641 goto end;
1642 }
hyattb0d9f602007-01-15 01:28:23 +00001643
hyatt275d0702005-11-03 23:53:57 +00001644 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00001645 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00001646 if (o->isFloating()) {
hyatt3ac01352003-03-22 01:37:33 +00001647 insertFloatingObject(o);
hyatt33f8d492002-11-12 21:44:52 +00001648 // check if it fits in the current line.
1649 // If it does, position it now, otherwise, position
1650 // it after moving to next line (in newLine() func)
mitz@apple.com25beac62008-02-24 18:48:27 +00001651 if (floatsFitOnLine && o->width() + o->marginLeft() + o->marginRight() + w + tmpW <= width) {
hyatt33f8d492002-11-12 21:44:52 +00001652 positionNewFloats();
1653 width = lineWidth(m_height);
mitz@apple.com25beac62008-02-24 18:48:27 +00001654 } else
1655 floatsFitOnLine = false;
hyatt275d0702005-11-03 23:53:57 +00001656 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00001657 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00001658 // go ahead and determine our static x position now.
hyatt851816b2003-07-08 07:54:17 +00001659 bool isInlineType = o->style()->isOriginalDisplayInlineType();
hyatt98ee7e42003-05-14 01:39:15 +00001660 bool needToSetStaticX = o->hasStaticX();
1661 if (o->hasStaticX() && !isInlineType) {
1662 o->setStaticX(o->parent()->style()->direction() == LTR ?
hyattb0d9f602007-01-15 01:28:23 +00001663 borderLeft() + paddingLeft() :
1664 borderRight() + paddingRight());
hyatt98ee7e42003-05-14 01:39:15 +00001665 needToSetStaticX = false;
1666 }
1667
1668 // If our original display was an INLINE type, then we can go ahead
1669 // and determine our static y position now.
1670 bool needToSetStaticY = o->hasStaticY();
1671 if (o->hasStaticY() && isInlineType) {
1672 o->setStaticY(m_height);
1673 needToSetStaticY = false;
1674 }
1675
hyatt853cd7d2004-11-05 02:59:48 +00001676 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
1677 RenderObject* c = o->container();
1678 if (c->isInlineFlow() && (!needToSetStaticX || !needToSetStaticY))
1679 needToCreateLineBox = true;
1680
hyatt98ee7e42003-05-14 01:39:15 +00001681 // If we're ignoring spaces, we have to stop and include this object and
1682 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00001683 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00001684 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001685 ignoreStart.obj = o;
1686 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00001687 if (ignoringSpaces) {
hyatt98b16282004-03-31 18:43:12 +00001688 addMidpoint(ignoreStart); // Stop ignoring spaces.
1689 addMidpoint(ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00001690 }
hyatt98b16282004-03-31 18:43:12 +00001691
hyatt851816b2003-07-08 07:54:17 +00001692 }
hyatt98ee7e42003-05-14 01:39:15 +00001693 }
hyattffe78712003-02-11 01:59:29 +00001694 } else if (o->isInlineFlow()) {
bdakinf876bee2007-10-30 05:27:09 +00001695 // Right now, we should only encounter empty inlines here.
ggarenf9f32ae2007-03-26 20:08:53 +00001696 ASSERT(!o->firstChild());
bdakinf876bee2007-10-30 05:27:09 +00001697
1698 // Now that some inline flows have line boxes, if we are already ignoring spaces, we need
1699 // to make sure that we stop to include this object and then start ignoring spaces again.
1700 // If this object is at the start of the line, we need to behave like list markers and
1701 // start ignoring spaces.
1702 if (inlineFlowRequiresLineBox(o)) {
hyatt@apple.comeb66ef42008-01-18 22:59:29 +00001703 isLineEmpty = false;
bdakinf876bee2007-10-30 05:27:09 +00001704 if (ignoringSpaces) {
1705 trailingSpaceObject = 0;
1706 addMidpoint(BidiIterator(0, o, 0)); // Stop ignoring spaces.
1707 addMidpoint(BidiIterator(0, o, 0)); // Start ignoring again.
mitz@apple.com1a301772008-03-11 18:30:36 +00001708 } else if (style()->collapseWhiteSpace() && start.position().obj == o
1709 && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001710 // Like with list markers, we start ignoring spaces to make sure that any
1711 // additional spaces we see will be discarded.
1712 currentCharacterIsSpace = true;
1713 currentCharacterIsWS = true;
1714 ignoringSpaces = true;
1715 }
1716 }
1717
hyattb0d9f602007-01-15 01:28:23 +00001718 tmpW += o->marginLeft() + o->borderLeft() + o->paddingLeft() +
1719 o->marginRight() + o->borderRight() + o->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00001720 } else if (o->isReplaced()) {
hyattde396342003-10-29 08:57:20 +00001721 // Break on replaced elements if either has normal white-space.
hyatt@apple.com69340902008-01-16 21:24:21 +00001722 if ((autoWrap || RenderStyle::autoWrap(lastWS)) && (!o->isImage() || allowImagesToBreak)) {
hyatt711fe232002-11-20 21:25:14 +00001723 w += tmpW;
1724 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00001725 lBreak.obj = o;
1726 lBreak.pos = 0;
hyatt711fe232002-11-20 21:25:14 +00001727 }
1728
mitz@apple.combfdc9112008-02-21 19:59:40 +00001729 if (ignoringSpaces)
1730 addMidpoint(BidiIterator(0, o, 0));
1731
hyatt33f8d492002-11-12 21:44:52 +00001732 isLineEmpty = false;
1733 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00001734 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001735 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001736 trailingSpaceObject = 0;
hyatte85e4a72002-12-08 02:06:16 +00001737
bdakinf876bee2007-10-30 05:27:09 +00001738 // Optimize for a common case. If we can't find whitespace after the list
1739 // item, then this is all moot. -dwh
mjsd26b2972007-02-13 13:09:04 +00001740 if (o->isListMarker() && !static_cast<RenderListMarker*>(o)->isInside()) {
mitz@apple.com1a301772008-03-11 18:30:36 +00001741 if (style()->collapseWhiteSpace() && shouldSkipWhitespaceAfterStartObject(this, o)) {
bdakinf876bee2007-10-30 05:27:09 +00001742 // Like with inline flows, we start ignoring spaces to make sure that any
1743 // additional spaces we see will be discarded.
1744 currentCharacterIsSpace = true;
1745 currentCharacterIsWS = true;
1746 ignoringSpaces = true;
hyatte85e4a72002-12-08 02:06:16 +00001747 }
justing244d3a32006-04-13 01:31:24 +00001748 } else
hyattb0d9f602007-01-15 01:28:23 +00001749 tmpW += o->width() + o->marginLeft() + o->marginRight() + inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00001750 } else if (o->isText()) {
mitz@apple.com51017322008-02-26 06:47:43 +00001751 if (!pos)
1752 appliedStartWidth = false;
1753
weinigf28a1c32007-02-14 14:10:31 +00001754 RenderText* t = static_cast<RenderText*>(o);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001755
darin42563ac52007-01-22 17:28:57 +00001756 int strlen = t->textLength();
hyatt33f8d492002-11-12 21:44:52 +00001757 int len = strlen - pos;
darin42563ac52007-01-22 17:28:57 +00001758 const UChar* str = t->characters();
kociendabb0c24b2001-08-24 14:24:40 +00001759
darin42563ac52007-01-22 17:28:57 +00001760 const Font& f = t->style(m_firstLine)->font();
weinigf28a1c32007-02-14 14:10:31 +00001761
hyatt33f8d492002-11-12 21:44:52 +00001762 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00001763 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00001764 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00001765
darin54008922006-01-13 16:39:05 +00001766 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
weiniged111c12007-07-13 22:45:11 +00001767 int charWidth = 0;
darin47ece0d2005-09-04 07:42:31 +00001768 int nextBreakable = -1;
weinigf28a1c32007-02-14 14:10:31 +00001769 bool breakNBSP = autoWrap && o->style()->nbspMode() == SPACE;
1770 // Auto-wrapping text should wrap in the middle of a word only if it could not wrap before the word,
1771 // 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 +00001772 bool breakWords = o->style()->breakWords() && ((autoWrap && !w) || currWS == PRE);
weinigf28a1c32007-02-14 14:10:31 +00001773 bool midWordBreak = false;
hyattea474f72007-04-20 05:02:19 +00001774 bool breakAll = o->style()->wordBreak() == BreakAllWordBreak && autoWrap;
1775
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001776 if (t->isWordBreak()) {
1777 w += tmpW;
1778 tmpW = 0;
1779 lBreak.obj = o;
1780 lBreak.pos = 0;
1781 ASSERT(!len);
1782 }
1783
hyatt275d0702005-11-03 23:53:57 +00001784 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00001785 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00001786 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00001787 UChar c = str[pos];
hyattb0d9f602007-01-15 01:28:23 +00001788 currentCharacterIsSpace = c == ' ' || c == '\t' || (!preserveNewline && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00001789
hyattb0d9f602007-01-15 01:28:23 +00001790 if (!collapseWhiteSpace || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001791 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00001792
hyatt78b85132004-03-29 20:07:45 +00001793 // Check for soft hyphens. Go ahead and ignore them.
darin42563ac52007-01-22 17:28:57 +00001794 if (c == softHyphen) {
hyatt78b85132004-03-29 20:07:45 +00001795 if (!ignoringSpaces) {
1796 // Ignore soft hyphens
mitz@apple.combe429562008-03-07 01:09:51 +00001797 BidiIterator beforeSoftHyphen;
1798 if (pos)
1799 beforeSoftHyphen = BidiIterator(0, o, pos - 1);
bdakin9151ba52005-10-24 22:51:06 +00001800 else
mitz@apple.com1a301772008-03-11 18:30:36 +00001801 beforeSoftHyphen = BidiIterator(0, last, last->isText() ? static_cast<RenderText*>(last)->textLength() - 1 : 0);
bdakin9151ba52005-10-24 22:51:06 +00001802 // Two consecutive soft hyphens. Avoid overlapping midpoints.
mitz@apple.combe429562008-03-07 01:09:51 +00001803 if (sNumMidpoints && smidpoints->at(sNumMidpoints - 1).obj == o && smidpoints->at(sNumMidpoints - 1).pos == pos)
bdakin9151ba52005-10-24 22:51:06 +00001804 sNumMidpoints--;
1805 else
mitz@apple.combe429562008-03-07 01:09:51 +00001806 addMidpoint(beforeSoftHyphen);
1807
hyatt78b85132004-03-29 20:07:45 +00001808 // Add the width up to but not including the hyphen.
weinigf28a1c32007-02-14 14:10:31 +00001809 tmpW += t->width(lastSpace, pos - lastSpace, f, w + tmpW) + lastSpaceWordSpacing;
mitz@apple.combe429562008-03-07 01:09:51 +00001810
hyattdca76e92005-11-02 08:52:50 +00001811 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00001812 // on the line if it shows when we break.
hyattb0d9f602007-01-15 01:28:23 +00001813 if (autoWrap)
weinigf28a1c32007-02-14 14:10:31 +00001814 tmpW += t->width(pos, 1, f, w + tmpW);
mitz@apple.combe429562008-03-07 01:09:51 +00001815
1816 BidiIterator afterSoftHyphen(0, o, pos);
mitz@apple.com1a301772008-03-11 18:30:36 +00001817 afterSoftHyphen.increment();
mitz@apple.combe429562008-03-07 01:09:51 +00001818 addMidpoint(afterSoftHyphen);
hyatt78b85132004-03-29 20:07:45 +00001819 }
mitz@apple.combe429562008-03-07 01:09:51 +00001820
hyatt78b85132004-03-29 20:07:45 +00001821 pos++;
1822 len--;
eseideld13fe532005-11-30 02:40:29 +00001823 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00001824 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
1825 continue;
1826 }
1827
hyatt3aff2332003-01-23 01:15:28 +00001828 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00001829
darinf9e5d6c2007-01-09 14:54:26 +00001830 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == noBreakSpace);
harrisone343c412005-01-18 01:07:26 +00001831
weiniged111c12007-07-13 22:45:11 +00001832 if ((breakAll || breakWords) && !midWordBreak) {
1833 wrapW += charWidth;
1834 charWidth = t->width(pos, 1, f, w + wrapW);
1835 midWordBreak = w + wrapW + charWidth > width;
weinigf28a1c32007-02-14 14:10:31 +00001836 }
darin47ece0d2005-09-04 07:42:31 +00001837
ddkilzere8759ef2007-03-25 06:28:19 +00001838 bool betweenWords = c == '\n' || (currWS != PRE && !atStart && isBreakable(str, pos, strlen, nextBreakable, breakNBSP));
hyattea474f72007-04-20 05:02:19 +00001839
weiniged111c12007-07-13 22:45:11 +00001840 if (betweenWords || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00001841 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00001842 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00001843 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001844 // Stop ignoring spaces and begin at this
1845 // new point.
hyatt48710d62003-08-21 09:17:13 +00001846 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001847 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00001848 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001849 addMidpoint(BidiIterator(0, o, pos));
hyatt0c05e102006-04-14 08:15:00 +00001850 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00001851 } else {
hyatt33f8d492002-11-12 21:44:52 +00001852 // Just keep ignoring these spaces.
1853 pos++;
1854 len--;
1855 continue;
1856 }
1857 }
rjwc9c257d2003-01-24 03:46:17 +00001858
darin54008922006-01-13 16:39:05 +00001859 int additionalTmpW = t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
1860 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00001861 if (!appliedStartWidth) {
1862 tmpW += inlineWidth(o, true, false);
1863 appliedStartWidth = true;
1864 }
1865
eseideld13fe532005-11-30 02:40:29 +00001866 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00001867
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00001868 if (!w && autoWrap && tmpW > width)
1869 fitBelowFloats(tmpW, width);
1870
hyattb0d9f602007-01-15 01:28:23 +00001871 if (autoWrap || breakWords) {
hyattdca76e92005-11-02 08:52:50 +00001872 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00001873 // as candidate width for this line.
ap932806a2006-10-01 09:06:09 +00001874 bool lineWasTooWide = false;
1875 if (w + tmpW <= width && currentCharacterIsWS && o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
1876 int charWidth = t->width(pos, 1, f, w + tmpW) + (applyWordSpacing ? wordSpacing : 0);
1877 // Check if line is too big even without the extra space
1878 // at the end of the line. If it is not, do nothing.
1879 // If the line needs the extra whitespace to be too long,
1880 // then move the line break to the space and skip all
1881 // additional whitespace.
1882 if (w + tmpW + charWidth > width) {
1883 lineWasTooWide = true;
1884 lBreak.obj = o;
1885 lBreak.pos = pos;
mitz@apple.com1a301772008-03-11 18:30:36 +00001886 skipWhitespace(lBreak);
kocienda9dbe9b12004-10-22 20:07:05 +00001887 }
ap932806a2006-10-01 09:06:09 +00001888 }
1889 if (lineWasTooWide || w + tmpW > width) {
mitz@apple.com3d3d92b2008-02-21 05:00:10 +00001890 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 +00001891 if (!stoppedIgnoringSpaces && pos > 0) {
1892 // We need to stop right before the newline and then start up again.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001893 addMidpoint(BidiIterator(0, o, pos - 1)); // Stop
hyatt0c05e102006-04-14 08:15:00 +00001894 addMidpoint(BidiIterator(0, o, pos)); // Start
1895 }
mitz@apple.com1a301772008-03-11 18:30:36 +00001896 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001897 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00001898 }
hyatt78b85132004-03-29 20:07:45 +00001899 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00001900 } else {
weiniged111c12007-07-13 22:45:11 +00001901 if (!betweenWords || (midWordBreak && !autoWrap))
darin54008922006-01-13 16:39:05 +00001902 tmpW -= additionalTmpW;
darin42563ac52007-01-22 17:28:57 +00001903 if (pos > 0 && str[pos-1] == softHyphen)
darin54008922006-01-13 16:39:05 +00001904 // Subtract the width of the soft hyphen out since we fit on a line.
1905 tmpW -= t->width(pos-1, 1, f, w+tmpW);
1906 }
rjwc9c257d2003-01-24 03:46:17 +00001907 }
hyatt33f8d492002-11-12 21:44:52 +00001908
hyattb0d9f602007-01-15 01:28:23 +00001909 if (c == '\n' && preserveNewline) {
hyatt0c05e102006-04-14 08:15:00 +00001910 if (!stoppedIgnoringSpaces && pos > 0) {
1911 // We need to stop right before the newline and then start up again.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001912 addMidpoint(BidiIterator(0, o, pos - 1)); // Stop
hyatt0c05e102006-04-14 08:15:00 +00001913 addMidpoint(BidiIterator(0, o, pos)); // Start
1914 }
hyatta9f48e32003-02-03 22:48:01 +00001915 lBreak.obj = o;
1916 lBreak.pos = pos;
mitz@apple.com1a301772008-03-11 18:30:36 +00001917 lBreak.increment();
adele7fc3e832006-02-17 09:31:35 +00001918 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00001919 return lBreak;
1920 }
hyatta9f48e32003-02-03 22:48:01 +00001921
weinigf28a1c32007-02-14 14:10:31 +00001922 if (autoWrap && betweenWords) {
hyatta9f48e32003-02-03 22:48:01 +00001923 w += tmpW;
weiniged111c12007-07-13 22:45:11 +00001924 wrapW = 0;
hyatta9f48e32003-02-03 22:48:01 +00001925 tmpW = 0;
1926 lBreak.obj = o;
1927 lBreak.pos = pos;
weinigf28a1c32007-02-14 14:10:31 +00001928 // Auto-wrapping text should not wrap in the middle of a word once it has had an
1929 // opportunity to break after a word.
1930 breakWords = false;
hyatta9f48e32003-02-03 22:48:01 +00001931 }
hyatt33f8d492002-11-12 21:44:52 +00001932
darin54008922006-01-13 16:39:05 +00001933 if (midWordBreak) {
1934 // Remember this as a breakable position in case
1935 // adding the end width forces a break.
1936 lBreak.obj = o;
1937 lBreak.pos = pos;
weiniged111c12007-07-13 22:45:11 +00001938 midWordBreak &= (breakWords || breakAll);
1939 }
1940
1941 if (betweenWords) {
darin54008922006-01-13 16:39:05 +00001942 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
1943 lastSpace = pos;
1944 }
hyatt33f8d492002-11-12 21:44:52 +00001945
hyattdca76e92005-11-02 08:52:50 +00001946 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00001947 // If we encounter a newline, or if we encounter a
1948 // second space, we need to go ahead and break up this
1949 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00001950 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00001951 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00001952
hyatt33f8d492002-11-12 21:44:52 +00001953 // We just entered a mode where we are ignoring
1954 // spaces. Create a midpoint to terminate the run
1955 // before the second space.
hyatt98b16282004-03-31 18:43:12 +00001956 addMidpoint(ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00001957 }
1958 }
eseidel789896f2005-11-27 22:52:09 +00001959 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00001960 // Stop ignoring spaces and begin at this
1961 // new point.
1962 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00001963 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00001964 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mitz@apple.combfdc9112008-02-21 19:59:40 +00001965 addMidpoint(BidiIterator(0, o, pos));
hyatt33f8d492002-11-12 21:44:52 +00001966 }
hyatt98b16282004-03-31 18:43:12 +00001967
1968 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
1969 ignoreStart.obj = o;
1970 ignoreStart.pos = pos;
1971 }
harrisone343c412005-01-18 01:07:26 +00001972
1973 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattb0d9f602007-01-15 01:28:23 +00001974 if (autoWrap && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00001975 lBreak.obj = o;
1976 lBreak.pos = pos;
1977 }
1978 }
hyatt33f8d492002-11-12 21:44:52 +00001979
hyattb0d9f602007-01-15 01:28:23 +00001980 if (collapseWhiteSpace && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00001981 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00001982 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00001983 trailingSpaceObject = 0;
1984
1985 pos++;
1986 len--;
ddkilzere8759ef2007-03-25 06:28:19 +00001987 atStart = false;
hyatt33f8d492002-11-12 21:44:52 +00001988 }
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00001989
kociendabb0c24b2001-08-24 14:24:40 +00001990 // IMPORTANT: pos is > length here!
hyatt33f8d492002-11-12 21:44:52 +00001991 if (!ignoringSpaces)
eseideld13fe532005-11-30 02:40:29 +00001992 tmpW += t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
weinigf28a1c32007-02-14 14:10:31 +00001993 tmpW += inlineWidth(o, !appliedStartWidth, true);
kociendabb0c24b2001-08-24 14:24:40 +00001994 } else
weinigf28a1c32007-02-14 14:10:31 +00001995 ASSERT_NOT_REACHED();
kociendabb0c24b2001-08-24 14:24:40 +00001996
mitz@apple.com1a301772008-03-11 18:30:36 +00001997 RenderObject* next = bidiNext(this, o);
hyattdca76e92005-11-02 08:52:50 +00001998 bool checkForBreak = autoWrap;
hyattb0d9f602007-01-15 01:28:23 +00001999 if (w && w + tmpW > width && lBreak.obj && currWS == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002000 checkForBreak = true;
2001 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00002002 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002003 if (currentCharacterIsSpace)
2004 checkForBreak = true;
2005 else {
harrison07b5e582005-08-15 23:31:16 +00002006 checkForBreak = false;
hyatta9f48e32003-02-03 22:48:01 +00002007 RenderText* nextText = static_cast<RenderText*>(next);
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002008 if (nextText->textLength()) {
darin42563ac52007-01-22 17:28:57 +00002009 UChar c = nextText->characters()[0];
zimmermannac3781f2007-02-04 01:25:03 +00002010 if (c == ' ' || c == '\t' || (c == '\n' && !shouldPreserveNewline(next)))
eseideld13fe532005-11-30 02:40:29 +00002011 // If the next item on the line is text, and if we did not end with
2012 // a space, then the next text run continues our word (and so it needs to
2013 // keep adding to |tmpW|. Just update and continue.
2014 checkForBreak = true;
mitz@apple.comfb8da4e2008-02-19 21:13:19 +00002015 } else if (nextText->isWordBreak())
2016 checkForBreak = true;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002017 bool willFitOnLine = w + tmpW <= width;
2018 if (!willFitOnLine && !w) {
2019 fitBelowFloats(tmpW, width);
2020 willFitOnLine = tmpW <= width;
2021 }
ddkilzere8759ef2007-03-25 06:28:19 +00002022 bool canPlaceOnLine = willFitOnLine || !autoWrapWasEverTrueOnLine;
hyatta9f48e32003-02-03 22:48:01 +00002023 if (canPlaceOnLine && checkForBreak) {
2024 w += tmpW;
2025 tmpW = 0;
2026 lBreak.obj = next;
2027 lBreak.pos = 0;
2028 }
2029 }
2030 }
2031 }
2032
darin54008922006-01-13 16:39:05 +00002033 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002034 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002035 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002036 trailingSpaceObject = 0;
mitz@apple.comcfd9b7b2008-02-24 04:00:21 +00002037
2038 if (w)
2039 goto end;
2040
2041 fitBelowFloats(tmpW, width);
hyattf14a4a32002-11-21 22:06:32 +00002042
hyatta14d1742003-01-02 20:25:46 +00002043 // |width| may have been adjusted because we got shoved down past a float (thus
2044 // giving us more room), so we need to retest, and only jump to
2045 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002046 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002047 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002048 }
hyatt1d9e29b2003-04-10 01:48:53 +00002049
mitz@apple.com1a301772008-03-11 18:30:36 +00002050 if (!o->isFloatingOrPositioned()) {
2051 last = o;
2052 if (last->isReplaced() && autoWrap && (!last->isImage() || allowImagesToBreak) && (!last->isListMarker() || static_cast<RenderListMarker*>(last)->isInside())) {
2053 w += tmpW;
2054 tmpW = 0;
2055 lBreak.obj = next;
2056 lBreak.pos = 0;
2057 }
hyatt711fe232002-11-20 21:25:14 +00002058 }
2059
mitz@apple.com1a301772008-03-11 18:30:36 +00002060 o = next;
2061
hyatta9f48e32003-02-03 22:48:01 +00002062 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2063 // with adjacent inline normal/nowrap spans.
hyattb0d9f602007-01-15 01:28:23 +00002064 if (!collapseWhiteSpace)
hyatta9f48e32003-02-03 22:48:01 +00002065 currentCharacterIsSpace = false;
2066
kociendabb0c24b2001-08-24 14:24:40 +00002067 pos = 0;
ddkilzere8759ef2007-03-25 06:28:19 +00002068 atStart = false;
kociendabb0c24b2001-08-24 14:24:40 +00002069 }
2070
hyattb0d9f602007-01-15 01:28:23 +00002071
2072 if (w + tmpW <= width || lastWS == NOWRAP) {
kociendabb0c24b2001-08-24 14:24:40 +00002073 lBreak.obj = 0;
2074 lBreak.pos = 0;
2075 }
2076
2077 end:
rjwc9c257d2003-01-24 03:46:17 +00002078
mitz@apple.com1a301772008-03-11 18:30:36 +00002079 if (lBreak == start.position() && !lBreak.obj->isBR()) {
kociendabb0c24b2001-08-24 14:24:40 +00002080 // we just add as much as possible
weinig98726da2007-03-15 01:22:19 +00002081 if (style()->whiteSpace() == PRE) {
hyattdca76e92005-11-02 08:52:50 +00002082 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002083 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002084 lBreak.obj = o;
2085 lBreak.pos = pos - 1;
2086 } else {
2087 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002088 lBreak.pos = last->isText() ? last->length() : 0;
kociendabb0c24b2001-08-24 14:24:40 +00002089 }
hyatt275d0702005-11-03 23:53:57 +00002090 } else if (lBreak.obj) {
2091 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002092 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002093 lBreak.obj = o;
2094 lBreak.pos = 0;
2095 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002096 // Don't ever break in the middle of a word if we can help it.
2097 // There's no room at all. We just have to be on this line,
2098 // even though we'll spill out.
2099 lBreak.obj = o;
2100 lBreak.pos = pos;
hyatt33f8d492002-11-12 21:44:52 +00002101 }
kociendabb0c24b2001-08-24 14:24:40 +00002102 }
2103 }
2104
2105 // make sure we consume at least one char/object.
mitz@apple.com1a301772008-03-11 18:30:36 +00002106 if (lBreak == start.position())
2107 lBreak.increment();
hyatt33f8d492002-11-12 21:44:52 +00002108
hyattfe99c872003-07-31 22:25:29 +00002109 // Sanity check our midpoints.
mitz@apple.com1a301772008-03-11 18:30:36 +00002110 checkMidpoints(lBreak);
hyattfe99c872003-07-31 22:25:29 +00002111
hyatt33f8d492002-11-12 21:44:52 +00002112 if (trailingSpaceObject) {
2113 // This object is either going to be part of the last midpoint, or it is going
2114 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2115 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt85586af2003-02-19 23:22:42 +00002116 if (sNumMidpoints%2==1) {
2117 BidiIterator* midpoints = smidpoints->data();
2118 midpoints[sNumMidpoints-1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002119 }
2120 //else if (lBreak.pos > 0)
2121 // lBreak.pos--;
2122 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002123 // Add a new end midpoint that stops right at the very end.
hyattd20075d2002-11-16 02:23:32 +00002124 RenderText* text = static_cast<RenderText *>(trailingSpaceObject);
darin42563ac52007-01-22 17:28:57 +00002125 unsigned length = text->textLength();
2126 unsigned pos = length >= 2 ? length - 2 : UINT_MAX;
eseidel789896f2005-11-27 22:52:09 +00002127 BidiIterator endMid(0, trailingSpaceObject, pos);
hyatt85586af2003-02-19 23:22:42 +00002128 addMidpoint(endMid);
hyatt33f8d492002-11-12 21:44:52 +00002129 }
2130 }
rjwc9c257d2003-01-24 03:46:17 +00002131
mjs54b64002003-04-02 02:59:02 +00002132 // We might have made lBreak an iterator that points past the end
2133 // of the object. Do this adjustment to make it point to the start
2134 // of the next object instead to avoid confusing the rest of the
2135 // code.
2136 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002137 lBreak.pos--;
mitz@apple.com1a301772008-03-11 18:30:36 +00002138 lBreak.increment();
mjs54b64002003-04-02 02:59:02 +00002139 }
2140
hyatt78b85132004-03-29 20:07:45 +00002141 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2142 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2143 // ignore the hyphen so that it will render at the end of the line.
darin42563ac52007-01-22 17:28:57 +00002144 UChar c = static_cast<RenderText*>(lBreak.obj)->characters()[lBreak.pos-1];
2145 if (c == softHyphen)
hyatt78b85132004-03-29 20:07:45 +00002146 chopMidpointsAt(lBreak.obj, lBreak.pos-2);
2147 }
2148
kociendabb0c24b2001-08-24 14:24:40 +00002149 return lBreak;
2150}
2151
hyattb4b20872004-10-20 21:34:01 +00002152void RenderBlock::checkLinesForOverflow()
2153{
hyattb4b20872004-10-20 21:34:01 +00002154 m_overflowWidth = m_width;
2155 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
darin7bd70952006-04-13 07:07:34 +00002156 m_overflowLeft = min(curr->leftOverflow(), m_overflowLeft);
2157 m_overflowTop = min(curr->topOverflow(), m_overflowTop);
2158 m_overflowWidth = max(curr->rightOverflow(), m_overflowWidth);
2159 m_overflowHeight = max(curr->bottomOverflow(), m_overflowHeight);
hyattb4b20872004-10-20 21:34:01 +00002160 }
2161}
2162
hyatted77ad82004-06-15 07:21:23 +00002163void RenderBlock::deleteEllipsisLineBoxes()
2164{
hyatted77ad82004-06-15 07:21:23 +00002165 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002166 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002167}
2168
2169void RenderBlock::checkLinesForTextOverflow()
2170{
2171 // Determine the width of the ellipsis using the current font.
darindbba2bb2007-01-11 12:23:49 +00002172 // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if horizontal ellipsis is "not renderable"
2173 TextRun ellipsisRun(&horizontalEllipsis, 1);
2174 static AtomicString ellipsisStr(&horizontalEllipsis, 1);
hyatt3e99d1c2006-02-24 21:13:08 +00002175 const Font& firstLineFont = firstLineStyle()->font();
2176 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002177 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2178 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002179
2180 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2181 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2182 // check the left edge of the line box to see if it is less
2183 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2184 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002185 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyattda77c4b2004-06-17 18:09:49 +00002186 int blockEdge = ltr ? rightOffset(curr->yPos()) : leftOffset(curr->yPos());
hyatt1a8d2512004-06-17 01:38:30 +00002187 int lineBoxEdge = ltr ? curr->xPos() + curr->width() : curr->xPos();
hyatted77ad82004-06-15 07:21:23 +00002188 if ((ltr && lineBoxEdge > blockEdge) || (!ltr && lineBoxEdge < blockEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002189 // 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 +00002190 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2191 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2192 // space.
2193 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
hyatt1a8d2512004-06-17 01:38:30 +00002194 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
2195 curr->placeEllipsis(ellipsisStr, ltr, blockEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002196 }
2197 }
2198}
2199
hyattffe78712003-02-11 01:59:29 +00002200}