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 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
|
/*
* $Id: cgm.c,v 1.2 2006-08-04 04:40:23 dhmunro Exp $
* Implement the CGM binary metafile engine for GIST.
*/
/* Copyright (c) 2005, The Regents of the University of California.
* All rights reserved.
* This file is part of yorick (http://yorick.sourceforge.net).
* Read the accompanying LICENSE file for details.
*/
#include "play.h"
#include "pstdlib.h"
#include "pstdio.h"
#include "cgm.h"
#include "gtext.h"
#include <string.h>
#include <time.h>
static g_callbacks g_cgm_on = { "gist CGMEngine", 0, 0, 0, 0, 0, 0, 0, 0 };
#define N_CGMFONTS 20
extern double floor(double);
#ifndef CREATE_CGM
#define CREATE_CGM(name) p_fopen(name, "wb")
#endif
/* ------------------------------------------------------------------------ */
static Octet *FormCommand(Octet *buffer, int klass, int id, long nbytes,
long *lpart);
static Octet *NextPartition(Octet *buffer, long nbytes, long *lpart);
static Octet *Pascalify(Octet *buffer, const char *text, long len, int pad);
static Octet *FormWords(Octet *buffer, const short *words, long nwords);
static void FormReal(short *fixed, GpReal x);
static int WriteB(p_file *file, void *buffer, long nbytes);
static void WriteError(CGMEngine *cgm, const char *msg);
static int BeginMetafile(CGMEngine *cgm);
static void SetPageDefaults(CGMEngine *cgmEngine);
static void SetCGMTransform(GpTransform *toPixels, int landscape,
GpReal scale);
static int BeginPage(CGMEngine *cgmEngine);
static void EndClip(CGMEngine *cgmEngine);
static void BeginClip(CGMEngine *cgmEngine, GpTransform *trans);
static int EndPage(CGMEngine *cgmEngine);
static int ChangePalette(Engine *engine);
static void Kill(Engine *engine);
static int Clear(Engine *engine, int always);
static int Flush(Engine *engine);
static int SetupColor(CGMEngine *cgmEngine, unsigned long color, int which);
static int SetupLine(CGMEngine *cgmEngine);
static int SetupEdge(CGMEngine *cgmEngine);
static void CheckClip(CGMEngine *cgmEngine);
static int DrawLines(Engine *engine, long n, const GpReal *px,
const GpReal *py, int closed, int smooth);
static int SetupMarker(CGMEngine *cgmEngine);
static int DrawMarkers(Engine *engine, long n, const GpReal *px,
const GpReal *py);
static int SetupText(CGMEngine *cgmEngine);
static int DrwText(Engine *engine, GpReal x0, GpReal y0, const char *text);
static int DrawFill(Engine *engine, long n, const GpReal *px,
const GpReal *py);
static int DrawCells(Engine *engine, GpReal px, GpReal py, GpReal qx,
GpReal qy, long width, long height, long nColumns,
const GpColor *colors);
static int DrawDisjoint(Engine *engine, long n, const GpReal *px,
const GpReal *py, const GpReal *qx, const GpReal *qy);
static void IncrementName(char *filename);
/* ------------------------------------------------------------------------ */
/* Allow for architectures in which a short does not have the same
size or order as presumed in the CGM binary encoding standard
(a short presumed to be 2 bytes in big-endian order, i.e.- MSB first). */
#define CGM_WORD_ORDER(words, nwords) if(cgm_not)cgm_swap(words,nwords)
static void cgm_swap(short *words, long nwords);
static int cgm_not = -1;
static void
cgm_swap(short *words, long nwords)
{
short word;
char *pos;
if (cgm_not<0) {
word = 1;
pos = (char *)&word;
cgm_not = (sizeof(short)!=2 || pos[0]);
if (!cgm_not) return;
}
pos = (char *)words; /* reordering is done in place */
while (nwords--) {
word = *(words++);
*(pos++) = word >> 8;
*(pos++) = word & 0xff;
}
}
/* ------------------------------------------------------------------------ */
static Octet *FormCommand(Octet *buffer, int klass, int id, long nbytes,
long *lpart)
{
long nhere;
*buffer++= (Octet)((klass<<4) | (id>>3));
if (nbytes < 0x1f) {
*buffer++= (Octet)((id<<5) | nbytes);
nhere= nbytes;
} else {
*buffer++= (Octet)((id<<5) | (nbytes<0x1f? nbytes : 0x1f));
if (nbytes < MAX_PARTITION) {
nhere= nbytes;
*buffer++= (Octet)(nhere>>8);
*buffer++= (Octet)(nhere&0xff);
} else {
nhere= MAX_PARTITION;
*buffer++= (Octet)((nhere>>8) | 0x80);
*buffer++= (Octet)(nhere&0xff);
}
}
*lpart= nhere;
return buffer;
}
static Octet *NextPartition(Octet *buffer, long nbytes, long *lpart)
{
long nhere;
if (nbytes < MAX_PARTITION) {
nhere= nbytes;
*buffer++= (Octet)(nhere>>8);
*buffer++= (Octet)(nhere&0xff);
} else {
nhere= MAX_PARTITION;
*buffer++= (Octet)((nhere>>8) | 0x80);
*buffer++= (Octet)(nhere&0xff);
}
*lpart= nhere;
return buffer;
}
static Octet *Pascalify(Octet *buffer, const char *text, long len, int pad)
{
/* Note-- strlen(text)<255 for this to work properly; writes
strlen(text)+1 bytes into buffer (+2 if pad set and len even) */
len= len>=0? len : (text? strlen(text) : 0);
if (len>254) len= 254; /* ignore text beyond 254 characters */
pad= (pad && !(len&1));
*buffer++= (Octet)len;
while (len--) *buffer++= *text++;
if (pad) *buffer++= '\0'; /* force total number of Octets to be even */
return buffer;
}
static Octet *FormWords(Octet *buffer, const short *words, long nwords)
{
while (nwords--) {
*buffer++= (Octet)(*words >> 8);
*buffer++= (Octet)(*words++ & 0xff);
}
return buffer;
}
static void FormReal(short *fixed, GpReal x)
{
unsigned short *fx= (unsigned short *)fixed;
GpReal ix= floor(x);
fx[0]= (unsigned short)((int)ix);
fx[1]= (unsigned short)(65536.0*(x-ix));
}
static int WriteB(p_file *file, void *buffer, long nbytes)
{
if (!file) return 1;
return p_fwrite(file, buffer, nbytes) != (unsigned long)nbytes;
}
static void WriteError(CGMEngine *cgm, const char *msg)
{
if (!cgm->file) return;
strcpy(gistError, msg);
p_fclose(cgm->file);
cgm->file= 0;
cgm->state= 1;
}
/* ------------------------------------------------------------------------ */
/* Gist font numbers match those used by the Apple LaserWriter (for no
particular reason). The GPLOT program (from Pittsburgh supercomputer
center) does a poor job with fonts, as does the CGTODL program on
LLNL Crays. The most important thing here is to make the Gist fonts
used by the standard Gist style sheets (e.g. work.gs), Helvetica and
Courier, be legible across these dysfunctional CGM interpreters. */
extern int cgmFontNumbers[N_CGMFONTS];
int cgmFontNumbers[N_CGMFONTS]= {
1,5,9,13, 3,7,11,15, 2,6,10,14, 4,8,12,16, 17,18,19,20 };
/* Use standard PostScript font names for now. */
extern char *cgmFontNames[N_CGMFONTS];
char *cgmFontNames[N_CGMFONTS]= {
"Courier", "Helvetica", "Times-Roman", "Symbol",
"Courier-Bold", "Helvetica-Bold", "Times-Bold", "Symbol",
"Courier-Oblique", "Helvetica-Oblique", "Times-Italic", "Symbol",
"Courier-BoldOblique", "Helvetica-BoldOblique",
"Times-BoldItalic", "Symbol",
"NewCenturySchlbk-Roman", "NewCenturySchlbk-Bold",
"NewCenturySchlbk-Italic", "NewCenturySchlbk-BoldItalic" };
static int BeginMetafile(CGMEngine *cgm)
{
p_file *file;
int i;
long lcmnd, lpart, len, lfont[N_CGMFONTS], lfonts;
char description[88];
Octet *descriptor, *now, *font;
short value[3];
time_t t= time((time_t *)0);
/* ctime returns 26 chars, e.g.- "Sun Jan 3 15:14:13 1988\n" */
char *st= (t==-1)? "\n" : ctime(&t);
if (!cgm || cgm->state) return 1;
lcmnd= cgm->e.name? strlen(cgm->e.name) : 0;
if (lcmnd>254) lcmnd= 254;
cgm->e.name[lcmnd]= '\0';
strcpy(description, "Gist; ");
strcpy(description+7, st? st : "\n");
strcpy(description+31, "; For: ");
strncat(description, p_getuser(), 50L);
lpart= strlen(description);
lfonts= 0;
for (i=0 ; i<N_CGMFONTS ; i++)
lfonts+= lfont[i]= strlen(cgmFontNames[i]);
lfonts+= N_CGMFONTS; /* allow for Pascalification... */
len= (lcmnd<31? 2 : 4) + lcmnd+1;
if (len&1) len++;
len+= 4;
len+= 4 + lpart+1;
if (len&1) len++;
len+= 6*4;
len+= 8;
len+= 784;
len+= 4 + lfonts;
if (len&1) len++;
descriptor= (Octet *)p_malloc(len+2);
if (!descriptor) {
strcpy(gistError, "memory manager failed in BeginMetafile");
cgm->state= 1; /* Metafile closed */
return 1;
}
file= CREATE_CGM(cgm->filename);
if (!file) {
strcpy(gistError, "unable to create CGM output");
cgm->state= 1; /* Metafile closed */
p_free(descriptor);
return 1;
}
now= descriptor;
now= FormCommand(now, 0, 1, lcmnd+1, &len); /* BEGIN METAFILE */
now= Pascalify(now, cgm->e.name, lcmnd, 1);
now= FormCommand(now, 1, 1, 2L, &len); /* METAFILE VERSION */
value[0]= 1;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 2, lpart+1, &len); /* METAFILE DESCRIPTION */
now= Pascalify(now, description, lpart, 1);
now= FormCommand(now, 1, 3, 2L, &len); /* VDC TYPE */
value[0]= 0;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 4, 2L, &len); /* INTEGER PRECISION */
value[0]= 16;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 6, 2L, &len); /* INDEX PRECISION */
value[0]= 16;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 7, 2L, &len); /* COLOR PRECISION */
value[0]= 8;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 8, 2L, &len); /* COLOR INDEX PRECISION */
value[0]= 8;
now= FormWords(now, value, 1L);
now= FormCommand(now, 1, 9, 1L, &len); /* MAXIMUM COLOR INDEX */
*now++= 255;
*now++= 0;
now= FormCommand(now, 1, 11, 6L, &len); /* METAFILE ELEMENT LIST */
value[0]= 1;
value[1]= -1;
value[2]= 1; /* drawing plus control set */
now= FormWords(now, value, 3L);
now= FormCommand(now, 1, 12, 780L, &len);
/* METAFILE DEFAULTS REPLACEMENT */
now= FormCommand(now, 3, 6, 2L, &len); /* CLIP INDICATOR */
value[0]= 0; /* clip off by default */
now= FormWords(now, value, 1L);
now= FormCommand(now, 5, 22, 2L, &len); /* INTERIOR STYLE */
value[0]= 1; /* solid */
now= FormWords(now, value, 1L);
now= FormCommand(now, 5, 11, 2L, &len); /* TEXT PRECISION */
value[0]= 2; /* stroke */
now= FormWords(now, value, 1L);
/* CGM color 0 is (by default) the background color, while color 1 is
the foreground color. Define the 8 standard Gist colors as
colors 2-9. This leaves colors 10-255 up for grabs. The index
into the Gist palette will therefore be incremented by 10 before
being written into a CGM file. Initialize these colors to a
240 color gray scale, so that even without dumping the color table,
we get sensible results. */
now= FormCommand(now, 5, 34, 763L, &len); /* COLOR TABLE */
*now++= 2; /* begin at color index 2 (BLACK_COLOR, which is -3) */
now[0]= now[1]= now[2]= 0; /* black */
now+= 3;
now[0]= now[1]= now[2]= 255; /* white */
now+= 3;
now[0]= 255; now[1]= now[2]= 0; /* red */
now+= 3;
now[1]= 255; now[0]= now[2]= 0; /* green */
now+= 3;
now[2]= 255; now[0]= now[1]= 0; /* blue */
now+= 3;
now[0]= 0; now[1]= now[2]= 255; /* cyan */
now+= 3;
now[1]= 0; now[0]= now[2]= 255; /* magenta */
now+= 3;
now[2]= 0; now[0]= now[1]= 255; /* yellow */
now+= 3;
for (i=0 ; i<240 ; i++) {
now[0]= now[1]= now[2]= i + ((i+8)/15); /* 240 grays */
now+= 3;
}
for (i=0 ; i<6 ; i++) {
now[0]= now[1]= now[2]= 255; /* extra whites at top */
now+= 3;
}
*now++= 0; /* Fill up to even number of bytes */
now= FormCommand(now, 1, 13, lfonts, &len); /* FONT LIST */
font= now;
for (i=0 ; i<N_CGMFONTS ; i++)
now= Pascalify(now, cgmFontNames[i], lfont[i], 0);
if ((now-font)&1) *now++= 0; /* even number of bytes */
cgm->file= file;
if (WriteB(file, descriptor, now-descriptor)) {
WriteError(cgm, "write to CGM failed in BeginMetafile");
p_free(descriptor);
return 1;
}
p_free(descriptor);
cgm->state= 2; /* Metafile Description (actually finished) */
return 0;
}
/* ------------------------------------------------------------------------ */
static void SetPageDefaults(CGMEngine *cgmEngine)
{
int i;
/* Set current state to match state set by GI procedure in ps.ps */
cgmEngine->curClip= 0;
cgmEngine->clipBox.xmin= cgmEngine->clipBox.xmax=
cgmEngine->clipBox.ymin= cgmEngine->clipBox.ymax= 0.0;
for (i=0 ; i<5 ; i++)
cgmEngine->curColor[i]= FG_COLOR; /* CGM color index #1 */
cgmEngine->curType= L_SOLID;
cgmEngine->curWidth= 1.0;
cgmEngine->curMark= M_ASTERISK;
cgmEngine->curSize= 1.0;
cgmEngine->curFont= T_TIMES; /* CGM font #1 */
cgmEngine->curHeight= 0.0; /* Default is unusable (1 VDC) */
cgmEngine->curAlignH= TH_NORMAL;
cgmEngine->curAlignV= TV_NORMAL;
cgmEngine->curPath= TX_RIGHT;
cgmEngine->curOpaque= 0;
cgmEngine->curEtype= L_NONE;
cgmEngine->curEwidth= 1.0;
}
static void SetCGMTransform(GpTransform *toPixels, int landscape,
GpReal scale)
{
toPixels->viewport.xmin= toPixels->viewport.ymin= 0.0;
if (landscape) {
toPixels->viewport.xmax= 1.033461; /* 11 in */
toPixels->viewport.ymax= 0.798584; /* 8.5 in */
} else {
toPixels->viewport.xmax= 0.798584; /* 8.5 in */
toPixels->viewport.ymax= 1.033461; /* 11 in */
}
toPixels->window.xmin= toPixels->window.ymin= 0.0;
toPixels->window.xmax= toPixels->viewport.xmax*scale;
toPixels->window.ymax= toPixels->viewport.ymax*scale;
}
static int BeginPage(CGMEngine *cgmEngine)
{
p_file *file= cgmEngine->file;
long len, lpage;
Octet command[44], *now;
char page[30];
short xy[4];
if (!cgmEngine) return 1;
if (!file) {
BeginMetafile(cgmEngine);
file= cgmEngine->file;
if (!file) return 1;
}
if (cgmEngine->state!=2 && cgmEngine->state!=5) {
WriteError(cgmEngine, "CGM driver bug found in BeginPage");
return 1;
}
/* A change in color table can take place only at the beginning
of a page. ChangePalette strobes the palette in the Engine base
class part into the cgmEngine palette. */
cgmEngine->nColors= 0; /* reset to mono mode */
ChangePalette((Engine *)cgmEngine);
/* Set transform viewport to reflect current page orientation */
if (cgmEngine->landscape != cgmEngine->e.landscape) {
SetCGMTransform(&cgmEngine->e.transform, cgmEngine->e.landscape,
cgmEngine->scale);
cgmEngine->landscape= cgmEngine->e.landscape;
}
now= command;
sprintf(page, "Page %d", cgmEngine->currentPage);
lpage= strlen(page);
now= FormCommand(now, 0, 3, lpage+1, &len); /* BEGIN PICTURE */
now= Pascalify(now, page, lpage, 1);
now= FormCommand(now, 2, 6, 8L, &len); /* VDC EXTENT */
xy[0]= (short)cgmEngine->e.transform.window.xmin;
xy[1]= (short)cgmEngine->e.transform.window.ymin;
xy[2]= (short)cgmEngine->e.transform.window.xmax;
xy[3]= (short)cgmEngine->e.transform.window.ymax;
now= FormWords(now, xy, 4L);
now= FormCommand(now, 0, 4, 0L, &len); /* BEGIN PICTURE BODY */
if (WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in BeginPage");
return 1;
}
if (cgmEngine->e.colorMode && cgmEngine->e.palette &&
cgmEngine->e.nColors>0) {
int i, nColors= cgmEngine->e.nColors;
GpColorCell *palette= cgmEngine->e.palette;
Octet *col;
if (nColors>246) nColors= 246;
col= (Octet *)p_malloc(3*nColors+6);
if (!col) {
WriteError(cgmEngine, "memory manager failed in CGM BeginPage");
return 1;
}
now= col;
now= FormCommand(now, 5, 34, 3*nColors+1, &len); /* COLOR TABLE */
*now++= 10; /* begin at color index 10 */
for (i=0 ; i<nColors ; i++) {
*now++= (Octet)(P_R(palette[i]));
*now++= (Octet)(P_G(palette[i]));
*now++= (Octet)(P_B(palette[i]));
}
if (!(nColors&1)) *now++= 0;
if (WriteB(file, col, now-col)) {
p_free(col);
WriteError(cgmEngine, "write CT to CGM failed in BeginPage");
return 1;
}
p_free(col);
now= command;
cgmEngine->colorMode= 1; /* color table has been written */
cgmEngine->nColors= nColors;
} else {
cgmEngine->colorMode= 0; /* NO color table exists on this page */
/* But, if there is a palette, still want to use grays */
if (cgmEngine->e.palette && cgmEngine->e.nColors>0)
cgmEngine->nColors= cgmEngine->e.nColors;
}
cgmEngine->state= 4; /* Picture open */
cgmEngine->e.marked= 1;
return 0;
}
static void EndClip(CGMEngine *cgmEngine)
{
if (!cgmEngine || cgmEngine->state!=4) return;
if (cgmEngine->curClip) {
p_file *file= cgmEngine->file;
long len;
Octet command[4];
short clip= 0;
FormCommand(command, 3, 6, 2L, &len); /* CLIP INDICATOR */
FormWords(command+2, &clip, 1L);
if (WriteB(file, command, 4L))
WriteError(cgmEngine, "write to CGM failed in EndClip");
cgmEngine->curClip= 0;
}
}
static void BeginClip(CGMEngine *cgmEngine, GpTransform *trans)
{
GpPoint *points;
GpBox *port= &trans->viewport;
GpBox *box= &cgmEngine->clipBox;
Octet command[16], *now;
p_file *file;
long len;
if (!cgmEngine || cgmEngine->state!=4) return;
file= cgmEngine->file;
now= command;
if (port->xmin!=box->xmin || port->xmax!=box->xmax ||
port->ymin!=box->ymin || port->ymax!=box->ymax) {
GpReal x[2], y[2];
x[0]= trans->window.xmin; x[1]= trans->window.xmax;
y[0]= trans->window.ymin; y[1]= trans->window.ymax;
GpIntPoints(&cgmEngine->e.map, 3, 2, x, y, &points);
if (points[0].x > points[1].x) {
GpReal xt= points[0].x;
points[0].x= points[1].x;
points[1].x= (short)xt;
}
if (points[0].y > points[1].y) {
GpReal yt= points[0].y;
points[0].y= points[1].y;
points[1].y= (short)yt;
}
now= FormCommand(now, 3, 5, 8L, &len); /* CLIP RECTANGLE */
/* Note-- this cast from GpPoint* to short* works on all machines
that I know about, but may fail someday... */
now= FormWords(now, (short *)points, 4L);
*box= *port;
}
if (!cgmEngine->curClip) {
short clip= 1;
now= FormCommand(now, 3, 6, 2L, &len); /* CLIP INDICATOR */
now= FormWords(now, &clip, 1L);
cgmEngine->curClip= 1;
} else if (now==command) {
return;
}
if (WriteB(file, command, now-command))
WriteError(cgmEngine, "write to CGM failed in BeginClip");
}
static int EndPage(CGMEngine *cgmEngine)
{
p_file *file;
long len;
Octet command[4];
unsigned long pos = 0;
int oops = 0;
if (!cgmEngine) return 1;
if (cgmEngine->state!=4) {
if (cgmEngine->state==2 || cgmEngine->state==5) return 0;
WriteError(cgmEngine, "CGM driver bug found in EndPage");
return 1;
}
file= cgmEngine->file;
FormCommand(command, 0, 5, 0L, &len); /* END PICTURE */
FormCommand(command+2, 0, 2, 0L, &len); /* END METAFILE */
/* Write END PICTURE END METAFILE, then backspace over
END METAFILE. Combined with the p_fflush, this assures
that a legal metafile is present on disk after EndPage. */
oops = WriteB(file, command, 4L);
if (!oops) pos = p_ftell(file), oops = (pos==-1L || p_fseek(file, pos-2));
if (oops) {
WriteError(cgmEngine, "write to CGM failed in EndPage");
return 1;
}
if (p_ftell(file) < (unsigned long)cgmEngine->fileSize) {
/* Just flush buffers if file still reasonable size */
p_fflush(file);
cgmEngine->state= 5; /* Picture closed */
} else {
/* File is too big, start a new one */
p_fclose(file);
cgmEngine->file= 0;
cgmEngine->IncrementName(cgmEngine->filename);
cgmEngine->state= 0; /* No state yet */
}
cgmEngine->e.marked= 0;
cgmEngine->currentPage++;
SetPageDefaults(cgmEngine);
return 0;
}
/* ------------------------------------------------------------------------ */
static int ChangePalette(Engine *engine)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
int nColors= engine->nColors;
if (nColors<=0 || !engine->palette) {
/* new color table is void-- don't allow indexed colors */
cgmEngine->colorMode= 0;
cgmEngine->nColors= 0;
if (nColors>246) engine->nColors= 246;
} else {
/* remember current color mode, then set to mono mode until
new color table can be written in BeginPage */
cgmEngine->colorMode= 0;
cgmEngine->nColors= 0; /* don't index into table before BeginPage */
}
engine->colorChange= 0;
return 246;
}
/* ------------------------------------------------------------------------ */
static void Kill(Engine *engine)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
if (engine->marked) EndPage(cgmEngine);
if (cgmEngine->file) {
/* Write directory of page locations */
p_fclose(cgmEngine->file);
}
GpDelEngine(engine);
}
static int Clear(Engine *engine, int always)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
if (always || engine->marked) EndPage(cgmEngine);
engine->marked= 0;
return 0;
}
static int Flush(Engine *engine)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
if (cgmEngine->file) p_fflush(cgmEngine->file);
return 0;
}
/* ------------------------------------------------------------------------ */
static int cgmColorID[]= { 4, 8, 14, 23, 29 };
static int SetupColor(CGMEngine *cgmEngine, unsigned long color, int which)
{
int nColors= cgmEngine->nColors;
p_file *file= cgmEngine->file;
long len;
short c = 0;
Octet command[4];
if (!cgmEngine->e.marked && BeginPage(cgmEngine)) return 1;
if (color==cgmEngine->curColor[which]) return 0;
if (color<240UL) {
/* "CI index C"-- "CI" omitted if current color is indexed */
GpColorCell *palette= cgmEngine->e.palette;
if (nColors>0) {
if (color>=(unsigned long)nColors) color= nColors-1;
if (cgmEngine->colorMode) c= (short)color; /* page has color table */
else { /* this is a 240 gray mono page */
c= (short)((P_R(palette[color]) +
P_G(palette[color]) + P_B(palette[color]))/3);
c-= (c+8)/16;
}
} else {
if (color>255) color= 255;
c= (short)color; /* No palette ==> no color table on page */
}
c+= 10;
} else if (color<256UL) {
/* standard color command FG, RED, etc. */
if (color<YELLOW_COLOR) color= FG_COLOR;
c= (short)(255UL-color); /* standard colors are 0 thru 9 */
} else {
c = (short)(255UL-FG_COLOR);
}
FormCommand(command, 5, cgmColorID[which], 1L, &len);
/* LINE COLOR (0) MARKER COLOR (1) TEXT COLOR (2) FILL COLOR (3) */
command[2]= (Octet)c;
command[3]= 0;
if (WriteB(file, command, 4L)) {
WriteError(cgmEngine, "write to CGM failed in SetupColor");
return 1;
}
cgmEngine->curColor[which]= color;
return 0;
}
static int SetupLine(CGMEngine *cgmEngine)
{
p_file *file= cgmEngine->file;
long len;
Octet command[14], *now;
if (cgmEngine->state!=4) return 1;
if (SetupColor(cgmEngine, gistA.l.color, 0)) return 1;
now= command;
if (cgmEngine->curType!=gistA.l.type) {
short ltype= (short)gistA.l.type;
if (ltype==L_NONE) return 1;
if (ltype<=0 || ltype>L_DASHDOTDOT) ltype= L_SOLID;
now= FormCommand(now, 5, 2, 2L, &len); /* LINE TYPE */
now= FormWords(now, <ype, 1L);
cgmEngine->curType= gistA.l.type;
}
if (cgmEngine->curWidth!=gistA.l.width) {
short lwidth[2];
now= FormCommand(now, 5, 3, 4L, &len); /* LINE WIDTH */
FormReal(lwidth, gistA.l.width);
now= FormWords(now, lwidth, 2L);
cgmEngine->curWidth= gistA.l.width;
}
if (cgmEngine->curOpaque!=0 && cgmEngine->curType!=L_SOLID) {
short trans= 0;
now= FormCommand(now, 3, 4, 2L, &len); /* TRANSPARENCY */
now= FormWords(now, &trans, 1L);
cgmEngine->curOpaque= trans;
}
if (now!=command && WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in SetupLine");
return 1;
}
return 0;
}
static int SetupEdge(CGMEngine *cgmEngine)
{
p_file *file= cgmEngine->file;
long len;
Octet command[14], *now;
if (cgmEngine->state!=4) return 1;
if (SetupColor(cgmEngine, gistA.e.color, 4)) return 1;
now= command;
if (cgmEngine->curEtype!=gistA.e.type) {
short ltype= (short)gistA.e.type;
short visible= (ltype!=L_NONE);
int change= visible^(cgmEngine->curEtype!=L_NONE);
if (visible) {
if (ltype<=0 || ltype>L_DASHDOTDOT) ltype= L_SOLID;
now= FormCommand(now, 5, 27, 2L, &len); /* EDGE TYPE */
now= FormWords(now, <ype, 1L);
}
if (change) {
now= FormCommand(now, 5, 30, 2L, &len); /* EDGE VISIBILITY */
now= FormWords(now, &visible, 1L);
}
cgmEngine->curEtype= gistA.e.type;
}
if (cgmEngine->curEwidth!=gistA.e.width) {
short lwidth[2];
now= FormCommand(now, 5, 28, 4L, &len); /* EDGE WIDTH */
FormReal(lwidth, gistA.e.width);
now= FormWords(now, lwidth, 2L);
cgmEngine->curEwidth= gistA.e.width;
}
if (cgmEngine->curOpaque!=0 && cgmEngine->curEtype!=L_SOLID) {
short trans= 0;
now= FormCommand(now, 3, 4, 2L, &len); /* TRANSPARENCY */
now= FormWords(now, &trans, 1L);
cgmEngine->curOpaque= trans;
}
if (now!=command && WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in SetupEdge");
return 1;
}
return 0;
}
static void CheckClip(CGMEngine *cgmEngine)
{
if (!cgmEngine->e.marked) BeginPage(cgmEngine);
if (gistClip) BeginClip(cgmEngine, &gistT);
else if (cgmEngine->curClip) EndClip(cgmEngine);
}
static int DrawLines(Engine *engine, long n, const GpReal *px,
const GpReal *py, int closed, int smooth)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
GpXYMap *map= &engine->map;
long maxPoints= 4050, nPoints;
int firstPass= 1;
GpPoint firstPoint, *points;
long len;
Octet command[10], *now;
CheckClip(cgmEngine);
if (n<1) return 0;
if (SetupLine(cgmEngine)) return 1;
file= cgmEngine->file;
while ((nPoints=
GpIntPoints(map, maxPoints, n, px, py, &points))) {
if (closed) {
if (firstPass) {
firstPoint= points[0];
firstPass= 0;
}
if (n==nPoints) {
n++;
points[nPoints++]= firstPoint;
}
}
if (smooth) {
/* There may be a GDP for Bezier curves, but I don't know how
standard it is. This is sure to be non-portable, but also
sure to produces SOME acceptable output (identical, in fact,
to what the Gist X engine displays). */
short value= smooth; /* identifier */
now= FormCommand(command, 7, 2, 3L, &len); /* APPLICATION DATA */
now= FormWords(now, &value, 1L);
now= Pascalify(now, "", 0, 1);
} else {
now= command;
}
now= FormCommand(now, 4, 1, 4*nPoints, &len); /* POLYLINE */
if (WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in DrawLines");
return 1;
}
/* The cast from GpPoint* to short* works on all machines I know of,
but may fail someday... Note that maxPoints is set low enough
that multiple partitions will never be required. */
CGM_WORD_ORDER((short *)points, nPoints<<1);
if (WriteB(file, (Octet *)points, nPoints<<2)) {
WriteError(cgmEngine, "write to CGM failed in DrawLines");
return 1;
}
if (n==nPoints) break;
n-= nPoints;
px+= nPoints;
py+= nPoints;
}
return 0;
}
/* ------------------------------------------------------------------------ */
static int SetupMarker(CGMEngine *cgmEngine)
{
p_file *file= cgmEngine->file;
long len;
Octet command[14], *now;
if (cgmEngine->state!=4) return 1;
if (SetupColor(cgmEngine, gistA.m.color, 1)) return 1;
now= command;
if (cgmEngine->curMark!=gistA.m.type) {
short mtype= (short)gistA.m.type;
now= FormCommand(now, 5, 6, 2L, &len); /* MARKER TYPE */
now= FormWords(now, &mtype, 1L);
cgmEngine->curMark= gistA.m.type;
}
if (cgmEngine->curSize!=gistA.m.size) {
short msize[2];
now= FormCommand(now, 5, 7, 4L, &len); /* MARKER SIZE */
FormReal(msize, gistA.m.size);
now= FormWords(now, msize, 2L);
cgmEngine->curSize= gistA.m.size;
}
if (cgmEngine->curOpaque!=0) {
short trans= 0;
now= FormCommand(now, 3, 4, 2L, &len); /* TRANSPARENCY */
now= FormWords(now, &trans, 1L);
cgmEngine->curOpaque= trans;
}
if (now!=command && WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in SetupMarker");
return 1;
}
return 0;
}
static int DrawMarkers(Engine *engine, long n, const GpReal *px,
const GpReal *py)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
GpXYMap *map= &engine->map;
long maxPoints= 4050, nPoints;
GpPoint *points;
long len;
Octet command[4], *now;
if (n<1 || gistA.m.type<=0) return 0;
CheckClip(cgmEngine);
if (SetupMarker(cgmEngine)) return 1;
file= cgmEngine->file;
while ((nPoints=
GpIntPoints(map, maxPoints, n, px, py, &points))) {
now= FormCommand(command, 4, 3, 4*nPoints, &len); /* POLYMARKER */
if (WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in DrawMarkers");
return 1;
}
/* The cast from GpPoint* to short* works on all machines I know of,
but may fail someday... Note that maxPoints is set low enough
that multiple partitions will never be required. */
CGM_WORD_ORDER((short *)points, nPoints<<1);
if (WriteB(file, (Octet *)points, nPoints<<2)) {
WriteError(cgmEngine, "write to CGM failed in DrawMarkers");
return 1;
}
if (n==nPoints) break;
n-= nPoints;
px+= nPoints;
py+= nPoints;
}
return 0;
}
/* ------------------------------------------------------------------------ */
static int SetupText(CGMEngine *cgmEngine)
{
p_file *file= cgmEngine->file;
int opq;
long len;
Octet command[36], *now;
int h= gistA.t.alignH, v= gistA.t.alignV;
GtGetAlignment(&gistA.t, &h, &v);
if (cgmEngine->state!=4) return 1;
if (SetupColor(cgmEngine, gistA.t.color, 2)) return 1;
now= command;
if (cgmEngine->curFont!=gistA.t.font) {
int fn= (gistA.t.font>=0 && gistA.t.font<N_CGMFONTS)?
gistA.t.font : 0;
short font= (short)cgmFontNumbers[fn];
now= FormCommand(now, 5, 10, 2L, &len); /* TEXT FONT INDEX */
now= FormWords(now, &font, 1L);
cgmEngine->curFont= gistA.t.font;
}
if (cgmEngine->curHeight!=gistA.t.height) {
/* -----------------------WARNING----------------------------- */
/* The CGM standard specifies the character height, that is, the
distance from the baseline to the capline of a line of text.
This is different from the standard typestters' point size,
which is the minimum vertical distance between lines of text
required for good legibility. The Gist "height" text attribute
follows the ordinary definition of point size, as in PostScript,
so here it must be shrunk by an arbitrary factor (0.8) to
allow for the messed up CGM definition. */
/* -----------------------WARNING----------------------------- */
short ch= (short)(0.8*gistA.t.height*cgmEngine->scale+0.5);
now= FormCommand(now, 5, 15, 2L, &len); /* CHARACTER HEIGHT */
now= FormWords(now, &ch, 1L);
cgmEngine->curHeight= gistA.t.height;
}
if (cgmEngine->curAlignH!=h || cgmEngine->curAlignV!=v) {
short align[6];
now= FormCommand(now, 5, 18, 12L, &len); /* TEXT ALIGNMENT */
align[0]= (short)h;
align[1]= (short)v;
align[2]= align[3]= align[4]= align[5]= 0;
now= FormWords(now, align, 6L);
cgmEngine->curAlignH= h;
cgmEngine->curAlignV= v;
}
if (cgmEngine->curPath!=gistA.t.orient) {
short orient[4];
now= FormCommand(now, 5, 16, 8L, &len); /* CHARACTER ORIENTATION */
if (gistA.t.orient==TX_RIGHT) {
orient[0]= 0;
orient[1]= 1;
orient[2]= 1;
orient[3]= 0;
} else if (gistA.t.orient==TX_UP) {
orient[0]= -1;
orient[1]= 0;
orient[2]= 0;
orient[3]= 1;
} else if (gistA.t.orient==TX_LEFT) {
orient[0]= 0;
orient[1]= -1;
orient[2]= -1;
orient[3]= 0;
} else {
orient[0]= 1;
orient[1]= 0;
orient[2]= 0;
orient[3]= -1;
}
now= FormWords(now, orient, 4L);
cgmEngine->curPath= gistA.t.orient;
}
opq= gistA.t.opaque;
/* if (gistA.t.orient!=TX_RIGHT) opq= 1; let this be X only limitation */
if (cgmEngine->curOpaque != (opq!=0)) {
short trans= (short)(opq==0);
now= FormCommand(now, 3, 4, 2L, &len); /* TRANSPARENCY */
now= FormWords(now, &trans, 1L);
cgmEngine->curOpaque= !trans;
}
if (now!=command && WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in SetupText");
return 1;
}
return 0;
}
static Octet cgmText[264]; /* header + point + flag + 254 pascal chars */
static int DrwText(Engine *engine, GpReal x0, GpReal y0, const char *text)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
int nlines;
GpReal width, height, lineHeight;
GpXYMap *map= &engine->map;
GpBox *wind= &engine->transform.window;
GpReal xmin, xmax, ymin, ymax;
int count, alignV /*, alignH*/;
short xyf[3];
Octet *now;
long len;
const char *t;
/* do not try to output zero size text */
if ((short)(0.8*gistA.t.height*cgmEngine->scale+0.5) <= 0) return 0;
CheckClip(cgmEngine);
if (SetupText(cgmEngine)) return 1;
file= cgmEngine->file;
/*alignH= cgmEngine->curAlignH;*/
alignV= cgmEngine->curAlignV;
/* handle multi-line strings */
nlines= GtTextShape(text, &gistA.t, (WidthFunction)0, &width);
lineHeight= gistA.t.height * cgmEngine->scale;
width*= 0.6*lineHeight;
height= lineHeight*(GpReal)nlines;
/* Compute text position in VDC coordinates */
x0= map->x.scale*x0+map->x.offset;
y0= map->y.scale*y0+map->y.offset;
/* Reject if and only if the specified point is off of the current
page by more than the approximate size of the text. Note that
the entire block of text is either accepted or rejected --
the CGM interpreter does the clipping. */
if (wind->xmax>wind->xmin) { xmin= wind->xmin; xmax= wind->xmax; }
else { xmin= wind->xmax; xmax= wind->xmin; }
if (wind->ymax>wind->ymin) { ymin= wind->ymin; ymax= wind->ymax; }
else { ymin= wind->ymax; ymax= wind->ymin; }
if (gistA.t.orient==TX_RIGHT || gistA.t.orient==TX_LEFT) {
if (x0<xmin-width || x0>xmax+width ||
y0<ymin-height || y0>ymax+height) return 0;
} else {
if (x0<xmin-height || x0>xmax+height ||
y0<ymin-width || y0>ymax+width) return 0;
}
/* Adjust y0 to represent topmost line */
if (nlines > 1) {
if (gistA.t.orient==TX_RIGHT) {
if (alignV==TV_BASE || alignV==TV_BOTTOM) y0+= height-lineHeight;
if (alignV==TV_HALF) y0+= 0.5*(height-lineHeight);
} else if (gistA.t.orient==TX_LEFT) {
if (alignV==TV_BASE || alignV==TV_BOTTOM) y0-= height-lineHeight;
if (alignV==TV_HALF) y0-= 0.5*(height-lineHeight);
} else if (gistA.t.orient==TX_UP) {
if (alignV==TV_BASE || alignV==TV_BOTTOM) x0-= height-lineHeight;
if (alignV==TV_HALF) x0-= 0.5*(height-lineHeight);
} else {
if (alignV==TV_BASE || alignV==TV_BOTTOM) x0+= height-lineHeight;
if (alignV==TV_HALF) x0+= 0.5*(height-lineHeight);
}
}
xyf[2]= 1; /* never use append text, all strings final */
while ((t= GtNextLine(text, &count, TX_RIGHT))) {
if (count>0) {
now= FormCommand(cgmText, 4, 4, (long)count+7, &len); /* TEXT */
xyf[0]= (short)x0;
xyf[1]= (short)y0;
now= FormWords(now, xyf, 3L);
now= Pascalify(now, t, (long)count, 1);
if (WriteB(file, cgmText, now-cgmText)) {
WriteError(cgmEngine, "write to CGM failed in DrwText");
return 1;
}
}
text= t+count;
if (gistA.t.orient==TX_RIGHT) y0-= lineHeight;
else if (gistA.t.orient==TX_UP) x0+= lineHeight;
else if (gistA.t.orient==TX_LEFT) y0+= lineHeight;
else x0-= lineHeight;
}
return 0;
}
/* ------------------------------------------------------------------------ */
static int DrawFill(Engine *engine, long n, const GpReal *px,
const GpReal *py)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
GpXYMap *map= &engine->map;
long maxPoints= 4050, nPoints;
GpPoint *points;
int value= 0;
long len;
Octet command[4], *now;
/* For now, only solid interior style supported */
if (n<3) return 0;
CheckClip(cgmEngine);
if (SetupColor(cgmEngine, gistA.f.color, 3) ||
SetupEdge(cgmEngine)) return 1;
file= cgmEngine->file;
while ((nPoints=
GpIntPoints(map, maxPoints, n, px, py, &points))) {
now= FormCommand(command, 4, 7, 4*nPoints, &len); /* POLYGON */
if (WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in DrawFill");
return 1;
}
/* The cast from GpPoint* to short* works on all machines I know of,
but may fail someday... Note that maxPoints is set low enough
that multiple partitions will never be required. */
CGM_WORD_ORDER((short *)points, nPoints<<1);
if (WriteB(file, (Octet *)points, nPoints<<2)) {
WriteError(cgmEngine, "write to CGM failed in DrawFill");
return 1;
}
if (n==nPoints) break;
n-= nPoints;
px+= nPoints;
py+= nPoints;
value= 1; /* Polygons with >4050 sides won't be filled correctly */
}
return value;
}
/* ------------------------------------------------------------------------ */
static int DrawCells(Engine *engine, GpReal px, GpReal py, GpReal qx,
GpReal qy, long width, long height, long nColumns,
const GpColor *colors)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
GpXYMap *map= &cgmEngine->e.map;
int nColors= cgmEngine->nColors;
GpColorCell *palette;
short pqrwhd[10];
long i, j, off, nCells, lPart;
int pad, color, colorMode;
Octet *buffer, *now;
if (!cgmEngine->e.marked && BeginPage(cgmEngine)) return 1;
CheckClip(cgmEngine);
file= cgmEngine->file;
/* Transform corner coordinates, clipping and adjusting width,
height, nColumns, and colors as necessary. */
width= GpClipCells(&map->x, &px, &qx,
gistT.window.xmin, gistT.window.xmax, width, &off);
colors+= off;
height= GpClipCells(&map->y, &py, &qy,
gistT.window.ymin, gistT.window.ymax, height, &off);
colors+= nColumns*off;
if (width>0x7ffe) width= 0x7ffe;
if (height>0x7ffe) height= 0x7ffe;
pad= ((width & 1)!=0);
if (width<=0 || height<=0) return 0;
nCells= (pad? width+1 : width)*height;
lPart= nCells+20;
if (lPart>MAX_PARTITION) lPart= MAX_PARTITION;
if (lPart & 1) lPart++;
buffer= (Octet *)p_malloc(lPart+4);
if (!buffer) {
WriteError(cgmEngine, "memory manager failed in CGM DrawCells");
return 1;
}
now= buffer;
now= FormCommand(now, 4, 9, lPart, &lPart); /* CELL ARRAY */
pqrwhd[0]= (short)px;
pqrwhd[1]= (short)py;
pqrwhd[2]= (short)qx;
pqrwhd[3]= (short)qy;
pqrwhd[4]= (short)qx;
pqrwhd[5]= (short)py;
pqrwhd[6]= (short)width; /* Note limitation to < 32767-by-32767 */
pqrwhd[7]= (short)height;
pqrwhd[8]= 0; /* default depth is color index precision */
pqrwhd[9]= 1; /* NOT run length encoded */
now= FormWords(now, pqrwhd, 10L);
if (nColors>0) {
/* Image value will be either index or palette gray */
colorMode= cgmEngine->colorMode;
if (colorMode) {
palette= 0; /* palette already written */
} else {
palette= cgmEngine->e.palette; /* must lookup gray level now */
}
} else {
/* Must assume image varies over maximum possible range */
colorMode= 1; /* That is, use index without trying palette lookup */
palette= 0;
}
i= j= 0;
lPart-= 20; /* first partition has 20 octets of pqrwhd */
for (;;) {
while (lPart--) {
if (i>=width) {
if (pad) {
*now++= 0;
nCells--;
if (!lPart--) break;
}
i= 0;
j+= nColumns;
}
if (gistA.rgb) {
color = (colors[3*(i+j)]+colors[3*(i+j)+1]+colors[3*(i+j)+2])/3;
color = (color*nColors)>>8;
} else {
color= colors[i+j];
}
i++;
if (color>=nColors && nColors>0) color= nColors-1;
if (!colorMode) { /* this is default 240 gray mono page */
color= (P_R(palette[color]) +
P_G(palette[color]) + P_B(palette[color]))/3;
color-= (color+8)/16;
}
*now++= 10+color; /* skip the 10 standard colors */
nCells--;
}
if (WriteB(file, buffer, now-buffer)) {
p_free(buffer);
WriteError(cgmEngine, "write to CGM failed in DrawCells");
return 1;
}
if (nCells<=0) break;
now= NextPartition(buffer, nCells, &lPart);
}
p_free(buffer);
return 0;
}
/* ------------------------------------------------------------------------ */
static int DrawDisjoint(Engine *engine, long n, const GpReal *px,
const GpReal *py, const GpReal *qx, const GpReal *qy)
{
CGMEngine *cgmEngine= (CGMEngine *)engine;
p_file *file;
GpXYMap *map= &engine->map;
long maxSegs= 2025, nSegs;
GpSegment *segs;
long len;
Octet command[4], *now;
CheckClip(cgmEngine);
if (n<1) return 0;
if (SetupLine(cgmEngine)) return 1;
file= cgmEngine->file;
while ((nSegs=
GpIntSegs(map, maxSegs, n, px, py, qx, qy, &segs))) {
now= FormCommand(command, 4, 2, 8*nSegs, &len); /* DISJOINT POLYLINE */
if (WriteB(file, command, now-command)) {
WriteError(cgmEngine, "write to CGM failed in DrawDisjoint");
return 1;
}
/* The cast from GpSegment* to short* works on all machines I know of,
but may fail someday... Note that maxSegs is set low enough
that multiple partitions will never be required. */
CGM_WORD_ORDER((short *)segs, nSegs<<2);
if (WriteB(file, (Octet *)segs, nSegs<<3)) {
WriteError(cgmEngine, "write to CGM failed in DrawDisjoint");
return 1;
}
if (n==nSegs) break;
n-= nSegs;
px+= nSegs;
py+= nSegs;
qx+= nSegs;
qy+= nSegs;
}
return 0;
}
/* ------------------------------------------------------------------------ */
static void IncrementName(char *filename)
{
int i, len= filename? strlen(filename) : 0;
if (len>4 && (strcmp(filename+len-4, ".cgm")==0 ||
strcmp(filename+len-4, ".CGM")==0)) i= len-4;
else i= len;
while (i-- > 0) {
if (filename[i]=='9') {
filename[i]= '0';
} else {
if (filename[i]=='Z' || filename[i]>='z') filename[i]= '0';
else filename[i]++;
break;
}
}
}
/* ------------------------------------------------------------------------ */
GpReal gCGMScale= 25545.24; /* default CGM scale is 2400 dpi (round up) */
long gCGMFileSize= 100000000; /* default max file size is about 100 Meg */
Engine *GpCGMEngine(char *name, int landscape, int mode, char *file)
{
CGMEngine *cgmEngine;
long flen= file? strlen(file) : 0;
long engineSize= sizeof(CGMEngine)+flen+1;
GpTransform toPixels;
if (flen<=0) return 0;
SetCGMTransform(&toPixels, landscape, gCGMScale);
cgmEngine=
(CGMEngine *)GpNewEngine(engineSize, name, &g_cgm_on, &toPixels, landscape,
&Kill, &Clear, &Flush, &GpComposeMap,
&ChangePalette, &DrawLines, &DrawMarkers,
&DrwText, &DrawFill, &DrawCells,
&DrawDisjoint);
if (!cgmEngine) {
strcpy(gistError, "memory manager failed in GpCGMEngine");
return 0;
}
cgmEngine->filename= (char *)(cgmEngine+1);
strcpy(cgmEngine->filename, file);
cgmEngine->scale= gCGMScale;
cgmEngine->fileSize= gCGMFileSize;
cgmEngine->IncrementName= &IncrementName;
cgmEngine->file= 0;
cgmEngine->state= 0;
SetPageDefaults(cgmEngine);
cgmEngine->e.colorMode= mode;
cgmEngine->colorMode= 0;
cgmEngine->nColors= 0;
cgmEngine->landscape= landscape;
cgmEngine->currentPage= 1;
return (Engine *)cgmEngine;
}
CGMEngine *GisCGMEngine(Engine *engine)
{
return (engine && engine->on==&g_cgm_on)? (CGMEngine *)engine : 0;
}
void GcgmSetScale(CGMEngine *cgmEngine, GpReal scale)
{
GpTransform toPixels;
if (!cgmEngine || cgmEngine->state!=0) return;
cgmEngine->scale= scale;
SetCGMTransform(&toPixels, cgmEngine->landscape, scale);
}
/* ------------------------------------------------------------------------ */
|