bidi.cpp 47.2 KB
Newer Older
kocienda's avatar
kocienda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * This file is part of the html renderer for KDE.
 *
 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */
#include "bidi.h"
#include "break_lines.h"
#include "render_flow.h"
#include "render_text.h"
26 27 28
#include "render_arena.h"
#include "xml/dom_docimpl.h"

kocienda's avatar
kocienda committed
29 30 31 32 33 34 35 36 37
using namespace khtml;

#include "kdebug.h"
#include "qdatetime.h"
#include "qfontmetrics.h"

#define BIDI_DEBUG 0
//#define DEBUG_LINEBREAKS

38 39 40

static BidiIterator sor;
static BidiIterator eor;
darin's avatar
darin committed
41 42
static BidiIterator last;
static BidiIterator current;
43 44 45
static BidiContext *context;
static BidiStatus status;
static QPtrList<BidiRun> *sruns = 0;
46 47 48
static QPtrList<BidiIterator> *smidpoints = 0;
static bool betweenMidpoints = false;
static bool isLineEmpty = true;
49 50 51
static QChar::Direction dir;
static bool adjustEmbeddding = false;
static bool emptyRun = true;
darin's avatar
darin committed
52
static int numSpaces;
53 54

static void embed( QChar::Direction d );
darin's avatar
darin committed
55
static void appendRun();
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
void BidiIterator::detach(RenderArena* renderArena)
{
    delete this;
    
    // Now perform the destroy.
    size_t* sz = (size_t*)this;
    renderArena->free(*sz, (void*)this);
}

void* BidiIterator::operator new(size_t sz, RenderArena* renderArena) throw()
{
    return renderArena->allocate(sz);
}

void BidiIterator::operator delete(void* ptr, size_t sz) {
    size_t* szPtr = (size_t*)ptr;
    *szPtr = sz;
}

kocienda's avatar
kocienda committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
// ---------------------------------------------------------------------

/* a small helper class used internally to resolve Bidi embedding levels.
   Each line of text caches the embedding level at the start of the line for faster
   relayouting
*/
BidiContext::BidiContext(unsigned char l, QChar::Direction e, BidiContext *p, bool o)
    : level(l) , override(o), dir(e)
{
    parent = p;
    if(p) {
        p->ref();
        basicDir = p->basicDir;
    } else
        basicDir = e;
    count = 0;
}

BidiContext::~BidiContext()
{
    if(parent) parent->deref();
}

void BidiContext::ref() const
{
    count++;
}

void BidiContext::deref() const
{
    count--;
    if(count <= 0) delete this;
}

// ---------------------------------------------------------------------

darin's avatar
darin committed
112 113 114 115 116 117 118
inline bool operator==( const BidiIterator &it1, const BidiIterator &it2 )
{
    if(it1.pos != it2.pos) return false;
    if(it1.obj != it2.obj) return false;
    return true;
}

darin's avatar
darin committed
119
inline bool operator!=( const BidiIterator &it1, const BidiIterator &it2 )
darin's avatar
darin committed
120 121 122 123 124 125
{
    if(it1.pos != it2.pos) return true;
    if(it1.obj != it2.obj) return true;
    return false;
}

126 127 128 129 130 131 132 133 134 135
static inline RenderObject *Bidinext(RenderObject *par, RenderObject *current)
{
    RenderObject *next = 0;
    while(current != 0)
    {
        //kdDebug( 6040 ) << "current = " << current << endl;
	if(!current->isFloating() && !current->isReplaced() && !current->isPositioned()) {
	    next = current->firstChild();
	    if ( next && adjustEmbeddding ) {
		EUnicodeBidi ub = next->style()->unicodeBidi();
darin's avatar
darin committed
136
		if ( ub != UBNormal && !emptyRun ) {
137 138 139 140 141 142 143 144 145 146 147
		    EDirection dir = next->style()->direction();
		    QChar::Direction d = ( ub == Embed ? ( dir == RTL ? QChar::DirRLE : QChar::DirLRE )
					   : ( dir == RTL ? QChar::DirRLO : QChar::DirLRO ) );
		    embed( d );
		}
	    }
	}
	if(!next) {
	    while(current && current != par) {
		next = current->nextSibling();
		if(next) break;
darin's avatar
darin committed
148
		if ( adjustEmbeddding && current->style()->unicodeBidi() != UBNormal && !emptyRun ) {
149
		    embed( QChar::DirPDF );
darin's avatar
darin committed
150
		}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		current = current->parent();
	    }
	}

        if(!next) break;

        if(next->isText() || next->isBR() || next->isFloating() || next->isReplaced() || next->isPositioned())
            break;
        current = next;
    }
    return next;
}

static RenderObject *first( RenderObject *par )
{
    if(!par->firstChild()) return 0;
    RenderObject *o = par->firstChild();

    if(!o->isText() && !o->isBR() && !o->isReplaced() && !o->isFloating() && !o->isPositioned())
        o = Bidinext( par, o );

    return o;
}

kocienda's avatar
kocienda committed
175 176 177 178 179 180 181 182 183 184
BidiIterator::BidiIterator()
{
    par = 0;
    obj = 0;
    pos = 0;
}

BidiIterator::BidiIterator(RenderFlow *_par)
{
    par = _par;
185
    obj = first( par );
kocienda's avatar
kocienda committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    pos = 0;
}

BidiIterator::BidiIterator(const BidiIterator &it)
{
    par = it.par;
    obj = it.obj;
    pos = it.pos;
}

BidiIterator::BidiIterator(RenderFlow *_par, RenderObject *_obj, int _pos)
{
    par = _par;
    obj = _obj;
    pos = _pos;
}

BidiIterator &BidiIterator::operator = (const BidiIterator &it)
{
    obj = it.obj;
    pos = it.pos;
    par = it.par;
    return *this;
}

