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

Displaying Glyphs' Codepoints as Characters [SOLVED] #494

Open
tingspain opened this issue Sep 5, 2024 · 2 comments
Open

Displaying Glyphs' Codepoints as Characters [SOLVED] #494

tingspain opened this issue Sep 5, 2024 · 2 comments

Comments

@tingspain
Copy link

Hello,

I am trying to create a simple window to list all the glyphs and theirs Unicode of a font. But I dont manage to use properly the Codepoint to render the character.

Does anyone know how I can achieve that?

Thanks in advance!

static void ShowGlyphWindow()
{
    unsafe {

        ImGui.Begin("Glyphs List");

        var io = ImGui.GetIO();
        var font = io.Fonts.Fonts[0]; // Access the default font (usually the first one)
        
        ImGui.Text(Regex.Unescape(font.GetDebugName().ToString()));

        // Iterate through all the glyphs in the default font
        for (int i = 0; i < font.Glyphs.Size; i++)
        {
            var glyph = font.Glyphs[i];
            if( glyph.Codepoint == 0)
                continue;

            // Create a string to display the glyph character and its Unicode codepoint
            var glyphString = new StringBuilder(); 
            string text = $"\\u{glyph.Codepoint:X4}";

            string text2 = $"{glyph.Codepoint:X4}";
            byte[] bytes = Encoding.Default.GetBytes(text2);
            text2 = Encoding.UTF8.GetString(bytes);
            
            glyphString.Append($"Char: {text}  Unicode: U+{glyph.Codepoint:X4}");

            // Display the glyph string
            ImGui.Text(Regex.Unescape(glyphString.ToString()));

        }

        ImGui.End();
    }
}
@tingspain
Copy link
Author

At the end I solved it by using different approach. I dont know if it is the most optimal way, but it works for me. I hope it will be useful for someone else. ^___^

Does anyone have any feedback?

unsafe {

        ImGui.Begin("Glyphs List");

        var io = ImGui.GetIO();
        var font = io.Fonts.Fonts[0];

        string[] icons = ImGuiExt.getIconList();
        List<string> glyphs = new List<string>();

        for (ushort i = '\ue800'; i <= '\uf2db'; i++)
        {
            char unicodeChar = (char)i;

            if(font.FindGlyphNoFallback(unicodeChar).NativePtr == null)
                continue;

            glyphs.Add($"U+{i:X4} - {unicodeChar}");
        }
    
        ImGui.Text("Font Name: " + Regex.Unescape(font.GetDebugName().ToString()));
        ImGui.Text("Characters: "  + font.Glyphs.Size);
        ImGui.Text("Glyphs: " + glyphs.Count);  

        ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, new System.Numerics.Vector2(10, 10));

        ImGui.BeginChild("scrolling", new System.Numerics.Vector2(0,0) , ImGuiChildFlags.FrameStyle);

        ImGui.Columns(6, null, false);
        foreach (var glyph in glyphs){
            ImGui.Text(glyph);
            ImGui.NextColumn();
        }
        ImGui.Columns(1);

        ImGui.EndChild();

        ImGui.PopStyleVar();

        ImGui.End();
    }
image

@tingspain tingspain changed the title Displaying Glyphs' Codepoints as Characters Displaying Glyphs' Codepoints as Characters [SOLVED] Sep 11, 2024
@grimdoomer
Copy link

Same issue as #206. The Colored/Visible/Codepoint fields in the ImFontGlyph struct are bit packed and ImGui.NET doesn't honor this. Your original code snippet could have worked if you incorrectly access the ImFontGlyph structure to compensate for the incorrect layout and do:

glyph.Colored >> 2

PR #245 should fix this but I don't know what the status is.

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