dpost.ps: increase linewidth to match groff
[troff.git] / tr2ps / draw.c
blobe01526debb9455c63087a0205617a4c64d38f7ae
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "ustr.h"
5 #include "common.h"
6 #include "tr2ps.h"
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 + ---------
24 * 2 2
27 * The coefficients in the Bezier cubic are,
30 * A = 0
31 * B = (x2 - 2 * x1 + x0) / 2
32 * C = x1 - x0
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
45 * x2' = (x1 + x2) / 2
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
54 * the file.
57 /* flag!=1 connect end points */
58 void drawspline(struct ustr *ustr, int flag)
60 int x[100], y[100];
61 int i, N;
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)
65 break;
67 x[0] = x[1] = hpos;
68 y[0] = y[1] = vpos;
70 for (i = 1; i < N; i++) {
71 x[i + 1] += x[i];
72 y[i + 1] += y[i];
75 x[N] = x[N - 1];
76 y[N] = y[N - 1];
78 for (i = ((flag != 1) ? 0 : 1); i < ((flag != 1) ? N - 1 : N - 2); i++) {
79 endstring();
80 if (pageon())
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 */
86 vpos = y[N];
89 void draw(struct ustr *ustr)
92 int x1, y1, x2, y2, i;
93 int d1, d2;
94 uc_t r;
96 drawflag = 1;
97 ustr_uc(ustr, &r);
98 switch (r) {
99 case 'l':
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");
103 endstring();
104 if (pageon())
105 fprintf(fout, "%d %d %d %d Dl\n",
106 hpos, vpos, hpos+x1, vpos+y1);
107 hpos += x1;
108 vpos += y1;
109 break;
110 case 'c':
111 if (ustr_int(ustr, &d1) <= 0)
112 error(FATAL, "draw circle function, diameter coordinates not found.\n");
113 endstring();
114 if (pageon())
115 fprintf(fout, "%d %d %d %d De\n", hpos, vpos, d1, d1);
116 hpos += d1;
117 break;
118 case 'e':
119 if (ustr_int(ustr, &d1) <= 0 || ustr_int(ustr, &d2) <= 0)
120 error(FATAL, "draw ellipse function, diameter coordinates not found.\n");
121 endstring();
122 if (pageon())
123 fprintf(fout, "%d %d %d %d De\n", hpos, vpos, d1, d2);
124 hpos += d1;
125 break;
126 case 'a':
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");
130 endstring();
131 if (pageon())
132 fprintf(fout, "%d %d %d %d %d %d Da\n",
133 hpos, vpos, x1, y1, x2, y2);
134 hpos += x1 + x2;
135 vpos += y1 + y2;
136 break;
137 case 'q':
138 drawspline(ustr, 1);
139 break;
140 case '~':
141 drawspline(ustr, 2);
142 break;
143 default:
144 error(FATAL, "unknown draw function <%c>\n", r);
145 break;
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) {
172 endstring();
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");
177 if (copy)
178 fprintf(fout, "%s\n", buf);
179 inpath = 1;
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)
206 if (inpath) {
207 if (copy)
208 fprintf(fout, "%s\n", buf);
209 else
210 parsebuf(buf);
211 fprintf(fout, "grestore\n");
212 fprintf(fout, "/inpath false def\n");
213 inpath = FALSE;
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)
257 char *q;
258 int gsavelevel = 0; /* non-zero if we've done a gsave */
260 for (; p != NULL; p = q ) {
261 if (q = strchr(p, ' '))
262 *q++ = '\0';
264 if (gsavelevel == 0) {
265 fprintf(fout, "gsave\n");
266 gsavelevel++;
268 if (strcmp(p, "stroke") == 0) {
269 fprintf(fout, "closepath stroke\ngrestore\n");
270 gsavelevel--;
271 } else if (strcmp(p, "openstroke") == 0) {
272 fprintf(fout, "stroke\ngrestore\n");
273 gsavelevel--;
274 } else if (strcmp(p, "fill") == 0) {
275 fprintf(fout, "eofill\ngrestore\n");
276 gsavelevel--;
277 } else if (strcmp(p, "wfill") == 0) {
278 fprintf(fout, "fill\ngrestore\n");
279 gsavelevel--;
280 } else if (strcmp(p, "sfill") == 0) {
281 fprintf(fout, "eofill\ngrestore\ngsave\nstroke\ngrestore\n");
282 gsavelevel--;
283 } else if (strncmp(p, "gray", strlen("gray")) == 0) {
284 if (q) {
285 p = q;
286 if (q = strchr(p, ' '))
287 *q++ = '\0';
288 fprintf(fout, "%s setgray\n", p);
290 } else if (strncmp(p, "color", strlen("color")) == 0) {
291 if (q) {
292 p = q;
293 if (q = strchr(p, ' '))
294 *q++ = '\0';
295 fprintf(fout, "/%s setcolor\n", p);
297 } else if (strncmp(p, "line", strlen("line")) == 0) {
298 if (q) {
299 p = q;
300 if (q = strchr(p, ' '))
301 *q++ = '\0';
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");
309 if (q != NULL)
310 *--q = ' ';
311 if ((q = strchr(p, '"')) != NULL) {
312 *q++ = '\0';
313 fprintf(fout, "%s\n", p);
318 for (; gsavelevel > 0; gsavelevel--)
319 fprintf(fout, "grestore\n");