replace bitmap baking with packed context

This commit is contained in:
Michael Bartlett 2020-10-03 21:49:02 -06:00
parent 79c4f9fcae
commit 695188df52
1 changed files with 13 additions and 11 deletions

View File

@ -14,7 +14,7 @@ struct RenImage {
typedef struct { typedef struct {
RenImage *image; RenImage *image;
stbtt_bakedchar glyphs[256]; stbtt_packedchar glyphs[256];
} GlyphSet; } GlyphSet;
struct RenFont { struct RenFont {
@ -111,16 +111,18 @@ static GlyphSet* load_glyphset(RenFont *font, int idx) {
/* init image */ /* init image */
int width = 128; int width = 128;
int height = 128; int height = 128;
stbtt_pack_context pc;
retry: retry:
set->image = ren_new_image(width, height); set->image = ren_new_image(width, height);
/* load glyphs */
float s = float s =
stbtt_ScaleForMappingEmToPixels(&font->stbfont, 1) / stbtt_ScaleForMappingEmToPixels(&font->stbfont, 1) /
stbtt_ScaleForPixelHeight(&font->stbfont, 1); stbtt_ScaleForPixelHeight(&font->stbfont, 1);
int res = stbtt_BakeFontBitmap(
font->data, 0, font->size * s, (void*) set->image->pixels, stbtt_PackBegin(&pc, (void*) set->image->pixels, width, height, 0, 1, NULL);
width, height, idx * 256, 256, set->glyphs); stbtt_PackSetOversampling(&pc, 1, 1);
int res = stbtt_PackFontRange(&pc, font->data, 0, font->size * s, idx*256, 256, set->glyphs);
stbtt_PackEnd(&pc);
/* retry with a larger image buffer if the buffer wasn't large enough */ /* retry with a larger image buffer if the buffer wasn't large enough */
if (res < 0) { if (res < 0) {
@ -189,10 +191,10 @@ RenFont* ren_load_font(const char *filename, float size) {
font->height = (ascent - descent + linegap) * scale + 0.5; font->height = (ascent - descent + linegap) * scale + 0.5;
/* make tab and newline glyphs invisible */ /* make tab and newline glyphs invisible */
stbtt_bakedchar *g = get_glyphset(font, '\n')->glyphs; stbtt_packedchar *g = get_glyphset(font, '\n')->glyphs;
g['\t'].x1 = g['\t'].x0; g['\t'].x1 = g['\t'].x0;
g['\n'].x1 = g['\n'].x0; g['\n'].x1 = g['\n'].x0;
return font; return font;
fail: fail:
@ -235,7 +237,7 @@ int ren_get_font_width(RenFont *font, const char *text) {
while (*p) { while (*p) {
p = utf8_to_codepoint(p, &codepoint); p = utf8_to_codepoint(p, &codepoint);
GlyphSet *set = get_glyphset(font, codepoint); GlyphSet *set = get_glyphset(font, codepoint);
stbtt_bakedchar *g = &set->glyphs[codepoint & 0xff]; stbtt_packedchar *g = &set->glyphs[codepoint & 0xff];
x += g->xadvance; x += g->xadvance;
} }
return x; return x;
@ -340,7 +342,7 @@ int ren_draw_text(RenFont *font, const char *text, int x, int y, RenColor color)
while (*p) { while (*p) {
p = utf8_to_codepoint(p, &codepoint); p = utf8_to_codepoint(p, &codepoint);
GlyphSet *set = get_glyphset(font, codepoint); GlyphSet *set = get_glyphset(font, codepoint);
stbtt_bakedchar *g = &set->glyphs[codepoint & 0xff]; stbtt_packedchar *g = &set->glyphs[codepoint & 0xff];
rect.x = g->x0; rect.x = g->x0;
rect.y = g->y0; rect.y = g->y0;
rect.width = g->x1 - g->x0; rect.width = g->x1 - g->x0;