Blob


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