1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qglobal.h"
5
6#if !defined(QT_NO_RAWFONT)
7
8#include "qrawfont.h"
9#include "qrawfont_p.h"
10#include "qplatformfontdatabase.h"
11
12#include <private/qguiapplication_p.h>
13#include <qpa/qplatformintegration.h>
14#include <qpa/qplatformfontdatabase.h>
15
16#include <QtCore/qendian.h>
17#include <QtCore/qfile.h>
18#include <QtGui/qpainterpath.h>
19
20QT_BEGIN_NAMESPACE
21
22/*!
23 \class QRawFont
24 \brief The QRawFont class provides access to a single physical instance of a font.
25 \since 4.8
26 \inmodule QtGui
27
28 \ingroup text
29 \ingroup shared
30
31 \note QRawFont is a low level class. For most purposes QFont is a more appropriate class.
32
33 Most commonly, when presenting text in a user interface, the exact fonts used
34 to render the characters is to some extent unknown. This can be the case for several
35 reasons: For instance, the actual, physical fonts present on the target system could be
36 unexpected to the developers, or the text could contain user selected styles, sizes or
37 writing systems that are not supported by font chosen in the code.
38
39 Therefore, Qt's QFont class really represents a query for fonts. When text is interpreted,
40 Qt will do its best to match the text to the query, but depending on the support, different
41 fonts can be used behind the scenes.
42
43 For most use cases, this is both expected and necessary, as it minimizes the possibility of
44 text in the user interface being undisplayable. In some cases, however, more direct control
45 over the process might be useful. It is for these use cases the QRawFont class exists.
46
47 A QRawFont object represents a single, physical instance of a given font in a given pixel size.
48 I.e. in the typical case it represents a set of TrueType or OpenType font tables and uses a
49 user specified pixel size to convert metrics into logical pixel units. It can be used in
50 combination with the QGlyphRun class to draw specific glyph indexes at specific positions, and
51 also have accessors to some relevant data in the physical font.
52
53 QRawFont only provides support for the main font technologies: GDI and DirectWrite on Windows
54 platforms, FreeType on Linux platforms and CoreText on \macos. For other
55 font back-ends, the APIs will be disabled.
56
57 QRawFont can be constructed in a number of ways:
58 \list
59 \li It can be constructed by calling QTextLayout::glyphs() or QTextFragment::glyphs(). The
60 returned QGlyphs objects will contain QRawFont objects which represent the actual fonts
61 used to render each portion of the text.
62 \li It can be constructed by passing a QFont object to QRawFont::fromFont(). The function
63 will return a QRawFont object representing the font that will be selected as response to
64 the QFont query and the selected writing system.
65 \li It can be constructed by passing a file name or QByteArray directly to the QRawFont
66 constructor, or by calling loadFromFile() or loadFromData(). In this case, the
67 font will not be registered in QFontDatabase, and it will not be available as part of
68 regular font selection.
69 \endlist
70
71 QRawFont is considered local to the thread in which it is constructed (either using a
72 constructor, or by calling loadFromData() or loadFromFile()). The QRawFont cannot be moved to a
73 different thread, but will have to be recreated in the thread in question.
74
75 \note For the requirement of caching glyph indexes and font selections for static text to avoid
76 reshaping and relayouting in the inner loop of an application, a better choice is the QStaticText
77 class, since it optimizes the memory cost of the cache and also provides the possibility of paint
78 engine specific caches for an additional speed-up.
79*/
80
81/*!
82 \enum QRawFont::AntialiasingType
83
84 This enum represents the different ways a glyph can be rasterized in the function
85 alphaMapForGlyph().
86
87 \value PixelAntialiasing Will rasterize by measuring the coverage of the shape on whole pixels.
88 The returned image contains the alpha values of each pixel based on the coverage of
89 the glyph shape.
90 \value SubPixelAntialiasing Will rasterize by measuring the coverage of each subpixel,
91 returning a separate alpha value for each of the red, green and blue components of
92 each pixel.
93*/
94
95/*!
96 \enum QRawFont::LayoutFlag
97 \since 5.1
98
99 This enum tells the function advancesForGlyphIndexes() how to calculate the advances.
100
101 \value SeparateAdvances Will calculate the advance for each glyph separately.
102 \value KernedAdvances Will apply kerning between adjacent glyphs. Note that OpenType GPOS based
103 kerning is currently not supported.
104 \value UseDesignMetrics Use design metrics instead of hinted metrics adjusted to the resolution
105 of the paint device.
106 Can be OR-ed with any of the options above.
107*/
108
109/*!
110 Constructs an invalid QRawFont.
111*/
112QRawFont::QRawFont()
113 : d(new QRawFontPrivate)
114{
115}
116
117/*!
118 Constructs a QRawFont representing the font contained in the file referenced
119 by \a fileName for the size (in pixels) given by \a pixelSize, and using the
120 hinting preference specified by \a hintingPreference.
121
122 \note The referenced file must contain a TrueType or OpenType font.
123*/
124QRawFont::QRawFont(const QString &fileName,
125 qreal pixelSize,
126 QFont::HintingPreference hintingPreference)
127 : d(new QRawFontPrivate)
128{
129 loadFromFile(fileName, pixelSize, hintingPreference);
130}
131
132/*!
133 Constructs a QRawFont representing the font contained in the supplied
134 \a fontData for the size (in pixels) given by \a pixelSize, and using the
135 hinting preference specified by \a hintingPreference.
136
137 \note The data must contain a TrueType or OpenType font.
138*/
139QRawFont::QRawFont(const QByteArray &fontData,
140 qreal pixelSize,
141 QFont::HintingPreference hintingPreference)
142 : d(new QRawFontPrivate)
143{
144 loadFromData(fontData, pixelSize, hintingPreference);
145}
146
147/*!
148 Creates a QRawFont which is a copy of \a other.
149*/
150QRawFont::QRawFont(const QRawFont &other)
151{
152 d = other.d;
153}
154
155/*!
156 Destroys the QRawFont
157*/
158QRawFont::~QRawFont()
159{
160}
161
162/*!
163 Assigns \a other to this QRawFont.
164*/
165QRawFont &QRawFont::operator=(const QRawFont &other)
166{
167 d = other.d;
168 return *this;
169}
170
171/*!
172 \fn void QRawFont::swap(QRawFont &other)
173 \since 5.0
174
175 Swaps this raw font with \a other. This function is very fast and
176 never fails.
177*/
178
179/*!
180 Returns \c true if the QRawFont is valid and false otherwise.
181*/
182bool QRawFont::isValid() const
183{
184 return d->isValid();
185}
186
187/*!
188 Replaces the current QRawFont with the contents of the file referenced
189 by \a fileName for the size (in pixels) given by \a pixelSize, and using the
190 hinting preference specified by \a hintingPreference.
191
192 The file must reference a TrueType or OpenType font.
193
194 \sa loadFromData()
195*/
196void QRawFont::loadFromFile(const QString &fileName,
197 qreal pixelSize,
198 QFont::HintingPreference hintingPreference)
199{
200 QFile file(fileName);
201 if (file.open(flags: QIODevice::ReadOnly))
202 loadFromData(fontData: file.readAll(), pixelSize, hintingPreference);
203}
204
205/*!
206 Replaces the current QRawFont with the font contained in the supplied
207 \a fontData for the size (in pixels) given by \a pixelSize, and using the
208 hinting preference specified by \a hintingPreference.
209
210 The \a fontData must contain a TrueType or OpenType font.
211
212 \sa loadFromFile()
213*/
214void QRawFont::loadFromData(const QByteArray &fontData,
215 qreal pixelSize,
216 QFont::HintingPreference hintingPreference)
217{
218 d.detach();
219 d->cleanUp();
220 d->hintingPreference = hintingPreference;
221 d->loadFromData(fontData, pixelSize, hintingPreference);
222}
223
224/*!
225 This function returns a rasterized image of the glyph at the given
226 \a glyphIndex in the underlying font, using the \a transform specified.
227 If the QRawFont is not valid, this function will return an invalid QImage.
228
229 If the font is a color font, then the resulting image will contain the rendered
230 glyph at the current pixel size. In this case, the \a antialiasingType will be
231 ignored.
232
233 Otherwise, if \a antialiasingType is set to QRawFont::SubPixelAntialiasing, then the resulting image
234 will be in QImage::Format_RGB32 and the RGB values of each pixel will represent the subpixel opacities
235 of the pixel in the rasterization of the glyph. Otherwise, the image will be in the format of
236 QImage::Format_Indexed8 and each pixel will contain the opacity of the pixel in the
237 rasterization.
238
239 \sa pathForGlyph(), QPainter::drawGlyphRun()
240*/
241QImage QRawFont::alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType,
242 const QTransform &transform) const
243{
244 if (!d->isValid())
245 return QImage();
246
247 if (d->fontEngine->glyphFormat == QFontEngine::Format_ARGB)
248 return d->fontEngine->bitmapForGlyph(glyphIndex, subPixelPosition: QFixedPoint(), t: transform);
249
250 if (antialiasingType == SubPixelAntialiasing)
251 return d->fontEngine->alphaRGBMapForGlyph(glyphIndex, subPixelPosition: QFixedPoint(), t: transform);
252
253 return d->fontEngine->alphaMapForGlyph(glyphIndex, subPixelPosition: QFixedPoint(), t: transform);
254}
255
256/*!
257 This function returns the shape of the glyph at a given \a glyphIndex in the underlying font
258 if the QRawFont is valid. Otherwise, it returns an empty QPainterPath.
259
260 The returned glyph will always be unhinted.
261
262 \sa alphaMapForGlyph(), QPainterPath::addText()
263*/
264QPainterPath QRawFont::pathForGlyph(quint32 glyphIndex) const
265{
266 if (!d->isValid())
267 return QPainterPath();
268
269 QFixedPoint position;
270 QPainterPath path;
271 d->fontEngine->addGlyphsToPath(glyphs: &glyphIndex, positions: &position, nglyphs: 1, path: &path, flags: { });
272 return path;
273}
274
275/*!
276 Returns \c true if this QRawFont is equal to \a other. Otherwise, returns \c false.
277*/
278bool QRawFont::operator==(const QRawFont &other) const
279{
280 return d->fontEngine == other.d->fontEngine;
281}
282
283/*!
284 Returns the hash value for \a font. If specified, \a seed is used
285 to initialize the hash.
286
287 \relates QRawFont
288 \since 5.8
289*/
290size_t qHash(const QRawFont &font, size_t seed) noexcept
291{
292 return qHash(t: QRawFontPrivate::get(font)->fontEngine, seed);
293}
294
295
296/*!
297 \fn bool QRawFont::operator!=(const QRawFont &other) const
298
299 Returns \c true if this QRawFont is not equal to \a other. Otherwise, returns \c false.
300*/
301
302/*!
303 Returns the ascent of this QRawFont in pixel units.
304
305 The ascent of a font is the distance from the baseline to the
306 highest position characters extend to. In practice, some font
307 designers break this rule, e.g. when they put more than one accent
308 on top of a character, or to accommodate an unusual character in
309 an exotic language, so it is possible (though rare) that this
310 value will be too small.
311
312 \sa QFontMetricsF::ascent()
313*/
314qreal QRawFont::ascent() const
315{
316 return d->isValid() ? d->fontEngine->ascent().toReal() : 0.0;
317}
318
319/*!
320 Returns the cap height of this QRawFont in pixel units.
321
322 \since 5.8
323
324 The cap height of a font is the height of a capital letter above
325 the baseline. It specifically is the height of capital letters
326 that are flat - such as H or I - as opposed to round letters such
327 as O, or pointed letters like A, both of which may display overshoot.
328
329 \sa QFontMetricsF::capHeight()
330*/
331qreal QRawFont::capHeight() const
332{
333 return d->isValid() ? d->fontEngine->capHeight().toReal() : 0.0;
334}
335
336/*!
337 Returns the descent of this QRawFont in pixel units.
338
339 The descent is the distance from the base line to the lowest point
340 characters extend to. In practice, some font designers break this rule,
341 e.g. to accommodate an unusual character in an exotic language, so
342 it is possible (though rare) that this value will be too small.
343
344 \sa QFontMetricsF::descent()
345*/
346qreal QRawFont::descent() const
347{
348 return d->isValid() ? d->fontEngine->descent().toReal() : 0.0;
349}
350
351/*!
352 Returns the xHeight of this QRawFont in pixel units.
353
354 This is often but not always the same as the height of the character 'x'.
355
356 \sa QFontMetricsF::xHeight()
357*/
358qreal QRawFont::xHeight() const
359{
360 return d->isValid() ? d->fontEngine->xHeight().toReal() : 0.0;
361}
362
363/*!
364 Returns the leading of this QRawFont in pixel units.
365
366 This is the natural inter-line spacing.
367
368 \sa QFontMetricsF::leading()
369*/
370qreal QRawFont::leading() const
371{
372 return d->isValid() ? d->fontEngine->leading().toReal() : 0.0;
373}
374
375/*!
376 Returns the average character width of this QRawFont in pixel units.
377
378 \sa QFontMetricsF::averageCharWidth()
379*/
380qreal QRawFont::averageCharWidth() const
381{
382 return d->isValid() ? d->fontEngine->averageCharWidth().toReal() : 0.0;
383}
384
385/*!
386 Returns the width of the widest character in the font.
387
388 \sa QFontMetricsF::maxWidth()
389*/
390qreal QRawFont::maxCharWidth() const
391{
392 return d->isValid() ? d->fontEngine->maxCharWidth() : 0.0;
393}
394
395/*!
396 Returns the pixel size set for this QRawFont. The pixel size affects how glyphs are
397 rasterized, the size of glyphs returned by pathForGlyph(), and is used to convert
398 internal metrics from design units to logical pixel units.
399
400 \sa setPixelSize()
401*/
402qreal QRawFont::pixelSize() const
403{
404 return d->isValid() ? d->fontEngine->fontDef.pixelSize : 0.0;
405}
406
407/*!
408 Returns the number of design units define the width and height of the em square
409 for this QRawFont. This value is used together with the pixel size when converting design metrics
410 to pixel units, as the internal metrics are specified in design units and the pixel size gives
411 the size of 1 em in pixels.
412
413 \sa pixelSize(), setPixelSize()
414*/
415qreal QRawFont::unitsPerEm() const
416{
417 return d->isValid() ? d->fontEngine->emSquareSize().toReal() : 0.0;
418}
419
420/*!
421 Returns the thickness for drawing lines (underline, overline, etc.)
422 along with text drawn in this font.
423 */
424qreal QRawFont::lineThickness() const
425{
426 return d->isValid() ? d->fontEngine->lineThickness().toReal() : 0.0;
427}
428
429/*!
430 Returns the position from baseline for drawing underlines below the text
431 rendered with this font.
432 */
433qreal QRawFont::underlinePosition() const
434{
435 return d->isValid() ? d->fontEngine->underlinePosition().toReal() : 0.0;
436}
437
438/*!
439 Returns the family name of this QRawFont.
440*/
441QString QRawFont::familyName() const
442{
443 return d->isValid() ? d->fontEngine->fontDef.families.first() : QString();
444}
445
446/*!
447 Returns the style name of this QRawFont.
448
449 \sa QFont::styleName()
450*/
451QString QRawFont::styleName() const
452{
453 return d->isValid() ? d->fontEngine->fontDef.styleName : QString();
454}
455
456/*!
457 Returns the style of this QRawFont.
458
459 \sa QFont::style()
460*/
461QFont::Style QRawFont::style() const
462{
463 return d->isValid() ? QFont::Style(d->fontEngine->fontDef.style) : QFont::StyleNormal;
464}
465
466/*!
467 Returns the weight of this QRawFont.
468
469 \sa QFont::weight()
470*/
471int QRawFont::weight() const
472{
473 return d->isValid() ? int(d->fontEngine->fontDef.weight) : -1;
474}
475
476/*!
477 Converts the string of unicode points given by \a text to glyph indexes
478 using the CMAP table in the underlying font, and returns a list containing
479 the result.
480
481 Note that, in cases where there are other tables in the font that affect the
482 shaping of the text, the returned glyph indexes will not correctly represent
483 the rendering of the text. To get the correctly shaped text, you can use
484 QTextLayout to lay out and shape the text, then call QTextLayout::glyphs()
485 to get the set of glyph index list and QRawFont pairs.
486
487 \sa advancesForGlyphIndexes(), glyphIndexesForChars(), QGlyphRun, QTextLayout::glyphRuns(), QTextFragment::glyphRuns()
488*/
489QList<quint32> QRawFont::glyphIndexesForString(const QString &text) const
490{
491 QList<quint32> glyphIndexes;
492 if (!d->isValid() || text.isEmpty())
493 return glyphIndexes;
494
495 int numGlyphs = text.size();
496 glyphIndexes.resize(size: numGlyphs);
497
498 QGlyphLayout glyphs;
499 glyphs.numGlyphs = numGlyphs;
500 glyphs.glyphs = glyphIndexes.data();
501 if (!d->fontEngine->stringToCMap(str: text.data(), len: text.size(), glyphs: &glyphs, nglyphs: &numGlyphs, flags: QFontEngine::GlyphIndicesOnly))
502 Q_UNREACHABLE();
503
504 glyphIndexes.resize(size: numGlyphs);
505 return glyphIndexes;
506}
507
508/*!
509 Converts a string of unicode points to glyph indexes using the CMAP table in the
510 underlying font. The function works like glyphIndexesForString() except it take
511 an array (\a chars), the results will be returned though \a glyphIndexes array
512 and number of glyphs will be set in \a numGlyphs. The size of \a glyphIndexes array
513 must be at least \a numChars, if that's still not enough, this function will return
514 false, then you can resize \a glyphIndexes from the size returned in \a numGlyphs.
515
516 \sa glyphIndexesForString(), advancesForGlyphIndexes(), QGlyphRun, QTextLayout::glyphRuns(), QTextFragment::glyphRuns()
517*/
518bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
519{
520 Q_ASSERT(numGlyphs);
521 if (!d->isValid() || numChars <= 0) {
522 *numGlyphs = 0;
523 return false;
524 }
525
526 if (*numGlyphs <= 0 || !glyphIndexes) {
527 *numGlyphs = numChars;
528 return false;
529 }
530
531 QGlyphLayout glyphs;
532 glyphs.numGlyphs = *numGlyphs;
533 glyphs.glyphs = glyphIndexes;
534 return d->fontEngine->stringToCMap(str: chars, len: numChars, glyphs: &glyphs, nglyphs: numGlyphs, flags: QFontEngine::GlyphIndicesOnly);
535}
536
537/*!
538 \fn QList<QPointF> QRawFont::advancesForGlyphIndexes(const QList<quint32> &glyphIndexes, LayoutFlags layoutFlags) const
539 \since 5.1
540
541 Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
542 give the distance from the position of a given glyph to where the next glyph should be drawn
543 to make it appear as if the two glyphs are unspaced. How the advances are calculated is
544 controlled by \a layoutFlags.
545
546 \note When \c KernedAdvances is requested, this function will apply kerning rules from the
547 TrueType table \c{KERN}, if this is available in the font. In many modern fonts, kerning is
548 handled through OpenType rules or AAT rules, which requires a full shaping step to be applied.
549 To get the results of fully shaping the text, use \l{QTextLayout}.
550
551 \sa QTextLine::horizontalAdvance(), QFontMetricsF::horizontalAdvance(), QTextLayout::glyphRuns()
552*/
553
554/*!
555 \fn QList<QPointF> QRawFont::advancesForGlyphIndexes(const QList<quint32> &glyphIndexes) const
556
557 \overload
558
559 Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
560 give the distance from the position of a given glyph to where the next glyph should be drawn
561 to make it appear as if the two glyphs are unspaced. The advance of each glyph is calculated
562 separately.
563
564 \sa QTextLine::horizontalAdvance(), QFontMetricsF::horizontalAdvance()
565*/
566
567/*!
568 \since 5.1
569
570 Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
571 give the distance from the position of a given glyph to where the next glyph should be drawn
572 to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the
573 array \a glyphIndexes while the results are returned through \a advances, both of them must
574 have \a numGlyphs elements. How the advances are calculated is controlled by \a layoutFlags.
575
576 \note When \c KernedAdvances is requested, this function will apply kerning rules from the
577 TrueType table \c{KERN}, if this is available in the font. In many modern fonts, kerning is
578 handled through OpenType rules or AAT rules, which requires a full shaping step to be applied.
579 To get the results of fully shaping the text, use \l{QTextLayout}.
580
581 \sa QTextLine::horizontalAdvance(), QFontMetricsF::horizontalAdvance(), QTextLayout::glyphRuns()
582*/
583bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs, LayoutFlags layoutFlags) const
584{
585 if (!d->isValid() || numGlyphs <= 0)
586 return false;
587 Q_ASSERT(glyphIndexes && advances);
588
589 QVarLengthArray<QFixed> tmpAdvances(numGlyphs);
590
591 QGlyphLayout glyphs;
592 glyphs.glyphs = const_cast<glyph_t *>(glyphIndexes);
593 glyphs.numGlyphs = numGlyphs;
594 glyphs.advances = tmpAdvances.data();
595
596 bool design = layoutFlags & UseDesignMetrics;
597
598 d->fontEngine->recalcAdvances(&glyphs, design ? QFontEngine::DesignMetrics : QFontEngine::ShaperFlag(0));
599 if (layoutFlags & KernedAdvances)
600 d->fontEngine->doKerning(&glyphs, design ? QFontEngine::DesignMetrics : QFontEngine::ShaperFlag(0));
601
602 for (int i=0; i<numGlyphs; ++i)
603 advances[i] = QPointF(tmpAdvances[i].toReal(), 0.0);
604
605 return true;
606}
607
608/*!
609 \overload
610
611 Returns the QRawFont's advances for each of the \a glyphIndexes in pixel units. The advances
612 give the distance from the position of a given glyph to where the next glyph should be drawn
613 to make it appear as if the two glyphs are unspaced. The glyph indexes are given with the
614 array \a glyphIndexes while the results are returned through \a advances, both of them must
615 have \a numGlyphs elements. The advance of each glyph is calculated separately
616
617 \sa QTextLine::horizontalAdvance(), QFontMetricsF::horizontalAdvance()
618*/
619bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
620{
621 return QRawFont::advancesForGlyphIndexes(glyphIndexes, advances, numGlyphs, layoutFlags: SeparateAdvances);
622}
623
624/*!
625 Returns the hinting preference used to construct this QRawFont.
626
627 \sa QFont::hintingPreference()
628*/
629QFont::HintingPreference QRawFont::hintingPreference() const
630{
631 return d->isValid() ? d->hintingPreference : QFont::PreferDefaultHinting;
632}
633
634/*!
635 Retrieves the sfnt table named \a tagName from the underlying physical font, or an empty
636 byte array if no such table was found. The returned font table's byte order is Big Endian, like
637 the sfnt format specifies. The \a tagName must be four characters long and should be formatted
638 in the default endianness of the current platform.
639*/
640QByteArray QRawFont::fontTable(const char *tagName) const
641{
642 if (!d->isValid())
643 return QByteArray();
644
645 return d->fontEngine->getSfntTable(MAKE_TAG(tagName[0], tagName[1], tagName[2], tagName[3]));
646}
647
648/*!
649 Returns a list of writing systems supported by the font according to designer supplied
650 information in the font file. Please note that this does not guarantee support for a
651 specific unicode point in the font. You can use the supportsCharacter() to check support
652 for a single, specific character.
653
654 \note The list is determined based on the unicode ranges and codepage ranges set in the font's
655 OS/2 table and requires such a table to be present in the underlying font file.
656
657 \sa supportsCharacter()
658*/
659QList<QFontDatabase::WritingSystem> QRawFont::supportedWritingSystems() const
660{
661 QList<QFontDatabase::WritingSystem> writingSystems;
662 if (d->isValid()) {
663 QByteArray os2Table = fontTable(tagName: "OS/2");
664 if (os2Table.size() > 86) {
665 const uchar * const data = reinterpret_cast<const uchar *>(os2Table.constData());
666 const uchar * const bigEndianUnicodeRanges = data + 42;
667 const uchar * const bigEndianCodepageRanges = data + 78;
668
669 quint32 unicodeRanges[4];
670 quint32 codepageRanges[2];
671
672 for (size_t i = 0; i < sizeof unicodeRanges / sizeof *unicodeRanges; ++i)
673 unicodeRanges[i] = qFromBigEndian<quint32>(src: bigEndianUnicodeRanges + i * sizeof(quint32));
674
675 for (size_t i = 0; i < sizeof codepageRanges / sizeof *codepageRanges; ++i)
676 codepageRanges[i] = qFromBigEndian<quint32>(src: bigEndianCodepageRanges + i * sizeof(quint32));
677
678 QSupportedWritingSystems ws = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange: unicodeRanges, codePageRange: codepageRanges);
679 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
680 if (ws.supported(QFontDatabase::WritingSystem(i)))
681 writingSystems.append(t: QFontDatabase::WritingSystem(i));
682 }
683 }
684 }
685
686 return writingSystems;
687}
688
689/*!
690 Returns \c true if the font has a glyph that corresponds to the given \a character.
691
692 \sa supportedWritingSystems()
693*/
694bool QRawFont::supportsCharacter(QChar character) const
695{
696 return supportsCharacter(ucs4: character.unicode());
697}
698
699/*!
700 \overload
701
702 Returns \c true if the font has a glyph that corresponds to the UCS-4 encoded character \a ucs4.
703
704 \sa supportedWritingSystems()
705*/
706bool QRawFont::supportsCharacter(uint ucs4) const
707{
708 return d->isValid() && d->fontEngine->canRender(ucs4);
709}
710
711// qfontdatabase.cpp
712extern int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem);
713
714/*!
715 Fetches the physical representation based on a \a font query. The physical font returned is
716 the font that will be preferred by Qt in order to display text in the selected \a writingSystem.
717
718 \warning This function is potentially expensive and should not be called in performance
719 sensitive code.
720*/
721QRawFont QRawFont::fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem)
722{
723 QRawFont rawFont;
724 const QFontPrivate *font_d = QFontPrivate::get(font);
725 int script = qt_script_for_writing_system(writingSystem);
726 QFontEngine *fe = font_d->engineForScript(script);
727
728 if (fe != nullptr && fe->type() == QFontEngine::Multi) {
729 QFontEngineMulti *multiEngine = static_cast<QFontEngineMulti *>(fe);
730 fe = multiEngine->engine(at: 0);
731
732 if (script > QChar::Script_Latin) {
733 // keep in sync with QFontEngineMulti::loadEngine()
734 QFontDef request(multiEngine->fontDef);
735 request.styleStrategy |= QFont::NoFontMerging;
736
737 if (QFontEngine *engine = QFontDatabasePrivate::findFont(request, script, preferScriptOverFamily: true)) {
738 if (request.weight > QFont::Normal)
739 engine->fontDef.weight = request.weight;
740 if (request.style > QFont::StyleNormal)
741 engine->fontDef.style = request.style;
742 fe = engine;
743 }
744 }
745 Q_ASSERT(fe);
746 }
747
748 if (fe != nullptr) {
749 rawFont.d.data()->setFontEngine(fe);
750 rawFont.d.data()->hintingPreference = font.hintingPreference();
751 }
752 return rawFont;
753}
754
755/*!
756 Sets the pixel size with which this font should be rendered to \a pixelSize.
757*/
758void QRawFont::setPixelSize(qreal pixelSize)
759{
760 if (!d->isValid() || qFuzzyCompare(p1: d->fontEngine->fontDef.pixelSize, p2: pixelSize))
761 return;
762
763 d.detach();
764 d->setFontEngine(d->fontEngine->cloneWithSize(pixelSize));
765}
766
767/*!
768 \internal
769*/
770void QRawFontPrivate::loadFromData(const QByteArray &fontData, qreal pixelSize,
771 QFont::HintingPreference hintingPreference)
772{
773 Q_ASSERT(fontEngine == nullptr);
774
775 QPlatformFontDatabase *pfdb = QGuiApplicationPrivate::platformIntegration()->fontDatabase();
776 setFontEngine(pfdb->fontEngine(fontData, pixelSize, hintingPreference));
777}
778
779/*!
780 Returns the smallest rectangle containing the glyph with the given \a glyphIndex.
781
782 \since 5.0
783*/
784QRectF QRawFont::boundingRect(quint32 glyphIndex) const
785{
786 if (!d->isValid())
787 return QRectF();
788
789 glyph_metrics_t gm = d->fontEngine->boundingBox(glyph: glyphIndex);
790 return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
791}
792
793#endif // QT_NO_RAWFONT
794
795QT_END_NAMESPACE
796

source code of qtbase/src/gui/text/qrawfont.cpp