File: genshape.c

package info (click to toggle)
transfig 1%3A3.2.5.e-4%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 9,300 kB
  • sloc: ansic: 32,666; makefile: 3,255; sh: 549; csh: 25; perl: 13
file content (1434 lines) | stat: -rwxr-xr-x 42,286 bytes parent folder | download | duplicates (7)
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
/*
 * TransFig: Facility for Translating Fig code
 * Copyright (c) 2002 by Christian Gollwitzer (auriocus)
 * Parts Copyright (c) 1999 by T. Sato
 * Parts Copyright (c) 1989-1999 by Brian V. Smith
 *
 * Any party obtaining a copy of these files is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish and/or distribute copies of
 * the Software, and to permit persons who receive copies from any such 
 * party to do so, with the only requirement being that this copyright 
 * notice remain intact.
 *
 */

/* genshape.c: LaTeX shapepar driver for fig2dev
 * derived from genmap.c
 *
 * "Christian Gollwitzer" <auriocus@web.de>
 * if an object has a comment starting with '+', it becomes
 * a filled shape. If the comment starts with '-'
 * then it is a hole. The objects are composed
 * in the order of their depths. A word following the initial
 * '+' or '-' is considered to be an identifier that groups
 * different shapes to one text segment. The order of filling these
 * segments is determined by the alphabetic sort order of the identifiers.
 * The special comments "center", "width" and "height" define
 * position and dimension.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "fig2dev.h"
#include "object.h"

static void die(const char * msg) {
 fprintf(stderr, "fig2dev(shape): %s\n", msg);
 exit(-1);
} 
#ifndef STRDUP
#define STRDUP(out,in) { out=malloc(strlen(in)+1); \
                       strcpy(out,in); }

#define STRNDUP(out, in, n) { out=malloc(n+1); \
                              strncpy(out,in, n); \
                              out[n]=0; }
#endif
 
static char*     macroname=NULL;

static int MAX_POINTS=100;
#define POINT_INC 100
static int MAX_SHAPES=20;
#define SHAPE_INC  20
static int  MAX_SHAPEGROUPS=5;
#define SHAPEGROUP_INC 5
#define bool int
#define true 1
#define false 0

static int      num_points = 0;
struct lineseg {
  int x;
  int y;
  bool intersect;
  bool intersect_hit;
  int shapenr;
  int x2;
  int y2;
};
typedef struct lineseg lineseg;

static lineseg *points;

static int num_shapes=0;
struct shape {
  bool ispositiv;
  bool inside;
  int linestart;
  int lineend;
  int depth;
  char * groupname;
};

typedef struct shape shape;
static shape *shapes;

struct shapegroup {
  int shapestart;
  int shapeend;
}; 

typedef struct shapegroup shapegroup;

static shapegroup *shapegroups;
static num_shapegroups=0;


static void alloc_arrays() {
 /* reserve initial space for data structures */
 points=malloc(sizeof(points[0])*MAX_POINTS);
 shapes=malloc(sizeof(shapes[0])*MAX_SHAPES);
 shapegroups=malloc(sizeof(shapegroups[0])*MAX_SHAPEGROUPS);
 
}

static void cleanup_memory() {
/* I'd like to have C++, because it has destructors ... */
  int i;
  free(points);
  for (i=0; i<num_shapes; i++) free(shapes[i].groupname);
  free(shapes);
  free(shapegroups);
}



/* adding shapes */
static void add_object(char *groupid, int lstart, int depth) {
  char *groupname=NULL;
  bool ispositiv=false;
  if (num_shapes>=MAX_SHAPES) {
    MAX_SHAPES+=SHAPE_INC;
    shapes=realloc(shapes, sizeof(shapes[0])*MAX_SHAPES);
   /**debugging stuff fprintf(stderr, "Inc to %d shapes\n", MAX_SHAPES); **/
  }  
  if ((groupid==NULL) || (strlen(groupid)==0)) die("Programming Error: no groupname specified");
  STRDUP(groupname, groupid+1); /*remove the first character from groupid*/
  
  switch (groupid[0]) {
    case '+': ispositiv=true;
              break;
    case '-': ispositiv=false;
              break;
    default:  die("Ill-formed shape comment - start with '+' or '-'");
              break;
  }
  
  shapes[num_shapes].ispositiv=ispositiv;
  shapes[num_shapes].groupname=groupname;
  shapes[num_shapes].linestart=lstart;
  shapes[num_shapes].depth=depth;

  shapes[num_shapes].inside=false;
}

static void finish_object(int lend) {
  shapes[num_shapes].lineend=lend;
  num_shapes++;
}

int shapecomp(const void* s1, const void * s2) {
  /* sorting the shapes in groups (same group identifier */
  return strcmp(((const shape*)s1)->groupname,((const shape*)s2)->groupname);
}

/* Adding shapegroups */
static void add_shapegroup(int shapestart, int shapeend) {
  if (num_shapegroups>=MAX_SHAPEGROUPS) {
     /* maybe later dynamically realloc */
     MAX_SHAPEGROUPS+=SHAPEGROUP_INC;
     shapegroups=realloc(shapegroups, sizeof(shapegroups[0])*MAX_SHAPEGROUPS);
  } 
  shapegroups[num_shapegroups].shapestart=shapestart;
  shapegroups[num_shapegroups].shapeend=shapeend;
  num_shapegroups++;
  
}  

/** debugging stuff **/
static void print_shapegroups() {
  int i;
  for (i=0; i<num_shapegroups; i++) 
    fprintf(stderr, "#%d from %d to %d, id %s \n", 
      i, shapegroups[i].shapestart, shapegroups[i].shapeend,
      shapes[shapegroups[i].shapestart].groupname);
}    

#define fscale 10

#define XZOOM(x)   round(mag * ((x) - llx) / fscale )
#define YZOOM(y)   round(mag * ((y) - lly) / fscale )
#define ZZOOM(z)   round(mag * ((z)/ fscale)) 

#define MIN_DIST    5  /* ignore closer points when processing polyline */

