5 static int fontresize(Font*, int, int, int);
6 static int freeup(Font*);
8 #define PJW 0 /* use NUL==pjw for invisible characters */
11 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
13 int i, th, sh, h, w, rw, wid, nc;
17 Cacheinfo *c, *tc, *ec;
28 for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
34 w = chartorune(&vr, sp);
44 sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
49 if(c->value==r && c->age)
56 * Not found; toss out oldest entry
71 if(a && (f->age-a)<500){ /* kicking out too recent; resize */
72 nc = 2*(f->ncache-NFLOOK) + NFLOOK;
75 fontresize(f, f->width, nc, f->maxdepth);
76 /* else flush first; retry will resize */
81 if(c->age == f->age) /* flush pending string output */
84 if(loadchar(f, r, c, h, i, subfontname) <= 0)
87 c = &f->cache[h]; /* may have reallocated f->cache */
127 if(s->age<SUBFAGE && s->cf->name != nil){
129 if(display == nil || s->f != display->defaultsubfont)
141 f->age = (65536>>2) + 1;
146 cf2subfont(Cachefont *cf, Font *f)
152 name = cf->subfontname;
154 if(f->display && f->display->screenimage)
155 depth = f->display->screenimage->depth;
158 name = subfontname(cf->name, f->name, depth);
161 cf->subfontname = name;
163 sf = lookupsubfont(f->display, name);
167 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
169 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
171 int i, oi, wid, top, bottom;
175 Cachesubf *subf, *of;
180 for(i=0; i<f->nsub; i++){
182 if(cf->min<=pic && pic<=cf->max)
194 * Choose exact or oldest
198 for(i=0; i<f->nsubf; i++){
201 if(subf->age < f->subf[oi].age)
208 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
210 /* ancient data; toss */
211 freesubfont(subf->f);
215 }else{ /* too recent; grow instead */
217 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
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];
231 subf->f = cf2subfont(cf, f);
233 if(cf->subfontname == nil)
235 *subfontname = cf->subfontname;
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 */
245 d = subf->f->ascent - f->ascent;
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;
253 subf->f->info[i].top = t;
254 t = subf->f->info[i].bottom-d;
257 subf->f->info[i].bottom = t;
259 subf->f->ascent = f->ascent;
265 /* possible overflow here, but works out okay */
268 if(pic >= subf->f->n)
270 fi = &subf->f->info[pic];
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)){
277 * Flush, free, reload (easier than reformatting f->b)
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);
288 /* c is still valid as didn't reallocate f->cache */
291 top = fi->top + (f->ascent-subf->f->ascent);
292 bottom = fi->bottom + (f->ascent-subf->f->ascent);
293 c->width = fi->width;
296 if(f->display == nil)
298 flushimage(f->display, 0); /* flush any pending errors */
299 b = bufimage(f->display, 37);
303 BPLONG(b+1, f->cacheimage->id);
304 BPLONG(b+5, subf->f->bits->id);
305 BPSHORT(b+9, c-f->cache);
308 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
309 BPLONG(b+23, bottom);
311 BPLONG(b+31, fi->top);
317 /* release all subfonts, return number freed */
325 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
343 /* return whether resize succeeded && f->cache is unchanged */
345 fontresize(Font *f, int wid, int ncache, int depth)
363 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
365 fprint(2, "font cache resize failed: %r\n");
368 flushimage(d, 0); /* flush any pending errors */
369 b = bufimage(d, 1+4+4+1);
375 BPLONG(b+1, new->id);
378 if(flushimage(d, 0) < 0){
379 fprint(2, "resize: init failed: %r\n");
383 freeimage(f->cacheimage);
389 if(f->ncache != ncache){
390 i = malloc(ncache*sizeof f->cache[0]);
397 /* else just wipe the cache clean and things will be ok */
400 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);