font.c 8.6 KB

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