#define ARC_STEP    (M_PI / 23) /* Make a circle to be a 46-gon */

void
genshape_option(opt, optarg)
     char opt;
     char *optarg;
{
  switch (opt) {
  case 'n': /* macro name */
    STRDUP(macroname,optarg);
    break;
  case 'L': /* ignore language and magnif. */
  case 'm':
    break;
  default:
    put_msg(Err_badarg, opt, "shape");
    exit(1);
  }
}

static bool scaleset=false;
static bool centerset=false;
static double centerpos=0.0;
static double scaledim=10.0;

static bool widthref(char *comm) {
  /* return true if this object is the reference for the width */
  return (strncmp(comm, "width", 5)==0);
}

static bool heightref(char *comm) {
  /* return true if this object is the reference for the height */
  return (strncmp(comm, "height", 6)==0);
}

static bool centerref(char *comm) {
  /* return true if this object is the reference for the center */
  return (strncmp(comm, "center", 6)==0);
}


/* reads the first comment of an object, if valid */
static char *get_comment(comment) 
     F_comment  *comment;
{ char *retval=NULL;
  int maxlen=0;
  if (comment==NULL) return NULL;
  if (comment->comment==NULL) return NULL;
#ifdef COM
#undef COM
#endif
#define COM comment->comment  
  /* if we are behind this point, there is some comment to examine 
     we get everything up to the first illegal character (should be whitespace) */
  if ((COM[0]=='+') || (COM[0]=='-')) maxlen=1; /* ignore the first character, if it is + or - */
  while (isalnum(COM[maxlen])) maxlen++;
  /* look for the first non-alphanumeric character, maybe the terminating 0 */
  STRNDUP(retval, COM, maxlen);
  if (strcmp(retval,"")==0) {
    /* no shape defining comment - free mem and return */
    free(retval);
    return NULL;
  }  
  /* it is a shape defining comment
     reject everything not starting with + or - or being special comment */
  if ((retval[0]!='+') && (retval[0]!='-') && (!widthref(retval)) && (!heightref(retval)) && (!centerref(retval))) {
     fprintf(stderr, "Comment \"%s\":\n", COM);
     die("Illegal shape specification, must start with '+' or '-' or be 'center', 'width' or height'\n"
         "Use blank to start real comment. See documentation for explanation");
  }       
  return retval;

#undef COM
}

/* Adding lines */
static bool was_horizontal=false;
static void add_point(int x, int y, int x2, int y2, bool intersect) {
  if (num_points>=MAX_POINTS) {
    /* realloc the linesegments */
    MAX_POINTS+=POINT_INC;
    points=realloc(points, sizeof(points[0])*MAX_POINTS);
  }  
  /* look, if this line continues the last one horizontally */
  if (was_horizontal && (y==y2)) {
    if (points[num_points-1].x2==x && points[num_points-1].y2==y && 
        points[num_points-1].shapenr==num_shapes) {
          /* only continue the line, don't append a new one */
          points[num_points-1].x2=x2;
          points[num_points-1].y2=y2;
          return;
    }
  }   

          
  /* construct a new point */    
  points[num_points].x=x;
  points[num_points].y=y;
  
  points[num_points].x2=x2;
  
  points[num_points].y2=y2;
  
  points[num_points].intersect=intersect;
  points[num_points].intersect_hit=false;

  points[num_points].shapenr=num_shapes;
  num_points++;
  
  was_horizontal=(y==y2);
}

  

static int lastx=0;
static int lasty=0;
static int startx=0;
static int starty=0;
static bool line_start=true;


static void start_line(char *groupid, int depth) {
  add_object(groupid, num_points, depth);
  line_start=true;
}

static void line_to(int x, int y) {
  if (line_start) {
    startx=lastx=x;
    starty=lasty=y;
    line_start=false;
  } else  {
    
    add_point(lastx, lasty, x, y,false);
    
    lastx=x;
    lasty=y;
      
    }  
}

static void finish_line(void) {
    if ((lastx!=startx) || (lasty!=starty)) add_point(lastx,lasty,startx,starty,false);
    finish_object(num_points-1);
    line_start=true;
}  

/** End of data structures **/



int floatcomp(const void* y1, const void * y2) {
  if (*((float*)y1)<*((float*)y2)) return -1;
  if (*((float*)y1)>*((float*)y2)) return +1;
  return 0;
}  

/** begin of the outer layer, derived from HTML map driver **/
void
genshape_start(objects)
     F_compound *objects;
 
{  F_comment* comment=objects->comments;
  alloc_arrays(); 
  /* Arrays for holding lines, shapes and groups */
  if (comment==NULL) return;
  if (comment->comment==NULL) return;
  if (macroname==NULL) STRDUP(macroname,comment->comment);
/* if the user has set a drawing name, use this
   for the macro name, if not overridden from the commandline */    
  
 }


void
genshape_arc(a)
     F_arc      *a;
{
  char *comm;
  int cx, cy, sx, sy, ex, ey;
  double r;
  double sa, ea;
  double alpha;

  comm = get_comment(a->comments);
  if (comm!=NULL) {
    cx = XZOOM(a->center.x);
    cy = YZOOM(a->center.y);
    sx = XZOOM(a->point[0].x);
    sy = YZOOM(a->point[0].y);
    ex = XZOOM(a->point[2].x);
    ey = YZOOM(a->point[2].y);

    if (a->direction == 0) {
      sa = atan2((double)(sy - cy), (double)(sx - cx));
      ea = atan2((double)(ey - cy), (double)(ex - cx));
    } else {
      ea = atan2((double)(sy - cy), (double)(sx - cx));
      sa = atan2((double)(ey - cy), (double)(ex - cx));
    }
    if (ea < sa) ea = ea + 2 * M_PI;

    r = sqrt((double)((sx - cx) * (sx - cx)
                      + (sy - cy) * (sy - cy)));
                      
    start_line(comm, a->depth); 
    if (a->type == T_PIE_WEDGE_ARC)
      line_to(cx, cy);
    for (alpha = sa; alpha < (ea - ARC_STEP / 4); alpha += ARC_STEP) {
      line_to(round(cx + r * cos(alpha)), round(cy + r * sin(alpha)));
    }
       line_to(round(cx + r * cos(ea)), round(cy + r * sin(ea)));
    finish_line();
    free(comm);
  }
}

void
genshape_ellipse(e)
     F_ellipse  *e;
{
  char *comm;
  int x0, y0;
  double rx, ry;
  double angle, theta;

  comm = get_comment(e->comments);
  if (comm!=NULL) {
    x0 = XZOOM(e->center.x);
    y0 = YZOOM(e->center.y);
    rx = mag * e->radiuses.x / fscale;
    ry = mag * e->radiuses.y / fscale;
    angle = - e->angle;
      start_line(comm, e->depth);
      for (theta = ARC_STEP/2; theta < 2.0 * M_PI+ARC_STEP/2; theta += ARC_STEP) {
        line_to(
                round(x0 + cos(angle) * rx * cos(theta)
                      - sin(angle) * ry * sin(theta)),
                round(y0 + sin(angle) * rx * cos(theta)
                      + cos(angle) * ry * sin(theta)));
      }
      finish_line();
      free(comm);
  }
}

void
genshape_line(l)
     F_line     *l;
{
  char *comm;
  int xmin, xmax, ymin, ymax;
  int x, y, r, last_x, last_y;
  F_point *p;
  double theta;

  comm = get_comment(l->comments);
  if (comm!=NULL) {
    if (widthref(comm)) {
      /* The width of this line will be the reference */
      xmin=xmax=XZOOM(l->points->x);
      for (p = l->points->next; p != NULL; p = p->next) {
        x=XZOOM(p->x);
        if (x<xmin) xmin=x;
        if (x>xmax) xmax=x;
      }
      if (xmin!=xmax) {
        scaleset=true;
        scaledim=xmax-xmin;
      } else {
         fprintf(stderr, "fig2dev(shape) Warning: Width of reference object=0 - ignored \n");
      }
      return;
      /* reference objects are *no* shapes */
    } 
    if (heightref(comm)) {
      /* The height of this line will be the reference */
      ymin=ymax=YZOOM(l->points->y);
      for (p = l->points->next; p != NULL; p = p->next) {
        y=YZOOM(p->y);
        if (y<ymin) ymin=y;
        if (y>ymax) ymax=y;
      }
      if (ymin!=ymax) {
        scaleset=true;
        scaledim=ymax-ymin;
      } else {
         fprintf(stderr, "fig2dev(shape) Warning: Height of reference object=0 - ignored \n");
      }
      return;
      /* reference objects are *no* shapes */
    }
    if (centerref(comm)) {
      /* The center of this line will be the reference */
      xmin=xmax=XZOOM(l->points->x);
      for (p = l->points->next; p != NULL; p = p->next) {
        x=XZOOM(p->x);
        if (x<xmin) xmin=x;
        if (x>xmax) xmax=x;
      }
      /* this cannot fail */
      centerset=true;
      centerpos=(xmin+xmax)/2;
      return;
      /* reference objects are *no* shapes */
    } 
    switch (l->type) {
    case T_BOX:
    case T_PIC_BOX:
      start_line(comm, l->depth);
      line_to(XZOOM(l->points->x),YZOOM(l->points->y));
      for (p = l->points->next; p != NULL; p = p->next) {
        line_to(XZOOM(p->x),YZOOM(p->y));
      }
            
      finish_line();
      /* rectangle */
      break;
    case T_ARC_BOX:
      start_line(comm, l->depth);
      xmin = xmax = l->points->x;
      ymin = ymax = l->points->y;
      for (p = l->points->next; p != NULL; p = p->next) {
        if (p->x < xmin) xmin = p->x;
        if (xmax < p->x) xmax = p->x;
        if (p->y < ymin) ymin = p->y;
        if (ymax < p->y) ymax = p->y;
      }
      
      r=ZZOOM((l->radius));
      xmin=XZOOM(xmin);
      xmax=XZOOM(xmax);
      ymin=YZOOM(ymin);
      ymax=YZOOM(ymax);
      
      r=MIN(r,(xmax-xmin)/2);
      r=MIN(r,(ymax-ymin)/2);
      /* corner radius cannot be more than half of one side */
#define ARCCORNER(FIN_ANGLE) while(theta<(FIN_ANGLE)) { \
                             line_to(round(x+r*cos(theta)), round(y+r*sin(theta)));\
                             theta+=ARC_STEP; }
/* define macro for the corner arc */                        
             
      x=xmax-r;
      y=ymax-r;
      theta=0;
      ARCCORNER(M_PI/2);
      /* lower right corner */
      
      x=xmin+r;
      ARCCORNER(M_PI);
      /* lower left corner */
      
      y=ymin+r;
      ARCCORNER(3*M_PI/2);
      /* upper left corner */

      x=xmax-r;
      ARCCORNER(2*M_PI);
      /* upper right corner */
      
      finish_line();
      break;
 
    case T_POLYLINE:
    case T_POLYGON:
      start_line(comm, l->depth);
      for (p = l->points; (l->type==T_POLYLINE? p: p->next) != NULL; p = p->next) {
        x = XZOOM(p->x);
        y = YZOOM(p->y);
        if (p == l->points
            || MIN_DIST < abs(last_x - x) || MIN_DIST < abs(last_y - y)) {
         
            line_to(x, y);
            last_x = x;
            last_y = y;
          }
        }
      
      finish_line();
      break;
    }
    free(comm);
  }
}

void
genshape_text(t)
     F_text     *t;
{
  char *comm;

  comm = get_comment(t->comments);
  if (comm != NULL) {
    fprintf(stderr, "fig2dev(shape): TEXT can't be used as shape\n");
  }
}
/** and of the outer layer **/


bool between(float b1, float b2, float test) {
  return (((b1<=test)  && (test<=b2)) || ((b2<=test)  && (test<=b1))) ;
}

