Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font stack cannot be manipulated within different windows #3224

Closed
wolfpld opened this issue May 11, 2020 · 4 comments
Closed

Font stack cannot be manipulated within different windows #3224

wolfpld opened this issue May 11, 2020 · 4 comments

Comments

@wolfpld
Copy link
Contributor

wolfpld commented May 11, 2020

Version: 03ea87ea28
Branch: docking

Required setup:

io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);   // will be used as default
ImFont* font = io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);

Test code:

ImGui::PushFont( font );
ImGui::Begin( "test" );
if( ImGui::Button( "click me" ) ) ImGui::OpenPopup( "popup" );
if( ImGui::BeginPopup( "popup" ) )
{
    ImGui::PopFont();
    ImGui::Text( "popup" );
    ImGui::PushFont( font );
    ImGui::EndPopup();
}
ImGui::End();
ImGui::PopFont();

This will display a window using a custom font (font), with the intention to display popup contents using the default one. This doesn't happen, as AddText() crashes in assert at imgui_draw.cpp:1228 (due to _TextureIdStack being empty).

@ocornut
Copy link
Owner

ocornut commented May 11, 2020

I confirmed this is an issue, we haven't looked at how the font stack was handled in a long while (it's not even exposing the (ugly form of) scaling).

Those functions PushFont/PopFont are currently mixing up context and per-window stacks:

g.FontStack.push_back(font);
g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID);
g.CurrentWindow->DrawList->PopTextureID();
g.FontStack.pop_back();

(The irony being that all those TextureId are going to be the same since your atlas is the same)

As as quick workaround this will work if you don't need a title bar, which popups don't have:

ImGui::PushFont(font2);
ImGui::Begin("test");
if (ImGui::Button("click me"))
    ImGui::OpenPopup("popup");
if (ImGui::BeginPopup("popup"))
{
    ImGui::PushFont(font1);
    ImGui::Text("popup");
    ImGui::PopFont();
    ImGui::EndPopup();
}
ImGui::End();
ImGui::PopFont();

Will be looking for the proper fix.

@ocornut
Copy link
Owner

ocornut commented May 11, 2020

Amend: actually this is the proper workaround which will also work with title bars:

ImGui::PushFont(font2);
ImGui::Begin("test");
if (ImGui::Button("click me"))
    ImGui::OpenPopup("popup");

if (ImGui::IsPopupOpen("popup"))
{
    ImGui::PushFont(font1);
    if (ImGui::BeginPopup("popup"))
    {
        ImGui::Text("popup");
        ImGui::EndPopup();
    }
    ImGui::PopFont();
}

ImGui::End();
ImGui::PopFont();

Question: was this popup example you posted representative of your actual issue? Trying to gauge if the workaround can unblock you for a while or if there is some need to find a more generic solution soon.

@wolfpld
Copy link
Contributor Author

wolfpld commented May 11, 2020

Yes -- wolfpld/tracy@e330b96. Since a workaround exists, it's not a pressing issue.

(I use fixed width font to display source/asm code, but I do need to occasionally jump back to default font with icons, etc.)

@ocornut
Copy link
Owner

ocornut commented Aug 20, 2024

I have pushed a fix for this: eb7201b

(It's not a perfect fix, in theory our code and system are still rather broken for the case of using multiple atlases textures, but I don't think that has been fully exercised ever, so we'll address that when the time comes.)

@ocornut ocornut closed this as completed Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants