5 #include "../common/common.h"
8 BOOLEAN drawflag = FALSE;
9 BOOLEAN inpath = FALSE; /* TRUE if we're putting pieces together */
12 cover(double x, double y) {
16 drawspline(Biobuf *Bp, int flag) { /* flag!=1 connect end points */
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).
28 * Parametric equation (x coordinate only):
31 * (x2 - 2 * x1 + x0) 2 (x0 + x1)
32 * x = ------------------ * t + (x1 - x0) * t + ---------
36 * The coefficients in the Bezier cubic are,
40 * B = (x2 - 2 * x1 + x0) / 2
44 * while the current point is,
46 * current-point = (x0 + x1) / 2
48 * Using the relationships given in the Postscript manual (page 121) it's easy to
49 * see that the control points are given by,
52 * x0' = (x0 + 5 * x1) / 6
53 * x1' = (x2 + 5 * x1) / 6
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.
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
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)
74 for (i = 1; i < N; i++) {
82 for (i = ((flag!=1)?0:1); i < ((flag!=1)?N-1:N-2); i++) {
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);
94 hpos = x[N]; /* where troff expects to be */
101 int r, x1, y1, x2, y2, i;
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");
115 Bprint(Bstdout, "%d %d %d %d Dl\n", hpos, vpos, hpos+x1, vpos+y1);
120 if (Bgetfield(Bp, 'd', &d1, 0)<=0) {
121 error(FATAL, "draw circle function, diameter coordinates not found.\n");
127 Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d1);
131 if (Bgetfield(Bp, 'd', &d1, 0)<=0 || Bgetfield(Bp, 'd', &d2, 0)<=0) {
132 error(FATAL, "draw ellipse function, diameter coordinates not found.\n");
138 Bprint(Bstdout, "%d %d %d %d De\n", hpos, vpos, d1, d2);
142 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) {
143 error(FATAL, "draw arc function, coordinates not found.\n");
149 Bprint(Bstdout, "%d %d %d %d %d %d Da\n", hpos, vpos, x1, y1, x2, y2);
160 error(FATAL, "unknown draw function <%c>\n", r);
166 beginpath(char *buf, int copy) {
169 * Called from devcntrl() whenever an "x X BeginPath" command is read. It's used
170 * to mark the start of a sequence of drawing commands that should be grouped
171 * together and treated as a single path. By default the drawing procedures in
172 * *drawfile treat each drawing command as a separate object, and usually start
173 * with a newpath (just as a precaution) and end with a stroke. The newpath and
174 * stroke isolate individual drawing commands and make it impossible to deal with
175 * composite objects. "x X BeginPath" can be used to mark the start of drawing
176 * commands that should be grouped together and treated as a single object, and
177 * part of what's done here ensures that the PostScript drawing commands defined
178 * in *drawfile skip the newpath and stroke, until after the next "x X DrawPath"
179 * command. At that point the path that's been built up can be manipulated in
180 * various ways (eg. filled and/or stroked with a different line width).
182 * Color selection is one of the options that's available in parsebuf(),
183 * so if we get here we add *colorfile to the output file before doing
184 * anything important.
187 if (inpath == FALSE) {
191 Bprint(Bstdout, "gsave\n");
192 Bprint(Bstdout, "newpath\n");
193 Bprint(Bstdout, "%d %d m\n", hpos, vpos);
194 Bprint(Bstdout, "/inpath true def\n");
196 Bprint(Bstdout, "%s\n", buf);
201 static void parsebuf(char*);
204 drawpath(char *buf, int copy) {
208 * Called from devcntrl() whenever an "x X DrawPath" command is read. It marks the
209 * end of the path started by the last "x X BeginPath" command and uses whatever
210 * has been passed along in *buf to manipulate the path (eg. fill and/or stroke
211 * the path). Once that's been done the drawing procedures are restored to their
212 * default behavior in which each drawing command is treated as an isolated path.
213 * The new version (called after "x X DrawPath") has copy set to FALSE, and calls
214 * parsebuf() to figure out what goes in the output file. It's a feeble attempt
215 * to free users and preprocessors (like pic) from having to know PostScript. The
216 * comments in parsebuf() describe what's handled.
218 * In the early version a path was started with "x X BeginObject" and ended with
219 * "x X EndObject". In both cases *buf was just copied to the output file, and
220 * was expected to be legitimate PostScript that manipulated the current path.
221 * The old escape sequence will be supported for a while (for Ravi), and always
222 * call this routine with copy set to TRUE.
227 if ( inpath == TRUE ) {
229 Bprint(Bstdout, "%s\n", buf);
232 Bprint(Bstdout, "grestore\n");
233 Bprint(Bstdout, "/inpath false def\n");
240 /*****************************************************************************/
245 char *p = (char*)0; /* usually the next token */
247 int gsavelevel = 0; /* non-zero if we've done a gsave */
251 * Simple minded attempt at parsing the string that followed an "x X DrawPath"
252 * command. Everything not recognized here is simply ignored - there's absolutely
253 * no error checking and what was originally in buf is clobbered by strtok().
254 * A typical *buf might look like,
256 * gray .9 fill stroke
258 * to fill the current path with a gray level of .9 and follow that by stroking the
259 * outline of the path. Since unrecognized tokens are ignored the last example
260 * could also be written as,
262 * with gray .9 fill then stroke
264 * The "with" and "then" strings aren't recognized tokens and are simply discarded.
265 * The "stroke", "fill", and "wfill" force out appropriate PostScript code and are
266 * followed by a grestore. In otherwords changes to the grahics state (eg. a gray
267 * level or color) are reset to default values immediately after the stroke, fill,
268 * or wfill tokens. For now "fill" gets invokes PostScript's eofill operator and
269 * "wfill" calls fill (ie. the operator that uses the non-zero winding rule).
271 * The tokens that cause temporary changes to the graphics state are "gray" (for
272 * setting the gray level), "color" (for selecting a known color from the colordict
273 * dictionary defined in *colorfile), and "line" (for setting the line width). All
274 * three tokens can be extended since strncmp() makes the comparison. For example
275 * the strings "line" and "linewidth" accomplish the same thing. Colors are named
276 * (eg. "red"), but must be appropriately defined in *colorfile. For now all three
277 * tokens must be followed immediately by their single argument. The gray level
278 * (ie. the argument that follows "gray") should be a number between 0 and 1, with
279 * 0 for black and 1 for white.
281 * To pass straight PostScript through enclose the appropriate commands in double
282 * quotes. Straight PostScript is only bracketed by the outermost gsave/grestore
283 * pair (ie. the one from the initial "x X BeginPath") although that's probably
284 * a mistake. Suspect I may have to change the double quote delimiters.
288 for( ; p != nil ; p = q ) {
289 if( q = strchr(p, ' ') ) {
293 if ( gsavelevel == 0 ) {
294 Bprint(Bstdout, "gsave\n");
297 if ( strcmp(p, "stroke") == 0 ) {
298 Bprint(Bstdout, "closepath stroke\ngrestore\n");
300 } else if ( strcmp(p, "openstroke") == 0 ) {
301 Bprint(Bstdout, "stroke\ngrestore\n");
303 } else if ( strcmp(p, "fill") == 0 ) {
304 Bprint(Bstdout, "eofill\ngrestore\n");
306 } else if ( strcmp(p, "wfill") == 0 ) {
307 Bprint(Bstdout, "fill\ngrestore\n");
309 } else if ( strcmp(p, "sfill") == 0 ) {
310 Bprint(Bstdout, "eofill\ngrestore\ngsave\nstroke\ngrestore\n");
312 } else if ( strncmp(p, "gray", strlen("gray")) == 0 ) {
315 if ( q = strchr(p, ' ') )
317 Bprint(Bstdout, "%s setgray\n", p);
319 } else if ( strncmp(p, "color", strlen("color")) == 0 ) {
322 if ( q = strchr(p, ' ') )
324 Bprint(Bstdout, "/%s setcolor\n", p);
326 } else if ( strncmp(p, "line", strlen("line")) == 0 ) {
329 if ( q = strchr(p, ' ') )
331 Bprint(Bstdout, "%s resolution mul 2 div setlinewidth\n", p);
333 } else if ( strncmp(p, "reverse", strlen("reverse")) == 0 )
334 Bprint(Bstdout, "reversepath\n");
335 else if ( *p == '"' ) {
336 for ( ; gsavelevel > 0; gsavelevel-- )
337 Bprint(Bstdout, "grestore\n");
340 if ( (q = strchr(p, '"')) != nil ) {
342 Bprint(Bstdout, "%s\n", p);
347 for ( ; gsavelevel > 0; gsavelevel-- )
348 Bprint(Bstdout, "grestore\n");