Get rid of no more used GetFontOverride
This commit is contained in:
parent
553d5509b5
commit
17cc6f3783
2 changed files with 17 additions and 76 deletions
|
|
@ -56,19 +56,18 @@ namespace {
|
|||
|
||||
#ifndef LIB_UI_USE_PACKAGED_FONTS
|
||||
const auto FontTypes = std::array{
|
||||
std::make_pair(u"OpenSans-Regular"_q, FontFlags()),
|
||||
std::make_pair(u"OpenSans-Italic"_q, FontItalic),
|
||||
std::make_pair(u"OpenSans-SemiBold"_q, FontSemibold),
|
||||
std::make_pair(u"OpenSans-SemiBoldItalic"_q, FontFlags(FontSemibold | FontItalic)),
|
||||
u"OpenSans-Regular"_q,
|
||||
u"OpenSans-Italic"_q,
|
||||
u"OpenSans-SemiBold"_q,
|
||||
u"OpenSans-SemiBoldItalic"_q,
|
||||
};
|
||||
const auto PersianFontTypes = std::array{
|
||||
std::make_pair(u"Vazirmatn-UI-NL-Regular"_q, FontFlags()),
|
||||
std::make_pair(u"Vazirmatn-UI-NL-SemiBold"_q, FontSemibold),
|
||||
u"Vazirmatn-UI-NL-Regular"_q,
|
||||
u"Vazirmatn-UI-NL-SemiBold"_q,
|
||||
};
|
||||
#endif // !LIB_UI_USE_PACKAGED_FONTS
|
||||
|
||||
bool Started = false;
|
||||
QString FontOverride;
|
||||
|
||||
QMap<QString, int> fontFamilyMap;
|
||||
QVector<QString> fontFamilies;
|
||||
|
|
@ -78,52 +77,19 @@ 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.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
|
||||
bool LoadCustomFont(const QString &filePath, const QString &familyName, int flags = 0) {
|
||||
bool LoadCustomFont(const QString &filePath) {
|
||||
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().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
|
||||
|
||||
|
|
@ -173,7 +139,7 @@ bool LoadCustomFont(const QString &filePath, const QString &familyName, int flag
|
|||
constexpr auto kMaxSizeShift = 6;
|
||||
|
||||
const auto family = font.family();
|
||||
const auto basic = GetFontOverride(flags);
|
||||
const auto basic = u"Open Sans"_q;
|
||||
if (family == basic) {
|
||||
return size;
|
||||
}
|
||||
|
|
@ -241,7 +207,7 @@ bool LoadCustomFont(const QString &filePath, const QString &familyName, int flag
|
|||
} else if (overriden) {
|
||||
result.setFamily(family);
|
||||
} else {
|
||||
result.setFamily(GetFontOverride(flags));
|
||||
result.setFamily("Open Sans"_q);
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
|
||||
result.setFeature("ss03", true);
|
||||
#endif // Qt >= 6.7.0
|
||||
|
|
@ -269,37 +235,17 @@ void StartFonts() {
|
|||
style_InitFontsResource();
|
||||
|
||||
#ifndef LIB_UI_USE_PACKAGED_FONTS
|
||||
//[[maybe_unused]] auto badFlags = std::optional<int>();
|
||||
const auto base = u":/gui/fonts/"_q;
|
||||
const auto name = u"Open Sans"_q;
|
||||
const auto persianFallback = u"Vazirmatn UI NL"_q;
|
||||
|
||||
for (const auto &[file, flags] : FontTypes) {
|
||||
if (!LoadCustomFont(base + file + u".ttf"_q, name, flags)) {
|
||||
//badFlags = flags;
|
||||
}
|
||||
for (const auto &file : FontTypes) {
|
||||
LoadCustomFont(base + file + u".ttf"_q);
|
||||
}
|
||||
|
||||
for (const auto &[file, flags] : PersianFontTypes) {
|
||||
LoadCustomFont(base + file + u".ttf"_q, persianFallback, flags);
|
||||
for (const auto &file : PersianFontTypes) {
|
||||
LoadCustomFont(base + file + u".ttf"_q);
|
||||
}
|
||||
QFont::insertSubstitution(name, persianFallback);
|
||||
|
||||
#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
|
||||
QFont::insertSubstitution(name, u"Vazirmatn UI NL"_q);
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
const auto list = QStringList{
|
||||
|
|
@ -317,10 +263,6 @@ void StartFonts() {
|
|||
QApplication::setFont(appFont);
|
||||
}
|
||||
|
||||
QString GetFontOverride(int32 flags) {
|
||||
return FontOverride.isEmpty() ? u"Open Sans"_q : FontOverride;
|
||||
}
|
||||
|
||||
void destroyFonts() {
|
||||
for (auto fontData : std::as_const(fontsMap)) {
|
||||
delete fontData;
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ void SetCustomFont(const QString &font);
|
|||
namespace internal {
|
||||
|
||||
void StartFonts();
|
||||
[[nodiscard]] QString GetFontOverride(int32 flags = 0);
|
||||
|
||||
void destroyFonts();
|
||||
int registerFontFamily(const QString &family);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue