From 3c27c643a9c79f2248aee53336c754749b8622e5 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 5 Jun 2025 14:40:37 +0200 Subject: [PATCH] Fonts: internals: renamed g.FontScale to g.FontBakedScale for clarity. Comments. --- imgui.cpp | 20 ++++++++++---------- imgui.h | 11 ++++++----- imgui_internal.h | 8 ++++---- imgui_tables.cpp | 2 +- imgui_widgets.cpp | 4 ++-- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index f6dd39c9d..3fb75eb25 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3979,7 +3979,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas) Initialized = false; Font = NULL; FontBaked = NULL; - FontSize = FontSizeBeforeScaling = FontScale = CurrentDpiScale = 0.0f; + FontSize = FontSizeBeforeScaling = FontBakedScale = CurrentDpiScale = 0.0f; FontRasterizerDensity = 1.0f; IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); if (shared_font_atlas == NULL) @@ -8773,9 +8773,9 @@ void ImGui::UpdateCurrentFontSize(float restore_font_size_after_scaling) g.Font->CurrentRasterizerDensity = g.FontRasterizerDensity; g.FontSize = final_size; g.FontBaked = (g.Font != NULL && window != NULL) ? g.Font->GetFontBaked(final_size) : NULL; - g.FontScale = (g.Font != NULL && window != NULL) ? (g.FontSize / g.FontBaked->Size) : 0.0f; + g.FontBakedScale = (g.Font != NULL && window != NULL) ? (g.FontSize / g.FontBaked->Size) : 0.0f; g.DrawListSharedData.FontSize = g.FontSize; - g.DrawListSharedData.FontScale = g.FontScale; + g.DrawListSharedData.FontScale = g.FontBakedScale; } // Exposed in case user may want to override setting density. @@ -8793,20 +8793,20 @@ void ImGui::SetFontRasterizerDensity(float rasterizer_density) // If you want to scale an existing font size: // - Use e.g. PushFontSize(style.FontSizeBase * factor) (= value before external scale factors applied). // - Do NOT use PushFontSize(GetFontSize() * factor) (= value after external scale factors applied). -void ImGui::PushFont(ImFont* font, float font_size) +void ImGui::PushFont(ImFont* font, float font_size_base) { ImGuiContext& g = *GImGui; g.FontStack.push_back({ g.Font, g.FontSizeBeforeScaling, g.FontSize }); if (font == NULL) font = GetDefaultFont(); - if (font_size <= 0.0f) + if (font_size_base <= 0.0f) { if (font->Flags & ImFontFlags_DefaultToLegacySize) - font_size = font->LegacySize; // Legacy: use AddFont() specified font size. Same as doing PushFont(font, font->LegacySize) + font_size_base = font->LegacySize; // Legacy: use AddFont() specified font size. Same as doing PushFont(font, font->LegacySize) else - font_size = g.FontSizeBeforeScaling; // Keep current font size + font_size_base = g.FontSizeBeforeScaling; // Keep current font size } - SetCurrentFont(font, font_size, 0.0f); + SetCurrentFont(font, font_size_base, 0.0f); } void ImGui::PopFont() @@ -8822,10 +8822,10 @@ void ImGui::PopFont() g.FontStack.pop_back(); } -void ImGui::PushFontSize(float font_size) +void ImGui::PushFontSize(float font_size_base) { ImGuiContext& g = *GImGui; - PushFont(g.Font, font_size); + PushFont(g.Font, font_size_base); } void ImGui::PopFontSize() diff --git a/imgui.h b/imgui.h index 3b69ac1ff..563d2eb92 100644 --- a/imgui.h +++ b/imgui.h @@ -500,12 +500,13 @@ namespace ImGui // - To use old behavior (single size font, size specified in AddFontXXX() call: // - Use 'PushFont(font, font->LegacySize)' at call site // - Or set 'ImFontConfig::Flags |= ImFontFlags_DefaultToLegacySize' before calling AddFont(), and then 'PushFont(font)' will use this size. + // - External scale factors are applied over the provided value. // *IMPORTANT* If you want to scale an existing font size: // - OK: PushFontSize(style.FontSizeBase * factor) (= value before external scale factors applied). // - KO: PushFontSize(GetFontSize() * factor) (= value after external scale factors applied. external scale factors are style.FontScaleMain + per-viewport scales.). - 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. + IMGUI_API void PushFont(ImFont* font, float font_size_base = -1); // use NULL as a shortcut to push default font. Use <0.0f to keep current font size. IMGUI_API void PopFont(); - IMGUI_API void PushFontSize(float font_size); + IMGUI_API void PushFontSize(float font_size_base); IMGUI_API void PopFontSize(); // Parameters stacks (shared) @@ -2226,9 +2227,9 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE struct ImGuiStyle { - float FontSizeBase; // Current base font size (scaling applied). Use PushFont()/PushFontSize() to modify. Use ImGui::GetFontSize() to obtain scaled value. - float FontScaleMain; // Main global scale factor. Other scale factors may apply. - float FontScaleDpi; // Scale factor from viewport/monitor. When io.ConfigDpiScaleFonts is enabled, this is automatically overwritten when changing monitor. + float FontSizeBase; // Current base font size before external scaling factors are applied. Use PushFont()/PushFontSize() to modify. Use ImGui::GetFontSize() to obtain scaled value. Final FontSize = FontSizeBase * (FontScaleBase * FontScaleDpi * other_factors) + float FontScaleMain; // Main scale factor. May be set by application once, or exposed to end-user. + float FontScaleDpi; // Scale factor from viewport/monitor contents scale. When io.ConfigDpiScaleFonts is enabled, this is automatically overwritten when changing monitor. float Alpha; // Global alpha applies to everything in Dear ImGui. float DisabledAlpha; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. diff --git a/imgui_internal.h b/imgui_internal.h index f4db19273..db308bc7a 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -871,8 +871,8 @@ struct ImDrawDataBuilder struct ImFontStackData { ImFont* Font; - float FontSizeBeforeScaling; - float FontSizeAfterScaling; + float FontSizeBeforeScaling; // ~~ style.FontSizeBase + float FontSizeAfterScaling; // ~~ g.FontSize }; //----------------------------------------------------------------------------- @@ -2140,8 +2140,8 @@ struct ImGuiContext ImFont* Font; // Currently bound font. (== FontStack.back().Font) ImFontBaked* FontBaked; // Currently bound font at currently bound size. (== Font->GetFontBaked(FontSize)) float FontSize; // Currently bound font size == line height (== FontSizeBeforeScaling + externals scales applied in the UpdateCurrentFontSize() function). - float FontSizeBeforeScaling; // == value passed to PushFont() / PushFontSize() when specified. - float FontScale; // == FontBaked->Size / Font->FontSize. Scale factor over baked size. + float FontSizeBeforeScaling; // Font size before scaling == style.FontSizeBase == value passed to PushFont() / PushFontSize() when specified. + float FontBakedScale; // == FontBaked->Size / FontSize. Scale factor over baked size. Rarely used nowadays, very often == 1.0f. float FontRasterizerDensity; // Current font density. Used by all calls to GetFontBaked(). float CurrentDpiScale; // Current window/viewport DpiScale == CurrentViewport->DpiScale ImDrawListSharedData DrawListSharedData; diff --git a/imgui_tables.cpp b/imgui_tables.cpp index e41041ce1..61bc576ca 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -3375,7 +3375,7 @@ void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label ButtonBehavior(row_r, row_id, NULL, NULL); KeepAliveID(row_id); - const float ascent_scaled = g.FontBaked->Ascent * g.FontScale; // FIXME: Standardize those scaling factors better + const float ascent_scaled = g.FontBaked->Ascent * g.FontBakedScale; // FIXME: Standardize those scaling factors better const float line_off_for_ascent_x = (ImMax((g.FontSize - ascent_scaled) * 0.5f, 0.0f) / -sin_a) * (flip_label ? -1.0f : 1.0f); const ImVec2 padding = g.Style.CellPadding; // We will always use swapped component const ImVec2 align = g.Style.TableAngledHeadersTextAlign; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 91d50933f..cdca228c4 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -1518,7 +1518,7 @@ bool ImGui::TextLink(const char* label) ColorConvertHSVtoRGB(h, s, v, line_colf.x, line_colf.y, line_colf.z); } - float line_y = bb.Max.y + ImFloor(g.FontBaked->Descent * g.FontScale * 0.20f); + float line_y = bb.Max.y + ImFloor(g.FontBaked->Descent * g.FontBakedScale * 0.20f); window->DrawList->AddLine(ImVec2(bb.Min.x, line_y), ImVec2(bb.Max.x, line_y), GetColorU32(line_colf)); // FIXME-TEXT: Underline mode // FIXME-DPI PushStyleColor(ImGuiCol_Text, GetColorU32(text_colf)); @@ -4012,7 +4012,7 @@ namespace ImStb { static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->TextLen; } static char STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->TextLen); return obj->TextSrc[idx]; } -static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { unsigned int c; ImTextCharFromUtf8(&c, obj->TextSrc + line_start_idx + char_idx, obj->TextSrc + obj->TextLen); if ((ImWchar)c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.FontBaked->GetCharAdvance((ImWchar)c) * g.FontScale; } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { unsigned int c; ImTextCharFromUtf8(&c, obj->TextSrc + line_start_idx + char_idx, obj->TextSrc + obj->TextLen); if ((ImWchar)c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.FontBaked->GetCharAdvance((ImWchar)c) * g.FontBakedScale; } static char STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) {