From 3aba95060ec7960e2c1c7d65163756b9d9c1e419 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 21 Jan 2026 19:53:31 +0100 Subject: [PATCH] Fonts: adding new fonts after removing all fonts mid-frame properly updates current state. --- docs/CHANGELOG.txt | 1 + imgui_draw.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 4324c69ef..63235b2c2 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -128,6 +128,7 @@ Other Changes: is enabled, creating side-effects when later disabling hinting or dynamically switching to stb_truetype rasterizer. - Post rescale GlyphOffset is always rounded. + - Adding new fonts after removing all fonts mid-frame properly updates current state. - Textures: - Fixed a building issue when ImTextureID is defined as a struct. - Fixed displaying texture # in Metrics/Debugger window. diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 780f6b844..9889b2884 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -3035,6 +3035,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in) ImFontAtlasBuildInit(this); // Create new font + const bool is_first_font = (Fonts.Size == 0); ImFont* font; if (!font_cfg_in->MergeMode) { @@ -3092,6 +3093,8 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg_in) } ImFontAtlasFontSourceAddToFont(this, font, font_cfg); + if (is_first_font) + ImFontAtlasBuildNotifySetFont(this, NULL, font); return font; } @@ -3254,6 +3257,9 @@ void ImFontAtlasBuildNotifySetFont(ImFontAtlas* atlas, ImFont* old_font, ImFont* shared_data->Font = new_font; if (ImGuiContext* ctx = shared_data->Context) { + if (ctx->FrameCount == 0 && old_font == NULL) // While this should work either way, we save ourselves the bother / debugging confusion of running ImGui code so early when it is not needed. + continue; + if (ctx->IO.FontDefault == old_font) ctx->IO.FontDefault = new_font; if (ctx->Font == old_font)