-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathstyle_core_font.cpp
389 lines (334 loc) · 10.1 KB
/
style_core_font.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/style/style_core_font.h"
#include "base/algorithm.h"
#include "base/debug_log.h"
#include "base/base_file_utilities.h"
#include "ui/style/style_core_custom_font.h"
#include "ui/integration.h"
#include <QtCore/QMap>
#include <QtCore/QVector>
#include <QtGui/QFontInfo>
#include <QtGui/QFontDatabase>
#include <QtWidgets/QApplication>
void style_InitFontsResource() {
#ifdef Q_OS_MAC // Use resources from the .app bundle on macOS.
base::RegisterResourceArchive(u"lib_ui.rcc"_q);
#else // Q_OS_MAC
#ifndef DESKTOP_APP_USE_PACKAGED_FONTS
Q_INIT_RESOURCE(fonts);
#endif // !DESKTOP_APP_USE_PACKAGED_FONTS
#ifdef Q_OS_WIN
Q_INIT_RESOURCE(win);
#endif // Q_OS_WIN
#endif // Q_OS_MAC
}
namespace style {
namespace internal {
namespace {
QMap<QString, int> fontFamilyMap;
QVector<QString> fontFamilies;
QMap<uint32, FontData*> fontsMap;
uint32 fontKey(int size, uint32 flags, int family) {
return (((uint32(family) << 12) | uint32(size)) << 6) | flags;
}
bool ValidateFont(const QString &familyName, int flags = 0) {
QFont checkFont(familyName);
checkFont.setBold(flags & style::internal::FontBold);
checkFont.setItalic(flags & style::internal::FontItalic);
checkFont.setUnderline(flags & style::internal::FontUnderline);
auto realFamily = QFontInfo(checkFont).family();
if (realFamily.trimmed().compare(familyName, Qt::CaseInsensitive)) {
LOG(("Font Error: could not resolve '%1' font, got '%2'.").arg(familyName, realFamily));
return false;
}
auto metrics = QFontMetrics(checkFont);
if (!metrics.height()) {
LOG(("Font Error: got a zero height in '%1'.").arg(familyName));
return false;
}
return true;
}
bool LoadCustomFont(const QString &filePath, const QString &familyName, int flags = 0) {
auto regularId = QFontDatabase::addApplicationFont(filePath);
if (regularId < 0) {
LOG(("Font Error: could not add '%1'.").arg(filePath));
return false;
}
const auto found = [&] {
for (auto &family : QFontDatabase::applicationFontFamilies(regularId)) {
LOG(("Font: from '%1' loaded '%2'").arg(filePath, family));
if (!family.trimmed().compare(familyName, Qt::CaseInsensitive)) {
return true;
}
}
return false;
}();
if (!found) {
LOG(("Font Error: could not locate '%1' font in '%2'.").arg(familyName, filePath));
return false;
}
return ValidateFont(familyName, flags);
}
[[nodiscard]] QString SystemMonospaceFont() {
const auto type = QFontDatabase::FixedFont;
return QFontDatabase::systemFont(type).family();
}
[[nodiscard]] QString ManualMonospaceFont() {
const auto kTryFirst = std::initializer_list<QString>{
"Consolas",
"Liberation Mono",
"Menlo",
"Courier"
};
for (const auto &family : kTryFirst) {
const auto resolved = QFontInfo(QFont(family)).family();
if (!resolved.trimmed().compare(family, Qt::CaseInsensitive)) {
return family;
}
}
return QString();
}
enum {
FontTypeRegular = 0,
FontTypeRegularItalic,
FontTypeBold,
FontTypeBoldItalic,
FontTypeSemibold,
FontTypeSemiboldItalic,
FontTypesCount,
};
#ifndef DESKTOP_APP_USE_PACKAGED_FONTS
QString FontTypeFiles[FontTypesCount] = {
"DAOpenSansRegular",
"DAOpenSansRegularItalic",
"DAOpenSansSemiboldAsBold",
"DAOpenSansSemiboldItalicAsBold",
"DAOpenSansSemiboldAsBold",
"DAOpenSansSemiboldItalicAsBold",
};
QString FontTypeNames[FontTypesCount] = {
"DAOpenSansRegular",
"DAOpenSansRegularItalic",
"DAOpenSansSemibold",
"DAOpenSansSemiboldItalic",
"DAOpenSansSemibold",
"DAOpenSansSemiboldItalic",
};
QString FontTypePersianFallbackFiles[FontTypesCount] = {
"DAVazirRegular",
"DAVazirRegular",
"DAVazirMediumAsBold",
"DAVazirMediumAsBold",
"DAVazirMediumAsBold",
"DAVazirMediumAsBold",
};
QString FontTypePersianFallback[FontTypesCount] = {
"DAVazirRegular",
"DAVazirRegular",
"DAVazirMedium",
"DAVazirMedium",
"DAVazirMedium",
"DAVazirMedium",
};
#endif // !DESKTOP_APP_USE_PACKAGED_FONTS
int32 FontTypeFlags[FontTypesCount] = {
0,
FontItalic,
FontBold,
FontBold | FontItalic,
FontSemibold,
FontSemibold | FontItalic,
};
bool Started = false;
QString Overrides[FontTypesCount];
} // namespace
void StartFonts() {
if (Started) {
return;
}
Started = true;
style_InitFontsResource();
#ifndef DESKTOP_APP_USE_PACKAGED_FONTS
[[maybe_unused]] bool areGood[FontTypesCount] = { false };
for (auto i = 0; i != FontTypesCount; ++i) {
const auto file = FontTypeFiles[i];
const auto name = FontTypeNames[i];
const auto flags = FontTypeFlags[i];
areGood[i] = LoadCustomFont(":/gui/fonts/" + file + ".ttf", name, flags);
Overrides[i] = name;
const auto persianFallbackFile = FontTypePersianFallbackFiles[i];
const auto persianFallback = FontTypePersianFallback[i];
LoadCustomFont(":/gui/fonts/" + persianFallbackFile + ".ttf", persianFallback, flags);
#ifdef Q_OS_WIN
// Attempt to workaround a strange font bug with Open Sans Semibold not loading.
// See https://github.com/telegramdesktop/tdesktop/issues/3276 for details.
// Crash happens on "options.maxh / _t->_st->font->height" with "division by zero".
// In that place "_t->_st->font" is "semiboldFont" is "font(13 "Open Sans Semibold").
const auto fallback = "Segoe UI";
if (!areGood[i]) {
if (ValidateFont(fallback, flags)) {
Overrides[i] = fallback;
LOG(("Fonts Info: Using '%1' instead of '%2'.").arg(fallback).arg(name));
}
}
// Disable default fallbacks to Segoe UI, see:
// https://github.com/telegramdesktop/tdesktop/issues/5368
//
//QFont::insertSubstitution(name, fallback);
#endif // Q_OS_WIN
QFont::insertSubstitution(name, persianFallback);
}
#ifdef Q_OS_MAC
auto list = QStringList();
list.append("STIXGeneral");
list.append(".SF NS Text");
list.append("Helvetica Neue");
list.append("Lucida Grande");
for (const auto &name : FontTypeNames) {
QFont::insertSubstitutions(name, list);
}
#endif // Q_OS_MAC
#endif // !DESKTOP_APP_USE_PACKAGED_FONTS
auto appFont = QApplication::font();
appFont.setStyleStrategy(QFont::PreferQuality);
QApplication::setFont(appFont);
}
QString GetPossibleEmptyOverride(int32 flags) {
flags = flags & (FontBold | FontSemibold | FontItalic);
int32 flagsBold = flags & (FontBold | FontItalic);
int32 flagsSemibold = flags & (FontSemibold | FontItalic);
if (flagsSemibold == (FontSemibold | FontItalic)) {
return Overrides[FontTypeSemiboldItalic];
} else if (flagsSemibold == FontSemibold) {
return Overrides[FontTypeSemibold];
} else if (flagsBold == (FontBold | FontItalic)) {
return Overrides[FontTypeBoldItalic];
} else if (flagsBold == FontBold) {
return Overrides[FontTypeBold];
} else if (flags == FontItalic) {
return Overrides[FontTypeRegularItalic];
} else if (flags == 0) {
return Overrides[FontTypeRegular];
}
return QString();
}
QString GetFontOverride(int32 flags) {
const auto result = GetPossibleEmptyOverride(flags);
return result.isEmpty() ? "Open Sans" : result;
}
QString MonospaceFont() {
static const auto family = [&]() -> QString {
const auto manual = ManualMonospaceFont();
const auto system = SystemMonospaceFont();
#if defined Q_OS_WIN || defined Q_OS_MAC
// Prefer our monospace font.
const auto useSystem = manual.isEmpty();
#else // Q_OS_WIN || Q_OS_MAC
// Prefer system monospace font.
const auto metrics = QFontMetrics(QFont(system));
const auto useSystem = manual.isEmpty()
|| (metrics.horizontalAdvance(QChar('i')) == metrics.horizontalAdvance(QChar('W')));
#endif // Q_OS_WIN || Q_OS_MAC
return useSystem ? system : manual;
}();
return family;
}
void destroyFonts() {
for (auto fontData : std::as_const(fontsMap)) {
delete fontData;
}
fontsMap.clear();
}
int registerFontFamily(const QString &family) {
auto result = fontFamilyMap.value(family, -1);
if (result < 0) {
result = fontFamilies.size();
fontFamilyMap.insert(family, result);
fontFamilies.push_back(family);
}
return result;
}
FontData::FontData(int size, uint32 flags, int family, Font *other)
: f(ResolveFont(flags, size))
, m(f)
, _size(size)
, _flags(flags)
, _family(family) {
if (other) {
memcpy(modified, other, sizeof(modified));
}
modified[_flags] = Font(this);
height = m.height();
ascent = m.ascent();
descent = m.descent();
spacew = width(QLatin1Char(' '));
elidew = width("...");
}
Font FontData::bold(bool set) const {
return otherFlagsFont(FontBold, set);
}
Font FontData::italic(bool set) const {
return otherFlagsFont(FontItalic, set);
}
Font FontData::underline(bool set) const {
return otherFlagsFont(FontUnderline, set);
}
Font FontData::strikeout(bool set) const {
return otherFlagsFont(FontStrikeOut, set);
}
Font FontData::semibold(bool set) const {
return otherFlagsFont(FontSemibold, set);
}
Font FontData::monospace(bool set) const {
return otherFlagsFont(FontMonospace, set);
}
int FontData::size() const {
return _size;
}
uint32 FontData::flags() const {
return _flags;
}
int FontData::family() const {
return _family;
}
Font FontData::otherFlagsFont(uint32 flag, bool set) const {
int32 newFlags = set ? (_flags | flag) : (_flags & ~flag);
if (!modified[newFlags].v()) {
modified[newFlags] = Font(_size, newFlags, _family, modified);
}
return modified[newFlags];
}
Font::Font(int size, uint32 flags, const QString &family) {
if (fontFamilyMap.isEmpty()) {
for (uint32 i = 0, s = fontFamilies.size(); i != s; ++i) {
fontFamilyMap.insert(fontFamilies.at(i), i);
}
}
auto i = fontFamilyMap.constFind(family);
if (i == fontFamilyMap.cend()) {
fontFamilies.push_back(family);
i = fontFamilyMap.insert(family, fontFamilies.size() - 1);
}
init(size, flags, i.value(), 0);
}
Font::Font(int size, uint32 flags, int family) {
init(size, flags, family, 0);
}
Font::Font(int size, uint32 flags, int family, Font *modified) {
init(size, flags, family, modified);
}
void Font::init(int size, uint32 flags, int family, Font *modified) {
uint32 key = fontKey(size, flags, family);
auto i = fontsMap.constFind(key);
if (i == fontsMap.cend()) {
i = fontsMap.insert(key, new FontData(size, flags, family, modified));
}
ptr = i.value();
}
} // namespace internal
} // namespace style