Browse Source

Fonts: tidying up font scale logic.

# Conflicts:
#	imgui_internal.h
ocornut 6 months ago
parent
commit
2bf6879dae
2 changed files with 28 additions and 24 deletions
  1. 22 21
      imgui.cpp
  2. 6 3
      imgui_internal.h

+ 22 - 21
imgui.cpp

@@ -3942,7 +3942,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
     FontAtlasOwnedByContext = shared_font_atlas ? false : true;
     Font = NULL;
     FontBaked = NULL;
-    FontSize = /*FontBaseSize = */FontScale = CurrentDpiScale = 0.0f;
+    FontSize = FontSizeBeforeScaling = FontScale = CurrentDpiScale = 0.0f;
     IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
     IO.Fonts->RefCount++;
     Time = 0.0f;
@@ -4375,9 +4375,7 @@ static void SetCurrentWindow(ImGuiWindow* window)
     g.CurrentDpiScale = 1.0f; // FIXME-DPI: WIP this is modified in docking
     if (window)
     {
-        // FIXME-BAKED
-        //g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
-        //g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize;
+        ImGui::UpdateCurrentFontSize();
         ImGui::NavUpdateCurrentWindowIsScrollPushableX();
     }
 }
@@ -8406,14 +8404,9 @@ ImVec2 ImGui::GetFontTexUvWhitePixel()
 void ImGui::SetWindowFontScale(float scale)
 {
     IM_ASSERT(scale > 0.0f);
-    // FIXME-BAKED
-    /*
-    ImGuiContext& g = *GImGui;
     ImGuiWindow* window = GetCurrentWindow();
     window->FontWindowScale = scale;
-    g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize();
-    g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize;
-    */
+    UpdateCurrentFontSize();
 }
 
 void ImGui::PushFocusScope(ImGuiID id)
@@ -8608,26 +8601,34 @@ void ImGui::SetCurrentFont(ImFont* font, float font_size)
 {
     ImGuiContext& g = *GImGui;
     g.Font = font;
-    //g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.FontBaked->Size * g.Font->Scale);
-    g.FontSize = font_size;// g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f;
+    g.FontSizeBeforeScaling = font_size;
+    UpdateCurrentFontSize();
+
     if (font != NULL)
     {
         IM_ASSERT(font && font->IsLoaded());    // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ?
         IM_ASSERT(font->Scale > 0.0f);
-        g.FontBaked = g.Font->GetFontBaked(g.FontSize);
-        g.FontScale = g.FontSize / g.FontBaked->Size;
         g.DrawListSharedData.Font = g.Font;
-        g.DrawListSharedData.FontSize = g.FontSize;
-        g.DrawListSharedData.FontScale = g.FontScale;
         ImFontAtlasUpdateDrawListsSharedData(g.Font->ContainerAtlas);
         if (g.CurrentWindow != NULL)
             g.CurrentWindow->DrawList->_SetTexture(font->ContainerAtlas->TexRef);
     }
-    else
-    {
-        g.FontBaked = NULL;
-        g.FontScale = 0.0f;
-    }
+}
+
+void ImGui::UpdateCurrentFontSize()
+{
+    ImGuiContext& g = *GImGui;
+    float final_size = g.FontSizeBeforeScaling * g.IO.FontGlobalScale;
+    final_size *= g.Font->Scale;
+    if (ImGuiWindow* window = g.CurrentWindow)
+        final_size *= window->FontWindowScale;
+    final_size = ImMax(1.0f, IM_ROUND(final_size));
+
+    g.FontSize = final_size;
+    g.FontBaked = (g.Font != NULL) ? g.Font->GetFontBaked(g.FontSize) : NULL;
+    g.FontScale = (g.Font != NULL) ? (g.FontSize / g.FontBaked->Size) : 0.0f;
+    g.DrawListSharedData.FontSize = g.FontSize;
+    g.DrawListSharedData.FontScale = g.FontScale;
 }
 
 void ImGui::PushFont(ImFont* font, float font_size)

+ 6 - 3
imgui_internal.h

@@ -2138,8 +2138,8 @@ struct ImGuiContext
     ImGuiStyle              Style;
     ImFont*                 Font;                               // == FontStack.back().Font
     ImFontBaked*            FontBaked;                          // == Font->GetFontBaked(FontSize)
-    float                   FontSize;                           // == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window.
-    //float                 FontBaseSize;                       // == io.FontGlobalScale * Font->Scale * Font->FontSize. Base text height.
+    float                   FontSize;                           // == FontSizeBeforeScaling * io.FontGlobalScale * font->Scale * g.CurrentWindow->FontWindowScale. Current text height.
+    float                   FontSizeBeforeScaling;              // == value passed to PushFontSize()
     float                   FontScale;                          // == FontBaked->Size / Font->FontSize. Scale factor over baked size.
     float                   CurrentDpiScale;                    // Current window/viewport DpiScale == CurrentViewport->DpiScale
     ImDrawListSharedData    DrawListSharedData;
@@ -2680,9 +2680,11 @@ public:
 
     // We don't use g.FontSize because the window may be != g.CurrentWindow.
     ImRect      Rect() const            { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); }
-    //float     CalcFontSize() const    { ImGuiContext& g = *Ctx; return g.FontBaseSize * FontWindowScale * FontWindowScaleParents; }
     ImRect      TitleBarRect() const    { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight)); }
     ImRect      MenuBarRect() const     { float y1 = Pos.y + TitleBarHeight; return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight); }
+
+    // [Obsolete] ImGuiWindow::CalcFontSize() was removed in 1.92.x because error-prone/misleading. You can use window->FontRefSize for a copy of g.FontSize at the time of the last Begin() call for this window.
+    //float     CalcFontSize() const    { ImGuiContext& g = *Ctx; return g.FontBaseSize * FontWindowScale * FontWindowScaleParents; }
 };
 
 //-----------------------------------------------------------------------------
@@ -3106,6 +3108,7 @@ namespace ImGui
 
     // Fonts, drawing
     IMGUI_API void          SetCurrentFont(ImFont* font, float font_size);
+    IMGUI_API void          UpdateCurrentFontSize();
     inline ImFont*          GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; }
     IMGUI_API void          PushPasswordFont();
     inline ImDrawList*      GetForegroundDrawList(ImGuiWindow* window) { IM_UNUSED(window); return GetForegroundDrawList(); } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches.