summaryrefslogtreecommitdiff
path: root/metapost/context/base/mp-core.mpiv
blob: 9b7182908ab1cf329179ec0c94365f7021462cef (plain)
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
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
%D \module
%D   [       file=mp-core.mpiv,
%D        version=1999.08.01, % anchoring
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=background macros,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

if known context_core : endinput ; fi ;

boolean context_core ; context_core := true ;

%D Copied to here .. not used any more.

if unknown NOfTextColumns : numeric NOfTextColumns ; NOfTextColumns := 1 ; fi ;
if unknown NOfTextAreas   : numeric NOfTextAreas   ; NOfTextAreas   := 1 ; fi ;

def SaveTextAreas =
    path    SavedTextAreas  []  ;
    path    SavedTextColumns[]  ;
    numeric NOfSavedTextAreas   ;
    numeric NOfSavedTextColumns ;
    for i=1 upto NOfTextAreas :
        SavedTextAreas[i] := TextAreas[i] ;
    endfor ;
    for i=1 upto NOfTextColumns :
        SavedTextColumns[i] := TextColumns[i] ;
    endfor ;
    NOfSavedTextAreas   := NOfTextAreas ;
    NOfSavedTextColumns := NOfTextColumns ;
enddef ;

def ResetTextAreas =
    path TextAreas[], TextColumns[], PlainTextArea, RegionTextArea ;
    numeric NOfTextAreas   ; NOfTextAreas   := 0 ;
    numeric NOfTextColumns ; NOfTextColumns := 0 ;
    numeric nofmultipars   ; nofmultipars   := 0 ;
    TextAreas[0] := TextColumns[0] := origin -- cycle ;
enddef ;

ResetTextAreas ; SaveTextAreas ; ;

def RegisterTextArea (expr x, y, w, h, d) =
    begingroup ;
    save p ; path p ;
    p := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
    if NOfTextAreas>0 :
        % if needed, concatenate areas
        if (round(llcorner TextAreas[NOfTextAreas]) = round(ulcorner p)) and
           (round(lrcorner TextAreas[NOfTextAreas]) = round(urcorner p)) :
            p :=
                ulcorner TextAreas[NOfTextAreas] --
                urcorner TextAreas[NOfTextAreas] --
                lrcorner p                       --
                llcorner p                       -- cycle ;
        else :
            NOfTextAreas := NOfTextAreas + 1 ;
        fi ;
    else :
        NOfTextAreas := NOfTextAreas + 1 ;
    fi ;
    TextAreas[NOfTextAreas] := p ;
    if NOfTextColumns>0 :
        if (round(xpart llcorner TextColumns[NOfTextColumns]) = round(xpart ulcorner p)) and
           (round(xpart lrcorner TextColumns[NOfTextColumns]) = round(xpart urcorner p)) :
            p :=
                ulcorner TextColumns[NOfTextColumns] --
                urcorner TextColumns[NOfTextColumns] --
                lrcorner p                           --
                llcorner p                           -- cycle ;
        else :
            NOfTextColumns := NOfTextColumns + 1 ;
        fi ;
    else :
        NOfTextColumns := NOfTextColumns + 1 ;
    fi ;
    TextColumns[NOfTextColumns] := p ;
    endgroup ;
enddef ;

%D We store a local area in slot zero.

def RegisterPlainTextArea(expr x,y,w,h,d) =
    PlainTextArea := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
enddef ;

def RegisterRegionTextArea(expr x,y,w,h,d) =
    RegionTextArea := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
  % RegionTextArea := RegionTextArea enlarged 2mm ;
enddef ;

def RegisterLocalTextArea (expr x, y, w, h, d) =
    TextAreas[0] := TextColumns[0] := unitsquare xyscaled(w,h+d) shifted (x,y-d) ;
enddef ;

def ResetLocalTextArea =
    TextAreas[0] := TextColumns[0] := origin -- cycle ;
enddef ;

ResetLocalTextArea ;

vardef InsideTextArea (expr _i_, _xy_) =
    (round(xpart _xy_) >= round(xpart llcorner TextAreas[_i_])) and
    (round(xpart _xy_) <= round(xpart lrcorner TextAreas[_i_])) and
    (round(ypart _xy_) >= round(ypart llcorner TextAreas[_i_])) and
    (round(ypart _xy_) <= round(ypart urcorner TextAreas[_i_]))
enddef ;

vardef InsideSavedTextArea (expr _i_, _xy_) =
    (round(xpart _xy_) >= round(xpart llcorner SavedTextAreas[_i_])) and
    (round(xpart _xy_) <= round(xpart lrcorner SavedTextAreas[_i_])) and
    (round(ypart _xy_) >= round(ypart llcorner SavedTextAreas[_i_])) and
    (round(ypart _xy_) <= round(ypart urcorner SavedTextAreas[_i_]))
enddef ;

vardef InsideSomeTextArea (expr _xy_) =
    save ok ; boolean ok ; ok := false ;
    for i := 1 upto NOfTextAreas :
        if InsideTextArea(i,_xy_) :
            ok := true ; % we can move the exit here
        fi ;
        exitif ok ;
    endfor ;
    ok
enddef ;

vardef InsideSomeSavedTextArea (expr _xy_) =
    save ok ; boolean ok ; ok := false ;
    for i := 1 upto NOfSavedTextAreas :
        if InsideSavedTextArea(i,_xy_) :
            ok := true ;
        fi ;
        exitif ok ;
    endfor ;
    ok
enddef ;

vardef TextAreaX (expr x) =
    numeric _TextAreaX_ ; _TextAreaX_ := 0 ;
    for i := 1 upto NOfTextAreas :
        if (round(x) >= round(xpart llcorner TextAreas[i])) and
           (round(x) <= round(xpart lrcorner TextAreas[i])) :
            _TextAreaX_ := xpart llcorner TextAreas[i] ;
        fi ;
    endfor ;
    _TextAreaX_
enddef ;

vardef TextAreaY (expr y) =
    numeric _TextAreaY_ ; _TextAreaY_ := 0 ;
    for i := 1 upto NOfTextAreas :
        if (round(y) >= round(ypart llcorner TextAreas[NOfTextAreas])) and
           (round(y) <= round(ypart ulcorner TextAreas[NOfTextAreas])) :
            _TextAreaY_ := ypart llcorner TextAreas[NOfTextAreas] ;
        fi ;
    endfor ;
    _TextAreaY_
enddef ;

vardef TextAreaXY (expr x, y) =
    pair _TextAreaXY_ ; _TextAreaXY_ := origin ;
    for i := 1 upto NOfTextAreas :
        if (round(x) >= round(xpart llcorner TextAreas[i])) and
           (round(x) <= round(xpart lrcorner TextAreas[i])) and
           (round(y) >= round(ypart llcorner TextAreas[i])) and
           (round(y) <= round(ypart ulcorner TextAreas[i])) :
            _TextAreaXY_ := llconer TextAreas[i] ;
        fi ;
    endfor ;
    _TextAreaXY_
enddef ;

vardef TextAreaW (expr x) =
    numeric _TextAreaW_ ; _TextAreaW_ := 0 ;
    for i := 1 upto NOfTextAreas :
        if (round(x) >= round(xpart llcorner TextAreas[i])) and
           (round(x) <= round(xpart lrcorner TextAreas[i])) :
            _TextAreaW_ := bbwidth(TextAreas[i]) ;
        fi ;
    endfor ;
    _TextAreaW_
enddef ;

vardef TextAreaH (expr y) =
    numeric _TextAreaH_ ; _TextAreaH_ := 0 ;
    for i := 1 upto NOfTextAreas :
        if (round(y) >= round(ypart llcorner TextAreas[i])) and
           (round(y) <= round(ypart ulcorner TextAreas[i])) :
            _TextAreaH_ := bbheight(TextAreas[i]) ;
        fi ;
      endfor ;
      _TextAreaH_
enddef ;

vardef TextAreaWH (expr x, y) =
      pair _TextAreaWH_ ; _TextAreaWH_ := origin ;
      for i := 1 upto NOfTextAreas :
        if (round(x) >= round(xpart llcorner TextAreas[i])) and
           (round(x) <= round(xpart lrcorner TextAreas[i])) and
           (round(y) >= round(ypart llcorner TextAreas[i])) and
           (round(y) <= round(ypart ulcorner TextAreas[i])) :
            _TextAreaWH_ := (bbwidth(TextAreas[i]),bbheight(TextAreas[i])) ;
        fi ;
      endfor ;
      _TextAreaWH_
enddef ;

%D Till here.

pair    lxy[], rxy[], cxy[], llxy[], lrxy[], ulxy[], urxy[] ;
path    pxy[] ;
numeric hxy[], wxy[], dxy[], nxy[] ;

def box_found (expr n,x,y,w,h,d) =
    not ((x=0) and (y=0) and (w=0) and (h=0) and (d=0))
enddef ;

def initialize_box_pos (expr pos,n,x,y,w,h,d) =
    pair lxy, rxy, cxy, llxy, lrxy, ulxy, urxy ;
    path pxy ; numeric hxy, wxy, dxy, nxy;
    lxy  := (x,y) ;
    llxy := (x,y-d) ;
    lrxy := (x+w,y-d) ;
    urxy := (x+w,y+h) ;
    ulxy := (x,y+h) ;
    wxy  := w ;
    hxy  := h ;
    dxy  := d ;
    rxy  := lxy shifted (wxy,0) ;
    pxy  := llxy--lrxy--urxy--ulxy--cycle ;
    cxy  := center pxy ;
    nxy  := n ;
    freeze_box(pos) ;
enddef ;

def freeze_box (expr pos) =
    lxy[pos]  := lxy  ;
    llxy[pos] := llxy ;
    lrxy[pos] := lrxy ;
    urxy[pos] := urxy ;
    ulxy[pos] := ulxy ;
    wxy[pos]  := wxy  ;
    hxy[pos]  := hxy  ;
    dxy[pos]  := dxy  ;
    rxy[pos]  := rxy  ;
    pxy[pos]  := pxy  ;
    cxy[pos]  := cxy  ;
    nxy[pos]  := nxy  ;
enddef ;

def initialize_box (expr n,x,y,w,h,d) =
    numeric bpos ; bpos := 0 ; initialize_box_pos(bpos,n,x,y,w,h,d) ;
enddef ;

def initialize_area (expr fn,fx,fy,fw,fh,fd,
                          tn,tx,ty,tw,th,td) =
    numeric fpos ; fpos := 1 ; initialize_box_pos(fpos,fn,fx,fy,fw,fh,fd) ;
    numeric tpos ; tpos := 2 ; initialize_box_pos(tpos,tn,tx,ty,tw,th,td) ;
    do_initialize_area (fpos, tpos) ;
enddef ;

def do_initialize_area (expr fpos, tpos) =
    lxy  := lxy[fpos] ;
    llxy := (xpart llxy[fpos], ypart llxy[tpos]) ;
    lrxy := lrxy[tpos] ;
    urxy := (xpart urxy[tpos], ypart urxy[fpos]) ;
    ulxy := ulxy[fpos] ;
    wxy  := xpart lrxy - xpart llxy ;
    hxy  := hxy[fpos] ;
    dxy  := dxy[tpos] ;
    rxy  := lxy shifted (wxy,0) ;
    pxy  := llxy--lrxy--urxy--ulxy--cycle ;
    cxy  := center pxy ;
enddef ;

def set_par_line_height (expr ph, pd) =
    par_strut_height := if ph>0 : ph elseif StrutHeight>0 : StrutHeight else : 8pt fi ;
    par_strut_depth  := if pd>0 : pd elseif StrutDepth >0 : StrutDepth  else : 3pt fi ;
    par_line_height  := par_strut_height + par_strut_depth ;
enddef ;

def initialize_par (expr fn,fx,fy,fw,fh,fd,
                         tn,tx,ty,tw,th,td,
                         mn,mx,my,mw,mh,md,
                         pn,px,py,pw,ph,pd,
                         rw,rl,rr,rh,ra,ri) =

    numeric fpos ; fpos := 1 ; initialize_box_pos(fpos,fn,fx,fy,fw,fh,fd) ;
    numeric tpos ; tpos := 2 ; initialize_box_pos(tpos,tn,tx,ty,tw,th,td) ;
    numeric mpos ; mpos := 3 ; initialize_box_pos(mpos,mn,mx,my,mw,mh,md) ;
    numeric ppos ; ppos := 4 ; initialize_box_pos(ppos,pn,px,py,pw,ph,pd) ;

    numeric par_strut_height, par_strut_depth, par_line_height ;

    set_par_line_height (ph, pd) ;

    do_initialize_area (fpos, tpos) ;
    do_initialize_par  (fpos, tpos, mpos, ppos, rw,rl,rr,rh,ra,ri) ;

enddef ;

def initialize_area_par (expr fn,fx,fy,fw,fh,fd,
                              tn,tx,ty,tw,th,td,
                              wn,wx,wy,ww,wh,wd) =

    numeric fpos ; fpos := 1 ; initialize_box_pos(fpos,fn,fx,fy,fw,fh,fd) ;
    numeric tpos ; tpos := 2 ; initialize_box_pos(tpos,tn,tx,ty,tw,th,td) ;
    numeric wpos ; wpos := 3 ; initialize_box_pos(wpos,wn,wx,wy,ww,wh,wd) ;

    numeric par_strut_height, par_strut_depth, par_line_height ;

    set_par_line_height (wh, wd) ;

    numeric ffpos ; ffpos := 4 ; initialize_box_pos(ffpos,wn,wx,fy,0,wh,wd) ;
    numeric ttpos ; ttpos := 5 ; initialize_box_pos(ttpos,wn,wx+ww,ty,0,wh,wd) ;

    do_initialize_area (ffpos, ttpos) ;

    numeric mpos ; mpos := 6 ; freeze_box(mpos) ;

    do_initialize_par  (fpos, tpos, mpos, ffpos, 0,0,0,0,0,0) ;

enddef ;

def do_initialize_par (expr fpos, tpos, mpos, ppos, rw,rl,rr,rh,ra,ri) =

    pair lref, rref, pref, lhref, rhref ;

    % clip the page area to the left and right skips

    llxy[mpos] := llxy[mpos] shifted (+rl,0) ;
    lrxy[mpos] := lrxy[mpos] shifted (-rr,0) ;
    urxy[mpos] := urxy[mpos] shifted (-rr,0) ;
    ulxy[mpos] := ulxy[mpos] shifted (+rl,0) ;

    % fixate the leftskip, rightskip and hanging indentation

    lref := (xpart llxy[mpos],ypart ulxy[ppos]) ; lhref := lref shifted (rh,0) ;
    rref := (xpart lrxy[mpos],ypart urxy[ppos]) ; rhref := rref shifted (rh,0) ;

    pref := lxy[ppos] ;

    if nxy[tpos] > nxy[fpos] :
        if nxy[fpos] = nxy[mpos] :
            % first of multiple pages
            llxy[tpos] := llxy[mpos] ;
            lrxy[tpos] := lrxy[mpos] ;
            urxy[tpos] := lrxy[mpos] shifted (0,par_line_height) ;
            ulxy[tpos] := llxy[mpos] shifted (0,par_line_height) ;
            boxgriddirection := down ;
        elseif nxy[tpos] = nxy[mpos] :
            % last of multiple pages
            llxy[fpos] := ulxy[mpos] shifted (0,-par_line_height) ;
            lrxy[fpos] := urxy[mpos] shifted (0,-par_line_height) ;
            urxy[fpos] := urxy[mpos] ;
            ulxy[fpos] := ulxy[mpos] ;
            boxgriddirection := up ;
        else :
            % middle of multiple pages
            llxy[fpos] := ulxy[mpos] shifted (0,-par_line_height) ;
            lrxy[fpos] := urxy[mpos] shifted (0,-par_line_height) ;
            urxy[fpos] := urxy[mpos] ;
            ulxy[fpos] := ulxy[mpos] ;
            llxy[tpos] := llxy[mpos] ;
            lrxy[tpos] := lrxy[mpos] ;
            urxy[tpos] := lrxy[mpos] shifted (0,par_line_height) ;
            ulxy[tpos] := llxy[mpos] shifted (0,par_line_height) ;
            boxgriddirection := up ;
        fi ;
    else :
        % just one page
        boxgriddirection := up ;
    fi ;

    path txy, bxy, pxy, mxy ;

    txy := originpath ; % top
    bxy := originpath ; % bottom
    pxy := originpath ; % composed

    boolean lefthang, righthang, somehang ;

    % we only hang on the first of a multiple page background

    if nxy[mpos] > nxy[fpos] :
        lefthang := righthang := somehang := false ;
    else :
        lefthang := (rh>0) ; righthang := (rh<0) ; somehang := false ;
    fi ;

    if lefthang :
        mxy  := boundingbox (lref -- lref shifted (rh,ra*par_line_height)) ;
    elseif righthang :
        mxy  := boundingbox (rref -- rref shifted (rh,ra*par_line_height)) ;
    else :
        mxy  := originpath ;
    fi ;

    if round(ypart llxy[fpos]) = round(ypart llxy[tpos]) :

        % We have a one-liner. Watch how er use the bottom pos for
        % determining the height.

        llxy[fpos] := (xpart llxy[fpos], ypart llxy[tpos]) ;
        ulxy[fpos] := (xpart ulxy[fpos], ypart ulxy[tpos]) ;

    else :

        % We have a multi-liner. For convenience we now correct the
        % begin and end points for indentation.

        if lefthang and (round(ypart llxy[tpos]) >= round(ypart lrcorner mxy)) :
            llxy[tpos] := (xpart lhref, ypart llxy[tpos]) ;
            ulxy[tpos] := (xpart lhref, ypart ulxy[tpos]) ;
        else :
            llxy[tpos] := (xpart  lref, ypart llxy[tpos]) ;
            ulxy[tpos] := (xpart  lref, ypart ulxy[tpos]) ;
        fi ;

        if righthang and (round(ypart lrxy[fpos]) >= round(ypart llcorner mxy)) :
            lrxy[fpos] := (xpart rhref, ypart lrxy[fpos]) ;
            urxy[fpos] := (xpart rhref, ypart urxy[fpos]) ;
        else :
            lrxy[fpos] := (xpart  rref, ypart lrxy[fpos]) ;
            urxy[fpos] := (xpart  rref, ypart urxy[fpos]) ;
        fi ;

    fi ;

    somehang := (ypart ulxy[fpos]>ypart llcorner mxy) and
                (ypart llxy[tpos]<ypart llcorner mxy) ;

    if round(ypart llxy[fpos]) = round(ypart llxy[tpos]) :

        % A (short) one-liner goes into the top box.

        txy := llxy[fpos] -- lrxy[tpos] -- urxy[tpos] -- ulxy[fpos] -- cycle ;

    elseif (round(ypart llxy[fpos]) = round(ypart ulxy[tpos])) and
           (round(xpart lrxy[tpos]) < round(xpart llxy[fpos])) :

        % We have a sentence that spans two lines but with only end
        % of line and begin of line segments. We need to take care of
        % indentation.

        txy := llxy[fpos] -- lrxy[fpos] -- urxy[fpos] -- ulxy[fpos] -- cycle ;
        bxy := llxy[tpos] -- lrxy[tpos] -- urxy[tpos] -- ulxy[tpos] -- cycle ;

    elseif (round(ypart llxy[fpos]) = round(ypart ulxy[tpos])) :

        % We have a sentence that spans two lines but with overlap.

        pxy :=
            llxy[tpos] -- lrxy[tpos] -- urxy[tpos] -- lrxy[fpos] --
            urxy[fpos] -- ulxy[fpos] -- llxy[fpos] -- ulxy[tpos] -- cycle ;

    elseif lefthang and somehang :

        % We have a sentence that spans more than two lines with
        % left hanging indentation.

        pxy :=
            llxy[tpos] -- lrxy[tpos] -- urxy[tpos] --
            (xpart urxy[fpos],ypart urxy[tpos]) --
            urxy[fpos] -- ulxy[fpos] -- llxy[fpos] --
            if round(ypart urxy[tpos]) < round(ypart llcorner mxy) :
               (xpart lrcorner mxy,ypart llxy[fpos]) --
               lrcorner mxy --
               (xpart llxy[tpos],ypart llcorner mxy) --
            else :
               (xpart llxy[tpos],ypart llxy[fpos]) --
            fi
            cycle ;

    elseif righthang and somehang :

        % We have a sentence that spans more than two lines with
        % right hanging indentation.

        pxy :=
            llxy[tpos] -- lrxy[tpos] -- urxy[tpos] --
            if round(ypart urxy[tpos]) < round(ypart llcorner mxy) :
                (xpart lrcorner mxy,ypart urxy[tpos]) --
                lrcorner mxy -- llcorner mxy --
            else :
                (xpart urxy[fpos],ypart urxy[tpos]) --
            fi
            urxy[fpos] -- ulxy[fpos] -- llxy[fpos] --
            (xpart llxy[tpos],ypart llxy[fpos]) --
            cycle ;

    else :

        % We have a sentence that spans more than two lines with
        % no hanging indentation.

        pxy :=
            llxy[tpos] -- lrxy[tpos] -- urxy[tpos] --
            (xpart urxy[fpos],ypart urxy[tpos]) --
            urxy[fpos] -- ulxy[fpos] -- llxy[fpos] --
            (xpart llxy[tpos],ypart llxy[fpos]) --
            cycle ;

    fi ;

    pxy := simplified pxy ;
    pxy := unspiked   pxy ;

enddef ;

TopSkip     := 0 ; % will move
StrutHeight := 0 ; % will move

pair last_multi_par_shift ; last_multi_par_shift := origin ;

def relocate_multipars (expr xy) =
    last_multi_par_shift := xy ;
    for i=1 upto nofmultipars :
        multipars[i] := multipars[i] shifted last_multi_par_shift ;
    endfor ;
enddef ;

boolean compensate_multi_par_topskip ;
boolean span_multi_column_pars ;
boolean auto_multi_par_hsize ;
boolean enable_multi_par_fallback ;

compensate_multi_par_topskip := true ;
span_multi_column_pars       := false ;
auto_multi_par_hsize         := false ; % true ;
enable_multi_par_fallback    := true ;

vardef multi_par_at_top (expr i) =
  (round (ypart ulcorner multipars[i]) = round (ypart ulcorner (TextAreas[multirefs[i]] shifted last_multi_par_shift)))
enddef ;

numeric nofmultipars ; nofmultipars := 0 ;

boolean obey_multi_par_hang    ; obey_multi_par_hang    := true  ;
boolean obey_multi_par_more    ; obey_multi_par_more    := true  ;
boolean snap_multi_par_tops    ; snap_multi_par_tops    := true  ;
boolean local_multi_par_area   ; local_multi_par_area   := false ;
boolean use_multi_par_region   ; use_multi_par_region   := false ;
boolean ignore_multi_par_page  ; ignore_multi_par_page  := false ;
boolean force_multi_par_chain  ; force_multi_par_chain  := true  ;
boolean one_piece_multi_par    ; one_piece_multi_par    := false ;
boolean check_multi_par_chain  ; check_multi_par_chain  := true  ; % extra page check

boolean multi_column_first_page_hack; multi_column_first_page_hack := true ; % seems to work ok

def simplify_multi_pars = % boundingbox ipv shape als optie
    for i := 1 upto nofmultipars :
        multipars[i] := boundingbox multipars[i] ;
    endfor ;
enddef ;

def save_multipar (expr i, l, p) =
    nofmultipars := nofmultipars + 1 ;
    multirefs[nofmultipars] := i ;
    multilocs[nofmultipars] := l ;
    multipars[nofmultipars] := unspiked (simplified p) ;
enddef ;

def prepare_multi_pars (expr fn,fx,fy,fw,fh,fd,
                             tn,tx,ty,tw,th,td,
                             wn,wx,wy,ww,wh,wd,
                             pn,px,py,pw,ph,pd,
                             rw,rl,rr,rh,ra,ri) =

% fill PlainTextArea withcolor red ;
% fill RegionTextArea withcolor green;

    if span_multi_column_pars :
        begingroup ;
        save TextAreas ; path TextAreas[] ;
        save NOfTextAreas ; numeric NOfTextAreas ;
        for i=1 upto NOfTextColumns :
            TextAreas[i] := TextColumns[i] ;
        endfor ;
        NOfTextAreas := NOfTextColumns ;
    fi ;

    last_multi_par_shift := origin ;

% save _tx_, _ty_, _fx_, _fy_ ;
% if use_multi_par_region :
%     _fx_ := fx ; %min(xpart ulcorner RegionTextArea,fx) ;
%     _fy_ := fy ; %min(xpart ulcorner RegionTextArea,fy) ;
%     _tx_ := min(xpart lrcorner RegionTextArea,tx) ;
%     _ty_ := min(xpart lrcorner RegionTextArea,ty) ;
% else :
%     _fx_ := fx ;
%     _fy_ := fy ;
%     _tx_ := tx ;
%     _ty_ := ty ;
% fi ;

%     numeric fpos ; fpos := 1 ; initialize_box_pos(fpos,fn,_fx_,_fy_,fw,fh,fd) ;
%     numeric tpos ; tpos := 2 ; initialize_box_pos(tpos,tn,_tx_,_ty_,tw,th,td) ;
    numeric fpos ; fpos := 1 ; initialize_box_pos(fpos,fn,fx,fy,fw,fh,fd) ;
    numeric tpos ; tpos := 2 ; initialize_box_pos(tpos,tn,tx,ty,tw,th,td) ;
    numeric wpos ; wpos := 3 ; initialize_box_pos(wpos,wn,wx,wy,ww,wh,wd) ;
    numeric ppos ; ppos := 4 ; initialize_box_pos(ppos,pn,px,py,pw,ph,pd) ;

    if local_multi_par_area :
        RealPageNumber := fn ;
        NOfTextAreas := 1 ;
        NOfSavedTextAreas := 0 ;
        TextAreas[1] := TextAreas[0] ;
        TextColumns[1] := TextColumns[0] ;
        nxy[fpos] := nxy[tpos] := nxy[wpos] := nxy[ppos] := RealPageNumber ;
    elseif use_multi_par_region :
        RealPageNumber := fn ;
        NOfTextAreas := 1 ;
        NOfSavedTextAreas := 0 ;
        TextAreas[1] := RegionTextArea ;
        TextColumns[1] := RegionTextArea ;
        nxy[fpos] := nxy[tpos] := nxy[wpos] := nxy[ppos] := RealPageNumber ;
    elseif ignore_multi_par_page :
        RealPageNumber := fn ;
        nxy[fpos] := nxy[tpos] := nxy[wpos] := nxy[ppos] := RealPageNumber ;
    fi ;

    numeric par_strut_height, par_strut_depth, par_line_height ;

    set_par_line_height (ph, pd) ;

    numeric par_hang_indent, par_hang_after, par_indent, par_left_skip, par_right_skip ;

    par_hang_indent  := rh ;
    par_hang_after   := ra ;
    par_indent       := ri ;
    par_left_skip    := rl ;
    par_right_skip   := rr ;

    pair par_start_pos ;
    pair par_stop_pos ;

    par_start_pos := llxy[fpos]
        if par_indent   <0: shifted (-par_indent,   0) fi
        if par_left_skip<0: shifted (-par_left_skip,0) fi ;

    par_stop_pos := lrxy[tpos]
        if par_right_skip<0: shifted (par_right_skip,0) fi ; % nasty as the endpos can be shifted by rightskip

    if wxy[wpos]>0 :
        left_skip  := rl + xpart llxy[wpos] - xpart llxy[ppos] ;
        right_skip := rw - left_skip - ww ;
    else :
        left_skip  := rl ;
        right_skip := rr ;
    fi ;

    path    multipar, multipars[] ;
    numeric multiref, multirefs[] ;
    numeric multiloc, multilocs[] ; % 1=begin 2=between 3=end

    numeric multi_par_pages ; multi_par_pages := nxy[tpos]-nxy[fpos]+1 ;

    % locals .. why can't i move these outside?

    vardef _pmp_set_multipar_ (expr i) =
        ( (TextAreas[i] leftenlarged -left_skip) rightenlarged (-right_skip
        if auto_multi_par_hsize : + rw - bbwidth(TextAreas[i]) fi) )
    enddef ;

    vardef _pmp_snapped_multi_pos_ (expr p) =
        if snap_multi_par_tops :
            if abs(ypart p - ypart ulcorner multipar) < par_line_height :
                (xpart p,ypart ulcorner multipar)
            else :
                p
            fi
        else :
            p
        fi
    enddef ;

    vardef _pmp_estimated_par_lines_ (expr h) =
        round(h/par_line_height)
    enddef ;

    vardef _pmp_top_multi_par_(expr p) =
        (round(_pmp_estimated_par_lines_(bbheight(p)*par_line_height))=round(bbheight(p)))
    enddef ;

    vardef _pmp_multi_par_tsc_(expr p) =
        if _pmp_top_multi_par_(p) : TopSkipCorrection else : 0 fi
    enddef ;

    vardef _pmp_estimated_multi_par_height_ (expr n, t) =
        if round(par_line_height)=0 :
            0
        else :
            save ok, h ; boolean ok ;
            numeric h ; h := 0 ;
            ok := false ;
            if (nxy[fpos]=RealPageNumber-1) :
                for i := 1 upto NOfSavedTextAreas :
                    if (InsideSavedTextArea(i,par_start_pos)) :
                        ok := true ;
                        h := h + _pmp_estimated_par_lines_(ypart ulxy[fpos] - ypart llcorner SavedTextAreas[i]) ;
                    elseif ok :
                        h := h + _pmp_estimated_par_lines_(bbheight(SavedTextAreas[i])) ;
                    fi ;
                endfor ;
            fi ;
            if ok :
                for i := 1 upto n-1 :
                    h := h + _pmp_estimated_par_lines_(bbheight(TextAreas[i])) ;
                endfor ;
            else :
                % already: ok := false ;
                for i := 1 upto n-1 :
                    if (InsideTextArea(i,par_start_pos)) :
                        ok := true ;
                        h := h + _pmp_estimated_par_lines_(ypart ulxy[fpos] - ypart llcorner TextAreas[i]) ;
                    elseif ok :
                        h := h + _pmp_estimated_par_lines_(bbheight(TextAreas[i])) ;
                    fi ;
                endfor ;
            fi ;
            h
        fi
    enddef ;

    vardef _pmp_left_top_hang_ (expr same_area) =

        par_hang_after := ra + _pmp_estimated_par_lines_(py-fy) ;

        if (par_hang_indent>0) and (par_hang_after<0) and obey_multi_par_hang :
            pair _ul_ ; _ul_ := (xpart ulcorner multipar, ypart _pmp_snapped_multi_pos_(ulxy[fpos]));
            pair _pa_ ; _pa_ := _ul_ shifted (0,par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_ -TopSkipCorrection,ypart llcorner multipar)) ;
            if same_area :
                _pa_ := (xpart _pa_,max(ypart _pa_ -TopSkipCorrection,ypart llxy[tpos])) ;
            fi ;
            if obey_multi_par_more and (round(par_line_height)>0) :
                par_hang_after := min(0,round(par_hang_after + (ypart urxy[fpos]-ypart _pa_)/par_line_height)) ;
            fi ;
            (xpart _ul_ + par_hang_indent, ypart lrxy[fpos]) --
            (xpart _ul_ + par_hang_indent, ypart _pa_) --
            (xpart ulcorner multipar, ypart _pa_)
        else :
            (xpart ulcorner multipar, ypart lrxy[fpos])
        fi
    enddef ;

    vardef _pmp_right_top_hang_ (expr same_area) =

        par_hang_after := ra + _pmp_estimated_par_lines_(py-fy) ;

        if (par_hang_indent<0) and (par_hang_after<0) and obey_multi_par_hang :
            pair _ur_ ; _ur_ := (xpart urcorner multipar, ypart _pmp_snapped_multi_pos_(urxy[fpos])) ;
            pair _pa_ ; _pa_ := _ur_ shifted (0,par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_ -TopSkipCorrection,ypart llcorner multipar)) ;
            if same_area :
                _pa_ := (xpart _pa_,max(ypart _pa_ -TopSkipCorrection,ypart _pmp_snapped_multi_pos_(ulxy[tpos]))) ;
            fi ;
            if obey_multi_par_more and (round(par_line_height)>0) :
                par_hang_after := min(0,round(par_hang_after + (ypart urxy[fpos]-ypart _pa_)/par_line_height)) ;
            fi ;
            (xpart urcorner multipar, ypart _pa_) --
            (xpart _ur_ + par_hang_indent, ypart _pa_) --
            (xpart _ur_ + par_hang_indent, ypart _pmp_snapped_multi_pos_(urxy[fpos]))
        else :
            (xpart urcorner multipar, ypart _pmp_snapped_multi_pos_(urxy[fpos]))
        fi
    enddef ;

    vardef _pmp_x_left_top_hang_ (expr i, t) =
        par_hang_after := min(0,ra + _pmp_estimated_multi_par_height_(i,t)) ;
        if (par_hang_indent>0) and (par_hang_after<0) :
            pair _ul_ ; _ul_ := ulcorner multipar ;
            pair _pa_ ; _pa_ := _ul_ shifted (0,par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart llcorner multipar)) ;
            if t :
                _pa_ := (xpart _pa_,max(ypart _pa_,ypart llxy[tpos]));
            fi ;
            if abs(ypart _pa_-ypart llxy[tpos])<par_line_height :
                _pa_ := (xpart _pa_,ypart llxy[tpos]);
            fi ;
            if abs(ypart _pa_-ypart llcorner multipar)<par_line_height :
                _pa_ := (xpart _pa_,ypart llcorner multipar);
            fi ;
            (xpart _ul_, ypart _pa_) --
            (xpart _ul_ + par_hang_indent, ypart _pa_) --
            (xpart _ul_ + par_hang_indent, ypart _ul_)
        else :
            ulcorner multipar
        fi
    enddef ;

    vardef _pmp_x_right_top_hang_ (expr i, t) =
        par_hang_after := min(0,ra + _pmp_estimated_multi_par_height_(i,t)) ;
        if (par_hang_indent<0) and (par_hang_after<0) :
            pair _ur_ ; _ur_ := urcorner multipar ;
            pair _pa_ ; _pa_ := _ur_ shifted (0,par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart lrcorner multipar)) ;
            if t :
                _pa_ := (xpart _pa_,max(ypart _pa_,ypart _pmp_snapped_multi_pos_(urxy[tpos]))) ;
            fi ;
            (xpart _ur_ + par_hang_indent, ypart _ur_) --
            (xpart _ur_ + par_hang_indent, ypart _pa_) --
            (xpart _ur_, ypart _pa_)
        else :
            urcorner multipar
        fi
    enddef ;

    vardef _pmp_left_bottom_hang_ (expr same_area) =
        pair _ll_, _sa_, _pa_ ;
        _sa_ := if same_area : llxy[tpos] else : lrcorner multipar fi ;
        if (par_hang_indent>0) and (par_hang_after>0) and obey_multi_par_hang :
            _ll_ := (xpart ulcorner multipar, ypart _pmp_snapped_multi_pos_(ulxy[fpos])) ;
            _pa_ := _ll_ shifted (0,-par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart llcorner multipar)) ;
            if same_area :
                _pa_ := (xpart _pa_,max(ypart _pa_,ypart _sa_)) ;
            fi ;
            if obey_multi_par_more and (round(par_line_height)>0) :
                par_hang_after := max(0,round(par_hang_after - (ypart urxy[fpos]-ypart _pa_)/par_line_height)) ;
            fi ;
            _pa_ --
            (xpart _pa_ + par_hang_indent,ypart _pa_) --
            (xpart _pa_ + par_hang_indent,ypart _sa_)
        else :
            (xpart llcorner multipar, ypart _sa_)
        fi
    enddef ;

    vardef _pmp_right_bottom_hang_ (expr same_area) =
        pair _lr_, _sa_, _pa_ ;
        _sa_ := if same_area : _pmp_snapped_multi_pos_(ulxy[tpos]) else : lrcorner multipar fi ;
        if (par_hang_indent<0) and (par_hang_after>0) and obey_multi_par_hang :
            _lr_ := (xpart urcorner multipar, ypart _pmp_snapped_multi_pos_(urxy[fpos])) ;
            _pa_ := _lr_ shifted (0,-par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart lrcorner multipar)) ;
            if same_area :
                _pa_ := (xpart _pa_,max(ypart _pa_,ypart _pmp_snapped_multi_pos_(ulxy[tpos]))) ;
            fi ;
            if obey_multi_par_more and (round(par_line_height)>0) :
                par_hang_after := max(0,round(par_hang_after - (ypart urxy[fpos]-ypart _pa_)/par_line_height)) ;
            fi ;
            (xpart _pa_ + par_hang_indent,ypart _sa_) --
            (xpart _pa_ + par_hang_indent,ypart _pa_) --
            _pa_
        else :
        (xpart lrcorner multipar, ypart _sa_)
        fi
    enddef ;

    vardef _pmp_x_left_bottom_hang_ (expr i, t) =
        pair _ll_, _sa_, _pa_ ;
        _sa_ := if t : llxy[tpos] else : llcorner multipar fi ;
        if (par_hang_indent>0) and (ra>0) :
            par_hang_after := max(0,ra - _pmp_estimated_multi_par_height_(i,t)) ;
            _ll_ := ulcorner multipar ;
            _pa_ := _ll_ shifted (0,-par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart llcorner multipar)) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart _sa_)) ;
            % we need to compensate for topskip enlarged areas
            if abs(ypart _pa_ - ypart _sa_) > par_line_height :
                (xpart _pa_ + par_hang_indent,ypart _sa_) --
                (xpart _pa_ + par_hang_indent,ypart _pa_) --
            fi
            _pa_
        else :
            (xpart llcorner multipar, ypart _sa_)
        fi
    enddef ;

    vardef _pmp_x_right_bottom_hang_ (expr i, t) =
        pair _lr_, _sa_, _pa_ ;
        _sa_ := if t : _pmp_snapped_multi_pos_(ulxy[tpos]) else : llcorner multipar fi ;
        if (par_hang_indent<0) and (ra>0) :
            par_hang_after := max(0,ra - _pmp_estimated_multi_par_height_(i, t)) ;
            _lr_ := urcorner multipar ;
            _pa_ := _lr_ shifted (0,-par_hang_after*par_line_height) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart lrcorner multipar)) ;
            _pa_ := (xpart _pa_,max(ypart _pa_,ypart _sa_)) ;
            % we need to compensate for topskip enlarged areas
            _pa_
            if abs(ypart _pa_ - ypart _sa_) > par_line_height :
                -- (xpart _pa_ + par_hang_indent,ypart _pa_)
                -- (xpart _pa_ + par_hang_indent,ypart _sa_)
            fi
        else :
            (xpart lrcorner multipar, ypart _sa_)
        fi
    enddef ;

    % def _pmp_test_multipar_ =
    %     multipar := boundingbox multipar ;
    % enddef ;

    % first loop

    ii := 0 ; nn := NOfTextAreas+1 ; nofmultipars := 0 ;

    if enable_multi_par_fallback  and (nxy[fpos]=RealPageNumber)
        and (nxy[tpos]=RealPageNumber) and not (InsideSomeTextArea(lxy[fpos]) and InsideSomeTextArea(rxy[tpos])) :

        % fallback

        % multipar :=
        %     llxy[fpos] --
        %     lrxy[tpos] --
        %     urxy[tpos] --
        %     ulxy[fpos] -- cycle ;
        %
        % save_multipar (1,1,multipar) ;

        % we need to take the boundingbox because there can be
        % more lines and we want a proper rectange

        multipar :=
            ulxy[fpos] --
            urxy[tpos] --
            lrxy[fpos] --
            llxy[tpos] -- cycle ;

        save_multipar (1,1,boundingbox(multipar)) ;

    else :

        % normal

        for i=1 upto NOfTextAreas :

            TopSkipCorrection := 0 ;

            multipar := _pmp_set_multipar_(i) ;

            % watch how we compensate for negative indentation

            if (nxy[fpos]=RealPageNumber) and (InsideTextArea(i,par_start_pos)) :

                % first one in chain

                ii := i ;

                if (nxy[tpos]=RealPageNumber) and (InsideTextArea(i,par_stop_pos)) :

                    % in same area

                    nn := i ;

                    if compensate_multi_par_topskip and (round(LineHeight-ph-pd)=0) :

                        TopSkipCorrection := TopSkip - StrutHeight ;

                        if round(ypart ulxy[fpos] + TopSkipCorrection) = round(ypart ulcorner TextAreas[i]) :
                            ulxy[fpos] := ulxy[fpos] shifted (0,TopSkipCorrection) ;
                            urxy[fpos] := urxy[fpos] shifted (0,TopSkipCorrection) ;
                        else :
                            TopSkipCorrection := 0 ;
                        fi ;

                    fi ;

                    if ypart llxy[fpos] = ypart llxy[tpos] :

                        multipar :=
                            llxy[fpos] --
                            lrxy[tpos] --
                            _pmp_snapped_multi_pos_(urxy[tpos]) --
                            _pmp_snapped_multi_pos_(ulxy[fpos]) --
                            cycle ;

                        save_multipar (i,1,multipar) ;

                    elseif (ypart llxy[fpos] = ypart ulxy[tpos]) and (xpart llxy[tpos] < xpart llxy[fpos]) :

                        % two loners

                        multipar := if obey_multi_par_hang :

                            _pmp_right_bottom_hang_(true) --
                            _pmp_right_top_hang_(true) --
                            _pmp_snapped_multi_pos_(urxy[fpos]) --
                            lrxy[fpos] --

                        else :

                            llxy[fpos] --
                            (xpart urcorner multipar, ypart llxy[fpos]) --
                            (xpart urcorner multipar, ypart ulxy[fpos]) --
                            _pmp_snapped_multi_pos_(ulxy[fpos]) --

                        fi cycle ;

                        save_multipar (i,1,multipar) ;

                        multipar := _pmp_set_multipar_(i) ;

                        multipar := if obey_multi_par_hang :

                            _pmp_left_bottom_hang_(true) --
                            llxy[tpos] --
                            _pmp_snapped_multi_pos_(ulxy[tpos]) --
                            _pmp_left_top_hang_(true) --

                        else :

                            (xpart llcorner multipar, ypart llxy[tpos]) --
                            llxy[tpos] --
                            _pmp_snapped_multi_pos_(ulxy[tpos]) --
                            (xpart llcorner multipar, ypart ulxy[tpos]) --

                        fi cycle ;

                        save_multipar (i,1,multipar) ;

                    else :

                        multipar := if obey_multi_par_hang :

                            _pmp_left_bottom_hang_(true) --
                            llxy[tpos] --
                            _pmp_snapped_multi_pos_(ulxy[tpos]) --
                            _pmp_right_bottom_hang_(true) --
                            _pmp_right_top_hang_(true) --
                            _pmp_snapped_multi_pos_(urxy[fpos]) --
                            lrxy[fpos] --
                            _pmp_left_top_hang_(true) --

                        else :

                            (xpart llcorner multipar, ypart llxy[tpos]) --
                            llxy[tpos] --
                            _pmp_snapped_multi_pos_(ulxy[tpos]) --
                            (xpart lrcorner multipar, ypart ulxy[tpos]) --
                            (xpart urcorner multipar, ypart urxy[fpos]) --
                            _pmp_snapped_multi_pos_(urxy[fpos]) --
                            lrxy[fpos] --
                            (xpart ulcorner multipar, ypart lrxy[fpos]) --

                        fi cycle  ;

                        save_multipar (i,1,multipar) ;

                    fi ;

                else :

                    multipar := if obey_multi_par_hang :

                        _pmp_left_bottom_hang_(false) --
                        _pmp_right_bottom_hang_(false) --
                        _pmp_right_top_hang_(false) --
                        _pmp_snapped_multi_pos_(urxy[fpos]) --
                        lrxy[fpos] --
                        _pmp_left_top_hang_(false) --

                    else :

                        llcorner multipar --
                        lrcorner multipar --
                        (xpart urcorner multipar, ypart urxy[fpos]) --
                        _pmp_snapped_multi_pos_(urxy[fpos]) --
                        lrxy[fpos] --
                        (xpart ulcorner multipar, ypart lrxy[fpos]) --

                    fi cycle ;

                    save_multipar (i,1,multipar) ;

                fi ;

            elseif (nxy[tpos]=RealPageNumber) and (InsideTextArea(i,par_stop_pos)) :

                % last one in chain

                nn := i ;

                if obey_multi_par_hang and obey_multi_par_more :

                    multipar :=
                        _pmp_x_left_top_hang_(i,true) --
                        _pmp_x_right_top_hang_(i,true) --
                        _pmp_x_right_bottom_hang_(i,true) --
                        _pmp_snapped_multi_pos_(ulxy[tpos]) --
                        llxy[tpos] --
                        _pmp_x_left_bottom_hang_(i,true) --
                        cycle ;

                else :

                    multipar :=
                        ulcorner multipar --
                        urcorner multipar --
                        (xpart lrcorner multipar, ypart urxy[tpos]) --
                        _pmp_snapped_multi_pos_(ulxy[tpos]) --
                        llxy[tpos] --
                        (xpart llcorner multipar, ypart llxy[tpos]) --
                        cycle ;

                fi ;

                save_multipar (i,3,multipar) ;

            elseif multi_column_first_page_hack and ((nxy[fpos]=RealPageNumber) and (nxy[tpos]>=RealPageNumber) and (NOfTextColumns>1)) :

                save_multipar (i,2,multipar) ;

            else :
                % handled later
            fi ;

        endfor ;


        % second loop

        if force_multi_par_chain or (ii > 1) :

            for i=ii+1 upto nn-1 :

                % rest of chain / todo : hang

                % hm, the second+ column in column sets now gets lost in a NOfTextColumns

                if (not check_multi_par_chain) or ((nxy[fpos]<RealPageNumber) and (nxy[tpos]>RealPageNumber)) :

                    multipar := _pmp_set_multipar_(i) ;

                    if obey_multi_par_hang and obey_multi_par_more :

                        multipar :=
                            _pmp_x_left_top_hang_(i,false) --
                            _pmp_x_right_top_hang_(i,false) --
                            _pmp_x_right_bottom_hang_(i,false) --
                            _pmp_x_left_bottom_hang_(i,false) --
                            cycle ;

                    fi ;

                    save_multipar(i,2,multipar) ;

                fi ;

            endfor ;

        fi ;

        % end of normal/fallback

    fi ;

    if span_multi_column_pars :
        endgroup ;
    fi ;

    % potential safeguard:

    % for i=1 upto nofmultipars :
    %     if length p <= 4 :
    %         multipars[i] := boundingbox(multipars[i]) ;
    %     fi ;
    % end ;

    % quick hack for gb:

    one_piece_multi_par := (nofmultipars=1) and (pn=tn) ;

enddef ;

def boxgridoptions = withcolor .8red   enddef ;
def boxlineoptions = withcolor .8blue  enddef ;
def boxfilloptions = withcolor .8white enddef ;

numeric boxgridtype      ; boxgridtype      := 0 ;
numeric boxlinetype      ; boxlinetype      := 1 ;
numeric boxfilltype      ; boxfilltype      := 1 ;
numeric boxdashtype      ; boxdashtype      := 0 ;
pair    boxgriddirection ; boxgriddirection := up ;
numeric boxgridwidth     ; boxgridwidth     := 1pt ;
numeric boxlinewidth     ; boxlinewidth     := 1pt ;
numeric boxlineradius    ; boxlineradius    := 0pt ;
numeric boxfilloffset    ; boxfilloffset    := 0pt ;
numeric boxgriddistance  ; boxgriddistance  := .5cm ;
numeric boxgridshift     ; boxgridshift     := 0pt ;

def draw_box =
    draw pxy        boxlineoptions withpen pencircle scaled boxlinewidth ;
    draw lxy -- rxy boxlineoptions withpen pencircle scaled boxgridwidth ;
enddef ;

def draw_par  = % 1 2 3 11 12
    do_draw_par(pxy) ; do_draw_par(txy) ; do_draw_par(bxy) ;
    for i = pxy, txy, bxy :
        if boxgridtype = 1 :
            boxgriddirection := origin ;
            draw baseline_grid (i,boxgriddirection,true ) boxgridoptions ;
        elseif boxgridtype = 2 :
            boxgriddirection := origin ;
            draw baseline_grid (i,boxgriddirection,false) boxgridoptions ;
        elseif boxgridtype = 3 :
            boxgriddirection := origin ;
            draw baseline_grid (i,boxgriddirection,true ) boxgridoptions ;
            draw baseline_grid (i,boxgriddirection,true ) shifted (0,ExHeight) boxgridoptions ;
        elseif boxgridtype = 4 :
            boxgriddirection := origin ;
            draw baseline_grid (i,boxgriddirection,true ) shifted (0,ExHeight/2) boxgridoptions ;
        elseif boxgridtype = 11 :
            draw graphic_grid(i,boxgriddistance,boxgriddistance,boxgriddistance/2,boxgriddistance/2) ;
        elseif boxgridtype = 12 :
            draw graphic_grid(i,boxgriddistance,boxgriddistance,0,0) ;
        fi ;
    endfor ;
