chartab.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Unicode | PostScript
  2. * start end | offset font name
  3. * 0x0000 0x00ff 0x00 LucidaSansUnicode00
  4. */
  5. #include <u.h>
  6. #include <libc.h>
  7. #include <bio.h>
  8. #include "common.h"
  9. #include "tr2post.h"
  10. #include "comments.h"
  11. #include "path.h"
  12. /* Postscript font names, e.g., `LucidaSansUnicode00'
  13. * names may only be added because reference to the
  14. * names is made by indexing into this table.
  15. */
  16. static struct pfnament *pfnafontmtab = 0;
  17. static int pfnamcnt = 0;
  18. int curpostfontid = -1;
  19. int curfontsize = -1;
  20. int curtrofffontid = -1;
  21. static int curfontpos = -1;
  22. static int fontheight = 0;
  23. static int fontslant = 0;
  24. /* This is troffs mounted font table. It is an anachronism resulting
  25. * from the design of the APS typesetter. fontmnt is the
  26. * number of positions available. fontmnt is really 11, but
  27. * should not be limited.
  28. */
  29. int fontmnt = 0;
  30. char **fontmtab;
  31. struct troffont *troffontab = 0;
  32. int troffontcnt = 0;
  33. void
  34. mountfont(int pos, char *fontname) {
  35. int i;
  36. if (debug) Bprint(Bstderr, "mountfont(%d, %s)\n", pos, fontname);
  37. if (pos < 0 || pos >= fontmnt)
  38. error(FATAL, "cannot mount a font at position %d,\n can only mount into postions 0-%d\n",
  39. pos, fontmnt-1);
  40. i = strlen(fontname);
  41. fontmtab[pos] = galloc(fontmtab[pos], i+1, "mountfont():fontmtab");
  42. strcpy(fontmtab[pos], fontname);
  43. if (curfontpos == pos) curfontpos = -1;
  44. }
  45. void
  46. settrfont(void) {
  47. if (curfontpos == fontpos) return;
  48. if (fontmtab[fontpos] == 0)
  49. error(FATAL, "Font at position %d was not initialized, botch!\n", fontpos);
  50. curtrofffontid = findtfn(fontmtab[fontpos], 1);
  51. if (debug) Bprint(Bstderr, "settrfont()-> curtrofffontid=%d\n", curtrofffontid);
  52. curfontpos = fontpos;
  53. if (curtrofffontid < 0) {
  54. int i;
  55. error(WARNING, "fontpos=%d\n", fontpos);
  56. for (i=0; i<fontmnt; i++)
  57. if (fontmtab[i] == 0)
  58. error(WARNING, "fontmtab[%d]=0x0\n", i);
  59. else
  60. error(WARNING, "fontmtab[%d]=%s\n", i, fontmtab[i]);
  61. exits("settrfont()");
  62. }
  63. }
  64. void
  65. setpsfont(int psftid, int fontsize) {
  66. if (psftid == curpostfontid && fontsize == curfontsize) return;
  67. if (psftid >= pfnamcnt)
  68. error(FATAL, "Postscript font index=%d used but not defined, there are only %d fonts\n",
  69. psftid, pfnamcnt);
  70. endstring();
  71. if (pageon()) {
  72. Bprint(Bstdout, "%d /%s f\n", fontsize, pfnafontmtab[psftid].str);
  73. if ( fontheight != 0 || fontslant != 0 )
  74. Bprint(Bstdout, "%d %d changefont\n", fontslant, (fontheight != 0) ? fontheight : fontsize);
  75. pfnafontmtab[psftid].used = 1;
  76. curpostfontid = psftid;
  77. curfontsize = fontsize;
  78. }
  79. }
  80. /* find index of PostScript font name in table
  81. * returns -1 if name is not in table
  82. * If insflg is not zero
  83. * and the name is not found in the table, insert it.
  84. */
  85. int
  86. findpfn(char *fontname, int insflg) {
  87. char *tp;
  88. int i;
  89. for (i=0; i<pfnamcnt; i++) {
  90. if (strcmp(pfnafontmtab[i].str, fontname) == 0)
  91. return(i);
  92. }
  93. if (insflg) {
  94. tp = galloc(pfnafontmtab, sizeof(struct pfnament)*(pfnamcnt+1), "findpfn():pfnafontmtab");
  95. if (tp == 0)
  96. return(-2);
  97. pfnafontmtab = (struct pfnament *)tp;
  98. i = strlen(fontname);
  99. pfnafontmtab[pfnamcnt].str = galloc(0, i+1, "findpfn():pfnafontmtab[].str");
  100. strncpy(pfnafontmtab[pfnamcnt].str, fontname, i);
  101. pfnafontmtab[pfnamcnt].str[i] = '\0';
  102. pfnafontmtab[pfnamcnt].used = 0;
  103. return(pfnamcnt++);
  104. }
  105. return(-1);
  106. }
  107. char postroffdirname[] = "/sys/lib/postscript/troff"; /* "/sys/lib/postscript/troff/"; */
  108. char troffmetricdirname[] = "/sys/lib/troff/font"; /* "/sys/lib/troff/font/devutf/"; */
  109. int
  110. readpsfontdesc(char *fontname, int trindex) {
  111. static char *filename = 0;
  112. Biobuf *bfd;
  113. Biobufhdr *Bfd;
  114. int warn = 0, errorflg = 0, line =1, rv;
  115. int start, end, offset;
  116. int startfont, endfont, startchar, endchar, i, pfid;
  117. char psfontnam[128];
  118. struct troffont *tp;
  119. struct charent *cp[];
  120. if (debug) Bprint(Bstderr, "readpsfontdesc(%s,%d)\n", fontname, trindex);
  121. filename=galloc(filename, strlen(postroffdirname)+1+strlen(fontname)+1, "readpsfontdesc: cannot allocate memory\n");
  122. sprint(filename, "%s/%s", postroffdirname, fontname);
  123. bfd = Bopen(filename, OREAD);
  124. if (bfd == 0) {
  125. error(WARNING, "cannot open file %s\n", filename);
  126. return(0);
  127. }
  128. Bfd = &(bfd->Biobufhdr);
  129. do {
  130. offset = 0;
  131. if ((rv=Bgetfield(Bfd, 'd', &start, 0)) == 0) {
  132. errorflg = 1;
  133. error(WARNING, "file %s:%d illegal start value\n", filename, line);
  134. } else if (rv < 0) break;
  135. if ((rv=Bgetfield(Bfd, 'd', &end, 0)) == 0) {
  136. errorflg = 1;
  137. error(WARNING, "file %s:%d illegal end value\n", filename, line);
  138. } else if (rv < 0) break;
  139. if ((rv=Bgetfield(Bfd, 'd', &offset, 0)) < 0) {
  140. errorflg = 1;
  141. error(WARNING, "file %s:%d illegal offset value\n", filename, line);
  142. }
  143. if ((rv=Bgetfield(Bfd, 's', psfontnam, 128)) == 0) {
  144. errorflg = 1;
  145. error(WARNING, "file %s:%d illegal fontname value\n", filename, line);
  146. } else if (rv < 0) break;
  147. Brdline(Bfd, '\n');
  148. if (!errorflg) {
  149. struct psfent *psfentp;
  150. startfont = RUNEGETGROUP(start);
  151. startchar = RUNEGETCHAR(start);
  152. endfont = RUNEGETGROUP(end);
  153. endchar = RUNEGETCHAR(end);
  154. pfid = findpfn(psfontnam, 1);
  155. if (startfont != endfont) {
  156. error(WARNING, "font descriptions must not cross 256 glyph block boundary\n");
  157. errorflg = 1;
  158. break;
  159. }
  160. tp = &(troffontab[trindex]);
  161. tp->psfmap = galloc(tp->psfmap, ++(tp->psfmapsize)*sizeof(struct psfent), "readpsfontdesc():psfmap");
  162. psfentp = &(tp->psfmap[tp->psfmapsize-1]);
  163. psfentp->start = start;
  164. psfentp->end = end;
  165. psfentp->offset = offset;
  166. psfentp->psftid = pfid;
  167. if (debug) {
  168. Bprint(Bstderr, "\tpsfmap->start=0x%x\n", start);
  169. Bprint(Bstderr, "\tpsfmap->end=0x%x\n", end);
  170. Bprint(Bstderr, "\tpsfmap->offset=0x%x\n", offset);
  171. Bprint(Bstderr, "\tpsfmap->pfid=0x%x\n", pfid);
  172. }
  173. /*
  174. for (i=startchar; i<=endchar; i++) {
  175. tp->charent[startfont][i].postfontid = pfid;
  176. tp->charent[startfont][i].postcharid = i + offset - startchar;
  177. }
  178. */
  179. if (debug) {
  180. Bprint(Bstderr, "%x %x ", start, end);
  181. if (offset) Bprint(Bstderr, "%x ", offset);
  182. Bprint(Bstderr, "%s\n", psfontnam);
  183. }
  184. line++;
  185. }
  186. } while(errorflg != 1);
  187. Bterm(Bfd);
  188. return(1);
  189. }
  190. int
  191. readtroffmetric(char *fontname, int trindex) {
  192. static char *filename = 0;
  193. Biobuf *bfd;
  194. Biobufhdr *Bfd;
  195. int warn = 0, errorflg = 0, line =1, rv;
  196. struct troffont *tp;
  197. struct charent **cp;
  198. char stoken[128], *str;
  199. int ntoken;
  200. Rune troffchar, quote;
  201. int width, flag, charnum, thisfont, thischar;
  202. BOOLEAN specharflag;
  203. if (debug) Bprint(Bstderr, "readtroffmetric(%s,%d)\n", fontname, trindex);
  204. filename=galloc(filename, strlen(troffmetricdirname)+4+strlen(devname)+1+strlen(fontname)+1, "readtroffmetric():filename");
  205. sprint(filename, "%s/dev%s/%s", troffmetricdirname, devname, fontname);
  206. bfd = Bopen(filename, OREAD);
  207. if (bfd == 0) {
  208. error(WARNING, "cannot open file %s\n", filename);
  209. return(0);
  210. }
  211. Bfd = &(bfd->Biobufhdr);
  212. do {
  213. /* deal with the few lines at the beginning of the
  214. * troff font metric files.
  215. */
  216. if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
  217. errorflg = 1;
  218. error(WARNING, "file %s:%d illegal token\n", filename, line);
  219. } else if (rv < 0) break;
  220. if (debug) {
  221. Bprint(Bstderr, "%s\n", stoken);
  222. }
  223. if (strcmp(stoken, "name") == 0) {
  224. if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
  225. errorflg = 1;
  226. error(WARNING, "file %s:%d illegal token\n", filename, line);
  227. } else if (rv < 0) break;
  228. } else if (strcmp(stoken, "named") == 0) {
  229. Brdline(Bfd, '\n');
  230. } else if (strcmp(stoken, "fontname") == 0) {
  231. if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
  232. errorflg = 1;
  233. error(WARNING, "file %s:%d illegal token\n", filename, line);
  234. } else if (rv < 0) break;
  235. } else if (strcmp(stoken, "spacewidth") == 0) {
  236. if ((rv=Bgetfield(Bfd, 'd', &ntoken, 0)) == 0) {
  237. errorflg = 1;
  238. error(WARNING, "file %s:%d illegal token\n", filename, line);
  239. } else if (rv < 0) break;
  240. troffontab[trindex].spacewidth = ntoken;
  241. thisfont = RUNEGETGROUP(' ');
  242. thischar = RUNEGETCHAR(' ');
  243. for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
  244. if ((*cp)->name)
  245. if (strcmp((*cp)->name, " ") == 0)
  246. break;
  247. if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
  248. (*cp)->postfontid = thisfont;
  249. (*cp)->postcharid = thischar;
  250. (*cp)->troffcharwidth = ntoken;
  251. (*cp)->name = galloc(0, 2, "readtroffmetric: char name");
  252. (*cp)->next = 0;
  253. strcpy((*cp)->name, " ");
  254. } else if (strcmp(stoken, "special") == 0) {
  255. troffontab[trindex].special = TRUE;
  256. } else if (strcmp(stoken, "charset") == 0) {
  257. line++;
  258. break;
  259. }
  260. if (!errorflg) {
  261. line++;
  262. }
  263. } while(!errorflg && rv>=0);
  264. while(!errorflg && rv>=0) {
  265. if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
  266. errorflg = 1;
  267. error(WARNING, "file %s:%d illegal rune token <0x%x> rv=%d\n", filename, line, troffchar, rv);
  268. } else if (rv < 0) break;
  269. if (utflen(stoken) > 1) specharflag = TRUE;
  270. else specharflag = FALSE;
  271. /* if this character is a quote we have to use the previous characters info */
  272. if ((rv=Bgetfield(Bfd, 'r', &quote, 0)) == 0) {
  273. errorflg = 1;
  274. error(WARNING, "file %s:%d illegal width or quote token <0x%x> rv=%d\n", filename, line, quote, rv);
  275. } else if (rv < 0) break;
  276. if (quote == '"') {
  277. /* need some code here */
  278. goto flush;
  279. } else {
  280. Bungetrune(Bfd);
  281. }
  282. if ((rv=Bgetfield(Bfd, 'd', &width, 0)) == 0) {
  283. errorflg = 1;
  284. error(WARNING, "file %s:%d illegal width token <0x%x> rv=%d\n", filename, line, troffchar, rv);
  285. } else if (rv < 0) break;
  286. if ((rv=Bgetfield(Bfd, 'd', &flag, 0)) == 0) {
  287. errorflg = 1;
  288. error(WARNING, "file %s:%d illegal flag token <0x%x> rv=%d\n", filename, line, troffchar, rv);
  289. } else if (rv < 0) break;
  290. if ((rv=Bgetfield(Bfd, 'd', &charnum, 0)) == 0) {
  291. errorflg = 1;
  292. error(WARNING, "file %s:%d illegal character number token <0x%x> rv=%d\n", filename, line, troffchar, rv);
  293. } else if (rv < 0) break;
  294. flush:
  295. str = Brdline(Bfd, '\n');
  296. /* stash the crap from the end of the line for debugging */
  297. if (debug) {
  298. if (str == 0) {
  299. Bprint(Bstderr, "premature EOF\n");
  300. return(0);
  301. }
  302. str[Blinelen(Bfd)-1] = '\0';
  303. }
  304. line++;
  305. chartorune(&troffchar, stoken);
  306. if (specharflag) {
  307. if (debug)
  308. Bprint(Bstderr, "%s %d %d 0x%x %s # special\n",stoken, width, flag, charnum, str);
  309. }
  310. if (strcmp(stoken, "---") == 0) {
  311. thisfont = RUNEGETGROUP(charnum);
  312. thischar = RUNEGETCHAR(charnum);
  313. stoken[0] = '\0';
  314. } else {
  315. thisfont = RUNEGETGROUP(troffchar);
  316. thischar = RUNEGETCHAR(troffchar);
  317. }
  318. for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
  319. if ((*cp)->name) {
  320. if (debug) Bprint(Bstderr, "installing <%s>, found <%s>\n", stoken, (*cp)->name);
  321. if (strcmp((*cp)->name, stoken) == 0)
  322. break;
  323. }
  324. if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
  325. (*cp)->postfontid = RUNEGETGROUP(charnum);
  326. (*cp)->postcharid = RUNEGETCHAR(charnum);
  327. (*cp)->troffcharwidth = width;
  328. (*cp)->name = galloc(0, strlen(stoken)+1, "readtroffmetric: char name");
  329. (*cp)->next = 0;
  330. strcpy((*cp)->name, stoken);
  331. if (debug) {
  332. if (specharflag)
  333. Bprint(Bstderr, "%s", stoken);
  334. else
  335. Bputrune(Bstderr, troffchar);
  336. Bprint(Bstderr, " %d %d 0x%x %s # psfontid=0x%x pscharid=0x%x thisfont=0x%x thischar=0x%x\n",
  337. width, flag, charnum, str,
  338. (*cp)->postfontid,
  339. (*cp)->postcharid,
  340. thisfont, thischar);
  341. }
  342. }
  343. Bterm(Bfd);
  344. Bflush(Bstderr);
  345. return(1);
  346. }
  347. /* find index of troff font name in table
  348. * returns -1 if name is not in table
  349. * returns -2 if it cannot allocate memory
  350. * returns -3 if there is a font mapping problem
  351. * If insflg is not zero
  352. * and the name is not found in the table, insert it.
  353. */
  354. int
  355. findtfn(char *fontname, BOOLEAN insflg) {
  356. struct troffont *tp;
  357. int i, j;
  358. if (debug) {
  359. if (fontname==0) fprint(2, "findtfn(0x%x,%d)\n", fontname, insflg);
  360. else fprint(2, "findtfn(%s,%d)\n", fontname, insflg);
  361. }
  362. for (i=0; i<troffontcnt; i++) {
  363. if (troffontab[i].trfontid==0) {
  364. error(WARNING, "findtfn:troffontab[%d].trfontid=0x%x, botch!\n",
  365. i, troffontab[i].trfontid);
  366. continue;
  367. }
  368. if (strcmp(troffontab[i].trfontid, fontname) == 0)
  369. return(i);
  370. }
  371. if (insflg) {
  372. tp = (struct troffont *)galloc(troffontab, sizeof(struct troffont)*(troffontcnt+1), "findtfn: struct troffont:");
  373. if (tp == 0)
  374. return(-2);
  375. troffontab = tp;
  376. tp = &(troffontab[troffontcnt]);
  377. i = strlen(fontname);
  378. tp->trfontid = galloc(0, i+1, "findtfn: trfontid:");
  379. /* initialize new troff font entry with name and numeric fields to 0 */
  380. strncpy(tp->trfontid, fontname, i);
  381. tp->trfontid[i] = '\0';
  382. tp->special = FALSE;
  383. tp->spacewidth = 0;
  384. tp->psfmapsize = 0;
  385. tp->psfmap = 0;
  386. for (i=0; i<NUMOFONTS; i++)
  387. for (j=0; j<FONTSIZE; j++)
  388. tp->charent[i][j] = 0;
  389. troffontcnt++;
  390. if (!readtroffmetric(fontname, troffontcnt-1))
  391. return(-3);
  392. if (!readpsfontdesc(fontname, troffontcnt-1))
  393. return(-3);
  394. return(troffontcnt-1);
  395. }
  396. return(-1);
  397. }
  398. void
  399. finish(void) {
  400. int i;
  401. Bprint(Bstdout, "%s", TRAILER);
  402. Bprint(Bstdout, "done\n");
  403. Bprint(Bstdout, "%s", DOCUMENTFONTS);
  404. for (i=0; i<pfnamcnt; i++)
  405. if (pfnafontmtab[i].used)
  406. Bprint(Bstdout, " %s", pfnafontmtab[i].str);
  407. Bprint(Bstdout, "\n");
  408. Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
  409. }
  410. /* Set slant to n degrees. Disable slanting if n is 0. */
  411. void
  412. t_slant(int n) {
  413. fontslant = n;
  414. curpostfontid = -1;
  415. }
  416. /* Set character height to n points. Disabled if n is 0 or the current size. */
  417. void
  418. t_charht(int n) {
  419. fontheight = (n == fontsize) ? 0 : n;
  420. curpostfontid = -1;
  421. }