| Line | Source | Count |
| 1 | | - |
| 2 | | - |
| 3 | | - |
| 4 | | - |
| 5 | | - |
| 6 | | - |
| 7 | | - |
| 8 | | - |
| 9 | | - |
| 10 | | - |
| 11 | | - |
| 12 | | - |
| 13 | | - |
| 14 | | - |
| 15 | | - |
| 16 | | - |
| 17 | | - |
| 18 | | - |
| 19 | | - |
| 20 | | - |
| 21 | | - |
| 22 | | - |
| 23 | | - |
| 24 | | - |
| 25 | | - |
| 26 | | - |
| 27 | | - |
| 28 | | - |
| 29 | | - |
| 30 | | - |
| 31 | | - |
| 32 | | - |
| 33 | | - |
| 34 | | - |
| 35 | | - |
| 36 | | - |
| 37 | | - |
| 38 | | - |
| 39 | | - |
| 40 | #include "qdebug.h" | - |
| 41 | #include "qtextformat.h" | - |
| 42 | #include "qtextformat_p.h" | - |
| 43 | #include "qtextengine_p.h" | - |
| 44 | #include "qabstracttextdocumentlayout.h" | - |
| 45 | #include "qtextlayout.h" | - |
| 46 | #include "qtextboundaryfinder.h" | - |
| 47 | #include "qvarlengtharray.h" | - |
| 48 | #include "qfont.h" | - |
| 49 | #include "qfont_p.h" | - |
| 50 | #include "qfontengine_p.h" | - |
| 51 | #include "qstring.h" | - |
| 52 | #include "qtextdocument_p.h" | - |
| 53 | #include "qrawfont.h" | - |
| 54 | #include "qrawfont_p.h" | - |
| 55 | #include <qguiapplication.h> | - |
| 56 | #include <qinputmethod.h> | - |
| 57 | #include <algorithm> | - |
| 58 | #include <stdlib.h> | - |
| 59 | | - |
| 60 | QT_BEGIN_NAMESPACE | - |
| 61 | | - |
| 62 | static const float smallCapsFraction = 0.7f; | - |
| 63 | | - |
| 64 | namespace { | - |
| 65 | | - |
| 66 | | - |
| 67 | class Itemizer { | - |
| 68 | public: | - |
| 69 | Itemizer(const QString &string, const QScriptAnalysis *analysis, QScriptItemArray &items) | - |
| 70 | : m_string(string), | - |
| 71 | m_analysis(analysis), | - |
| 72 | m_items(items), | - |
| 73 | m_splitter(0) | - |
| 74 | { | - |
| 75 | } | - |
| 76 | ~Itemizer() | - |
| 77 | { | - |
| 78 | delete m_splitter; | - |
| 79 | } | - |
| 80 | | - |
| 81 | | - |
| 82 | | - |
| 83 | void generate(int start, int length, QFont::Capitalization caps) | - |
| 84 | { | - |
| 85 | if (caps == QFont::SmallCaps) | - |
| 86 | generateScriptItemsSmallCaps(reinterpret_cast<const ushort *>(m_string.unicode()), start, length); | - |
| 87 | else if(caps == QFont::Capitalize) | - |
| 88 | generateScriptItemsCapitalize(start, length); | - |
| 89 | else if(caps != QFont::MixedCase) { | - |
| 90 | generateScriptItemsAndChangeCase(start, length, | - |
| 91 | caps == QFont::AllLowercase ? QScriptAnalysis::Lowercase : QScriptAnalysis::Uppercase); | - |
| 92 | } | - |
| 93 | else | - |
| 94 | generateScriptItems(start, length); | - |
| 95 | } | - |
| 96 | | - |
| 97 | private: | - |
| 98 | enum { MaxItemLength = 4096 }; | - |
| 99 | | - |
| 100 | void generateScriptItemsAndChangeCase(int start, int length, QScriptAnalysis::Flags flags) | - |
| 101 | { | - |
| 102 | generateScriptItems(start, length); | - |
| 103 | if (m_items.isEmpty()) | - |
| 104 | return; | - |
| 105 | QScriptItemArray::Iterator iter = m_items.end(); | - |
| 106 | do { | - |
| 107 | iter--; | - |
| 108 | if (iter->analysis.flags < QScriptAnalysis::LineOrParagraphSeparator) | - |
| 109 | iter->analysis.flags = flags; | - |
| 110 | } while (iter->position > start); | - |
| 111 | } | - |
| 112 | | - |
| 113 | void generateScriptItems(int start, int length) | - |
| 114 | { | - |
| 115 | if (!length) | - |
| 116 | return; | - |
| 117 | const int end = start + length; | - |
| 118 | for (int i = start + 1; i < end; ++i) { | - |
| 119 | if (m_analysis[i].bidiLevel == m_analysis[start].bidiLevel | - |
| 120 | && m_analysis[i].flags == m_analysis[start].flags | - |
| 121 | && (m_analysis[i].script == m_analysis[start].script || m_string[i] == QLatin1Char('.')) | - |
| 122 | && m_analysis[i].flags < QScriptAnalysis::SpaceTabOrObject | - |
| 123 | && i - start < MaxItemLength) | - |
| 124 | continue; | - |
| 125 | m_items.append(QScriptItem(start, m_analysis[start])); | - |
| 126 | start = i; | - |
| 127 | } | - |
| 128 | m_items.append(QScriptItem(start, m_analysis[start])); | - |
| 129 | } | - |
| 130 | | - |
| 131 | void generateScriptItemsCapitalize(int start, int length) | - |
| 132 | { | - |
| 133 | if (!length) | - |
| 134 | return; | - |
| 135 | | - |
| 136 | if (!m_splitter) | - |
| 137 | m_splitter = new QTextBoundaryFinder(QTextBoundaryFinder::Word, | - |
| 138 | m_string.constData(), m_string.length(), | - |
| 139 | 0, 0); | - |
| 140 | | - |
| 141 | m_splitter->setPosition(start); | - |
| 142 | QScriptAnalysis itemAnalysis = m_analysis[start]; | - |
| 143 | | - |
| 144 | if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartOfItem) | - |
| 145 | itemAnalysis.flags = QScriptAnalysis::Uppercase; | - |
| 146 | | - |
| 147 | m_splitter->toNextBoundary(); | - |
| 148 | | - |
| 149 | const int end = start + length; | - |
| 150 | for (int i = start + 1; i < end; ++i) { | - |
| 151 | bool atWordStart = false; | - |
| 152 | | - |
| 153 | if (i == m_splitter->position()) { | - |
| 154 | if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartOfItem) { | - |
| 155 | Q_ASSERT(m_analysis[i].flags < QScriptAnalysis::TabOrObject); | - |
| 156 | atWordStart = true; | - |
| 157 | } | - |
| 158 | | - |
| 159 | m_splitter->toNextBoundary(); | - |
| 160 | } | - |
| 161 | | - |
| 162 | if (m_analysis[i] == itemAnalysis | - |
| 163 | && m_analysis[i].flags < QScriptAnalysis::TabOrObject | - |
| 164 | && !atWordStart | - |
| 165 | && i - start < MaxItemLength) | - |
| 166 | continue; | - |
| 167 | | - |
| 168 | m_items.append(QScriptItem(start, itemAnalysis)); | - |
| 169 | start = i; | - |
| 170 | itemAnalysis = m_analysis[start]; | - |
| 171 | | - |
| 172 | if (atWordStart) | - |
| 173 | itemAnalysis.flags = QScriptAnalysis::Uppercase; | - |
| 174 | } | - |
| 175 | m_items.append(QScriptItem(start, itemAnalysis)); | - |
| 176 | } | - |
| 177 | | - |
| 178 | void generateScriptItemsSmallCaps(const ushort *uc, int start, int length) | - |
| 179 | { | - |
| 180 | if (!length) | - |
| 181 | return; | - |
| 182 | bool lower = (QChar::category(uc[start]) == QChar::Letter_Lowercase); | - |
| 183 | const int end = start + length; | - |
| 184 | | - |
| 185 | for (int i = start + 1; i < end; ++i) { | - |
| 186 | bool l = (QChar::category(uc[i]) == QChar::Letter_Lowercase); | - |
| 187 | if ((m_analysis[i] == m_analysis[start]) | - |
| 188 | && m_analysis[i].flags < QScriptAnalysis::TabOrObject | - |
| 189 | && l == lower | - |
| 190 | && i - start < MaxItemLength) | - |
| 191 | continue; | - |
| 192 | m_items.append(QScriptItem(start, m_analysis[start])); | - |
| 193 | if (lower) | - |
| 194 | m_items.last().analysis.flags = QScriptAnalysis::SmallCaps; | - |
| 195 | | - |
| 196 | start = i; | - |
| 197 | lower = l; | - |
| 198 | } | - |
| 199 | m_items.append(QScriptItem(start, m_analysis[start])); | - |
| 200 | if (lower) | - |
| 201 | m_items.last().analysis.flags = QScriptAnalysis::SmallCaps; | - |
| 202 | } | - |
| 203 | | - |
| 204 | const QString &m_string; | - |
| 205 | const QScriptAnalysis * const m_analysis; | - |
| 206 | QScriptItemArray &m_items; | - |
| 207 | QTextBoundaryFinder *m_splitter; | - |
| 208 | }; | - |
| 209 | } | - |
| 210 | | - |
| 211 | | - |
| 212 | | - |
| 213 | | - |
| 214 | | - |
| 215 | | - |
| 216 | | - |
| 217 | | - |
| 218 | #define BIDI_DEBUG 0 | - |
| 219 | #if (BIDI_DEBUG >= 1) | - |
| 220 | QT_BEGIN_INCLUDE_NAMESPACE | - |
| 221 | #include <iostream> | - |
| 222 | QT_END_INCLUDE_NAMESPACE | - |
| 223 | using namespace std; | - |
| 224 | | - |
| 225 | static const char *directions[] = { | - |
| 226 | "DirL", "DirR", "DirEN", "DirES", "DirET", "DirAN", "DirCS", "DirB", "DirS", "DirWS", "DirON", | - |
| 227 | "DirLRE", "DirLRO", "DirAL", "DirRLE", "DirRLO", "DirPDF", "DirNSM", "DirBN", | - |
| 228 | "DirLRI", "DirRLI", "DirFSI", "DirPDI" | - |
| 229 | }; | - |
| 230 | | - |
| 231 | #endif | - |
| 232 | | - |
| 233 | struct QBidiStatus { | - |
| 234 | QBidiStatus() { | - |
| 235 | eor = QChar::DirON; | - |
| 236 | lastStrong = QChar::DirON; | - |
| 237 | last = QChar:: DirON; | - |
| 238 | dir = QChar::DirON; | - |
| 239 | } | - |
| 240 | QChar::Direction eor; | - |
| 241 | QChar::Direction lastStrong; | - |
| 242 | QChar::Direction last; | - |
| 243 | QChar::Direction dir; | - |
| 244 | }; | - |
| 245 | | - |
| 246 | enum { MaxBidiLevel = 61 }; | - |
| 247 | | - |
| 248 | struct QBidiControl { | - |
| 249 | inline QBidiControl(bool rtl) | - |
| 250 | : cCtx(0), base(rtl ? 1 : 0), level(rtl ? 1 : 0), override(false) {} | - |
| 251 | | - |
| 252 | inline void embed(bool rtl, bool o = false) { | - |
| 253 | unsigned int toAdd = 1; | - |
| 254 | if((level%2 != 0) == rtl ) { | - |
| 255 | ++toAdd; | - |
| 256 | } | - |
| 257 | if (level + toAdd <= MaxBidiLevel) { | - |
| 258 | ctx[cCtx].level = level; | - |
| 259 | ctx[cCtx].override = override; | - |
| 260 | cCtx++; | - |
| 261 | override = o; | - |
| 262 | level += toAdd; | - |
| 263 | } | - |
| 264 | } | - |
| 265 | inline bool canPop() const { return cCtx != 0; } | - |
| 266 | inline void pdf() { | - |
| 267 | Q_ASSERT(cCtx); | - |
| 268 | --cCtx; | - |
| 269 | level = ctx[cCtx].level; | - |
| 270 | override = ctx[cCtx].override; | - |
| 271 | } | - |
| 272 | | - |
| 273 | inline QChar::Direction basicDirection() const { | - |
| 274 | return (base ? QChar::DirR : QChar:: DirL); | - |
| 275 | } | - |
| 276 | inline unsigned int baseLevel() const { | - |
| 277 | return base; | - |
| 278 | } | - |
| 279 | inline QChar::Direction direction() const { | - |
| 280 | return ((level%2) ? QChar::DirR : QChar:: DirL); | - |
| 281 | } | - |
| 282 | | - |
| 283 | struct { | - |
| 284 | unsigned int level; | - |
| 285 | bool override; | - |
| 286 | } ctx[MaxBidiLevel]; | - |
| 287 | unsigned int cCtx; | - |
| 288 | const unsigned int base; | - |
| 289 | unsigned int level; | - |
| 290 | bool override; | - |
| 291 | }; | - |
| 292 | | - |
| 293 | | - |
| 294 | static void appendItems(QScriptAnalysis *analysis, int &start, int &stop, const QBidiControl &control, QChar::Direction dir) | - |
| 295 | { | - |
| 296 | if (start > stop) | - |
| 297 | return; | - |
| 298 | | - |
| 299 | int level = control.level; | - |
| 300 | | - |
| 301 | if(dir != QChar::DirON && !control.override) { | - |
| 302 | | - |
| 303 | if(level % 2) { | - |
| 304 | if(dir == QChar::DirL || dir == QChar::DirAN || dir == QChar::DirEN) | - |
| 305 | level++; | - |
| 306 | } else { | - |
| 307 | if(dir == QChar::DirR) | - |
| 308 | level++; | - |
| 309 | else if(dir == QChar::DirAN || dir == QChar::DirEN) | - |
| 310 | level += 2; | - |
| 311 | } | - |
| 312 | } | - |
| 313 | | - |
| 314 | #if (BIDI_DEBUG >= 1) | - |
| 315 | qDebug("new run: dir=%s from %d, to %d level = %d override=%d", directions[dir], start, stop, level, control.override); | - |
| 316 | #endif | - |
| 317 | QScriptAnalysis *s = analysis + start; | - |
| 318 | const QScriptAnalysis *e = analysis + stop; | - |
| 319 | while (s <= e) { | - |
| 320 | s->bidiLevel = level; | - |
| 321 | ++s; | - |
| 322 | } | - |
| 323 | ++stop; | - |
| 324 | start = stop; | - |
| 325 | } | - |
| 326 | | - |
| 327 | static QChar::Direction skipBoundryNeutrals(QScriptAnalysis *analysis, | - |
| 328 | const ushort *unicode, int length, | - |
| 329 | int &sor, int &eor, QBidiControl &control) | - |
| 330 | { | - |
| 331 | QChar::Direction dir = control.basicDirection(); | - |
| 332 | int level = sor > 0 ? analysis[sor - 1].bidiLevel : control.level; | - |
| 333 | while (sor < length) { | - |
| 334 | dir = QChar::direction(unicode[sor]); | - |
| 335 | | - |
| 336 | if (dir != QChar::DirBN) | - |
| 337 | break; | - |
| 338 | analysis[sor++].bidiLevel = level; | - |
| 339 | } | - |
| 340 | | - |
| 341 | eor = sor; | - |
| 342 | if (eor == length) | - |
| 343 | dir = control.basicDirection(); | - |
| 344 | | - |
| 345 | return dir; | - |
| 346 | } | - |
| 347 | | - |
| 348 | | - |
| 349 | static bool bidiItemize(QTextEngine *engine, QScriptAnalysis *analysis, QBidiControl &control) | - |
| 350 | { | - |
| 351 | bool rightToLeft = (control.basicDirection() == 1); | - |
| 352 | bool hasBidi = rightToLeft; | - |
| 353 | #if BIDI_DEBUG >= 2 | - |
| 354 | qDebug() << "bidiItemize: rightToLeft=" << rightToLeft << engine->layoutData->string; | - |
| 355 | #endif | - |
| 356 | | - |
| 357 | int sor = 0; | - |
| 358 | int eor = -1; | - |
| 359 | | - |
| 360 | | - |
| 361 | int length = engine->layoutData->string.length(); | - |
| 362 | | - |
| 363 | const ushort *unicode = (const ushort *)engine->layoutData->string.unicode(); | - |
| 364 | int current = 0; | - |
| 365 | | - |
| 366 | QChar::Direction dir = rightToLeft ? QChar::DirR : QChar::DirL; | - |
| 367 | QBidiStatus status; | - |
| 368 | | - |
| 369 | QChar::Direction sdir = QChar::direction(*unicode); | - |
| 370 | if (sdir != QChar::DirL && sdir != QChar::DirR && sdir != QChar::DirEN && sdir != QChar::DirAN) | - |
| 371 | sdir = QChar::DirON; | - |
| 372 | else | - |
| 373 | dir = QChar::DirON; | - |
| 374 | status.eor = sdir; | - |
| 375 | status.lastStrong = rightToLeft ? QChar::DirR : QChar::DirL; | - |
| 376 | status.last = status.lastStrong; | - |
| 377 | status.dir = sdir; | - |
| 378 | | - |
| 379 | | - |
| 380 | while (current <= length) { | - |
| 381 | | - |
| 382 | QChar::Direction dirCurrent; | - |
| 383 | if (current == (int)length) | - |
| 384 | dirCurrent = control.basicDirection(); | - |
| 385 | else | - |
| 386 | dirCurrent = QChar::direction(unicode[current]); | - |
| 387 | | - |
| 388 | #if (BIDI_DEBUG >= 2) | - |
| 389 | | - |
| 390 | | - |
| 391 | | - |
| 392 | | - |
| 393 | | - |
| 394 | | - |
| 395 | #endif | - |
| 396 | | - |
| 397 | switch(dirCurrent) { | - |
| 398 | | - |
| 399 | | - |
| 400 | case QChar::DirRLE: | - |
| 401 | case QChar::DirRLO: | - |
| 402 | case QChar::DirLRE: | - |
| 403 | case QChar::DirLRO: | - |
| 404 | { | - |
| 405 | bool rtl = (dirCurrent == QChar::DirRLE || dirCurrent == QChar::DirRLO); | - |
| 406 | hasBidi |= rtl; | - |
| 407 | bool override = (dirCurrent == QChar::DirLRO || dirCurrent == QChar::DirRLO); | - |
| 408 | | - |
| 409 | unsigned int level = control.level+1; | - |
| 410 | if ((level%2 != 0) == rtl) ++level; | - |
| 411 | if(level < MaxBidiLevel) { | - |
| 412 | eor = current-1; | - |
| 413 | appendItems(analysis, sor, eor, control, dir); | - |
| 414 | eor = current; | - |
| 415 | control.embed(rtl, override); | - |
| 416 | QChar::Direction edir = (rtl ? QChar::DirR : QChar::DirL); | - |
| 417 | dir = status.eor = edir; | - |
| 418 | status.lastStrong = edir; | - |
| 419 | } | - |
| 420 | break; | - |
| 421 | } | - |
| 422 | case QChar::DirPDF: | - |
| 423 | { | - |
| 424 | if (control.canPop()) { | - |
| 425 | if (dir != control.direction()) { | - |
| 426 | eor = current-1; | - |
| 427 | appendItems(analysis, sor, eor, control, dir); | - |
| 428 | dir = control.direction(); | - |
| 429 | } | - |
| 430 | eor = current; | - |
| 431 | appendItems(analysis, sor, eor, control, dir); | - |
| 432 | control.pdf(); | - |
| 433 | dir = QChar::DirON; status.eor = QChar::DirON; | - |
| 434 | status.last = control.direction(); | - |
| 435 | if (control.override) | - |
| 436 | dir = control.direction(); | - |
| 437 | else | - |
| 438 | dir = QChar::DirON; | - |
| 439 | status.lastStrong = control.direction(); | - |
| 440 | } | - |
| 441 | break; | - |
| 442 | } | - |
| 443 | | - |
| 444 | | - |
| 445 | case QChar::DirL: | - |
| 446 | if(dir == QChar::DirON) | - |
| 447 | dir = QChar::DirL; | - |
| 448 | switch(status.last) | - |
| 449 | { | - |
| 450 | case QChar::DirL: | - |
| 451 | eor = current; status.eor = QChar::DirL; break; | - |
| 452 | case QChar::DirR: | - |
| 453 | case QChar::DirAL: | - |
| 454 | case QChar::DirEN: | - |
| 455 | case QChar::DirAN: | - |
| 456 | if (eor >= 0) { | - |
| 457 | appendItems(analysis, sor, eor, control, dir); | - |
| 458 | status.eor = dir = skipBoundryNeutrals(analysis, unicode, length, sor, eor, control); | - |
| 459 | } else { | - |
| 460 | eor = current; status.eor = dir; | - |
| 461 | } | - |
| 462 | break; | - |
| 463 | case QChar::DirES: | - |
| 464 | case QChar::DirET: | - |
| 465 | case QChar::DirCS: | - |
| 466 | case QChar::DirBN: | - |
| 467 | case QChar::DirB: | - |
| 468 | case QChar::DirS: | - |
| 469 | case QChar::DirWS: | - |
| 470 | case QChar::DirON: | - |
| 471 | if(dir != QChar::DirL) { | - |
| 472 | | - |
| 473 | if(control.direction() == QChar::DirR) { | - |
| 474 | if(status.eor != QChar::DirR) { | - |
| 475 | | - |
| 476 | appendItems(analysis, sor, eor, control, dir); | - |
| 477 | status.eor = QChar::DirON; | - |
| 478 | dir = QChar::DirR; | - |
| 479 | } | - |
| 480 | eor = current - 1; | - |
| 481 | appendItems(analysis, sor, eor, control, dir); | - |
| 482 | status.eor = dir = skipBoundryNeutrals(analysis, unicode, length, sor, eor, control); | - |
| 483 | } else { | - |
| 484 | if(status.eor != QChar::DirL) { | - |
| 485 | appendItems(analysis, sor, eor, control, dir); | - |
| 486 | status.eor = QChar::DirON; | - |
| 487 | dir = QChar::DirL; | - |
| 488 | } else { | - |
| 489 | eor = current; status.eor = QChar::DirL; break; | - |
| 490 | } | - |
| 491 | } | - |
| 492 | } else { | - |
| 493 | eor = current; status.eor = QChar::DirL; | - |
| 494 | } | - |
| 495 | default: | - |
| 496 | break; | - |
| 497 | } | - |
| 498 | status.lastStrong = QChar::DirL; | - |
| 499 | break; | - |
| 500 | case QChar::DirAL: | - |
| 501 | case QChar::DirR: | - |
| 502 | hasBidi = true; | - |
| 503 | if(dir == QChar::DirON) dir = QChar::DirR; | - |
| 504 | switch(status.last) | - |
| 505 | { | - |
| 506 | case QChar::DirL: | - |
| 507 | case QChar::DirEN: | - |
| 508 | case QChar::DirAN: | - |
| 509 | if (eor >= 0) | - |
| 510 | appendItems(analysis, sor, eor, control, dir); | - |
| 511 | | - |
| 512 | case QChar::DirR: | - |
| 513 | case QChar::DirAL: | - |
| 514 | dir = QChar::DirR; eor = current; status.eor = QChar::DirR; break; | - |
| 515 | case QChar::DirES: | - |
| 516 | case QChar::DirET: | - |
| 517 | case QChar::DirCS: | - |
| 518 | case QChar::DirBN: | - |
| 519 | case QChar::DirB: | - |
| 520 | case QChar::DirS: | - |
| 521 | case QChar::DirWS: | - |
| 522 | case QChar::DirON: | - |
| 523 | if(status.eor != QChar::DirR && status.eor != QChar::DirAL) { | - |
| 524 | | - |
| 525 | if(control.direction() == QChar::DirR | - |
| 526 | || status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) { | - |
| 527 | appendItems(analysis, sor, eor, control, dir); | - |
| 528 | dir = QChar::DirR; status.eor = QChar::DirON; | - |
| 529 | eor = current; | - |
| 530 | } else { | - |
| 531 | eor = current - 1; | - |
| 532 | appendItems(analysis, sor, eor, control, dir); | - |
| 533 | dir = QChar::DirR; status.eor = QChar::DirON; | - |
| 534 | } | - |
| 535 | } else { | - |
| 536 | eor = current; status.eor = QChar::DirR; | - |
| 537 | } | - |
| 538 | default: | - |
| 539 | break; | - |
| 540 | } | - |
| 541 | status.lastStrong = dirCurrent; | - |
| 542 | break; | - |
| 543 | | - |
| 544 | | - |
| 545 | | - |
| 546 | case QChar::DirNSM: | - |
| 547 | if (eor == current-1) | - |
| 548 | eor = current; | - |
| 549 | break; | - |
| 550 | case QChar::DirEN: | - |
| 551 | | - |
| 552 | if(status.lastStrong != QChar::DirAL) { | - |
| 553 | if(dir == QChar::DirON) { | - |
| 554 | if(status.lastStrong == QChar::DirL) | - |
| 555 | dir = QChar::DirL; | - |
| 556 | else | - |
| 557 | dir = QChar::DirEN; | - |
| 558 | } | - |
| 559 | switch(status.last) | - |
| 560 | { | - |
| 561 | case QChar::DirET: | - |
| 562 | if (status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) { | - |
| 563 | appendItems(analysis, sor, eor, control, dir); | - |
| 564 | status.eor = QChar::DirON; | - |
| 565 | dir = QChar::DirAN; | - |
| 566 | } | - |
| 567 | | - |
| 568 | case QChar::DirEN: | - |
| 569 | case QChar::DirL: | - |
| 570 | eor = current; | - |
| 571 | status.eor = dirCurrent; | - |
| 572 | break; | - |
| 573 | case QChar::DirR: | - |
| 574 | case QChar::DirAL: | - |
| 575 | case QChar::DirAN: | - |
| 576 | if (eor >= 0) | - |
| 577 | appendItems(analysis, sor, eor, control, dir); | - |
| 578 | else | - |
| 579 | eor = current; | - |
| 580 | status.eor = QChar::DirEN; | - |
| 581 | dir = QChar::DirAN; break; | - |
| 582 | case QChar::DirES: | - |
| 583 | case QChar::DirCS: | - |
| 584 | if(status.eor == QChar::DirEN || dir == QChar::DirAN) { | - |
| 585 | eor = current; break; | - |
| 586 | } | - |
| 587 | case QChar::DirBN: | - |
| 588 | case QChar::DirB: | - |
| 589 | case QChar::DirS: | - |
| 590 | case QChar::DirWS: | - |
| 591 | case QChar::DirON: | - |
| 592 | if(status.eor == QChar::DirR) { | - |
| 593 | | - |
| 594 | eor = current - 1; | - |
| 595 | appendItems(analysis, sor, eor, control, dir); | - |
| 596 | dir = QChar::DirON; status.eor = QChar::DirEN; | - |
| 597 | dir = QChar::DirAN; | - |
| 598 | } | - |
| 599 | else if(status.eor == QChar::DirL || | - |
| 600 | (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) { | - |
| 601 | eor = current; status.eor = dirCurrent; | - |
| 602 | } else { | - |
| 603 | | - |
| 604 | if(dir != QChar::DirL) { | - |
| 605 | appendItems(analysis, sor, eor, control, dir); | - |
| 606 | dir = QChar::DirON; status.eor = QChar::DirON; | - |
| 607 | eor = current - 1; | - |
| 608 | dir = QChar::DirR; | - |
| 609 | appendItems(analysis, sor, eor, control, dir); | - |
| 610 | dir = QChar::DirON; status.eor = QChar::DirON; | - |
| 611 | dir = QChar::DirAN; | - |
| 612 | } else { | - |
| 613 | eor = current; status.eor = dirCurrent; | - |
| 614 | } | - |
| 615 | } | - |
| 616 | default: | - |
| 617 | break; | - |
| 618 | } | - |
| 619 | break; | - |
| 620 | } | - |
| 621 | case QChar::DirAN: | - |
| 622 | hasBidi = true; | - |
| 623 | dirCurrent = QChar::DirAN; | - |
| 624 | if(dir == QChar::DirON) dir = QChar::DirAN; | - |
| 625 | switch(status.last) | - |
| 626 | { | - |
| 627 | case QChar::DirL: | - |
| 628 | case QChar::DirAN: | - |
| 629 | eor = current; status.eor = QChar::DirAN; break; | - |
| 630 | case QChar::DirR: | - |
| 631 | case QChar::DirAL: | - |
| 632 | case QChar::DirEN: | - |
| 633 | if (eor >= 0){ | - |
| 634 | appendItems(analysis, sor, eor, control, dir); | - |
| 635 | } else { | - |
| 636 | eor = current; | - |
| 637 | } | - |
| 638 | dir = QChar::DirAN; status.eor = QChar::DirAN; | - |
| 639 | break; | - |
| 640 | case QChar::DirCS: | - |
| 641 | if(status.eor == QChar::DirAN) { | - |
| 642 | eor = current; break; | - |
| 643 | } | - |
| 644 | case QChar::DirES: | - |
| 645 | case QChar::DirET: | - |
| 646 | case QChar::DirBN: | - |
| 647 | case QChar::DirB: | - |
| 648 | case QChar::DirS: | - |
| 649 | case QChar::DirWS: | - |
| 650 | case QChar::DirON: | - |
| 651 | if(status.eor == QChar::DirR) { | - |
| 652 | | - |
| 653 | eor = current - 1; | - |
| 654 | appendItems(analysis, sor, eor, control, dir); | - |
| 655 | status.eor = QChar::DirAN; | - |
| 656 | dir = QChar::DirAN; | - |
| 657 | } else if(status.eor == QChar::DirL || | - |
| 658 | (status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) { | - |
| 659 | eor = current; status.eor = dirCurrent; | - |
| 660 | } else { | - |
| 661 | | - |
| 662 | if(dir != QChar::DirL) { | - |
| 663 | appendItems(analysis, sor, eor, control, dir); | - |
| 664 | status.eor = QChar::DirON; | - |
| 665 | eor = current - 1; | - |
| 666 | dir = QChar::DirR; | - |
| 667 | appendItems(analysis, sor, eor, control, dir); | - |
| 668 | status.eor = QChar::DirAN; | - |
| 669 | dir = QChar::DirAN; | - |
| 670 | } else { | - |
| 671 | eor = current; status.eor = dirCurrent; | - |
| 672 | } | - |
| 673 | } | - |
| 674 | default: | - |
| 675 | break; | - |
| 676 | } | - |
| 677 | break; | - |
| 678 | case QChar::DirES: | - |
| 679 | case QChar::DirCS: | - |
| 680 | break; | - |
| 681 | case QChar::DirET: | - |
| 682 | if(status.last == QChar::DirEN) { | - |
| 683 | dirCurrent = QChar::DirEN; | - |
| 684 | eor = current; status.eor = dirCurrent; | - |
| 685 | } | - |
| 686 | break; | - |
| 687 | | - |
| 688 | | - |
| 689 | case QChar::DirBN: | - |
| 690 | break; | - |
| 691 | | - |
| 692 | case QChar::DirB: | - |
| 693 | | - |
| 694 | break; | - |
| 695 | case QChar::DirS: | - |
| 696 | | - |
| 697 | break; | - |
| 698 | case QChar::DirWS: | - |
| 699 | case QChar::DirON: | - |
| 700 | break; | - |
| 701 | default: | - |
| 702 | break; | - |
| 703 | } | - |
| 704 | | - |
| 705 | | - |
| 706 | | - |
| 707 | if(current >= (int)length) break; | - |
| 708 | | - |
| 709 | | - |
| 710 | switch(dirCurrent) { | - |
| 711 | case QChar::DirET: | - |
| 712 | case QChar::DirES: | - |
| 713 | case QChar::DirCS: | - |
| 714 | case QChar::DirS: | - |
| 715 | case QChar::DirWS: | - |
| 716 | case QChar::DirON: | - |
| 717 | switch(status.last) | - |
| 718 | { | - |
| 719 | case QChar::DirL: | - |
| 720 | case QChar::DirR: | - |
| 721 | case QChar::DirAL: | - |
| 722 | case QChar::DirEN: | - |
| 723 | case QChar::DirAN: | - |
| 724 | status.last = dirCurrent; | - |
| 725 | break; | - |
| 726 | default: | - |
| 727 | status.last = QChar::DirON; | - |
| 728 | } | - |
| 729 | break; | - |
| 730 | case QChar::DirNSM: | - |
| 731 | case QChar::DirBN: | - |
| 732 | | - |
| 733 | break; | - |
| 734 | case QChar::DirLRO: | - |
| 735 | case QChar::DirLRE: | - |
| 736 | status.last = QChar::DirL; | - |
| 737 | break; | - |
| 738 | case QChar::DirRLO: | - |
| 739 | case QChar::DirRLE: | - |
| 740 | status.last = QChar::DirR; | - |
| 741 | break; | - |
| 742 | case QChar::DirEN: | - |
| 743 | if (status.last == QChar::DirL) { | - |
| 744 | status.last = QChar::DirL; | - |
| 745 | break; | - |
| 746 | } | - |
| 747 | | - |
| 748 | default: | - |
| 749 | status.last = dirCurrent; | - |
| 750 | } | - |
| 751 | | - |
| 752 | ++current; | - |
| 753 | } | - |
| 754 | | - |
| 755 | #if (BIDI_DEBUG >= 1) | - |
| 756 | qDebug() << "reached end of line current=" << current << ", eor=" << eor; | - |
| 757 | #endif | - |
| 758 | eor = current - 1; | - |
| 759 | | - |
| 760 | if (sor <= eor) | - |
| 761 | appendItems(analysis, sor, eor, control, dir); | - |
| 762 | | - |
| 763 | return hasBidi; | - |
| 764 | } | - |
| 765 | | - |
| 766 | void QTextEngine::bidiReorder(int numItems, const quint8 *levels, int *visualOrder) | - |
| 767 | { | - |
| 768 | | - |
| 769 | | - |
| 770 | quint8 levelLow = 128; | - |
| 771 | quint8 levelHigh = 0; | - |
| 772 | int i = 0; | - |
| 773 | while (i < numItems) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 774 | | - |
| 775 | if (levels[i] > levelHigh)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 776 | levelHigh = levels[i]; never executed: levelHigh = levels[i]; | 0 |
| 777 | if (levels[i] < levelLow)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 778 | levelLow = levels[i]; never executed: levelLow = levels[i]; | 0 |
| 779 | i++; | - |
| 780 | } never executed: end of block | 0 |
| 781 | | - |
| 782 | | - |
| 783 | | - |
| 784 | | - |
| 785 | | - |
| 786 | | - |
| 787 | if(!(levelLow%2)) levelLow++; never executed: levelLow++; | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 788 | | - |
| 789 | #if (BIDI_DEBUG >= 1) | - |
| 790 | | - |
| 791 | #endif | - |
| 792 | | - |
| 793 | int count = numItems - 1; | - |
| 794 | for (i = 0; i < numItems; i++)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 795 | visualOrder[i] = i; never executed: visualOrder[i] = i; | 0 |
| 796 | | - |
| 797 | while(levelHigh >= levelLow) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 798 | int i = 0; | - |
| 799 | while (i < count) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 800 | while(i < count && levels[i] < levelHigh) i++; never executed: i++; | TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 801 | int start = i; | - |
| 802 | while(i <= count && levels[i] >= levelHigh) i++; never executed: i++; | TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 803 | int end = i-1; | - |
| 804 | | - |
| 805 | if(start != end) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 806 | | - |
| 807 | for(int j = 0; j < (end-start+1)/2; j++) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 808 | int tmp = visualOrder[start+j]; | - |
| 809 | visualOrder[start+j] = visualOrder[end-j]; | - |
| 810 | visualOrder[end-j] = tmp; | - |
| 811 | } never executed: end of block | 0 |
| 812 | } never executed: end of block | 0 |
| 813 | i++; | - |
| 814 | } never executed: end of block | 0 |
| 815 | levelHigh--; | - |
| 816 | } never executed: end of block | 0 |
| 817 | | - |
| 818 | #if (BIDI_DEBUG >= 1) | - |
| 819 | | - |
| 820 | | - |
| 821 | | - |
| 822 | #endif | - |
| 823 | } never executed: end of block | 0 |
| 824 | | - |
| 825 | | - |
| 826 | enum JustificationClass { | - |
| 827 | Justification_Prohibited = 0, | - |
| 828 | Justification_Arabic_Space = 1, | - |
| 829 | Justification_Character = 2, | - |
| 830 | Justification_Space = 4, | - |
| 831 | Justification_Arabic_Normal = 7, | - |
| 832 | Justification_Arabic_Waw = 8, | - |
| 833 | Justification_Arabic_BaRa = 9, | - |
| 834 | Justification_Arabic_Alef = 10, | - |
| 835 | Justification_Arabic_HahDal = 11, | - |
| 836 | Justification_Arabic_Seen = 12, | - |
| 837 | Justification_Arabic_Kashida = 13 | - |
| 838 | }; | - |
| 839 | | - |
| 840 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 841 | | - |
| 842 | | - |
| 843 | | - |
| 844 | | - |
| 845 | | - |
| 846 | static inline void qt_getDefaultJustificationOpportunities(const ushort *string, int length, QGlyphLayout g, ushort *log_clusters, int spaceAs) | - |
| 847 | { | - |
| 848 | int str_pos = 0; | - |
| 849 | while (str_pos < length) { | - |
| 850 | int glyph_pos = log_clusters[str_pos]; | - |
| 851 | | - |
| 852 | Q_ASSERT(glyph_pos < g.numGlyphs && g.attributes[glyph_pos].clusterStart); | - |
| 853 | | - |
| 854 | uint ucs4 = string[str_pos]; | - |
| 855 | if (QChar::isHighSurrogate(ucs4) && str_pos + 1 < length) { | - |
| 856 | ushort low = string[str_pos + 1]; | - |
| 857 | if (QChar::isLowSurrogate(low)) { | - |
| 858 | ++str_pos; | - |
| 859 | ucs4 = QChar::surrogateToUcs4(ucs4, low); | - |
| 860 | } | - |
| 861 | } | - |
| 862 | | - |
| 863 | | - |
| 864 | do { | - |
| 865 | ++str_pos; | - |
| 866 | } while (str_pos < length && log_clusters[str_pos] == glyph_pos); | - |
| 867 | do { | - |
| 868 | ++glyph_pos; | - |
| 869 | } while (glyph_pos < g.numGlyphs && !g.attributes[glyph_pos].clusterStart); | - |
| 870 | --glyph_pos; | - |
| 871 | | - |
| 872 | | - |
| 873 | if (Q_LIKELY(QChar::isLetterOrNumber(ucs4))) | - |
| 874 | g.attributes[glyph_pos].justification = Justification_Character; | - |
| 875 | else if (Q_LIKELY(QChar::isSpace(ucs4))) | - |
| 876 | g.attributes[glyph_pos].justification = spaceAs; | - |
| 877 | } | - |
| 878 | } | - |
| 879 | | - |
| 880 | static inline void qt_getJustificationOpportunities(const ushort *string, int length, const QScriptItem &si, QGlyphLayout g, ushort *log_clusters) | - |
| 881 | { | - |
| 882 | Q_ASSERT(length > 0 && g.numGlyphs > 0); | - |
| 883 | | - |
| 884 | for (int glyph_pos = 0; glyph_pos < g.numGlyphs; ++glyph_pos) | - |
| 885 | g.attributes[glyph_pos].justification = Justification_Prohibited; | - |
| 886 | | - |
| 887 | int spaceAs; | - |
| 888 | | - |
| 889 | switch (si.analysis.script) { | - |
| 890 | case QChar::Script_Arabic: | - |
| 891 | case QChar::Script_Syriac: | - |
| 892 | case QChar::Script_Nko: | - |
| 893 | case QChar::Script_Mandaic: | - |
| 894 | case QChar::Script_Mongolian: | - |
| 895 | case QChar::Script_PhagsPa: | - |
| 896 | case QChar::Script_Manichaean: | - |
| 897 | case QChar::Script_PsalterPahlavi: | - |
| 898 | | - |
| 899 | spaceAs = Justification_Arabic_Space; | - |
| 900 | break; | - |
| 901 | | - |
| 902 | case QChar::Script_Tibetan: | - |
| 903 | case QChar::Script_Hiragana: | - |
| 904 | case QChar::Script_Katakana: | - |
| 905 | case QChar::Script_Bopomofo: | - |
| 906 | case QChar::Script_Han: | - |
| 907 | | - |
| 908 | spaceAs = Justification_Character; | - |
| 909 | break; | - |
| 910 | | - |
| 911 | default: | - |
| 912 | spaceAs = Justification_Space; | - |
| 913 | break; | - |
| 914 | } | - |
| 915 | | - |
| 916 | qt_getDefaultJustificationOpportunities(string, length, g, log_clusters, spaceAs); | - |
| 917 | } | - |
| 918 | | - |
| 919 | #endif // QT_ENABLE_HARFBUZZ_NG | - |
| 920 | | - |
| 921 | | - |
| 922 | | - |
| 923 | void QTextEngine::shapeLine(const QScriptLine &line) | - |
| 924 | { | - |
| 925 | QFixed x; | - |
| 926 | bool first = true; | - |
| 927 | int item = findItem(line.from); | - |
| 928 | if (item == -1) | - |
| 929 | return; | - |
| 930 | | - |
| 931 | const int end = findItem(line.from + line.length + line.trailingSpaces - 1, item); | - |
| 932 | for ( ; item <= end; ++item) { | - |
| 933 | QScriptItem &si = layoutData->items[item]; | - |
| 934 | if (si.analysis.flags == QScriptAnalysis::Tab) { | - |
| 935 | ensureSpace(1); | - |
| 936 | si.width = calculateTabWidth(item, x); | - |
| 937 | } else { | - |
| 938 | shape(item); | - |
| 939 | } | - |
| 940 | if (first && si.position != line.from) { | - |
| 941 | QGlyphLayout glyphs = shapedGlyphs(&si); | - |
| 942 | Q_ASSERT(line.from > si.position); | - |
| 943 | for (int i = line.from - si.position - 1; i >= 0; i--) { | - |
| 944 | x -= glyphs.effectiveAdvance(i); | - |
| 945 | } | - |
| 946 | } | - |
| 947 | first = false; | - |
| 948 | | - |
| 949 | x += si.width; | - |
| 950 | } | - |
| 951 | } | - |
| 952 | | - |
| 953 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 954 | extern bool qt_useHarfbuzzNG(); | - |
| 955 | #endif | - |
| 956 | | - |
| 957 | void QTextEngine::shapeText(int item) const | - |
| 958 | { | - |
| 959 | Q_ASSERT(item < layoutData->items.size()); | - |
| 960 | QScriptItem &si = layoutData->items[item]; | - |
| 961 | | - |
| 962 | if (si.num_glyphs) | - |
| 963 | return; | - |
| 964 | | - |
| 965 | si.width = 0; | - |
| 966 | si.glyph_data_offset = layoutData->used; | - |
| 967 | | - |
| 968 | const ushort *string = reinterpret_cast<const ushort *>(layoutData->string.constData()) + si.position; | - |
| 969 | const int itemLength = length(item); | - |
| 970 | | - |
| 971 | QString casedString; | - |
| 972 | if (si.analysis.flags && si.analysis.flags <= QScriptAnalysis::SmallCaps) { | - |
| 973 | casedString.resize(itemLength); | - |
| 974 | ushort *uc = reinterpret_cast<ushort *>(casedString.data()); | - |
| 975 | for (int i = 0; i < itemLength; ++i) { | - |
| 976 | uint ucs4 = string[i]; | - |
| 977 | if (QChar::isHighSurrogate(ucs4) && i + 1 < itemLength) { | - |
| 978 | uint low = string[i + 1]; | - |
| 979 | if (QChar::isLowSurrogate(low)) { | - |
| 980 | ++i; | - |
| 981 | ucs4 = QChar::surrogateToUcs4(ucs4, low); | - |
| 982 | ucs4 = si.analysis.flags == QScriptAnalysis::Lowercase ? QChar::toLower(ucs4) | - |
| 983 | : QChar::toUpper(ucs4); | - |
| 984 | | - |
| 985 | uc[i] = QChar::lowSurrogate(ucs4); | - |
| 986 | } | - |
| 987 | } else { | - |
| 988 | uc[i] = si.analysis.flags == QScriptAnalysis::Lowercase ? QChar::toLower(ucs4) | - |
| 989 | : QChar::toUpper(ucs4); | - |
| 990 | } | - |
| 991 | } | - |
| 992 | string = reinterpret_cast<const ushort *>(casedString.constData()); | - |
| 993 | } | - |
| 994 | | - |
| 995 | if (Q_UNLIKELY(!ensureSpace(itemLength))) { | - |
| 996 | Q_UNREACHABLE(); | - |
| 997 | return; | - |
| 998 | } | - |
| 999 | | - |
| 1000 | QFontEngine *fontEngine = this->fontEngine(si, &si.ascent, &si.descent, &si.leading); | - |
| 1001 | | - |
| 1002 | | - |
| 1003 | | - |
| 1004 | QVector<uint> itemBoundaries; | - |
| 1005 | itemBoundaries.reserve(24); | - |
| 1006 | if (fontEngine->type() == QFontEngine::Multi) { | - |
| 1007 | | - |
| 1008 | | - |
| 1009 | QGlyphLayout initialGlyphs = availableGlyphs(&si); | - |
| 1010 | | - |
| 1011 | int nGlyphs = initialGlyphs.numGlyphs; | - |
| 1012 | QFontEngine::ShaperFlags shaperFlags(QFontEngine::GlyphIndicesOnly); | - |
| 1013 | if (!fontEngine->stringToCMap(reinterpret_cast<const QChar *>(string), itemLength, &initialGlyphs, &nGlyphs, shaperFlags)) | - |
| 1014 | Q_UNREACHABLE(); | - |
| 1015 | | - |
| 1016 | uint lastEngine = ~0u; | - |
| 1017 | for (int i = 0, glyph_pos = 0; i < itemLength; ++i, ++glyph_pos) { | - |
| 1018 | const uint engineIdx = initialGlyphs.glyphs[glyph_pos] >> 24; | - |
| 1019 | if (lastEngine != engineIdx) { | - |
| 1020 | itemBoundaries.append(i); | - |
| 1021 | itemBoundaries.append(glyph_pos); | - |
| 1022 | itemBoundaries.append(engineIdx); | - |
| 1023 | | - |
| 1024 | if (engineIdx != 0) { | - |
| 1025 | QFontEngine *actualFontEngine = static_cast<QFontEngineMulti *>(fontEngine)->engine(engineIdx); | - |
| 1026 | si.ascent = qMax(actualFontEngine->ascent(), si.ascent); | - |
| 1027 | si.descent = qMax(actualFontEngine->descent(), si.descent); | - |
| 1028 | si.leading = qMax(actualFontEngine->leading(), si.leading); | - |
| 1029 | } | - |
| 1030 | | - |
| 1031 | lastEngine = engineIdx; | - |
| 1032 | } | - |
| 1033 | | - |
| 1034 | if (QChar::isHighSurrogate(string[i]) && i + 1 < itemLength && QChar::isLowSurrogate(string[i + 1])) | - |
| 1035 | ++i; | - |
| 1036 | } | - |
| 1037 | } else { | - |
| 1038 | itemBoundaries.append(0); | - |
| 1039 | itemBoundaries.append(0); | - |
| 1040 | itemBoundaries.append(0); | - |
| 1041 | } | - |
| 1042 | | - |
| 1043 | bool kerningEnabled; | - |
| 1044 | bool letterSpacingIsAbsolute; | - |
| 1045 | QFixed letterSpacing, wordSpacing; | - |
| 1046 | #ifndef QT_NO_RAWFONT | - |
| 1047 | if (useRawFont) { | - |
| 1048 | QTextCharFormat f = format(&si); | - |
| 1049 | kerningEnabled = f.fontKerning(); | - |
| 1050 | wordSpacing = QFixed::fromReal(f.fontWordSpacing()); | - |
| 1051 | letterSpacing = QFixed::fromReal(f.fontLetterSpacing()); | - |
| 1052 | letterSpacingIsAbsolute = true; | - |
| 1053 | } else | - |
| 1054 | #endif | - |
| 1055 | { | - |
| 1056 | QFont font = this->font(si); | - |
| 1057 | kerningEnabled = font.d->kerning; | - |
| 1058 | letterSpacingIsAbsolute = font.d->letterSpacingIsAbsolute; | - |
| 1059 | letterSpacing = font.d->letterSpacing; | - |
| 1060 | wordSpacing = font.d->wordSpacing; | - |
| 1061 | | - |
| 1062 | if (letterSpacingIsAbsolute && letterSpacing.value()) | - |
| 1063 | letterSpacing *= font.d->dpi / qt_defaultDpiY(); | - |
| 1064 | } | - |
| 1065 | | - |
| 1066 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 1067 | if (Q_LIKELY(qt_useHarfbuzzNG())) | - |
| 1068 | si.num_glyphs = shapeTextWithHarfbuzzNG(si, string, itemLength, fontEngine, itemBoundaries, kerningEnabled, letterSpacing != 0); | - |
| 1069 | else | - |
| 1070 | #endif | - |
| 1071 | si.num_glyphs = shapeTextWithHarfbuzz(si, string, itemLength, fontEngine, itemBoundaries, kerningEnabled); | - |
| 1072 | if (Q_UNLIKELY(si.num_glyphs == 0)) { | - |
| 1073 | Q_UNREACHABLE(); | - |
| 1074 | return; | - |
| 1075 | } | - |
| 1076 | | - |
| 1077 | | - |
| 1078 | layoutData->used += si.num_glyphs; | - |
| 1079 | | - |
| 1080 | QGlyphLayout glyphs = shapedGlyphs(&si); | - |
| 1081 | | - |
| 1082 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 1083 | if (Q_LIKELY(qt_useHarfbuzzNG())) | - |
| 1084 | qt_getJustificationOpportunities(string, itemLength, si, glyphs, logClusters(&si)); | - |
| 1085 | #endif | - |
| 1086 | | - |
| 1087 | if (letterSpacing != 0) { | - |
| 1088 | for (int i = 1; i < si.num_glyphs; ++i) { | - |
| 1089 | if (glyphs.attributes[i].clusterStart) { | - |
| 1090 | if (letterSpacingIsAbsolute) | - |
| 1091 | glyphs.advances[i - 1] += letterSpacing; | - |
| 1092 | else { | - |
| 1093 | QFixed &advance = glyphs.advances[i - 1]; | - |
| 1094 | advance += (letterSpacing - 100) * advance / 100; | - |
| 1095 | } | - |
| 1096 | } | - |
| 1097 | } | - |
| 1098 | if (letterSpacingIsAbsolute) | - |
| 1099 | glyphs.advances[si.num_glyphs - 1] += letterSpacing; | - |
| 1100 | else { | - |
| 1101 | QFixed &advance = glyphs.advances[si.num_glyphs - 1]; | - |
| 1102 | advance += (letterSpacing - 100) * advance / 100; | - |
| 1103 | } | - |
| 1104 | } | - |
| 1105 | if (wordSpacing != 0) { | - |
| 1106 | for (int i = 0; i < si.num_glyphs; ++i) { | - |
| 1107 | if (glyphs.attributes[i].justification == Justification_Space | - |
| 1108 | || glyphs.attributes[i].justification == Justification_Arabic_Space) { | - |
| 1109 | | - |
| 1110 | if (i + 1 == si.num_glyphs | - |
| 1111 | ||(glyphs.attributes[i+1].justification != Justification_Space | - |
| 1112 | && glyphs.attributes[i+1].justification != Justification_Arabic_Space)) | - |
| 1113 | glyphs.advances[i] += wordSpacing; | - |
| 1114 | } | - |
| 1115 | } | - |
| 1116 | } | - |
| 1117 | | - |
| 1118 | for (int i = 0; i < si.num_glyphs; ++i) | - |
| 1119 | si.width += glyphs.advances[i] * !glyphs.attributes[i].dontPrint; | - |
| 1120 | } | - |
| 1121 | | - |
| 1122 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 1123 | | - |
| 1124 | QT_BEGIN_INCLUDE_NAMESPACE | - |
| 1125 | | - |
| 1126 | #include "qharfbuzzng_p.h" | - |
| 1127 | | - |
| 1128 | QT_END_INCLUDE_NAMESPACE | - |
| 1129 | | - |
| 1130 | int QTextEngine::shapeTextWithHarfbuzzNG(const QScriptItem &si, | - |
| 1131 | const ushort *string, | - |
| 1132 | int itemLength, | - |
| 1133 | QFontEngine *fontEngine, | - |
| 1134 | const QVector<uint> &itemBoundaries, | - |
| 1135 | bool kerningEnabled, | - |
| 1136 | bool hasLetterSpacing) const | - |
| 1137 | { | - |
| 1138 | uint glyphs_shaped = 0; | - |
| 1139 | | - |
| 1140 | hb_buffer_t *buffer = hb_buffer_create(); | - |
| 1141 | hb_buffer_set_unicode_funcs(buffer, hb_qt_get_unicode_funcs()); | - |
| 1142 | hb_buffer_pre_allocate(buffer, itemLength); | - |
| 1143 | if (Q_UNLIKELY(!hb_buffer_allocation_successful(buffer))) { | - |
| 1144 | hb_buffer_destroy(buffer); | - |
| 1145 | return 0; | - |
| 1146 | } | - |
| 1147 | | - |
| 1148 | hb_segment_properties_t props = HB_SEGMENT_PROPERTIES_DEFAULT; | - |
| 1149 | props.direction = si.analysis.bidiLevel % 2 ? HB_DIRECTION_RTL : HB_DIRECTION_LTR; | - |
| 1150 | QChar::Script script = QChar::Script(si.analysis.script); | - |
| 1151 | props.script = hb_qt_script_to_script(script); | - |
| 1152 | | - |
| 1153 | | - |
| 1154 | for (int k = 0; k < itemBoundaries.size(); k += 3) { | - |
| 1155 | const uint item_pos = itemBoundaries[k]; | - |
| 1156 | const uint item_length = (k + 4 < itemBoundaries.size() ? itemBoundaries[k + 3] : itemLength) - item_pos; | - |
| 1157 | const uint engineIdx = itemBoundaries[k + 2]; | - |
| 1158 | | - |
| 1159 | QFontEngine *actualFontEngine = fontEngine->type() != QFontEngine::Multi ? fontEngine | - |
| 1160 | : static_cast<QFontEngineMulti *>(fontEngine)->engine(engineIdx); | - |
| 1161 | | - |
| 1162 | | - |
| 1163 | | - |
| 1164 | hb_buffer_clear_contents(buffer); | - |
| 1165 | hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16_t *>(string) + item_pos, item_length, 0, item_length); | - |
| 1166 | | - |
| 1167 | hb_buffer_set_segment_properties(buffer, &props); | - |
| 1168 | hb_buffer_guess_segment_properties(buffer); | - |
| 1169 | | - |
| 1170 | uint buffer_flags = HB_BUFFER_FLAG_DEFAULT; | - |
| 1171 | | - |
| 1172 | | - |
| 1173 | if (Q_UNLIKELY(actualFontEngine->symbol)) | - |
| 1174 | buffer_flags |= HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES; | - |
| 1175 | hb_buffer_set_flags(buffer, hb_buffer_flags_t(buffer_flags)); | - |
| 1176 | | - |
| 1177 | | - |
| 1178 | | - |
| 1179 | { | - |
| 1180 | hb_font_t *hb_font = hb_qt_font_get_for_engine(actualFontEngine); | - |
| 1181 | Q_ASSERT(hb_font); | - |
| 1182 | hb_qt_font_set_use_design_metrics(hb_font, option.useDesignMetrics() ? uint(QFontEngine::DesignMetrics) : 0); | - |
| 1183 | | - |
| 1184 | | - |
| 1185 | | - |
| 1186 | bool scriptRequiresOpenType = ((script >= QChar::Script_Syriac && script <= QChar::Script_Sinhala) | - |
| 1187 | || script == QChar::Script_Khmer || script == QChar::Script_Nko); | - |
| 1188 | | - |
| 1189 | bool dontLigate = hasLetterSpacing && !scriptRequiresOpenType; | - |
| 1190 | const hb_feature_t features[5] = { | - |
| 1191 | { HB_TAG('k','e','r','n'), !!kerningEnabled, 0, uint(-1) }, | - |
| 1192 | { HB_TAG('l','i','g','a'), !dontLigate, 0, uint(-1) }, | - |
| 1193 | { HB_TAG('c','l','i','g'), !dontLigate, 0, uint(-1) }, | - |
| 1194 | { HB_TAG('d','l','i','g'), !dontLigate, 0, uint(-1) }, | - |
| 1195 | { HB_TAG('h','l','i','g'), !dontLigate, 0, uint(-1) } }; | - |
| 1196 | const int num_features = dontLigate ? 5 : 1; | - |
| 1197 | | - |
| 1198 | const char *const *shaper_list = Q_NULLPTR; | - |
| 1199 | #if defined(Q_OS_DARWIN) | - |
| 1200 | | - |
| 1201 | | - |
| 1202 | | - |
| 1203 | if (actualFontEngine->type() != QFontEngine::Mac) { | - |
| 1204 | static const char *s_shaper_list_without_coretext[] = { | - |
| 1205 | "graphite2", | - |
| 1206 | "ot", | - |
| 1207 | "fallback", | - |
| 1208 | Q_NULLPTR | - |
| 1209 | }; | - |
| 1210 | shaper_list = s_shaper_list_without_coretext; | - |
| 1211 | } | - |
| 1212 | #endif | - |
| 1213 | | - |
| 1214 | bool shapedOk = hb_shape_full(hb_font, buffer, features, num_features, shaper_list); | - |
| 1215 | if (Q_UNLIKELY(!shapedOk)) { | - |
| 1216 | hb_buffer_destroy(buffer); | - |
| 1217 | return 0; | - |
| 1218 | } | - |
| 1219 | | - |
| 1220 | if (Q_UNLIKELY(HB_DIRECTION_IS_BACKWARD(props.direction))) | - |
| 1221 | hb_buffer_reverse(buffer); | - |
| 1222 | } | - |
| 1223 | | - |
| 1224 | const uint num_glyphs = hb_buffer_get_length(buffer); | - |
| 1225 | | - |
| 1226 | if (Q_UNLIKELY(num_glyphs == 0 || !ensureSpace(glyphs_shaped + num_glyphs))) { | - |
| 1227 | hb_buffer_destroy(buffer); | - |
| 1228 | return 0; | - |
| 1229 | } | - |
| 1230 | | - |
| 1231 | | - |
| 1232 | QGlyphLayout g = availableGlyphs(&si).mid(glyphs_shaped, num_glyphs); | - |
| 1233 | ushort *log_clusters = logClusters(&si) + item_pos; | - |
| 1234 | | - |
| 1235 | hb_glyph_info_t *infos = hb_buffer_get_glyph_infos(buffer, 0); | - |
| 1236 | hb_glyph_position_t *positions = hb_buffer_get_glyph_positions(buffer, 0); | - |
| 1237 | uint str_pos = 0; | - |
| 1238 | uint last_cluster = ~0u; | - |
| 1239 | uint last_glyph_pos = glyphs_shaped; | - |
| 1240 | for (uint i = 0; i < num_glyphs; ++i, ++infos, ++positions) { | - |
| 1241 | g.glyphs[i] = infos->codepoint; | - |
| 1242 | | - |
| 1243 | g.advances[i] = QFixed::fromFixed(positions->x_advance); | - |
| 1244 | g.offsets[i].x = QFixed::fromFixed(positions->x_offset); | - |
| 1245 | g.offsets[i].y = QFixed::fromFixed(positions->y_offset); | - |
| 1246 | | - |
| 1247 | uint cluster = infos->cluster; | - |
| 1248 | if (Q_LIKELY(last_cluster != cluster)) { | - |
| 1249 | g.attributes[i].clusterStart = true; | - |
| 1250 | | - |
| 1251 | | - |
| 1252 | | - |
| 1253 | while (last_cluster++ < cluster && str_pos < item_length) | - |
| 1254 | log_clusters[str_pos++] = last_glyph_pos; | - |
| 1255 | last_glyph_pos = i + glyphs_shaped; | - |
| 1256 | last_cluster = cluster; | - |
| 1257 | | - |
| 1258 | | - |
| 1259 | switch (string[item_pos + str_pos]) { | - |
| 1260 | case QChar::LineFeed: | - |
| 1261 | case 0x000c: | - |
| 1262 | case QChar::CarriageReturn: | - |
| 1263 | case QChar::LineSeparator: | - |
| 1264 | case QChar::ParagraphSeparator: | - |
| 1265 | g.attributes[i].dontPrint = true; | - |
| 1266 | break; | - |
| 1267 | case QChar::SoftHyphen: | - |
| 1268 | if (!actualFontEngine->symbol) { | - |
| 1269 | | - |
| 1270 | | - |
| 1271 | | - |
| 1272 | g.glyphs[i] = actualFontEngine->glyphIndex('-'); | - |
| 1273 | if (Q_LIKELY(g.glyphs[i] != 0)) { | - |
| 1274 | QGlyphLayout tmp = g.mid(i, 1); | - |
| 1275 | actualFontEngine->recalcAdvances(&tmp, 0); | - |
| 1276 | } | - |
| 1277 | g.attributes[i].dontPrint = true; | - |
| 1278 | } | - |
| 1279 | break; | - |
| 1280 | default: | - |
| 1281 | break; | - |
| 1282 | } | - |
| 1283 | } | - |
| 1284 | } | - |
| 1285 | while (str_pos < item_length) | - |
| 1286 | log_clusters[str_pos++] = last_glyph_pos; | - |
| 1287 | | - |
| 1288 | if (Q_UNLIKELY(engineIdx != 0)) { | - |
| 1289 | for (quint32 i = 0; i < num_glyphs; ++i) | - |
| 1290 | g.glyphs[i] |= (engineIdx << 24); | - |
| 1291 | } | - |
| 1292 | | - |
| 1293 | #ifdef Q_OS_DARWIN | - |
| 1294 | if (actualFontEngine->type() == QFontEngine::Mac) { | - |
| 1295 | | - |
| 1296 | | - |
| 1297 | if (QSysInfo::MacintoshVersion != QSysInfo::MV_10_6 && actualFontEngine->fontDef.stretch != 100) { | - |
| 1298 | QFixed stretch = QFixed(int(actualFontEngine->fontDef.stretch)) / QFixed(100); | - |
| 1299 | for (uint i = 0; i < num_glyphs; ++i) | - |
| 1300 | g.advances[i] *= stretch; | - |
| 1301 | } | - |
| 1302 | } | - |
| 1303 | #endif | - |
| 1304 | | - |
| 1305 | if (!actualFontEngine->supportsSubPixelPositions() || (actualFontEngine->fontDef.styleStrategy & QFont::ForceIntegerMetrics)) { | - |
| 1306 | for (uint i = 0; i < num_glyphs; ++i) | - |
| 1307 | g.advances[i] = g.advances[i].round(); | - |
| 1308 | } | - |
| 1309 | | - |
| 1310 | glyphs_shaped += num_glyphs; | - |
| 1311 | } | - |
| 1312 | | - |
| 1313 | hb_buffer_destroy(buffer); | - |
| 1314 | | - |
| 1315 | return glyphs_shaped; | - |
| 1316 | } | - |
| 1317 | | - |
| 1318 | #endif // QT_ENABLE_HARFBUZZ_NG | - |
| 1319 | | - |
| 1320 | | - |
| 1321 | QT_BEGIN_INCLUDE_NAMESPACE | - |
| 1322 | | - |
| 1323 | #include <private/qharfbuzz_p.h> | - |
| 1324 | | - |
| 1325 | QT_END_INCLUDE_NAMESPACE | - |
| 1326 | | - |
| 1327 | Q_STATIC_ASSERT(sizeof(HB_Glyph) == sizeof(glyph_t)); | - |
| 1328 | Q_STATIC_ASSERT(sizeof(HB_Fixed) == sizeof(QFixed)); | - |
| 1329 | Q_STATIC_ASSERT(sizeof(HB_FixedPoint) == sizeof(QFixedPoint)); | - |
| 1330 | | - |
| 1331 | static inline void moveGlyphData(const QGlyphLayout &destination, const QGlyphLayout &source, int num) | - |
| 1332 | { | - |
| 1333 | if (num > 0 && destination.glyphs != source.glyphs) | - |
| 1334 | memmove(destination.glyphs, source.glyphs, num * sizeof(glyph_t)); | - |
| 1335 | } | - |
| 1336 | | - |
| 1337 | int QTextEngine::shapeTextWithHarfbuzz(const QScriptItem &si, const ushort *string, int itemLength, QFontEngine *fontEngine, const QVector<uint> &itemBoundaries, bool kerningEnabled) const | - |
| 1338 | { | - |
| 1339 | HB_ShaperItem entire_shaper_item; | - |
| 1340 | memset(&entire_shaper_item, 0, sizeof(entire_shaper_item)); | - |
| 1341 | entire_shaper_item.string = reinterpret_cast<const HB_UChar16 *>(string); | - |
| 1342 | entire_shaper_item.stringLength = itemLength; | - |
| 1343 | entire_shaper_item.item.script = script_to_hbscript(si.analysis.script); | - |
| 1344 | entire_shaper_item.item.pos = 0; | - |
| 1345 | entire_shaper_item.item.length = itemLength; | - |
| 1346 | entire_shaper_item.item.bidiLevel = si.analysis.bidiLevel; | - |
| 1347 | | - |
| 1348 | entire_shaper_item.shaperFlags = 0; | - |
| 1349 | if (!kerningEnabled) | - |
| 1350 | entire_shaper_item.shaperFlags |= HB_ShaperFlag_NoKerning; | - |
| 1351 | if (option.useDesignMetrics()) | - |
| 1352 | entire_shaper_item.shaperFlags |= HB_ShaperFlag_UseDesignMetrics; | - |
| 1353 | | - |
| 1354 | | - |
| 1355 | entire_shaper_item.num_glyphs = 0; | - |
| 1356 | for (int i = 0; i < itemLength; ++i, ++entire_shaper_item.num_glyphs) { | - |
| 1357 | if (QChar::isHighSurrogate(string[i]) && i + 1 < itemLength && QChar::isLowSurrogate(string[i + 1])) | - |
| 1358 | ++i; | - |
| 1359 | } | - |
| 1360 | | - |
| 1361 | | - |
| 1362 | int remaining_glyphs = entire_shaper_item.num_glyphs; | - |
| 1363 | int glyph_pos = 0; | - |
| 1364 | | - |
| 1365 | for (int k = 0; k < itemBoundaries.size(); k += 3) { | - |
| 1366 | HB_ShaperItem shaper_item = entire_shaper_item; | - |
| 1367 | shaper_item.item.pos = itemBoundaries[k]; | - |
| 1368 | if (k + 4 < itemBoundaries.size()) { | - |
| 1369 | shaper_item.item.length = itemBoundaries[k + 3] - shaper_item.item.pos; | - |
| 1370 | shaper_item.num_glyphs = itemBoundaries[k + 4] - itemBoundaries[k + 1]; | - |
| 1371 | } else { | - |
| 1372 | shaper_item.item.length -= shaper_item.item.pos - entire_shaper_item.item.pos; | - |
| 1373 | shaper_item.num_glyphs -= itemBoundaries[k + 1]; | - |
| 1374 | } | - |
| 1375 | shaper_item.initialGlyphCount = shaper_item.num_glyphs; | - |
| 1376 | if (shaper_item.num_glyphs < shaper_item.item.length) | - |
| 1377 | shaper_item.num_glyphs = shaper_item.item.length; | - |
| 1378 | | - |
| 1379 | uint engineIdx = itemBoundaries[k + 2]; | - |
| 1380 | QFontEngine *actualFontEngine = fontEngine; | - |
| 1381 | if (fontEngine->type() == QFontEngine::Multi) { | - |
| 1382 | actualFontEngine = static_cast<QFontEngineMulti *>(fontEngine)->engine(engineIdx); | - |
| 1383 | | - |
| 1384 | if ((si.analysis.bidiLevel % 2) == 0) | - |
| 1385 | shaper_item.glyphIndicesPresent = true; | - |
| 1386 | } | - |
| 1387 | | - |
| 1388 | shaper_item.font = (HB_Font)actualFontEngine->harfbuzzFont(); | - |
| 1389 | shaper_item.face = (HB_Face)actualFontEngine->harfbuzzFace(); | - |
| 1390 | | - |
| 1391 | remaining_glyphs -= shaper_item.initialGlyphCount; | - |
| 1392 | | - |
| 1393 | QVarLengthArray<HB_GlyphAttributes, 128> hbGlyphAttributes; | - |
| 1394 | do { | - |
| 1395 | if (!ensureSpace(glyph_pos + shaper_item.num_glyphs + remaining_glyphs)) | - |
| 1396 | return 0; | - |
| 1397 | if (hbGlyphAttributes.size() < int(shaper_item.num_glyphs)) { | - |
| 1398 | hbGlyphAttributes.resize(shaper_item.num_glyphs); | - |
| 1399 | memset(hbGlyphAttributes.data(), 0, hbGlyphAttributes.size() * sizeof(HB_GlyphAttributes)); | - |
| 1400 | } | - |
| 1401 | | - |
| 1402 | const QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos); | - |
| 1403 | if (fontEngine->type() == QFontEngine::Multi && shaper_item.num_glyphs > shaper_item.item.length) | - |
| 1404 | moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs); | - |
| 1405 | | - |
| 1406 | shaper_item.glyphs = reinterpret_cast<HB_Glyph *>(g.glyphs); | - |
| 1407 | shaper_item.advances = reinterpret_cast<HB_Fixed *>(g.advances); | - |
| 1408 | shaper_item.offsets = reinterpret_cast<HB_FixedPoint *>(g.offsets); | - |
| 1409 | shaper_item.attributes = hbGlyphAttributes.data(); | - |
| 1410 | | - |
| 1411 | if (engineIdx != 0 && shaper_item.glyphIndicesPresent) { | - |
| 1412 | for (quint32 i = 0; i < shaper_item.initialGlyphCount; ++i) | - |
| 1413 | shaper_item.glyphs[i] &= 0x00ffffff; | - |
| 1414 | } | - |
| 1415 | | - |
| 1416 | shaper_item.log_clusters = logClusters(&si) + shaper_item.item.pos - entire_shaper_item.item.pos; | - |
| 1417 | } while (!qShapeItem(&shaper_item)); | - |
| 1418 | | - |
| 1419 | QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos, shaper_item.num_glyphs); | - |
| 1420 | if (fontEngine->type() == QFontEngine::Multi) | - |
| 1421 | moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs); | - |
| 1422 | | - |
| 1423 | for (quint32 i = 0; i < shaper_item.num_glyphs; ++i) { | - |
| 1424 | HB_GlyphAttributes hbAttrs = hbGlyphAttributes.at(i); | - |
| 1425 | QGlyphAttributes &attrs = g.attributes[i]; | - |
| 1426 | attrs.clusterStart = hbAttrs.clusterStart; | - |
| 1427 | attrs.dontPrint = hbAttrs.dontPrint; | - |
| 1428 | attrs.justification = hbAttrs.justification; | - |
| 1429 | } | - |
| 1430 | | - |
| 1431 | for (quint32 i = 0; i < shaper_item.item.length; ++i) { | - |
| 1432 | | - |
| 1433 | if (shaper_item.log_clusters[i] >= shaper_item.num_glyphs) | - |
| 1434 | shaper_item.log_clusters[i] = shaper_item.num_glyphs - 1; | - |
| 1435 | shaper_item.log_clusters[i] += glyph_pos; | - |
| 1436 | } | - |
| 1437 | | - |
| 1438 | if (kerningEnabled && !shaper_item.kerning_applied) | - |
| 1439 | actualFontEngine->doKerning(&g, option.useDesignMetrics() ? QFontEngine::DesignMetrics : QFontEngine::ShaperFlags(0)); | - |
| 1440 | | - |
| 1441 | if (engineIdx != 0) { | - |
| 1442 | for (quint32 i = 0; i < shaper_item.num_glyphs; ++i) | - |
| 1443 | g.glyphs[i] |= (engineIdx << 24); | - |
| 1444 | } | - |
| 1445 | | - |
| 1446 | glyph_pos += shaper_item.num_glyphs; | - |
| 1447 | } | - |
| 1448 | | - |
| 1449 | return glyph_pos; | - |
| 1450 | } | - |
| 1451 | | - |
| 1452 | void QTextEngine::init(QTextEngine *e) | - |
| 1453 | { | - |
| 1454 | e->ignoreBidi = false; | - |
| 1455 | e->cacheGlyphs = false; | - |
| 1456 | e->forceJustification = false; | - |
| 1457 | e->visualMovement = false; | - |
| 1458 | e->delayDecorations = false; | - |
| 1459 | | - |
| 1460 | e->layoutData = 0; | - |
| 1461 | | - |
| 1462 | e->minWidth = 0; | - |
| 1463 | e->maxWidth = 0; | - |
| 1464 | | - |
| 1465 | e->specialData = 0; | - |
| 1466 | e->stackEngine = false; | - |
| 1467 | #ifndef QT_NO_RAWFONT | - |
| 1468 | e->useRawFont = false; | - |
| 1469 | #endif | - |
| 1470 | } | - |
| 1471 | | - |
| 1472 | QTextEngine::QTextEngine() | - |
| 1473 | { | - |
| 1474 | init(this); | - |
| 1475 | } | - |
| 1476 | | - |
| 1477 | QTextEngine::QTextEngine(const QString &str, const QFont &f) | - |
| 1478 | : text(str), | - |
| 1479 | fnt(f) | - |
| 1480 | { | - |
| 1481 | init(this); | - |
| 1482 | } | - |
| 1483 | | - |
| 1484 | QTextEngine::~QTextEngine() | - |
| 1485 | { | - |
| 1486 | if (!stackEngine) | - |
| 1487 | delete layoutData; | - |
| 1488 | delete specialData; | - |
| 1489 | resetFontEngineCache(); | - |
| 1490 | } | - |
| 1491 | | - |
| 1492 | const QCharAttributes *QTextEngine::attributes() const | - |
| 1493 | { | - |
| 1494 | if (layoutData && layoutData->haveCharAttributes) | - |
| 1495 | return (QCharAttributes *) layoutData->memory; | - |
| 1496 | | - |
| 1497 | itemize(); | - |
| 1498 | if (! ensureSpace(layoutData->string.length())) | - |
| 1499 | return NULL; | - |
| 1500 | | - |
| 1501 | QVarLengthArray<QUnicodeTools::ScriptItem> scriptItems(layoutData->items.size()); | - |
| 1502 | for (int i = 0; i < layoutData->items.size(); ++i) { | - |
| 1503 | const QScriptItem &si = layoutData->items[i]; | - |
| 1504 | scriptItems[i].position = si.position; | - |
| 1505 | scriptItems[i].script = si.analysis.script; | - |
| 1506 | } | - |
| 1507 | | - |
| 1508 | QUnicodeTools::initCharAttributes(reinterpret_cast<const ushort *>(layoutData->string.constData()), | - |
| 1509 | layoutData->string.length(), | - |
| 1510 | scriptItems.data(), scriptItems.size(), | - |
| 1511 | (QCharAttributes *)layoutData->memory); | - |
| 1512 | | - |
| 1513 | | - |
| 1514 | layoutData->haveCharAttributes = true; | - |
| 1515 | return (QCharAttributes *) layoutData->memory; | - |
| 1516 | } | - |
| 1517 | | - |
| 1518 | void QTextEngine::shape(int item) const | - |
| 1519 | { | - |
| 1520 | if (layoutData->items[item].analysis.flags == QScriptAnalysis::Object) { | - |
| 1521 | ensureSpace(1); | - |
| 1522 | if (block.docHandle()) { | - |
| 1523 | docLayout()->resizeInlineObject(QTextInlineObject(item, const_cast<QTextEngine *>(this)), | - |
| 1524 | layoutData->items[item].position + block.position(), | - |
| 1525 | format(&layoutData->items[item])); | - |
| 1526 | } | - |
| 1527 | } else if (layoutData->items[item].analysis.flags == QScriptAnalysis::Tab) { | - |
| 1528 | | - |
| 1529 | fontEngine(layoutData->items[item], | - |
| 1530 | &layoutData->items[item].ascent, | - |
| 1531 | &layoutData->items[item].descent, | - |
| 1532 | &layoutData->items[item].leading); | - |
| 1533 | } else { | - |
| 1534 | shapeText(item); | - |
| 1535 | } | - |
| 1536 | } | - |
| 1537 | | - |
| 1538 | static inline void releaseCachedFontEngine(QFontEngine *fontEngine) | - |
| 1539 | { | - |
| 1540 | if (fontEngine && !fontEngine->ref.deref()) | - |
| 1541 | delete fontEngine; | - |
| 1542 | } | - |
| 1543 | | - |
| 1544 | void QTextEngine::resetFontEngineCache() | - |
| 1545 | { | - |
| 1546 | releaseCachedFontEngine(feCache.prevFontEngine); | - |
| 1547 | releaseCachedFontEngine(feCache.prevScaledFontEngine); | - |
| 1548 | feCache.reset(); | - |
| 1549 | } | - |
| 1550 | | - |
| 1551 | void QTextEngine::invalidate() | - |
| 1552 | { | - |
| 1553 | freeMemory(); | - |
| 1554 | minWidth = 0; | - |
| 1555 | maxWidth = 0; | - |
| 1556 | | - |
| 1557 | resetFontEngineCache(); | - |
| 1558 | } | - |
| 1559 | | - |
| 1560 | void QTextEngine::clearLineData() | - |
| 1561 | { | - |
| 1562 | lines.clear(); | - |
| 1563 | } | - |
| 1564 | | - |
| 1565 | void QTextEngine::validate() const | - |
| 1566 | { | - |
| 1567 | if (layoutData)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1568 | return; never executed: return; | 0 |
| 1569 | layoutData = new LayoutData(); | - |
| 1570 | if (block.docHandle()) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1571 | layoutData->string = block.text(); | - |
| 1572 | const bool nextBlockValid = block.next().isValid(); | - |
| 1573 | if (!nextBlockValid && option.flags() & QTextOption::ShowDocumentTerminator) {| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1574 | layoutData->string += QChar(0xA7); | - |
| 1575 | } else never executed: end of block | TRUE | never evaluated | | FALSE | never evaluated |
if (option.flags() & QTextOption::ShowLineAndParagraphSeparators) {never executed: end of block | TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1576 | layoutData->string += QLatin1Char(block.next().isValid()nextBlockValid ? 0xb6 : 0x20); | - |
| 1577 | } never executed: end of block | 0 |
| 1578 | | - |
| 1579 | } else { never executed: end of block | 0 |
| 1580 | layoutData->string = text; | - |
| 1581 | } never executed: end of block | 0 |
| 1582 | if (specialData && specialData->preeditPosition != -1)| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 1583 | layoutData->string.insert(specialData->preeditPosition, specialData->preeditText); never executed: layoutData->string.insert(specialData->preeditPosition, specialData->preeditText); | 0 |
| 1584 | } never executed: end of block | 0 |
| 1585 | | - |
| 1586 | void QTextEngine::itemize() const | - |
| 1587 | { | - |
| 1588 | validate(); | - |
| 1589 | if (layoutData->items.size()) | - |
| 1590 | return; | - |
| 1591 | | - |
| 1592 | int length = layoutData->string.length(); | - |
| 1593 | if (!length) | - |
| 1594 | return; | - |
| 1595 | | - |
| 1596 | const ushort *string = reinterpret_cast<const ushort *>(layoutData->string.unicode()); | - |
| 1597 | | - |
| 1598 | bool ignore = ignoreBidi; | - |
| 1599 | | - |
| 1600 | bool rtl = isRightToLeft(); | - |
| 1601 | | - |
| 1602 | if (!ignore && !rtl) { | - |
| 1603 | ignore = true; | - |
| 1604 | const QChar *start = layoutData->string.unicode(); | - |
| 1605 | const QChar * const end = start + length; | - |
| 1606 | while (start < end) { | - |
| 1607 | if (start->unicode() >= 0x590) { | - |
| 1608 | ignore = false; | - |
| 1609 | break; | - |
| 1610 | } | - |
| 1611 | ++start; | - |
| 1612 | } | - |
| 1613 | } | - |
| 1614 | | - |
| 1615 | QVarLengthArray<QScriptAnalysis, 4096> scriptAnalysis(length); | - |
| 1616 | QScriptAnalysis *analysis = scriptAnalysis.data(); | - |
| 1617 | | - |
| 1618 | QBidiControl control(rtl); | - |
| 1619 | | - |
| 1620 | if (ignore) { | - |
| 1621 | memset(analysis, 0, length*sizeof(QScriptAnalysis)); | - |
| 1622 | if (option.textDirection() == Qt::RightToLeft) { | - |
| 1623 | for (int i = 0; i < length; ++i) | - |
| 1624 | analysis[i].bidiLevel = 1; | - |
| 1625 | layoutData->hasBidi = true; | - |
| 1626 | } | - |
| 1627 | } else { | - |
| 1628 | layoutData->hasBidi = bidiItemize(const_cast<QTextEngine *>(this), analysis, control); | - |
| 1629 | } | - |
| 1630 | | - |
| 1631 | { | - |
| 1632 | QVarLengthArray<uchar> scripts(length); | - |
| 1633 | QUnicodeTools::initScripts(string, length, scripts.data()); | - |
| 1634 | for (int i = 0; i < length; ++i) | - |
| 1635 | analysis[i].script = scripts.at(i); | - |
| 1636 | } | - |
| 1637 | | - |
| 1638 | const ushort *uc = string; | - |
| 1639 | const ushort *e = uc + length; | - |
| 1640 | while (uc < e) { | - |
| 1641 | switch (*uc) { | - |
| 1642 | case QChar::ObjectReplacementCharacter: | - |
| 1643 | analysis->flags = QScriptAnalysis::Object; | - |
| 1644 | break; | - |
| 1645 | case QChar::LineSeparator: | - |
| 1646 | if (analysis->bidiLevel % 2) | - |
| 1647 | --analysis->bidiLevel; | - |
| 1648 | analysis->flags = QScriptAnalysis::LineOrParagraphSeparator; | - |
| 1649 | if (option.flags() & QTextOption::ShowLineAndParagraphSeparators) { | - |
| 1650 | const int offset = uc - string; | - |
| 1651 | layoutData->string.detach(); | - |
| 1652 | string = reinterpret_cast<const ushort *>(layoutData->string.unicode()); | - |
| 1653 | uc = string + offset; | - |
| 1654 | e = uc + length; | - |
| 1655 | *const_cast<ushort*>(uc) = 0x21B5; | - |
| 1656 | } | - |
| 1657 | break; | - |
| 1658 | case QChar::Tabulation: | - |
| 1659 | analysis->flags = QScriptAnalysis::Tab; | - |
| 1660 | analysis->bidiLevel = control.baseLevel(); | - |
| 1661 | break; | - |
| 1662 | case QChar::Space: | - |
| 1663 | case QChar::Nbsp: | - |
| 1664 | if (option.flags() & QTextOption::ShowTabsAndSpaces) { | - |
| 1665 | analysis->flags = QScriptAnalysis::Space; | - |
| 1666 | analysis->bidiLevel = control.baseLevel(); | - |
| 1667 | break; | - |
| 1668 | } | - |
| 1669 | | - |
| 1670 | default: | - |
| 1671 | analysis->flags = QScriptAnalysis::None; | - |
| 1672 | break; | - |
| 1673 | } | - |
| 1674 | #ifndef QT_ENABLE_HARFBUZZ_NG | - |
| 1675 | analysis->script = hbscript_to_script(script_to_hbscript(analysis->script)); | - |
| 1676 | #endif | - |
| 1677 | ++uc; | - |
| 1678 | ++analysis; | - |
| 1679 | } | - |
| 1680 | if (option.flags() & QTextOption::ShowLineAndParagraphSeparators) { | - |
| 1681 | (analysis-1)->flags = QScriptAnalysis::LineOrParagraphSeparator; | - |
| 1682 | } | - |
| 1683 | #ifdef QT_ENABLE_HARFBUZZ_NG | - |
| 1684 | analysis = scriptAnalysis.data(); | - |
| 1685 | if (qt_useHarfbuzzNG()) { | - |
| 1686 | | - |
| 1687 | for (int i = 0; i < length; ++i) { | - |
| 1688 | switch (analysis[i].script) { | - |
| 1689 | case QChar::Script_Latin: | - |
| 1690 | case QChar::Script_Hiragana: | - |
| 1691 | case QChar::Script_Katakana: | - |
| 1692 | case QChar::Script_Bopomofo: | - |
| 1693 | case QChar::Script_Han: | - |
| 1694 | analysis[i].script = QChar::Script_Common; | - |
| 1695 | break; | - |
| 1696 | default: | - |
| 1697 | break; | - |
| 1698 | } | - |
| 1699 | } | - |
| 1700 | } else { | - |
| 1701 | for (int i = 0; i < length; ++i) | - |
| 1702 | analysis[i].script = hbscript_to_script(script_to_hbscript(analysis[i].script)); | - |
| 1703 | } | - |
| 1704 | #endif | - |
| 1705 | | - |
| 1706 | Itemizer itemizer(layoutData->string, scriptAnalysis.data(), layoutData->items); | - |
| 1707 | | - |
| 1708 | const QTextDocumentPrivate *p = block.docHandle(); | - |
| 1709 | if (p) { | - |
| 1710 | SpecialData *s = specialData; | - |
| 1711 | | - |
| 1712 | QTextDocumentPrivate::FragmentIterator it = p->find(block.position()); | - |
| 1713 | QTextDocumentPrivate::FragmentIterator end = p->find(block.position() + block.length() - 1); | - |
| 1714 | int format = it.value()->format; | - |
| 1715 | | - |
| 1716 | int prevPosition = 0; | - |
| 1717 | int position = prevPosition; | - |
| 1718 | while (1) { | - |
| 1719 | const QTextFragmentData * const frag = it.value(); | - |
| 1720 | if (it == end || format != frag->format) { | - |
| 1721 | if (s && position >= s->preeditPosition) { | - |
| 1722 | position += s->preeditText.length(); | - |
| 1723 | s = 0; | - |
| 1724 | } | - |
| 1725 | Q_ASSERT(position <= length); | - |
| 1726 | QFont::Capitalization capitalization = | - |
| 1727 | formatCollection()->charFormat(format).hasProperty(QTextFormat::FontCapitalization) | - |
| 1728 | ? formatCollection()->charFormat(format).fontCapitalization() | - |
| 1729 | : formatCollection()->defaultFont().capitalization(); | - |
| 1730 | itemizer.generate(prevPosition, position - prevPosition, capitalization); | - |
| 1731 | if (it == end) { | - |
| 1732 | if (position < length) | - |
| 1733 | itemizer.generate(position, length - position, capitalization); | - |
| 1734 | break; | - |
| 1735 | } | - |
| 1736 | format = frag->format; | - |
| 1737 | prevPosition = position; | - |
| 1738 | } | - |
| 1739 | position += frag->size_array[0]; | - |
| 1740 | ++it; | - |
| 1741 | } | - |
| 1742 | } else { | - |
| 1743 | #ifndef QT_NO_RAWFONT | - |
| 1744 | if (useRawFont && specialData) { | - |
| 1745 | int lastIndex = 0; | - |
| 1746 | for (int i = 0; i < specialData->formats.size(); ++i) { | - |
| 1747 | const QTextLayout::FormatRange &range = specialData->formats.at(i); | - |
| 1748 | const QTextCharFormat &format = range.format; | - |
| 1749 | if (format.hasProperty(QTextFormat::FontCapitalization)) { | - |
| 1750 | itemizer.generate(lastIndex, range.start - lastIndex, QFont::MixedCase); | - |
| 1751 | itemizer.generate(range.start, range.length, format.fontCapitalization()); | - |
| 1752 | lastIndex = range.start + range.length; | - |
| 1753 | } | - |
| 1754 | } | - |
| 1755 | itemizer.generate(lastIndex, length - lastIndex, QFont::MixedCase); | - |
| 1756 | } else | - |
| 1757 | #endif | - |
| 1758 | itemizer.generate(0, length, static_cast<QFont::Capitalization> (fnt.d->capital)); | - |
| 1759 | } | - |
| 1760 | | - |
| 1761 | addRequiredBoundaries(); | - |
| 1762 | resolveFormats(); | - |
| 1763 | } | - |
| 1764 | | - |
| 1765 | bool QTextEngine::isRightToLeft() const | - |
| 1766 | { | - |
| 1767 | switch (option.textDirection()) { | - |
| 1768 | case Qt::LeftToRight: | - |
| 1769 | return false; | - |
| 1770 | case Qt::RightToLeft: | - |
| 1771 | return true; | - |
| 1772 | default: | - |
| 1773 | break; | - |
| 1774 | } | - |
| 1775 | if (!layoutData) | - |
| 1776 | itemize(); | - |
| 1777 | | - |
| 1778 | if (layoutData->string.isEmpty()) | - |
| 1779 | return QGuiApplication::inputMethod()->inputDirection() == Qt::RightToLeft; | - |
| 1780 | return layoutData->string.isRightToLeft(); | - |
| 1781 | } | - |
| 1782 | | - |
| 1783 | | - |
| 1784 | int QTextEngine::findItem(int strPos, int firstItem) const | - |
| 1785 | { | - |
| 1786 | itemize(); | - |
| 1787 | if (strPos < 0 || strPos >= layoutData->string.size() || firstItem < 0) | - |
| 1788 | return -1; | - |
| 1789 | | - |
| 1790 | int left = firstItem + 1; | - |
| 1791 | int right = layoutData->items.size()-1; | - |
| 1792 | while(left <= right) { | - |
| 1793 | int middle = ((right-left)/2)+left; | - |
| 1794 | if (strPos > layoutData->items[middle].position) | - |
| 1795 | left = middle+1; | - |
| 1796 | else if(strPos < layoutData->items[middle].position) | - |
| 1797 | right = middle-1; | - |
| 1798 | else { | - |
| 1799 | return middle; | - |
| 1800 | } | - |
| 1801 | } | - |
| 1802 | return right; | - |
| 1803 | } | - |
| 1804 | | - |
| 1805 | QFixed QTextEngine::width(int from, int len) const | - |
| 1806 | { | - |
| 1807 | itemize(); | - |
| 1808 | | - |
| 1809 | QFixed w = 0; | - |
| 1810 | | - |
| 1811 | | - |
| 1812 | for (int i = 0; i < layoutData->items.size(); i++) { | - |
| 1813 | const QScriptItem *si = layoutData->items.constData() + i; | - |
| 1814 | int pos = si->position; | - |
| 1815 | int ilen = length(i); | - |
| 1816 | | - |
| 1817 | if (pos >= from + len) | - |
| 1818 | break; | - |
| 1819 | if (pos + ilen > from) { | - |
| 1820 | if (!si->num_glyphs) | - |
| 1821 | shape(i); | - |
| 1822 | | - |
| 1823 | if (si->analysis.flags == QScriptAnalysis::Object) { | - |
| 1824 | w += si->width; | - |
| 1825 | continue; | - |
| 1826 | } else if (si->analysis.flags == QScriptAnalysis::Tab) { | - |
| 1827 | w += calculateTabWidth(i, w); | - |
| 1828 | continue; | - |
| 1829 | } | - |
| 1830 | | - |
| 1831 | | - |
| 1832 | QGlyphLayout glyphs = shapedGlyphs(si); | - |
| 1833 | unsigned short *logClusters = this->logClusters(si); | - |
| 1834 | | - |
| 1835 | | - |
| 1836 | | - |
| 1837 | | - |
| 1838 | | - |
| 1839 | | - |
| 1840 | int charFrom = from - pos; | - |
| 1841 | if (charFrom < 0) | - |
| 1842 | charFrom = 0; | - |
| 1843 | int glyphStart = logClusters[charFrom]; | - |
| 1844 | if (charFrom > 0 && logClusters[charFrom-1] == glyphStart) | - |
| 1845 | while (charFrom < ilen && logClusters[charFrom] == glyphStart) | - |
| 1846 | charFrom++; | - |
| 1847 | if (charFrom < ilen) { | - |
| 1848 | glyphStart = logClusters[charFrom]; | - |
| 1849 | int charEnd = from + len - 1 - pos; | - |
| 1850 | if (charEnd >= ilen) | - |
| 1851 | charEnd = ilen-1; | - |
| 1852 | int glyphEnd = logClusters[charEnd]; | - |
| 1853 | while (charEnd < ilen && logClusters[charEnd] == glyphEnd) | - |
| 1854 | charEnd++; | - |
| 1855 | glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd]; | - |
| 1856 | | - |
| 1857 | | - |
| 1858 | for (int i = glyphStart; i < glyphEnd; i++) | - |
| 1859 | w += glyphs.advances[i] * !glyphs.attributes[i].dontPrint; | - |
| 1860 | } | - |
| 1861 | } | - |
| 1862 | } | - |
| 1863 | | - |
| 1864 | return w; | - |
| 1865 | } | - |
| 1866 | | - |
| 1867 | glyph_metrics_t QTextEngine::boundingBox(int from, int len) const | - |
| 1868 | { | - |
| 1869 | itemize(); | - |
| 1870 | | - |
| 1871 | glyph_metrics_t gm; | - |
| 1872 | | - |
| 1873 | for (int i = 0; i < layoutData->items.size(); i++) { | - |
| 1874 | const QScriptItem *si = layoutData->items.constData() + i; | - |
| 1875 | | - |
| 1876 | int pos = si->position; | - |
| 1877 | int ilen = length(i); | - |
| 1878 | if (pos > from + len) | - |
| 1879 | break; | - |
| 1880 | if (pos + ilen > from) { | - |
| 1881 | if (!si->num_glyphs) | - |
| 1882 | shape(i); | - |
| 1883 | | - |
| 1884 | if (si->analysis.flags == QScriptAnalysis::Object) { | - |
| 1885 | gm.width += si->width; | - |
| 1886 | continue; | - |
| 1887 | } else if (si->analysis.flags == QScriptAnalysis::Tab) { | - |
| 1888 | gm.width += calculateTabWidth(i, gm.width); | - |
| 1889 | continue; | - |
| 1890 | } | - |
| 1891 | | - |
| 1892 | unsigned short *logClusters = this->logClusters(si); | - |
| 1893 | QGlyphLayout glyphs = shapedGlyphs(si); | - |
| 1894 | | - |
| 1895 | | - |
| 1896 | int charFrom = from - pos; | - |
| 1897 | if (charFrom < 0) | - |
| 1898 | charFrom = 0; | - |
| 1899 | int glyphStart = logClusters[charFrom]; | - |
| 1900 | if (charFrom > 0 && logClusters[charFrom-1] == glyphStart) | - |
| 1901 | while (charFrom < ilen && logClusters[charFrom] == glyphStart) | - |
| 1902 | charFrom++; | - |
| 1903 | if (charFrom < ilen) { | - |
| 1904 | QFontEngine *fe = fontEngine(*si); | - |
| 1905 | glyphStart = logClusters[charFrom]; | - |
| 1906 | int charEnd = from + len - 1 - pos; | - |
| 1907 | if (charEnd >= ilen) | - |
| 1908 | charEnd = ilen-1; | - |
| 1909 | int glyphEnd = logClusters[charEnd]; | - |
| 1910 | while (charEnd < ilen && logClusters[charEnd] == glyphEnd) | - |
| 1911 | charEnd++; | - |
| 1912 | glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd]; | - |
| 1913 | if (glyphStart <= glyphEnd ) { | - |
| 1914 | glyph_metrics_t m = fe->boundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart)); | - |
| 1915 | gm.x = qMin(gm.x, m.x + gm.xoff); | - |
| 1916 | gm.y = qMin(gm.y, m.y + gm.yoff); | - |
| 1917 | gm.width = qMax(gm.width, m.width+gm.xoff); | - |
| 1918 | gm.height = qMax(gm.height, m.height+gm.yoff); | - |
| 1919 | gm.xoff += m.xoff; | - |
| 1920 | gm.yoff += m.yoff; | - |
| 1921 | } | - |
| 1922 | } | - |
| 1923 | } | - |
| 1924 | } | - |
| 1925 | return gm; | - |
| 1926 | } | - |
| 1927 | | - |
| 1928 | glyph_metrics_t QTextEngine::tightBoundingBox(int from, int len) const | - |
| 1929 | { | - |
| 1930 | itemize(); | - |
| 1931 | | - |
| 1932 | glyph_metrics_t gm; | - |
| 1933 | | - |
| 1934 | for (int i = 0; i < layoutData->items.size(); i++) { | - |
| 1935 | const QScriptItem *si = layoutData->items.constData() + i; | - |
| 1936 | int pos = si->position; | - |
| 1937 | int ilen = length(i); | - |
| 1938 | if (pos > from + len) | - |
| 1939 | break; | - |
| 1940 | if (pos + len > from) { | - |
| 1941 | if (!si->num_glyphs) | - |
| 1942 | shape(i); | - |
| 1943 | unsigned short *logClusters = this->logClusters(si); | - |
| 1944 | QGlyphLayout glyphs = shapedGlyphs(si); | - |
| 1945 | | - |
| 1946 | | - |
| 1947 | int charFrom = from - pos; | - |
| 1948 | if (charFrom < 0) | - |
| 1949 | charFrom = 0; | - |
| 1950 | int glyphStart = logClusters[charFrom]; | - |
| 1951 | if (charFrom > 0 && logClusters[charFrom-1] == glyphStart) | - |
| 1952 | while (charFrom < ilen && logClusters[charFrom] == glyphStart) | - |
| 1953 | charFrom++; | - |
| 1954 | if (charFrom < ilen) { | - |
| 1955 | glyphStart = logClusters[charFrom]; | - |
| 1956 | int charEnd = from + len - 1 - pos; | - |
| 1957 | if (charEnd >= ilen) | - |
| 1958 | charEnd = ilen-1; | - |
| 1959 | int glyphEnd = logClusters[charEnd]; | - |
| 1960 | while (charEnd < ilen && logClusters[charEnd] == glyphEnd) | - |
| 1961 | charEnd++; | - |
| 1962 | glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd]; | - |
| 1963 | if (glyphStart <= glyphEnd ) { | - |
| 1964 | QFontEngine *fe = fontEngine(*si); | - |
| 1965 | glyph_metrics_t m = fe->tightBoundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart)); | - |
| 1966 | gm.x = qMin(gm.x, m.x + gm.xoff); | - |
| 1967 | gm.y = qMin(gm.y, m.y + gm.yoff); | - |
| 1968 | gm.width = qMax(gm.width, m.width+gm.xoff); | - |
| 1969 | gm.height = qMax(gm.height, m.height+gm.yoff); | - |
| 1970 | gm.xoff += m.xoff; | - |
| 1971 | gm.yoff += m.yoff; | - |
| 1972 | } | - |
| 1973 | } | - |
| 1974 | } | - |
| 1975 | } | - |
| 1976 | return gm; | - |
| 1977 | } | - |
| 1978 | | - |
| 1979 | QFont QTextEngine::font(const QScriptItem &si) const | - |
| 1980 | { | - |
| 1981 | QFont font = fnt; | - |
| 1982 | if (hasFormats()) { | - |
| 1983 | QTextCharFormat f = format(&si); | - |
| 1984 | font = f.font(); | - |
| 1985 | | - |
| 1986 | if (block.docHandle() && block.docHandle()->layout()) { | - |
| 1987 | | - |
| 1988 | QPaintDevice *pdev = block.docHandle()->layout()->paintDevice(); | - |
| 1989 | if (pdev) | - |
| 1990 | font = QFont(font, pdev); | - |
| 1991 | } else { | - |
| 1992 | font = font.resolve(fnt); | - |
| 1993 | } | - |
| 1994 | QTextCharFormat::VerticalAlignment valign = f.verticalAlignment(); | - |
| 1995 | if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) { | - |
| 1996 | if (font.pointSize() != -1) | - |
| 1997 | font.setPointSize((font.pointSize() * 2) / 3); | - |
| 1998 | else | - |
| 1999 | font.setPixelSize((font.pixelSize() * 2) / 3); | - |
| 2000 | } | - |
| 2001 | } | - |
| 2002 | | - |
| 2003 | if (si.analysis.flags == QScriptAnalysis::SmallCaps) | - |
| 2004 | font = font.d->smallCapsFont(); | - |
| 2005 | | - |
| 2006 | return font; | - |
| 2007 | } | - |
| 2008 | | - |
| 2009 | QTextEngine::FontEngineCache::FontEngineCache() | - |
| 2010 | { | - |
| 2011 | reset(); | - |
| 2012 | } | - |
| 2013 | | - |
| 2014 | | - |
| 2015 | | - |
| 2016 | QFontEngine *QTextEngine::fontEngine(const QScriptItem &si, QFixed *ascent, QFixed *descent, QFixed *leading) const | - |
| 2017 | { | - |
| 2018 | QFontEngine *engine = 0; | - |
| 2019 | QFontEngine *scaledEngine = 0; | - |
| 2020 | int script = si.analysis.script; | - |
| 2021 | | - |
| 2022 | QFont font = fnt; | - |
| 2023 | #ifndef QT_NO_RAWFONT | - |
| 2024 | if (useRawFont && rawFont.isValid()) { | - |
| 2025 | if (feCache.prevFontEngine && feCache.prevFontEngine->type() == QFontEngine::Multi && feCache.prevScript == script) { | - |
| 2026 | engine = feCache.prevFontEngine; | - |
| 2027 | } else { | - |
| 2028 | engine = QFontEngineMulti::createMultiFontEngine(rawFont.d->fontEngine, script); | - |
| 2029 | feCache.prevFontEngine = engine; | - |
| 2030 | feCache.prevScript = script; | - |
| 2031 | engine->ref.ref(); | - |
| 2032 | if (feCache.prevScaledFontEngine) { | - |
| 2033 | releaseCachedFontEngine(feCache.prevScaledFontEngine); | - |
| 2034 | feCache.prevScaledFontEngine = 0; | - |
| 2035 | } | - |
| 2036 | } | - |
| 2037 | if (si.analysis.flags == QScriptAnalysis::SmallCaps) { | - |
| 2038 | if (feCache.prevScaledFontEngine) { | - |
| 2039 | scaledEngine = feCache.prevScaledFontEngine; | - |
| 2040 | } else { | - |
| 2041 | QFontEngine *scEngine = rawFont.d->fontEngine->cloneWithSize(smallCapsFraction * rawFont.pixelSize()); | - |
| 2042 | scEngine->ref.ref(); | - |
| 2043 | scaledEngine = QFontEngineMulti::createMultiFontEngine(scEngine, script); | - |
| 2044 | scaledEngine->ref.ref(); | - |
| 2045 | feCache.prevScaledFontEngine = scaledEngine; | - |
| 2046 | | - |
| 2047 | if (!scEngine->ref.deref()) | - |
| 2048 | delete scEngine; | - |
| 2049 | | - |
| 2050 | } | - |
| 2051 | } | - |
| 2052 | } else | - |
| 2053 | #endif | - |
| 2054 | { | - |
| 2055 | if (hasFormats()) { | - |
| 2056 | if (feCache.prevFontEngine && feCache.prevPosition == si.position && feCache.prevLength == length(&si) && feCache.prevScript == script) { | - |
| 2057 | engine = feCache.prevFontEngine; | - |
| 2058 | scaledEngine = feCache.prevScaledFontEngine; | - |
| 2059 | } else { | - |
| 2060 | QTextCharFormat f = format(&si); | - |
| 2061 | font = f.font(); | - |
| 2062 | | - |
| 2063 | if (block.docHandle() && block.docHandle()->layout()) { | - |
| 2064 | | - |
| 2065 | QPaintDevice *pdev = block.docHandle()->layout()->paintDevice(); | - |
| 2066 | if (pdev) | - |
| 2067 | font = QFont(font, pdev); | - |
| 2068 | } else { | - |
| 2069 | font = font.resolve(fnt); | - |
| 2070 | } | - |
| 2071 | engine = font.d->engineForScript(script); | - |
| 2072 | if (engine) | - |
| 2073 | engine->ref.ref(); | - |
| 2074 | | - |
| 2075 | QTextCharFormat::VerticalAlignment valign = f.verticalAlignment(); | - |
| 2076 | if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) { | - |
| 2077 | if (font.pointSize() != -1) | - |
| 2078 | font.setPointSize((font.pointSize() * 2) / 3); | - |
| 2079 | else | - |
| 2080 | font.setPixelSize((font.pixelSize() * 2) / 3); | - |
| 2081 | scaledEngine = font.d->engineForScript(script); | - |
| 2082 | if (scaledEngine) | - |
| 2083 | scaledEngine->ref.ref(); | - |
| 2084 | } | - |
| 2085 | | - |
| 2086 | if (feCache.prevFontEngine) | - |
| 2087 | releaseCachedFontEngine(feCache.prevFontEngine); | - |
| 2088 | feCache.prevFontEngine = engine; | - |
| 2089 | | - |
| 2090 | if (feCache.prevScaledFontEngine) | - |
| 2091 | releaseCachedFontEngine(feCache.prevScaledFontEngine); | - |
| 2092 | feCache.prevScaledFontEngine = scaledEngine; | - |
| 2093 | | - |
| 2094 | feCache.prevScript = script; | - |
| 2095 | feCache.prevPosition = si.position; | - |
| 2096 | feCache.prevLength = length(&si); | - |
| 2097 | } | - |
| 2098 | } else { | - |
| 2099 | if (feCache.prevFontEngine && feCache.prevScript == script && feCache.prevPosition == -1) | - |
| 2100 | engine = feCache.prevFontEngine; | - |
| 2101 | else { | - |
| 2102 | engine = font.d->engineForScript(script); | - |
| 2103 | | - |
| 2104 | if (engine) | - |
| 2105 | engine->ref.ref(); | - |
| 2106 | if (feCache.prevFontEngine) | - |
| 2107 | releaseCachedFontEngine(feCache.prevFontEngine); | - |
| 2108 | feCache.prevFontEngine = engine; | - |
| 2109 | | - |
| 2110 | feCache.prevScript = script; | - |
| 2111 | feCache.prevPosition = -1; | - |
| 2112 | feCache.prevLength = -1; | - |
| 2113 | feCache.prevScaledFontEngine = 0; | - |
| 2114 | } | - |
| 2115 | } | - |
| 2116 | | - |
| 2117 | if (si.analysis.flags == QScriptAnalysis::SmallCaps) { | - |
| 2118 | QFontPrivate *p = font.d->smallCapsFontPrivate(); | - |
| 2119 | scaledEngine = p->engineForScript(script); | - |
| 2120 | } | - |
| 2121 | } | - |
| 2122 | | - |
| 2123 | if (ascent) { | - |
| 2124 | *ascent = engine->ascent(); | - |
| 2125 | *descent = engine->descent(); | - |
| 2126 | *leading = engine->leading(); | - |
| 2127 | } | - |
| 2128 | | - |
| 2129 | if (scaledEngine) | - |
| 2130 | return scaledEngine; | - |
| 2131 | return engine; | - |
| 2132 | } | - |
| 2133 | | - |
| 2134 | struct QJustificationPoint { | - |
| 2135 | int type; | - |
| 2136 | QFixed kashidaWidth; | - |
| 2137 | QGlyphLayout glyph; | - |
| 2138 | }; | - |
| 2139 | | - |
| 2140 | Q_DECLARE_TYPEINFO(QJustificationPoint, Q_PRIMITIVE_TYPE); | - |
| 2141 | | - |
| 2142 | static void set(QJustificationPoint *point, int type, const QGlyphLayout &glyph, QFontEngine *fe) | - |
| 2143 | { | - |
| 2144 | point->type = type; | - |
| 2145 | point->glyph = glyph; | - |
| 2146 | | - |
| 2147 | if (type >= Justification_Arabic_Normal) { | - |
| 2148 | QChar ch(0x640); | - |
| 2149 | | - |
| 2150 | glyph_t kashidaGlyph = fe->glyphIndex(ch.unicode()); | - |
| 2151 | if (kashidaGlyph != 0) { | - |
| 2152 | QGlyphLayout g; | - |
| 2153 | g.numGlyphs = 1; | - |
| 2154 | g.glyphs = &kashidaGlyph; | - |
| 2155 | g.advances = &point->kashidaWidth; | - |
| 2156 | fe->recalcAdvances(&g, 0); | - |
| 2157 | | - |
| 2158 | if (point->kashidaWidth == 0) | - |
| 2159 | point->type = Justification_Prohibited; | - |
| 2160 | } else { | - |
| 2161 | point->type = Justification_Prohibited; | - |
| 2162 | point->kashidaWidth = 0; | - |
| 2163 | } | - |
| 2164 | } | - |
| 2165 | } | - |
| 2166 | | - |
| 2167 | | - |
| 2168 | void QTextEngine::justify(const QScriptLine &line) | - |
| 2169 | { | - |
| 2170 | | - |
| 2171 | if (line.gridfitted && line.justified) | - |
| 2172 | return; | - |
| 2173 | | - |
| 2174 | if (!line.gridfitted) { | - |
| 2175 | | - |
| 2176 | const_cast<QScriptLine &>(line).gridfitted = true; | - |
| 2177 | } | - |
| 2178 | | - |
| 2179 | if ((option.alignment() & Qt::AlignHorizontal_Mask) != Qt::AlignJustify) | - |
| 2180 | return; | - |
| 2181 | | - |
| 2182 | itemize(); | - |
| 2183 | | - |
| 2184 | if (!forceJustification) { | - |
| 2185 | int end = line.from + (int)line.length + line.trailingSpaces; | - |
| 2186 | if (end == layoutData->string.length()) | - |
| 2187 | return; | - |
| 2188 | if (end && layoutData->items[findItem(end-1)].analysis.flags == QScriptAnalysis::LineOrParagraphSeparator) | - |
| 2189 | return; | - |
| 2190 | } | - |
| 2191 | | - |
| 2192 | | - |
| 2193 | int maxJustify = 0; | - |
| 2194 | | - |
| 2195 | | - |
| 2196 | int line_length = line.length; | - |
| 2197 | const QCharAttributes *a = attributes(); | - |
| 2198 | if (! a) | - |
| 2199 | return; | - |
| 2200 | a += line.from; | - |
| 2201 | while (line_length && a[line_length-1].whiteSpace) | - |
| 2202 | --line_length; | - |
| 2203 | | - |
| 2204 | --line_length; | - |
| 2205 | | - |
| 2206 | if (line_length <= 0) | - |
| 2207 | return; | - |
| 2208 | | - |
| 2209 | int firstItem = findItem(line.from); | - |
| 2210 | int lastItem = findItem(line.from + line_length - 1, firstItem); | - |
| 2211 | int nItems = (firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0; | - |
| 2212 | | - |
| 2213 | QVarLengthArray<QJustificationPoint> justificationPoints; | - |
| 2214 | int nPoints = 0; | - |
| 2215 | | - |
| 2216 | QFixed minKashida = 0x100000; | - |
| 2217 | | - |
| 2218 | | - |
| 2219 | | - |
| 2220 | | - |
| 2221 | for (int i = 0; i < nItems; ++i) { | - |
| 2222 | QScriptItem &si = layoutData->items[firstItem + i]; | - |
| 2223 | if (!si.num_glyphs) | - |
| 2224 | shape(firstItem + i); | - |
| 2225 | } | - |
| 2226 | | - |
| 2227 | for (int i = 0; i < nItems; ++i) { | - |
| 2228 | QScriptItem &si = layoutData->items[firstItem + i]; | - |
| 2229 | | - |
| 2230 | int kashida_type = Justification_Arabic_Normal; | - |
| 2231 | int kashida_pos = -1; | - |
| 2232 | | - |
| 2233 | int start = qMax(line.from - si.position, 0); | - |
| 2234 | int end = qMin(line.from + line_length - (int)si.position, length(firstItem+i)); | - |
| 2235 | | - |
| 2236 | unsigned short *log_clusters = logClusters(&si); | - |
| 2237 | | - |
| 2238 | int gs = log_clusters[start]; | - |
| 2239 | int ge = (end == length(firstItem+i) ? si.num_glyphs : log_clusters[end]); | - |
| 2240 | | - |
| 2241 | Q_ASSERT(ge <= si.num_glyphs); | - |
| 2242 | | - |
| 2243 | const QGlyphLayout g = shapedGlyphs(&si); | - |
| 2244 | | - |
| 2245 | for (int i = gs; i < ge; ++i) { | - |
| 2246 | g.justifications[i].type = QGlyphJustification::JustifyNone; | - |
| 2247 | g.justifications[i].nKashidas = 0; | - |
| 2248 | g.justifications[i].space_18d6 = 0; | - |
| 2249 | | - |
| 2250 | justificationPoints.resize(nPoints+3); | - |
| 2251 | int justification = g.attributes[i].justification; | - |
| 2252 | | - |
| 2253 | switch(justification) { | - |
| 2254 | case Justification_Prohibited: | - |
| 2255 | break; | - |
| 2256 | case Justification_Space: | - |
| 2257 | | - |
| 2258 | case Justification_Arabic_Space: | - |
| 2259 | if (kashida_pos >= 0) { | - |
| 2260 | | - |
| 2261 | set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si)); | - |
| 2262 | if (justificationPoints[nPoints].kashidaWidth > 0) { | - |
| 2263 | minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth); | - |
| 2264 | maxJustify = qMax(maxJustify, justificationPoints[nPoints].type); | - |
| 2265 | ++nPoints; | - |
| 2266 | } | - |
| 2267 | } | - |
| 2268 | kashida_pos = -1; | - |
| 2269 | kashida_type = Justification_Arabic_Normal; | - |
| 2270 | | - |
| 2271 | case Justification_Character: | - |
| 2272 | set(&justificationPoints[nPoints++], justification, g.mid(i), fontEngine(si)); | - |
| 2273 | maxJustify = qMax(maxJustify, justification); | - |
| 2274 | break; | - |
| 2275 | case Justification_Arabic_Normal: | - |
| 2276 | case Justification_Arabic_Waw: | - |
| 2277 | case Justification_Arabic_BaRa: | - |
| 2278 | case Justification_Arabic_Alef: | - |
| 2279 | case Justification_Arabic_HahDal: | - |
| 2280 | case Justification_Arabic_Seen: | - |
| 2281 | case Justification_Arabic_Kashida: | - |
| 2282 | if (justification >= kashida_type) { | - |
| 2283 | kashida_pos = i; | - |
| 2284 | kashida_type = justification; | - |
| 2285 | } | - |
| 2286 | } | - |
| 2287 | } | - |
| 2288 | if (kashida_pos >= 0) { | - |
| 2289 | set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si)); | - |
| 2290 | if (justificationPoints[nPoints].kashidaWidth > 0) { | - |
| 2291 | minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth); | - |
| 2292 | maxJustify = qMax(maxJustify, justificationPoints[nPoints].type); | - |
| 2293 | ++nPoints; | - |
| 2294 | } | - |
| 2295 | } | - |
| 2296 | } | - |
| 2297 | | - |
| 2298 | QFixed leading = leadingSpaceWidth(line); | - |
| 2299 | QFixed need = line.width - line.textWidth - leading; | - |
| 2300 | if (need < 0) { | - |
| 2301 | | - |
| 2302 | const_cast<QScriptLine &>(line).justified = true; | - |
| 2303 | return; | - |
| 2304 | } | - |
| 2305 | | - |
| 2306 | | - |
| 2307 | | - |
| 2308 | | - |
| 2309 | | - |
| 2310 | if (maxJustify >= Justification_Arabic_Normal) { | - |
| 2311 | while (need >= minKashida) { | - |
| 2312 | for (int type = maxJustify; need >= minKashida && type >= Justification_Arabic_Normal; --type) { | - |
| 2313 | for (int i = 0; need >= minKashida && i < nPoints; ++i) { | - |
| 2314 | if (justificationPoints[i].type == type && justificationPoints[i].kashidaWidth <= need) { | - |
| 2315 | justificationPoints[i].glyph.justifications->nKashidas++; | - |
| 2316 | | - |
| 2317 | justificationPoints[i].glyph.justifications->space_18d6 += justificationPoints[i].kashidaWidth.value(); | - |
| 2318 | need -= justificationPoints[i].kashidaWidth; | - |
| 2319 | | - |
| 2320 | } | - |
| 2321 | } | - |
| 2322 | } | - |
| 2323 | } | - |
| 2324 | } | - |
| 2325 | Q_ASSERT(need >= 0); | - |
| 2326 | if (!need) | - |
| 2327 | goto end; | - |
| 2328 | | - |
| 2329 | maxJustify = qMin(maxJustify, int(Justification_Space)); | - |
| 2330 | for (int type = maxJustify; need != 0 && type > 0; --type) { | - |
| 2331 | int n = 0; | - |
| 2332 | for (int i = 0; i < nPoints; ++i) { | - |
| 2333 | if (justificationPoints[i].type == type) | - |
| 2334 | ++n; | - |
| 2335 | } | - |
| 2336 | | - |
| 2337 | | - |
| 2338 | | - |
| 2339 | if (!n) | - |
| 2340 | continue; | - |
| 2341 | | - |
| 2342 | for (int i = 0; i < nPoints; ++i) { | - |
| 2343 | if (justificationPoints[i].type == type) { | - |
| 2344 | QFixed add = need/n; | - |
| 2345 | | - |
| 2346 | justificationPoints[i].glyph.justifications[0].space_18d6 = add.value(); | - |
| 2347 | need -= add; | - |
| 2348 | --n; | - |
| 2349 | } | - |
| 2350 | } | - |
| 2351 | | - |
| 2352 | Q_ASSERT(!need); | - |
| 2353 | } | - |
| 2354 | end: | - |
| 2355 | const_cast<QScriptLine &>(line).justified = true; | - |
| 2356 | } | - |
| 2357 | | - |
| 2358 | void QScriptLine::setDefaultHeight(QTextEngine *eng) | - |
| 2359 | { | - |
| 2360 | QFont f; | - |
| 2361 | QFontEngine *e; | - |
| 2362 | | - |
| 2363 | if (eng->block.docHandle() && eng->block.docHandle()->layout()) { | - |
| 2364 | f = eng->block.charFormat().font(); | - |
| 2365 | | - |
| 2366 | QPaintDevice *pdev = eng->block.docHandle()->layout()->paintDevice(); | - |
| 2367 | if (pdev) | - |
| 2368 | f = QFont(f, pdev); | - |
| 2369 | e = f.d->engineForScript(QChar::Script_Common); | - |
| 2370 | } else { | - |
| 2371 | e = eng->fnt.d->engineForScript(QChar::Script_Common); | - |
| 2372 | } | - |
| 2373 | | - |
| 2374 | QFixed other_ascent = e->ascent(); | - |
| 2375 | QFixed other_descent = e->descent(); | - |
| 2376 | QFixed other_leading = e->leading(); | - |
| 2377 | leading = qMax(leading + ascent, other_leading + other_ascent) - qMax(ascent, other_ascent); | - |
| 2378 | ascent = qMax(ascent, other_ascent); | - |
| 2379 | descent = qMax(descent, other_descent); | - |
| 2380 | } | - |
| 2381 | | - |
| 2382 | QTextEngine::LayoutData::LayoutData() | - |
| 2383 | { | - |
| 2384 | memory = 0; | - |
| 2385 | allocated = 0; | - |
| 2386 | memory_on_stack = false; | - |
| 2387 | used = 0; | - |
| 2388 | hasBidi = false; | - |
| 2389 | layoutState = LayoutEmpty; | - |
| 2390 | haveCharAttributes = false; | - |
| 2391 | logClustersPtr = 0; | - |
| 2392 | available_glyphs = 0; | - |
| 2393 | } | - |
| 2394 | | - |
| 2395 | QTextEngine::LayoutData::LayoutData(const QString &str, void **stack_memory, int _allocated) | - |
| 2396 | : string(str) | - |
| 2397 | { | - |
| 2398 | allocated = _allocated; | - |
| 2399 | | - |
| 2400 | int space_charAttributes = sizeof(QCharAttributes)*string.length()/sizeof(void*) + 1; | - |
| 2401 | int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1; | - |
| 2402 | available_glyphs = ((int)allocated - space_charAttributes - space_logClusters)*(int)sizeof(void*)/(int)QGlyphLayout::SpaceNeeded; | - |
| 2403 | | - |
| 2404 | if (available_glyphs < str.length()) { | - |
| 2405 | | - |
| 2406 | allocated = 0; | - |
| 2407 | | - |
| 2408 | memory_on_stack = false; | - |
| 2409 | memory = 0; | - |
| 2410 | logClustersPtr = 0; | - |
| 2411 | } else { | - |
| 2412 | memory_on_stack = true; | - |
| 2413 | memory = stack_memory; | - |
| 2414 | logClustersPtr = (unsigned short *)(memory + space_charAttributes); | - |
| 2415 | | - |
| 2416 | void *m = memory + space_charAttributes + space_logClusters; | - |
| 2417 | glyphLayout = QGlyphLayout(reinterpret_cast<char *>(m), str.length()); | - |
| 2418 | glyphLayout.clear(); | - |
| 2419 | memset(memory, 0, space_charAttributes*sizeof(void *)); | - |
| 2420 | } | - |
| 2421 | used = 0; | - |
| 2422 | hasBidi = false; | - |
| 2423 | layoutState = LayoutEmpty; | - |
| 2424 | haveCharAttributes = false; | - |
| 2425 | } | - |
| 2426 | | - |
| 2427 | QTextEngine::LayoutData::~LayoutData() | - |
| 2428 | { | - |
| 2429 | if (!memory_on_stack) | - |
| 2430 | free(memory); | - |
| 2431 | memory = 0; | - |
| 2432 | } | - |
| 2433 | | - |
| 2434 | bool QTextEngine::LayoutData::reallocate(int totalGlyphs) | - |
| 2435 | { | - |
| 2436 | Q_ASSERT(totalGlyphs >= glyphLayout.numGlyphs); | - |
| 2437 | if (memory_on_stack && available_glyphs >= totalGlyphs) { | - |
| 2438 | glyphLayout.grow(glyphLayout.data(), totalGlyphs); | - |
| 2439 | return true; | - |
| 2440 | } | - |
| 2441 | | - |
| 2442 | int space_charAttributes = sizeof(QCharAttributes)*string.length()/sizeof(void*) + 1; | - |
| 2443 | int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1; | - |
| 2444 | int space_glyphs = (totalGlyphs * QGlyphLayout::SpaceNeeded) / sizeof(void *) + 2; | - |
| 2445 | | - |
| 2446 | int newAllocated = space_charAttributes + space_glyphs + space_logClusters; | - |
| 2447 | | - |
| 2448 | | - |
| 2449 | | - |
| 2450 | if (space_charAttributes < 0 || space_logClusters < 0 || space_glyphs < 0 || newAllocated < allocated) { | - |
| 2451 | layoutState = LayoutFailed; | - |
| 2452 | return false; | - |
| 2453 | } | - |
| 2454 | | - |
| 2455 | void **newMem = (void **)::realloc(memory_on_stack ? 0 : memory, newAllocated*sizeof(void *)); | - |
| 2456 | if (!newMem) { | - |
| 2457 | layoutState = LayoutFailed; | - |
| 2458 | return false; | - |
| 2459 | } | - |
| 2460 | if (memory_on_stack) | - |
| 2461 | memcpy(newMem, memory, allocated*sizeof(void *)); | - |
| 2462 | memory = newMem; | - |
| 2463 | memory_on_stack = false; | - |
| 2464 | | - |
| 2465 | void **m = memory; | - |
| 2466 | m += space_charAttributes; | - |
| 2467 | logClustersPtr = (unsigned short *) m; | - |
| 2468 | m += space_logClusters; | - |
| 2469 | | - |
| 2470 | const int space_preGlyphLayout = space_charAttributes + space_logClusters; | - |
| 2471 | if (allocated < space_preGlyphLayout) | - |
| 2472 | memset(memory + allocated, 0, (space_preGlyphLayout - allocated)*sizeof(void *)); | - |
| 2473 | | - |
| 2474 | glyphLayout.grow(reinterpret_cast<char *>(m), totalGlyphs); | - |
| 2475 | | - |
| 2476 | allocated = newAllocated; | - |
| 2477 | return true; | - |
| 2478 | } | - |
| 2479 | | - |
| 2480 | | - |
| 2481 | void QGlyphLayout::grow(char *address, int totalGlyphs) | - |
| 2482 | { | - |
| 2483 | QGlyphLayout oldLayout(address, numGlyphs); | - |
| 2484 | QGlyphLayout newLayout(address, totalGlyphs); | - |
| 2485 | | - |
| 2486 | if (numGlyphs) { | - |
| 2487 | | - |
| 2488 | memmove(newLayout.attributes, oldLayout.attributes, numGlyphs * sizeof(QGlyphAttributes)); | - |
| 2489 | memmove(newLayout.justifications, oldLayout.justifications, numGlyphs * sizeof(QGlyphJustification)); | - |
| 2490 | memmove(newLayout.advances, oldLayout.advances, numGlyphs * sizeof(QFixed)); | - |
| 2491 | memmove(newLayout.glyphs, oldLayout.glyphs, numGlyphs * sizeof(glyph_t)); | - |
| 2492 | } | - |
| 2493 | | - |
| 2494 | | - |
| 2495 | newLayout.clear(numGlyphs); | - |
| 2496 | | - |
| 2497 | *this = newLayout; | - |
| 2498 | } | - |
| 2499 | | - |
| 2500 | void QTextEngine::freeMemory() | - |
| 2501 | { | - |
| 2502 | if (!stackEngine) { | - |
| 2503 | delete layoutData; | - |
| 2504 | layoutData = 0; | - |
| 2505 | } else { | - |
| 2506 | layoutData->used = 0; | - |
| 2507 | layoutData->hasBidi = false; | - |
| 2508 | layoutData->layoutState = LayoutEmpty; | - |
| 2509 | layoutData->haveCharAttributes = false; | - |
| 2510 | layoutData->items.clear(); | - |
| 2511 | } | - |
| 2512 | if (specialData) | - |
| 2513 | specialData->resolvedFormats.clear(); | - |
| 2514 | for (int i = 0; i < lines.size(); ++i) { | - |
| 2515 | lines[i].justified = 0; | - |
| 2516 | lines[i].gridfitted = 0; | - |
| 2517 | } | - |
| 2518 | } | - |
| 2519 | | - |
| 2520 | int QTextEngine::formatIndex(const QScriptItem *si) const | - |
| 2521 | { | - |
| 2522 | if (specialData && !specialData->resolvedFormats.isEmpty()) { | - |
| 2523 | QTextFormatCollection *collection = formatCollection(); | - |
| 2524 | Q_ASSERT(collection); | - |
| 2525 | return collection->indexForFormat(specialData->resolvedFormats.at(si - &layoutData->items[0])); | - |
| 2526 | } | - |
| 2527 | | - |
| 2528 | QTextDocumentPrivate *p = block.docHandle(); | - |
| 2529 | if (!p) | - |
| 2530 | return -1; | - |
| 2531 | int pos = si->position; | - |
| 2532 | if (specialData && si->position >= specialData->preeditPosition) { | - |
| 2533 | if (si->position < specialData->preeditPosition + specialData->preeditText.length()) | - |
| 2534 | pos = qMax(qMin(block.length(), specialData->preeditPosition) - 1, 0); | - |
| 2535 | else | - |
| 2536 | pos -= specialData->preeditText.length(); | - |
| 2537 | } | - |
| 2538 | QTextDocumentPrivate::FragmentIterator it = p->find(block.position() + pos); | - |
| 2539 | return it.value()->format; | - |
| 2540 | } | - |
| 2541 | | - |
| 2542 | | - |
| 2543 | QTextCharFormat QTextEngine::format(const QScriptItem *si) const | - |
| 2544 | { | - |
| 2545 | if (const QTextFormatCollection *collection = formatCollection()) | - |
| 2546 | return collection->charFormat(formatIndex(si)); | - |
| 2547 | return QTextCharFormat(); | - |
| 2548 | } | - |
| 2549 | | - |
| 2550 | void QTextEngine::addRequiredBoundaries() const | - |
| 2551 | { | - |
| 2552 | if (specialData) { | - |
| 2553 | for (int i = 0; i < specialData->formats.size(); ++i) { | - |
| 2554 | const QTextLayout::FormatRange &r = specialData->formats.at(i); | - |
| 2555 | setBoundary(r.start); | - |
| 2556 | setBoundary(r.start + r.length); | - |
| 2557 | | - |
| 2558 | } | - |
| 2559 | } | - |
| 2560 | } | - |
| 2561 | | - |
| 2562 | bool QTextEngine::atWordSeparator(int position) const | - |
| 2563 | { | - |
| 2564 | const QChar c = layoutData->string.at(position); | - |
| 2565 | switch (c.unicode()) { | - |
| 2566 | case '.': | - |
| 2567 | case ',': | - |
| 2568 | case '?': | - |
| 2569 | case '!': | - |
| 2570 | case '@': | - |
| 2571 | case '#': | - |
| 2572 | case '$': | - |
| 2573 | case ':': | - |
| 2574 | case ';': | - |
| 2575 | case '-': | - |
| 2576 | case '<': | - |
| 2577 | case '>': | - |
| 2578 | case '[': | - |
| 2579 | case ']': | - |
| 2580 | case '(': | - |
| 2581 | case ')': | - |
| 2582 | case '{': | - |
| 2583 | case '}': | - |
| 2584 | case '=': | - |
| 2585 | case '/': | - |
| 2586 | case '+': | - |
| 2587 | case '%': | - |
| 2588 | case '&': | - |
| 2589 | case '^': | - |
| 2590 | case '*': | - |
| 2591 | case '\'': | - |
| 2592 | case '"': | - |
| 2593 | case '`': | - |
| 2594 | case '~': | - |
| 2595 | case '|': | - |
| 2596 | case '\\': | - |
| 2597 | return true; | - |
| 2598 | default: | - |
| 2599 | break; | - |
| 2600 | } | - |
| 2601 | return false; | - |
| 2602 | } | - |
| 2603 | | - |
| 2604 | void QTextEngine::setPreeditArea(int position, const QString &preeditText) | - |
| 2605 | { | - |
| 2606 | if (preeditText.isEmpty()) { | - |
| 2607 | if (!specialData) | - |
| 2608 | return; | - |
| 2609 | if (specialData->formats.isEmpty()) { | - |
| 2610 | delete specialData; | - |
| 2611 | specialData = 0; | - |
| 2612 | } else { | - |
| 2613 | specialData->preeditText = QString(); | - |
| 2614 | specialData->preeditPosition = -1; | - |
| 2615 | } | - |
| 2616 | } else { | - |
| 2617 | if (!specialData) | - |
| 2618 | specialData = new SpecialData; | - |
| 2619 | specialData->preeditPosition = position; | - |
| 2620 | specialData->preeditText = preeditText; | - |
| 2621 | } | - |
| 2622 | invalidate(); | - |
| 2623 | clearLineData(); | - |
| 2624 | } | - |
| 2625 | | - |
| 2626 | void QTextEngine::setFormats(const QVector<QTextLayout::FormatRange> &formats) | - |
| 2627 | { | - |
| 2628 | if (formats.isEmpty()) { | - |
| 2629 | if (!specialData) | - |
| 2630 | return; | - |
| 2631 | if (specialData->preeditText.isEmpty()) { | - |
| 2632 | delete specialData; | - |
| 2633 | specialData = 0; | - |
| 2634 | } else { | - |
| 2635 | specialData->formats.clear(); | - |
| 2636 | } | - |
| 2637 | } else { | - |
| 2638 | if (!specialData) { | - |
| 2639 | specialData = new SpecialData; | - |
| 2640 | specialData->preeditPosition = -1; | - |
| 2641 | } | - |
| 2642 | specialData->formats = formats; | - |
| 2643 | indexFormats(); | - |
| 2644 | } | - |
| 2645 | invalidate(); | - |
| 2646 | clearLineData(); | - |
| 2647 | } | - |
| 2648 | | - |
| 2649 | void QTextEngine::indexFormats() | - |
| 2650 | { | - |
| 2651 | QTextFormatCollection *collection = formatCollection(); | - |
| 2652 | if (!collection) { | - |
| 2653 | Q_ASSERT(!block.docHandle()); | - |
| 2654 | specialData->formatCollection.reset(new QTextFormatCollection); | - |
| 2655 | collection = specialData->formatCollection.data(); | - |
| 2656 | } | - |
| 2657 | | - |
| 2658 | | - |
| 2659 | for (int i = 0; i < specialData->formats.size(); ++i) { | - |
| 2660 | QTextCharFormat &format = specialData->formats[i].format; | - |
| 2661 | format = collection->charFormat(collection->indexForFormat(format)); | - |
| 2662 | } | - |
| 2663 | } | - |
| 2664 | | - |
| 2665 | | - |
| 2666 | | - |
| 2667 | | - |
| 2668 | | - |
| 2669 | static inline bool nextCharJoins(const QString &string, int pos) | - |
| 2670 | { | - |
| 2671 | while (pos < string.length() && string.at(pos).category() == QChar::Mark_NonSpacing) | - |
| 2672 | ++pos; | - |
| 2673 | if (pos == string.length()) | - |
| 2674 | return false; | - |
| 2675 | QChar::JoiningType joining = string.at(pos).joiningType(); | - |
| 2676 | return joining != QChar::Joining_None && joining != QChar::Joining_Transparent; | - |
| 2677 | } | - |
| 2678 | | - |
| 2679 | static inline bool prevCharJoins(const QString &string, int pos) | - |
| 2680 | { | - |
| 2681 | while (pos > 0 && string.at(pos - 1).category() == QChar::Mark_NonSpacing) | - |
| 2682 | --pos; | - |
| 2683 | if (pos == 0) | - |
| 2684 | return false; | - |
| 2685 | QChar::JoiningType joining = string.at(pos - 1).joiningType(); | - |
| 2686 | return joining == QChar::Joining_Dual || joining == QChar::Joining_Causing; | - |
| 2687 | } | - |
| 2688 | | - |
| 2689 | static inline bool isRetainableControlCode(QChar c) | - |
| 2690 | { | - |
| 2691 | return (c.unicode() >= 0x202a && c.unicode() <= 0x202e) | - |
| 2692 | || (c.unicode() >= 0x200e && c.unicode() <= 0x200f) | - |
| 2693 | || (c.unicode() >= 0x2066 && c.unicode() <= 0x2069); | - |
| 2694 | } | - |
| 2695 | | - |
| 2696 | static QString stringMidRetainingBidiCC(const QString &string, | - |
| 2697 | const QString &ellidePrefix, | - |
| 2698 | const QString &ellideSuffix, | - |
| 2699 | int subStringFrom, | - |
| 2700 | int subStringTo, | - |
| 2701 | int midStart, | - |
| 2702 | int midLength) | - |
| 2703 | { | - |
| 2704 | QString prefix; | - |
| 2705 | for (int i=subStringFrom; i<midStart; ++i) { | - |
| 2706 | QChar c = string.at(i); | - |
| 2707 | if (isRetainableControlCode(c)) | - |
| 2708 | prefix += c; | - |
| 2709 | } | - |
| 2710 | | - |
| 2711 | QString suffix; | - |
| 2712 | for (int i=midStart + midLength; i<subStringTo; ++i) { | - |
| 2713 | QChar c = string.at(i); | - |
| 2714 | if (isRetainableControlCode(c)) | - |
| 2715 | suffix += c; | - |
| 2716 | } | - |
| 2717 | | - |
| 2718 | return prefix + ellidePrefix + string.mid(midStart, midLength) + ellideSuffix + suffix; | - |
| 2719 | } | - |
| 2720 | | - |
| 2721 | QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int flags, int from, int count) const | - |
| 2722 | { | - |
| 2723 | | - |
| 2724 | | - |
| 2725 | if (flags & Qt::TextShowMnemonic) { | - |
| 2726 | itemize(); | - |
| 2727 | QCharAttributes *attributes = const_cast<QCharAttributes *>(this->attributes()); | - |
| 2728 | if (!attributes) | - |
| 2729 | return QString(); | - |
| 2730 | for (int i = 0; i < layoutData->items.size(); ++i) { | - |
| 2731 | QScriptItem &si = layoutData->items[i]; | - |
| 2732 | if (!si.num_glyphs) | - |
| 2733 | shape(i); | - |
| 2734 | | - |
| 2735 | unsigned short *logClusters = this->logClusters(&si); | - |
| 2736 | QGlyphLayout glyphs = shapedGlyphs(&si); | - |
| 2737 | | - |
| 2738 | const int end = si.position + length(&si); | - |
| 2739 | for (int i = si.position; i < end - 1; ++i) { | - |
| 2740 | if (layoutData->string.at(i) == QLatin1Char('&') | - |
| 2741 | && !attributes[i + 1].whiteSpace && attributes[i + 1].graphemeBoundary) { | - |
| 2742 | const int gp = logClusters[i - si.position]; | - |
| 2743 | glyphs.attributes[gp].dontPrint = true; | - |
| 2744 | | - |
| 2745 | attributes[i] = attributes[i + 1]; | - |
| 2746 | memset(attributes + i + 1, 0, sizeof(QCharAttributes)); | - |
| 2747 | if (layoutData->string.at(i + 1) == QLatin1Char('&')) | - |
| 2748 | ++i; | - |
| 2749 | } | - |
| 2750 | } | - |
| 2751 | } | - |
| 2752 | } | - |
| 2753 | | - |
| 2754 | validate(); | - |
| 2755 | | - |
| 2756 | const int to = count >= 0 && count <= layoutData->string.length() - from | - |
| 2757 | ? from + count | - |
| 2758 | : layoutData->string.length(); | - |
| 2759 | | - |
| 2760 | if (mode == Qt::ElideNone | - |
| 2761 | || this->width(from, layoutData->string.length()) <= width | - |
| 2762 | || to - from <= 1) | - |
| 2763 | return layoutData->string.mid(from, from - to); | - |
| 2764 | | - |
| 2765 | QFixed ellipsisWidth; | - |
| 2766 | QString ellipsisText; | - |
| 2767 | { | - |
| 2768 | QFontEngine *engine = fnt.d->engineForScript(QChar::Script_Common); | - |
| 2769 | | - |
| 2770 | QChar ellipsisChar(0x2026); | - |
| 2771 | | - |
| 2772 | glyph_t glyph = engine->glyphIndex(ellipsisChar.unicode()); | - |
| 2773 | | - |
| 2774 | QGlyphLayout glyphs; | - |
| 2775 | glyphs.numGlyphs = 1; | - |
| 2776 | glyphs.glyphs = &glyph; | - |
| 2777 | glyphs.advances = &ellipsisWidth; | - |
| 2778 | | - |
| 2779 | if (glyph != 0) { | - |
| 2780 | engine->recalcAdvances(&glyphs, 0); | - |
| 2781 | | - |
| 2782 | ellipsisText = ellipsisChar; | - |
| 2783 | } else { | - |
| 2784 | glyph = engine->glyphIndex('.'); | - |
| 2785 | if (glyph != 0) { | - |
| 2786 | engine->recalcAdvances(&glyphs, 0); | - |
| 2787 | | - |
| 2788 | ellipsisWidth *= 3; | - |
| 2789 | ellipsisText = QStringLiteral("..."); | - |
| 2790 | } | - |
| 2791 | } | - |
| 2792 | } | - |
| 2793 | | - |
| 2794 | const QFixed availableWidth = width - ellipsisWidth; | - |
| 2795 | if (availableWidth < 0) | - |
| 2796 | return QString(); | - |
| 2797 | | - |
| 2798 | const QCharAttributes *attributes = this->attributes(); | - |
| 2799 | if (!attributes) | - |
| 2800 | return QString(); | - |
| 2801 | | - |
| 2802 | if (mode == Qt::ElideRight) { | - |
| 2803 | QFixed currentWidth; | - |
| 2804 | int pos; | - |
| 2805 | int nextBreak = from; | - |
| 2806 | | - |
| 2807 | do { | - |
| 2808 | pos = nextBreak; | - |
| 2809 | | - |
| 2810 | ++nextBreak; | - |
| 2811 | while (nextBreak < layoutData->string.length() && !attributes[nextBreak].graphemeBoundary) | - |
| 2812 | ++nextBreak; | - |
| 2813 | | - |
| 2814 | currentWidth += this->width(pos, nextBreak - pos); | - |
| 2815 | } while (nextBreak < to | - |
| 2816 | && currentWidth < availableWidth); | - |
| 2817 | | - |
| 2818 | if (nextCharJoins(layoutData->string, pos)) | - |
| 2819 | ellipsisText.prepend(QChar(0x200d) ); | - |
| 2820 | | - |
| 2821 | return stringMidRetainingBidiCC(layoutData->string, | - |
| 2822 | QString(), ellipsisText, | - |
| 2823 | from, to, | - |
| 2824 | from, pos - from); | - |
| 2825 | } else if (mode == Qt::ElideLeft) { | - |
| 2826 | QFixed currentWidth; | - |
| 2827 | int pos; | - |
| 2828 | int nextBreak = to; | - |
| 2829 | | - |
| 2830 | do { | - |
| 2831 | pos = nextBreak; | - |
| 2832 | | - |
| 2833 | --nextBreak; | - |
| 2834 | while (nextBreak > 0 && !attributes[nextBreak].graphemeBoundary) | - |
| 2835 | --nextBreak; | - |
| 2836 | | - |
| 2837 | currentWidth += this->width(nextBreak, pos - nextBreak); | - |
| 2838 | } while (nextBreak > from | - |
| 2839 | && currentWidth < availableWidth); | - |
| 2840 | | - |
| 2841 | if (prevCharJoins(layoutData->string, pos)) | - |
| 2842 | ellipsisText.append(QChar(0x200d) ); | - |
| 2843 | | - |
| 2844 | return stringMidRetainingBidiCC(layoutData->string, | - |
| 2845 | ellipsisText, QString(), | - |
| 2846 | from, to, | - |
| 2847 | pos, to - pos); | - |
| 2848 | } else if (mode == Qt::ElideMiddle) { | - |
| 2849 | QFixed leftWidth; | - |
| 2850 | QFixed rightWidth; | - |
| 2851 | | - |
| 2852 | int leftPos = from; | - |
| 2853 | int nextLeftBreak = from; | - |
| 2854 | | - |
| 2855 | int rightPos = to; | - |
| 2856 | int nextRightBreak = to; | - |
| 2857 | | - |
| 2858 | do { | - |
| 2859 | leftPos = nextLeftBreak; | - |
| 2860 | rightPos = nextRightBreak; | - |
| 2861 | | - |
| 2862 | ++nextLeftBreak; | - |
| 2863 | while (nextLeftBreak < layoutData->string.length() && !attributes[nextLeftBreak].graphemeBoundary) | - |
| 2864 | ++nextLeftBreak; | - |
| 2865 | | - |
| 2866 | --nextRightBreak; | - |
| 2867 | while (nextRightBreak > from && !attributes[nextRightBreak].graphemeBoundary) | - |
| 2868 | --nextRightBreak; | - |
| 2869 | | - |
| 2870 | leftWidth += this->width(leftPos, nextLeftBreak - leftPos); | - |
| 2871 | rightWidth += this->width(nextRightBreak, rightPos - nextRightBreak); | - |
| 2872 | } while (nextLeftBreak < to | - |
| 2873 | && nextRightBreak > from | - |
| 2874 | && leftWidth + rightWidth < availableWidth); | - |
| 2875 | | - |
| 2876 | if (nextCharJoins(layoutData->string, leftPos)) | - |
| 2877 | ellipsisText.prepend(QChar(0x200d) ); | - |
| 2878 | if (prevCharJoins(layoutData->string, rightPos)) | - |
| 2879 | ellipsisText.append(QChar(0x200d) ); | - |
| 2880 | | - |
| 2881 | return layoutData->string.mid(from, leftPos - from) + ellipsisText + layoutData->string.mid(rightPos, to - rightPos); | - |
| 2882 | } | - |
| 2883 | | - |
| 2884 | return layoutData->string.mid(from, to - from); | - |
| 2885 | } | - |
| 2886 | | - |
| 2887 | void QTextEngine::setBoundary(int strPos) const | - |
| 2888 | { | - |
| 2889 | const int item = findItem(strPos); | - |
| 2890 | if (item < 0) | - |
| 2891 | return; | - |
| 2892 | | - |
| 2893 | QScriptItem newItem = layoutData->items.at(item); | - |
| 2894 | if (newItem.position != strPos) { | - |
| 2895 | newItem.position = strPos; | - |
| 2896 | layoutData->items.insert(item + 1, newItem); | - |
| 2897 | } | - |
| 2898 | } | - |
| 2899 | | - |
| 2900 | QFixed QTextEngine::calculateTabWidth(int item, QFixed x) const | - |
| 2901 | { | - |
| 2902 | const QScriptItem &si = layoutData->items[item]; | - |
| 2903 | | - |
| 2904 | QFixed dpiScale = 1; | - |
| 2905 | if (block.docHandle() && block.docHandle()->layout()) { | - |
| 2906 | QPaintDevice *pdev = block.docHandle()->layout()->paintDevice(); | - |
| 2907 | if (pdev) | - |
| 2908 | dpiScale = QFixed::fromReal(pdev->logicalDpiY() / qreal(qt_defaultDpiY())); | - |
| 2909 | } else { | - |
| 2910 | dpiScale = QFixed::fromReal(fnt.d->dpi / qreal(qt_defaultDpiY())); | - |
| 2911 | } | - |
| 2912 | | - |
| 2913 | QList<QTextOption::Tab> tabArray = option.tabs(); | - |
| 2914 | if (!tabArray.isEmpty()) { | - |
| 2915 | if (isRightToLeft()) { | - |
| 2916 | QList<QTextOption::Tab> newTabs; | - |
| 2917 | newTabs.reserve(tabArray.count()); | - |
| 2918 | QList<QTextOption::Tab>::Iterator iter = tabArray.begin(); | - |
| 2919 | while(iter != tabArray.end()) { | - |
| 2920 | QTextOption::Tab tab = *iter; | - |
| 2921 | if (tab.type == QTextOption::LeftTab) | - |
| 2922 | tab.type = QTextOption::RightTab; | - |
| 2923 | else if (tab.type == QTextOption::RightTab) | - |
| 2924 | tab.type = QTextOption::LeftTab; | - |
| 2925 | newTabs << tab; | - |
| 2926 | ++iter; | - |
| 2927 | } | - |
| 2928 | tabArray = newTabs; | - |
| 2929 | } | - |
| 2930 | for (int i = 0; i < tabArray.size(); ++i) { | - |
| 2931 | QFixed tab = QFixed::fromReal(tabArray[i].position) * dpiScale; | - |
| 2932 | if (tab > x) { | - |
| 2933 | QTextOption::Tab tabSpec = tabArray[i]; | - |
| 2934 | int tabSectionEnd = layoutData->string.count(); | - |
| 2935 | if (tabSpec.type == QTextOption::RightTab || tabSpec.type == QTextOption::CenterTab) { | - |
| 2936 | | - |
| 2937 | tab = QFixed::fromReal(tabSpec.position); | - |
| 2938 | for (int i=item + 1; i < layoutData->items.count(); i++) { | - |
| 2939 | const QScriptItem &item = layoutData->items[i]; | - |
| 2940 | if (item.analysis.flags == QScriptAnalysis::TabOrObject) { | - |
| 2941 | tabSectionEnd = item.position; | - |
| 2942 | break; | - |
| 2943 | } | - |
| 2944 | } | - |
| 2945 | } | - |
| 2946 | else if (tabSpec.type == QTextOption::DelimiterTab) | - |
| 2947 | | - |
| 2948 | tabSectionEnd = qMax(si.position, layoutData->string.indexOf(tabSpec.delimiter, si.position) + 1); | - |
| 2949 | | - |
| 2950 | if (tabSectionEnd > si.position) { | - |
| 2951 | QFixed length; | - |
| 2952 | | - |
| 2953 | for (int i=item; i < layoutData->items.count(); i++) { | - |
| 2954 | QScriptItem &item = layoutData->items[i]; | - |
| 2955 | if (item.position > tabSectionEnd || item.position <= si.position) | - |
| 2956 | continue; | - |
| 2957 | shape(i); | - |
| 2958 | if (item.analysis.flags == QScriptAnalysis::Object) { | - |
| 2959 | length += item.width; | - |
| 2960 | continue; | - |
| 2961 | } | - |
| 2962 | QGlyphLayout glyphs = this->shapedGlyphs(&item); | - |
| 2963 | const int end = qMin(item.position + item.num_glyphs, tabSectionEnd) - item.position; | - |
| 2964 | for (int i=0; i < end; i++) | - |
| 2965 | length += glyphs.advances[i] * !glyphs.attributes[i].dontPrint; | - |
| 2966 | if (end + item.position == tabSectionEnd && tabSpec.type == QTextOption::DelimiterTab) | - |
| 2967 | length -= glyphs.advances[end] / 2 * !glyphs.attributes[end].dontPrint; | - |
| 2968 | } | - |
| 2969 | | - |
| 2970 | switch (tabSpec.type) { | - |
| 2971 | case QTextOption::CenterTab: | - |
| 2972 | length /= 2; | - |
| 2973 | | - |
| 2974 | case QTextOption::DelimiterTab: | - |
| 2975 | | - |
| 2976 | case QTextOption::RightTab: | - |
| 2977 | tab = QFixed::fromReal(tabSpec.position) * dpiScale - length; | - |
| 2978 | if (tab < x) | - |
| 2979 | return QFixed(); | - |
| 2980 | break; | - |
| 2981 | case QTextOption::LeftTab: | - |
| 2982 | break; | - |
| 2983 | } | - |
| 2984 | } | - |
| 2985 | return tab - x; | - |
| 2986 | } | - |
| 2987 | } | - |
| 2988 | } | - |
| 2989 | QFixed tab = QFixed::fromReal(option.tabStop()); | - |
| 2990 | if (tab <= 0) | - |
| 2991 | tab = 80; | - |
| 2992 | tab *= dpiScale; | - |
| 2993 | QFixed nextTabPos = ((x / tab).truncate() + 1) * tab; | - |
| 2994 | QFixed tabWidth = nextTabPos - x; | - |
| 2995 | | - |
| 2996 | return tabWidth; | - |
| 2997 | } | - |
| 2998 | | - |
| 2999 | namespace { | - |
| 3000 | class FormatRangeComparatorByStart { | - |
| 3001 | const QVector<QTextLayout::FormatRange> &list; | - |
| 3002 | public: | - |
| 3003 | FormatRangeComparatorByStart(const QVector<QTextLayout::FormatRange> &list) : list(list) { } | - |
| 3004 | bool operator()(int a, int b) { | - |
| 3005 | return list.at(a).start < list.at(b).start; | - |
| 3006 | } | - |
| 3007 | }; | - |
| 3008 | class FormatRangeComparatorByEnd { | - |
| 3009 | const QVector<QTextLayout::FormatRange> &list; | - |
| 3010 | public: | - |
| 3011 | FormatRangeComparatorByEnd(const QVector<QTextLayout::FormatRange> &list) : list(list) { } | - |
| 3012 | bool operator()(int a, int b) { | - |
| 3013 | return list.at(a).start + list.at(a).length < list.at(b).start + list.at(b).length; | - |
| 3014 | } | - |
| 3015 | }; | - |
| 3016 | } | - |
| 3017 | | - |
| 3018 | void QTextEngine::resolveFormats() const | - |
| 3019 | { | - |
| 3020 | if (!specialData || specialData->formats.isEmpty())| TRUE | never evaluated | | FALSE | never evaluated |
| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3021 | return; never executed: return; | 0 |
| 3022 | Q_ASSERT(specialData->resolvedFormats.isEmpty()); | - |
| 3023 | | - |
| 3024 | QTextFormatCollection *collection = formatCollection(); | - |
| 3025 | | - |
| 3026 | QVector<QTextCharFormat> resolvedFormats(layoutData->items.count()); | - |
| 3027 | | - |
| 3028 | QVarLengthArray<int, 64> formatsSortedByStart; | - |
| 3029 | formatsSortedByStart.reserve(specialData->formats.size()); | - |
| 3030 | for (int i = 0; i < specialData->formats.size(); ++i) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3031 | if (specialData->formats.at(i).length >= 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3032 | formatsSortedByStart.append(i); never executed: formatsSortedByStart.append(i); | 0 |
| 3033 | } never executed: end of block | 0 |
| 3034 | QVarLengthArray<int, 64> formatsSortedByEnd = formatsSortedByStart; | - |
| 3035 | std::sort(formatsSortedByStart.begin(), formatsSortedByStart.end(), | - |
| 3036 | FormatRangeComparatorByStart(specialData->formats)); | - |
| 3037 | std::sort(formatsSortedByEnd.begin(), formatsSortedByEnd.end(), | - |
| 3038 | FormatRangeComparatorByEnd(specialData->formats)); | - |
| 3039 | | - |
| 3040 | QVarLengthArray<int, 16> currentFormats; | - |
| 3041 | const int *startIt = formatsSortedByStart.constBegin(); | - |
| 3042 | const int *endIt = formatsSortedByEnd.constBegin(); | - |
| 3043 | | - |
| 3044 | for (int i = 0; i < layoutData->items.count(); ++i) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3045 | const QScriptItem *si = &layoutData->items.at(i); | - |
| 3046 | int end = si->position + length(si); | - |
| 3047 | | - |
| 3048 | while (startIt != formatsSortedByStart.constEnd() &&| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3049 | specialData->formats.at(*startIt).start <= si->position) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3050 | currentFormats.insert(std::upper_bound(currentFormats.begin(), currentFormats.end(), *startIt), | - |
| 3051 | *startIt); | - |
| 3052 | ++startIt; | - |
| 3053 | } never executed: end of block | 0 |
| 3054 | while (endIt != formatsSortedByEnd.constEnd() &&| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3055 | specialData->formats.at(*endIt).start + specialData->formats.at(*endIt).length < end) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3056 | int *currentFormatIterator = std::lower_bound(currentFormats.begin(), currentFormats.end(), *endIt); | - |
| 3057 | if (*endIt < *currentFormatIterator)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3058 | currentFormatIterator = currentFormats.end(); never executed: currentFormatIterator = currentFormats.end(); | 0 |
| 3059 | currentFormats.remove(currentFormatIterator - currentFormats.begin()); | - |
| 3060 | ++endIt; | - |
| 3061 | } never executed: end of block | 0 |
| 3062 | | - |
| 3063 | QTextCharFormat &format = resolvedFormats[i]; | - |
| 3064 | if (block.docHandle()) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3065 | | - |
| 3066 | | - |
| 3067 | format = collection->charFormat(formatIndex(si)); | - |
| 3068 | } never executed: end of block | 0 |
| 3069 | if (!currentFormats.isEmpty()) {| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3070 | foreachfor (int cur, : currentFormats) { | - |
| 3071 | const QTextLayout::FormatRange &range = specialData->formats.at(cur); | - |
| 3072 | Q_ASSERT(range.start <= si->position && range.start + range.length >= end); | - |
| 3073 | format.merge(range.format); | - |
| 3074 | } never executed: end of block | 0 |
| 3075 | format = collection->charFormat(collection->indexForFormat(format)); | - |
| 3076 | } never executed: end of block | 0 |
| 3077 | } never executed: end of block | 0 |
| 3078 | | - |
| 3079 | specialData->resolvedFormats = resolvedFormats; | - |
| 3080 | } never executed: end of block | 0 |
| 3081 | | - |
| 3082 | QFixed QTextEngine::leadingSpaceWidth(const QScriptLine &line) | - |
| 3083 | { | - |
| 3084 | if (!line.hasTrailingSpaces | - |
| 3085 | || (option.flags() & QTextOption::IncludeTrailingSpaces) | - |
| 3086 | || !isRightToLeft()) | - |
| 3087 | return QFixed(); | - |
| 3088 | | - |
| 3089 | return width(line.from + line.length, line.trailingSpaces); | - |
| 3090 | } | - |
| 3091 | | - |
| 3092 | QFixed QTextEngine::alignLine(const QScriptLine &line) | - |
| 3093 | { | - |
| 3094 | QFixed x = 0; | - |
| 3095 | justify(line); | - |
| 3096 | | - |
| 3097 | if (!line.justified && line.width != QFIXED_MAX) { | - |
| 3098 | int align = option.alignment(); | - |
| 3099 | if (align & Qt::AlignJustify && isRightToLeft()) | - |
| 3100 | align = Qt::AlignRight; | - |
| 3101 | if (align & Qt::AlignRight) | - |
| 3102 | x = line.width - (line.textAdvance); | - |
| 3103 | else if (align & Qt::AlignHCenter) | - |
| 3104 | x = (line.width - line.textAdvance)/2; | - |
| 3105 | } | - |
| 3106 | return x; | - |
| 3107 | } | - |
| 3108 | | - |
| 3109 | QFixed QTextEngine::offsetInLigature(const QScriptItem *si, int pos, int max, int glyph_pos) | - |
| 3110 | { | - |
| 3111 | unsigned short *logClusters = this->logClusters(si); | - |
| 3112 | const QGlyphLayout &glyphs = shapedGlyphs(si); | - |
| 3113 | | - |
| 3114 | int offsetInCluster = 0; | - |
| 3115 | for (int i = pos - 1; i >= 0; i--) { | - |
| 3116 | if (logClusters[i] == glyph_pos) | - |
| 3117 | offsetInCluster++; | - |
| 3118 | else | - |
| 3119 | break; | - |
| 3120 | } | - |
| 3121 | | - |
| 3122 | | - |
| 3123 | | - |
| 3124 | if (offsetInCluster > 0) { | - |
| 3125 | int clusterLength = 0; | - |
| 3126 | for (int i = pos - offsetInCluster; i < max; i++) { | - |
| 3127 | if (logClusters[i] == glyph_pos) | - |
| 3128 | clusterLength++; | - |
| 3129 | else | - |
| 3130 | break; | - |
| 3131 | } | - |
| 3132 | if (clusterLength) | - |
| 3133 | return glyphs.advances[glyph_pos] * offsetInCluster / clusterLength; | - |
| 3134 | } | - |
| 3135 | | - |
| 3136 | return 0; | - |
| 3137 | } | - |
| 3138 | | - |
| 3139 | | - |
| 3140 | int QTextEngine::getClusterLength(unsigned short *logClusters, | - |
| 3141 | const QCharAttributes *attributes, | - |
| 3142 | int from, int to, int glyph_pos, int *start) | - |
| 3143 | { | - |
| 3144 | int clusterLength = 0; | - |
| 3145 | for (int i = from; i < to; i++) { | - |
| 3146 | if (logClusters[i] == glyph_pos && attributes[i].graphemeBoundary) { | - |
| 3147 | if (*start < 0) | - |
| 3148 | *start = i; | - |
| 3149 | clusterLength++; | - |
| 3150 | } | - |
| 3151 | else if (clusterLength) | - |
| 3152 | break; | - |
| 3153 | } | - |
| 3154 | return clusterLength; | - |
| 3155 | } | - |
| 3156 | | - |
| 3157 | int QTextEngine::positionInLigature(const QScriptItem *si, int end, | - |
| 3158 | QFixed x, QFixed edge, int glyph_pos, | - |
| 3159 | bool cursorOnCharacter) | - |
| 3160 | { | - |
| 3161 | unsigned short *logClusters = this->logClusters(si); | - |
| 3162 | int clusterStart = -1; | - |
| 3163 | int clusterLength = 0; | - |
| 3164 | | - |
| 3165 | if (si->analysis.script != QChar::Script_Common && | - |
| 3166 | si->analysis.script != QChar::Script_Greek) { | - |
| 3167 | if (glyph_pos == -1) | - |
| 3168 | return si->position + end; | - |
| 3169 | else { | - |
| 3170 | int i; | - |
| 3171 | for (i = 0; i < end; i++) | - |
| 3172 | if (logClusters[i] == glyph_pos) | - |
| 3173 | break; | - |
| 3174 | return si->position + i; | - |
| 3175 | } | - |
| 3176 | } | - |
| 3177 | | - |
| 3178 | if (glyph_pos == -1 && end > 0) | - |
| 3179 | glyph_pos = logClusters[end - 1]; | - |
| 3180 | else { | - |
| 3181 | if (x <= edge) | - |
| 3182 | glyph_pos--; | - |
| 3183 | } | - |
| 3184 | | - |
| 3185 | const QCharAttributes *attrs = attributes() + si->position; | - |
| 3186 | logClusters = this->logClusters(si); | - |
| 3187 | clusterLength = getClusterLength(logClusters, attrs, 0, end, glyph_pos, &clusterStart); | - |
| 3188 | | - |
| 3189 | if (clusterLength) { | - |
| 3190 | const QGlyphLayout &glyphs = shapedGlyphs(si); | - |
| 3191 | QFixed glyphWidth = glyphs.effectiveAdvance(glyph_pos); | - |
| 3192 | | - |
| 3193 | QFixed perItemWidth = glyphWidth / clusterLength; | - |
| 3194 | if (perItemWidth <= 0) | - |
| 3195 | return si->position + clusterStart; | - |
| 3196 | QFixed left = x > edge ? edge : edge - glyphWidth; | - |
| 3197 | int n = ((x - left) / perItemWidth).floor().toInt(); | - |
| 3198 | QFixed dist = x - left - n * perItemWidth; | - |
| 3199 | int closestItem = dist > (perItemWidth / 2) ? n + 1 : n; | - |
| 3200 | if (cursorOnCharacter && closestItem > 0) | - |
| 3201 | closestItem--; | - |
| 3202 | int pos = clusterStart + closestItem; | - |
| 3203 | | - |
| 3204 | while (pos < end && !attrs[pos].graphemeBoundary) | - |
| 3205 | pos++; | - |
| 3206 | return si->position + pos; | - |
| 3207 | } | - |
| 3208 | return si->position + end; | - |
| 3209 | } | - |
| 3210 | | - |
| 3211 | int QTextEngine::previousLogicalPosition(int oldPos) const | - |
| 3212 | { | - |
| 3213 | const QCharAttributes *attrs = attributes(); | - |
| 3214 | int len = block.isValid() ? block.length() - 1 | - |
| 3215 | : layoutData->string.length(); | - |
| 3216 | Q_ASSERT(len <= layoutData->string.length()); | - |
| 3217 | if (!attrs || oldPos <= 0 || oldPos > len) | - |
| 3218 | return oldPos; | - |
| 3219 | | - |
| 3220 | oldPos--; | - |
| 3221 | while (oldPos && !attrs[oldPos].graphemeBoundary) | - |
| 3222 | oldPos--; | - |
| 3223 | return oldPos; | - |
| 3224 | } | - |
| 3225 | | - |
| 3226 | int QTextEngine::nextLogicalPosition(int oldPos) const | - |
| 3227 | { | - |
| 3228 | const QCharAttributes *attrs = attributes(); | - |
| 3229 | int len = block.isValid() ? block.length() - 1 | - |
| 3230 | : layoutData->string.length(); | - |
| 3231 | Q_ASSERT(len <= layoutData->string.length()); | - |
| 3232 | if (!attrs || oldPos < 0 || oldPos >= len) | - |
| 3233 | return oldPos; | - |
| 3234 | | - |
| 3235 | oldPos++; | - |
| 3236 | while (oldPos < len && !attrs[oldPos].graphemeBoundary) | - |
| 3237 | oldPos++; | - |
| 3238 | return oldPos; | - |
| 3239 | } | - |
| 3240 | | - |
| 3241 | int QTextEngine::lineNumberForTextPosition(int pos) | - |
| 3242 | { | - |
| 3243 | if (!layoutData) | - |
| 3244 | itemize(); | - |
| 3245 | if (pos == layoutData->string.length() && lines.size()) | - |
| 3246 | return lines.size() - 1; | - |
| 3247 | for (int i = 0; i < lines.size(); ++i) { | - |
| 3248 | const QScriptLine& line = lines[i]; | - |
| 3249 | if (line.from + line.length + line.trailingSpaces > pos) | - |
| 3250 | return i; | - |
| 3251 | } | - |
| 3252 | return -1; | - |
| 3253 | } | - |
| 3254 | | - |
| 3255 | void QTextEngine::insertionPointsForLine(int lineNum, QVector<int> &insertionPoints) | - |
| 3256 | { | - |
| 3257 | QTextLineItemIterator iterator(this, lineNum); | - |
| 3258 | | - |
| 3259 | insertionPoints.reserve(iterator.line.length); | - |
| 3260 | | - |
| 3261 | bool lastLine = lineNum >= lines.size() - 1; | - |
| 3262 | | - |
| 3263 | while (!iterator.atEnd()) { | - |
| 3264 | const QScriptItem &si = iterator.next(); | - |
| 3265 | | - |
| 3266 | int end = iterator.itemEnd; | - |
| 3267 | if (lastLine && iterator.item == iterator.lastItem) | - |
| 3268 | ++end; | - |
| 3269 | if (si.analysis.bidiLevel % 2) { | - |
| 3270 | for (int i = end - 1; i >= iterator.itemStart; --i) | - |
| 3271 | insertionPoints.push_back(i); | - |
| 3272 | } else { | - |
| 3273 | for (int i = iterator.itemStart; i < end; ++i) | - |
| 3274 | insertionPoints.push_back(i); | - |
| 3275 | } | - |
| 3276 | } | - |
| 3277 | } | - |
| 3278 | | - |
| 3279 | int QTextEngine::endOfLine(int lineNum) | - |
| 3280 | { | - |
| 3281 | QVector<int> insertionPoints; | - |
| 3282 | insertionPointsForLine(lineNum, insertionPoints); | - |
| 3283 | | - |
| 3284 | if (insertionPoints.size() > 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3285 | return insertionPoints.lastconstLast(); never executed: return insertionPoints.constLast(); | 0 |
| 3286 | return 0; never executed: return 0; | 0 |
| 3287 | } | - |
| 3288 | | - |
| 3289 | int QTextEngine::beginningOfLine(int lineNum) | - |
| 3290 | { | - |
| 3291 | QVector<int> insertionPoints; | - |
| 3292 | insertionPointsForLine(lineNum, insertionPoints); | - |
| 3293 | | - |
| 3294 | if (insertionPoints.size() > 0)| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3295 | return insertionPoints.firstconstFirst(); never executed: return insertionPoints.constFirst(); | 0 |
| 3296 | return 0; never executed: return 0; | 0 |
| 3297 | } | - |
| 3298 | | - |
| 3299 | int QTextEngine::positionAfterVisualMovement(int pos, QTextCursor::MoveOperation op) | - |
| 3300 | { | - |
| 3301 | itemize(); | - |
| 3302 | | - |
| 3303 | bool moveRight = (op == QTextCursor::Right); | - |
| 3304 | bool alignRight = isRightToLeft(); | - |
| 3305 | if (!layoutData->hasBidi) | - |
| 3306 | return moveRight ^ alignRight ? nextLogicalPosition(pos) : previousLogicalPosition(pos); | - |
| 3307 | | - |
| 3308 | int lineNum = lineNumberForTextPosition(pos); | - |
| 3309 | if (lineNum < 0) | - |
| 3310 | return pos; | - |
| 3311 | | - |
| 3312 | QVector<int> insertionPoints; | - |
| 3313 | insertionPointsForLine(lineNum, insertionPoints); | - |
| 3314 | int i, max = insertionPoints.size(); | - |
| 3315 | for (i = 0; i < max; i++) | - |
| 3316 | if (pos == insertionPoints[i]) { | - |
| 3317 | if (moveRight) { | - |
| 3318 | if (i + 1 < max) | - |
| 3319 | return insertionPoints[i + 1]; | - |
| 3320 | } else { | - |
| 3321 | if (i > 0) | - |
| 3322 | return insertionPoints[i - 1]; | - |
| 3323 | } | - |
| 3324 | | - |
| 3325 | if (moveRight ^ alignRight) { | - |
| 3326 | if (lineNum + 1 < lines.size()) | - |
| 3327 | return alignRight ? endOfLine(lineNum + 1) : beginningOfLine(lineNum + 1); | - |
| 3328 | } | - |
| 3329 | else { | - |
| 3330 | if (lineNum > 0) | - |
| 3331 | return alignRight ? beginningOfLine(lineNum - 1) : endOfLine(lineNum - 1); | - |
| 3332 | } | - |
| 3333 | | - |
| 3334 | break; | - |
| 3335 | } | - |
| 3336 | | - |
| 3337 | return pos; | - |
| 3338 | } | - |
| 3339 | | - |
| 3340 | void QTextEngine::addItemDecoration(QPainter *painter, const QLineF &line, ItemDecorationList *decorationList) | - |
| 3341 | { | - |
| 3342 | if (delayDecorations) { | - |
| 3343 | decorationList->append(ItemDecoration(line.x1(), line.x2(), line.y1(), painter->pen())); | - |
| 3344 | } else { | - |
| 3345 | painter->drawLine(line); | - |
| 3346 | } | - |
| 3347 | } | - |
| 3348 | | - |
| 3349 | void QTextEngine::addUnderline(QPainter *painter, const QLineF &line) | - |
| 3350 | { | - |
| 3351 | | - |
| 3352 | addItemDecoration(painter, line, &underlineList); | - |
| 3353 | } | - |
| 3354 | | - |
| 3355 | void QTextEngine::addStrikeOut(QPainter *painter, const QLineF &line) | - |
| 3356 | { | - |
| 3357 | addItemDecoration(painter, line, &strikeOutList); | - |
| 3358 | } | - |
| 3359 | | - |
| 3360 | void QTextEngine::addOverline(QPainter *painter, const QLineF &line) | - |
| 3361 | { | - |
| 3362 | addItemDecoration(painter, line, &overlineList); | - |
| 3363 | } | - |
| 3364 | | - |
| 3365 | void QTextEngine::drawItemDecorationList(QPainter *painter, const ItemDecorationList &decorationList) | - |
| 3366 | { | - |
| 3367 | | - |
| 3368 | if (decorationList.isEmpty())| TRUE | never evaluated | | FALSE | never evaluated |
| 0 |
| 3369 | return; never executed: return; | 0 |
| 3370 | | - |
| 3371 | foreachfor (const ItemDecoration &decoration, : decorationList) { | - |
| 3372 | painter->setPen(decoration.pen); | - |
| 3373 | painter->drawLine(QLineF(decoration.x1, decoration.y, decoration.x2, decoration.y)); | - |
| 3374 | } never executed: end of block | 0 |
| 3375 | } never executed: end of block | 0 |
| 3376 | | - |
| 3377 | void QTextEngine::drawDecorations(QPainter *painter) | - |
| 3378 | { | - |
| 3379 | QPen oldPen = painter->pen(); | - |
| 3380 | | - |
| 3381 | bool wasCompatiblePainting = painter->renderHints() | - |
| 3382 | & QPainter::Qt4CompatiblePainting; | - |
| 3383 | | - |
| 3384 | if (wasCompatiblePainting) | - |
| 3385 | painter->setRenderHint(QPainter::Qt4CompatiblePainting, false); | - |
| 3386 | | - |
| 3387 | adjustUnderlines(); | - |
| 3388 | drawItemDecorationList(painter, underlineList); | - |
| 3389 | drawItemDecorationList(painter, strikeOutList); | - |
| 3390 | drawItemDecorationList(painter, overlineList); | - |
| 3391 | | - |
| 3392 | clearDecorations(); | - |
| 3393 | | - |
| 3394 | if (wasCompatiblePainting) | - |
| 3395 | painter->setRenderHint(QPainter::Qt4CompatiblePainting); | - |
| 3396 | | - |
| 3397 | painter->setPen(oldPen); | - |
| 3398 | } | - |
| 3399 | | - |
| 3400 | void QTextEngine::clearDecorations() | - |
| 3401 | { | - |
| 3402 | underlineList.clear(); | - |
| 3403 | strikeOutList.clear(); | - |
| 3404 | overlineList.clear(); | - |
| 3405 | } | - |
| 3406 | | - |
| 3407 | void QTextEngine::adjustUnderlines() | - |
| 3408 | { | - |
| 3409 | | - |
| 3410 | if (underlineList.isEmpty()) | - |
| 3411 | return; | - |
| 3412 | | - |
| 3413 | ItemDecorationList::iterator start = underlineList.begin(); | - |
| 3414 | ItemDecorationList::iterator end = underlineList.end(); | - |
| 3415 | ItemDecorationList::iterator it = start; | - |
| 3416 | qreal underlinePos = start->y; | - |
| 3417 | qreal penWidth = start->pen.widthF(); | - |
| 3418 | qreal lastLineEnd = start->x1; | - |
| 3419 | | - |
| 3420 | while (it != end) { | - |
| 3421 | if (qFuzzyCompare(lastLineEnd, it->x1)) { | - |
| 3422 | underlinePos = qMax(underlinePos, it->y); | - |
| 3423 | penWidth = qMax(penWidth, it->pen.widthF()); | - |
| 3424 | } else { | - |
| 3425 | adjustUnderlines(start, it, underlinePos, penWidth); | - |
| 3426 | start = it; | - |
| 3427 | underlinePos = start->y; | - |
| 3428 | penWidth = start->pen.widthF(); | - |
| 3429 | } | - |
| 3430 | lastLineEnd = it->x2; | - |
| 3431 | ++it; | - |
| 3432 | } | - |
| 3433 | | - |
| 3434 | adjustUnderlines(start, end, underlinePos, penWidth); | - |
| 3435 | } | - |
| 3436 | | - |
| 3437 | void QTextEngine::adjustUnderlines(ItemDecorationList::iterator start, | - |
| 3438 | ItemDecorationList::iterator end, | - |
| 3439 | qreal underlinePos, qreal penWidth) | - |
| 3440 | { | - |
| 3441 | for (ItemDecorationList::iterator it = start; it != end; ++it) { | - |
| 3442 | it->y = underlinePos; | - |
| 3443 | it->pen.setWidthF(penWidth); | - |
| 3444 | } | - |
| 3445 | } | - |
| 3446 | | - |
| 3447 | QStackTextEngine::QStackTextEngine(const QString &string, const QFont &f) | - |
| 3448 | : QTextEngine(string, f), | - |
| 3449 | _layoutData(string, _memory, MemSize) | - |
| 3450 | { | - |
| 3451 | stackEngine = true; | - |
| 3452 | layoutData = &_layoutData; | - |
| 3453 | } | - |
| 3454 | | - |
| 3455 | QTextItemInt::QTextItemInt(const QScriptItem &si, QFont *font, const QTextCharFormat &format) | - |
| 3456 | : justified(false), underlineStyle(QTextCharFormat::NoUnderline), charFormat(format), | - |
| 3457 | num_chars(0), chars(0), logClusters(0), f(0), fontEngine(0) | - |
| 3458 | { | - |
| 3459 | f = font; | - |
| 3460 | fontEngine = f->d->engineForScript(si.analysis.script); | - |
| 3461 | Q_ASSERT(fontEngine); | - |
| 3462 | | - |
| 3463 | initWithScriptItem(si); | - |
| 3464 | } | - |
| 3465 | | - |
| 3466 | QTextItemInt::QTextItemInt(const QGlyphLayout &g, QFont *font, const QChar *chars_, int numChars, QFontEngine *fe, const QTextCharFormat &format) | - |
| 3467 | : flags(0), justified(false), underlineStyle(QTextCharFormat::NoUnderline), charFormat(format), | - |
| 3468 | num_chars(numChars), chars(chars_), logClusters(0), f(font), glyphs(g), fontEngine(fe) | - |
| 3469 | { | - |
| 3470 | } | - |
| 3471 | | - |
| 3472 | | - |
| 3473 | void QTextItemInt::initWithScriptItem(const QScriptItem &si) | - |
| 3474 | { | - |
| 3475 | | - |
| 3476 | | - |
| 3477 | flags = 0; | - |
| 3478 | if (si.analysis.bidiLevel %2) | - |
| 3479 | flags |= QTextItem::RightToLeft; | - |
| 3480 | ascent = si.ascent; | - |
| 3481 | descent = si.descent; | - |
| 3482 | | - |
| 3483 | if (charFormat.hasProperty(QTextFormat::TextUnderlineStyle)) { | - |
| 3484 | underlineStyle = charFormat.underlineStyle(); | - |
| 3485 | } else if (charFormat.boolProperty(QTextFormat::FontUnderline) | - |
| 3486 | || f->d->underline) { | - |
| 3487 | underlineStyle = QTextCharFormat::SingleUnderline; | - |
| 3488 | } | - |
| 3489 | | - |
| 3490 | | - |
| 3491 | if (underlineStyle == QTextCharFormat::SingleUnderline) | - |
| 3492 | flags |= QTextItem::Underline; | - |
| 3493 | | - |
| 3494 | if (f->d->overline || charFormat.fontOverline()) | - |
| 3495 | flags |= QTextItem::Overline; | - |
| 3496 | if (f->d->strikeOut || charFormat.fontStrikeOut()) | - |
| 3497 | flags |= QTextItem::StrikeOut; | - |
| 3498 | } | - |
| 3499 | | - |
| 3500 | QTextItemInt QTextItemInt::midItem(QFontEngine *fontEngine, int firstGlyphIndex, int numGlyphs) const | - |
| 3501 | { | - |
| 3502 | QTextItemInt ti = *this; | - |
| 3503 | const int end = firstGlyphIndex + numGlyphs; | - |
| 3504 | ti.glyphs = glyphs.mid(firstGlyphIndex, numGlyphs); | - |
| 3505 | ti.fontEngine = fontEngine; | - |
| 3506 | | - |
| 3507 | if (logClusters && chars) { | - |
| 3508 | const int logClusterOffset = logClusters[0]; | - |
| 3509 | while (logClusters[ti.chars - chars] - logClusterOffset < firstGlyphIndex) | - |
| 3510 | ++ti.chars; | - |
| 3511 | | - |
| 3512 | ti.logClusters += (ti.chars - chars); | - |
| 3513 | | - |
| 3514 | ti.num_chars = 0; | - |
| 3515 | int char_start = ti.chars - chars; | - |
| 3516 | while (char_start + ti.num_chars < num_chars && ti.logClusters[ti.num_chars] - logClusterOffset < end) | - |
| 3517 | ++ti.num_chars; | - |
| 3518 | } | - |
| 3519 | return ti; | - |
| 3520 | } | - |
| 3521 | | - |
| 3522 | | - |
| 3523 | QTransform qt_true_matrix(qreal w, qreal h, QTransform x) | - |
| 3524 | { | - |
| 3525 | QRectF rect = x.mapRect(QRectF(0, 0, w, h)); | - |
| 3526 | return x * QTransform::fromTranslate(-rect.x(), -rect.y()); | - |
| 3527 | } | - |
| 3528 | | - |
| 3529 | | - |
| 3530 | glyph_metrics_t glyph_metrics_t::transformed(const QTransform &matrix) const | - |
| 3531 | { | - |
| 3532 | if (matrix.type() < QTransform::TxTranslate) | - |
| 3533 | return *this; | - |
| 3534 | | - |
| 3535 | glyph_metrics_t m = *this; | - |
| 3536 | | - |
| 3537 | qreal w = width.toReal(); | - |
| 3538 | qreal h = height.toReal(); | - |
| 3539 | QTransform xform = qt_true_matrix(w, h, matrix); | - |
| 3540 | | - |
| 3541 | QRectF rect(0, 0, w, h); | - |
| 3542 | rect = xform.mapRect(rect); | - |
| 3543 | m.width = QFixed::fromReal(rect.width()); | - |
| 3544 | m.height = QFixed::fromReal(rect.height()); | - |
| 3545 | | - |
| 3546 | QLineF l = xform.map(QLineF(x.toReal(), y.toReal(), xoff.toReal(), yoff.toReal())); | - |
| 3547 | | - |
| 3548 | m.x = QFixed::fromReal(l.x1()); | - |
| 3549 | m.y = QFixed::fromReal(l.y1()); | - |
| 3550 | | - |
| 3551 | | - |
| 3552 | m.xoff = QFixed::fromReal(l.dx()); | - |
| 3553 | m.yoff = QFixed::fromReal(l.dy()); | - |
| 3554 | | - |
| 3555 | return m; | - |
| 3556 | } | - |
| 3557 | | - |
| 3558 | QTextLineItemIterator::QTextLineItemIterator(QTextEngine *_eng, int _lineNum, const QPointF &pos, | - |
| 3559 | const QTextLayout::FormatRange *_selection) | - |
| 3560 | : eng(_eng), | - |
| 3561 | line(eng->lines[_lineNum]), | - |
| 3562 | si(0), | - |
| 3563 | lineNum(_lineNum), | - |
| 3564 | lineEnd(line.from + line.length), | - |
| 3565 | firstItem(eng->findItem(line.from)), | - |
| 3566 | lastItem(eng->findItem(lineEnd - 1, firstItem)), | - |
| 3567 | nItems((firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0), | - |
| 3568 | logicalItem(-1), | - |
| 3569 | item(-1), | - |
| 3570 | visualOrder(nItems), | - |
| 3571 | selection(_selection) | - |
| 3572 | { | - |
| 3573 | x = QFixed::fromReal(pos.x()); | - |
| 3574 | | - |
| 3575 | x += line.x; | - |
| 3576 | | - |
| 3577 | x += eng->alignLine(line); | - |
| 3578 | | - |
| 3579 | QVarLengthArray<uchar> levels(nItems); | - |
| 3580 | for (int i = 0; i < nItems; ++i) | - |
| 3581 | levels[i] = eng->layoutData->items[i+firstItem].analysis.bidiLevel; | - |
| 3582 | QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data()); | - |
| 3583 | | - |
| 3584 | eng->shapeLine(line); | - |
| 3585 | } | - |
| 3586 | | - |
| 3587 | QScriptItem &QTextLineItemIterator::next() | - |
| 3588 | { | - |
| 3589 | x += itemWidth; | - |
| 3590 | | - |
| 3591 | ++logicalItem; | - |
| 3592 | item = visualOrder[logicalItem] + firstItem; | - |
| 3593 | itemLength = eng->length(item); | - |
| 3594 | si = &eng->layoutData->items[item]; | - |
| 3595 | if (!si->num_glyphs) | - |
| 3596 | eng->shape(item); | - |
| 3597 | | - |
| 3598 | itemStart = qMax(line.from, si->position); | - |
| 3599 | itemEnd = qMin(lineEnd, si->position + itemLength); | - |
| 3600 | | - |
| 3601 | if (si->analysis.flags >= QScriptAnalysis::TabOrObject) { | - |
| 3602 | glyphsStart = 0; | - |
| 3603 | glyphsEnd = 1; | - |
| 3604 | itemWidth = si->width; | - |
| 3605 | return *si; | - |
| 3606 | } | - |
| 3607 | | - |
| 3608 | unsigned short *logClusters = eng->logClusters(si); | - |
| 3609 | QGlyphLayout glyphs = eng->shapedGlyphs(si); | - |
| 3610 | | - |
| 3611 | glyphsStart = logClusters[itemStart - si->position]; | - |
| 3612 | glyphsEnd = (itemEnd == si->position + itemLength) ? si->num_glyphs : logClusters[itemEnd - si->position]; | - |
| 3613 | | - |
| 3614 | | - |
| 3615 | if (si->position + itemLength >= lineEnd | - |
| 3616 | && eng->layoutData->string.at(lineEnd - 1).unicode() == QChar::SoftHyphen) | - |
| 3617 | glyphs.attributes[glyphsEnd - 1].dontPrint = false; | - |
| 3618 | | - |
| 3619 | itemWidth = 0; | - |
| 3620 | for (int g = glyphsStart; g < glyphsEnd; ++g) | - |
| 3621 | itemWidth += glyphs.effectiveAdvance(g); | - |
| 3622 | | - |
| 3623 | return *si; | - |
| 3624 | } | - |
| 3625 | | - |
| 3626 | bool QTextLineItemIterator::getSelectionBounds(QFixed *selectionX, QFixed *selectionWidth) const | - |
| 3627 | { | - |
| 3628 | *selectionX = *selectionWidth = 0; | - |
| 3629 | | - |
| 3630 | if (!selection) | - |
| 3631 | return false; | - |
| 3632 | | - |
| 3633 | if (si->analysis.flags >= QScriptAnalysis::TabOrObject) { | - |
| 3634 | if (si->position >= selection->start + selection->length | - |
| 3635 | || si->position + itemLength <= selection->start) | - |
| 3636 | return false; | - |
| 3637 | | - |
| 3638 | *selectionX = x; | - |
| 3639 | *selectionWidth = itemWidth; | - |
| 3640 | } else { | - |
| 3641 | unsigned short *logClusters = eng->logClusters(si); | - |
| 3642 | QGlyphLayout glyphs = eng->shapedGlyphs(si); | - |
| 3643 | | - |
| 3644 | int from = qMax(itemStart, selection->start) - si->position; | - |
| 3645 | int to = qMin(itemEnd, selection->start + selection->length) - si->position; | - |
| 3646 | if (from >= to) | - |
| 3647 | return false; | - |
| 3648 | | - |
| 3649 | int start_glyph = logClusters[from]; | - |
| 3650 | int end_glyph = (to == itemLength) ? si->num_glyphs : logClusters[to]; | - |
| 3651 | QFixed soff; | - |
| 3652 | QFixed swidth; | - |
| 3653 | if (si->analysis.bidiLevel %2) { | - |
| 3654 | for (int g = glyphsEnd - 1; g >= end_glyph; --g) | - |
| 3655 | soff += glyphs.effectiveAdvance(g); | - |
| 3656 | for (int g = end_glyph - 1; g >= start_glyph; --g) | - |
| 3657 | swidth += glyphs.effectiveAdvance(g); | - |
| 3658 | } else { | - |
| 3659 | for (int g = glyphsStart; g < start_glyph; ++g) | - |
| 3660 | soff += glyphs.effectiveAdvance(g); | - |
| 3661 | for (int g = start_glyph; g < end_glyph; ++g) | - |
| 3662 | swidth += glyphs.effectiveAdvance(g); | - |
| 3663 | } | - |
| 3664 | | - |
| 3665 | | - |
| 3666 | | - |
| 3667 | | - |
| 3668 | | - |
| 3669 | QFixed leftOffsetInLigature = eng->offsetInLigature(si, from, to, start_glyph); | - |
| 3670 | *selectionX = x + soff + leftOffsetInLigature; | - |
| 3671 | *selectionWidth = swidth - leftOffsetInLigature; | - |
| 3672 | | - |
| 3673 | | - |
| 3674 | | - |
| 3675 | *selectionWidth += eng->offsetInLigature(si, to, itemLength, end_glyph); | - |
| 3676 | } | - |
| 3677 | return true; | - |
| 3678 | } | - |
| 3679 | | - |
| 3680 | QT_END_NAMESPACE | - |
| | |