Skip to content

Commit

Permalink
fonts: add per font sizing for themes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
KrahJohlito committed Nov 18, 2024
1 parent 1430917 commit 5ad541c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion include/fntsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/// Value returned on errors
#define FNT_ERROR (-1)

#define FNTSYS_DEFAULT_SIZE 17

/** Initializes the font subsystem */
void fntInit();

Expand All @@ -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);
Expand Down
25 changes: 14 additions & 11 deletions src/fntsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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)
Expand All @@ -220,6 +221,7 @@ static void fntDeleteSlot(font_t *font)
}

font->isValid = 0;
font->fontSize = 0;
}

void fntRelease(int id)
Expand All @@ -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;
Expand Down Expand Up @@ -256,6 +258,7 @@ static int fntLoadSlot(font_t *font, char *path)
}

font->isValid = 1;
font->fontSize = fontSize;
fntUpdateAspectRatio();

return 0;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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!
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion src/themes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5ad541c

Please sign in to comment.