enddef ;

def do_show_par (expr p, r, c) =
    if length(p) > 2 :
        for i=0 upto length(p) :
            draw fullcircle scaled r shifted point i of p withpen pencircle scaled .5pt withcolor c ;
        endfor ;
    fi ;
    draw p withpen pencircle scaled .5pt withcolor c ;
enddef ;

def show_par  =
    if length(mxy) > 2 :
        draw mxy dashed evenly withpen pencircle scaled .5pt withcolor .5white ;
    fi ;
    do_show_par(txy, 4pt, .5green) ;
    do_show_par(bxy, 6pt, .5blue ) ;
    do_show_par(pxy, 8pt, .5red  ) ;
    draw pref withpen pencircle scaled 2pt ;
enddef ;

def sort_multi_pars =
    if nofmultipars>1 :
        begingroup ;
        save _p_, _n_ ; path _p_ ; numeric _n_ ;
        for i := 1 upto nofmultipars :
            if multilocs[i] = 3 :
                _p_ := multipars[nofmultipars] ;
                multipars[nofmultipars] := multipars[i] ;
                multipars[i] := _p_ ;
                _n_ := multirefs[nofmultipars] ;
                multirefs[nofmultipars] := multirefs[i] ;
                multirefs[i] := _n_ ;
                _n_ := multilocs[nofmultipars] ;
                multilocs[nofmultipars] := multilocs[i] ;
                multilocs[i] := _n_ ;
            fi ;
        endfor ;
        endgroup ;
    fi ;
