font.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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[10];
  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. sprint(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. static void
  99. loadfont(int n, int s)
  100. {
  101. char file[100];
  102. int i, fd, t, deep;
  103. static char *try[3] = {"", "times/R.", "pelm/"};
  104. Subfont *f;
  105. Font *ff;
  106. try[0] = fname[n];
  107. for (t = 0; t < 3; t++){
  108. i = s * mag * charmap[fmap[n]].xheight/0.72; /* a pixel is 0.72 points */
  109. if (i < MINSIZE)
  110. i = MINSIZE;
  111. dprint(2, "size %d, i %d, mag %g\n", s, i, mag);
  112. for(; i >= MINSIZE; i--){
  113. /* if .font file exists, take that */
  114. sprint(file, "%s/%s%d.font", libfont, try[t], i);
  115. ff = openfont(display, file);
  116. if(ff != 0){
  117. fonttab[n][s] = ff;
  118. dprint(2, "using %s for font %d %d\n", file, n, s);
  119. return;
  120. }
  121. /* else look for a subfont file */
  122. for (deep = log2[screen->depth]; deep >= 0; deep--){
  123. sprint(file, "%s/%s%d.%d", libfont, try[t], i, deep);
  124. dprint(2, "trying %s for %d\n", file, i);
  125. if ((fd = open(file, 0)) >= 0){
  126. f = readsubfont(display, file, fd, 0);
  127. if (f == 0) {
  128. fprint(2, "can't rdsubfontfile %s: %r\n", file);
  129. exits("rdsubfont");
  130. }
  131. close(fd);
  132. ff = mkfont(f, 0);
  133. if(ff == 0){
  134. fprint(2, "can't mkfont %s: %r\n", file);
  135. exits("rdsubfont");
  136. }
  137. fonttab[n][s] = ff;
  138. dprint(2, "using %s for font %d %d\n", file, n, s);
  139. return;
  140. }
  141. }
  142. }
  143. }
  144. fprint(2, "can't find font %s.%d or substitute, quitting\n", fname[n], s);
  145. exits("no font");
  146. }
  147. void
  148. loadfontname(int n, char *s)
  149. {
  150. int i;
  151. Font *f, *g = 0;
  152. if (strcmp(s, fname[n]) == 0)
  153. return;
  154. if(fname[n] && fname[n][0]){
  155. if(lastload[n] && strcmp(lastload[n], fname[n]) == 0)
  156. return;
  157. strcpy(lastload[n], fname[n]);
  158. }
  159. fontlookup(n, s);
  160. for (i = 0; i < NSIZE; i++)
  161. if (f = fonttab[n][i]){
  162. if (f != g) {
  163. freefont(f);
  164. g = f;
  165. }
  166. fonttab[n][i] = 0;
  167. }
  168. }
  169. void
  170. allfree(void)
  171. {
  172. int i;
  173. for (i=0; i<NFONT; i++)
  174. loadfontname(i, "??");
  175. }
  176. void
  177. readmapfile(char *file)
  178. {
  179. Biobuf *fp;
  180. char *p, cmd[100];
  181. if ((fp=Bopen(file, OREAD)) == 0){
  182. fprint(2, "proof: can't open map file %s\n", file);
  183. exits("urk");
  184. }
  185. while((p=Brdline(fp, '\n')) != 0) {
  186. p[Blinelen(fp)-1] = 0;
  187. scanstr(p, cmd, 0);
  188. if(p[0]=='\0' || eq(cmd, "#")) /* skip comments, empty */
  189. continue;
  190. else if(eq(cmd, "xheight"))
  191. buildxheight(fp);
  192. else if(eq(cmd, "map"))
  193. buildmap(fp);
  194. else if(eq(cmd, "special"))
  195. buildtroff(p);
  196. else if(eq(cmd, "troff"))
  197. buildtroff(p);
  198. else
  199. fprint(2, "weird map line %s\n", p);
  200. }
  201. Bterm(fp);
  202. }
  203. static void
  204. buildxheight(Biobuf *fp) /* map goes from char name to value to print via *string() */
  205. {
  206. char *line;
  207. line = Brdline(fp, '\n');
  208. if(line == 0){
  209. fprint(2, "proof: bad map file\n");
  210. exits("map");
  211. }
  212. charmap[curmap].xheight = atof(line);
  213. }
  214. static void
  215. buildmap(Biobuf *fp) /* map goes from char name to value to print via *string() */
  216. {
  217. uchar *p, *line, ch[100];
  218. int val;
  219. Rune r;
  220. curmap++;
  221. if(curmap >= NMAP){
  222. fprint(2, "proof: out of char maps; recompile\n");
  223. exits("charmap");
  224. }
  225. while ((line = Brdline(fp, '\n'))!= 0){
  226. if (line[0] == '\n')
  227. return;
  228. line[Blinelen(fp)-1] = 0;
  229. scanstr((char *) line, (char *) ch, (char **) &p);
  230. if (ch[0] == '\0') {
  231. fprint(2, "bad map file line '%s'\n", (char*)line);
  232. continue;
  233. }
  234. val = strtol((char *) p, 0, 10);
  235. dprint(2, "buildmap %s (%x %x) %s %d\n", (char*)ch, ch[0], ch[1], (char*)p, val);
  236. chartorune(&r, (char*)ch);
  237. if(utflen((char*)ch)==1 && r<QUICK)
  238. charmap[curmap].quick[r] = val;
  239. else
  240. addmap(curmap, strdup((char *) ch), val); /* put somewhere else */
  241. }
  242. }
  243. static void
  244. addmap(int n, char *s, int val) /* stick a new link on */
  245. {
  246. Link *p = (Link *) malloc(sizeof(Link));
  247. Link *prev = charmap[n].slow;
  248. if(p == 0)
  249. exits("out of memory in addmap");
  250. p->name = (uchar *) s;
  251. p->val = val;
  252. p->next = prev;
  253. charmap[n].slow = p;
  254. }
  255. static void
  256. buildtroff(char *buf) /* map troff names into bitmap filenames */
  257. { /* e.g., R -> times/R., I -> times/I., etc. */
  258. char *p, cmd[100], name[200], prefix[400], fallback[100];
  259. scanstr(buf, cmd, &p);
  260. scanstr(p, name, &p);
  261. scanstr(p, prefix, &p);
  262. while(*p!=0 && isspace(*p))
  263. p++;
  264. if(*p != 0){
  265. scanstr(p, fallback, &p);
  266. fontmap[nfontmap].fallback = strdup(fallback);
  267. }else
  268. fontmap[nfontmap].fallback = 0;
  269. fontmap[nfontmap].troffname = strdup(name);
  270. fontmap[nfontmap].prefix = strdup(prefix);
  271. fontmap[nfontmap].map = curmap;
  272. dprint(2, "troff name %s is bitmap %s map %d in slot %d fallback %s\n", name, prefix, curmap, nfontmap, fontmap[nfontmap].fallback? fontmap[nfontmap].fallback : "<null>");
  273. nfontmap++;
  274. }
  275. static void
  276. fontlookup(int n, char *s) /* map troff name of s into position n */
  277. {
  278. int i;
  279. for(i = 0; i < nfontmap; i++)
  280. if (eq(s, fontmap[i].troffname)) {
  281. strcpy(fname[n], fontmap[i].prefix);
  282. fmap[n] = fontmap[i].map;
  283. pos2fontmap[n] = i;
  284. if (eq(s, "S"))
  285. specfont = n;
  286. dprint(2, "font %d %s is %s\n", n, s, fname[n]);
  287. return;
  288. }
  289. /* god help us if this font isn't there */
  290. }
  291. static char *
  292. map(Rune rp[], int font) /* figure out mapping for char in this font */
  293. {
  294. static char s[100];
  295. char c[10];
  296. Link *p;
  297. Rune r;
  298. if(rp[1]==0 && rp[0]<QUICK) /* fast lookup */
  299. r = charmap[fmap[font]].quick[rp[0]];
  300. else { /* high-valued or compound character name */
  301. sprint(c, "%S", rp);
  302. r = 0;
  303. for (p = charmap[fmap[font]].slow; p; p = p->next)
  304. if(eq(c, p->name)){
  305. r = p->val;
  306. break;
  307. }
  308. }
  309. if(r == 0){ /* not there */
  310. dprint(2, "didn't find %S font# %d\n", rp, font);
  311. return 0;
  312. }
  313. dprint(2, "map %S to %s font# %d\n", rp, s, font);
  314. s[runetochar(s, &r)] = 0;
  315. return s;
  316. }
  317. static void
  318. scanstr(char *s, char *ans, char **ep)
  319. {
  320. for (; isspace((uchar) *s); s++)
  321. ;
  322. for (; *s!=0 && !isspace((uchar) *s); )
  323. *ans++ = *s++;
  324. *ans = 0;
  325. if (ep)
  326. *ep = s;
  327. }