From 5ad541c5ec6b4c5f19bf1938a2c1a9bd7dce79d2 Mon Sep 17 00:00:00 2001 From: KrahJohlito Date: Fri, 15 Nov 2024 14:28:16 +1030 Subject: [PATCH] fonts: add per font sizing for themes - cfg entry example: default_font=myDefaultFont.ttf default_font_size=20 font1=mySmallerFont.ttf font1_size=12 main3: type=ItemsList font=0 main4: type=ItemText font=1 --- include/fntsys.h | 4 +++- src/fntsys.c | 25 ++++++++++++++----------- src/themes.c | 12 +++++++++++- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/fntsys.h b/include/fntsys.h index ea58879aa..2706fe30d 100644 --- a/include/fntsys.h +++ b/include/fntsys.h @@ -6,6 +6,8 @@ /// Value returned on errors #define FNT_ERROR (-1) +#define FNTSYS_DEFAULT_SIZE 17 + /** Initializes the font subsystem */ void fntInit(); @@ -15,7 +17,7 @@ void fntEnd(); /** Loads a font from a file path * @param path The path to the font file * @return font slot id (negative value means error happened) */ -int fntLoadFile(char *path); +int fntLoadFile(char *path, int fontSize); /** Reloads the default font */ int fntLoadDefault(char *path); diff --git a/src/fntsys.c b/src/fntsys.c index 0e55a10c3..35f50f10d 100644 --- a/src/fntsys.c +++ b/src/fntsys.c @@ -27,8 +27,6 @@ extern int size_poeveticanew_raw; /// Atlas height in pixels #define ATLAS_HEIGHT 256 -#define FNTSYS_CHAR_SIZE 17 - // freetype vars static FT_Library font_library; @@ -76,6 +74,8 @@ typedef struct /// Nonzero if font is used int isValid; + int fontSize; + /// Texture atlases (default to NULL) atlas_t *atlases[ATLAS_MAX]; @@ -200,6 +200,7 @@ static void fntInitSlot(font_t *font) font->cacheMaxPageID = -1; font->dataPtr = NULL; font->isValid = 0; + font->fontSize = 0; int aid = 0; for (; aid < ATLAS_MAX; ++aid) @@ -220,6 +221,7 @@ static void fntDeleteSlot(font_t *font) } font->isValid = 0; + font->fontSize = 0; } void fntRelease(int id) @@ -228,7 +230,7 @@ void fntRelease(int id) fntDeleteSlot(&fonts[id]); } -static int fntLoadSlot(font_t *font, char *path) +static int fntLoadSlot(font_t *font, char *path, int fontSize) { void *buffer = NULL; int bufferSize = -1; @@ -256,6 +258,7 @@ static int fntLoadSlot(font_t *font, char *path) } font->isValid = 1; + font->fontSize = fontSize; fntUpdateAspectRatio(); return 0; @@ -285,14 +288,14 @@ void fntInit() fntLoadDefault(NULL); } -int fntLoadFile(char *path) +int fntLoadFile(char *path, int fontSize) { font_t *font; int i = 1; for (; i < FNT_MAX_COUNT; i++) { font = &fonts[i]; if (!font->isValid) { - if (fntLoadSlot(font, path) != FNT_ERROR) + if (fntLoadSlot(font, path, fontSize) != FNT_ERROR) return i; break; } @@ -305,7 +308,7 @@ int fntLoadDefault(char *path) { font_t newFont, oldFont; - if (fntLoadSlot(&newFont, path) != FNT_ERROR) { + if (fntLoadSlot(&newFont, path, FNTSYS_DEFAULT_SIZE) != FNT_ERROR) { // copy over the new font definition // we have to lock this phase, as the old font may still be used // Note: No check for concurrency is done here, which is kinda funky! @@ -454,7 +457,7 @@ void fntUpdateAspectRatio() if (fonts[i].isValid) { fntCacheFlush(&fonts[i]); // TODO: this seems correct, but the rest of the OPL UI (i.e. spacers) doesn't seem to be correctly scaled. - FT_Set_Char_Size(fonts[i].face, FNTSYS_CHAR_SIZE * 64, FNTSYS_CHAR_SIZE * 64, fDPI * ws, fDPI * hs); + FT_Set_Char_Size(fonts[i].face, fonts[i].fontSize * 64, fonts[i].fontSize * 64, fDPI * ws, fDPI * hs); } } } @@ -518,9 +521,9 @@ int fntRenderString(int id, int x, int y, short aligned, size_t width, size_t he } if (aligned & ALIGN_VCENTER) { - y += rmScaleY(FNTSYS_CHAR_SIZE - 4) >> 1; + y += rmScaleY(fonts[id].fontSize - 4) >> 1; } else { - y += rmScaleY(FNTSYS_CHAR_SIZE - 2); + y += rmScaleY(fonts[id].fontSize - 2); } quad.color = colour; @@ -644,9 +647,9 @@ int fntRenderString(int id, int x, int y, short aligned, size_t width, size_t he } if (aligned & ALIGN_VCENTER) { - y += rmScaleY(FNTSYS_CHAR_SIZE - 4) >> 1; + y += rmScaleY(fonts[id].fontSize - 4) >> 1; } else { - y += rmScaleY(FNTSYS_CHAR_SIZE - 2); + y += rmScaleY(fonts[id].fontSize - 2); } quad.color = colour; diff --git a/src/themes.c b/src/themes.c index f3ccdb4f1..338dcb1c7 100644 --- a/src/themes.c +++ b/src/themes.c @@ -1224,8 +1224,18 @@ static void thmLoadFonts(config_set_t *themeConfig, const char *themePath, theme const char *fntFile; if (configGetStr(themeConfig, fntKey, &fntFile)) { snprintf(fullPath, sizeof(fullPath), "%s%s", themePath, fntFile); - int fntHandle = fntLoadFile(fullPath); + int fontSize; + char sizeKey[64]; + if (fntID == 0) + snprintf(sizeKey, sizeof(sizeKey), "default_font_size"); + else + snprintf(sizeKey, sizeof(sizeKey), "font%d_size", fntID); + + if (!configGetInt(themeConfig, sizeKey, &fontSize) || fontSize <= 0) + fontSize = FNTSYS_DEFAULT_SIZE; + + int fntHandle = fntLoadFile(fullPath, fontSize); // Do we have a valid font? Assign the font handle to the theme font slot if (fntHandle != FNT_ERROR) theme->fonts[fntID] = fntHandle;