8 #define UNKNOWNCHAR unsharp("#9/postscript/prologues/pjw.char.ps")
10 char *optnames = "a:c:f:l:m:n:o:p:s:t:x:y:P:";
12 Biobuf *bstdin, *bstdout, *bstderr;
13 Biobuf *Bstdin, *Bstdout, *Bstderr;
14 int char_no = 0; /* character to be done on a line */
15 int line_no = 0; /* line number on a page */
16 int page_no = 0; /* page number in a document */
17 int in_string; /* Boolean, to know whether or not we are inside a Postscript string */
21 double aspectratio = 1.0;
23 double magnification = 1.0;
26 int linesperpage = 66;
30 char *passthrough = 0;
31 static int pplistmaxsize=0;
33 unsigned char *pplist=0; /* bitmap list for storing pages to print */
41 struct strtab charcode[256] = {
42 {4, "\\000"}, {4, "\\001"}, {4, "\\002"}, {4, "\\003"},
43 {4, "\\004"}, {4, "\\005"}, {4, "\\006"}, {4, "\\007"},
44 {4, "\\010"}, {4, "\\011"}, {4, "\\012"}, {4, "\\013"},
45 {4, "\\014"}, {4, "\\015"}, {4, "\\016"}, {4, "\\017"},
46 {4, "\\020"}, {4, "\\021"}, {4, "\\022"}, {4, "\\023"},
47 {4, "\\024"}, {4, "\\025"}, {4, "\\026"}, {4, "\\027"},
48 {4, "\\030"}, {4, "\\031"}, {4, "\\032"}, {4, "\\033"},
49 {4, "\\034"}, {4, "\\035"}, {4, "\\036"}, {4, "\\037"},
50 {1, " "}, {1, "!"}, {1, "\""}, {1, "#"},
51 {1, "$"}, {1, "%"}, {1, "&"}, {1, "'"},
52 {2, "\\("}, {2, "\\)"}, {1, "*"}, {1, "+"},
53 {1, ","}, {1, "-"}, {1, "."}, {1, "/"},
54 {1, "0"}, {1, "1"}, {1, "2"}, {1, "3"},
55 {1, "4"}, {1, "5"}, {1, "6"}, {1, "7"},
56 {1, "8"}, {1, "9"}, {1, ":"}, {1, ";"},
57 {1, "<"}, {1, "="}, {1, ">"}, {1, "?"},
58 {1, "@"}, {1, "A"}, {1, "B"}, {1, "C"},
59 {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
60 {1, "H"}, {1, "I"}, {1, "J"}, {1, "K"},
61 {1, "L"}, {1, "M"}, {1, "N"}, {1, "O"},
62 {1, "P"}, {1, "Q"}, {1, "R"}, {1, "S"},
63 {1, "T"}, {1, "U"}, {1, "V"}, {1, "W"},
64 {1, "X"}, {1, "Y"}, {1, "Z"}, {1, "["},
65 {2, "\\\\"}, {1, "]"}, {1, "^"}, {1, "_"},
66 {1, "`"}, {1, "a"}, {1, "b"}, {1, "c"},
67 {1, "d"}, {1, "e"}, {1, "f"}, {1, "g"},
68 {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"},
69 {1, "l"}, {1, "m"}, {1, "n"}, {1, "o"},
70 {1, "p"}, {1, "q"}, {1, "r"}, {1, "s"},
71 {1, "t"}, {1, "u"}, {1, "v"}, {1, "w"},
72 {1, "x"}, {1, "y"}, {1, "z"}, {1, "{"},
73 {1, "|"}, {1, "}"}, {1, "~"}, {4, "\\177"},
74 {4, "\\200"}, {4, "\\201"}, {4, "\\202"}, {4, "\\203"},
75 {4, "\\204"}, {4, "\\205"}, {4, "\\206"}, {4, "\\207"},
76 {4, "\\210"}, {4, "\\211"}, {4, "\\212"}, {4, "\\213"},
77 {4, "\\214"}, {4, "\\215"}, {4, "\\216"}, {4, "\\217"},
78 {4, "\\220"}, {4, "\\221"}, {4, "\\222"}, {4, "\\223"},
79 {4, "\\224"}, {4, "\\225"}, {4, "\\226"}, {4, "\\227"},
80 {4, "\\230"}, {4, "\\231"}, {4, "\\232"}, {4, "\\233"},
81 {4, "\\234"}, {4, "\\235"}, {4, "\\236"}, {4, "\\237"},
82 {4, "\\240"}, {4, "\\241"}, {4, "\\242"}, {4, "\\243"},
83 {4, "\\244"}, {4, "\\245"}, {4, "\\246"}, {4, "\\247"},
84 {4, "\\250"}, {4, "\\251"}, {4, "\\252"}, {4, "\\253"},
85 {4, "\\254"}, {4, "\\255"}, {4, "\\256"}, {4, "\\257"},
86 {4, "\\260"}, {4, "\\261"}, {4, "\\262"}, {4, "\\263"},
87 {4, "\\264"}, {4, "\\265"}, {4, "\\266"}, {4, "\\267"},
88 {4, "\\270"}, {4, "\\271"}, {4, "\\272"}, {4, "\\273"},
89 {4, "\\274"}, {4, "\\275"}, {4, "\\276"}, {4, "\\277"},
90 {4, "\\300"}, {4, "\\301"}, {4, "\\302"}, {4, "\\303"},
91 {4, "\\304"}, {4, "\\305"}, {4, "\\306"}, {4, "\\307"},
92 {4, "\\310"}, {4, "\\311"}, {4, "\\312"}, {4, "\\313"},
93 {4, "\\314"}, {4, "\\315"}, {4, "\\316"}, {4, "\\317"},
94 {4, "\\320"}, {4, "\\321"}, {4, "\\322"}, {4, "\\323"},
95 {4, "\\324"}, {4, "\\325"}, {4, "\\326"}, {4, "\\327"},
96 {4, "\\330"}, {4, "\\331"}, {4, "\\332"}, {4, "\\333"},
97 {4, "\\334"}, {4, "\\335"}, {4, "\\336"}, {4, "\\337"},
98 {4, "\\340"}, {4, "\\341"}, {4, "\\342"}, {4, "\\343"},
99 {4, "\\344"}, {4, "\\345"}, {4, "\\346"}, {4, "\\347"},
100 {4, "\\350"}, {4, "\\351"}, {4, "\\352"}, {4, "\\353"},
101 {4, "\\354"}, {4, "\\355"}, {4, "\\356"}, {4, "\\357"},
102 {4, "\\360"}, {4, "\\361"}, {4, "\\362"}, {4, "\\363"},
103 {4, "\\364"}, {4, "\\365"}, {4, "\\366"}, {4, "\\367"},
104 {4, "\\370"}, {4, "\\371"}, {4, "\\372"}, {4, "\\373"},
105 {4, "\\374"}, {4, "\\375"}, {4, "\\376"}, {4, "\\377"}
108 #define FONTABSIZE 0x2D
109 struct strtab fontname[FONTABSIZE] = {
110 {12, "DejaVuSans00", 0},
111 {12, "DejaVuSans01", 0},
112 {12, "DejaVuSans02", 0},
113 {12, "DejaVuSans03", 0},
114 {12, "DejaVuSans04", 0},
115 {12, "DejaVuSans05", 0},
139 {12, "DejaVuSans1D", 0},
140 {12, "DejaVuSans1E", 0},
141 {12, "DejaVuSans1F", 0},
142 {12, "DejaVuSans20", 0},
143 {12, "DejaVuSans21", 0},
144 {12, "DejaVuSans22", 0},
145 {12, "DejaVuSans23", 0},
146 {12, "DejaVuSans24", 0},
147 {12, "DejaVuSans25", 0},
148 {12, "DejaVuSans26", 0},
149 {12, "DejaVuSans27", 0},
150 {12, "DejaVuSans28", 0},
151 {12, "DejaVuSans29", 0},
152 {12, "DejaVuSans2A", 0},
153 {12, "DejaVuSans2B", 0},
158 #define FONTABSIZE 0x24
159 struct strtab fontname[FONTABSIZE] = {
160 {10, "LuxiSans00", 0},
161 {10, "LuxiSans01", 0},
162 {10, "LuxiSans02", 0},
163 {10, "LuxiSans03", 0},
192 {10, "LuxiSans20", 0},
193 {10, "LuxiSans21", 0},
194 {10, "LuxiSans22", 0},
199 /* This was taken from postprint */
202 cat(char *filename) {
203 Biobuf *bfile, *Bfile;
205 static char buf[Bsize];
207 filename = unsharp(filename);
208 bstdin = Bopen(filename, 0);
213 if ((bfile = Bopen(filename, OREAD)) == 0) {
217 while ((n=Bread(Bfile, buf, Bsize)) > 0) {
218 if (Bwrite(Bstdout, buf, n) != n) {
233 Bprint(Bstdout, "%s", CONFORMING);
234 Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
235 Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
236 Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
237 Bprint(Bstdout, "%s", ENDCOMMENTS);
239 if (cat(POSTPRINT)) {
240 Bprint(Bstderr, "can't read %s", POSTPRINT);
248 ts = getenv("tabstop");
250 tabstop = strtol(ts, nil, 0);
253 Bprint(Bstdout, "/f {findfont pointsize scalefont setfont} bind def\n");
254 Bprint(Bstdout, "/tabwidth /Courier f (");
257 Bprint(Bstdout, ") stringwidth pop def\n");
258 Bprint(Bstdout, "/tab {tabwidth 0 ne {currentpoint 3 1 roll exch tabwidth mul add tabwidth\n");
259 Bprint(Bstdout, "\tdiv truncate tabwidth mul exch moveto} if} bind def\n");
260 Bprint(Bstdout, "/spacewidth /%s f ( ) stringwidth pop def\n", fontname[0].str);
261 Bprint(Bstdout, "/sp {spacewidth mul 0 rmoveto} bind def\n");
262 Bprint(Bstdout, "%s", ENDPROLOG);
263 Bprint(Bstdout, "%s", BEGINSETUP);
264 Bprint(Bstdout, "mark\n");
266 if (formsperpage > 1) {
267 Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
268 Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
270 if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
271 if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
272 if (landscape) Bprint(Bstdout, "/landscape true def\n");
273 if (magnification != 1) Bprint(Bstdout, "/magnification %s def\n", magnification);
274 if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
275 if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
276 if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
277 cat(unsharp("#9/postscript/prologues/Latin1.enc"));
278 if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
279 Bprint(Bstdout, "setup\n");
280 if (formsperpage > 1) {
282 Bprint(Bstdout, "%d setupforms \n", formsperpage);
284 if (cat(UNKNOWNCHAR))
285 Bprint(Bstderr, "cannot open %s\n", UNKNOWNCHAR);
286 Bprint(Bstdout, "%s", ENDSETUP);
291 if (pplist == 0 && page_no != 0) return(1); /* no page list, print all pages */
292 if (page_no/8 < pplistmaxsize && (pplist[page_no/8] & 1<<(page_no%8)))
305 Bprint(Bstdout, "%s %d %d\n", PAGE, page_no, pages_printed);
306 Bprint(Bstdout, "/saveobj save def\n");
307 Bprint(Bstdout, "mark\n");
308 Bprint(Bstdout, "%d pagesetup\n", pages_printed);
317 Bprint(Bstdout, "cleartomark\n");
318 Bprint(Bstdout, "showpage\n");
319 Bprint(Bstdout, "saveobj restore\n");
320 Bprint(Bstdout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
327 if (pageon()) Bprint(Bstdout, "(");
335 if (pageon()) Bprint(Bstdout, ") show ");
344 if (pageon()) Bprint(Bstdout, "%d sp ", spaces);
353 if (pageon()) Bprint(Bstdout, "%d tab ", tabs);
362 int thisfont, thischar;
371 while ((r=Bgetrune(Bstdin)) >= 0) {
373 thisfont = (r>>8) & 0xff;
375 if (line_no == 0 && char_no == 0)
378 if (line_no == 1 && char_no == 1) {
379 if (pageon()) Bprint(Bstdout, " /%s f\n", fontname[thisfont].str);
394 if (pageon()) Bprint(Bstdout, ")l\n");
399 if (++line_no > linesperpage || r == '\f') {
411 /* just toss out backspaces for now */
412 if (lastchar != -1) {
414 if (pageon()) Bprint(Bstdout, "(%s) stringwidth pop neg 0 rmoveto ", charcode[lastchar].str);
421 /* do something if font is out of table range */
422 if (thisfont>=FONTABSIZE || fontname[thisfont].size == 0) {
426 Bprint(Bstdout, "pw ");
432 if (thisfont != lastfont) {
435 Bprint(Bstdout, "/%s f\n", fontname[thisfont].str);
437 fontname[thisfont].used++;
442 if (pageon()) Bprint(Bstdout, "%s", charcode[thischar].str);
443 /* if (pageon()) Bprint(Bstdout, "%2.2x", thischar); /* try hex strings*/
448 if (line_no != 0 || char_no != 0) {
450 Bprint(Bstderr, "premature EOF: newline appended\n");
452 if (pageon()) Bprint(Bstdout, ")l\n");
459 pagelist(char *list) {
464 if (list == 0) return;
467 while ((c=*list) != '\0') {
470 n = n * 10 + c - '0';
477 if (n/8+1 > pplistmaxsize) {
478 pplistmaxsize = n/8+1;
479 if ((pplist = realloc(pplist, n/8+1)) == 0) {
480 Bprint(Bstderr, "cannot allocate memory for page list\n");
484 for (m=start; m<=n; m++)
485 pplist[m/8] |= 1<<(m%8);
507 Bprint(Bstdout, "%s", TRAILER);
508 Bprint(Bstdout, "done\n");
509 Bprint(Bstdout, "%s", DOCUMENTFONTS);
511 for (i=0; i<FONTABSIZE; i++)
512 if (fontname[i].used)
513 Bprint(Bstdout, " %s", fontname[i].str);
514 Bprint(Bstdout, "\n");
516 Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
521 main(int argc, char *argv[]) {
525 if ((bstderr = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
527 if (Binit(bstderr, 2, OWRITE) == Beof)
531 if ((bstdout = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
533 if (Binit(bstdout, 1, OWRITE) == Beof)
538 case 'a': /* aspect ratio */
539 aspectratio = atof(ARGF());
541 case 'c': /* copies */
542 copies = atoi(ARGF());
544 case 'f': /* primary font, for now */
546 fontname[0].str = malloc(strlen(t)+1);
547 strcpy(fontname[0].str, t);
549 case 'l': /* lines per page */
550 linesperpage = atoi(ARGF());
552 case 'm': /* magnification */
553 magnification = atof(ARGF());
555 case 'n': /* forms per page */
556 formsperpage = atoi(ARGF());
558 case 'o': /* output page list */
561 case 'p': /* landscape or portrait mode */
562 if ( ARGF()[0] == 'l' )
567 case 's': /* point size */
568 pointsize = atoi(ARGF());
570 case 'x': /* shift things horizontally */
571 xoffset = atof(ARGF());
574 case 'y': /* and vertically on the page */
575 yoffset = atof(ARGF());
577 case 'P': /* PostScript pass through */
580 passthrough = malloc(i);
581 if (passthrough == 0) {
582 Bprint(Bstderr, "cannot allocate memory for argument string\n");
585 strncpy(passthrough, t, i);
587 default: /* don't know what to do for ch */
588 Bprint(Bstderr, "unknown option %C\n", ARGC());
593 if ((bstdin = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
595 if (Binit(bstdin, 0, OREAD) == Beof) {
596 fprint(2, "cannot Binit stdin\n");
602 for (i=0; i<argc; i++) {
603 bstdin = Bopen(argv[i], 0);
605 fprint(2, "cannot open file %s\n", argv[i]);