Jelajahi Sumber

(Breaking) Fonts: obsolete PushFont() default parameter.

ocornut 1 bulan lalu
induk
melakukan
ca72eb0596
3 mengubah file dengan 45 tambahan dan 31 penghapusan
  1. 17 9
      docs/CHANGELOG.txt
  2. 10 10
      imgui.cpp
  3. 18 12
      imgui.h

+ 17 - 9
docs/CHANGELOG.txt

@@ -73,17 +73,25 @@ Breaking changes:
         Platform_GetWindowFramebufferScale() handler in 'docking' branch.
 - Fonts: **IMPORTANT** on Font Sizing:
    - Before 1.92, fonts were of a single size. They can now be dynamically sized.
-   - PushFont() API now has an optional size parameter. PushFontSize() was also added.
-       void PushFont(ImFont* font) --> void PushFont(ImFont* font, float size = 0.0f);
-   - Before 1.92: ImGui::PushFont() always used font "default" size specified in AddFont() call.
-   - Since  1.92: ImGui::PushFont() preserve the current font size which is a shared value.
+   - PushFont() API now has a REQUIRED size parameter.
+       void PushFont(ImFont* font) --> void PushFont(ImFont* font, float size);
+       - PushFont(font, 0.0f)                       // Change font and keep current size
+       - PushFont(NULL, 20.0f)                      // Keep font and change current size
+       - PushFont(font, 20.0f)                      // Change font and set size to 20.0f
+       - PushFont(font, style.FontSizeBase * 2.0f)  // Change font and set size to be twice bigger than current size.
+       - PushFont(font, font->LegacySize)           // Change font and set size to size passed to AddFontXXX() function. Same as pre-1.92 behavor, for fixed size fonts.
    - To use old behavior:
      - use 'ImGui::PushFont(font, font->LegacySize)' at call site (preferred).
      - or set 'ImFontConfig::Flags |= ImFontFlags_DefaultToLegacySize' in AddFont() call
        (not desirable as it requires e.g. all third-party code to be aware of it).
+     We intentionally didn't add a default parameter because it would make the long-term
+     transition more difficult.
+   - Kept inline redirection font. Will obsolete.
+   - External scale factors may be applied over the provided size.
+     This is why it is called 'FontSizeBase' in the style structure.
    - ImFont::FontSize was removed and does not make sense anymore.
      ImFont::LegacySize is the size passed to AddFont().
-   - Removed support for PushFont(NULL) which was a shortcut for "default font".
+   - Removed support for old PushFont(NULL) which was a shortcut for "revert to default font".
    - Renamed/moved 'io.FontGlobalScale' to 'style.FontScaleMain'.
 - Fonts: **IMPORTANT** on Font Merging:
   - When searching for a glyph in multiple merged fonts: font inputs are now scanned in order
@@ -182,8 +190,8 @@ Breaking changes:
      - renamed ImGuiFreeType::GetBuilderForFreeType() to ImGuiFreeType::GetFontLoader()
        - old:  io.Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType()
        - new:  io.Fonts.FontLoader = ImGuiFreeType::GetFontLoader();
