1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
#include "defs.h"
#include "global.h"
#include <math.h>
#include "emit.h"
#include "ps.h"
char *GetKeyStr();
int GetKeyVal();
int IsSame();
extern double cos(), sin(), sqrt();
static void ps_pensize();
static void ps_flushpath();
static void ps_flushdashedpath();
static void ps_drawto();
static void ps_arc();
static void ps_flushspline();
static int dist();
static void ps_whiten();
static void ps_blacken();
static void ps_shade();
static void push_location(), pop_location();
static void ps_setrgbcolor(), ps_sethsbcolor(), ps_setcmykcolor();
static void draw_line();
static void ps_texture();
static void ps_rotate();
#define TWOPI (3.14157926536*2.0)
#define MAXPOINTS 600 /* Most number of points in a path */
#define SPLINEPOINTS 5000 /* Most points in a spline */
#define RADTODEG (360/(TWOPI))
#define xconv(x) ((int) ((x) * ( ((float) resolution) / 1000.0) + 0.5))
#define yconv(y) ((int) ((y) * ( ((float) resolution) / 1000.0) + 0.5))
static int xx[MAXPOINTS], yy[MAXPOINTS], pathlen,
pensize = 2; /* Size we want Imagen to draw at, default 2 pixels */
static double graylevel = 0.5;
#define NOFILL 0
#define BLACK 1
#define GRAY 2
#define WHITE 3
static int
/* texture_defined = FALSE,*//* Have we done a set_texture yet? */
shade_next = NOFILL, /* Should next object be shaded? */
is_rotated = FALSE;
BOOLEAN usesspecial = FALSE;
BOOLEAN usescolor = FALSE;
#define skipblank(p) for (; *p == ' '; p++)
struct bangspecial {
struct bangspecial *bs_next;
char bs_string[1];
};
static struct bangspecial *bangspecials = NULL;
static struct bangspecial **nextbs = &bangspecials;
/*-->DoSpecial*/
/*********************************************************************/
/***************************** DoSpecial ***************************/
/*********************************************************************/
typedef enum {
None, String, Integer, Number, Dimension
} ValTyp;
typedef struct {
char *Key; /* the keyword string */
char *Val; /* the value string */
ValTyp vt; /* the value type */
union { /* the decoded value */
int i;
float n;
} v;
} KeyWord;
typedef struct {
char *Entry;
ValTyp Type;
} KeyDesc;
#define PSFILE 0
#define EPSFILE 1
KeyDesc KeyTab[] = {{"psfile", String},
{"epsfile", String},
{"hsize", Dimension},
{"vsize", Dimension},
{"hoffset", Dimension},
{"voffset", Dimension},
{"hscale", Number},
{"vscale", Number},
{"rotation", Dimension},
{"angle", Number},
{"llx", Number},
{"lly", Number},
{"urx", Number},
{"ury", Number},
{"rwi", Number},
{"rhi", Number},
{"clip", None}
};
#define NKEYS (sizeof(KeyTab)/sizeof(KeyTab[0]))
/* ARGSUSED */
void
dev_predospecial(str, n)
char *str;
int n;
{
char spbuf[STRSIZE];
char *sf = NULL;
char *p, *q;
struct bangspecial *b;
KeyWord k;
int i;
str[n] = '\0';
spbuf[0] = '\0';
p = str;
skipblank(p);
if (*p == '\0')
return;
usesspecial = TRUE;
if (*p == '!') {
b = (struct bangspecial *)
alloc_check(malloc((unsigned)sizeof(struct bangspecial)+n-1),
"bangspecials");
strncpy(b->bs_string, p+1, n);
b->bs_next = NULL;
*nextbs = b;
nextbs = &(b->bs_next);
return;
}
if (strncmp(p, "landscape", 9) == 0) {
landscape = TRUE;
return;
}
if (strncmp(p, "papersize", 9) == 0) {
p += 9;
while (*p == '=' || *p == ' ')
p++;
/* TODO */
return;
}
if (strncmp(p, "header", 6) == 0) {
p += 6;
while ((*p <= ' ' || *p == '=' || *p == '(') && *p != '\0')
p++;
q = p + strlen(p) - 1;
while ((*q <= ' '|| *q == '(') && p <= q)
q--;
*++q = '\0';
if (p <= q)
ps_add_include(p);
return;
}
if (strncmp(p, "background", 10) == 0) {
usescolor = TRUE;
p += 11;
skipblank(p);
background(p);
return;
}
if (strncmp(p, "color", 5) == 0) {
usescolor = TRUE;
p += 6;
skipblank(p);
if (strncmp(p, "push", 4) == 0) {
p += 5;
skipblank(p);
pushcolor(p, FALSE);
} else if (strncmp(p, "pop", 3) == 0) {
popcolor(FALSE);
} else {
resetcolorstack(p, FALSE);
}
return;
}
if (strncmp(p, "src:", 4) == 0) {
return;
}
if (strncmp(p, "pos:", 4) == 0) {
return;
}
if (strncmp(p, "om:", 3) == 0) {
return;
}
if (strncmp(p, "xtex:", 5) == 0) {
return;
}
if (strncmp(p, "html:", 5) == 0) {
return;
}
while ((p=GetKeyStr(p,&k)) != NULL) { /* get all keyword-value pairs */
/* for compatibility, single words are taken as file names */
if (k.vt == None && access(k.Key,F_OK) == 0) {
/*
if (sf)
Warning(" More than one \\special file name given. %s ignored", sf);
*/
(void)strcpy(spbuf, k.Key);
sf = spbuf;
} else if (GetKeyVal(&k, KeyTab, NKEYS, &i) && i != -1) {
if (i == PSFILE || i == EPSFILE ) {
/*
if (sf)
Warning(" More than one \\special file name given. %s ignored", sf);
*/
(void)strcpy(spbuf, k.Val);
sf = spbuf;
} /* the keywords are ignored */
} /*else
Warning(" Invalid keyword or value in \\special - \"%s\" ignored",
k.Key);*/
}
if (sf) {
if (*sf == '`')
/* TO DO? */;
else
ps_scanifont(sf);
} /*else
Warning(" No special file name provided.");*/
}
dev_outbangspecials()
{
struct bangspecial *b;
if (bangspecials != NULL) {
EMITS("TeXDict begin @defspecial\n");
for (b = bangspecials; b != NULL; b = b->bs_next) {
EMITC('\n');
ps_string(b->bs_string);
EMITC('\n');
}
EMITS("\n@fedspecial end\n");
}
}
/* interpret a \special command, made up of keyword=value pairs */
void
dev_dospecial(str, n)
char *str;
int n;
{
char spbuf[STRSIZE];
char *sf = NULL;
char *p, *q;
KeyWord k;
int i;
float llx, lly, urx, ury, hsize, vsize;
str[n] = '\0';
spbuf[0] = '\0';
p = str;
end_string();
dev_setposn(h, v);
for (; *p == ' '; p++)
;
if (*p == '\0')
return;
if (*p == '!')
return;
if (strncmp(p, "landscape", 9) == 0)
return;
if (strncmp(p, "papersize", 9) == 0)
return;
if (strncmp(p, "header", 6) == 0)
return;
if (strncmp(p, "ps:", 3) == 0) {
dev_initfont();
p += 3;
if (*p == ':') {
p++;
if (strncmp(p, "[begin]", 7) == 0) {
EMITC('\n');
ps_string(p+7);
EMITC('\n');
} else if (strncmp(p, "[end]", 5) == 0) {
EMITC('\n');
ps_string(p+5);
EMITC('\n');
} else {
EMITC('\n');
ps_string(p);
EMITC('\n');
}
} else if (strncmp(p, " plotfile ", 10) == 0) {
p += 10;
for (; *p == ' '; p++);
if (*p == '"')
for (q = ++p; *q != '\0' && *q != '"'; q++);
else
for (q = p; *q != '\0' && *q != ' '; q++);
*q = '\0';
if (*p == '`')
ps_createpipe(++p);
else
ps_copyfigfile(p);
} else {
EMITC('\n');
ps_string(p);
EMITC('\n');
dev_setposn(h, v);
dev_initfont();
}
return;
}
if (*p == '"') {
EMIT(outfp, "@beginspecial\n");
EMITS("@setspecial\n");
EMITS("TeXDict begin\n");
ps_string(++p);
EMITS("\nend\n");
EMITS("@endspecial\n");
return;
}
if (strncmp(p, "pn ", 3) == 0) {
ps_pensize(p+3);
return;
}
if (strcmp(p, "fp") == 0) {
ps_flushpath(0);
return;
}
if (strcmp(p, "ip") == 0) { /* tpic 2.0 */
ps_flushpath(1);
return;
}
if (strncmp(p, "da ", 3) == 0) {
ps_flushdashedpath(p+3, 0);
return;
}
if (strncmp(p, "dt ", 3) == 0) {
ps_flushdashedpath(p+3, 1);
return;
}
if (strncmp(p, "pa ", 3) == 0) {
ps_drawto(p+3);
return;
}
if (strncmp(p, "ar ", 3) == 0) { /* tpic 2.0 */
ps_arc(p+3, 0);
return;
}
if (strncmp(p, "ia ", 3) == 0) { /* tpic 2.0 */
ps_arc(p+3, 1);
return;
}
if (strcmp(p, "sp") == 0) { /* tpic 2.0 */
ps_flushspline(p+2);
return;
}
if (strncmp(p, "sp ", 3) == 0) { /* tpic 2.0 */
ps_flushspline(p+3);
return;
}
if (strcmp(p, "sh") == 0) { /* tpic 2.0 */
ps_shade(p+2);
return;
}
if (strncmp(p, "sh", 2) == 0) { /* tpic 2.0 */
ps_shade(p+3);
return;
}
if (strcmp(p, "wh") == 0) {
ps_whiten();
return;
}
if (strcmp(p, "bk") == 0) {
ps_blacken();
return;
}
if (strncmp(p, "tx ", 3) == 0) {
ps_texture(p+3);
return;
}
if (strncmp(p, "rt ", 3) == 0) { /* dviout/prt */
ps_rotate(p+2);
return;
}
if (strncmp(p, "background", 10) == 0)
return;
if (strncmp(p, "color", 5) == 0) {
p += 6;
skipblank(p);
if (strncmp(p, "push", 4) == 0) {
p += 5;
skipblank(p);
pushcolor(p, TRUE);
} else if (strncmp(p, "pop", 3) == 0) {
popcolor(TRUE);
} else {
resetcolorstack(p, TRUE);
}
return;
}
if (strncmp(p, "RGB", 3) == 0) { /* eclcolor.sty */
p += 3;
if (*p == '=')
p++;
ps_setrgbcolor(p);
return;
}
if (strncmp(p, "HSB", 3) == 0) { /* eclcolor.sty */
p += 3;
if (*p == '=')
p++;
ps_sethsbcolor(p);
return;
}
if (strncmp(p, "CMYK", 4) == 0) { /* eclcolor.sty */
p += 4;
if (*p == '=')
p++;
ps_setcmykcolor(p);
return;
}
if (strncmp(p, "postscriptbox", 13) == 0) { /* for jdvi2kps */
EMIT(outfp, "@beginspecial\n");
for (p += 13; *p == ' '; p++)
;
if (sscanf(p, "{ %fpt }{ %fpt }{ %[^ {}] }", &hsize, &vsize, spbuf) != 3)
Warning(" badly formed postscriptbox command - ignored");
sf = spbuf;
if (scanfile(sf, &llx, &lly, &urx, &ury)) {
EMIT(outfp, "%f %f %f %f false @bbox\n", llx, lly, urx, ury);
EMIT(outfp, "%f @hsize\n", hsize*72/72.27);
EMIT(outfp, "%f @vsize\n", vsize*72/72.27);
}
EMITS("@setspecial\n");
if (sf) {
if (*sf == '`')
ps_createpipe(++sf);
else
ps_copyfigfile(sf);
} else
Warning(" No special file name provided.");
EMITS("@endspecial\n");
return;
}
if (strncmp(p, "src:", 4) == 0) {
return;
}
if (strncmp(p, "pos:", 4) == 0) {
return;
}
if (strncmp(p, "om:", 3) == 0) {
return;
}
if (strncmp(p, "xtex:", 5) == 0) {
return;
}
if (strncmp(p, "html:", 5) == 0) {
return;
}
EMIT(outfp, "@beginspecial\n");
while ((p=GetKeyStr(p,&k)) != NULL) { /* get all keyword-value pairs */
/* for compatibility, single words are taken as file names */
if (k.vt == None && access(k.Key,F_OK) == 0) {
if (sf)
Warning(" More than one \\special file name given. %s ignored", sf);
(void)strcpy(spbuf, k.Key);
sf = spbuf;
} else if (GetKeyVal(&k, KeyTab, NKEYS, &i) && i != -1) {
if (i == PSFILE || i == EPSFILE ) {
if (sf)
Warning(" More than one \\special file name given. %s ignored", sf);
(void)strcpy(spbuf, k.Val);
sf = spbuf;
if (i == EPSFILE && scanfile(sf, &llx, &lly, &urx, &ury)) {
EMIT(outfp, "%f %f %f %f true @bbox\n",
llx, lly, urx, ury);
EMIT(outfp, "@ecl\n");
}
} else /* the keywords are simply output as PS procedure calls */
EMIT(outfp, "%f @%s\n", k.v.n, KeyTab[i].Entry);
} else
Warning(" Invalid keyword or value in \\special - \"%s\" ignored",
k.Key);
}
EMITS("@setspecial\n");
if (sf) {
if (*sf == '`')
ps_createpipe(++sf);
else
ps_copyfigfile(sf);
} else
Warning(" No special file name provided.");
EMITS("@endspecial\n");
return;
}
/*-->GetKeyStr*/
/**********************************************************************/
/***************************** GetKeyStr ****************************/
/**********************************************************************/
/* extract first keyword-value pair from string (value part may be null)
* return pointer to remainder of string
* return NULL if none found
*/
char KeyStr[STRSIZE];
char ValStr[STRSIZE];
char *GetKeyStr(str, kw)
char *str;
KeyWord *kw;
{
char *s, *k, *v, t;
if (!str)
return NULL;
for (s = str ; *s == ' '; s++ )
;
if (*s == '\0')
return NULL;
/* extract keyword portion */
for (k = KeyStr; *s != ' ' && *s != '\0' && *s != '='; *k++ = *s++)
;
*k = '\0';
kw->Key = KeyStr;
kw->Val = v = NULL;
kw->vt = None;
for (; *s == ' '; s++) /* skip over blanks */
;
if (*s != '=') /* look for "=" */
return s;
for (s++ ; *s == ' '; s++) /* skip over blanks */
;
if (*s == '\'' || *s == '\"') /* get string delimiter */
t = *s++;
else
t = ' ';
/* copy value portion up to delim */
for (v = ValStr; *s != t && *s != '\0'; *v++ = *s++)
;
if (t != ' ' && *s == t)
s++;
*v = '\0';
kw->Val = ValStr;
kw->vt = String;
return s;
}
/*-->GetKeyVal*/
/**********************************************************************/
/***************************** GetKeyVal ****************************/
/**********************************************************************/
/* get next keyword-value pair
* decode value according to table entry
*/
int GetKeyVal(kw, tab, nt, tno)
KeyWord *kw;
KeyDesc tab[];
int nt;
int *tno;
{
int i;
char c = '\0';
*tno = -1;
for (i=0; i<nt; i++)
if (IsSame(kw->Key, tab[i].Entry)) {
*tno = i;
switch (tab[i].Type) {
case None:
if (kw->vt != None)
return FALSE;
break;
case String:
if (kw->vt != String)
return FALSE;
break;
case Integer:
if (kw->vt != String)
return FALSE;
if (sscanf(kw->Val,"%d%c", &(kw->v.i), &c) != 1 || c != '\0')
return FALSE;
break;
case Number:
case Dimension:
if (kw->vt != String)
return FALSE;
if (sscanf(kw->Val,"%f%c", &(kw->v.n), &c) != 1 || c != '\0')
return FALSE;
break;
}
kw->vt = tab[i].Type;
return TRUE;
}
return TRUE;
}
char ToLower(c)
char c;
{
return (isupper(c)?tolower(c):c);
}
int IsSame(a, b) /* compare strings, ignore case */
char *a, *b;
{
for ( ; *a != '\0'; )
if (ToLower(*a++) != ToLower(*b++))
return FALSE;
return (*a == *b ? TRUE : FALSE);
}
/*-->scanfile*/ /* scan a file */
/*********************************************************************/
/***************************** scanfile ******************************/
/*********************************************************************/
BOOLEAN
scanfile(str, llx, lly, urx, ury)
char *str;
float *llx, *lly, *urx, *ury;
{
FILE *spfp;
char buffer[BUFSIZ];
int i;
if ((spfp = fopen(str,"r")) == NULL) {
Warning("Unable to open file %s", str);
return FALSE;
}
while (fgets(buffer, BUFSIZ, spfp) != NULL) {
i = 0;
if (buffer[i++] == '%' && buffer[i++] == '%') {
if (!strncmp(&buffer[i], "BoundingBox:", 12)) {
for (; buffer[i++] != ':'; )
;
for (; buffer[i] == ' '; i++) /* skip space */
;
if (buffer[i] != '?' && strncmp(&buffer[i], "(atend)", 7)) {
(void)sscanf(&buffer[i], "%f %f %f %f", llx, lly, urx, ury);
(void)fclose(spfp);
return TRUE;
}
}
}
}
(void)fclose(spfp);
return FALSE;
}
/*
* Support drawing routines for Chris Torek's DVI->ImPress program.
*
* Requires v1.7 or later ImPress to handle paths.
* Better if v1.9 or later for arc, circle, and ellipse primitives
* (define USEGRAPHICS).
*
* Tim Morgan, UC Irvine, 11/17/85
*
* PostScript version
* Kazuhiro Kazama, NTT Software Laboratories.
*/
/*
* Save the graphic state and set up a new coordinate system
*/
static void push_location()
{
EMIT(outfp, "%.3f @push\n", (float)mag/1000);
}
/*
* Restore the graphic state
*/
static void pop_location()
{
EMIT(outfp, "@pop\n");
}
/*
* Set the pen size
* Called as \special{pn size}
* eg: \special{pn 8}
* The size is the number of milli-inches for the diameter of the pen.
* This routine converts that value to device-dependent pixels, and makes
* sure that the resulting value is within legal bounds.
*/
static void ps_pensize(cp)
char *cp;
{
int size;
if (sscanf(cp, "%d ", &size) != 1) {
Warning("Illegal format for pn: %s", cp);
return;
}
pensize = xconv(size);
if (pensize < 0) pensize = 0;
}
/*
* Make sure the pen size is set. Since we push and pop the state,
* this has to be sent for each different object (I think).
*/
static void set_pen_size()
{
EMIT(outfp, "%d setlinewidth\n", pensize);
}
/*
* Actually apply the attributes (shade, whiten, or blacken) to the currently
* defined path/figure.
*/
static void do_attributes()
{
switch (shade_next) {
case BLACK:
EMIT(outfp, "0 setgray ");
break;
case GRAY:
EMIT(outfp, "%1.3f setgray ", graylevel);
break;
case WHITE:
EMIT(outfp, "1 setgray ");
break;
}
shade_next = NOFILL;
}
/*
* Flush the path that we've built up with ps_drawto()
* Called as \special{fp}
*/
static void ps_flushpath(invisible)
int invisible;
{
int i;
push_location();
if (pathlen <= 0) return;
set_pen_size();
EMIT(outfp, "%d %d p\n", xx[1], yy[1]);
for (i=2; i<=pathlen; i++) {
EMIT(outfp, "%d %d l\n", xx[i], yy[i]);
}
pathlen = 0;
if (shade_next == NOFILL)
EMIT(outfp, "stroke\n");
else {
if (invisible) {
do_attributes();
EMIT(outfp, "fill\n");
} else {
EMIT(outfp, "gsave ");
do_attributes();
EMIT(outfp, "fill grestore stroke\n");
}
}
pop_location();
}
/*
* Draw a dashed or dotted line between the first pair of points in the array
* Called as \special{da <inchesperdash>} (dashed line)
* or \special{dt <inchesperdot>} (dotted line)
* eg: \special{da 0.05}
*/
static void ps_flushdashedpath(cp, dotted)
char *cp;
int dotted; /* boolean */
{
int i;
int ipd;
float inchesperdash;
if (sscanf(cp, "%f ", &inchesperdash) != 1) {
Warning("Illegal format for dotted/dashed line da/dt: %s", cp);
return;
}
ipd = (int)(1000.0 * inchesperdash + 0.5);
if (ipd == 0)
ipd = 1;
push_location();
if (pathlen <= 0) return;
set_pen_size();
EMIT(outfp, "%d %d p\n", xx[1], yy[1]);
for (i=2; i<=pathlen; i++) {
if (dotted) {
EMIT(outfp, "1 setlinecap\n");
EMIT(outfp, "[%d %d] 0 setdash\n",
pensize, (int)xconv((ipd)) - pensize);
} else {
EMIT(outfp, "[%d] 0 setdash\n", (int)xconv(ipd));
}
EMIT(outfp, "%d %d l\n", xx[i], yy[i]);
}
pathlen = 0;
if (shade_next == NOFILL)
EMIT(outfp, "stroke\n");
else {
EMIT(outfp, "gsave ");
do_attributes();
EMIT(outfp, "fill grestore stroke\n");
}
pop_location();
}
/*
* Virtually draw to a given x,y position on the virtual page.
* X and Y are expressed in thousandths of an inch, and this
* routine converts them to pixels.
*
* Called as \special{pa <x> <y>}
* eg: \special{pa 0 1200}
*/
static void ps_drawto(cp)
char *cp;
{
int x,y;
if (sscanf(cp, "%d %d ", &x, &y) != 2) {
Warning("Illegal format for pa: %s", cp);
return;
}
if (++pathlen >= MAXPOINTS)
Fatal("Too many points specified\n", 0);
xx[pathlen] = xconv(x);
yy[pathlen] = yconv(y);
}
static void ps_arc(cp, invisible)
char *cp;
int invisible;
{
int xc, yc, xrad, yrad;
float start_angle, end_angle;
short alpha0, alpha1;
if (sscanf(cp, "%d %d %d %d %f %f ",
&xc, &yc, &xrad, &yrad, &start_angle, &end_angle) != 6) {
Warning("Illegal format for arc: %s", cp);
return;
}
push_location();
set_pen_size();
if (start_angle < 0.0) /* for gpic */
start_angle += TWOPI;
if (end_angle < 0.0)
end_angle += TWOPI;
alpha0 = start_angle * RADTODEG + 0.5;
alpha1 = end_angle * RADTODEG + 0.5;
EMIT(outfp, "%d %d %d %d %d %d @ar\n",
xconv(xc), yconv(yc), xconv((double) xrad), yconv((double) yrad),
alpha0, alpha1);
if (shade_next == NOFILL)
EMIT(outfp, "stroke\n");
else {
if (invisible) {
do_attributes();
EMIT(outfp, "fill\n");
} else {
EMIT(outfp, "gsave ");
do_attributes();
EMIT(outfp, "fill grestore stroke\n");
}
}
pop_location();
}
/*
* Create a spline through the points in the array.
* Called like flush path (fp) command, after points
* have been defined via pa command(s).
*
* eg: \special{sp}
*/
int splinex[SPLINEPOINTS], spliney[SPLINEPOINTS], splinelen;
static void ps_flushspline(cp)
char *cp;
{
int xp, yp, N;
double t1, t2, t3, w;
int i, j, steps;
push_location();
set_pen_size();
if (*cp) {
float f;
int ipd;
(void) sscanf(cp, "%f", &f);
ipd = (int)(1000.0 * (f > 0 ? f : -f) + 0.5);
if (ipd == 0)
ipd = 1;
if (f < 0.0) { /* dotted */
EMIT(outfp, "1 setlinecap\n");
EMIT(outfp, "[%d %d] 0 setdash\n",
pensize, (int)xconv((ipd)) - pensize);
} else if (f > 0.0) /* dashed */
EMIT(outfp, "[%d] 0 setdash\n", (int)xconv(ipd));
}
splinelen = 0;
N = pathlen + 1;
xx[0] = xx[1];
yy[0] = yy[1];
xx[N] = xx[N-1];
yy[N] = yy[N-1];
for (i = 0; i < N-1; i++) { /* interval */
steps = (dist(xx[i],yy[i], xx[i+1],yy[i+1]) +
dist(xx[i+1],yy[i+1], xx[i+2],yy[i+2])) / 20;
for (j = 0; j < steps; j++) { /* points within */
w = ((double) j + 0.5) / ((double) steps);
t1 = 0.5 * w * w;
w -= 0.5;
t2 = 0.75 - w * w;
w -= 0.5;
t3 = 0.5 * w * w;
xp = t1 * xx[i+2] + t2 * xx[i+1] + t3 * xx[i] + 0.5;
yp = t1 * yy[i+2] + t2 * yy[i+1] + t3 * yy[i] + 0.5;
if (splinelen >= SPLINEPOINTS)
Fatal("Too many points in spline\n", 0);
splinex[splinelen] = xp;
spliney[splinelen++] = yp;
}
}
EMIT(outfp, "%d %d p\n", splinex[0], spliney[0]);
for (i=1; i<splinelen; i++) {
EMIT(outfp, "%d %d l\n", splinex[i], spliney[i]);
}
pathlen = 0;
if (shade_next == NOFILL)
EMIT(outfp, "stroke\n");
else {
EMIT(outfp, "gsave ");
do_attributes();
EMIT(outfp, "fill grestore stroke\n");
}
pop_location();
}
static int dist(x1, y1, x2, y2) /* integer distance from x1,y1 to x2,y2 */
{
double dx, dy;
dx = x2 - x1;
dy = y2 - y1;
return sqrt(dx*dx + dy*dy) + 0.5;
}
/*
* Whiten the interior of the next figure (path). Command is:
* \special{wh}
*/
static void ps_whiten()
{
shade_next = WHITE;
}
/*
* Blacken the interior of the next figure (path). Command is:
* \special{bk}
*/
static void ps_blacken()
{
shade_next = BLACK;
}
/*
* Shade the interior of the next figure (path) with the predefined
* texture. Command is:
* \special{sh}
*/
static void ps_shade(cp)
char *cp;
{
if (*cp) {
float f;
if (sscanf(cp, "%f", &f) != 1) {
Warning("Illegal format for shading value sh: %s", cp);
return;
}
if (f >= 0.0 && f <= 1.0)
graylevel = 1.0 - f;
} else
graylevel = 0.5;
shade_next = GRAY;
}
static void ps_texture(cp)
char *cp;
{
int blackbits = 0, totalbits = 0;
while (*cp) {
switch (*cp) {
case '0':
totalbits += 4;
break;
case '1':
case '2':
case '4':
case '8':
blackbits++;
totalbits += 4;
break;
case '3':
case '5':
case '6':
case '9':
case 'a':
case 'A':
case 'c':
case 'C':
blackbits += 2;
totalbits += 4;
break;
case '7':
case 'b':
case 'B':
case 'd':
case 'D':
case 'e':
case 'E':
blackbits += 3;
totalbits += 4;
break;
case 'f':
case 'F':
blackbits += 4;
totalbits += 4;
break;
case ' ':
break;
default:
/* error */
break;
}
cp++;
}
graylevel = 1.0 - ((double) blackbits / (double) totalbits);
shade_next = GRAY;
}
static void ps_rotate(cp)
char *cp;
{
int x, y;
float angle;
if (sscanf(cp, "%d %d %f", &x, &y, &angle) != 3) {
Warning("Illegal format for rt: %s", cp);
return;
}
dev_initfont();
if (angle == 0 && is_rotated) {
EMIT(outfp, "currentpoint grestore moveto\n");
is_rotated = FALSE;
} else {
if (is_rotated)
EMIT(outfp, "currentpoint grestore moveto\n");
else
is_rotated = TRUE;
EMIT(outfp, "gsave currentpoint %d add exch %d add exch 2 copy\n",
yconv(y), xconv(x));
EMIT(outfp, "translate %f rotate neg exch neg exch translate\n",
angle * RADTODEG);
}
}
static void ps_setrgbcolor(cp)
char *cp;
{
float r, g, b;
(void)sscanf(cp, "%f %f %f", &r, &g, &b);
EMIT(outfp, "%f %f %f @RGB\n", r, g, b);
}
static void ps_sethsbcolor(cp)
char *cp;
{
float h, s, b;
(void)sscanf(cp, "%f %f %f", &h, &s, &b);
EMIT(outfp, "%f %f %f @HSB\n", h, s, b);
}
static void ps_setcmykcolor(cp)
char *cp;
{
float c, m, y, k;
(void)sscanf(cp, "%f %f %f %f", &c, &m, &y, &k);
EMIT(outfp, "%f %f %f %f @CMYK\n", c, m, y, k);
}
|