font.c 7.4 KB

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