Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <ctype.h>
4 #include <bio.h>
5 #include <comments.h>
6 #include <path.h>
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 */
18 int spaces = 0;
19 int tabs = 0;
20 int pages_printed;
21 double aspectratio = 1.0;
22 int copies = 1;
23 double magnification = 1.0;
24 int landscape = 0;
25 int formsperpage = 1;
26 int linesperpage = 66;
27 int pointsize = 10;
28 double xoffset = .25;
29 double yoffset = .25;
30 char *passthrough = 0;
31 static int pplistmaxsize=0;
33 unsigned char *pplist=0; /* bitmap list for storing pages to print */
35 struct strtab {
36 int size;
37 char *str;
38 int used;
39 };
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"}
106 };
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},
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 {0, "", 0},
136 {0, "", 0},
137 {0, "", 0},
138 {0, "", 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},
154 {7, "Courier", 0}
155 };
157 /*
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},
164 {0, "", 0},
165 {0, "", 0},
166 {0, "", 0},
167 {0, "", 0},
168 {0, "", 0},
169 {0, "", 0},
170 {0, "", 0},
171 {0, "", 0},
172 {0, "", 0},
173 {0, "", 0},
174 {0, "", 0},
175 {0, "", 0},
176 {0, "", 0},
177 {0, "", 0},
178 {0, "", 0},
179 {0, "", 0},
180 {0, "", 0},
181 {0, "", 0},
182 {0, "", 0},
183 {0, "", 0},
184 {0, "", 0},
185 {0, "", 0},
186 {0, "", 0},
187 {0, "", 0},
188 {0, "", 0},
189 {0, "", 0},
190 {0, "", 0},
191 {0, "", 0},
192 {10, "LuxiSans20", 0},
193 {10, "LuxiSans21", 0},
194 {10, "LuxiSans22", 0},
195 {7, "Courier", 0}
196 };
197 */
199 /* This was taken from postprint */
201 int
202 cat(char *filename) {
203 Biobuf *bfile, *Bfile;
204 int n;
205 static char buf[Bsize];
207 filename = unsharp(filename);
208 bstdin = Bopen(filename, 0);
209 if (bstdin == 0) {
210 return(1);
212 Bstdin = bstdin;
213 if ((bfile = Bopen(filename, OREAD)) == 0) {
214 return(1);
216 Bfile = bfile;
217 while ((n=Bread(Bfile, buf, Bsize)) > 0) {
218 if (Bwrite(Bstdout, buf, n) != n) {
219 return(1);
222 if (n != 0) {
223 return(1);
225 return(0);
228 void
229 prologues(void) {
230 char *ts;
231 int tabstop;
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);
241 exits("prologue");
244 if (DOROUND)
245 cat(ROUNDPAGE);
247 tabstop = 0;
248 ts = getenv("tabstop");
249 if(ts != nil)
250 tabstop = strtol(ts, nil, 0);
251 if(tabstop == 0)
252 tabstop = 8;
253 Bprint(Bstdout, "/f {findfont pointsize scalefont setfont} bind def\n");
254 Bprint(Bstdout, "/tabwidth /Courier f (");
255 while(tabstop--)
256 Bputc(Bstdout, 'n');
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) {
281 cat(FORMFILE);
282 Bprint(Bstdout, "%d setupforms \n", formsperpage);
284 if (cat(UNKNOWNCHAR))
285 Bprint(Bstderr, "cannot open %s\n", UNKNOWNCHAR);
286 Bprint(Bstdout, "%s", ENDSETUP);
289 int
290 pageon(void) {
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)))
293 return(1);
294 else
295 return(0);
298 void
299 startpage(void) {
300 ++char_no;
301 ++line_no;
302 ++page_no;
303 if (pageon()) {
304 ++pages_printed;
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);
312 void
313 endpage(void) {
314 line_no = 0;
315 char_no = 0;
316 if (pageon()) {
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);
324 void
325 startstring(void) {
326 if (!in_string) {
327 if (pageon()) Bprint(Bstdout, "(");
328 in_string = 1;
332 void
333 endstring(void) {
334 if (in_string) {
335 if (pageon()) Bprint(Bstdout, ") show ");
336 in_string = 0;
340 void
341 prspace(void) {
342 if (spaces) {
343 endstring();
344 if (pageon()) Bprint(Bstdout, "%d sp ", spaces);
345 spaces = 0;
349 void
350 prtab(void) {
351 if (tabs) {
352 endstring();
353 if (pageon()) Bprint(Bstdout, "%d tab ", tabs);
354 tabs = 0;
358 void
359 txt2post(void) {
360 int lastfont = -1;
361 int lastchar = -1;
362 int thisfont, thischar;
363 long r;
365 in_string = 0;
366 char_no = 0;
367 line_no = 0;
368 page_no = 0;
369 spaces = 0;
370 fontname[0].used++;
371 while ((r=Bgetrune(Bstdin)) >= 0) {
372 thischar = r & 0xff;
373 thisfont = (r>>8) & 0xff;
375 if (line_no == 0 && char_no == 0)
376 startpage();
378 if (line_no == 1 && char_no == 1) {
379 if (pageon()) Bprint(Bstdout, " /%s f\n", fontname[thisfont].str);
380 lastfont = thisfont;
383 switch (r) {
384 case ' ':
385 prtab();
386 if (lastfont > 0) {
387 spaces++;
388 continue;
390 break;
391 case '\n':
392 case '\f':
393 startstring();
394 if (pageon()) Bprint(Bstdout, ")l\n");
395 char_no = 1;
396 in_string = 0;
397 spaces = 0;
398 tabs = 0;
399 if (++line_no > linesperpage || r == '\f') {
400 endpage();
402 lastchar = -1;
403 continue;
404 case '\t':
405 prspace();
406 tabs++;
407 char_no++;
408 lastchar = -1;
409 continue;
410 case '\b':
411 /* just toss out backspaces for now */
412 if (lastchar != -1) {
413 endstring();
414 if (pageon()) Bprint(Bstdout, "(%s) stringwidth pop neg 0 rmoveto ", charcode[lastchar].str);
416 char_no++;
417 lastchar = -1;
418 continue;
421 /* do something if font is out of table range */
422 if (thisfont>=FONTABSIZE || fontname[thisfont].size == 0) {
423 prspace();
424 prtab();
425 endstring();
426 Bprint(Bstdout, "pw ");
427 char_no++;
428 lastchar = -1;
429 continue;
432 if (thisfont != lastfont) {
433 endstring();
434 if (pageon()) {
435 Bprint(Bstdout, "/%s f\n", fontname[thisfont].str);
437 fontname[thisfont].used++;
439 prspace();
440 prtab();
441 startstring();
442 if (pageon()) Bprint(Bstdout, "%s", charcode[thischar].str);
443 /* if (pageon()) Bprint(Bstdout, "%2.2x", thischar); /* try hex strings*/
444 char_no++;
445 lastchar = thischar;
446 lastfont = thisfont;
448 if (line_no != 0 || char_no != 0) {
449 if (char_no != 1) {
450 Bprint(Bstderr, "premature EOF: newline appended\n");
451 startstring();
452 if (pageon()) Bprint(Bstdout, ")l\n");
454 endpage();
458 void
459 pagelist(char *list) {
460 char c;
461 int n, m;
462 int state, start;
464 if (list == 0) return;
465 state = 1;
466 start = 0;
467 while ((c=*list) != '\0') {
468 n = 0;
469 while (isdigit(c)) {
470 n = n * 10 + c - '0';
471 c = *++list;
473 switch (state) {
474 case 1:
475 start = n;
476 case 2:
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");
481 exits("malloc");
484 for (m=start; m<=n; m++)
485 pplist[m/8] |= 1<<(m%8);
486 break;
488 switch (c) {
489 case '-':
490 state = 2;
491 list++;
492 break;
493 case ',':
494 state = 1;
495 list++;
496 break;
497 case '\0':
498 break;
503 void
504 finish(void) {
505 int i;
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);
520 int
521 main(int argc, char *argv[]) {
522 int i;
523 char *t;
525 if ((bstderr = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
526 exits("malloc");
527 if (Binit(bstderr, 2, OWRITE) == Beof)
528 exits("Binit");
529 Bstderr = bstderr;
531 if ((bstdout = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
532 exits("malloc");
533 if (Binit(bstdout, 1, OWRITE) == Beof)
534 exits("Binit");
535 Bstdout = bstdout;
537 ARGBEGIN{
538 case 'a': /* aspect ratio */
539 aspectratio = atof(ARGF());
540 break;
541 case 'c': /* copies */
542 copies = atoi(ARGF());
543 break;
544 case 'f': /* primary font, for now */
545 t = ARGF();
546 fontname[0].str = malloc(strlen(t)+1);
547 strcpy(fontname[0].str, t);
548 break;
549 case 'l': /* lines per page */
550 linesperpage = atoi(ARGF());
551 break;
552 case 'm': /* magnification */
553 magnification = atof(ARGF());
554 break;
555 case 'n': /* forms per page */
556 formsperpage = atoi(ARGF());
557 break;
558 case 'o': /* output page list */
559 pagelist(ARGF());
560 break;
561 case 'p': /* landscape or portrait mode */
562 if ( ARGF()[0] == 'l' )
563 landscape = 1;
564 else
565 landscape = 0;
566 break;
567 case 's': /* point size */
568 pointsize = atoi(ARGF());
569 break;
570 case 'x': /* shift things horizontally */
571 xoffset = atof(ARGF());
572 break;
574 case 'y': /* and vertically on the page */
575 yoffset = atof(ARGF());
576 break;
577 case 'P': /* PostScript pass through */
578 t = ARGF();
579 i = strlen(t) + 1;
580 passthrough = malloc(i);
581 if (passthrough == 0) {
582 Bprint(Bstderr, "cannot allocate memory for argument string\n");
583 exits("malloc");
585 strncpy(passthrough, t, i);
586 break;
587 default: /* don't know what to do for ch */
588 Bprint(Bstderr, "unknown option %C\n", ARGC());
589 break;
590 }ARGEND;
591 prologues();
592 if (argc <= 0) {
593 if ((bstdin = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
594 exits("malloc");
595 if (Binit(bstdin, 0, OREAD) == Beof) {
596 fprint(2, "cannot Binit stdin\n");
597 exits("Binit");
599 Bstdin = bstdin;
600 txt2post();
602 for (i=0; i<argc; i++) {
603 bstdin = Bopen(argv[i], 0);
604 if (bstdin == 0) {
605 fprint(2, "cannot open file %s\n", argv[i]);
606 continue;
608 Bstdin = bstdin;
609 txt2post();
611 finish();
612 exits("");
613 return 0;