71762df09fa55461610c54cac01fa4b82d25d365
[profile/ivi/qtbase.git] / src / gui / text / qrawfont.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 ** 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 "qrawfont.h"
47 #include "qrawfont_p.h"
48
49 #include <QtCore/qthread.h>
50 #include <QtCore/qendian.h>
51
52 QT_BEGIN_NAMESPACE
53
54 /*!
55    \class QRawFont
56    \brief The QRawFont class provides access to a single physical instance of a font.
57    \since 4.8
58
59    \ingroup text
60    \mainclass
61
62    \note QRawFont is a low level class. For most purposes QFont is a more appropriate class.
63
64    Most commonly, when presenting text in a user interface, the exact fonts used
65    to render the characters is to some extent unknown. This can be the case for several
66    reasons: For instance, the actual, physical fonts present on the target system could be
67    unexpected to the developers, or the text could contain user selected styles, sizes or
68    writing systems that are not supported by font chosen in the code.
69
70    Therefore, Qt's QFont class really represents a query for fonts. When text is interpreted,
71    Qt will do its best to match the text to the query, but depending on the support, different
72    fonts can be used behind the scenes.
73
74    For most use cases, this is both expected and necessary, as it minimizes the possibility of
75    text in the user interface being undisplayable. In some cases, however, more direct control
76    over the process might be useful. It is for these use cases the QRawFont class exists.
77
78    A QRawFont object represents a single, physical instance of a given font in a given pixel size.
79    I.e. in the typical case it represents a set of TrueType or OpenType font tables and uses a
80    user specified pixel size to convert metrics into logical pixel units. In can be used in
81    combination with the QGlyphRun class to draw specific glyph indexes at specific positions, and
82    also have accessors to some relevant data in the physical font.
83
84    QRawFont only provides support for the main font technologies: GDI and DirectWrite on Windows
85    platforms, FreeType on Symbian and Linux platforms and CoreText on Mac OS X. For other
86    font back-ends, the APIs will be disabled.
87
88    QRawFont can be constructed in a number of ways:
89    \list
90    \o It can be constructed by calling QTextLayout::glyphs() or QTextFragment::glyphs(). The
91       returned QGlyphs objects will contain QRawFont objects which represent the actual fonts
92       used to render each portion of the text.
93    \o It can be constructed by passing a QFont object to QRawFont::fromFont(). The function
94       will return a QRawFont object representing the font that will be selected as response to
95       the QFont query and the selected writing system.
96    \o It can be constructed by passing a file name or QByteArray directly to the QRawFont
97       constructor, or by calling loadFromFile() or loadFromData(). In this case, the
98       font will not be registered in QFontDatabase, and it will not be available as part of
99       regular font selection.
100    \endlist
101
102    QRawFont is considered local to the thread in which it is constructed (either using a
103    constructor, or by calling loadFromData() or loadFromFile()). The QRawFont cannot be moved to a
104    different thread, but will have to be recreated in the thread in question.
105
106    \note For the requirement of caching glyph indexes and font selections for static text to avoid
107    reshaping and relayouting in the inner loop of an application, a better choice is the QStaticText
108    class, since it optimizes the memory cost of the cache and also provides the possibility of paint
109    engine specific caches for an additional speed-up.
110 */
111
112 /*!
113     \enum QRawFont::AntialiasingType
114
115     This enum represents the different ways a glyph can be rasterized in the function
116     alphaMapForGlyph().
117
118     \value PixelAntialiasing Will rasterize by measuring the coverage of the shape on whole pixels.
119            The returned image contains the alpha values of each pixel based on the coverage of
120            the glyph shape.
121     \value SubPixelAntialiasing Will rasterize by measuring the coverage of each subpixel,
122            returning a separate alpha value for each of the red, green and blue components of
123            each pixel.
124 */
125
126 /*!
127    Constructs an invalid QRawFont.
128 */
129 QRawFont::QRawFont()
130     : d(new QRawFontPrivate)
131 {
132 }
133
134 /*!
135    Constructs a QRawFont representing the font contained in the file referenced
136    by \a fileName for the size (in pixels) given by \a pixelSize, and using the
137    hinting preference specified by \a hintingPreference.
138
139    \note The referenced file must contain a TrueType or OpenType font.
140 */
141 QRawFont::QRawFont(const QString &fileName,
142                    int pixelSize,
143                    QFont::HintingPreference hintingPreference)
144     : d(new QRawFontPrivate)
145 {
146     loadFromFile(fileName, pixelSize, hintingPreference);
147 }
148
149 /*!
150    Constructs a QRawFont representing the font contained in the supplied
151    \a fontData for the size (in pixels) given by \a pixelSize, and using the
152    hinting preference specified by \a hintingPreference.
153
154    \note The data must contain a TrueType or OpenType font.
155 */
156 QRawFont::QRawFont(const QByteArray &fontData,
157                    int pixelSize,
158                    QFont::HintingPreference hintingPreference)
159     : d(new QRawFontPrivate)
160 {
161     loadFromData(fontData, pixelSize, hintingPreference);
162 }
163
164 /*!
165    Creates a QRawFont which is a copy of \a other.
166 */
167 QRawFont::QRawFont(const QRawFont &other)
168 {
169     d = other.d;
170 }
171
172 /*!
173    Destroys the QRawFont
174 */
175 QRawFont::~QRawFont()
176 {
177 }
178
179 /*!
180   Assigns \a other to this QRawFont.
181 */
182 QRawFont &QRawFont::operator=(const QRawFont &other)
183 {
184     d = other.d;
185     return *this;
186 }
187
188 /*!
189    Returns true if the QRawFont is valid and false otherwise.
190 */
191 bool QRawFont::isValid() const
192 {
193     Q_ASSERT(d->thread == 0 || d->thread == QThread::currentThread());
194     return d->fontEngine != 0;
195 }
196
197 /*!
198    Replaces the current QRawFont with the contents of the file referenced
199    by \a fileName for the size (in pixels) given by \a pixelSize, and using the
200    hinting preference specified by \a hintingPreference.
201
202    The file must reference a TrueType or OpenType font.
203
204    \sa loadFromData()
205 */
206 void QRawFont::loadFromFile(const QString &fileName,
207                             int pixelSize,
208                             QFont::HintingPreference hintingPreference)
209 {
210     QFile file(fileName);
211     if (file.open(QIODevice::ReadOnly))
212         loadFromData(file.readAll(), pixelSize, hintingPreference);
213 }
214
215 /*!
216    Replaces the current QRawFont with the font contained in the supplied
217    \a fontData for the size (in pixels) given by \a pixelSize, and using the
218    hinting preference specified by \a hintingPreference.
219
220    The \a fontData must contain a TrueType or OpenType font.
221
222    \sa loadFromFile()
223 */
224 void QRawFont::loadFromData(const QByteArray &fontData,
225                             int pixelSize,
226                             QFont::HintingPreference hintingPreference)
227 {
228     detach();
229     d->cleanUp();
230     d->hintingPreference = hintingPreference;
231     d->thread = QThread::currentThread();
232     d->platformLoadFromData(fontData, pixelSize, hintingPreference);
233 }
234
235 /*!
236    This function returns a rasterized image of the glyph at the given
237    \a glyphIndex in the underlying font, using the \a transform specified.
238    If the QRawFont is not valid, this function will return an invalid QImage.
239
240    If \a antialiasingType is set to QRawFont::SubPixelAntialiasing, then the resulting image will be
241    in QImage::Format_RGB32 and the RGB values of each pixel will represent the subpixel opacities of
242    the pixel in the rasterization of the glyph. Otherwise, the image will be in the format of
243    QImage::Format_A8 and each pixel will contain the opacity of the pixel in the rasterization.
244
245    \sa pathForGlyph(), QPainter::drawGlyphRun()
246 */
247 QImage QRawFont::alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType,
248                                   const QTransform &transform) const
249 {
250     if (!isValid())
251         return QImage();
252
253     if (antialiasingType == SubPixelAntialiasing)
254         return d->fontEngine->alphaRGBMapForGlyph(glyphIndex, QFixed(), 0, transform);
255     else
256         return d->fontEngine->alphaMapForGlyph(glyphIndex, QFixed(), transform);
257 }
258
259 /*!
260    This function returns the shape of the glyph at a given \a glyphIndex in the underlying font
261    if the QRawFont is valid. Otherwise, it returns an empty QPainterPath.
262
263    The returned glyph will always be unhinted.
264
265    \sa alphaMapForGlyph(), QPainterPath::addText()
266 */
267 QPainterPath QRawFont::pathForGlyph(quint32 glyphIndex) const
268 {
269     if (!isValid())
270         return QPainterPath();
271
272     QFixedPoint position;
273     QPainterPath path;
274     d->fontEngine->addGlyphsToPath(&glyphIndex, &position, 1, &path, 0);
275     return path;
276 }
277
278 /*!
279    Returns true if this QRawFont is equal to \a other. Otherwise, returns false.
280 */
281 bool QRawFont::operator==(const QRawFont &other) const
282 {
283     return d->fontEngine == other.d->fontEngine;
284 }
285
286 /*!
287    Returns the ascent of this QRawFont in pixel units.
288
289    \sa QFontMetricsF::ascent()
290 */
291 qreal QRawFont::ascent() const
292 {
293     if (!isValid())
294         return 0.0;
295
296     return d->fontEngine->ascent().toReal();
297 }
298
299 /*!
300    Returns the descent of this QRawFont in pixel units.
301
302    \sa QFontMetricsF::descent()
303 */
304 qreal QRawFont::descent() const
305 {
306     if (!isValid())
307         return 0.0;
308
309     return d->fontEngine->descent().toReal();
310 }
311
312 /*!
313    Returns the xHeight of this QRawFont in pixel units.
314
315    \sa QFontMetricsF::xHeight()
316 */
317 qreal QRawFont::xHeight() const
318 {
319     if (!isValid())
320         return 0.0;
321
322     return d->fontEngine->xHeight().toReal();
323 }
324
325 /*!
326    Returns the leading of this QRawFont in pixel units.
327
328    \sa QFontMetricsF::leading()
329 */
330 qreal QRawFont::leading() const
331 {
332     if (!isValid())
333         return 0.0;
334
335     return d->fontEngine->leading().toReal();
336 }
337
338 /*!
339    Returns the average character width of this QRawFont in pixel units.
340
341    \sa QFontMetricsF::averageCharWidth()
342 */
343 qreal QRawFont::averageCharWidth() const
344 {
345     if (!isValid())
346         return 0.0;
347
348     return d->fontEngine->averageCharWidth().toReal();
349 }
350
351 /*!
352    Returns the width of the widest character in the font.
353
354    \sa QFontMetricsF::maxWidth()
355 */
356 qreal QRawFont::maxCharWidth() const
357 {
358     if (!isValid())
359         return 0.0;
360
361     return d->fontEngine->maxCharWidth();
362 }
363
364 /*!
365    Returns the pixel size set for this QRawFont. The pixel size affects how glyphs are
366    rasterized, the size of glyphs returned by pathForGlyph(), and is used to convert
367    internal metrics from design units to logical pixel units.
368
369    \sa setPixelSize()
370 */
371 qreal QRawFont::pixelSize() const
372 {
373     if (!isValid())
374         return 0.0;
375
376     return d->fontEngine->fontDef.pixelSize;
377 }
378
379 /*!
380    Returns the number of design units define the width and height of the em square
381    for this QRawFont. This value is used together with the pixel size when converting design metrics
382    to pixel units, as the internal metrics are specified in design units and the pixel size gives
383    the size of 1 em in pixels.
384
385    \sa pixelSize(), setPixelSize()
386 */
387 qreal QRawFont::unitsPerEm() const
388 {
389     if (!isValid())
390         return 0.0;
391
392     return d->fontEngine->emSquareSize().toReal();
393 }
394
395 /*!
396    Returns the family name of this QRawFont.
397 */
398 QString QRawFont::familyName() const
399 {
400     if (!isValid())
401         return QString();
402
403     return d->fontEngine->fontDef.family;
404 }
405
406 /*!
407    Returns the style name of this QRawFont.
408
409    \sa QFont::styleName()
410 */
411 QString QRawFont::styleName() const
412 {
413     if (!isValid())
414         return QString();
415
416     return d->fontEngine->fontDef.styleName;
417 }
418
419 /*!
420    Returns the style of this QRawFont.
421
422    \sa QFont::style()
423 */
424 QFont::Style QRawFont::style() const
425 {
426     if (!isValid())
427         return QFont::StyleNormal;
428
429     return QFont::Style(d->fontEngine->fontDef.style);
430 }
431
432 /*!
433    Returns the weight of this QRawFont.
434
435    \sa QFont::weight()
436 */
437 int QRawFont::weight() const
438 {
439     if (!isValid())
440         return -1;
441
442     return int(d->fontEngine->fontDef.weight);
443 }
444
445 /*!
446    Converts the string of unicode points given by \a text to glyph indexes
447    using the CMAP table in the underlying font, and returns a vector containing
448    the result.
449
450    Note that, in cases where there are other tables in the font that affect the
451    shaping of the text, the returned glyph indexes will not correctly represent
452    the rendering of the text. To get the correctly shaped text, you can use
453    QTextLayout to lay out and shape the text, then call QTextLayout::glyphs()
454    to get the set of glyph index list and QRawFont pairs.
455
456    \sa advancesForGlyphIndexes(), glyphIndexesForChars(), QGlyphRun, QTextLayout::glyphRuns(), QTextFragment::glyphRuns()
457 */
458 QVector<quint32> QRawFont::glyphIndexesForString(const QString &text) const
459 {
460     if (!isValid())
461         return QVector<quint32>();
462
463     int nglyphs = text.size();
464     QVarLengthGlyphLayoutArray glyphs(nglyphs);
465     if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) {
466         glyphs.resize(nglyphs);
467         if (!glyphIndexesForChars(text.data(), text.size(), glyphs.glyphs, &nglyphs)) {
468             Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
469             return QVector<quint32>();
470         }
471     }
472
473     QVector<quint32> glyphIndexes;
474     for (int i=0; i<nglyphs; ++i)
475         glyphIndexes.append(glyphs.glyphs[i]);
476
477     return glyphIndexes;
478 }
479
480 /*!
481    Converts a string of unicode points to glyph indexes using the CMAP table in the
482    underlying font. The function works like glyphIndexesForString() except it take
483    an array (\a chars), the results will be returned though \a glyphIndexes array
484    and number of glyphs will be set in \a numGlyphs. The size of \a glyphIndexes array
485    must be at least \a numChars, if that's still not enough, this function will return
486    false, then you can resize \a glyphIndexes from the size returned in \a numGlyphs.
487
488    \sa glyphIndexesForString(), advancesForGlyphIndexes(), QGlyphs, QTextLayout::glyphs(), QTextFragment::glyphs()
489 */
490 bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
491 {
492     if (!isValid())
493         return false;
494
495     QGlyphLayout glyphs;
496     glyphs.glyphs = glyphIndexes;
497     return d->fontEngine->stringToCMap(chars, numChars, &glyphs, numGlyphs, QTextEngine::GlyphIndicesOnly);
498 }
499
500 /*!
501    Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
502    give the distance from the position of a given glyph to where the next glyph should be drawn
503    to make it appear as if the two glyphs are unspaced.
504
505    \sa QTextLine::horizontalAdvance(), QFontMetricsF::width()
506 */
507 QVector<QPointF> QRawFont::advancesForGlyphIndexes(const QVector<quint32> &glyphIndexes) const
508 {
509     if (!isValid())
510         return QVector<QPointF>();
511
512     int numGlyphs = glyphIndexes.size();
513     QVarLengthGlyphLayoutArray glyphs(numGlyphs);
514     qMemCopy(glyphs.glyphs, glyphIndexes.data(), numGlyphs * sizeof(quint32));
515
516     d->fontEngine->recalcAdvances(&glyphs, 0);
517
518     QVector<QPointF> advances;
519     for (int i=0; i<numGlyphs; ++i)
520         advances.append(QPointF(glyphs.advances_x[i].toReal(), glyphs.advances_y[i].toReal()));
521
522     return advances;
523 }
524
525 /*!
526    Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
527    give the distance from the position of a given glyph to where the next glyph should be drawn
528    to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the
529    array \a glyphIndexes while the results are returned through \a advances, both of them must
530    have \a numGlyphs elements.
531
532    \sa QTextLine::horizontalAdvance(), QFontMetricsF::width()
533 */
534 bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
535 {
536     if (!isValid())
537         return false;
538
539     QGlyphLayout glyphs;
540     glyphs.glyphs = const_cast<HB_Glyph *>(glyphIndexes);
541     glyphs.numGlyphs = numGlyphs;
542     QVarLengthArray<QFixed> advances_x(numGlyphs);
543     QVarLengthArray<QFixed> advances_y(numGlyphs);
544     glyphs.advances_x = advances_x.data();
545     glyphs.advances_y = advances_y.data();
546
547     d->fontEngine->recalcAdvances(&glyphs, 0);
548
549     for (int i=0; i<numGlyphs; ++i)
550         advances[i] = QPointF(glyphs.advances_x[i].toReal(), glyphs.advances_y[i].toReal());
551
552     return true;
553 }
554
555 /*!
556    Returns the hinting preference used to construct this QRawFont.
557
558    \sa QFont::hintingPreference()
559 */
560 QFont::HintingPreference QRawFont::hintingPreference() const
561 {
562     if (!isValid())
563         return QFont::PreferDefaultHinting;
564
565     return d->hintingPreference;
566 }
567
568 /*!
569    Retrieves the sfnt table named \a tagName from the underlying physical font, or an empty
570    byte array if no such table was found. The returned font table's byte order is Big Endian, like
571    the sfnt format specifies. The \a tagName must be four characters long and should be formatted
572    in the default endianness of the current platform.
573 */
574 QByteArray QRawFont::fontTable(const char *tagName) const
575 {
576     if (!isValid())
577         return QByteArray();
578
579     const quint32 *tagId = reinterpret_cast<const quint32 *>(tagName);
580     return d->fontEngine->getSfntTable(qToBigEndian(*tagId));
581 }
582
583 // From qfontdatabase.cpp
584 extern QList<QFontDatabase::WritingSystem> qt_determine_writing_systems_from_truetype_bits(quint32 unicodeRange[4], quint32 codePageRange[2]);
585
586 /*!
587    Returns a list of writing systems supported by the font according to designer supplied
588    information in the font file. Please note that this does not guarantee support for a
589    specific unicode point in the font. You can use the supportsCharacter() to check support
590    for a single, specific character.
591
592    \note The list is determined based on the unicode ranges and codepage ranges set in the font's
593    OS/2 table and requires such a table to be present in the underlying font file.
594
595    \sa supportsCharacter()
596 */
597 QList<QFontDatabase::WritingSystem> QRawFont::supportedWritingSystems() const
598 {
599     if (isValid()) {
600         QByteArray os2Table = fontTable("OS/2");
601         if (!os2Table.isEmpty() && os2Table.size() > 86) {
602             char *data = os2Table.data();
603             quint32 *bigEndianUnicodeRanges = reinterpret_cast<quint32 *>(data + 42);
604             quint32 *bigEndianCodepageRanges = reinterpret_cast<quint32 *>(data + 78);
605
606             quint32 unicodeRanges[4];
607             quint32 codepageRanges[2];
608
609             for (int i=0; i<4; ++i) {
610                 if (i < 2)
611                     codepageRanges[i] = qFromBigEndian(bigEndianCodepageRanges[i]);
612                 unicodeRanges[i] = qFromBigEndian(bigEndianUnicodeRanges[i]);
613             }
614
615             return qt_determine_writing_systems_from_truetype_bits(unicodeRanges, codepageRanges);
616         }
617     }
618
619     return QList<QFontDatabase::WritingSystem>();
620 }
621
622 /*!
623     Returns true if the font has a glyph that corresponds to the given \a character.
624
625     \sa supportedWritingSystems()
626 */
627 bool QRawFont::supportsCharacter(const QChar &character) const
628 {
629     if (!isValid())
630         return false;
631
632     return d->fontEngine->canRender(&character, 1);
633 }
634
635 /*!
636    Returns true if the font has a glyph that corresponds to the UCS-4 encoded character \a ucs4.
637
638    \sa supportedWritingSystems()
639 */
640 bool QRawFont::supportsCharacter(quint32 ucs4) const
641 {
642     if (!isValid())
643         return false;
644
645     QString str = QString::fromUcs4(&ucs4, 1);
646     return d->fontEngine->canRender(str.constData(), str.size());
647 }
648
649 // qfontdatabase.cpp
650 extern int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem);
651
652 /*!
653    Fetches the physical representation based on a \a font query. The physical font returned is
654    the font that will be preferred by Qt in order to display text in the selected \a writingSystem.
655 */
656 QRawFont QRawFont::fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem)
657 {
658 #if defined(Q_WS_MAC)
659     QTextLayout layout(QFontDatabase::writingSystemSample(writingSystem), font);
660     layout.beginLayout();
661     QTextLine line = layout.createLine();
662     layout.endLayout();
663     QList<QGlyphRun> list = layout.glyphRuns();
664     if (list.size()) {
665         // Pick the one matches the family name we originally requested,
666         // if none of them match, just pick the first one
667         for (int i = 0; i < list.size(); i++) {
668             QGlyphRun glyphs = list.at(i);
669             QRawFont rawfont = glyphs.rawFont();
670             if (rawfont.familyName() == font.family())
671                 return rawfont;
672         }
673         return list.at(0).rawFont();
674     }
675     return QRawFont();
676 #else
677     QFontPrivate *font_d = QFontPrivate::get(font);
678     int script = qt_script_for_writing_system(writingSystem);
679     QFontEngine *fe = font_d->engineForScript(script);
680
681     if (fe != 0 && fe->type() == QFontEngine::Multi) {
682         QFontEngineMulti *multiEngine = static_cast<QFontEngineMulti *>(fe);
683         fe = multiEngine->engine(0);
684         if (fe == 0) {
685             multiEngine->loadEngine(0);
686             fe = multiEngine->engine(0);
687         }
688     }
689
690     if (fe != 0) {
691         QRawFont rawFont;
692         rawFont.d.data()->fontEngine = fe;
693         rawFont.d.data()->fontEngine->ref.ref();
694         rawFont.d.data()->hintingPreference = font.hintingPreference();
695         return rawFont;
696     } else {
697         return QRawFont();
698     }
699 #endif
700 }
701
702 /*!
703    Sets the pixel size with which this font should be rendered to \a pixelSize.
704 */
705 void QRawFont::setPixelSize(qreal pixelSize)
706 {
707     if (d->fontEngine == 0)
708         return;
709
710     detach();
711     QFontEngine *oldFontEngine = d->fontEngine;
712
713     d->fontEngine = d->fontEngine->cloneWithSize(pixelSize);
714     if (d->fontEngine != 0)
715         d->fontEngine->ref.ref();
716
717     oldFontEngine->ref.deref();
718     if (oldFontEngine->cache_count == 0 && oldFontEngine->ref == 0)
719         delete oldFontEngine;
720 }
721
722 /*!
723     \internal
724 */
725 void QRawFont::detach()
726 {
727     if (d->ref != 1)
728         d.detach();
729 }
730
731 /*!
732     \internal
733 */
734 void QRawFontPrivate::cleanUp()
735 {
736     platformCleanUp();
737     if (fontEngine != 0) {
738         fontEngine->ref.deref();
739         if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
740             delete fontEngine;
741         fontEngine = 0;
742     }
743     hintingPreference = QFont::PreferDefaultHinting;
744 }
745
746 /*!
747   Returns the smallest rectangle containing the glyph with the given \a glyphIndex.
748
749   \since 5.0
750 */
751 QRectF QRawFont::boundingRect(quint32 glyphIndex) const
752 {
753     if (!isValid())
754         return QRectF();
755
756     glyph_metrics_t gm = d->fontEngine->boundingBox(glyphIndex);
757     return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
758 }
759
760 #endif // QT_NO_RAWFONT
761
762 QT_END_NAMESPACE