Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qrawfont.cpp
Go to the documentation of this file.
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"
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
21
113 : d(new QRawFontPrivate)
114{
115}
116
125 qreal pixelSize,
126 QFont::HintingPreference hintingPreference)
127 : d(new QRawFontPrivate)
128{
130}
131
146
151{
152 d = other.d;
153}
154
161
166{
167 d = other.d;
168 return *this;
169}
170
183{
184 return d->isValid();
185}
186
204
215 qreal pixelSize,
216 QFont::HintingPreference hintingPreference)
217{
218 d.detach();
219 d->cleanUp();
222}
223
242 const QTransform &transform) const
243{
244 if (!d->isValid())
245 return QImage();
246
248 return d->fontEngine->bitmapForGlyph(glyphIndex, QFixedPoint(), transform);
249
250 if (antialiasingType == SubPixelAntialiasing)
251 return d->fontEngine->alphaRGBMapForGlyph(glyphIndex, QFixedPoint(), transform);
252
253 return d->fontEngine->alphaMapForGlyph(glyphIndex, QFixedPoint(), transform);
254}
255
265{
266 if (!d->isValid())
267 return QPainterPath();
268
271 d->fontEngine->addGlyphsToPath(&glyphIndex, &position, 1, &path, { });
272 return path;
273}
274
279{
280 return d->fontEngine == other.d->fontEngine;
281}
282
290size_t qHash(const QRawFont &font, size_t seed) noexcept
291{
292 return qHash(QRawFontPrivate::get(font)->fontEngine, seed);
293}
294
295
315{
316 return d->isValid() ? d->fontEngine->ascent().toReal() : 0.0;
317}
318
332{
333 return d->isValid() ? d->fontEngine->capHeight().toReal() : 0.0;
334}
335
347{
348 return d->isValid() ? d->fontEngine->descent().toReal() : 0.0;
349}
350
359{
360 return d->isValid() ? d->fontEngine->xHeight().toReal() : 0.0;
361}
362
371{
372 return d->isValid() ? d->fontEngine->leading().toReal() : 0.0;
373}
374
381{
382 return d->isValid() ? d->fontEngine->averageCharWidth().toReal() : 0.0;
383}
384
391{
392 return d->isValid() ? d->fontEngine->maxCharWidth() : 0.0;
393}
394
403{
404 return d->isValid() ? d->fontEngine->fontDef.pixelSize : 0.0;
405}
406
416{
417 return d->isValid() ? d->fontEngine->emSquareSize().toReal() : 0.0;
418}
419
425{
426 return d->isValid() ? d->fontEngine->lineThickness().toReal() : 0.0;
427}
428
434{
435 return d->isValid() ? d->fontEngine->underlinePosition().toReal() : 0.0;
436}
437
442{
443 return d->isValid() ? d->fontEngine->fontDef.families.constFirst() : QString();
444}
445
452{
453 return d->isValid() ? d->fontEngine->fontDef.styleName : QString();
454}
455
465
472{
473 return d->isValid() ? int(d->fontEngine->fontDef.weight) : -1;
474}
475
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(numGlyphs);
497
498 QGlyphLayout glyphs;
499 glyphs.numGlyphs = numGlyphs;
500 glyphs.glyphs = glyphIndexes.data();
502 Q_UNREACHABLE();
503
504 glyphIndexes.resize(numGlyphs);
505 return glyphIndexes;
506}
507
518bool QRawFont::glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
519{
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(chars, numChars, &glyphs, numGlyphs, QFontEngine::GlyphIndicesOnly) >= 0;
535}
536
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
599 if (layoutFlags & KernedAdvances)
601
602 for (int i=0; i<numGlyphs; ++i)
603 advances[i] = QPointF(tmpAdvances[i].toReal(), 0.0);
604
605 return true;
606}
607
619bool QRawFont::advancesForGlyphIndexes(const quint32 *glyphIndexes, QPointF *advances, int numGlyphs) const
620{
621 return QRawFont::advancesForGlyphIndexes(glyphIndexes, advances, numGlyphs, SeparateAdvances);
622}
623
633
650{
651 if (auto maybeTag = QFont::Tag::fromString(tag))
652 return fontTable(*maybeTag);
653 return QByteArray();
654}
655
657{
658 if (!d->isValid())
659 return QByteArray();
660
661 return d->fontEngine->getSfntTable(tag.value());
662}
663
675QList<QFontDatabase::WritingSystem> QRawFont::supportedWritingSystems() const
676{
677 QList<QFontDatabase::WritingSystem> writingSystems;
678 if (d->isValid()) {
679 QByteArray os2Table = fontTable("OS/2");
680 if (os2Table.size() > 86) {
681 const uchar * const data = reinterpret_cast<const uchar *>(os2Table.constData());
682 const uchar * const bigEndianUnicodeRanges = data + 42;
683 const uchar * const bigEndianCodepageRanges = data + 78;
684
685 quint32 unicodeRanges[4];
686 quint32 codepageRanges[2];
687
688 for (size_t i = 0; i < sizeof unicodeRanges / sizeof *unicodeRanges; ++i)
689 unicodeRanges[i] = qFromBigEndian<quint32>(bigEndianUnicodeRanges + i * sizeof(quint32));
690
691 for (size_t i = 0; i < sizeof codepageRanges / sizeof *codepageRanges; ++i)
692 codepageRanges[i] = qFromBigEndian<quint32>(bigEndianCodepageRanges + i * sizeof(quint32));
693
695 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
696 if (ws.supported(QFontDatabase::WritingSystem(i)))
697 writingSystems.append(QFontDatabase::WritingSystem(i));
698 }
699 }
700 }
701
702 return writingSystems;
703}
704
711{
712 return supportsCharacter(character.unicode());
713}
714
723{
724 return d->isValid() && d->fontEngine->canRender(ucs4);
725}
726
727// qfontdatabase.cpp
729
738{
739 QRawFont rawFont;
740 const QFontPrivate *font_d = QFontPrivate::get(font);
741 int script = qt_script_for_writing_system(writingSystem);
742 QFontEngine *fe = font_d->engineForScript(script);
743
744 if (fe != nullptr && fe->type() == QFontEngine::Multi) {
745 QFontEngineMulti *multiEngine = static_cast<QFontEngineMulti *>(fe);
746 fe = multiEngine->engine(0);
747
748 if (script > QChar::Script_Latin) {
749 // keep in sync with QFontEngineMulti::loadEngine()
750 QFontDef request(multiEngine->fontDef);
751 request.styleStrategy |= QFont::NoFontMerging;
752
754 if (request.weight > QFont::Normal)
755 engine->fontDef.weight = request.weight;
756 if (request.style > QFont::StyleNormal)
757 engine->fontDef.style = request.style;
758 fe = engine;
759 }
760 }
761 Q_ASSERT(fe);
762 }
763
764 if (fe != nullptr) {
765 rawFont.d.data()->setFontEngine(fe);
767 }
768 return rawFont;
769}
770
775{
777 return;
778
779 d.detach();
781}
782
787 QFont::HintingPreference hintingPreference)
788{
789 Q_ASSERT(fontEngine == nullptr);
790
792 setFontEngine(pfdb->fontEngine(fontData, pixelSize, hintingPreference));
793}
794
801{
802 if (!d->isValid())
803 return QRectF();
804
805 glyph_metrics_t gm = d->fontEngine->boundingBox(glyphIndex);
806 return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
807}
808
809#endif // QT_NO_RAWFONT
810
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
T * data() const noexcept
Returns a pointer to the shared data object.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
static QFontEngine * findFont(const QFontDef &request, int script, bool preferScriptOverFamily=false)
WritingSystem
\value Any \value Latin \value Greek \value Cyrillic \value Armenian \value Hebrew \value Arabic \val...
QFontEngine * engine(int at) const
virtual QFixed capHeight() const =0
virtual QFixed descent() const
virtual QImage bitmapForGlyph(glyph_t, const QFixedPoint &subPixelPosition, const QTransform &t, const QColor &color=QColor())
virtual int stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, ShaperFlags flags) const =0
virtual void addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nglyphs, QPainterPath *path, QTextItem::RenderFlags flags)
virtual qreal maxCharWidth() const =0
QFontDef fontDef
virtual QFixed ascent() const
virtual QFontEngine * cloneWithSize(qreal) const
bool canRender(uint ucs4) const
virtual QImage alphaMapForGlyph(glyph_t)
virtual QImage alphaRGBMapForGlyph(glyph_t, const QFixedPoint &subPixelPosition, const QTransform &t)
QByteArray getSfntTable(uint tag) const
virtual QFixed averageCharWidth() const
virtual void doKerning(QGlyphLayout *, ShaperFlags) const
virtual QFixed lineThickness() const
virtual QFixed underlinePosition() const
GlyphFormat glyphFormat
virtual glyph_metrics_t boundingBox(const QGlyphLayout &glyphs)
virtual void recalcAdvances(QGlyphLayout *, ShaperFlags) const
virtual QFixed leading() const
virtual QFixed xHeight() const
virtual QFixed emSquareSize() const
static QFontPrivate * get(const QFont &font)
Definition qfont_p.h:193
\reentrant
Definition qfont.h:22
HintingPreference hintingPreference() const
Definition qfont.cpp:974
HintingPreference
Definition qfont.h:55
@ PreferDefaultHinting
Definition qfont.h:56
@ NoFontMerging
Definition qfont.h:51
@ Normal
Definition qfont.h:67
Style
This enum describes the different styles of glyphs that are used to display text.
Definition qfont.h:76
@ StyleNormal
Definition qfont.h:77
static QPlatformIntegration * platformIntegration()
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
\inmodule QtGui
Definition qimage.h:37
\inmodule QtGui
The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they ...
static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
Helper function that determines the writing systems support by a given unicodeRange and codePageRange...
\inmodule QtCore\reentrant
Definition qpoint.h:217
static QRawFontPrivate * get(const QRawFont &font)
Definition qrawfont_p.h:104
QFont::HintingPreference hintingPreference
Definition qrawfont_p.h:107
bool isValid() const
Definition qrawfont_p.h:65
void loadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
Definition qrawfont.cpp:786
QFontEngine * fontEngine
Definition qrawfont_p.h:106
void setFontEngine(QFontEngine *engine)
Definition qrawfont_p.h:73
The QRawFont class provides access to a single physical instance of a font.
Definition qrawfont.h:24
bool supportsCharacter(uint ucs4) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrawfont.cpp:722
QString familyName() const
Returns the family name of this QRawFont.
Definition qrawfont.cpp:441
int weight() const
Returns the weight of this QRawFont.
Definition qrawfont.cpp:471
QRawFont & operator=(const QRawFont &other)
Assigns other to this QRawFont.
Definition qrawfont.cpp:165
QRectF boundingRect(quint32 glyphIndex) const
Returns the smallest rectangle containing the glyph with the given glyphIndex.
Definition qrawfont.cpp:800
QList< QPointF > advancesForGlyphIndexes(const QList< quint32 > &glyphIndexes) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qrawfont.h:136
qreal leading() const
Returns the leading of this QRawFont in pixel units.
Definition qrawfont.cpp:370
qreal ascent() const
Returns the ascent of this QRawFont in pixel units.
Definition qrawfont.cpp:314
qreal averageCharWidth() const
Returns the average character width of this QRawFont in pixel units.
Definition qrawfont.cpp:380
AntialiasingType
This enum represents the different ways a glyph can be rasterized in the function alphaMapForGlyph().
Definition qrawfont.h:26
@ SubPixelAntialiasing
Definition qrawfont.h:28
QRawFont()
Constructs an invalid QRawFont.
Definition qrawfont.cpp:112
qreal pixelSize() const
Returns the pixel size set for this QRawFont.
Definition qrawfont.cpp:402
QFont::Style style() const
Returns the style of this QRawFont.
Definition qrawfont.cpp:461
size_t qHash(const QRawFont &font, size_t seed) noexcept
Returns the hash value for font.
Definition qrawfont.cpp:290
qreal xHeight() const
Returns the xHeight of this QRawFont in pixel units.
Definition qrawfont.cpp:358
qreal underlinePosition() const
Returns the position from baseline for drawing underlines below the text rendered with this font.
Definition qrawfont.cpp:433
QByteArray fontTable(const char *tagName) const
Definition qrawfont.cpp:649
bool glyphIndexesForChars(const QChar *chars, int numChars, quint32 *glyphIndexes, int *numGlyphs) const
Converts a string of unicode points to glyph indexes using the CMAP table in the underlying font.
Definition qrawfont.cpp:518
QFont::HintingPreference hintingPreference() const
Returns the hinting preference used to construct this QRawFont.
Definition qrawfont.cpp:629
QImage alphaMapForGlyph(quint32 glyphIndex, AntialiasingType antialiasingType=SubPixelAntialiasing, const QTransform &transform=QTransform()) const
This function returns a rasterized image of the glyph at the given glyphIndex in the underlying font,...
Definition qrawfont.cpp:241
qreal unitsPerEm() const
Returns the number of design units define the width and height of the em square for this QRawFont.
Definition qrawfont.cpp:415
void loadFromFile(const QString &fileName, qreal pixelSize, QFont::HintingPreference hintingPreference)
Replaces the current QRawFont with the contents of the file referenced by fileName for the size (in p...
Definition qrawfont.cpp:196
qreal maxCharWidth() const
Returns the width of the widest character in the font.
Definition qrawfont.cpp:390
static QRawFont fromFont(const QFont &font, QFontDatabase::WritingSystem writingSystem=QFontDatabase::Any)
Fetches the physical representation based on a font query.
Definition qrawfont.cpp:737
qreal descent() const
Returns the descent of this QRawFont in pixel units.
Definition qrawfont.cpp:346
@ UseDesignMetrics
Definition qrawfont.h:34
@ KernedAdvances
Definition qrawfont.h:33
@ SeparateAdvances
Definition qrawfont.h:32
void setPixelSize(qreal pixelSize)
Sets the pixel size with which this font should be rendered to pixelSize.
Definition qrawfont.cpp:774
qreal capHeight() const
Returns the cap height of this QRawFont in pixel units.
Definition qrawfont.cpp:331
qreal lineThickness() const
Returns the thickness for drawing lines (underline, overline, etc.) along with text drawn in this fon...
Definition qrawfont.cpp:424
~QRawFont()
Destroys the QRawFont.
Definition qrawfont.cpp:158
QList< QFontDatabase::WritingSystem > supportedWritingSystems() const
Returns a list of writing systems supported by the font according to designer supplied information in...
Definition qrawfont.cpp:675
QPainterPath pathForGlyph(quint32 glyphIndex) const
This function returns the shape of the glyph at a given glyphIndex in the underlying font if the QRaw...
Definition qrawfont.cpp:264
void loadFromData(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
Replaces the current QRawFont with the font contained in the supplied fontData for the size (in pixel...
Definition qrawfont.cpp:214
QList< quint32 > glyphIndexesForString(const QString &text) const
Converts the string of unicode points given by text to glyph indexes using the CMAP table in the unde...
Definition qrawfont.cpp:489
bool operator==(const QRawFont &other) const
Returns true if this QRawFont is equal to other.
Definition qrawfont.cpp:278
QString styleName() const
Returns the style name of this QRawFont.
Definition qrawfont.cpp:451
bool isValid() const
Returns true if the QRawFont is valid and false otherwise.
Definition qrawfont.cpp:182
\inmodule QtCore\reentrant
Definition qrect.h:484
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1240
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase.
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
QString text
Combined button and popup list for selecting options.
AudioChannelLayoutTag tag
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
Q_GUI_EXPORT int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum const void GLbitfield GLsizei numGlyphs
GLenum GLsizeiptr const void * fontData
GLuint GLenum GLenum transform
GLsizei const GLchar *const * path
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem)
unsigned int glyph_t
unsigned int quint32
Definition qtypes.h:50
unsigned char uchar
Definition qtypes.h:32
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
QFile file
[0]
QSharedPointer< T > other(t)
[5]
QNetworkRequest request(url)
QJSEngine engine
[0]
uint style
Definition qfont_p.h:66
qreal pixelSize
Definition qfont_p.h:61
uint weight
Definition qfont_p.h:70
QStringList families
Definition qfont_p.h:54
QString styleName
Definition qfont_p.h:55
The QFont::Tag type provides access to advanced font features.
Definition qfont.h:215
glyph_t * glyphs
QFixed * advances