| 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 "qfontengine_ft_p.h" |
| 52 | |
| 53 | #include <ft2build.h> |
| 54 | #include FT_TRUETYPE_TABLES_H |
| 55 | #include FT_ERRORS_H |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | void QFreeTypeFontDatabase::populateFontDatabase() |
| 60 | { |
| 61 | QString fontpath = fontDir(); |
| 62 | QDir dir(fontpath); |
| 63 | |
| 64 | if (!dir.exists()) { |
| 65 | qWarning("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, QDir::Files); |
| 79 | for (const QFileInfo &fi : fis) { |
| 80 | const QByteArray file = QFile::encodeName(fi.absoluteFilePath()); |
| 81 | QFreeTypeFontDatabase::addTTFile(QByteArray(), file); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | QFontEngine *QFreeTypeFontDatabase::fontEngine(const QFontDef &fontDef, void *usrPtr) |
| 86 | { |
| 87 | FontFile *fontfile = static_cast<FontFile *>(usrPtr); |
| 88 | QFontEngine::FaceId faceId; |
| 89 | faceId.filename = QFile::encodeName(fontfile->fileName); |
| 90 | faceId.index = fontfile->indexValue; |
| 91 | |
| 92 | return QFontEngineFT::create(fontDef, faceId); |
| 93 | } |
| 94 | |
| 95 | QFontEngine *QFreeTypeFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize, |
| 96 | QFont::HintingPreference hintingPreference) |
| 97 | { |
| 98 | return QFontEngineFT::create(fontData, pixelSize, hintingPreference); |
| 99 | } |
| 100 | |
| 101 | QStringList QFreeTypeFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *applicationFont) |
| 102 | { |
| 103 | return QFreeTypeFontDatabase::addTTFile(fontData, fileName.toLocal8Bit(), applicationFont); |
| 104 | } |
| 105 | |
| 106 | void QFreeTypeFontDatabase::releaseHandle(void *handle) |
| 107 | { |
| 108 | FontFile *file = static_cast<FontFile *>(handle); |
| 109 | delete file; |
| 110 | } |
| 111 | |
| 112 | extern FT_Library qt_getFreetype(); |
| 113 | |
| 114 | QStringList QFreeTypeFontDatabase::addTTFile(const QByteArray &fontData, const QByteArray &file, QFontDatabasePrivate::ApplicationFont *applicationFont) |
| 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, (const FT_Byte *)fontData.constData(), fontData.size(), index, &face); |
| 126 | } else { |
| 127 | error = FT_New_Face(library, file.constData(), index, &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 | TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2); |
| 157 | if (os2) { |
| 158 | quint32 unicodeRange[4] = { |
| 159 | quint32(os2->ulUnicodeRange1), |
| 160 | quint32(os2->ulUnicodeRange2), |
| 161 | quint32(os2->ulUnicodeRange3), |
| 162 | quint32(os2->ulUnicodeRange4) |
| 163 | }; |
| 164 | quint32 [2] = { |
| 165 | quint32(os2->ulCodePageRange1), |
| 166 | quint32(os2->ulCodePageRange2) |
| 167 | }; |
| 168 | |
| 169 | writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange); |
| 170 | |
| 171 | if (os2->usWeightClass) { |
| 172 | weight = static_cast<QFont::Weight>(os2->usWeightClass); |
| 173 | } else if (os2->panose[2]) { |
| 174 | int w = os2->panose[2]; |
| 175 | if (w <= 1) |
| 176 | weight = QFont::Thin; |
| 177 | else if (w <= 2) |
| 178 | weight = QFont::ExtraLight; |
| 179 | else if (w <= 3) |
| 180 | weight = QFont::Light; |
| 181 | else if (w <= 5) |
| 182 | weight = QFont::Normal; |
| 183 | else if (w <= 6) |
| 184 | weight = QFont::Medium; |
| 185 | else if (w <= 7) |
| 186 | weight = QFont::DemiBold; |
| 187 | else if (w <= 8) |
| 188 | weight = QFont::Bold; |
| 189 | else if (w <= 9) |
| 190 | weight = QFont::ExtraBold; |
| 191 | else if (w <= 10) |
| 192 | weight = QFont::Black; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | QString family = QString::fromLatin1(face->family_name); |
| 197 | FontFile *fontFile = new FontFile; |
| 198 | fontFile->fileName = QFile::decodeName(file); |
| 199 | fontFile->indexValue = index; |
| 200 | |
| 201 | QFont::Stretch stretch = QFont::Unstretched; |
| 202 | QString styleName = QString::fromLatin1(face->style_name); |
| 203 | |
| 204 | if (applicationFont != nullptr) { |
| 205 | QFontDatabasePrivate::ApplicationFont::Properties properties; |
| 206 | properties.familyName = family; |
| 207 | properties.styleName = styleName; |
| 208 | properties.weight = weight; |
| 209 | properties.stretch = stretch; |
| 210 | properties.style = style; |
| 211 | |
| 212 | applicationFont->properties.append(properties); |
| 213 | } |
| 214 | |
| 215 | registerFont(family, styleName, QString(), weight, style, stretch, true, true, 0, fixedPitch, writingSystems, fontFile); |
| 216 | families.append(family); |
| 217 | |
| 218 | FT_Done_Face(face); |
| 219 | ++index; |
| 220 | } while (index < numFaces); |
| 221 | return families; |
| 222 | } |
| 223 | |
| 224 | QT_END_NAMESPACE |
| 225 | |