Skip to content

Font.FindGlyph and font.Glyphs return wrong data #206

@babooncodes

Description

@babooncodes

Font ProggySmall.ttf everything renders and works correctly.
However if you print the contents of each glyph from ImGui.GetFont() you get uvs that dont make sense, AdvanceX that doesnt make sense etc. I tried to find the issue to the best of my knowledge, set structs to sequential in hopes that there is some layout mismatch, converted chars to utf8, the result is always the same - wrong data is provided.
I am not sure what is going on there.

here is the case where i found out the problem

static bool test = false;

    /// Draws vertical text. The position is the bottom left of the text rect.
    public static unsafe void AddTextVertical(string text, Color text_color, Vector2? position = null)
    {
        if (string.IsNullOrEmpty(text))
        {
            text = "Null";
        }

        Vector2 pos = position.HasValue ? position.Value + ImGui.GetWindowPos() : ImGui.GetCursorPos() + ImGui.GetWindowPos();
        ImDrawListPtr DrawList = ImGui.GetWindowDrawList();
        pos.x = Mathf.Round(pos.x);
        pos.y = Mathf.Round(pos.y);
        ImFontPtr font = ImGui.GetFont();

        if (!test)
        {
            var glyphs = font.Glyphs;
            for (int i = 0; i < glyphs.Size; i++)
            {
                var g = glyphs[i];
                Debug.Log("g: " + g.Codepoint + " a:" + g.AdvanceX + " v0:" + g.V0 + " v1:" + g.V1);
            }
            test = true;
        }

        byte[] bytes = Encoding.Default.GetBytes(text);
        text = Encoding.UTF8.GetString(bytes);

        ImFontGlyphPtr glyph;
        char c;
        //ImGuiContext g = *GImGui;
        Vector2 text_size = ImGui.CalcTextSize(text);

        float textX = text_size.x / text.Length;
        for (int i = 0; i < text.Length; i++)
        {
            c = text[i];

            glyph = font.FindGlyph(c);

            if (glyph.NativePtr == null) continue;

            DrawList.PrimReserve(6, 4);
            DrawList.PrimQuadUV(
                    pos + new Vector2(glyph.Y0, -glyph.X0),
                    pos + new Vector2(glyph.Y0, -glyph.X1),
                    pos + new Vector2(glyph.Y1, -glyph.X1),
                    pos + new Vector2(glyph.Y1, -glyph.X0),

                    new Vector2(glyph.U0, glyph.V0),
                    new Vector2(glyph.U1, glyph.V0),
                    new Vector2(glyph.U1, glyph.V1),
                    new Vector2(glyph.U0, glyph.V1),
                    text_color.ToUint());
            
            pos.y -= textX ;
        }

        ImGui.SetCursorPosX(text_size.y);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions