From 93f1f5d07609c7a4eacddbd994654cd3aa15ffa7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 25 Jan 2025 22:27:59 +0100 Subject: [PATCH] impr: Better font scaling with larger backing scale factors --- main/gui/source/init/splash_window.cpp | 5 ----- main/gui/source/window/web_window.cpp | 22 ++++++++++++++++++++++ main/gui/source/window/win_window.cpp | 7 +++---- main/gui/source/window/window.cpp | 2 -- plugins/fonts/include/font_atlas.hpp | 18 +++++++++--------- plugins/fonts/source/font_loader.cpp | 2 +- plugins/fonts/source/library_fonts.cpp | 6 +++--- 7 files changed, 38 insertions(+), 24 deletions(-) diff --git a/main/gui/source/init/splash_window.cpp b/main/gui/source/init/splash_window.cpp index 2d1cab084..4ed8fe493 100644 --- a/main/gui/source/init/splash_window.cpp +++ b/main/gui/source/init/splash_window.cpp @@ -484,11 +484,6 @@ namespace hex::init { meanScale /= hex::ImHexApi::System::getBackingScaleFactor(); - // Force native scale factor to 1.0 on web builds - #if defined(OS_WEB) - meanScale = 1.0F; - #endif - ImHexApi::System::impl::setGlobalScale(meanScale); ImHexApi::System::impl::setNativeScale(meanScale); diff --git a/main/gui/source/window/web_window.cpp b/main/gui/source/window/web_window.cpp index 0babc5f94..032d4934d 100644 --- a/main/gui/source/window/web_window.cpp +++ b/main/gui/source/window/web_window.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -154,6 +155,27 @@ namespace hex { } void Window::endNativeWindowFrame() { + static float prevScaleFactor = 0; + + const float currScaleFactor = MAIN_THREAD_EM_ASM_DOUBLE({ + try { + // Take square root of scaling to counter scaling applied by Browser + return Math.sqrt(window.devicePixelRatio); + } catch (e) { + return 1.0; + } + }); + if (prevScaleFactor != 0 && prevScaleFactor != currScaleFactor) { + EventDPIChanged::post(prevScaleFactor, currScaleFactor); + resizeCanvas(); + + ImHexApi::System::impl::setNativeScale(currScaleFactor); + + ThemeManager::reapplyCurrentTheme(); + ImGui::GetStyle().ScaleAllSizes(currScaleFactor); + } + + prevScaleFactor = currScaleFactor; } } diff --git a/main/gui/source/window/win_window.cpp b/main/gui/source/window/win_window.cpp index 34e1c6312..2e767ee93 100644 --- a/main/gui/source/window/win_window.cpp +++ b/main/gui/source/window/win_window.cpp @@ -1,13 +1,12 @@ -#include -#include - #include "window.hpp" - #if defined(OS_WINDOWS) #include "messaging.hpp" + #include + #include + #include #include #include diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 7623a157c..f8b0c995c 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -813,8 +813,6 @@ namespace hex { // NOTE: This needs to be done before a new frame is started, otherwise ImGui won't handle docking correctly LayoutManager::process(); WorkspaceManager::process(); - - ImGui::GetIO().FontGlobalScale = 1.0F / ImHexApi::System::getBackingScaleFactor(); } void Window::drawImGui() { diff --git a/plugins/fonts/include/font_atlas.hpp b/plugins/fonts/include/font_atlas.hpp index 0736cb001..ac8ea64d4 100644 --- a/plugins/fonts/include/font_atlas.hpp +++ b/plugins/fonts/include/font_atlas.hpp @@ -24,6 +24,8 @@ namespace hex::fonts { return m_font->Descent; } + ImFont* getFont() { return m_font; } + private: explicit Font(ImFont *font) : m_font(font) { } @@ -62,9 +64,12 @@ namespace hex::fonts { Font addDefaultFont() { auto &config = m_fontConfigs.emplace_back(m_defaultConfig); config.FontBuilderFlags |= ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting; - config.SizePixels = std::floor(getAdjustedFontSize(ImHexApi::System::getGlobalScale() * 13.0F)); + config.SizePixels = std::max(1.0F, std::floor(ImHexApi::System::getGlobalScale() * 13.0F)); auto font = m_fontAtlas->AddFontDefault(&config); + + font->Scale = 1.0 / std::floor(ImHexApi::System::getBackingScaleFactor()); + m_fontSizes.emplace_back(false, config.SizePixels); m_defaultConfig.MergeMode = true; @@ -79,7 +84,9 @@ namespace hex::fonts { config.FontDataOwnedByAtlas = false; config.GlyphOffset = { offset.x, offset.y }; - auto font = m_fontAtlas->AddFontFromMemoryTTF(storedFontData.data(), int(storedFontData.size()), getAdjustedFontSize(fontSize), &config, !glyphRange.empty() ? glyphRange.Data : m_glyphRange.Data); + auto font = m_fontAtlas->AddFontFromMemoryTTF(storedFontData.data(), int(storedFontData.size()), fontSize, &config, !glyphRange.empty() ? glyphRange.Data : m_glyphRange.Data); + font->Scale = 1.0 / ImHexApi::System::getBackingScaleFactor(); + m_fontSizes.emplace_back(scalable, fontSize); m_defaultConfig.MergeMode = true; @@ -209,13 +216,6 @@ namespace hex::fonts { } } - float getAdjustedFontSize(float fontSize) const { - // Since macOS reports half the framebuffer size that's actually available, - // we'll multiply all font sizes by that and then divide the global font scale - // by the same amount to get super crisp font rendering. - return fontSize * hex::ImHexApi::System::getBackingScaleFactor(); - } - private: ImFontAtlas* m_fontAtlas = nullptr; std::vector> m_fontSizes; diff --git a/plugins/fonts/source/font_loader.cpp b/plugins/fonts/source/font_loader.cpp index 11255e10a..91eb72216 100644 --- a/plugins/fonts/source/font_loader.cpp +++ b/plugins/fonts/source/font_loader.cpp @@ -55,8 +55,8 @@ namespace hex::fonts { // If there's no custom font set, or it failed to load, fall back to the default font if (!defaultFont.has_value()) { if (pixelPerfectFont) { + fontSize = std::max(1.0F, std::floor(ImHexApi::System::getGlobalScale() * 13.0F)); defaultFont = fontAtlas->addDefaultFont(); - fontSize = std::floor(fontAtlas->getAdjustedFontSize(ImHexApi::System::getGlobalScale() * 13.0F)); } else defaultFont = fontAtlas->addFontFromRomFs("fonts/JetBrainsMono.ttf", fontSize, true, ImVec2()); diff --git a/plugins/fonts/source/library_fonts.cpp b/plugins/fonts/source/library_fonts.cpp index bfa1b29dc..07dde9139 100644 --- a/plugins/fonts/source/library_fonts.cpp +++ b/plugins/fonts/source/library_fonts.cpp @@ -65,12 +65,12 @@ namespace hex::fonts { return; } - loadFont(widget, name, &font, ImHexApi::System::getGlobalScale()); + loadFont(widget, name, &font, ImHexApi::System::getGlobalScale() * ImHexApi::System::getBackingScaleFactor()); }); - loadFont(widget.getWidget(), name, &font, ImHexApi::System::getGlobalScale()); + loadFont(widget.getWidget(), name, &font, ImHexApi::System::getGlobalScale() * ImHexApi::System::getBackingScaleFactor()); EventDPIChanged::subscribe(font, [&widget, name, &font](float, float newScaling) { - loadFont(widget.getWidget(), name, &font, newScaling); + loadFont(widget.getWidget(), name, &font, ImHexApi::System::getGlobalScale() * newScaling); }); }