| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTEXTENGINE_P_H |
| 5 | #define QTEXTENGINE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/private/qtguiglobal_p.h> |
| 19 | #include "QtGui/qpaintengine.h" |
| 20 | #include "QtGui/qtextcursor.h" |
| 21 | #include "QtGui/qtextobject.h" |
| 22 | #include "QtGui/qtextoption.h" |
| 23 | #include "QtGui/qtextlayout.h" |
| 24 | |
| 25 | #include "QtCore/qdebug.h" |
| 26 | #include "QtCore/qlist.h" |
| 27 | #include "QtCore/qnamespace.h" |
| 28 | #include "QtCore/qset.h" |
| 29 | #include <QtCore/qspan.h> |
| 30 | #include "QtCore/qstring.h" |
| 31 | #include "QtCore/qvarlengtharray.h" |
| 32 | |
| 33 | #include "private/qfixed_p.h" |
| 34 | #include "private/qfont_p.h" |
| 35 | #include "private/qtextformat_p.h" |
| 36 | #include "private/qunicodetools_p.h" |
| 37 | #ifndef QT_BUILD_COMPAT_LIB |
| 38 | #include "private/qtextdocument_p.h" |
| 39 | #endif |
| 40 | |
| 41 | #include <stdlib.h> |
| 42 | #include <vector> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | class QFontPrivate; |
| 47 | class QFontEngine; |
| 48 | |
| 49 | class QString; |
| 50 | class QPainter; |
| 51 | |
| 52 | class QAbstractTextDocumentLayout; |
| 53 | |
| 54 | typedef quint32 glyph_t; |
| 55 | |
| 56 | // this uses the same coordinate system as Qt, but a different one to freetype. |
| 57 | // * y is usually negative, and is equal to the ascent. |
| 58 | // * negative yoff means the following stuff is drawn higher up. |
| 59 | // the characters bounding rect is given by QRect(x,y,width,height), its advance by |
| 60 | // xoo and yoff |
| 61 | struct Q_GUI_EXPORT glyph_metrics_t |
| 62 | { |
| 63 | inline glyph_metrics_t() |
| 64 | : x(100000), y(100000) {} |
| 65 | inline glyph_metrics_t(QFixed _x, QFixed _y, QFixed _width, QFixed _height, QFixed _xoff, QFixed _yoff) |
| 66 | : x(_x), |
| 67 | y(_y), |
| 68 | width(_width), |
| 69 | height(_height), |
| 70 | xoff(_xoff), |
| 71 | yoff(_yoff) |
| 72 | {} |
| 73 | QFixed x; |
| 74 | QFixed y; |
| 75 | QFixed width; |
| 76 | QFixed height; |
| 77 | QFixed xoff; |
| 78 | QFixed yoff; |
| 79 | |
| 80 | glyph_metrics_t transformed(const QTransform &xform) const; |
| 81 | inline bool isValid() const {return x != 100000 && y != 100000;} |
| 82 | |
| 83 | inline QFixed leftBearing() const |
| 84 | { |
| 85 | if (!isValid()) |
| 86 | return QFixed(); |
| 87 | |
| 88 | return x; |
| 89 | } |
| 90 | |
| 91 | inline QFixed rightBearing() const |
| 92 | { |
| 93 | if (!isValid()) |
| 94 | return QFixed(); |
| 95 | |
| 96 | return xoff - x - width; |
| 97 | } |
| 98 | }; |
| 99 | Q_DECLARE_TYPEINFO(glyph_metrics_t, Q_PRIMITIVE_TYPE); |
| 100 | |
| 101 | struct Q_AUTOTEST_EXPORT QScriptAnalysis |
| 102 | { |
| 103 | enum Flags { |
| 104 | None = 0, |
| 105 | Lowercase = 1, |
| 106 | Uppercase = 2, |
| 107 | SmallCaps = 3, |
| 108 | LineOrParagraphSeparator = 4, |
| 109 | Space = 5, |
| 110 | SpaceTabOrObject = Space, |
| 111 | Nbsp = 6, |
| 112 | Tab = 7, |
| 113 | TabOrObject = Tab, |
| 114 | Object = 8 |
| 115 | }; |
| 116 | enum BidiFlags { |
| 117 | BidiBN = 1, |
| 118 | BidiMaybeResetToParagraphLevel = 2, |
| 119 | BidiResetToParagraphLevel = 4, |
| 120 | BidiMirrored = 8 |
| 121 | }; |
| 122 | unsigned short script : 8; |
| 123 | unsigned short flags : 4; |
| 124 | unsigned short bidiFlags : 4; |
| 125 | unsigned short bidiLevel : 8; // Unicode Bidi algorithm embedding level (0-125) |
| 126 | QChar::Direction bidiDirection : 8; // used when running the bidi algorithm |
| 127 | inline bool operator == (const QScriptAnalysis &other) const { |
| 128 | return script == other.script && bidiLevel == other.bidiLevel && flags == other.flags; |
| 129 | } |
| 130 | }; |
| 131 | Q_DECLARE_TYPEINFO(QScriptAnalysis, Q_PRIMITIVE_TYPE); |
| 132 | |
| 133 | struct QGlyphJustification |
| 134 | { |
| 135 | inline QGlyphJustification() |
| 136 | : type(0), nKashidas(0), space_18d6(0) |
| 137 | {} |
| 138 | |
| 139 | enum JustificationType { |
| 140 | JustifyNone, |
| 141 | JustifySpace, |
| 142 | JustifyKashida |
| 143 | }; |
| 144 | |
| 145 | uint type :2; |
| 146 | uint nKashidas : 6; // more do not make sense... |
| 147 | uint space_18d6 : 24; |
| 148 | }; |
| 149 | Q_DECLARE_TYPEINFO(QGlyphJustification, Q_PRIMITIVE_TYPE); |
| 150 | |
| 151 | struct QGlyphAttributes { |
| 152 | uchar clusterStart : 1; |
| 153 | uchar dontPrint : 1; |
| 154 | uchar justification : 4; |
| 155 | uchar reserved : 2; |
| 156 | }; |
| 157 | static_assert(sizeof(QGlyphAttributes) == 1); |
| 158 | Q_DECLARE_TYPEINFO(QGlyphAttributes, Q_PRIMITIVE_TYPE); |
| 159 | |
| 160 | struct QGlyphLayout |
| 161 | { |
| 162 | static constexpr qsizetype SpaceNeeded = sizeof(glyph_t) + sizeof(QFixed) + sizeof(QFixedPoint) |
| 163 | + sizeof(QGlyphAttributes) + sizeof(QGlyphJustification); |
| 164 | |
| 165 | // init to 0 not needed, done when shaping |
| 166 | QFixedPoint *offsets; // 8 bytes per element |
| 167 | glyph_t *glyphs; // 4 bytes per element |
| 168 | QFixed *advances; // 4 bytes per element |
| 169 | QGlyphJustification *justifications; // 4 bytes per element |
| 170 | QGlyphAttributes *attributes; // 1 byte per element |
| 171 | |
| 172 | int numGlyphs; |
| 173 | |
| 174 | inline QGlyphLayout() : numGlyphs(0) {} |
| 175 | |
| 176 | inline explicit QGlyphLayout(char *address, int totalGlyphs) |
| 177 | { |
| 178 | offsets = reinterpret_cast<QFixedPoint *>(address); |
| 179 | qsizetype offset = totalGlyphs * sizeof(QFixedPoint); |
| 180 | glyphs = reinterpret_cast<glyph_t *>(address + offset); |
| 181 | offset += totalGlyphs * sizeof(glyph_t); |
| 182 | advances = reinterpret_cast<QFixed *>(address + offset); |
| 183 | offset += totalGlyphs * sizeof(QFixed); |
| 184 | justifications = reinterpret_cast<QGlyphJustification *>(address + offset); |
| 185 | offset += totalGlyphs * sizeof(QGlyphJustification); |
| 186 | attributes = reinterpret_cast<QGlyphAttributes *>(address + offset); |
| 187 | numGlyphs = totalGlyphs; |
| 188 | } |
| 189 | |
| 190 | inline QGlyphLayout mid(int position, int n = -1) const { |
| 191 | QGlyphLayout copy = *this; |
| 192 | copy.glyphs += position; |
| 193 | copy.advances += position; |
| 194 | copy.offsets += position; |
| 195 | copy.justifications += position; |
| 196 | copy.attributes += position; |
| 197 | if (n == -1) |
| 198 | copy.numGlyphs -= position; |
| 199 | else |
| 200 | copy.numGlyphs = n; |
| 201 | return copy; |
| 202 | } |
| 203 | |
| 204 | inline QFixed effectiveAdvance(int item) const |
| 205 | { return (advances[item] + QFixed::fromFixed(fixed: justifications[item].space_18d6)) * !attributes[item].dontPrint; } |
| 206 | |
| 207 | inline void clear(int first = 0, int last = -1) { |
| 208 | if (last == -1) |
| 209 | last = numGlyphs; |
| 210 | if (first == 0 && last == numGlyphs |
| 211 | && reinterpret_cast<char *>(offsets + numGlyphs) == reinterpret_cast<char *>(glyphs)) { |
| 212 | memset(s: static_cast<void *>(offsets), c: 0, n: qsizetype(numGlyphs) * SpaceNeeded); |
| 213 | } else { |
| 214 | const int num = last - first; |
| 215 | memset(s: static_cast<void *>(offsets + first), c: 0, n: num * sizeof(QFixedPoint)); |
| 216 | memset(s: glyphs + first, c: 0, n: num * sizeof(glyph_t)); |
| 217 | memset(s: static_cast<void *>(advances + first), c: 0, n: num * sizeof(QFixed)); |
| 218 | memset(s: static_cast<void *>(justifications + first), c: 0, n: num * sizeof(QGlyphJustification)); |
| 219 | memset(s: attributes + first, c: 0, n: num * sizeof(QGlyphAttributes)); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | inline char *data() { |
| 224 | return reinterpret_cast<char *>(offsets); |
| 225 | } |
| 226 | |
| 227 | void copy(QGlyphLayout *other); |
| 228 | void grow(char *address, int totalGlyphs); |
| 229 | }; |
| 230 | |
| 231 | class QVarLengthGlyphLayoutArray : private QVarLengthArray<void *>, public QGlyphLayout |
| 232 | { |
| 233 | private: |
| 234 | typedef QVarLengthArray<void *> Array; |
| 235 | public: |
| 236 | QVarLengthGlyphLayoutArray(int totalGlyphs) |
| 237 | : Array((totalGlyphs * SpaceNeeded) / sizeof(void *) + 1) |
| 238 | , QGlyphLayout(reinterpret_cast<char *>(Array::data()), totalGlyphs) |
| 239 | { |
| 240 | memset(s: Array::data(), c: 0, n: Array::size() * sizeof(void *)); |
| 241 | } |
| 242 | |
| 243 | void resize(int totalGlyphs) |
| 244 | { |
| 245 | Array::resize(sz: (totalGlyphs * SpaceNeeded) / sizeof(void *) + 1); |
| 246 | |
| 247 | *((QGlyphLayout *)this) = QGlyphLayout(reinterpret_cast<char *>(Array::data()), totalGlyphs); |
| 248 | memset(s: Array::data(), c: 0, n: Array::size() * sizeof(void *)); |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | template <int N> struct QGlyphLayoutArray : public QGlyphLayout |
| 253 | { |
| 254 | public: |
| 255 | QGlyphLayoutArray() |
| 256 | : QGlyphLayout(reinterpret_cast<char *>(buffer), N) |
| 257 | { |
| 258 | memset(buffer, 0, sizeof(buffer)); |
| 259 | } |
| 260 | |
| 261 | private: |
| 262 | void *buffer[(N * SpaceNeeded) / sizeof(void *) + 1]; |
| 263 | }; |
| 264 | |
| 265 | struct QScriptItem; |
| 266 | /// Internal QTextItem |
| 267 | class QTextItemInt : public QTextItem |
| 268 | { |
| 269 | public: |
| 270 | inline QTextItemInt() = default; |
| 271 | QTextItemInt(const QScriptItem &si, QFont *font, const QTextCharFormat &format = QTextCharFormat()); |
| 272 | QTextItemInt(const QGlyphLayout &g, QFont *font, const QChar *chars, int numChars, QFontEngine *fe, |
| 273 | const QTextCharFormat &format = QTextCharFormat()); |
| 274 | |
| 275 | /// copy the structure items, adjusting the glyphs arrays to the right subarrays. |
| 276 | /// the width of the returned QTextItemInt is not adjusted, for speed reasons |
| 277 | QTextItemInt midItem(QFontEngine *fontEngine, int firstGlyphIndex, int numGlyphs) const; |
| 278 | void initWithScriptItem(const QScriptItem &si); |
| 279 | |
| 280 | QFixed descent; |
| 281 | QFixed ascent; |
| 282 | QFixed width; |
| 283 | |
| 284 | RenderFlags flags; |
| 285 | bool justified = false; |
| 286 | QTextCharFormat::UnderlineStyle underlineStyle = QTextCharFormat::NoUnderline; |
| 287 | const QTextCharFormat charFormat; |
| 288 | int num_chars = 0; |
| 289 | const QChar *chars = nullptr; |
| 290 | const unsigned short *logClusters = nullptr; |
| 291 | const QFont *f = nullptr; |
| 292 | |
| 293 | QGlyphLayout glyphs; |
| 294 | QFontEngine *fontEngine = nullptr; |
| 295 | }; |
| 296 | |
| 297 | struct QScriptItem |
| 298 | { |
| 299 | constexpr QScriptItem(int p, QScriptAnalysis a) noexcept |
| 300 | : position(p), analysis(a), |
| 301 | num_glyphs(0), descent(-1), ascent(-1), leading(-1), width(-1), |
| 302 | glyph_data_offset(0) {} |
| 303 | |
| 304 | int position; |
| 305 | QScriptAnalysis analysis; |
| 306 | unsigned short num_glyphs; |
| 307 | QFixed descent; |
| 308 | QFixed ascent; |
| 309 | QFixed leading; |
| 310 | QFixed width; |
| 311 | int glyph_data_offset; |
| 312 | constexpr QFixed height() const noexcept { return ascent + descent; } |
| 313 | private: |
| 314 | friend class QList<QScriptItem>; |
| 315 | QScriptItem() {} // for QList, don't use |
| 316 | }; |
| 317 | Q_DECLARE_TYPEINFO(QScriptItem, Q_PRIMITIVE_TYPE); |
| 318 | |
| 319 | typedef QList<QScriptItem> QScriptItemArray; |
| 320 | |
| 321 | struct Q_AUTOTEST_EXPORT QScriptLine |
| 322 | { |
| 323 | // created and filled in QTextLine::layout_helper |
| 324 | QScriptLine() |
| 325 | : from(0), trailingSpaces(0), length(0), |
| 326 | justified(0), gridfitted(0), |
| 327 | hasTrailingSpaces(0), leadingIncluded(0) {} |
| 328 | QFixed descent; |
| 329 | QFixed ascent; |
| 330 | QFixed leading; |
| 331 | QFixed x; |
| 332 | QFixed y; |
| 333 | QFixed width; |
| 334 | QFixed textWidth; |
| 335 | QFixed textAdvance; |
| 336 | int from; |
| 337 | unsigned short trailingSpaces; |
| 338 | signed int length : 28; |
| 339 | mutable uint justified : 1; |
| 340 | mutable uint gridfitted : 1; |
| 341 | uint hasTrailingSpaces : 1; |
| 342 | uint leadingIncluded : 1; |
| 343 | QFixed height() const { return ascent + descent |
| 344 | + (leadingIncluded? qMax(a: QFixed(),b: leading) : QFixed()); } |
| 345 | QFixed base() const { return ascent; } |
| 346 | void setDefaultHeight(QTextEngine *eng); |
| 347 | void operator+=(const QScriptLine &other); |
| 348 | }; |
| 349 | Q_DECLARE_TYPEINFO(QScriptLine, Q_PRIMITIVE_TYPE); |
| 350 | |
| 351 | |
| 352 | inline void QScriptLine::operator+=(const QScriptLine &other) |
| 353 | { |
| 354 | leading= qMax(a: leading + ascent, b: other.leading + other.ascent) - qMax(a: ascent, b: other.ascent); |
| 355 | descent = qMax(a: descent, b: other.descent); |
| 356 | ascent = qMax(a: ascent, b: other.ascent); |
| 357 | textWidth += other.textWidth; |
| 358 | length += other.length; |
| 359 | } |
| 360 | |
| 361 | typedef QList<QScriptLine> QScriptLineArray; |
| 362 | |
| 363 | class QFontPrivate; |
| 364 | class QTextFormatCollection; |
| 365 | |
| 366 | class Q_GUI_EXPORT QTextEngine { |
| 367 | public: |
| 368 | enum LayoutState { |
| 369 | LayoutEmpty, |
| 370 | InLayout, |
| 371 | LayoutFailed |
| 372 | }; |
| 373 | struct Q_GUI_EXPORT LayoutData { |
| 374 | LayoutData(const QString &str, void **stack_memory, qsizetype mem_size); |
| 375 | LayoutData(); |
| 376 | ~LayoutData(); |
| 377 | mutable QScriptItemArray items; |
| 378 | qsizetype allocated; |
| 379 | qsizetype available_glyphs; |
| 380 | void **memory; |
| 381 | unsigned short *logClustersPtr; |
| 382 | QGlyphLayout glyphLayout; |
| 383 | mutable int used; |
| 384 | uint hasBidi : 1; |
| 385 | uint layoutState : 2; |
| 386 | uint memory_on_stack : 1; |
| 387 | uint haveCharAttributes : 1; |
| 388 | QFixed currentMaxWidth; |
| 389 | QString string; |
| 390 | bool reallocate(int totalGlyphs); |
| 391 | }; |
| 392 | |
| 393 | struct ItemDecoration { |
| 394 | ItemDecoration() { } // for QList, don't use |
| 395 | ItemDecoration(qreal x1, qreal x2, qreal y, const QPen &pen): |
| 396 | x1(x1), x2(x2), y(y), pen(pen) {} |
| 397 | |
| 398 | qreal x1; |
| 399 | qreal x2; |
| 400 | qreal y; |
| 401 | QPen pen; |
| 402 | }; |
| 403 | |
| 404 | typedef QList<ItemDecoration> ItemDecorationList; |
| 405 | |
| 406 | QTextEngine(); |
| 407 | QTextEngine(const QString &str, const QFont &f); |
| 408 | ~QTextEngine(); |
| 409 | |
| 410 | enum Mode { |
| 411 | WidthOnly = 0x07 |
| 412 | }; |
| 413 | |
| 414 | void invalidate(); |
| 415 | void clearLineData(); |
| 416 | |
| 417 | void validate() const; |
| 418 | void itemize() const; |
| 419 | |
| 420 | bool isRightToLeft() const; |
| 421 | static void bidiReorder(int numRuns, const quint8 *levels, int *visualOrder); |
| 422 | |
| 423 | const QCharAttributes *attributes() const; |
| 424 | |
| 425 | void shape(int item) const; |
| 426 | |
| 427 | void justify(const QScriptLine &si); |
| 428 | QFixed alignLine(const QScriptLine &line); |
| 429 | |
| 430 | QFixed width(int charFrom, int numChars) const; |
| 431 | glyph_metrics_t boundingBox(int from, int len) const; |
| 432 | glyph_metrics_t tightBoundingBox(int from, int len) const; |
| 433 | |
| 434 | int length(int item) const { |
| 435 | const QScriptItem &si = layoutData->items[item]; |
| 436 | int from = si.position; |
| 437 | item++; |
| 438 | return (item < layoutData->items.size() ? layoutData->items[item].position : layoutData->string.size()) - from; |
| 439 | } |
| 440 | int length(const QScriptItem *si) const { |
| 441 | int end; |
| 442 | if (si + 1 < layoutData->items.constData()+ layoutData->items.size()) |
| 443 | end = (si+1)->position; |
| 444 | else |
| 445 | end = layoutData->string.size(); |
| 446 | return end - si->position; |
| 447 | } |
| 448 | |
| 449 | QFontEngine *fontEngine(const QScriptItem &si, QFixed *ascent = nullptr, QFixed *descent = nullptr, QFixed *leading = nullptr) const; |
| 450 | QFont font(const QScriptItem &si) const; |
| 451 | inline QFont font() const { return fnt; } |
| 452 | |
| 453 | /** |
| 454 | * Returns a pointer to an array of log clusters, offset at the script item. |
| 455 | * Each item in the array is a unsigned short. For each character in the original string there is an entry in the table |
| 456 | * so there is a one to one correlation in indexes between the original text and the index in the logcluster. |
| 457 | * The value of each item is the position in the glyphs array. Multiple similar pointers in the logclusters array imply |
| 458 | * that one glyph is used for more than one character. |
| 459 | * \sa glyphs() |
| 460 | */ |
| 461 | inline unsigned short *logClusters(const QScriptItem *si) const |
| 462 | { return layoutData->logClustersPtr+si->position; } |
| 463 | /** |
| 464 | * Returns an array of QGlyphLayout items, offset at the script item. |
| 465 | * Each item in the array matches one glyph in the text, storing the advance, position etc. |
| 466 | * The returned item's length equals to the number of available glyphs. This may be more |
| 467 | * than what was actually shaped. |
| 468 | * \sa logClusters() |
| 469 | */ |
| 470 | inline QGlyphLayout availableGlyphs(const QScriptItem *si) const { |
| 471 | return layoutData->glyphLayout.mid(position: si->glyph_data_offset); |
| 472 | } |
| 473 | /** |
| 474 | * Returns an array of QGlyphLayout items, offset at the script item. |
| 475 | * Each item in the array matches one glyph in the text, storing the advance, position etc. |
| 476 | * The returned item's length equals to the number of shaped glyphs. |
| 477 | * \sa logClusters() |
| 478 | */ |
| 479 | inline QGlyphLayout shapedGlyphs(const QScriptItem *si) const { |
| 480 | return layoutData->glyphLayout.mid(position: si->glyph_data_offset, n: si->num_glyphs); |
| 481 | } |
| 482 | |
| 483 | inline bool ensureSpace(int nGlyphs) const { |
| 484 | if (layoutData->glyphLayout.numGlyphs - layoutData->used < nGlyphs) |
| 485 | return layoutData->reallocate(totalGlyphs: (((layoutData->used + nGlyphs)*3/2 + 15) >> 4) << 4); |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | void freeMemory(); |
| 490 | |
| 491 | int findItem(int strPos, int firstItem = 0) const; |
| 492 | inline QTextFormatCollection *formatCollection() const { |
| 493 | if (QTextDocumentPrivate::get(block) != nullptr) |
| 494 | return const_cast<QTextFormatCollection *>(QTextDocumentPrivate::get(block)->formatCollection()); |
| 495 | return specialData ? specialData->formatCollection.data() : nullptr; |
| 496 | } |
| 497 | QTextCharFormat format(const QScriptItem *si) const; |
| 498 | inline QAbstractTextDocumentLayout *docLayout() const { |
| 499 | Q_ASSERT(QTextDocumentPrivate::get(block) != nullptr); |
| 500 | return QTextDocumentPrivate::get(block)->document()->documentLayout(); |
| 501 | } |
| 502 | int formatIndex(const QScriptItem *si) const; |
| 503 | |
| 504 | /// returns the width of tab at index (in the tabs array) with the tab-start at position x |
| 505 | QFixed calculateTabWidth(int index, QFixed x) const; |
| 506 | |
| 507 | mutable QScriptLineArray lines; |
| 508 | |
| 509 | private: |
| 510 | struct FontEngineCache { |
| 511 | FontEngineCache(); |
| 512 | mutable QFontEngine *prevFontEngine; |
| 513 | mutable QFontEngine *prevScaledFontEngine; |
| 514 | mutable int prevScript; |
| 515 | mutable int prevPosition; |
| 516 | mutable int prevLength; |
| 517 | inline void reset() { |
| 518 | prevFontEngine = nullptr; |
| 519 | prevScaledFontEngine = nullptr; |
| 520 | prevScript = -1; |
| 521 | prevPosition = -1; |
| 522 | prevLength = -1; |
| 523 | } |
| 524 | }; |
| 525 | mutable FontEngineCache feCache; |
| 526 | |
| 527 | public: |
| 528 | QString text; |
| 529 | mutable QFont fnt; |
| 530 | #ifndef QT_NO_RAWFONT |
| 531 | QRawFont rawFont; |
| 532 | #endif |
| 533 | QTextBlock block; |
| 534 | |
| 535 | QTextOption option; |
| 536 | |
| 537 | QFixed minWidth; |
| 538 | QFixed maxWidth; |
| 539 | QPointF position; |
| 540 | uint ignoreBidi : 1; |
| 541 | uint cacheGlyphs : 1; |
| 542 | uint stackEngine : 1; |
| 543 | uint forceJustification : 1; |
| 544 | uint visualMovement : 1; |
| 545 | uint delayDecorations: 1; |
| 546 | #ifndef QT_NO_RAWFONT |
| 547 | uint useRawFont : 1; |
| 548 | #endif |
| 549 | |
| 550 | mutable LayoutData *layoutData; |
| 551 | |
| 552 | ItemDecorationList underlineList; |
| 553 | ItemDecorationList strikeOutList; |
| 554 | ItemDecorationList overlineList; |
| 555 | |
| 556 | inline bool visualCursorMovement() const |
| 557 | { return visualMovement || (QTextDocumentPrivate::get(block) != nullptr && QTextDocumentPrivate::get(block)->defaultCursorMoveStyle == Qt::VisualMoveStyle); } |
| 558 | |
| 559 | inline int preeditAreaPosition() const { return specialData ? specialData->preeditPosition : -1; } |
| 560 | inline QString preeditAreaText() const { return specialData ? specialData->preeditText : QString(); } |
| 561 | void setPreeditArea(int position, const QString &text); |
| 562 | |
| 563 | inline bool hasFormats() const |
| 564 | { return QTextDocumentPrivate::get(block) != nullptr || (specialData && !specialData->formats.isEmpty()); } |
| 565 | inline QList<QTextLayout::FormatRange> formats() const |
| 566 | { |
| 567 | return specialData ? specialData->formats : QList<QTextLayout::FormatRange>(); |
| 568 | } |
| 569 | void setFormats(const QList<QTextLayout::FormatRange> &formats); |
| 570 | |
| 571 | private: |
| 572 | static void init(QTextEngine *e); |
| 573 | |
| 574 | struct SpecialData { |
| 575 | int preeditPosition; |
| 576 | QString preeditText; |
| 577 | QList<QTextLayout::FormatRange> formats; |
| 578 | QList<QTextCharFormat> resolvedFormats; |
| 579 | // only used when no QTextDocumentPrivate is available |
| 580 | QScopedPointer<QTextFormatCollection> formatCollection; |
| 581 | }; |
| 582 | SpecialData *specialData; |
| 583 | |
| 584 | void indexFormats(); |
| 585 | void resolveFormats() const; |
| 586 | |
| 587 | public: |
| 588 | bool atWordSeparator(int position) const; |
| 589 | |
| 590 | QString elidedText(Qt::TextElideMode mode, QFixed width, int flags = 0, int from = 0, int count = -1) const; |
| 591 | |
| 592 | void shapeLine(const QScriptLine &line); |
| 593 | QFixed leadingSpaceWidth(const QScriptLine &line); |
| 594 | |
| 595 | QFixed offsetInLigature(const QScriptItem *si, int pos, int max, int glyph_pos); |
| 596 | int positionInLigature(const QScriptItem *si, int end, QFixed x, QFixed edge, int glyph_pos, bool cursorOnCharacter); |
| 597 | int previousLogicalPosition(int oldPos) const; |
| 598 | int nextLogicalPosition(int oldPos) const; |
| 599 | int lineNumberForTextPosition(int pos); |
| 600 | int positionAfterVisualMovement(int oldPos, QTextCursor::MoveOperation op); |
| 601 | std::vector<int> insertionPointsForLine(int lineNum); |
| 602 | void resetFontEngineCache(); |
| 603 | |
| 604 | void enableDelayDecorations(bool enable = true) { delayDecorations = enable; } |
| 605 | |
| 606 | void addUnderline(QPainter *painter, const QLineF &line); |
| 607 | void addStrikeOut(QPainter *painter, const QLineF &line); |
| 608 | void addOverline(QPainter *painter, const QLineF &line); |
| 609 | |
| 610 | void drawDecorations(QPainter *painter); |
| 611 | void clearDecorations(); |
| 612 | void adjustUnderlines(); |
| 613 | |
| 614 | private: |
| 615 | void addItemDecoration(QPainter *painter, const QLineF &line, ItemDecorationList *decorationList); |
| 616 | void adjustUnderlines(ItemDecorationList::iterator start, |
| 617 | ItemDecorationList::iterator end, |
| 618 | qreal underlinePos, qreal penWidth); |
| 619 | void drawItemDecorationList(QPainter *painter, const ItemDecorationList &decorationList); |
| 620 | void setBoundary(int strPos) const; |
| 621 | void addRequiredBoundaries() const; |
| 622 | void shapeText(int item) const; |
| 623 | #if QT_CONFIG(harfbuzz) |
| 624 | int shapeTextWithHarfbuzzNG(const QScriptItem &si, |
| 625 | const ushort *string, |
| 626 | int itemLength, |
| 627 | QFontEngine *fontEngine, |
| 628 | QSpan<uint> itemBoundaries, |
| 629 | bool kerningEnabled, |
| 630 | bool hasLetterSpacing, |
| 631 | const QHash<QFont::Tag, quint32> &features) const; |
| 632 | #endif |
| 633 | |
| 634 | int endOfLine(int lineNum); |
| 635 | int beginningOfLine(int lineNum); |
| 636 | int getClusterLength(unsigned short *logClusters, const QCharAttributes *attributes, int from, int to, int glyph_pos, int *start); |
| 637 | }; |
| 638 | |
| 639 | class Q_GUI_EXPORT QStackTextEngine : public QTextEngine { |
| 640 | public: |
| 641 | enum { MemSize = 256*40/sizeof(void *) }; |
| 642 | QStackTextEngine(const QString &string, const QFont &f); |
| 643 | LayoutData _layoutData; |
| 644 | void *_memory[MemSize]; |
| 645 | }; |
| 646 | Q_DECLARE_TYPEINFO(QTextEngine::ItemDecoration, Q_RELOCATABLE_TYPE); |
| 647 | |
| 648 | struct QTextLineItemIterator |
| 649 | { |
| 650 | QTextLineItemIterator(QTextEngine *eng, int lineNum, const QPointF &pos = QPointF(), |
| 651 | const QTextLayout::FormatRange *_selection = nullptr); |
| 652 | |
| 653 | inline bool atEnd() const { return logicalItem >= nItems - 1; } |
| 654 | inline bool atBeginning() const { return logicalItem <= 0; } |
| 655 | QScriptItem &next(); |
| 656 | |
| 657 | bool getSelectionBounds(QFixed *selectionX, QFixed *selectionWidth) const; |
| 658 | inline bool isOutsideSelection() const { |
| 659 | QFixed tmp1, tmp2; |
| 660 | return !getSelectionBounds(selectionX: &tmp1, selectionWidth: &tmp2); |
| 661 | } |
| 662 | |
| 663 | QTextEngine *eng; |
| 664 | |
| 665 | QFixed x; |
| 666 | const QScriptLine &line; |
| 667 | QScriptItem *si; |
| 668 | |
| 669 | const int lineNum; |
| 670 | const int lineEnd; |
| 671 | const int firstItem; |
| 672 | const int lastItem; |
| 673 | const int nItems; |
| 674 | int logicalItem; |
| 675 | int item; |
| 676 | int itemLength; |
| 677 | |
| 678 | int glyphsStart; |
| 679 | int glyphsEnd; |
| 680 | int itemStart; |
| 681 | int itemEnd; |
| 682 | |
| 683 | QFixed itemWidth; |
| 684 | |
| 685 | QVarLengthArray<int> visualOrder; |
| 686 | |
| 687 | const QTextLayout::FormatRange *selection; |
| 688 | }; |
| 689 | |
| 690 | QT_END_NAMESPACE |
| 691 | |
| 692 | #endif // QTEXTENGINE_P_H |
| 693 | |