text2post.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <bio.h>
  5. #include <comments.h>
  6. #include <path.h>
  7. #define UNKNOWNCHAR "/sys/lib/postscript/prologues/pjw.char.ps"
  8. char *optnames = "a:c:f:l:m:n:o:p:s:t:x:y:P:";
  9. Biobuf *bstdin, *bstdout, *bstderr;
  10. Biobufhdr *Bstdin, *Bstdout, *Bstderr;
  11. int char_no = 0; /* character to be done on a line */
  12. int line_no = 0; /* line number on a page */
  13. int page_no = 0; /* page number in a document */
  14. int in_string; /* Boolean, to know whether or not we are inside a Postscript string */
  15. int spaces = 0;
  16. int tabs = 0;
  17. int pages_printed;
  18. double aspectratio = 1.0;
  19. int copies = 1;
  20. double magnification = 1.0;
  21. int landscape = 0;
  22. int formsperpage = 1;
  23. int linesperpage = 66;
  24. int pointsize = 10;
  25. double xoffset = .25;
  26. double yoffset = .25;
  27. char *passthrough = 0;
  28. static int pplistmaxsize=0;
  29. unsigned char *pplist=0; /* bitmap list for storing pages to print */
  30. struct strtab {
  31. int size;
  32. char *str;
  33. int used;
  34. };
  35. struct strtab charcode[256] = {
  36. {4, "\\000"}, {4, "\\001"}, {4, "\\002"}, {4, "\\003"},
  37. {4, "\\004"}, {4, "\\005"}, {4, "\\006"}, {4, "\\007"},
  38. {4, "\\010"}, {4, "\\011"}, {4, "\\012"}, {4, "\\013"},
  39. {4, "\\014"}, {4, "\\015"}, {4, "\\016"}, {4, "\\017"},
  40. {4, "\\020"}, {4, "\\021"}, {4, "\\022"}, {4, "\\023"},
  41. {4, "\\024"}, {4, "\\025"}, {4, "\\026"}, {4, "\\027"},
  42. {4, "\\030"}, {4, "\\031"}, {4, "\\032"}, {4, "\\033"},
  43. {4, "\\034"}, {4, "\\035"}, {4, "\\036"}, {4, "\\037"},
  44. {1, " "}, {1, "!"}, {1, "\""}, {1, "#"},
  45. {1, "$"}, {1, "%"}, {1, "&"}, {1, "'"},
  46. {2, "\\("}, {2, "\\)"}, {1, "*"}, {1, "+"},
  47. {1, ","}, {1, "-"}, {1, "."}, {1, "/"},
  48. {1, "0"}, {1, "1"}, {1, "2"}, {1, "3"},
  49. {1, "4"}, {1, "5"}, {1, "6"}, {1, "7"},
  50. {1, "8"}, {1, "9"}, {1, ":"}, {1, ";"},
  51. {1, "<"}, {1, "="}, {1, ">"}, {1, "?"},
  52. {1, "@"}, {1, "A"}, {1, "B"}, {1, "C"},
  53. {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
  54. {1, "H"}, {1, "I"}, {1, "J"}, {1, "K"},
  55. {1, "L"}, {1, "M"}, {1, "N"}, {1, "O"},
  56. {1, "P"}, {1, "Q"}, {1, "R"}, {1, "S"},
  57. {1, "T"}, {1, "U"}, {1, "V"}, {1, "W"},
  58. {1, "X"}, {1, "Y"}, {1, "Z"}, {1, "["},
  59. {2, "\\\\"}, {1, "]"}, {1, "^"}, {1, "_"},
  60. {1, "`"}, {1, "a"}, {1, "b"}, {1, "c"},
  61. {1, "d"}, {1, "e"}, {1, "f"}, {1, "g"},
  62. {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"},
  63. {1, "l"}, {1, "m"}, {1, "n"}, {1, "o"},
  64. {1, "p"}, {1, "q"}, {1, "r"}, {1, "s"},
  65. {1, "t"}, {1, "u"}, {1, "v"}, {1, "w"},
  66. {1, "x"}, {1, "y"}, {1, "z"}, {1, "{"},
  67. {1, "|"}, {1, "}"}, {1, "~"}, {4, "\\177"},
  68. {4, "\\200"}, {4, "\\201"}, {4, "\\202"}, {4, "\\203"},
  69. {4, "\\204"}, {4, "\\205"}, {4, "\\206"}, {4, "\\207"},
  70. {4, "\\210"}, {4, "\\211"}, {4, "\\212"}, {4, "\\213"},
  71. {4, "\\214"}, {4, "\\215"}, {4, "\\216"}, {4, "\\217"},
  72. {4, "\\220"}, {4, "\\221"}, {4, "\\222"}, {4, "\\223"},
  73. {4, "\\224"}, {4, "\\225"}, {4, "\\226"}, {4, "\\227"},
  74. {4, "\\230"}, {4, "\\231"}, {4, "\\232"}, {4, "\\233"},
  75. {4, "\\234"}, {4, "\\235"}, {4, "\\236"}, {4, "\\237"},
  76. {4, "\\240"}, {4, "\\241"}, {4, "\\242"}, {4, "\\243"},
  77. {4, "\\244"}, {4, "\\245"}, {4, "\\246"}, {4, "\\247"},
  78. {4, "\\250"}, {4, "\\251"}, {4, "\\252"}, {4, "\\253"},
  79. {4, "\\254"}, {4, "\\255"}, {4, "\\256"}, {4, "\\257"},
  80. {4, "\\260"}, {4, "\\261"}, {4, "\\262"}, {4, "\\263"},
  81. {4, "\\264"}, {4, "\\265"}, {4, "\\266"}, {4, "\\267"},
  82. {4, "\\270"}, {4, "\\271"}, {4, "\\272"}, {4, "\\273"},
  83. {4, "\\274"}, {4, "\\275"}, {4, "\\276"}, {4, "\\277"},
  84. {4, "\\300"}, {4, "\\301"}, {4, "\\302"}, {4, "\\303"},
  85. {4, "\\304"}, {4, "\\305"}, {4, "\\306"}, {4, "\\307"},
  86. {4, "\\310"}, {4, "\\311"}, {4, "\\312"}, {4, "\\313"},
  87. {4, "\\314"}, {4, "\\315"}, {4, "\\316"}, {4, "\\317"},
  88. {4, "\\320"}, {4, "\\321"}, {4, "\\322"}, {4, "\\323"},
  89. {4, "\\324"}, {4, "\\325"}, {4, "\\326"}, {4, "\\327"},
  90. {4, "\\330"}, {4, "\\331"}, {4, "\\332"}, {4, "\\333"},
  91. {4, "\\334"}, {4, "\\335"}, {4, "\\336"}, {4, "\\337"},
  92. {4, "\\340"}, {4, "\\341"}, {4, "\\342"}, {4, "\\343"},
  93. {4, "\\344"}, {4, "\\345"}, {4, "\\346"}, {4, "\\347"},
  94. {4, "\\350"}, {4, "\\351"}, {4, "\\352"}, {4, "\\353"},
  95. {4, "\\354"}, {4, "\\355"}, {4, "\\356"}, {4, "\\357"},
  96. {4, "\\360"}, {4, "\\361"}, {4, "\\362"}, {4, "\\363"},
  97. {4, "\\364"}, {4, "\\365"}, {4, "\\366"}, {4, "\\367"},
  98. {4, "\\370"}, {4, "\\371"}, {4, "\\372"}, {4, "\\373"},
  99. {4, "\\374"}, {4, "\\375"}, {4, "\\376"}, {4, "\\377"}
  100. };
  101. #define FONTABSIZE 0x27
  102. struct strtab fontname[FONTABSIZE] = {
  103. {19, "LucidaSansUnicode00", 0},
  104. {19, "LucidaSansUnicode01", 0},
  105. {19, "LucidaSansUnicode02", 0},
  106. {19, "LucidaSansUnicode03", 0},
  107. {19, "LucidaSansUnicode04", 0},
  108. {19, "LucidaSansUnicode05", 0},
  109. {0, "", 0},
  110. {0, "", 0},
  111. {0, "", 0},
  112. {0, "", 0},
  113. {0, "", 0},
  114. {0, "", 0},
  115. {0, "", 0},
  116. {0, "", 0},
  117. {0, "", 0},
  118. {0, "", 0},
  119. {0, "", 0},
  120. {0, "", 0},
  121. {0, "", 0},
  122. {0, "", 0},
  123. {0, "", 0},
  124. {0, "", 0},
  125. {0, "", 0},
  126. {0, "", 0},
  127. {0, "", 0},
  128. {0, "", 0},
  129. {0, "", 0},
  130. {0, "", 0},
  131. {0, "", 0},
  132. {0, "", 0},
  133. {0, "", 0},
  134. {0, "", 0},
  135. {19, "LucidaSansUnicode20", 0},
  136. {19, "LucidaSansUnicode21", 0},
  137. {19, "LucidaSansUnicode22", 0},
  138. {0, "", 0},
  139. {19, "LucidaSansUnicode24", 0},
  140. {19, "LucidaSansUnicode25", 0},
  141. {7, "Courier", 0}
  142. };
  143. /* This was taken from postprint */
  144. int
  145. cat(char *filename) {
  146. Biobuf *bfile;
  147. Biobufhdr *Bfile;
  148. int n;
  149. static char buf[Bsize];
  150. bstdin = Bopen(filename, 0);
  151. if (bstdin == 0) {
  152. return(1);
  153. }
  154. Bstdin = &(bstdin->Biobufhdr);
  155. if ((bfile = Bopen(filename, OREAD)) == 0) {
  156. return(1);
  157. }
  158. Bfile = &(bfile->Biobufhdr);
  159. while ((n=Bread(Bfile, buf, Bsize)) > 0) {
  160. if (Bwrite(Bstdout, buf, n) != n) {
  161. return(1);
  162. }
  163. }
  164. if (n != 0) {
  165. return(1);
  166. }
  167. return(0);
  168. }
  169. void
  170. prologues(void) {
  171. char *ts;
  172. int tabstop;
  173. Bprint(Bstdout, "%s", CONFORMING);
  174. Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
  175. Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
  176. Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
  177. Bprint(Bstdout, "%s", ENDCOMMENTS);
  178. if (cat(POSTPRINT)) {
  179. Bprint(Bstderr, "can't read %s", POSTPRINT);
  180. exits("prologue");
  181. }
  182. if (DOROUND)
  183. cat(ROUNDPAGE);
  184. tabstop = 0;
  185. ts = getenv("tabstop");
  186. if(ts != nil)
  187. tabstop = strtol(ts, nil, 0);
  188. if(tabstop == 0)
  189. tabstop = 8;
  190. Bprint(Bstdout, "/f {findfont pointsize scalefont setfont} bind def\n");
  191. Bprint(Bstdout, "/tabwidth /Courier f (");
  192. while(tabstop--)
  193. Bputc(Bstdout, 'n');
  194. Bprint(Bstdout, ") stringwidth pop def\n");
  195. Bprint(Bstdout, "/tab {tabwidth 0 ne {currentpoint 3 1 roll exch tabwidth mul add tabwidth\n");
  196. Bprint(Bstdout, "\tdiv truncate tabwidth mul exch moveto} if} bind def\n");
  197. Bprint(Bstdout, "/spacewidth /%s f ( ) stringwidth pop def\n", fontname[0].str);
  198. Bprint(Bstdout, "/sp {spacewidth mul 0 rmoveto} bind def\n");
  199. Bprint(Bstdout, "%s", ENDPROLOG);
  200. Bprint(Bstdout, "%s", BEGINSETUP);
  201. Bprint(Bstdout, "mark\n");
  202. if (formsperpage > 1) {
  203. Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
  204. Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
  205. }
  206. if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
  207. if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
  208. if (landscape) Bprint(Bstdout, "/landscape true def\n");
  209. if (magnification != 1) Bprint(Bstdout, "/magnification %s def\n", magnification);
  210. if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
  211. if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
  212. if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
  213. cat(ENCODINGDIR"/Latin1.enc");
  214. if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
  215. Bprint(Bstdout, "setup\n");
  216. if (formsperpage > 1) {
  217. cat(FORMFILE);
  218. Bprint(Bstdout, "%d setupforms \n", formsperpage);
  219. }
  220. if (cat(UNKNOWNCHAR))
  221. Bprint(Bstderr, "cannot open %s\n", UNKNOWNCHAR);
  222. Bprint(Bstdout, "%s", ENDSETUP);
  223. }
  224. int
  225. pageon(void) {
  226. if (pplist == 0 && page_no != 0) return(1); /* no page list, print all pages */
  227. if (page_no/8 < pplistmaxsize && (pplist[page_no/8] & 1<<(page_no%8)))
  228. return(1);
  229. else
  230. return(0);
  231. }
  232. void
  233. startpage(void) {
  234. ++char_no;
  235. ++line_no;
  236. ++page_no;
  237. if (pageon()) {
  238. ++pages_printed;
  239. Bprint(Bstdout, "%s %d %d\n", PAGE, page_no, pages_printed);
  240. Bprint(Bstdout, "/saveobj save def\n");
  241. Bprint(Bstdout, "mark\n");
  242. Bprint(Bstdout, "%d pagesetup\n", pages_printed);
  243. }
  244. }
  245. void
  246. endpage(void) {
  247. line_no = 0;
  248. char_no = 0;
  249. if (pageon()) {
  250. Bprint(Bstdout, "cleartomark\n");
  251. Bprint(Bstdout, "showpage\n");
  252. Bprint(Bstdout, "saveobj restore\n");
  253. Bprint(Bstdout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
  254. }
  255. }
  256. void
  257. startstring(void) {
  258. if (!in_string) {
  259. if (pageon()) Bprint(Bstdout, "(");
  260. in_string = 1;
  261. }
  262. }
  263. void
  264. endstring(void) {
  265. if (in_string) {
  266. if (pageon()) Bprint(Bstdout, ") show ");
  267. in_string = 0;
  268. }
  269. }
  270. void
  271. prspace(void) {
  272. if (spaces) {
  273. endstring();
  274. if (pageon()) Bprint(Bstdout, "%d sp ", spaces);
  275. spaces = 0;
  276. }
  277. }
  278. void
  279. prtab(void) {
  280. if (tabs) {
  281. endstring();
  282. if (pageon()) Bprint(Bstdout, "%d tab ", tabs);
  283. tabs = 0;
  284. }
  285. }
  286. void
  287. txt2post(void) {
  288. int lastfont = -1;
  289. int lastchar = -1;
  290. int thisfont, thischar;
  291. long r;
  292. in_string = 0;
  293. char_no = 0;
  294. line_no = 0;
  295. page_no = 0;
  296. spaces = 0;
  297. fontname[0].used++;
  298. while ((r=Bgetrune(Bstdin)) >= 0) {
  299. thischar = r & 0xff;
  300. thisfont = (r>>8) & 0xff;
  301. if (line_no == 0 && char_no == 0)
  302. startpage();
  303. if (line_no == 1 && char_no == 1) {
  304. if (pageon()) Bprint(Bstdout, " /%s f\n", fontname[thisfont].str);
  305. lastfont = thisfont;
  306. }
  307. switch (r) {
  308. case ' ':
  309. prtab();
  310. if (lastfont > 0) {
  311. spaces++;
  312. continue;
  313. }
  314. break;
  315. case '\n':
  316. case '\f':
  317. startstring();
  318. if (pageon()) Bprint(Bstdout, ")l\n");
  319. char_no = 1;
  320. in_string = 0;
  321. spaces = 0;
  322. tabs = 0;
  323. if (++line_no > linesperpage || r == '\f') {
  324. endpage();
  325. }
  326. lastchar = -1;
  327. continue;
  328. case '\t':
  329. prspace();
  330. tabs++;
  331. char_no++;
  332. lastchar = -1;
  333. continue;
  334. case '\b':
  335. /* just toss out backspaces for now */
  336. if (lastchar != -1) {
  337. endstring();
  338. if (pageon()) Bprint(Bstdout, "(%s) stringwidth pop neg 0 rmoveto ", charcode[lastchar].str);
  339. }
  340. char_no++;
  341. lastchar = -1;
  342. continue;
  343. }
  344. /* do something if font is out of table range */
  345. if (thisfont>=FONTABSIZE || fontname[thisfont].size == 0) {
  346. prspace();
  347. prtab();
  348. endstring();
  349. Bprint(Bstdout, "pw ");
  350. char_no++;
  351. lastchar = -1;
  352. continue;
  353. }
  354. if (thisfont != lastfont) {
  355. endstring();
  356. if (pageon()) {
  357. Bprint(Bstdout, "/%s f\n", fontname[thisfont].str);
  358. }
  359. fontname[thisfont].used++;
  360. }
  361. prspace();
  362. prtab();
  363. startstring();
  364. if (pageon()) Bprint(Bstdout, "%s", charcode[thischar].str);
  365. /* if (pageon()) Bprint(Bstdout, "%2.2x", thischar); /* try hex strings*/
  366. char_no++;
  367. lastchar = thischar;
  368. lastfont = thisfont;
  369. }
  370. if (line_no != 0 || char_no != 0) {
  371. if (char_no != 1) {
  372. Bprint(Bstderr, "premature EOF: newline appended\n");
  373. startstring();
  374. if (pageon()) Bprint(Bstdout, ")l\n");
  375. }
  376. endpage();
  377. }
  378. }
  379. void
  380. pagelist(char *list) {
  381. char c;
  382. int n, m;
  383. int state, start, end;
  384. if (list == 0) return;
  385. state = 1;
  386. while ((c=*list) != '\0') {
  387. n = 0;
  388. while (isdigit(c)) {
  389. n = n * 10 + c - '0';
  390. c = *++list;
  391. }
  392. switch (state) {
  393. case 1:
  394. start = n;
  395. case 2:
  396. if (n/8+1 > pplistmaxsize) {
  397. pplistmaxsize = n/8+1;
  398. if ((pplist = realloc(pplist, n/8+1)) == 0) {
  399. Bprint(Bstderr, "cannot allocate memory for page list\n");
  400. exits("malloc");
  401. }
  402. }
  403. for (m=start; m<=n; m++)
  404. pplist[m/8] |= 1<<(m%8);
  405. break;
  406. }
  407. switch (c) {
  408. case '-':
  409. state = 2;
  410. list++;
  411. break;
  412. case ',':
  413. state = 1;
  414. list++;
  415. break;
  416. case '\0':
  417. break;
  418. }
  419. }
  420. }
  421. void
  422. finish(void) {
  423. int i;
  424. Bprint(Bstdout, "%s", TRAILER);
  425. Bprint(Bstdout, "done\n");
  426. Bprint(Bstdout, "%s", DOCUMENTFONTS);
  427. for (i=0; i<FONTABSIZE; i++)
  428. if (fontname[i].used)
  429. Bprint(Bstdout, " %s", fontname[i].str);
  430. Bprint(Bstdout, "\n");
  431. Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
  432. }
  433. main(int argc, char *argv[]) {
  434. int i;
  435. char *t;
  436. Biobuf *input;
  437. if ((bstderr = (Biobuf *)malloc(sizeof(Biobuf))) == nil)
  438. exits("malloc");
  439. if (Binit(bstderr, 2, OWRITE) == Beof)
  440. exits("Binit");
  441. Bstderr = &(bstderr->Biobufhdr);
  442. if ((bstdout = (Biobuf *)malloc(sizeof(Biobuf))) == nil)
  443. exits("malloc");
  444. if (Binit(bstdout, 1, OWRITE) == Beof)
  445. exits("Binit");
  446. Bstdout = &(bstdout->Biobufhdr);
  447. ARGBEGIN{
  448. case 'a': /* aspect ratio */
  449. aspectratio = atof(ARGF());
  450. break;
  451. case 'c': /* copies */
  452. copies = atoi(ARGF());
  453. break;
  454. case 'f': /* primary font, for now */
  455. t = ARGF();
  456. fontname[0].str = malloc(strlen(t)+1);
  457. strcpy(fontname[0].str, t);
  458. break;
  459. case 'l': /* lines per page */
  460. linesperpage = atoi(ARGF());
  461. break;
  462. case 'm': /* magnification */
  463. magnification = atof(ARGF());
  464. break;
  465. case 'n': /* forms per page */
  466. formsperpage = atoi(ARGF());
  467. break;
  468. case 'o': /* output page list */
  469. pagelist(ARGF());
  470. break;
  471. case 'p': /* landscape or portrait mode */
  472. if ( ARGF()[0] == 'l' )
  473. landscape = 1;
  474. else
  475. landscape = 0;
  476. break;
  477. case 's': /* point size */
  478. pointsize = atoi(ARGF());
  479. break;
  480. case 'x': /* shift things horizontally */
  481. xoffset = atof(ARGF());
  482. break;
  483. case 'y': /* and vertically on the page */
  484. yoffset = atof(ARGF());
  485. break;
  486. case 'P': /* PostScript pass through */
  487. t = ARGF();
  488. i = strlen(t) + 1;
  489. passthrough = malloc(i);
  490. if (passthrough == 0) {
  491. Bprint(Bstderr, "cannot allocate memory for argument string\n");
  492. exits("malloc");
  493. }
  494. strncpy(passthrough, t, i);
  495. break;
  496. default: /* don't know what to do for ch */
  497. Bprint(Bstderr, "unknown option %C\n", ARGC());
  498. break;
  499. }ARGEND;
  500. prologues();
  501. if (argc <= 0) {
  502. if ((bstdin = (Biobuf *)malloc(sizeof(Biobuf))) == nil)
  503. exits("malloc");
  504. if (Binit(bstdin, 0, OREAD) == Beof) {
  505. fprint(2, "cannot Binit stdin\n");
  506. exits("Binit");
  507. }
  508. Bstdin = &(bstdin->Biobufhdr);
  509. txt2post();
  510. }
  511. for (i=0; i<argc; i++) {
  512. bstdin = Bopen(argv[i], 0);
  513. if (bstdin == 0) {
  514. fprint(2, "cannot open file %s\n", argv[i]);
  515. continue;
  516. }
  517. Bstdin = &(bstdin->Biobufhdr);
  518. txt2post();
  519. }
  520. finish();
  521. exits("");
  522. }