aboutsummaryrefslogtreecommitdiff
path: root/font.c
blob: 9198b3a4f5d52f3eb7d81440d33430c6e7137e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}