8000 Can't load font with glyphs. · Issue #353 · pyimgui/pyimgui · GitHub
[go: up one dir, main page]

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

Can't load font with glyphs. #353

Open
ShaiAvr opened this issue Oct 12, 2023 · 2 comments
Open

Can't load font with glyphs. #353

ShaiAvr opened this issue Oct 12, 2023 · 2 comments

Comments

@ShaiAvr
Copy link
ShaiAvr commented Oct 12, 2023

I am new to ImGui and I am trying to make a log window that shows the log messages I receive in the console:

Screenshot 2023-10-12 180145

As you can see, the log messages contain emojis and I'd like to display them too. Right now, It shows them as question marks:

Screenshot 2023-10-12 180733

I tried searching online and from this documentation of ImGui and this documentation of pyimgui it seems that I need to load a custom font with glyph ranges.

Loading a simple font with io.font.add_font_from_file_ttf(font, 16) works just fine, but when I try to add glyphs with io.fonts.get_glyph_ranges_chinese() or something similar, it just crashes and I don't understand why.

I am using pygame as a backend (pygame-ce actually). Here's the sample code I tried to use:

from imgui.integrations.pygame import PygameRenderer
import imgui
import moderngl as mgl
import pygame


def main():
    pygame.init()
    pygame.display.gl_set_attribute(
        pygame.GL_CONTEXT_PROFILE_MASK, pygame.GL_CONTEXT_PROFILE_CORE
    )
    size = 1600, 900

    pygame.display.set_mode(size, pygame.DOUBLEBUF | pygame.OPENGL)

    ctx = mgl.create_context(require=330)
    context = imgui.create_context()
    impl = PygameRenderer()
    clock = pygame.time.Clock()

    io = imgui.get_io()
    io.display_size = size

    f = io.fonts.add_font_from_file_ttf(
        r"C:\Windows\Fonts\calibri.ttf",  # Just a sample font
        16,
        io.fonts.get_glyph_ranges_chinese(),  # Adding this causes ImGui to crash!
    )
    impl.refresh_font_texture()

    show_custom_window = True

    # Note the emoji at the end
    example_msg = "12.10.2023 18:00:35.464 | INFO       ℹ️ |"

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            impl.process_event(event)
        impl.process_inputs()

        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", "Ctrl+Q", False, True
                )

                if clicked_quit:
                    running = False

                imgui.end_menu()
            imgui.end_main_menu_bar()

        imgui.show_test_window()

        if show_custom_window:
            is_expand, show_custom_window = imgui.begin("Custom window", True)
            if is_expand:
                imgui.text(example_msg)  # Doesn't work with normal font
                with imgui.font(f):  # Doesn't work without the third argument which I can't get to work
                    imgui.text(example_msg)
            imgui.end()

        ctx.clear(0.08, 0.16, 0.18)
        imgui.render()
        impl.render(imgui.get_draw_data())

        pygame.display.flip()
        clock.tick(60)
    impl.shutdown()
    imgui.destroy_context(context)
    ctx.release()
    pygame.quit()


if __name__ == "__main__":
    main()

I don't understand why adding io.fonts.get_glyph_ranges_chinese() causes it to crash because it seems to be the correct way to add glyph ranges. What am I missing here? How can I fix this code to render emojis correctly?

@learn-more
Copy link
Collaborator

Are there any relevant details in the crash?
Is it a python exception or a straight-out process going down crash?

@ShaiAvr
Copy link
Author
ShaiAvr commented Oct 16, 2023

@learn-more It's an ImGuiError. Here's the traceback:

Screenshot 2023-10-16 125709

It seems to come from the C++ code. Other than that, I can't really understand anything from this error.

RainbowUnicorn7297 added a commit to RainbowUnicorn7297/mltd-relive that referenced this issue Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0