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, 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);
45 h = (17 * (uint)r) & (f->ncache-NFLOOK-1);
50 if(c->value==r && c->age)
59 /* Not found; use oldest entry */
63 if(a && (f->age-a)<500){ /* kicking out too recent; resize */
64 nc = 2*(f->ncache-NFLOOK) + NFLOOK;
67 fontresize(f, f->width, nc, f->maxdepth);
68 /* else flush first; retry will resize */
73 if(i > 0 && c->age == f->age) /* flush pending string output */
76 j = loadchar(f, r, c, h, i, subfontname);
78 if(j < 0 || i > 0) /* flush output or retry */
80 return -1; /* stop retrying */
121 if(s->age<SUBFAGE && s->cf->name != nil){
123 if(f->display == nil || s->f != f->display->defaultsubfont)
135 f->age = (65536>>2) + 1;
140 cf2subfont(Cachefont *cf, Font *f)
146 name = cf->subfontname;
148 if(f->display != nil && f->display->screenimage != nil)
149 depth = f->display->screenimage->depth;
152 name = subfontname(cf->name, f->name, depth);
155 cf->subfontname = name;
157 sf = lookupsubfont(f->display, name);
161 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
163 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
165 int i, oi, wid, top, bottom;
169 Cachesubf *subf, *of;
174 for(i=0; i<f->nsub; i++){
176 if(cf->min<=pic && pic<=cf->max)
188 * Choose exact or oldest
192 for(i=0; i<f->nsubf; i++){
195 if(subf->age < f->subf[oi].age)
202 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
204 /* ancient data; toss */
205 freesubfont(subf->f);
209 }else{ /* too recent; grow instead */
211 f->subf = realloc(of, (f->nsubf+DSUBF)*sizeof *subf);
216 subf = &f->subf[f->nsubf];
217 memset(subf, 0, DSUBF*sizeof *subf);
223 subf->f = cf2subfont(cf, f);
225 if(cf->subfontname == nil)
227 *subfontname = cf->subfontname;
232 if(subf->f->ascent > f->ascent && f->display){
233 /* should print something? this is a mistake in the font file */
234 /* must prevent c->top from going negative when loading cache */
237 d = subf->f->ascent - f->ascent;
239 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
240 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);
241 for(i=0; i<subf->f->n; i++){
242 t = subf->f->info[i].top-d;
245 subf->f->info[i].top = t;
246 t = subf->f->info[i].bottom-d;
249 subf->f->info[i].bottom = t;
251 subf->f->ascent = f->ascent;
257 /* possible overflow here, but works out okay */
260 if(pic >= subf->f->n)
262 fi = &subf->f->info[pic];
265 wid = (fi+1)->x - fi->x;
266 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth
267 || (f->display != nil && f->cacheimage == nil)){
269 * Flush, free, reload (easier than reformatting f->b)
275 if(f->maxdepth < subf->f->bits->depth)
276 f->maxdepth = subf->f->bits->depth;
277 if(fontresize(f, f->width, f->ncache, f->maxdepth) <= 0)
279 /* c is still valid as didn't reallocate f->cache */
282 c->width = fi->width;
285 if(f->display == nil)
287 b = bufimage(f->display, 37);
290 top = fi->top + (f->ascent-subf->f->ascent);
291 bottom = fi->bottom + (f->ascent-subf->f->ascent);
293 BPLONG(b+1, f->cacheimage->id);
294 BPLONG(b+5, subf->f->bits->id);
298 BPLONG(b+19, c->x+wid);
299 BPLONG(b+23, bottom);
301 BPLONG(b+31, fi->top);
307 /* returns whether resize succeeded && f->cache is unchanged */
309 fontresize(Font *f, int wid, int ncache, int depth)
327 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
329 fprint(2, "font cache resize failed: %r\n");
332 b = bufimage(d, 1+4+4+1);
338 BPLONG(b+1, new->id);
341 freeimage(f->cacheimage);
347 if(f->ncache != ncache){
348 i = malloc(ncache*sizeof f->cache[0]);
355 /* else just wipe the cache clean and things will be ok */
358 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);