aboutsummaryrefslogtreecommitdiff
path: root/src/libdraw/subfontname.c
blob: bf397cb5b5f18b964e7060ca4be45d0e7bcf7bb8 (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
#include <u.h>
#include <libc.h>
#include <draw.h>

/*
 * Default version: convert to file name
 */

char*
subfontname(char *cfname, char *fname, int maxdepth)
{
	char *t, *u, tmp1[64], tmp2[64];
	int i;

	if(strcmp(cfname, "*default*") == 0)
		return strdup(cfname);
	t = cfname;
	if(t[0] != '/'){
		snprint(tmp2, sizeof tmp2, "%s", fname);
		u = utfrrune(tmp2, '/');
		if(u)
			u[0] = 0;
		else
			strcpy(tmp2, ".");
		snprint(tmp1, sizeof tmp1, "%s/%s", tmp2, t);
		t = tmp1;
	}

	if(maxdepth > 8)
		maxdepth = 8;

	for(i=3; i>=0; i--){
		if((1<<i) > maxdepth)
			continue;
		/* try i-bit grey */
		snprint(tmp2, sizeof tmp2, "%s.%d", t, i);
		if(access(tmp2, AREAD) == 0)
			return strdup(tmp2);
	}

	/* try default */
	if(access(t, AREAD) == 0)
		return strdup(t);

	return nil;
}