enddef ;

def collapse_multi_pars =
    if nofmultipars>1 :
        begingroup ;
        save _nofmultipars_ ; numeric _nofmultipars_ ;
        _nofmultipars_ := 1 ;
        sort_multi_pars ; % block not in order: 1, 3, 2....
        for i:=1 upto nofmultipars-1 :
            if (round(xpart(llcorner multipars[i]-llcorner multipars[i+1]))=0) and
               (round(xpart(lrcorner multipars[i]-lrcorner multipars[i+1]))=0) :
                multilocs[_nofmultipars_] := multilocs[i+1] ;
                multirefs[_nofmultipars_] := multirefs[i+1] ;
                multipars[_nofmultipars_] :=
                    ulcorner multipars[_nofmultipars_] --
                    urcorner multipars[_nofmultipars_] --
                    lrcorner multipars[i+1] --
                    llcorner multipars[i+1] -- cycle ;
            else :
                _nofmultipars_ := _nofmultipars_ + 1 ;
                multipars[_nofmultipars_] := multipars[i+1] ;
                multilocs[_nofmultipars_] := multilocs[i+1] ;
                multirefs[_nofmultipars_] := multirefs[i+1] ;
            fi  ;
        endfor ;
        nofmultipars := _nofmultipars_ ;
        endgroup ;
    fi ;
