aboutsummaryrefslogtreecommitdiff
path: root/font.c
diff options
context:
space:
mode:
authorRiley Beckett <rbeckettvt@gmail.com>2025-10-12 00:35:52 -0400
committerRiley Beckett <rbeckettvt@gmail.com>2025-10-12 01:53:54 -0400
commit91759767b0c7793060f447fd545b639cf91b7ae3 (patch)
tree352721239f5fccde336aec48d6ff6acdb3fbd6ed /font.c
parentd58f2e870b8f998262609b98f91d290b65ab80d3 (diff)
freetype/fontconfig base
Diffstat (limited to 'font.c')
-rw-r--r--font.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/font.c b/font.c
new file mode 100644
index 0000000..9198b3a
--- /dev/null
+++ b/font.c
@@ -0,0 +1,64 @@
+#include <fontconfig/fontconfig.h>
+#include "font.h"
+
+char* get_font(const char* font_name) {
+ FcPattern* pattern = FcNameParse((const FcChar8*)font_name);
+ if (!pattern) {
+ fprintf(stderr, "Failed to create font pattern\n");
+ return NULL;
+ }
+
+ FcConfigSubstitute(0, pattern, FcMatchPattern);
+ FcDefaultSubstitute(pattern);
+
+ FcResult result = 0;
+ FcFontSet* font_patterns = FcFontSort(0, pattern, FcTrue, 0, &result);
+ if (!font_patterns || !font_patterns->nfont) {
+ fprintf(stderr, "No fonts installed on system\n");
+ return NULL;
+ }
+
+ FcFontSet* fs = FcFontSetCreate();
+ FcPattern* font_pattern = FcFontRenderPrepare(0, pattern, font_patterns->fonts[0]);
+ if (!font_pattern) {
+ fprintf(stderr, "Could not prepare matched font for loading\n");
+ return NULL;
+ }
+ FcFontSetAdd(fs, font_pattern);
+
+ FcFontSetSortDestroy(font_patterns);
+ FcPatternDestroy(pattern);
+
+ if (!fs || !font_patterns->nfont) {
+ fprintf(stderr, "Could not prepare font set\n");
+ }
+ FcObjectSet* os = FcObjectSetBuild(FC_FILE, (char*)0);
+ FcValue v = { 0 };
+
+ FcPattern* fontp = FcPatternFilter(fs->fonts[0], os);
+ FcPatternGet(fontp, FC_FILE, 0, &v);
+ char* font = strdup((char*)v.u.f);
+
+ FcPatternDestroy(fontp);
+ FcFontSetDestroy(fs);
+ FcObjectSetDestroy(os);
+
+ return font;
+}
+
+bool init_freetype(client_state* state, const char* file) {
+ if (FT_Init_FreeType(&state->ft_library)) {
+ fprintf(stderr, "Failed to intalize FreeType Library\n");
+ return false;
+ }
+ if (FT_New_Face(state->ft_library, file, 0, &state->ft_face)) {
+ fprintf(stderr, "Failed to create font\n");
+ return false;
+ }
+ if (FT_Set_Char_Size(state->ft_face, 0, 12 << 6, 0, 0)) {
+ fprintf(stderr, "Failed to set font size\n");
+ return false;
+ }
+
+ return true;
+}