From 12c955cf1911edc3fbab9d675c14204058b4a134 Mon Sep 17 00:00:00 2001 From: KonAki Date: Thu, 8 Oct 2026 17:23:50 +0800 Subject: Add resizable debug text with ctrl plus and minus --- kon/konfont.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'kon/konfont.h') diff --git a/kon/konfont.h b/kon/konfont.h index 822e1f3..a75c502 100644 --- a/kon/konfont.h +++ b/kon/konfont.h @@ -45,6 +45,10 @@ void kon_freeFont(kon_font_t *font); kon_image *kon_fontGetAtlas(kon_font_t *font); +/* the ttf data has to stay alive for as long as the font, resizing bakes from it again */ +float kon_fontPixelHeight(const kon_font_t *font); +int kon_fontResize(kon_font_t *font, float pixelHeight); + /*** text draw declarations ***/ void kon_drawText(kon_framebuffer_t *fb, kon_font_t *font, int x, int y, const char *text, uint32_t color); @@ -57,6 +61,7 @@ void kon_measureText(kon_font_t *font, const char *text, int *outWidth, int *out #include "stb_truetype.h" struct kon_font { + const uint8_t *ttf; stbtt_bakedchar chardata[KON_FONT_NUM_CHARS]; uint8_t *atlasPixels; int atlasSize; @@ -98,6 +103,7 @@ kon_font_t *kon_loadFont(const uint8_t *ttf_data, size_t size, float pixelHeight kon_font_t *font = malloc(sizeof(kon_font_t)); if (!font) return NULL; + font->ttf = ttf_data; font->atlasPixels = NULL; font->pixelHeight = pixelHeight; @@ -116,6 +122,25 @@ void kon_freeFont(kon_font_t *font) { free(font); } +float kon_fontPixelHeight(const kon_font_t *font) { + return font ? font->pixelHeight : 0.0f; +} + +int kon_fontResize(kon_font_t *font, float pixelHeight) { + kon_font_t next; + + if (!font || pixelHeight <= 0.0f) return 0; + + next = *font; + next.atlasPixels = NULL; + if (!kon_fontBakeAtlas(&next, font->ttf, pixelHeight)) return 0; + + free(font->atlasPixels); + next.pixelHeight = pixelHeight; + *font = next; + return 1; +} + kon_image *kon_fontGetAtlas(kon_font_t *font) { if (!font) return NULL; -- cgit