inline void BidiIterator::operator ++ ()
{
    if(!obj) return;
    if(obj->isText()) {
        pos++;
darin's avatar
darin committed
216
        if(pos >= static_cast<RenderText *>(obj)->stringLength()) {
217
            obj = Bidinext( par, obj );
kocienda's avatar
kocienda committed
218 219 220
            pos = 0;
        }
    } else {
221
        obj = Bidinext( par, obj );
kocienda's avatar
kocienda committed
222 223 224 225
        pos = 0;
    }
}

226
inline bool BidiIterator::atEnd() const
kocienda's avatar
kocienda committed
227 228 229 230 231
{
    if(!obj) return true;
    return false;
}

darin's avatar
darin committed
232 233 234
static const QChar nbsp = QChar(0xA0);

inline const QChar &BidiIterator::current() const
kocienda's avatar
kocienda committed
235 236 237 238 239
{
    if( !obj || !obj->isText()) return nbsp; // non breaking space
    return static_cast<RenderText *>(obj)->text()[pos];
}

darin's avatar
darin committed
240
inline QChar::Direction BidiIterator::direction() const
kocienda's avatar
kocienda committed
241
{
darin's avatar
darin committed
242 243
    if(!obj || !obj->isText() ) return QChar::DirON;
    
kocienda's avatar
kocienda committed
244
    RenderText *renderTxt = static_cast<RenderText *>( obj );
darin's avatar
darin committed
245
    if ( pos >= renderTxt->stringLength() )
kocienda's avatar
kocienda committed
246 247 248 249 250 251
        return QChar::DirON;
    return renderTxt->text()[pos].direction();
}

// -------------------------------------------------------------------------------------------------

252 253
static void appendRunsForObject(int start, int end, RenderObject* obj)
{
254 255 256
    if (start > end)
        return;
        
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    BidiIterator* nextMidpoint = (smidpoints && smidpoints->count()) ? smidpoints->at(0) : 0;
    if (betweenMidpoints) {
        if (!(nextMidpoint && nextMidpoint->obj == obj))
            return;
        // This is a new start point. Stop ignoring objects and 
        // adjust our start.
        betweenMidpoints = false;
        start = nextMidpoint->pos;
        smidpoints->removeFirst(); // Delete the midpoint.
        if (start < end)
            return appendRunsForObject(start, end, obj);
    }
    else {
        if (!smidpoints || !nextMidpoint || (obj != nextMidpoint->obj)) {
            sruns->append( new BidiRun(start, end, obj, context, dir) );
            return;
        }
        
        // An end midpoint has been encounted within our object.  We
        // need to go ahead and append a run with our endpoint.
277 278 279 280 281 282 283 284 285
        if (int(nextMidpoint->pos+1) <= end) {
            sruns->append( new BidiRun(start, nextMidpoint->pos+1, obj, context, dir) );
            betweenMidpoints = true;
            int nextPos = nextMidpoint->pos+1;
            smidpoints->removeFirst();
            return appendRunsForObject(nextPos, end, obj);
        }
        else
            sruns->append( new BidiRun(start, end, obj, context, dir) );
286 287 288
    }
}

289
static void appendRun()
kocienda's avatar
kocienda committed
290
{
darin's avatar
darin committed
291
    if ( emptyRun ) return;
kocienda's avatar
kocienda committed
292 293 294
#if BIDI_DEBUG > 1
    kdDebug(6041) << "appendRun: dir="<<(int)dir<<endl;
#endif
darin's avatar
darin committed
295

darin's avatar
darin committed
296 297
    bool b = adjustEmbeddding;
    adjustEmbeddding = false;
kocienda's avatar
kocienda committed
298 299 300 301

    int start = sor.pos;
    RenderObject *obj = sor.obj;
    while( obj && obj != eor.obj ) {
302
        appendRunsForObject(start, obj->length(), obj);        
kocienda's avatar
kocienda committed
303
        start = 0;
304
        obj = Bidinext( sor.par, obj );
kocienda's avatar
kocienda committed
305
    }
306 307
    appendRunsForObject(start, eor.pos+1, obj);
    
308 309 310 311
    ++eor;
    sor = eor;
    dir = QChar::DirON;
    status.eor = QChar::DirON;
darin's avatar
darin committed
312
    adjustEmbeddding = b;
313 314 315 316
}

static void embed( QChar::Direction d )
{
darin's avatar
darin committed
317
#if BIDI_DEBUG > 1
darin's avatar
darin committed
318
    qDebug("*** embed dir=%d emptyrun=%d", d, emptyRun );
darin's avatar
darin committed
319
#endif
320 321 322 323 324
    bool b = adjustEmbeddding ;
    adjustEmbeddding = false;
    if ( d == QChar::DirPDF ) {
	BidiContext *c = context->parent;
	if(c && sruns) {
darin's avatar
darin committed
325 326 327 328
	    if ( eor != last ) {
		appendRun();
		eor = last;
	    }
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	    appendRun();
	    emptyRun = true;
	    status.last = context->dir;
	    context->deref();
	    context = c;
	    if(context->override)
		dir = context->dir;
	    else
		dir = QChar::DirON;
	    status.lastStrong = context->dir;
	}
    } else {
	QChar::Direction runDir;
	if( d == QChar::DirRLE || d == QChar::DirRLO )
	    runDir = QChar::DirR;
	else
	    runDir = QChar::DirL;
	bool override;
	if( d == QChar::DirLRO || d == QChar::DirRLO )
	    override = true;
	else
	    override = false;

	unsigned char level = context->level;
	if ( runDir == QChar::DirR ) {
	    if(level%2) // we have an odd level
		level += 2;
	    else
		level++;
	} else {
	    if(level%2) // we have an odd level
		level++;
	    else
		level += 2;
	}

	if(level < 61) {
	    if ( sruns ) {
darin's avatar
darin committed
367 368 369 370
		if ( eor != last ) {
		    appendRun();
		    eor = last;
		}
371 372
		appendRun();
		emptyRun = true;
darin's avatar
darin committed
373

374 375 376 377 378 379 380 381 382 383
	    }
	    context = new BidiContext(level, runDir, context, override);
	    context->ref();
	    if ( override )
		dir = runDir;
	    status.last = runDir;
	    status.lastStrong = runDir;
	}
    }
    adjustEmbeddding = b;
kocienda's avatar
kocienda committed
384 385 386 387
}


