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