From d944b4e4ef94c7785bb987ab68d360cd0119b97a Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 23 Apr 2024 10:20:30 +0400 Subject: [PATCH] Try disabling manual Segoe UI fallback. --- ui/style/style_core_font.cpp | 90 ++++++++++++++++++++++++++++-------- ui/style/style_core_font.h | 8 ++-- 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/ui/style/style_core_font.cpp b/ui/style/style_core_font.cpp index 97b559c..cabf6f6 100644 --- a/ui/style/style_core_font.cpp +++ b/ui/style/style_core_font.cpp @@ -38,12 +38,12 @@ void style_InitFontsResource() { namespace style { namespace { -Font CustomFont; +CustomFont Custom; } // namespace -void SetCustomFont(const Font &font) { - CustomFont = font; +void SetCustomFont(const CustomFont &font) { + Custom = font; } namespace internal { @@ -144,7 +144,7 @@ bool LoadCustomFont(const QString &filePath, const QString &familyName, int flag return QString(); } -QString MonospaceFont() { +[[nodiscard]] QString MonospaceFont() { static const auto family = [&]() -> QString { const auto manual = ManualMonospaceFont(); const auto system = SystemMonospaceFont(); @@ -164,15 +164,64 @@ QString MonospaceFont() { return family; } -QFont ResolveFont(const QString &familyOverride, uint32 flags, int size) { +[[nodiscard]] int ComputePixelSize(QFont font, uint32 flags, int size) { + const auto family = font.family(); + const auto basic = GetFontOverride(flags); + if (family == basic) { + return size; + } + auto copy = font; + copy.setFamily(basic); + const auto desired = QFontMetricsF(copy).capHeight(); + if (desired < 1.) { + return size; + } + font.setPixelSize(size); + auto current = QFontMetricsF(font).capHeight(); + constexpr auto kMaxSizeShift = 4; + if (current < 1. || std::abs(current - desired) < 0.2) { + return size; + } else if (current < desired) { + for (auto i = 0; i != kMaxSizeShift; ++i) { + const auto shift = i + 1; + font.setPixelSize(size + shift); + const auto now = QFontMetricsF(font).capHeight(); + if (now > desired) { + return (now - desired * 2 < desired - current) + ? (size + shift) + : (size + shift - 1); + } + current = now; + } + return size + kMaxSizeShift; + } else { + for (auto i = 0; i != kMaxSizeShift; ++i) { + const auto shift = i + 1; + font.setPixelSize(size - shift); + const auto now = QFontMetricsF(font).capHeight(); + if (now < desired) { + return (desired - now * 2 < current - desired) + ? (size - shift) + : (size - shift + 1); + } + current = now; + } + return size - kMaxSizeShift; + } +} + +[[nodiscard]] QFont ResolveFont( + const QString &familyOverride, + uint32 flags, + int size) { auto result = QFont(); if (!familyOverride.isEmpty()) { result.setFamily(familyOverride); } else if (flags & FontMonospace) { result.setFamily(MonospaceFont()); - } else if (v::is(CustomFont)) { - result.setFamily(v::get(CustomFont)); - } else if (!v::is(CustomFont)) { + } else if (const auto name = std::get_if(&Custom)) { + result.setFamily(*name); + } else if (!v::is(Custom)) { result.setFamily(GetFontOverride(flags)); #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) result.setFeature("ss03", true); @@ -184,7 +233,9 @@ QFont ResolveFont(const QString &familyOverride, uint32 flags, int size) { result.setItalic(flags & FontItalic); result.setUnderline(flags & FontUnderline); result.setStrikeOut(flags & FontStrikeOut); - result.setPixelSize(size); + result.setPixelSize(familyOverride.isEmpty() + ? ComputePixelSize(result, flags, size) + : size); return result; } @@ -199,18 +250,19 @@ void StartFonts() { style_InitFontsResource(); #ifndef LIB_UI_USE_PACKAGED_FONTS - [[maybe_unused]] bool good = true; + //[[maybe_unused]] auto badFlags = std::optional(); + 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(u":/gui/fonts/"_q + file + u".ttf"_q, name, flags)) { - good = false; + if (!LoadCustomFont(base + file + u".ttf"_q, name, flags)) { + //badFlags = flags; } } for (const auto &[file, flags] : PersianFontTypes) { - LoadCustomFont(u":/gui/fonts/"_q + file + u".ttf"_q, persianFallback, flags); + LoadCustomFont(base + file + u".ttf"_q, persianFallback, flags); } QFont::insertSubstitution(name, persianFallback); @@ -219,13 +271,11 @@ void StartFonts() { // 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 (!good) { - if (ValidateFont(fallback, flags)) { - FontOverride = fallback; - LOG(("Fonts Info: Using '%1' instead of '%2'.").arg(fallback, name)); - } - } + //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 // diff --git a/ui/style/style_core_font.h b/ui/style/style_core_font.h index c575617..c6f7c8e 100644 --- a/ui/style/style_core_font.h +++ b/ui/style/style_core_font.h @@ -11,13 +11,15 @@ #include #include +#include #include namespace style { -struct SystemFont {}; -using Font = std::variant; -void SetCustomFont(const Font &font); +struct SystemFont{ +}; +using CustomFont = std::variant; +void SetCustomFont(const CustomFont &font); namespace internal {