Support visual cursor movement for BIDI text
[profile/ivi/qtbase.git] / src / gui / text / qtextengine.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdebug.h"
43 #include "qtextformat.h"
44 #include "qtextformat_p.h"
45 #include "qtextengine_p.h"
46 #include "qabstracttextdocumentlayout.h"
47 #include "qtextlayout.h"
48 #include "qtextboundaryfinder.h"
49 #include "qvarlengtharray.h"
50 #include "qfont.h"
51 #include "qfont_p.h"
52 #include "qfontengine_p.h"
53 #include "qstring.h"
54 #include <private/qunicodetables_p.h>
55 #include "qtextdocument_p.h"
56 #include <qapplication.h>
57 #include <stdlib.h>
58
59
60 QT_BEGIN_NAMESPACE
61
62 namespace {
63 // Helper class used in QTextEngine::itemize
64 // keep it out here to allow us to keep supporting various compilers.
65 class Itemizer {
66 public:
67     Itemizer(const QString &string, const QScriptAnalysis *analysis, QScriptItemArray &items)
68         : m_string(string),
69         m_analysis(analysis),
70         m_items(items),
71         m_splitter(0)
72     {
73     }
74     ~Itemizer()
75     {
76         delete m_splitter;
77     }
78
79     /// generate the script items
80     /// The caps parameter is used to choose the algoritm of splitting text and assiging roles to the textitems
81     void generate(int start, int length, QFont::Capitalization caps)
82     {
83         if ((int)caps == (int)QFont::SmallCaps)
84             generateScriptItemsSmallCaps(reinterpret_cast<const ushort *>(m_string.unicode()), start, length);
85         else if(caps == QFont::Capitalize)
86             generateScriptItemsCapitalize(start, length);
87         else if(caps != QFont::MixedCase) {
88             generateScriptItemsAndChangeCase(start, length,
89                 caps == QFont::AllLowercase ? QScriptAnalysis::Lowercase : QScriptAnalysis::Uppercase);
90         }
91         else
92             generateScriptItems(start, length);
93     }
94
95 private:
96     enum { MaxItemLength = 4096 };
97
98     void generateScriptItemsAndChangeCase(int start, int length, QScriptAnalysis::Flags flags)
99     {
100         generateScriptItems(start, length);
101         if (m_items.isEmpty()) // the next loop won't work in that case
102             return;
103         QScriptItemArray::Iterator iter = m_items.end();
104         do {
105             iter--;
106             if (iter->analysis.flags < QScriptAnalysis::TabOrObject)
107                 iter->analysis.flags = flags;
108         } while (iter->position > start);
109     }
110
111     void generateScriptItems(int start, int length)
112     {
113         if (!length)
114             return;
115         const int end = start + length;
116         for (int i = start + 1; i < end; ++i) {
117             if ((m_analysis[i] == m_analysis[start])
118                 && m_analysis[i].flags < QScriptAnalysis::SpaceTabOrObject
119                 && i - start < MaxItemLength)
120                 continue;
121             m_items.append(QScriptItem(start, m_analysis[start]));
122             start = i;
123         }
124         m_items.append(QScriptItem(start, m_analysis[start]));
125     }
126
127     void generateScriptItemsCapitalize(int start, int length)
128     {
129         if (!length)
130             return;
131
132         if (!m_splitter)
133             m_splitter = new QTextBoundaryFinder(QTextBoundaryFinder::Word,
134                                                  m_string.constData(), m_string.length(),
135                                                  /*buffer*/0, /*buffer size*/0);
136
137         m_splitter->setPosition(start);
138         QScriptAnalysis itemAnalysis = m_analysis[start];
139
140         if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartWord) {
141             itemAnalysis.flags = QScriptAnalysis::Uppercase;
142             m_splitter->toNextBoundary();
143         }
144
145         const int end = start + length;
146         for (int i = start + 1; i < end; ++i) {
147
148             bool atWordBoundary = false;
149
150             if (i == m_splitter->position()) {
151                 if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartWord
152                     && m_analysis[i].flags < QScriptAnalysis::TabOrObject)
153                     atWordBoundary = true;
154
155                 m_splitter->toNextBoundary();
156             }
157
158             if (m_analysis[i] == itemAnalysis
159                 && m_analysis[i].flags < QScriptAnalysis::TabOrObject
160                 && !atWordBoundary
161                 && i - start < MaxItemLength)
162                 continue;
163
164             m_items.append(QScriptItem(start, itemAnalysis));
165             start = i;
166             itemAnalysis = m_analysis[start];
167
168             if (atWordBoundary)
169                 itemAnalysis.flags = QScriptAnalysis::Uppercase;
170         }
171         m_items.append(QScriptItem(start, itemAnalysis));
172     }
173
174     void generateScriptItemsSmallCaps(const ushort *uc, int start, int length)
175     {
176         if (!length)
177             return;
178         bool lower = (QChar::category(uc[start]) == QChar::Letter_Lowercase);
179         const int end = start + length;
180         // split text into parts that are already uppercase and parts that are lowercase, and mark the latter to be uppercased later.
181         for (int i = start + 1; i < end; ++i) {
182             bool l = (QChar::category(uc[i]) == QChar::Letter_Lowercase);
183             if ((m_analysis[i] == m_analysis[start])
184                 && m_analysis[i].flags < QScriptAnalysis::TabOrObject
185                 && l == lower
186                 && i - start < MaxItemLength)
187                 continue;
188             m_items.append(QScriptItem(start, m_analysis[start]));
189             if (lower)
190                 m_items.last().analysis.flags = QScriptAnalysis::SmallCaps;
191
192             start = i;
193             lower = l;
194         }
195         m_items.append(QScriptItem(start, m_analysis[start]));
196         if (lower)
197             m_items.last().analysis.flags = QScriptAnalysis::SmallCaps;
198     }
199
200     const QString &m_string;
201     const QScriptAnalysis * const m_analysis;
202     QScriptItemArray &m_items;
203     QTextBoundaryFinder *m_splitter;
204 };
205 }
206
207
208 // ----------------------------------------------------------------------------
209 //
210 // The BiDi algorithm
211 //
212 // ----------------------------------------------------------------------------
213
214 #define BIDI_DEBUG 0
215 #if (BIDI_DEBUG >= 1)
216 QT_BEGIN_INCLUDE_NAMESPACE
217 #include <iostream>
218 QT_END_INCLUDE_NAMESPACE
219 using namespace std;
220
221 static const char *directions[] = {
222     "DirL", "DirR", "DirEN", "DirES", "DirET", "DirAN", "DirCS", "DirB", "DirS", "DirWS", "DirON",
223     "DirLRE", "DirLRO", "DirAL", "DirRLE", "DirRLO", "DirPDF", "DirNSM", "DirBN"
224 };
225
226 #endif
227
228 struct QBidiStatus {
229     QBidiStatus() {
230         eor = QChar::DirON;
231         lastStrong = QChar::DirON;
232         last = QChar:: DirON;
233         dir = QChar::DirON;
234     }
235     QChar::Direction eor;
236     QChar::Direction lastStrong;
237     QChar::Direction last;
238     QChar::Direction dir;
239 };
240
241 enum { MaxBidiLevel = 61 };
242
243 struct QBidiControl {
244     inline QBidiControl(bool rtl)
245         : cCtx(0), base(rtl ? 1 : 0), level(rtl ? 1 : 0), override(false) {}
246
247     inline void embed(bool rtl, bool o = false) {
248         unsigned int toAdd = 1;
249         if((level%2 != 0) == rtl ) {
250             ++toAdd;
251         }
252         if (level + toAdd <= MaxBidiLevel) {
253             ctx[cCtx].level = level;
254             ctx[cCtx].override = override;
255             cCtx++;
256             override = o;
257             level += toAdd;
258         }
259     }
260     inline bool canPop() const { return cCtx != 0; }
261     inline void pdf() {
262         Q_ASSERT(cCtx);
263         --cCtx;
264         level = ctx[cCtx].level;
265         override = ctx[cCtx].override;
266     }
267
268     inline QChar::Direction basicDirection() const {
269         return (base ? QChar::DirR : QChar:: DirL);
270     }
271     inline unsigned int baseLevel() const {
272         return base;
273     }
274     inline QChar::Direction direction() const {
275         return ((level%2) ? QChar::DirR : QChar:: DirL);
276     }
277
278     struct {
279         unsigned int level;
280         bool override;
281     } ctx[MaxBidiLevel];
282     unsigned int cCtx;
283     const unsigned int base;
284     unsigned int level;
285     bool override;
286 };
287
288
289 static void appendItems(QScriptAnalysis *analysis, int &start, int &stop, const QBidiControl &control, QChar::Direction dir)
290 {
291     if (start > stop)
292         return;
293
294     int level = control.level;
295
296     if(dir != QChar::DirON && !control.override) {
297         // add level of run (cases I1 & I2)
298         if(level % 2) {
299             if(dir == QChar::DirL || dir == QChar::DirAN || dir == QChar::DirEN)
300                 level++;
301         } else {
302             if(dir == QChar::DirR)
303                 level++;
304             else if(dir == QChar::DirAN || dir == QChar::DirEN)
305                 level += 2;
306         }
307     }
308
309 #if (BIDI_DEBUG >= 1)
310     qDebug("new run: dir=%s from %d, to %d level = %d override=%d", directions[dir], start, stop, level, control.override);
311 #endif
312     QScriptAnalysis *s = analysis + start;
313     const QScriptAnalysis *e = analysis + stop;
314     while (s <= e) {
315         s->bidiLevel = level;
316         ++s;
317     }
318     ++stop;
319     start = stop;
320 }
321
322
323 // creates the next QScript items.
324 static bool bidiItemize(QTextEngine *engine, QScriptAnalysis *analysis, QBidiControl &control)
325 {
326     bool rightToLeft = (control.basicDirection() == 1);
327     bool hasBidi = rightToLeft;
328 #if BIDI_DEBUG >= 2
329     qDebug() << "bidiItemize: rightToLeft=" << rightToLeft << engine->layoutData->string;
330 #endif
331
332     int sor = 0;
333     int eor = -1;
334
335
336     int length = engine->layoutData->string.length();
337
338     const ushort *unicode = (const ushort *)engine->layoutData->string.unicode();
339     int current = 0;
340
341     QChar::Direction dir = rightToLeft ? QChar::DirR : QChar::DirL;
342     QBidiStatus status;
343
344     QChar::Direction sdir = QChar::direction(*unicode);
345     if (sdir != QChar::DirL && sdir != QChar::DirR && sdir != QChar::DirEN && sdir != QChar::DirAN)
346         sdir = QChar::DirON;
347     else
348         dir = QChar::DirON;
349     status.eor = sdir;
350     status.lastStrong = rightToLeft ? QChar::DirR : QChar::DirL;
351     status.last = status.lastStrong;
352     status.dir = sdir;
353
354
355     while (current <= length) {
356
357         QChar::Direction dirCurrent;
358         if (current == (int)length)
359             dirCurrent = control.basicDirection();
360         else
361             dirCurrent = QChar::direction(unicode[current]);
362
363 #if (BIDI_DEBUG >= 2)
364 //         qDebug() << "pos=" << current << " dir=" << directions[dir]
365 //                  << " current=" << directions[dirCurrent] << " last=" << directions[status.last]
366 //                  << " eor=" << eor << '/' << directions[status.eor]
367 //                  << " sor=" << sor << " lastStrong="
368 //                  << directions[status.lastStrong]
369 //                  << " level=" << (int)control.level << " override=" << (bool)control.override;
370 #endif
371
372         switch(dirCurrent) {
373
374             // embedding and overrides (X1-X9 in the BiDi specs)
375         case QChar::DirRLE:
376         case QChar::DirRLO:
377         case QChar::DirLRE:
378         case QChar::DirLRO:
379             {
380                 bool rtl = (dirCurrent == QChar::DirRLE || dirCurrent == QChar::DirRLO);
381                 hasBidi |= rtl;
382                 bool override = (dirCurrent == QChar::DirLRO || dirCurrent == QChar::DirRLO);
383
384                 unsigned int level = control.level+1;
385                 if ((level%2 != 0) == rtl) ++level;
386                 if(level < MaxBidiLevel) {
387                     eor = current-1;
388                     appendItems(analysis, sor, eor, control, dir);
389                     eor = current;
390                     control.embed(rtl, override);
391                     QChar::Direction edir = (rtl ? QChar::DirR : QChar::DirL);
392                     dir = status.eor = edir;
393                     status.lastStrong = edir;
394                 }
395                 break;
396             }
397         case QChar::DirPDF:
398             {
399                 if (control.canPop()) {
400                     if (dir != control.direction()) {
401                         eor = current-1;
402                         appendItems(analysis, sor, eor, control, dir);
403                         dir = control.direction();
404                     }
405                     eor = current;
406                     appendItems(analysis, sor, eor, control, dir);
407                     control.pdf();
408                     dir = QChar::DirON; status.eor = QChar::DirON;
409                     status.last = control.direction();
410                     if (control.override)
411                         dir = control.direction();
412                     else
413                         dir = QChar::DirON;
414                     status.lastStrong = control.direction();
415                 }
416                 break;
417             }
418
419             // strong types
420         case QChar::DirL:
421             if(dir == QChar::DirON)
422                 dir = QChar::DirL;
423             switch(status.last)
424                 {
425                 case QChar::DirL:
426                     eor = current; status.eor = QChar::DirL; break;
427                 case QChar::DirR:
428                 case QChar::DirAL:
429                 case QChar::DirEN:
430                 case QChar::DirAN:
431                     if (eor >= 0) {
432                         appendItems(analysis, sor, eor, control, dir);
433                         dir = eor < length ? QChar::direction(unicode[eor]) : control.basicDirection();
434                         status.eor = dir;
435                     } else {
436                         eor = current; status.eor = dir;
437                     }
438                     break;
439                 case QChar::DirES:
440                 case QChar::DirET:
441                 case QChar::DirCS:
442                 case QChar::DirBN:
443                 case QChar::DirB:
444                 case QChar::DirS:
445                 case QChar::DirWS:
446                 case QChar::DirON:
447                     if(dir != QChar::DirL) {
448                         //last stuff takes embedding dir
449                         if(control.direction() == QChar::DirR) {
450                             if(status.eor != QChar::DirR) {
451                                 // AN or EN
452                                 appendItems(analysis, sor, eor, control, dir);
453                                 status.eor = QChar::DirON;
454                                 dir = QChar::DirR;
455                             }
456                             eor = current - 1;
457                             appendItems(analysis, sor, eor, control, dir);
458                             dir = eor < length ? QChar::direction(unicode[eor]) : control.basicDirection();
459                             status.eor = dir;
460                         } else {
461                             if(status.eor != QChar::DirL) {
462                                 appendItems(analysis, sor, eor, control, dir);
463                                 status.eor = QChar::DirON;
464                                 dir = QChar::DirL;
465                             } else {
466                                 eor = current; status.eor = QChar::DirL; break;
467                             }
468                         }
469                     } else {
470                         eor = current; status.eor = QChar::DirL;
471                     }
472                 default:
473                     break;
474                 }
475             status.lastStrong = QChar::DirL;
476             break;
477         case QChar::DirAL:
478         case QChar::DirR:
479             hasBidi = true;
480             if(dir == QChar::DirON) dir = QChar::DirR;
481             switch(status.last)
482                 {
483                 case QChar::DirL:
484                 case QChar::DirEN:
485                 case QChar::DirAN:
486                     if (eor >= 0)
487                         appendItems(analysis, sor, eor, control, dir);
488                     // fall through
489                 case QChar::DirR:
490                 case QChar::DirAL:
491                     dir = QChar::DirR; eor = current; status.eor = QChar::DirR; break;
492                 case QChar::DirES:
493                 case QChar::DirET:
494                 case QChar::DirCS:
495                 case QChar::DirBN:
496                 case QChar::DirB:
497                 case QChar::DirS:
498                 case QChar::DirWS:
499                 case QChar::DirON:
500                     if(status.eor != QChar::DirR && status.eor != QChar::DirAL) {
501                         //last stuff takes embedding dir
502                         if(control.direction() == QChar::DirR
503                            || status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) {
504                             appendItems(analysis, sor, eor, control, dir);
505                             dir = QChar::DirR; status.eor = QChar::DirON;
506                             eor = current;
507                         } else {
508                             eor = current - 1;
509                             appendItems(analysis, sor, eor, control, dir);
510                             dir = QChar::DirR; status.eor = QChar::DirON;
511                         }
512                     } else {
513                         eor = current; status.eor = QChar::DirR;
514                     }
515                 default:
516                     break;
517                 }
518             status.lastStrong = dirCurrent;
519             break;
520
521             // weak types:
522
523         case QChar::DirNSM:
524             if (eor == current-1)
525                 eor = current;
526             break;
527         case QChar::DirEN:
528             // if last strong was AL change EN to AN
529             if(status.lastStrong != QChar::DirAL) {
530                 if(dir == QChar::DirON) {
531                     if(status.lastStrong == QChar::DirL)
532                         dir = QChar::DirL;
533                     else
534                         dir = QChar::DirEN;
535                 }
536                 switch(status.last)
537                     {
538                     case QChar::DirET:
539                         if (status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) {
540                             appendItems(analysis, sor, eor, control, dir);
541                             status.eor = QChar::DirON;
542                             dir = QChar::DirAN;
543                         }
544                         // fall through
545                     case QChar::DirEN:
546                     case QChar::DirL:
547                         eor = current;
548                         status.eor = dirCurrent;
549                         break;
550                     case QChar::DirR:
551                     case QChar::DirAL:
552                     case QChar::DirAN:
553                         if (eor >= 0)
554                             appendItems(analysis, sor, eor, control, dir);
555                         else
556                             eor = current;
557                         status.eor = QChar::DirEN;
558                         dir = QChar::DirAN; break;
559                     case QChar::DirES:
560                     case QChar::DirCS:
561                         if(status.eor == QChar::DirEN || dir == QChar::DirAN) {
562                             eor = current; break;
563                         }
564                     case QChar::DirBN:
565                     case QChar::DirB:
566                     case QChar::DirS:
567                     case QChar::DirWS:
568                     case QChar::DirON:
569                         if(status.eor == QChar::DirR) {
570                             // neutrals go to R
571                             eor = current - 1;
572                             appendItems(analysis, sor, eor, control, dir);
573                             dir = QChar::DirON; status.eor = QChar::DirEN;
574                             dir = QChar::DirAN;
575                         }
576                         else if(status.eor == QChar::DirL ||
577                                  (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
578                             eor = current; status.eor = dirCurrent;
579                         } else {
580                             // numbers on both sides, neutrals get right to left direction
581                             if(dir != QChar::DirL) {
582                                 appendItems(analysis, sor, eor, control, dir);
583                                 dir = QChar::DirON; status.eor = QChar::DirON;
584                                 eor = current - 1;
585                                 dir = QChar::DirR;
586                                 appendItems(analysis, sor, eor, control, dir);
587                                 dir = QChar::DirON; status.eor = QChar::DirON;
588                                 dir = QChar::DirAN;
589                             } else {
590                                 eor = current; status.eor = dirCurrent;
591                             }
592                         }
593                     default:
594                         break;
595                     }
596                 break;
597             }
598         case QChar::DirAN:
599             hasBidi = true;
600             dirCurrent = QChar::DirAN;
601             if(dir == QChar::DirON) dir = QChar::DirAN;
602             switch(status.last)
603                 {
604                 case QChar::DirL:
605                 case QChar::DirAN:
606                     eor = current; status.eor = QChar::DirAN; break;
607                 case QChar::DirR:
608                 case QChar::DirAL:
609                 case QChar::DirEN:
610                     if (eor >= 0){
611                         appendItems(analysis, sor, eor, control, dir);
612                     } else {
613                         eor = current;
614                     }
615                     dir = QChar::DirAN; status.eor = QChar::DirAN;
616                     break;
617                 case QChar::DirCS:
618                     if(status.eor == QChar::DirAN) {
619                         eor = current; break;
620                     }
621                 case QChar::DirES:
622                 case QChar::DirET:
623                 case QChar::DirBN:
624                 case QChar::DirB:
625                 case QChar::DirS:
626                 case QChar::DirWS:
627                 case QChar::DirON:
628                     if(status.eor == QChar::DirR) {
629                         // neutrals go to R
630                         eor = current - 1;
631                         appendItems(analysis, sor, eor, control, dir);
632                         status.eor = QChar::DirAN;
633                         dir = QChar::DirAN;
634                     } else if(status.eor == QChar::DirL ||
635                                (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
636                         eor = current; status.eor = dirCurrent;
637                     } else {
638                         // numbers on both sides, neutrals get right to left direction
639                         if(dir != QChar::DirL) {
640                             appendItems(analysis, sor, eor, control, dir);
641                             status.eor = QChar::DirON;
642                             eor = current - 1;
643                             dir = QChar::DirR;
644                             appendItems(analysis, sor, eor, control, dir);
645                             status.eor = QChar::DirAN;
646                             dir = QChar::DirAN;
647                         } else {
648                             eor = current; status.eor = dirCurrent;
649                         }
650                     }
651                 default:
652                     break;
653                 }
654             break;
655         case QChar::DirES:
656         case QChar::DirCS:
657             break;
658         case QChar::DirET:
659             if(status.last == QChar::DirEN) {
660                 dirCurrent = QChar::DirEN;
661                 eor = current; status.eor = dirCurrent;
662             }
663             break;
664
665             // boundary neutrals should be ignored
666         case QChar::DirBN:
667             break;
668             // neutrals
669         case QChar::DirB:
670             // ### what do we do with newline and paragraph separators that come to here?
671             break;
672         case QChar::DirS:
673             // ### implement rule L1
674             break;
675         case QChar::DirWS:
676         case QChar::DirON:
677             break;
678         default:
679             break;
680         }
681
682         //qDebug() << "     after: dir=" << //        dir << " current=" << dirCurrent << " last=" << status.last << " eor=" << status.eor << " lastStrong=" << status.lastStrong << " embedding=" << control.direction();
683
684         if(current >= (int)length) break;
685
686         // set status.last as needed.
687         switch(dirCurrent) {
688         case QChar::DirET:
689         case QChar::DirES:
690         case QChar::DirCS:
691         case QChar::DirS:
692         case QChar::DirWS:
693         case QChar::DirON:
694             switch(status.last)
695             {
696             case QChar::DirL:
697             case QChar::DirR:
698             case QChar::DirAL:
699             case QChar::DirEN:
700             case QChar::DirAN:
701                 status.last = dirCurrent;
702                 break;
703             default:
704                 status.last = QChar::DirON;
705             }
706             break;
707         case QChar::DirNSM:
708         case QChar::DirBN:
709             // ignore these
710             break;
711         case QChar::DirLRO:
712         case QChar::DirLRE:
713             status.last = QChar::DirL;
714             break;
715         case QChar::DirRLO:
716         case QChar::DirRLE:
717             status.last = QChar::DirR;
718             break;
719         case QChar::DirEN:
720             if (status.last == QChar::DirL) {
721                 status.last = QChar::DirL;
722                 break;
723             }
724             // fall through
725         default:
726             status.last = dirCurrent;
727         }
728
729         ++current;
730     }
731
732 #if (BIDI_DEBUG >= 1)
733     qDebug() << "reached end of line current=" << current << ", eor=" << eor;
734 #endif
735     eor = current - 1; // remove dummy char
736
737     if (sor <= eor)
738         appendItems(analysis, sor, eor, control, dir);
739
740     return hasBidi;
741 }
742
743 void QTextEngine::bidiReorder(int numItems, const quint8 *levels, int *visualOrder)
744 {
745
746     // first find highest and lowest levels
747     quint8 levelLow = 128;
748     quint8 levelHigh = 0;
749     int i = 0;
750     while (i < numItems) {
751         //printf("level = %d\n", r->level);
752         if (levels[i] > levelHigh)
753             levelHigh = levels[i];
754         if (levels[i] < levelLow)
755             levelLow = levels[i];
756         i++;
757     }
758
759     // implements reordering of the line (L2 according to BiDi spec):
760     // L2. From the highest level found in the text to the lowest odd level on each line,
761     // reverse any contiguous sequence of characters that are at that level or higher.
762
763     // reversing is only done up to the lowest odd level
764     if(!(levelLow%2)) levelLow++;
765
766 #if (BIDI_DEBUG >= 1)
767 //     qDebug() << "reorderLine: lineLow = " << (uint)levelLow << ", lineHigh = " << (uint)levelHigh;
768 #endif
769
770     int count = numItems - 1;
771     for (i = 0; i < numItems; i++)
772         visualOrder[i] = i;
773
774     while(levelHigh >= levelLow) {
775         int i = 0;
776         while (i < count) {
777             while(i < count && levels[i] < levelHigh) i++;
778             int start = i;
779             while(i <= count && levels[i] >= levelHigh) i++;
780             int end = i-1;
781
782             if(start != end) {
783                 //qDebug() << "reversing from " << start << " to " << end;
784                 for(int j = 0; j < (end-start+1)/2; j++) {
785                     int tmp = visualOrder[start+j];
786                     visualOrder[start+j] = visualOrder[end-j];
787                     visualOrder[end-j] = tmp;
788                 }
789             }
790             i++;
791         }
792         levelHigh--;
793     }
794
795 #if (BIDI_DEBUG >= 1)
796 //     qDebug() << "visual order is:";
797 //     for (i = 0; i < numItems; i++)
798 //         qDebug() << visualOrder[i];
799 #endif
800 }
801
802 QT_BEGIN_INCLUDE_NAMESPACE
803
804 #if defined(Q_WS_X11) || defined (Q_WS_QWS)
805 #   include "qfontengine_ft_p.h"
806 #elif defined(Q_WS_MAC)
807 # include "qtextengine_mac.cpp"
808 #endif
809
810 #include <private/qharfbuzz_p.h>
811
812 QT_END_INCLUDE_NAMESPACE
813
814 // ask the font engine to find out which glyphs (as an index in the specific font) to use for the text in one item.
815 static bool stringToGlyphs(HB_ShaperItem *item, QGlyphLayout *glyphs, QFontEngine *fontEngine)
816 {
817     int nGlyphs = item->num_glyphs;
818
819     QTextEngine::ShaperFlags shaperFlags(QTextEngine::GlyphIndicesOnly);
820     if (item->item.bidiLevel % 2)
821         shaperFlags |= QTextEngine::RightToLeft;
822
823     bool result = fontEngine->stringToCMap(reinterpret_cast<const QChar *>(item->string + item->item.pos), item->item.length, glyphs, &nGlyphs, shaperFlags);
824     item->num_glyphs = nGlyphs;
825     glyphs->numGlyphs = nGlyphs;
826     return result;
827 }
828
829 // shape all the items that intersect with the line, taking tab widths into account to find out what text actually fits in the line.
830 void QTextEngine::shapeLine(const QScriptLine &line)
831 {
832     QFixed x;
833     bool first = true;
834     const int end = findItem(line.from + line.length - 1);
835     int item = findItem(line.from);
836     if (item == -1)
837         return;
838     for (item = findItem(line.from); item <= end; ++item) {
839         QScriptItem &si = layoutData->items[item];
840         if (si.analysis.flags == QScriptAnalysis::Tab) {
841             ensureSpace(1);
842             si.width = calculateTabWidth(item, x);
843         } else {
844             shape(item);
845         }
846         if (first && si.position != line.from) { // that means our x position has to be offset
847             QGlyphLayout glyphs = shapedGlyphs(&si);
848             Q_ASSERT(line.from > si.position);
849             for (int i = line.from - si.position - 1; i >= 0; i--) {
850                 x -= glyphs.effectiveAdvance(i);
851             }
852         }
853         first = false;
854
855         x += si.width;
856     }
857 }
858
859 void QTextEngine::shapeText(int item) const
860 {
861     Q_ASSERT(item < layoutData->items.size());
862     QScriptItem &si = layoutData->items[item];
863
864     if (si.num_glyphs)
865         return;
866
867 #if defined(Q_WS_MAC)
868     shapeTextMac(item);
869 #elif defined(Q_WS_WINCE)
870     shapeTextWithCE(item);
871 #else
872     shapeTextWithHarfbuzz(item);
873 #endif
874
875     si.width = 0;
876
877     if (!si.num_glyphs)
878         return;
879     QGlyphLayout glyphs = shapedGlyphs(&si);
880
881     QFont font = this->font(si);
882     bool letterSpacingIsAbsolute = font.d->letterSpacingIsAbsolute;
883     QFixed letterSpacing = font.d->letterSpacing;
884     QFixed wordSpacing = font.d->wordSpacing;
885
886     if (letterSpacingIsAbsolute && letterSpacing.value())
887         letterSpacing *= font.d->dpi / qt_defaultDpiY();
888
889     if (letterSpacing != 0) {
890         for (int i = 1; i < si.num_glyphs; ++i) {
891             if (glyphs.attributes[i].clusterStart) {
892                 if (letterSpacingIsAbsolute)
893                     glyphs.advances_x[i-1] += letterSpacing;
894                 else {
895                     QFixed &advance = glyphs.advances_x[i-1];
896                     advance += (letterSpacing - 100) * advance / 100;
897                 }
898             }
899         }
900         if (letterSpacingIsAbsolute)
901             glyphs.advances_x[si.num_glyphs-1] += letterSpacing;
902         else {
903             QFixed &advance = glyphs.advances_x[si.num_glyphs-1];
904             advance += (letterSpacing - 100) * advance / 100;
905         }
906     }
907     if (wordSpacing != 0) {
908         for (int i = 0; i < si.num_glyphs; ++i) {
909             if (glyphs.attributes[i].justification == HB_Space
910                 || glyphs.attributes[i].justification == HB_Arabic_Space) {
911                 // word spacing only gets added once to a consecutive run of spaces (see CSS spec)
912                 if (i + 1 == si.num_glyphs
913                     ||(glyphs.attributes[i+1].justification != HB_Space
914                        && glyphs.attributes[i+1].justification != HB_Arabic_Space))
915                     glyphs.advances_x[i] += wordSpacing;
916             }
917         }
918     }
919
920     for (int i = 0; i < si.num_glyphs; ++i)
921         si.width += glyphs.advances_x[i];
922 }
923
924 static inline bool hasCaseChange(const QScriptItem &si)
925 {
926     return si.analysis.flags == QScriptAnalysis::SmallCaps ||
927            si.analysis.flags == QScriptAnalysis::Uppercase ||
928            si.analysis.flags == QScriptAnalysis::Lowercase;
929 }
930
931 #if defined(Q_WS_WINCE) //TODO
932 // set the glyph attributes heuristically. Assumes a 1 to 1 relationship between chars and glyphs
933 // and no reordering.
934 // also computes logClusters heuristically
935 static void heuristicSetGlyphAttributes(const QChar *uc, int length, QGlyphLayout *glyphs, unsigned short *logClusters, int num_glyphs)
936 {
937     // ### zeroWidth and justification are missing here!!!!!
938
939     Q_UNUSED(num_glyphs);
940     Q_ASSERT(num_glyphs <= length);
941
942 //     qDebug("QScriptEngine::heuristicSetGlyphAttributes, num_glyphs=%d", item->num_glyphs);
943
944     int glyph_pos = 0;
945     for (int i = 0; i < length; i++) {
946         if (uc[i].unicode() >= 0xd800 && uc[i].unicode() < 0xdc00 && i < length-1
947             && uc[i+1].unicode() >= 0xdc00 && uc[i+1].unicode() < 0xe000) {
948             logClusters[i] = glyph_pos;
949             logClusters[++i] = glyph_pos;
950         } else {
951             logClusters[i] = glyph_pos;
952         }
953         ++glyph_pos;
954     }
955
956     // first char in a run is never (treated as) a mark
957     int cStart = 0;
958
959     const bool symbolFont = false; // ####
960     glyphs->attributes[0].mark = false;
961     glyphs->attributes[0].clusterStart = true;
962     glyphs->attributes[0].dontPrint = (!symbolFont && uc[0].unicode() == 0x00ad) || qIsControlChar(uc[0].unicode());
963
964     int pos = 0;
965     int lastCat = QChar::category(uc[0].unicode());
966     for (int i = 1; i < length; ++i) {
967         if (logClusters[i] == pos)
968             // same glyph
969             continue;
970         ++pos;
971         while (pos < logClusters[i]) {
972             glyphs[pos].attributes = glyphs[pos-1].attributes;
973             ++pos;
974         }
975         // hide soft-hyphens by default
976         if ((!symbolFont && uc[i].unicode() == 0x00ad) || qIsControlChar(uc[i].unicode()))
977             glyphs->attributes[pos].dontPrint = true;
978         const QUnicodeTables::Properties *prop = QUnicodeTables::properties(uc[i].unicode());
979         int cat = prop->category;
980         if (cat != QChar::Mark_NonSpacing) {
981             glyphs->attributes[pos].mark = false;
982             glyphs->attributes[pos].clusterStart = true;
983             glyphs->attributes[pos].combiningClass = 0;
984             cStart = logClusters[i];
985         } else {
986             int cmb = prop->combiningClass;
987
988             if (cmb == 0) {
989                 // Fix 0 combining classes
990                 if ((uc[pos].unicode() & 0xff00) == 0x0e00) {
991                     // thai or lao
992                     unsigned char col = uc[pos].cell();
993                     if (col == 0x31 ||
994                          col == 0x34 ||
995                          col == 0x35 ||
996                          col == 0x36 ||
997                          col == 0x37 ||
998                          col == 0x47 ||
999                          col == 0x4c ||
1000                          col == 0x4d ||
1001                          col == 0x4e) {
1002                         cmb = QChar::Combining_AboveRight;
1003                     } else if (col == 0xb1 ||
1004                                 col == 0xb4 ||
1005                                 col == 0xb5 ||
1006                                 col == 0xb6 ||
1007                                 col == 0xb7 ||
1008                                 col == 0xbb ||
1009                                 col == 0xcc ||
1010                                 col == 0xcd) {
1011                         cmb = QChar::Combining_Above;
1012                     } else if (col == 0xbc) {
1013                         cmb = QChar::Combining_Below;
1014                     }
1015                 }
1016             }
1017
1018             glyphs->attributes[pos].mark = true;
1019             glyphs->attributes[pos].clusterStart = false;
1020             glyphs->attributes[pos].combiningClass = cmb;
1021             logClusters[i] = cStart;
1022             glyphs->advances_x[pos] = 0;
1023             glyphs->advances_y[pos] = 0;
1024         }
1025
1026         // one gets an inter character justification point if the current char is not a non spacing mark.
1027         // as then the current char belongs to the last one and one gets a space justification point
1028         // after the space char.
1029         if (lastCat == QChar::Separator_Space)
1030             glyphs->attributes[pos-1].justification = HB_Space;
1031         else if (cat != QChar::Mark_NonSpacing)
1032             glyphs->attributes[pos-1].justification = HB_Character;
1033         else
1034             glyphs->attributes[pos-1].justification = HB_NoJustification;
1035
1036         lastCat = cat;
1037     }
1038     pos = logClusters[length-1];
1039     if (lastCat == QChar::Separator_Space)
1040         glyphs->attributes[pos].justification = HB_Space;
1041     else
1042         glyphs->attributes[pos].justification = HB_Character;
1043 }
1044
1045 void QTextEngine::shapeTextWithCE(int item) const
1046 {
1047     QScriptItem &si = layoutData->items[item];
1048     si.glyph_data_offset = layoutData->used;
1049
1050     QFontEngine *fe = fontEngine(si, &si.ascent, &si.descent, &si.leading);
1051
1052     QTextEngine::ShaperFlags flags;
1053     if (si.analysis.bidiLevel % 2)
1054         flags |= RightToLeft;
1055     if (option.useDesignMetrics())
1056         flags |= DesignMetrics;
1057
1058     // pre-initialize char attributes
1059     if (! attributes())
1060         return;
1061
1062     const int len = length(item);
1063     int num_glyphs = length(item);
1064     const QChar *str = layoutData->string.unicode() + si.position;
1065     ushort upperCased[256];
1066     if (hasCaseChange(si)) {
1067         ushort *uc = upperCased;
1068         if (len > 256)
1069             uc = new ushort[len];
1070         for (int i = 0; i < len; ++i) {
1071             if(si.analysis.flags == QScriptAnalysis::Lowercase)
1072                 uc[i] = str[i].toLower().unicode();
1073             else
1074                 uc[i] = str[i].toUpper().unicode();
1075         }
1076         str = reinterpret_cast<const QChar *>(uc);
1077     }
1078
1079     while (true) {
1080         if (! ensureSpace(num_glyphs)) {
1081             // If str is converted to uppercase/lowercase form with a new buffer,
1082             // we need to delete that buffer before return for error
1083             const ushort *uc = reinterpret_cast<const ushort *>(str);
1084             if (hasCaseChange(si) && uc != upperCased)
1085                 delete [] uc;
1086             return;
1087         }
1088         num_glyphs = layoutData->glyphLayout.numGlyphs - layoutData->used;
1089
1090         QGlyphLayout g = availableGlyphs(&si);
1091         unsigned short *log_clusters = logClusters(&si);
1092
1093         if (fe->stringToCMap(str,
1094                              len,
1095                              &g,
1096                              &num_glyphs,
1097                              flags)) {
1098             heuristicSetGlyphAttributes(str, len, &g, log_clusters, num_glyphs);
1099                     break;
1100         }
1101     }
1102
1103     si.num_glyphs = num_glyphs;
1104
1105     layoutData->used += si.num_glyphs;
1106
1107     const ushort *uc = reinterpret_cast<const ushort *>(str);
1108     if (hasCaseChange(si) && uc != upperCased)
1109         delete [] uc;
1110 }
1111 #endif
1112
1113 static inline void moveGlyphData(const QGlyphLayout &destination, const QGlyphLayout &source, int num)
1114 {
1115     if (num > 0 && destination.glyphs != source.glyphs) {
1116         memmove(destination.glyphs, source.glyphs, num * sizeof(HB_Glyph));
1117         memmove(destination.attributes, source.attributes, num * sizeof(HB_GlyphAttributes));
1118         memmove(destination.advances_x, source.advances_x, num * sizeof(HB_Fixed));
1119         memmove(destination.offsets, source.offsets, num * sizeof(HB_FixedPoint));
1120     }
1121 }
1122
1123 /// take the item from layoutData->items and
1124 void QTextEngine::shapeTextWithHarfbuzz(int item) const
1125 {
1126     Q_ASSERT(sizeof(HB_Fixed) == sizeof(QFixed));
1127     Q_ASSERT(sizeof(HB_FixedPoint) == sizeof(QFixedPoint));
1128
1129     QScriptItem &si = layoutData->items[item];
1130
1131     si.glyph_data_offset = layoutData->used;
1132
1133     QFontEngine *font = fontEngine(si, &si.ascent, &si.descent, &si.leading);
1134
1135     bool kerningEnabled = this->font(si).d->kerning;
1136
1137     HB_ShaperItem entire_shaper_item;
1138     qMemSet(&entire_shaper_item, 0, sizeof(entire_shaper_item));
1139     entire_shaper_item.string = reinterpret_cast<const HB_UChar16 *>(layoutData->string.constData());
1140     entire_shaper_item.stringLength = layoutData->string.length();
1141     entire_shaper_item.item.script = (HB_Script)si.analysis.script;
1142     entire_shaper_item.item.pos = si.position;
1143     entire_shaper_item.item.length = length(item);
1144     entire_shaper_item.item.bidiLevel = si.analysis.bidiLevel;
1145
1146     HB_UChar16 upperCased[256]; // XXX what about making this 4096, so we don't have to extend it ever.
1147     if (hasCaseChange(si)) {
1148         HB_UChar16 *uc = upperCased;
1149         if (entire_shaper_item.item.length > 256)
1150             uc = new HB_UChar16[entire_shaper_item.item.length];
1151         for (uint i = 0; i < entire_shaper_item.item.length; ++i) {
1152             if(si.analysis.flags == QScriptAnalysis::Lowercase)
1153                 uc[i] = QChar::toLower(entire_shaper_item.string[si.position + i]);
1154             else
1155                 uc[i] = QChar::toUpper(entire_shaper_item.string[si.position + i]);
1156         }
1157         entire_shaper_item.item.pos = 0;
1158         entire_shaper_item.string = uc;
1159         entire_shaper_item.stringLength = entire_shaper_item.item.length;
1160     }
1161
1162     entire_shaper_item.shaperFlags = 0;
1163     if (!kerningEnabled)
1164         entire_shaper_item.shaperFlags |= HB_ShaperFlag_NoKerning;
1165     if (option.useDesignMetrics())
1166         entire_shaper_item.shaperFlags |= HB_ShaperFlag_UseDesignMetrics;
1167
1168     entire_shaper_item.num_glyphs = qMax(layoutData->glyphLayout.numGlyphs - layoutData->used, int(entire_shaper_item.item.length));
1169     if (! ensureSpace(entire_shaper_item.num_glyphs)) {
1170         if (hasCaseChange(si))
1171             delete [] const_cast<HB_UChar16 *>(entire_shaper_item.string);
1172         return;
1173     }
1174     QGlyphLayout initialGlyphs = availableGlyphs(&si).mid(0, entire_shaper_item.num_glyphs);
1175
1176     if (!stringToGlyphs(&entire_shaper_item, &initialGlyphs, font)) {
1177         if (! ensureSpace(entire_shaper_item.num_glyphs)) {
1178             if (hasCaseChange(si))
1179                 delete [] const_cast<HB_UChar16 *>(entire_shaper_item.string);
1180             return;
1181         }
1182         initialGlyphs = availableGlyphs(&si).mid(0, entire_shaper_item.num_glyphs);
1183
1184         if (!stringToGlyphs(&entire_shaper_item, &initialGlyphs, font)) {
1185             // ############ if this happens there's a bug in the fontengine
1186             if (hasCaseChange(si) && entire_shaper_item.string != upperCased)
1187                 delete [] const_cast<HB_UChar16 *>(entire_shaper_item.string);
1188             return;
1189         }
1190     }
1191
1192     // split up the item into parts that come from different font engines.
1193     QVarLengthArray<int> itemBoundaries(2);
1194     // k * 2 entries, array[k] == index in string, array[k + 1] == index in glyphs
1195     itemBoundaries[0] = entire_shaper_item.item.pos;
1196     itemBoundaries[1] = 0;
1197
1198     if (font->type() == QFontEngine::Multi) {
1199         uint lastEngine = 0;
1200         int charIdx = entire_shaper_item.item.pos;
1201         const int stringEnd = charIdx + entire_shaper_item.item.length;
1202         for (quint32 i = 0; i < entire_shaper_item.num_glyphs; ++i, ++charIdx) {
1203             uint engineIdx = initialGlyphs.glyphs[i] >> 24;
1204             if (engineIdx != lastEngine && i > 0) {
1205                 itemBoundaries.append(charIdx);
1206                 itemBoundaries.append(i);
1207             }
1208             lastEngine = engineIdx;
1209             if (HB_IsHighSurrogate(entire_shaper_item.string[charIdx])
1210                 && charIdx < stringEnd - 1
1211                 && HB_IsLowSurrogate(entire_shaper_item.string[charIdx + 1]))
1212                 ++charIdx;
1213         }
1214     }
1215
1216
1217
1218     int remaining_glyphs = entire_shaper_item.num_glyphs;
1219     int glyph_pos = 0;
1220     // for each item shape using harfbuzz and store the results in our layoutData's glyphs array.
1221     for (int k = 0; k < itemBoundaries.size(); k += 2) { // for the +2, see the comment at the definition of itemBoundaries
1222
1223         HB_ShaperItem shaper_item = entire_shaper_item;
1224
1225         shaper_item.item.pos = itemBoundaries[k];
1226         if (k < itemBoundaries.size() - 3) {
1227             shaper_item.item.length = itemBoundaries[k + 2] - shaper_item.item.pos;
1228             shaper_item.num_glyphs = itemBoundaries[k + 3] - itemBoundaries[k + 1];
1229         } else { // last combo in the list, avoid out of bounds access.
1230             shaper_item.item.length -= shaper_item.item.pos - entire_shaper_item.item.pos;
1231             shaper_item.num_glyphs -= itemBoundaries[k + 1];
1232         }
1233         shaper_item.initialGlyphCount = shaper_item.num_glyphs;
1234         if (shaper_item.num_glyphs < shaper_item.item.length)
1235             shaper_item.num_glyphs = shaper_item.item.length;
1236
1237         QFontEngine *actualFontEngine = font;
1238         uint engineIdx = 0;
1239         if (font->type() == QFontEngine::Multi) {
1240             engineIdx = uint(availableGlyphs(&si).glyphs[glyph_pos] >> 24);
1241
1242             actualFontEngine = static_cast<QFontEngineMulti *>(font)->engine(engineIdx);
1243         }
1244
1245         shaper_item.font = actualFontEngine->harfbuzzFont();
1246         shaper_item.face = actualFontEngine->harfbuzzFace();
1247
1248         shaper_item.glyphIndicesPresent = true;
1249
1250         remaining_glyphs -= shaper_item.initialGlyphCount;
1251
1252         do {
1253             if (! ensureSpace(glyph_pos + shaper_item.num_glyphs + remaining_glyphs)) {
1254                 if (hasCaseChange(si))
1255                     delete [] const_cast<HB_UChar16 *>(entire_shaper_item.string);
1256                 return;
1257             }
1258
1259             const QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos);
1260             if (shaper_item.num_glyphs > shaper_item.item.length)
1261                 moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs);
1262
1263             shaper_item.glyphs = g.glyphs;
1264             shaper_item.attributes = g.attributes;
1265             shaper_item.advances = reinterpret_cast<HB_Fixed *>(g.advances_x);
1266             shaper_item.offsets = reinterpret_cast<HB_FixedPoint *>(g.offsets);
1267
1268             if (shaper_item.glyphIndicesPresent) {
1269                 for (hb_uint32 i = 0; i < shaper_item.initialGlyphCount; ++i)
1270                     shaper_item.glyphs[i] &= 0x00ffffff;
1271             }
1272
1273             shaper_item.log_clusters = logClusters(&si) + shaper_item.item.pos - entire_shaper_item.item.pos;
1274
1275 //          qDebug("    .. num_glyphs=%d, used=%d, item.num_glyphs=%d", num_glyphs, used, shaper_item.num_glyphs);
1276         } while (!qShapeItem(&shaper_item)); // this does the actual shaping via harfbuzz.
1277
1278         QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos, shaper_item.num_glyphs);
1279         moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs);
1280
1281         for (hb_uint32 i = 0; i < shaper_item.num_glyphs; ++i)
1282             g.glyphs[i] = g.glyphs[i] | (engineIdx << 24);
1283
1284         for (hb_uint32 i = 0; i < shaper_item.item.length; ++i)
1285             shaper_item.log_clusters[i] += glyph_pos;
1286
1287         if (kerningEnabled && !shaper_item.kerning_applied)
1288             font->doKerning(&g, option.useDesignMetrics() ? QFlag(QTextEngine::DesignMetrics) : QFlag(0));
1289
1290         glyph_pos += shaper_item.num_glyphs;
1291     }
1292
1293 //     qDebug("    -> item: script=%d num_glyphs=%d", shaper_item.script, shaper_item.num_glyphs);
1294     si.num_glyphs = glyph_pos;
1295
1296     layoutData->used += si.num_glyphs;
1297
1298     if (hasCaseChange(si) && entire_shaper_item.string != upperCased)
1299         delete [] const_cast<HB_UChar16 *>(entire_shaper_item.string);
1300 }
1301
1302 static void init(QTextEngine *e)
1303 {
1304     e->ignoreBidi = false;
1305     e->cacheGlyphs = false;
1306     e->forceJustification = false;
1307     e->visualMovement = false;
1308
1309     e->layoutData = 0;
1310
1311     e->minWidth = 0;
1312     e->maxWidth = 0;
1313
1314     e->underlinePositions = 0;
1315     e->specialData = 0;
1316     e->stackEngine = false;
1317 }
1318
1319 QTextEngine::QTextEngine()
1320 {
1321     init(this);
1322 }
1323
1324 QTextEngine::QTextEngine(const QString &str, const QFont &f)
1325     : text(str),
1326       fnt(f)
1327 {
1328     init(this);
1329 }
1330
1331 QTextEngine::~QTextEngine()
1332 {
1333     if (!stackEngine)
1334         delete layoutData;
1335     delete specialData;
1336 }
1337
1338 const HB_CharAttributes *QTextEngine::attributes() const
1339 {
1340     if (layoutData && layoutData->haveCharAttributes)
1341         return (HB_CharAttributes *) layoutData->memory;
1342
1343     itemize();
1344     if (! ensureSpace(layoutData->string.length()))
1345         return NULL;
1346
1347     QVarLengthArray<HB_ScriptItem> hbScriptItems(layoutData->items.size());
1348
1349     for (int i = 0; i < layoutData->items.size(); ++i) {
1350         const QScriptItem &si = layoutData->items[i];
1351         hbScriptItems[i].pos = si.position;
1352         hbScriptItems[i].length = length(i);
1353         hbScriptItems[i].bidiLevel = si.analysis.bidiLevel;
1354         hbScriptItems[i].script = (HB_Script)si.analysis.script;
1355     }
1356
1357     qGetCharAttributes(reinterpret_cast<const HB_UChar16 *>(layoutData->string.constData()),
1358                        layoutData->string.length(),
1359                        hbScriptItems.data(), hbScriptItems.size(),
1360                        (HB_CharAttributes *)layoutData->memory);
1361
1362
1363     layoutData->haveCharAttributes = true;
1364     return (HB_CharAttributes *) layoutData->memory;
1365 }
1366
1367 void QTextEngine::shape(int item) const
1368 {
1369     if (layoutData->items[item].analysis.flags == QScriptAnalysis::Object) {
1370         ensureSpace(1);
1371         if (block.docHandle()) {
1372             QTextFormat format = formats()->format(formatIndex(&layoutData->items[item]));
1373             docLayout()->resizeInlineObject(QTextInlineObject(item, const_cast<QTextEngine *>(this)),
1374                                             layoutData->items[item].position + block.position(), format);
1375         }
1376     } else if (layoutData->items[item].analysis.flags == QScriptAnalysis::Tab) {
1377         // set up at least the ascent/descent/leading of the script item for the tab
1378         fontEngine(layoutData->items[item],
1379                    &layoutData->items[item].ascent,
1380                    &layoutData->items[item].descent,
1381                    &layoutData->items[item].leading);
1382     } else {
1383         shapeText(item);
1384     }
1385 }
1386
1387 static inline void releaseCachedFontEngine(QFontEngine *fontEngine)
1388 {
1389     if (fontEngine) {
1390         fontEngine->ref.deref();
1391         if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
1392             delete fontEngine;
1393     }
1394 }
1395
1396 void QTextEngine::invalidate()
1397 {
1398     freeMemory();
1399     minWidth = 0;
1400     maxWidth = 0;
1401     if (specialData)
1402         specialData->resolvedFormatIndices.clear();
1403
1404     releaseCachedFontEngine(feCache.prevFontEngine);
1405     releaseCachedFontEngine(feCache.prevScaledFontEngine);
1406     feCache.reset();
1407 }
1408
1409 void QTextEngine::clearLineData()
1410 {
1411     lines.clear();
1412 }
1413
1414 void QTextEngine::validate() const
1415 {
1416     if (layoutData)
1417         return;
1418     layoutData = new LayoutData();
1419     if (block.docHandle()) {
1420         layoutData->string = block.text();
1421         if (option.flags() & QTextOption::ShowLineAndParagraphSeparators)
1422             layoutData->string += QLatin1Char(block.next().isValid() ? 0xb6 : 0x20);
1423     } else {
1424         layoutData->string = text;
1425     }
1426     if (specialData && specialData->preeditPosition != -1)
1427         layoutData->string.insert(specialData->preeditPosition, specialData->preeditText);
1428 }
1429
1430 void QTextEngine::itemize() const
1431 {
1432     validate();
1433     if (layoutData->items.size())
1434         return;
1435
1436     int length = layoutData->string.length();
1437     if (!length)
1438         return;
1439 #if defined(Q_WS_MAC) && !defined(QT_MAC_USE_COCOA)
1440     // ATSUI requires RTL flags to correctly identify the character stops.
1441     bool ignore = false;
1442 #else
1443     bool ignore = ignoreBidi;
1444 #endif
1445
1446     bool rtl = isRightToLeft();
1447
1448     if (!ignore && !rtl) {
1449         ignore = true;
1450         const QChar *start = layoutData->string.unicode();
1451         const QChar * const end = start + length;
1452         while (start < end) {
1453             if (start->unicode() >= 0x590) {
1454                 ignore = false;
1455                 break;
1456             }
1457             ++start;
1458         }
1459     }
1460
1461     QVarLengthArray<QScriptAnalysis, 4096> scriptAnalysis(length);
1462     QScriptAnalysis *analysis = scriptAnalysis.data();
1463
1464     QBidiControl control(rtl);
1465
1466     if (ignore) {
1467         memset(analysis, 0, length*sizeof(QScriptAnalysis));
1468         if (option.textDirection() == Qt::RightToLeft) {
1469             for (int i = 0; i < length; ++i)
1470                 analysis[i].bidiLevel = 1;
1471             layoutData->hasBidi = true;
1472         }
1473     } else {
1474         layoutData->hasBidi = bidiItemize(const_cast<QTextEngine *>(this), analysis, control);
1475     }
1476
1477     const ushort *uc = reinterpret_cast<const ushort *>(layoutData->string.unicode());
1478     const ushort *e = uc + length;
1479     int lastScript = QUnicodeTables::Common;
1480     while (uc < e) {
1481         int script = QUnicodeTables::script(*uc);
1482         if (script == QUnicodeTables::Inherited)
1483             script = lastScript;
1484         analysis->flags = QScriptAnalysis::None;
1485         if (*uc == QChar::ObjectReplacementCharacter) {
1486             if (analysis->bidiLevel % 2)
1487                 --analysis->bidiLevel;
1488             analysis->script = QUnicodeTables::Common;
1489             analysis->flags = QScriptAnalysis::Object;
1490         } else if (*uc == QChar::LineSeparator) {
1491             if (analysis->bidiLevel % 2)
1492                 --analysis->bidiLevel;
1493             analysis->script = QUnicodeTables::Common;
1494             analysis->flags = QScriptAnalysis::LineOrParagraphSeparator;
1495             if (option.flags() & QTextOption::ShowLineAndParagraphSeparators)
1496                 *const_cast<ushort*>(uc) = 0x21B5; // visual line separator
1497         } else if (*uc == 9) {
1498             analysis->script = QUnicodeTables::Common;
1499             analysis->flags = QScriptAnalysis::Tab;
1500             analysis->bidiLevel = control.baseLevel();
1501         } else if ((*uc == 32 || *uc == QChar::Nbsp)
1502                    && (option.flags() & QTextOption::ShowTabsAndSpaces)) {
1503             analysis->script = QUnicodeTables::Common;
1504             analysis->flags = QScriptAnalysis::Space;
1505             analysis->bidiLevel = control.baseLevel();
1506         } else {
1507             analysis->script = script;
1508         }
1509         lastScript = analysis->script;
1510         ++uc;
1511         ++analysis;
1512     }
1513     if (option.flags() & QTextOption::ShowLineAndParagraphSeparators) {
1514         (analysis-1)->flags = QScriptAnalysis::LineOrParagraphSeparator; // to exclude it from width
1515     }
1516
1517     Itemizer itemizer(layoutData->string, scriptAnalysis.data(), layoutData->items);
1518
1519     const QTextDocumentPrivate *p = block.docHandle();
1520     if (p) {
1521         SpecialData *s = specialData;
1522
1523         QTextDocumentPrivate::FragmentIterator it = p->find(block.position());
1524         QTextDocumentPrivate::FragmentIterator end = p->find(block.position() + block.length() - 1); // -1 to omit the block separator char
1525         int format = it.value()->format;
1526
1527         int prevPosition = 0;
1528         int position = prevPosition;
1529         while (1) {
1530             const QTextFragmentData * const frag = it.value();
1531             if (it == end || format != frag->format) {
1532                 if (s && position >= s->preeditPosition) {
1533                     position += s->preeditText.length();
1534                     s = 0;
1535                 }
1536                 Q_ASSERT(position <= length);
1537                 itemizer.generate(prevPosition, position - prevPosition,
1538                     formats()->charFormat(format).fontCapitalization());
1539                 if (it == end) {
1540                     if (position < length)
1541                         itemizer.generate(position, length - position,
1542                                           formats()->charFormat(format).fontCapitalization());
1543                     break;
1544                 }
1545                 format = frag->format;
1546                 prevPosition = position;
1547             }
1548             position += frag->size_array[0];
1549             ++it;
1550         }
1551     } else {
1552         itemizer.generate(0, length, static_cast<QFont::Capitalization> (fnt.d->capital));
1553     }
1554
1555     addRequiredBoundaries();
1556     resolveAdditionalFormats();
1557 }
1558
1559 bool QTextEngine::isRightToLeft() const
1560 {
1561     switch (option.textDirection()) {
1562     case Qt::LeftToRight:
1563         return false;
1564     case Qt::RightToLeft:
1565         return true;
1566     default:
1567         break;
1568     }
1569     // this places the cursor in the right position depending on the keyboard layout
1570     if (layoutData->string.isEmpty())
1571         return QApplication::keyboardInputDirection() == Qt::RightToLeft;
1572     return layoutData->string.isRightToLeft();
1573 }
1574
1575
1576 int QTextEngine::findItem(int strPos) const
1577 {
1578     itemize();
1579     int left = 0;
1580     int right = layoutData->items.size()-1;
1581     while(left <= right) {
1582         int middle = ((right-left)/2)+left;
1583         if (strPos > layoutData->items[middle].position)
1584             left = middle+1;
1585         else if(strPos < layoutData->items[middle].position)
1586             right = middle-1;
1587         else {
1588             return middle;
1589         }
1590     }
1591     return right;
1592 }
1593
1594 QFixed QTextEngine::width(int from, int len) const
1595 {
1596     itemize();
1597
1598     QFixed w = 0;
1599
1600 //     qDebug("QTextEngine::width(from = %d, len = %d), numItems=%d, strleng=%d", from,  len, items.size(), string.length());
1601     for (int i = 0; i < layoutData->items.size(); i++) {
1602         const QScriptItem *si = layoutData->items.constData() + i;
1603         int pos = si->position;
1604         int ilen = length(i);
1605 //          qDebug("item %d: from %d len %d", i, pos, ilen);
1606         if (pos >= from + len)
1607             break;
1608         if (pos + ilen > from) {
1609             if (!si->num_glyphs)
1610                 shape(i);
1611
1612             if (si->analysis.flags == QScriptAnalysis::Object) {
1613                 w += si->width;
1614                 continue;
1615             } else if (si->analysis.flags == QScriptAnalysis::Tab) {
1616                 w += calculateTabWidth(i, w);
1617                 continue;
1618             }
1619
1620
1621             QGlyphLayout glyphs = shapedGlyphs(si);
1622             unsigned short *logClusters = this->logClusters(si);
1623
1624 //             fprintf(stderr, "  logclusters:");
1625 //             for (int k = 0; k < ilen; k++)
1626 //                 fprintf(stderr, " %d", logClusters[k]);
1627 //             fprintf(stderr, "\n");
1628             // do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
1629             int charFrom = from - pos;
1630             if (charFrom < 0)
1631                 charFrom = 0;
1632             int glyphStart = logClusters[charFrom];
1633             if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
1634                 while (charFrom < ilen && logClusters[charFrom] == glyphStart)
1635                     charFrom++;
1636             if (charFrom < ilen) {
1637                 glyphStart = logClusters[charFrom];
1638                 int charEnd = from + len - 1 - pos;
1639                 if (charEnd >= ilen)
1640                     charEnd = ilen-1;
1641                 int glyphEnd = logClusters[charEnd];
1642                 while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
1643                     charEnd++;
1644                 glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
1645
1646 //                 qDebug("char: start=%d end=%d / glyph: start = %d, end = %d", charFrom, charEnd, glyphStart, glyphEnd);
1647                 for (int i = glyphStart; i < glyphEnd; i++)
1648                     w += glyphs.advances_x[i] * !glyphs.attributes[i].dontPrint;
1649             }
1650         }
1651     }
1652 //     qDebug("   --> w= %d ", w);
1653     return w;
1654 }
1655
1656 glyph_metrics_t QTextEngine::boundingBox(int from,  int len) const
1657 {
1658     itemize();
1659
1660     glyph_metrics_t gm;
1661
1662     for (int i = 0; i < layoutData->items.size(); i++) {
1663         const QScriptItem *si = layoutData->items.constData() + i;
1664
1665         int pos = si->position;
1666         int ilen = length(i);
1667         if (pos > from + len)
1668             break;
1669         if (pos + ilen > from) {
1670             if (!si->num_glyphs)
1671                 shape(i);
1672
1673             if (si->analysis.flags == QScriptAnalysis::Object) {
1674                 gm.width += si->width;
1675                 continue;
1676             } else if (si->analysis.flags == QScriptAnalysis::Tab) {
1677                 gm.width += calculateTabWidth(i, gm.width);
1678                 continue;
1679             }
1680
1681             unsigned short *logClusters = this->logClusters(si);
1682             QGlyphLayout glyphs = shapedGlyphs(si);
1683
1684             // do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
1685             int charFrom = from - pos;
1686             if (charFrom < 0)
1687                 charFrom = 0;
1688             int glyphStart = logClusters[charFrom];
1689             if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
1690                 while (charFrom < ilen && logClusters[charFrom] == glyphStart)
1691                     charFrom++;
1692             if (charFrom < ilen) {
1693                 QFontEngine *fe = fontEngine(*si);
1694                 glyphStart = logClusters[charFrom];
1695                 int charEnd = from + len - 1 - pos;
1696                 if (charEnd >= ilen)
1697                     charEnd = ilen-1;
1698                 int glyphEnd = logClusters[charEnd];
1699                 while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
1700                     charEnd++;
1701                 glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
1702                 if (glyphStart <= glyphEnd ) {
1703                     glyph_metrics_t m = fe->boundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart));
1704                     gm.x = qMin(gm.x, m.x + gm.xoff);
1705                     gm.y = qMin(gm.y, m.y + gm.yoff);
1706                     gm.width = qMax(gm.width, m.width+gm.xoff);
1707                     gm.height = qMax(gm.height, m.height+gm.yoff);
1708                     gm.xoff += m.xoff;
1709                     gm.yoff += m.yoff;
1710                 }
1711             }
1712         }
1713     }
1714     return gm;
1715 }
1716
1717 glyph_metrics_t QTextEngine::tightBoundingBox(int from,  int len) const
1718 {
1719     itemize();
1720
1721     glyph_metrics_t gm;
1722
1723     for (int i = 0; i < layoutData->items.size(); i++) {
1724         const QScriptItem *si = layoutData->items.constData() + i;
1725         int pos = si->position;
1726         int ilen = length(i);
1727         if (pos > from + len)
1728             break;
1729         if (pos + len > from) {
1730             if (!si->num_glyphs)
1731                 shape(i);
1732             unsigned short *logClusters = this->logClusters(si);
1733             QGlyphLayout glyphs = shapedGlyphs(si);
1734
1735             // do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
1736             int charFrom = from - pos;
1737             if (charFrom < 0)
1738                 charFrom = 0;
1739             int glyphStart = logClusters[charFrom];
1740             if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
1741                 while (charFrom < ilen && logClusters[charFrom] == glyphStart)
1742                     charFrom++;
1743             if (charFrom < ilen) {
1744                 glyphStart = logClusters[charFrom];
1745                 int charEnd = from + len - 1 - pos;
1746                 if (charEnd >= ilen)
1747                     charEnd = ilen-1;
1748                 int glyphEnd = logClusters[charEnd];
1749                 while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
1750                     charEnd++;
1751                 glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
1752                 if (glyphStart <= glyphEnd ) {
1753                     QFontEngine *fe = fontEngine(*si);
1754                     glyph_metrics_t m = fe->tightBoundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart));
1755                     gm.x = qMin(gm.x, m.x + gm.xoff);
1756                     gm.y = qMin(gm.y, m.y + gm.yoff);
1757                     gm.width = qMax(gm.width, m.width+gm.xoff);
1758                     gm.height = qMax(gm.height, m.height+gm.yoff);
1759                     gm.xoff += m.xoff;
1760                     gm.yoff += m.yoff;
1761                 }
1762             }
1763         }
1764     }
1765     return gm;
1766 }
1767
1768 QFont QTextEngine::font(const QScriptItem &si) const
1769 {
1770     QFont font = fnt;
1771     if (hasFormats()) {
1772         QTextCharFormat f = format(&si);
1773         font = f.font();
1774
1775         if (block.docHandle() && block.docHandle()->layout()) {
1776             // Make sure we get the right dpi on printers
1777             QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
1778             if (pdev)
1779                 font = QFont(font, pdev);
1780         } else {
1781             font = font.resolve(fnt);
1782         }
1783         QTextCharFormat::VerticalAlignment valign = f.verticalAlignment();
1784         if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) {
1785             if (font.pointSize() != -1)
1786                 font.setPointSize((font.pointSize() * 2) / 3);
1787             else
1788                 font.setPixelSize((font.pixelSize() * 2) / 3);
1789         }
1790     }
1791
1792     if (si.analysis.flags == QScriptAnalysis::SmallCaps)
1793         font = font.d->smallCapsFont();
1794
1795     return font;
1796 }
1797
1798 QTextEngine::FontEngineCache::FontEngineCache()
1799 {
1800     reset();
1801 }
1802
1803 //we cache the previous results of this function, as calling it numerous times with the same effective
1804 //input is common (and hard to cache at a higher level)
1805 QFontEngine *QTextEngine::fontEngine(const QScriptItem &si, QFixed *ascent, QFixed *descent, QFixed *leading) const
1806 {
1807     QFontEngine *engine = 0;
1808     QFontEngine *scaledEngine = 0;
1809     int script = si.analysis.script;
1810
1811     QFont font = fnt;
1812     if (hasFormats()) {
1813         if (feCache.prevFontEngine && feCache.prevPosition == si.position && feCache.prevLength == length(&si) && feCache.prevScript == script) {
1814             engine = feCache.prevFontEngine;
1815             scaledEngine = feCache.prevScaledFontEngine;
1816         } else {
1817             QTextCharFormat f = format(&si);
1818             font = f.font();
1819
1820             if (block.docHandle() && block.docHandle()->layout()) {
1821                 // Make sure we get the right dpi on printers
1822                 QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
1823                 if (pdev)
1824                     font = QFont(font, pdev);
1825             } else {
1826                 font = font.resolve(fnt);
1827             }
1828             engine = font.d->engineForScript(script);
1829             QTextCharFormat::VerticalAlignment valign = f.verticalAlignment();
1830             if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) {
1831                 if (font.pointSize() != -1)
1832                     font.setPointSize((font.pointSize() * 2) / 3);
1833                 else
1834                     font.setPixelSize((font.pixelSize() * 2) / 3);
1835                 scaledEngine = font.d->engineForScript(script);
1836             }
1837             feCache.prevFontEngine = engine;
1838             if (engine)
1839                 engine->ref.ref();
1840             feCache.prevScaledFontEngine = scaledEngine;
1841             if (scaledEngine)
1842                 scaledEngine->ref.ref();
1843             feCache.prevScript = script;
1844             feCache.prevPosition = si.position;
1845             feCache.prevLength = length(&si);
1846         }
1847     } else {
1848         if (feCache.prevFontEngine && feCache.prevScript == script && feCache.prevPosition == -1)
1849             engine = feCache.prevFontEngine;
1850         else {
1851             engine = font.d->engineForScript(script);
1852             feCache.prevFontEngine = engine;
1853             if (engine)
1854                 engine->ref.ref();
1855             feCache.prevScript = script;
1856             feCache.prevPosition = -1;
1857             feCache.prevLength = -1;
1858             feCache.prevScaledFontEngine = 0;
1859         }
1860     }
1861
1862     if (si.analysis.flags == QScriptAnalysis::SmallCaps) {
1863         QFontPrivate *p = font.d->smallCapsFontPrivate();
1864         scaledEngine = p->engineForScript(script);
1865     }
1866
1867     if (ascent) {
1868         *ascent = engine->ascent();
1869         *descent = engine->descent();
1870         *leading = engine->leading();
1871     }
1872
1873     if (scaledEngine)
1874         return scaledEngine;
1875     return engine;
1876 }
1877
1878 struct QJustificationPoint {
1879     int type;
1880     QFixed kashidaWidth;
1881     QGlyphLayout glyph;
1882     QFontEngine *fontEngine;
1883 };
1884
1885 Q_DECLARE_TYPEINFO(QJustificationPoint, Q_PRIMITIVE_TYPE);
1886
1887 static void set(QJustificationPoint *point, int type, const QGlyphLayout &glyph, QFontEngine *fe)
1888 {
1889     point->type = type;
1890     point->glyph = glyph;
1891     point->fontEngine = fe;
1892
1893     if (type >= HB_Arabic_Normal) {
1894         QChar ch(0x640); // Kashida character
1895         QGlyphLayoutArray<8> glyphs;
1896         int nglyphs = 7;
1897         fe->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
1898         if (glyphs.glyphs[0] && glyphs.advances_x[0] != 0) {
1899             point->kashidaWidth = glyphs.advances_x[0];
1900         } else {
1901             point->type = HB_NoJustification;
1902             point->kashidaWidth = 0;
1903         }
1904     }
1905 }
1906
1907
1908 void QTextEngine::justify(const QScriptLine &line)
1909 {
1910 //     qDebug("justify: line.gridfitted = %d, line.justified=%d", line.gridfitted, line.justified);
1911     if (line.gridfitted && line.justified)
1912         return;
1913
1914     if (!line.gridfitted) {
1915         // redo layout in device metrics, then adjust
1916         const_cast<QScriptLine &>(line).gridfitted = true;
1917     }
1918
1919     if ((option.alignment() & Qt::AlignHorizontal_Mask) != Qt::AlignJustify)
1920         return;
1921
1922     itemize();
1923
1924     if (!forceJustification) {
1925         int end = line.from + (int)line.length;
1926         if (end == layoutData->string.length())
1927             return; // no justification at end of paragraph
1928         if (end && layoutData->items[findItem(end-1)].analysis.flags == QScriptAnalysis::LineOrParagraphSeparator)
1929             return; // no justification at the end of an explicitly separated line
1930     }
1931
1932     // justify line
1933     int maxJustify = 0;
1934
1935     // don't include trailing white spaces when doing justification
1936     int line_length = line.length;
1937     const HB_CharAttributes *a = attributes();
1938     if (! a)
1939         return;
1940     a += line.from;
1941     while (line_length && a[line_length-1].whiteSpace)
1942         --line_length;
1943     // subtract one char more, as we can't justfy after the last character
1944     --line_length;
1945
1946     if (!line_length)
1947         return;
1948
1949     int firstItem = findItem(line.from);
1950     int nItems = findItem(line.from + line_length - 1) - firstItem + 1;
1951
1952     QVarLengthArray<QJustificationPoint> justificationPoints;
1953     int nPoints = 0;
1954 //     qDebug("justifying from %d len %d, firstItem=%d, nItems=%d (%s)", line.from, line_length, firstItem, nItems, layoutData->string.mid(line.from, line_length).toUtf8().constData());
1955     QFixed minKashida = 0x100000;
1956
1957     // we need to do all shaping before we go into the next loop, as we there
1958     // store pointers to the glyph data that could get reallocated by the shaping
1959     // process.
1960     for (int i = 0; i < nItems; ++i) {
1961         QScriptItem &si = layoutData->items[firstItem + i];
1962         if (!si.num_glyphs)
1963             shape(firstItem + i);
1964     }
1965
1966     for (int i = 0; i < nItems; ++i) {
1967         QScriptItem &si = layoutData->items[firstItem + i];
1968
1969         int kashida_type = HB_Arabic_Normal;
1970         int kashida_pos = -1;
1971
1972         int start = qMax(line.from - si.position, 0);
1973         int end = qMin(line.from + line_length - (int)si.position, length(firstItem+i));
1974
1975         unsigned short *log_clusters = logClusters(&si);
1976
1977         int gs = log_clusters[start];
1978         int ge = (end == length(firstItem+i) ? si.num_glyphs : log_clusters[end]);
1979
1980         const QGlyphLayout g = shapedGlyphs(&si);
1981
1982         for (int i = gs; i < ge; ++i) {
1983             g.justifications[i].type = QGlyphJustification::JustifyNone;
1984             g.justifications[i].nKashidas = 0;
1985             g.justifications[i].space_18d6 = 0;
1986
1987             justificationPoints.resize(nPoints+3);
1988             int justification = g.attributes[i].justification;
1989
1990             switch(justification) {
1991             case HB_NoJustification:
1992                 break;
1993             case HB_Space          :
1994                 // fall through
1995             case HB_Arabic_Space   :
1996                 if (kashida_pos >= 0) {
1997 //                     qDebug("kashida position at %d in word", kashida_pos);
1998                     set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si));
1999                     if (justificationPoints[nPoints].kashidaWidth > 0) {
2000                         minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth);
2001                         maxJustify = qMax(maxJustify, justificationPoints[nPoints].type);
2002                         ++nPoints;
2003                     }
2004                 }
2005                 kashida_pos = -1;
2006                 kashida_type = HB_Arabic_Normal;
2007                 // fall through
2008             case HB_Character      :
2009                 set(&justificationPoints[nPoints++], justification, g.mid(i), fontEngine(si));
2010                 maxJustify = qMax(maxJustify, justification);
2011                 break;
2012             case HB_Arabic_Normal  :
2013             case HB_Arabic_Waw     :
2014             case HB_Arabic_BaRa    :
2015             case HB_Arabic_Alef    :
2016             case HB_Arabic_HaaDal  :
2017             case HB_Arabic_Seen    :
2018             case HB_Arabic_Kashida :
2019                 if (justification >= kashida_type) {
2020                     kashida_pos = i;
2021                     kashida_type = justification;
2022                 }
2023             }
2024         }
2025         if (kashida_pos >= 0) {
2026             set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si));
2027             if (justificationPoints[nPoints].kashidaWidth > 0) {
2028                 minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth);
2029                 maxJustify = qMax(maxJustify, justificationPoints[nPoints].type);
2030                 ++nPoints;
2031             }
2032         }
2033     }
2034
2035     QFixed need = line.width - line.textWidth;
2036     if (need < 0) {
2037         // line overflows already!
2038         const_cast<QScriptLine &>(line).justified = true;
2039         return;
2040     }
2041
2042 //     qDebug("doing justification: textWidth=%x, requested=%x, maxJustify=%d", line.textWidth.value(), line.width.value(), maxJustify);
2043 //     qDebug("     minKashida=%f, need=%f", minKashida.toReal(), need.toReal());
2044
2045     // distribute in priority order
2046     if (maxJustify >= HB_Arabic_Normal) {
2047         while (need >= minKashida) {
2048             for (int type = maxJustify; need >= minKashida && type >= HB_Arabic_Normal; --type) {
2049                 for (int i = 0; need >= minKashida && i < nPoints; ++i) {
2050                     if (justificationPoints[i].type == type && justificationPoints[i].kashidaWidth <= need) {
2051                         justificationPoints[i].glyph.justifications->nKashidas++;
2052                         // ############
2053                         justificationPoints[i].glyph.justifications->space_18d6 += justificationPoints[i].kashidaWidth.value();
2054                         need -= justificationPoints[i].kashidaWidth;
2055 //                         qDebug("adding kashida type %d with width %x, neednow %x", type, justificationPoints[i].kashidaWidth, need.value());
2056                     }
2057                 }
2058             }
2059         }
2060     }
2061     Q_ASSERT(need >= 0);
2062     if (!need)
2063         goto end;
2064
2065     maxJustify = qMin(maxJustify, (int)HB_Space);
2066     for (int type = maxJustify; need != 0 && type > 0; --type) {
2067         int n = 0;
2068         for (int i = 0; i < nPoints; ++i) {
2069             if (justificationPoints[i].type == type)
2070                 ++n;
2071         }
2072 //          qDebug("number of points for justification type %d: %d", type, n);
2073
2074
2075         if (!n)
2076             continue;
2077
2078         for (int i = 0; i < nPoints; ++i) {
2079             if (justificationPoints[i].type == type) {
2080                 QFixed add = need/n;
2081 //                  qDebug("adding %x to glyph %x", add.value(), justificationPoints[i].glyph->glyph);
2082                 justificationPoints[i].glyph.justifications[0].space_18d6 = add.value();
2083                 need -= add;
2084                 --n;
2085             }
2086         }
2087
2088         Q_ASSERT(!need);
2089     }
2090  end:
2091     const_cast<QScriptLine &>(line).justified = true;
2092 }
2093
2094 void QScriptLine::setDefaultHeight(QTextEngine *eng)
2095 {
2096     QFont f;
2097     QFontEngine *e;
2098
2099     if (eng->block.docHandle() && eng->block.docHandle()->layout()) {
2100         f = eng->block.charFormat().font();
2101         // Make sure we get the right dpi on printers
2102         QPaintDevice *pdev = eng->block.docHandle()->layout()->paintDevice();
2103         if (pdev)
2104             f = QFont(f, pdev);
2105         e = f.d->engineForScript(QUnicodeTables::Common);
2106     } else {
2107         e = eng->fnt.d->engineForScript(QUnicodeTables::Common);
2108     }
2109
2110     QFixed other_ascent = e->ascent();
2111     QFixed other_descent = e->descent();
2112     QFixed other_leading = e->leading();
2113     leading = qMax(leading + ascent, other_leading + other_ascent) - qMax(ascent, other_ascent);
2114     ascent = qMax(ascent, other_ascent);
2115     descent = qMax(descent, other_descent);
2116 }
2117
2118 QTextEngine::LayoutData::LayoutData()
2119 {
2120     memory = 0;
2121     allocated = 0;
2122     memory_on_stack = false;
2123     used = 0;
2124     hasBidi = false;
2125     layoutState = LayoutEmpty;
2126     haveCharAttributes = false;
2127     logClustersPtr = 0;
2128     available_glyphs = 0;
2129 }
2130
2131 QTextEngine::LayoutData::LayoutData(const QString &str, void **stack_memory, int _allocated)
2132     : string(str)
2133 {
2134     allocated = _allocated;
2135
2136     int space_charAttributes = sizeof(HB_CharAttributes)*string.length()/sizeof(void*) + 1;
2137     int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1;
2138     available_glyphs = ((int)allocated - space_charAttributes - space_logClusters)*(int)sizeof(void*)/(int)QGlyphLayout::spaceNeededForGlyphLayout(1);
2139
2140     if (available_glyphs < str.length()) {
2141         // need to allocate on the heap
2142         allocated = 0;
2143
2144         memory_on_stack = false;
2145         memory = 0;
2146         logClustersPtr = 0;
2147     } else {
2148         memory_on_stack = true;
2149         memory = stack_memory;
2150         logClustersPtr = (unsigned short *)(memory + space_charAttributes);
2151
2152         void *m = memory + space_charAttributes + space_logClusters;
2153         glyphLayout = QGlyphLayout(reinterpret_cast<char *>(m), str.length());
2154         glyphLayout.clear();
2155         memset(memory, 0, space_charAttributes*sizeof(void *));
2156     }
2157     used = 0;
2158     hasBidi = false;
2159     layoutState = LayoutEmpty;
2160     haveCharAttributes = false;
2161 }
2162
2163 QTextEngine::LayoutData::~LayoutData()
2164 {
2165     if (!memory_on_stack)
2166         free(memory);
2167     memory = 0;
2168 }
2169
2170 bool QTextEngine::LayoutData::reallocate(int totalGlyphs)
2171 {
2172     Q_ASSERT(totalGlyphs >= glyphLayout.numGlyphs);
2173     if (memory_on_stack && available_glyphs >= totalGlyphs) {
2174         glyphLayout.grow(glyphLayout.data(), totalGlyphs);
2175         return true;
2176     }
2177
2178     int space_charAttributes = sizeof(HB_CharAttributes)*string.length()/sizeof(void*) + 1;
2179     int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1;
2180     int space_glyphs = QGlyphLayout::spaceNeededForGlyphLayout(totalGlyphs)/sizeof(void*) + 2;
2181
2182     int newAllocated = space_charAttributes + space_glyphs + space_logClusters;
2183     // These values can be negative if the length of string/glyphs causes overflow,
2184     // we can't layout such a long string all at once, so return false here to
2185     // indicate there is a failure
2186     if (space_charAttributes < 0 || space_logClusters < 0 || space_glyphs < 0 || newAllocated < allocated) {
2187         layoutState = LayoutFailed;
2188         return false;
2189     }
2190
2191     void **newMem = memory;
2192     newMem = (void **)::realloc(memory_on_stack ? 0 : memory, newAllocated*sizeof(void *));
2193     if (!newMem) {
2194         layoutState = LayoutFailed;
2195         return false;
2196     }
2197     if (memory_on_stack)
2198         memcpy(newMem, memory, allocated*sizeof(void *));
2199     memory = newMem;
2200     memory_on_stack = false;
2201
2202     void **m = memory;
2203     m += space_charAttributes;
2204     logClustersPtr = (unsigned short *) m;
2205     m += space_logClusters;
2206
2207     const int space_preGlyphLayout = space_charAttributes + space_logClusters;
2208     if (allocated < space_preGlyphLayout)
2209         memset(memory + allocated, 0, (space_preGlyphLayout - allocated)*sizeof(void *));
2210
2211     glyphLayout.grow(reinterpret_cast<char *>(m), totalGlyphs);
2212
2213     allocated = newAllocated;
2214     return true;
2215 }
2216
2217 // grow to the new size, copying the existing data to the new layout
2218 void QGlyphLayout::grow(char *address, int totalGlyphs)
2219 {
2220     QGlyphLayout oldLayout(address, numGlyphs);
2221     QGlyphLayout newLayout(address, totalGlyphs);
2222
2223     if (numGlyphs) {
2224         // move the existing data
2225         memmove(newLayout.attributes, oldLayout.attributes, numGlyphs * sizeof(HB_GlyphAttributes));
2226         memmove(newLayout.justifications, oldLayout.justifications, numGlyphs * sizeof(QGlyphJustification));
2227         memmove(newLayout.advances_y, oldLayout.advances_y, numGlyphs * sizeof(QFixed));
2228         memmove(newLayout.advances_x, oldLayout.advances_x, numGlyphs * sizeof(QFixed));
2229         memmove(newLayout.glyphs, oldLayout.glyphs, numGlyphs * sizeof(HB_Glyph));
2230     }
2231
2232     // clear the new data
2233     newLayout.clear(numGlyphs);
2234
2235     *this = newLayout;
2236 }
2237
2238 void QTextEngine::freeMemory()
2239 {
2240     if (!stackEngine) {
2241         delete layoutData;
2242         layoutData = 0;
2243     } else {
2244         layoutData->used = 0;
2245         layoutData->hasBidi = false;
2246         layoutData->layoutState = LayoutEmpty;
2247         layoutData->haveCharAttributes = false;
2248     }
2249     for (int i = 0; i < lines.size(); ++i) {
2250         lines[i].justified = 0;
2251         lines[i].gridfitted = 0;
2252     }
2253 }
2254
2255 int QTextEngine::formatIndex(const QScriptItem *si) const
2256 {
2257     if (specialData && !specialData->resolvedFormatIndices.isEmpty())
2258         return specialData->resolvedFormatIndices.at(si - &layoutData->items[0]);
2259     QTextDocumentPrivate *p = block.docHandle();
2260     if (!p)
2261         return -1;
2262     int pos = si->position;
2263     if (specialData && si->position >= specialData->preeditPosition) {
2264         if (si->position < specialData->preeditPosition + specialData->preeditText.length())
2265             pos = qMax(specialData->preeditPosition - 1, 0);
2266         else
2267             pos -= specialData->preeditText.length();
2268     }
2269     QTextDocumentPrivate::FragmentIterator it = p->find(block.position() + pos);
2270     return it.value()->format;
2271 }
2272
2273
2274 QTextCharFormat QTextEngine::format(const QScriptItem *si) const
2275 {
2276     QTextCharFormat format;
2277     const QTextFormatCollection *formats = 0;
2278     if (block.docHandle()) {
2279         formats = this->formats();
2280         format = formats->charFormat(formatIndex(si));
2281     }
2282     if (specialData && specialData->resolvedFormatIndices.isEmpty()) {
2283         int end = si->position + length(si);
2284         for (int i = 0; i < specialData->addFormats.size(); ++i) {
2285             const QTextLayout::FormatRange &r = specialData->addFormats.at(i);
2286             if (r.start <= si->position && r.start + r.length >= end) {
2287                 if (!specialData->addFormatIndices.isEmpty())
2288                     format.merge(formats->format(specialData->addFormatIndices.at(i)));
2289                 else
2290                     format.merge(r.format);
2291             }
2292         }
2293     }
2294     return format;
2295 }
2296
2297 void QTextEngine::addRequiredBoundaries() const
2298 {
2299     if (specialData) {
2300         for (int i = 0; i < specialData->addFormats.size(); ++i) {
2301             const QTextLayout::FormatRange &r = specialData->addFormats.at(i);
2302             setBoundary(r.start);
2303             setBoundary(r.start + r.length);
2304             //qDebug("adding boundaries %d %d", r.start, r.start+r.length);
2305         }
2306     }
2307 }
2308
2309 bool QTextEngine::atWordSeparator(int position) const
2310 {
2311     const QChar c = layoutData->string.at(position);
2312     switch (c.toLatin1()) {
2313     case '.':
2314     case ',':
2315     case '?':
2316     case '!':
2317     case '@':
2318     case '#':
2319     case '$':
2320     case ':':
2321     case ';':
2322     case '-':
2323     case '<':
2324     case '>':
2325     case '[':
2326     case ']':
2327     case '(':
2328     case ')':
2329     case '{':
2330     case '}':
2331     case '=':
2332     case '/':
2333     case '+':
2334     case '%':
2335     case '&':
2336     case '^':
2337     case '*':
2338     case '\'':
2339     case '"':
2340     case '`':
2341     case '~':
2342     case '|':
2343         return true;
2344     default:
2345         return false;
2346     }
2347 }
2348
2349 bool QTextEngine::atSpace(int position) const
2350 {
2351     const QChar c = layoutData->string.at(position);
2352
2353     return c == QLatin1Char(' ')
2354         || c == QChar::Nbsp
2355         || c == QChar::LineSeparator
2356         || c == QLatin1Char('\t')
2357         ;
2358 }
2359
2360
2361 void QTextEngine::indexAdditionalFormats()
2362 {
2363     if (!block.docHandle())
2364         return;
2365
2366     specialData->addFormatIndices.resize(specialData->addFormats.count());
2367     QTextFormatCollection * const formats = this->formats();
2368
2369     for (int i = 0; i < specialData->addFormats.count(); ++i) {
2370         specialData->addFormatIndices[i] = formats->indexForFormat(specialData->addFormats.at(i).format);
2371         specialData->addFormats[i].format = QTextCharFormat();
2372     }
2373 }
2374
2375 /* These two helper functions are used to determine whether we need to insert a ZWJ character
2376    between the text that gets truncated and the ellipsis. This is important to get
2377    correctly shaped results for arabic text.
2378 */
2379 static bool nextCharJoins(const QString &string, int pos)
2380 {
2381     while (pos < string.length() && string.at(pos).category() == QChar::Mark_NonSpacing)
2382         ++pos;
2383     if (pos == string.length())
2384         return false;
2385     return string.at(pos).joining() != QChar::OtherJoining;
2386 }
2387
2388 static bool prevCharJoins(const QString &string, int pos)
2389 {
2390     while (pos > 0 && string.at(pos - 1).category() == QChar::Mark_NonSpacing)
2391         --pos;
2392     if (pos == 0)
2393         return false;
2394     return (string.at(pos - 1).joining() == QChar::Dual || string.at(pos - 1).joining() == QChar::Center);
2395 }
2396
2397 QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int flags) const
2398 {
2399 //    qDebug() << "elidedText; available width" << width.toReal() << "text width:" << this->width(0, layoutData->string.length()).toReal();
2400
2401     if (flags & Qt::TextShowMnemonic) {
2402         itemize();
2403         HB_CharAttributes *attributes = const_cast<HB_CharAttributes *>(this->attributes());
2404         if (!attributes)
2405             return QString();
2406         for (int i = 0; i < layoutData->items.size(); ++i) {
2407             QScriptItem &si = layoutData->items[i];
2408             if (!si.num_glyphs)
2409                 shape(i);
2410
2411             unsigned short *logClusters = this->logClusters(&si);
2412             QGlyphLayout glyphs = shapedGlyphs(&si);
2413
2414             const int end = si.position + length(&si);
2415             for (int i = si.position; i < end - 1; ++i) {
2416                 if (layoutData->string.at(i) == QLatin1Char('&')) {
2417                     const int gp = logClusters[i - si.position];
2418                     glyphs.attributes[gp].dontPrint = true;
2419                     attributes[i + 1].charStop = false;
2420                     attributes[i + 1].whiteSpace = false;
2421                     attributes[i + 1].lineBreakType = HB_NoBreak;
2422                     if (layoutData->string.at(i + 1) == QLatin1Char('&'))
2423                         ++i;
2424                 }
2425             }
2426         }
2427     }
2428
2429     validate();
2430
2431     if (mode == Qt::ElideNone
2432         || this->width(0, layoutData->string.length()) <= width
2433         || layoutData->string.length() <= 1)
2434         return layoutData->string;
2435
2436     QFixed ellipsisWidth;
2437     QString ellipsisText;
2438     {
2439         QChar ellipsisChar(0x2026);
2440
2441         QFontEngine *fe = fnt.d->engineForScript(QUnicodeTables::Common);
2442
2443         QGlyphLayoutArray<1> ellipsisGlyph;
2444         {
2445             QFontEngine *feForEllipsis = (fe->type() == QFontEngine::Multi)
2446                 ? static_cast<QFontEngineMulti *>(fe)->engine(0)
2447                 : fe;
2448
2449             if (feForEllipsis->type() == QFontEngine::Mac)
2450                 feForEllipsis = fe;
2451
2452             // the lookup can be really slow when we use XLFD fonts
2453             if (feForEllipsis->type() != QFontEngine::XLFD
2454                 && feForEllipsis->canRender(&ellipsisChar, 1)) {
2455                     int nGlyphs = 1;
2456                     feForEllipsis->stringToCMap(&ellipsisChar, 1, &ellipsisGlyph, &nGlyphs, 0);
2457                 }
2458         }
2459
2460         if (ellipsisGlyph.glyphs[0]) {
2461             ellipsisWidth = ellipsisGlyph.advances_x[0];
2462             ellipsisText = ellipsisChar;
2463         } else {
2464             QString dotDotDot(QLatin1String("..."));
2465
2466             QGlyphLayoutArray<3> glyphs;
2467             int nGlyphs = 3;
2468             if (!fe->stringToCMap(dotDotDot.constData(), 3, &glyphs, &nGlyphs, 0))
2469                 // should never happen...
2470                 return layoutData->string;
2471             for (int i = 0; i < nGlyphs; ++i)
2472                 ellipsisWidth += glyphs.advances_x[i];
2473             ellipsisText = dotDotDot;
2474         }
2475     }
2476
2477     const QFixed availableWidth = width - ellipsisWidth;
2478     if (availableWidth < 0)
2479         return QString();
2480
2481     const HB_CharAttributes *attributes = this->attributes();
2482     if (!attributes)
2483         return QString();
2484
2485     if (mode == Qt::ElideRight) {
2486         QFixed currentWidth;
2487         int pos;
2488         int nextBreak = 0;
2489
2490         do {
2491             pos = nextBreak;
2492
2493             ++nextBreak;
2494             while (nextBreak < layoutData->string.length() && !attributes[nextBreak].charStop)
2495                 ++nextBreak;
2496
2497             currentWidth += this->width(pos, nextBreak - pos);
2498         } while (nextBreak < layoutData->string.length()
2499                  && currentWidth < availableWidth);
2500
2501         if (nextCharJoins(layoutData->string, pos))
2502             ellipsisText.prepend(QChar(0x200d) /* ZWJ */);
2503
2504         return layoutData->string.left(pos) + ellipsisText;
2505     } else if (mode == Qt::ElideLeft) {
2506         QFixed currentWidth;
2507         int pos;
2508         int nextBreak = layoutData->string.length();
2509
2510         do {
2511             pos = nextBreak;
2512
2513             --nextBreak;
2514             while (nextBreak > 0 && !attributes[nextBreak].charStop)
2515                 --nextBreak;
2516
2517             currentWidth += this->width(nextBreak, pos - nextBreak);
2518         } while (nextBreak > 0
2519                  && currentWidth < availableWidth);
2520
2521         if (prevCharJoins(layoutData->string, pos))
2522             ellipsisText.append(QChar(0x200d) /* ZWJ */);
2523
2524         return ellipsisText + layoutData->string.mid(pos);
2525     } else if (mode == Qt::ElideMiddle) {
2526         QFixed leftWidth;
2527         QFixed rightWidth;
2528
2529         int leftPos = 0;
2530         int nextLeftBreak = 0;
2531
2532         int rightPos = layoutData->string.length();
2533         int nextRightBreak = layoutData->string.length();
2534
2535         do {
2536             leftPos = nextLeftBreak;
2537             rightPos = nextRightBreak;
2538
2539             ++nextLeftBreak;
2540             while (nextLeftBreak < layoutData->string.length() && !attributes[nextLeftBreak].charStop)
2541                 ++nextLeftBreak;
2542
2543             --nextRightBreak;
2544             while (nextRightBreak > 0 && !attributes[nextRightBreak].charStop)
2545                 --nextRightBreak;
2546
2547             leftWidth += this->width(leftPos, nextLeftBreak - leftPos);
2548             rightWidth += this->width(nextRightBreak, rightPos - nextRightBreak);
2549         } while (nextLeftBreak < layoutData->string.length()
2550                  && nextRightBreak > 0
2551                  && leftWidth + rightWidth < availableWidth);
2552
2553         if (nextCharJoins(layoutData->string, leftPos))
2554             ellipsisText.prepend(QChar(0x200d) /* ZWJ */);
2555         if (prevCharJoins(layoutData->string, rightPos))
2556             ellipsisText.append(QChar(0x200d) /* ZWJ */);
2557
2558         return layoutData->string.left(leftPos) + ellipsisText + layoutData->string.mid(rightPos);
2559     }
2560
2561     return layoutData->string;
2562 }
2563
2564 void QTextEngine::setBoundary(int strPos) const
2565 {
2566     if (strPos <= 0 || strPos >= layoutData->string.length())
2567         return;
2568
2569     int itemToSplit = 0;
2570     while (itemToSplit < layoutData->items.size() && layoutData->items.at(itemToSplit).position <= strPos)
2571         itemToSplit++;
2572     itemToSplit--;
2573     if (layoutData->items.at(itemToSplit).position == strPos) {
2574         // already a split at the requested position
2575         return;
2576     }
2577     splitItem(itemToSplit, strPos - layoutData->items.at(itemToSplit).position);
2578 }
2579
2580 void QTextEngine::splitItem(int item, int pos) const
2581 {
2582     if (pos <= 0)
2583         return;
2584
2585     layoutData->items.insert(item + 1, layoutData->items[item]);
2586     QScriptItem &oldItem = layoutData->items[item];
2587     QScriptItem &newItem = layoutData->items[item+1];
2588     newItem.position += pos;
2589
2590     if (oldItem.num_glyphs) {
2591         // already shaped, break glyphs aswell
2592         int breakGlyph = logClusters(&oldItem)[pos];
2593
2594         newItem.num_glyphs = oldItem.num_glyphs - breakGlyph;
2595         oldItem.num_glyphs = breakGlyph;
2596         newItem.glyph_data_offset = oldItem.glyph_data_offset + breakGlyph;
2597
2598         for (int i = 0; i < newItem.num_glyphs; i++)
2599             logClusters(&newItem)[i] -= breakGlyph;
2600
2601         QFixed w = 0;
2602         const QGlyphLayout g = shapedGlyphs(&oldItem);
2603         for(int j = 0; j < breakGlyph; ++j)
2604             w += g.advances_x[j];
2605
2606         newItem.width = oldItem.width - w;
2607         oldItem.width = w;
2608     }
2609
2610 //     qDebug("split at position %d itempos=%d", pos, item);
2611 }
2612
2613 QFixed QTextEngine::calculateTabWidth(int item, QFixed x) const
2614 {
2615     const QScriptItem &si = layoutData->items[item];
2616
2617     QFixed dpiScale = 1;
2618     if (block.docHandle() && block.docHandle()->layout()) {
2619         QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
2620         if (pdev)
2621             dpiScale = QFixed::fromReal(pdev->logicalDpiY() / qreal(qt_defaultDpiY()));
2622     } else {
2623         dpiScale = QFixed::fromReal(fnt.d->dpi / qreal(qt_defaultDpiY()));
2624     }
2625
2626     QList<QTextOption::Tab> tabArray = option.tabs();
2627     if (!tabArray.isEmpty()) {
2628         if (isRightToLeft()) { // rebase the tabArray positions.
2629             QList<QTextOption::Tab> newTabs;
2630             QList<QTextOption::Tab>::Iterator iter = tabArray.begin();
2631             while(iter != tabArray.end()) {
2632                 QTextOption::Tab tab = *iter;
2633                 if (tab.type == QTextOption::LeftTab)
2634                     tab.type = QTextOption::RightTab;
2635                 else if (tab.type == QTextOption::RightTab)
2636                     tab.type = QTextOption::LeftTab;
2637                 newTabs << tab;
2638                 ++iter;
2639             }
2640             tabArray = newTabs;
2641         }
2642         for (int i = 0; i < tabArray.size(); ++i) {
2643             QFixed tab = QFixed::fromReal(tabArray[i].position) * dpiScale;
2644             if (tab > x) {  // this is the tab we need.
2645                 QTextOption::Tab tabSpec = tabArray[i];
2646                 int tabSectionEnd = layoutData->string.count();
2647                 if (tabSpec.type == QTextOption::RightTab || tabSpec.type == QTextOption::CenterTab) {
2648                     // find next tab to calculate the width required.
2649                     tab = QFixed::fromReal(tabSpec.position);
2650                     for (int i=item + 1; i < layoutData->items.count(); i++) {
2651                         const QScriptItem &item = layoutData->items[i];
2652                         if (item.analysis.flags == QScriptAnalysis::TabOrObject) { // found it.
2653                             tabSectionEnd = item.position;
2654                             break;
2655                         }
2656                     }
2657                 }
2658                 else if (tabSpec.type == QTextOption::DelimiterTab)
2659                     // find delimitor character to calculate the width required
2660                     tabSectionEnd = qMax(si.position, layoutData->string.indexOf(tabSpec.delimiter, si.position) + 1);
2661
2662                 if (tabSectionEnd > si.position) {
2663                     QFixed length;
2664                     // Calculate the length of text between this tab and the tabSectionEnd
2665                     for (int i=item; i < layoutData->items.count(); i++) {
2666                         QScriptItem &item = layoutData->items[i];
2667                         if (item.position > tabSectionEnd || item.position <= si.position)
2668                             continue;
2669                         shape(i); // first, lets make sure relevant text is already shaped
2670                         QGlyphLayout glyphs = this->shapedGlyphs(&item);
2671                         const int end = qMin(item.position + item.num_glyphs, tabSectionEnd) - item.position;
2672                         for (int i=0; i < end; i++)
2673                             length += glyphs.advances_x[i] * !glyphs.attributes[i].dontPrint;
2674                         if (end + item.position == tabSectionEnd && tabSpec.type == QTextOption::DelimiterTab) // remove half of matching char
2675                             length -= glyphs.advances_x[end] / 2 * !glyphs.attributes[end].dontPrint;
2676                     }
2677
2678                     switch (tabSpec.type) {
2679                     case QTextOption::CenterTab:
2680                         length /= 2;
2681                         // fall through
2682                     case QTextOption::DelimiterTab:
2683                         // fall through
2684                     case QTextOption::RightTab:
2685                         tab = QFixed::fromReal(tabSpec.position) * dpiScale - length;
2686                         if (tab < 0) // default to tab taking no space
2687                             return QFixed();
2688                         break;
2689                     case QTextOption::LeftTab:
2690                         break;
2691                     }
2692                 }
2693                 return tab - x;
2694             }
2695         }
2696     }
2697     QFixed tab = QFixed::fromReal(option.tabStop());
2698     if (tab <= 0)
2699         tab = 80; // default
2700     tab *= dpiScale;
2701     QFixed nextTabPos = ((x / tab).truncate() + 1) * tab;
2702     QFixed tabWidth = nextTabPos - x;
2703
2704     return tabWidth;
2705 }
2706
2707 void QTextEngine::resolveAdditionalFormats() const
2708 {
2709     if (!specialData || specialData->addFormats.isEmpty()
2710         || !block.docHandle()
2711         || !specialData->resolvedFormatIndices.isEmpty())
2712         return;
2713
2714     QTextFormatCollection *collection = this->formats();
2715
2716     specialData->resolvedFormatIndices.clear();
2717     QVector<int> indices(layoutData->items.count());
2718     for (int i = 0; i < layoutData->items.count(); ++i) {
2719         QTextCharFormat f = format(&layoutData->items.at(i));
2720         indices[i] = collection->indexForFormat(f);
2721     }
2722     specialData->resolvedFormatIndices = indices;
2723 }
2724
2725 QFixed QTextEngine::leadingSpaceWidth(const QScriptLine &line)
2726 {
2727     if (!line.hasTrailingSpaces
2728         || (option.flags() & QTextOption::IncludeTrailingSpaces)
2729         || !isRightToLeft())
2730         return QFixed();
2731
2732     int pos = line.length;
2733     const HB_CharAttributes *attributes = this->attributes();
2734     if (!attributes)
2735         return QFixed();
2736     while (pos > 0 && attributes[line.from + pos - 1].whiteSpace)
2737         --pos;
2738     return width(line.from + pos, line.length - pos);
2739 }
2740
2741 QFixed QTextEngine::alignLine(const QScriptLine &line)
2742 {
2743     QFixed x = 0;
2744     justify(line);
2745     // if width is QFIXED_MAX that means we used setNumColumns() and that implicitly makes this line left aligned.
2746     if (!line.justified && line.width != QFIXED_MAX) {
2747         int align = option.alignment();
2748         if (align & Qt::AlignJustify && isRightToLeft())
2749             align = Qt::AlignRight;
2750         if (align & Qt::AlignRight)
2751             x = line.width - (line.textAdvance + leadingSpaceWidth(line));
2752         else if (align & Qt::AlignHCenter)
2753             x = (line.width - line.textAdvance)/2;
2754     }
2755     return x;
2756 }
2757
2758 QFixed QTextEngine::offsetInLigature(const QScriptItem *si, int pos, int max, int glyph_pos)
2759 {
2760     unsigned short *logClusters = this->logClusters(si);
2761     const QGlyphLayout &glyphs = shapedGlyphs(si);
2762
2763     int offsetInCluster = 0;
2764     for (int i = pos - 1; i >= 0; i--) {
2765         if (logClusters[i] == glyph_pos)
2766             offsetInCluster++;
2767         else
2768             break;
2769     }
2770
2771     // in the case that the offset is inside a (multi-character) glyph,
2772     // interpolate the position.
2773     if (offsetInCluster > 0) {
2774         int clusterLength = 0;
2775         for (int i = pos - offsetInCluster; i < max; i++) {
2776             if (logClusters[i] == glyph_pos)
2777                 clusterLength++;
2778             else
2779                 break;
2780         }
2781         if (clusterLength)
2782             return glyphs.advances_x[glyph_pos] * offsetInCluster / clusterLength;
2783     }
2784
2785     return 0;
2786 }
2787
2788 int QTextEngine::previousLogicalPosition(int oldPos) const
2789 {
2790     const HB_CharAttributes *attrs = attributes();
2791     if (!attrs || oldPos < 0)
2792         return oldPos;
2793
2794     if (oldPos <= 0)
2795         return 0;
2796     oldPos--;
2797     while (oldPos && !attrs[oldPos].charStop)
2798         oldPos--;
2799     return oldPos;
2800 }
2801
2802 int QTextEngine::nextLogicalPosition(int oldPos) const
2803 {
2804     const HB_CharAttributes *attrs = attributes();
2805     int len = block.isValid() ? block.length() - 1
2806                               : layoutData->string.length();
2807     Q_ASSERT(len <= layoutData->string.length());
2808     if (!attrs || oldPos < 0 || oldPos >= len)
2809         return oldPos;
2810
2811     oldPos++;
2812     while (oldPos < len && !attrs[oldPos].charStop)
2813         oldPos++;
2814     return oldPos;
2815 }
2816
2817 int QTextEngine::lineNumberForTextPosition(int pos)
2818 {
2819     if (!layoutData)
2820         itemize();
2821     if (pos == layoutData->string.length() && lines.size())
2822         return lines.size() - 1;
2823     for (int i = 0; i < lines.size(); ++i) {
2824         const QScriptLine& line = lines[i];
2825         if (line.from + line.length > pos)
2826             return i;
2827     }
2828     return -1;
2829 }
2830
2831 void QTextEngine::insertionPointsForLine(int lineNum, QVector<int> &insertionPoints)
2832 {
2833     QTextLineItemIterator iterator(this, lineNum);
2834     bool rtl = isRightToLeft();
2835     bool lastLine = lineNum >= lines.size() - 1;
2836
2837     while (!iterator.atEnd()) {
2838         iterator.next();
2839         const QScriptItem *si = &layoutData->items[iterator.item];
2840         if (si->analysis.bidiLevel % 2) {
2841             int i = iterator.itemEnd - 1, min = iterator.itemStart;
2842             if (lastLine && (rtl ? iterator.atBeginning() : iterator.atEnd()))
2843                 i++;
2844             for (; i >= min; i--)
2845                 insertionPoints.push_back(i);
2846         } else {
2847             int i = iterator.itemStart, max = iterator.itemEnd;
2848             if (lastLine && (rtl ? iterator.atBeginning() : iterator.atEnd()))
2849                 max++;
2850             for (; i < max; i++)
2851                 insertionPoints.push_back(i);
2852         }
2853     }
2854 }
2855
2856 int QTextEngine::endOfLine(int lineNum)
2857 {
2858     QVector<int> insertionPoints;
2859     insertionPointsForLine(lineNum, insertionPoints);
2860
2861     if (insertionPoints.size() > 0)
2862         return insertionPoints.last();
2863     return 0;
2864 }
2865
2866 int QTextEngine::beginningOfLine(int lineNum)
2867 {
2868     QVector<int> insertionPoints;
2869     insertionPointsForLine(lineNum, insertionPoints);
2870
2871     if (insertionPoints.size() > 0)
2872         return insertionPoints.first();
2873     return 0;
2874 }
2875
2876 int QTextEngine::positionAfterVisualMovement(int pos, QTextCursor::MoveOperation op)
2877 {
2878     if (!layoutData)
2879         itemize();
2880
2881     bool moveRight = (op == QTextCursor::Right);
2882     bool alignRight = isRightToLeft();
2883     if (!layoutData->hasBidi)
2884         return moveRight ^ alignRight ? nextLogicalPosition(pos) : previousLogicalPosition(pos);
2885
2886     int lineNum = lineNumberForTextPosition(pos);
2887     Q_ASSERT(lineNum >= 0);
2888
2889     QVector<int> insertionPoints;
2890     insertionPointsForLine(lineNum, insertionPoints);
2891     int i, max = insertionPoints.size();
2892     for (i = 0; i < max; i++)
2893         if (pos == insertionPoints[i]) {
2894             if (moveRight) {
2895                 if (i + 1 < max)
2896                     return insertionPoints[i + 1];
2897             } else {
2898                 if (i > 0)
2899                     return insertionPoints[i - 1];
2900             }
2901
2902             if (moveRight ^ alignRight) {
2903                 if (lineNum + 1 < lines.size())
2904                     return alignRight ? endOfLine(lineNum + 1) : beginningOfLine(lineNum + 1);
2905             }
2906             else {
2907                 if (lineNum > 0)
2908                     return alignRight ? beginningOfLine(lineNum - 1) : endOfLine(lineNum - 1);
2909             }
2910         }
2911
2912     return pos;
2913 }
2914
2915 QStackTextEngine::QStackTextEngine(const QString &string, const QFont &f)
2916     : QTextEngine(string, f),
2917       _layoutData(string, _memory, MemSize)
2918 {
2919     stackEngine = true;
2920     layoutData = &_layoutData;
2921 }
2922
2923 QTextItemInt::QTextItemInt(const QScriptItem &si, QFont *font, const QTextCharFormat &format)
2924     : justified(false), underlineStyle(QTextCharFormat::NoUnderline), charFormat(format),
2925       num_chars(0), chars(0), logClusters(0), f(0), fontEngine(0)
2926 {
2927     // explicitly initialize flags so that initFontAttributes can be called
2928     // multiple times on the same TextItem
2929     flags = 0;
2930     if (si.analysis.bidiLevel %2)
2931         flags |= QTextItem::RightToLeft;
2932     ascent = si.ascent;
2933     descent = si.descent;
2934     f = font;
2935     fontEngine = f->d->engineForScript(si.analysis.script);
2936     Q_ASSERT(fontEngine);
2937
2938     if (format.hasProperty(QTextFormat::TextUnderlineStyle)) {
2939         underlineStyle = format.underlineStyle();
2940     } else if (format.boolProperty(QTextFormat::FontUnderline)
2941                || f->d->underline) {
2942         underlineStyle = QTextCharFormat::SingleUnderline;
2943     }
2944
2945     // compat
2946     if (underlineStyle == QTextCharFormat::SingleUnderline)
2947         flags |= QTextItem::Underline;
2948
2949     if (f->d->overline || format.fontOverline())
2950         flags |= QTextItem::Overline;
2951     if (f->d->strikeOut || format.fontStrikeOut())
2952         flags |= QTextItem::StrikeOut;
2953 }
2954
2955 QTextItemInt::QTextItemInt(const QGlyphLayout &g, QFont *font, const QChar *chars_, int numChars, QFontEngine *fe)
2956     : flags(0), justified(false), underlineStyle(QTextCharFormat::NoUnderline),
2957       num_chars(numChars), chars(chars_), logClusters(0), f(font),  glyphs(g), fontEngine(fe)
2958 {
2959 }
2960
2961 QTextItemInt QTextItemInt::midItem(QFontEngine *fontEngine, int firstGlyphIndex, int numGlyphs) const
2962 {
2963     QTextItemInt ti = *this;
2964     const int end = firstGlyphIndex + numGlyphs;
2965     ti.glyphs = glyphs.mid(firstGlyphIndex, numGlyphs);
2966     ti.fontEngine = fontEngine;
2967
2968     if (logClusters && chars) {
2969         const int logClusterOffset = logClusters[0];
2970         while (logClusters[ti.chars - chars] - logClusterOffset < firstGlyphIndex)
2971             ++ti.chars;
2972
2973         ti.logClusters += (ti.chars - chars);
2974
2975         ti.num_chars = 0;
2976         int char_start = ti.chars - chars;
2977         while (char_start + ti.num_chars < num_chars && ti.logClusters[ti.num_chars] - logClusterOffset < end)
2978             ++ti.num_chars;
2979     }
2980     return ti;
2981 }
2982
2983
2984 QTransform qt_true_matrix(qreal w, qreal h, QTransform x)
2985 {
2986     QRectF rect = x.mapRect(QRectF(0, 0, w, h));
2987     return x * QTransform::fromTranslate(-rect.x(), -rect.y());
2988 }
2989
2990
2991 glyph_metrics_t glyph_metrics_t::transformed(const QTransform &matrix) const
2992 {
2993     if (matrix.type() < QTransform::TxTranslate)
2994         return *this;
2995
2996     glyph_metrics_t m = *this;
2997
2998     qreal w = width.toReal();
2999     qreal h = height.toReal();
3000     QTransform xform = qt_true_matrix(w, h, matrix);
3001
3002     QRectF rect(0, 0, w, h);
3003     rect = xform.mapRect(rect);
3004     m.width = QFixed::fromReal(rect.width());
3005     m.height = QFixed::fromReal(rect.height());
3006
3007     QLineF l = xform.map(QLineF(x.toReal(), y.toReal(), xoff.toReal(), yoff.toReal()));
3008
3009     m.x = QFixed::fromReal(l.x1());
3010     m.y = QFixed::fromReal(l.y1());
3011
3012     // The offset is relative to the baseline which is why we use dx/dy of the line
3013     m.xoff = QFixed::fromReal(l.dx());
3014     m.yoff = QFixed::fromReal(l.dy());
3015
3016     return m;
3017 }
3018
3019 QTextLineItemIterator::QTextLineItemIterator(QTextEngine *_eng, int _lineNum, const QPointF &pos,
3020                                              const QTextLayout::FormatRange *_selection)
3021     : eng(_eng),
3022       line(eng->lines[_lineNum]),
3023       si(0),
3024       lineNum(_lineNum),
3025       lineEnd(line.from + line.length),
3026       firstItem(eng->findItem(line.from)),
3027       lastItem(eng->findItem(lineEnd - 1)),
3028       nItems((firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0),
3029       logicalItem(-1),
3030       item(-1),
3031       visualOrder(nItems),
3032       levels(nItems),
3033       selection(_selection)
3034 {
3035     pos_x = x = QFixed::fromReal(pos.x());
3036
3037     x += line.x;
3038
3039     x += eng->alignLine(line);
3040
3041     for (int i = 0; i < nItems; ++i)
3042         levels[i] = eng->layoutData->items[i+firstItem].analysis.bidiLevel;
3043     QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
3044
3045     eng->shapeLine(line);
3046 }
3047
3048 QScriptItem &QTextLineItemIterator::next()
3049 {
3050     x += itemWidth;
3051
3052     ++logicalItem;
3053     item = visualOrder[logicalItem] + firstItem;
3054     itemLength = eng->length(item);
3055     si = &eng->layoutData->items[item];
3056     if (!si->num_glyphs)
3057         eng->shape(item);
3058
3059     if (si->analysis.flags >= QScriptAnalysis::TabOrObject) {
3060         itemWidth = si->width;
3061         return *si;
3062     }
3063
3064     unsigned short *logClusters = eng->logClusters(si);
3065     QGlyphLayout glyphs = eng->shapedGlyphs(si);
3066
3067     itemStart = qMax(line.from, si->position);
3068     glyphsStart = logClusters[itemStart - si->position];
3069     if (lineEnd < si->position + itemLength) {
3070         itemEnd = lineEnd;
3071         glyphsEnd = logClusters[itemEnd-si->position];
3072     } else {
3073         itemEnd = si->position + itemLength;
3074         glyphsEnd = si->num_glyphs;
3075     }
3076     // show soft-hyphen at line-break
3077     if (si->position + itemLength >= lineEnd
3078         && eng->layoutData->string.at(lineEnd - 1) == 0x00ad)
3079         glyphs.attributes[glyphsEnd - 1].dontPrint = false;
3080
3081     itemWidth = 0;
3082     for (int g = glyphsStart; g < glyphsEnd; ++g)
3083         itemWidth += glyphs.effectiveAdvance(g);
3084
3085     return *si;
3086 }
3087
3088 bool QTextLineItemIterator::getSelectionBounds(QFixed *selectionX, QFixed *selectionWidth) const
3089 {
3090     *selectionX = *selectionWidth = 0;
3091
3092     if (!selection)
3093         return false;
3094
3095     if (si->analysis.flags >= QScriptAnalysis::TabOrObject) {
3096         if (si->position >= selection->start + selection->length
3097             || si->position + itemLength <= selection->start)
3098             return false;
3099
3100         *selectionX = x;
3101         *selectionWidth = itemWidth;
3102     } else {
3103         unsigned short *logClusters = eng->logClusters(si);
3104         QGlyphLayout glyphs = eng->shapedGlyphs(si);
3105
3106         int from = qMax(itemStart, selection->start) - si->position;
3107         int to = qMin(itemEnd, selection->start + selection->length) - si->position;
3108         if (from >= to)
3109             return false;
3110
3111         int start_glyph = logClusters[from];
3112         int end_glyph = (to == eng->length(item)) ? si->num_glyphs : logClusters[to];
3113         QFixed soff;
3114         QFixed swidth;
3115         if (si->analysis.bidiLevel %2) {
3116             for (int g = glyphsEnd - 1; g >= end_glyph; --g)
3117                 soff += glyphs.effectiveAdvance(g);
3118             for (int g = end_glyph - 1; g >= start_glyph; --g)
3119                 swidth += glyphs.effectiveAdvance(g);
3120         } else {
3121             for (int g = glyphsStart; g < start_glyph; ++g)
3122                 soff += glyphs.effectiveAdvance(g);
3123             for (int g = start_glyph; g < end_glyph; ++g)
3124                 swidth += glyphs.effectiveAdvance(g);
3125         }
3126
3127         // If the starting character is in the middle of a ligature,
3128         // selection should only contain the right part of that ligature
3129         // glyph, so we need to get the width of the left part here and
3130         // add it to *selectionX
3131         QFixed leftOffsetInLigature = eng->offsetInLigature(si, from, to, start_glyph);
3132         *selectionX = x + soff + leftOffsetInLigature;
3133         *selectionWidth = swidth - leftOffsetInLigature;
3134         // If the ending character is also part of a ligature, swidth does
3135         // not contain that part yet, we also need to find out the width of
3136         // that left part
3137         *selectionWidth += eng->offsetInLigature(si, to, eng->length(item), end_glyph);
3138     }
3139     return true;
3140 }
3141
3142 QT_END_NAMESPACE