Imgui: Fonts at runtime after initial initiation?

Created on 27 Jan 2019  路  5Comments  路  Source: ocornut/imgui

Hi!
Hope format is correct and I'll go straight to the matter / deleted most of the format for things I don't have. I've been looking at related issues but I didn't see anyone trying this.

Version: // dear imgui, v1.67 WIP
Branch: master

My Question:
Is it even possible to use io.Fonts->AddFontFromFileTTF(filePath.c_str(), fontSize); at runtime after the initial initiation of ImGui?
It does load the font but doesn't build the atlas somehow: when I try to use the loaded font, it throws an error at,
IM_ASSERT(font && font->IsLoaded());
in ImGui::SetCurrentFont(ImFont* font) when trying to use ImGui::PushFont(ImFont* font)

Thank you for your time.

fontext

Most helpful comment

Hello, I am trying to implement similar feature but does not seem to work. I call Fonts->AddFontFromFileTTF() + Fonts->Build() before NewFrame() but everything in main window disappears (modifying code in example_win32_directx11 main.cpp file).

    // Main loop
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    while (msg.message != WM_QUIT)
    {
          if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {鈥;
          if (load_font_file)  {  // set to true if some button has been hit
                 const auto * font = io.Fonts->AddFontFromFileTTF(new_font_file.c_str(), 12.0f);
                 io.Fonts->Build();
                 load_font_file = false;
           }
           // Start the Dear ImGui frame
           ImGui_ImplDX11_NewFrame();
           ImGui_ImplWin32_NewFrame();
           ImGui::NewFrame();
          ....
      ....

Is there any missing function call? You mention "reupload the texture", but I have no idea on how to do this.
Thanks in advance.

EDIT: Adding call to ImGui_ImplDX11_InvalidateDeviceObjects(); after io.Fonts->Build(); seems to solve the problem.

All 5 comments

Hello,

You need to rebuild the atlas if you add new fonts. Atlas needs to be rebuilt before NewFrame().

I can't rebuild Atlas in runtime?
Say in a scenario where an user decides to load a custom font.
I can't really do this, can I?

Yes you can.. just call Fonts->Build() et Fonts->GetTexDataAsXXX() and reupload the texture..

Hello, I am trying to implement similar feature but does not seem to work. I call Fonts->AddFontFromFileTTF() + Fonts->Build() before NewFrame() but everything in main window disappears (modifying code in example_win32_directx11 main.cpp file).

    // Main loop
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    while (msg.message != WM_QUIT)
    {
          if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {鈥;
          if (load_font_file)  {  // set to true if some button has been hit
                 const auto * font = io.Fonts->AddFontFromFileTTF(new_font_file.c_str(), 12.0f);
                 io.Fonts->Build();
                 load_font_file = false;
           }
           // Start the Dear ImGui frame
           ImGui_ImplDX11_NewFrame();
           ImGui_ImplWin32_NewFrame();
           ImGui::NewFrame();
          ....
      ....

Is there any missing function call? You mention "reupload the texture", but I have no idea on how to do this.
Thanks in advance.

EDIT: Adding call to ImGui_ImplDX11_InvalidateDeviceObjects(); after io.Fonts->Build(); seems to solve the problem.

Adding call to ImGui_ImplDX11_InvalidateDeviceObjects(); after io.Fonts->Build(); seems to solve the problem.

Good to hear!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Folling picture Folling  路  3Comments

NPatch picture NPatch  路  3Comments

spaderthomas picture spaderthomas  路  3Comments

ILoveImgui picture ILoveImgui  路  3Comments

mnemode2 picture mnemode2  路  3Comments