blob: 48ffd9dbd5c430dd4f710728d34c6890e4ced930 [file] [log] [blame]
kociendabb0c24b2001-08-24 14:24:40 +00001/**
2 * This file is part of the html renderer for KDE.
3 *
4 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
darin7ab31092006-05-10 04:59:57 +00005 * Copyright (C) 2004, 2006 Apple Computer, Inc.
kociendabb0c24b2001-08-24 14:24:40 +00006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
kociendabb0c24b2001-08-24 14:24:40 +000022 */
darinbe4c67d2005-12-19 19:53:12 +000023
mjsb64c50a2005-10-03 21:13:12 +000024#include "config.h"
kociendabb0c24b2001-08-24 14:24:40 +000025#include "bidi.h"
darin36d11362006-04-11 16:30:21 +000026
darinb9481ed2006-03-20 02:57:59 +000027#include "Document.h"
eseidel40eb1b92006-03-25 22:20:36 +000028#include "Element.h"
mjsd4145d12006-01-11 09:36:47 +000029#include "FrameView.h"
eseidel3a6d1322006-01-09 03:14:50 +000030#include "InlineTextBox.h"
darin36d11362006-04-11 16:30:21 +000031#include "RenderArena.h"
hyattd8048342006-05-31 01:48:18 +000032#include "RenderView.h"
darin36d11362006-04-11 16:30:21 +000033#include "break_lines.h"
mjsbb863512006-05-09 09:27:55 +000034#include <wtf/AlwaysInline.h>
hyatt8c371e52004-06-16 01:19:26 +000035
darin7bd70952006-04-13 07:07:34 +000036using namespace std;
37
darinb9481ed2006-03-20 02:57:59 +000038namespace WebCore {
mjsfe301d72003-10-02 18:46:18 +000039
hyatt275d0702005-11-03 23:53:57 +000040// an iterator which traverses all the objects within a block
darin7ab31092006-05-10 04:59:57 +000041struct BidiIterator {
hyatt275d0702005-11-03 23:53:57 +000042 BidiIterator() : block(0), obj(0), pos(0) {}
43 BidiIterator(RenderBlock* b, RenderObject* o, unsigned int p)
darin7ab31092006-05-10 04:59:57 +000044 : block(b), obj(o), pos(p) {}
hyattffe78712003-02-11 01:59:29 +000045
hyatt275d0702005-11-03 23:53:57 +000046 void increment(BidiState& state);
mjsfe301d72003-10-02 18:46:18 +000047 bool atEnd() const;
48
darin7ab31092006-05-10 04:59:57 +000049 UChar current() const;
50 UCharDirection direction() const;
hyatt275d0702005-11-03 23:53:57 +000051
52 RenderBlock* block;
53 RenderObject* obj;
mjsfe301d72003-10-02 18:46:18 +000054 unsigned int pos;
55};
56
mjsfe301d72003-10-02 18:46:18 +000057struct BidiState {
darin7ab31092006-05-10 04:59:57 +000058 BidiState() : context(0), dir(U_OTHER_NEUTRAL), adjustEmbedding(false), reachedEndOfLine(false) {}
mjsfe301d72003-10-02 18:46:18 +000059
60 BidiIterator sor;
61 BidiIterator eor;
62 BidiIterator last;
63 BidiIterator current;
mjsbb3d15c2005-12-01 10:32:32 +000064 RefPtr<BidiContext> context;
mjsfe301d72003-10-02 18:46:18 +000065 BidiStatus status;
darin7ab31092006-05-10 04:59:57 +000066 UCharDirection dir;
hyatt275d0702005-11-03 23:53:57 +000067 bool adjustEmbedding;
eseidel789896f2005-11-27 22:52:09 +000068 BidiIterator endOfLine;
69 bool reachedEndOfLine;
darine4fa9e22005-12-16 18:18:50 +000070 BidiIterator lastBeforeET;
mjsfe301d72003-10-02 18:46:18 +000071};
hyatt2f1e7102003-02-20 01:27:03 +000072
eseidel789896f2005-11-27 22:52:09 +000073inline bool operator==(const BidiStatus& status1, const BidiStatus& status2)
74{
75 return status1.eor == status2.eor && status1.last == status2.last && status1.lastStrong == status2.lastStrong;
76}
77
78inline bool operator!=(const BidiStatus& status1, const BidiStatus& status2)
79{
80 return !(status1 == status2);
81}
82
hyatt2f1e7102003-02-20 01:27:03 +000083// Used to track a list of chained bidi runs.
mjsfe301d72003-10-02 18:46:18 +000084static BidiRun* sFirstBidiRun;
85static BidiRun* sLastBidiRun;
86static int sBidiRunCount;
87static BidiRun* sCompactFirstBidiRun;
88static BidiRun* sCompactLastBidiRun;
89static int sCompactBidiRunCount;
90static bool sBuildingCompactRuns;
hyatt85586af2003-02-19 23:22:42 +000091
92// Midpoint globals. The goal is not to do any allocation when dealing with
93// these midpoints, so we just keep an array around and never clear it. We track
94// the number of items and position using the two other variables.
darinb9481ed2006-03-20 02:57:59 +000095static DeprecatedArray<BidiIterator> *smidpoints;
96static unsigned sNumMidpoints;
97static unsigned sCurrMidpoint;
mjsfe301d72003-10-02 18:46:18 +000098static bool betweenMidpoints;
hyatt85586af2003-02-19 23:22:42 +000099
hyatt33f8d492002-11-12 21:44:52 +0000100static bool isLineEmpty = true;
hyatt0c3a9862004-02-23 21:26:26 +0000101static bool previousLineBrokeCleanly = true;
mjs6f821c82002-03-22 00:31:57 +0000102static bool emptyRun = true;
darinb70665a2002-04-15 23:43:21 +0000103static int numSpaces;
mjs6f821c82002-03-22 00:31:57 +0000104
darin7ab31092006-05-10 04:59:57 +0000105static void embed(UCharDirection, BidiState&);
106static void appendRun(BidiState&);
mjs6f821c82002-03-22 00:31:57 +0000107
hyattffe78712003-02-11 01:59:29 +0000108static int getBPMWidth(int childValue, Length cssUnit)
109{
hyatt275d0702005-11-03 23:53:57 +0000110 if (!cssUnit.isIntrinsicOrAuto())
darin947a31b2006-02-24 03:08:41 +0000111 return (cssUnit.isFixed() ? cssUnit.value() : childValue);
hyattffe78712003-02-11 01:59:29 +0000112 return 0;
113}
114
115static int getBorderPaddingMargin(RenderObject* child, bool endOfInline)
116{
117 RenderStyle* cstyle = child->style();
118 int result = 0;
119 bool leftSide = (cstyle->direction() == LTR) ? !endOfInline : endOfInline;
120 result += getBPMWidth((leftSide ? child->marginLeft() : child->marginRight()),
121 (leftSide ? cstyle->marginLeft() :
122 cstyle->marginRight()));
123 result += getBPMWidth((leftSide ? child->paddingLeft() : child->paddingRight()),
124 (leftSide ? cstyle->paddingLeft() :
125 cstyle->paddingRight()));
126 result += leftSide ? child->borderLeft() : child->borderRight();
127 return result;
128}
129
130static int inlineWidth(RenderObject* child, bool start = true, bool end = true)
131{
132 int extraWidth = 0;
133 RenderObject* parent = child->parent();
hyatt450813d2003-07-25 20:22:34 +0000134 while (parent->isInline() && !parent->isInlineBlockOrInlineTable()) {
hyattffe78712003-02-11 01:59:29 +0000135 if (start && parent->firstChild() == child)
136 extraWidth += getBorderPaddingMargin(parent, false);
137 if (end && parent->lastChild() == child)
138 extraWidth += getBorderPaddingMargin(parent, true);
139 child = parent;
140 parent = child->parent();
141 }
142 return extraWidth;
143}
144
darin35355e52002-12-20 09:19:00 +0000145#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000146static bool inBidiRunDestroy;
hyattffe78712003-02-11 01:59:29 +0000147#endif
148
harrisone8363b42005-10-06 00:54:06 +0000149void BidiRun::destroy(RenderArena* renderArena)
hyattffe78712003-02-11 01:59:29 +0000150{
151#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000152 inBidiRunDestroy = true;
hyattffe78712003-02-11 01:59:29 +0000153#endif
154 delete this;
155#ifndef NDEBUG
harrison0012ced2005-10-06 18:37:42 +0000156 inBidiRunDestroy = false;
hyattffe78712003-02-11 01:59:29 +0000157#endif
158
159 // Recover the size left there for us by operator delete and free the memory.
160 renderArena->free(*(size_t *)this, this);
161}
162
163void* BidiRun::operator new(size_t sz, RenderArena* renderArena) throw()
164{
165 return renderArena->allocate(sz);
166}
167
168void BidiRun::operator delete(void* ptr, size_t sz)
169{
harrison0012ced2005-10-06 18:37:42 +0000170 assert(inBidiRunDestroy);
hyattffe78712003-02-11 01:59:29 +0000171
harrisone8363b42005-10-06 00:54:06 +0000172 // Stash size where destroy() can find it.
hyattffe78712003-02-11 01:59:29 +0000173 *(size_t*)ptr = sz;
174}
175
176static void deleteBidiRuns(RenderArena* arena)
177{
darinef0c09f2005-10-09 01:46:17 +0000178 emptyRun = true;
hyatt2f1e7102003-02-20 01:27:03 +0000179 if (!sFirstBidiRun)
hyattffe78712003-02-11 01:59:29 +0000180 return;
181
hyatt2f1e7102003-02-20 01:27:03 +0000182 BidiRun* curr = sFirstBidiRun;
183 while (curr) {
184 BidiRun* s = curr->nextRun;
harrisone8363b42005-10-06 00:54:06 +0000185 curr->destroy(arena);
hyatt2f1e7102003-02-20 01:27:03 +0000186 curr = s;
hyattffe78712003-02-11 01:59:29 +0000187 }
hyatt2f1e7102003-02-20 01:27:03 +0000188
189 sFirstBidiRun = 0;
190 sLastBidiRun = 0;
191 sBidiRunCount = 0;
hyattffe78712003-02-11 01:59:29 +0000192}
193
kociendabb0c24b2001-08-24 14:24:40 +0000194// ---------------------------------------------------------------------
195
196/* a small helper class used internally to resolve Bidi embedding levels.
197 Each line of text caches the embedding level at the start of the line for faster
198 relayouting
199*/
darin7ab31092006-05-10 04:59:57 +0000200BidiContext::BidiContext(unsigned char l, UCharDirection e, BidiContext *p, bool o)
mjs08191d52006-03-06 02:53:41 +0000201 : level(l), override(o), m_dir(e)
kociendabb0c24b2001-08-24 14:24:40 +0000202{
203 parent = p;
hyatt275d0702005-11-03 23:53:57 +0000204 if (p) {
kociendabb0c24b2001-08-24 14:24:40 +0000205 p->ref();
mjs08191d52006-03-06 02:53:41 +0000206 m_basicDir = p->basicDir();
kociendabb0c24b2001-08-24 14:24:40 +0000207 } else
mjs08191d52006-03-06 02:53:41 +0000208 m_basicDir = e;
kociendabb0c24b2001-08-24 14:24:40 +0000209 count = 0;
210}
211
212BidiContext::~BidiContext()
213{
hyatt275d0702005-11-03 23:53:57 +0000214 if (parent)
215 parent->deref();
kociendabb0c24b2001-08-24 14:24:40 +0000216}
217
218void BidiContext::ref() const
219{
220 count++;
221}
222
223void BidiContext::deref() const
224{
225 count--;
hyatt275d0702005-11-03 23:53:57 +0000226 if (count <= 0)
227 delete this;
kociendabb0c24b2001-08-24 14:24:40 +0000228}
229
eseidel789896f2005-11-27 22:52:09 +0000230bool operator==(const BidiContext& c1, const BidiContext& c2)
231{
232 if (&c1 == &c2)
233 return true;
mjs08191d52006-03-06 02:53:41 +0000234 if (c1.level != c2.level || c1.override != c2.override || c1.dir() != c2.dir() || c1.basicDir() != c2.basicDir())
eseidel789896f2005-11-27 22:52:09 +0000235 return false;
eseidel6465a0f2006-01-10 13:35:45 +0000236 if (!c1.parent)
237 return !c2.parent;
238 return c2.parent && *c1.parent == *c2.parent;
eseidel789896f2005-11-27 22:52:09 +0000239}
240
241inline bool operator!=(const BidiContext& c1, const BidiContext& c2)
242{
243 return !(c1 == c2);
244}
245
kociendabb0c24b2001-08-24 14:24:40 +0000246// ---------------------------------------------------------------------
247
hyatt275d0702005-11-03 23:53:57 +0000248inline bool operator==(const BidiIterator& it1, const BidiIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000249{
hyatt275d0702005-11-03 23:53:57 +0000250 if (it1.pos != it2.pos)
251 return false;
252 if (it1.obj != it2.obj)
253 return false;
darinb70665a2002-04-15 23:43:21 +0000254 return true;
255}
256
hyatt275d0702005-11-03 23:53:57 +0000257inline bool operator!=(const BidiIterator& it1, const BidiIterator& it2)
darinb70665a2002-04-15 23:43:21 +0000258{
hyatt275d0702005-11-03 23:53:57 +0000259 if (it1.pos != it2.pos)
260 return true;
261 if (it1.obj != it2.obj)
262 return true;
darinb70665a2002-04-15 23:43:21 +0000263 return false;
264}
265
hyatt275d0702005-11-03 23:53:57 +0000266static inline RenderObject* bidiNext(RenderBlock* block, RenderObject* current, BidiState& bidi,
hyattffe78712003-02-11 01:59:29 +0000267 bool skipInlines = true, bool* endOfInline = 0)
mjs6f821c82002-03-22 00:31:57 +0000268{
hyatt275d0702005-11-03 23:53:57 +0000269 RenderObject* next = 0;
hyattffe78712003-02-11 01:59:29 +0000270 bool oldEndOfInline = endOfInline ? *endOfInline : false;
271 if (endOfInline)
272 *endOfInline = false;
273
hyatt275d0702005-11-03 23:53:57 +0000274 while (current) {
hyattffe78712003-02-11 01:59:29 +0000275 if (!oldEndOfInline && !current->isFloating() && !current->isReplaced() && !current->isPositioned()) {
276 next = current->firstChild();
darindde01502005-12-18 22:55:35 +0000277 if (next && bidi.adjustEmbedding && next->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000278 EUnicodeBidi ub = next->style()->unicodeBidi();
hyatt275d0702005-11-03 23:53:57 +0000279 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000280 TextDirection dir = next->style()->direction();
darin7ab31092006-05-10 04:59:57 +0000281 UCharDirection d = (ub == Embed
282 ? (dir == RTL ? U_RIGHT_TO_LEFT_EMBEDDING : U_LEFT_TO_RIGHT_EMBEDDING)
283 : (dir == RTL ? U_RIGHT_TO_LEFT_OVERRIDE : U_LEFT_TO_RIGHT_OVERRIDE));
hyatt275d0702005-11-03 23:53:57 +0000284 embed(d, bidi);
hyattffe78712003-02-11 01:59:29 +0000285 }
286 }
287 }
hyatt275d0702005-11-03 23:53:57 +0000288
hyattffe78712003-02-11 01:59:29 +0000289 if (!next) {
hyatt275d0702005-11-03 23:53:57 +0000290 if (!skipInlines && !oldEndOfInline && current->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000291 next = current;
292 if (endOfInline)
293 *endOfInline = true;
294 break;
295 }
mjs6f821c82002-03-22 00:31:57 +0000296
hyatt275d0702005-11-03 23:53:57 +0000297 while (current && current != block) {
darindde01502005-12-18 22:55:35 +0000298 if (bidi.adjustEmbedding && current->isInlineFlow() && current->style()->unicodeBidi() != UBNormal)
darin7ab31092006-05-10 04:59:57 +0000299 embed(U_POP_DIRECTIONAL_FORMAT, bidi);
hyatt275d0702005-11-03 23:53:57 +0000300
darindde01502005-12-18 22:55:35 +0000301 next = current->nextSibling();
302 if (next) {
303 if (bidi.adjustEmbedding && next->isInlineFlow()) {
304 EUnicodeBidi ub = next->style()->unicodeBidi();
305 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000306 TextDirection dir = next->style()->direction();
darin7ab31092006-05-10 04:59:57 +0000307 UCharDirection d = (ub == Embed
308 ? (dir == RTL ? U_RIGHT_TO_LEFT_EMBEDDING : U_LEFT_TO_RIGHT_EMBEDDING)
309 : (dir == RTL ? U_RIGHT_TO_LEFT_OVERRIDE : U_LEFT_TO_RIGHT_OVERRIDE));
darindde01502005-12-18 22:55:35 +0000310 embed(d, bidi);
311 }
312 }
313 break;
314 }
315
hyattffe78712003-02-11 01:59:29 +0000316 current = current->parent();
hyatt275d0702005-11-03 23:53:57 +0000317 if (!skipInlines && current && current != block && current->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000318 next = current;
319 if (endOfInline)
320 *endOfInline = true;
321 break;
322 }
323 }
324 }
mjs6f821c82002-03-22 00:31:57 +0000325
hyatt275d0702005-11-03 23:53:57 +0000326 if (!next)
327 break;
hyattffe78712003-02-11 01:59:29 +0000328
329 if (next->isText() || next->isBR() || next->isFloating() || next->isReplaced() || next->isPositioned()
330 || ((!skipInlines || !next->firstChild()) // Always return EMPTY inlines.
331 && next->isInlineFlow()))
mjs6f821c82002-03-22 00:31:57 +0000332 break;
333 current = next;
334 }
335 return next;
336}
337
hyatt275d0702005-11-03 23:53:57 +0000338static RenderObject* bidiFirst(RenderBlock* block, BidiState& bidi, bool skipInlines = true )
mjs6f821c82002-03-22 00:31:57 +0000339{
hyatt275d0702005-11-03 23:53:57 +0000340 if (!block->firstChild())
341 return 0;
342
343 RenderObject* o = block->firstChild();
hyattffe78712003-02-11 01:59:29 +0000344 if (o->isInlineFlow()) {
darindde01502005-12-18 22:55:35 +0000345 if (bidi.adjustEmbedding) {
346 EUnicodeBidi ub = o->style()->unicodeBidi();
347 if (ub != UBNormal) {
darin9d0a6282006-03-01 07:49:33 +0000348 TextDirection dir = o->style()->direction();
darin7ab31092006-05-10 04:59:57 +0000349 UCharDirection d = (ub == Embed
350 ? (dir == RTL ? U_RIGHT_TO_LEFT_EMBEDDING : U_LEFT_TO_RIGHT_EMBEDDING)
351 : (dir == RTL ? U_RIGHT_TO_LEFT_OVERRIDE : U_LEFT_TO_RIGHT_OVERRIDE));
darindde01502005-12-18 22:55:35 +0000352 embed(d, bidi);
353 }
354 }
hyattffe78712003-02-11 01:59:29 +0000355 if (skipInlines && o->firstChild())
hyatt275d0702005-11-03 23:53:57 +0000356 o = bidiNext(block, o, bidi, skipInlines);
hyattffe78712003-02-11 01:59:29 +0000357 else
358 return o; // Never skip empty inlines.
359 }
hyattc5334f12003-08-08 22:26:08 +0000360
361 if (o && !o->isText() && !o->isBR() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
hyatt275d0702005-11-03 23:53:57 +0000362 o = bidiNext(block, o, bidi, skipInlines);
mjs6f821c82002-03-22 00:31:57 +0000363 return o;
364}
365
hyatt275d0702005-11-03 23:53:57 +0000366inline void BidiIterator::increment(BidiState& bidi)
kociendabb0c24b2001-08-24 14:24:40 +0000367{
hyatt275d0702005-11-03 23:53:57 +0000368 if (!obj)
369 return;
370 if (obj->isText()) {
kociendabb0c24b2001-08-24 14:24:40 +0000371 pos++;
eseidel789896f2005-11-27 22:52:09 +0000372 if (pos >= static_cast<RenderText *>(obj)->stringLength()) {
hyatt275d0702005-11-03 23:53:57 +0000373 obj = bidiNext(block, obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000374 pos = 0;
375 }
376 } else {
hyatt275d0702005-11-03 23:53:57 +0000377 obj = bidiNext(block, obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000378 pos = 0;
379 }
380}
381
mjs6f821c82002-03-22 00:31:57 +0000382inline bool BidiIterator::atEnd() const
kociendabb0c24b2001-08-24 14:24:40 +0000383{
hyatt275d0702005-11-03 23:53:57 +0000384 return !obj;
kociendabb0c24b2001-08-24 14:24:40 +0000385}
386
darin7ab31092006-05-10 04:59:57 +0000387UChar BidiIterator::current() const
kociendabb0c24b2001-08-24 14:24:40 +0000388{
hyatt30586b42003-12-02 23:19:11 +0000389 if (!obj || !obj->isText())
darin7ab31092006-05-10 04:59:57 +0000390 return 0;
hyatt30586b42003-12-02 23:19:11 +0000391
392 RenderText* text = static_cast<RenderText*>(obj);
393 if (!text->text())
darin7ab31092006-05-10 04:59:57 +0000394 return 0;
hyatt30586b42003-12-02 23:19:11 +0000395
396 return text->text()[pos];
kociendabb0c24b2001-08-24 14:24:40 +0000397}
398
darin7ab31092006-05-10 04:59:57 +0000399ALWAYS_INLINE UCharDirection BidiIterator::direction() const
kociendabb0c24b2001-08-24 14:24:40 +0000400{
hyatt5c18e1a2004-09-28 18:32:47 +0000401 if (!obj)
darin7ab31092006-05-10 04:59:57 +0000402 return U_OTHER_NEUTRAL;
hyatt5c18e1a2004-09-28 18:32:47 +0000403 if (obj->isListMarker())
darin7ab31092006-05-10 04:59:57 +0000404 return obj->style()->direction() == LTR ? U_LEFT_TO_RIGHT : U_RIGHT_TO_LEFT;
hyatt5c18e1a2004-09-28 18:32:47 +0000405 if (!obj->isText())
darin7ab31092006-05-10 04:59:57 +0000406 return U_OTHER_NEUTRAL;
407 RenderText* renderTxt = static_cast<RenderText*>(obj);
hyatt59136b72005-07-09 20:19:28 +0000408 if (pos >= renderTxt->stringLength())
darin7ab31092006-05-10 04:59:57 +0000409 return U_OTHER_NEUTRAL;
410 return u_charDirection(renderTxt->text()[pos]);
kociendabb0c24b2001-08-24 14:24:40 +0000411}
412
kociendabb0c24b2001-08-24 14:24:40 +0000413// -------------------------------------------------------------------------------------------------
414
hyatt2f1e7102003-02-20 01:27:03 +0000415static void addRun(BidiRun* bidiRun)
416{
417 if (!sFirstBidiRun)
418 sFirstBidiRun = sLastBidiRun = bidiRun;
419 else {
420 sLastBidiRun->nextRun = bidiRun;
421 sLastBidiRun = bidiRun;
422 }
423 sBidiRunCount++;
hyatt4b381692003-03-10 21:11:59 +0000424 bidiRun->compact = sBuildingCompactRuns;
hyattfe99c872003-07-31 22:25:29 +0000425
426 // Compute the number of spaces in this run,
427 if (bidiRun->obj && bidiRun->obj->isText()) {
428 RenderText* text = static_cast<RenderText*>(bidiRun->obj);
hyattefebbdd2003-12-02 22:25:31 +0000429 if (text->text()) {
430 for (int i = bidiRun->start; i < bidiRun->stop; i++) {
darin7ab31092006-05-10 04:59:57 +0000431 UChar c = text->text()[i];
harrison208ea792005-07-29 23:42:59 +0000432 if (c == ' ' || c == '\n' || c == '\t')
hyattefebbdd2003-12-02 22:25:31 +0000433 numSpaces++;
434 }
darina3c48282003-10-07 15:49:30 +0000435 }
hyattfe99c872003-07-31 22:25:29 +0000436 }
hyatt2f1e7102003-02-20 01:27:03 +0000437}
438
439static void reverseRuns(int start, int end)
440{
441 if (start >= end)
442 return;
443
444 assert(start >= 0 && end < sBidiRunCount);
445
446 // Get the item before the start of the runs to reverse and put it in
447 // |beforeStart|. |curr| should point to the first run to reverse.
448 BidiRun* curr = sFirstBidiRun;
449 BidiRun* beforeStart = 0;
450 int i = 0;
451 while (i < start) {
452 i++;
453 beforeStart = curr;
454 curr = curr->nextRun;
455 }
456
hyatt793fbbc2003-02-26 02:38:01 +0000457 BidiRun* startRun = curr;
hyatt2f1e7102003-02-20 01:27:03 +0000458 while (i < end) {
hyatt793fbbc2003-02-26 02:38:01 +0000459 i++;
460 curr = curr->nextRun;
461 }
462 BidiRun* endRun = curr;
463 BidiRun* afterEnd = curr->nextRun;
464
465 i = start;
466 curr = startRun;
467 BidiRun* newNext = afterEnd;
468 while (i <= end) {
hyatt2f1e7102003-02-20 01:27:03 +0000469 // Do the reversal.
470 BidiRun* next = curr->nextRun;
hyatt793fbbc2003-02-26 02:38:01 +0000471 curr->nextRun = newNext;
472 newNext = curr;
hyatt2f1e7102003-02-20 01:27:03 +0000473 curr = next;
474 i++;
475 }
476
hyatt2f1e7102003-02-20 01:27:03 +0000477 // Now hook up beforeStart and afterEnd to the newStart and newEnd.
478 if (beforeStart)
hyatt793fbbc2003-02-26 02:38:01 +0000479 beforeStart->nextRun = endRun;
hyatt2f1e7102003-02-20 01:27:03 +0000480 else
hyatt793fbbc2003-02-26 02:38:01 +0000481 sFirstBidiRun = endRun;
hyatt2f1e7102003-02-20 01:27:03 +0000482
hyatt793fbbc2003-02-26 02:38:01 +0000483 startRun->nextRun = afterEnd;
hyatt2f1e7102003-02-20 01:27:03 +0000484 if (!afterEnd)
hyatt793fbbc2003-02-26 02:38:01 +0000485 sLastBidiRun = startRun;
hyatt2f1e7102003-02-20 01:27:03 +0000486}
487
darinb9481ed2006-03-20 02:57:59 +0000488static void chopMidpointsAt(RenderObject* obj, unsigned pos)
hyatt78b85132004-03-29 20:07:45 +0000489{
hyatt275d0702005-11-03 23:53:57 +0000490 if (!sNumMidpoints)
491 return;
hyatt78b85132004-03-29 20:07:45 +0000492 BidiIterator* midpoints = smidpoints->data();
darinb9481ed2006-03-20 02:57:59 +0000493 for (unsigned i = 0; i < sNumMidpoints; i++) {
hyatt78b85132004-03-29 20:07:45 +0000494 const BidiIterator& point = midpoints[i];
495 if (point.obj == obj && point.pos == pos) {
496 sNumMidpoints = i;
497 break;
498 }
499 }
500}
501
hyatt275d0702005-11-03 23:53:57 +0000502static void checkMidpoints(BidiIterator& lBreak, BidiState& bidi)
hyattfe99c872003-07-31 22:25:29 +0000503{
504 // Check to see if our last midpoint is a start point beyond the line break. If so,
hyattdca76e92005-11-02 08:52:50 +0000505 // shave it off the list, and shave off a trailing space if the previous end point doesn't
506 // preserve whitespace.
hyattfe99c872003-07-31 22:25:29 +0000507 if (lBreak.obj && sNumMidpoints && sNumMidpoints%2 == 0) {
508 BidiIterator* midpoints = smidpoints->data();
509 BidiIterator& endpoint = midpoints[sNumMidpoints-2];
510 const BidiIterator& startpoint = midpoints[sNumMidpoints-1];
511 BidiIterator currpoint = endpoint;
512 while (!currpoint.atEnd() && currpoint != startpoint && currpoint != lBreak)
hyatt275d0702005-11-03 23:53:57 +0000513 currpoint.increment(bidi);
hyattfe99c872003-07-31 22:25:29 +0000514 if (currpoint == lBreak) {
515 // We hit the line break before the start point. Shave off the start point.
516 sNumMidpoints--;
hyattdca76e92005-11-02 08:52:50 +0000517 if (endpoint.obj->style()->collapseWhiteSpace()) {
hyattee1bcae2004-03-29 21:36:04 +0000518 if (endpoint.obj->isText()) {
519 // Don't shave a character off the endpoint if it was from a soft hyphen.
520 RenderText* textObj = static_cast<RenderText*>(endpoint.obj);
bdakin9151ba52005-10-24 22:51:06 +0000521 if (endpoint.pos+1 < textObj->length()) {
darin7ab31092006-05-10 04:59:57 +0000522 if (textObj->text()[endpoint.pos+1] == SOFT_HYPHEN)
bdakin9151ba52005-10-24 22:51:06 +0000523 return;
524 } else if (startpoint.obj->isText()) {
525 RenderText *startText = static_cast<RenderText*>(startpoint.obj);
darin7ab31092006-05-10 04:59:57 +0000526 if (startText->length() > 0 && startText->text()[0] == SOFT_HYPHEN)
bdakin9151ba52005-10-24 22:51:06 +0000527 return;
528 }
hyattee1bcae2004-03-29 21:36:04 +0000529 }
hyattfe99c872003-07-31 22:25:29 +0000530 endpoint.pos--;
hyattee1bcae2004-03-29 21:36:04 +0000531 }
hyattfe99c872003-07-31 22:25:29 +0000532 }
533 }
534}
535
hyatt85586af2003-02-19 23:22:42 +0000536static void addMidpoint(const BidiIterator& midpoint)
537{
538 if (!smidpoints)
539 return;
540
541 if (smidpoints->size() <= sNumMidpoints)
542 smidpoints->resize(sNumMidpoints+10);
543
544 BidiIterator* midpoints = smidpoints->data();
545 midpoints[sNumMidpoints++] = midpoint;
546}
547
mjsfe301d72003-10-02 18:46:18 +0000548static void appendRunsForObject(int start, int end, RenderObject* obj, BidiState &bidi)
hyatt33f8d492002-11-12 21:44:52 +0000549{
hyatt98ee7e42003-05-14 01:39:15 +0000550 if (start > end || obj->isFloating() ||
hyatt853cd7d2004-11-05 02:59:48 +0000551 (obj->isPositioned() && !obj->hasStaticX() && !obj->hasStaticY() && !obj->container()->isInlineFlow()))
hyatteb003b82002-11-15 22:35:10 +0000552 return;
hyatt85586af2003-02-19 23:22:42 +0000553
554 bool haveNextMidpoint = (smidpoints && sCurrMidpoint < sNumMidpoints);
mjsfe301d72003-10-02 18:46:18 +0000555 BidiIterator nextMidpoint;
hyatt85586af2003-02-19 23:22:42 +0000556 if (haveNextMidpoint)
557 nextMidpoint = smidpoints->at(sCurrMidpoint);
hyatt33f8d492002-11-12 21:44:52 +0000558 if (betweenMidpoints) {
hyatt85586af2003-02-19 23:22:42 +0000559 if (!(haveNextMidpoint && nextMidpoint.obj == obj))
hyatt33f8d492002-11-12 21:44:52 +0000560 return;
561 // This is a new start point. Stop ignoring objects and
562 // adjust our start.
563 betweenMidpoints = false;
hyatt85586af2003-02-19 23:22:42 +0000564 start = nextMidpoint.pos;
565 sCurrMidpoint++;
hyatt33f8d492002-11-12 21:44:52 +0000566 if (start < end)
mjsfe301d72003-10-02 18:46:18 +0000567 return appendRunsForObject(start, end, obj, bidi);
hyatt33f8d492002-11-12 21:44:52 +0000568 }
569 else {
hyatt85586af2003-02-19 23:22:42 +0000570 if (!smidpoints || !haveNextMidpoint || (obj != nextMidpoint.obj)) {
eseidel789896f2005-11-27 22:52:09 +0000571 addRun(new (obj->renderArena()) BidiRun(start, end, obj, bidi.context.get(), bidi.dir));
hyatt33f8d492002-11-12 21:44:52 +0000572 return;
573 }
574
hyatt78b85132004-03-29 20:07:45 +0000575 // An end midpoint has been encountered within our object. We
hyatt33f8d492002-11-12 21:44:52 +0000576 // need to go ahead and append a run with our endpoint.
hyatt85586af2003-02-19 23:22:42 +0000577 if (int(nextMidpoint.pos+1) <= end) {
hyatt26179432002-11-17 01:57:27 +0000578 betweenMidpoints = true;
hyatt85586af2003-02-19 23:22:42 +0000579 sCurrMidpoint++;
hyattc64f9fc2003-03-14 01:25:59 +0000580 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 +0000581 if (int(nextMidpoint.pos+1) > start)
582 addRun(new (obj->renderArena())
eseidel789896f2005-11-27 22:52:09 +0000583 BidiRun(start, nextMidpoint.pos+1, obj, bidi.context.get(), bidi.dir));
mjsfe301d72003-10-02 18:46:18 +0000584 return appendRunsForObject(nextMidpoint.pos+1, end, obj, bidi);
hyattc64f9fc2003-03-14 01:25:59 +0000585 }
hyatt26179432002-11-17 01:57:27 +0000586 }
587 else
eseidel789896f2005-11-27 22:52:09 +0000588 addRun(new (obj->renderArena()) BidiRun(start, end, obj, bidi.context.get(), bidi.dir));
hyatt33f8d492002-11-12 21:44:52 +0000589 }
590}
591
hyatt275d0702005-11-03 23:53:57 +0000592static void appendRun(BidiState &bidi)
kociendabb0c24b2001-08-24 14:24:40 +0000593{
ggarenef026b22005-07-06 00:21:53 +0000594 if (emptyRun || !bidi.eor.obj)
595 return;
kociendabb0c24b2001-08-24 14:24:40 +0000596#if BIDI_DEBUG > 1
597 kdDebug(6041) << "appendRun: dir="<<(int)dir<<endl;
598#endif
darinf028f812002-06-10 20:08:04 +0000599
hyatt275d0702005-11-03 23:53:57 +0000600 bool b = bidi.adjustEmbedding;
601 bidi.adjustEmbedding = false;
kociendabb0c24b2001-08-24 14:24:40 +0000602
mjsfe301d72003-10-02 18:46:18 +0000603 int start = bidi.sor.pos;
604 RenderObject *obj = bidi.sor.obj;
eseidel789896f2005-11-27 22:52:09 +0000605 while (obj && obj != bidi.eor.obj && obj != bidi.endOfLine.obj) {
mjsfe301d72003-10-02 18:46:18 +0000606 appendRunsForObject(start, obj->length(), obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000607 start = 0;
hyatt275d0702005-11-03 23:53:57 +0000608 obj = bidiNext(bidi.sor.block, obj, bidi);
kociendabb0c24b2001-08-24 14:24:40 +0000609 }
justing5b0e0422005-08-01 03:20:49 +0000610 if (obj) {
eseidel789896f2005-11-27 22:52:09 +0000611 unsigned pos = obj == bidi.eor.obj ? bidi.eor.pos : UINT_MAX;
612 if (obj == bidi.endOfLine.obj && bidi.endOfLine.pos <= pos) {
613 bidi.reachedEndOfLine = true;
614 pos = bidi.endOfLine.pos;
615 }
justing5b0e0422005-08-01 03:20:49 +0000616 // 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 +0000617 int end = obj->length() ? pos+1 : 0;
justing5b0e0422005-08-01 03:20:49 +0000618 appendRunsForObject(start, end, obj, bidi);
619 }
hyatt33f8d492002-11-12 21:44:52 +0000620
hyatt275d0702005-11-03 23:53:57 +0000621 bidi.eor.increment(bidi);
mjsfe301d72003-10-02 18:46:18 +0000622 bidi.sor = bidi.eor;
darin7ab31092006-05-10 04:59:57 +0000623 bidi.dir = U_OTHER_NEUTRAL;
624 bidi.status.eor = U_OTHER_NEUTRAL;
hyatt275d0702005-11-03 23:53:57 +0000625 bidi.adjustEmbedding = b;
mjs6f821c82002-03-22 00:31:57 +0000626}
627
darin7ab31092006-05-10 04:59:57 +0000628static void embed(UCharDirection d, BidiState& bidi)
mjs6f821c82002-03-22 00:31:57 +0000629{
hyatt275d0702005-11-03 23:53:57 +0000630 bool b = bidi.adjustEmbedding;
631 bidi.adjustEmbedding = false;
darin7ab31092006-05-10 04:59:57 +0000632 if (d == U_POP_DIRECTIONAL_FORMAT) {
darin54008922006-01-13 16:39:05 +0000633 BidiContext *c = bidi.context->parent;
634 if (c) {
635 if (!emptyRun && bidi.eor != bidi.last) {
darin7ab31092006-05-10 04:59:57 +0000636 assert(bidi.status.eor != U_OTHER_NEUTRAL);
hyatt275d0702005-11-03 23:53:57 +0000637 // bidi.sor ... bidi.eor ... bidi.last eor; need to append the bidi.sor-bidi.eor run or extend it through bidi.last
darin7ab31092006-05-10 04:59:57 +0000638 assert(bidi.status.last == U_EUROPEAN_NUMBER_SEPARATOR
639 || bidi.status.last == U_EUROPEAN_NUMBER_TERMINATOR
640 || bidi.status.last == U_COMMON_NUMBER_SEPARATOR
641 || bidi.status.last == U_BOUNDARY_NEUTRAL
642 || bidi.status.last == U_BLOCK_SEPARATOR
643 || bidi.status.last == U_SEGMENT_SEPARATOR
644 || bidi.status.last == U_WHITE_SPACE_NEUTRAL
645 || bidi.status.last == U_OTHER_NEUTRAL);
646 if (bidi.dir == U_OTHER_NEUTRAL)
mjs08191d52006-03-06 02:53:41 +0000647 bidi.dir = bidi.context->dir();
darin7ab31092006-05-10 04:59:57 +0000648 if (bidi.context->dir() == U_LEFT_TO_RIGHT) {
hyatt275d0702005-11-03 23:53:57 +0000649 // bidi.sor ... bidi.eor ... bidi.last L
darin7ab31092006-05-10 04:59:57 +0000650 if (bidi.status.eor == U_EUROPEAN_NUMBER) {
651 if (bidi.status.lastStrong != U_LEFT_TO_RIGHT) {
652 bidi.dir = U_EUROPEAN_NUMBER;
hyatt275d0702005-11-03 23:53:57 +0000653 appendRun(bidi);
654 }
darin7ab31092006-05-10 04:59:57 +0000655 } else if (bidi.status.eor == U_ARABIC_NUMBER) {
656 bidi.dir = U_ARABIC_NUMBER;
darinef0c09f2005-10-09 01:46:17 +0000657 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +0000658 } else if (bidi.status.eor != U_LEFT_TO_RIGHT)
hyatt275d0702005-11-03 23:53:57 +0000659 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +0000660 } else if (bidi.status.eor != U_RIGHT_TO_LEFT && bidi.status.eor != U_RIGHT_TO_LEFT_ARABIC)
darinef0c09f2005-10-09 01:46:17 +0000661 appendRun(bidi);
hyatt275d0702005-11-03 23:53:57 +0000662 bidi.eor = bidi.last;
663 }
darin54008922006-01-13 16:39:05 +0000664 appendRun(bidi);
665 emptyRun = true;
hyatt275d0702005-11-03 23:53:57 +0000666 // sor for the new run is determined by the higher level (rule X10)
mjs08191d52006-03-06 02:53:41 +0000667 bidi.status.last = bidi.context->dir();
668 bidi.status.lastStrong = bidi.context->dir();
darin54008922006-01-13 16:39:05 +0000669 bidi.context = c;
mjs08191d52006-03-06 02:53:41 +0000670 bidi.status.eor = bidi.context->dir();
hyatt275d0702005-11-03 23:53:57 +0000671 bidi.eor.obj = 0;
672 }
mjs6f821c82002-03-22 00:31:57 +0000673 } else {
darin7ab31092006-05-10 04:59:57 +0000674 UCharDirection runDir;
675 if (d == U_RIGHT_TO_LEFT_EMBEDDING || d == U_RIGHT_TO_LEFT_OVERRIDE)
676 runDir = U_RIGHT_TO_LEFT;
darin54008922006-01-13 16:39:05 +0000677 else
darin7ab31092006-05-10 04:59:57 +0000678 runDir = U_LEFT_TO_RIGHT;
679 bool override = d == U_LEFT_TO_RIGHT_OVERRIDE || d == U_RIGHT_TO_LEFT_OVERRIDE;
mjs6f821c82002-03-22 00:31:57 +0000680
darin54008922006-01-13 16:39:05 +0000681 unsigned char level = bidi.context->level;
darin7ab31092006-05-10 04:59:57 +0000682 if (runDir == U_RIGHT_TO_LEFT) {
darin54008922006-01-13 16:39:05 +0000683 if (level%2) // we have an odd level
684 level += 2;
685 else
686 level++;
687 } else {
688 if (level%2) // we have an odd level
689 level++;
690 else
691 level += 2;
692 }
mjs6f821c82002-03-22 00:31:57 +0000693
darin54008922006-01-13 16:39:05 +0000694 if (level < 61) {
695 if (!emptyRun && bidi.eor != bidi.last) {
darin7ab31092006-05-10 04:59:57 +0000696 assert(bidi.status.eor != U_OTHER_NEUTRAL);
hyatt275d0702005-11-03 23:53:57 +0000697 // bidi.sor ... bidi.eor ... bidi.last eor; need to append the bidi.sor-bidi.eor run or extend it through bidi.last
darin7ab31092006-05-10 04:59:57 +0000698 assert(bidi.status.last == U_EUROPEAN_NUMBER_SEPARATOR
699 || bidi.status.last == U_EUROPEAN_NUMBER_TERMINATOR
700 || bidi.status.last == U_COMMON_NUMBER_SEPARATOR
701 || bidi.status.last == U_BOUNDARY_NEUTRAL
702 || bidi.status.last == U_BLOCK_SEPARATOR
703 || bidi.status.last == U_SEGMENT_SEPARATOR
704 || bidi.status.last == U_WHITE_SPACE_NEUTRAL
705 || bidi.status.last == U_OTHER_NEUTRAL);
706 if (bidi.dir == U_OTHER_NEUTRAL)
hyatt275d0702005-11-03 23:53:57 +0000707 bidi.dir = runDir;
darin7ab31092006-05-10 04:59:57 +0000708 if (runDir == U_LEFT_TO_RIGHT) {
hyatt275d0702005-11-03 23:53:57 +0000709 // bidi.sor ... bidi.eor ... bidi.last L
darin7ab31092006-05-10 04:59:57 +0000710 if (bidi.status.eor == U_EUROPEAN_NUMBER) {
711 if (bidi.status.lastStrong != U_LEFT_TO_RIGHT) {
712 bidi.dir = U_EUROPEAN_NUMBER;
hyatt275d0702005-11-03 23:53:57 +0000713 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +0000714 if (bidi.context->dir() != U_LEFT_TO_RIGHT)
715 bidi.dir = U_RIGHT_TO_LEFT;
hyatt275d0702005-11-03 23:53:57 +0000716 }
darin7ab31092006-05-10 04:59:57 +0000717 } else if (bidi.status.eor == U_ARABIC_NUMBER) {
718 bidi.dir = U_ARABIC_NUMBER;
darinef0c09f2005-10-09 01:46:17 +0000719 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +0000720 if (bidi.context->dir() != U_LEFT_TO_RIGHT) {
hyatt275d0702005-11-03 23:53:57 +0000721 bidi.eor = bidi.last;
darin7ab31092006-05-10 04:59:57 +0000722 bidi.dir = U_RIGHT_TO_LEFT;
hyatt275d0702005-11-03 23:53:57 +0000723 appendRun(bidi);
724 }
darin7ab31092006-05-10 04:59:57 +0000725 } else if (bidi.status.eor != U_LEFT_TO_RIGHT) {
726 if (bidi.context->dir() == U_LEFT_TO_RIGHT || bidi.status.lastStrong == U_LEFT_TO_RIGHT)
hyatt275d0702005-11-03 23:53:57 +0000727 appendRun(bidi);
728 else
darin7ab31092006-05-10 04:59:57 +0000729 bidi.dir = U_RIGHT_TO_LEFT;
darinef0c09f2005-10-09 01:46:17 +0000730 }
darin7ab31092006-05-10 04:59:57 +0000731 } else if (bidi.status.eor != U_RIGHT_TO_LEFT && bidi.status.eor != U_RIGHT_TO_LEFT_ARABIC) {
hyatt275d0702005-11-03 23:53:57 +0000732 // bidi.sor ... bidi.eor ... bidi.last R; bidi.eor=L/EN/AN; EN,AN behave like R (rule N1)
darin7ab31092006-05-10 04:59:57 +0000733 if (bidi.context->dir() == U_RIGHT_TO_LEFT || bidi.status.lastStrong == U_RIGHT_TO_LEFT || bidi.status.lastStrong == U_RIGHT_TO_LEFT_ARABIC)
darinef0c09f2005-10-09 01:46:17 +0000734 appendRun(bidi);
735 else
darin7ab31092006-05-10 04:59:57 +0000736 bidi.dir = U_LEFT_TO_RIGHT;
darinef0c09f2005-10-09 01:46:17 +0000737 }
hyatt275d0702005-11-03 23:53:57 +0000738 bidi.eor = bidi.last;
hyatt2f1e7102003-02-20 01:27:03 +0000739 }
hyatt275d0702005-11-03 23:53:57 +0000740 appendRun(bidi);
741 emptyRun = true;
eseidel789896f2005-11-27 22:52:09 +0000742 bidi.context = new BidiContext(level, runDir, bidi.context.get(), override);
hyatt275d0702005-11-03 23:53:57 +0000743 bidi.status.last = runDir;
744 bidi.status.lastStrong = runDir;
745 bidi.status.eor = runDir;
746 bidi.eor.obj = 0;
darinef0c09f2005-10-09 01:46:17 +0000747 }
mjs6f821c82002-03-22 00:31:57 +0000748 }
hyatt275d0702005-11-03 23:53:57 +0000749 bidi.adjustEmbedding = b;
kociendabb0c24b2001-08-24 14:24:40 +0000750}
751
hyattffe78712003-02-11 01:59:29 +0000752InlineFlowBox* RenderBlock::createLineBoxes(RenderObject* obj)
753{
754 // See if we have an unconstructed line box for this object that is also
755 // the last item on the line.
756 KHTMLAssert(obj->isInlineFlow() || obj == this);
757 RenderFlow* flow = static_cast<RenderFlow*>(obj);
758
759 // Get the last box we made for this render object.
760 InlineFlowBox* box = flow->lastLineBox();
761
762 // If this box is constructed then it is from a previous line, and we need
763 // to make a new box for our line. If this box is unconstructed but it has
764 // something following it on the line, then we know we have to make a new box
765 // as well. In this situation our inline has actually been split in two on
766 // the same line (this can happen with very fancy language mixtures).
767 if (!box || box->isConstructed() || box->nextOnLine()) {
768 // We need to make a new box for this render object. Once
769 // made, we need to place it at the end of the current line.
hyatt450813d2003-07-25 20:22:34 +0000770 InlineBox* newBox = obj->createInlineBox(false, obj == this);
hyattffe78712003-02-11 01:59:29 +0000771 KHTMLAssert(newBox->isInlineFlowBox());
772 box = static_cast<InlineFlowBox*>(newBox);
773 box->setFirstLineStyleBit(m_firstLine);
774
775 // We have a new box. Append it to the inline box we get by constructing our
776 // parent. If we have hit the block itself, then |box| represents the root
777 // inline box for the line, and it doesn't have to be appended to any parent
778 // inline.
779 if (obj != this) {
780 InlineFlowBox* parentBox = createLineBoxes(obj->parent());
781 parentBox->addToLine(box);
782 }
783 }
784
785 return box;
786}
787
hyatt275d0702005-11-03 23:53:57 +0000788RootInlineBox* RenderBlock::constructLine(const BidiIterator& start, const BidiIterator& end)
hyattffe78712003-02-11 01:59:29 +0000789{
hyatt2f1e7102003-02-20 01:27:03 +0000790 if (!sFirstBidiRun)
hyattffe78712003-02-11 01:59:29 +0000791 return 0; // We had no runs. Don't make a root inline box at all. The line is empty.
792
793 InlineFlowBox* parentBox = 0;
hyatt2f1e7102003-02-20 01:27:03 +0000794 for (BidiRun* r = sFirstBidiRun; r; r = r->nextRun) {
hyattffe78712003-02-11 01:59:29 +0000795 // Create a box for our object.
harrison8d773e72006-01-29 15:06:17 +0000796 bool isOnlyRun = (sBidiRunCount == 1);
797 if (sBidiRunCount == 2 && !r->obj->isListMarker())
798 isOnlyRun = ((style()->direction() == RTL) ? sLastBidiRun : sFirstBidiRun)->obj->isListMarker();
799 r->box = r->obj->createInlineBox(r->obj->isPositioned(), false, isOnlyRun);
hyatt0c3a9862004-02-23 21:26:26 +0000800 if (r->box) {
801 // If we have no parent box yet, or if the run is not simply a sibling,
802 // then we need to construct inline boxes as necessary to properly enclose the
803 // run's inline box.
hyatt275d0702005-11-03 23:53:57 +0000804 if (!parentBox || parentBox->object() != r->obj->parent())
hyatt0c3a9862004-02-23 21:26:26 +0000805 // Create new inline boxes all the way back to the appropriate insertion point.
806 parentBox = createLineBoxes(r->obj->parent());
hyattffe78712003-02-11 01:59:29 +0000807
hyatt0c3a9862004-02-23 21:26:26 +0000808 // Append the inline box to this line.
809 parentBox->addToLine(r->box);
darin06dcb9c2005-08-15 04:31:09 +0000810
811 if (r->box->isInlineTextBox()) {
812 InlineTextBox *text = static_cast<InlineTextBox*>(r->box);
813 text->setStart(r->start);
814 text->setLen(r->stop-r->start);
815 }
hyatt0c3a9862004-02-23 21:26:26 +0000816 }
hyattffe78712003-02-11 01:59:29 +0000817 }
818
819 // We should have a root inline box. It should be unconstructed and
820 // be the last continuation of our line list.
hyatt275d0702005-11-03 23:53:57 +0000821 assert(lastLineBox() && !lastLineBox()->isConstructed());
hyattffe78712003-02-11 01:59:29 +0000822
823 // Set bits on our inline flow boxes that indicate which sides should
824 // paint borders/margins/padding. This knowledge will ultimately be used when
825 // we determine the horizontal positions and widths of all the inline boxes on
826 // the line.
827 RenderObject* endObject = 0;
828 bool lastLine = !end.obj;
829 if (end.obj && end.pos == 0)
830 endObject = end.obj;
831 lastLineBox()->determineSpacingForFlowBoxes(lastLine, endObject);
832
833 // Now mark the line boxes as being constructed.
834 lastLineBox()->setConstructed();
835
836 // Return the last line.
hyatt0c3a9862004-02-23 21:26:26 +0000837 return lastRootBox();
hyattffe78712003-02-11 01:59:29 +0000838}
839
harrison208ea792005-07-29 23:42:59 +0000840// usage: tw - (xpos % tw);
841int RenderBlock::tabWidth(bool isWhitespacePre)
842{
843 if (!isWhitespacePre)
844 return 0;
845
846 if (!m_tabWidth) {
darin7ab31092006-05-10 04:59:57 +0000847 const UChar spaceChar = ' ';
hyatt3e99d1c2006-02-24 21:13:08 +0000848 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +0000849 int spaceWidth = font.width(TextRun(&spaceChar, 1));
harrison208ea792005-07-29 23:42:59 +0000850 m_tabWidth = spaceWidth * 8;
851 assert(m_tabWidth != 0);
852 }
853
854 return m_tabWidth;
855}
856
hyatt275d0702005-11-03 23:53:57 +0000857void RenderBlock::computeHorizontalPositionsForLine(RootInlineBox* lineBox, BidiState& bidi)
hyattffe78712003-02-11 01:59:29 +0000858{
859 // First determine our total width.
kociendae40cb942004-10-05 20:05:38 +0000860 int availableWidth = lineWidth(m_height);
hyattffe78712003-02-11 01:59:29 +0000861 int totWidth = lineBox->getFlowSpacingWidth();
hyatt2f1e7102003-02-20 01:27:03 +0000862 BidiRun* r = 0;
darin06dcb9c2005-08-15 04:31:09 +0000863 bool needsWordSpacing = false;
hyatt2f1e7102003-02-20 01:27:03 +0000864 for (r = sFirstBidiRun; r; r = r->nextRun) {
hyatt0c05e102006-04-14 08:15:00 +0000865 if (!r->box || r->obj->isPositioned() || r->box->isLineBreak())
hyatt98ee7e42003-05-14 01:39:15 +0000866 continue; // Positioned objects are only participating to figure out their
867 // correct static x position. They have no effect on the width.
hyatt0c05e102006-04-14 08:15:00 +0000868 // Similarly, line break boxes have no effect on the width.
kociendae40cb942004-10-05 20:05:38 +0000869 if (r->obj->isText()) {
darin06dcb9c2005-08-15 04:31:09 +0000870 RenderText *rt = static_cast<RenderText *>(r->obj);
871 int textWidth = rt->width(r->start, r->stop-r->start, totWidth, m_firstLine);
872 int effectiveWidth = textWidth;
873 int rtLength = rt->length();
874 if (rtLength != 0) {
darin7ab31092006-05-10 04:59:57 +0000875 if (r->start == 0 && needsWordSpacing && QChar(rt->text()[r->start]).isSpace())
hyattecad67c2006-03-01 06:16:38 +0000876 effectiveWidth += rt->font(m_firstLine)->wordSpacing();
darin7ab31092006-05-10 04:59:57 +0000877 needsWordSpacing = !QChar(rt->text()[r->stop-1]).isSpace() && r->stop == rtLength;
darin06dcb9c2005-08-15 04:31:09 +0000878 }
kociendae40cb942004-10-05 20:05:38 +0000879 if (!r->compact) {
880 RenderStyle *style = r->obj->style();
hyattdca76e92005-11-02 08:52:50 +0000881 if (style->autoWrap() && style->breakOnlyAfterWhiteSpace()) {
kociendae40cb942004-10-05 20:05:38 +0000882 // shrink the box as needed to keep the line from overflowing the available width
darin7bd70952006-04-13 07:07:34 +0000883 textWidth = min(effectiveWidth, availableWidth - totWidth);
kociendae40cb942004-10-05 20:05:38 +0000884 }
kociendae40cb942004-10-05 20:05:38 +0000885 }
kociendaee701982004-10-05 23:06:58 +0000886 r->box->setWidth(textWidth);
darin06dcb9c2005-08-15 04:31:09 +0000887 } else if (!r->obj->isInlineFlow()) {
hyattffe78712003-02-11 01:59:29 +0000888 r->obj->calcWidth();
889 r->box->setWidth(r->obj->width());
hyatt4b381692003-03-10 21:11:59 +0000890 if (!r->compact)
darin06dcb9c2005-08-15 04:31:09 +0000891 totWidth += r->obj->marginLeft() + r->obj->marginRight();
hyattffe78712003-02-11 01:59:29 +0000892 }
hyatt4b381692003-03-10 21:11:59 +0000893
894 // Compacts don't contribute to the width of the line, since they are placed in the margin.
895 if (!r->compact)
896 totWidth += r->box->width();
hyattffe78712003-02-11 01:59:29 +0000897 }
898
899 // Armed with the total width of the line (without justification),
900 // we now examine our text-align property in order to determine where to position the
901 // objects horizontally. The total width of the line can be increased if we end up
902 // justifying text.
903 int x = leftOffset(m_height);
hyattffe78712003-02-11 01:59:29 +0000904 switch(style()->textAlign()) {
905 case LEFT:
hyatt47c1d4d2003-07-24 22:07:45 +0000906 case KHTML_LEFT:
hyatt959a54e2004-09-27 17:52:18 +0000907 // The direction of the block should determine what happens with wide lines. In
908 // particular with RTL blocks, wide lines should still spill out to the left.
909 if (style()->direction() == RTL && totWidth > availableWidth)
910 x -= (totWidth - availableWidth);
hyattffe78712003-02-11 01:59:29 +0000911 numSpaces = 0;
912 break;
913 case JUSTIFY:
hyatt0c3a9862004-02-23 21:26:26 +0000914 if (numSpaces != 0 && !bidi.current.atEnd() && !lineBox->endsWithBreak())
hyattffe78712003-02-11 01:59:29 +0000915 break;
916 // fall through
917 case TAAUTO:
918 numSpaces = 0;
919 // for right to left fall through to right aligned
darin7ab31092006-05-10 04:59:57 +0000920 if (bidi.context->basicDir() == U_LEFT_TO_RIGHT)
hyattffe78712003-02-11 01:59:29 +0000921 break;
922 case RIGHT:
hyatt47c1d4d2003-07-24 22:07:45 +0000923 case KHTML_RIGHT:
hyatt959a54e2004-09-27 17:52:18 +0000924 // Wide lines spill out of the block based off direction.
925 // So even if text-align is right, if direction is LTR, wide lines should overflow out of the right
926 // side of the block.
927 if (style()->direction() == RTL || totWidth < availableWidth)
928 x += availableWidth - totWidth;
hyattffe78712003-02-11 01:59:29 +0000929 numSpaces = 0;
930 break;
931 case CENTER:
hyatt47c1d4d2003-07-24 22:07:45 +0000932 case KHTML_CENTER:
hyattffe78712003-02-11 01:59:29 +0000933 int xd = (availableWidth - totWidth)/2;
eseidel789896f2005-11-27 22:52:09 +0000934 x += xd > 0 ? xd : 0;
hyattffe78712003-02-11 01:59:29 +0000935 numSpaces = 0;
936 break;
937 }
938
hyattacbb0d42003-04-25 01:32:49 +0000939 if (numSpaces > 0) {
940 for (r = sFirstBidiRun; r; r = r->nextRun) {
hyatt0c3a9862004-02-23 21:26:26 +0000941 if (!r->box) continue;
942
hyattacbb0d42003-04-25 01:32:49 +0000943 int spaceAdd = 0;
944 if (numSpaces > 0 && r->obj->isText() && !r->compact) {
945 // get the number of spaces in the run
946 int spaces = 0;
darina3c48282003-10-07 15:49:30 +0000947 for ( int i = r->start; i < r->stop; i++ ) {
darin7ab31092006-05-10 04:59:57 +0000948 UChar c = static_cast<RenderText*>(r->obj)->text()[i];
harrison208ea792005-07-29 23:42:59 +0000949 if (c == ' ' || c == '\n' || c == '\t')
hyattacbb0d42003-04-25 01:32:49 +0000950 spaces++;
darina3c48282003-10-07 15:49:30 +0000951 }
hyatt870bdda2003-05-21 23:37:33 +0000952
hyattacbb0d42003-04-25 01:32:49 +0000953 KHTMLAssert(spaces <= numSpaces);
hyatt870bdda2003-05-21 23:37:33 +0000954
hyattdca76e92005-11-02 08:52:50 +0000955 // Only justify text if whitespace is collapsed.
956 if (r->obj->style()->collapseWhiteSpace()) {
hyatt870bdda2003-05-21 23:37:33 +0000957 spaceAdd = (availableWidth - totWidth)*spaces/numSpaces;
kocienda01bcc352003-09-25 16:51:11 +0000958 static_cast<InlineTextBox*>(r->box)->setSpaceAdd(spaceAdd);
hyatt870bdda2003-05-21 23:37:33 +0000959 totWidth += spaceAdd;
960 }
hyattacbb0d42003-04-25 01:32:49 +0000961 numSpaces -= spaces;
hyattacbb0d42003-04-25 01:32:49 +0000962 }
hyattffe78712003-02-11 01:59:29 +0000963 }
hyattffe78712003-02-11 01:59:29 +0000964 }
hyattacbb0d42003-04-25 01:32:49 +0000965
hyattffe78712003-02-11 01:59:29 +0000966 // The widths of all runs are now known. We can now place every inline box (and
967 // compute accurate widths for the inline flow boxes).
hyatt1f14a612004-12-07 00:34:02 +0000968 int leftPosition = x;
969 int rightPosition = x;
darin06dcb9c2005-08-15 04:31:09 +0000970 needsWordSpacing = false;
971 lineBox->placeBoxesHorizontally(x, leftPosition, rightPosition, needsWordSpacing);
hyatt1f14a612004-12-07 00:34:02 +0000972 lineBox->setHorizontalOverflowPositions(leftPosition, rightPosition);
hyattffe78712003-02-11 01:59:29 +0000973}
974
hyatt0c3a9862004-02-23 21:26:26 +0000975void RenderBlock::computeVerticalPositionsForLine(RootInlineBox* lineBox)
hyattffe78712003-02-11 01:59:29 +0000976{
977 lineBox->verticallyAlignBoxes(m_height);
hyatt0c3a9862004-02-23 21:26:26 +0000978 lineBox->setBlockHeight(m_height);
hyattffe78712003-02-11 01:59:29 +0000979
hyatt35a85e52003-02-14 19:43:07 +0000980 // See if the line spilled out. If so set overflow height accordingly.
981 int bottomOfLine = lineBox->bottomOverflow();
982 if (bottomOfLine > m_height && bottomOfLine > m_overflowHeight)
983 m_overflowHeight = bottomOfLine;
984
hyattffe78712003-02-11 01:59:29 +0000985 // Now make sure we place replaced render objects correctly.
hyatt98ee7e42003-05-14 01:39:15 +0000986 for (BidiRun* r = sFirstBidiRun; r; r = r->nextRun) {
eseidel789896f2005-11-27 22:52:09 +0000987 if (!r->box)
988 continue; // Skip runs with no line boxes.
hyatt0c3a9862004-02-23 21:26:26 +0000989
hyatt98ee7e42003-05-14 01:39:15 +0000990 // Align positioned boxes with the top of the line box. This is
991 // a reasonable approximation of an appropriate y position.
992 if (r->obj->isPositioned())
993 r->box->setYPos(m_height);
994
995 // Position is used to properly position both replaced elements and
996 // to update the static normal flow x/y of positioned elements.
darin0ce8a6b2005-09-24 11:45:21 +0000997 r->obj->position(r->box, r->start, r->stop - r->start, r->level%2, r->override);
hyatt98ee7e42003-05-14 01:39:15 +0000998 }
hyattffe78712003-02-11 01:59:29 +0000999}
kociendabb0c24b2001-08-24 14:24:40 +00001000
1001// collects one line of the paragraph and transforms it to visual order
hyatt275d0702005-11-03 23:53:57 +00001002void RenderBlock::bidiReorderLine(const BidiIterator& start, const BidiIterator& end, BidiState& bidi)
kociendabb0c24b2001-08-24 14:24:40 +00001003{
hyatt275d0702005-11-03 23:53:57 +00001004 if (start == end) {
1005 if (start.current() == '\n')
1006 m_height += lineHeight(m_firstLine, true);
hyatt33f8d492002-11-12 21:44:52 +00001007 return;
darinb70665a2002-04-15 23:43:21 +00001008 }
hyattffe78712003-02-11 01:59:29 +00001009
hyatt2f1e7102003-02-20 01:27:03 +00001010 sFirstBidiRun = 0;
1011 sLastBidiRun = 0;
1012 sBidiRunCount = 0;
rjw834c8062005-02-25 23:41:39 +00001013
darin7ab31092006-05-10 04:59:57 +00001014 assert(bidi.dir == U_OTHER_NEUTRAL);
eseidel789896f2005-11-27 22:52:09 +00001015
mjs6f821c82002-03-22 00:31:57 +00001016 emptyRun = true;
eseidel789896f2005-11-27 22:52:09 +00001017
ggarenef026b22005-07-06 00:21:53 +00001018 bidi.eor.obj = 0;
darinf028f812002-06-10 20:08:04 +00001019
darinb70665a2002-04-15 23:43:21 +00001020 numSpaces = 0;
kociendabb0c24b2001-08-24 14:24:40 +00001021
mjsfe301d72003-10-02 18:46:18 +00001022 bidi.current = start;
1023 bidi.last = bidi.current;
eseidel789896f2005-11-27 22:52:09 +00001024 bool pastEnd = false;
eseidel789896f2005-11-27 22:52:09 +00001025 BidiState stateAtEnd;
rjw834c8062005-02-25 23:41:39 +00001026
hyatt275d0702005-11-03 23:53:57 +00001027 while (true) {
darin7ab31092006-05-10 04:59:57 +00001028 UCharDirection dirCurrent;
adele7fc3e832006-02-17 09:31:35 +00001029 if (pastEnd && (previousLineBrokeCleanly || bidi.current.atEnd())) {
eseidel789896f2005-11-27 22:52:09 +00001030 BidiContext *c = bidi.context.get();
1031 while (c->parent)
1032 c = c->parent;
mjs08191d52006-03-06 02:53:41 +00001033 dirCurrent = c->dir();
adele7fc3e832006-02-17 09:31:35 +00001034 if (previousLineBrokeCleanly) {
eseidel789896f2005-11-27 22:52:09 +00001035 // A deviation from the Unicode Bidi Algorithm in order to match
1036 // Mac OS X text and WinIE: a hard line break resets bidi state.
1037 stateAtEnd.context = c;
1038 stateAtEnd.status.eor = dirCurrent;
1039 stateAtEnd.status.last = dirCurrent;
1040 stateAtEnd.status.lastStrong = dirCurrent;
1041 }
darinb70665a2002-04-15 23:43:21 +00001042 } else {
mjsfe301d72003-10-02 18:46:18 +00001043 dirCurrent = bidi.current.direction();
darin7ab31092006-05-10 04:59:57 +00001044 if (bidi.context->override
1045 && dirCurrent != U_RIGHT_TO_LEFT_EMBEDDING
1046 && dirCurrent != U_LEFT_TO_RIGHT_EMBEDDING
1047 && dirCurrent != U_RIGHT_TO_LEFT_OVERRIDE
1048 && dirCurrent != U_LEFT_TO_RIGHT_OVERRIDE
1049 && dirCurrent != U_POP_DIRECTIONAL_FORMAT)
mjs08191d52006-03-06 02:53:41 +00001050 dirCurrent = bidi.context->dir();
darin7ab31092006-05-10 04:59:57 +00001051 else if (dirCurrent == U_DIR_NON_SPACING_MARK)
darinef0c09f2005-10-09 01:46:17 +00001052 dirCurrent = bidi.status.last;
hyatt33f8d492002-11-12 21:44:52 +00001053 }
darinf028f812002-06-10 20:08:04 +00001054
darin7ab31092006-05-10 04:59:57 +00001055 assert(bidi.status.eor != U_OTHER_NEUTRAL);
hyatt275d0702005-11-03 23:53:57 +00001056 switch (dirCurrent) {
kociendabb0c24b2001-08-24 14:24:40 +00001057
hyatt275d0702005-11-03 23:53:57 +00001058 // embedding and overrides (X1-X9 in the Bidi specs)
darin7ab31092006-05-10 04:59:57 +00001059 case U_RIGHT_TO_LEFT_EMBEDDING:
1060 case U_LEFT_TO_RIGHT_EMBEDDING:
1061 case U_RIGHT_TO_LEFT_OVERRIDE:
1062 case U_LEFT_TO_RIGHT_OVERRIDE:
1063 case U_POP_DIRECTIONAL_FORMAT:
hyatt275d0702005-11-03 23:53:57 +00001064 embed(dirCurrent, bidi);
hyatt33f8d492002-11-12 21:44:52 +00001065 break;
kociendabb0c24b2001-08-24 14:24:40 +00001066
1067 // strong types
darin7ab31092006-05-10 04:59:57 +00001068 case U_LEFT_TO_RIGHT:
hyatt275d0702005-11-03 23:53:57 +00001069 switch(bidi.status.last) {
darin7ab31092006-05-10 04:59:57 +00001070 case U_RIGHT_TO_LEFT:
1071 case U_RIGHT_TO_LEFT_ARABIC:
1072 case U_EUROPEAN_NUMBER:
1073 case U_ARABIC_NUMBER:
1074 if (bidi.status.last != U_EUROPEAN_NUMBER || bidi.status.lastStrong != U_LEFT_TO_RIGHT)
hyatt275d0702005-11-03 23:53:57 +00001075 appendRun(bidi);
eseidel789896f2005-11-27 22:52:09 +00001076 break;
darin7ab31092006-05-10 04:59:57 +00001077 case U_LEFT_TO_RIGHT:
kociendabb0c24b2001-08-24 14:24:40 +00001078 break;
darin7ab31092006-05-10 04:59:57 +00001079 case U_EUROPEAN_NUMBER_SEPARATOR:
1080 case U_EUROPEAN_NUMBER_TERMINATOR:
1081 case U_COMMON_NUMBER_SEPARATOR:
1082 case U_BOUNDARY_NEUTRAL:
1083 case U_BLOCK_SEPARATOR:
1084 case U_SEGMENT_SEPARATOR:
1085 case U_WHITE_SPACE_NEUTRAL:
1086 case U_OTHER_NEUTRAL:
1087 if (bidi.status.eor == U_EUROPEAN_NUMBER) {
1088 if (bidi.status.lastStrong != U_LEFT_TO_RIGHT) {
ggarenef026b22005-07-06 00:21:53 +00001089 // the numbers need to be on a higher embedding level, so let's close that run
darin7ab31092006-05-10 04:59:57 +00001090 bidi.dir = U_EUROPEAN_NUMBER;
ggarenef026b22005-07-06 00:21:53 +00001091 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001092 if (bidi.context->dir() != U_LEFT_TO_RIGHT) {
ggarenef026b22005-07-06 00:21:53 +00001093 // the neutrals take the embedding direction, which is R
1094 bidi.eor = bidi.last;
darin7ab31092006-05-10 04:59:57 +00001095 bidi.dir = U_RIGHT_TO_LEFT;
ggarenef026b22005-07-06 00:21:53 +00001096 appendRun(bidi);
1097 }
1098 }
darin7ab31092006-05-10 04:59:57 +00001099 } else if (bidi.status.eor == U_ARABIC_NUMBER) {
ggarenef026b22005-07-06 00:21:53 +00001100 // Arabic numbers are always on a higher embedding level, so let's close that run
darin7ab31092006-05-10 04:59:57 +00001101 bidi.dir = U_ARABIC_NUMBER;
ggarenef026b22005-07-06 00:21:53 +00001102 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001103 if (bidi.context->dir() != U_LEFT_TO_RIGHT) {
ggarenef026b22005-07-06 00:21:53 +00001104 // the neutrals take the embedding direction, which is R
1105 bidi.eor = bidi.last;
darin7ab31092006-05-10 04:59:57 +00001106 bidi.dir = U_RIGHT_TO_LEFT;
ggarenef026b22005-07-06 00:21:53 +00001107 appendRun(bidi);
1108 }
darin7ab31092006-05-10 04:59:57 +00001109 } else if(bidi.status.eor != U_LEFT_TO_RIGHT) {
eseidel789896f2005-11-27 22:52:09 +00001110 //last stuff takes embedding dir
darin7ab31092006-05-10 04:59:57 +00001111 if (bidi.context->dir() != U_LEFT_TO_RIGHT && bidi.status.lastStrong != U_LEFT_TO_RIGHT) {
darin6f05b4c2005-05-27 01:17:32 +00001112 bidi.eor = bidi.last;
darin7ab31092006-05-10 04:59:57 +00001113 bidi.dir = U_RIGHT_TO_LEFT;
kociendabb0c24b2001-08-24 14:24:40 +00001114 }
eseidel789896f2005-11-27 22:52:09 +00001115 appendRun(bidi);
kociendabb0c24b2001-08-24 14:24:40 +00001116 }
1117 default:
1118 break;
eseidel789896f2005-11-27 22:52:09 +00001119 }
1120 bidi.eor = bidi.current;
darin7ab31092006-05-10 04:59:57 +00001121 bidi.status.eor = U_LEFT_TO_RIGHT;
1122 bidi.status.lastStrong = U_LEFT_TO_RIGHT;
1123 bidi.dir = U_LEFT_TO_RIGHT;
kociendabb0c24b2001-08-24 14:24:40 +00001124 break;
darin7ab31092006-05-10 04:59:57 +00001125 case U_RIGHT_TO_LEFT_ARABIC:
1126 case U_RIGHT_TO_LEFT:
eseidel789896f2005-11-27 22:52:09 +00001127 switch (bidi.status.last) {
darin7ab31092006-05-10 04:59:57 +00001128 case U_LEFT_TO_RIGHT:
1129 case U_EUROPEAN_NUMBER:
1130 case U_ARABIC_NUMBER:
darinef0c09f2005-10-09 01:46:17 +00001131 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001132 case U_RIGHT_TO_LEFT:
1133 case U_RIGHT_TO_LEFT_ARABIC:
kociendabb0c24b2001-08-24 14:24:40 +00001134 break;
darin7ab31092006-05-10 04:59:57 +00001135 case U_EUROPEAN_NUMBER_SEPARATOR:
1136 case U_EUROPEAN_NUMBER_TERMINATOR:
1137 case U_COMMON_NUMBER_SEPARATOR:
1138 case U_BOUNDARY_NEUTRAL:
1139 case U_BLOCK_SEPARATOR:
1140 case U_SEGMENT_SEPARATOR:
1141 case U_WHITE_SPACE_NEUTRAL:
1142 case U_OTHER_NEUTRAL:
1143 if (bidi.status.eor != U_RIGHT_TO_LEFT && bidi.status.eor != U_RIGHT_TO_LEFT_ARABIC) {
kociendabb0c24b2001-08-24 14:24:40 +00001144 //last stuff takes embedding dir
darin7ab31092006-05-10 04:59:57 +00001145 if (bidi.context->dir() != U_RIGHT_TO_LEFT && bidi.status.lastStrong != U_RIGHT_TO_LEFT
1146 && bidi.status.lastStrong != U_RIGHT_TO_LEFT_ARABIC) {
mjsfe301d72003-10-02 18:46:18 +00001147 bidi.eor = bidi.last;
darin7ab31092006-05-10 04:59:57 +00001148 bidi.dir = U_LEFT_TO_RIGHT;
kociendabb0c24b2001-08-24 14:24:40 +00001149 }
eseidel789896f2005-11-27 22:52:09 +00001150 appendRun(bidi);
1151 }
kociendabb0c24b2001-08-24 14:24:40 +00001152 default:
1153 break;
eseidel789896f2005-11-27 22:52:09 +00001154 }
1155 bidi.eor = bidi.current;
darin7ab31092006-05-10 04:59:57 +00001156 bidi.status.eor = U_RIGHT_TO_LEFT;
mjsfe301d72003-10-02 18:46:18 +00001157 bidi.status.lastStrong = dirCurrent;
darin7ab31092006-05-10 04:59:57 +00001158 bidi.dir = U_RIGHT_TO_LEFT;
kociendabb0c24b2001-08-24 14:24:40 +00001159 break;
1160
1161 // weak types:
1162
darin7ab31092006-05-10 04:59:57 +00001163 case U_EUROPEAN_NUMBER:
1164 if (bidi.status.lastStrong != U_RIGHT_TO_LEFT_ARABIC) {
kociendabb0c24b2001-08-24 14:24:40 +00001165 // if last strong was AL change EN to AN
hyatt275d0702005-11-03 23:53:57 +00001166 switch (bidi.status.last) {
darin7ab31092006-05-10 04:59:57 +00001167 case U_EUROPEAN_NUMBER:
1168 case U_LEFT_TO_RIGHT:
kociendabb0c24b2001-08-24 14:24:40 +00001169 break;
darin7ab31092006-05-10 04:59:57 +00001170 case U_RIGHT_TO_LEFT:
1171 case U_RIGHT_TO_LEFT_ARABIC:
1172 case U_ARABIC_NUMBER:
darinef0c09f2005-10-09 01:46:17 +00001173 bidi.eor = bidi.last;
hyatt275d0702005-11-03 23:53:57 +00001174 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001175 bidi.dir = U_EUROPEAN_NUMBER;
darinef0c09f2005-10-09 01:46:17 +00001176 break;
darin7ab31092006-05-10 04:59:57 +00001177 case U_EUROPEAN_NUMBER_SEPARATOR:
1178 case U_COMMON_NUMBER_SEPARATOR:
1179 if (bidi.status.eor == U_EUROPEAN_NUMBER)
eseidel789896f2005-11-27 22:52:09 +00001180 break;
darin7ab31092006-05-10 04:59:57 +00001181 case U_EUROPEAN_NUMBER_TERMINATOR:
1182 case U_BOUNDARY_NEUTRAL:
1183 case U_BLOCK_SEPARATOR:
1184 case U_SEGMENT_SEPARATOR:
1185 case U_WHITE_SPACE_NEUTRAL:
1186 case U_OTHER_NEUTRAL:
1187 if (bidi.status.eor == U_RIGHT_TO_LEFT) {
kociendabb0c24b2001-08-24 14:24:40 +00001188 // neutrals go to R
darin7ab31092006-05-10 04:59:57 +00001189 bidi.eor = bidi.status.last == U_EUROPEAN_NUMBER_TERMINATOR ? bidi.lastBeforeET : bidi.last;
hyatt275d0702005-11-03 23:53:57 +00001190 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001191 bidi.dir = U_EUROPEAN_NUMBER;
1192 } else if (bidi.status.eor != U_LEFT_TO_RIGHT &&
1193 (bidi.status.eor != U_EUROPEAN_NUMBER || bidi.status.lastStrong != U_LEFT_TO_RIGHT) &&
1194 bidi.dir != U_LEFT_TO_RIGHT) {
kociendabb0c24b2001-08-24 14:24:40 +00001195 // numbers on both sides, neutrals get right to left direction
eseidel789896f2005-11-27 22:52:09 +00001196 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001197 bidi.eor = bidi.status.last == U_EUROPEAN_NUMBER_TERMINATOR ? bidi.lastBeforeET : bidi.last;
1198 bidi.dir = U_RIGHT_TO_LEFT;
eseidel789896f2005-11-27 22:52:09 +00001199 appendRun(bidi);
darin7ab31092006-05-10 04:59:57 +00001200 bidi.dir = U_EUROPEAN_NUMBER;
kociendabb0c24b2001-08-24 14:24:40 +00001201 }
1202 default:
1203 break;
eseidel789896f2005-11-27 22:52:09 +00001204 }
1205 bidi.eor = bidi.current;
darin7ab31092006-05-10 04:59:57 +00001206 bidi.status.eor = U_EUROPEAN_NUMBER;
1207 if (bidi.dir == U_OTHER_NEUTRAL)
1208 bidi.dir = U_LEFT_TO_RIGHT;
kociendabb0c24b2001-08-24 14:24:40 +00001209 break;
1210 }
darin7ab31092006-05-10 04:59:57 +00001211 case U_ARABIC_NUMBER:
1212 dirCurrent = U_ARABIC_NUMBER;
eseidel789896f2005-11-27 22:52:09 +00001213 switch (bidi.status.last) {
darin7ab31092006-05-10 04:59:57 +00001214 case U_LEFT_TO_RIGHT:
1215 if (bidi.context->dir() == U_LEFT_TO_RIGHT)
mjs89ab8912005-11-28 04:37:33 +00001216 appendRun(bidi);
1217 break;
darin7ab31092006-05-10 04:59:57 +00001218 case U_ARABIC_NUMBER:
eseidel789896f2005-11-27 22:52:09 +00001219 break;
darin7ab31092006-05-10 04:59:57 +00001220 case U_RIGHT_TO_LEFT:
1221 case U_RIGHT_TO_LEFT_ARABIC:
1222 case U_EUROPEAN_NUMBER:
darinef0c09f2005-10-09 01:46:17 +00001223 bidi.eor = bidi.last;
hyatt275d0702005-11-03 23:53:57 +00001224 appendRun(bidi);
kociendabb0c24b2001-08-24 14:24:40 +00001225 break;
darin7ab31092006-05-10 04:59:57 +00001226 case U_COMMON_NUMBER_SEPARATOR:
1227 if (bidi.status.eor == U_ARABIC_NUMBER)
eseidel789896f2005-11-27 22:52:09 +00001228 break;
darin7ab31092006-05-10 04:59:57 +00001229 case U_EUROPEAN_NUMBER_SEPARATOR:
1230 case U_EUROPEAN_NUMBER_TERMINATOR:
1231 case U_BOUNDARY_NEUTRAL:
1232 case U_BLOCK_SEPARATOR:
1233 case U_SEGMENT_SEPARATOR:
1234 case U_WHITE_SPACE_NEUTRAL:
1235 case U_OTHER_NEUTRAL:
1236 if (bidi.status.eor != U_RIGHT_TO_LEFT && bidi.status.eor != U_RIGHT_TO_LEFT_ARABIC) {
ggarenef026b22005-07-06 00:21:53 +00001237 // run of L before neutrals, neutrals take embedding dir (N2)
darin7ab31092006-05-10 04:59:57 +00001238 if (bidi.context->dir() == U_RIGHT_TO_LEFT || bidi.status.lastStrong == U_RIGHT_TO_LEFT
1239 || bidi.status.lastStrong == U_RIGHT_TO_LEFT_ARABIC) {
ggarenef026b22005-07-06 00:21:53 +00001240 // the embedding direction is R
1241 // close the L run
hyatt275d0702005-11-03 23:53:57 +00001242 appendRun(bidi);
ggarenef026b22005-07-06 00:21:53 +00001243 // neutrals become an R run
darin7ab31092006-05-10 04:59:57 +00001244 bidi.dir = U_RIGHT_TO_LEFT;
kociendabb0c24b2001-08-24 14:24:40 +00001245 } else {
ggarenef026b22005-07-06 00:21:53 +00001246 // the embedding direction is L
1247 // append neutrals to the L run and close it
darin7ab31092006-05-10 04:59:57 +00001248 bidi.dir = U_LEFT_TO_RIGHT;
kociendabb0c24b2001-08-24 14:24:40 +00001249 }
1250 }
eseidel789896f2005-11-27 22:52:09 +00001251 bidi.eor = bidi.last;
1252 appendRun(bidi);
kociendabb0c24b2001-08-24 14:24:40 +00001253 default:
1254 break;
hyatt275d0702005-11-03 23:53:57 +00001255 }
eseidel789896f2005-11-27 22:52:09 +00001256 bidi.eor = bidi.current;
darin7ab31092006-05-10 04:59:57 +00001257 bidi.status.eor = U_ARABIC_NUMBER;
1258 if (bidi.dir == U_OTHER_NEUTRAL)
1259 bidi.dir = U_ARABIC_NUMBER;
kociendabb0c24b2001-08-24 14:24:40 +00001260 break;
darin7ab31092006-05-10 04:59:57 +00001261 case U_EUROPEAN_NUMBER_SEPARATOR:
1262 case U_COMMON_NUMBER_SEPARATOR:
kociendabb0c24b2001-08-24 14:24:40 +00001263 break;
darin7ab31092006-05-10 04:59:57 +00001264 case U_EUROPEAN_NUMBER_TERMINATOR:
1265 if (bidi.status.last == U_EUROPEAN_NUMBER) {
1266 dirCurrent = U_EUROPEAN_NUMBER;
hyatt275d0702005-11-03 23:53:57 +00001267 bidi.eor = bidi.current;
1268 bidi.status.eor = dirCurrent;
darin7ab31092006-05-10 04:59:57 +00001269 } else if (bidi.status.last != U_EUROPEAN_NUMBER_TERMINATOR)
darine4fa9e22005-12-16 18:18:50 +00001270 bidi.lastBeforeET = emptyRun ? bidi.eor : bidi.last;
kociendabb0c24b2001-08-24 14:24:40 +00001271 break;
1272
1273 // boundary neutrals should be ignored
darin7ab31092006-05-10 04:59:57 +00001274 case U_BOUNDARY_NEUTRAL:
darinef0c09f2005-10-09 01:46:17 +00001275 if (bidi.eor == bidi.last)
1276 bidi.eor = bidi.current;
kociendabb0c24b2001-08-24 14:24:40 +00001277 break;
1278 // neutrals
darin7ab31092006-05-10 04:59:57 +00001279 case U_BLOCK_SEPARATOR:
kociendabb0c24b2001-08-24 14:24:40 +00001280 // ### what do we do with newline and paragraph seperators that come to here?
1281 break;
darin7ab31092006-05-10 04:59:57 +00001282 case U_SEGMENT_SEPARATOR:
kociendabb0c24b2001-08-24 14:24:40 +00001283 // ### implement rule L1
1284 break;
darin7ab31092006-05-10 04:59:57 +00001285 case U_WHITE_SPACE_NEUTRAL:
hyattfe99c872003-07-31 22:25:29 +00001286 break;
darin7ab31092006-05-10 04:59:57 +00001287 case U_OTHER_NEUTRAL:
kociendabb0c24b2001-08-24 14:24:40 +00001288 break;
1289 default:
1290 break;
1291 }
1292
eseidel789896f2005-11-27 22:52:09 +00001293 if (pastEnd) {
1294 if (bidi.eor == bidi.current) {
1295 if (!bidi.reachedEndOfLine) {
1296 bidi.eor = bidi.endOfLine;
1297 switch (bidi.status.eor) {
darin7ab31092006-05-10 04:59:57 +00001298 case U_LEFT_TO_RIGHT:
1299 case U_RIGHT_TO_LEFT:
1300 case U_ARABIC_NUMBER:
eseidel789896f2005-11-27 22:52:09 +00001301 bidi.dir = bidi.status.eor;
1302 break;
darin7ab31092006-05-10 04:59:57 +00001303 case U_EUROPEAN_NUMBER:
1304 bidi.dir = bidi.status.lastStrong == U_LEFT_TO_RIGHT ? U_LEFT_TO_RIGHT : U_EUROPEAN_NUMBER;
eseidel789896f2005-11-27 22:52:09 +00001305 break;
1306 default:
1307 assert(false);
1308 }
1309 appendRun(bidi);
1310 }
1311 bidi = stateAtEnd;
darin7ab31092006-05-10 04:59:57 +00001312 bidi.dir = U_OTHER_NEUTRAL;
eseidel789896f2005-11-27 22:52:09 +00001313 break;
1314 }
1315 }
kociendabb0c24b2001-08-24 14:24:40 +00001316
1317 // set status.last as needed.
hyatt275d0702005-11-03 23:53:57 +00001318 switch (dirCurrent) {
darin7ab31092006-05-10 04:59:57 +00001319 case U_EUROPEAN_NUMBER_TERMINATOR:
1320 if (bidi.status.last != U_EUROPEAN_NUMBER)
1321 bidi.status.last = U_EUROPEAN_NUMBER_TERMINATOR;
ggarenef026b22005-07-06 00:21:53 +00001322 break;
darin7ab31092006-05-10 04:59:57 +00001323 case U_EUROPEAN_NUMBER_SEPARATOR:
1324 case U_COMMON_NUMBER_SEPARATOR:
1325 case U_SEGMENT_SEPARATOR:
1326 case U_WHITE_SPACE_NEUTRAL:
1327 case U_OTHER_NEUTRAL:
hyatt275d0702005-11-03 23:53:57 +00001328 switch(bidi.status.last) {
darin7ab31092006-05-10 04:59:57 +00001329 case U_LEFT_TO_RIGHT:
1330 case U_RIGHT_TO_LEFT:
1331 case U_RIGHT_TO_LEFT_ARABIC:
1332 case U_EUROPEAN_NUMBER:
1333 case U_ARABIC_NUMBER:
mjsfe301d72003-10-02 18:46:18 +00001334 bidi.status.last = dirCurrent;
kociendabb0c24b2001-08-24 14:24:40 +00001335 break;
1336 default:
darin7ab31092006-05-10 04:59:57 +00001337 bidi.status.last = U_OTHER_NEUTRAL;
kociendabb0c24b2001-08-24 14:24:40 +00001338 }
1339 break;
darin7ab31092006-05-10 04:59:57 +00001340 case U_DIR_NON_SPACING_MARK:
1341 case U_BOUNDARY_NEUTRAL:
1342 case U_RIGHT_TO_LEFT_EMBEDDING:
1343 case U_LEFT_TO_RIGHT_EMBEDDING:
1344 case U_RIGHT_TO_LEFT_OVERRIDE:
1345 case U_LEFT_TO_RIGHT_OVERRIDE:
1346 case U_POP_DIRECTIONAL_FORMAT:
kociendabb0c24b2001-08-24 14:24:40 +00001347 // ignore these
1348 break;
darin7ab31092006-05-10 04:59:57 +00001349 case U_EUROPEAN_NUMBER:
rjw2028aa42003-01-14 21:14:16 +00001350 // fall through
kociendabb0c24b2001-08-24 14:24:40 +00001351 default:
mjsfe301d72003-10-02 18:46:18 +00001352 bidi.status.last = dirCurrent;
hyatt275d0702005-11-03 23:53:57 +00001353 }
kociendabb0c24b2001-08-24 14:24:40 +00001354
mjsfe301d72003-10-02 18:46:18 +00001355 bidi.last = bidi.current;
mjs6f821c82002-03-22 00:31:57 +00001356
darin7ab31092006-05-10 04:59:57 +00001357 if (emptyRun && !(dirCurrent == U_RIGHT_TO_LEFT_EMBEDDING
1358 || dirCurrent == U_LEFT_TO_RIGHT_EMBEDDING
1359 || dirCurrent == U_RIGHT_TO_LEFT_OVERRIDE
1360 || dirCurrent == U_LEFT_TO_RIGHT_OVERRIDE
1361 || dirCurrent == U_POP_DIRECTIONAL_FORMAT)) {
darin54008922006-01-13 16:39:05 +00001362 bidi.sor = bidi.current;
1363 emptyRun = false;
1364 }
darinb70665a2002-04-15 23:43:21 +00001365
darin54008922006-01-13 16:39:05 +00001366 // this causes the operator ++ to open and close embedding levels as needed
1367 // for the CSS unicode-bidi property
1368 bidi.adjustEmbedding = true;
hyatt275d0702005-11-03 23:53:57 +00001369 bidi.current.increment(bidi);
darin54008922006-01-13 16:39:05 +00001370 bidi.adjustEmbedding = false;
darin7ab31092006-05-10 04:59:57 +00001371 if (emptyRun && (dirCurrent == U_RIGHT_TO_LEFT_EMBEDDING
1372 || dirCurrent == U_LEFT_TO_RIGHT_EMBEDDING
1373 || dirCurrent == U_RIGHT_TO_LEFT_OVERRIDE
1374 || dirCurrent == U_LEFT_TO_RIGHT_OVERRIDE
1375 || dirCurrent == U_POP_DIRECTIONAL_FORMAT)) {
darinef0c09f2005-10-09 01:46:17 +00001376 // exclude the embedding char itself from the new run so that ATSUI will never see it
1377 bidi.eor.obj = 0;
1378 bidi.last = bidi.current;
1379 bidi.sor = bidi.current;
1380 }
darinb70665a2002-04-15 23:43:21 +00001381
eseidel789896f2005-11-27 22:52:09 +00001382 if (!pastEnd && (bidi.current == end || bidi.current.atEnd())) {
1383 if (emptyRun)
1384 break;
1385 stateAtEnd = bidi;
1386 bidi.endOfLine = bidi.last;
1387 pastEnd = true;
eseidel789896f2005-11-27 22:52:09 +00001388 }
mjs6f821c82002-03-22 00:31:57 +00001389 }
kociendabb0c24b2001-08-24 14:24:40 +00001390
kociendabb0c24b2001-08-24 14:24:40 +00001391 // reorder line according to run structure...
kociendabb0c24b2001-08-24 14:24:40 +00001392 // do not reverse for visually ordered web sites
hyatt275d0702005-11-03 23:53:57 +00001393 if (!style()->visuallyOrdered()) {
eseidel789896f2005-11-27 22:52:09 +00001394
1395 // first find highest and lowest levels
darin479ef852006-01-30 06:06:32 +00001396 unsigned char levelLow = 128;
1397 unsigned char levelHigh = 0;
eseidel789896f2005-11-27 22:52:09 +00001398 BidiRun* r = sFirstBidiRun;
1399 while (r) {
1400 if (r->level > levelHigh)
1401 levelHigh = r->level;
1402 if (r->level < levelLow)
1403 levelLow = r->level;
1404 r = r->nextRun;
1405 }
1406
1407 // implements reordering of the line (L2 according to Bidi spec):
1408 // L2. From the highest level found in the text to the lowest odd level on each line,
1409 // reverse any contiguous sequence of characters that are at that level or higher.
1410
1411 // reversing is only done up to the lowest odd level
1412 if (!(levelLow%2))
1413 levelLow++;
1414
1415 int count = sBidiRunCount - 1;
1416
hyatt275d0702005-11-03 23:53:57 +00001417 while (levelHigh >= levelLow) {
kociendabb0c24b2001-08-24 14:24:40 +00001418 int i = 0;
hyatt2f1e7102003-02-20 01:27:03 +00001419 BidiRun* currRun = sFirstBidiRun;
hyatt275d0702005-11-03 23:53:57 +00001420 while (i < count) {
1421 while (i < count && currRun && currRun->level < levelHigh) {
kociendabb0c24b2001-08-24 14:24:40 +00001422 i++;
hyatt2f1e7102003-02-20 01:27:03 +00001423 currRun = currRun->nextRun;
kociendabb0c24b2001-08-24 14:24:40 +00001424 }
hyatt2f1e7102003-02-20 01:27:03 +00001425 int start = i;
hyatt275d0702005-11-03 23:53:57 +00001426 while (i <= count && currRun && currRun->level >= levelHigh) {
hyatt2f1e7102003-02-20 01:27:03 +00001427 i++;
1428 currRun = currRun->nextRun;
1429 }
1430 int end = i-1;
1431 reverseRuns(start, end);
kociendabb0c24b2001-08-24 14:24:40 +00001432 }
1433 levelHigh--;
1434 }
1435 }
eseidel789896f2005-11-27 22:52:09 +00001436 bidi.endOfLine.obj = 0;
hyatt4b381692003-03-10 21:11:59 +00001437}
kociendabb0c24b2001-08-24 14:24:40 +00001438
hyatt275d0702005-11-03 23:53:57 +00001439static void buildCompactRuns(RenderObject* compactObj, BidiState& bidi)
hyatt4b381692003-03-10 21:11:59 +00001440{
1441 sBuildingCompactRuns = true;
1442 if (!compactObj->isRenderBlock()) {
1443 // Just append a run for our object.
1444 isLineEmpty = false;
eseidel789896f2005-11-27 22:52:09 +00001445 addRun(new (compactObj->renderArena()) BidiRun(0, compactObj->length(), compactObj, bidi.context.get(), bidi.dir));
hyatt4b381692003-03-10 21:11:59 +00001446 }
1447 else {
1448 // Format the compact like it is its own single line. We build up all the runs for
1449 // the little compact and then reorder them for bidi.
1450 RenderBlock* compactBlock = static_cast<RenderBlock*>(compactObj);
hyatt275d0702005-11-03 23:53:57 +00001451 bidi.adjustEmbedding = true;
1452 BidiIterator start(compactBlock, bidiFirst(compactBlock, bidi), 0);
1453 bidi.adjustEmbedding = false;
darin8341f372003-04-29 18:30:31 +00001454 BidiIterator end = start;
hyattffe78712003-02-11 01:59:29 +00001455
hyatt4b381692003-03-10 21:11:59 +00001456 betweenMidpoints = false;
1457 isLineEmpty = true;
hyatt0c3a9862004-02-23 21:26:26 +00001458 previousLineBrokeCleanly = true;
hyatt01eff982003-03-14 20:13:23 +00001459
mjsfe301d72003-10-02 18:46:18 +00001460 end = compactBlock->findNextLineBreak(start, bidi);
hyatt4b381692003-03-10 21:11:59 +00001461 if (!isLineEmpty)
mjsfe301d72003-10-02 18:46:18 +00001462 compactBlock->bidiReorderLine(start, end, bidi);
hyatt4b381692003-03-10 21:11:59 +00001463 }
hyatt275d0702005-11-03 23:53:57 +00001464
hyatt4b381692003-03-10 21:11:59 +00001465 sCompactFirstBidiRun = sFirstBidiRun;
1466 sCompactLastBidiRun = sLastBidiRun;
1467 sCompactBidiRunCount = sBidiRunCount;
1468
1469 sNumMidpoints = 0;
1470 sCurrMidpoint = 0;
1471 betweenMidpoints = false;
1472 sBuildingCompactRuns = false;
kociendabb0c24b2001-08-24 14:24:40 +00001473}
1474
hyatt673184e2006-01-14 22:07:08 +00001475IntRect RenderBlock::layoutInlineChildren(bool relayoutChildren)
kociendabb0c24b2001-08-24 14:24:40 +00001476{
mjsfe301d72003-10-02 18:46:18 +00001477 BidiState bidi;
1478
hyatt0c3a9862004-02-23 21:26:26 +00001479 bool useRepaintRect = false;
hyatt673184e2006-01-14 22:07:08 +00001480 IntRect repaintRect(0,0,0,0);
hyatt0c3a9862004-02-23 21:26:26 +00001481
hyatta70560a2002-11-20 01:53:20 +00001482 m_overflowHeight = 0;
1483
kociendabb0c24b2001-08-24 14:24:40 +00001484 invalidateVerticalPositions();
hyattdd7dfe92003-11-16 20:48:12 +00001485
1486 m_height = borderTop() + paddingTop();
1487 int toAdd = borderBottom() + paddingBottom();
hyatt0bab2a62004-04-13 22:57:12 +00001488 if (includeScrollbarSize())
hyattdd7dfe92003-11-16 20:48:12 +00001489 toAdd += m_layer->horizontalScrollbarHeight();
1490
hyatt0c3a9862004-02-23 21:26:26 +00001491 // Figure out if we should clear out our line boxes.
1492 // FIXME: Handle resize eventually!
1493 // FIXME: Do something better when floats are present.
1494 bool fullLayout = !firstLineBox() || !firstChild() || selfNeedsLayout() || relayoutChildren || containsFloats();
1495 if (fullLayout)
1496 deleteLineBoxes();
hyatted77ad82004-06-15 07:21:23 +00001497
1498 // Text truncation only kicks in if your overflow isn't visible and your text-overflow-mode isn't
1499 // clip.
1500 // FIXME: CSS3 says that descendants that are clipped must also know how to truncate. This is insanely
1501 // difficult to figure out (especially in the middle of doing layout), and is really an esoteric pile of nonsense
1502 // anyway, so we won't worry about following the draft here.
1503 bool hasTextOverflow = style()->textOverflow() && hasOverflowClip();
hyatt33f8d492002-11-12 21:44:52 +00001504
hyatted77ad82004-06-15 07:21:23 +00001505 // Walk all the lines and delete our ellipsis line boxes if they exist.
1506 if (hasTextOverflow)
1507 deleteEllipsisLineBoxes();
1508
hyatt0dd79772004-10-26 21:08:41 +00001509 int oldLineBottom = lastRootBox() ? lastRootBox()->bottomOverflow() : m_height;
hyatt2161d952005-01-12 23:33:21 +00001510 int startLineBottom = 0;
1511
hyattffe78712003-02-11 01:59:29 +00001512 if (firstChild()) {
kociendabb0c24b2001-08-24 14:24:40 +00001513 // layout replaced elements
hyattffe78712003-02-11 01:59:29 +00001514 bool endOfInline = false;
hyatt275d0702005-11-03 23:53:57 +00001515 RenderObject *o = bidiFirst(this, bidi, false);
hyatt0c3a9862004-02-23 21:26:26 +00001516 bool hasFloat = false;
1517 while (o) {
1518 if (o->isReplaced() || o->isFloating() || o->isPositioned()) {
hyatt89377f12002-12-13 21:24:40 +00001519 if (relayoutChildren || o->style()->width().isPercent() || o->style()->height().isPercent())
hyattadbb2ef2003-11-18 09:21:44 +00001520 o->setChildNeedsLayout(true, false);
hyatt3ac01352003-03-22 01:37:33 +00001521 if (o->isPositioned())
1522 o->containingBlock()->insertPositionedObject(o);
hyatt0c3a9862004-02-23 21:26:26 +00001523 else {
1524 if (o->isFloating())
1525 hasFloat = true;
1526 else if (fullLayout || o->needsLayout()) // Replaced elements
1527 o->dirtyLineBoxes(fullLayout);
hyatt390427a2003-05-03 18:33:42 +00001528 o->layoutIfNeeded();
hyatt0c3a9862004-02-23 21:26:26 +00001529 }
kociendabb0c24b2001-08-24 14:24:40 +00001530 }
hyatt0c3a9862004-02-23 21:26:26 +00001531 else if (o->isText() || (o->isInlineFlow() && !endOfInline)) {
1532 if (fullLayout || o->selfNeedsLayout())
1533 o->dirtyLineBoxes(fullLayout);
hyattf4fe67f2003-10-07 04:43:23 +00001534 o->setNeedsLayout(false);
hyattf4fe67f2003-10-07 04:43:23 +00001535 }
hyatt275d0702005-11-03 23:53:57 +00001536 o = bidiNext(this, o, bidi, false, &endOfInline);
kociendabb0c24b2001-08-24 14:24:40 +00001537 }
1538
hyatt0c3a9862004-02-23 21:26:26 +00001539 if (hasFloat)
1540 fullLayout = true; // FIXME: Will need to find a way to optimize floats some day.
1541
hyatt837eb362004-05-21 22:17:10 +00001542 if (fullLayout && !selfNeedsLayout()) {
hyatt0c3a9862004-02-23 21:26:26 +00001543 setNeedsLayout(true, false); // Mark ourselves as needing a full layout. This way we'll repaint like
1544 // we're supposed to.
hyatt837eb362004-05-21 22:17:10 +00001545 if (!document()->view()->needsFullRepaint() && m_layer) {
1546 // Because we waited until we were already inside layout to discover
1547 // that the block really needed a full layout, we missed our chance to repaint the layer
1548 // before layout started. Luckily the layer has cached the repaint rect for its original
1549 // position and size, and so we can use that to make a repaint happen now.
hyattd8048342006-05-31 01:48:18 +00001550 RenderView* c = view();
hyatt837eb362004-05-21 22:17:10 +00001551 if (c && !c->printingMode())
1552 c->repaintViewRectangle(m_layer->repaintRect());
1553 }
1554 }
hyatt0c3a9862004-02-23 21:26:26 +00001555
kociendabb0c24b2001-08-24 14:24:40 +00001556 BidiContext *startEmbed;
hyatt275d0702005-11-03 23:53:57 +00001557 if (style()->direction() == LTR) {
darin7ab31092006-05-10 04:59:57 +00001558 startEmbed = new BidiContext( 0, U_LEFT_TO_RIGHT, NULL, style()->unicodeBidi() == Override );
1559 bidi.status.eor = U_LEFT_TO_RIGHT;
kociendabb0c24b2001-08-24 14:24:40 +00001560 } else {
darin7ab31092006-05-10 04:59:57 +00001561 startEmbed = new BidiContext( 1, U_RIGHT_TO_LEFT, NULL, style()->unicodeBidi() == Override );
1562 bidi.status.eor = U_RIGHT_TO_LEFT;
kociendabb0c24b2001-08-24 14:24:40 +00001563 }
kociendabb0c24b2001-08-24 14:24:40 +00001564
mjs08191d52006-03-06 02:53:41 +00001565 bidi.status.lastStrong = startEmbed->dir();
1566 bidi.status.last = startEmbed->dir();
1567 bidi.status.eor = startEmbed->dir();
mjsfe301d72003-10-02 18:46:18 +00001568 bidi.context = startEmbed;
darin7ab31092006-05-10 04:59:57 +00001569 bidi.dir = U_OTHER_NEUTRAL;
hyatt33f8d492002-11-12 21:44:52 +00001570
hyatt85586af2003-02-19 23:22:42 +00001571 if (!smidpoints)
darinb9481ed2006-03-20 02:57:59 +00001572 smidpoints = new DeprecatedArray<BidiIterator>;
hyatt0c3a9862004-02-23 21:26:26 +00001573
hyatt85586af2003-02-19 23:22:42 +00001574 sNumMidpoints = 0;
1575 sCurrMidpoint = 0;
hyatt4b381692003-03-10 21:11:59 +00001576 sCompactFirstBidiRun = sCompactLastBidiRun = 0;
1577 sCompactBidiRunCount = 0;
hyatt01eff982003-03-14 20:13:23 +00001578
hyatt0c3a9862004-02-23 21:26:26 +00001579 // We want to skip ahead to the first dirty line
1580 BidiIterator start;
1581 RootInlineBox* startLine = determineStartPosition(fullLayout, start, bidi);
1582
1583 // We also find the first clean line and extract these lines. We will add them back
1584 // if we determine that we're able to synchronize after handling all our dirty lines.
1585 BidiIterator cleanLineStart;
eseidel789896f2005-11-27 22:52:09 +00001586 BidiStatus cleanLineBidiStatus;
1587 BidiContext* cleanLineBidiContext;
hyatt0c3a9862004-02-23 21:26:26 +00001588 int endLineYPos;
1589 RootInlineBox* endLine = (fullLayout || !startLine) ?
eseidel789896f2005-11-27 22:52:09 +00001590 0 : determineEndPosition(startLine, cleanLineStart, cleanLineBidiStatus, cleanLineBidiContext, endLineYPos);
1591 if (endLine && cleanLineBidiContext)
1592 cleanLineBidiContext->ref();
hyatt0c3a9862004-02-23 21:26:26 +00001593 if (startLine) {
1594 useRepaintRect = true;
hyatt2161d952005-01-12 23:33:21 +00001595 startLineBottom = startLine->bottomOverflow();
darin7bd70952006-04-13 07:07:34 +00001596 repaintRect.setY(min(m_height, startLine->topOverflow()));
hyatt0c3a9862004-02-23 21:26:26 +00001597 RenderArena* arena = renderArena();
1598 RootInlineBox* box = startLine;
1599 while (box) {
1600 RootInlineBox* next = box->nextRootBox();
1601 box->deleteLine(arena);
1602 box = next;
1603 }
1604 startLine = 0;
1605 }
1606
1607 BidiIterator end = start;
1608
1609 bool endLineMatched = false;
1610 while (!end.atEnd()) {
kociendabb0c24b2001-08-24 14:24:40 +00001611 start = end;
eseidel789896f2005-11-27 22:52:09 +00001612 if (endLine && (endLineMatched = matchedEndLine(start, bidi.status, bidi.context.get(), cleanLineStart, cleanLineBidiStatus, cleanLineBidiContext, endLine, endLineYPos)))
hyatt0c3a9862004-02-23 21:26:26 +00001613 break;
1614
hyatt33f8d492002-11-12 21:44:52 +00001615 betweenMidpoints = false;
1616 isLineEmpty = true;
hyatt75c30b62004-07-16 20:15:34 +00001617 if (m_firstLine && firstChild() && firstChild()->isCompact() && firstChild()->isRenderBlock()) {
mjsfe301d72003-10-02 18:46:18 +00001618 buildCompactRuns(firstChild(), bidi);
hyatt4b381692003-03-10 21:11:59 +00001619 start.obj = firstChild()->nextSibling();
1620 end = start;
1621 }
mjsfe301d72003-10-02 18:46:18 +00001622 end = findNextLineBreak(start, bidi);
eseidel789896f2005-11-27 22:52:09 +00001623 if (start.atEnd())
1624 break;
hyatt33f8d492002-11-12 21:44:52 +00001625 if (!isLineEmpty) {
mjsfe301d72003-10-02 18:46:18 +00001626 bidiReorderLine(start, end, bidi);
hyatt4b381692003-03-10 21:11:59 +00001627
1628 // Now that the runs have been ordered, we create the line boxes.
1629 // At the same time we figure out where border/padding/margin should be applied for
1630 // inline flow boxes.
1631 if (sCompactFirstBidiRun) {
1632 // We have a compact line sharing this line. Link the compact runs
1633 // to our runs to create a single line of runs.
1634 sCompactLastBidiRun->nextRun = sFirstBidiRun;
1635 sFirstBidiRun = sCompactFirstBidiRun;
1636 sBidiRunCount += sCompactBidiRunCount;
1637 }
hyatt4b381692003-03-10 21:11:59 +00001638
hyatt0c3a9862004-02-23 21:26:26 +00001639 RootInlineBox* lineBox = 0;
hyattdfb09052003-03-11 02:58:59 +00001640 if (sBidiRunCount) {
hyatt0c3a9862004-02-23 21:26:26 +00001641 lineBox = constructLine(start, end);
hyattdfb09052003-03-11 02:58:59 +00001642 if (lineBox) {
hyatt0c3a9862004-02-23 21:26:26 +00001643 lineBox->setEndsWithBreak(previousLineBrokeCleanly);
1644
hyattdfb09052003-03-11 02:58:59 +00001645 // Now we position all of our text runs horizontally.
mjsfe301d72003-10-02 18:46:18 +00001646 computeHorizontalPositionsForLine(lineBox, bidi);
hyattdfb09052003-03-11 02:58:59 +00001647
1648 // Now position our text runs vertically.
1649 computeVerticalPositionsForLine(lineBox);
1650
1651 deleteBidiRuns(renderArena());
1652 }
1653 }
hyatt4b381692003-03-10 21:11:59 +00001654
hyatt0c05e102006-04-14 08:15:00 +00001655 if (end == start) {
hyatt275d0702005-11-03 23:53:57 +00001656 bidi.adjustEmbedding = true;
mjsfe301d72003-10-02 18:46:18 +00001657 end.increment(bidi);
hyatt275d0702005-11-03 23:53:57 +00001658 bidi.adjustEmbedding = false;
hyatt33f8d492002-11-12 21:44:52 +00001659 }
hyatt35a85e52003-02-14 19:43:07 +00001660
hyatt0c3a9862004-02-23 21:26:26 +00001661 if (lineBox)
eseidel789896f2005-11-27 22:52:09 +00001662 lineBox->setLineBreakInfo(end.obj, end.pos, &bidi.status, bidi.context.get());
hyatt0c3a9862004-02-23 21:26:26 +00001663
hyatt35a85e52003-02-14 19:43:07 +00001664 m_firstLine = false;
hyatt33f8d492002-11-12 21:44:52 +00001665 newLine();
darinb70665a2002-04-15 23:43:21 +00001666 }
hyatt74eec4d2003-03-23 08:02:47 +00001667
hyatt85586af2003-02-19 23:22:42 +00001668 sNumMidpoints = 0;
1669 sCurrMidpoint = 0;
hyatt4b381692003-03-10 21:11:59 +00001670 sCompactFirstBidiRun = sCompactLastBidiRun = 0;
1671 sCompactBidiRunCount = 0;
kociendabb0c24b2001-08-24 14:24:40 +00001672 }
hyatt0c3a9862004-02-23 21:26:26 +00001673
1674 if (endLine) {
1675 if (endLineMatched) {
hyatt2161d952005-01-12 23:33:21 +00001676 // Note our current y-position for correct repainting when no lines move. If no lines move, we still have to
1677 // repaint up to the maximum of the bottom overflow of the old start line or the bottom overflow of the new last line.
darin7bd70952006-04-13 07:07:34 +00001678 int currYPos = max(startLineBottom, m_height);
hyatt2161d952005-01-12 23:33:21 +00001679 if (lastRootBox())
darin7bd70952006-04-13 07:07:34 +00001680 currYPos = max(currYPos, lastRootBox()->bottomOverflow());
hyatt2161d952005-01-12 23:33:21 +00001681
hyatt0c3a9862004-02-23 21:26:26 +00001682 // Attach all the remaining lines, and then adjust their y-positions as needed.
1683 for (RootInlineBox* line = endLine; line; line = line->nextRootBox())
1684 line->attachLine();
1685
1686 // Now apply the offset to each line if needed.
1687 int delta = m_height - endLineYPos;
harrison208ea792005-07-29 23:42:59 +00001688 if (delta) {
hyatt0c3a9862004-02-23 21:26:26 +00001689 for (RootInlineBox* line = endLine; line; line = line->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00001690 line->adjustPosition(0, delta);
harrison208ea792005-07-29 23:42:59 +00001691 }
hyatt0c3a9862004-02-23 21:26:26 +00001692 m_height = lastRootBox()->blockHeight();
darin7bd70952006-04-13 07:07:34 +00001693 m_overflowHeight = max(m_height, m_overflowHeight);
hyatt0c3a9862004-02-23 21:26:26 +00001694 int bottomOfLine = lastRootBox()->bottomOverflow();
1695 if (bottomOfLine > m_height && bottomOfLine > m_overflowHeight)
1696 m_overflowHeight = bottomOfLine;
1697 if (delta)
darin7bd70952006-04-13 07:07:34 +00001698 repaintRect.setHeight(max(m_overflowHeight-delta, m_overflowHeight) - repaintRect.y());
hyatt0c3a9862004-02-23 21:26:26 +00001699 else
1700 repaintRect.setHeight(currYPos - repaintRect.y());
1701 }
1702 else {
1703 // Delete all the remaining lines.
darin7bd70952006-04-13 07:07:34 +00001704 m_overflowHeight = max(m_height, m_overflowHeight);
hyatt0c3a9862004-02-23 21:26:26 +00001705 InlineRunBox* line = endLine;
1706 RenderArena* arena = renderArena();
1707 while (line) {
1708 InlineRunBox* next = line->nextLineBox();
1709 if (!next)
darin7bd70952006-04-13 07:07:34 +00001710 repaintRect.setHeight(max(m_overflowHeight, line->bottomOverflow()) - repaintRect.y());
hyatt0c3a9862004-02-23 21:26:26 +00001711 line->deleteLine(arena);
1712 line = next;
1713 }
1714 }
eseidel789896f2005-11-27 22:52:09 +00001715 if (cleanLineBidiContext)
1716 cleanLineBidiContext->deref();
hyatt0c3a9862004-02-23 21:26:26 +00001717 }
kociendabb0c24b2001-08-24 14:24:40 +00001718 }
hyatt85586af2003-02-19 23:22:42 +00001719
1720 sNumMidpoints = 0;
1721 sCurrMidpoint = 0;
hyatta70560a2002-11-20 01:53:20 +00001722
hyattefa00882003-03-22 23:27:03 +00001723 // in case we have a float on the last line, it might not be positioned up to now.
1724 // This has to be done before adding in the bottom border/padding, or the float will
1725 // include the padding incorrectly. -dwh
1726 positionNewFloats();
1727
hyatta70560a2002-11-20 01:53:20 +00001728 // Now add in the bottom border/padding.
kociendabb0c24b2001-08-24 14:24:40 +00001729 m_height += toAdd;
1730
hyatta70560a2002-11-20 01:53:20 +00001731 // Always make sure this is at least our height.
darin7bd70952006-04-13 07:07:34 +00001732 m_overflowHeight = max(m_height, m_overflowHeight);
hyatta70560a2002-11-20 01:53:20 +00001733
hyattb4b20872004-10-20 21:34:01 +00001734 // See if any lines spill out of the block. If so, we need to update our overflow width.
1735 checkLinesForOverflow();
1736
hyatt0c3a9862004-02-23 21:26:26 +00001737 if (useRepaintRect) {
eseidelcf075d02006-03-26 22:58:26 +00001738 repaintRect.setX(m_overflowLeft);
darin7bd70952006-04-13 07:07:34 +00001739 repaintRect.setWidth(max((int)m_width, m_overflowWidth) - m_overflowLeft);
hyatt0c3a9862004-02-23 21:26:26 +00001740 if (repaintRect.height() == 0)
darin7bd70952006-04-13 07:07:34 +00001741 repaintRect.setHeight(max(oldLineBottom, m_overflowHeight) - repaintRect.y());
hyattea43b002006-04-19 18:47:03 +00001742 if (hasOverflowClip())
1743 // Don't allow this rect to spill out of our overflow box.
1744 repaintRect.intersect(IntRect(0, 0, m_width, m_height));
hyatt0c3a9862004-02-23 21:26:26 +00001745 }
hyatta6960b12004-12-07 02:09:10 +00001746
adele7a470a72006-04-20 22:22:14 +00001747 if (!firstLineBox() && hasLineIfEmpty())
kocienda616320e2004-04-01 00:34:07 +00001748 m_height += lineHeight(true);
hyatted77ad82004-06-15 07:21:23 +00001749
1750 // See if we have any lines that spill out of our block. If we do, then we will possibly need to
1751 // truncate text.
1752 if (hasTextOverflow)
1753 checkLinesForTextOverflow();
1754
hyatt0c3a9862004-02-23 21:26:26 +00001755 return repaintRect;
1756
darinb70665a2002-04-15 23:43:21 +00001757#if BIDI_DEBUG > 1
1758 kdDebug(6041) << " ------- bidi end " << this << " -------" << endl;
darinf028f812002-06-10 20:08:04 +00001759#endif
kociendabb0c24b2001-08-24 14:24:40 +00001760}
1761
hyatt0c3a9862004-02-23 21:26:26 +00001762RootInlineBox* RenderBlock::determineStartPosition(bool fullLayout, BidiIterator& start, BidiState& bidi)
1763{
1764 RootInlineBox* curr = 0;
1765 RootInlineBox* last = 0;
1766 RenderObject* startObj = 0;
1767 int pos = 0;
1768
1769 if (fullLayout) {
1770 // Nuke all our lines.
1771 if (firstRootBox()) {
1772 RenderArena* arena = renderArena();
1773 curr = firstRootBox();
1774 while (curr) {
1775 RootInlineBox* next = curr->nextRootBox();
1776 curr->deleteLine(arena);
1777 curr = next;
1778 }
1779 KHTMLAssert(!m_firstLineBox && !m_lastLineBox);
1780 }
eseidel789896f2005-11-27 22:52:09 +00001781 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001782 for (curr = firstRootBox(); curr && !curr->isDirty(); curr = curr->nextRootBox());
1783 if (curr) {
1784 // We have a dirty line.
1785 if (curr->prevRootBox()) {
1786 // We have a previous line.
1787 if (!curr->prevRootBox()->endsWithBreak())
1788 curr = curr->prevRootBox(); // The previous line didn't break cleanly, so treat it as dirty also.
1789 }
eseidel789896f2005-11-27 22:52:09 +00001790 } else {
hyatt0c3a9862004-02-23 21:26:26 +00001791 // No dirty lines were found.
1792 // If the last line didn't break cleanly, treat it as dirty.
1793 if (lastRootBox() && !lastRootBox()->endsWithBreak())
1794 curr = lastRootBox();
1795 }
1796
1797 // If we have no dirty lines, then last is just the last root box.
1798 last = curr ? curr->prevRootBox() : lastRootBox();
1799 }
1800
1801 m_firstLine = !last;
1802 previousLineBrokeCleanly = !last || last->endsWithBreak();
1803 if (last) {
1804 m_height = last->blockHeight();
1805 int bottomOfLine = last->bottomOverflow();
1806 if (bottomOfLine > m_height && bottomOfLine > m_overflowHeight)
1807 m_overflowHeight = bottomOfLine;
1808 startObj = last->lineBreakObj();
1809 pos = last->lineBreakPos();
eseidel789896f2005-11-27 22:52:09 +00001810 bidi.status = last->lineBreakBidiStatus();
1811 bidi.context = last->lineBreakBidiContext();
darindde01502005-12-18 22:55:35 +00001812 } else {
1813 bidi.adjustEmbedding = true;
hyatt275d0702005-11-03 23:53:57 +00001814 startObj = bidiFirst(this, bidi, 0);
darindde01502005-12-18 22:55:35 +00001815 bidi.adjustEmbedding = false;
1816 }
hyatt0c3a9862004-02-23 21:26:26 +00001817
hyatt0c3a9862004-02-23 21:26:26 +00001818 start = BidiIterator(this, startObj, pos);
hyatt0c3a9862004-02-23 21:26:26 +00001819
1820 return curr;
1821}
1822
1823RootInlineBox* RenderBlock::determineEndPosition(RootInlineBox* startLine, BidiIterator& cleanLineStart,
eseidel789896f2005-11-27 22:52:09 +00001824 BidiStatus& cleanLineBidiStatus, BidiContext*& cleanLineBidiContext,
hyatt0c3a9862004-02-23 21:26:26 +00001825 int& yPos)
1826{
1827 RootInlineBox* last = 0;
hyatta6960b12004-12-07 02:09:10 +00001828 if (!startLine)
hyatt0c3a9862004-02-23 21:26:26 +00001829 last = 0;
1830 else {
hyatt04420ca2004-07-16 00:05:42 +00001831 for (RootInlineBox* curr = startLine->nextRootBox(); curr; curr = curr->nextRootBox()) {
1832 if (curr->isDirty())
1833 last = 0;
1834 else if (!last)
1835 last = curr;
1836 }
hyatt0c3a9862004-02-23 21:26:26 +00001837 }
1838
1839 if (!last)
1840 return 0;
1841
eseidel789896f2005-11-27 22:52:09 +00001842 RootInlineBox* prev = last->prevRootBox();
1843 cleanLineStart = BidiIterator(this, prev->lineBreakObj(), prev->lineBreakPos());
1844 cleanLineBidiStatus = prev->lineBreakBidiStatus();
1845 cleanLineBidiContext = prev->lineBreakBidiContext();
hyatt0c3a9862004-02-23 21:26:26 +00001846 yPos = last->prevRootBox()->blockHeight();
1847
1848 for (RootInlineBox* line = last; line; line = line->nextRootBox())
1849 line->extractLine(); // Disconnect all line boxes from their render objects while preserving
1850 // their connections to one another.
1851
1852 return last;
1853}
1854
eseidel789896f2005-11-27 22:52:09 +00001855bool RenderBlock::matchedEndLine(const BidiIterator& start, const BidiStatus& status, BidiContext* context,
1856 const BidiIterator& endLineStart, const BidiStatus& endLineStatus,
1857 BidiContext* endLineContext, RootInlineBox*& endLine, int& endYPos)
hyatt0c3a9862004-02-23 21:26:26 +00001858{
hyatt40731602005-04-18 18:12:42 +00001859 if (start == endLineStart)
eseidel789896f2005-11-27 22:52:09 +00001860 return status == endLineStatus && *context == *endLineContext;
hyatt40731602005-04-18 18:12:42 +00001861 else {
hyatt0c3a9862004-02-23 21:26:26 +00001862 // The first clean line doesn't match, but we can check a handful of following lines to try
1863 // to match back up.
1864 static int numLines = 8; // The # of lines we're willing to match against.
1865 RootInlineBox* line = endLine;
1866 for (int i = 0; i < numLines && line; i++, line = line->nextRootBox()) {
1867 if (line->lineBreakObj() == start.obj && line->lineBreakPos() == start.pos) {
1868 // We have a match.
eseidel789896f2005-11-27 22:52:09 +00001869 if (line->lineBreakBidiStatus() != status || *line->lineBreakBidiContext() != *context)
1870 return false; // ...but the bidi state doesn't match.
hyatt0c3a9862004-02-23 21:26:26 +00001871 RootInlineBox* result = line->nextRootBox();
1872
1873 // Set our yPos to be the block height of endLine.
1874 if (result)
1875 endYPos = line->blockHeight();
1876
1877 // Now delete the lines that we failed to sync.
1878 RootInlineBox* boxToDelete = endLine;
1879 RenderArena* arena = renderArena();
1880 while (boxToDelete && boxToDelete != result) {
1881 RootInlineBox* next = boxToDelete->nextRootBox();
1882 boxToDelete->deleteLine(arena);
1883 boxToDelete = next;
1884 }
1885
1886 endLine = result;
1887 return result;
1888 }
1889 }
1890 }
1891 return false;
1892}
1893
darin479ef852006-01-30 06:06:32 +00001894static const unsigned short nonBreakingSpace = 0xa0;
kociendae40cb942004-10-05 20:05:38 +00001895
hyattd9953212005-11-03 21:05:59 +00001896static inline bool skipNonBreakingSpace(BidiIterator &it)
kocienda98440082004-10-14 23:51:47 +00001897{
darin7ab31092006-05-10 04:59:57 +00001898 if (it.obj->style()->nbspMode() != SPACE || it.current() != nonBreakingSpace)
kocienda98440082004-10-14 23:51:47 +00001899 return false;
1900
hyattdca76e92005-11-02 08:52:50 +00001901 // FIXME: This is bad. It makes nbsp inconsistent with space and won't work correctly
1902 // with m_minWidth/m_maxWidth.
kocienda498d1982004-10-15 21:07:24 +00001903 // Do not skip a non-breaking space if it is the first character
hyattdca76e92005-11-02 08:52:50 +00001904 // on a line after a clean line break (or on the first line, since previousLineBrokeCleanly starts off
1905 // |true|).
1906 if (isLineEmpty && previousLineBrokeCleanly)
kocienda498d1982004-10-15 21:07:24 +00001907 return false;
1908
1909 return true;
kocienda98440082004-10-14 23:51:47 +00001910}
1911
hyattd9953212005-11-03 21:05:59 +00001912static inline bool shouldCollapseWhiteSpace(const RenderStyle* style)
1913{
1914 return style->collapseWhiteSpace() || (style->whiteSpace() == PRE_WRAP && (!isLineEmpty || !previousLineBrokeCleanly));
1915}
1916
kociendae40cb942004-10-05 20:05:38 +00001917int RenderBlock::skipWhitespace(BidiIterator &it, BidiState &bidi)
kociendabb0c24b2001-08-24 14:24:40 +00001918{
hyatt853cd7d2004-11-05 02:59:48 +00001919 // FIXME: The entire concept of the skipWhitespace function is flawed, since we really need to be building
1920 // line boxes even for containers that may ultimately collapse away. Otherwise we'll never get positioned
1921 // elements quite right. In other words, we need to build this function's work into the normal line
1922 // object iteration process.
1923 int w = lineWidth(m_height);
hyattd9953212005-11-03 21:05:59 +00001924 while (!it.atEnd() && (it.obj->isInlineFlow() || (shouldCollapseWhiteSpace(it.obj->style()) && !it.obj->isBR() &&
hyattdca76e92005-11-02 08:52:50 +00001925 (it.current() == ' ' || it.current() == '\t' || (!it.obj->style()->preserveNewline() && it.current() == '\n') ||
darin7ab31092006-05-10 04:59:57 +00001926 it.current() == SOFT_HYPHEN || skipNonBreakingSpace(it) || it.obj->isFloatingOrPositioned())))) {
kociendae40cb942004-10-05 20:05:38 +00001927 if (it.obj->isFloatingOrPositioned()) {
1928 RenderObject *o = it.obj;
hyatt3b5905b2003-02-01 01:13:30 +00001929 // add to special objects...
hyatt98ee7e42003-05-14 01:39:15 +00001930 if (o->isFloating()) {
hyatt3ac01352003-03-22 01:37:33 +00001931 insertFloatingObject(o);
hyatt38e32632003-07-31 00:46:03 +00001932 positionNewFloats();
hyatt853cd7d2004-11-05 02:59:48 +00001933 w = lineWidth(m_height);
hyatt33f8d492002-11-12 21:44:52 +00001934 }
hyatt98ee7e42003-05-14 01:39:15 +00001935 else if (o->isPositioned()) {
hyatt853cd7d2004-11-05 02:59:48 +00001936 // FIXME: The math here is actually not really right. It's a best-guess approximation that
1937 // will work for the common cases
1938 RenderObject* c = o->container();
1939 if (c->isInlineFlow()) {
1940 // A relative positioned inline encloses us. In this case, we also have to determine our
1941 // position as though we were an inline. Set |staticX| and |staticY| on the relative positioned
1942 // inline so that we can obtain the value later.
1943 c->setStaticX(style()->direction() == LTR ?
1944 leftOffset(m_height) : rightOffset(m_height));
1945 c->setStaticY(m_height);
1946 }
1947
1948 if (o->hasStaticX()) {
1949 bool wasInline = o->style()->isOriginalDisplayInlineType();
1950 if (wasInline)
1951 o->setStaticX(style()->direction() == LTR ?
1952 leftOffset(m_height) :
1953 width() - rightOffset(m_height));
1954 else
1955 o->setStaticX(style()->direction() == LTR ?
1956 borderLeft() + paddingLeft() :
1957 borderRight() + paddingRight());
1958 }
hyatt98ee7e42003-05-14 01:39:15 +00001959 if (o->hasStaticY())
1960 o->setStaticY(m_height);
1961 }
hyatt33f8d492002-11-12 21:44:52 +00001962 }
hyatt4b381692003-03-10 21:11:59 +00001963
hyatt275d0702005-11-03 23:53:57 +00001964 bidi.adjustEmbedding = true;
kociendae40cb942004-10-05 20:05:38 +00001965 it.increment(bidi);
hyatt275d0702005-11-03 23:53:57 +00001966 bidi.adjustEmbedding = false;
mjs6f821c82002-03-22 00:31:57 +00001967 }
hyatt853cd7d2004-11-05 02:59:48 +00001968 return w;
kociendae40cb942004-10-05 20:05:38 +00001969}
1970
1971BidiIterator RenderBlock::findNextLineBreak(BidiIterator &start, BidiState &bidi)
1972{
thatcher459b94a2005-10-13 22:07:51 +00001973 // eliminate spaces at beginning of line
1974 int width = skipWhitespace(start, bidi);
kociendae40cb942004-10-05 20:05:38 +00001975 int w = 0;
1976 int tmpW = 0;
kociendae40cb942004-10-05 20:05:38 +00001977
hyatt853cd7d2004-11-05 02:59:48 +00001978 if (start.atEnd())
darinb70665a2002-04-15 23:43:21 +00001979 return start;
mjs6f821c82002-03-22 00:31:57 +00001980
hyatt33f8d492002-11-12 21:44:52 +00001981 // This variable is used only if whitespace isn't set to PRE, and it tells us whether
1982 // or not we are currently ignoring whitespace.
1983 bool ignoringSpaces = false;
hyatt98b16282004-03-31 18:43:12 +00001984 BidiIterator ignoreStart;
hyatt33f8d492002-11-12 21:44:52 +00001985
1986 // This variable tracks whether the very last character we saw was a space. We use
1987 // this to detect when we encounter a second space so we know we have to terminate
1988 // a run.
rjwc9c257d2003-01-24 03:46:17 +00001989 bool currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00001990 bool currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00001991 RenderObject* trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00001992
mjs6f821c82002-03-22 00:31:57 +00001993 BidiIterator lBreak = start;
1994
kociendabb0c24b2001-08-24 14:24:40 +00001995 RenderObject *o = start.obj;
1996 RenderObject *last = o;
bdakin9151ba52005-10-24 22:51:06 +00001997 RenderObject *previous = o;
kociendabb0c24b2001-08-24 14:24:40 +00001998 int pos = start.pos;
1999
hyatt0c3a9862004-02-23 21:26:26 +00002000 bool prevLineBrokeCleanly = previousLineBrokeCleanly;
2001 previousLineBrokeCleanly = false;
hyatt01eff982003-03-14 20:13:23 +00002002
hyatt275d0702005-11-03 23:53:57 +00002003 while (o) {
2004 if (o->isBR()) {
hyatt0c3a9862004-02-23 21:26:26 +00002005 if (w + tmpW <= width) {
kociendabb0c24b2001-08-24 14:24:40 +00002006 lBreak.obj = o;
2007 lBreak.pos = 0;
hyatt0c3a9862004-02-23 21:26:26 +00002008 lBreak.increment(bidi);
2009
hyatt33f8d492002-11-12 21:44:52 +00002010 // A <br> always breaks a line, so don't let the line be collapsed
2011 // away. Also, the space at the end of a line with a <br> does not
hyatt01eff982003-03-14 20:13:23 +00002012 // get collapsed away. It only does this if the previous line broke
2013 // cleanly. Otherwise the <br> has no effect on whether the line is
2014 // empty or not.
2015 if (prevLineBrokeCleanly)
2016 isLineEmpty = false;
hyatt33f8d492002-11-12 21:44:52 +00002017 trailingSpaceObject = 0;
hyatt0c3a9862004-02-23 21:26:26 +00002018 previousLineBrokeCleanly = true;
hyatt74eec4d2003-03-23 08:02:47 +00002019
2020 if (!isLineEmpty) {
2021 // only check the clear status for non-empty lines.
2022 EClear clear = o->style()->clear();
eseidel789896f2005-11-27 22:52:09 +00002023 if (clear != CNONE)
hyatt74eec4d2003-03-23 08:02:47 +00002024 m_clearStatus = (EClear) (m_clearStatus | clear);
kociendabb0c24b2001-08-24 14:24:40 +00002025 }
2026 }
2027 goto end;
2028 }
hyatt275d0702005-11-03 23:53:57 +00002029 if (o->isFloatingOrPositioned()) {
kociendabb0c24b2001-08-24 14:24:40 +00002030 // add to special objects...
hyatt275d0702005-11-03 23:53:57 +00002031 if (o->isFloating()) {
hyatt3ac01352003-03-22 01:37:33 +00002032 insertFloatingObject(o);
hyatt33f8d492002-11-12 21:44:52 +00002033 // check if it fits in the current line.
2034 // If it does, position it now, otherwise, position
2035 // it after moving to next line (in newLine() func)
2036 if (o->width()+o->marginLeft()+o->marginRight()+w+tmpW <= width) {
2037 positionNewFloats();
2038 width = lineWidth(m_height);
2039 }
hyatt275d0702005-11-03 23:53:57 +00002040 } else if (o->isPositioned()) {
hyatt851816b2003-07-08 07:54:17 +00002041 // If our original display wasn't an inline type, then we can
hyatt98ee7e42003-05-14 01:39:15 +00002042 // go ahead and determine our static x position now.
hyatt851816b2003-07-08 07:54:17 +00002043 bool isInlineType = o->style()->isOriginalDisplayInlineType();
hyatt98ee7e42003-05-14 01:39:15 +00002044 bool needToSetStaticX = o->hasStaticX();
2045 if (o->hasStaticX() && !isInlineType) {
2046 o->setStaticX(o->parent()->style()->direction() == LTR ?
2047 borderLeft()+paddingLeft() :
2048 borderRight()+paddingRight());
2049 needToSetStaticX = false;
2050 }
2051
2052 // If our original display was an INLINE type, then we can go ahead
2053 // and determine our static y position now.
2054 bool needToSetStaticY = o->hasStaticY();
2055 if (o->hasStaticY() && isInlineType) {
2056 o->setStaticY(m_height);
2057 needToSetStaticY = false;
2058 }
2059
hyatt853cd7d2004-11-05 02:59:48 +00002060 bool needToCreateLineBox = needToSetStaticX || needToSetStaticY;
2061 RenderObject* c = o->container();
2062 if (c->isInlineFlow() && (!needToSetStaticX || !needToSetStaticY))
2063 needToCreateLineBox = true;
2064
hyatt98ee7e42003-05-14 01:39:15 +00002065 // If we're ignoring spaces, we have to stop and include this object and
2066 // then start ignoring spaces again.
hyatt853cd7d2004-11-05 02:59:48 +00002067 if (needToCreateLineBox) {
hyattbc7f07f2003-05-27 20:04:26 +00002068 trailingSpaceObject = 0;
hyatt98b16282004-03-31 18:43:12 +00002069 ignoreStart.obj = o;
2070 ignoreStart.pos = 0;
hyattbc7f07f2003-05-27 20:04:26 +00002071 if (ignoringSpaces) {
hyatt98b16282004-03-31 18:43:12 +00002072 addMidpoint(ignoreStart); // Stop ignoring spaces.
2073 addMidpoint(ignoreStart); // Start ignoring again.
hyattbc7f07f2003-05-27 20:04:26 +00002074 }
hyatt98b16282004-03-31 18:43:12 +00002075
hyatt851816b2003-07-08 07:54:17 +00002076 }
hyatt98ee7e42003-05-14 01:39:15 +00002077 }
hyattffe78712003-02-11 01:59:29 +00002078 } else if (o->isInlineFlow()) {
2079 // Only empty inlines matter. We treat those similarly to replaced elements.
hyatt275d0702005-11-03 23:53:57 +00002080 assert(!o->firstChild());
hyattffe78712003-02-11 01:59:29 +00002081 tmpW += o->marginLeft()+o->borderLeft()+o->paddingLeft()+
2082 o->marginRight()+o->borderRight()+o->paddingRight();
hyatt275d0702005-11-03 23:53:57 +00002083 } else if (o->isReplaced()) {
hyattde396342003-10-29 08:57:20 +00002084 EWhiteSpace currWS = o->style()->whiteSpace();
2085 EWhiteSpace lastWS = last->style()->whiteSpace();
2086
2087 // WinIE marquees have different whitespace characteristics by default when viewed from
2088 // the outside vs. the inside. Text inside is NOWRAP, and so we altered the marquee's
2089 // style to reflect this, but we now have to get back to the original whitespace value
2090 // for the marquee when checking for line breaking.
2091 if (o->isHTMLMarquee() && o->layer() && o->layer()->marquee())
2092 currWS = o->layer()->marquee()->whiteSpace();
2093 if (last->isHTMLMarquee() && last->layer() && last->layer()->marquee())
2094 lastWS = last->layer()->marquee()->whiteSpace();
2095
2096 // Break on replaced elements if either has normal white-space.
2097 // FIXME: This does not match WinIE, Opera, and Mozilla. They treat replaced elements
2098 // like characters in a word, and require spaces between the replaced elements in order
2099 // to break.
hyattdca76e92005-11-02 08:52:50 +00002100 if (RenderStyle::autoWrap(currWS) || RenderStyle::autoWrap(lastWS)) {
hyatt711fe232002-11-20 21:25:14 +00002101 w += tmpW;
2102 tmpW = 0;
hyattf14a4a32002-11-21 22:06:32 +00002103 lBreak.obj = o;
2104 lBreak.pos = 0;
hyatt711fe232002-11-20 21:25:14 +00002105 }
2106
hyatt33f8d492002-11-12 21:44:52 +00002107 if (ignoringSpaces) {
mjsfe301d72003-10-02 18:46:18 +00002108 BidiIterator startMid( 0, o, 0 );
hyatt85586af2003-02-19 23:22:42 +00002109 addMidpoint(startMid);
hyatt33f8d492002-11-12 21:44:52 +00002110 }
2111 isLineEmpty = false;
2112 ignoringSpaces = false;
rjwc9c257d2003-01-24 03:46:17 +00002113 currentCharacterIsSpace = false;
harrisone343c412005-01-18 01:07:26 +00002114 currentCharacterIsWS = false;
hyatt33f8d492002-11-12 21:44:52 +00002115 trailingSpaceObject = 0;
hyatte85e4a72002-12-08 02:06:16 +00002116
2117 if (o->isListMarker() && o->style()->listStylePosition() == OUTSIDE) {
2118 // The marker must not have an effect on whitespace at the start
2119 // of the line. We start ignoring spaces to make sure that any additional
2120 // spaces we see will be discarded.
2121 //
2122 // Optimize for a common case. If we can't find whitespace after the list
2123 // item, then this is all moot. -dwh
hyatt275d0702005-11-03 23:53:57 +00002124 RenderObject* next = bidiNext(start.block, o, bidi);
harrison8d773e72006-01-29 15:06:17 +00002125 if (style()->collapseWhiteSpace() && next && !next->isBR() && next->isText() && static_cast<RenderText*>(next)->stringLength() > 0) {
harrison208ea792005-07-29 23:42:59 +00002126 RenderText *nextText = static_cast<RenderText*>(next);
darin7ab31092006-05-10 04:59:57 +00002127 UChar nextChar = nextText->text()[0];
hyattdca76e92005-11-02 08:52:50 +00002128 if (nextText->style()->isCollapsibleWhiteSpace(nextChar)) {
harrisone343c412005-01-18 01:07:26 +00002129 currentCharacterIsSpace = true;
2130 currentCharacterIsWS = true;
2131 ignoringSpaces = true;
2132 BidiIterator endMid( 0, o, 0 );
2133 addMidpoint(endMid);
2134 }
hyatte85e4a72002-12-08 02:06:16 +00002135 }
justing244d3a32006-04-13 01:31:24 +00002136 } else
2137 tmpW += o->width()+o->marginLeft()+o->marginRight()+inlineWidth(o);
eseidel789896f2005-11-27 22:52:09 +00002138 } else if (o->isText()) {
hyatt33f8d492002-11-12 21:44:52 +00002139 RenderText *t = static_cast<RenderText *>(o);
2140 int strlen = t->stringLength();
2141 int len = strlen - pos;
darin7ab31092006-05-10 04:59:57 +00002142 const UChar* str = t->text();
kociendabb0c24b2001-08-24 14:24:40 +00002143
hyattecad67c2006-03-01 06:16:38 +00002144 const Font *f = t->font(m_firstLine);
hyatt33f8d492002-11-12 21:44:52 +00002145 // proportional font, needs a bit more work.
2146 int lastSpace = pos;
hyatt3aff2332003-01-23 01:15:28 +00002147 int wordSpacing = o->style()->wordSpacing();
eseideld13fe532005-11-30 02:40:29 +00002148 int lastSpaceWordSpacing = 0;
hyattffe78712003-02-11 01:59:29 +00002149
2150 bool appliedStartWidth = pos > 0; // If the span originated on a previous line,
2151 // then assume the start width has been applied.
darin54008922006-01-13 16:39:05 +00002152 int wrapW = tmpW + inlineWidth(o, !appliedStartWidth, true);
darin47ece0d2005-09-04 07:42:31 +00002153 int nextBreakable = -1;
kociendae4134242004-10-25 18:48:44 +00002154
hyatt275d0702005-11-03 23:53:57 +00002155 while (len) {
rjwc9c257d2003-01-24 03:46:17 +00002156 bool previousCharacterIsSpace = currentCharacterIsSpace;
harrisone343c412005-01-18 01:07:26 +00002157 bool previousCharacterIsWS = currentCharacterIsWS;
darin7ab31092006-05-10 04:59:57 +00002158 UChar c = str[pos];
hyattdca76e92005-11-02 08:52:50 +00002159 currentCharacterIsSpace = c == ' ' || c == '\t' || (!o->style()->preserveNewline() && (c == '\n'));
darin47ece0d2005-09-04 07:42:31 +00002160
hyatt7555cbd2005-12-07 22:48:56 +00002161 if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00002162 isLineEmpty = false;
hyatt3aff2332003-01-23 01:15:28 +00002163
hyatt78b85132004-03-29 20:07:45 +00002164 // Check for soft hyphens. Go ahead and ignore them.
darin7ab31092006-05-10 04:59:57 +00002165 if (c == SOFT_HYPHEN) {
hyatt78b85132004-03-29 20:07:45 +00002166 if (!ignoringSpaces) {
2167 // Ignore soft hyphens
bdakin9151ba52005-10-24 22:51:06 +00002168 BidiIterator endMid;
2169 if (pos > 0)
2170 endMid = BidiIterator(0, o, pos-1);
2171 else
2172 endMid = BidiIterator(0, previous, previous->isText() ? static_cast<RenderText *>(previous)->stringLength() - 1 : 0);
2173 // Two consecutive soft hyphens. Avoid overlapping midpoints.
2174 if (sNumMidpoints && smidpoints->at(sNumMidpoints - 1).obj == endMid.obj && smidpoints->at(sNumMidpoints - 1).pos > endMid.pos)
2175 sNumMidpoints--;
2176 else
2177 addMidpoint(endMid);
hyatt78b85132004-03-29 20:07:45 +00002178
2179 // Add the width up to but not including the hyphen.
eseideld13fe532005-11-30 02:40:29 +00002180 tmpW += t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
hyatt78b85132004-03-29 20:07:45 +00002181
hyattdca76e92005-11-02 08:52:50 +00002182 // For wrapping text only, include the hyphen. We need to ensure it will fit
hyatt78b85132004-03-29 20:07:45 +00002183 // on the line if it shows when we break.
hyattdca76e92005-11-02 08:52:50 +00002184 if (o->style()->autoWrap())
harrison208ea792005-07-29 23:42:59 +00002185 tmpW += t->width(pos, 1, f, w+tmpW);
hyatt78b85132004-03-29 20:07:45 +00002186
2187 BidiIterator startMid(0, o, pos+1);
2188 addMidpoint(startMid);
2189 }
2190
2191 pos++;
2192 len--;
eseideld13fe532005-11-30 02:40:29 +00002193 lastSpaceWordSpacing = 0;
hyatt78b85132004-03-29 20:07:45 +00002194 lastSpace = pos; // Cheesy hack to prevent adding in widths of the run twice.
2195 continue;
2196 }
2197
hyatt3aff2332003-01-23 01:15:28 +00002198 bool applyWordSpacing = false;
hyattdca76e92005-11-02 08:52:50 +00002199 bool allowBreak = o->style()->autoWrap();
2200 bool breakNBSP = allowBreak && o->style()->nbspMode() == SPACE;
2201
2202 // FIXME: This check looks suspicious. Why does w have to be 0?
2203 bool breakWords = o->style()->wordWrap() == BREAK_WORD && ((allowBreak && w == 0) || o->style()->whiteSpace() == PRE);
harrisone343c412005-01-18 01:07:26 +00002204
darin7ab31092006-05-10 04:59:57 +00002205 currentCharacterIsWS = currentCharacterIsSpace || (breakNBSP && c == nonBreakingSpace);
harrisone343c412005-01-18 01:07:26 +00002206
kociendae4ebdd92004-09-29 21:55:48 +00002207 if (breakWords)
harrison208ea792005-07-29 23:42:59 +00002208 wrapW += t->width(pos, 1, f, w+wrapW);
darin54008922006-01-13 16:39:05 +00002209 bool midWordBreak = breakWords && (w + wrapW > width);
darin47ece0d2005-09-04 07:42:31 +00002210
darin54008922006-01-13 16:39:05 +00002211 if (c == '\n' || (o->style()->whiteSpace() != PRE && isBreakable(str, pos, strlen, nextBreakable, breakNBSP)) || midWordBreak) {
hyatt0c05e102006-04-14 08:15:00 +00002212 bool stoppedIgnoringSpaces = false;
hyatt33f8d492002-11-12 21:44:52 +00002213 if (ignoringSpaces) {
rjwc9c257d2003-01-24 03:46:17 +00002214 if (!currentCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002215 // Stop ignoring spaces and begin at this
2216 // new point.
hyatt48710d62003-08-21 09:17:13 +00002217 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002218 lastSpaceWordSpacing = 0;
hyatt48710d62003-08-21 09:17:13 +00002219 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
mjsfe301d72003-10-02 18:46:18 +00002220 BidiIterator startMid ( 0, o, pos );
hyatt85586af2003-02-19 23:22:42 +00002221 addMidpoint(startMid);
hyatt0c05e102006-04-14 08:15:00 +00002222 stoppedIgnoringSpaces = true;
harrisone343c412005-01-18 01:07:26 +00002223 } else {
hyatt33f8d492002-11-12 21:44:52 +00002224 // Just keep ignoring these spaces.
2225 pos++;
2226 len--;
2227 continue;
2228 }
2229 }
rjwc9c257d2003-01-24 03:46:17 +00002230
darin54008922006-01-13 16:39:05 +00002231 int additionalTmpW = t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
2232 tmpW += additionalTmpW;
hyattffe78712003-02-11 01:59:29 +00002233 if (!appliedStartWidth) {
2234 tmpW += inlineWidth(o, true, false);
2235 appliedStartWidth = true;
2236 }
2237
eseideld13fe532005-11-30 02:40:29 +00002238 applyWordSpacing = wordSpacing && currentCharacterIsSpace && !previousCharacterIsSpace;
hyatt3aff2332003-01-23 01:15:28 +00002239
hyattdca76e92005-11-02 08:52:50 +00002240 if (o->style()->autoWrap() && w + tmpW > width && w == 0) {
hyatte2b14092003-01-23 23:33:39 +00002241 int fb = nearestFloatBottom(m_height);
hyatt33f8d492002-11-12 21:44:52 +00002242 int newLineWidth = lineWidth(fb);
hyatt2df52192003-03-28 22:05:02 +00002243 // See if |tmpW| will fit on the new line. As long as it does not,
2244 // keep adjusting our float bottom until we find some room.
2245 int lastFloatBottom = m_height;
2246 while (lastFloatBottom < fb && tmpW > newLineWidth) {
2247 lastFloatBottom = fb;
2248 fb = nearestFloatBottom(fb);
2249 newLineWidth = lineWidth(fb);
2250 }
2251
hyatt275d0702005-11-03 23:53:57 +00002252 if (!w && m_height < fb && width < newLineWidth) {
hyatt33f8d492002-11-12 21:44:52 +00002253 m_height = fb;
2254 width = newLineWidth;
hyatt33f8d492002-11-12 21:44:52 +00002255 }
2256 }
2257
hyattdca76e92005-11-02 08:52:50 +00002258 if (o->style()->autoWrap() || breakWords) {
2259 // If we break only after white-space, consider the current character
kociendae4134242004-10-25 18:48:44 +00002260 // as candidate width for this line.
ap593ea522006-05-13 16:28:31 +00002261 int charWidth = o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak ?
eseideld13fe532005-11-30 02:40:29 +00002262 t->width(pos, 1, f, w + tmpW) + (applyWordSpacing ? wordSpacing : 0) : 0;
kocienda9dbe9b12004-10-22 20:07:05 +00002263 if (w + tmpW + charWidth > width) {
ap593ea522006-05-13 16:28:31 +00002264 if (o->style()->breakOnlyAfterWhiteSpace() && !midWordBreak) {
kociendae4134242004-10-25 18:48:44 +00002265 // Check if line is too big even without the extra space
2266 // at the end of the line. If it is not, do nothing.
2267 // If the line needs the extra whitespace to be too long,
2268 // then move the line break to the space and skip all
2269 // additional whitespace.
harrison07b5e582005-08-15 23:31:16 +00002270 if (w + tmpW <= width) {
kociendae4134242004-10-25 18:48:44 +00002271 lBreak.obj = o;
2272 lBreak.pos = pos;
2273 skipWhitespace(lBreak, bidi);
2274 }
kocienda9dbe9b12004-10-22 20:07:05 +00002275 }
adele7fc3e832006-02-17 09:31:35 +00002276 if (lBreak.obj && lBreak.obj->style()->preserveNewline() && lBreak.obj->isText() && static_cast<RenderText*>(lBreak.obj)->text()[lBreak.pos] == '\n') {
hyatt0c05e102006-04-14 08:15:00 +00002277 if (!stoppedIgnoringSpaces && pos > 0) {
2278 // We need to stop right before the newline and then start up again.
2279 BidiIterator midpoint(0, o, pos);
2280 addMidpoint(BidiIterator(0, o, pos-1)); // Stop
2281 addMidpoint(BidiIterator(0, o, pos)); // Start
2282 }
2283 lBreak.increment(bidi);
adele7fc3e832006-02-17 09:31:35 +00002284 previousLineBrokeCleanly = true;
adele7fc3e832006-02-17 09:31:35 +00002285 }
hyatt78b85132004-03-29 20:07:45 +00002286 goto end; // Didn't fit. Jump to the end.
darin54008922006-01-13 16:39:05 +00002287 } else {
2288 if (midWordBreak)
2289 tmpW -= additionalTmpW;
darin7ab31092006-05-10 04:59:57 +00002290 if (pos > 0 && str[pos-1] == SOFT_HYPHEN)
darin54008922006-01-13 16:39:05 +00002291 // Subtract the width of the soft hyphen out since we fit on a line.
2292 tmpW -= t->width(pos-1, 1, f, w+tmpW);
2293 }
rjwc9c257d2003-01-24 03:46:17 +00002294 }
hyatt33f8d492002-11-12 21:44:52 +00002295
adele7fc3e832006-02-17 09:31:35 +00002296 if (c == '\n' && o->style()->preserveNewline()) {
hyatt0c05e102006-04-14 08:15:00 +00002297 if (!stoppedIgnoringSpaces && pos > 0) {
2298 // We need to stop right before the newline and then start up again.
2299 BidiIterator midpoint(0, o, pos);
2300 addMidpoint(BidiIterator(0, o, pos-1)); // Stop
2301 addMidpoint(BidiIterator(0, o, pos)); // Start
2302 }
hyatta9f48e32003-02-03 22:48:01 +00002303 lBreak.obj = o;
2304 lBreak.pos = pos;
hyatt0c05e102006-04-14 08:15:00 +00002305 lBreak.increment(bidi);
adele7fc3e832006-02-17 09:31:35 +00002306 previousLineBrokeCleanly = true;
hyatt33f8d492002-11-12 21:44:52 +00002307 return lBreak;
2308 }
hyatta9f48e32003-02-03 22:48:01 +00002309
hyattdca76e92005-11-02 08:52:50 +00002310 if (o->style()->autoWrap()) {
hyatta9f48e32003-02-03 22:48:01 +00002311 w += tmpW;
2312 tmpW = 0;
2313 lBreak.obj = o;
2314 lBreak.pos = pos;
2315 }
hyatt33f8d492002-11-12 21:44:52 +00002316
darin54008922006-01-13 16:39:05 +00002317 if (midWordBreak) {
2318 // Remember this as a breakable position in case
2319 // adding the end width forces a break.
2320 lBreak.obj = o;
2321 lBreak.pos = pos;
2322 } else {
2323 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
2324 lastSpace = pos;
2325 }
hyatt33f8d492002-11-12 21:44:52 +00002326
hyattdca76e92005-11-02 08:52:50 +00002327 if (!ignoringSpaces && o->style()->collapseWhiteSpace()) {
hyatt33f8d492002-11-12 21:44:52 +00002328 // If we encounter a newline, or if we encounter a
2329 // second space, we need to go ahead and break up this
2330 // run and enter a mode where we start collapsing spaces.
hyatt98b16282004-03-31 18:43:12 +00002331 if (currentCharacterIsSpace && previousCharacterIsSpace) {
hyatt33f8d492002-11-12 21:44:52 +00002332 ignoringSpaces = true;
hyatt98b16282004-03-31 18:43:12 +00002333
hyatt33f8d492002-11-12 21:44:52 +00002334 // We just entered a mode where we are ignoring
2335 // spaces. Create a midpoint to terminate the run
2336 // before the second space.
hyatt98b16282004-03-31 18:43:12 +00002337 addMidpoint(ignoreStart);
hyatt33f8d492002-11-12 21:44:52 +00002338 }
2339 }
eseidel789896f2005-11-27 22:52:09 +00002340 } else if (ignoringSpaces) {
hyatt33f8d492002-11-12 21:44:52 +00002341 // Stop ignoring spaces and begin at this
2342 // new point.
2343 ignoringSpaces = false;
eseideld13fe532005-11-30 02:40:29 +00002344 lastSpaceWordSpacing = applyWordSpacing ? wordSpacing : 0;
hyatt33f8d492002-11-12 21:44:52 +00002345 lastSpace = pos; // e.g., "Foo goo", don't add in any of the ignored spaces.
eseidel789896f2005-11-27 22:52:09 +00002346 BidiIterator startMid(0, o, pos);
hyatt85586af2003-02-19 23:22:42 +00002347 addMidpoint(startMid);
hyatt33f8d492002-11-12 21:44:52 +00002348 }
hyatt98b16282004-03-31 18:43:12 +00002349
2350 if (currentCharacterIsSpace && !previousCharacterIsSpace) {
2351 ignoreStart.obj = o;
2352 ignoreStart.pos = pos;
2353 }
harrisone343c412005-01-18 01:07:26 +00002354
2355 if (!currentCharacterIsWS && previousCharacterIsWS) {
hyattdca76e92005-11-02 08:52:50 +00002356 if (o->style()->autoWrap() && o->style()->breakOnlyAfterWhiteSpace()) {
harrisone343c412005-01-18 01:07:26 +00002357 lBreak.obj = o;
2358 lBreak.pos = pos;
2359 }
2360 }
hyatt33f8d492002-11-12 21:44:52 +00002361
hyattdca76e92005-11-02 08:52:50 +00002362 if (o->style()->collapseWhiteSpace() && currentCharacterIsSpace && !ignoringSpaces)
hyatt33f8d492002-11-12 21:44:52 +00002363 trailingSpaceObject = o;
hyattdca76e92005-11-02 08:52:50 +00002364 else if (!o->style()->collapseWhiteSpace() || !currentCharacterIsSpace)
hyatt33f8d492002-11-12 21:44:52 +00002365 trailingSpaceObject = 0;
2366
2367 pos++;
2368 len--;
2369 }
2370
kociendabb0c24b2001-08-24 14:24:40 +00002371 // IMPORTANT: pos is > length here!
hyatt33f8d492002-11-12 21:44:52 +00002372 if (!ignoringSpaces)
eseideld13fe532005-11-30 02:40:29 +00002373 tmpW += t->width(lastSpace, pos - lastSpace, f, w+tmpW) + lastSpaceWordSpacing;
hyattffe78712003-02-11 01:59:29 +00002374 if (!appliedStartWidth)
2375 tmpW += inlineWidth(o, true, false);
darin54008922006-01-13 16:39:05 +00002376 tmpW += inlineWidth(o, false, true);
kociendabb0c24b2001-08-24 14:24:40 +00002377 } else
mjs6f821c82002-03-22 00:31:57 +00002378 KHTMLAssert( false );
kociendabb0c24b2001-08-24 14:24:40 +00002379
hyatt275d0702005-11-03 23:53:57 +00002380 RenderObject* next = bidiNext(start.block, o, bidi);
hyattdca76e92005-11-02 08:52:50 +00002381 bool autoWrap = o->style()->autoWrap();
2382 bool checkForBreak = autoWrap;
darin54008922006-01-13 16:39:05 +00002383 if (w && w + tmpW > width && lBreak.obj && o->style()->whiteSpace() == NOWRAP)
hyatt74eec4d2003-03-23 08:02:47 +00002384 checkForBreak = true;
2385 else if (next && o->isText() && next->isText() && !next->isBR()) {
hyattdca76e92005-11-02 08:52:50 +00002386 if (autoWrap || (next->style()->autoWrap())) {
hyatta9f48e32003-02-03 22:48:01 +00002387 if (currentCharacterIsSpace)
2388 checkForBreak = true;
2389 else {
harrison07b5e582005-08-15 23:31:16 +00002390 checkForBreak = false;
hyatta9f48e32003-02-03 22:48:01 +00002391 RenderText* nextText = static_cast<RenderText*>(next);
harrison07b5e582005-08-15 23:31:16 +00002392 if (nextText->stringLength() != 0) {
darin7ab31092006-05-10 04:59:57 +00002393 UChar c = nextText->text()[0];
2394 if (c == ' ' || c == '\t' || (c == '\n' && !next->style()->preserveNewline()))
eseideld13fe532005-11-30 02:40:29 +00002395 // If the next item on the line is text, and if we did not end with
2396 // a space, then the next text run continues our word (and so it needs to
2397 // keep adding to |tmpW|. Just update and continue.
2398 checkForBreak = true;
harrison07b5e582005-08-15 23:31:16 +00002399 }
darin54008922006-01-13 16:39:05 +00002400 bool canPlaceOnLine = (w + tmpW <= width) || !autoWrap;
hyatta9f48e32003-02-03 22:48:01 +00002401 if (canPlaceOnLine && checkForBreak) {
2402 w += tmpW;
2403 tmpW = 0;
2404 lBreak.obj = next;
2405 lBreak.pos = 0;
2406 }
2407 }
2408 }
2409 }
2410
darin54008922006-01-13 16:39:05 +00002411 if (checkForBreak && (w + tmpW > width)) {
kociendabb0c24b2001-08-24 14:24:40 +00002412 // if we have floats, try to get below them.
hyattdca76e92005-11-02 08:52:50 +00002413 if (currentCharacterIsSpace && !ignoringSpaces && o->style()->collapseWhiteSpace())
hyatt33f8d492002-11-12 21:44:52 +00002414 trailingSpaceObject = 0;
hyatta14d1742003-01-02 20:25:46 +00002415
hyatte2b14092003-01-23 23:33:39 +00002416 int fb = nearestFloatBottom(m_height);
hyatt33f8d492002-11-12 21:44:52 +00002417 int newLineWidth = lineWidth(fb);
hyatt2df52192003-03-28 22:05:02 +00002418 // See if |tmpW| will fit on the new line. As long as it does not,
2419 // keep adjusting our float bottom until we find some room.
2420 int lastFloatBottom = m_height;
2421 while (lastFloatBottom < fb && tmpW > newLineWidth) {
2422 lastFloatBottom = fb;
2423 fb = nearestFloatBottom(fb);
2424 newLineWidth = lineWidth(fb);
2425 }
hyatt275d0702005-11-03 23:53:57 +00002426 if (!w && m_height < fb && width < newLineWidth) {
kociendabb0c24b2001-08-24 14:24:40 +00002427 m_height = fb;
darinb70665a2002-04-15 23:43:21 +00002428 width = newLineWidth;
kociendabb0c24b2001-08-24 14:24:40 +00002429 }
hyattf14a4a32002-11-21 22:06:32 +00002430
hyatta14d1742003-01-02 20:25:46 +00002431 // |width| may have been adjusted because we got shoved down past a float (thus
2432 // giving us more room), so we need to retest, and only jump to
2433 // the end label if we still don't fit on the line. -dwh
darin54008922006-01-13 16:39:05 +00002434 if (w + tmpW > width)
hyatta14d1742003-01-02 20:25:46 +00002435 goto end;
kociendabb0c24b2001-08-24 14:24:40 +00002436 }
hyatt1d9e29b2003-04-10 01:48:53 +00002437
hyattf14a4a32002-11-21 22:06:32 +00002438 last = o;
bdakin9151ba52005-10-24 22:51:06 +00002439 if (!o->isFloating() && (!o->isPositioned() || o->hasStaticX() || o->hasStaticY() || !o->container()->isInlineFlow()))
2440 previous = o;
hyatta9f48e32003-02-03 22:48:01 +00002441 o = next;
hyattf14a4a32002-11-21 22:06:32 +00002442
hyattdca76e92005-11-02 08:52:50 +00002443 if (!last->isFloatingOrPositioned() && last->isReplaced() && last->style()->autoWrap() &&
bdakina964eb32005-10-24 17:47:26 +00002444 (!last->isListMarker() || last->style()->listStylePosition()==INSIDE)) {
hyatt711fe232002-11-20 21:25:14 +00002445 // Go ahead and add in tmpW.
2446 w += tmpW;
2447 tmpW = 0;
2448 lBreak.obj = o;
2449 lBreak.pos = 0;
2450 }
2451
hyatta9f48e32003-02-03 22:48:01 +00002452 // Clear out our character space bool, since inline <pre>s don't collapse whitespace
2453 // with adjacent inline normal/nowrap spans.
hyattdca76e92005-11-02 08:52:50 +00002454 if (!last->style()->collapseWhiteSpace())
hyatta9f48e32003-02-03 22:48:01 +00002455 currentCharacterIsSpace = false;
2456
kociendabb0c24b2001-08-24 14:24:40 +00002457 pos = 0;
2458 }
2459
hyatt275d0702005-11-03 23:53:57 +00002460 if (w + tmpW <= width || (last && last->style()->whiteSpace() == NOWRAP)) {
kociendabb0c24b2001-08-24 14:24:40 +00002461 lBreak.obj = 0;
2462 lBreak.pos = 0;
2463 }
2464
2465 end:
rjwc9c257d2003-01-24 03:46:17 +00002466
hyatt275d0702005-11-03 23:53:57 +00002467 if (lBreak == start && !lBreak.obj->isBR()) {
kociendabb0c24b2001-08-24 14:24:40 +00002468 // we just add as much as possible
hyattdca76e92005-11-02 08:52:50 +00002469 if (style()->whiteSpace() == PRE) {
2470 // FIXME: Don't really understand this case.
hyatt275d0702005-11-03 23:53:57 +00002471 if (pos != 0) {
kociendabb0c24b2001-08-24 14:24:40 +00002472 lBreak.obj = o;
2473 lBreak.pos = pos - 1;
2474 } else {
2475 lBreak.obj = last;
hyattc3731d42002-12-12 06:20:22 +00002476 lBreak.pos = last->isText() ? last->length() : 0;
kociendabb0c24b2001-08-24 14:24:40 +00002477 }
hyatt275d0702005-11-03 23:53:57 +00002478 } else if (lBreak.obj) {
2479 if (last != o && !last->isListMarker()) {
bdakina964eb32005-10-24 17:47:26 +00002480 // better to break between object boundaries than in the middle of a word (except for list markers)
hyatt33f8d492002-11-12 21:44:52 +00002481 lBreak.obj = o;
2482 lBreak.pos = 0;
2483 } else {
hyattdda1d1b2002-12-13 09:44:17 +00002484 // Don't ever break in the middle of a word if we can help it.
2485 // There's no room at all. We just have to be on this line,
2486 // even though we'll spill out.
2487 lBreak.obj = o;
2488 lBreak.pos = pos;
hyatt33f8d492002-11-12 21:44:52 +00002489 }
kociendabb0c24b2001-08-24 14:24:40 +00002490 }
2491 }
2492
2493 // make sure we consume at least one char/object.
hyatt275d0702005-11-03 23:53:57 +00002494 if (lBreak == start)
mjsfe301d72003-10-02 18:46:18 +00002495 lBreak.increment(bidi);
hyatt33f8d492002-11-12 21:44:52 +00002496
hyattfe99c872003-07-31 22:25:29 +00002497 // Sanity check our midpoints.
mjsfe301d72003-10-02 18:46:18 +00002498 checkMidpoints(lBreak, bidi);
hyattfe99c872003-07-31 22:25:29 +00002499
hyatt33f8d492002-11-12 21:44:52 +00002500 if (trailingSpaceObject) {
2501 // This object is either going to be part of the last midpoint, or it is going
2502 // to be the actual endpoint. In both cases we just decrease our pos by 1 level to
2503 // exclude the space, allowing it to - in effect - collapse into the newline.
hyatt85586af2003-02-19 23:22:42 +00002504 if (sNumMidpoints%2==1) {
2505 BidiIterator* midpoints = smidpoints->data();
2506 midpoints[sNumMidpoints-1].pos--;
hyatt33f8d492002-11-12 21:44:52 +00002507 }
2508 //else if (lBreak.pos > 0)
2509 // lBreak.pos--;
2510 else if (lBreak.obj == 0 && trailingSpaceObject->isText()) {
hyattd20075d2002-11-16 02:23:32 +00002511 // Add a new end midpoint that stops right at the very end.
hyattd20075d2002-11-16 02:23:32 +00002512 RenderText* text = static_cast<RenderText *>(trailingSpaceObject);
darin8341f372003-04-29 18:30:31 +00002513 unsigned pos = text->length() >=2 ? text->length() - 2 : UINT_MAX;
eseidel789896f2005-11-27 22:52:09 +00002514 BidiIterator endMid(0, trailingSpaceObject, pos);
hyatt85586af2003-02-19 23:22:42 +00002515 addMidpoint(endMid);
hyatt33f8d492002-11-12 21:44:52 +00002516 }
2517 }
rjwc9c257d2003-01-24 03:46:17 +00002518
mjs54b64002003-04-02 02:59:02 +00002519 // We might have made lBreak an iterator that points past the end
2520 // of the object. Do this adjustment to make it point to the start
2521 // of the next object instead to avoid confusing the rest of the
2522 // code.
2523 if (lBreak.pos > 0) {
darin54008922006-01-13 16:39:05 +00002524 lBreak.pos--;
2525 lBreak.increment(bidi);
mjs54b64002003-04-02 02:59:02 +00002526 }
2527
hyatt78b85132004-03-29 20:07:45 +00002528 if (lBreak.obj && lBreak.pos >= 2 && lBreak.obj->isText()) {
2529 // For soft hyphens on line breaks, we have to chop out the midpoints that made us
2530 // ignore the hyphen so that it will render at the end of the line.
darin7ab31092006-05-10 04:59:57 +00002531 UChar c = static_cast<RenderText*>(lBreak.obj)->text()[lBreak.pos-1];
2532 if (c == SOFT_HYPHEN)
hyatt78b85132004-03-29 20:07:45 +00002533 chopMidpointsAt(lBreak.obj, lBreak.pos-2);
2534 }
2535
kociendabb0c24b2001-08-24 14:24:40 +00002536 return lBreak;
2537}
2538
hyattb4b20872004-10-20 21:34:01 +00002539void RenderBlock::checkLinesForOverflow()
2540{
hyattb4b20872004-10-20 21:34:01 +00002541 // FIXME: Inline blocks can have overflow. Need to understand when those objects are present on a line
2542 // and factor that in somehow.
2543 m_overflowWidth = m_width;
2544 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
darin7bd70952006-04-13 07:07:34 +00002545 m_overflowLeft = min(curr->leftOverflow(), m_overflowLeft);
2546 m_overflowTop = min(curr->topOverflow(), m_overflowTop);
2547 m_overflowWidth = max(curr->rightOverflow(), m_overflowWidth);
2548 m_overflowHeight = max(curr->bottomOverflow(), m_overflowHeight);
hyattb4b20872004-10-20 21:34:01 +00002549 }
2550}
2551
hyatted77ad82004-06-15 07:21:23 +00002552void RenderBlock::deleteEllipsisLineBoxes()
2553{
hyatted77ad82004-06-15 07:21:23 +00002554 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox())
hyattda77c4b2004-06-17 18:09:49 +00002555 curr->clearTruncation();
hyatted77ad82004-06-15 07:21:23 +00002556}
2557
2558void RenderBlock::checkLinesForTextOverflow()
2559{
2560 // Determine the width of the ellipsis using the current font.
darin7ab31092006-05-10 04:59:57 +00002561 const UChar ellipsis = 0x2026; // FIXME: CSS3 says this is configurable, also need to use 0x002E (FULL STOP) if 0x2026 not renderable
hyatt43d6c792006-05-11 10:19:34 +00002562 TextRun ellipsisRun(&ellipsis, 1);
darin7ab31092006-05-10 04:59:57 +00002563 static AtomicString ellipsisStr(&ellipsis, 1);
hyatt3e99d1c2006-02-24 21:13:08 +00002564 const Font& firstLineFont = firstLineStyle()->font();
2565 const Font& font = style()->font();
hyatt43d6c792006-05-11 10:19:34 +00002566 int firstLineEllipsisWidth = firstLineFont.width(ellipsisRun);
2567 int ellipsisWidth = (font == firstLineFont) ? firstLineEllipsisWidth : font.width(ellipsisRun);
hyatted77ad82004-06-15 07:21:23 +00002568
2569 // For LTR text truncation, we want to get the right edge of our padding box, and then we want to see
2570 // if the right edge of a line box exceeds that. For RTL, we use the left edge of the padding box and
2571 // check the left edge of the line box to see if it is less
2572 // Include the scrollbar for overflow blocks, which means we want to use "contentWidth()"
2573 bool ltr = style()->direction() == LTR;
hyatted77ad82004-06-15 07:21:23 +00002574 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
hyattda77c4b2004-06-17 18:09:49 +00002575 int blockEdge = ltr ? rightOffset(curr->yPos()) : leftOffset(curr->yPos());
hyatt1a8d2512004-06-17 01:38:30 +00002576 int lineBoxEdge = ltr ? curr->xPos() + curr->width() : curr->xPos();
hyatted77ad82004-06-15 07:21:23 +00002577 if ((ltr && lineBoxEdge > blockEdge) || (!ltr && lineBoxEdge < blockEdge)) {
hyattf918d2d2004-06-15 07:24:11 +00002578 // 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 +00002579 // can be truncated. In order for truncation to be possible, the line must have sufficient space to
2580 // accommodate our truncation string, and no replaced elements (images, tables) can overlap the ellipsis
2581 // space.
2582 int width = curr == firstRootBox() ? firstLineEllipsisWidth : ellipsisWidth;
hyatt1a8d2512004-06-17 01:38:30 +00002583 if (curr->canAccommodateEllipsis(ltr, blockEdge, lineBoxEdge, width))
2584 curr->placeEllipsis(ellipsisStr, ltr, blockEdge, width);
hyatted77ad82004-06-15 07:21:23 +00002585 }
2586 }
2587}
2588
hyattffe78712003-02-11 01:59:29 +00002589}