aboutsummaryrefslogtreecommitdiff
path: root/draw/initfont.c
blob: 77d3182f7ab8f0369e2df9973f6aa5702a23a3f3 (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
/* See LICENSE file for copyright and license details. */
#include <X11/Xlib.h>
#include "draw.h"

#define MAX(a, b)               ((a) > (b) ? (a) : (b))

void
initfont(DC *dc, const char *fontstr) {
	char *def, **missing = NULL;
	int i, n;

	if(!fontstr || !*fontstr)
		eprint("cannot load null font\n");
	dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def);
	if(missing)
		XFreeStringList(missing);
	if(dc->font.set) {
		XFontStruct **xfonts;
		char **font_names;
		dc->font.ascent = dc->font.descent = 0;
		n = XFontsOfFontSet(dc->font.set, &xfonts, &font_names);
		for(i = 0; i < n; i++) {
			dc->font.ascent = MAX(dc->font.ascent, (*xfonts)->ascent);
			dc->font.descent = MAX(dc->font.descent, (*xfonts)->descent);
			xfonts++;
		}
	}
	else {
		if(!(dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))
		&& !(dc->font.xfont = XLoadQueryFont(dc->dpy, "fixed")))
			eprint("cannot load font '%s'\n", fontstr);
		dc->font.ascent = dc->font.xfont->ascent;
		dc->font.descent = dc->font.xfont->descent;
	}
	dc->font.height = dc->font.ascent + dc->font.descent;
}