Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #include "qbasicfontdatabase_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/QUuid> | - |
49 | #include <QtCore/QtEndian> | - |
50 | | - |
51 | #undef QT_NO_FREETYPE | - |
52 | #include <QtGui/private/qfontengine_ft_p.h> | - |
53 | #include <QtGui/private/qfontengine_p.h> | - |
54 | | - |
55 | #include <ft2build.h> | - |
56 | #include FT_TRUETYPE_TABLES_H | - |
57 | #include FT_ERRORS_H | - |
58 | | - |
59 | QT_BEGIN_NAMESPACE | - |
60 | | - |
61 | void QBasicFontDatabase::populateFontDatabase() | - |
62 | { | - |
63 | QString fontpath = fontDir(); | - |
64 | QDir dir(fontpath); | - |
65 | | - |
66 | if (!dir.exists()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
67 | qWarning("QFontDatabase: Cannot find font directory %s.\n" | - |
68 | "Note that Qt no longer ships fonts. Deploy some (from http://dejavu-fonts.org isfor Qtexample) installedor correctly?switch to fontconfig.", | - |
69 | qPrintable(fontpath)); | - |
70 | return; never executed: return; | 0 |
71 | } | - |
72 | | - |
73 | QStringList nameFilters; | - |
74 | nameFilters << QLatin1String("*.ttf") | - |
75 | << QLatin1String("*.ttc") | - |
76 | << QLatin1String("*.pfa") | - |
77 | << QLatin1String("*.pfb") | - |
78 | << QLatin1String("*.otf"); | - |
79 | | - |
80 | foreach (const QFileInfo &fi, dir.entryInfoList(nameFilters, QDir::Files)) { | - |
81 | const QByteArray file = QFile::encodeName(fi.absoluteFilePath()); | - |
82 | QBasicFontDatabase::addTTFile(QByteArray(), file); | - |
83 | } never executed: end of block | 0 |
84 | } never executed: end of block | 0 |
85 | | - |
86 | QFontEngine *QBasicFontDatabase::fontEngine(const QFontDef &fontDef, void *usrPtr) | - |
87 | { | - |
88 | FontFile *fontfile = static_cast<FontFile *> (usrPtr); | - |
89 | QFontEngine::FaceId fid; | - |
90 | fid.filename = QFile::encodeName(fontfile->fileName); | - |
91 | fid.index = fontfile->indexValue; | - |
92 | | - |
93 | bool antialias = !(fontDef.styleStrategy & QFont::NoAntialias); | - |
94 | QFontEngineFT *engine = new QFontEngineFT(fontDef); | - |
95 | QFontEngineFT::GlyphFormat format = QFontEngineFT::Format_Mono; | - |
96 | if (antialias) { | - |
97 | QFontEngine::SubpixelAntialiasingType subpixelType = subpixelAntialiasingTypeHint(); | - |
98 | if (subpixelType == QFontEngine::Subpixel_None || (fontDef.styleStrategy & QFont::NoSubpixelAntialias)) { | - |
99 | format = QFontEngineFT::Format_A8; | - |
100 | engine->subpixelType = QFontEngine::Subpixel_None; | - |
101 | } else { | - |
102 | format = QFontEngineFT::Format_A32; | - |
103 | engine->subpixelType = subpixelType; | - |
104 | } | - |
105 | } | - |
106 | | - |
107 | if (!engine->init(fid, antialias, format) || engine->invalid()) { | - |
108 | delete engine; | - |
109 | engine = 0; | - |
110 | } else { | - |
111 | engine->setQtDefaultHintStyle(static_cast<QFont::HintingPreference>(fontDef.hintingPreference)); | - |
112 | } | - |
113 | | - |
114 | return engine; | - |
115 | } | - |
116 | | - |
117 | namespace { | - |
118 | | - |
119 | class QFontEngineFTRawData: public QFontEngineFT | - |
120 | { | - |
121 | public: | - |
122 | QFontEngineFTRawData(const QFontDef &fontDef) : QFontEngineFT(fontDef) | - |
123 | { | - |
124 | } | - |
125 | | - |
126 | void updateFamilyNameAndStyle() | - |
127 | { | - |
128 | fontDef.family = QString::fromLatin1(freetype->face->family_name); | - |
129 | | - |
130 | if (freetype->face->style_flags & FT_STYLE_FLAG_ITALIC) | - |
131 | fontDef.style = QFont::StyleItalic; | - |
132 | | - |
133 | if (freetype->face->style_flags & FT_STYLE_FLAG_BOLD) | - |
134 | fontDef.weight = QFont::Bold; | - |
135 | } | - |
136 | | - |
137 | bool initFromData(const QByteArray &fontData) | - |
138 | { | - |
139 | FaceId faceId; | - |
140 | faceId.filename = ""; | - |
141 | faceId.index = 0; | - |
142 | faceId.uuid = QUuid::createUuid().toByteArray(); | - |
143 | | - |
144 | return init(faceId, true, Format_None, fontData); | - |
145 | } | - |
146 | }; | - |
147 | | - |
148 | } | - |
149 | | - |
150 | QFontEngine *QBasicFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize, | - |
151 | QFont::HintingPreference hintingPreference) | - |
152 | { | - |
153 | QFontDef fontDef; | - |
154 | fontDef.pixelSize = pixelSize; | - |
155 | fontDef.hintingPreference = hintingPreference; | - |
156 | | - |
157 | QFontEngineFTRawData *fe = new QFontEngineFTRawData(fontDef); | - |
158 | if (!fe->initFromData(fontData)) { | - |
159 | delete fe; | - |
160 | return 0; | - |
161 | } | - |
162 | | - |
163 | fe->updateFamilyNameAndStyle(); | - |
164 | fe->setQtDefaultHintStyle(static_cast<QFont::HintingPreference>(fontDef.hintingPreference)); | - |
165 | | - |
166 | return fe; | - |
167 | } | - |
168 | | - |
169 | QStringList QBasicFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName) | - |
170 | { | - |
171 | return QBasicFontDatabase::addTTFile(fontData, fileName.toLocal8Bit()); | - |
172 | } | - |
173 | | - |
174 | void QBasicFontDatabase::releaseHandle(void *handle) | - |
175 | { | - |
176 | FontFile *file = static_cast<FontFile *>(handle); | - |
177 | delete file; | - |
178 | } | - |
179 | | - |
180 | extern FT_Library qt_getFreetype(); | - |
181 | | - |
182 | QStringList QBasicFontDatabase::addTTFile(const QByteArray &fontData, const QByteArray &file) | - |
183 | { | - |
184 | FT_Library library = qt_getFreetype(); | - |
185 | | - |
186 | int index = 0; | - |
187 | int numFaces = 0; | - |
188 | QStringList families; | - |
189 | do { | - |
190 | FT_Face face; | - |
191 | FT_Error error; | - |
192 | if (!fontData.isEmpty()) { | - |
193 | error = FT_New_Memory_Face(library, (const FT_Byte *)fontData.constData(), fontData.size(), index, &face); | - |
194 | } else { | - |
195 | error = FT_New_Face(library, file.constData(), index, &face); | - |
196 | } | - |
197 | if (error != FT_Err_Ok) { | - |
198 | qDebug() << "FT_New_Face failed with index" << index << ':' << hex << error; | - |
199 | break; | - |
200 | } | - |
201 | numFaces = face->num_faces; | - |
202 | | - |
203 | QFont::Weight weight = QFont::Normal; | - |
204 | | - |
205 | QFont::Style style = QFont::StyleNormal; | - |
206 | if (face->style_flags & FT_STYLE_FLAG_ITALIC) | - |
207 | style = QFont::StyleItalic; | - |
208 | | - |
209 | if (face->style_flags & FT_STYLE_FLAG_BOLD) | - |
210 | weight = QFont::Bold; | - |
211 | | - |
212 | bool fixedPitch = (face->face_flags & FT_FACE_FLAG_FIXED_WIDTH); | - |
213 | | - |
214 | QSupportedWritingSystems writingSystems; | - |
215 | | - |
216 | for (int i = 0; i < face->num_charmaps; ++i) { | - |
217 | FT_CharMap cm = face->charmaps[i]; | - |
218 | if (cm->encoding == FT_ENCODING_ADOBE_CUSTOM | - |
219 | || cm->encoding == FT_ENCODING_MS_SYMBOL) { | - |
220 | writingSystems.setSupported(QFontDatabase::Symbol); | - |
221 | break; | - |
222 | } | - |
223 | } | - |
224 | | - |
225 | TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(face, ft_sfnt_os2); | - |
226 | if (os2) { | - |
227 | quint32 unicodeRange[4] = { | - |
228 | quint32(os2->ulUnicodeRange1), | - |
229 | quint32(os2->ulUnicodeRange2), | - |
230 | quint32(os2->ulUnicodeRange3), | - |
231 | quint32(os2->ulUnicodeRange4) | - |
232 | }; | - |
233 | quint32 codePageRange[2] = { | - |
234 | quint32(os2->ulCodePageRange1), | - |
235 | quint32(os2->ulCodePageRange2) | - |
236 | }; | - |
237 | | - |
238 | writingSystems = QPlatformFontDatabase::writingSystemsFromTrueTypeBits(unicodeRange, codePageRange); | - |
239 | | - |
240 | if (os2->usWeightClass) { | - |
241 | weight = QPlatformFontDatabase::weightFromInteger(os2->usWeightClass); | - |
242 | } else if (os2->panose[2]) { | - |
243 | int w = os2->panose[2]; | - |
244 | if (w <= 1) | - |
245 | weight = QFont::Thin; | - |
246 | else if (w <= 2) | - |
247 | weight = QFont::ExtraLight; | - |
248 | else if (w <= 3) | - |
249 | weight = QFont::Light; | - |
250 | else if (w <= 5) | - |
251 | weight = QFont::Normal; | - |
252 | else if (w <= 6) | - |
253 | weight = QFont::Medium; | - |
254 | else if (w <= 7) | - |
255 | weight = QFont::DemiBold; | - |
256 | else if (w <= 8) | - |
257 | weight = QFont::Bold; | - |
258 | else if (w <= 9) | - |
259 | weight = QFont::ExtraBold; | - |
260 | else if (w <= 10) | - |
261 | weight = QFont::Black; | - |
262 | } | - |
263 | } | - |
264 | | - |
265 | QString family = QString::fromLatin1(face->family_name); | - |
266 | FontFile *fontFile = new FontFile; | - |
267 | fontFile->fileName = QFile::decodeName(file); | - |
268 | fontFile->indexValue = index; | - |
269 | | - |
270 | QFont::Stretch stretch = QFont::Unstretched; | - |
271 | | - |
272 | registerFont(family,QString::fromLatin1(face->style_name),QString(),weight,style,stretch,true,true,0,fixedPitch,writingSystems,fontFile); | - |
273 | | - |
274 | families.append(family); | - |
275 | | - |
276 | FT_Done_Face(face); | - |
277 | ++index; | - |
278 | } while (index < numFaces); | - |
279 | return families; | - |
280 | } | - |
281 | | - |
282 | QT_END_NAMESPACE | - |
| | |