1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #if !defined(QT_NO_RAWFONT)
46 #include "qglyphrun.h"
47 #include "qglyphrun_p.h"
54 \brief The QGlyphRun class provides direct access to the internal glyphs in a font.
61 When Qt displays a string of text encoded in Unicode, it will first convert the Unicode points
62 into a list of glyph indexes and a list of positions based on one or more fonts. The Unicode
63 representation of the text and the QFont object will in this case serve as a convenient
64 abstraction that hides the details of what actually takes place when displaying the text
65 on-screen. For instance, by the time the text actually reaches the screen, it may be represented
66 by a set of fonts in addition to the one specified by the user, e.g. in case the originally
67 selected font did not support all the writing systems contained in the text.
69 Under certain circumstances, it can be useful as an application developer to have more low-level
70 control over which glyphs in a specific font are drawn to the screen. This could for instance
71 be the case in applications that use an external font engine and text shaper together with Qt.
72 QGlyphRun provides an interface to the raw data needed to get text on the screen. It
73 contains a list of glyph indexes, a position for each glyph and a font.
75 It is the user's responsibility to ensure that the selected font actually contains the
76 provided glyph indexes.
78 QTextLayout::glyphRuns() or QTextFragment::glyphRuns() can be used to convert unicode encoded
79 text into a list of QGlyphRun objects, and QPainter::drawGlyphRun() can be used to draw the
82 \note Please note that QRawFont is considered local to the thread in which it is constructed.
83 This in turn means that a new QRawFont will have to be created and set on the QGlyphRun if it is
84 moved to a different thread. If the QGlyphRun contains a reference to a QRawFont from a different
85 thread than the current, it will not be possible to draw the glyphs using a QPainter, as the
86 QRawFont is considered invalid and inaccessible in this case.
90 \enum QGlyphRun::GlyphRunFlag
93 This enum describes flags that alter the way the run of glyphs might be presented or behave in
94 a visual layout. The layout which generates the glyph runs can set these flags based on relevant
95 internal data, to retain information needed to present the text as intended by the user of the
98 \value Overline Indicates that the glyphs should be visualized together with an overline.
99 \value Underline Indicates that the glyphs should be visualized together with an underline.
100 \value StrikeOut Indicates that the glyphs should be struck out visually.
101 \value RightToLeft Indicates that the glyphs are ordered right to left. This can affect the
102 positioning of other screen elements that are relative to the glyph run, such as an inline
104 \value SplitLigature Indicates that the glyph run splits a ligature glyph. This means
105 that a ligature glyph is included in the run, but the characters represented by it corresponds
106 only to part of that ligature. The glyph run's boundingRect() function can in this case be used
107 to retrieve the area covered by glyphs that correspond to the characters represented by the
108 glyph run. When visualizing the glyphs, care needs to be taken to clip to this bounding rect to
109 ensure that only the corresponding part of the ligature is painted. In particular, this can be
110 the case when retrieving a glyph run from a QTextLayout for a specific character range, e.g.
111 when retrieving the selected area of a QTextLayout.
115 Constructs an empty QGlyphRun object.
117 QGlyphRun::QGlyphRun() : d(new QGlyphRunPrivate)
122 Constructs a QGlyphRun object which is a copy of \a other.
124 QGlyphRun::QGlyphRun(const QGlyphRun &other)
130 Destroys the QGlyphRun.
132 QGlyphRun::~QGlyphRun()
134 // Required for QExplicitlySharedDataPointer
140 void QGlyphRun::detach()
142 if (d->ref.load() != 1)
147 Assigns \a other to this QGlyphRun object.
149 QGlyphRun &QGlyphRun::operator=(const QGlyphRun &other)
156 Compares \a other to this QGlyphRun object. Returns true if the list of glyph indexes,
157 the list of positions and the font are all equal, otherwise returns false.
159 bool QGlyphRun::operator==(const QGlyphRun &other) const
164 if ((d->glyphIndexDataSize != other.d->glyphIndexDataSize)
165 || (d->glyphPositionDataSize != other.d->glyphPositionDataSize)) {
169 if (d->glyphIndexData != other.d->glyphIndexData) {
170 for (int i = 0; i < d->glyphIndexDataSize; ++i) {
171 if (d->glyphIndexData[i] != other.d->glyphIndexData[i])
175 if (d->glyphPositionData != other.d->glyphPositionData) {
176 for (int i = 0; i < d->glyphPositionDataSize; ++i) {
177 if (d->glyphPositionData[i] != other.d->glyphPositionData[i])
182 return (d->flags == other.d->flags && d->rawFont == other.d->rawFont);
186 \fn bool QGlyphRun::operator!=(const QGlyphRun &other) const
188 Compares \a other to this QGlyphRun object. Returns true if any of the list of glyph
189 indexes, the list of positions or the font are different, otherwise returns false.
193 Returns the font selected for this QGlyphRun object.
197 QRawFont QGlyphRun::rawFont() const
203 Sets the font in which to look up the glyph indexes to the \a rawFont
206 \sa rawFont(), setGlyphIndexes()
208 void QGlyphRun::setRawFont(const QRawFont &rawFont)
211 d->rawFont = rawFont;
215 Returns the glyph indexes for this QGlyphRun object.
217 \sa setGlyphIndexes(), setPositions()
219 QVector<quint32> QGlyphRun::glyphIndexes() const
221 if (d->glyphIndexes.constData() == d->glyphIndexData) {
222 return d->glyphIndexes;
224 QVector<quint32> indexes(d->glyphIndexDataSize);
225 memcpy(indexes.data(), d->glyphIndexData, d->glyphIndexDataSize * sizeof(quint32));
231 Set the glyph indexes for this QGlyphRun object to \a glyphIndexes. The glyph indexes must
232 be valid for the selected font.
234 void QGlyphRun::setGlyphIndexes(const QVector<quint32> &glyphIndexes)
237 d->glyphIndexes = glyphIndexes; // Keep a reference to the QVector to avoid copying
238 d->glyphIndexData = glyphIndexes.constData();
239 d->glyphIndexDataSize = glyphIndexes.size();
243 Returns the position of the edge of the baseline for each glyph in this set of glyph indexes.
245 QVector<QPointF> QGlyphRun::positions() const
247 if (d->glyphPositions.constData() == d->glyphPositionData) {
248 return d->glyphPositions;
250 QVector<QPointF> glyphPositions(d->glyphPositionDataSize);
251 memcpy(glyphPositions.data(), d->glyphPositionData,
252 d->glyphPositionDataSize * sizeof(QPointF));
253 return glyphPositions;
258 Sets the positions of the edge of the baseline for each glyph in this set of glyph indexes to
261 void QGlyphRun::setPositions(const QVector<QPointF> &positions)
264 d->glyphPositions = positions; // Keep a reference to the vector to avoid copying
265 d->glyphPositionData = positions.constData();
266 d->glyphPositionDataSize = positions.size();
270 Clears all data in the QGlyphRun object.
272 void QGlyphRun::clear()
275 d->rawFont = QRawFont();
278 setPositions(QVector<QPointF>());
279 setGlyphIndexes(QVector<quint32>());
283 Sets the glyph indexes and positions of this QGlyphRun to use the first \a size
284 elements in the arrays \a glyphIndexArray and \a glyphPositionArray. The data is
285 \e not copied. The caller must guarantee that the arrays are not deleted as long
286 as this QGlyphRun and any copies of it exists.
288 \sa setGlyphIndexes(), setPositions()
290 void QGlyphRun::setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray,
294 d->glyphIndexes.clear();
295 d->glyphPositions.clear();
297 d->glyphIndexData = glyphIndexArray;
298 d->glyphPositionData = glyphPositionArray;
299 d->glyphIndexDataSize = d->glyphPositionDataSize = size;
303 Returns true if this QGlyphRun should be painted with an overline decoration.
305 \sa setOverline(), flags()
307 bool QGlyphRun::overline() const
309 return d->flags & Overline;
313 Indicates that this QGlyphRun should be painted with an overline decoration if \a overline is true.
314 Otherwise the QGlyphRun should be painted with no overline decoration.
316 \sa overline(), setFlag(), setFlags()
318 void QGlyphRun::setOverline(bool overline)
320 setFlag(Overline, overline);
324 Returns true if this QGlyphRun should be painted with an underline decoration.
326 \sa setUnderline(), flags()
328 bool QGlyphRun::underline() const
330 return d->flags & Underline;
334 Indicates that this QGlyphRun should be painted with an underline decoration if \a underline is
335 true. Otherwise the QGlyphRun should be painted with no underline decoration.
337 \sa underline(), setFlag(), setFlags()
339 void QGlyphRun::setUnderline(bool underline)
341 setFlag(Underline, underline);
345 Returns true if this QGlyphRun should be painted with a strike out decoration.
347 \sa setStrikeOut(), flags()
349 bool QGlyphRun::strikeOut() const
351 return d->flags & StrikeOut;
355 Indicates that this QGlyphRun should be painted with an strike out decoration if \a strikeOut is
356 true. Otherwise the QGlyphRun should be painted with no strike out decoration.
358 \sa strikeOut(), setFlag(), setFlags()
360 void QGlyphRun::setStrikeOut(bool strikeOut)
362 setFlag(StrikeOut, strikeOut);
366 Returns true if this QGlyphRun contains glyphs that are painted from the right to the left.
369 \sa setRightToLeft(), flags()
371 bool QGlyphRun::isRightToLeft() const
373 return d->flags & RightToLeft;
377 Indicates that this QGlyphRun contains glyphs that should be ordered from the right to left
378 if \a rightToLeft is true. Otherwise the order of the glyphs is assumed to be left to right.
381 \sa isRightToLeft(), setFlag(), setFlags()
383 void QGlyphRun::setRightToLeft(bool rightToLeft)
385 setFlag(RightToLeft, rightToLeft);
389 Returns the flags set for this QGlyphRun.
392 \sa setFlag(), setFlag()
394 QGlyphRun::GlyphRunFlags QGlyphRun::flags() const
400 If \a enabled is true, then \a flag is enabled; otherwise, it is disabled.
403 \sa flags(), setFlags()
405 void QGlyphRun::setFlag(GlyphRunFlag flag, bool enabled)
407 if (d->flags.testFlag(flag) == enabled)
418 Sets the flags of this QGlyphRun to \a flags.
421 \sa setFlag(), flags()
423 void QGlyphRun::setFlags(GlyphRunFlags flags)
425 if (d->flags == flags)
433 Sets the bounding rect of the glyphs in this QGlyphRun to be \a boundingRect. This rectangle
434 will be returned by boundingRect() unless it is empty, in which case the bounding rectangle of the
435 glyphs in the glyph run will be returned instead.
437 \note Unless you are implementing text shaping, you should not have to use this function.
438 It is used specifically when the QGlyphRun should represent an area which is smaller than the
439 area of the glyphs it contains. This could happen e.g. if the glyph run is retrieved by calling
440 QTextLayout::glyphRuns() and the specified range only includes part of a ligature (where two or
441 more characters are combined to a single glyph.) When this is the case, the bounding rect should
442 only include the appropriate part of the ligature glyph, based on a calculation of the average
443 width of the characters in the ligature.
445 In order to support such a case (an example is selections which should be drawn with a different
446 color than the main text color), it is necessary to clip the painting mechanism to the rectangle
447 returned from boundingRect() to avoid drawing the entire ligature glyph.
453 void QGlyphRun::setBoundingRect(const QRectF &boundingRect)
456 d->boundingRect = boundingRect;
460 Returns the smallest rectangle that contains all glyphs in this QGlyphRun. If a bounding rect
461 has been set using setBoundingRect(), then this will be returned. Otherwise the bounding rect
462 will be calculated based on the font metrics of the glyphs in the glyph run.
466 QRectF QGlyphRun::boundingRect() const
468 if (!d->boundingRect.isEmpty())
469 return d->boundingRect;
471 qreal minX, minY, maxX, maxY;
472 minX = minY = maxX = maxY = 0;
474 for (int i=0; i<qMin(d->glyphPositions.size(), d->glyphIndexes.size()); ++i) {
475 QRectF glyphRect = d->rawFont.boundingRect(d->glyphIndexes.at(i));
476 glyphRect.translate(d->glyphPositions.at(i));
479 minX = glyphRect.left();
480 minY = glyphRect.top();
481 maxX = glyphRect.right();
482 maxY = glyphRect.bottom();
484 minX = qMin(glyphRect.left(), minX);
485 minY = qMin(glyphRect.top(), minY);
486 maxX = qMax(glyphRect.right(),maxX);
487 maxY = qMax(glyphRect.bottom(), maxY);
491 return QRectF(QPointF(minX, minY), QPointF(maxX, maxY));
495 Returns true if the QGlyphRun does not contain any glyphs.
499 bool QGlyphRun::isEmpty() const
501 return d->glyphIndexes.isEmpty();
506 #endif // QT_NO_RAWFONT