From d02ac078404fbe7b7138baee55d7a0332c544bfe Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Sun, 11 Aug 2024 21:39:59 +0200 Subject: Add fontconfig lookup --- Makefile | 4 ++-- xnote.c | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 821cb9b..f400d3e 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ PREFIX ?= /usr/local BINDIR ?= /bin -LDLIBS = -lglfw -lGL $(shell pkg-config --libs freetype2) -CFLAGS = $(shell pkg-config --cflags freetype2) +LDLIBS = -lglfw -lGL $(shell pkg-config --libs freetype2 fontconfig) +CFLAGS = $(shell pkg-config --cflags freetype2 fontconfig) ifeq ($(DEBUG),1) CFLAGS += -Og -g diff --git a/xnote.c b/xnote.c index a554f42..c35193c 100644 --- a/xnote.c +++ b/xnote.c @@ -1,5 +1,6 @@ #include #include +#include #include #include FT_FREETYPE_H @@ -355,11 +356,27 @@ main(void) push_event(EVENT_START); + const char *fontname = getenv("XNOTE_FONT") ?: "Arial"; + FcConfig *config = FcInitLoadConfigAndFonts(); + FcPattern *pattern = FcNameParse((const FcChar8 *)fontname); + FcConfigSubstitute(config, pattern, FcMatchPattern); + FcDefaultSubstitute(pattern); + FcResult result; + FcPattern *font = FcFontMatch(config, pattern, &result); + if (!font) die("Failed to load font: %s", fontname); + FcChar8 *filepath = NULL; + assert(FcPatternGetString(font, FC_FILE, 0, &filepath) == FcResultMatch); + int fontsize = 0; + assert(FcPatternGetInteger(font, FC_SIZE, 0, &fontsize) == FcResultMatch); + FT_Library freetype; FT_Face face; assert(!FT_Init_FreeType(&freetype)); - assert(!FT_New_Face(freetype, "NotoSansMono-Regular.ttf", 0, &face)); - assert(!FT_Set_Char_Size(face, 16 * 64, 0, 300, 300)); + assert(!FT_New_Face(freetype, (const char *)filepath, 0, &face)); + assert(!FT_Set_Char_Size(face, 0, fontsize * 64, 300, 300)); + + FcPatternDestroy(pattern); + FcConfigDestroy(config); glGenTextures(128, glyphs); for (size_t c = 0; c < 128; c++) { -- cgit v1.2.3-71-gd317