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; (*sp || *rp) && i<max; 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(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;
165 name = subfontname(cf->name, f->name, depth);
166 if(name == nil)
167 return nil;
168 cf->subfontname = name;
170 sf = lookupsubfont(f->display, name);
171 return sf;
174 /* return 1 if load succeeded, 0 if failed, -1 if must retry */
175 int
176 loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
178 int i, oi, wid, top, bottom;
179 Rune pic;
180 Fontchar *fi;
181 Cachefont *cf;
182 Cachesubf *subf, *of;
183 uchar *b;
185 pic = r;
186 Again:
187 for(i=0; i<f->nsub; i++){
188 cf = f->sub[i];
189 if(cf->min<=pic && pic<=cf->max)
190 goto Found;
192 TryPJW:
193 if(pic != PJW){
194 pic = PJW;
195 goto Again;
197 return 0;
199 Found:
200 /*
201 * Choose exact or oldest
202 */
203 oi = 0;
204 subf = &f->subf[0];
205 for(i=0; i<f->nsubf; i++){
206 if(cf == subf->cf)
207 goto Found2;
208 if(subf->age < f->subf[oi].age)
209 oi = i;
210 subf++;
212 subf = &f->subf[oi];
214 if(subf->f){
215 if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
216 Toss:
217 /* ancient data; toss */
218 freesubfont(subf->f);
219 subf->cf = nil;
220 subf->f = nil;
221 subf->age = 0;
222 }else{ /* too recent; grow instead */
223 of = f->subf;
224 f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
225 if(f->subf == nil){
226 f->subf = of;
227 goto Toss;
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];
232 f->nsubf += DSUBF;
233 free(of);
236 subf->age = 0;
237 subf->cf = nil;
238 subf->f = cf2subfont(cf, f);
239 if(subf->f == nil){
240 if(cf->subfontname == nil)
241 goto TryPJW;
242 *subfontname = cf->subfontname;
243 return -1;
246 subf->cf = cf;
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 */
250 Image *b;
251 int d, t;
252 d = subf->f->ascent - f->ascent;
253 b = subf->f->bits;
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;
258 if(t < 0)
259 t = 0;
260 subf->f->info[i].top = t;
261 t = subf->f->info[i].bottom-d;
262 if(t < 0)
263 t = 0;
264 subf->f->info[i].bottom = t;
266 subf->f->ascent = f->ascent;
269 Found2:
270 subf->age = f->age;
272 pic += cf->offset;
273 if(pic-cf->min >= subf->f->n)
274 goto TryPJW;
275 fi = &subf->f->info[pic - cf->min];
276 if(fi->width == 0)
277 goto TryPJW;
278 wid = (fi+1)->x - fi->x;
279 if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
280 /*
281 * Flush, free, reload (easier than reformatting f->b)
282 */
283 if(noflush)
284 return -1;
285 if(f->width < wid)
286 f->width = wid;
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);
290 if(i <= 0)
291 return i;
292 /* c is still valid as didn't reallocate f->cache */
294 c->value = r;
295 top = fi->top + (f->ascent-subf->f->ascent);
296 bottom = fi->bottom + (f->ascent-subf->f->ascent);
297 c->width = fi->width;
298 c->x = h*f->width;
299 c->left = fi->left;
300 flushimage(f->display, 0); /* flush any pending errors */
301 b = bufimage(f->display, 37);
302 if(b == 0)
303 return 0;
304 b[0] = 'l';
305 BPLONG(b+1, f->cacheimage->id);
306 BPLONG(b+5, subf->f->bits->id);
307 BPSHORT(b+9, c-f->cache);
308 BPLONG(b+11, c->x);
309 BPLONG(b+15, top);
310 BPLONG(b+19, c->x+((fi+1)->x-fi->x));
311 BPLONG(b+23, bottom);
312 BPLONG(b+27, fi->x);
313 BPLONG(b+31, fi->top);
314 b[35] = fi->left;
315 b[36] = fi->width;
316 return 1;
319 /* release all subfonts, return number freed */
320 #if 0
321 static
322 int
323 freeup(Font *f)
325 Cachesubf *s, *es;
326 int nf;
328 if(f->sub[0]->name == nil) /* font from mkfont; don't free */
329 return 0;
330 s = f->subf;
331 es = s+f->nsubf;
332 nf = 0;
333 while(s < es){
334 if(s->age){
335 freesubfont(s->f);
336 s->cf = nil;
337 s->f = nil;
338 s->age = 0;
339 nf++;
341 s++;
343 return nf;
345 #endif
347 /* return whether resize succeeded && f->cache is unchanged */
348 static int
349 fontresize(Font *f, int wid, int ncache, int depth)
351 Cacheinfo *i;
352 int ret;
353 Image *new;
354 uchar *b;
355 Display *d;
357 ret = 0;
358 d = f->display;
359 if(depth <= 0)
360 depth = 1;
362 new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
363 if(new == nil){
364 fprint(2, "font cache resize failed: %r\n");
365 abort();
366 goto Return;
368 flushimage(d, 0); /* flush any pending errors */
369 b = bufimage(d, 1+4+4+1);
370 if(b == 0){
371 freeimage(new);
372 goto Return;
374 b[0] = 'i';
375 BPLONG(b+1, new->id);
376 BPLONG(b+5, ncache);
377 b[9] = f->ascent;
378 if(flushimage(d, 0) < 0){
379 fprint(2, "resize: init failed: %r\n");
380 freeimage(new);
381 goto Return;
383 freeimage(f->cacheimage);
384 f->cacheimage = new;
385 f->width = wid;
386 f->maxdepth = depth;
387 ret = 1;
388 if(f->ncache != ncache){
389 i = malloc(ncache*sizeof f->cache[0]);
390 if(i != nil){
391 ret = 0;
392 free(f->cache);
393 f->ncache = ncache;
394 f->cache = i;
396 /* else just wipe the cache clean and things will be ok */
398 Return:
399 memset(f->cache, 0, f->ncache*sizeof f->cache[0]);
400 return ret;