font.c 7.6 KB

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