5 static int fontresize(Font*, int, int, int);
7 static int freeup(Font*);
10 #define PJW 0 /* use NUL==pjw for invisible characters */
12 static Rune empty[] = { 0 };
14 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
16 int i, th, sh, h, ld, w, rw, wid, nc;
20 Cacheinfo *c, *tc, *ec;
31 for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
37 w = chartorune(&vr, sp);
47 sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
52 if(c->value==r && c->age)
59 * Not found; toss out oldest entry
74 if(a && (f->age-a)<500){ /* kicking out too recent; resize */
75 nc = 2*(f->ncache-NFLOOK) + NFLOOK;
78 fontresize(f, f->width, nc, f->maxdepth);
79 /* else flush first; retry will resize */
84 if(c->age == f->age) /* flush pending string output */
87 ld = loadchar(f, r, c, h, i, subfontname);
93 c = &f->cache[h]; /* may have reallocated f->cache */
133 if(s->age<SUBFAGE && s->cf->name != nil){
135 if(s->f != display->defaultsubfont)
147 f->age = (65536>>2) + 1;
152 cf2subfont(Cachefont *cf, Font *f)
158 name = cf->subfontname;
162 if(f->display->screenimage)
163 depth = f->display->screenimage->depth;
166 name = subfontname(cf->name, f->name, depth);
169 cf->subfontname = name;
171 sf = lookupsubfont(f->display, name);
175 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
177 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
179 int i, oi, wid, top, bottom;
183 Cachesubf *subf, *of;
188 for(i=0; i<f->nsub; i++){
190 if(cf->min<=pic && pic<=cf->max)
202 * Choose exact or oldest
206 for(i=0; i<f->nsubf; i++){
209 if(subf->age < f->subf[oi].age)
216 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
218 /* ancient data; toss */
219 freesubfont(subf->f);
223 }else{ /* too recent; grow instead */
225 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
230 memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
231 memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
232 subf = &f->subf[f->nsubf];
239 subf->f = cf2subfont(cf, f);
241 if(cf->subfontname == nil)
243 *subfontname = cf->subfontname;
248 if(subf->f->ascent > f->ascent && f->display){
249 /* should print something? this is a mistake in the font file */
250 /* must prevent c->top from going negative when loading cache */
253 d = subf->f->ascent - f->ascent;
255 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
256 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);
257 for(i=0; i<subf->f->n; i++){
258 t = subf->f->info[i].top-d;
261 subf->f->info[i].top = t;
262 t = subf->f->info[i].bottom-d;
265 subf->f->info[i].bottom = t;
267 subf->f->ascent = f->ascent;
274 if(pic-cf->min >= subf->f->n)
276 fi = &subf->f->info[pic - cf->min];
279 wid = (fi+1)->x - fi->x;
280 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
282 * Flush, free, reload (easier than reformatting f->b)
288 if(f->maxdepth < subf->f->bits->depth)
289 f->maxdepth = subf->f->bits->depth;
290 i = fontresize(f, f->width, f->ncache, f->maxdepth);
293 /* c is still valid as didn't reallocate f->cache */
296 top = fi->top + (f->ascent-subf->f->ascent);
297 bottom = fi->bottom + (f->ascent-subf->f->ascent);
298 c->width = fi->width;
301 if(f->display == nil)
303 flushimage(f->display, 0); /* flush any pending errors */
304 b = bufimage(f->display, 37);
308 BPLONG(b+1, f->cacheimage->id);
309 BPLONG(b+5, subf->f->bits->id);
310 BPSHORT(b+9, c-f->cache);
313 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
314 BPLONG(b+23, bottom);
316 BPLONG(b+31, fi->top);
322 /* release all subfonts, return number freed */
331 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
350 /* return whether resize succeeded && f->cache is unchanged */
352 fontresize(Font *f, int wid, int ncache, int depth)
368 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
370 fprint(2, "font cache resize failed: %r\n");
374 flushimage(d, 0); /* flush any pending errors */
375 b = bufimage(d, 1+4+4+1);
381 BPLONG(b+1, new->id);
384 if(flushimage(d, 0) < 0){
385 fprint(2, "resize: init failed: %r\n");
389 freeimage(f->cacheimage);
395 if(f->ncache != ncache){
396 i = malloc(ncache*sizeof f->cache[0]);
403 /* else just wipe the cache clean and things will be ok */
406 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);