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