Difficulty Adding New Font: font->ContainerAtlas is nullptr

Hi again, new problem: I’m trying to add a custom ttf font to my ImGui project and am having trouble. I am loading my font after my ImGui::NewFrame() call and it is recognizing the font exists, as the output from my call to AddFontFromFileTTF([fontPath], [fontSize]) is not null, but analyzing the stuff inside the font, it says the scale is 0, the fallback glyph is NULL, it has no glyphs, etc. When I try to run ImGui::PushFont(font), it throws an exception inside ImGui::SetCurrentFont(font) saying font->ContainerAtlas was nullptr (which is expected, seeing as the font looks like a shell when ana.

It looks to me like the font just isn’t being loaded correctly, even though it finds the file, but I’m not sure why as there are no instances I’ve found online of this incorrect loading occurring and there are no examples I could find of loading a custom font. Any help would be appreciated!


there are no examples I could find of loading a custom font.

This is documented in the main.cpp file of any of the 16 examples of the examples/ folder, as well as the FAQ and in misc/fonts/README.txt. Please read those.

You need to load and build font atlas before calling NewFrame().

There should be an assert if you try to rebuild the font atlas between NewFrame and EndFrame. When developing you need to make sure asserts are enabled (to test them, you can use “IM_ASSERT(0);” in your code and make sure that makes the debugger stops).

Got it, thanks! I knew about the 16 examples in the examples folder, but I always thought they were just the same example made for different rendering engines. I didn’t even know about the misc/fonts folder though, so that’s useful! Was able to get it to work by just calling the AddFontFromFileTTF function once at the start, I think that was my problem.

1 Like