bool between_exclude(float b1, float b2, float test) {
  return (((b1<test)  && (test<b2)) || ((b2<test)  && (test<b1))) ;
}

bool between_int(int b1, int b2, int test) {
  return (((b1<=test)  && (test<=b2)) || ((b2<=test)  && (test<=b1))) ;
}




struct intersect_point {
  float x; float y;
  float x2; float y2;
  int shapenr;
  bool is_endpoint;
  bool is_horizontal;
};  
typedef struct intersect_point intersect_point;
static intersect_point *intersect_points=NULL;
static int MAX_INTERSECTPOINTS=0;
#define INTERSECTPOINT_INC 100

static realloc_intersects(int minimum) {
  while (minimum>=MAX_INTERSECTPOINTS) {
    MAX_INTERSECTPOINTS+=INTERSECTPOINT_INC;
    intersect_points=realloc(intersect_points, sizeof(intersect_points[0])*MAX_INTERSECTPOINTS);
  }  
}

static void 
intersect(float y_val, lineseg* lseg, int * num_xvalues) {
  /* compute the intersection between horizontal line at y_val
    and the linesegment lseg, store it in intersect_points */
#define ip intersect_points[*num_xvalues]
/* for readability define this abbreviation */
  /* fprintf(stderr, "Intersecting at %f with lineseg in shape %d\n", y_val, lseg->shapenr); */
  if (lseg->intersect_hit) return;
  /* this line has been handled by find_intersects */
  if (lseg->y==y_val) {
    /* it hits the endpoint */
    realloc_intersects((*num_xvalues)+1);
    ip.x=lseg->x;   ip.y=lseg->y;
    ip.x2=lseg->x2; ip.y2=lseg->y2;
    ip.shapenr=lseg->shapenr;
    ip.is_endpoint=!lseg->intersect;
    if (lseg->y2==y_val) {
      /* it is horizontal */
      ip.is_horizontal=true;
      (*num_xvalues)++;
      
     } else {
     
         ip.is_horizontal=false;
         (*num_xvalues)++;
         return;
     }   
    /* if it is horizontal, hits the second endpoint too*/
  }
  
  if (lseg->y2==y_val) {
    /* it hits the second endpoint */
    realloc_intersects((*num_xvalues)+1);
    ip.x=lseg->x2;   ip.y=lseg->y2;
    ip.x2=lseg->x; ip.y2=lseg->y;
    ip.shapenr=lseg->shapenr;
    ip.is_endpoint=!lseg->intersect;
    ip.is_horizontal=(ip.y2==ip.y);
    (*num_xvalues)++;
    return;
  }
  
  if (between(lseg->y, lseg->y2, y_val)) {
    /* only then there is an intersection */
    realloc_intersects((*num_xvalues)+1);
    ip.y=y_val;
    ip.x=lseg->x2+ ((float)(lseg->x-lseg->x2)*(float)(y_val-lseg->y2))/(float)(lseg->y-lseg->y2);
    ip.x2=lseg->x2;
    ip.y2=lseg->y2; 
    ip.is_endpoint=false;
    ip.is_horizontal=false;
    ip.shapenr=lseg->shapenr;
    (*num_xvalues)++;
    return;
  }
  
  /* no intersection, don`t increment num_xvalues */   
}
#undef ip  

#define IP1 ((intersect_point*)ip1)
#define IP2 ((intersect_point*)ip2)

static int intersect_comp_above(const void* ip1, const void * ip2) {
  /* first compare the x-values */
  if (IP1->x < IP2->x) return -1;
  if (IP1->x > IP2->x) return +1;
  /* for real intersections, falling line must before rising */
  if ((IP1->y != IP1->y2) && (IP2->y != IP2->y2)) {
    if ((IP1->x2 - IP1->x)/(IP1->y2 - IP1->y) > (IP2->x2 - IP2->x)/(IP2->y2 - IP2->y)) return -1;
    if ((IP1->x2 - IP1->x)/(IP1->y2 - IP1->y) < (IP2->x2 - IP2->x)/(IP2->y2 - IP2->y)) return +1;
    

  }
  if (IP1->shapenr < IP2->shapenr) return -1;
  if (IP1->shapenr > IP2->shapenr) return +1;
  return 0;
}  

static int intersect_comp_below(const void* ip1, const void * ip2) {
  /* first compare the x-values */
  if (IP1->x < IP2->x) return -1;
  if (IP1->x > IP2->x) return +1;
  /* for real intersections, falling line must before rising */
  if ((IP1->y != IP1->y2) && (IP2->y != IP2->y2)) {
    if ((IP1->x2 - IP1->x)/(IP1->y2 - IP1->y) > (IP2->x2 - IP2->x)/(IP2->y2 - IP2->y)) return +1;
    if ((IP1->x2 - IP1->x)/(IP1->y2 - IP1->y) < (IP2->x2 - IP2->x)/(IP2->y2 - IP2->y)) return -1;
    

  }
  if (IP1->shapenr < IP2->shapenr) return -1;
  if (IP1->shapenr > IP2->shapenr) return +1;
  return 0;
}

#undef IP1
#undef IP2

struct fullintersect_point {
  float x; float y;
  int nr1, nr2;
};

typedef struct fullintersect_point fullintersect_point;
static fullintersect_point* fips;
static int num_fips=0;
static int MAX_FIPS=20;
#define FIPS_INC 20
  
