]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/buildfont.c
remove debug print
[plan9front.git] / sys / src / libdraw / buildfont.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 static char*
6 skip(char *s)
7 {
8         while(*s==' ' || *s=='\n' || *s=='\t')
9                 s++;
10         return s;
11 }
12
13 Font*
14 buildfont(Display *d, char *buf, char *name)
15 {
16         Font *fnt;
17         Cachefont *c, **sub;
18         char *s, *t;
19         ulong min, max;
20         int offset;
21         char badform[] = "bad font format: number expected (char position %d)";
22
23         s = buf;
24         fnt = malloc(sizeof(Font));
25         if(fnt == nil)
26                 return nil;
27         memset(fnt, 0, sizeof(Font));
28         fnt->display = d;
29         fnt->name = strdup(name);
30         fnt->ncache = NFCACHE+NFLOOK;
31         fnt->nsubf = NFSUBF;
32         fnt->cache = malloc(fnt->ncache * sizeof(fnt->cache[0]));
33         fnt->subf = malloc(fnt->nsubf * sizeof(fnt->subf[0]));
34         if(fnt->name==nil || fnt->cache==nil || fnt->subf==nil){
35     Err2:
36                 free(fnt->name);
37                 free(fnt->cache);
38                 free(fnt->subf);
39                 free(fnt->sub);
40                 free(fnt);
41                 return nil;
42         }
43         fnt->height = strtol(s, &s, 0);
44         s = skip(s);
45         fnt->ascent = strtol(s, &s, 0);
46         s = skip(s);
47         if(fnt->height<=0 || fnt->ascent<=0){
48                 werrstr("bad height or ascent in font file");
49                 goto Err2;
50         }
51         fnt->width = 0;
52         fnt->nsub = 0;
53         fnt->sub = nil;
54
55         memset(fnt->subf, 0, fnt->nsubf * sizeof(fnt->subf[0]));
56         memset(fnt->cache, 0, fnt->ncache*sizeof(fnt->cache[0]));
57         fnt->age = 1;
58         do{
59                 /* must be looking at a number now */
60                 if(*s<'0' || '9'<*s){
61                         werrstr(badform, s-buf);
62                         goto Err3;
63                 }
64                 min = strtol(s, &s, 0);
65                 s = skip(s);
66                 /* must be looking at a number now */
67                 if(*s<'0' || '9'<*s){
68                         werrstr(badform, s-buf);
69                         goto Err3;
70                 }
71                 max = strtol(s, &s, 0);
72                 s = skip(s);
73                 if(*s==0 || min>Runemax || max>Runemax || min>max){
74                         werrstr("illegal subfont range");
75     Err3:
76                         freefont(fnt);
77                         return 0;
78                 }
79                 t = s;
80                 offset = strtol(s, &t, 0);
81                 if(t>s && (*t==' ' || *t=='\t' || *t=='\n'))
82                         s = skip(t);
83                 else
84                         offset = 0;
85                 sub = realloc(fnt->sub, (fnt->nsub+1)*sizeof(Cachefont*));
86                 if(sub == nil)
87                         goto Err3;
88                 fnt->sub = sub;
89                 c = malloc(sizeof(Cachefont));
90                 if(c == nil)
91                         goto Err3;
92                 c->min = min;
93                 c->max = max;
94                 c->offset = offset;
95                 t = s;
96                 while(*s && *s!=' ' && *s!='\n' && *s!='\t')
97                         s++;
98                 *s++ = 0;
99                 c->subfontname = nil;
100                 c->name = strdup(t);
101                 if(c->name == nil){
102                         free(c);
103                         goto Err3;
104                 }
105                 sub[fnt->nsub++] = c;
106                 s = skip(s);
107         }while(*s);
108         return fnt;
109 }
110
111 void
112 freefont(Font *f)
113 {
114         int i;
115         Cachefont *c;
116         Subfont *s;
117
118         if(f == nil)
119                 return;
120
121         for(i=0; i<f->nsub; i++){
122                 c = f->sub[i];
123                 free(c->subfontname);
124                 free(c->name);
125                 free(c);
126         }
127         for(i=0; i<f->nsubf; i++){
128                 s = f->subf[i].f;
129                 if(s != nil){
130                         if(f->display == nil || s != f->display->defaultsubfont)
131                                 freesubfont(s);
132                 }
133         }
134         freeimage(f->cacheimage);
135         free(f->name);
136         free(f->cache);
137         free(f->subf);
138         free(f->sub);
139         free(f);
140 }