Imgui asertion error when loading custom font

1.4k Views Asked by At

I'm loading a custom font in pyimgui:

 io = imgui.get_io();
    io.fonts.clear()
    io.font_global_scale = 1
    new_font = io.fonts.add_font_from_file_ttf(
        "Broken Glass.ttf", 20.0, io.fonts.get_glyph_ranges_latin()
    );
    impl.refresh_font_texture() 

This is how everyone seems to do it, but it doesn't work. I get: ImGui assertion error (0) at imgui-cpp/imgui_draw.cpp:1573 every time. I'm on macos big sur.

1

There are 1 best solutions below

1
On

One way I've been able to reproduce your error is by using an erroneous filename. I suspect your "Broken Glass.ttf" file either contains a typo in the name, or is not located in the root folder of your project, in which case you should use the full path to the .ttf file.

Here's an example of a glfw + imgui implementation with a different font that works for me. Your code is in start():

import glfw
import OpenGL.GL as gl
import imgui
from imgui.integrations.glfw import GlfwRenderer

window_width = 960
window_height = 540
window_name = "glfw / imgui window"

def glfw_init():

    if not glfw.init():
        print("Could not initialize OpenGL context.")
        exit(1)

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

    window = glfw.create_window(window_width, window_height, window_name, None, None);
    glfw.make_context_current(window)

    if not window:
        glfw.terminate()
        print("Could not initialize Window")
        exit(1)

    return window

def start():
    io = imgui.get_io()
    io.fonts.clear()
    io.font_global_scale = 1
    new_font = io.fonts.add_font_from_file_ttf("Roboto-Regular.ttf", 20.0, io.fonts.get_glyph_ranges_latin())
    impl.refresh_font_texture()

def onupdate():
    imgui.begin("Test window")
    imgui.text("ABCDEFGHIJKLMNOPQRTSUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n1234567890!@#$%^&*()")
    imgui.image(imgui.get_io().fonts.texture_id, 512, 512)
    imgui.end()


if __name__ == "__main__":
    imgui.create_context()
    window = glfw_init()
    impl = GlfwRenderer(window)
    start()
    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()
        gl.glClearColor(0.0, 0.0, 0.0, 1.0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        onupdate()

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()