pthom / imgui_bundle

Your fast track to powerful GUIs: Dear ImGui Bundle, an extensive toolkit for Python and C++ with immediate mode efficiency.
https://pthom.github.io/imgui_bundle/
MIT License
703 stars 73 forks source link

Minor Bug in IO.Fonts #263

Closed tingspain closed 1 month ago

tingspain commented 1 month ago

Hello,

I think that there is a minor issue with the method ImGui.GetIO().Fonts.Font[].get_debug_name(), as it returns an empty string for the font name.

Additionally, I'm uncertain whether this is a bug or an intended difference, but the glyphs[] property within ImGui.GetIO().Fonts.Font[] seems to lack the codepoint attribute. In my experience with the C# version of ImGui, glyphs[] typically includes the codepoint attribute. Could you clarify if this discrepancy is expected in this context?

Development Environment

DEMO VIEW

image
static = lambda: None

def viewFontIcons(app_state: AppState):

    io = imgui.get_io()
    font = io.fonts.fonts[0]
    glyphs = []

    # BUG: the debug name is returning empty
    print( str( font.get_debug_name()) )

    # ISSUE? The glyphs are missing the codepoint attribute 
    # font.glyphs[0].Codepoint

    IconMin:ImWchar = ord('\ue800')
    IconMax:ImWchar = ord('\uf800')

    for i in range(IconMin, IconMax + 1):
        unicode_char =  chr(i)

        if font.find_glyph_no_fallback(i) is None:
            continue

        glyphs.append(f"U+{i:04X} - {unicode_char}")

    imgui.begin('\uf09a' + "  FontIcons View")

    combo_box = []
    for x in io.fonts.fonts:
        combo_box.append(str(x.get_debug_name()))

    if not hasattr(static, 'item_current'): static.item_current = 0
    changed, static.item_current = imgui.combo("combo", static.item_current, combo_box, io.fonts.fonts.size())

    font_name       = font.get_debug_name()
    size_characters = len(font.glyphs)
    size_glyphs     = len(glyphs)

    imgui.push_style_var(imgui.StyleVar_.separator_text_padding, ImVec2(10, 10))

    imgui.text("Font Info: " )
    imgui.begin_child("font info", ImVec2(0, 70), imgui.ChildFlags_.frame_style)
    imgui.text("Font Name: "   + str(font_name) )
    imgui.text("Characters: "  + str(size_characters) )
    imgui.text("Glyphs: "      + str(size_glyphs) )
    imgui.end_child()

    imgui.pop_style_var()

    imgui.set_cursor_pos_y(imgui.get_cursor_pos_y() + 20.)

    imgui.push_style_var(imgui.StyleVar_.frame_padding, ImVec2(10,10))

    imgui.text("Glyphs: " )
    imgui.begin_child("Glyphs List", ImVec2(0,0), imgui.ChildFlags_.frame_style)
    imgui.columns(6, None, False)
    for curGlyph in glyphs:
        imgui.text(curGlyph)
        imgui.next_column()
    imgui.columns(1)
    imgui.end_child()

    imgui.pop_style_var()

    imgui.end()
pthom commented 1 month ago

Hello,

ImFontConfig.Name is a debug only string which was not filled. It is now

struct ImFontConfig
{
    void*           FontData;               //          // TTF/OTF data
    ...
    // [Internal]
    char            Name[40];               // Name (strictly to ease debugging)
    ...
};

ImFontGlyph uses advanced C-style memory layout techniques: Colored and Visible use 1 bit each, and CodePoint uses 30 bits. They cannot be published as standard fields.

struct ImFontGlyph
{
    unsigned int    Colored : 1;    // : 1 means "use one bit"
    unsigned int    Visible : 1;
    unsigned int    Codepoint : 30;     // : 30 => use the 30 remaining bits to use 32 bits in total up until here
    float           AdvanceX;
    float           X0, Y0, X1, Y1;
    float           U0, V0, U1, V1;

//  ==> I added the following getters for them, when using Python.
#ifdef IMGUI_BUNDLE_PYTHON_API
    // [ADAPT_IMGUI_BUNDLE]
    bool isColored() const { return Colored != 0; }
    bool isVisible() const { return Visible != 0; }
    unsigned int getCodepoint() const { return Codepoint; }
    // [/ADAPT_IMGUI_BUNDLE]
#endif // IMGUI_BUNDLE_PYTHON_API
};
pthom commented 1 month ago

You may build from source to get the new features, or downloads wheels from here

tingspain commented 1 month ago

@pthom Thank you very much. Really thanks for your time and for your fast reply.

I will check it tomorrow.

pthom commented 1 month ago

Closing, as this is now solved.