From 5f6789631da08f8cdcdae7ffb2faac1a613b2fa6 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Mon, 6 Apr 2026 23:27:27 +0900 Subject: [PATCH] fix font size --- configure | 2 +- include/Mw/LowLevel.h | 2 +- src/text/ft2.c | 14 +++++++------- src/text/stbtt.c | 4 ++-- src/text/text.c | 4 ++-- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/configure b/configure index 2acabea..0371bcd 100755 --- a/configure +++ b/configure @@ -204,7 +204,7 @@ print(OUT " cp -rf include \$(DESTDIR)\$(PREFIX)/\n"); print(OUT "\n"); print(OUT "format:\n"); print(OUT -" clang-format --verbose -i `find src include examples -name \"*.c\" -or -name \"*.h\" -or -name \"*.m\"`\n" +" clang-format --verbose -i `find src include examples \"(\" -name \"*.c\" -or -name \"*.h\" -or -name \"*.m\" \")\" -and -not -name \"*ttf.c\"`\n" ); print(OUT " perltidy -b -bext=\"/\" --paren-tightness=2 `find tools pl configure -name \"*.pl\"`\n" diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index c38bf32..d5a0b1b 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -253,7 +253,7 @@ MWDECL int MwFL_STBTTSetup(void); MWDECL int (*MwFLDrawText)(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color); MWDECL int (*MwFLTextWidth)(MwFLFont ttf, const char* text); MWDECL int (*MwFLTextHeight)(MwFLFont ttf, int count); -MWDECL void* (*MwFLFontLoad)(unsigned char* data, unsigned int size); +MWDECL void* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px); MWDECL void (*MwFLFontFree)(void* handle); #ifdef __cplusplus diff --git a/src/text/ft2.c b/src/text/ft2.c index 1307dc1..ac25726 100644 --- a/src/text/ft2.c +++ b/src/text/ft2.c @@ -4,8 +4,6 @@ #include #include FT_FREETYPE_H -#define HEIGHT 14 - static struct ft_table { FT_Error (*FT_Init_FreeType)(FT_Library* alibrary); FT_Error (*FT_New_Memory_Face)(FT_Library library, @@ -33,6 +31,7 @@ struct _MwFLFont { FT_Library library; FT_Face face; void* data; + int px; }; static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const char* text, MwLLColor color) { int tw, th; @@ -58,7 +57,7 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c if(c == '\n') { x = 0; - y += ttf->face->height * HEIGHT / ttf->face->units_per_EM; + y += ttf->face->height * ttf->px / ttf->face->units_per_EM; continue; } @@ -68,7 +67,7 @@ static int ft2_MwDrawText(MwWidget handle, MwFLFont ttf, MwPoint* point, const c for(cy = 0; cy < bmp->rows; cy++) { for(cx = 0; cx < bmp->width; cx++) { int ox = x + (ttf->face->glyph->metrics.horiBearingX / 64) + cx + ttf->face->glyph->bitmap_left; - int oy = y + (ttf->face->height * HEIGHT / ttf->face->units_per_EM) - ttf->face->glyph->bitmap_top + cy + (ttf->face->descender * HEIGHT / ttf->face->units_per_EM); + int oy = y + (ttf->face->height * ttf->px / ttf->face->units_per_EM) - ttf->face->glyph->bitmap_top + cy + (ttf->face->descender * ttf->px / ttf->face->units_per_EM); unsigned char* opx = &px[(oy * tw + ox) * 4]; opx[0] = color->common.red; @@ -140,17 +139,18 @@ static int ft2_MwTextWidth(MwFLFont ttf, const char* text) { } static int ft2_MwTextHeight(MwFLFont ttf, int count) { - return ceil((ttf->face->height * HEIGHT / ttf->face->units_per_EM) * count); + return ceil((ttf->face->height * ttf->px / ttf->face->units_per_EM) * count); } -static void* ft2_MwFontLoad(unsigned char* data, unsigned int size) { +static void* ft2_MwFontLoad(unsigned char* data, unsigned int size, int px) { MwFLFont ttf = malloc(sizeof(*ttf)); ttf->data = malloc(size); memcpy(ttf->data, data, size); ft_table.FT_Init_FreeType(&ttf->library); ft_table.FT_New_Memory_Face(ttf->library, ttf->data, size, 0, &ttf->face); - ft_table.FT_Set_Pixel_Sizes(ttf->face, 0, HEIGHT); + ttf->px = px; + ft_table.FT_Set_Pixel_Sizes(ttf->face, 0, ttf->px); return ttf; } diff --git a/src/text/stbtt.c b/src/text/stbtt.c index a37a8b7..ba97279 100644 --- a/src/text/stbtt.c +++ b/src/text/stbtt.c @@ -128,13 +128,13 @@ static int stbtt_MwTextHeight(MwFLFont ttf, int count) { return ceil((ttf->ascent - ttf->descent) * ttf->scale * count); } -static void* stbtt_MwFontLoad(unsigned char* data, unsigned int size) { +static void* stbtt_MwFontLoad(unsigned char* data, unsigned int size, int px) { MwFLFont ttf = malloc(sizeof(*ttf)); ttf->data = malloc(size); memcpy(ttf->data, data, size); stbtt_InitFont(&ttf->font, ttf->data, 0); - ttf->scale = stbtt_ScaleForPixelHeight(&ttf->font, 16); + ttf->scale = stbtt_ScaleForMappingEmToPixels(&ttf->font, px); stbtt_GetFontVMetrics(&ttf->font, &ttf->ascent, &ttf->descent, 0); return ttf; diff --git a/src/text/text.c b/src/text/text.c index e0778b4..57e5785 100644 --- a/src/text/text.c +++ b/src/text/text.c @@ -3,7 +3,7 @@ int (*MwFLDrawText)(MwWidget handle, MwFLFont font, MwPoint* point, const char* text, MwLLColor color) = NULL; int (*MwFLTextWidth)(MwFLFont font, const char* text) = NULL; int (*MwFLTextHeight)(MwFLFont font, int count) = NULL; -void* (*MwFLFontLoad)(unsigned char* data, unsigned int size) = NULL; +void* (*MwFLFontLoad)(unsigned char* data, unsigned int size, int px) = NULL; void (*MwFLFontFree)(void* handle) = NULL; #if defined(USE_FREETYPE2) || defined(USE_STB_TRUETYPE) @@ -116,7 +116,7 @@ int MwTextHeight(MwWidget handle, MwFLFont ttf, const char* text) { void* MwFontLoad(unsigned char* data, unsigned int size) { if(MwFLFontLoad) - return MwFLFontLoad(data, size); + return MwFLFontLoad(data, size, 16); return NULL; }