font.c 8.9 KB

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