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;
165 name = subfontname(cf->name, f->name, depth);
168 cf->subfontname = name;
170 sf = lookupsubfont(f->display, name);
174 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
176 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
178 int i, oi, wid, top, bottom;
182 Cachesubf *subf, *of;
187 for(i=0; i<f->nsub; i++){
189 if(cf->min<=pic && pic<=cf->max)
201 * Choose exact or oldest
205 for(i=0; i<f->nsubf; i++){
208 if(subf->age < f->subf[oi].age)
215 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
217 /* ancient data; toss */
218 freesubfont(subf->f);
222 }else{ /* too recent; grow instead */
224 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
229 memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
230 memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
231 subf = &f->subf[f->nsubf];
238 subf->f = cf2subfont(cf, f);
240 if(cf->subfontname == nil)
242 *subfontname = cf->subfontname;
247 if(subf->f->ascent > f->ascent){
248 /* should print something? this is a mistake in the font file */
249 /* must prevent c->top from going negative when loading cache */
252 d = subf->f->ascent - f->ascent;
254 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
255 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);
256 for(i=0; i<subf->f->n; i++){
257 t = subf->f->info[i].top-d;
260 subf->f->info[i].top = t;
261 t = subf->f->info[i].bottom-d;
264 subf->f->info[i].bottom = t;
266 subf->f->ascent = f->ascent;
273 if(pic-cf->min >= subf->f->n)
275 fi = &subf->f->info[pic - cf->min];
278 wid = (fi+1)->x - fi->x;
279 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
281 * Flush, free, reload (easier than reformatting f->b)
287 if(f->maxdepth < subf->f->bits->depth)
288 f->maxdepth = subf->f->bits->depth;
289 i = fontresize(f, f->width, f->ncache, f->maxdepth);
292 /* c is still valid as didn't reallocate f->cache */
295 top = fi->top + (f->ascent-subf->f->ascent);
296 bottom = fi->bottom + (f->ascent-subf->f->ascent);
297 c->width = fi->width;
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 */
328 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
347 /* return whether resize succeeded && f->cache is unchanged */
349 fontresize(Font *f, int wid, int ncache, int depth)
362 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
364 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);
388 if(f->ncache != ncache){
389 i = malloc(ncache*sizeof f->cache[0]);
396 /* else just wipe the cache clean and things will be ok */
399 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);