enddef ;

def draw_multi_pars  =
    for i=1 upto nofmultipars :
        do_draw_par(multipars[i]) ;
        if boxgridtype= 1 :
            draw baseline_grid (multipars[i],if multilocs[i]=1: down else: up fi,true) ;
        elseif boxgridtype= 2 :
            draw baseline_grid (multipars[i],if multilocs[i]=1: down else: up fi,false) ;
        elseif boxgridtype= 3 :
            draw baseline_grid (multipars[i],if multilocs[i]=1: down else: up fi,true) ;
            draw baseline_grid (multipars[i],if multilocs[i]=1: down else: up fi,true) shifted (0,ExHeight) ;
        elseif boxgridtype= 4 :
            draw baseline_grid (multipars[i],if multilocs[i]=1: down else: up fi,true) shifted (0,ExHeight/2) ;
        elseif boxgridtype=11 :
            draw graphic_grid(multipars[i],boxgriddistance,boxgriddistance,boxgriddistance/2,boxgriddistance/2) ;
        elseif boxgridtype=12 :
            draw graphic_grid(multipars[i],boxgriddistance,boxgriddistance,0,0) ;
        fi ;
    endfor ;
enddef ;

def show_multi_pars  =
    for i=1 upto nofmultipars :
        do_show_par(multipars[i], 6pt, .5blue) ;
    endfor ;
