]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/font.c
4eaeb9e8dc4899dfe02af8ec7d7a6d6d5b94f8fa
[plan9front.git] / sys / src / libdraw / font.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 static int      fontresize(Font*, int, int, int);
6 static int      freeup(Font*);
7
8 #define PJW     0       /* use NUL==pjw for invisible characters */
9
10 int
11 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
12 {
13         int i, th, sh, h, w, rw, wid, nc;
14         char *sp;
15         Rune r, *rp, vr;
16         ulong a;
17         Cacheinfo *c, *tc, *ec;
18
19         if(ss){
20                 sp = *ss;
21                 rp = L"";
22         }else{
23                 sp = "";
24                 rp = *rr;
25         }
26         wid = 0;
27         *subfontname = 0;
28         for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
29                 if(ss){
30                         r = *(uchar*)sp;
31                         if(r < Runeself)
32                                 w = 1;
33                         else{
34                                 w = chartorune(&vr, sp);
35                                 r = vr;
36                         }
37                         rw = 0;
38                 }else{
39                         r = *rp;
40                         w = 0;
41                         rw = 1;
42                 }
43
44                 sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
45                 c = &f->cache[sh];
46                 ec = c+NFLOOK;
47                 h = sh;
48                 while(c < ec){
49                         if(c->value==r && c->age)
50                                 goto Found;
51                         c++;
52                         h++;
53                 }
54         
55                 /*
56                  * Not found; toss out oldest entry
57                  */
58                 a = ~0;
59                 th = sh;
60                 tc = &f->cache[th];
61                 while(tc < ec){
62                         if(tc->age < a){
63                                 a = tc->age;
64                                 h = th;
65                                 c = tc;
66                         }
67                         tc++;
68                         th++;
69                 }
70
71                 if(a && (f->age-a)<500){        /* kicking out too recent; resize */
72                         nc = 2*(f->ncache-NFLOOK) + NFLOOK;
73                         if(nc <= MAXFCACHE){
74                                 if(i == 0)
75                                         fontresize(f, f->width, nc, f->maxdepth);
76                                 /* else flush first; retry will resize */
77                                 break;
78                         }
79                 }
80
81                 if(c->age == f->age)    /* flush pending string output */
82                         break;
83
84                 if(loadchar(f, r, c, h, i, subfontname) <= 0)
85                         break;
86
87                 c = &f->cache[h];       /* may have reallocated f->cache */
88         
89             Found:
90                 wid += c->width;
91                 c->age = f->age;
92                 cp[i] = h;
93                 i++;
94         }
95         if(ss)
96                 *ss = sp;
97         else
98                 *rr = rp;
99         *wp = wid;
100         return i;
101 }
102
103 void
104 agefont(Font *f)
105 {
106         Cacheinfo *c, *ec;
107         Cachesubf *s, *es;
108
109         f->age++;
110         if(f->age == 65536){
111                 /*
112                  * Renormalize ages
113                  */
114                 c = f->cache;
115                 ec = c+f->ncache;
116                 while(c < ec){
117                         if(c->age){
118                                 c->age >>= 2;
119                                 c->age++;
120                         }
121                         c++;
122                 }
123                 s = f->subf;
124                 es = s+f->nsubf;
125                 while(s < es){
126                         if(s->age){
127                                 if(s->age<SUBFAGE && s->cf->name != nil){
128                                         /* clean up */
129                                         if(display == nil || s->f != display->defaultsubfont)
130                                                 freesubfont(s->f);
131                                         s->cf = nil;
132                                         s->f = nil;
133                                         s->age = 0;
134                                 }else{
135                                         s->age >>= 2;
136                                         s->age++;
137                                 }
138                         }
139                         s++;
140                 }
141                 f->age = (65536>>2) + 1;
142         }
143 }
144
145 static Subfont*
146 cf2subfont(Cachefont *cf, Font *f)
147 {
148         int depth;
149         char *name;
150         Subfont *sf;
151
152         name = cf->subfontname;
153         if(name == nil){
154                 if(f->display && f->display->screenimage)
155                         depth = f->display->screenimage->depth;
156                 else
157                         depth = 8;
158                 name = subfontname(cf->name, f->name, depth);
159                 if(name == nil)
160                         return nil;
161                 cf->subfontname = name;
162         }
163         sf = lookupsubfont(f->display, name);
164         return sf;
165 }
166
167 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
168 int
169 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
170 {
171         int i, oi, wid, top, bottom;
172         Rune pic;
173         Fontchar *fi;
174         Cachefont *cf;
175         Cachesubf *subf, *of;
176         uchar *b;
177
178         pic = r;
179     Again:
180         for(i=0; i<f->nsub; i++){
181                 cf = f->sub[i];
182                 if(cf->min<=pic && pic<=cf->max)
183                         goto Found;
184         }
185     TryPJW:
186         if(pic != PJW){
187                 pic = PJW;
188                 goto Again;
189         }
190         return 0;
191
192     Found:
193         /*
194          * Choose exact or oldest
195          */
196         oi = 0;
197         subf = &f->subf[0];
198         for(i=0; i<f->nsubf; i++){
199                 if(cf == subf->cf)
200                         goto Found2;
201                 if(subf->age < f->subf[oi].age)
202                         oi = i;
203                 subf++;
204         }
205         subf = &f->subf[oi];
206
207         if(subf->f){
208                 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
209     Toss:
210                         /* ancient data; toss */
211                         freesubfont(subf->f);
212                         subf->cf = nil;
213                         subf->f = nil;
214                         subf->age = 0;
215                 }else{                          /* too recent; grow instead */
216                         of = f->subf;
217                         f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
218                         if(f->subf == nil){
219                                 f->subf = of;
220                                 goto Toss;
221                         }
222                         memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
223                         memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
224                         subf = &f->subf[f->nsubf];
225                         f->nsubf += DSUBF;
226                         free(of);
227                 }
228         }
229         subf->age = 0;
230         subf->cf = nil;
231         subf->f = cf2subfont(cf, f);
232         if(subf->f == nil){
233                 if(cf->subfontname == nil)
234                         goto TryPJW;
235                 *subfontname = cf->subfontname;
236                 return -1;
237         }
238
239         subf->cf = cf;
240         if(subf->f->ascent > f->ascent && f->display){
241                 /* should print something? this is a mistake in the font file */
242                 /* must prevent c->top from going negative when loading cache */
243                 Image *b;
244                 int d, t;
245                 d = subf->f->ascent - f->ascent;
246                 b = subf->f->bits;
247                 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
248                 draw(b, Rect(b->r.min.x, b->r.max.y-d, b->r.max.x, b->r.max.y), f->display->black, nil, b->r.min);
249                 for(i=0; i<subf->f->n; i++){
250                         t = subf->f->info[i].top-d;
251                         if(t < 0)
252                                 t = 0;
253                         subf->f->info[i].top = t;
254                         t = subf->f->info[i].bottom-d;
255                         if(t < 0)
256                                 t = 0;
257                         subf->f->info[i].bottom = t;
258                 }
259                 subf->f->ascent = f->ascent;
260         }
261
262     Found2:
263         subf->age = f->age;
264
265         /* possible overflow here, but works out okay */
266         pic += cf->offset;
267         pic -= cf->min;
268         if(pic >= subf->f->n)
269                 goto TryPJW;
270         fi = &subf->f->info[pic];
271         if(fi->width == 0)
272                 goto TryPJW;
273         wid = (fi+1)->x - fi->x;
274         if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth
275         || (f->display != nil && f->cacheimage == nil)){
276                 /*
277                  * Flush, free, reload (easier than reformatting f->b)
278                  */
279                 if(noflush)
280                         return -1;
281                 if(f->width < wid)
282                         f->width = wid;
283                 if(f->maxdepth < subf->f->bits->depth)
284                         f->maxdepth = subf->f->bits->depth;
285                 i = fontresize(f, f->width, f->ncache, f->maxdepth);
286                 if(i <= 0)
287                         return i;
288                 /* c is still valid as didn't reallocate f->cache */
289         }
290         c->value = r;
291         top = fi->top + (f->ascent-subf->f->ascent);
292         bottom = fi->bottom + (f->ascent-subf->f->ascent);
293         c->width = fi->width;
294         c->x = h*f->width;
295         c->left = fi->left;
296         if(f->display == nil)
297                 return 1;
298         flushimage(f->display, 0);      /* flush any pending errors */
299         b = bufimage(f->display, 37);
300         if(b == 0)
301                 return 0;
302         b[0] = 'l';
303         BPLONG(b+1, f->cacheimage->id);
304         BPLONG(b+5, subf->f->bits->id);
305         BPSHORT(b+9, c-f->cache);
306         BPLONG(b+11, c->x);
307         BPLONG(b+15, top);
308         BPLONG(b+19, c->x+((fi+1)->x-fi->x));
309         BPLONG(b+23, bottom);
310         BPLONG(b+27, fi->x);
311         BPLONG(b+31, fi->top);
312         b[35] = fi->left;
313         b[36] = fi->width;
314         return 1;
315 }
316
317 /* release all subfonts, return number freed */
318 static
319 int
320 freeup(Font *f)
321 {
322         Cachesubf *s, *es;
323         int nf;
324
325         if(f->sub[0]->name == nil)      /* font from mkfont; don't free */
326                 return 0;
327         s = f->subf;
328         es = s+f->nsubf;
329         nf = 0;
330         while(s < es){
331                 if(s->age){
332                         freesubfont(s->f);
333                         s->cf = nil;
334                         s->f = nil;
335                         s->age = 0;
336                         nf++;
337                 }
338                 s++;
339         }
340         return nf;
341 }
342
343 /* return whether resize succeeded && f->cache is unchanged */
344 static int
345 fontresize(Font *f, int wid, int ncache, int depth)
346 {
347         Cacheinfo *i;
348         int ret;
349         Image *new;
350         uchar *b;
351         Display *d;
352
353         ret = 0;
354         if(depth <= 0)
355                 depth = 1;
356         if(wid <= 0)
357                 wid = 1;
358
359         d = f->display;
360         if(d == nil)
361                 goto Nodisplay;
362
363         new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
364         if(new == nil){
365                 fprint(2, "font cache resize failed: %r\n");
366                 goto Return;
367         }
368         flushimage(d, 0);       /* flush any pending errors */
369         b = bufimage(d, 1+4+4+1);
370         if(b == 0){
371                 freeimage(new);
372                 goto Return;
373         }
374         b[0] = 'i';
375         BPLONG(b+1, new->id);
376         BPLONG(b+5, ncache);
377         b[9] = f->ascent;
378         if(flushimage(d, 0) < 0){
379                 fprint(2, "resize: init failed: %r\n");
380                 freeimage(new);
381                 goto Return;
382         }
383         freeimage(f->cacheimage);
384         f->cacheimage = new;
385     Nodisplay:
386         f->width = wid;
387         f->maxdepth = depth;
388         ret = 1;
389         if(f->ncache != ncache){
390                 i = malloc(ncache*sizeof f->cache[0]);
391                 if(i != nil){
392                         ret = 0;
393                         free(f->cache);
394                         f->ncache = ncache;
395                         f->cache = i;
396                 }
397                 /* else just wipe the cache clean and things will be ok */
398         }
399     Return:
400         memset(f->cache, 0, f->ncache*sizeof f->cache[0]);
401         return ret;
402 }