// collects one line of the paragraph and transforms it to visual order
388
void RenderFlow::bidiReorderLine(const BidiIterator &start, const BidiIterator &end)
kocienda's avatar
kocienda committed
389
{
darin's avatar
darin committed
390
    if ( start == end ) {
391 392 393 394
        if ( start.current() == '\n' ) {
            m_height += lineHeight( firstLine );
        }
        return;
darin's avatar
darin committed
395
    }
darin's avatar
darin committed
396
#if BIDI_DEBUG > 1
darin's avatar
darin committed
397 398
    kdDebug(6041) << "reordering Line from " << start.obj << "/" << start.pos << " to " << end.obj << "/" << end.pos << endl;
#endif
darin's avatar
darin committed
399

400
    QPtrList<BidiRun> runs;
kocienda's avatar
kocienda committed
401
    runs.setAutoDelete(true);
402
    sruns = &runs;
kocienda's avatar
kocienda committed
403 404 405

    //    context->ref();

406 407
    dir = QChar::DirON;
    emptyRun = true;
darin's avatar
darin committed
408

darin's avatar
darin committed
409
    numSpaces = 0;
kocienda's avatar
kocienda committed
410

darin's avatar
darin committed
411 412 413 414
    current = start;
    last = current;
    bool atEnd = false;
    while( 1 ) {
415

kocienda's avatar
kocienda committed
416
        QChar::Direction dirCurrent;
417
        if (atEnd) {
kocienda's avatar
kocienda committed
418 419
            //kdDebug(6041) << "atEnd" << endl;
            BidiContext *c = context;
420 421 422
            if ( current.atEnd())
                while ( c->parent )
                    c = c->parent;
kocienda's avatar
kocienda committed
423
            dirCurrent = c->dir;
darin's avatar
darin committed
424
        } else {
kocienda's avatar
kocienda committed
425
            dirCurrent = current.direction();
426
        }
darin's avatar
darin committed
427

428
#ifndef QT_NO_UNICODETABLES
kocienda's avatar
kocienda committed
429 430 431 432 433 434 435 436 437 438 439 440 441

#if BIDI_DEBUG > 1
        kdDebug(6041) << "directions: dir=" << (int)dir << " current=" << (int)dirCurrent << " last=" << status.last << " eor=" << status.eor << " lastStrong=" << status.lastStrong << " embedding=" << (int)context->dir << " level =" << (int)context->level << endl;
#endif

        switch(dirCurrent) {

            // embedding and overrides (X1-X9 in the Bidi specs)
        case QChar::DirRLE:
        case QChar::DirLRE:
        case QChar::DirRLO:
        case QChar::DirLRO:
        case QChar::DirPDF:
442 443 444
            eor = last;
            embed( dirCurrent );
            break;
kocienda's avatar
kocienda committed
445 446 447 448 449 450 451 452 453 454 455 456 457

            // strong types
        case QChar::DirL:
            if(dir == QChar::DirON)
                dir = QChar::DirL;
            switch(status.last)
                {
                case QChar::DirL:
                    eor = current; status.eor = QChar::DirL; break;
                case QChar::DirR:
                case QChar::DirAL:
                case QChar::DirEN:
                case QChar::DirAN:
darin's avatar
darin committed
458
                    appendRun();
kocienda's avatar
kocienda committed
459 460 461 462 463 464 465 466 467 468 469 470
                    break;
                case QChar::DirES:
                case QChar::DirET:
                case QChar::DirCS:
                case QChar::DirBN:
                case QChar::DirB:
                case QChar::DirS:
                case QChar::DirWS:
                case QChar::DirON:
                    if(dir != QChar::DirL) {
                        //last stuff takes embedding dir
                        if( context->dir == QChar::DirR ) {
471
                            if(!(status.eor == QChar::DirR)) {
kocienda's avatar
kocienda committed
472
                                // AN or EN
473
                                appendRun();
kocienda's avatar
kocienda committed
474 475 476 477
                                dir = QChar::DirR;
                            }
                            else
                                eor = last;
478
                            appendRun();
kocienda's avatar
kocienda committed
479 480
                        } else {
                            if(status.eor == QChar::DirR) {
481
                                appendRun();
kocienda's avatar
kocienda committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
                                dir = QChar::DirL;
                            } else {
                                eor = current; status.eor = QChar::DirL; break;
                            }
                        }
                    } else {
                        eor = current; status.eor = QChar::DirL;
                    }
                default:
                    break;
                }
            status.lastStrong = QChar::DirL;
            break;
        case QChar::DirAL:
        case QChar::DirR:
            if(dir == QChar::DirON) dir = QChar::DirR;
            switch(status.last)
                {
                case QChar::DirR:
                case QChar::DirAL:
                    eor = current; status.eor = QChar::DirR; break;
                case QChar::DirL:
                case QChar::DirEN:
                case QChar::DirAN:
506
                    appendRun();
darin's avatar
darin committed
507 508 509
		    dir = QChar::DirR;
		    eor = current;
		    status.eor = QChar::DirR;
kocienda's avatar
kocienda committed
510 511 512 513 514 515 516 517 518
                    break;
                case QChar::DirES:
                case QChar::DirET:
                case QChar::DirCS:
                case QChar::DirBN:
                case QChar::DirB:
                case QChar::DirS:
                case QChar::DirWS:
                case QChar::DirON:
519
                    if( !(status.eor == QChar::DirR) && !(status.eor == QChar::DirAL) ) {
kocienda's avatar
kocienda committed
520 521
                        //last stuff takes embedding dir
                        if(context->dir == QChar::DirR || status.lastStrong == QChar::DirR) {
522
                            appendRun();
kocienda's avatar
kocienda committed
523 524
                            dir = QChar::DirR;
                            eor = current;
darin's avatar
darin committed
525
			    status.eor = QChar::DirR;
kocienda's avatar
kocienda committed
526 527
                        } else {
                            eor = last;
528
                            appendRun();
kocienda's avatar
kocienda committed
529
                            dir = QChar::DirR;
darin's avatar
darin committed
530
			    status.eor = QChar::DirR;
kocienda's avatar
kocienda committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
                        }
                    } else {
                        eor = current; status.eor = QChar::DirR;
                    }
                default:
                    break;
                }
            status.lastStrong = dirCurrent;
            break;

            // weak types:

        case QChar::DirNSM:
            // ### if @sor, set dir to dirSor
            break;
        case QChar::DirEN:
547
            if(!(status.lastStrong == QChar::DirAL)) {
kocienda's avatar
kocienda committed
548 549 550 551 552 553 554 555 556 557 558
                // if last strong was AL change EN to AN
                if(dir == QChar::DirON) {
                    if(status.lastStrong == QChar::DirAL)
                        dir = QChar::DirAN;
                    else
                        dir = QChar::DirL;
                }
                switch(status.last)
                    {
                    case QChar::DirET:
			if ( status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL ) {
559
			    appendRun();
kocienda's avatar
kocienda committed
560
			    dir = QChar::DirAN;
darin's avatar
darin committed
561
			    status.eor = QChar::DirAN;
kocienda's avatar
kocienda committed
562 563 564 565 566 567 568 569 570 571
			}
			// fall through
                    case QChar::DirEN:
                    case QChar::DirL:
                        eor = current;
                        status.eor = dirCurrent;
                        break;
                    case QChar::DirR:
                    case QChar::DirAL:
                    case QChar::DirAN:
572
                        appendRun();
darin's avatar
darin committed
573
			status.eor = QChar::DirEN;
kocienda's avatar
kocienda committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587
                        dir = QChar::DirAN; break;
                    case QChar::DirES:
                    case QChar::DirCS:
                        if(status.eor == QChar::DirEN) {
                            eor = current; break;
                        }
                    case QChar::DirBN:
                    case QChar::DirB:
                    case QChar::DirS:
                    case QChar::DirWS:
                    case QChar::DirON:
                        if(status.eor == QChar::DirR) {
                            // neutrals go to R
                            eor = last;
588
                            appendRun();
kocienda's avatar
kocienda committed
589 590 591 592 593 594 595 596
                            dir = QChar::DirAN;
                        }
                        else if( status.eor == QChar::DirL ||
                                 (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
                            eor = current; status.eor = dirCurrent;
                        } else {
                            // numbers on both sides, neutrals get right to left direction
                            if(dir != QChar::DirL) {
597
                                appendRun();
kocienda's avatar
kocienda committed
598 599
                                eor = last;
                                dir = QChar::DirR;
600
                                appendRun();
kocienda's avatar
kocienda committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
                                dir = QChar::DirAN;
                            } else {
                                eor = current; status.eor = dirCurrent;
                            }
                        }
                    default:
                        break;
                    }
                break;
            }
        case QChar::DirAN:
            dirCurrent = QChar::DirAN;
            if(dir == QChar::DirON) dir = QChar::DirAN;
            switch(status.last)
                {
                case QChar::DirL:
                case QChar::DirAN:
                    eor = current; status.eor = QChar::DirAN; break;
                case QChar::DirR:
                case QChar::DirAL:
                case QChar::DirEN:
622
                    appendRun();
kocienda's avatar
kocienda committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                    break;
                case QChar::DirCS:
                    if(status.eor == QChar::DirAN) {
                        eor = current; status.eor = QChar::DirR; break;
                    }
                case QChar::DirES:
                case QChar::DirET:
                case QChar::DirBN:
                case QChar::DirB:
                case QChar::DirS:
                case QChar::DirWS:
                case QChar::DirON:
                    if(status.eor == QChar::DirR) {
                        // neutrals go to R
                        eor = last;
638
                        appendRun();
kocienda's avatar
kocienda committed
639 640 641 642 643 644 645
                        dir = QChar::DirAN;
                    } else if( status.eor == QChar::DirL ||
                               (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
                        eor = current; status.eor = dirCurrent;
                    } else {
                        // numbers on both sides, neutrals get right to left direction
                        if(dir != QChar::DirL) {
646
                            appendRun();
kocienda's avatar
kocienda committed
647 648
                            eor = last;
                            dir = QChar::DirR;
649
                            appendRun();
kocienda's avatar
kocienda committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
                            dir = QChar::DirAN;
                        } else {
                            eor = current; status.eor = dirCurrent;
                        }
                    }
                default:
                    break;
                }
            break;
        case QChar::DirES:
        case QChar::DirCS:
            break;
        case QChar::DirET:
            if(status.last == QChar::DirEN) {
                dirCurrent = QChar::DirEN;
                eor = current; status.eor = dirCurrent;
                break;
            }
            break;

        // boundary neutrals should be ignored
        case QChar::DirBN:
            break;
            // neutrals
        case QChar::DirB:
            // ### what do we do with newline and paragraph seperators that come to here?
            break;
        case QChar::DirS:
            // ### implement rule L1
            break;
        case QChar::DirWS:
darin's avatar
darin committed
681
	    numSpaces++;
kocienda's avatar
kocienda committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        case QChar::DirON:
            break;
        default:
            break;
        }

        //cout << "     after: dir=" << //        dir << " current=" << dirCurrent << " last=" << status.last << " eor=" << status.eor << " lastStrong=" << status.lastStrong << " embedding=" << context->dir << endl;

        if(current.atEnd()) break;

        // set status.last as needed.
        switch(dirCurrent)
            {
            case QChar::DirET:
            case QChar::DirES:
            case QChar::DirCS:
            case QChar::DirS:
            case QChar::DirWS:
            case QChar::DirON:
                switch(status.last)
                    {
                    case QChar::DirL:
                    case QChar::DirR:
                    case QChar::DirAL:
                    case QChar::DirEN:
                    case QChar::DirAN:
                        status.last = dirCurrent;
                        break;
                    default:
                        status.last = QChar::DirON;
                    }
                break;
            case QChar::DirNSM:
            case QChar::DirBN:
                // ignore these
                break;
            default:
                status.last = dirCurrent;
            }
721
#endif
kocienda's avatar
kocienda committed
722

darin's avatar
darin committed
723
	if ( atEnd ) break;
kocienda's avatar
kocienda committed
724
        last = current;
725

darin's avatar
darin committed
726 727 728 729 730 731
	if ( emptyRun ) {
	    sor = current;
	    eor = current;
	    emptyRun = false;
	}

732 733 734
	// this causes the operator ++ to open and close embedding levels as needed
	// for the CSS unicode-bidi property
	adjustEmbeddding = true;
kocienda's avatar
kocienda committed
735
        ++current;
736
	adjustEmbeddding = false;
darin's avatar
darin committed
737 738 739 740 741 742

	if ( current == end ) {
	    if ( emptyRun )
		break;
	    atEnd = true;
	}
kocienda's avatar
kocienda committed
743 744 745
    }

#if BIDI_DEBUG > 0
darin's avatar
darin committed
746
    kdDebug(6041) << "reached end of line current=" << current.obj << "/" << current.pos
darin's avatar
darin committed
747
		  << ", eor=" << eor.obj << "/" << eor.pos << endl;
kocienda's avatar
kocienda committed
748
#endif
darin's avatar
darin committed
749 750 751
    if ( !emptyRun && sor != current ) {
	    eor = last;
	    appendRun();
752
    }
kocienda's avatar
kocienda committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

    BidiContext *endEmbed = context;
    // both commands below together give a noop...
    //endEmbed->ref();
    //context->deref();

    // reorder line according to run structure...

    // first find highest and lowest levels
    uchar levelLow = 128;
    uchar levelHigh = 0;
    BidiRun *r = runs.first();

    while ( r ) {
        //printf("level = %d\n", r->level);
        if ( r->level > levelHigh )
            levelHigh = r->level;
        if ( r->level < levelLow )
            levelLow = r->level;
        r = runs.next();
    }

    // implements reordering of the line (L2 according to Bidi spec):
    // L2. From the highest level found in the text to the lowest odd level on each line,
    // reverse any contiguous sequence of characters that are at that level or higher.

    // reversing is only done up to the lowest odd level
780
    if( !(levelLow%2) ) levelLow++;
kocienda's avatar
kocienda committed
781 782

#if BIDI_DEBUG > 0
darin's avatar
darin committed
783
    kdDebug(6041) << "lineLow = " << (uint)levelLow << ", lineHigh = " << (uint)levelHigh << endl;
kocienda's avatar
kocienda committed
784
    kdDebug(6041) << "logical order is:" << endl;
785
    QPtrListIterator<BidiRun> it2(runs);
kocienda's avatar
kocienda committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    BidiRun *r2;
    for ( ; (r2 = it2.current()); ++it2 )
        kdDebug(6041) << "    " << r2 << "  start=" << r2->start << "  stop=" << r2->stop << "  level=" << (uint)r2->level << endl;
#endif

    int count = runs.count() - 1;

    // do not reverse for visually ordered web sites
    if(!style()->visuallyOrdered()) {
        while(levelHigh >= levelLow) {
            int i = 0;
            while ( i < count ) {
                while(i < count && runs.at(i)->level < levelHigh)
                    i++;
                int start = i;
                while(i <= count && runs.at(i)->level >= levelHigh)
                    i++;
                int end = i-1;

                if(start != end) {
                    //kdDebug(6041) << "reversing from " << start << " to " << end << endl;
                    for(int j = 0; j < (end-start+1)/2; j++)
                        {
                            BidiRun *first = runs.take(start+j);
                            BidiRun *last = runs.take(end-j-1);
                            runs.insert(start+j, last);
                            runs.insert(end-j, first);
                        }
                }
                i++;
                if(i >= count) break;
            }
            levelHigh--;
        }
    }

#if BIDI_DEBUG > 0
    kdDebug(6041) << "visual order is:" << endl;
824
    QPtrListIterator<BidiRun> it3(runs);
kocienda's avatar
kocienda committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    BidiRun *r3;
    for ( ; (r3 = it3.current()); ++it3 )
    {
        kdDebug(6041) << "    " << r3 << endl;
    }
#endif

    int maxPositionTop = 0;
    int maxPositionBottom = 0;
    int maxAscent = 0;
    int maxDescent = 0;
    r = runs.first();
    while ( r ) {
        r->height = r->obj->lineHeight( firstLine );
	r->baseline = r->obj->baselinePosition( firstLine );
840 841
// 	if ( r->baseline > r->height )
// 	    r->baseline = r->height;
kocienda's avatar
kocienda committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        r->vertical = r->obj->verticalPositionHint( firstLine );
        //kdDebug(6041) << "object="<< r->obj << " height="<<r->height<<" baseline="<< r->baseline << " vertical=" << r->vertical <<endl;
        //int ascent;
        if ( r->vertical == PositionTop ) {
            if ( maxPositionTop < r->height ) maxPositionTop = r->height;
        }
        else if ( r->vertical == PositionBottom ) {
            if ( maxPositionBottom < r->height ) maxPositionBottom = r->height;
        }
        else {
            int ascent = r->baseline - r->vertical;
            int descent = r->height - ascent;
            if(maxAscent < ascent) maxAscent = ascent;
            if(maxDescent < descent) maxDescent = descent;
        }
        r = runs.next();
    }
    if ( maxAscent+maxDescent < QMAX( maxPositionTop, maxPositionBottom ) ) {
        // now the computed lineheight needs to be extended for the
        // positioned elements
        // see khtmltests/rendering/html_align.html
        // ### only iterate over the positioned ones!
        for ( r = runs.first(); r; r = runs.next() ) {
            if ( r->vertical == PositionTop ) {
                if ( maxAscent + maxDescent < r->height )
                    maxDescent = r->height - maxAscent;
            }
            else if ( r->vertical == PositionBottom ) {
                if ( maxAscent + maxDescent < r->height )
                    maxAscent = r->height - maxDescent;
            }
            else
                continue;

            if ( maxAscent + maxDescent >= QMAX( maxPositionTop, maxPositionBottom ) )
                break;

        }
    }
    int maxHeight = maxAscent + maxDescent;
882 883
    // CSS2: 10.8.1: line-height on the block level element specifies the *minimum*
    // height of the generated line box
kocienda's avatar
kocienda committed
884
    r = runs.first();
885 886 887 888 889 890 891
    // ### we have no reliable way of detecting empty lineboxes - which
    // are not allowed to have any height. sigh.(Dirk)
//     if ( r ) {
//         int blockHeight = lineHeight( firstLine );
//         if ( blockHeight > maxHeight )
//             maxHeight = blockHeight;
//     }
kocienda's avatar
kocienda committed
892
    int totWidth = 0;
893 894 895
#if BIDI_DEBUG > 0
    kdDebug( 6040 ) << "starting run.." << endl;
#endif
kocienda's avatar
kocienda committed
896 897 898 899 900 901 902 903
    while ( r ) {
        if(r->vertical == PositionTop)
            r->vertical = m_height;
        else if(r->vertical == PositionBottom)
            r->vertical = m_height + maxHeight - r->height;
        else
            r->vertical += m_height + maxAscent - r->baseline;

904
#if BIDI_DEBUG > 0
darin's avatar
darin committed
905
	kdDebug(6040) << "object="<< r->obj << " placing at vertical=" << r->vertical <<endl;
906
#endif
kocienda's avatar
kocienda committed
907
        if(r->obj->isText())
darin's avatar
darin committed
908
            r->width = static_cast<RenderText *>(r->obj)->width(r->start, r->stop-r->start, firstLine);
kocienda's avatar
kocienda committed
909 910 911 912 913 914 915
        else {
            r->obj->calcWidth();
            r->width = r->obj->width()+r->obj->marginLeft()+r->obj->marginRight();
        }
        totWidth += r->width;
        r = runs.next();
    }
916
    //kdDebug(6040) << "yPos of line=" << m_height << "  lineBoxHeight=" << maxHeight << endl;
kocienda's avatar
kocienda committed
917 918 919 920 921 922 923 924

    // now construct the reordered string out of the runs...

    r = runs.first();
    int x = leftOffset(m_height);
    int availableWidth = lineWidth(m_height);
    switch(style()->textAlign()) {
    case LEFT:
darin's avatar
darin committed
925
	numSpaces = 0;
kocienda's avatar
kocienda committed
926 927
        break;
    case JUSTIFY:
darin's avatar
darin committed
928
        if(numSpaces != 0 && !current.atEnd() && !current.obj->isBR() )
kocienda's avatar
kocienda committed
929
            break;
darin's avatar
darin committed
930 931 932
	// fall through
    case TAAUTO:
	numSpaces = 0;
kocienda's avatar
kocienda committed
933
        // for right to left fall through to right aligned
darin's avatar
darin committed
934 935
	if ( endEmbed->basicDir == QChar::DirL )
	    break;
kocienda's avatar
kocienda committed
936 937
    case RIGHT:
        x += availableWidth - totWidth;
darin's avatar
darin committed
938
	numSpaces = 0;
kocienda's avatar
kocienda committed
939 940 941 942 943
        break;
    case CENTER:
    case KONQ_CENTER:
        int xd = (availableWidth - totWidth)/2;
        x += xd>0?xd:0;
darin's avatar
darin committed
944
	numSpaces = 0;
kocienda's avatar
kocienda committed
945 946 947
        break;
    }
    while ( r ) {
948 949 950
#if BIDI_DEBUG > 1
        kdDebug(6040) << "positioning " << r->obj << " start=" << r->start << " stop=" << r->stop << " yPos=" << r->vertical << endl;
#endif
darin's avatar
darin committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
	int spaceAdd = 0;
	if ( numSpaces > 0 ) {
	    if ( r->obj->isText() ) {
		// get number of spaces in run
		int spaces = 0;
		for ( int i = r->start; i < r->stop; i++ )
		    if ( static_cast<RenderText *>(r->obj)->text()[i].direction() == QChar::DirWS )
			spaces++;
		if ( spaces > numSpaces ) // should never happen...
		    spaces = numSpaces;
		spaceAdd = (availableWidth - totWidth)*spaces/numSpaces;
		numSpaces -= spaces;
		totWidth += spaceAdd;
	    }
	}
        r->obj->position(x, r->vertical, r->start, r->stop - r->start, r->width, r->level%2, firstLine, spaceAdd);
        x += r->width + spaceAdd;
kocienda's avatar
kocienda committed
968 969 970 971 972
        r = runs.next();
    }

    m_height += maxHeight;

973
    sruns = 0;
kocienda's avatar
kocienda committed
974 975 976
}


977 978 979 980 981 982 983 984 985 986 987 988 989 990
static void deleteMidpoints(RenderArena* arena, QPtrList<BidiIterator>* midpoints)
{
    if (!midpoints)
        return;
        
    unsigned int len = midpoints->count();
    for(unsigned int i=0; i < len; i++) {
        BidiIterator* s = midpoints->at(i);
        if (s)
            s->detach(arena);
        midpoints->remove(i);
    }
}

kocienda's avatar
kocienda committed
991 992 993 994 995 996 997 998
void RenderFlow::layoutInlineChildren()
{
    invalidateVerticalPositions();
#ifdef DEBUG_LAYOUT
    QTime qt;
    qt.start();
    kdDebug( 6040 ) << renderName() << " layoutInlineChildren( " << this <<" )" << endl;
#endif
darin's avatar
darin committed
999 1000
#if BIDI_DEBUG > 1 || defined( DEBUG_LINEBREAKS )
    kdDebug(6041) << " ------- bidi start " << this << " -------" << endl;
darin's avatar
darin committed
1001
#endif
kocienda's avatar
kocienda committed
1002 1003 1004
    int toAdd = style()->borderBottomWidth();
    m_height = style()->borderTopWidth();

1005
    if(hasPadding())
kocienda's avatar
kocienda committed
1006 1007 1008 1009
    {
        m_height += paddingTop();
        toAdd += paddingBottom();
    }
1010
    
kocienda's avatar
kocienda committed
1011 1012
    if(firstChild()) {
        // layout replaced elements
1013
        RenderObject *o = first( this );
kocienda's avatar
kocienda committed
1014 1015 1016
        while ( o ) {
            if(o->isReplaced() || o->isFloating() || o->isPositioned()) {
                //kdDebug(6041) << "layouting replaced or floating child" << endl;
1017 1018 1019 1020
                if (o->isReplaced() && (o->style()->width().isPercent() || o->style()->height().isPercent()))
                    o->setLayouted(false);
                if( !o->layouted() )
                    o->layout();
kocienda's avatar
kocienda committed
1021
                if(o->isPositioned())
1022
                    static_cast<RenderFlow*>(o->containingBlock())->insertSpecialObject(o);
kocienda's avatar
kocienda committed
1023 1024 1025
            }
            else if(o->isText())
                static_cast<RenderText *>(o)->deleteSlaves();
1026
            o = Bidinext( this, o );
kocienda's avatar
kocienda committed
1027 1028 1029
        }

        BidiContext *startEmbed;
1030
        status = BidiStatus();
kocienda's avatar
kocienda committed
1031 1032 1033 1034 1035 1036 1037 1038 1039
        if( style()->direction() == LTR ) {
            startEmbed = new BidiContext( 0, QChar::DirL );
            status.eor = QChar::DirL;
        } else {
            startEmbed = new BidiContext( 1, QChar::DirR );
            status.eor = QChar::DirR;
        }
        startEmbed->ref();

1040
        context = startEmbed;
1041
        adjustEmbeddding = true;
kocienda's avatar
kocienda committed
1042
        BidiIterator start(this);
1043
        adjustEmbeddding = false;
kocienda's avatar
kocienda committed
1044 1045 1046
        BidiIterator end(this);

        firstLine = true;
1047 1048 1049 1050 1051 1052
        
        if (!smidpoints) {
            smidpoints = new QPtrList<BidiIterator>;
            smidpoints->setAutoDelete(false);
        }
                
kocienda's avatar
kocienda committed
1053 1054
        while( !end.atEnd() ) {
            start = end;
1055 1056 1057
            betweenMidpoints = false;
            isLineEmpty = true;
            end = findNextLineBreak(start, *smidpoints);
1058
            if( start.atEnd() ) break;
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
            if (!isLineEmpty) {
                bidiReorderLine(start, end);
    
                if( end == start || (end.obj && end.obj->isBR() && !start.obj->isBR() ) ) {
                    adjustEmbeddding = true;
                    ++end;
                    adjustEmbeddding = false;
                } else if(m_pre && end.current() == QChar('\n') ) {
                    adjustEmbeddding = true;
                    ++end;
                    adjustEmbeddding = false;
                }
    
                newLine();
darin's avatar
darin committed
1073
            }
kocienda's avatar
kocienda committed
1074
            firstLine = false;
1075
            deleteMidpoints(renderArena(), smidpoints);
kocienda's avatar
kocienda committed
1076 1077 1078 1079
        }
        startEmbed->deref();
        //embed->deref();
    }
1080 1081 1082
    
    deleteMidpoints(renderArena(), smidpoints);
    
kocienda's avatar
kocienda committed
1083 1084
    m_height += toAdd;

1085 1086
    // in case we have a float on the last line, it might not be positioned up to now.
    positionNewFloats();
kocienda's avatar
kocienda committed
1087

darin's avatar
darin committed
1088 1089
#if BIDI_DEBUG > 1
    kdDebug(6041) << " ------- bidi end " << this << " -------" << endl;
darin's avatar
darin committed
1090
#endif
kocienda's avatar
kocienda committed
1091 1092 1093 1094
    //kdDebug() << "RenderFlow::layoutInlineChildren time used " << qt.elapsed() << endl;
    //kdDebug(6040) << "height = " << m_height <<endl;
}

1095
BidiIterator RenderFlow::findNextLineBreak(BidiIterator &start, QPtrList<BidiIterator>& midpoints)
kocienda's avatar
kocienda committed
1096 1097 1098 1099 1100 1101 1102 1103 1104
{
    int width = lineWidth(m_height);
    int w = 0;
    int tmpW = 0;
#ifdef DEBUG_LINEBREAKS
    kdDebug(6041) << "findNextLineBreak: line at " << m_height << " line width " << width << endl;
    kdDebug(6041) << "sol: " << start.obj << " " << start.pos << endl;
#endif

1105 1106
    // eliminate spaces at beginning of line
    if(!m_pre) {
1107 1108
        // remove leading spaces
        while(!start.atEnd() &&
1109
#ifndef QT_NO_UNICODETABLES
1110
            ( start.direction() == QChar::DirWS || start.obj->isSpecial() )
1111
#else
1112
            ( start.current() == ' ' || start.obj->isSpecial() )
1113
#endif
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
            ) {
            if( start.obj->isSpecial() ) {
                RenderObject *o = start.obj;
                // add to special objects...
                if(o->isFloating()) {
                    insertSpecialObject(o);
                    // check if it fits in the current line.
                    // If it does, position it now, otherwise, position
                    // it after moving to next line (in newLine() func)
                    if (o->width()+o->marginLeft()+o->marginRight()+w+tmpW <= width) {
                        positionNewFloats();
                        width = lineWidth(m_height);
                    }
                } else if(o->isPositioned()) {
                    static_cast<RenderFlow*>(o->containingBlock())->insertSpecialObject(o);
                }
            }
    
            adjustEmbeddding = true;
            ++start;
            adjustEmbeddding = false;
        }
1136 1137
    }
    if ( start.atEnd() )
darin's avatar
darin committed
1138
        return start;
1139

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    // This variable is used only if whitespace isn't set to PRE, and it tells us whether
    // or not we are currently ignoring whitespace.
    bool ignoringSpaces = false;
    
    // This variable tracks whether the very last character we saw was a space.  We use
    // this to detect when we encounter a second space so we know we have to terminate
    // a run.
    bool sawSpace = false;
    RenderObject* trailingSpaceObject = 0;
    
    // The pos of the last whitespace char we saw, not to be confused with the lastSpace
    // variable below, which is really the last breakable char.
    int lastSpacePos = 0;
    
1154 1155
    BidiIterator lBreak = start;

kocienda's avatar
kocienda committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    RenderObject *o = start.obj;
    RenderObject *last = o;
    int pos = start.pos;

    while( o ) {
#ifdef DEBUG_LINEBREAKS
        kdDebug(6041) << "new object "<< o <<" width = " << w <<" tmpw = " << tmpW << endl;
#endif
        if(o->isBR()) {
            if( w + tmpW <= width ) {
                lBreak.obj = o;
                lBreak.pos = 0;
1168 1169 1170 1171 1172 1173 1174
             
                // A <br> always breaks a line, so don't let the line be collapsed
                // away. Also, the space at the end of a line with a <br> does not
                // get collapsed away. -dwh
                isLineEmpty = false;
                trailingSpaceObject = 0;
                
kocienda's avatar
kocienda committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
                //check the clear status
                EClear clear = o->style()->clear();
                if(clear != CNONE) {
                    m_clearStatus = (EClear) (m_clearStatus | clear);
                }
            }
            goto end;
        }
        if( o->isSpecial() ) {
            // add to special objects...
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
            if(o->isFloating()) {
                insertSpecialObject(o);
                // check if it fits in the current line.
                // If it does, position it now, otherwise, position
                // it after moving to next line (in newLine() func)
                if (o->width()+o->marginLeft()+o->marginRight()+w+tmpW <= width) {
                    positionNewFloats();
                    width = lineWidth(m_height);
                }
            } else if(o->isPositioned()) {
                static_cast<RenderFlow*>(o->containingBlock())->insertSpecialObject(o);
            }
kocienda's avatar
kocienda committed
1197 1198
        } else if ( o->isReplaced() ) {
            tmpW += o->width()+o->marginLeft()+o->marginRight();
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
            if (ignoringSpaces) {
                BidiIterator* startMid = new (o->renderArena()) BidiIterator();
                startMid->obj = o;
                startMid->pos = 0;
                midpoints.append(startMid);
            }
            isLineEmpty = false;
            ignoringSpaces = false;
            sawSpace = false;
            lastSpacePos = 0;
            trailingSpaceObject = 0;
kocienda's avatar
kocienda committed
1210
        } else if ( o->isText() ) {
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
            RenderText *t = static_cast<RenderText *>(o);
            int strlen = t->stringLength();
            int len = strlen - pos;
            QChar *str = t->text();

            const Font *f = t->htmlFont( firstLine );
            // proportional font, needs a bit more work.
            int lastSpace = pos;
            bool isPre = o->style()->whiteSpace() == PRE;
            //QChar space[1]; space[0] = ' ';
            //int spaceWidth = f->width(space, 1, 0);
            while(len) {
                //XXXdwh This is wrong. Still mutating the DOM
                // string for newlines... will fix in second stage.
                if (!isPre && str[pos] == '\n')
                    str[pos] = ' ';
                    
                bool oldSawSpace = sawSpace;
                sawSpace = (str[pos].direction() == QChar::DirWS);
                    
                if (isPre || !sawSpace)
                    isLineEmpty = false;
                    
                if( (isPre && str[pos] == '\n') ||
                    (!isPre && isBreakable( str, pos, strlen ) ) ) {
                    
                    if (ignoringSpaces) {
                        if (!sawSpace) {
                            // Stop ignoring spaces and begin at this
                            // new point.
                            BidiIterator* startMid = new (o->renderArena()) BidiIterator();
                            startMid->obj = o;
                            startMid->pos = pos;
                            midpoints.append(startMid);
                        }
                        else {
                            // Just keep ignoring these spaces.
                            pos++;
                            len--;
                            continue;
                        }
                    }
                    else {
                        if (sawSpace && !oldSawSpace)
                            lastSpacePos = pos;
                        tmpW += t->width(lastSpace, pos - lastSpace, f);
                    }
                    
kocienda's avatar
kocienda committed
1259
#ifdef DEBUG_LINEBREAKS
1260
                    kdDebug(6041) << "found space at " << pos << " in string '" << QString( str, strlen ).latin1() << "' adding " << tmpW << " new width = " << w << endl;
kocienda's avatar
kocienda committed
1261
#endif
1262 1263 1264 1265 1266 1267
                    if ( !isPre && w + tmpW > width && w == 0 ) {
                        int fb = floatBottom();
                        int newLineWidth = lineWidth(fb);
                        if(!w && m_height < fb && width < newLineWidth) {
                            m_height = fb;
                            width = newLineWidth;
kocienda's avatar
kocienda committed
1268
#ifdef DEBUG_LINEBREAKS
1269
                            kdDebug() << "RenderFlow::findNextLineBreak new position at " << m_height << " newWidth " << width << endl;
kocienda's avatar
kocienda committed
1270
#endif
1271 1272 1273 1274 1275
                        }
                    }
        
                    if ( !isPre && w + tmpW > width )
                        goto end;
kocienda's avatar
kocienda committed
1276

1277 1278 1279 1280
                    lBreak.obj = o;
                    lBreak.pos = pos;
    
                    if( *(str+pos) == '\n' && isPre) {
kocienda's avatar
kocienda committed
1281
#ifdef DEBUG_LINEBREAKS
1282
                        kdDebug(6041) << "forced break sol: " << start.obj << " " << start.pos << "   end: " << lBreak.obj << " " << lBreak.pos << "   width=" << w << endl;
kocienda's avatar
kocienda committed
1283
#endif
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
                        return lBreak;
                    }
                    
                    w += tmpW;
                    tmpW = 0;
                    lastSpace = pos;
                    
                    if (!ignoringSpaces && !isPre) {
                        // If we encounter a newline, or if we encounter a