font.c 7.9 KB

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