imgui icon indicating copy to clipboard operation
imgui copied to clipboard

Different fonts have different rendered sizes

Open devkaiwang opened this issue 5 months ago • 5 comments

Version/Branch of Dear ImGui:

Version v1.92.1, Branch: docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx12.cpp

Compiler, OS:

Window 11 + VS2022

Full config/build information:

Dear ImGui 1.92.1 (19210)
--------------------------------

sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201703
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------

io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000481
 NavEnableKeyboard
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C1E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------

io.Fonts: 4 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: FreeType
io.DisplaySize: 700.00,900.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------

style.WindowPadding: 12.00,12.00
style.WindowBorderSize: 1.00
style.FramePadding: 6.00,4.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 12.00,6.00
style.ItemInnerSpacing: 6.00,6.00

Details:

My Issue/Question:

Different fonts appear different in sizes even we use same size value for them. (ImGui::PushFont(font, **font_size**)). In my case, one of the fonts just looks much larger, so for the previous version of ImGui, I created this font in a smaller size.

Since version 1.92, seems we don't assign size value for the fonts anymore, but I don't want to assign a different font size the larger font every time I push the font to the stack, I prefer to leave it as default (0.0f). (Assigning a size every time means a lot of lines of changes and a very different way of size management). Is it possible to pre-assign a ratio like 0.8 for it?

Thanks a lot!

devkaiwang avatar Jul 22 '25 15:07 devkaiwang

This is a good idea I’ll try to investigate it soon. I am not currently in front of the code but I believe there’s a now-marked-obsolete-but-functional Scale field inside ImFont. Could you toy with that ?

If it works well I could move it to ImFontConfig.

ocornut avatar Jul 22 '25 15:07 ocornut

Hi @ocornut ,

Seems ImFont::Scale works very well. I'd turn IMGUI_DISABLE_OBSOLETE_FUNCTIONS for now :)

I guess in order to work well with merging font, which may look different, moving to ImFontConfig is a good idea?

Thank you!

devkaiwang avatar Jul 22 '25 15:07 devkaiwang

Different fonts appear different in sizes even we use same size value for them.

See #4780: ImGui uses the total ascent - descent as the height instead of just the EM square.

It can be changed to being the EM square size with a patch like this (super experimental): cfillion/imgui@0345a710178ddaf883d1dd3b56cd30916d2f2397.

Apparent size becomes as expected at the cost of allowing some glyphs to overflow the line height (which is currently == the requested font size).

Image

(I load arbitrary fonts from the user's system and I'm implementing automatic fallback for non-latin scripts, needing consistent size and alignment.)

cfillion avatar Jul 22 '25 16:07 cfillion

 1f7f1f54af38b0350d8c0008b096a9af6de299c7 3pp/imgui (v1.92.2b-docking)

I meet the same issue, in my case, I see different render size for Asian language and English, if I load the font with size_pixels = 0.0F I have to set the value to different numbers to let the rendered size look correctly. Not sure if this is a good practice.

Image
//
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <misc/freetype/imgui_freetype.h>

namespace
{
auto loadFontLookBad() -> ImFont *
{

    ImGuiIO &io = ImGui::GetIO();
    ImFont *font = nullptr;
    ImFontConfig fontCfg;
    font = io.Fonts->AddFontFromFileTTF(R"(C:\Windows\Fonts\segoeui.ttf)", 0.0F, &fontCfg);
    ImFontConfig fontCfgCN;
    fontCfgCN.MergeMode = true;
    fontCfgCN.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_LoadColor;
    font = io.Fonts->AddFontFromFileTTF(R"(C:\Windows\Fonts\SimHei.ttf)", 0.0F, &fontCfgCN);
    return font;
}

auto loadFontLookGood() -> ImFont *
{

    ImGuiIO &io = ImGui::GetIO();
    ImFont *font = nullptr;
    ImFontConfig fontCfg;
    font = io.Fonts->AddFontFromFileTTF(R"(C:\Windows\Fonts\segoeui.ttf)", 22.0F, &fontCfg);
    ImFontConfig fontCfgCN;
    fontCfgCN.MergeMode = true;
    fontCfgCN.FontLoaderFlags |= ImGuiFreeTypeLoaderFlags_LoadColor;
    font = io.Fonts->AddFontFromFileTTF(R"(C:\Windows\Fonts\SimHei.ttf)", 16.0F, &fontCfgCN);
    return font;
}


} // namespace
auto main() -> int
{
    // Init GLFW
    glfwInit();
    const char *glsl_version = "#version 130";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    GLFWwindow *window = glfwCreateWindow(800, 600, "Minimal ImGui", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1); // Enable vsync

    // Init ImGui
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO &io = ImGui::GetIO();
    (void) io;
    io.IniFilename = nullptr;
    ImGui::StyleColorsDark(); // or Light()

    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init(glsl_version);
    auto *fontLookBad = loadFontLookBad();
    auto *fontLookGood = loadFontLookGood();
    // Main loop
    while (glfwWindowShouldClose(window) == 0)
    {
        glfwPollEvents();
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
        // Test window with negative position
        ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Once);
        ImGui::Begin("Test Window");
        ImGui::PushFont(fontLookBad, 100.0F);
        ImGui::TextUnformatted(" 啊啊啊啊aaaa");
        ImGui::PopFont();
        ImGui::PushFont(fontLookGood, 100.0F);
        ImGui::TextUnformatted(" 啊啊啊啊aaaa");
        ImGui::PopFont();
        ImGui::End();
        // Render
        ImGui::Render();
        int display_w = 0;
        int display_h = 0;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        glfwSwapBuffers(window);
    }

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

xuboying avatar Nov 07 '25 09:11 xuboying

Thank you I’ll take note to try your fonts with the code in #8857

ocornut avatar Nov 07 '25 10:11 ocornut