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#include "qglobal.h"
5
6#if !defined(QT_NO_RAWFONT)
7
8#include "qglyphrun.h"
9#include "qglyphrun_p.h"
10#include <qdebug.h>
11
12QT_BEGIN_NAMESPACE
13
14/*!
15 \class QGlyphRun
16 \brief The QGlyphRun class provides direct access to the internal glyphs in a font.
17 \since 4.8
18 \inmodule QtGui
19
20 \ingroup text
21 \ingroup shared
22
23 When Qt displays a string of text encoded in Unicode, it will first convert the Unicode points
24 into a list of glyph indexes and a list of positions based on one or more fonts. The Unicode
25 representation of the text and the QFont object will in this case serve as a convenient
26 abstraction that hides the details of what actually takes place when displaying the text
27 on-screen. For instance, by the time the text actually reaches the screen, it may be represented
28 by a set of fonts in addition to the one specified by the user, e.g. in case the originally
29 selected font did not support all the writing systems contained in the text.
30
31 Under certain circumstances, it can be useful as an application developer to have more low-level
32 control over which glyphs in a specific font are drawn to the screen. This could for instance
33 be the case in applications that use an external font engine and text shaper together with Qt.
34 QGlyphRun provides an interface to the raw data needed to get text on the screen. It
35 contains a list of glyph indexes, a position for each glyph and a font.
36
37 It is the user's responsibility to ensure that the selected font actually contains the
38 provided glyph indexes.
39
40 QTextLayout::glyphRuns() or QTextFragment::glyphRuns() can be used to convert unicode encoded
41 text into a list of QGlyphRun objects, and QPainter::drawGlyphRun() can be used to draw the
42 glyphs.
43
44 \note Please note that QRawFont is considered local to the thread in which it is constructed.
45 This in turn means that a new QRawFont will have to be created and set on the QGlyphRun if it is
46 moved to a different thread. If the QGlyphRun contains a reference to a QRawFont from a different
47 thread than the current, it will not be possible to draw the glyphs using a QPainter, as the
48 QRawFont is considered invalid and inaccessible in this case.
49*/
50
51/*!
52 \enum QGlyphRun::GlyphRunFlag
53 \since 5.0
54
55 This enum describes flags that alter the way the run of glyphs might be presented or behave in
56 a visual layout. The layout which generates the glyph runs can set these flags based on relevant
57 internal data, to retain information needed to present the text as intended by the user of the
58 layout.
59
60 \value Overline Indicates that the glyphs should be visualized together with an overline.
61 \value Underline Indicates that the glyphs should be visualized together with an underline.
62 \value StrikeOut Indicates that the glyphs should be struck out visually.
63 \value RightToLeft Indicates that the glyphs are ordered right to left. This can affect the
64 positioning of other screen elements that are relative to the glyph run, such as an inline
65 text object.
66 \value SplitLigature Indicates that the glyph run splits a ligature glyph. This means
67 that a ligature glyph is included in the run, but the characters represented by it corresponds
68 only to part of that ligature. The glyph run's boundingRect() function can in this case be used
69 to retrieve the area covered by glyphs that correspond to the characters represented by the
70 glyph run. When visualizing the glyphs, care needs to be taken to clip to this bounding rect to
71 ensure that only the corresponding part of the ligature is painted. In particular, this can be
72 the case when retrieving a glyph run from a QTextLayout for a specific character range, e.g.
73 when retrieving the selected area of a QTextLayout.
74*/
75
76/*!
77 Constructs an empty QGlyphRun object.
78*/
79QGlyphRun::QGlyphRun() : d(new QGlyphRunPrivate)
80{
81}
82
83/*!
84 Constructs a QGlyphRun object which is a copy of \a other.
85*/
86QGlyphRun::QGlyphRun(const QGlyphRun &other)
87{
88 d = other.d;
89}
90
91/*!
92 Destroys the QGlyphRun.
93*/
94QGlyphRun::~QGlyphRun()
95{
96 // Required for QExplicitlySharedDataPointer
97}
98
99/*!
100 \internal
101*/
102void QGlyphRun::detach()
103{
104 if (d->ref.loadRelaxed() != 1)
105 d.detach();
106}
107
108/*!
109 Assigns \a other to this QGlyphRun object.
110*/
111QGlyphRun &QGlyphRun::operator=(const QGlyphRun &other)
112{
113 d = other.d;
114 return *this;
115}
116
117/*!
118 \fn void QGlyphRun::swap(QGlyphRun &other)
119 \since 5.0
120
121 Swaps this glyph run instance with \a other. This function is very
122 fast and never fails.
123*/
124
125/*!
126 Compares \a other to this QGlyphRun object. Returns \c true if the list of glyph indexes,
127 the list of positions and the font are all equal, otherwise returns \c false.
128*/
129bool QGlyphRun::operator==(const QGlyphRun &other) const
130{
131 if (d == other.d)
132 return true;
133
134 if ((d->glyphIndexDataSize != other.d->glyphIndexDataSize)
135 || (d->glyphPositionDataSize != other.d->glyphPositionDataSize)) {
136 return false;
137 }
138
139 if (d->glyphIndexData != other.d->glyphIndexData) {
140 for (int i = 0; i < d->glyphIndexDataSize; ++i) {
141 if (d->glyphIndexData[i] != other.d->glyphIndexData[i])
142 return false;
143 }
144 }
145 if (d->glyphPositionData != other.d->glyphPositionData) {
146 for (int i = 0; i < d->glyphPositionDataSize; ++i) {
147 if (d->glyphPositionData[i] != other.d->glyphPositionData[i])
148 return false;
149 }
150 }
151
152 return (d->flags == other.d->flags && d->rawFont == other.d->rawFont);
153}
154
155/*!
156 \fn bool QGlyphRun::operator!=(const QGlyphRun &other) const
157
158 Compares \a other to this QGlyphRun object. Returns \c true if any of the list of glyph
159 indexes, the list of positions or the font are different, otherwise returns \c false.
160*/
161
162/*!
163 Returns the font selected for this QGlyphRun object.
164
165 \sa setRawFont()
166*/
167QRawFont QGlyphRun::rawFont() const
168{
169 return d->rawFont;
170}
171
172/*!
173 Sets the font in which to look up the glyph indexes to the \a rawFont
174 specified.
175
176 \sa rawFont(), setGlyphIndexes()
177*/
178void QGlyphRun::setRawFont(const QRawFont &rawFont)
179{
180 detach();
181 d->rawFont = rawFont;
182}
183
184/*!
185 Returns the glyph indexes for this QGlyphRun object.
186
187 \sa setGlyphIndexes(), setPositions()
188*/
189QList<quint32> QGlyphRun::glyphIndexes() const
190{
191 if (d->glyphIndexes.constData() == d->glyphIndexData) {
192 return d->glyphIndexes;
193 } else {
194 QList<quint32> indexes(d->glyphIndexDataSize);
195 memcpy(dest: indexes.data(), src: d->glyphIndexData, n: d->glyphIndexDataSize * sizeof(quint32));
196 return indexes;
197 }
198}
199
200/*!
201 Set the glyph indexes for this QGlyphRun object to \a glyphIndexes. The glyph indexes must
202 be valid for the selected font.
203*/
204void QGlyphRun::setGlyphIndexes(const QList<quint32> &glyphIndexes)
205{
206 detach();
207 d->glyphIndexes = glyphIndexes; // Keep a reference to the QList to avoid copying
208 d->glyphIndexData = glyphIndexes.constData();
209 d->glyphIndexDataSize = glyphIndexes.size();
210}
211
212/*!
213 Returns the position of the edge of the baseline for each glyph in this set of glyph indexes.
214*/
215QList<QPointF> QGlyphRun::positions() const
216{
217 if (d->glyphPositions.constData() == d->glyphPositionData) {
218 return d->glyphPositions;
219 } else {
220 QList<QPointF> glyphPositions(d->glyphPositionDataSize);
221 memcpy(dest: glyphPositions.data(), src: d->glyphPositionData,
222 n: d->glyphPositionDataSize * sizeof(QPointF));
223 return glyphPositions;
224 }
225}
226
227/*!
228 Sets the positions of the edge of the baseline for each glyph in this set of glyph indexes to
229 \a positions.
230*/
231void QGlyphRun::setPositions(const QList<QPointF> &positions)
232{
233 detach();
234 d->glyphPositions = positions; // Keep a reference to the list to avoid copying
235 d->glyphPositionData = positions.constData();
236 d->glyphPositionDataSize = positions.size();
237}
238
239/*!
240 Clears all data in the QGlyphRun object.
241*/
242void QGlyphRun::clear()
243{
244 detach();
245 d->rawFont = QRawFont();
246 d->flags = { };
247
248 setPositions(QList<QPointF>());
249 setGlyphIndexes(QList<quint32>());
250}
251
252/*!
253 Sets the glyph indexes and positions of this QGlyphRun to use the first \a size
254 elements in the arrays \a glyphIndexArray and \a glyphPositionArray. The data is
255 \e not copied. The caller must guarantee that the arrays are not deleted as long
256 as this QGlyphRun and any copies of it exists.
257
258 \sa setGlyphIndexes(), setPositions()
259*/
260void QGlyphRun::setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray,
261 int size)
262{
263 detach();
264 d->glyphIndexes.clear();
265 d->glyphPositions.clear();
266
267 d->glyphIndexData = glyphIndexArray;
268 d->glyphPositionData = glyphPositionArray;
269 d->glyphIndexDataSize = d->glyphPositionDataSize = size;
270}
271
272/*!
273 Returns \c true if this QGlyphRun should be painted with an overline decoration.
274
275 \sa setOverline(), flags()
276*/
277bool QGlyphRun::overline() const
278{
279 return d->flags & Overline;
280}
281
282/*!
283 Indicates that this QGlyphRun should be painted with an overline decoration if \a overline is true.
284 Otherwise the QGlyphRun should be painted with no overline decoration.
285
286 \sa overline(), setFlag(), setFlags()
287*/
288void QGlyphRun::setOverline(bool overline)
289{
290 setFlag(flag: Overline, enabled: overline);
291}
292
293/*!
294 Returns \c true if this QGlyphRun should be painted with an underline decoration.
295
296 \sa setUnderline(), flags()
297*/
298bool QGlyphRun::underline() const
299{
300 return d->flags & Underline;
301}
302
303/*!
304 Indicates that this QGlyphRun should be painted with an underline decoration if \a underline is
305 true. Otherwise the QGlyphRun should be painted with no underline decoration.
306
307 \sa underline(), setFlag(), setFlags()
308*/
309void QGlyphRun::setUnderline(bool underline)
310{
311 setFlag(flag: Underline, enabled: underline);
312}
313
314/*!
315 Returns \c true if this QGlyphRun should be painted with a strike out decoration.
316
317 \sa setStrikeOut(), flags()
318*/
319bool QGlyphRun::strikeOut() const
320{
321 return d->flags & StrikeOut;
322}
323
324/*!
325 Indicates that this QGlyphRun should be painted with an strike out decoration if \a strikeOut is
326 true. Otherwise the QGlyphRun should be painted with no strike out decoration.
327
328 \sa strikeOut(), setFlag(), setFlags()
329*/
330void QGlyphRun::setStrikeOut(bool strikeOut)
331{
332 setFlag(flag: StrikeOut, enabled: strikeOut);
333}
334
335/*!
336 Returns \c true if this QGlyphRun contains glyphs that are painted from the right to the left.
337
338 \since 5.0
339 \sa setRightToLeft(), flags()
340*/
341bool QGlyphRun::isRightToLeft() const
342{
343 return d->flags & RightToLeft;
344}
345
346/*!
347 Indicates that this QGlyphRun contains glyphs that should be ordered from the right to left
348 if \a rightToLeft is true. Otherwise the order of the glyphs is assumed to be left to right.
349
350 \since 5.0
351 \sa isRightToLeft(), setFlag(), setFlags()
352*/
353void QGlyphRun::setRightToLeft(bool rightToLeft)
354{
355 setFlag(flag: RightToLeft, enabled: rightToLeft);
356}
357
358/*!
359 Returns the flags set for this QGlyphRun.
360
361 \since 5.0
362 \sa setFlag(), setFlag()
363*/
364QGlyphRun::GlyphRunFlags QGlyphRun::flags() const
365{
366 return d->flags;
367}
368
369/*!
370 If \a enabled is true, then \a flag is enabled; otherwise, it is disabled.
371
372 \since 5.0
373 \sa flags(), setFlags()
374*/
375void QGlyphRun::setFlag(GlyphRunFlag flag, bool enabled)
376{
377 if (d->flags.testFlag(flag) == enabled)
378 return;
379
380 detach();
381 d->flags.setFlag(flag, on: enabled);
382}
383
384/*!
385 Sets the flags of this QGlyphRun to \a flags.
386
387 \since 5.0
388 \sa setFlag(), flags()
389*/
390void QGlyphRun::setFlags(GlyphRunFlags flags)
391{
392 if (d->flags == flags)
393 return;
394
395 detach();
396 d->flags = flags;
397}
398
399/*!
400 Sets the bounding rect of the glyphs in this QGlyphRun to be \a boundingRect. This rectangle
401 will be returned by boundingRect() unless it is null, in which case the bounding rectangle of the
402 glyphs in the glyph run will be returned instead.
403
404 \note Unless you are implementing text shaping, you should not have to use this function.
405 It is used specifically when the QGlyphRun should represent an area which is smaller than the
406 area of the glyphs it contains. This could happen e.g. if the glyph run is retrieved by calling
407 QTextLayout::glyphRuns() and the specified range only includes part of a ligature (where two or
408 more characters are combined to a single glyph.) When this is the case, the bounding rect should
409 only include the appropriate part of the ligature glyph, based on a calculation of the average
410 width of the characters in the ligature.
411
412 In order to support such a case (an example is selections which should be drawn with a different
413 color than the main text color), it is necessary to clip the painting mechanism to the rectangle
414 returned from boundingRect() to avoid drawing the entire ligature glyph.
415
416 \sa boundingRect()
417
418 \since 5.0
419*/
420void QGlyphRun::setBoundingRect(const QRectF &boundingRect)
421{
422 detach();
423 d->boundingRect = boundingRect;
424}
425
426/*!
427 Returns the smallest rectangle that contains all glyphs in this QGlyphRun. If a bounding rect
428 has been set using setBoundingRect(), then this will be returned. Otherwise the bounding rect
429 will be calculated based on the font metrics of the glyphs in the glyph run.
430
431 \since 5.0
432*/
433QRectF QGlyphRun::boundingRect() const
434{
435 if (!d->boundingRect.isNull() || !d->rawFont.isValid())
436 return d->boundingRect;
437
438 qreal minX, minY, maxX, maxY;
439 minX = minY = maxX = maxY = 0;
440
441 for (int i = 0, n = qMin(a: d->glyphIndexDataSize, b: d->glyphPositionDataSize); i < n; ++i) {
442 QRectF glyphRect = d->rawFont.boundingRect(glyphIndex: d->glyphIndexData[i]);
443 glyphRect.translate(p: d->glyphPositionData[i]);
444
445 if (i == 0) {
446 minX = glyphRect.left();
447 minY = glyphRect.top();
448 maxX = glyphRect.right();
449 maxY = glyphRect.bottom();
450 } else {
451 minX = qMin(a: glyphRect.left(), b: minX);
452 minY = qMin(a: glyphRect.top(), b: minY);
453 maxX = qMax(a: glyphRect.right(),b: maxX);
454 maxY = qMax(a: glyphRect.bottom(), b: maxY);
455 }
456 }
457
458 return QRectF(QPointF(minX, minY), QPointF(maxX, maxY));
459}
460
461/*!
462 Returns \c true if the QGlyphRun does not contain any glyphs.
463
464 \since 5.0
465*/
466bool QGlyphRun::isEmpty() const
467{
468 return d->glyphIndexDataSize == 0
469 && d->glyphPositionDataSize == 0
470 && d->stringIndexes.isEmpty()
471 && d->sourceString.isEmpty();
472}
473
474/*!
475 \since 6.5
476
477 Returns the string indexes corresponding to each glyph index, if the glyph run has been
478 constructed from a string and string indexes have been requested from the layout. In this case,
479 the length of the returned vector will correspond to the length of glyphIndexes(). In other
480 cases, it will be empty.
481
482 Since a single glyph may correspond to multiple characters in the source string, there may be
483 gaps in the list of string indexes. For instance, if the string "first" is processed by a font
484 which contains a ligature for the character pair "fi", then the five character string will
485 generate a glyph run consisting of only four glyphs. Then the glyph indexes may in this case be
486 (1, 2, 3, 4) (four arbitrary glyph indexes) whereas the string indexes would be (0, 2, 3, 4).
487 The glyphs are in the logical order of the string, thus it is implied that the first glyphs
488 spans characters 0 and 1 in this case.
489
490 Inversely, a single character may also generate multiple glyphs, in which case there will be
491 duplicate entries in the list of string indexes.
492
493 The string indexes correspond to the string, optionally available through sourceString().
494
495 \sa setStringIndexes(), sourceString(), QTextLayout::glyphRuns()
496*/
497QList<qsizetype> QGlyphRun::stringIndexes() const
498{
499 return d->stringIndexes;
500}
501
502/*!
503 \since 6.5
504
505 Sets the list of string indexes corresponding to the glyph indexes to \a stringIndexes
506
507 See stringIndexes() for more details on the conventions of this list.
508
509 \sa sourceString()
510 */
511void QGlyphRun::setStringIndexes(const QList<qsizetype> &stringIndexes)
512{
513 detach();
514 d->stringIndexes = stringIndexes;
515}
516
517/*!
518 \since 6.5
519
520 Returns the string corresponding to the glyph run, if the glyph run has been created from
521 a string and the string has been requested from the layout.
522
523 \sa setSourceString(), stringIndexes(), QTextLayout::glyphRuns()
524 */
525QString QGlyphRun::sourceString() const
526{
527 return d->sourceString;
528}
529
530/*!
531 \since 6.5
532
533 Set the string corresponding to the glyph run to \a sourceString. If set, the indexes returned
534 by stringIndexes() should be indexes into this string.
535
536 \sa sourceString(), stringIndexes()
537 */
538void QGlyphRun::setSourceString(const QString &sourceString)
539{
540 detach();
541 d->sourceString = sourceString;
542}
543
544QT_END_NAMESPACE
545
546#endif // QT_NO_RAWFONT
547

source code of qtbase/src/gui/text/qglyphrun.cpp