Get started with patching up the Qt GUI docs
[profile/ivi/qtbase.git] / src / gui / text / qglyphrun.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
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 #include <qdebug.h>
49
50 QT_BEGIN_NAMESPACE
51
52 /*!
53     \class QGlyphRun
54     \brief The QGlyphRun class provides direct access to the internal glyphs in a font.
55     \since 4.8
56     \inmodule QtGui
57
58     \ingroup text
59     \mainclass
60
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.
68
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.
74
75     It is the user's responsibility to ensure that the selected font actually contains the
76     provided glyph indexes.
77
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
80     glyphs.
81
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.
87 */
88
89 /*!
90   \enum QGlyphRun::GlyphRunFlag
91   \since 5.0
92
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
96   layout.
97
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
103   text object.
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.
112 */
113
114 /*!
115     Constructs an empty QGlyphRun object.
116 */
117 QGlyphRun::QGlyphRun() : d(new QGlyphRunPrivate)
118 {
119 }
120
121 /*!
122     Constructs a QGlyphRun object which is a copy of \a other.
123 */
124 QGlyphRun::QGlyphRun(const QGlyphRun &other)
125 {
126     d = other.d;
127 }
128
129 /*!
130     Destroys the QGlyphRun.
131 */
132 QGlyphRun::~QGlyphRun()
133 {
134     // Required for QExplicitlySharedDataPointer
135 }
136
137 /*!
138     \internal
139 */
140 void QGlyphRun::detach()
141 {
142     if (d->ref.load() != 1)
143         d.detach();
144 }
145
146 /*!
147     Assigns \a other to this QGlyphRun object.
148 */
149 QGlyphRun &QGlyphRun::operator=(const QGlyphRun &other)
150 {
151     d = other.d;
152     return *this;
153 }
154
155 /*!
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.
158 */
159 bool QGlyphRun::operator==(const QGlyphRun &other) const
160 {
161     if (d == other.d)
162         return true;
163
164     if ((d->glyphIndexDataSize != other.d->glyphIndexDataSize)
165      || (d->glyphPositionDataSize != other.d->glyphPositionDataSize)) {
166         return false;
167     }
168
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])
172                return false;
173         }
174     }
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])
178                return false;
179         }
180     }
181
182     return (d->flags == other.d->flags && d->rawFont == other.d->rawFont);
183 }
184
185 /*!
186     \fn bool QGlyphRun::operator!=(const QGlyphRun &other) const
187
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.
190 */
191
192 /*!
193     Returns the font selected for this QGlyphRun object.
194
195     \sa setRawFont()
196 */
197 QRawFont QGlyphRun::rawFont() const
198 {
199     return d->rawFont;
200 }
201
202 /*!
203     Sets the font in which to look up the glyph indexes to the \a rawFont
204     specified.
205
206     \sa rawFont(), setGlyphIndexes()
207 */
208 void QGlyphRun::setRawFont(const QRawFont &rawFont)
209 {
210     detach();
211     d->rawFont = rawFont;
212 }
213
214 /*!
215     Returns the glyph indexes for this QGlyphRun object.
216
217     \sa setGlyphIndexes(), setPositions()
218 */
219 QVector<quint32> QGlyphRun::glyphIndexes() const
220 {
221     if (d->glyphIndexes.constData() == d->glyphIndexData) {
222         return d->glyphIndexes;
223     } else {
224         QVector<quint32> indexes(d->glyphIndexDataSize);
225         memcpy(indexes.data(), d->glyphIndexData, d->glyphIndexDataSize * sizeof(quint32));
226         return indexes;
227     }
228 }
229
230 /*!
231     Set the glyph indexes for this QGlyphRun object to \a glyphIndexes. The glyph indexes must
232     be valid for the selected font.
233 */
234 void QGlyphRun::setGlyphIndexes(const QVector<quint32> &glyphIndexes)
235 {
236     detach();
237     d->glyphIndexes = glyphIndexes; // Keep a reference to the QVector to avoid copying
238     d->glyphIndexData = glyphIndexes.constData();
239     d->glyphIndexDataSize = glyphIndexes.size();
240 }
241
242 /*!
243     Returns the position of the edge of the baseline for each glyph in this set of glyph indexes.
244 */
245 QVector<QPointF> QGlyphRun::positions() const
246 {
247     if (d->glyphPositions.constData() == d->glyphPositionData) {
248         return d->glyphPositions;
249     } else {
250         QVector<QPointF> glyphPositions(d->glyphPositionDataSize);
251         memcpy(glyphPositions.data(), d->glyphPositionData,
252                  d->glyphPositionDataSize * sizeof(QPointF));
253         return glyphPositions;
254     }
255 }
256
257 /*!
258     Sets the positions of the edge of the baseline for each glyph in this set of glyph indexes to
259     \a positions.
260 */
261 void QGlyphRun::setPositions(const QVector<QPointF> &positions)
262 {
263     detach();
264     d->glyphPositions = positions; // Keep a reference to the vector to avoid copying
265     d->glyphPositionData = positions.constData();
266     d->glyphPositionDataSize = positions.size();
267 }
268
269 /*!
270     Clears all data in the QGlyphRun object.
271 */
272 void QGlyphRun::clear()
273 {
274     detach();
275     d->rawFont = QRawFont();
276     d->flags = 0;
277
278     setPositions(QVector<QPointF>());
279     setGlyphIndexes(QVector<quint32>());
280 }
281
282 /*!
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.
287
288     \sa setGlyphIndexes(), setPositions()
289 */
290 void QGlyphRun::setRawData(const quint32 *glyphIndexArray, const QPointF *glyphPositionArray,
291                            int size)
292 {
293     detach();
294     d->glyphIndexes.clear();
295     d->glyphPositions.clear();
296
297     d->glyphIndexData = glyphIndexArray;
298     d->glyphPositionData = glyphPositionArray;
299     d->glyphIndexDataSize = d->glyphPositionDataSize = size;
300 }
301
302 /*!
303    Returns true if this QGlyphRun should be painted with an overline decoration.
304
305    \sa setOverline(), flags()
306 */
307 bool QGlyphRun::overline() const
308 {
309     return d->flags & Overline;
310 }
311
312 /*!
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.
315
316   \sa overline(), setFlag(), setFlags()
317 */
318 void QGlyphRun::setOverline(bool overline)
319 {
320     setFlag(Overline, overline);
321 }
322
323 /*!
324    Returns true if this QGlyphRun should be painted with an underline decoration.
325
326    \sa setUnderline(), flags()
327 */
328 bool QGlyphRun::underline() const
329 {
330     return d->flags & Underline;
331 }
332
333 /*!
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.
336
337   \sa underline(), setFlag(), setFlags()
338 */
339 void QGlyphRun::setUnderline(bool underline)
340 {
341     setFlag(Underline, underline);
342 }
343
344 /*!
345    Returns true if this QGlyphRun should be painted with a strike out decoration.
346
347    \sa setStrikeOut(), flags()
348 */
349 bool QGlyphRun::strikeOut() const
350 {
351     return d->flags & StrikeOut;
352 }
353
354 /*!
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.
357
358   \sa strikeOut(), setFlag(), setFlags()
359 */
360 void QGlyphRun::setStrikeOut(bool strikeOut)
361 {
362     setFlag(StrikeOut, strikeOut);
363 }
364
365 /*!
366   Returns true if this QGlyphRun contains glyphs that are painted from the right to the left.
367
368   \since 5.0
369   \sa setRightToLeft(), flags()
370 */
371 bool QGlyphRun::isRightToLeft() const
372 {
373     return d->flags & RightToLeft;
374 }
375
376 /*!
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.
379
380   \since 5.0
381   \sa isRightToLeft(), setFlag(), setFlags()
382 */
383 void QGlyphRun::setRightToLeft(bool rightToLeft)
384 {
385     setFlag(RightToLeft, rightToLeft);
386 }
387
388 /*!
389   Returns the flags set for this QGlyphRun.
390
391   \since 5.0
392   \sa setFlag(), setFlag()
393 */
394 QGlyphRun::GlyphRunFlags QGlyphRun::flags() const
395 {
396     return d->flags;
397 }
398
399 /*!
400   If \a enabled is true, then \a flag is enabled; otherwise, it is disabled.
401
402   \since 5.0
403   \sa flags(), setFlags()
404 */
405 void QGlyphRun::setFlag(GlyphRunFlag flag, bool enabled)
406 {
407     if (d->flags.testFlag(flag) == enabled)
408         return;
409
410     detach();
411     if (enabled)
412         d->flags |= flag;
413     else
414         d->flags &= ~flag;
415 }
416
417 /*!
418   Sets the flags of this QGlyphRun to \a flags.
419
420   \since 5.0
421   \sa setFlag(), flags()
422 */
423 void QGlyphRun::setFlags(GlyphRunFlags flags)
424 {
425     if (d->flags == flags)
426         return;
427
428     detach();
429     d->flags = flags;
430 }
431
432 /*!
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.
436
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.
444
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.
448
449   \sa boundingRect()
450
451   \since 5.0
452 */
453 void QGlyphRun::setBoundingRect(const QRectF &boundingRect)
454 {
455     detach();
456     d->boundingRect = boundingRect;
457 }
458
459 /*!
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.
463
464   \since 5.0
465 */
466 QRectF QGlyphRun::boundingRect() const
467 {
468     if (!d->boundingRect.isEmpty())
469         return d->boundingRect;
470
471     qreal minX, minY, maxX, maxY;
472     minX = minY = maxX = maxY = 0;
473
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));
477
478         if (i == 0) {
479             minX = glyphRect.left();
480             minY = glyphRect.top();
481             maxX = glyphRect.right();
482             maxY = glyphRect.bottom();
483         } else {
484             minX = qMin(glyphRect.left(), minX);
485             minY = qMin(glyphRect.top(), minY);
486             maxX = qMax(glyphRect.right(),maxX);
487             maxY = qMax(glyphRect.bottom(), maxY);
488         }
489     }
490
491     return QRectF(QPointF(minX, minY), QPointF(maxX, maxY));
492 }
493
494 /*!
495   Returns true if the QGlyphRun does not contain any glyphs.
496
497   \since 5.0
498 */
499 bool QGlyphRun::isEmpty() const
500 {
501     return d->glyphIndexes.isEmpty();
502 }
503
504 QT_END_NAMESPACE
505
506 #endif // QT_NO_RAWFONT