summaryrefslogtreecommitdiff
path: root/sys/src/libdraw/openfont.c
blob: 6d4aa2d2ff4af514169a71b7ae2526a6fcd10506 (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
#include <u.h>
#include <libc.h>
#include <draw.h>

static char*
readfile(char *name)
{
	enum { HUNK = 8*1024, };
	int f, n, r;
	char *s, *p;

	n = 0;
	r = -1;
	if((s = malloc(HUNK)) != nil){
		if((f = open(name, OREAD|OCEXEC)) >= 0){
			while((r = read(f, s+n, HUNK)) > 0){
				n += r;
				r = -1;
				if((p = realloc(s, n+HUNK)) == nil)
					break;
				s = p;
			}
			close(f);
		}
	}
	if(r < 0 || (p = realloc(s, n+1)) == nil){
		free(s);
		return nil;
	}
	p[n] = 0;
	return p;
}

Font*
openfont(Display *d, char *name)
{
	Font *fnt;
	char *buf;

	if((buf = readfile(name)) == nil){
		werrstr("openfont: %r");
		return nil;
	}
	fnt = buildfont(d, buf, name);
	free(buf);
	return fnt;
}