5 static int fontresize(Font*, int, int, int);
7 #define PJW 0 /* use NUL==pjw for invisible characters */
9 /* return number of translated cache indices, 0 must retry, -1 on error */
11 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
13 int i, j, 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(i > 0 && c->age == f->age) /* flush pending string output */
84 j = loadchar(f, r, c, h, i, subfontname);
86 if(j < 0 || i > 0) /* flush output or retry */
88 return -1; /* stop retrying */
129 if(s->age<SUBFAGE && s->cf->name != nil){
131 if(f->display == nil || s->f != f->display->defaultsubfont)
143 f->age = (65536>>2) + 1;
148 cf2subfont(Cachefont *cf, Font *f)
154 name = cf->subfontname;
156 if(f->display && f->display->screenimage)
157 depth = f->display->screenimage->depth;
160 name = subfontname(cf->name, f->name, depth);
163 cf->subfontname = name;
165 sf = lookupsubfont(f->display, name);
169 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
171 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
173 int i, oi, wid, top, bottom;
177 Cachesubf *subf, *of;
182 for(i=0; i<f->nsub; i++){
184 if(cf->min<=pic && pic<=cf->max)
196 * Choose exact or oldest
200 for(i=0; i<f->nsubf; i++){
203 if(subf->age < f->subf[oi].age)
210 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
212 /* ancient data; toss */
213 freesubfont(subf->f);
217 }else{ /* too recent; grow instead */
219 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
224 memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
225 memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
226 subf = &f->subf[f->nsubf];
233 subf->f = cf2subfont(cf, f);
235 if(cf->subfontname == nil)
237 *subfontname = cf->subfontname;
242 if(subf->f->ascent > f->ascent && f->display){
243 /* should print something? this is a mistake in the font file */
244 /* must prevent c->top from going negative when loading cache */
247 d = subf->f->ascent - f->ascent;
249 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
250 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);
251 for(i=0; i<subf->f->n; i++){
252 t = subf->f->info[i].top-d;
255 subf->f->info[i].top = t;
256 t = subf->f->info[i].bottom-d;
259 subf->f->info[i].bottom = t;
261 subf->f->ascent = f->ascent;
267 /* possible overflow here, but works out okay */
270 if(pic >= subf->f->n)
272 fi = &subf->f->info[pic];
275 wid = (fi+1)->x - fi->x;
276 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth
277 || (f->display != nil && f->cacheimage == nil)){
279 * Flush, free, reload (easier than reformatting f->b)
285 if(f->maxdepth < subf->f->bits->depth)
286 f->maxdepth = subf->f->bits->depth;
287 if(fontresize(f, f->width, f->ncache, f->maxdepth) <= 0)
289 /* c is still valid as didn't reallocate f->cache */
292 top = fi->top + (f->ascent-subf->f->ascent);
293 bottom = fi->bottom + (f->ascent-subf->f->ascent);
294 c->width = fi->width;
297 if(f->display == nil)
299 flushimage(f->display, 0); /* flush any pending errors */
300 b = bufimage(f->display, 37);
304 BPLONG(b+1, f->cacheimage->id);
305 BPLONG(b+5, subf->f->bits->id);
306 BPSHORT(b+9, c-f->cache);
309 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
310 BPLONG(b+23, bottom);
312 BPLONG(b+31, fi->top);
318 /* returns whether resize succeeded && f->cache is unchanged */
320 fontresize(Font *f, int wid, int ncache, int depth)
338 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
340 fprint(2, "font cache resize failed: %r\n");
343 flushimage(d, 0); /* flush any pending errors */
344 b = bufimage(d, 1+4+4+1);
350 BPLONG(b+1, new->id);
353 if(flushimage(d, 0) < 0){
354 fprint(2, "resize: init failed: %r\n");
358 freeimage(f->cacheimage);
364 if(f->ncache != ncache){
365 i = malloc(ncache*sizeof f->cache[0]);
372 /* else just wipe the cache clean and things will be ok */
375 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);