1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include "../common/common.h"
6 #include "tr2post.h"
7
8 BOOLEAN drawflag = FALSE;
9 BOOLEAN inpath = FALSE; /* TRUE if we're putting pieces together */
10
11 void
cover(double x,double y)12 cover(double x, double y) {
13 USED(x, y);
14 }
15
16 void
drawspline(Biobufhdr * Bp,int flag)17 drawspline(Biobufhdr *Bp, int flag) { /* flag!=1 connect end points */
18 int x[100], y[100];
19 int i, N;
20 /*
21 * Spline drawing routine for Postscript printers. The complicated stuff is
22 * handled by procedure Ds, which should be defined in the library file. I've
23 * seen wrong implementations of troff's spline drawing, so fo the record I'll
24 * write down the parametric equations and the necessary conversions to Bezier
25 * cubic splines (as used in Postscript).
26 *
27 * Parametric equation (x coordinate only):
28 *
29 * (x2 - 2 * x1 + x0) 2 (x0 + x1)
30 * x = ------------------ * t + (x1 - x0) * t + ---------
31 * 2 2
32 *
33 * The coefficients in the Bezier cubic are,
34 *
35 * A = 0
36 * B = (x2 - 2 * x1 + x0) / 2
37 * C = x1 - x0
38 *
39 * while the current point is,
40 *
41 * current-point = (x0 + x1) / 2
42 *
43 * Using the relationships given in the Postscript manual (page 121) it's easy to
44 * see that the control points are given by,
45 *
46 * x0' = (x0 + 5 * x1) / 6
47 * x1' = (x2 + 5 * x1) / 6
48 * x2' = (x1 + x2) / 2
49 *
50 * where the primed variables are the ones used by curveto. The calculations
51 * shown above are done in procedure Ds using the coordinates set up in both
52 * the x[] and y[] arrays.
53 *
54 * A simple test of whether your spline drawing is correct would be to use cip
55 * to draw a spline and some tangent lines at appropriate points and then print
56 * the file.
57 */
58 for (N=2; N<sizeof(x)/sizeof(x[0]); N++)
59 if (Bgetfield(Bp, 'd', &x[N], 0)<=0 || Bgetfield(Bp, 'd', &y[N], 0)<=0)
60 break;
61
62 x[0] = x[1] = hpos;
63 y[0] = y[1] = vpos;
64
65 for (i = 1; i < N; i++) {
66 x[i+1] += x[i];
67 y[i+1] += y[i];
68 }
69
70 x[N] = x[N-1];
71 y[N] = y[N-1];
72
73 for (i = ((flag!=1)?0:1); i < ((flag!=1)?N-1:N-2); i++) {
74 endstring();
75 if (pageon())
76 Bprint(Bstdout, "%d %d %d %d %d %d Ds\n", x[i], y[i], x[i+1], y[i+1], x[i+2], y[i+2]);
77 /* if (dobbox == TRUE) { /* could be better */
78 /* cover((double)(x[i] + x[i+1])/2,(double)-(y[i] + y[i+1])/2);
79 /* cover((double)x[i+1], (double)-y[i+1]);
80 /* cover((double)(x[i+1] + x[i+2])/2, (double)-(y[i+1] + y[i+2])/2);
81 /* }
82 */
83 }
84
85 hpos = x[N]; /* where troff expects to be */
86 vpos = y[N];
87 }
88
89 void
draw(Biobufhdr * Bp)90 draw(Biobufhdr *Bp) {
91
92 int r, x1, y1, x2, y2, i;
93 int d1, d2;
94
95 drawflag = TRUE;
96 r = Bgetrune(Bp);
97 switch(r) {
98 case 'l':
99 if (Bgetfield(Bp, 'd', &x1, 0)<=0 || Bgetfield(Bp, 'd', &y1, 0)<=0 || Bgetfield(Bp, 'r', &i, 0)<=0)
100 error(FATAL, "draw line function, destination coordinates not found.\n");
101
102 endstring();
103 if (pageon())
104 Bprint(Bstdout, "%d %d %d %d Dl\n", hpos, vpos, hpos+x1, vpos+y1);
105 hpos += x1;
106 vpos += y1;
107 break;
108 case 'c':
109 if (Bgetfield(Bp, 'd', &d1, 0)<=0)
110 error(FATAL, "draw circle function, diameter coordinates not found.\n");
111
112 endstring();
113 if (pageon())
114 Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d1);
115 hpos += d1;
116 break;
117 case 'e':
118 if (Bgetfield(Bp, 'd', &d1, 0)<=0 || Bgetfield(Bp, 'd', &d2, 0)<=0)
119 error(FATAL, "draw ellipse function, diameter coordinates not found.\n");
120
121 endstring();
122 if (pageon())
123 Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d2);
124 hpos += d1;
125 break;
126 case 'a':
127 if (Bgetfield(Bp, 'd', &x1, 0)<=0 || Bgetfield(Bp, 'd', &y1, 0)<=0 || Bgetfield(Bp, 'd', &x2, 0)<=0 || Bgetfield(Bp, 'd', &y2, 0)<=0)
128 error(FATAL, "draw arc function, coordinates not found.\n");
129
130 endstring();
131 if (pageon())
132 Bprint(Bstdout, "%d %d %d %d %d %d Da\n", hpos, vpos, x1, y1, x2, y2);
133 hpos += x1 + x2;
134 vpos += y1 + y2;
135 break;
136 case 'q':
137 drawspline(Bp, 1);
138 break;
139 case '~':
140 drawspline(Bp, 2);
141 break;
142 default:
143 error(FATAL, "unknown draw function <%c>\n", r);
144 break;
145 }
146 }
147
148 void
beginpath(char * buf,int copy)149 beginpath(char *buf, int copy) {
150
151 /*
152 * Called from devcntrl() whenever an "x X BeginPath" command is read. It's used
153 * to mark the start of a sequence of drawing commands that should be grouped
154 * together and treated as a single path. By default the drawing procedures in
155 * *drawfile treat each drawing command as a separate object, and usually start
156 * with a newpath (just as a precaution) and end with a stroke. The newpath and
157 * stroke isolate individual drawing commands and make it impossible to deal with
158 * composite objects. "x X BeginPath" can be used to mark the start of drawing
159 * commands that should be grouped together and treated as a single object, and
160 * part of what's done here ensures that the PostScript drawing commands defined
161 * in *drawfile skip the newpath and stroke, until after the next "x X DrawPath"
162 * command. At that point the path that's been built up can be manipulated in
163 * various ways (eg. filled and/or stroked with a different line width).
164 *
165 * Color selection is one of the options that's available in parsebuf(),
166 * so if we get here we add *colorfile to the output file before doing
167 * anything important.
168 *
169 */
170 if (inpath == FALSE) {
171 endstring();
172 /* getdraw(); */
173 /* getcolor(); */
174 Bprint(Bstdout, "gsave\n");
175 Bprint(Bstdout, "newpath\n");
176 Bprint(Bstdout, "%d %d m\n", hpos, vpos);
177 Bprint(Bstdout, "/inpath true def\n");
178 if ( copy == TRUE )
179 Bprint(Bstdout, "%s\n", buf);
180 inpath = TRUE;
181 }
182 }
183
184 static void parsebuf(char*);
185
186 void
drawpath(char * buf,int copy)187 drawpath(char *buf, int copy) {
188
189 /*
190 *
191 * Called from devcntrl() whenever an "x X DrawPath" command is read. It marks the
192 * end of the path started by the last "x X BeginPath" command and uses whatever
193 * has been passed along in *buf to manipulate the path (eg. fill and/or stroke
194 * the path). Once that's been done the drawing procedures are restored to their
195 * default behavior in which each drawing command is treated as an isolated path.
196 * The new version (called after "x X DrawPath") has copy set to FALSE, and calls
197 * parsebuf() to figure out what goes in the output file. It's a feeble attempt
198 * to free users and preprocessors (like pic) from having to know PostScript. The
199 * comments in parsebuf() describe what's handled.
200 *
201 * In the early version a path was started with "x X BeginObject" and ended with
202 * "x X EndObject". In both cases *buf was just copied to the output file, and
203 * was expected to be legitimate PostScript that manipulated the current path.
204 * The old escape sequence will be supported for a while (for Ravi), and always
205 * call this routine with copy set to TRUE.
206 *
207 *
208 */
209
210 if ( inpath == TRUE ) {
211 if ( copy == TRUE )
212 Bprint(Bstdout, "%s\n", buf);
213 else
214 parsebuf(buf);
215 Bprint(Bstdout, "grestore\n");
216 Bprint(Bstdout, "/inpath false def\n");
217 /* reset(); */
218 inpath = FALSE;
219 }
220 }
221
222
223 static void
parsebuf(char * buf)224 parsebuf(char *buf)
225 {
226 char *p; /* usually the next token */
227 char *q;
228 int gsavelevel = 0; /* non-zero if we've done a gsave */
229
230 /*
231 * Simple minded attempt at parsing the string that followed an "x X DrawPath"
232 * command. Everything not recognized here is simply ignored - there's absolutely
233 * no error checking and what was originally in buf is clobbered by strtok().
234 * A typical *buf might look like,
235 *
236 * gray .9 fill stroke
237 *
238 * to fill the current path with a gray level of .9 and follow that by stroking the
239 * outline of the path. Since unrecognized tokens are ignored the last example
240 * could also be written as,
241 *
242 * with gray .9 fill then stroke
243 *
244 * The "with" and "then" strings aren't recognized tokens and are simply discarded.
245 * The "stroke", "fill", and "wfill" force out appropriate PostScript code and are
246 * followed by a grestore. In otherwords changes to the grahics state (eg. a gray
247 * level or color) are reset to default values immediately after the stroke, fill,
248 * or wfill tokens. For now "fill" gets invokes PostScript's eofill operator and
249 * "wfill" calls fill (ie. the operator that uses the non-zero winding rule).
250 *
251 * The tokens that cause temporary changes to the graphics state are "gray" (for
252 * setting the gray level), "color" (for selecting a known color from the colordict
253 * dictionary defined in *colorfile), and "line" (for setting the line width). All
254 * three tokens can be extended since strncmp() makes the comparison. For example
255 * the strings "line" and "linewidth" accomplish the same thing. Colors are named
256 * (eg. "red"), but must be appropriately defined in *colorfile. For now all three
257 * tokens must be followed immediately by their single argument. The gray level
258 * (ie. the argument that follows "gray") should be a number between 0 and 1, with
259 * 0 for black and 1 for white.
260 *
261 * To pass straight PostScript through enclose the appropriate commands in double
262 * quotes. Straight PostScript is only bracketed by the outermost gsave/grestore
263 * pair (ie. the one from the initial "x X BeginPath") although that's probably
264 * a mistake. Suspect I may have to change the double quote delimiters.
265 */
266 for(p = buf; p != nil; p = q) {
267 if( q = strchr(p, ' ') )
268 *q++ = '\0';
269
270 if ( gsavelevel == 0 ) {
271 Bprint(Bstdout, "gsave\n");
272 gsavelevel++;
273 }
274 if ( strcmp(p, "stroke") == 0 ) {
275 Bprint(Bstdout, "closepath stroke\ngrestore\n");
276 gsavelevel--;
277 } else if ( strcmp(p, "openstroke") == 0 ) {
278 Bprint(Bstdout, "stroke\ngrestore\n");
279 gsavelevel--;
280 } else if ( strcmp(p, "fill") == 0 ) {
281 Bprint(Bstdout, "eofill\ngrestore\n");
282 gsavelevel--;
283 } else if ( strcmp(p, "wfill") == 0 ) {
284 Bprint(Bstdout, "fill\ngrestore\n");
285 gsavelevel--;
286 } else if ( strcmp(p, "sfill") == 0 ) {
287 Bprint(Bstdout, "eofill\ngrestore\ngsave\nstroke\ngrestore\n");
288 gsavelevel--;
289 } else if ( strncmp(p, "gray", strlen("gray")) == 0 ) {
290 if( q ) {
291 p = q;
292 if ( q = strchr(p, ' ') )
293 *q++ = '\0';
294 Bprint(Bstdout, "%s setgray\n", p);
295 }
296 } else if ( strncmp(p, "color", strlen("color")) == 0 ) {
297 if( q ) {
298 p = q;
299 if ( q = strchr(p, ' ') )
300 *q++ = '\0';
301 Bprint(Bstdout, "/%s setcolor\n", p);
302 }
303 } else if ( strncmp(p, "line", strlen("line")) == 0 ) {
304 if( q ) {
305 p = q;
306 if ( q = strchr(p, ' ') )
307 *q++ = '\0';
308 Bprint(Bstdout, "%s resolution mul 2 div setlinewidth\n", p);
309 }
310 } else if ( strncmp(p, "reverse", strlen("reverse")) == 0 )
311 Bprint(Bstdout, "reversepath\n");
312 else if ( *p == '"' ) {
313 for ( ; gsavelevel > 0; gsavelevel-- )
314 Bprint(Bstdout, "grestore\n");
315 if ( q != nil )
316 *--q = ' ';
317 if ( (q = strchr(p, '"')) != nil ) {
318 *q++ = '\0';
319 Bprint(Bstdout, "%s\n", p);
320 }
321 }
322 }
323
324 for ( ; gsavelevel > 0; gsavelevel-- )
325 Bprint(Bstdout, "grestore\n");
326
327 }
328