Commit: f16d477a58ea9bb2f9df81a3cc45691b02ad65d7
Parent: a6b8f7b4e0ea79ec2b58cbb746bfde2993011efa
Author: Randy Palamar
Date: Sun, 18 Aug 2024 21:27:49 -0600
get font size from fontconfig pattern
This way fallback fonts can use different sizes.
Diffstat:
4 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -1,9 +1,8 @@
/* See LICENSE for copyright details */
static s8 g_default_title = s8("vtgl");
-static u32 g_default_fontsize = 36;
static char *g_fonts[] = {
- "monospace:style=Regular",
+ "monospace:style=Regular:pixelsize=16",
};
static u8 g_tabstop = 8;
diff --git a/font.c b/font.c
@@ -1,6 +1,6 @@
/* See LICENSE for copyright details */
static b32
-init_font(FontAtlas *fa, Font *f, u8 *fontstr, u32 fontsize)
+init_font(FontAtlas *fa, Font *f, u8 *fontstr)
{
f->pattern = FcNameParse(fontstr);
if (!f->pattern) {
@@ -19,6 +19,11 @@ init_font(FontAtlas *fa, Font *f, u8 *fontstr, u32 fontsize)
if (!(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch))
goto err_fc2;
+ f64 pixelsize = 0;
+ if (!(FcPatternGetDouble(font, FC_PIXEL_SIZE, 0, &pixelsize) == FcResultMatch))
+ goto err_fc2;
+ f->fontsize = (u32)pixelsize;
+
os_mapped_file map = os_map_file((char *)file, OS_MAP_READ, OS_MAP_PRIVATE);
f->bufsize = map.len;
@@ -26,7 +31,7 @@ init_font(FontAtlas *fa, Font *f, u8 *fontstr, u32 fontsize)
if (FT_New_Memory_Face(fa->ftlib, f->buf, f->bufsize, 0, &f->face))
goto err_ft1;
- if (FT_Set_Pixel_Sizes(f->face, 0, fontsize))
+ if (FT_Set_Pixel_Sizes(f->face, 0, f->fontsize))
goto err_ft2;
if (FT_Select_Charmap(f->face, FT_ENCODING_UNICODE))
goto err_ft2;
@@ -102,7 +107,7 @@ set_font_sizes(FontAtlas *fa, u32 fontsize)
}
static void
-init_fonts(Term *t, char **fontstrs, u32 nfontstrs, u32 fontsize, Arena *a)
+init_fonts(Term *t, char **fontstrs, u32 nfontstrs, Arena *a)
{
if (FT_Init_FreeType(&t->fa.ftlib))
die("init_fonts: failed to init FreeType\n");
@@ -113,7 +118,7 @@ init_fonts(Term *t, char **fontstrs, u32 nfontstrs, u32 fontsize, Arena *a)
t->fa.fonts = alloc(a, Font, nfontstrs);
for (t->fa.nfonts = 0; t->fa.nfonts < nfontstrs; t->fa.nfonts++) {
valid_atlas |= init_font(&t->fa, t->fa.fonts + t->fa.nfonts,
- (u8 *)fontstrs[t->fa.nfonts], fontsize);
+ (u8 *)fontstrs[t->fa.nfonts]);
}
if (!valid_atlas) {
/* TODO: open some default font */
diff --git a/main.c b/main.c
@@ -273,7 +273,7 @@ main(void)
Term term = {0};
init_window(&term, memory);
- init_fonts(&term, g_fonts, ARRAY_COUNT(g_fonts), g_default_fontsize, &memory);
+ init_fonts(&term, g_fonts, ARRAY_COUNT(g_fonts), &memory);
for (u32 i = 0; i < ARRAY_COUNT(term.saved_cursors); i++) {
cursor_reset(&term);
diff --git a/util.h b/util.h
@@ -254,6 +254,7 @@ typedef struct {
typedef struct {
u8 *buf;
i32 bufsize;
+ u32 fontsize;
FcPattern *pattern;
FT_Face face;
} Font;