Qt 6.x
The Qt SDK
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
140 qreal pixelSize,
141 QFont::HintingPreference hintingPreference)
142 : d(new QRawFontPrivate)
143{
145}
146
151{
152 d = other.d;
153}
154
159{
160}
161
166{
167 d = other.d;
168 return *this;
169}
170
183{
184 return d->isValid();
185}
186
197 qreal pixelSize,
198 QFont::HintingPreference hintingPreference)
199{
203}
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.first() : QString();
444}
445
452{
453 return d->isValid() ? d->fontEngine->fontDef.styleName : QString();
454}
455
462{
464}
465
472{
473 return d->isValid() ? int(d->fontEngine->fontDef.weight) : -1;
474}
475
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);
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
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
630{
632}
633
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
660{
662 if (d->isValid()) {
663 QByteArray os2Table = fontTable("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>(bigEndianUnicodeRanges + i * sizeof(quint32));
674
675 for (size_t i = 0; i < sizeof codepageRanges / sizeof *codepageRanges; ++i)
676 codepageRanges[i] = qFromBigEndian<quint32>(bigEndianCodepageRanges + i * sizeof(quint32));
677
679 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
680 if (ws.supported(QFontDatabase::WritingSystem(i)))
681 writingSystems.append(QFontDatabase::WritingSystem(i));
682 }
683 }
684 }
685
686 return writingSystems;
687}
688
695{
696 return supportsCharacter(character.unicode());
697}
698
707{
708 return d->isValid() && d->fontEngine->canRender(ucs4);
709}
710
711// qfontdatabase.cpp
713
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(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
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);
751 }
752 return rawFont;
753}
754
759{
761 return;
762
763 d.detach();
765}
766
771 QFont::HintingPreference hintingPreference)
772{
773 Q_ASSERT(fontEngine == nullptr);
774
777}
778
785{
786 if (!d->isValid())
787 return QRectF();
788
789 glyph_metrics_t gm = d->fontEngine->boundingBox(glyphIndex);
790 return QRectF(gm.x.toReal(), gm.y.toReal(), gm.width.toReal(), gm.height.toReal());
791}
792
793#endif // QT_NO_RAWFONT
794
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
\inmodule QtCore
Definition qchar.h:48
constexpr char16_t unicode() const noexcept
Returns the numeric Unicode value of the QChar.
Definition qchar.h:458
@ Script_Latin
Definition qchar.h:149
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
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:881
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 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
Type type() 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 bool stringToCMap(const QChar *str, int len, QGlyphLayout *glyphs, int *nglyphs, ShaperFlags flags) const =0
virtual QFixed emSquareSize() const
static QFontPrivate * get(const QFont &font)
Definition qfont_p.h:173
QFontEngine * engineForScript(int script) const
Definition qfont.cpp:238
\reentrant
Definition qfont.h:20
HintingPreference hintingPreference() const
Definition qfont.cpp:959
HintingPreference
Definition qfont.h:52
@ PreferDefaultHinting
Definition qfont.h:53
@ NoFontMerging
Definition qfont.h:48
@ Normal
Definition qfont.h:64
Style
This enum describes the different styles of glyphs that are used to display text.
Definition qfont.h:73
@ StyleNormal
Definition qfont.h:74
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
Definition qlist.h:74
pointer data()
Definition qlist.h:414
void resize(qsizetype size)
Definition qlist.h:392
void append(parameter_type t)
Definition qlist.h:441
\inmodule QtGui
The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they ...
virtual QFontEngine * fontEngine(const QFontDef &fontDef, void *handle)
Returns the font engine that can be used to render the font described by the font definition,...
static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
Helper function that determines the writing systems support by a given unicodeRange and codePageRange...
virtual QPlatformFontDatabase * fontDatabase() const
Accessor for the platform integration's fontdatabase.
\inmodule QtCore\reentrant
Definition qpoint.h:214
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:770
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:706
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:784
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:135
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
Retrieves the sfnt table named tagName from the underlying physical font, or an empty byte array if n...
Definition qrawfont.cpp:640
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:721
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:758
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:659
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:483
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1095
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
T * data() noexcept
QString text
Combined button and popup list for selecting options.
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:287
Q_GUI_EXPORT int qt_script_for_writing_system(QFontDatabase::WritingSystem writingSystem)
#define MAKE_TAG(ch1, ch2, ch3, ch4)
GLenum const void GLbitfield GLsizei numGlyphs
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
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:45
unsigned char uchar
Definition qtypes.h:27
unsigned int uint
Definition qtypes.h:29
double qreal
Definition qtypes.h:92
QFile file
[0]
QSharedPointer< T > other(t)
[5]
QNetworkRequest request(url)
QJSEngine engine
[0]
constexpr qreal toReal() const
Definition qfixed_p.h:42
uint style
Definition qfont_p.h:65
qreal pixelSize
Definition qfont_p.h:60
uint weight
Definition qfont_p.h:69
QStringList families
Definition qfont_p.h:54
QString styleName
Definition qfont_p.h:55
glyph_t * glyphs
QFixed * advances