enddef ;

vardef do_draw_par (expr p) =
    if (length p>2) and (bbwidth(p)>1) and (bbheight(p)>1) :
        save pp ; path pp ;
        if (boxlineradius>0) and (boxlinetype=2) :
            pp := p cornered boxlineradius ;
        else :
            pp := p ;
        fi ;
        if boxfilltype>0 :
            if boxfilloffset>0 :
                % temporary hack
                begingroup ;
                interim linejoin := mitered ;
                filldraw pp boxfilloptions withpen pencircle scaled (2*boxfilloffset) ;
                endgroup ;
            else :
                fill pp boxfilloptions ;
            fi ;
        fi ;
        if boxlinetype>0 :
            draw pp boxlineoptions withpen pencircle scaled boxlinewidth ;
        fi ;
    fi ;
enddef ;

vardef baseline_grid (expr pxy, pdir, at_baseline) =
    save width ; width := bbwidth(pxy) ;
    save height ; height := bbheight(pxy) ;
    if (par_line_height>0) and (height>1) and (width>1) and (boxgridwidth>0) :
        save i, grid, bb ; picture grid ; pair start ; path bb ;
        def _do_ (expr start) =
            % 1 = normal, 2 = with background (i.e. no shine-through)
            if boxdashtype = 2 :
                draw start -- start shifted (width,0)
                    withpen pencircle scaled boxgridwidth
                    boxfilloptions ;
            fi ;
            draw start -- start shifted (width,0)
                if boxdashtype > 0 :
                    dashed evenly
                fi
                withpen pencircle scaled boxgridwidth
                boxgridoptions ;
        enddef ;
        grid := image ( % fails with inlinespace
            if pdir=up :
                for i = if at_baseline : par_strut_depth else : 0 fi step  par_line_height until max(height,par_line_height) :
                    _do_ (llcorner pxy shifted (0,+i)) ;
                endfor ;
            else :
                for i = if at_baseline : par_strut_height else : 0 fi step  par_line_height until height :
                    _do_ (ulcorner pxy shifted (0,-i)) ;
                endfor ;
            fi ;
        ) ;
        clip grid to pxy ;
        bb := boundingbox grid ;
        grid := grid shifted (0,boxgridshift) ;
        setbounds grid to bb ;
        grid
    else :
        nullpicture
    fi
