stbtt truetype works

This commit is contained in:
John Alanbrook 2022-06-23 22:41:30 +00:00
parent 2e267e0b39
commit 6a681764af

View file

@ -8,6 +8,8 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
#include <stb_truetype.h> #include <stb_truetype.h>
static uint32_t VBO = 0; static uint32_t VBO = 0;
@ -30,50 +32,53 @@ struct sFont *MakeFont(const char *fontfile, int height)
char fontpath[256]; char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile); snprintf(fontpath, 256, "fonts/%s", fontfile);
stbtt_fontinfo fontinfo = { 0 };
int ascent, descent, linegap;
fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb")); 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"); 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<tw; ++j)
putchar(" .:ioVM@"[testbitmap[i*tw+j]>>5]);
putchar('\n');
}
float scale = stbtt_ScaleForPixelHeight(&fontinfo, 64); float scale = stbtt_ScaleForPixelHeight(&fontinfo, 64);
int ascent, descent, linegap;
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap); stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
ascent = roundf(ascent*scale); ascent = roundf(ascent*scale);
descent=roundf(descent*scale); descent=roundf(descent*scale);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 32; c < 128; c++) { for (unsigned char c = 32; c < 128; c++) {
unsigned char *bitmap; unsigned char *bitmap;
int advance, lsb, w, h; int advance, lsb, w, h;
stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb); stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb);
bitmap = bitmap = stbtt_GetCodepointBitmap(&fontinfo, 0,
stbtt_GetCodepointBitmap(&fontinfo, 0, stbtt_ScaleForPixelHeight(&fontinfo, newfont->height), c, &w, &h, 0, 0);
stbtt_ScaleForPixelHeight(&fontinfo,
newfont->
height), c,
&w, &h, 0, 0);
GLuint ftexture; GLuint ftexture;
glGenTextures(1, &ftexture); glGenTextures(1, &ftexture);
glBindTexture(GL_TEXTURE_2D, ftexture); glBindTexture(GL_TEXTURE_2D, ftexture);
glTexImage2D(GL_TEXTURE_2D, glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
//glGenerateMipmap(GL_TEXTURE_2D); //glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
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].TextureID = ftexture;
newfont->Characters[c].Advance = advance; newfont->Characters[c].Advance = advance;
@ -97,6 +102,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
return newfont; return newfont;
} }