ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.16k stars 10.31k forks source link

Custom font issues with the docking branch #2127

Open samhocevar opened 6 years ago

samhocevar commented 6 years ago

I am using custom bitmap fonts instead of using the TTF engine. There are three major reasons for using bitmap fonts:

However, with the docking branch, I cannot seem to properly override the default font using PushFont(). Here is what happens:

Maybe I am doing something wrong, since custom fonts are not really documented and I know the API has a lot of legacy… but that code used to work perfectly without docking.

Here is a patch that modifies example_sdl_opengl3 to demonstrate the two problems above : patch-bitmap-font-test.diff.txt

Below is the font creation part if you have any comments on it:

static ImWchar const ranges[] = { 0x20, 0x7f, 0 };

ImFontAtlas *atlas = IM_NEW(ImFontAtlas)();
atlas->TexWidth = 128;
atlas->TexHeight = 128;
atlas->TexUvScale = ImVec2(1.f / 128, 1.f / 128);

ImFontConfig config;
config.FontData = malloc(1);
config.FontDataSize = 1;
config.FontDataOwnedByAtlas = false;
config.SizePixels = 8;
config.GlyphRanges = ranges;
ImFont *font = atlas->AddFont(&config);

// Operations copied from ImFontAtlasBuildSetupFont()
font->FontSize = config.SizePixels;
font->ConfigData = &atlas->ConfigData[0];
font->ContainerAtlas = atlas;
font->Ascent = 0;
font->Descent = config.SizePixels;
font->ConfigDataCount++;

// Add some glyphs from a hypothetical regular grid
for (int ch = 0x20; ch < 0x80; ++ch)
{
    font->AddGlyph(ch, 0, 0, 6, 8, (ch % 0x10) * 8.f / 128, (ch / 0x10) * 8.f / 128, (ch % 0x10 + 1) * 8.f / 128, (ch / 0x10 + 1) * 8.f / 128, 6);
}
font->BuildLookupTable();
ocornut commented 6 years ago

Thanks for your report Sam.

the default font is still used for windows/tab titles the application randomly crashes when playing around with the dock:

I'll have to look in more details, but I think both issues would be mitigated if you passed your atlas to the ImGui::CreateContext() call so your atlas and your font becomes the default font. Is that what you did in your actual application?

(In particular, when merging two windows together in the docking branch, the "host" window is created in NewFrame() using the default font. Which can be slightly problematic, but my solutions for it currently have other issues.)

samhocevar commented 6 years ago

I remember all sort of weird things happening if I used my custom atlas in ImGui::CreateContext() but that wasn’t with a minimal repro case, so I’ll double check!