Get rid of no more used GetFontOverride

This commit is contained in:
Ilya Fedin 2024-04-23 09:54:23 +04:00 committed by John Preston
parent 553d5509b5
commit 17cc6f3783
2 changed files with 17 additions and 76 deletions

View file

@ -56,19 +56,18 @@ namespace {
#ifndef LIB_UI_USE_PACKAGED_FONTS #ifndef LIB_UI_USE_PACKAGED_FONTS
const auto FontTypes = std::array{ const auto FontTypes = std::array{
std::make_pair(u"OpenSans-Regular"_q, FontFlags()), u"OpenSans-Regular"_q,
std::make_pair(u"OpenSans-Italic"_q, FontItalic), u"OpenSans-Italic"_q,
std::make_pair(u"OpenSans-SemiBold"_q, FontSemibold), u"OpenSans-SemiBold"_q,
std::make_pair(u"OpenSans-SemiBoldItalic"_q, FontFlags(FontSemibold | FontItalic)), u"OpenSans-SemiBoldItalic"_q,
}; };
const auto PersianFontTypes = std::array{ const auto PersianFontTypes = std::array{
std::make_pair(u"Vazirmatn-UI-NL-Regular"_q, FontFlags()), u"Vazirmatn-UI-NL-Regular"_q,
std::make_pair(u"Vazirmatn-UI-NL-SemiBold"_q, FontSemibold), u"Vazirmatn-UI-NL-SemiBold"_q,
}; };
#endif // !LIB_UI_USE_PACKAGED_FONTS #endif // !LIB_UI_USE_PACKAGED_FONTS
bool Started = false; bool Started = false;
QString FontOverride;
QMap<QString, int> fontFamilyMap; QMap<QString, int> fontFamilyMap;
QVector<QString> fontFamilies; QVector<QString> fontFamilies;
@ -78,52 +77,19 @@ uint32 fontKey(int size, uint32 flags, int family) {
return (((uint32(family) << 12) | uint32(size)) << 6) | flags; return (((uint32(family) << 12) | uint32(size)) << 6) | flags;
} }
bool ValidateFont(const QString &familyName, int flags = 0) {
QFont checkFont(familyName);
checkFont.setWeight(((flags & FontBold) || (flags & FontSemibold))
? QFont::DemiBold
: QFont::Normal);
checkFont.setItalic(flags & FontItalic);
checkFont.setUnderline(flags & FontUnderline);
checkFont.setStrikeOut(flags & FontStrikeOut);
auto realFamily = QFontInfo(checkFont).family();
if (!realFamily.trimmed().startsWith(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;
}
#ifndef LIB_UI_USE_PACKAGED_FONTS #ifndef LIB_UI_USE_PACKAGED_FONTS
bool LoadCustomFont(const QString &filePath, const QString &familyName, int flags = 0) { bool LoadCustomFont(const QString &filePath) {
auto regularId = QFontDatabase::addApplicationFont(filePath); auto regularId = QFontDatabase::addApplicationFont(filePath);
if (regularId < 0) { if (regularId < 0) {
LOG(("Font Error: could not add '%1'.").arg(filePath)); LOG(("Font Error: could not add '%1'.").arg(filePath));
return false; return false;
} }
const auto found = [&] { for (auto &family : QFontDatabase::applicationFontFamilies(regularId)) {
for (auto &family : QFontDatabase::applicationFontFamilies(regularId)) { LOG(("Font: from '%1' loaded '%2'").arg(filePath, family));
LOG(("Font: from '%1' loaded '%2'").arg(filePath, family));
if (family.trimmed().startsWith(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); return true;
} }
#endif // !LIB_UI_USE_PACKAGED_FONTS #endif // !LIB_UI_USE_PACKAGED_FONTS
@ -173,7 +139,7 @@ bool LoadCustomFont(const QString &filePath, const QString &familyName, int flag
constexpr auto kMaxSizeShift = 6; constexpr auto kMaxSizeShift = 6;
const auto family = font.family(); const auto family = font.family();
const auto basic = GetFontOverride(flags); const auto basic = u"Open Sans"_q;
if (family == basic) { if (family == basic) {
return size; return size;
} }
@ -241,7 +207,7 @@ bool LoadCustomFont(const QString &filePath, const QString &familyName, int flag
} else if (overriden) { } else if (overriden) {
result.setFamily(family); result.setFamily(family);
} else { } else {
result.setFamily(GetFontOverride(flags)); result.setFamily("Open Sans"_q);
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
result.setFeature("ss03", true); result.setFeature("ss03", true);
#endif // Qt >= 6.7.0 #endif // Qt >= 6.7.0
@ -269,37 +235,17 @@ void StartFonts() {
style_InitFontsResource(); style_InitFontsResource();
#ifndef LIB_UI_USE_PACKAGED_FONTS #ifndef LIB_UI_USE_PACKAGED_FONTS
//[[maybe_unused]] auto badFlags = std::optional<int>();
const auto base = u":/gui/fonts/"_q; const auto base = u":/gui/fonts/"_q;
const auto name = u"Open Sans"_q; const auto name = u"Open Sans"_q;
const auto persianFallback = u"Vazirmatn UI NL"_q;
for (const auto &[file, flags] : FontTypes) { for (const auto &file : FontTypes) {
if (!LoadCustomFont(base + file + u".ttf"_q, name, flags)) { LoadCustomFont(base + file + u".ttf"_q);
//badFlags = flags;
}
} }
for (const auto &[file, flags] : PersianFontTypes) { for (const auto &file : PersianFontTypes) {
LoadCustomFont(base + file + u".ttf"_q, persianFallback, flags); LoadCustomFont(base + file + u".ttf"_q);
} }
QFont::insertSubstitution(name, persianFallback); QFont::insertSubstitution(name, u"Vazirmatn UI NL"_q);
#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 = u"Segoe UI"_q;
//if (badFlags && ValidateFont(fallback, *badFlags)) {
// FontOverride = fallback;
// LOG(("Fonts Info: Using '%1' instead of '%2'.").arg(fallback, name));
//}
// Disable default fallbacks to Segoe UI, see:
// https://github.com/telegramdesktop/tdesktop/issues/5368
//
//QFont::insertSubstitution(name, fallback);
#endif // Q_OS_WIN
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
const auto list = QStringList{ const auto list = QStringList{
@ -317,10 +263,6 @@ void StartFonts() {
QApplication::setFont(appFont); QApplication::setFont(appFont);
} }
QString GetFontOverride(int32 flags) {
return FontOverride.isEmpty() ? u"Open Sans"_q : FontOverride;
}
void destroyFonts() { void destroyFonts() {
for (auto fontData : std::as_const(fontsMap)) { for (auto fontData : std::as_const(fontsMap)) {
delete fontData; delete fontData;

View file

@ -26,7 +26,6 @@ void SetCustomFont(const QString &font);
namespace internal { namespace internal {
void StartFonts(); void StartFonts();
[[nodiscard]] QString GetFontOverride(int32 flags = 0);
void destroyFonts(); void destroyFonts();
int registerFontFamily(const QString &family); int registerFontFamily(const QString &family);