From 44498825cd9a5bf8c3f2e7c5b4bb439b537fb91e Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 21 Mar 2025 19:13:23 +0100 Subject: [PATCH] (Breaking) Fonts: PushFont() default to preserve current font size. --- imgui.cpp | 11 ++++++++--- imgui.h | 12 +++++++++--- imgui_draw.cpp | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 334ace32c..20ebdc56a 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -8582,7 +8582,7 @@ void ImGui::UpdateFontsNewFrame() atlas->Locked = true; // We do this really unusual thing of calling *push_front()*, the reason behind that we want to support the PushFont()/NewFrame()/PopFont() idiom. - ImFontStackData font_stack_data = { ImGui::GetDefaultFont(), ImGui::GetDefaultFont()->Sources[0].SizePixels }; + ImFontStackData font_stack_data = { ImGui::GetDefaultFont(), ImGui::GetDefaultFont()->DefaultSize }; g.FontStack.push_front(font_stack_data); if (g.FontStack.Size == 1) ImGui::SetCurrentFont(font_stack_data.Font, font_stack_data.FontSize); @@ -8647,8 +8647,13 @@ void ImGui::PushFont(ImFont* font, float font_size) ImGuiContext& g = *GImGui; if (font == NULL) font = GetDefaultFont(); - if (font_size < 0.0f) - font_size = font->Sources[0].SizePixels; // g.FontSize; + if (font_size <= 0.0f) + { + if (font->Flags & ImFontFlags_UseDefaultSize) + font_size = font->DefaultSize; // Legacy: use default font size. Same as doing PushFont(font, font->DefaultSize). // FIXME-NEWATLAS + else + font_size = g.FontSizeBeforeScaling; // Keep current font size + } g.FontStack.push_back({ font, font_size }); SetCurrentFont(font, font_size); } diff --git a/imgui.h b/imgui.h index a04ac423a..5e1737a5d 100644 --- a/imgui.h +++ b/imgui.h @@ -491,9 +491,13 @@ namespace ImGui IMGUI_API void SetScrollFromPosY(float local_y, float center_y_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position. // Parameters stacks (font) - IMGUI_API void PushFont(ImFont* font, float font_size = -1); // use NULL as a shortcut to push default font. Use <0.0f to keep current font size. + // *IMPORTANT* before 1.92, fonts had a single size. They can now be dynamically be adjusted. + // - Before 1.92: PushFont() always used font default size. + // - Since 1.92: PushFont() preserve the current shared font size. + // - To use old behavior: use 'PushFont(font, font->DefaultSize)' in call site, or set 'ImFontConfig::Flags |= ImFontFlags_UseDefaultSize' before calling AddFont(). + IMGUI_API void PushFont(ImFont* font, float font_size = -1); // use NULL as a shortcut to push default font. Use <0.0f to keep current font size. Use font->DefaultSize to revert to font default size. IMGUI_API void PopFont(); - IMGUI_API void PushFontSize(float size); + IMGUI_API void PushFontSize(float font_size); IMGUI_API void PopFontSize(); // Parameters stacks (shared) @@ -3709,6 +3713,7 @@ enum ImFontFlags_ ImFontFlags_LockBakedSizes = 1 << 0, // Disable loading new baked sizes, disable garbage collecting current ones. e.g. if you want to lock a font to a single size. ImFontFlags_NoLoadGlyphs = 1 << 1, // Disable loading new glyphs. ImFontFlags_NoLoadError = 1 << 2, // Disable throwing an error/assert when calling AddFontXXX() with missing file/data. Calling code is expected to check AddFontXXX() return value. + ImFontFlags_UseDefaultSize = 1 << 3, // Legacy compatibility: make PushFont() calls without explicit size use font->DefaultSize instead of current font size. }; // Font runtime data and rendering @@ -3726,6 +3731,7 @@ struct ImFont // [Internal] Members: Cold ~24-52 bytes // Conceptually Sources[] is the list of font sources merged to create this font. ImGuiID FontId; // Unique identifier for the font + float DefaultSize; // 4 // in // Default font size short SourcesCount; // 2 // in // Number of ImFontConfig involved in creating this font. Usually 1, or >1 when merging multiple font sources into one ImFont. ImFontConfig* Sources; // 4-8 // in // Pointer within ContainerAtlas->Sources[], to SourcesCount instances ImWchar EllipsisChar; // 2-4 // out // Character used for ellipsis rendering ('...'). @@ -3750,7 +3756,7 @@ struct ImFont IMGUI_API void RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c, const ImVec4* cpu_fine_clip = NULL); IMGUI_API void RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) { return CalcWordWrapPosition(Sources[0].SizePixels * scale, text, text_end, wrap_width); } + inline const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) { return CalcWordWrapPosition(DefaultSize * scale, text, text_end, wrap_width); } #endif // [Internal] Don't use! diff --git a/imgui_draw.cpp b/imgui_draw.cpp index db95fd14d..4a7bff240 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -3005,6 +3005,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) font = IM_NEW(ImFont)(); font->FontId = FontNextUniqueID++; font->Flags = font_cfg->Flags; + font->DefaultSize = font_cfg->SizePixels; Fonts.push_back(font); } else @@ -3248,7 +3249,7 @@ int ImFontAtlas::AddCustomRectRegular(int width, int height) // myfont->Flags |= ImFontFlags_LockBakedSizes; int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar codepoint, int width, int height, float advance_x, const ImVec2& offset) { - float font_size = font->Sources[0].SizePixels; + float font_size = font->DefaultSize; return AddCustomRectFontGlyphForSize(font, font_size, codepoint, width, height, advance_x, offset); } // FIXME: we automatically set glyph.Colored=true by default.