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