diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2022-08-24 13:25:19 +0200 |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2022-09-29 18:06:40 +0200 |
commit | 1fe7144a0746353ea629cd91b5c0d04a8434159b (patch) | |
tree | f0521c73e7a233920e09d133e7a92f0e69aee468 /src/gui/text/qglyphrun.cpp | |
parent | 87cabd0b92bc348f73be2fe0a20a57fa485f6665 (diff) | |
download | qtbase-1fe7144a0746353ea629cd91b5c0d04a8434159b.tar.gz |
Introduce QGlyphRun::stringIndexes()
This introduces a way to trace each entry in the glyph index
array to a specific index in the original text passed to
QTextLayout, as well as a convenience function to access
the original string from the QGlyphRun.
The index information is stored in the logClusters array internally
in Qt, but it contains the inverse information: For each
character in the output string, it contains an index into the
glyph array. In order to get the string indexes for each glyph,
which makes a lot more sense in the context of the QGlyphRun
API, we need to do a little search to construct the data.
To avoid adding unnecessary allocations, we make the new APIs
opt-in. If you do not specify anything, you will only get the
glyph indexes and glyph positions as before. However, you
can now specify exactly which parts of the layout to extract
using an optional flags parameter.
This also adds a manual test which can be very handy to
visualize QTextLayouts and how they are split into QGlyphRuns.
Fixes: QTBUG-103932
Change-Id: Ie4288fff338b9482aba0aba29fc7e1e59fa60900
Reviewed-by: Lars Knoll <lars@knoll.priv.no>
Diffstat (limited to 'src/gui/text/qglyphrun.cpp')
-rw-r--r-- | src/gui/text/qglyphrun.cpp | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/gui/text/qglyphrun.cpp b/src/gui/text/qglyphrun.cpp index 371dd26637..389b7403fd 100644 --- a/src/gui/text/qglyphrun.cpp +++ b/src/gui/text/qglyphrun.cpp @@ -465,7 +465,72 @@ QRectF QGlyphRun::boundingRect() const */ bool QGlyphRun::isEmpty() const { - return d->glyphIndexDataSize == 0; + return d->glyphIndexDataSize == 0 + && d->glyphPositionDataSize == 0 + && d->stringIndexes.isEmpty() + && d->sourceString.isEmpty(); +} + +/*! + Returns the string indexes corresponding to each glyph index, if the glyph run has been + constructed from a string and string indexes have been requested from the layout. In this case, + the length of the returned vector will correspond to the length of glyphIndexes(). In other + cases, it will be empty. + + Since a single glyph may correspond to multiple characters in the source string, there may be + gaps in the list of string indexes. For instance, if the string "first" is processed by a font + which contains a ligature for the character pair "fi", then the five character string will + generate a glyph run consisting of only four glyphs. Then the glyph indexes may in this case be + (1, 2, 3, 4) (four arbitrary glyph indexes) whereas the string indexes would be (0, 2, 3, 4). + The glyphs are in the logical order of the string, thus it is implied that the first glyphs + spans characters 0 and 1 in this case. + + Inversely, a single character may also generate multiple glyphs, in which case there will be + duplicate entries in the list of string indexes. + + The string indexes correspond to the string, optionally available through sourceString(). + + \sa setStringIndexes(), sourceString(), QTextLayout::glyphRuns() +*/ +QList<qsizetype> QGlyphRun::stringIndexes() const +{ + return d->stringIndexes; +} + +/*! + Sets the list of string indexes corresponding to the glyph indexes to \a stringIndexes + + See stringIndexes() for more details on the conventions of this list. + + \sa sourceString() + */ +void QGlyphRun::setStringIndexes(const QList<qsizetype> &stringIndexes) +{ + detach(); + d->stringIndexes = stringIndexes; +} + +/*! + Returns the string corresponding to the glyph run, if the glyph run has been created from + a string and the string has been requested from the layout. + + \sa setSourceString(), stringIndexes(), QTextLayout::glyphRuns() + */ +QString QGlyphRun::sourceString() const +{ + return d->sourceString; +} + +/*! + Set the string corresponding to the glyph run to \a sourceString. If set, the indexes returned + by stringIndexes() should be indexes into this string. + + \sa sourceString(), stringIndexes() + */ +void QGlyphRun::setSourceString(const QString &sourceString) +{ + detach(); + d->sourceString = sourceString; } QT_END_NAMESPACE |