static void 
full_intersect(lineseg* lines, int nr1, int nr2, int * num_fip) {
  long D, f;
  int dx, dy, dxs, dys;
  float xs, ys;
  lineseg *ls1, *ls2;
  ls1=(&lines[nr1]);
  ls2=(&lines[nr2]);
  
  dx=ls1->x-ls1->x2;
  dy=ls1->y-ls1->y2;
  dxs=ls2->x-ls2->x2;
  dys=ls2->y-ls2->y2;
  
  if ((dy==0) || (dys==0)) return;
  /* intersection with horizontal line is *not* needed, instead harmful */
  D=dx*(long)dys-dxs*(long)dy;
  /* determinant of the linear equation system */
  if (D==0) return; 
  /* the lines are parallel */
  f=(ls2->x-ls1->x)*(long)dys-(ls2->y-ls1->y)*(long)dxs;
  /* avoid floating point arithmetic till the last step */
  xs=ls1->x+((float)(dx*f))/D;
  ys=ls1->y+((float)(dy*f))/D;
  /* test if the intersection point is inside both lines */
  if ((between_exclude(ls2->x, ls2->x2, xs) || between_exclude(ls2->y, ls2->y2, ys))
     && (between_exclude(ls1->x, ls1->x2, xs) || between_exclude(ls1->y, ls1->y2, ys))) {
    if (*num_fip>=MAX_FIPS) {
      /* realloc the fips */
      MAX_FIPS+=FIPS_INC;
      fips=realloc(fips, sizeof(fips[0])*MAX_FIPS);
    }  
    fips[*num_fip].x=xs;
    fips[*num_fip].y=ys;
    fips[*num_fip].nr1=nr1;
    fips[*num_fip].nr2=nr2;
    (*num_fip)++;
    /* fprintf(stderr, "(%d,%d)->(%d,%d) / (%d,%d)->(%d,%d) = (%g,%g) \n",
                   ls1->x, ls1->y, ls1->x2, ls1->y2, ls2->x, ls2->y, ls2->x2, ls2->y2, xs, ys); */


  }
    
}

static void clear_intersects(lineseg *lseg) {
 int i;
 for (i=0; i<num_fips; i++) {
   lseg[fips[i].nr1].intersect_hit=false;
   lseg[fips[i].nr2].intersect_hit=false;
 }
}

static void find_intersects(float y_val, lineseg *lseg, int *num_xvalues) {
 int i;
 for (i=0; i<num_fips; i++) {
   if (fips[i].y==y_val) {
     /* yeah, we found one */
#define ip intersect_points     
     realloc_intersects((*num_xvalues)+2);
     ip[*num_xvalues].x=fips[i].x;
     ip[*num_xvalues].y=fips[i].y;
     ip[*num_xvalues].is_endpoint=false;
     ip[*num_xvalues].x2=lseg[fips[i].nr1].x2;
     ip[*num_xvalues].y2=lseg[fips[i].nr1].y2;
     ip[*num_xvalues].shapenr=lseg[fips[i].nr1].shapenr;
     ip[*num_xvalues].is_horizontal=(ip[*num_xvalues].y==ip[*num_xvalues].y2);
     lseg[fips[i].nr1].intersect_hit=true;
     (*num_xvalues)++;
     
     ip[*num_xvalues].x=fips[i].x;
     ip[*num_xvalues].y=fips[i].y;
     ip[*num_xvalues].is_endpoint=false;
     ip[*num_xvalues].x2=lseg[fips[i].nr2].x2;
     ip[*num_xvalues].y2=lseg[fips[i].nr2].y2;
     ip[*num_xvalues].shapenr=lseg[fips[i].nr2].shapenr;
     ip[*num_xvalues].is_horizontal=(ip[*num_xvalues].y==ip[*num_xvalues].y2);
     lseg[fips[i].nr2].intersect_hit=true;
     (*num_xvalues)++;
   }
 }  
#undef ip 
}

typedef struct Textblock {
  float x; float len;
  char special;
  
  struct Textblock *next;
}  textblock;

typedef struct Scanline {
  float y;
  bool contains_horizontal;
  textblock* blocks[2];
}

scanline;

bool total_inside() {
 /* compute if we are in the shape totally
    according to what individual shapes we are in */
 
 int takethis=-1;
 int lastdepth=-1;
 int i;
 for (i=0; i<num_shapes; i++) {
   if (shapes[i].inside) {
     if ((lastdepth==-1) || (shapes[i].depth<lastdepth)) {
       takethis=i;
       lastdepth=shapes[i].depth;
     }
    }
 }
 if (takethis==-1) return false;
 return (shapes[takethis].ispositiv==1)?true:false;
} 

static textblock *blockptr=NULL, *oldblockptr=NULL;
static bool inblock;
static float block_xval;

static void start_blocks() {
 blockptr=oldblockptr=NULL;
 inblock=false;
}

static void begin_block(float atx) {
  inblock=true;
  block_xval=atx;
}

static void end_block(float atx) {
 if (!inblock) die("End block outside of a block ???");
 inblock=false;
 if (oldblockptr==NULL) {
   oldblockptr=malloc(sizeof(textblock));
   blockptr=oldblockptr;
   blockptr->x=block_xval;
   blockptr->len=atx-block_xval;
   blockptr->special=0;
   blockptr->next=NULL;
 } else {
   blockptr->next=malloc(sizeof(textblock));
   blockptr=blockptr->next;
   blockptr->x=block_xval;
   blockptr->len=atx-block_xval;
   blockptr->special=0;
   blockptr->next=NULL;
 }
}

static void special_block(float atx, char special) {
  if (inblock) {
     /* end block here */
     end_block(atx);
     blockptr->next=malloc(sizeof(textblock));
     blockptr=blockptr->next;
     blockptr->x=atx;
     blockptr->special=special;
     blockptr->len=0;
     blockptr->next=NULL;
     begin_block(atx);
  } else {
     if (oldblockptr==NULL) {
       /* the first block is special */
       oldblockptr=malloc(sizeof(textblock));
       blockptr=oldblockptr;
       blockptr->x=atx;
       blockptr->special=special;
       blockptr->len=0;
       blockptr->next=NULL;
     } else {
       /* only insert the special block */
       blockptr->next=malloc(sizeof(textblock));
       blockptr=blockptr->next;
       blockptr->x=atx;
       blockptr->special=special;
       blockptr->len=0;
       blockptr->next=NULL;
     }
  }
}

static void destroy_blocks(textblock* blockptr) {
  textblock *oldblockptr;
  oldblockptr=blockptr;
  while (blockptr!=NULL) {
    blockptr=blockptr->next;
    free(oldblockptr);
    oldblockptr=blockptr;
  }
}  

