font.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. static int fontresize(Font*, int, int, int);
  5. static int freeup(Font*);
  6. #define PJW 0 /* use NUL==pjw for invisible characters */
  7. int
  8. cachechars(Font *f, char **ss, Rune **rr, ushort *cp, int max, int *wp, char **subfontname)
  9. {
  10. int i, th, sh, h, ld, w, rw, wid, nc;
  11. char *sp;
  12. Rune r, *rp, vr;
  13. ulong a;
  14. Cacheinfo *c, *tc, *ec;
  15. if(ss){
  16. sp = *ss;
  17. rp = L"";
  18. }else{
  19. sp = "";
  20. rp = *rr;
  21. }
  22. wid = 0;
  23. *subfontname = 0;
  24. for(i=0; i<max && (*sp || *rp); sp+=w, rp+=rw){
  25. if(ss){
  26. r = *(uchar*)sp;
  27. if(r < Runeself)
  28. w = 1;
  29. else{
  30. w = chartorune(&vr, sp);
  31. r = vr;
  32. }
  33. rw = 0;
  34. }else{
  35. r = *rp;
  36. w = 0;
  37. rw = 1;
  38. }
  39. sh = (17 * (uint)r) & (f->ncache-NFLOOK-1);
  40. c = &f->cache[sh];
  41. ec = c+NFLOOK;
  42. h = sh;
  43. while(c < ec){
  44. if(c->value==r && c->age)
  45. goto Found;
  46. c++;
  47. h++;
  48. }
  49. /*
  50. * Not found; toss out oldest entry
  51. */
  52. a = ~0;
  53. th = sh;
  54. tc = &f->cache[th];
  55. while(tc < ec){
  56. if(tc->age < a){
  57. a = tc->age;
  58. h = th;
  59. c = tc;
  60. }
  61. tc++;
  62. th++;
  63. }
  64. if(a && (f->age-a)<500){ /* kicking out too recent; resize */
  65. nc = 2*(f->ncache-NFLOOK) + NFLOOK;
  66. if(nc <= MAXFCACHE){
  67. if(i == 0)
  68. fontresize(f, f->width, nc, f->maxdepth);
  69. /* else flush first; retry will resize */
  70. break;
  71. }
  72. }
  73. if(c->age == f->age) /* flush pending string output */
  74. break;
  75. ld = loadchar(f, r, c, h, i, subfontname);
  76. if(ld <= 0){
  77. if(ld == 0)
  78. continue;
  79. break;
  80. }
  81. c = &f->cache[h]; /* may have reallocated f->cache */
  82. Found:
  83. wid += c->width;
  84. c->age = f->age;
  85. cp[i] = h;
  86. i++;
  87. }
  88. if(ss)
  89. *ss = sp;
  90. else
  91. *rr = rp;
  92. *wp = wid;
  93. return i;
  94. }
  95. void
  96. agefont(Font *f)
  97. {
  98. Cacheinfo *c, *ec;
  99. Cachesubf *s, *es;
  100. f->age++;
  101. if(f->age == 65536){
  102. /*
  103. * Renormalize ages
  104. */
  105. c = f->cache;
  106. ec = c+f->ncache;
  107. while(c < ec){
  108. if(c->age){
  109. c->age >>= 2;
  110. c->age++;
  111. }
  112. c++;
  113. }
  114. s = f->subf;
  115. es = s+f->nsubf;
  116. while(s < es){
  117. if(s->age){
  118. if(s->age<SUBFAGE && s->cf->name != nil){
  119. /* clean up */
  120. if(s->f != display->defaultsubfont)
  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. int depth;
  139. char *name;
  140. Subfont *sf;
  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. if(f->display->screenimage)
  148. depth = f->display->screenimage->depth;
  149. }
  150. name = subfontname(cf->name, f->name, depth);
  151. if(name == nil)
  152. return nil;
  153. cf->subfontname = name;
  154. }
  155. sf = lookupsubfont(f->display, name);
  156. return sf;
  157. }
  158. /* return 1 if load succeeded, 0 if failed, -1 if must retry */
  159. int
  160. loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
  161. {
  162. int i, oi, wid, top, bottom;
  163. Rune pic;
  164. Fontchar *fi;
  165. Cachefont *cf;
  166. Cachesubf *subf, *of;
  167. uchar *b;
  168. pic = r;
  169. Again:
  170. for(i=0; i<f->nsub; i++){
  171. cf = f->sub[i];
  172. if(cf->min<=pic && pic<=cf->max)
  173. goto Found;
  174. }
  175. TryPJW:
  176. if(pic != PJW){
  177. pic = PJW;
  178. goto Again;
  179. }
  180. return 0;
  181. Found:
  182. /*
  183. * Choose exact or oldest
  184. */
  185. oi = 0;
  186. subf = &f->subf[0];
  187. for(i=0; i<f->nsubf; i++){
  188. if(cf == subf->cf)
  189. goto Found2;
  190. if(subf->age < f->subf[oi].age)
  191. oi = i;
  192. subf++;
  193. }
  194. subf = &f->subf[oi];
  195. if(subf->f){
  196. if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
  197. Toss:
  198. /* ancient data; toss */
  199. freesubfont(subf->f);
  200. subf->cf = nil;
  201. subf->f = nil;
  202. subf->age = 0;
  203. }else{ /* too recent; grow instead */
  204. of = f->subf;
  205. f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
  206. if(f->subf == nil){
  207. f->subf = of;
  208. goto Toss;
  209. }
  210. memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
  211. memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
  212. subf = &f->subf[f->nsubf];
  213. f->nsubf += DSUBF;
  214. free(of);
  215. }
  216. }
  217. subf->age = 0;
  218. subf->cf = nil;
  219. subf->f = cf2subfont(cf, f);
  220. if(subf->f == nil){
  221. if(cf->subfontname == nil)
  222. goto TryPJW;
  223. *subfontname = cf->subfontname;
  224. return -1;
  225. }
  226. subf->cf = cf;
  227. if(subf->f->ascent > f->ascent){
  228. /* should print something? this is a mistake in the font file */
  229. /* must prevent c->top from going negative when loading cache */
  230. Image *b;
  231. int d, t;
  232. d = subf->f->ascent - f->ascent;
  233. b = subf->f->bits;
  234. draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
  235. 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);
  236. for(i=0; i<subf->f->n; i++){
  237. t = subf->f->info[i].top-d;
  238. if(t < 0)
  239. t = 0;
  240. subf->f->info[i].top = t;
  241. t = subf->f->info[i].bottom-d;
  242. if(t < 0)
  243. t = 0;
  244. subf->f->info[i].bottom = t;
  245. }
  246. subf->f->ascent = f->ascent;
  247. }
  248. Found2:
  249. subf->age = f->age;
  250. pic += cf->offset;
  251. if(pic-cf->min >= subf->f->n)
  252. goto TryPJW;
  253. fi = &subf->f->info[pic - cf->min];
  254. if(fi->width == 0)
  255. goto TryPJW;
  256. wid = (fi+1)->x - fi->x;
  257. if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
  258. /*
  259. * Flush, free, reload (easier than reformatting f->b)
  260. */
  261. if(noflush)
  262. return -1;
  263. if(f->width < wid)
  264. f->width = wid;
  265. if(f->maxdepth < subf->f->bits->depth)
  266. f->maxdepth = subf->f->bits->depth;
  267. i = fontresize(f, f->width, f->ncache, f->maxdepth);
  268. if(i <= 0)
  269. return i;
  270. /* c is still valid as didn't reallocate f->cache */
  271. }
  272. c->value = r;
  273. top = fi->top + (f->ascent-subf->f->ascent);
  274. bottom = fi->bottom + (f->ascent-subf->f->ascent);
  275. c->width = fi->width;
  276. c->x = h*f->width;
  277. c->left = fi->left;
  278. flushimage(f->display, 0); /* flush any pending errors */
  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. fprint(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. fprint(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. }