| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
|
|
static const char RCSid[] = "$Id$"; |
| 3 |
|
|
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Program to convert plot(5) files to metafiles |
| 6 |
|
|
* |
| 7 |
|
|
* cc -o plotin plotin.c primout.o mfio.o syscalls.o misc.o -lm |
| 8 |
|
|
* |
| 9 |
|
|
* 5/20/85 |
| 10 |
|
|
*/ |
| 11 |
|
|
|
| 12 |
|
|
#include "meta.h" |
| 13 |
|
|
|
| 14 |
|
|
|
| 15 |
|
|
#define getsi(f, m) ICONV(geti(f)-(m), size) |
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
|
|
|
| 19 |
|
|
char *progname; |
| 20 |
|
|
|
| 21 |
|
|
static int xmin = 0, ymin = 0, /* current space settings */ |
| 22 |
|
|
size = XYSIZE; |
| 23 |
|
|
|
| 24 |
|
|
static int curx = -1, /* current position */ |
| 25 |
|
|
cury = -1; |
| 26 |
|
|
|
| 27 |
|
|
static short curmod = 0; /* current line drawing mode */ |
| 28 |
|
|
|
| 29 |
|
|
|
| 30 |
|
|
|
| 31 |
|
|
main(argc, argv) |
| 32 |
|
|
|
| 33 |
|
|
int argc; |
| 34 |
|
|
char **argv; |
| 35 |
|
|
|
| 36 |
|
|
{ |
| 37 |
|
|
FILE *fp; |
| 38 |
|
|
|
| 39 |
|
|
progname = *argv++; |
| 40 |
|
|
argc--; |
| 41 |
|
|
|
| 42 |
|
|
while (argc && **argv == '-') { |
| 43 |
|
|
switch (*(*argv+1)) { |
| 44 |
|
|
default: |
| 45 |
|
|
error(WARNING, "unknown option"); |
| 46 |
|
|
break; |
| 47 |
|
|
} |
| 48 |
|
|
argv++; |
| 49 |
|
|
argc--; |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
if (argc) |
| 53 |
|
|
while (argc) { |
| 54 |
|
|
fp = efopen(*argv, "r"); |
| 55 |
|
|
convert(fp); |
| 56 |
|
|
fclose(fp); |
| 57 |
|
|
argv++; |
| 58 |
|
|
argc--; |
| 59 |
|
|
} |
| 60 |
|
|
else |
| 61 |
|
|
convert(stdin); |
| 62 |
|
|
|
| 63 |
|
|
pglob(PEOF, 0200, NULL); |
| 64 |
|
|
|
| 65 |
|
|
return(0); |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
|
|
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
convert(infp) /* convert to meta-file */ |
| 73 |
|
|
|
| 74 |
|
|
FILE *infp; |
| 75 |
|
|
|
| 76 |
|
|
{ |
| 77 |
|
|
char *fgets(), sbuf[BUFSIZ]; |
| 78 |
|
|
int command; |
| 79 |
|
|
int a1, a2, a3, a4, a5, a6; |
| 80 |
|
|
|
| 81 |
|
|
while ((command = getc(infp)) != EOF) |
| 82 |
|
|
switch (command) { |
| 83 |
|
|
case 'm': |
| 84 |
|
|
a1 = getsi(infp, xmin); |
| 85 |
|
|
a2 = getsi(infp, ymin); |
| 86 |
|
|
move(a1, a2); |
| 87 |
|
|
break; |
| 88 |
|
|
case 'n': |
| 89 |
|
|
a1 = getsi(infp, xmin); |
| 90 |
|
|
a2 = getsi(infp, ymin); |
| 91 |
|
|
cont(a1, a2); |
| 92 |
|
|
break; |
| 93 |
|
|
case 'p': |
| 94 |
|
|
a1 = getsi(infp, xmin); |
| 95 |
|
|
a2 = getsi(infp, ymin); |
| 96 |
|
|
point(a1, a2); |
| 97 |
|
|
break; |
| 98 |
|
|
case 'l': |
| 99 |
|
|
a1 = getsi(infp, xmin); |
| 100 |
|
|
a2 = getsi(infp, ymin); |
| 101 |
|
|
a3 = getsi(infp, xmin); |
| 102 |
|
|
a4 = getsi(infp, ymin); |
| 103 |
|
|
line(a1, a2, a3, a4); |
| 104 |
|
|
break; |
| 105 |
|
|
case 't': |
| 106 |
|
|
fgets(sbuf, sizeof(sbuf), infp); |
| 107 |
|
|
sbuf[strlen(sbuf)-1] = '\0'; |
| 108 |
|
|
label(sbuf); |
| 109 |
|
|
break; |
| 110 |
|
|
case 'a': |
| 111 |
|
|
a1 = getsi(infp, xmin); |
| 112 |
|
|
a2 = getsi(infp, ymin); |
| 113 |
|
|
a3 = getsi(infp, xmin); |
| 114 |
|
|
a4 = getsi(infp, ymin); |
| 115 |
|
|
a5 = getsi(infp, xmin); |
| 116 |
|
|
a6 = getsi(infp, ymin); |
| 117 |
|
|
arc(a1, a2, a3, a4, a5, a6); |
| 118 |
|
|
break; |
| 119 |
|
|
case 'c': |
| 120 |
|
|
a1 = getsi(infp, xmin); |
| 121 |
|
|
a2 = getsi(infp, ymin); |
| 122 |
|
|
a3 = getsi(infp, 0); |
| 123 |
|
|
circle(a1, a2, a3); |
| 124 |
|
|
break; |
| 125 |
|
|
case 'e': |
| 126 |
|
|
erase(); |
| 127 |
|
|
break; |
| 128 |
|
|
case 'f': |
| 129 |
|
|
fgets(sbuf, sizeof(sbuf), infp); |
| 130 |
|
|
linemod(sbuf); |
| 131 |
|
|
break; |
| 132 |
|
|
case 's': |
| 133 |
|
|
a1 = geti(infp); |
| 134 |
|
|
a2 = geti(infp); |
| 135 |
|
|
a3 = geti(infp); |
| 136 |
|
|
a4 = geti(infp); |
| 137 |
|
|
space(a1, a2, a3, a4); |
| 138 |
|
|
break; |
| 139 |
|
|
default: |
| 140 |
|
|
error(USER, "unknown command in convert"); |
| 141 |
|
|
break; |
| 142 |
|
|
} |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
|
| 146 |
|
|
|
| 147 |
|
|
|
| 148 |
|
|
int |
| 149 |
|
|
geti(fp) /* get two-byte integer from file */ |
| 150 |
|
|
|
| 151 |
|
|
register FILE *fp; |
| 152 |
|
|
|
| 153 |
|
|
{ |
| 154 |
|
|
register int i; |
| 155 |
|
|
|
| 156 |
|
|
i = getc(fp); |
| 157 |
|
|
i |= getc(fp) << 8; |
| 158 |
|
|
if (i & 0100000) |
| 159 |
|
|
i |= ~0177777; /* manual sign extend */ |
| 160 |
|
|
if (feof(fp)) |
| 161 |
|
|
error(USER, "unexpected end of file in geti"); |
| 162 |
|
|
|
| 163 |
|
|
return(i); |
| 164 |
|
|
} |
| 165 |
|
|
|
| 166 |
|
|
|
| 167 |
|
|
|
| 168 |
|
|
|
| 169 |
|
|
move(x, y) /* move to new position */ |
| 170 |
|
|
|
| 171 |
|
|
register int x, y; |
| 172 |
|
|
|
| 173 |
|
|
{ |
| 174 |
|
|
|
| 175 |
|
|
curx = x; |
| 176 |
|
|
cury = y; |
| 177 |
|
|
|
| 178 |
|
|
} |
| 179 |
|
|
|
| 180 |
|
|
|
| 181 |
|
|
|
| 182 |
|
|
|
| 183 |
|
|
cont(x, y) /* draw line to point */ |
| 184 |
|
|
|
| 185 |
|
|
register int x, y; |
| 186 |
|
|
|
| 187 |
|
|
{ |
| 188 |
|
|
|
| 189 |
|
|
plseg(curmod, curx, cury, x, y); |
| 190 |
|
|
curx = x; |
| 191 |
|
|
cury = y; |
| 192 |
|
|
|
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
|
| 197 |
|
|
point(x, y) /* draw a point */ |
| 198 |
|
|
|
| 199 |
|
|
register int x, y; |
| 200 |
|
|
|
| 201 |
|
|
{ |
| 202 |
|
|
|
| 203 |
|
|
plseg(0, x, y, x, y); |
| 204 |
|
|
curx = x; |
| 205 |
|
|
cury = y; |
| 206 |
|
|
|
| 207 |
|
|
} |
| 208 |
|
|
|
| 209 |
|
|
|
| 210 |
|
|
|
| 211 |
|
|
|
| 212 |
|
|
line(x0, y0, x1, y1) /* draw a line segment */ |
| 213 |
|
|
|
| 214 |
|
|
int x0, y0, x1, y1; |
| 215 |
|
|
|
| 216 |
|
|
{ |
| 217 |
|
|
|
| 218 |
|
|
move(x0, y0); |
| 219 |
|
|
cont(x1, y1); |
| 220 |
|
|
|
| 221 |
|
|
} |
| 222 |
|
|
|
| 223 |
|
|
|
| 224 |
|
|
|
| 225 |
|
|
label(s) /* print a label */ |
| 226 |
|
|
|
| 227 |
|
|
register char *s; |
| 228 |
|
|
|
| 229 |
|
|
{ |
| 230 |
|
|
|
| 231 |
|
|
pprim(PMSTR, 0, curx, cury, curx, cury, s); |
| 232 |
|
|
|
| 233 |
|
|
} |
| 234 |
|
|
|
| 235 |
|
|
|
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
static int del = 100; |
| 239 |
|
|
static step(d){ |
| 240 |
|
|
del = d; |
| 241 |
|
|
} |
| 242 |
|
|
arc(x,y,x0,y0,x1,y1){ |
| 243 |
|
|
double pc; |
| 244 |
|
|
double sqrt(); |
| 245 |
|
|
int flg,m,xc,yc,xs,ys,qs,qf; |
| 246 |
|
|
float dx,dy,r; |
| 247 |
|
|
char use; |
| 248 |
|
|
dx = x-x0; |
| 249 |
|
|
dy = y-y0; |
| 250 |
|
|
r = dx*dx+dy*dy; |
| 251 |
|
|
pc = r; |
| 252 |
|
|
pc = sqrt(pc); |
| 253 |
|
|
flg = pc/4; |
| 254 |
|
|
if(flg == 0)step(1); |
| 255 |
|
|
else if(flg < del)step(flg); |
| 256 |
|
|
xc = xs = x0; |
| 257 |
|
|
yc = ys = y0; |
| 258 |
|
|
move(xs,ys); |
| 259 |
|
|
if(x0 == x1 && y0 == y1)flg=0; |
| 260 |
|
|
else flg=1; |
| 261 |
|
|
qs = quadr(x,y,x0,y0); |
| 262 |
|
|
qf = quadr(x,y,x1,y1); |
| 263 |
|
|
if(abs(x-x1) < abs(y-y1)){ |
| 264 |
|
|
use = 'x'; |
| 265 |
|
|
if(qs == 2 || qs ==3)m = -1; |
| 266 |
|
|
else m=1; |
| 267 |
|
|
} |
| 268 |
|
|
else { |
| 269 |
|
|
use = 'y'; |
| 270 |
|
|
if(qs > 2)m= -1; |
| 271 |
|
|
else m= 1; |
| 272 |
|
|
} |
| 273 |
|
|
while(1){ |
| 274 |
|
|
switch(use){ |
| 275 |
|
|
case 'x': |
| 276 |
|
|
if(qs == 2 || qs == 3)yc -= del; |
| 277 |
|
|
else yc += del; |
| 278 |
|
|
dy = yc-y; |
| 279 |
|
|
pc = r-dy*dy; |
| 280 |
|
|
xc = m*sqrt(pc)+x; |
| 281 |
|
|
if((x < xs && x >= xc) || ( x > xs && x <= xc) || |
| 282 |
|
|
(y < ys && y >= yc) || ( y > ys && y <= yc) ) |
| 283 |
|
|
{ |
| 284 |
|
|
if(++qs > 4)qs=1; |
| 285 |
|
|
if(qs == 2 || qs == 3)m= -1; |
| 286 |
|
|
else m=1; |
| 287 |
|
|
flg=1; |
| 288 |
|
|
} |
| 289 |
|
|
cont(xc,yc); |
| 290 |
|
|
xs = xc; |
| 291 |
|
|
ys = yc; |
| 292 |
|
|
if(qs == qf && flg == 1) |
| 293 |
|
|
switch(qf){ |
| 294 |
|
|
case 3: |
| 295 |
|
|
case 4: |
| 296 |
|
|
if(xs >= x1)return; |
| 297 |
|
|
continue; |
| 298 |
|
|
case 1: |
| 299 |
|
|
case 2: |
| 300 |
|
|
if(xs <= x1)return; |
| 301 |
|
|
} |
| 302 |
|
|
continue; |
| 303 |
|
|
case 'y': |
| 304 |
|
|
if(qs > 2)xc += del; |
| 305 |
|
|
else xc -= del; |
| 306 |
|
|
dx = xc-x; |
| 307 |
|
|
pc = r-dx*dx; |
| 308 |
|
|
yc = m*sqrt(pc)+y; |
| 309 |
|
|
if((x < xs && x >= xc) || ( x > xs && x <= xc ) || |
| 310 |
|
|
(y < ys && y >= yc) || (y > ys && y <= yc) ) |
| 311 |
|
|
{ |
| 312 |
|
|
if(++qs > 4)qs=1; |
| 313 |
|
|
if(qs > 2)m = -1; |
| 314 |
|
|
else m = 1; |
| 315 |
|
|
flg=1; |
| 316 |
|
|
} |
| 317 |
|
|
cont(xc,yc); |
| 318 |
|
|
xs = xc; |
| 319 |
|
|
ys = yc; |
| 320 |
|
|
if(qs == qf && flg == 1) |
| 321 |
|
|
switch(qs){ |
| 322 |
|
|
case 1: |
| 323 |
|
|
case 4: |
| 324 |
|
|
if(ys >= y1)return; |
| 325 |
|
|
continue; |
| 326 |
|
|
case 2: |
| 327 |
|
|
case 3: |
| 328 |
|
|
if(ys <= y1)return; |
| 329 |
|
|
} |
| 330 |
|
|
} |
| 331 |
|
|
} |
| 332 |
|
|
} |
| 333 |
|
|
quadr(x,y,xp,yp){ |
| 334 |
|
|
if(x < xp) |
| 335 |
|
|
if(y <= yp)return(1); |
| 336 |
|
|
else return(4); |
| 337 |
|
|
else if(x > xp) |
| 338 |
|
|
if(y < yp)return(2); |
| 339 |
|
|
else return(3); |
| 340 |
|
|
else if(y < yp)return(2); |
| 341 |
|
|
else return(4); |
| 342 |
|
|
} |
| 343 |
|
|
|
| 344 |
|
|
|
| 345 |
|
|
|
| 346 |
|
|
circle(x,y,r){ |
| 347 |
|
|
arc(x,y,x+r,y,x+r,y); |
| 348 |
|
|
} |
| 349 |
|
|
|
| 350 |
|
|
|
| 351 |
|
|
|
| 352 |
|
|
|
| 353 |
|
|
erase() /* erase plot */ |
| 354 |
|
|
|
| 355 |
|
|
{ |
| 356 |
|
|
|
| 357 |
|
|
pglob(PEOP, 0200, NULL); |
| 358 |
|
|
|
| 359 |
|
|
} |
| 360 |
|
|
|
| 361 |
|
|
|
| 362 |
|
|
|
| 363 |
|
|
|
| 364 |
|
|
|
| 365 |
|
|
|
| 366 |
|
|
linemod(s) /* set line mode according to s */ |
| 367 |
|
|
|
| 368 |
|
|
char s[]; |
| 369 |
|
|
|
| 370 |
|
|
{ |
| 371 |
|
|
|
| 372 |
|
|
switch (s[2]) { |
| 373 |
|
|
case 'l': /* solid */ |
| 374 |
|
|
curmod = 0; |
| 375 |
|
|
break; |
| 376 |
|
|
case 't': /* dotted */ |
| 377 |
|
|
curmod = 1; |
| 378 |
|
|
break; |
| 379 |
|
|
case 'n': /* long dashed */ |
| 380 |
|
|
curmod = 2; |
| 381 |
|
|
break; |
| 382 |
|
|
case 'o': /* short dashed (dot dashed) */ |
| 383 |
|
|
curmod = 3; |
| 384 |
|
|
break; |
| 385 |
|
|
default: |
| 386 |
|
|
error(WARNING, "unknown line mode in linemod"); |
| 387 |
|
|
break; |
| 388 |
|
|
} |
| 389 |
|
|
|
| 390 |
|
|
} |
| 391 |
|
|
|
| 392 |
|
|
|
| 393 |
|
|
|
| 394 |
|
|
|
| 395 |
|
|
space(xmn, ymn, xmx, ymx) /* change space */ |
| 396 |
|
|
|
| 397 |
|
|
int xmn, ymn, xmx, ymx; |
| 398 |
|
|
|
| 399 |
|
|
{ |
| 400 |
|
|
|
| 401 |
|
|
if (xmn >= xmx || ymn >= ymx) |
| 402 |
|
|
error(USER, "illegal space specification in space"); |
| 403 |
|
|
|
| 404 |
|
|
xmin = xmn; |
| 405 |
|
|
ymin = ymn; |
| 406 |
|
|
size = min(xmx-xmn, ymx-ymn); |
| 407 |
|
|
|
| 408 |
|
|
} |