xref: /inferno-os/libdraw/getdefont.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 
4 Subfont*
getdefont(Display * d)5 getdefont(Display *d)
6 {
7 	char *hdr, *p;
8 	int n;
9 	Fontchar *fc;
10 	Subfont *f;
11 	int ld;
12 	Rectangle r;
13 	Image *i;
14 
15 	/*
16 	 * make sure data is word-aligned.  this is true with Plan 9 compilers
17 	 * but not in general.  the byte order is right because the data is
18 	 * declared as char*, not ulong*.
19 	 */
20 	p = (char*)defontdata;
21 	n = (ulong)p & 3;
22 	if(n != 0){
23 		memmove(p+(4-n), p, sizeofdefont-n);
24 		p += 4-n;
25 	}
26 	ld = atoi(p+0*12);
27 	r.min.x = atoi(p+1*12);
28 	r.min.y = atoi(p+2*12);
29 	r.max.x = atoi(p+3*12);
30 	r.max.y = atoi(p+4*12);
31 
32 	i = allocimage(d, r, drawld2chan[ld], 0, 0);
33 	if(i == 0)
34 		return 0;
35 
36 	p += 5*12;
37 	n = loadimage(i, r, (uchar*)p, (defontdata+sizeofdefont)-(uchar*)p);
38 	if(n < 0){
39 		freeimage(i);
40 		return 0;
41 	}
42 
43 	hdr = p+n;
44 	n = atoi(hdr);
45 	p = hdr+3*12;
46 	fc = malloc(sizeof(Fontchar)*(n+1));
47 	if(fc == 0){
48 		freeimage(i);
49 		return 0;
50 	}
51 	_unpackinfo(fc, (uchar*)p, n);
52 	f = allocsubfont("*default*", n, atoi(hdr+12), atoi(hdr+24), fc, i);
53 	if(f == 0){
54 		freeimage(i);
55 		free(fc);
56 		return 0;
57 	}
58 	return f;
59 }
60