| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/platformsupport/fontdatabases/basic/qbasicfontdatabase.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the plugins of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qbasicfontdatabase_p.h" | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #include <QtGui/private/qguiapplication_p.h> | - | ||||||||||||
| 37 | #include <qpa/qplatformscreen.h> | - | ||||||||||||
| 38 | - | |||||||||||||
| 39 | #include <QtCore/QFile> | - | ||||||||||||
| 40 | #include <QtCore/QLibraryInfo> | - | ||||||||||||
| 41 | #include <QtCore/QDir> | - | ||||||||||||
| 42 | #include <QtCore/QUuid> | - | ||||||||||||
| 43 | #include <QtCore/QtEndian> | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | #undef QT_NO_FREETYPE | - | ||||||||||||
| 46 | #include <QtGui/private/qfontengine_ft_p.h> | - | ||||||||||||
| 47 | #include <QtGui/private/qfontengine_p.h> | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | #include <ft2build.h> | - | ||||||||||||
| 50 | #include FT_TRUETYPE_TABLES_H | - | ||||||||||||
| 51 | #include FT_ERRORS_H | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 54 | - | |||||||||||||
| 55 | void QBasicFontDatabase::populateFontDatabase() | - | ||||||||||||
| 56 | { | - | ||||||||||||
| 57 | QString fontpath = fontDir(); | - | ||||||||||||
| 58 | QDir dir(fontpath); | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | if (!dir.exists()) {
| 0 | ||||||||||||
| 61 | qWarning("QFontDatabase: Cannot find font directory %s - is Qt installed correctly?", | - | ||||||||||||
| 62 | qPrintable(fontpath)); | - | ||||||||||||
| 63 | return; never executed: return; | 0 | ||||||||||||
| 64 | } | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | QStringList nameFilters; | - | ||||||||||||
| 67 | nameFilters << QLatin1String("*.ttf") | - | ||||||||||||
| 68 | << QLatin1String("*.ttc") | - | ||||||||||||
| 69 | << QLatin1String("*.pfa") | - | ||||||||||||
| 70 | << QLatin1String("*.pfb") | - | ||||||||||||
| 71 | << QLatin1String("*.otf"); | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | foreach (const QFileInfo &fi, dir.entryInfoList(nameFilters, QDir::Files)) { | - | ||||||||||||
| 74 | const QByteArray file = QFile::encodeName(fi.absoluteFilePath()); | - | ||||||||||||
| 75 | QBasicFontDatabase::addTTFile(QByteArray(), file); | - | ||||||||||||
| 76 | } never executed: end of block | 0 | ||||||||||||
| 77 | } never executed: end of block | 0 | ||||||||||||
| 78 | - | |||||||||||||
| 79 | QFontEngine *QBasicFontDatabase::fontEngine(const QFontDef &fontDef, void *usrPtr) | - | ||||||||||||
| 80 | { | - | ||||||||||||
| 81 | FontFile *fontfile = static_cast<FontFile *> (usrPtr); | - | ||||||||||||
| 82 | QFontEngine::FaceId fid; | - | ||||||||||||
| 83 | fid.filename = QFile::encodeName(fontfile->fileName); | - | ||||||||||||
| 84 | fid.index = fontfile->indexValue; | - | ||||||||||||
| 85 | - | |||||||||||||
| 86 | bool antialias = !(fontDef.styleStrategy & QFont::NoAntialias); | - | ||||||||||||
| 87 | QFontEngineFT *engine = new QFontEngineFT(fontDef); | - | ||||||||||||
| 88 | QFontEngineFT::GlyphFormat format = QFontEngineFT::Format_Mono; | - | ||||||||||||
| 89 | if (antialias) {
| 0 | ||||||||||||
| 90 | QFontEngine::SubpixelAntialiasingType subpixelType = subpixelAntialiasingTypeHint(); | - | ||||||||||||
| 91 | if (subpixelType == QFontEngine::Subpixel_None || (fontDef.styleStrategy & QFont::NoSubpixelAntialias)) {
| 0 | ||||||||||||
| 92 | format = QFontEngineFT::Format_A8; | - | ||||||||||||
| 93 | engine->subpixelType = QFontEngine::Subpixel_None; | - | ||||||||||||
| 94 | } else { never executed: end of block | 0 | ||||||||||||
| 95 | format = QFontEngineFT::Format_A32; | - | ||||||||||||
| 96 | engine->subpixelType = subpixelType; | - | ||||||||||||
| 97 | } never executed: end of block | 0 | ||||||||||||
| 98 | } | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | if (!engine->init(fid, antialias, format) || engine->invalid()) {
| 0 | ||||||||||||
| 101 | delete engine; | - | ||||||||||||
| 102 | engine = 0; | - | ||||||||||||
| 103 | } else { never executed: end of block | 0 | ||||||||||||
| 104 | engine->setQtDefaultHintStyle(static_cast<QFont::HintingPreference>(fontDef.hintingPreference)); | - | ||||||||||||
| 105 | } never executed: end of block | 0 | ||||||||||||
| 106 | - | |||||||||||||
| 107 | return engine; never executed: return engine; | 0 | ||||||||||||
| 108 | } | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | namespace { | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | class QFontEngineFTRawData: public QFontEngineFT | - | ||||||||||||
| 113 | { | - | ||||||||||||
| 114 | public: | - | ||||||||||||
| 115 | QFontEngineFTRawData(const QFontDef &fontDef) : QFontEngineFT(fontDef) | - | ||||||||||||
| 116 | { | - | ||||||||||||
| 117 | } executed 1249 times by 1 test: end of blockExecuted by:
| 1249 | ||||||||||||
| 118 | - | |||||||||||||
| 119 | void updateFamilyNameAndStyle() | - | ||||||||||||
| 120 | { | - | ||||||||||||
| 121 | fontDef.family = QString::fromLatin1(freetype->face->family_name); | - | ||||||||||||
| 122 | - | |||||||||||||
| 123 | if (freetype->face->style_flags & FT_STYLE_FLAG_ITALIC)
| 605-642 | ||||||||||||
| 124 | fontDef.style = QFont::StyleItalic; executed 605 times by 1 test: fontDef.style = QFont::StyleItalic;Executed by:
| 605 | ||||||||||||
| 125 | - | |||||||||||||
| 126 | if (freetype->face->style_flags & FT_STYLE_FLAG_BOLD)
| 605-642 | ||||||||||||
| 127 | fontDef.weight = QFont::Bold; executed 605 times by 1 test: fontDef.weight = QFont::Bold;Executed by:
| 605 | ||||||||||||
| 128 | } executed 1247 times by 1 test: end of blockExecuted by:
| 1247 | ||||||||||||
| 129 | - | |||||||||||||
| 130 | bool initFromData(const QByteArray &fontData) | - | ||||||||||||
| 131 | { | - | ||||||||||||
| 132 | FaceId faceId; | - | ||||||||||||
| 133 | faceId.filename = ""; | - | ||||||||||||
| 134 | faceId.index = 0; | - | ||||||||||||
| 135 | faceId.uuid = QUuid::createUuid().toByteArray(); | - | ||||||||||||
| 136 | - | |||||||||||||
| 137 | return init(faceId, true, Format_None, fontData); executed 1249 times by 1 test: return init(faceId, true, Format_None, fontData);Executed by:
| 1249 | ||||||||||||
| 138 | } | - | ||||||||||||
| 139 | }; | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | } | - | ||||||||||||
| 142 | - | |||||||||||||
| 143 | QFontEngine *QBasicFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize, | - | ||||||||||||
| 144 | QFont::HintingPreference hintingPreference) | - | ||||||||||||
| 145 | { | - | ||||||||||||
| 146 | QFontDef fontDef; | - | ||||||||||||
| 147 | fontDef.pixelSize = pixelSize; | - | ||||||||||||
| 148 | fontDef.hintingPreference = hintingPreference; | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | QFontEngineFTRawData *fe = new QFontEngineFTRawData(fontDef); | - | ||||||||||||
| 151 | if (!fe->initFromData(fontData)) {
| 2-1247 | ||||||||||||
| 152 | delete fe; | - | ||||||||||||
| 153 | return 0; executed 2 times by 1 test: return 0;Executed by:
| 2 | ||||||||||||
| 154 | } | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | fe->updateFamilyNameAndStyle(); | - | ||||||||||||
| 157 | fe->setQtDefaultHintStyle(static_cast<QFont::HintingPreference>(fontDef.hintingPreference)); | - | ||||||||||||
| 158 | - | |||||||||||||
| 159 | return fe; executed 1247 times by 1 test: return fe;Executed by:
| 1247 | ||||||||||||
| 160 | } | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | QStringList QBasicFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName) | - | ||||||||||||
| 163 | { | - | ||||||||||||
| 164 | return QBasicFontDatabase::addTTFile(fontData, fileName.toLocal8Bit()); never executed: return QBasicFontDatabase::addTTFile(fontData, fileName.toLocal8Bit()); | 0 | ||||||||||||
| 165 | } | - | ||||||||||||
| 166 | - | |||||||||||||
| 167 | void QBasicFontDatabase::releaseHandle(void *handle) | - | ||||||||||||
| 168 | { | - | ||||||||||||
| 169 | FontFile *file = static_cast<FontFile *>(handle); | - | ||||||||||||
| 170 | delete file; | - | ||||||||||||
| 171 | } executed 21921 times by 127 tests: end of blockExecuted by:
| 21921 | ||||||||||||
| 172 | - | |||||||||||||
| 173 | extern FT_Library qt_getFreetype(); | - | ||||||||||||
| 174 | - | |||||||||||||
| 175 | QStringList QBasicFontDatabase::addTTFile(const QByteArray &fontData, const QByteArray &file) | - | ||||||||||||
| 176 | { | - | ||||||||||||
| 177 | FT_Library library = qt_getFreetype(); | - | ||||||||||||
| 178 | - | |||||||||||||
| 179 | int index = 0; | - | ||||||||||||
| 180 | int numFaces = 0; | - | ||||||||||||
| 181 | QStringList families; | - | ||||||||||||
| 182 | do { | - | ||||||||||||
| 183 | FT_Face face; | - | ||||||||||||
| 184 | FT_Error error; | - | ||||||||||||
| 185 | if (!fontData.isEmpty()) {
| 0 | ||||||||||||
| 186 | error = FT_New_Memory_Face(library, (const FT_Byte *)fontData.constData(), fontData.size(), index, &face); | - | ||||||||||||
| 187 | } else { never executed: end of block | 0 | ||||||||||||
| 188 | error = FT_New_Face(library, file.constData(), index, &face); | - | ||||||||||||
| 189 | } never executed: end of block | 0 | ||||||||||||
| 190 | if (error != FT_Err_Ok) {
| 0 | ||||||||||||
| 191 | qDebug() << "FT_New_Face failed with index" << index << ':' << hex << error; | - | ||||||||||||
| 192 | break; never executed: break; | 0 | ||||||||||||
| 193 | } | - | ||||||||||||
| 194 | numFaces = face->num_faces; | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | QFont::Weight weight = QFont::Normal; | - | ||||||||||||
| 197 | - | |||||||||||||
| 198 | QFont::Style style = QFont::StyleNormal; | - | ||||||||||||
| 199 | if (face->style_flags & FT_STYLE_FLAG_ITALIC)
| 0 | ||||||||||||
| 200 | style = QFont::StyleItalic; never executed: style = QFont::StyleItalic; | 0 | ||||||||||||
| 201 | - | |||||||||||||
| 202 | if (face->style_flags & FT_STYLE_FLAG_BOLD)
| 0 | ||||||||||||
| 203 | weight = QFont::Bold; never executed: weight = QFont::Bold; | 0 | ||||||||||||
| 204 | - | |||||||||||||
| 205 | bool fixedPitch = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH); | - | ||||||||||||
| 206 | - | |||||||||||||
| 207 | QSupportedWritingSystems writingSystems; | - | ||||||||||||
| 208 | // detect symbol fonts | - | ||||||||||||
| 209 | for (int i = 0; i < face->num_charmaps; ++i) {
| 0 | ||||||||||||
| 210 | FT_CharMap cm = face->charmaps[i]; | - | ||||||||||||
| 211 | if (cm->encoding == FT_ENCODING_ADOBE_CUSTOM
| 0 | ||||||||||||
| 212 | || cm->encoding == FT_ENCODING_MS_SYMBOL) {
| 0 | ||||||||||||
| 213 | writingSystems.setSupported(QFontDatabase::Symbol); | - | ||||||||||||
| 214 | break; never executed: break; | 0 | ||||||||||||
| 215 | } | - | ||||||||||||
| 216 | } never executed: end of block | 0 | ||||||||||||
| 217 | - | |||||||||||||
| 218 | TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2); | - | ||||||||||||
| 219 | if (os2) {
| 0 | ||||||||||||
| 220 | quint32 unicodeRange[4] = { | - | ||||||||||||
| 221 | quint32(os2->ulUnicodeRange1), | - | ||||||||||||
| 222 | quint32(os2->ulUnicodeRange2), | - | ||||||||||||
| 223 | quint32(os2->ulUnicodeRange3), | - | ||||||||||||
| 224 | quint32(os2->ulUnicodeRange4) | - | ||||||||||||
| 225 | }; | - | ||||||||||||
| 226 | quint32 codePageRange[2] = { | - | ||||||||||||
| 227 | quint32(os2->ulCodePageRange1), | - | ||||||||||||
| 228 | quint32(os2->ulCodePageRange2) | - | ||||||||||||
| 229 | }; | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange); | - | ||||||||||||
| 232 | - | |||||||||||||
| 233 | if (os2->usWeightClass) {
| 0 | ||||||||||||
| 234 | weight = QPlatformFontDatabase::weightFromInteger(os2->usWeightClass); | - | ||||||||||||
| 235 | } else if (os2->panose[2]) { never executed: end of block
| 0 | ||||||||||||
| 236 | int w = os2->panose[2]; | - | ||||||||||||
| 237 | if (w <= 1)
| 0 | ||||||||||||
| 238 | weight = QFont::Thin; never executed: weight = QFont::Thin; | 0 | ||||||||||||
| 239 | else if (w <= 2)
| 0 | ||||||||||||
| 240 | weight = QFont::ExtraLight; never executed: weight = QFont::ExtraLight; | 0 | ||||||||||||
| 241 | else if (w <= 3)
| 0 | ||||||||||||
| 242 | weight = QFont::Light; never executed: weight = QFont::Light; | 0 | ||||||||||||
| 243 | else if (w <= 5)
| 0 | ||||||||||||
| 244 | weight = QFont::Normal; never executed: weight = QFont::Normal; | 0 | ||||||||||||
| 245 | else if (w <= 6)
| 0 | ||||||||||||
| 246 | weight = QFont::Medium; never executed: weight = QFont::Medium; | 0 | ||||||||||||
| 247 | else if (w <= 7)
| 0 | ||||||||||||
| 248 | weight = QFont::DemiBold; never executed: weight = QFont::DemiBold; | 0 | ||||||||||||
| 249 | else if (w <= 8)
| 0 | ||||||||||||
| 250 | weight = QFont::Bold; never executed: weight = QFont::Bold; | 0 | ||||||||||||
| 251 | else if (w <= 9)
| 0 | ||||||||||||
| 252 | weight = QFont::ExtraBold; never executed: weight = QFont::ExtraBold; | 0 | ||||||||||||
| 253 | else if (w <= 10)
| 0 | ||||||||||||
| 254 | weight = QFont::Black; never executed: weight = QFont::Black; | 0 | ||||||||||||
| 255 | } never executed: end of block | 0 | ||||||||||||
| 256 | } never executed: end of block | 0 | ||||||||||||
| 257 | - | |||||||||||||
| 258 | QString family = QString::fromLatin1(face->family_name); | - | ||||||||||||
| 259 | FontFile *fontFile = new FontFile; | - | ||||||||||||
| 260 | fontFile->fileName = QFile::decodeName(file); | - | ||||||||||||
| 261 | fontFile->indexValue = index; | - | ||||||||||||
| 262 | - | |||||||||||||
| 263 | QFont::Stretch stretch = QFont::Unstretched; | - | ||||||||||||
| 264 | - | |||||||||||||
| 265 | registerFont(family,QString::fromLatin1(face->style_name),QString(),weight,style,stretch,true,true,0,fixedPitch,writingSystems,fontFile); | - | ||||||||||||
| 266 | - | |||||||||||||
| 267 | families.append(family); | - | ||||||||||||
| 268 | - | |||||||||||||
| 269 | FT_Done_Face(face); | - | ||||||||||||
| 270 | ++index; | - | ||||||||||||
| 271 | } while (index < numFaces); never executed: end of block
| 0 | ||||||||||||
| 272 | return families; never executed: return families; | 0 | ||||||||||||
| 273 | } | - | ||||||||||||
| 274 | - | |||||||||||||
| 275 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |