5 static int fontresize(Font*, int, int, int);
6 static int freeup(Font*);
8 #define PJW 0 /* use NUL==pjw for invisible characters */
10 /* return number of translated cache indices, 0 must retry, -1 on error */
12 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
14 int i, j, th, sh, h, w, rw, wid, nc;
18 Cacheinfo *c, *tc, *ec;
29 for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
35 w = chartorune(&vr, sp);
45 sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
50 if(c->value==r && c->age)
57 * Not found; toss out oldest entry
72 if(a && (f->age-a)<500){ /* kicking out too recent; resize */
73 nc = 2*(f->ncache-NFLOOK) + NFLOOK;
76 fontresize(f, f->width, nc, f->maxdepth);
77 /* else flush first; retry will resize */
82 if(i > 0 && c->age == f->age) /* flush pending string output */
85 j = loadchar(f, r, c, h, i, subfontname);
87 if(j < 0 || i > 0) /* flush output or retry */
89 return -1; /* stop retrying */
130 if(s->age<SUBFAGE && s->cf->name != nil){
132 if(display == nil || s->f != display->defaultsubfont)
144 f->age = (65536>>2) + 1;
149 cf2subfont(Cachefont *cf, Font *f)
155 name = cf->subfontname;
157 if(f->display && f->display->screenimage)
158 depth = f->display->screenimage->depth;
161 name = subfontname(cf->name, f->name, depth);
164 cf->subfontname = name;
166 sf = lookupsubfont(f->display, name);
170 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
172 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
174 int i, oi, wid, top, bottom;
178 Cachesubf *subf, *of;
183 for(i=0; i<f->nsub; i++){
185 if(cf->min<=pic && pic<=cf->max)
197 * Choose exact or oldest
201 for(i=0; i<f->nsubf; i++){
204 if(subf->age < f->subf[oi].age)
211 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
213 /* ancient data; toss */
214 freesubfont(subf->f);
218 }else{ /* too recent; grow instead */
220 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
225 memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
226 memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
227 subf = &f->subf[f->nsubf];
234 subf->f = cf2subfont(cf, f);
236 if(cf->subfontname == nil)
238 *subfontname = cf->subfontname;
243 if(subf->f->ascent > f->ascent && f->display){
244 /* should print something? this is a mistake in the font file */
245 /* must prevent c->top from going negative when loading cache */
248 d = subf->f->ascent - f->ascent;
250 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
251 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);
252 for(i=0; i<subf->f->n; i++){
253 t = subf->f->info[i].top-d;
256 subf->f->info[i].top = t;
257 t = subf->f->info[i].bottom-d;
260 subf->f->info[i].bottom = t;
262 subf->f->ascent = f->ascent;
268 /* possible overflow here, but works out okay */
271 if(pic >= subf->f->n)
273 fi = &subf->f->info[pic];
276 wid = (fi+1)->x - fi->x;
277 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth
278 || (f->display != nil && f->cacheimage == nil)){
280 * Flush, free, reload (easier than reformatting f->b)
286 if(f->maxdepth < subf->f->bits->depth)
287 f->maxdepth = subf->f->bits->depth;
288 if(fontresize(f, f->width, f->ncache, f->maxdepth) <= 0)
290 /* c is still valid as didn't reallocate f->cache */
293 top = fi->top + (f->ascent-subf->f->ascent);
294 bottom = fi->bottom + (f->ascent-subf->f->ascent);
295 c->width = fi->width;
298 if(f->display == nil)
300 flushimage(f->display, 0); /* flush any pending errors */
301 b = bufimage(f->display, 37);
305 BPLONG(b+1, f->cacheimage->id);
306 BPLONG(b+5, subf->f->bits->id);
307 BPSHORT(b+9, c-f->cache);
310 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
311 BPLONG(b+23, bottom);
313 BPLONG(b+31, fi->top);
319 /* release all subfonts, return number freed */
327 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
345 /* returns whether resize succeeded && f->cache is unchanged */
347 fontresize(Font *f, int wid, int ncache, int depth)
365 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
367 fprint(2, "font cache resize failed: %r\n");
370 flushimage(d, 0); /* flush any pending errors */
371 b = bufimage(d, 1+4+4+1);
377 BPLONG(b+1, new->id);
380 if(flushimage(d, 0) < 0){
381 fprint(2, "resize: init failed: %r\n");
385 freeimage(f->cacheimage);
391 if(f->ncache != ncache){
392 i = malloc(ncache*sizeof f->cache[0]);
399 /* else just wipe the cache clean and things will be ok */
402 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);