font.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #include <event.h>
  13. #include <bio.h>
  14. #include "proof.h"
  15. char fname[NFONT][20]; /* font names */
  16. char lastload[NFONT][20]; /* last file name prefix loaded for this font */
  17. Font *fonttab[NFONT][NSIZE]; /* pointers to fonts */
  18. int fmap[NFONT]; /* what map to use with this font */
  19. static void bufchar(Point, Subfont *, uint8_t *);
  20. static void loadfont(int, int);
  21. static void fontlookup(int, char *);
  22. static void buildxheight(Biobuf*);
  23. static void buildmap(Biobuf*);
  24. static void buildtroff(char *);
  25. static void addmap(int, char *, int);
  26. static char *map(Rune*, int);
  27. static void scanstr(char *, char *, char **);
  28. int specfont; /* somehow, number of special font */
  29. #define NMAP 5
  30. #define QUICK 2048 /* char values less than this are quick to look up */
  31. #define eq(s,t) strcmp((char *) s, (char *) t) == 0
  32. int curmap = -1; /* what map are we working on */
  33. typedef struct Link Link;
  34. struct Link /* link names together */
  35. {
  36. uint8_t *name;
  37. int val;
  38. Link *next;
  39. };
  40. typedef struct Map Map;
  41. struct Map /* holds a mapping from uchar name to index */
  42. {
  43. double xheight;
  44. Rune quick[QUICK]; /* low values get special treatment */
  45. Link *slow; /* other stuff goes into a link list */
  46. };
  47. Map charmap[5];
  48. typedef struct Fontmap Fontmap;
  49. struct Fontmap /* mapping from troff name to filename */
  50. {
  51. char *troffname;
  52. char *prefix;
  53. int map; /* which charmap to use for this font */
  54. char *fallback; /* font to look in if can't find char here */
  55. };
  56. Fontmap fontmap[100];
  57. int pos2fontmap[NFONT]; /* indexed by troff font position, gives Fontmap */
  58. int nfontmap = 0; /* how many are there */
  59. void
  60. dochar(Rune r[])
  61. {
  62. char *s, *fb;
  63. Font *f;
  64. Point p;
  65. int fontno, fm, i;
  66. char buf[32];
  67. fontno = curfont;
  68. if((s = map(r, curfont)) == 0){ /* not on current font */
  69. if ((s = map(r, specfont)) != 0) /* on special font */
  70. fontno = specfont;
  71. else{
  72. /* look for fallback */
  73. fm = pos2fontmap[curfont];
  74. fb = fontmap[fm].fallback;
  75. if(fb){
  76. /* see if fallback is mounted */
  77. for(i = 0; i < NFONT; i++){
  78. if(eq(fb, fontmap[pos2fontmap[i]].troffname)){
  79. s = map(r, i);
  80. if(s){
  81. fontno = i;
  82. goto found;
  83. }
  84. }
  85. }
  86. }
  87. /* no such char; use name itself on defont */
  88. /* this is not a general solution */
  89. p.x = hpos/DIV + xyoffset.x + offset.x;
  90. p.y = vpos/DIV + xyoffset.y + offset.y;
  91. p.y -= font->ascent;
  92. snprint(buf, sizeof buf, "%S", r);
  93. string(screen, p, display->black, ZP, font, buf);
  94. return;
  95. }
  96. }
  97. found:
  98. p.x = hpos/DIV + xyoffset.x + offset.x;
  99. p.y = vpos/DIV + xyoffset.y + offset.y;
  100. while ((f = fonttab[fontno][cursize]) == 0)
  101. loadfont(fontno, cursize);
  102. p.y -= f->ascent;
  103. dprint(2, "putting %S at %d,%d font %d, size %d\n", r, p.x, p.y, fontno, cursize);
  104. string(screen, p, display->black, ZP, f, s);
  105. }
  106. /* imported from libdraw/arith.c to permit an extern log2 function */
  107. static int log2[] = {
  108. -1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4,
  109. -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
  110. };
  111. static void
  112. loadfont(int n, int s)
  113. {
  114. char file[256];
  115. int i, fd, t, deep;
  116. static char *try[3] = {"", "times/R.", "pelm/"};
  117. Subfont *f;
  118. Font *ff;
  119. try[0] = fname[n];
  120. for (t = 0; t < 3; t++){
  121. i = s * mag * charmap[fmap[n]].xheight/0.72; /* a pixel is 0.72 points */
  122. if (i < MINSIZE)
  123. i = MINSIZE;
  124. dprint(2, "size %d, i %d, mag %g\n", s, i, mag);
  125. for(; i >= MINSIZE; i--){
  126. /* if .font file exists, take that */
  127. snprint(file, sizeof file, "%s/%s%d.font",
  128. libfont, try[t], i);
  129. ff = openfont(display, file);
  130. if(ff != 0){
  131. fonttab[n][s] = ff;
  132. dprint(2, "using %s for font %d %d\n", file, n, s);
  133. return;
  134. }
  135. /* else look for a subfont file */
  136. for (deep = log2[screen->depth]; deep >= 0; deep--){
  137. snprint(file, sizeof file, "%s/%s%d.%d",
  138. libfont, try[t], i, deep);
  139. dprint(2, "trying %s for %d\n", file, i);
  140. if ((fd = open(file, 0)) >= 0){
  141. f = readsubfont(display, file, fd, 0);
  142. if (f == 0) {
  143. fprint(2, "can't rdsubfontfile %s: %r\n", file);
  144. exits("rdsubfont");
  145. }
  146. close(fd);
  147. ff = mkfont(f, 0);
  148. if(ff == 0){
  149. fprint(2, "can't mkfont %s: %r\n", file);
  150. exits("rdsubfont");
  151. }
  152. fonttab[n][s] = ff;
  153. dprint(2, "using %s for font %d %d\n", file, n, s);
  154. return;
  155. }
  156. }
  157. }
  158. }
  159. fprint(2, "can't find font %s.%d or substitute, quitting\n", fname[n], s);
  160. exits("no font");
  161. }
  162. void
  163. loadfontname(int n, char *s)
  164. {
  165. int i;
  166. Font *f, *g = 0;
  167. if (strcmp(s, fname[n]) == 0)
  168. return;
  169. if(fname[n] && fname[n][0]){
  170. if(lastload[n] && strcmp(lastload[n], fname[n]) == 0)
  171. return;
  172. strcpy(lastload[n], fname[n]);
  173. }
  174. fontlookup(n, s);
  175. for (i = 0; i < NSIZE; i++)
  176. if (f = fonttab[n][i]){
  177. if (f != g) {
  178. freefont(f);
  179. g = f;
  180. }
  181. fonttab[n][i] = 0;
  182. }
  183. }
  184. void
  185. allfree(void)
  186. {
  187. int i;
  188. for (i=0; i<NFONT; i++)
  189. loadfontname(i, "??");
  190. }
  191. void
  192. readmapfile(char *file)
  193. {
  194. Biobuf *fp;
  195. char *p, cmd[100];
  196. if ((fp=Bopen(file, OREAD)) == 0){
  197. fprint(2, "proof: can't open map file %s\n", file);
  198. exits("urk");
  199. }
  200. while((p=Brdline(fp, '\n')) != 0) {
  201. p[Blinelen(fp)-1] = 0;
  202. scanstr(p, cmd, 0);
  203. if(p[0]=='\0' || eq(cmd, "#")) /* skip comments, empty */
  204. continue;
  205. else if(eq(cmd, "xheight"))
  206. buildxheight(fp);
  207. else if(eq(cmd, "map"))
  208. buildmap(fp);
  209. else if(eq(cmd, "special"))
  210. buildtroff(p);
  211. else if(eq(cmd, "troff"))
  212. buildtroff(p);
  213. else
  214. fprint(2, "weird map line %s\n", p);
  215. }
  216. Bterm(fp);
  217. }
  218. static void
  219. buildxheight(Biobuf *fp) /* map goes from char name to value to print via *string() */
  220. {
  221. char *line;
  222. line = Brdline(fp, '\n');
  223. if(line == 0){
  224. fprint(2, "proof: bad map file\n");
  225. exits("map");
  226. }
  227. charmap[curmap].xheight = atof(line);
  228. }
  229. static void
  230. buildmap(Biobuf *fp) /* map goes from char name to value to print via *string() */
  231. {
  232. uint8_t *p, *line, ch[100];
  233. int val;
  234. Rune r;
  235. curmap++;
  236. if(curmap >= NMAP){
  237. fprint(2, "proof: out of char maps; recompile\n");
  238. exits("charmap");
  239. }
  240. while ((line = Brdline(fp, '\n'))!= 0){
  241. if (line[0] == '\n')
  242. return;
  243. line[Blinelen(fp)-1] = 0;
  244. scanstr((char *) line, (char *) ch, (char **) &p);
  245. if (ch[0] == '\0') {
  246. fprint(2, "bad map file line '%s'\n", (char*)line);
  247. continue;
  248. }
  249. val = strtol((char *) p, 0, 10);
  250. dprint(2, "buildmap %s (%x %x) %s %d\n", (char*)ch, ch[0], ch[1],
  251. (char*)p, val);
  252. chartorune(&r, (char*)ch);
  253. if(utflen((char*)ch)==1 && r<QUICK)
  254. charmap[curmap].quick[r] = val;
  255. else
  256. addmap(curmap, strdup((char *) ch), val); /* put somewhere else */
  257. }
  258. }
  259. static void
  260. addmap(int n, char *s, int val) /* stick a new link on */
  261. {
  262. Link *p = (Link *) malloc(sizeof(Link));
  263. Link *prev = charmap[n].slow;
  264. if(p == 0)
  265. exits("out of memory in addmap");
  266. p->name = (uint8_t *) s;
  267. p->val = val;
  268. p->next = prev;
  269. charmap[n].slow = p;
  270. }
  271. static void
  272. buildtroff(char *buf) /* map troff names into bitmap filenames */
  273. { /* e.g., R -> times/R., I -> times/I., etc. */
  274. char *p, cmd[100], name[200], prefix[400], fallback[100];
  275. scanstr(buf, cmd, &p);
  276. scanstr(p, name, &p);
  277. scanstr(p, prefix, &p);
  278. while(*p!=0 && isspace(*p))
  279. p++;
  280. if(*p != 0){
  281. scanstr(p, fallback, &p);
  282. fontmap[nfontmap].fallback = strdup(fallback);
  283. }else
  284. fontmap[nfontmap].fallback = 0;
  285. fontmap[nfontmap].troffname = strdup(name);
  286. fontmap[nfontmap].prefix = strdup(prefix);
  287. fontmap[nfontmap].map = curmap;
  288. dprint(2, "troff name %s is bitmap %s map %d in slot %d fallback %s\n",
  289. name, prefix, curmap, nfontmap, fontmap[nfontmap].fallback?
  290. fontmap[nfontmap].fallback: "<null>");
  291. nfontmap++;
  292. }
  293. static void
  294. fontlookup(int n, char *s) /* map troff name of s into position n */
  295. {
  296. int i;
  297. for(i = 0; i < nfontmap; i++)
  298. if (eq(s, fontmap[i].troffname)) {
  299. strcpy(fname[n], fontmap[i].prefix);
  300. fmap[n] = fontmap[i].map;
  301. pos2fontmap[n] = i;
  302. if (eq(s, "S"))
  303. specfont = n;
  304. dprint(2, "font %d %s is %s\n", n, s, fname[n]);
  305. return;
  306. }
  307. /* god help us if this font isn't there */
  308. }
  309. static char *
  310. map(Rune rp[], int font) /* figure out mapping for char in this font */
  311. {
  312. static char s[100];
  313. unsigned m;
  314. char c[32];
  315. Link *p;
  316. Rune r;
  317. if((unsigned)font >= NFONT) {
  318. dprint(2, "map: font %ud >= NFONT (%d)\n", font, NFONT);
  319. return 0;
  320. }
  321. m = fmap[font];
  322. if(m >= nelem(charmap)) {
  323. dprint(2, "map: fmap[font] %ud >= nelem(charmap) (%d)\n",
  324. m, nelem(charmap));
  325. return 0;
  326. }
  327. if(rp[1] == 0 && rp[0] < QUICK) /* fast lookup */
  328. r = charmap[m].quick[rp[0]];
  329. else { /* high-valued or compound character name */
  330. snprint(c, sizeof c, "%S", rp);
  331. r = 0;
  332. for (p = charmap[m].slow; p; p = p->next)
  333. if(eq(c, p->name)){
  334. r = p->val;
  335. break;
  336. }
  337. }
  338. if(r == 0){ /* not there */
  339. dprint(2, "didn't find %S font# %d\n", rp, font);
  340. return 0;
  341. }
  342. dprint(2, "map %S to %s font# %d\n", rp, s, font);
  343. s[runetochar(s, &r)] = 0;
  344. return s;
  345. }
  346. static void
  347. scanstr(char *s, char *ans, char **ep)
  348. {
  349. for (; isspace((uint8_t) *s); s++)
  350. ;
  351. for (; *s!=0 && !isspace((uint8_t) *s); )
  352. *ans++ = *s++;
  353. *ans = 0;
  354. if (ep)
  355. *ep = s;
  356. }