enddef ;

vardef graphic_grid (expr pxy, dx, dy, x, y) =
    if (bbheight(pxy)>dy) and (bbwidth(pxy)>dx) and (boxgridwidth>0) :
        save grid ; picture grid ;
        grid := image (
            for i = xpart llcorner pxy step dx until xpart lrcorner pxy :
                draw (i,ypart llcorner pxy) -- (i,ypart ulcorner pxy) withpen pencircle scaled boxgridwidth ;
            endfor ;
            for i = ypart llcorner pxy step dy until ypart ulcorner pxy :
                draw (xpart llcorner pxy,i) -- (xpart lrcorner pxy,i) withpen pencircle scaled boxgridwidth ;
            endfor
        ) shifted (x,y) ;
        clip grid to pxy ;
        grid
    else :
        nullpicture
    fi
enddef ;

def anchor_box (expr n,x,y,w,h,d) =
    currentpicture := currentpicture shifted (-x,-y) ;
enddef ;

let draw_area   = draw_box ;
let anchor_area = anchor_box ;
let anchor_par  = anchor_box ;

numeric sync_n[], sync_p[][], sync_w[][], sync_h[][], sync_d[][], sync_t[][] ;
pair sync_xy[][] ; color sync_c[][] ;

def ResetSyncTasks =
    path SyncPaths[] ; numeric SyncTasks[], NOfSyncPaths, CurrentSyncClass ;
    NOfSyncPaths := CurrentSyncClass := 0 ;
    if unknown SyncLeftOffset : numeric SyncLeftOffset ; SyncLeftOffset := 0          ; fi ;
    if unknown SyncWidth      : numeric SyncWidth      ; SyncWidth      := 0          ; fi ;
    if unknown SyncThreshold  : numeric SyncThreshold  ; SyncThreshold  := LineHeight ; fi ;
    if unknown SyncColor      : color   SyncColor      ; SyncColor      := .5white    ; fi ;
    if (SyncLeftOffset = 0) and (SyncWidth = 0) :
        SyncWidth := if known TextWidth : TextWidth else : -1cm fi ;
    fi ;
enddef ;

ResetSyncTasks ;

vardef SyncBox(expr n, i, leftoffset, width, topoffset, bottomoffset) =
    save o ; pair o ; o := (xpart llcorner PlainTextArea,ypart sync_xy[n][i]) ;
    o shifted (leftoffset,sync_h[n][i]+topoffset) --
    o shifted (width+leftoffset,sync_h[n][i]+topoffset) --
    o shifted (width+leftoffset,bottomoffset) --
    o shifted (leftoffset,bottomoffset) -- cycle
enddef ;

def SetSyncColor(expr n, i, c) =
    sync_c[n][i] := c ;
enddef ;

def SetSyncThreshold(expr n, i, th) =
    sync_th[n][i] := th ;
enddef ;

vardef TheSyncColor(expr n, i) =
    if known sync_c[n][i] : sync_c[n][i] else : SyncColor fi
enddef ;

vardef TheSyncThreshold(expr n, i) =
    if known sync_th[n][i] : sync_th[n][i] else : SyncThreshold fi
enddef ;

vardef PrepareSyncTasks(expr n, collapse, extendtop, prestartnext) =
    ResetSyncTasks ;
    if known sync_n[n] :
        CurrentSyncClass := n ;
        save ok, l, d ; boolean ok ; ok := false ; NOfSyncPaths := l := 0 ;
        for i=1 upto sync_n[n] :
            if RealPageNumber > sync_p[n][i] :
                l := i ;
            elseif RealPageNumber = sync_p[n][i] :
                NOfSyncPaths := NOfSyncPaths + 1 ;
                if not ok :
                    if i>1 :
                        if sync_t[n][i-1] = sync_t[n][i] :
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i ;
                        else :
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i-1, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i-1 ;
                            NOfSyncPaths := NOfSyncPaths + 1 ;
                            SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                            SyncTasks[NOfSyncPaths] := i ;
                        fi ;
                    else :
                        SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                        SyncTasks[NOfSyncPaths] := i ;
                    fi ;
                else :
                    SyncPaths[NOfSyncPaths] := SyncBox(n, i, SyncLeftOffset, SyncWidth, 0, -PaperHeight) ;
                    SyncTasks[NOfSyncPaths] := i ;
                fi ;
                ok := true ;
            fi ;
        endfor ;
        if (NOfSyncPaths = 0) and (l > 0) :
            NOfSyncPaths := 1 ;
            SyncPaths[NOfSyncPaths] := SyncBox(n, l, SyncLeftOffset, SyncWidth, PaperHeight, -PaperHeight) ;
            SyncTasks[NOfSyncPaths] := l ;
        fi ;
        if NOfSyncPaths > 0 :
            for i = 1 upto NOfSyncPaths-1 :
                SyncPaths[i] := topboundary SyncPaths[i] -- reverse topboundary SyncPaths[i+1] -- cycle ;
            endfor ;
            if unknown SyncThresholdMethod :
                numeric SyncThresholdMethod ; SyncThresholdMethod := 2 ;
            fi ;
            if extendtop :
                if SyncThresholdMethod = 1 :
                    if NOfSyncPaths>1 :
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[2]]) ;
                        if (SyncTasks[2]>1) and (d > 0pt) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[2]])) and (sync_p[n][SyncTasks[2]] = RealPageNumber) :
                            SyncPaths[2] := SyncPaths[2] topenlarged PaperHeight ;
                        fi ;
                    fi ;
                else :
                    for i = 1 upto NOfSyncPaths :
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[i]]) ;
                        if (d > 0) and (d <= TheSyncThreshold(n,sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[i]] = RealPageNumber) :
                            SyncPaths[i] := SyncPaths[i] topenlarged PaperHeight ;
                        fi ;
                    endfor ;
                fi ;
            fi ;
            if prestartnext :
                if NOfSyncPaths>1 :
                    if SyncTasks[NOfSyncPaths] < sync_n[n] : % there is a next one
                        d := ypart (ulcorner PlainTextArea - sync_xy[n][SyncTasks[NOfSyncPaths]+1]) ;
                        if (d > 0) and (d <= TheSyncThreshold(n, sync_t[n][SyncTasks[i]])) and (sync_p[n][SyncTasks[NOfSyncPaths]+1] = RealPageNumber+1) :
                            SyncPaths[NOfSyncPaths+1] :=
                                (xpart ulcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) --
                                (xpart urcorner SyncPaths[NOfSyncPaths],ypart llcorner PlainTextArea) --
                                lrcorner SyncPaths[NOfSyncPaths] --
                                llcorner SyncPaths[NOfSyncPaths] -- cycle ;
                            SyncTasks[NOfSyncPaths+1] := SyncTasks[NOfSyncPaths]+1 ;
                            NOfSyncPaths := NOfSyncPaths + 1 ;
                        fi ;
                    fi ;
                fi ;
            else :
                if NOfSyncPaths>1 :
                    d := ypart (sync_xy[n][SyncTasks[NOfSyncPaths]] - llcorner PlainTextArea) ;
                    if (d < TheSyncThreshold(n, SyncTasks[NOfSyncPaths])) :
                        NOfSyncPaths := NOfSyncPaths - 1 ;
                        SyncPaths[NOfSyncPaths] := SyncPaths[NOfSyncPaths] bottomenlarged PaperHeight ;
                    fi ;
                fi ;
            fi ;
            if (NOfSyncPaths>1) and collapse :
                save j ; numeric j ; j := 1 ;
                for i = 2 upto NOfSyncPaths :
                    if sync_t[n][SyncTasks[i]] = sync_t[n][SyncTasks[j]] :
                        SyncPaths[j] := boundingbox image (draw SyncPaths[i] ; draw SyncPaths[j] ; ) ;
                        SyncTasks[j] := SyncTasks[i] ;
                    else :
                        j := j + 1 ;
                        SyncPaths[j] := SyncPaths[i] ;
                        SyncTasks[j] := SyncTasks[i] ;
                    fi ;
                endfor ;
                NOfSyncPaths := j ;
            fi ;
        fi ;
    fi ;
enddef ;

def SyncTask(expr n) =
    if known SyncTasks[n] : SyncTasks[n] else : 0 fi
enddef ;

def FlushSyncTasks =
    for i = 1 upto NOfSyncPaths :
        ProcessSyncTask(SyncPaths[i], TheSyncColor(CurrentSyncClass,sync_t[CurrentSyncClass][SyncTasks[i]])) ;
    endfor ;
enddef ;

def ProcessSyncTask(expr p, c) =
    fill p withcolor c ;
enddef ;