How to achieve good text height for freetypegl?

1

I am struggling with freetypegl and height calculations. I found the cherno used the library in his Sparky engine and he calculates it like so

float Font::GetHeight(const String& text) const
{
    using namespace ftgl;

    const maths::vec2& scale = m_Scale;
    float min = 0.0f;
    float max = 0.0f;
    for (int i = 0; i < text.size(); i++)
    {
        texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
        SP_ASSERT(glyph);
        float height = glyph->height / scale.y;
        float offset = glyph->offset_y / scale.y - height;
        if (offset < min)
            min = offset;
        if (height > max)
            max = height;
    }
    return abs(min) + abs(max);
}

maths::vec2 Font::GetOffsets(const String& text) const
{
    using namespace ftgl;

    if (text.empty())
        return maths::vec2(0.0f, 0.0f);

    texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[0]);
    SP_ASSERT(glyph);

    float yo = 0.0f;
    const maths::vec2& scale = m_Scale;
    for (int i = 0; i < text.size(); i++)
    {
        texture_glyph_t* glyph = texture_font_get_glyph(m_FTFont, text[i]);
        SP_ASSERT(glyph);
        float height = glyph->height / scale.y;
        float offset = glyph->offset_y / scale.y - height;
        if (offset < yo)
            yo = offset;
    }

    return maths::vec2(glyph->offset_x / scale.x, yo);
}

and it does not satisfy me, because for example [fps] string is a bit too height as I'd like it to be.

my code example:

//                                                        A B G R
// text = "[fps]"; position = {200.0f, 200.0f}; color = 0xffffffff
renderer2D->DrawString("[fps]", 200.0f, 0xffffffff, font);

//position X = 200.0f; position Y = 200.0f + font.getOffsets("[fps]").y
//width = font.getWidth("[fps]"; height = font.getHeight("[fps]");
//          A B G R
//color = 0xff0000ff
//thickness = 0.2f
renderer2D->DrawRect(Rect(200.0f, 
                          200.0f + font.getOffsets("[fps]").y, 
                          font.getWidth("[fps]"), 
                          font.getHeight("[fps]")), 0xff0000ff, 0.2f);

I want the rect to be around the text so I add offset (which is negative) to position Y, because letters like p, q, g etc. have a bit different height, so when I add offset Y it will cover whole letter.

the problem shows when the text has chars like: @, [, ]

its height is a bit too big

enter image description here

it should be a bit lower like this:

enter image description here

and that'd be perfect.

How can I achieve this?

c++
opengl
text
rendering
freetype
asked on Stack Overflow Nov 24, 2018 by ANewGuy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0