1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtGui module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "qglobal.h"
43
44
#if !defined(QT_NO_RAWFONT)
45
46
#include "qglyphrun.h"
47
#include "qglyphrun_p.h"
48
49
QT_BEGIN_NAMESPACE
50
51
/*!
52
    \class QGlyphRun
53
    \brief The QGlyphRun class provides direct access to the internal glyphs in a font.
54
    \since 4.8
55
56
    \ingroup text
57
    \mainclass
58
59
    When Qt displays a string of text encoded in Unicode, it will first convert the Unicode points
60
    into a list of glyph indexes and a list of positions based on one or more fonts. The Unicode
61
    representation of the text and the QFont object will in this case serve as a convenient
62
    abstraction that hides the details of what actually takes place when displaying the text
63
    on-screen. For instance, by the time the text actually reaches the screen, it may be represented
64
    by a set of fonts in addition to the one specified by the user, e.g. in case the originally
65
    selected font did not support all the writing systems contained in the text.
66
67
    Under certain circumstances, it can be useful as an application developer to have more low-level
68
    control over which glyphs in a specific font are drawn to the screen. This could for instance
69
    be the case in applications that use an external font engine and text shaper together with Qt.
70
    QGlyphRun provides an interface to the raw data needed to get text on the screen. It
71
    contains a list of glyph indexes, a position for each glyph and a font.
72
73
    It is the user's responsibility to ensure that the selected font actually contains the
74
    provided glyph indexes.
75
76
    QTextLayout::glyphRuns() or QTextFragment::glyphRuns() can be used to convert unicode encoded
77
    text into a list of QGlyphRun objects, and QPainter::drawGlyphRun() can be used to draw the
78
    glyphs.
79
80
    \note Please note that QRawFont is considered local to the thread in which it is constructed.
81
    This in turn means that a new QRawFont will have to be created and set on the QGlyphRun if it is
82
    moved to a different thread. If the QGlyphRun contains a reference to a QRawFont from a different
83
    thread than the current, it will not be possible to draw the glyphs using a QPainter, as the
84
    QRawFont is considered invalid and inaccessible in this case.
85
*/
86
87
88
/*!
89
    Constructs an empty QGlyphRun object.
90
*/
91
QGlyphRun::QGlyphRun() : d(new QGlyphRunPrivate)
92
{
93
}
94
95
/*!
96
    Constructs a QGlyphRun object which is a copy of \a other.
97
*/
98
QGlyphRun::QGlyphRun(const QGlyphRun &other)
99
{
100
    d = other.d;
101
}
102
103
/*!
104
    Destroys the QGlyphRun.
105
*/
106
QGlyphRun::~QGlyphRun()
107
{
108
    // Required for QExplicitlySharedDataPointer
109
}
110
111
/*!
112
    \internal
113
*/
114
void QGlyphRun::detach()
115
{
116
    if (d->ref != 1)
117
        d.detach();
118
}
119
120
/*!
121
    Assigns \a other to this QGlyphRun object.
122
*/
123
QGlyphRun &QGlyphRun::operator=(const QGlyphRun &other)
124
{
125
    d = other.d;
126
    return *this;
127
}
128
129
/*!
130
    Compares \a other to this QGlyphRun object. Returns true if the list of glyph indexes,
131
    the list of positions and the font are all equal, otherwise returns false.
132
*/
133
bool QGlyphRun::operator==(const QGlyphRun &other) const
134
{
135
    if (d == other.d)
136
        return true;
137
138
    if ((d->glyphIndexDataSize != other.d->glyphIndexDataSize)
139
     || (d->glyphPositionDataSize != other.d->glyphPositionDataSize)) {
140
        return false;
141
    }
142
143
    if (d->glyphIndexData != other.d->glyphIndexData) {
144
        for (int i = 0; i < d->glyphIndexDataSize; ++i) {
145
            if (d->glyphIndexData[i] != other.d->glyphIndexData[i])
146
               return false;
147
        }
148
    }
149
    if (d->glyphPositionData != other.d->glyphPositionData) {
150
        for (int i = 0; i < d->glyphPositionDataSize; ++i) {
151
            if (d->glyphPositionData[i] != other.d->glyphPositionData[i])
152
               return false;
153
        }
154
    }
155
156
    return (d->overline == other.d->overline
157
            && d->underline == other.d->underline
158
            && d->strikeOut == other.d->strikeOut
159
            && d->rawFont == other.d->rawFont);
160
}
161
162
/*!
163
    \fn bool QGlyphRun::operator!=(const QGlyphRun &other) const
164
165
    Compares \a other to this QGlyphRun object. Returns true if any of the list of glyph
166
    indexes, the list of positions or the font are different, otherwise returns false.
167
*/
168
169
/*!
170
    Returns the font selected for this QGlyphRun object.
171
172
    \sa setRawFont()
173
*/
174
QRawFont QGlyphRun::rawFont() const
175
{
176
    return d->rawFont;
177
}
178
179
/*!
180
    Sets the font specified by \a rawFont to be the font used to look up the
181
    glyph indexes.
182
183
    \sa rawFont(), setGlyphIndexes()
184
*/
185
void QGlyphRun::setRawFont(const QRawFont &rawFont)
186
{
187
    detach();
188
    d->rawFont = rawFont;
189
}
190
191
/*!
192
    Returns the glyph indexes for this QGlyphRun object.
193
194
    \sa setGlyphIndexes(), setPositions()
195
*/
196
QVector<quint32> QGlyphRun::glyphIndexes() const
197
{
198
    if (d->glyphIndexes.constData() == d->glyphIndexData) {
199
        return d->glyphIndexes;
200
    } else {
201
        QVector<quint32> indexes(d->glyphIndexDataSize);
202
        qMemCopy(indexes.data(), d->glyphIndexData, d->glyphIndexDataSize * sizeof(quint32));
203
        return indexes;
204
    }
205
}
206
207
/*!
208
    Set the glyph indexes for this QGlyphRun object to \a glyphIndexes. The glyph indexes must
209
    be valid for the selected font.
210
*/
211
void QGlyphRun::setGlyphIndexes(const QVector<quint32> &glyphIndexes)
212
{
213
    detach();
214
    d->glyphIndexes = glyphIndexes; // Keep a reference to the QVector to avoid copying
215
    d->glyphIndexData = glyphIndexes.constData();
216
    d->glyphIndexDataSize = glyphIndexes.size();
217
}
218
219
/*!
220
    Returns the position of the edge of the baseline for each glyph in this set of glyph indexes.
221
*/
222
QVector<QPointF> QGlyphRun::positions() const
223
{
224
    if (d->glyphPositions.constData() == d->glyphPositionData) {
225
        return d->glyphPositions;
226
    } else {
227
        QVector<QPointF> glyphPositions(d->glyphPositionDataSize);
228
        qMemCopy(glyphPositions.data(), d->glyphPositionData,
229
                 d->glyphPositionDataSize * sizeof(QPointF));
230
        return glyphPositions;
231
    }
232
}
233
234
/*!
235
    Sets the positions of the edge of the baseline for each glyph in this set of glyph indexes to
236
    \a positions.
237
*/
238
void QGlyphRun::setPositions(const QVector<QPointF> &positions)
239
{
240
    detach();
241
    d->glyphPositions = positions; // Keep a reference to the vector to avoid copying
242
    d->glyphPositionData = positions.constData();
243
    d->glyphPositionDataSize = positions.size();
244
}
245
246
/*!
247
    Clears all data in the QGlyphRun object.
248
*/
249
void QGlyphRun::clear()
250
{
251
    detach();
252
    d->rawFont = QRawFont();
253
    d->strikeOut = false;
254
    d->overline = false;
255
    d->underline = false;
256
257
    setPositions(QVector<QPointF>());
258
    setGlyphIndexes(QVector<quint32>());
259
}
260
261
/*!
262
    Sets the glyph indexes and positions of this QGlyphRun to use the first \a size
263
    elements in the arrays \a glyphIndexArray and \a glyphPositionArray. The data is
264
    \e not copied. The caller must guarantee that the arrays are not deleted as long
265
    as this QGlyphRun and any copies of it exists.
266
267
    \sa setGlyphIndexes(), setPositions()
268
*/
269
void QGlyphRun::setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray,
270
                           int size)
271
{
272
    detach();
273
    d->glyphIndexes.clear();
274
    d->glyphPositions.clear();
275
276
    d->glyphIndexData = glyphIndexArray;
277
    d->glyphPositionData = glyphPositionArray;
278
    d->glyphIndexDataSize = d->glyphPositionDataSize = size;
279
}
280
281
/*!
282
   Returns true if this QGlyphRun should be painted with an overline decoration.
283
284
   \sa setOverline()
285
*/
286
bool QGlyphRun::overline() const
287
{
288
    return d->overline;
289
}
290
291
/*!
292
  Indicates that this QGlyphRun should be painted with an overline decoration if \a overline is true.
293
  Otherwise the QGlyphRun should be painted with no overline decoration.
294
295
  \sa overline()
296
*/
297
void QGlyphRun::setOverline(bool overline)
298
{
299
    if (d->overline == overline)
300
        return;
301
302
    detach();
303
    d->overline = overline;
304
}
305
306
/*!
307
   Returns true if this QGlyphRun should be painted with an underline decoration.
308
309
   \sa setUnderline()
310
*/
311
bool QGlyphRun::underline() const
312
{
313
    return d->underline;
314
}
315
316
/*!
317
  Indicates that this QGlyphRun should be painted with an underline decoration if \a underline is
318
  true. Otherwise the QGlyphRun should be painted with no underline decoration.
319
320
  \sa underline()
321
*/
322
void QGlyphRun::setUnderline(bool underline)
323
{
324
    if (d->underline == underline)
325
        return;
326
327
    detach();
328
    d->underline = underline;
329
}
330
331
/*!
332
   Returns true if this QGlyphRun should be painted with a strike out decoration.
333
334
   \sa setStrikeOut()
335
*/
336
bool QGlyphRun::strikeOut() const
337
{
338
    return d->strikeOut;
339
}
340
341
/*!
342
  Indicates that this QGlyphRun should be painted with an strike out decoration if \a strikeOut is
343
  true. Otherwise the QGlyphRun should be painted with no strike out decoration.
344
345
  \sa strikeOut()
346
*/
347
void QGlyphRun::setStrikeOut(bool strikeOut)
348
{
349
    if (d->strikeOut == strikeOut)
350
        return;
351
352
    detach();
353
    d->strikeOut = strikeOut;
354
}
355
356
QT_END_NAMESPACE
357
358
#endif // QT_NO_RAWFONT