1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qfreetypefontdatabase_p.h"
41
42#include <QtGui/private/qguiapplication_p.h>
43#include <qpa/qplatformscreen.h>
44
45#include <QtCore/QFile>
46#include <QtCore/QLibraryInfo>
47#include <QtCore/QDir>
48#include <QtCore/QtEndian>
49
50#undef QT_NO_FREETYPE
51#include <QtFontDatabaseSupport/private/qfontengine_ft_p.h>
52
53#include <ft2build.h>
54#include FT_TRUETYPE_TABLES_H
55#include FT_ERRORS_H
56
57QT_BEGIN_NAMESPACE
58
59void QFreeTypeFontDatabase::populateFontDatabase()
60{
61 QString fontpath = fontDir();
62 QDir dir(fontpath);
63
64 if (!dir.exists()) {
65 qWarning(msg: "QFontDatabase: Cannot find font directory %s.\n"
66 "Note that Qt no longer ships fonts. Deploy some (from https://dejavu-fonts.github.io/ for example) or switch to fontconfig.",
67 qPrintable(fontpath));
68 return;
69 }
70
71 QStringList nameFilters;
72 nameFilters << QLatin1String("*.ttf")
73 << QLatin1String("*.ttc")
74 << QLatin1String("*.pfa")
75 << QLatin1String("*.pfb")
76 << QLatin1String("*.otf");
77
78 const auto fis = dir.entryInfoList(nameFilters, filters: QDir::Files);
79 for (const QFileInfo &fi : fis) {
80 const QByteArray file = QFile::encodeName(fileName: fi.absoluteFilePath());
81 QFreeTypeFontDatabase::addTTFile(fontData: QByteArray(), file);
82 }
83}
84
85QFontEngine *QFreeTypeFontDatabase::fontEngine(const QFontDef &fontDef, void *usrPtr)
86{
87 FontFile *fontfile = static_cast<FontFile *>(usrPtr);
88 QFontEngine::FaceId faceId;
89 faceId.filename = QFile::encodeName(fileName: fontfile->fileName);
90 faceId.index = fontfile->indexValue;
91
92 return QFontEngineFT::create(fontDef, faceId);
93}
94
95QFontEngine *QFreeTypeFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize,
96 QFont::HintingPreference hintingPreference)
97{
98 return QFontEngineFT::create(fontData, pixelSize, hintingPreference);
99}
100
101QStringList QFreeTypeFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName)
102{
103 return QFreeTypeFontDatabase::addTTFile(fontData, file: fileName.toLocal8Bit());
104}
105
106void QFreeTypeFontDatabase::releaseHandle(void *handle)
107{
108 FontFile *file = static_cast<FontFile *>(handle);
109 delete file;
110}
111
112extern FT_Library qt_getFreetype();
113
114QStringList QFreeTypeFontDatabase::addTTFile(const QByteArray &fontData, const QByteArray &file)
115{
116 FT_Library library = qt_getFreetype();
117
118 int index = 0;
119 int numFaces = 0;
120 QStringList families;
121 do {
122 FT_Face face;
123 FT_Error error;
124 if (!fontData.isEmpty()) {
125 error = FT_New_Memory_Face(library, file_base: (const FT_Byte *)fontData.constData(), file_size: fontData.size(), face_index: index, aface: &face);
126 } else {
127 error = FT_New_Face(library, filepathname: file.constData(), face_index: index, aface: &face);
128 }
129 if (error != FT_Err_Ok) {
130 qDebug() << "FT_New_Face failed with index" << index << ':' << Qt::hex << error;
131 break;
132 }
133 numFaces = face->num_faces;
134
135 QFont::Weight weight = QFont::Normal;
136
137 QFont::Style style = QFont::StyleNormal;
138 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
139 style = QFont::StyleItalic;
140
141 if (face->style_flags & FT_STYLE_FLAG_BOLD)
142 weight = QFont::Bold;
143
144 bool fixedPitch = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH);
145 QSupportedWritingSystems writingSystems;
146 // detect symbol fonts
147 for (int i = 0; i < face->num_charmaps; ++i) {
148 FT_CharMap cm = face->charmaps[i];
149 if (cm->encoding == FT_ENCODING_ADOBE_CUSTOM
150 || cm->encoding == FT_ENCODING_MS_SYMBOL) {
151 writingSystems.setSupported(QFontDatabase::Symbol);
152 break;
153 }
154 }
155
156 QFont::Stretch stretch = QFont::Unstretched;
157 TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2);
158 if (os2) {
159 quint32 unicodeRange[4] = {
160 quint32(os2->ulUnicodeRange1),
161 quint32(os2->ulUnicodeRange2),
162 quint32(os2->ulUnicodeRange3),
163 quint32(os2->ulUnicodeRange4)
164 };
165 quint32 codePageRange[2] = {
166 quint32(os2->ulCodePageRange1),
167 quint32(os2->ulCodePageRange2)
168 };
169
170 writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange);
171
172 if (os2->usWeightClass) {
173 weight = QPlatformFontDatabase::weightFromInteger(weight: os2->usWeightClass);
174 } else if (os2->panose[2]) {
175 int w = os2->panose[2];
176 if (w <= 1)
177 weight = QFont::Thin;
178 else if (w <= 2)
179 weight = QFont::ExtraLight;
180 else if (w <= 3)
181 weight = QFont::Light;
182 else if (w <= 5)
183 weight = QFont::Normal;
184 else if (w <= 6)
185 weight = QFont::Medium;
186 else if (w <= 7)
187 weight = QFont::DemiBold;
188 else if (w <= 8)
189 weight = QFont::Bold;
190 else if (w <= 9)
191 weight = QFont::ExtraBold;
192 else if (w <= 10)
193 weight = QFont::Black;
194 }
195
196 switch (os2->usWidthClass) {
197 case 1:
198 stretch = QFont::UltraCondensed;
199 break;
200 case 2:
201 stretch = QFont::ExtraCondensed;
202 break;
203 case 3:
204 stretch = QFont::Condensed;
205 break;
206 case 4:
207 stretch = QFont::SemiCondensed;
208 break;
209 case 5:
210 stretch = QFont::Unstretched;
211 break;
212 case 6:
213 stretch = QFont::SemiExpanded;
214 break;
215 case 7:
216 stretch = QFont::Expanded;
217 break;
218 case 8:
219 stretch = QFont::ExtraExpanded;
220 break;
221 case 9:
222 stretch = QFont::UltraExpanded;
223 break;
224 }
225 }
226
227 QString family = QString::fromLatin1(str: face->family_name);
228 FontFile *fontFile = new FontFile;
229 fontFile->fileName = QFile::decodeName(localFileName: file);
230 fontFile->indexValue = index;
231
232
233 registerFont(familyname: family,stylename: QString::fromLatin1(str: face->style_name),foundryname: QString(),weight,style,stretch,antialiased: true,scalable: true,pixelSize: 0,fixedPitch,writingSystems,handle: fontFile);
234
235 families.append(t: family);
236
237 FT_Done_Face(face);
238 ++index;
239 } while (index < numFaces);
240 return families;
241}
242
243QT_END_NAMESPACE
244

source code of qtbase/src/platformsupport/fontdatabases/freetype/qfreetypefontdatabase.cpp