aboutsummaryrefslogtreecommitdiff
path: root/src/libdraw/readsubfont.c
blob: 05962640661e86b07e626db68eb694f6011ff3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <u.h>
#include <libc.h>
#include <draw.h>

Subfont*
readsubfonti(Display*d, char *name, int fd, Image *ai, int dolock)
{
	char hdr[3*12+4+1];
	int n;
	uchar *p;
	Fontchar *fc;
	Subfont *f;
	Image *i;

	i = ai;
	if(i == nil){
		i = readimage(d, fd, dolock);
		if(i == nil)
			return nil;
	}
	if(read(fd, hdr, 3*12) != 3*12){
		if(ai == nil)
			freeimage(i);
		werrstr("rdsubfonfile: header read error: %r");
		return nil;
	}
	n = atoi(hdr);
	p = malloc(6*(n+1));
	if(p == nil)
		goto Err;
	if(read(fd, p, 6*(n+1)) != 6*(n+1)){
		werrstr("rdsubfonfile: fontchar read error: %r");
    Err:
		if(ai == nil)
			freeimage(i);
		free(p);
		return nil;
	}
	fc = malloc(sizeof(Fontchar)*(n+1));
	if(fc == nil)
		goto Err;
	_unpackinfo(fc, p, n);
	if(dolock)
		lockdisplay(d);
	f = allocsubfont(name, n, atoi(hdr+12), atoi(hdr+24), fc, i);
	if(dolock)
		unlockdisplay(d);
	if(f == nil){
		free(fc);
		goto Err;
	}
	free(p);
	return f;
}

Subfont*
readsubfont(Display*d, char *name, int fd, int dolock)
{
	return readsubfonti(d, name, fd, nil, dolock);
}