1
0
mirror of synced 2025-02-07 06:31:24 +01:00

impr: Better font scaling with larger backing scale factors

This commit is contained in:
WerWolv 2025-01-25 22:27:59 +01:00
parent 93e5d62782
commit 93f1f5d076
7 changed files with 38 additions and 24 deletions

View File

@ -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);

View File

@ -8,6 +8,7 @@
#include <hex/api/events/events_gui.hpp>
#include <hex/api/events/events_interaction.hpp>
#include <hex/api/events/requests_gui.hpp>
#include <hex/api/theme_manager.hpp>
#include <imgui.h>
#include <imgui_internal.h>
@ -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;
}
}

View File

@ -1,13 +1,12 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/theme_manager.hpp>
#include "window.hpp"
#if defined(OS_WINDOWS)
#include "messaging.hpp"
#include <hex/api/content_registry.hpp>
#include <hex/api/theme_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/default_paths.hpp>

View File

@ -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() {

View File

@ -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<std::pair<bool, float>> m_fontSizes;

View File

@ -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());

View File

@ -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);
});
}