xref: /inferno-os/libdraw/mkfont.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 
4 /*
5  * Cobble fake font using existing subfont
6  */
7 Font*
mkfont(Subfont * subfont,Rune min)8 mkfont(Subfont *subfont, Rune min)
9 {
10 	Font *font;
11 	Cachefont *c;
12 
13 	font = malloc(sizeof(Font));
14 	if(font == 0)
15 		return 0;
16 	memset(font, 0, sizeof(Font));
17 	font->display = subfont->bits->display;
18 	font->name = strdup("<synthetic>");
19 	font->ncache = NFCACHE+NFLOOK;
20 	font->nsubf = NFSUBF;
21 	font->cache = malloc(font->ncache * sizeof(font->cache[0]));
22 	font->subf = malloc(font->nsubf * sizeof(font->subf[0]));
23 	if(font->name==0 || font->cache==0 || font->subf==0){
24     Err:
25 		free(font->name);
26 		free(font->cache);
27 		free(font->subf);
28 		free(font->sub);
29 		free(font);
30 		return 0;
31 	}
32 	memset(font->cache, 0, font->ncache*sizeof(font->cache[0]));
33 	memset(font->subf, 0, font->nsubf*sizeof(font->subf[0]));
34 	font->height = subfont->height;
35 	font->ascent = subfont->ascent;
36 	font->age = 1;
37 	font->sub = malloc(sizeof(Cachefont*));
38 	if(font->sub == 0)
39 		goto Err;
40 	c = malloc(sizeof(Cachefont));
41 	if(c == 0)
42 		goto Err;
43 	font->nsub = 1;
44 	font->sub[0] = c;
45 	c->min = min;
46 	c->max = min+subfont->n-1;
47 	c->offset = 0;
48 	c->name = 0;	/* noticed by freeup() and agefont() */
49 	c->subfontname = 0;
50 	font->subf[0].age = 0;
51 	font->subf[0].cf = c;
52 	font->subf[0].f = subfont;
53 	return font;
54 }
55