font.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. if(f->display && f->display->screenimage)
  144. depth = f->display->screenimage->depth;
  145. else
  146. depth = 8;
  147. name = subfontname(cf->name, f->name, depth);
  148. if(name == nil)
  149. return nil;
  150. cf->subfontname = name;
  151. }
  152. sf = lookupsubfont(f->display, name);
  153. return sf;
  154. }
  155. /* return 1 if load succeeded, 0 if failed, -1 if must retry */
  156. int
  157. loadchar(Font *f, Rune r, Cacheinfo *c, int h, int noflush, char **subfontname)
  158. {
  159. int i, oi, wid, top, bottom;
  160. Rune pic;
  161. Fontchar *fi;
  162. Cachefont *cf;
  163. Cachesubf *subf, *of;
  164. uchar *b;
  165. pic = r;
  166. Again:
  167. for(i=0; i<f->nsub; i++){
  168. cf = f->sub[i];
  169. if(cf->min<=pic && pic<=cf->max)
  170. goto Found;
  171. }
  172. TryPJW:
  173. if(pic != PJW){
  174. pic = PJW;
  175. goto Again;
  176. }
  177. return 0;
  178. Found:
  179. /*
  180. * Choose exact or oldest
  181. */
  182. oi = 0;
  183. subf = &f->subf[0];
  184. for(i=0; i<f->nsubf; i++){
  185. if(cf == subf->cf)
  186. goto Found2;
  187. if(subf->age < f->subf[oi].age)
  188. oi = i;
  189. subf++;
  190. }
  191. subf = &f->subf[oi];
  192. if(subf->f){
  193. if(f->age-subf->age>SUBFAGE || f->nsubf>MAXSUBF){
  194. Toss:
  195. /* ancient data; toss */
  196. freesubfont(subf->f);
  197. subf->cf = nil;
  198. subf->f = nil;
  199. subf->age = 0;
  200. }else{ /* too recent; grow instead */
  201. of = f->subf;
  202. f->subf = malloc((f->nsubf+DSUBF)*sizeof *subf);
  203. if(f->subf == nil){
  204. f->subf = of;
  205. goto Toss;
  206. }
  207. memmove(f->subf, of, (f->nsubf+DSUBF)*sizeof *subf);
  208. memset(f->subf+f->nsubf, 0, DSUBF*sizeof *subf);
  209. subf = &f->subf[f->nsubf];
  210. f->nsubf += DSUBF;
  211. free(of);
  212. }
  213. }
  214. subf->age = 0;
  215. subf->cf = nil;
  216. subf->f = cf2subfont(cf, f);
  217. if(subf->f == nil){
  218. if(cf->subfontname == nil)
  219. goto TryPJW;
  220. *subfontname = cf->subfontname;
  221. return -1;
  222. }
  223. subf->cf = cf;
  224. if(subf->f->ascent > f->ascent && f->display){
  225. /* should print something? this is a mistake in the font file */
  226. /* must prevent c->top from going negative when loading cache */
  227. Image *b;
  228. int d, t;
  229. d = subf->f->ascent - f->ascent;
  230. b = subf->f->bits;
  231. draw(b, b->r, b, nil, addpt(b->r.min, Pt(0, d)));
  232. 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);
  233. for(i=0; i<subf->f->n; i++){
  234. t = subf->f->info[i].top-d;
  235. if(t < 0)
  236. t = 0;
  237. subf->f->info[i].top = t;
  238. t = subf->f->info[i].bottom-d;
  239. if(t < 0)
  240. t = 0;
  241. subf->f->info[i].bottom = t;
  242. }
  243. subf->f->ascent = f->ascent;
  244. }
  245. Found2:
  246. subf->age = f->age;
  247. /* possible overflow here, but works out okay */
  248. pic += cf->offset;
  249. pic -= cf->min;
  250. if(pic >= subf->f->n)
  251. goto TryPJW;
  252. fi = &subf->f->info[pic];
  253. if(fi->width == 0)
  254. goto TryPJW;
  255. wid = (fi+1)->x - fi->x;
  256. if(f->width < wid || f->width == 0 || f->maxdepth < subf->f->bits->depth){
  257. /*
  258. * Flush, free, reload (easier than reformatting f->b)
  259. */
  260. if(noflush)
  261. return -1;
  262. if(f->width < wid)
  263. f->width = wid;
  264. if(f->maxdepth < subf->f->bits->depth)
  265. f->maxdepth = subf->f->bits->depth;
  266. i = fontresize(f, f->width, f->ncache, f->maxdepth);
  267. if(i <= 0)
  268. return i;
  269. /* c is still valid as didn't reallocate f->cache */
  270. }
  271. c->value = r;
  272. top = fi->top + (f->ascent-subf->f->ascent);
  273. bottom = fi->bottom + (f->ascent-subf->f->ascent);
  274. c->width = fi->width;
  275. c->x = h*f->width;
  276. c->left = fi->left;
  277. if(f->display == nil)
  278. return 1;
  279. flushimage(f->display, 0); /* flush any pending errors */
  280. b = bufimage(f->display, 37);
  281. if(b == 0)
  282. return 0;
  283. b[0] = 'l';
  284. BPLONG(b+1, f->cacheimage->id);
  285. BPLONG(b+5, subf->f->bits->id);
  286. BPSHORT(b+9, c-f->cache);
  287. BPLONG(b+11, c->x);
  288. BPLONG(b+15, top);
  289. BPLONG(b+19, c->x+((fi+1)->x-fi->x));
  290. BPLONG(b+23, bottom);
  291. BPLONG(b+27, fi->x);
  292. BPLONG(b+31, fi->top);
  293. b[35] = fi->left;
  294. b[36] = fi->width;
  295. return 1;
  296. }
  297. /* release all subfonts, return number freed */
  298. static
  299. int
  300. freeup(Font *f)
  301. {
  302. Cachesubf *s, *es;
  303. int nf;
  304. if(f->sub[0]->name == nil) /* font from mkfont; don't free */
  305. return 0;
  306. s = f->subf;
  307. es = s+f->nsubf;
  308. nf = 0;
  309. while(s < es){
  310. if(s->age){
  311. freesubfont(s->f);
  312. s->cf = nil;
  313. s->f = nil;
  314. s->age = 0;
  315. nf++;
  316. }
  317. s++;
  318. }
  319. return nf;
  320. }
  321. /* return whether resize succeeded && f->cache is unchanged */
  322. static int
  323. fontresize(Font *f, int wid, int ncache, int depth)
  324. {
  325. Cacheinfo *i;
  326. int ret;
  327. Image *new;
  328. uchar *b;
  329. Display *d;
  330. ret = 0;
  331. if(depth <= 0)
  332. depth = 1;
  333. if(wid <= 0)
  334. wid = 1;
  335. d = f->display;
  336. if(d == nil)
  337. goto Nodisplay;
  338. new = allocimage(d, Rect(0, 0, ncache*wid, f->height), CHAN1(CGrey, depth), 0, 0);
  339. if(new == nil){
  340. fprint(2, "font cache resize failed: %r\n");
  341. abort();
  342. goto Return;
  343. }
  344. flushimage(d, 0); /* flush any pending errors */
  345. b = bufimage(d, 1+4+4+1);
  346. if(b == 0){
  347. freeimage(new);
  348. goto Return;
  349. }
  350. b[0] = 'i';
  351. BPLONG(b+1, new->id);
  352. BPLONG(b+5, ncache);
  353. b[9] = f->ascent;
  354. if(flushimage(d, 0) < 0){
  355. fprint(2, "resize: init failed: %r\n");
  356. freeimage(new);
  357. goto Return;
  358. }
  359. freeimage(f->cacheimage);
  360. f->cacheimage = new;
  361. Nodisplay:
  362. f->width = wid;
  363. f->maxdepth = depth;
  364. ret = 1;
  365. if(f->ncache != ncache){
  366. i = malloc(ncache*sizeof f->cache[0]);
  367. if(i != nil){
  368. ret = 0;
  369. free(f->cache);
  370. f->ncache = ncache;
  371. f->cache = i;
  372. }
  373. /* else just wipe the cache clean and things will be ok */
  374. }
  375. Return:
  376. memset(f->cache, 0, f->ncache*sizeof f->cache[0]);
  377. return ret;
  378. }