/* I'd really like to have C++ here, because of the destructors */
static void destroy_scanlines(scanline* scanlines, int num_yvalues) { 
 int i;
 /*free memory*/
  for (i=0; i<num_yvalues; i++) {
    destroy_blocks(scanlines[i].blocks[0]);
    if (scanlines[i].contains_horizontal) destroy_blocks(scanlines[i].blocks[1]);
  }
  free(scanlines);
}  

/* this is debugging stuff */
void print_inside(float last_xvalue) {
  int i;
  fprintf(stderr, "%g: ",last_xvalue);
  for (i=0; i<num_shapes; i++) fprintf (stderr, "%d(%d) ", shapes[i].inside, shapes[i].depth);
  fprintf(stderr,"\n");
}  

static void print_shape(int num_yvalues, scanline* scanlines) {
  int y_nr,i;
  float lastscale=0.1;
  float xmin, xmax;
  bool unset=true;
  bool firstline=true;
  
  if (macroname==NULL) macroname="newshape";
  /* write the definition */
  fprintf(tfp, "\\def\\%spar#1{\\shapepar{\\%sshape}#1\\par}\n\\def\\%sshape{%%\n", macroname, macroname, macroname);
  /* compute the center and the scale */
  for (y_nr=0; y_nr<num_yvalues; y_nr++) {
    blockptr=scanlines[y_nr].blocks[0];
    while (blockptr!=NULL) {
      if (unset) {
        xmax=xmin=blockptr->x;
        unset=false;
      } else {
        if (xmin>blockptr->x) xmin=blockptr->x;
        if (xmax<(blockptr->x+blockptr->len)) xmax=blockptr->x+blockptr->len;
      }
      blockptr=blockptr->next;
    }
  }
  if (xmax!=xmin) lastscale=50.0/(xmax-xmin);
  if (scaleset)  lastscale=10.0/scaledim;
  if (!centerset) centerpos=(xmax+xmin)/2;
  fprintf(tfp, "{%g}%%\n", centerpos*lastscale);
  /* center the shape */
  for (y_nr=0; y_nr<num_yvalues; y_nr++) {
   for (i=0;i<(scanlines[y_nr].contains_horizontal?2:1); i++) {
     blockptr=scanlines[y_nr].blocks[i];
     if (blockptr==NULL) continue;
     
     if (firstline) firstline=false;
     else fprintf(tfp, "\\\\%%\n");
     
     if (blockptr!=NULL) fprintf(tfp, "{%g}", scanlines[y_nr].y*lastscale);
     while (blockptr!=NULL) {
       switch (blockptr->special) {
        case 's':
        case 'j':
          fprintf(tfp, "%c",blockptr->special);
          break;
        case 'e':
        case 'b':
          fprintf(tfp, "%c{%g}",blockptr->special, blockptr->x*lastscale);
          break;
        case 0:
           fprintf(tfp, "t{%g}{%g}", blockptr->x*lastscale, blockptr->len*lastscale);
           break;
        default:
           die("Unknown blocktype ???");
        }
        blockptr=blockptr->next;
        
     }  
      
    }  
  }

  fprintf(tfp, "%%\n}\n");
}


