xref: /inferno-os/libdraw/subfontname.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 
4 /*
5  * Default version: convert to file name
6  */
7 
8 char*
subfontname(char * cfname,char * fname,int maxdepth)9 subfontname(char *cfname, char *fname, int maxdepth)
10 {
11 	char *t, *u, tmp1[128], tmp2[128];
12 	int i;
13 
14 	if(strcmp(cfname, "*default*") == 0)
15 		return strdup(cfname);
16 	t = cfname;
17 	if(t[0] != '/'){
18 		snprint(tmp2, sizeof tmp2, "%s", fname);
19 		u = utfrrune(tmp2, '/');
20 		if(u)
21 			u[0] = 0;
22 		else
23 			strcpy(tmp2, ".");
24 		snprint(tmp1, sizeof tmp1, "%s/%s", tmp2, t);
25 		t = tmp1;
26 	}
27 
28 	if(maxdepth > 8)
29 		maxdepth = 8;
30 
31 	for(i=3; i>=0; i--){
32 		if((1<<i) > maxdepth)
33 			continue;
34 		/* try i-bit grey */
35 		snprint(tmp2, sizeof tmp2, "%s.%d", t, i);
36 		if(access(tmp2, AREAD) == 0)
37 			return strdup(tmp2);
38 	}
39 
40 	/* try default */
41 	if(access(t, AREAD) == 0)
42 		return strdup(t);
43 
44 	return nil;
45 }
46