From 02eb8fc160671ddc0c2042ba5d074d3fc84277fb Mon Sep 17 00:00:00 2001 From: Brett Bergstrom Date: Tue, 22 Sep 2026 18:07:55 -0500 Subject: [PATCH] Fix glyph advance, also fixes rasterization of empty characters in the process --- allegro_schrift.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/allegro_schrift.c b/allegro_schrift.c index dddd860..d0179df 100644 --- a/allegro_schrift.c +++ b/allegro_schrift.c @@ -13,6 +13,7 @@ #include "schrift.h" #include +#include #include "../primitives/allegro5/allegro_primitives.h" @@ -65,7 +66,7 @@ ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, al_get_config_value(system_cfg, "ttf", "cache_text"); if (!cache_text_str) { ALLEGRO_ERROR("No characters defined in cache_text! Using defaults\n"); - cache_text_str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?/()@#$%^&*-_=+<>~`\u007F "; + cache_text_str = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?/()@#$%^&*-_=+<>~`\u007F"; } // read font file @@ -92,7 +93,7 @@ ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, int32_t woff = 1, hoff = 1, hmax = 128; // signed to calculate difference at texture edges gtex = al_create_bitmap(wmax, hmax); al_set_target_bitmap(gtex); - al_clear_to_color(delimit_color); // todo invert alpha at final pass + al_clear_to_color(delimit_color); unsigned int i = 0; const unsigned int len = strlen(cache_text_str); @@ -124,7 +125,8 @@ ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, if (0 > sft_lookup(&sft, cache_text_str[i], &glyph)) goto glyph_lookup_fail; if (0 > sft_gmetrics(&sft, glyph, &geom)) goto glyph_lookup_fail; - if ((wmax - woff) < geom.minWidth + 1) { + const unsigned int rectw = (floor(geom.advanceWidth) > geom.minWidth) ? floor(geom.advanceWidth) : geom.minWidth; + if ((wmax - woff) < rectw + 1) { hoff += rh + 1; woff = 1; } @@ -139,16 +141,18 @@ ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, img.pixels = malloc(img.width * img.height); if (!img.pixels) goto glyph_lookup_oom; if (0 > sft_render(&sft, glyph, img)) goto glyph_lookup_fail; - ALLEGRO_INFO("got raster\n"); + ALLEGRO_INFO("got raster\n"); // i want to use ceiling, it seems more correct? but it still looks too crooked al_draw_filled_rectangle( woff, hoff, - woff + geom.minWidth, + woff + rectw, hoff + rh, al_map_rgba(0, 0, 0, 255) ); + int padding = (int)(geom.advanceWidth - (double)geom.minWidth)/2; + if (padding < 0) padding = 0; block = - al_lock_bitmap_region(gtex, woff, hoff + (geom.yOffset + lh), geom.minWidth, geom.minHeight, + al_lock_bitmap_region(gtex, woff + padding, hoff + (geom.yOffset + lh), geom.minWidth, geom.minHeight, ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8, ALLEGRO_LOCK_WRITEONLY); assert(block->pixel_size == 1); ALLEGRO_INFO("got vram lock\n"); @@ -165,7 +169,7 @@ ALLEGRO_FONT *al_load_ttf_font_f(ALLEGRO_FILE *file, al_unlock_bitmap(gtex); ALLEGRO_INFO("blit raster to vram\n"); - woff += geom.minWidth + 1; + woff += rectw + 1; i++; continue; glyph_lookup_oom: