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