Fix glyph advance, also fixes rasterization of empty characters in the process

This commit is contained in:
2026-09-22 18:07:55 -05:00
parent 6e79035cc3
commit 02eb8fc160

View File

@@ -13,6 +13,7 @@
#include "schrift.h"
#include <stdlib.h>
#include <math.h>
#include "../primitives/allegro5/allegro_primitives.h"
@@ -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: