Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qfreetypefontdatabase.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
5
6#include <QtGui/private/qguiapplication_p.h>
7#include <qpa/qplatformscreen.h>
8
9#include <QtCore/QFile>
10#include <QtCore/QLibraryInfo>
11#include <QtCore/QDir>
12#include <QtCore/QtEndian>
13
14#undef QT_NO_FREETYPE
15#include "qfontengine_ft_p.h"
16
17#include <ft2build.h>
18#include FT_TRUETYPE_TABLES_H
19#include FT_ERRORS_H
20
22
23using namespace Qt::StringLiterals;
24
26{
27 QString fontpath = fontDir();
28 QDir dir(fontpath);
29
30 if (!dir.exists()) {
31 qWarning("QFontDatabase: Cannot find font directory %s.\n"
32 "Note that Qt no longer ships fonts. Deploy some (from https://dejavu-fonts.github.io/ for example) or switch to fontconfig.",
33 qPrintable(fontpath));
34 return;
35 }
36
37 static const QString nameFilters[] = {
38 u"*.ttf"_s,
39 u"*.pfa"_s,
40 u"*.pfb"_s,
41 u"*.otf"_s,
42 };
43
44 const auto fis = dir.entryInfoList(QStringList::fromReadOnlyData(nameFilters), QDir::Files);
45 for (const QFileInfo &fi : fis) {
48 }
49}
50
52{
53 FontFile *fontfile = static_cast<FontFile *>(usrPtr);
55 faceId.filename = QFile::encodeName(fontfile->fileName);
56 faceId.index = fontfile->indexValue;
57
58 return QFontEngineFT::create(fontDef, faceId, fontfile->data);
59}
60
62 QFont::HintingPreference hintingPreference)
63{
64 return QFontEngineFT::create(fontData, pixelSize, hintingPreference);
65}
66
68{
69 return QFreeTypeFontDatabase::addTTFile(fontData, fileName.toLocal8Bit(), applicationFont);
70}
71
73{
74 FontFile *file = static_cast<FontFile *>(handle);
75 delete file;
76}
77
78extern FT_Library qt_getFreetype();
79
81{
82 FT_Library library = qt_getFreetype();
83
84 int index = 0;
85 int numFaces = 0;
86 QStringList families;
87 do {
88 FT_Face face;
89 FT_Error error;
90 if (!fontData.isEmpty()) {
91 error = FT_New_Memory_Face(library, (const FT_Byte *)fontData.constData(), fontData.size(), index, &face);
92 } else {
93 error = FT_New_Face(library, file.constData(), index, &face);
94 }
95 if (error != FT_Err_Ok) {
96 qDebug() << "FT_New_Face failed with index" << index << ':' << Qt::hex << error;
97 break;
98 }
99 numFaces = face->num_faces;
100
102
104 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
105 style = QFont::StyleItalic;
106
107 if (face->style_flags & FT_STYLE_FLAG_BOLD)
109
110 bool fixedPitch = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH);
111 QSupportedWritingSystems writingSystems;
112 // detect symbol fonts
113 for (int i = 0; i < face->num_charmaps; ++i) {
114 FT_CharMap cm = face->charmaps[i];
115 if (cm->encoding == FT_ENCODING_ADOBE_CUSTOM
116 || cm->encoding == FT_ENCODING_MS_SYMBOL) {
117 writingSystems.setSupported(QFontDatabase::Symbol);
118 break;
119 }
120 }
121
123 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
124 if (os2) {
125 quint32 unicodeRange[4] = {
126 quint32(os2->ulUnicodeRange1),
127 quint32(os2->ulUnicodeRange2),
128 quint32(os2->ulUnicodeRange3),
129 quint32(os2->ulUnicodeRange4)
130 };
131 quint32 codePageRange[2] = {
132 quint32(os2->ulCodePageRange1),
133 quint32(os2->ulCodePageRange2)
134 };
135
136 writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange);
137
138 if (os2->usWeightClass) {
139 weight = static_cast<QFont::Weight>(os2->usWeightClass);
140 } else if (os2->panose[2]) {
141 int w = os2->panose[2];
142 if (w <= 1)
144 else if (w <= 2)
146 else if (w <= 3)
148 else if (w <= 5)
150 else if (w <= 6)
152 else if (w <= 7)
154 else if (w <= 8)
156 else if (w <= 9)
158 else if (w <= 10)
160 }
161
162 switch (os2->usWidthClass) {
163 case 1:
164 stretch = QFont::UltraCondensed;
165 break;
166 case 2:
167 stretch = QFont::ExtraCondensed;
168 break;
169 case 3:
170 stretch = QFont::Condensed;
171 break;
172 case 4:
173 stretch = QFont::SemiCondensed;
174 break;
175 case 5:
176 stretch = QFont::Unstretched;
177 break;
178 case 6:
179 stretch = QFont::SemiExpanded;
180 break;
181 case 7:
182 stretch = QFont::Expanded;
183 break;
184 case 8:
185 stretch = QFont::ExtraExpanded;
186 break;
187 case 9:
188 stretch = QFont::UltraExpanded;
189 break;
190 }
191 }
192
193 QString family = QString::fromLatin1(face->family_name);
194 FontFile *fontFile = new FontFile{
196 index,
198 };
199
200 QString styleName = QString::fromLatin1(face->style_name);
201
202 if (applicationFont != nullptr) {
204 properties.familyName = family;
205 properties.styleName = styleName;
206 properties.weight = weight;
207 properties.stretch = stretch;
208 properties.style = style;
209
210 applicationFont->properties.append(properties);
211 }
212
213 registerFont(family, styleName, QString(), weight, style, stretch, true, true, 0, fixedPitch, writingSystems, fontFile);
214 families.append(family);
215
216 FT_Done_Face(face);
217 ++index;
218 } while (index < numFaces);
219 return families;
220}
221
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qdir.h:19
@ Files
Definition qdir.h:22
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
QString absoluteFilePath() const
Returns an absolute path including the file name.
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
Definition qfile.h:158
static QString decodeName(const QByteArray &localFileName)
This does the reverse of QFile::encodeName() using localFileName.
Definition qfile.h:162
static QFontEngineFT * create(const QFontDef &fontDef, FaceId faceId, const QByteArray &fontData=QByteArray())
HintingPreference
Definition qfont.h:52
Stretch
Predefined stretch values that follow the CSS naming convention.
Definition qfont.h:80
@ ExtraCondensed
Definition qfont.h:83
@ Expanded
Definition qfont.h:88
@ Unstretched
Definition qfont.h:86
@ SemiCondensed
Definition qfont.h:85
@ SemiExpanded
Definition qfont.h:87
@ UltraCondensed
Definition qfont.h:82
@ ExtraExpanded
Definition qfont.h:89
@ Condensed
Definition qfont.h:84
@ UltraExpanded
Definition qfont.h:90
Weight
Qt uses a weighting scale from 1 to 1000 compatible with OpenType.
Definition qfont.h:60
@ DemiBold
Definition qfont.h:66
@ Thin
Definition qfont.h:61
@ ExtraBold
Definition qfont.h:68
@ Black
Definition qfont.h:69
@ Bold
Definition qfont.h:67
@ ExtraLight
Definition qfont.h:62
@ Normal
Definition qfont.h:64
@ Light
Definition qfont.h:63
@ Medium
Definition qfont.h:65
Style
This enum describes the different styles of glyphs that are used to display text.
Definition qfont.h:73
@ StyleItalic
Definition qfont.h:75
@ StyleNormal
Definition qfont.h:74
QFontEngine * fontEngine(const QFontDef &fontDef, void *handle) override
Returns the font engine that can be used to render the font described by the font definition,...
void populateFontDatabase() override
This function is called once at startup by Qt's internal font database.
static QStringList addTTFile(const QByteArray &fontData, const QByteArray &file, QFontDatabasePrivate::ApplicationFont *applicationFont=nullptr)
void releaseHandle(void *handle) override
Releases the specified font handle.
QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *applicationFont=nullptr) override
Adds an application font described by the font contained supplied fontData or using the font containe...
static QList< QString > fromReadOnlyData(const QString(&t)[N]) noexcept
Definition qlist.h:722
virtual QString fontDir() const
Returns the directory containing the fonts used by the database.
static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
Helper function that determines the writing systems support by a given unicodeRange and codePageRange...
static void registerFont(const QString &familyname, const QString &stylename, const QString &foundryname, QFont::Weight weight, QFont::Style style, QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize, bool fixedPitch, const QSupportedWritingSystems &writingSystems, void *handle)
Registers a font with the given set of attributes describing the font's foundry, family name,...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase.
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets or clears support for the specified writingSystem based on the value given by support.
Combined button and popup list for selecting options.
QTextStream & hex(QTextStream &stream)
Calls QTextStream::setIntegerBase(16) on stream and returns stream.
static const QCssKnownValue properties[NumProperties - 1]
DBusConnection const char DBusError * error
FT_Library qt_getFreetype()
FT_Library qt_getFreetype()
#define qDebug
[1]
Definition qlogging.h:160
#define qWarning
Definition qlogging.h:162
GLuint64 GLenum void * handle
GLfloat GLfloat GLfloat w
[0]
GLuint index
[2]
GLenum face
GLuint GLuint GLfloat weight
GLenum GLsizeiptr const void * fontData
#define qPrintable(string)
Definition qstring.h:1391
unsigned int quint32
Definition qtypes.h:45
double qreal
Definition qtypes.h:92
QFile file
[0]
QFileInfo fi("c:/temp/foo")
[newstuff]
QString dir
[11]
const QByteArray data