Skip to content

stb_truetype: Fonts rendering small? #689

@vexe

Description

@vexe

Hi Sean!

So I'm pretty sure I'm doing something dumb I just can't see it. But my fonts are rendering smaller than they should. New Courier at 16 is barely readable, much smaller than what it should be (see comparison screenshot). I even tried this example (https://github.com/justinmeiners/stb-truetype-example/blob/master/main.c) and wrote it out to a png, still the same.

Info:
Using a slightly modified version of the rendering code in stb_truetype.
Using stb_rect_pack for the packing.
Using fixed GL on Win32.
Using VS2015 compiling for x64

image

Here's my rendering code:


    void DrawTextAt(float X, float Y, char *Text, FDColor Color, FDFont *Font)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, Font->TextureId);
        glBegin(GL_QUADS);
        glColor(Color);
        while (*Text)
        {
            if (*Text >= 32 && *Text < 128)
            {
                stbtt_aligned_quad Quad;
                stbtt_GetPackedQuad(Font->Data, Font->BitmapWidth, Font->BitmapHeight,
                    *Text - 32, &X, &Y, &Quad, 0);

                if (*(Text+1))
                {
                    float Scale = stbtt_ScaleForPixelHeight(&Font->Info, Font->Size);
                    X += Scale*stbtt_GetCodepointKernAdvance(&Font->Info, *(Text)-32, *(Text+1)-32);
                }

                glTexCoord2f(Quad.s0, Quad.t0); glVertex2f(Quad.x0, Quad.y0);
                glTexCoord2f(Quad.s1, Quad.t0); glVertex2f(Quad.x1, Quad.y0);
                glTexCoord2f(Quad.s1, Quad.t1); glVertex2f(Quad.x1, Quad.y1);
                glTexCoord2f(Quad.s0, Quad.t1); glVertex2f(Quad.x0, Quad.y1);
            }
            ++Text;
        }
        glEnd();
        glDisable(GL_TEXTURE_2D);
    }

Here's my font making code:

void MakeFontFromData(byte *FontData, float Size, FDFont *Font)
{
    const int W = 1024;
    const int H = 1024;
    *Font = {};
    Font->BitmapWidth = W;
    Font->BitmapHeight = H;
    Font->Bitmap = (byte *)malloc(W*H);
    Font->Size = Size;
    stbtt_InitFont(&Font->Info, FontData, 0);

    stbtt_pack_context PackContext={};
    stbtt_PackBegin(&PackContext, Font->Bitmap, W, H, 0, 1, 0);
    stbtt_PackSetOversampling(&PackContext, 2, 2);
    stbtt_pack_range Ranges[1]={};
    Ranges[0].chardata_for_range = Font->Data;
    Ranges[0].array_of_unicode_codepoints = 0;
    Ranges[0].first_unicode_codepoint_in_range = 32;
    Ranges[0].num_chars = 96;
    Ranges[0].font_size = STBTT_POINT_SIZE(Size);
    stbtt_PackFontRanges(&PackContext, FontData, 0, Ranges, 1);
    stbtt_PackEnd(&PackContext);

    int Ascend;
    stbtt_GetFontVMetrics(&Font->Info, &Ascend, 0, 0);
    float Scale = stbtt_ScaleForPixelHeight(&Font->Info, Size);
    Font->Baseline = Ascend*Scale;

    glGenTextures(1, &Font->TextureId);
    glBindTexture(GL_TEXTURE_2D, Font->TextureId);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, W, H, 0, GL_ALPHA, GL_UNSIGNED_BYTE, Font->Bitmap);
}

My GL setup:

void InitMain()
{
    // OpenGL
    glClearColor(0, 0, 0.5f, 1);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glDisable(GL_CULL_FACE);
    glDepthMask(false);
    glEnable(GL_BLEND);
    glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    GLenum GLError = glGetError();
    if (GLError != GL_NO_ERROR)
    {
        printf("GL Error: %s\n", glGetString(GLError));
    }

    // Fonts
    GL.MakeFontFromFile("resources/fonts/cour.ttf", 16, Editor.Fonts+0);
}

void UpdateMain(float dt)
{
    FDServices *PS = Editor.PS;

    if (GL.ScreenWidth != PS->ScreenWidth ||
        GL.ScreenHeight != PS->ScreenHeight)
    {
        GL.ScreenWidth = PS->ScreenWidth;
        GL.ScreenHeight = PS->ScreenHeight;

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, GL.ScreenWidth, GL.ScreenHeight, 0, -1, +1);
    }

    glViewport(0, 0, GL.ScreenWidth, GL.ScreenHeight);
    glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    glClearStencil(0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    GL.DrawTextAt(10, 40, "1 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);", FDColor(1, 1, 1, 1), Editor.Fonts+0);
}

What am I missing/doing wrong?
Appreciate any help!

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions