8 BOOLEAN drawflag
= FALSE
;
9 BOOLEAN inpath
= FALSE
; /* TRUE if we're putting pieces together */
12 * Spline drawing routine for Postscript printers. The complicated stuff is
13 * handled by procedure Ds, which should be defined in the library file. I've
14 * seen wrong implementations of troff's spline drawing, so fo the record I'll
15 * write down the parametric equations and the necessary conversions to Bezier
16 * cubic splines (as used in Postscript).
19 * Parametric equation (x coordinate only):
22 * (x2 - 2 * x1 + x0) 2 (x0 + x1)
23 * x = ------------------ * t + (x1 - x0) * t + ---------
27 * The coefficients in the Bezier cubic are,
31 * B = (x2 - 2 * x1 + x0) / 2
35 * while the current point is,
37 * current-point = (x0 + x1) / 2
39 * Using the relationships given in the Postscript manual (page 121) it's easy to
40 * see that the control points are given by,
43 * x0' = (x0 + 5 * x1) / 6
44 * x1' = (x2 + 5 * x1) / 6
48 * where the primed variables are the ones used by curveto. The calculations
49 * shown above are done in procedure Ds using the coordinates set up in both
50 * the x[] and y[] arrays.
52 * A simple test of whether your spline drawing is correct would be to use cip
53 * to draw a spline and some tangent lines at appropriate points and then print
57 /* flag!=1 connect end points */
58 void drawspline(struct ustr
*ustr
, int flag
)
63 for (N
= 2; N
< sizeof(x
) / sizeof(x
[0]); N
++)
64 if (ustr_int(ustr
, &x
[N
]) <= 0 || ustr_int(ustr
, &y
[N
]) <= 0)
70 for (i
= 1; i
< N
; i
++) {
78 for (i
= ((flag
!= 1) ? 0 : 1); i
< ((flag
!= 1) ? N
- 1 : N
- 2); i
++) {
81 fprintf(fout
, "%d %d %d %d %d %d Ds\n",
82 x
[i
], y
[i
], x
[i
+1], y
[i
+1], x
[i
+2], y
[i
+2]);
85 hpos
= x
[N
]; /* where troff expects to be */
89 void draw(struct ustr
*ustr
)
92 int x1
, y1
, x2
, y2
, i
;
100 if (ustr_int(ustr
, &x1
) <= 0 || ustr_int(ustr
, &y1
) <= 0 ||
101 ustr_uc_nospace(ustr
, &i
) <= 0)
102 error(FATAL
, "draw line function, destination coordinates not found.\n");
105 fprintf(fout
, "%d %d %d %d Dl\n",
106 hpos
, vpos
, hpos
+x1
, vpos
+y1
);
111 if (ustr_int(ustr
, &d1
) <= 0)
112 error(FATAL
, "draw circle function, diameter coordinates not found.\n");
115 fprintf(fout
, "%d %d %d %d De\n", hpos
, vpos
, d1
, d1
);
119 if (ustr_int(ustr
, &d1
) <= 0 || ustr_int(ustr
, &d2
) <= 0)
120 error(FATAL
, "draw ellipse function, diameter coordinates not found.\n");
123 fprintf(fout
, "%d %d %d %d De\n", hpos
, vpos
, d1
, d2
);
127 if (ustr_int(ustr
, &x1
) <= 0 || ustr_int(ustr
, &y1
) <= 0 ||
128 ustr_int(ustr
, &x2
) <= 0 || ustr_int(ustr
, &y2
) <= 0)
129 error(FATAL
, "draw arc function, coordinates not found.\n");
132 fprintf(fout
, "%d %d %d %d %d %d Da\n",
133 hpos
, vpos
, x1
, y1
, x2
, y2
);
144 error(FATAL
, "unknown draw function <%c>\n", r
);
150 * Called from devcntrl() whenever an "x X BeginPath" command is read. It's used
151 * to mark the start of a sequence of drawing commands that should be grouped
152 * together and treated as a single path. By default the drawing procedures in
153 * *drawfile treat each drawing command as a separate object, and usually start
154 * with a newpath (just as a precaution) and end with a stroke. The newpath and
155 * stroke isolate individual drawing commands and make it impossible to deal with
156 * composite objects. "x X BeginPath" can be used to mark the start of drawing
157 * commands that should be grouped together and treated as a single object, and
158 * part of what's done here ensures that the PostScript drawing commands defined
159 * in *drawfile skip the newpath and stroke, until after the next "x X DrawPath"
160 * command. At that point the path that's been built up can be manipulated in
161 * various ways (eg. filled and/or stroked with a different line width).
163 * Color selection is one of the options that's available in parsebuf(),
164 * so if we get here we add *colorfile to the output file before doing
165 * anything important.
168 void beginpath(char *buf
, int copy
)
171 if (inpath
== FALSE
) {
173 fprintf(fout
, "gsave\n");
174 fprintf(fout
, "newpath\n");
175 fprintf(fout
, "%d %d m\n", hpos
, vpos
);
176 fprintf(fout
, "/inpath true def\n");
178 fprintf(fout
, "%s\n", buf
);
183 static void parsebuf(char*);
186 * Called from devcntrl() whenever an "x X DrawPath" command is read. It marks the
187 * end of the path started by the last "x X BeginPath" command and uses whatever
188 * has been passed along in *buf to manipulate the path (eg. fill and/or stroke
189 * the path). Once that's been done the drawing procedures are restored to their
190 * default behavior in which each drawing command is treated as an isolated path.
191 * The new version (called after "x X DrawPath") has copy set to FALSE, and calls
192 * parsebuf() to figure out what goes in the output file. It's a feeble attempt
193 * to free users and preprocessors (like pic) from having to know PostScript. The
194 * comments in parsebuf() describe what's handled.
196 * In the early version a path was started with "x X BeginObject" and ended with
197 * "x X EndObject". In both cases *buf was just copied to the output file, and
198 * was expected to be legitimate PostScript that manipulated the current path.
199 * The old escape sequence will be supported for a while (for Ravi), and always
200 * call this routine with copy set to TRUE.
204 void drawpath(char *buf
, int copy
)
208 fprintf(fout
, "%s\n", buf
);
211 fprintf(fout
, "grestore\n");
212 fprintf(fout
, "/inpath false def\n");
219 * Simple minded attempt at parsing the string that followed an "x X DrawPath"
220 * command. Everything not recognized here is simply ignored - there's absolutely
221 * no error checking and what was originally in buf is clobbered by strtok().
222 * A typical *buf might look like,
224 * gray .9 fill stroke
226 * to fill the current path with a gray level of .9 and follow that by stroking the
227 * outline of the path. Since unrecognized tokens are ignored the last example
228 * could also be written as,
230 * with gray .9 fill then stroke
232 * The "with" and "then" strings aren't recognized tokens and are simply discarded.
233 * The "stroke", "fill", and "wfill" force out appropriate PostScript code and are
234 * followed by a grestore. In otherwords changes to the grahics state (eg. a gray
235 * level or color) are reset to default values immediately after the stroke, fill,
236 * or wfill tokens. For now "fill" gets invokes PostScript's eofill operator and
237 * "wfill" calls fill (ie. the operator that uses the non-zero winding rule).
239 * The tokens that cause temporary changes to the graphics state are "gray" (for
240 * setting the gray level), "color" (for selecting a known color from the colordict
241 * dictionary defined in *colorfile), and "line" (for setting the line width). All
242 * three tokens can be extended since strncmp() makes the comparison. For example
243 * the strings "line" and "linewidth" accomplish the same thing. Colors are named
244 * (eg. "red"), but must be appropriately defined in *colorfile. For now all three
245 * tokens must be followed immediately by their single argument. The gray level
246 * (ie. the argument that follows "gray") should be a number between 0 and 1, with
247 * 0 for black and 1 for white.
249 * To pass straight PostScript through enclose the appropriate commands in double
250 * quotes. Straight PostScript is only bracketed by the outermost gsave/grestore
251 * pair (ie. the one from the initial "x X BeginPath") although that's probably
252 * a mistake. Suspect I may have to change the double quote delimiters.
255 static void parsebuf(char *p
)
258 int gsavelevel
= 0; /* non-zero if we've done a gsave */
260 for (; p
!= NULL
; p
= q
) {
261 if (q
= strchr(p
, ' '))
264 if (gsavelevel
== 0) {
265 fprintf(fout
, "gsave\n");
268 if (strcmp(p
, "stroke") == 0) {
269 fprintf(fout
, "closepath stroke\ngrestore\n");
271 } else if (strcmp(p
, "openstroke") == 0) {
272 fprintf(fout
, "stroke\ngrestore\n");
274 } else if (strcmp(p
, "fill") == 0) {
275 fprintf(fout
, "eofill\ngrestore\n");
277 } else if (strcmp(p
, "wfill") == 0) {
278 fprintf(fout
, "fill\ngrestore\n");
280 } else if (strcmp(p
, "sfill") == 0) {
281 fprintf(fout
, "eofill\ngrestore\ngsave\nstroke\ngrestore\n");
283 } else if (strncmp(p
, "gray", strlen("gray")) == 0) {
286 if (q
= strchr(p
, ' '))
288 fprintf(fout
, "%s setgray\n", p
);
290 } else if (strncmp(p
, "color", strlen("color")) == 0) {
293 if (q
= strchr(p
, ' '))
295 fprintf(fout
, "/%s setcolor\n", p
);
297 } else if (strncmp(p
, "line", strlen("line")) == 0) {
300 if (q
= strchr(p
, ' '))
302 fprintf(fout
, "%s resolution mul 2 div setlinewidth\n", p
);
304 } else if (strncmp(p
, "reverse", strlen("reverse")) == 0)
305 fprintf(fout
, "reversepath\n");
306 else if (*p
== '"') {
307 for (; gsavelevel
> 0; gsavelevel
--)
308 fprintf(fout
, "grestore\n");
311 if ((q
= strchr(p
, '"')) != NULL
) {
313 fprintf(fout
, "%s\n", p
);
318 for (; gsavelevel
> 0; gsavelevel
--)
319 fprintf(fout
, "grestore\n");