]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/openfont.c
libaml: fix gc bug, need to amltake()/amldrop() temporary buffer
[plan9front.git] / sys / src / libdraw / openfont.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 static char*
6 readfile(char *name)
7 {
8         enum { HUNK = 8*1024, };
9         int f, n, r;
10         char *s, *p;
11
12         n = 0;
13         r = -1;
14         if((s = malloc(HUNK)) != nil){
15                 if((f = open(name, OREAD|OCEXEC)) >= 0){
16                         while((r = read(f, s+n, HUNK)) > 0){
17                                 n += r;
18                                 r = -1;
19                                 if((p = realloc(s, n+HUNK)) == nil)
20                                         break;
21                                 s = p;
22                         }
23                         close(f);
24                 }
25         }
26         if(r < 0 || (p = realloc(s, n+1)) == nil){
27                 free(s);
28                 return nil;
29         }
30         p[n] = 0;
31         return p;
32 }
33
34 Font*
35 openfont(Display *d, char *name)
36 {
37         Font *fnt;
38         char *buf;
39
40         if((buf = readfile(name)) == nil){
41                 werrstr("openfont: %r");
42                 return nil;
43         }
44         fnt = buildfont(d, buf, name);
45         free(buf);
46         return fnt;
47 }