int
genshape_end()
{ /* everything is done here */
  int	 i, shapenr,shapenr2, shapestart, shapeend, shapegroupnr;
  float	 y0, y_val;
  int	 num_yvalues, num_rawyvalues, num_xvalues;
  int	 y_nr=0;
  bool	 inside=false, inside_now;
  scanline *scanlines;
  float	*y_values;
  float	*above_block_borders, *below_block_borders;
  int	 num_above_borders, num_below_borders;
  int	 snr, aind, bind;
  bool	 equal;
  char	*oldgroupname;
  int	 oldscanlines=0;

  
  if (num_points==0) die("No shape found - have you forgotten to set comments '+'?");

  intersect_points=malloc(sizeof(intersect_point)*num_points);
  MAX_INTERSECTPOINTS=num_points;
  fips=malloc(sizeof(fullintersect_point)*num_points);
  MAX_FIPS=num_points;

  qsort(shapes,num_shapes, sizeof(shapes[0]), shapecomp); 
  /* sort the shapes by their groupname */
  /* now the shapenrs has changed, to which the points belong
     but we need them for quick reverse lookup 
     solve this by rewriting the shapenrs */
  
  for (shapenr=0; shapenr<num_shapes; shapenr++) {
    for (i=shapes[shapenr].linestart; i<=shapes[shapenr].lineend; i++)
      points[i].shapenr=shapenr;
  }    

  
  oldgroupname=shapes[0].groupname;
  /* now find the borders in between the shapegroups */
  shapestart=0;
  for (i=0; i<num_shapes; i++) {
    if (strcmp(shapes[shapestart].groupname, shapes[i].groupname)!=0) {
      /* here two consecutive shapes have different groupnames*/
      add_shapegroup(shapestart, i-1);
      shapestart=i;
    }
  }
  /* add the last shapegroup till the end of the shapes */
  add_shapegroup(shapestart, num_shapes-1);
 
  /* print_shapegroups(); */
  /* now run for each shapegroup inidvidual */
  for (shapegroupnr=0; shapegroupnr<num_shapegroups; shapegroupnr++) {
    shapestart=shapegroups[shapegroupnr].shapestart;
    shapeend=shapegroups[shapegroupnr].shapeend;
    
    /* intersect each polygonline with each other in this shapegroup*/
    num_fips=0;
    for (shapenr=shapestart; shapenr<=shapeend; shapenr++) {
      for (shapenr2=shapenr+1; shapenr2<=shapeend; shapenr2++) {

       int nr1, nr2;
       for (nr1=shapes[shapenr].linestart; nr1<=shapes[shapenr].lineend; nr1++) {
         /* all lines from shape shapenr */
         for (nr2=shapes[shapenr2].linestart; nr2<=shapes[shapenr2].lineend; nr2++) {
          /* have to be compared with all lines from shape shapenr2 */
          /* avoid intersecting the lines of one polygon
             can cause confusion, but has the effect that the polygons *may not*
             intersect itself like an '8' for example */
           full_intersect(points, nr1, nr2, &num_fips);
          /* fprintf(stderr, "Intersecting %d/%d \n", nr1, nr2); */
         }  /* lines from shape shapenr2 */
       } /* lines from shape shapenr */
      }  /* shapenr2 */
    } /* shapenr */  

    /* Now sort all existing y-Coordinates */
    /* allocate memory */
    y_values=malloc(sizeof(y_values[0])*(num_points+num_fips)); 
    num_rawyvalues=0;
    for (i=0; i<num_points; i++) { 
      if (between_int(shapestart, shapeend, points[i].shapenr)) y_values[num_rawyvalues++]=points[i].y;
    } 
    /* fprintf(stderr, "Points in shapegroup %d: %d\n", shapegroupnr, num_rawyvalues); */
    for (i=0; i<num_fips; i++) y_values[num_rawyvalues++]=fips[i].y;
    
    qsort(y_values, num_rawyvalues, sizeof(y_values[0]), floatcomp);
    /* make them unique */
    y0=y_values[0];
    for (i=num_yvalues=1; i<num_rawyvalues; i++ ) {
      if (y_values[i]!=y0) {
        y_values[num_yvalues]=y_values[i];
        num_yvalues++;
        y0=y_values[i];
      }
    } 
    /* allocate memory for the scanlines */
    if (oldscanlines==0) scanlines=malloc(sizeof(scanline)*num_yvalues);
    else scanlines=realloc(scanlines, sizeof(scanline)*(num_yvalues+oldscanlines));
    /* make it bigger, if this a second pass */
    
    for (y_nr=0; y_nr<num_yvalues; y_nr++) {
      y_val=y_values[y_nr];
  
      scanlines[oldscanlines+y_nr].y=y_val;
      scanlines[oldscanlines+y_nr].contains_horizontal=false;
  
      
      /* compute the intersection of the horizontal scan lines
         with all defined lines in this shapegroup */
      num_xvalues=0;
      /* first look for intersection points of polygons */
      clear_intersects(points);
      find_intersects(y_val, points, &num_xvalues);
      /* then compute horizontal intersections, leave out the found intersect-points */
      for (i=0; i<num_points; i++) {
        if (between_int(shapestart, shapeend, points[i].shapenr)) {
           /* only in this shapegroup */
           intersect(y_val, &points[i], &num_xvalues);
           
        }
      }
      
      /* allocate memory for the blocks */
      above_block_borders=malloc(sizeof(float)*num_xvalues*2);
      num_above_borders=0;
      
  
     /* sort the intersections with the horizontal scan line 
        in ascending x- order  for intersection above the line */
      qsort(intersect_points, num_xvalues, sizeof(intersect_point), intersect_comp_above);
      for (i=shapestart; i<=shapeend; i++) {
       shapes[i].inside=false;
      }
      
      inside=false;
      for (i=0; i<num_xvalues;i++) {
        /* the point is hit either if the second is situated above the current line,
           or if it is a real intersection (not endpoint) */
        if (!intersect_points[i].is_endpoint || intersect_points[i].y2<y_val) {
          snr=intersect_points[i].shapenr;
          shapes[snr].inside=!shapes[snr].inside;
          inside_now=total_inside();
          if (inside!= inside_now) {
            /* we crossed the border of the figure */
            above_block_borders[num_above_borders++]=intersect_points[i].x;
            inside=inside_now;
          }  
        }
      }
      if (inside) die("Outside all polygons (above) inside the shape ???");
  
      /* allocate memory for the blocks */
      below_block_borders=malloc(sizeof(float)*num_xvalues*2);
      num_below_borders=0;
      
  
     /* sort the intersections with the horizontal scan line 
        in ascending x- order  for intersection above the line */
      qsort(intersect_points, num_xvalues, sizeof(intersect_point), intersect_comp_below);
      for (i=shapestart; i<=shapeend; i++) {
       shapes[i].inside=false;
      }
      
      inside=false;
      for (i=0; i<num_xvalues;i++) {
        /* the point is hit either if the second is situated below the current line,
           or if it is a real intersection (not endpoint) */
        if (!intersect_points[i].is_endpoint || intersect_points[i].y2>y_val) {
          snr=intersect_points[i].shapenr;
        shapes[snr].inside=!shapes[snr].inside;
        inside_now=total_inside();
        if (inside!= inside_now) {
          /* we crossed the border of the figure */
          below_block_borders[num_below_borders++]=intersect_points[i].x;
          inside=inside_now;
        }  
        }
      }
      if (inside) die("Outside all polygons (below) inside the shape ???");
  
  
      /* comparison algorithm 
         look, if the blocks are identical if zero-lenghts blocks and gaps are neglected
         then it is an ordinary line without horizontals */
      
      start_blocks();
      /* start the textblock engine */
      equal=true;
      aind=0; bind=0;
      while (equal && (aind<num_above_borders) && (bind<num_below_borders)) {
       if (above_block_borders[aind]==below_block_borders[bind]) {
        /* normal begin or end */
        if ((aind % 2)==0)
          begin_block(above_block_borders[aind]);
        else
          end_block(above_block_borders[aind]);
        
        aind++;
        bind++;
        continue;
       }
       if (above_block_borders[aind]<below_block_borders[bind]) {
         /* look for a zero-length difference */
         if (((aind+1)<num_above_borders) && 
            (above_block_borders[aind]==above_block_borders[aind+1])) {
           /* join or end */
           if ((aind % 2)==0)
             special_block(above_block_borders[aind],'e');
           else
             special_block(above_block_borders[aind],'j');
           aind+=2; /* skip both */
           continue;
          } else {
           /* difference more than a 0-gap */
           equal=false;
           break;
           
         }
       }
       
       if (above_block_borders[aind]>below_block_borders[bind]) {
         /* look for a zero-length difference */
         if (((bind+1)<num_below_borders) && 
            (below_block_borders[bind]==below_block_borders[bind+1])) {
           /* begin or split */
           if ((bind % 2)==0)
             special_block(below_block_borders[bind],'b');
           else
             special_block(below_block_borders[bind],'s');
           bind+=2; /* skip both */
           continue;
        } else {
           /* difference more than a 0-gap */
           equal=false;
           break;
          
         }
       }  
      }
  
      while (equal && aind<num_above_borders) {
        /* zero blocks above left ? */
        if ((aind+1)<num_above_borders) {
           if (above_block_borders[aind]==above_block_borders[aind+1]) {
           /* join or end */
           if ((aind % 2)==0)
             special_block(above_block_borders[aind],'e');
           else
             special_block(above_block_borders[aind],'j');
  
             aind+=2; /* skip both */
           continue;
         } else {
           /* difference more than a 0-gap */
           equal=false;
           break;
         }  
         }
      }
  
  
      while (equal && bind<num_below_borders) {
        /* zero blocks below left ? */
        if ((bind+1)<num_below_borders) {
           if (below_block_borders[bind]==below_block_borders[bind+1]) {
           /* begin or split */
           if ((bind % 2)==0)
             special_block(below_block_borders[bind],'b');
           else
             special_block(below_block_borders[bind],'s');
           bind+=2; /* skip both */
           continue;
         } else {
           /* difference more than a 0-gap */
           equal=false;
           break;
         }  
         }
      }
       
      if (equal) {
        /* one line is enough */
        /* now set the textblockchain into the scanline */
        scanlines[oldscanlines+y_nr].blocks[0]=oldblockptr;
        scanlines[oldscanlines+y_nr].blocks[1]=NULL;
        scanlines[oldscanlines+y_nr].contains_horizontal=false;
      } else {
        int aind, bind;
        float ax, bx, last_ax, last_bx;
        scanlines[oldscanlines+y_nr].contains_horizontal=true;
        destroy_blocks(oldblockptr);
        
        /* look deeper, horizontal lines must be handled */
  
        /* start textblock engine for 'above' lines */
        start_blocks();
        bind=1;
        last_ax=MIN(above_block_borders[0], below_block_borders[0])-1;
        /* start left from all */
        for (aind=0; aind<num_above_borders; aind++, last_ax=ax) {
          ax=above_block_borders[aind];
          if (ax==last_ax) {
            /* found a cute special */
            special_block(ax, (aind%2==1)?'e':'j');
            continue;
          }
    
          /* normal textblock ? */
          if (aind%2==1) begin_block(last_ax);
          while ((bind<num_below_borders) && (below_block_borders[bind]<ax)) {
            last_bx=below_block_borders[bind-1];
            bx=below_block_borders[bind];
            if (between(last_ax,ax,last_bx) && between(last_ax,ax,bx)
                 && (last_bx!=bx)) {
              /* complete nonzero element inside a block or hole */
              if ((bind%2==1) && (aind%2==0)) special_block(last_bx,'b');
                /* block inside a hole */
              if ((bind%2==0) && (aind%2==1)) special_block(bx,'s');
                  /* hole inside a block */
            }     
            bind++;
          }
          if (aind%2==1) end_block(ax);
        }
        /* something left below? */
        while ((bind<num_below_borders)) {
          last_bx=below_block_borders[bind-1];
          bx=below_block_borders[bind];
          if ((last_bx!=bx) && ((last_bx>ax) || (num_above_borders==0))) {
            if (bind%2==1) special_block(last_bx,'b');
            /* block outside all */
          }     
          bind++;
        }
  
  
        scanlines[oldscanlines+y_nr].blocks[0]=oldblockptr;
  
        /* now the same for 'below' lines */
        start_blocks();
        aind=1;
        last_bx=MIN(above_block_borders[0], below_block_borders[0])-1;
        /* start left from all */
        for (bind=0; bind<num_below_borders; bind++, last_bx=bx) {
          bx=below_block_borders[bind];
          if (bx==last_bx) {
            /* found a cute special */
            special_block(bx, (bind%2==1)?'b':'s');
            continue;
          }
    
          /* normal textblock ? */
          if (bind%2==1) begin_block(last_bx);
          while ((aind<num_above_borders) && (above_block_borders[aind]<bx)) {
            last_ax=above_block_borders[aind-1];
            ax=above_block_borders[aind];
            if (between(last_bx,bx,last_ax) && between(last_bx,bx,ax)
                 && (last_ax!=ax)) {
              /* complete nonzero element inside a block or hole */
              if ((aind%2==1) && (bind%2==0)) special_block(ax,'e');
                /* block inside a hole */
              if ((aind%2==0) && (bind%2==1)) special_block(last_ax,'j');
                  /* hole inside a block */
            }     
            aind++;
          }
          if (bind%2==1) end_block(bx);
        }
        /* something left above? */
        while ((aind<num_above_borders)) {
          last_ax=above_block_borders[aind-1];
          ax=above_block_borders[aind];
          if ((last_ax!=ax) && ((last_ax>bx) || (num_below_borders==0))) {
            /* complete nonzero element inside a block or hole */
            if (aind%2==1) special_block(ax,'e');
              /* block outside all  */
            }     
          aind++;
        }
  
        scanlines[oldscanlines+y_nr].blocks[1]=oldblockptr;
  
  
  
      }
      /* free the memory for the block borders */
      free(above_block_borders);
      free(below_block_borders);
  
      
  
        
    } /* for all y_values */
    free(y_values);
    oldscanlines+=num_yvalues;
  } /* for all shapegroups */  
  
  /* now print the result */
  print_shape(oldscanlines, scanlines);

  destroy_scanlines(scanlines, num_yvalues);
  free(intersect_points);
  free(fips);
  cleanup_memory();   
  /* all ok */
  return 0;
}


struct driver dev_shape = {
        genshape_option,
        genshape_start,
        gendev_null,
        genshape_arc,
        genshape_ellipse,
        genshape_line,
        gendev_null,
        genshape_text,
        genshape_end,
        INCLUDE_TEXT
};