From 6a681764af21df379da7d4a4a9fbad02ef522aff Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 23 Jun 2022 22:41:30 +0000 Subject: [PATCH] stbtt truetype works --- source/engine/font.c | 48 +++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/source/engine/font.c b/source/engine/font.c index b4b49b3..c5bb1ff 100755 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -8,6 +8,8 @@ #include #include +#define STB_TRUETYPE_IMPLEMENTATION +#define STBTT_STATIC #include static uint32_t VBO = 0; @@ -30,50 +32,53 @@ struct sFont *MakeFont(const char *fontfile, int height) char fontpath[256]; snprintf(fontpath, 256, "fonts/%s", fontfile); - - stbtt_fontinfo fontinfo = { 0 }; - int ascent, descent, linegap; - fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb")); - if (!stbtt_InitFont(&fontinfo, ttf_buffer, 0)) { + + stbtt_fontinfo fontinfo = { 0 }; + if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) { printf("failed\n"); } + int tw,th; + + unsigned char *testbitmap = stbtt_GetCodepointBitmap(&fontinfo, 0, stbtt_ScaleForPixelHeight(&fontinfo, 40), 'a', &tw, &th, 0,0); + + for (int i = 0; i < th; ++i) { + for (int j = 0; j>5]); + putchar('\n'); + } + float scale = stbtt_ScaleForPixelHeight(&fontinfo, 64); + int ascent, descent, linegap; + stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap); ascent = roundf(ascent*scale); descent=roundf(descent*scale); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + for (unsigned char c = 32; c < 128; c++) { unsigned char *bitmap; int advance, lsb, w, h; stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb); - bitmap = - stbtt_GetCodepointBitmap(&fontinfo, 0, - stbtt_ScaleForPixelHeight(&fontinfo, - newfont-> - height), c, - &w, &h, 0, 0); + bitmap = stbtt_GetCodepointBitmap(&fontinfo, 0, + stbtt_ScaleForPixelHeight(&fontinfo, newfont->height), c, &w, &h, 0, 0); GLuint ftexture; glGenTextures(1, &ftexture); glBindTexture(GL_TEXTURE_2D, ftexture); - glTexImage2D(GL_TEXTURE_2D, - 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap); //glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, - GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, - GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, - GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, - GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); newfont->Characters[c].TextureID = ftexture; newfont->Characters[c].Advance = advance; @@ -97,6 +102,7 @@ struct sFont *MakeFont(const char *fontfile, int height) glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + return newfont; }