Rename QGlyphs -> QGlyphRun
[profile/ivi/qtbase.git] / src / gui / text / qglyphrun.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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     return ((d == other.d)
136             || (d->glyphIndexes == other.d->glyphIndexes
137                 && d->glyphPositions == other.d->glyphPositions
138                 && d->overline == other.d->overline
139                 && d->underline == other.d->underline
140                 && d->strikeOut == other.d->strikeOut
141                 && d->rawFont == other.d->rawFont));
142 }
143
144 /*!
145     Compares \a other to this QGlyphRun object. Returns true if any of the list of glyph
146     indexes, the list of positions or the font are different, otherwise returns false.
147 */
148 bool QGlyphRun::operator!=(const QGlyphRun &other) const
149 {
150     return !(*this == other);
151 }
152
153 /*!
154     \internal
155
156     Adds together the lists of glyph indexes and positions in \a other and this QGlyphRun
157     object and returns the result. The font in the returned QGlyphRun will be the same as in
158     this QGlyphRun object.
159 */
160 QGlyphRun QGlyphRun::operator+(const QGlyphRun &other) const
161 {
162     QGlyphRun ret(*this);
163     ret += other;
164     return ret;
165 }
166
167 /*!
168     \internal
169
170     Appends the glyph indexes and positions in \a other to this QGlyphRun object and returns
171     a reference to the current object.
172 */
173 QGlyphRun &QGlyphRun::operator+=(const QGlyphRun &other)
174 {
175     detach();
176
177     d->glyphIndexes += other.d->glyphIndexes;
178     d->glyphPositions += other.d->glyphPositions;
179
180     return *this;
181 }
182
183 /*!
184     Returns the font selected for this QGlyphRun object.
185
186     \sa setRawFont()
187 */
188 QRawFont QGlyphRun::rawFont() const
189 {
190     return d->rawFont;
191 }
192
193 /*!
194     Sets the font in which to look up the glyph indexes to \a font.
195
196     \sa rawFont(), setGlyphIndexes()
197 */
198 void QGlyphRun::setRawFont(const QRawFont &rawFont)
199 {
200     detach();
201     d->rawFont = rawFont;
202 }
203
204 /*!
205     Returns the glyph indexes for this QGlyphRun object.
206
207     \sa setGlyphIndexes(), setPositions()
208 */
209 QVector<quint32> QGlyphRun::glyphIndexes() const
210 {
211     return d->glyphIndexes;
212 }
213
214 /*!
215     Set the glyph indexes for this QGlyphRun object to \a glyphIndexes. The glyph indexes must
216     be valid for the selected font.
217 */
218 void QGlyphRun::setGlyphIndexes(const QVector<quint32> &glyphIndexes)
219 {
220     detach();
221     d->glyphIndexes = glyphIndexes;
222 }
223
224 /*!
225     Returns the position of the edge of the baseline for each glyph in this set of glyph indexes.
226 */
227 QVector<QPointF> QGlyphRun::positions() const
228 {
229     return d->glyphPositions;
230 }
231
232 /*!
233     Sets the positions of the edge of the baseline for each glyph in this set of glyph indexes to
234     \a positions.
235 */
236 void QGlyphRun::setPositions(const QVector<QPointF> &positions)
237 {
238     detach();
239     d->glyphPositions = positions;
240 }
241
242 /*!
243     Clears all data in the QGlyphRun object.
244 */
245 void QGlyphRun::clear()
246 {
247     detach();
248     d->glyphPositions = QVector<QPointF>();
249     d->glyphIndexes = QVector<quint32>();
250     d->rawFont = QRawFont();
251     d->strikeOut = false;
252     d->overline = false;
253     d->underline = false;
254 }
255
256 /*!
257    Returns true if this QGlyphRun should be painted with an overline decoration.
258
259    \sa setOverline()
260 */
261 bool QGlyphRun::overline() const
262 {
263     return d->overline;
264 }
265
266 /*!
267   Indicates that this QGlyphRun should be painted with an overline decoration if \a overline is true.
268   Otherwise the QGlyphRun should be painted with no overline decoration.
269
270   \sa overline()
271 */
272 void QGlyphRun::setOverline(bool overline)
273 {
274     detach();
275     d->overline = overline;
276 }
277
278 /*!
279    Returns true if this QGlyphRun should be painted with an underline decoration.
280
281    \sa setUnderline()
282 */
283 bool QGlyphRun::underline() const
284 {
285     return d->underline;
286 }
287
288 /*!
289   Indicates that this QGlyphRun should be painted with an underline decoration if \a underline is
290   true. Otherwise the QGlyphRun should be painted with no underline decoration.
291
292   \sa underline()
293 */
294 void QGlyphRun::setUnderline(bool underline)
295 {
296     detach();
297     d->underline = underline;
298 }
299
300 /*!
301    Returns true if this QGlyphRun should be painted with a strike out decoration.
302
303    \sa setStrikeOut()
304 */
305 bool QGlyphRun::strikeOut() const
306 {
307     return d->strikeOut;
308 }
309
310 /*!
311   Indicates that this QGlyphRun should be painted with an strike out decoration if \a strikeOut is
312   true. Otherwise the QGlyphRun should be painted with no strike out decoration.
313
314   \sa strikeOut()
315 */
316 void QGlyphRun::setStrikeOut(bool strikeOut)
317 {
318     detach();
319     d->strikeOut = strikeOut;
320 }
321
322 QT_END_NAMESPACE
323
324 #endif // QT_NO_RAWFONT