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 0x27
110 struct strtab fontname[FONTABSIZE] = {
111 {19, "LucidaSansUnicode00", 0},
112 {19, "LucidaSansUnicode01", 0},
113 {19, "LucidaSansUnicode02", 0},
114 {19, "LucidaSansUnicode03", 0},
115 {19, "LucidaSansUnicode04", 0},
116 {19, "LucidaSansUnicode05", 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 {0, "", 0},
140 {0, "", 0},
141 {0, "", 0},
142 {0, "", 0},
143 {19, "LucidaSansUnicode20", 0},
144 {19, "LucidaSansUnicode21", 0},
145 {19, "LucidaSansUnicode22", 0},
146 {0, "", 0},
147 {19, "LucidaSansUnicode24", 0},
148 {19, "LucidaSansUnicode25", 0},
149 {7, "Courier", 0}
150 };
152 /* This was taken from postprint */
154 int
155 cat(char *filename) {
156 Biobuf *bfile, *Bfile;
157 int n;
158 static char buf[Bsize];
160 bstdin = Bopen(filename, 0);
161 if (bstdin == 0) {
162 return(1);
164 Bstdin = bstdin;
165 if ((bfile = Bopen(filename, OREAD)) == 0) {
166 return(1);
168 Bfile = bfile;
169 while ((n=Bread(Bfile, buf, Bsize)) > 0) {
170 if (Bwrite(Bstdout, buf, n) != n) {
171 return(1);
174 if (n != 0) {
175 return(1);
177 return(0);
180 void
181 prologues(void) {
182 char *ts;
183 int tabstop;
185 Bprint(Bstdout, "%s", CONFORMING);
186 Bprint(Bstdout, "%s %s\n", VERSION, PROGRAMVERSION);
187 Bprint(Bstdout, "%s %s\n", DOCUMENTFONTS, ATEND);
188 Bprint(Bstdout, "%s %s\n", PAGES, ATEND);
189 Bprint(Bstdout, "%s", ENDCOMMENTS);
191 if (cat(POSTPRINT)) {
192 Bprint(Bstderr, "can't read %s", POSTPRINT);
193 exits("prologue");
196 if (DOROUND)
197 cat(ROUNDPAGE);
199 tabstop = 0;
200 ts = getenv("tabstop");
201 if(ts != nil)
202 tabstop = strtol(ts, nil, 0);
203 if(tabstop == 0)
204 tabstop = 8;
205 Bprint(Bstdout, "/f {findfont pointsize scalefont setfont} bind def\n");
206 Bprint(Bstdout, "/tabwidth /Courier f (");
207 while(tabstop--)
208 Bputc(Bstdout, 'n');
209 Bprint(Bstdout, ") stringwidth pop def\n");
210 Bprint(Bstdout, "/tab {tabwidth 0 ne {currentpoint 3 1 roll exch tabwidth mul add tabwidth\n");
211 Bprint(Bstdout, "\tdiv truncate tabwidth mul exch moveto} if} bind def\n");
212 Bprint(Bstdout, "/spacewidth /%s f ( ) stringwidth pop def\n", fontname[0].str);
213 Bprint(Bstdout, "/sp {spacewidth mul 0 rmoveto} bind def\n");
214 Bprint(Bstdout, "%s", ENDPROLOG);
215 Bprint(Bstdout, "%s", BEGINSETUP);
216 Bprint(Bstdout, "mark\n");
218 if (formsperpage > 1) {
219 Bprint(Bstdout, "%s %d\n", FORMSPERPAGE, formsperpage);
220 Bprint(Bstdout, "/formsperpage %d def\n", formsperpage);
222 if (aspectratio != 1) Bprint(Bstdout, "/aspectratio %g def\n", aspectratio);
223 if (copies != 1) Bprint(Bstdout, "/#copies %d store\n", copies);
224 if (landscape) Bprint(Bstdout, "/landscape true def\n");
225 if (magnification != 1) Bprint(Bstdout, "/magnification %s def\n", magnification);
226 if (pointsize != 10) Bprint(Bstdout, "/pointsize %d def\n", pointsize);
227 if (xoffset != .25) Bprint(Bstdout, "/xoffset %g def\n", xoffset);
228 if (yoffset != .25) Bprint(Bstdout, "/yoffset %g def\n", yoffset);
229 cat(unsharp("#9/postscript/prologues/Latin1.enc"));
230 if (passthrough != 0) Bprint(Bstdout, "%s\n", passthrough);
231 Bprint(Bstdout, "setup\n");
232 if (formsperpage > 1) {
233 cat(FORMFILE);
234 Bprint(Bstdout, "%d setupforms \n", formsperpage);
236 if (cat(UNKNOWNCHAR))
237 Bprint(Bstderr, "cannot open %s\n", UNKNOWNCHAR);
238 Bprint(Bstdout, "%s", ENDSETUP);
241 int
242 pageon(void) {
243 if (pplist == 0 && page_no != 0) return(1); /* no page list, print all pages */
244 if (page_no/8 < pplistmaxsize && (pplist[page_no/8] & 1<<(page_no%8)))
245 return(1);
246 else
247 return(0);
250 void
251 startpage(void) {
252 ++char_no;
253 ++line_no;
254 ++page_no;
255 if (pageon()) {
256 ++pages_printed;
257 Bprint(Bstdout, "%s %d %d\n", PAGE, page_no, pages_printed);
258 Bprint(Bstdout, "/saveobj save def\n");
259 Bprint(Bstdout, "mark\n");
260 Bprint(Bstdout, "%d pagesetup\n", pages_printed);
264 void
265 endpage(void) {
266 line_no = 0;
267 char_no = 0;
268 if (pageon()) {
269 Bprint(Bstdout, "cleartomark\n");
270 Bprint(Bstdout, "showpage\n");
271 Bprint(Bstdout, "saveobj restore\n");
272 Bprint(Bstdout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
276 void
277 startstring(void) {
278 if (!in_string) {
279 if (pageon()) Bprint(Bstdout, "(");
280 in_string = 1;
284 void
285 endstring(void) {
286 if (in_string) {
287 if (pageon()) Bprint(Bstdout, ") show ");
288 in_string = 0;
292 void
293 prspace(void) {
294 if (spaces) {
295 endstring();
296 if (pageon()) Bprint(Bstdout, "%d sp ", spaces);
297 spaces = 0;
301 void
302 prtab(void) {
303 if (tabs) {
304 endstring();
305 if (pageon()) Bprint(Bstdout, "%d tab ", tabs);
306 tabs = 0;
310 void
311 txt2post(void) {
312 int lastfont = -1;
313 int lastchar = -1;
314 int thisfont, thischar;
315 long r;
317 in_string = 0;
318 char_no = 0;
319 line_no = 0;
320 page_no = 0;
321 spaces = 0;
322 fontname[0].used++;
323 while ((r=Bgetrune(Bstdin)) >= 0) {
324 thischar = r & 0xff;
325 thisfont = (r>>8) & 0xff;
327 if (line_no == 0 && char_no == 0)
328 startpage();
330 if (line_no == 1 && char_no == 1) {
331 if (pageon()) Bprint(Bstdout, " /%s f\n", fontname[thisfont].str);
332 lastfont = thisfont;
335 switch (r) {
336 case ' ':
337 prtab();
338 if (lastfont > 0) {
339 spaces++;
340 continue;
342 break;
343 case '\n':
344 case '\f':
345 startstring();
346 if (pageon()) Bprint(Bstdout, ")l\n");
347 char_no = 1;
348 in_string = 0;
349 spaces = 0;
350 tabs = 0;
351 if (++line_no > linesperpage || r == '\f') {
352 endpage();
354 lastchar = -1;
355 continue;
356 case '\t':
357 prspace();
358 tabs++;
359 char_no++;
360 lastchar = -1;
361 continue;
362 case '\b':
363 /* just toss out backspaces for now */
364 if (lastchar != -1) {
365 endstring();
366 if (pageon()) Bprint(Bstdout, "(%s) stringwidth pop neg 0 rmoveto ", charcode[lastchar].str);
368 char_no++;
369 lastchar = -1;
370 continue;
373 /* do something if font is out of table range */
374 if (thisfont>=FONTABSIZE || fontname[thisfont].size == 0) {
375 prspace();
376 prtab();
377 endstring();
378 Bprint(Bstdout, "pw ");
379 char_no++;
380 lastchar = -1;
381 continue;
384 if (thisfont != lastfont) {
385 endstring();
386 if (pageon()) {
387 Bprint(Bstdout, "/%s f\n", fontname[thisfont].str);
389 fontname[thisfont].used++;
391 prspace();
392 prtab();
393 startstring();
394 if (pageon()) Bprint(Bstdout, "%s", charcode[thischar].str);
395 /* if (pageon()) Bprint(Bstdout, "%2.2x", thischar); /* try hex strings*/
396 char_no++;
397 lastchar = thischar;
398 lastfont = thisfont;
400 if (line_no != 0 || char_no != 0) {
401 if (char_no != 1) {
402 Bprint(Bstderr, "premature EOF: newline appended\n");
403 startstring();
404 if (pageon()) Bprint(Bstdout, ")l\n");
406 endpage();
410 void
411 pagelist(char *list) {
412 char c;
413 int n, m;
414 int state, start, end;
416 if (list == 0) return;
417 state = 1;
418 while ((c=*list) != '\0') {
419 n = 0;
420 while (isdigit(c)) {
421 n = n * 10 + c - '0';
422 c = *++list;
424 switch (state) {
425 case 1:
426 start = n;
427 case 2:
428 if (n/8+1 > pplistmaxsize) {
429 pplistmaxsize = n/8+1;
430 if ((pplist = realloc(pplist, n/8+1)) == 0) {
431 Bprint(Bstderr, "cannot allocate memory for page list\n");
432 exits("malloc");
435 for (m=start; m<=n; m++)
436 pplist[m/8] |= 1<<(m%8);
437 break;
439 switch (c) {
440 case '-':
441 state = 2;
442 list++;
443 break;
444 case ',':
445 state = 1;
446 list++;
447 break;
448 case '\0':
449 break;
454 void
455 finish(void) {
456 int i;
458 Bprint(Bstdout, "%s", TRAILER);
459 Bprint(Bstdout, "done\n");
460 Bprint(Bstdout, "%s", DOCUMENTFONTS);
462 for (i=0; i<FONTABSIZE; i++)
463 if (fontname[i].used)
464 Bprint(Bstdout, " %s", fontname[i].str);
465 Bprint(Bstdout, "\n");
467 Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
471 main(int argc, char *argv[]) {
472 int i;
473 char *t;
474 Biobuf *input;
476 if ((bstderr = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
477 exits("malloc");
478 if (Binit(bstderr, 2, OWRITE) == Beof)
479 exits("Binit");
480 Bstderr = bstderr;
482 if ((bstdout = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
483 exits("malloc");
484 if (Binit(bstdout, 1, OWRITE) == Beof)
485 exits("Binit");
486 Bstdout = bstdout;
488 ARGBEGIN{
489 case 'a': /* aspect ratio */
490 aspectratio = atof(ARGF());
491 break;
492 case 'c': /* copies */
493 copies = atoi(ARGF());
494 break;
495 case 'f': /* primary font, for now */
496 t = ARGF();
497 fontname[0].str = malloc(strlen(t)+1);
498 strcpy(fontname[0].str, t);
499 break;
500 case 'l': /* lines per page */
501 linesperpage = atoi(ARGF());
502 break;
503 case 'm': /* magnification */
504 magnification = atof(ARGF());
505 break;
506 case 'n': /* forms per page */
507 formsperpage = atoi(ARGF());
508 break;
509 case 'o': /* output page list */
510 pagelist(ARGF());
511 break;
512 case 'p': /* landscape or portrait mode */
513 if ( ARGF()[0] == 'l' )
514 landscape = 1;
515 else
516 landscape = 0;
517 break;
518 case 's': /* point size */
519 pointsize = atoi(ARGF());
520 break;
521 case 'x': /* shift things horizontally */
522 xoffset = atof(ARGF());
523 break;
525 case 'y': /* and vertically on the page */
526 yoffset = atof(ARGF());
527 break;
528 case 'P': /* PostScript pass through */
529 t = ARGF();
530 i = strlen(t) + 1;
531 passthrough = malloc(i);
532 if (passthrough == 0) {
533 Bprint(Bstderr, "cannot allocate memory for argument string\n");
534 exits("malloc");
536 strncpy(passthrough, t, i);
537 break;
538 default: /* don't know what to do for ch */
539 Bprint(Bstderr, "unknown option %C\n", ARGC());
540 break;
541 }ARGEND;
542 prologues();
543 if (argc <= 0) {
544 if ((bstdin = (Biobuf *)malloc(sizeof(Biobuf))) < (Biobuf *)0)
545 exits("malloc");
546 if (Binit(bstdin, 0, OREAD) == Beof) {
547 fprint(2, "cannot Binit stdin\n");
548 exits("Binit");
550 Bstdin = bstdin;
551 txt2post();
553 for (i=0; i<argc; i++) {
554 bstdin = Bopen(argv[i], 0);
555 if (bstdin == 0) {
556 fprint(2, "cannot open file %s\n", argv[i]);
557 continue;
559 Bstdin = bstdin;
560 txt2post();
562 finish();
563 exits("");