-- DrawList: Fixed a regression from 1.91.1 where a Begin()/PushFont()/AddImage() sequence 
-  would not restore the correct atlas Texture Identifier when the PushFont() call used 
+- DrawList: Fixed a regression from 1.91.1 where a Begin()/PushFont()/AddImage() sequence
+  would not restore the correct atlas Texture Identifier when the PushFont() call used
   a font from a different atlas. (#8694, caused by #3224, #3875, #6398, #7903)
 - DrawList: Renamed ImDrawList::PushTextureID()/PopTextureID() to PushTexture()/PopTexture().
 - Fonts: (users of custom rectangles)
@@ -332,7 +340,7 @@ Other changes:
   one in docking (they accidentally diverged). (#8554)
 - Windows: BeginChild(): fixed being unable to combine manual resize on one axis
   and automatic resize on the other axis. (#8690)
-  e.g. neither ImGuiChildFlags_ResizeX | ImGuiChildFlags_AutoResizeY 
+  e.g. neither ImGuiChildFlags_ResizeX | ImGuiChildFlags_AutoResizeY
             or ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX worked before.
 - TreeNode: added experimental flags to draw tree hierarchy outlines linking
   parent and tree nodes: (#2920)
@@ -357,7 +365,7 @@ Other changes:
 - TreeNode: fixed incorrect clipping of arrow/bullet when using ImGuiTreeNodeFlags_SpanAllColumns.
 - InputText: fixed cursor positioning issue using up/down keys near end of lines while
   editing non-ASCII text. (Regression from 1.91.2) (#8635, #7925)
-- InputText: fixed a buffer overrun that could happen when using dynamically resizing 
+- InputText: fixed a buffer overrun that could happen when using dynamically resizing
   buffers (e.g. imgui_stdlib.cpp for std::string, or ImGuiInputTextFlags_CallbackRezize)
   and programmatically making an insertion. (#8689) [@ocornut, @m9710797]
 - Tables: fixed TableHeader() eager vertical clipping of text which may be noticeable

+ 10 - 10
imgui.cpp

@@ -470,10 +470,11 @@ CODE
                          - With a legacy backend (< 1.92): Instead of setting io.FontGlobalScale = 1.0f/N -> set ImFontCfg::RasterizerDensity = N. This already worked before, but is now pretty much required.
                          - With a new backend (1.92+): This should be all automatic. FramebufferScale is automatically used to set current font RasterizerDensity. FramebufferScale is a per-viewport property provided by backend through the Platform_GetWindowFramebufferScale() handler in 'docking' branch.
                        - Fonts: **IMPORTANT** on Font Sizing: Before 1.92, fonts were of a single size. They can now be dynamically sized.
-                         - PushFont() API now has an optional size parameter. PushFontSize() was also added.
-                         - Before 1.92: ImGui::PushFont() always used font "default" size specified in AddFont() call.
-                         - Since  1.92: ImGui::PushFont() preserve the current font size which is a shared value.
+                         - PushFont() API now has a REQUIRED size parameter. PushFontSize() was also added.
+                         - Before 1.92: PushFont() always used font "default" size specified in AddFont() call. It is equivalent to calling PushFont(font, font->LegacySize).
+                         - Since  1.92: PushFont(font, 0.0f) preserve the current font size which is a shared value.
                          - To use old behavior: (A) use 'ImGui::PushFont(font, font->LegacySize)' at call site (preferred). (B) Set 'ImFontConfig::Flags |= ImFontFlags_DefaultToLegacySize' in AddFont() call (not desirable as it requires e.g. third-party code to be aware of it).
+                         - Kept inline single parameter function. Will obsolete.
                        - Fonts: **IMPORTANT** on Font Merging:
                          - When searching for a glyph in multiple merged fonts: font inputs are now scanned in orderfor the first font input which the desired glyph. This is technically a different behavior than before!
                          - e.g. If you are merging fonts you may have glyphs that you expected to load from Font Source 2 which exists in Font Source 1. After the update and when using a new backend, those glyphs may now loaded from Font Source 1!
@@ -8901,23 +8902,22 @@ void ImGui::SetFontRasterizerDensity(float rasterizer_density)
     UpdateCurrentFontSize(0.0f);
 }
 
-// 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).
+// If you want to scale an existing font size! Read comments in imgui.h!
 void ImGui::PushFont(ImFont* font, float font_size_base)
 {
     ImGuiContext& g = *GImGui;
     //if (font == NULL) // Before 1.92 (June 2025), PushFont(NULL) == PushFont(GetDefaultFont())
     //    font = g.Font;
     IM_ASSERT(font != NULL);
+    IM_ASSERT(font_size_base >= 0.0f);
 
     g.FontStack.push_back({ g.Font, g.FontSizeBase, g.FontSize });
-    if (font_size_base <= 0.0f)
+    if (font_size_base == 0.0f)
     {
         if (font->Flags & ImFontFlags_DefaultToLegacySize)
-            font_size_base = 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_base = g.FontSizeBase; // Keep current font size
+            font_size_base = g.FontSizeBase;    // Keep current font size
     }
     SetCurrentFont(font, font_size_base, 0.0f);
 }
@@ -16909,7 +16909,7 @@ void ImGui::DebugNodeFont(ImFont* font)
     Indent();
     if (cfg->ShowFontPreview)
     {
-        PushFont(font);
+        PushFont(font, 0.0f);
         Text("The quick brown fox jumps over the lazy dog");
         PopFont();
     }

+ 18 - 12
imgui.h

@@ -493,21 +493,26 @@ 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)
+    //  - PushFont(font, 0.0f)                       // Change font and keep current size
+    //  - PushFont(NULL, 20.0f)                      // Keep font and change current size
+    //  - PushFont(font, 20.0f)                      // Change font and set size to 20.0f
+    //  - PushFont(font, style.FontSizeBase * 2.0f)  // Change font and set size to be twice bigger than current size.
+    //  - PushFont(font, font->LegacySize)           // Change font and set size to size passed to AddFontXXX() function. Same as pre-1.92 behavior.
     // *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 (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.
-    // *IMPORTANT* External scale factors are applied over the provided value. If you want to scale an existing font size:
-    //   -     OK: PushFontSize(style.FontSizeBase * 2.0f) (= value before external scale factors applied).
-    //   - NOT OK: PushFontSize(GetFontSize() * 2.0f)      (= value after external scale factors applied. External scale factors are: 'style.FontScaleMain * style.FontScaleDpi * maybe more').
-    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.
+    //  - In 1.92 we have REMOVED the single parameter version of PushFont() because it seems like the easiest way to provide an error-proof transition.
+    //  - PushFont(font) before 1.92 = PushFont(font, font->LegacySize) after 1.92          // Use default font size as passed to AddFontXXX() function.
+    // *IMPORTANT* external scale factors are applied over the provided size. If you want to scale an *existing* font size:
+    //  - External scale factors are: 'style.FontScaleMain * style.FontScaleDpi' and maybe more.
+    //  - CORRECT:   PushFont(NULL, style.FontSizeBase)         // use current unscaled size    == does nothing
+    //  - CORRECT:   PushFont(NULL, style.FontSizeBase * 2.0f)  // use current unscaled size x2 == make text twice bigger
+    //  - INCORRECT: PushFont(NULL, GetFontSize())              // INCORRECT! use size after external factors applied == EXTERNAL SCALING FACTORS WILL APPLY TWICE!
+    //  - INCORRECT: PushFont(NULL, GetFontSize() * 2.0f)       // INCORRECT! use size after external factors applied == EXTERNAL SCALING FACTORS WILL APPLY TWICE!
+    IMGUI_API void          PushFont(ImFont* font, float font_size_base_unscaled);          // Use NULL as a shortcut to keep current font. Use 0.0f to keep current size.
     IMGUI_API void          PopFont();
     IMGUI_API void          PushFontSize(float font_size_base);                             // keep current font, change its size. Final 'font size = font_size_base * external scale factors'.
     IMGUI_API void          PopFontSize();
     IMGUI_API ImFont*       GetFont();                                                      // get current font
-    IMGUI_API float         GetFontSize();                                                  // get current font size (= height in pixels) AFTER external scale factors applied. *IMPORTANT* DO NOT PASS THIS VALUE TO PushFont()/PushFontSize()! Use ImGui::GetStyle().FontSizeBase to get value before external scale factors.
+    IMGUI_API float         GetFontSize();                                                  // get current scaled font size (= height in pixels). AFTER external scale factors applied. *IMPORTANT* DO NOT PASS THIS VALUE TO PushFont()/PushFontSize()! Use ImGui::GetStyle().FontSizeBase to get value before external scale factors.
     IMGUI_API ImFontBaked*  GetFontBaked();                                                 // get current font bound at current size // == GetFont()->GetFontBaked(GetFontSize())
 
     // Parameters stacks (shared)
@@ -3756,7 +3761,7 @@ struct ImFontBaked
 enum ImFontFlags_
 {
     ImFontFlags_None                    = 0,
-    ImFontFlags_DefaultToLegacySize     = 1 << 0,   // Legacy compatibility: make PushFont() calls without explicit size use font->LegacySize instead of current font size.
+    ImFontFlags_DefaultToLegacySize     = 1 << 0,   // Legacy compatibility: make `PushFont(font)` == `PushFont(font, font->LegacySize)`. Otherwise by default/shared current font size is used.
     ImFontFlags_NoLoadError             = 1 << 1,   // Disable throwing an error/assert when calling AddFontXXX() with missing file/data. Calling code is expected to check AddFontXXX() return value.
     ImFontFlags_NoLoadGlyphs            = 1 << 2,   // [Internal] Disable loading new glyphs.
     ImFontFlags_LockBakedSizes          = 1 << 3,   // [Internal] Disable loading new baked sizes, disable garbage collecting current ones. e.g. if you want to lock a font to a single size. Important: if you use this to preload given sizes, consider the possibility of multiple font density used on Retina display.
@@ -3949,7 +3954,8 @@ struct ImGuiPlatformImeData
 namespace ImGui
 {
     // OBSOLETED in 1.92.0 (from June 2025)
-    IMGUI_API void      SetWindowFontScale(float scale);                        // Set font scale factor for current window. Prefer using PushFontSize(style.FontSizeBase * factor) or use style.FontScaleMain to scale all windows.
+    static inline void  PushFont(ImFont* font)                                  { IM_ASSERT(font != NULL); PushFont(font, font->LegacySize); }
+    IMGUI_API void      SetWindowFontScale(float scale);                        // Set font scale factor for current window. Prefer using PushFont(NULL, style.FontSizeBase * factor) or use style.FontScaleMain to scale all windows.
     // OBSOLETED in 1.91.9 (from February 2025)
     IMGUI_API void      Image(ImTextureRef tex_ref, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col); // <-- 'border_col' was removed in favor of ImGuiCol_ImageBorder. If you use 'tint_col', use ImageWithBg() instead.
     // OBSOLETED in 1.91.0 (from July 2024)