xref: /inferno-os/libmemdraw/defont.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 #include "lib9.h"
2 #include "draw.h"
3 #include "memdraw.h"
4 
5 Memsubfont*
getmemdefont(void)6 getmemdefont(void)
7 {
8 	char *hdr, *p;
9 	int n;
10 	Fontchar *fc;
11 	Memsubfont *f;
12 	int ld;
13 	Rectangle r;
14 	Memdata *md;
15 	Memimage *i;
16 
17 	/*
18 	 * make sure data is word-aligned.  this is true with Plan 9 compilers
19 	 * but not in general.  the byte order is right because the data is
20 	 * declared as char*, not ulong*.
21 	 */
22 	p = (char*)defontdata;
23 	n = (ulong)p & 3;
24 	if(n != 0){
25 		memmove(p+(4-n), p, sizeofdefont-n);
26 		p += 4-n;
27 	}
28 	ld = atoi(p+0*12);
29 	r.min.x = atoi(p+1*12);
30 	r.min.y = atoi(p+2*12);
31 	r.max.x = atoi(p+3*12);
32 	r.max.y = atoi(p+4*12);
33 
34 	md = mallocz(sizeof(Memdata), 1);
35 	if(md == nil)
36 		return nil;
37 
38 	p += 5*12;
39 
40 	md->base = nil;		/* so freememimage doesn't free p */
41 	md->bdata = (uchar*)p;	/* ick */
42 	md->ref = 1;
43 	md->allocd = 1;		/* so freememimage does free md */
44 
45 	i = allocmemimaged(r, drawld2chan[ld], md);
46 	if(i == nil){
47 		free(md);
48 		return nil;
49 	}
50 
51 	hdr = p+Dy(r)*i->width*sizeof(ulong);
52 	n = atoi(hdr);
53 	p = hdr+3*12;
54 	fc = malloc(sizeof(Fontchar)*(n+1));
55 	if(fc == 0){
56 		freememimage(i);
57 		return 0;
58 	}
59 	_unpackinfo(fc, (uchar*)p, n);
60 	f = allocmemsubfont("*default*", n, atoi(hdr+12), atoi(hdr+24), fc, i);
61 	if(f == 0){
62 		freememimage(i);
63 		free(fc);
64 		return 0;
65 	}
66 	return f;
67 }
68