xref: /plan9-contrib/sys/src/cmd/postscript/tr2post/ps_include.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <stdio.h>
5 #include "../common/common.h"
6 #include "ps_include.h"
7 
8 extern int curpostfontid;
9 extern int curfontsize;
10 
11 typedef struct {long start, end;} Section;
12 static char *buf;
13 
14 static
15 copy(Biobufhdr *fin, Biobufhdr *fout, Section *s) {
16 
17 	if (s->end <= s->start)
18 		return;
19 	Bseek(fin, s->start, 0);
20 	while (Bseek(fin, 0L, 1) < s->end && (buf=Brdline(fin, '\n')) != NULL)
21 		if (buf[0] != '%')
22 			Bwrite(fout, buf, Blinelen(fin));
23 }
24 
25 /*
26  *
27  * Reads a PostScript file (*fin), and uses structuring comments to locate the
28  * prologue, trailer, global definitions, and the requested page. After the whole
29  * file is scanned, the  special ps_include PostScript definitions are copied to
30  * *fout, followed by the prologue, global definitions, the requested page, and
31  * the trailer. Before returning the initial environment (saved in PS_head) is
32  * restored.
33  *
34  * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
35  * comment, if found, takes precedence.
36  *
37  */
38 /*	*fin, *fout;		/* input and output files */
39 /*	page_no;		/* physical page number from *fin */
40 /*	whiteout;		/* erase picture area */
41 /*	outline;		/* draw a box around it and */
42 /*	scaleboth;		/* scale both dimensions - if not zero */
43 /*	cx, cy;			/* center of the picture and */
44 /*	sx, sy;			/* its size - in current coordinates */
45 /*	ax, ay;			/* left-right, up-down adjustment */
46 /*	rot;			/* rotation - in clockwise degrees */
47 
48 void
49 ps_include(Biobufhdr *fin, Biobufhdr *fout, int page_no, int whiteout,
50 	int outline, int scaleboth, double cx, double cy, double sx, double sy,
51 	double ax, double ay, double rot) {
52 	char		**strp;
53 	int		foundpage = 0;		/* found the page when non zero */
54 	int		foundpbox = 0;		/* found the page bounding box */
55 	int		nglobal = 0;		/* number of global defs so far */
56 	int		maxglobal = 0;		/* and the number we've got room for */
57 	Section	prolog, page, trailer;	/* prologue, page, and trailer offsets */
58 	Section	*global;		/* offsets for all global definitions */
59 	double	llx, lly;		/* lower left and */
60 	double	urx, ury;		/* upper right corners - default coords */
61 	double	w = whiteout != 0;	/* mostly for the var() macro */
62 	double	o = outline != 0;
63 	double	s = scaleboth != 0;
64 	int		i;		/* loop index */
65 
66 #define has(word)	(strncmp(buf, word, strlen(word)) == 0)
67 #define grab(n)		((Section *)(nglobal \
68 			? realloc((char *)global, n*sizeof(Section)) \
69 			: calloc(n, sizeof(Section))))
70 
71 	llx = lly = 0;		/* default BoundingBox - 8.5x11 inches */
72 	urx = 72 * 8.5;
73 	ury = 72 * 11.0;
74 
75 	/* section boundaries and bounding box */
76 
77 	prolog.start = prolog.end = 0;
78 	page.start = page.end = 0;
79 	trailer.start = 0;
80 	Bseek(fin, 0L, 0);
81 
82 	while ((buf=Brdline(fin, '\n')) != NULL) {
83 		buf[Blinelen(fin)-1] = '\0';
84 		if (!has("%%"))
85 			continue;
86 		else if (has("%%Page: ")) {
87 			if (!foundpage)
88 				page.start = Bseek(fin, 0L, 1);
89 			sscanf(buf, "%*s %*s %d", &i);
90 			if (i == page_no)
91 				foundpage = 1;
92 			else if (foundpage && page.end <= page.start)
93 				page.end = Bseek(fin, 0L, 1);
94 		} else if (has("%%EndPage: ")) {
95 			sscanf(buf, "%*s %*s %d", &i);
96 			if (i == page_no) {
97 				foundpage = 1;
98 				page.end = Bseek(fin, 0L, 1);
99 			}
100 			if (!foundpage)
101 				page.start = Bseek(fin, 0L, 1);
102 		} else if (has("%%PageBoundingBox: ")) {
103 			if (i == page_no) {
104 				foundpbox = 1;
105 				sscanf(buf, "%*s %lf %lf %lf %lf",
106 						&llx, &lly, &urx, &ury);
107 			}
108 		} else if (has("%%BoundingBox: ")) {
109 			if (!foundpbox)
110 				sscanf(buf,"%*s %lf %lf %lf %lf",
111 						&llx, &lly, &urx, &ury);
112 		} else if (has("%%EndProlog") || has("%%EndSetup") || has("%%EndDocumentSetup"))
113 			prolog.end = page.start = Bseek(fin, 0L, 1);
114 		else if (has("%%Trailer"))
115 			trailer.start = Bseek(fin, 0L, 1);
116 		else if (has("%%BeginGlobal")) {
117 			if (page.end <= page.start) {
118 				if (nglobal >= maxglobal) {
119 					maxglobal += 20;
120 					global = grab(maxglobal);
121 				}
122 				global[nglobal].start = Bseek(fin, 0L, 1);
123 			}
124 		} else if (has("%%EndGlobal"))
125 			if (page.end <= page.start)
126 				global[nglobal++].end = Bseek(fin, 0L, 1);
127 	}
128 	Bseek(fin, 0L, 2);
129 	if (trailer.start == 0)
130 		trailer.start = Bseek(fin, 0L, 1);
131 	trailer.end = Bseek(fin, 0L, 1);
132 
133 	if (page.end <= page.start)
134 		page.end = trailer.start;
135 
136 /*
137 fprint(2, "prolog=(%d,%d)\n", prolog.start, prolog.end);
138 fprint(2, "page=(%d,%d)\n", page.start, page.end);
139 for(i = 0; i < nglobal; i++)
140 	fprint(2, "global[%d]=(%d,%d)\n", i, global[i].start, global[i].end);
141 fprint(2, "trailer=(%d,%d)\n", trailer.start, trailer.end);
142 */
143 
144 	/* all output here */
145 	for (strp = PS_head; *strp != NULL; strp++)
146 		Bwrite(fout, *strp, strlen(*strp));
147 
148 	Bprint(fout, "/llx %g def\n", llx);
149 	Bprint(fout, "/lly %g def\n", lly);
150 	Bprint(fout, "/urx %g def\n", urx);
151 	Bprint(fout, "/ury %g def\n", ury);
152 	Bprint(fout, "/w %g def\n", w);
153 	Bprint(fout, "/o %g def\n", o);
154 	Bprint(fout, "/s %g def\n", s);
155 	Bprint(fout, "/cx %g def\n", cx);
156 	Bprint(fout, "/cy %g def\n", cy);
157 	Bprint(fout, "/sx %g def\n", sx);
158 	Bprint(fout, "/sy %g def\n", sy);
159 	Bprint(fout, "/ax %g def\n", ax);
160 	Bprint(fout, "/ay %g def\n", ay);
161 	Bprint(fout, "/rot %g def\n", rot);
162 
163 	for (strp = PS_setup; *strp != NULL; strp++)
164 		Bwrite(fout, *strp, strlen(*strp));
165 
166 	copy(fin, fout, &prolog);
167 	for(i = 0; i < nglobal; i++)
168 		copy(fin, fout, &global[i]);
169 	copy(fin, fout, &page);
170 	copy(fin, fout, &trailer);
171 	for (strp = PS_tail; *strp != NULL; strp++)
172 		Bwrite(fout, *strp, strlen(*strp));
173 
174 	if(nglobal)
175 		free(global);
176 
177 	/* force the program to reestablish its state */
178 	curpostfontid = -1;
179 	curfontsize = -1;
180 }
181