1 3ebbd193 2017-06-19 rsc #include <stdio.h>
2 3ebbd193 2017-06-19 rsc #include <math.h>
3 3ebbd193 2017-06-19 rsc #include <string.h>
4 3ebbd193 2017-06-19 rsc #include "pic.h"
5 3ebbd193 2017-06-19 rsc extern int dbg;
7 3ebbd193 2017-06-19 rsc #define abs(n) (n >= 0 ? n : -(n))
8 3ebbd193 2017-06-19 rsc #define max(x,y) ((x)>(y) ? (x) : (y))
10 3ebbd193 2017-06-19 rsc char *textshift = "\\v'.2m'"; /* move text this far down */
12 3ebbd193 2017-06-19 rsc /* scaling stuff defined by s command as X0,Y0 to X1,Y1 */
13 3ebbd193 2017-06-19 rsc /* output dimensions set by -l,-w options to 0,0 to hmax, vmax */
14 3ebbd193 2017-06-19 rsc /* default output is 6x6 inches */
17 3ebbd193 2017-06-19 rsc double xscale;
18 3ebbd193 2017-06-19 rsc double yscale;
20 3ebbd193 2017-06-19 rsc double hpos = 0; /* current horizontal position in output coordinate system */
21 3ebbd193 2017-06-19 rsc double vpos = 0; /* current vertical position; 0 is top of page */
23 3ebbd193 2017-06-19 rsc double htrue = 0; /* where we really are */
24 3ebbd193 2017-06-19 rsc double vtrue = 0;
26 3ebbd193 2017-06-19 rsc double X0, Y0; /* left bottom of input */
27 3ebbd193 2017-06-19 rsc double X1, Y1; /* right top of input */
29 3ebbd193 2017-06-19 rsc double hmax; /* right end of output */
30 3ebbd193 2017-06-19 rsc double vmax; /* top of output (down is positive) */
32 3ebbd193 2017-06-19 rsc extern double deltx;
33 3ebbd193 2017-06-19 rsc extern double delty;
34 3ebbd193 2017-06-19 rsc extern double xmin, ymin, xmax, ymax;
36 3ebbd193 2017-06-19 rsc double xconv(double), yconv(double), xsc(double), ysc(double);
37 3ebbd193 2017-06-19 rsc void space(double, double, double, double);
38 3ebbd193 2017-06-19 rsc void hgoto(double), vgoto(double), hmot(double), vmot(double);
39 3ebbd193 2017-06-19 rsc void move(double, double), movehv(double, double);
41 3ebbd193 2017-06-19 rsc char svgfill[40] = "transparent";
42 3ebbd193 2017-06-19 rsc char svgstroke[40] = "black";
44 3ebbd193 2017-06-19 rsc void openpl(char *s) /* initialize device; s is residue of .PS invocation line */
46 3ebbd193 2017-06-19 rsc double maxw, maxh, ratio = 1;
47 3ebbd193 2017-06-19 rsc double odeltx = deltx, odelty = delty;
49 3ebbd193 2017-06-19 rsc hpos = vpos = 0;
50 3ebbd193 2017-06-19 rsc maxw = getfval("maxpswid");
51 3ebbd193 2017-06-19 rsc maxh = getfval("maxpsht");
52 3ebbd193 2017-06-19 rsc if (deltx > maxw) { /* shrink horizontal */
53 3ebbd193 2017-06-19 rsc ratio = maxw / deltx;
54 3ebbd193 2017-06-19 rsc deltx *= ratio;
55 3ebbd193 2017-06-19 rsc delty *= ratio;
57 3ebbd193 2017-06-19 rsc if (delty > maxh) { /* shrink vertical */
58 3ebbd193 2017-06-19 rsc ratio = maxh / delty;
59 3ebbd193 2017-06-19 rsc deltx *= ratio;
60 3ebbd193 2017-06-19 rsc delty *= ratio;
62 3ebbd193 2017-06-19 rsc if (ratio != 1) {
63 3ebbd193 2017-06-19 rsc fprintf(stderr, "pic: %g X %g picture shrunk to", odeltx, odelty);
64 3ebbd193 2017-06-19 rsc fprintf(stderr, " %g X %g\n", deltx, delty);
66 3ebbd193 2017-06-19 rsc space(xmin, ymin, xmax, ymax);
68 3ebbd193 2017-06-19 rsc printf("<svg height=\"%.3f\" width=\"%.3f\"\n", yconv(ymin)+10, xconv(xmax)+10);
69 3ebbd193 2017-06-19 rsc printf(" xmlns=\"http://www.w3.org/2000/svg\">\n");
70 3ebbd193 2017-06-19 rsc printf("<g transform=\"translate(5 5)\">\n");
73 3ebbd193 2017-06-19 rsc printf("... %g %g %g %g\n", xmin, ymin, xmax, ymax);
74 3ebbd193 2017-06-19 rsc printf("... %.3fi %.3fi %.3fi %.3fi\n",
75 3ebbd193 2017-06-19 rsc xconv(xmin), yconv(ymin), xconv(xmax), yconv(ymax));
76 3ebbd193 2017-06-19 rsc printf(".nr 00 \\n(.u\n");
77 3ebbd193 2017-06-19 rsc printf(".nf\n");
78 3ebbd193 2017-06-19 rsc printf(".PS %.3fi %.3fi %s", yconv(ymin), xconv(xmax), s);
82 3ebbd193 2017-06-19 rsc void space(double x0, double y0, double x1, double y1) /* set limits of page */
88 3ebbd193 2017-06-19 rsc xscale = deltx == 0.0 ? 1.0 : deltx / (X1-X0);
89 3ebbd193 2017-06-19 rsc yscale = delty == 0.0 ? 1.0 : delty / (Y1-Y0);
91 3ebbd193 2017-06-19 rsc xscale *= 144;
92 3ebbd193 2017-06-19 rsc yscale *= 144;
95 3ebbd193 2017-06-19 rsc double xconv(double x) /* convert x from external to internal form */
97 3ebbd193 2017-06-19 rsc return (x-X0) * xscale;
100 3ebbd193 2017-06-19 rsc double xsc(double x) /* convert x from external to internal form, scaling only */
103 3ebbd193 2017-06-19 rsc return (x) * xscale;
106 3ebbd193 2017-06-19 rsc double yconv(double y) /* convert y from external to internal form */
108 3ebbd193 2017-06-19 rsc return (Y1-y) * yscale;
111 3ebbd193 2017-06-19 rsc double ysc(double y) /* convert y from external to internal form, scaling only */
113 3ebbd193 2017-06-19 rsc return (y) * yscale;
116 3ebbd193 2017-06-19 rsc void closepl(char *PEline) /* clean up after finished */
118 3ebbd193 2017-06-19 rsc printf("</g>\n");
119 3ebbd193 2017-06-19 rsc printf("</svg>\n");
122 3ebbd193 2017-06-19 rsc void move(double x, double y) /* go to position x, y in external coords */
124 3ebbd193 2017-06-19 rsc hgoto(xconv(x));
125 3ebbd193 2017-06-19 rsc vgoto(yconv(y));
128 3ebbd193 2017-06-19 rsc void movehv(double h, double v) /* go to internal position h, v */
134 3ebbd193 2017-06-19 rsc void hmot(double n) /* generate n units of horizontal motion */
139 3ebbd193 2017-06-19 rsc void vmot(double n) /* generate n units of vertical motion */
144 3ebbd193 2017-06-19 rsc void hgoto(double n)
149 3ebbd193 2017-06-19 rsc void vgoto(double n)
154 3ebbd193 2017-06-19 rsc void hvflush(void) /* get to proper point for output */
157 3ebbd193 2017-06-19 rsc if (fabs(hpos-htrue) >= 0.0005) {
158 3ebbd193 2017-06-19 rsc printf("\\h'%.3fi'", hpos - htrue);
159 3ebbd193 2017-06-19 rsc htrue = hpos;
161 3ebbd193 2017-06-19 rsc if (fabs(vpos-vtrue) >= 0.0005) {
162 3ebbd193 2017-06-19 rsc printf("\\v'%.3fi'", vpos - vtrue);
163 3ebbd193 2017-06-19 rsc vtrue = vpos;
168 3ebbd193 2017-06-19 rsc void printlf(int n, char *f)
172 3ebbd193 2017-06-19 rsc void troff(char *s) /* output troff right here */
174 3ebbd193 2017-06-19 rsc printf("%s\n", s);
177 3ebbd193 2017-06-19 rsc void label(char *s, int t, int nh) /* text s of type t nh half-lines up */
179 3ebbd193 2017-06-19 rsc char *anchor;
184 3ebbd193 2017-06-19 rsc if (t & ABOVE)
186 3ebbd193 2017-06-19 rsc else if (t & BELOW)
188 3ebbd193 2017-06-19 rsc t &= ~(ABOVE|BELOW);
190 3ebbd193 2017-06-19 rsc if (t & LJUST) {
192 3ebbd193 2017-06-19 rsc } else if (t & RJUST) {
193 3ebbd193 2017-06-19 rsc anchor = "end";
194 3ebbd193 2017-06-19 rsc } else { /* CENTER */
195 3ebbd193 2017-06-19 rsc anchor = "middle";
197 3ebbd193 2017-06-19 rsc printf("<text x=\"%.3f\" y=\"%.3f\"", hpos, vpos-(double)(nh-0.4)*(12.0/72)/2*144);
199 3ebbd193 2017-06-19 rsc printf(" text-anchor=\"%s\"", anchor);
200 3ebbd193 2017-06-19 rsc printf(">%s</text>\n", s);
203 3ebbd193 2017-06-19 rsc void line(double x0, double y0, double x1, double y1, int attr, double ddval) /* draw line from x0,y0 to x1,y1 */
205 3ebbd193 2017-06-19 rsc printf("<path d=\"M %.3f %.3f L %.3f %.3f\" fill=\"transparent\" stroke=\"black\"", xconv(x0), yconv(y0), xconv(x1), yconv(y1));
206 3ebbd193 2017-06-19 rsc if(attr & DASHBIT)
207 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"%.3f, %.3f\"", xsc(ddval), xsc(ddval));
208 3ebbd193 2017-06-19 rsc else if(attr & DOTBIT)
209 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"1, %.3f\"", xsc(ddval));
210 3ebbd193 2017-06-19 rsc printf("/>\n");
213 3ebbd193 2017-06-19 rsc void arrow(double x0, double y0, double x1, double y1, double w, double h,
214 3ebbd193 2017-06-19 rsc double ang, int nhead) /* draw arrow (without shaft) */
216 3ebbd193 2017-06-19 rsc double alpha, rot, drot, hyp;
217 3ebbd193 2017-06-19 rsc double dx, dy;
220 3ebbd193 2017-06-19 rsc rot = atan2(w / 2, h);
221 3ebbd193 2017-06-19 rsc hyp = sqrt(w/2 * w/2 + h * h);
222 3ebbd193 2017-06-19 rsc alpha = atan2(y1-y0, x1-x0) + ang;
223 3ebbd193 2017-06-19 rsc if (nhead < 2)
225 3ebbd193 2017-06-19 rsc dprintf("rot=%g, hyp=%g, alpha=%g\n", rot, hyp, alpha);
226 3ebbd193 2017-06-19 rsc printf("<path d=\"");
227 3ebbd193 2017-06-19 rsc for (i = 1; i >= 0; i--) {
228 3ebbd193 2017-06-19 rsc drot = 2 * rot / (double) (2-1) * (double) i;
229 3ebbd193 2017-06-19 rsc dx = hyp * cos(alpha + PI - rot + drot);
230 3ebbd193 2017-06-19 rsc dy = hyp * sin(alpha + PI - rot + drot);
231 3ebbd193 2017-06-19 rsc dprintf("dx,dy = %g,%g\n", dx, dy);
233 3ebbd193 2017-06-19 rsc printf("M %.3f %.3f L %.3f %.3f", xconv(x1+dx), yconv(y1+dy), xconv(x1), yconv(y1));
235 fa325e9b 2020-01-10 cross printf(" L %.3f %.3f", xconv(x1+dx), yconv(y1+dy));
237 3ebbd193 2017-06-19 rsc if (nhead > 2)
238 3ebbd193 2017-06-19 rsc printf(" Z");
239 3ebbd193 2017-06-19 rsc printf("\"");
240 3ebbd193 2017-06-19 rsc if(nhead > 3)
241 3ebbd193 2017-06-19 rsc printf(" fill=\"black\" stroke=\"black\"");
242 3ebbd193 2017-06-19 rsc else if(nhead == 3)
243 3ebbd193 2017-06-19 rsc printf(" fill=\"white\" stroke=\"black\"");
245 3ebbd193 2017-06-19 rsc printf(" fill=\"transparent\" stroke=\"black\"");
246 3ebbd193 2017-06-19 rsc printf("/>\n");
249 3ebbd193 2017-06-19 rsc double lastgray = 0;
251 3ebbd193 2017-06-19 rsc void fillstart(double v, int vis, int fill)
256 3ebbd193 2017-06-19 rsc x = (int)(v*255.0);
257 3ebbd193 2017-06-19 rsc sprintf(svgfill, "#%02x%02x%02x", x, x, x);
259 3ebbd193 2017-06-19 rsc strcpy(svgfill, "transparent");
261 3ebbd193 2017-06-19 rsc strcpy(svgstroke, "black");
263 3ebbd193 2017-06-19 rsc strcpy(svgstroke, "transparent");
266 3ebbd193 2017-06-19 rsc void fillend(void)
268 3ebbd193 2017-06-19 rsc strcpy(svgfill, "transparent");
269 3ebbd193 2017-06-19 rsc strcpy(svgstroke, "black");
272 3ebbd193 2017-06-19 rsc void box(double x0, double y0, double x1, double y1, int attr, double ddval)
274 3ebbd193 2017-06-19 rsc printf("<path d=\"M %.3f %.3f V %.3f H %.3f V %.3f Z\" fill=\"transparent\" stroke=\"black\"", xconv(x0), yconv(y0), yconv(y1), xconv(x1), yconv(y0));
275 3ebbd193 2017-06-19 rsc if(attr & DASHBIT)
276 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"%.3f, %.3f\"", xsc(ddval), xsc(ddval));
277 3ebbd193 2017-06-19 rsc else if(attr & DOTBIT)
278 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"1, %.3f\"", xsc(ddval));
279 3ebbd193 2017-06-19 rsc printf("/>\n");
283 3ebbd193 2017-06-19 rsc void circle(double x, double y, double r)
285 3ebbd193 2017-06-19 rsc printf("<circle cx=\"%.3f\" cy=\"%.3f\" r=\"%.3f\" fill=\"%s\" stroke=\"%s\"/>\n", xconv(x), yconv(y), xsc(r), svgfill, svgstroke);
288 3ebbd193 2017-06-19 rsc void spline(double x, double y, double n, ofloat *p, int attr, double ddval)
291 bb044732 2017-09-01 0intro double x1, y1;
293 3ebbd193 2017-06-19 rsc printf("<path d=\"M %.3f %.3f", xconv(x), yconv(y));
296 3ebbd193 2017-06-19 rsc for (i = 0; i < 2 * n; i += 2) {
300 3ebbd193 2017-06-19 rsc y += p[i+1];
302 3ebbd193 2017-06-19 rsc printf(" L %.3f %.3f", xconv((x+x1)/2), yconv((y+y1)/2));
304 3ebbd193 2017-06-19 rsc printf(" Q %.3f %.3f %.3f %.3f", xconv(x1), yconv(y1), xconv((x+x1)/2), yconv((y+y1)/2));
306 3ebbd193 2017-06-19 rsc printf(" L %.3f %.3f", xconv(x), yconv(y));
307 3ebbd193 2017-06-19 rsc printf("\" fill=\"%s\" stroke=\"%s\"", svgfill, svgstroke);
308 3ebbd193 2017-06-19 rsc if(attr & DASHBIT)
309 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"%.3f, %.3f\"", xsc(ddval), xsc(ddval));
310 3ebbd193 2017-06-19 rsc else if(attr & DOTBIT)
311 3ebbd193 2017-06-19 rsc printf(" stroke-dasharray=\"1, %.3f\"", xsc(ddval));
312 3ebbd193 2017-06-19 rsc printf("/>\n");
315 3ebbd193 2017-06-19 rsc void ellipse(double x, double y, double r1, double r2)
317 3ebbd193 2017-06-19 rsc printf("<ellipse cx=\"%.3f\" cy=\"%.3f\" rx=\"%.3f\" ry=\"%.3f\" fill=\"%s\" stroke=\"%s\"/>\n", xconv(x), yconv(y), xsc(r1), ysc(r2), svgfill, svgstroke);
320 3ebbd193 2017-06-19 rsc void arc(double x, double y, double x0, double y0, double x1, double y1, double r) /* draw arc with center x,y */
322 3ebbd193 2017-06-19 rsc printf("<path d=\"M %.3f %.3f A %.3f %.3f %d %d %d %.3f %.3f\" fill=\"%s\" stroke=\"%s\"/>\n",
323 3ebbd193 2017-06-19 rsc xconv(x0), yconv(y0),
324 3ebbd193 2017-06-19 rsc xsc(r), ysc(r), 0, 0, 0, xconv(x1), yconv(y1),
325 3ebbd193 2017-06-19 rsc svgfill, svgstroke);