Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
5 static int fontresize(Font*, int, int, int);
6 #if 0
7 static int freeup(Font*);
8 #endif
10 #define PJW 0 /* use NUL==pjw for invisible characters */
12 static Rune empty[] = { 0 };
13 int
14 cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
15 {
16 int i, th, sh, h, ld, w, rw, wid, nc;
17 char *sp;
18 Rune r, *rp, vr;
19 ulong a;
20 Cacheinfo *c, *tc, *ec;
22 if(ss){
23 sp = *ss;
24 rp = empty;
25 }else{
26 sp = "";
27 rp = *rr;
28 }
29 wid = 0;
30 *subfontname = 0;
31 for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
32 if(ss){
33 r = *(uchar*)sp;
34 if(r < Runeself)
35 w = 1;
36 else{
37 w = chartorune(&vr, sp);
38 r = vr;
39 }
40 rw = 0;
41 }else{
42 r = *rp;
43 w = 0;
44 rw = 1;
45 }
47 sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
48 c = &f->cache[sh];
49 ec = c+NFLOOK;
50 h = sh;
51 while(c < ec){
52 if(c->value==r && c->age)
53 goto Found;
54 c++;
55 h++;
56 }
58 /*
59 * Not found; toss out oldest entry
60 */
61 a = ~0;
62 th = sh;
63 tc = &f->cache[th];
64 while(tc < ec){
65 if(tc->age < a){
66 a = tc->age;
67 h = th;
68 c = tc;
69 }
70 tc++;
71 th++;
72 }
74 if(a && (f->age-a)<500){ /* kicking out too recent; resize */
75 nc = 2*(f->ncache-NFLOOK) + NFLOOK;
76 if(nc <= MAXFCACHE){
77 if(i == 0)
78 fontresize(f, f->width, nc, f->maxdepth);
79 /* else flush first; retry will resize */
80 break;
81 }
82 }
84 if(c->age == f->age) /* flush pending string output */
85 break;
87 ld = loadchar(f, r, c, h, i, subfontname);
88 if(ld <= 0){
89 if(ld == 0)
90 continue;
91 break;
92 }
93 c = &f->cache[h]; /* may have reallocated f->cache */
95 Found:
96 wid += c->width;
97 c->age = f->age;
98 cp[i] = h;
99 i++;
101 if(ss)
102 *ss = sp;
103 else
104 *rr = rp;
105 *wp = wid;
106 return i;
109 void
110 agefont(Font *f)
112 Cacheinfo *c, *ec;
113 Cachesubf *s, *es;
115 f->age++;
116 if(f->age == 65536){
117 /*
118 * Renormalize ages
119 */
120 c = f->cache;
121 ec = c+f->ncache;
122 while(c < ec){
123 if(c->age){
124 c->age >>= 2;
125 c->age++;
127 c++;
129 s = f->subf;
130 es = s+f->nsubf;
131 while(s < es){
132 if(s->age){
133 if(s->age<SUBFAGE && s->cf->name != nil){
134 /* clean up */
135 if(display==nil || s->f != display->defaultsubfont)
136 freesubfont(s->f);
137 s->cf = nil;
138 s->f = nil;
139 s->age = 0;
140 }else{
141 s->age >>= 2;
142 s->age++;
145 s++;
147 f->age = (65536>>2) + 1;
151 static Subfont*
152 cf2subfont(Cachefont *cf, Font *f)
154 int depth;
155 char *name;
156 Subfont *sf;
158 name = cf->subfontname;
159 if(name == nil){
160 depth = 0;
161 if(f->display){
162 if(f->display->screenimage)
163 depth = f->display->screenimage->depth;
164 }else
165 depth = 8;
166 name = subfontname(cf->name, f->name, depth);
167 if(name == nil)
168 return nil;
169 cf->subfontname = name;
171 sf = lookupsubfont(f->display, name);
172 return sf;
175 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
176 int
177 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
179 int i, oi, wid, top, bottom;
180 int pic; /* need >16 bits for adding offset below */
181 Fontchar *fi;
182 Cachefont *cf;
183 Cachesubf *subf, *of;
184 uchar *b;
186 pic = r;
187 Again:
188 for(i=0; i<f->nsub; i++){
189 cf = f->sub[i];
190 if(cf->min<=pic && pic<=cf->max)
191 goto Found;
193 TryPJW:
194 if(pic != PJW){
195 pic = PJW;
196 goto Again;
198 return 0;
200 Found:
201 /*
202 * Choose exact or oldest
203 */
204 oi = 0;
205 subf = &f->subf[0];
206 for(i=0; i<f->nsubf; i++){
207 if(cf == subf->cf)
208 goto Found2;
209 if(subf->age < f->subf[oi].age)
210 oi = i;
211 subf++;
213 subf = &f->subf[oi];
215 if(subf->f){
216 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
217 Toss:
218 /* ancient data; toss */
219 freesubfont(subf->f);
220 subf->cf = nil;
221 subf->f = nil;
222 subf->age = 0;
223 }else{ /* too recent; grow instead */
224 of = f->subf;
225 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
226 if(f->subf == nil){
227 f->subf = of;
228 goto Toss;
230 memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
231 memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
232 subf = &f->subf[f->nsubf];
233 f->nsubf += DSUBF;
234 free(of);
237 subf->age = 0;
238 subf->cf = nil;
239 subf->f = cf2subfont(cf, f);
240 if(subf->f == nil){
241 if(cf->subfontname == nil)
242 goto TryPJW;
243 *subfontname = cf->subfontname;
244 return -1;
247 subf->cf = cf;
248 if(subf->f->ascent > f->ascent && f->display){
249 /* should print something? this is a mistake in the font file */
250 /* must prevent c->top from going negative when loading cache */
251 Image *b;
252 int d, t;
253 d = subf->f->ascent - f->ascent;
254 b = subf->f->bits;
255 draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
256 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);
257 for(i=0; i<subf->f->n; i++){
258 t = subf->f->info[i].top-d;
259 if(t < 0)
260 t = 0;
261 subf->f->info[i].top = t;
262 t = subf->f->info[i].bottom-d;
263 if(t < 0)
264 t = 0;
265 subf->f->info[i].bottom = t;
267 subf->f->ascent = f->ascent;
270 Found2:
271 subf->age = f->age;
273 /* possible overflow here, but works out okay */
274 pic += cf->offset;
275 pic -= cf->min;
276 if(pic >= subf->f->n)
277 goto TryPJW;
278 fi = &subf->f->info[pic];
279 if(fi->width == 0)
280 goto TryPJW;
281 wid = (fi+1)->x - fi->x;
282 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
283 /*
284 * Flush, free, reload (easier than reformatting f->b)
285 */
286 if(noflush)
287 return -1;
288 if(f->width < wid)
289 f->width = wid;
290 if(f->maxdepth < subf->f->bits->depth)
291 f->maxdepth = subf->f->bits->depth;
292 i = fontresize(f, f->width, f->ncache, f->maxdepth);
293 if(i <= 0)
294 return i;
295 /* c is still valid as didn't reallocate f->cache */
297 c->value = r;
298 top = fi->top + (f->ascent-subf->f->ascent);
299 bottom = fi->bottom + (f->ascent-subf->f->ascent);
300 c->width = fi->width;
301 c->x = h*f->width;
302 c->left = fi->left;
303 if(f->display == nil)
304 return 1;
305 flushimage(f->display, 0); /* flush any pending errors */
306 b = bufimage(f->display, 37);
307 if(b == 0)
308 return 0;
309 b[0] = 'l';
310 BPLONG(b+1, f->cacheimage->id);
311 BPLONG(b+5, subf->f->bits->id);
312 BPSHORT(b+9, c-f->cache);
313 BPLONG(b+11, c->x);
314 BPLONG(b+15, top);
315 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
316 BPLONG(b+23, bottom);
317 BPLONG(b+27, fi->x);
318 BPLONG(b+31, fi->top);
319 b[35] = fi->left;
320 b[36] = fi->width;
321 return 1;
324 /* release all subfonts, return number freed */
325 #if 0
326 static
327 int
328 freeup(Font *f)
330 Cachesubf *s, *es;
331 int nf;
333 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
334 return 0;
335 s = f->subf;
336 es = s+f->nsubf;
337 nf = 0;
338 while(s < es){
339 if(s->age){
340 freesubfont(s->f);
341 s->cf = nil;
342 s->f = nil;
343 s->age = 0;
344 nf++;
346 s++;
348 return nf;
350 #endif
352 /* return whether resize succeeded && f->cache is unchanged */
353 static int
354 fontresize(Font *f, int wid, int ncache, int depth)
356 Cacheinfo *i;
357 int ret;
358 Image *new;
359 uchar *b;
360 Display *d;
362 ret = 0;
363 if(depth <= 0)
364 depth = 1;
366 d = f->display;
367 if(d == nil)
368 goto Nodisplay;
370 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
371 if(new == nil){
372 fprint(2, "font cache resize failed: %r\n");
373 abort();
374 goto Return;
376 flushimage(d, 0); /* flush any pending errors */
377 b = bufimage(d, 1+4+4+1);
378 if(b == 0){
379 freeimage(new);
380 goto Return;
382 b[0] = 'i';
383 BPLONG(b+1, new->id);
384 BPLONG(b+5, ncache);
385 b[9] = f->ascent;
386 if(flushimage(d, 0) < 0){
387 fprint(2, "resize: init failed: %r\n");
388 freeimage(new);
389 goto Return;
391 freeimage(f->cacheimage);
392 f->cacheimage = new;
393 Nodisplay:
394 f->width = wid;
395 f->maxdepth = depth;
396 ret = 1;
397 if(f->ncache != ncache){
398 i = malloc(ncache*sizeof f->cache[0]);
399 if(i != nil){
400 ret = 0;
401 free(f->cache);
402 f->ncache = ncache;
403 f->cache = i;
405 /* else just wipe the cache clean and things will be ok */
407 Return:
408 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);
409 return ret;