#include <draw.h>
static int fontresize(Font*, int, int, int);
-static int freeup(Font*);
#define PJW 0 /* use NUL==pjw for invisible characters */
+/* return number of translated cache indices, 0 must retry, -1 on error */
int
cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
{
- int i, th, sh, h, w, rw, wid, nc;
+ int i, j, h, w, rw, wid, nc;
char *sp;
Rune r, *rp, vr;
ulong a;
rp = *rr;
}
wid = 0;
- *subfontname = 0;
+ *subfontname = nil;
for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
if(ss){
r = *(uchar*)sp;
rw = 1;
}
- sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
- c = &f->cache[sh];
+ a = ~0;
+ h = (17 * (uint)r) & (f->ncache-NFLOOK-1);
+ c = &f->cache[h];
+ tc = c;
ec = c+NFLOOK;
- h = sh;
while(c < ec){
if(c->value==r && c->age)
goto Found;
+ if(c->age < a){
+ a = c->age;
+ tc = c;
+ }
c++;
h++;
}
-
- /*
- * Not found; toss out oldest entry
- */
- a = ~0;
- th = sh;
- tc = &f->cache[th];
- while(tc < ec){
- if(tc->age < a){
- a = tc->age;
- h = th;
- c = tc;
- }
- tc++;
- th++;
- }
+ /* Not found; use oldest entry */
+ c = tc;
+ h = tc - f->cache;
if(a && (f->age-a)<500){ /* kicking out too recent; resize */
nc = 2*(f->ncache-NFLOOK) + NFLOOK;
}
}
- if(c->age == f->age) /* flush pending string output */
+ if(i > 0 && c->age == f->age) /* flush pending string output */
break;
- if(loadchar(f, r, c, h, i, subfontname) <= 0)
- break;
+ j = loadchar(f, r, c, h, i, subfontname);
+ if(j <= 0){
+ if(j < 0 || i > 0) /* flush output or retry */
+ break;
+ return -1; /* stop retrying */
+ }
- c = &f->cache[h]; /* may have reallocated f->cache */
-
Found:
wid += c->width;
c->age = f->age;
if(s->age){
if(s->age<SUBFAGE && s->cf->name != nil){
/* clean up */
- if(display == nil || s->f != display->defaultsubfont)
+ if(f->display == nil || s->f != f->display->defaultsubfont)
freesubfont(s->f);
s->cf = nil;
s->f = nil;
name = cf->subfontname;
if(name == nil){
- if(f->display && f->display->screenimage)
+ if(f->display != nil && f->display->screenimage != nil)
depth = f->display->screenimage->depth;
else
depth = 8;
subf->age = 0;
}else{ /* too recent; grow instead */
of = f->subf;
- f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
+ f->subf = realloc(of, (f->nsubf+DSUBF)*sizeof *subf);
if(f->subf == nil){
f->subf = of;
goto Toss;
}
- memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
- memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
subf = &f->subf[f->nsubf];
+ memset(subf, 0, DSUBF*sizeof *subf);
f->nsubf += DSUBF;
- free(of);
}
}
subf->age = 0;
f->width = wid;
if(f->maxdepth < subf->f->bits->depth)
f->maxdepth = subf->f->bits->depth;
- i = fontresize(f, f->width, f->ncache, f->maxdepth);
- if(i <= 0)
- return i;
+ if(fontresize(f, f->width, f->ncache, f->maxdepth) <= 0)
+ return -1;
/* c is still valid as didn't reallocate f->cache */
}
c->value = r;
- top = fi->top + (f->ascent-subf->f->ascent);
- bottom = fi->bottom + (f->ascent-subf->f->ascent);
c->width = fi->width;
c->x = h*f->width;
c->left = fi->left;
if(f->display == nil)
return 1;
- flushimage(f->display, 0); /* flush any pending errors */
b = bufimage(f->display, 37);
- if(b == 0)
+ if(b == nil)
return 0;
+ top = fi->top + (f->ascent-subf->f->ascent);
+ bottom = fi->bottom + (f->ascent-subf->f->ascent);
b[0] = 'l';
BPLONG(b+1, f->cacheimage->id);
BPLONG(b+5, subf->f->bits->id);
- BPSHORT(b+9, c-f->cache);
+ BPSHORT(b+9, h);
BPLONG(b+11, c->x);
BPLONG(b+15, top);
- BPLONG(b+19, c->x+((fi+1)->x-fi->x));
+ BPLONG(b+19, c->x+wid);
BPLONG(b+23, bottom);
BPLONG(b+27, fi->x);
BPLONG(b+31, fi->top);
return 1;
}
-/* release all subfonts, return number freed */
-static
-int
-freeup(Font *f)
-{
- Cachesubf *s, *es;
- int nf;
-
- if(f->sub[0]->name == nil) /* font from mkfont; don't free */
- return 0;
- s = f->subf;
- es = s+f->nsubf;
- nf = 0;
- while(s < es){
- if(s->age){
- freesubfont(s->f);
- s->cf = nil;
- s->f = nil;
- s->age = 0;
- nf++;
- }
- s++;
- }
- return nf;
-}
-
-/* return whether resize succeeded && f->cache is unchanged */
+/* returns whether resize succeeded && f->cache is unchanged */
static int
fontresize(Font *f, int wid, int ncache, int depth)
{
fprint(2, "font cache resize failed: %r\n");
goto Return;
}
- flushimage(d, 0); /* flush any pending errors */
b = bufimage(d, 1+4+4+1);
- if(b == 0){
+ if(b == nil){
freeimage(new);
goto Return;
}
BPLONG(b+1, new->id);
BPLONG(b+5, ncache);
b[9] = f->ascent;
- if(flushimage(d, 0) < 0){
- fprint(2, "resize: init failed: %r\n");
- freeimage(new);
- goto Return;
- }
freeimage(f->cacheimage);
f->cacheimage = new;
Nodisplay: