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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
|
/* $XConsortium: regions.c,v 1.4 91/10/10 11:18:57 rws Exp $ */
/* Copyright International Business Machines, Corp. 1991
* All Rights Reserved
* Copyright Lexmark International, Inc. 1991
* All Rights Reserved
*
* License to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of IBM or Lexmark not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF
* ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
* AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE
* QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
* OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF THE
* SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE
* ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN NO EVENT SHALL
* IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
/* REGIONS CWEB V0023 LOTS */
/*
:h1 id=regions.REGIONS Module - Regions Operator Handler
This module is responsible for creating and manipulating regions.
&author. Jeffrey B. Lotspiech (lotspiech@almaden.ibm.com)
:h3.Include Files
The included files are:
*/
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "objects.h"
#include "spaces.h"
#include "regions.h"
#include "paths.h"
#include "curves.h"
#include "lines.h"
#include "pictures.h"
#include "fonts.h"
#include "hints.h"
#include "strokes.h" /* to pick up 'DoStroke' */
static int Unwind();
static int newfilledge();
static struct edgelist *splitedge();
static int vertjoin();
static int touches();
static int crosses();
static int edgemin();
static int edgemax();
static int discard();
static int edgecheck();
static struct edgelist *NewEdge();
struct edgelist *swathxsort(); /* 'SortSwath' function */
extern struct XYspace *IDENTITY;
/*
:h3.Functions Provided to the TYPE1IMAGER User
This module provides the following TYPE1IMAGER entry points:
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
:h3.Functions Provided to Other Modules
This module provides the following entry points to other modules:
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
:h3.Macros Provided to Other Modules
:h4.GOING_TO() - Macro Predicate Needed for Changing Direction, Etc.
The actual generation of run end lists (edge boundaries) is left
to the low level rasterizing modules, LINES and CURVES. There
are some global region-type
questions that occur when doing a low-level
rasterization:
:ol.
:li.Did we just change direction in Y and therefore need to start
a new edge?
:li.Did we run out of allocated edge space?
:li.Do the minimum or maximum X values for the current edge need
updating?
:eol.
In general the REGIONS is not smart enough to answer those questions
itself. (For example, determining if and when a curve changes direction
may need detailed curve knowledge.) Yet, this must be done efficiently.
We provide a macro "GOING_TO" where the invoker tells us where it is
heading for (x2,y2), plus where it is now (x1,y1), plus the current
region under construction, and the macro answers the questions above.
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
:h2.Data Structures Used to Represent Regions
:h3.The "region" Structure
The region structure is an anchor for a linked list of "edgelist"
structures (see :hdref refid=edgelist..). It also summarizes the
information in the edgelist structures (for example, the bounding
box of the region). And, it contains scratch areas used during
the creation of a region.
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
The ISOPTIMIZED flag tells us if we've put a permanent region in
'optimal' form.
*/
#define ISOPTIMIZED(flag) ((flag)&0x10)
/*
The ISRECTANGULAR flag tells us if a region is a rectangle. We don't
always notice rectangles--if this flag is set, the region definitely
is a rectangle, but some rectangular regions will not have the flag
set. The flag is used to optimize some paths.
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
:h4."INFINITY" - A Constant Region Structure of Infinite Extent
Infinity is the complement of a null area:
Note - removed the refcount = 1 init, replaced with references = 2 3-26-91 PNM
*/
static struct region infinity = { REGIONTYPE,
ISCOMPLEMENT(ON)+ISINFINITE(ON)+ISPERMANENT(ON)+ISIMMORTAL(ON), 2,
{0, 0}, {0, 0},
0, 0, 0, 0,
NULL, NULL,
0, 0, 0, 0, 0, NULL, NULL,
NULL, 0, NULL, NULL };
/* we rename INFINITY to T1_INFINITY. Anyhow it is currently not used */
struct region *T1_INFINITY = &infinity;
/*
:h4."EmptyRegion" - A Region Structure with Zero Area
This structure is used to initialize the region to be built in
Interior():
Note - replaced refcount = 1 init with references = 2 3-26-91 PNM
*/
/*SHARED LINE(S) ORIGINATED HERE*/
struct region EmptyRegion = { REGIONTYPE,
ISPERMANENT(ON)+ISIMMORTAL(ON), 2,
{0, 0}, {0, 0},
MAXPEL, MAXPEL, MINPEL, MINPEL,
NULL, NULL,
0, 0, 0, 0, 0, NULL, NULL,
NULL, 0, NULL, NULL };
/*
:h3 id=edgelist.The "edgelist" Structure
Regions are represented by a linked list of 'edgelist' structures.
When a region is complete, the structures are paired, one for the
left and one for the right edge. While a region is being built,
this rule may be violated temporarily.
An 'edgelist' structure contains the X values for a given span
of Y values. The (X,Y) pairs define an edge. We use the crack
and edge coordinate system, so that integer values of X and Y
go between pels. The edge is defined between the minimum Y and
maximum Y.
The linked list is kept sorted from top to bottom, that is, in
increasing y. Also, if 'e1' is an edgelist structure and 'e2' is the
next one in the list, they must have exactly the same ymin,ymax values
or be totally disjoint. These two requirements mean that if e2's ymin
is less than e1's ymax, it must be exactly equal to e1's ymin. A
sublist of structures with identical ymin and ymax values is called a
'swath'.
In addition, edgelist structures are separately linked together based
on what subpath originally created them; each subpath is kept as a
separate circular linked list. This information is ignored unless
continuity checking is invoked. See :hdref refid=subpath. for a
complete description of this.
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
The "edgelist" structure follows the convention of TYPE1IMAGER user
objects, having a type field and a flag field as the first two
elements. However, the user never sees "edgelist" structures
directly; he is given handles to "region" structures only.
By having a type field, we can use the "copy" feature of Allocate()
to duplicate edge lists quickly.
We also define two flag bits for this structure. The ISDOWN bit is set
if the edge is going in the direction of increasing Y. The ISAMBIGUOUS
bit is set if the edge is identical to its neighbor (edge->link); such
edges may be "left" when they should be "right", or vice versa,
unnecessarily confusing the continuity checking logic. The FixSubPaths()
routine in HINTS will swap ambiguous edges if that avoids crossing edges;
see :hdref refid=fixsubp..
*/
/*SHARED LINE(S) ORIGINATED HERE*/
/*
:h3.KillRegion() - Destroys a Region
KillRegion nominally just decrements the reference count to that region.
If the reference count becomes 0, all memory associated with it is
freed. We just follow the linked list, freeing as we go, then kill any
associated (thresholded) picture.
Note - added conditional return based on references 3-26-91 PNM
*/
void KillRegion(area)
register struct region *area; /* area to free */
{
register struct edgelist *p; /* loop variable */
register struct edgelist *next; /* loop variable */
if (area->references < 0)
abort("KillRegion: negative reference count", 28);
if ( (--(area->references) > 1) ||
( (area->references == 1) && !ISPERMANENT(area->flag) ) )
return;
for (p=area->anchor; p != NULL; p=next) {
next = p->link;
Free(p);
}
/* KillPicture-macro removed from sources (RMz, 2001-04-01)
if (area->thresholded != NULL)
KillPicture(area->thresholded);
*/
Free(area);
}
/*
:h3.CopyRegion() - Makes a Copy of a Region
*/
struct region *CopyRegion(area)
register struct region *area; /* region to duplicate */
{
register struct region *r; /* output region built here */
register struct edgelist *last=NULL; /* loop variable */
register struct edgelist *p,*newp; /* loop variables */
r = (struct region *)Allocate(sizeof(struct region), area, 0);
r->anchor = NULL;
for (p=area->anchor; VALIDEDGE(p); p=p->link) {
newp = NewEdge(p->xmin, p->xmax, p->ymin, p->ymax, p->xvalues, ISDOWN(p->flag));
newp->fpx1 = p->fpx1;
newp->fpx2 = p->fpx2;
newp->fpy1 = p->fpy1;
newp->fpy2 = p->fpy2;
if (r->anchor == NULL)
r->anchor = last = newp;
else
last->link = newp;
last = newp;
}
if (area->thresholded != NULL)
/* replaced DupPicture with Dup() 3-26-91 PNM */
r->thresholded = (struct picture *)Dup(area->thresholded);
return(r);
}
/*
:h4.NewEdge() - Allocates and Returns a New "edgelist" Structure
We allocate space for the X values contiguously with the 'edgelist'
structure that locates them. That way, we only have to free the
edgelist structure to free all memory associated with it. Damn
clever, huh?
*/
static struct edgelist *NewEdge(xmin, xmax, ymin, ymax, xvalues, isdown)
pel xmin,xmax; /* X extent of edge */
pel ymin,ymax; /* Y extent of edge */
pel *xvalues; /* list of X values for entire edge */
int isdown; /* flag: TRUE means edge progresses downward */
{
static struct edgelist template = {
EDGETYPE, 0, 1, NULL, NULL,
0, 0, 0, 0, NULL };
register struct edgelist *r; /* returned structure */
register int iy; /* ymin adjusted for 'long' alignment purposes */
IfTrace2((RegionDebug),"....new edge: ymin=%d, ymax=%d ",
(LONG)ymin, (LONG) ymax);
if (ymin >= ymax)
abort("newedge: height not positive", 29);
/*
We are going to copy the xvalues into a newly allocated area. It
helps performance if the values are all "long" aligned. We can test
if the xvalues are long aligned by ANDing the address with the
(sizeof(long) - 1)--if non zero, the xvalues are not aligned well. We
set 'iy' to the ymin value that would give us good alignment:
*/
iy = ymin - (((unsigned long) xvalues) & (sizeof(LONG) - 1)) / sizeof(pel);
r = (struct edgelist *)Allocate(sizeof(struct edgelist), &template,
(ymax - iy) * sizeof(pel));
if (isdown) r->flag = ISDOWN(ON);
r->xmin = xmin;
r->xmax = xmax;
r->ymin = ymin;
r->ymax = ymax;
r->xvalues = (pel *) FOLLOWING(r);
if (ymin != iy) {
r->xvalues += ymin - iy;
xvalues -= ymin - iy;
}
/*
We must round up (ymax - iy) so we get the ceiling of the number of
longs. The destination must be able to hold these extra bytes because
Allocate() makes everything it allocates be in multiples of longs.
*/
LONGCOPY(&r[1], xvalues, (ymax - iy) * sizeof(pel) + sizeof(LONG) - 1);
IfTrace1((RegionDebug),"result=%p\n", r);
return(r);
}
/*
:h2.Building Regions
:h3.Interior() - Iterate Through a Path, Building a Region
This routine is the workhorse driver routine that iterates through a
path, calling the appropriate stepping routines to actually produce the
run end "edgelist" structures.
:ol.
:li."Interior" calls StepLine or StepConic or StepBezier as appropriate
to produce run ends.
:li.Occasionally these routines will notice a change in Y direction
and will call ChangeDirection (through the GOING_TO macro); this is
a call back to the REGIONS module.
:li.ChangeDirection will call whatever function is in the region
structure; for Interior, this function is 'newfilledge'.
:li.Newfilledge will call NewEdge to create a new edgelist structure,
then, call SortSwath to sort it onto the linked list being built at
the region "anchor".
:eol.
By making the function called by ChangeDirection be a parameter of the
region, we allow the same ChangeDirection logic to be used by stroking.
*/
/*SHARED LINE(S) ORIGINATED HERE*/
struct region *Interior(p, fillrule)
register struct segment *p; /* take interior of this path */
register int fillrule; /* rule to follow if path crosses itself */
{
register fractpel x,y; /* keeps ending point of path segment */
fractpel lastx,lasty; /* previous x,y from path segment before */
register struct region *R; /* region I will build */
register struct segment *nextP; /* next segment of path */
char tempflag; /* flag; is path temporary? */
char Cflag; /* flag; should we apply continuity? */
IfTrace2((MustTraceCalls),". INTERIOR(%p, %d)\n", p, (LONG) fillrule);
if (p == NULL)
return(NULL);
/*
Establish the 'Cflag' continuity flag based on user's fill rule and
our own 'Continuity' pragmatic (0: never do continuity, 1: do what
user asked, >1: do it regardless).
*/
if (fillrule > 0) {
Cflag = Continuity > 0;
fillrule -= CONTINUITY;
}
else
Cflag = Continuity > 1;
ARGCHECK((fillrule != WINDINGRULE && fillrule != EVENODDRULE),
"Interior: bad fill rule", NULL, NULL, (1,p), struct region *);
if (p->type == TEXTTYPE)
/* if (fillrule != EVENODDRULE)
else */
return((struct region *)UniquePath(p));
if (p->type == STROKEPATHTYPE){
if (fillrule == WINDINGRULE)
return((struct region *)DoStroke(p));
else
p = CoercePath(p);
}
R = (struct region *)Allocate(sizeof(struct region), &EmptyRegion, 0);
ARGCHECK(!ISPATHANCHOR(p), "Interior: bad path", p, R, (0), struct region *);
ARGCHECK((p->type != MOVETYPE), "Interior: path not closed", p, R, (0), struct region *);
/* changed definition from !ISPERMANENT to references <= 1 3-26-91 PNM */
tempflag = (p->references <= 1); /* only first segment in path is so marked */
if (!ISPERMANENT(p->flag)) p->references -= 1;
R->newedgefcn = newfilledge;
/*
Believe it or not, "R" is now completely initialized. We are counting
on the copy of template to get other fields the way we want them,
namely
:ol.
:li.anchor = NULL
:li.xmin, ymin, xmax, ymax, to minimum and maximum values respectively.
:eol.
Anchor = NULL is very
important to ChangeDirection.
See :hdref refid=CD..
To minimize problems of "wrapping" in our pel arithmetic, we keep an
origin of the region which is the first move. Hopefully, that keeps
numbers within plus or minus 32K pels.
*/
R->origin.x = 0/*TOFRACTPEL(NEARESTPEL(p->dest.x))*/;
R->origin.y = 0/*TOFRACTPEL(NEARESTPEL(p->dest.y))*/;
lastx = - R->origin.x;
lasty = - R->origin.y;
/*
ChangeDirection initializes other important fields in R, such as
lastdy, edge, edgeYstop, edgexmin, and edgexmax. The first segment
is a MOVETYPE, so it will be called first.
*/
/*
Note: Hinting is completely performed in charspace coordinates
in the Type 1 module. Therefore, I have removed the code
to handle hint segments. (2002-08-11)
*/
while (p != NULL) {
x = lastx + p->dest.x;
y = lasty + p->dest.y;
nextP = p->link;
switch(p->type) {
case LINETYPE:
StepLine(R, lastx, lasty, x, y);
break;
case CONICTYPE:
/* 2nd order Beziers not implemented! */
break;
case BEZIERTYPE:
{
register struct beziersegment *bp = (struct beziersegment *) p;
StepBezier(R, lastx, lasty,
lastx + bp->B.x, lasty + bp->B.y,
lastx + bp->C.x,
lasty + bp->C.y,
x, y);
}
break;
case MOVETYPE:
/* At this point we have encountered a MOVE segment. This breaks the
path, making it disjoint. */
if (p->last == NULL) /* i.e., not first in path */
ChangeDirection(CD_LAST, R, lastx, lasty, (fractpel) 0, (fractpel) 0, (fractpel) 0);
ChangeDirection(CD_FIRST, R, x, y, (fractpel) 0, (fractpel) 0, (fractpel) 0);
/* We'll just double check for closure here. We forgive an appended
MOVETYPE at the end of the path, if it isn't closed: */
if (!ISCLOSED(p->flag) && p->link != NULL)
return((struct region *)ArgErr("Fill: sub-path not closed", p, NULL));
break;
default:
abort("Interior: path type error", 30);
}
/* We're done with this segment. Advance to the next path segment in
the list, freeing this one if necessary: */
lastx = x; lasty = y;
if (tempflag)
Free(p);
p = nextP;
}
ChangeDirection(CD_LAST, R, lastx, lasty, (fractpel) 0, (fractpel) 0, (fractpel) 0);
R->ending.x = lastx;
R->ending.y = lasty;
/* Finally, clean up the region's based on the user's 'fillrule' request: */
if (Cflag)
ApplyContinuity(R);
if (fillrule == WINDINGRULE)
Unwind(R->anchor);
return R;
}
/*
:h4.Unwind() - Discards Edges That Fail the Winding Rule Test
The winding rule says that upward going edges should be paired with
downward going edges only, and vice versa. So, if two upward edges
or two downward edges are nominally left/right pairs, Unwind() should
discard the second one. Everything should balance; we should discard
an even number of edges; of course, we abort if we don't.
*/
static int Unwind(area)
register struct edgelist *area; /* input area modified in place */
{
register struct edgelist *last=NULL,*next; /* struct before and after current one */
register int y; /* ymin of current swath */
register int count,newcount; /* winding count registers */
IfTrace1((RegionDebug>0),"...Unwind(%p)\n", area);
while (VALIDEDGE(area)) {
count = 0;
y = area->ymin;
do {
next = area->link;
if (ISDOWN(area->flag))
newcount = count + 1;
else
newcount = count - 1;
if (count == 0 || newcount == 0)
last = area;
else
discard(last, next);
count = newcount;
area = next;
} while (area != NULL && area->ymin == y);
if (count != 0)
abort("Unwind: uneven edges", 31);
}
/* We retunr a value for ANSI-C-compiler */
return(0);
}
/*
:h3."workedge" Array
This is a statically allocated array where edges are built
before being copied into more permanent storage by NewEdge().
*/
#ifndef MAXEDGE
#define MAXEDGE 1000
#endif
static pel workedge[MAXEDGE];
static pel *currentworkarea = workedge;
static pel currentsize = MAXEDGE;
/*
:h3 id=cd.ChangeDirection() - Called When Y Direction Changes
The rasterizing routines call this entry point when they detect
a change in Y. We then build the current edge and sort it into
emerging edgelist at 'anchor' by calling whatever "newedgefcn"
is appropriate.
*/
void ChangeDirection(type, R, x, y, dy, x2, y2)
int type; /* CD_FIRST, CD_CONTINUE, or CD_LAST */
register struct region *R; /* region in which we are changing direction */
fractpel x,y; /* current beginning x,y */
fractpel dy; /* direction and magnitude of change in y */
{
register fractpel ymin,ymax; /* minimum and maximum Y since last call */
register fractpel x_at_ymin,x_at_ymax; /* their respective X's */
register pel iy; /* nearest integer pel to 'y' */
register pel idy; /* nearest integer pel to 'dy' */
register int ydiff; /* allowed Y difference in 'currentworkarea' */
IfTrace4((RegionDebug>0),"Change Y direction (%d) from (%d,%d), dy=%d\n",
(LONG) type, x, y, dy);
if (type != CD_FIRST) {
if (R->lastdy > 0) {
ymin = R->firsty;
x_at_ymin = R->firstx;
ymax = y;
x_at_ymax = x;
}
else {
ymin = y;
x_at_ymin = x;
ymax = R->firsty;
x_at_ymax = R->firstx;
}
if (ymax < ymin)
abort("negative sized edge?", 32);
(*R->newedgefcn)(R, R->edgexmin, R->edgexmax, ymin, ymax,
R->lastdy > 0, x_at_ymin, x_at_ymax,
x, y, x2, y2);
}
R->firsty = y;
R->firstx = x;
R->lastdy = dy;
iy = NEARESTPEL(y);
idy = NEARESTPEL(dy);
if (currentworkarea != workedge && idy < MAXEDGE && idy > -MAXEDGE) {
NonObjectFree(currentworkarea);
currentworkarea = workedge;
currentsize = MAXEDGE;
}
ydiff = currentsize - 1;
if (dy > 0) {
R->edge = ¤tworkarea[-iy];
R->edgeYstop = TOFRACTPEL(ydiff + iy) + FPHALF;
}
else {
R->edge = ¤tworkarea[ydiff - iy];
R->edgeYstop = TOFRACTPEL(iy - ydiff) - FPHALF;
}
R->edgexmax = R->edgexmin = x;
/*
If this is the end of a subpath, we complete the subpath circular
chain:
*/
if (type == CD_LAST && R->lastedge != NULL) {
register struct edgelist *e = R->firstedge;
while (e->subpath != NULL)
e = e->subpath;
e->subpath = R->lastedge;
R->lastedge = R->firstedge = NULL;
}
}
/*
:h3 id=newfill.newfilledge() - Called When We Have a New Edge While Filling
This is the prototypical "newedge" function passed to "Rasterize" and
stored in "newedgefcn" in the region being built.
If the edge is non-null, we sort it onto the list of edges we are
building at "anchor".
This function also has to keep the bounding box of the region
up to date.
*/
static int newfilledge(R, xmin, xmax, ymin, ymax, isdown, x1, y1, x2, y2)
register struct region *R; /* region being built */
fractpel xmin,xmax; /* X range of this edge */
fractpel ymin,ymax; /* Y range of this edge */
int isdown; /* flag: TRUE means edge goes down, else up */
fractpel x1;
fractpel y1;
fractpel x2;
fractpel y2;
{
register pel pelxmin,pelymin,pelxmax,pelymax; /* pel versions of bounds */
register struct edgelist *edge; /* newly created edge */
pelymin = NEARESTPEL(ymin);
pelymax = NEARESTPEL(ymax);
if (pelymin == pelymax)
return(0);
pelxmin = NEARESTPEL(xmin);
pelxmax = NEARESTPEL(xmax);
if (pelxmin < R->xmin) R->xmin = pelxmin;
if (pelxmax > R->xmax) R->xmax = pelxmax;
if (pelymin < R->ymin) R->ymin = pelymin;
if (pelymax > R->ymax) R->ymax = pelymax;
edge = NewEdge(pelxmin, pelxmax, pelymin, pelymax, &R->edge[pelymin], isdown);
/* Save maximum and minimum values of edge in order to be able to
use them in ApplyContinity. */
edge->fpx1 = x1;
edge->fpy1 = y1;
edge->fpx2 = x2;
edge->fpy2 = y2;
edge->subpath = R->lastedge;
R->lastedge = edge;
if (R->firstedge == NULL)
R->firstedge = edge;
R->anchor = SortSwath(R->anchor, edge, swathxsort);
/*
{
struct region* r = (struct region*) R;
struct edgelist* el = (struct edgelist*) (r->anchor);
while ( el != 0 )
{
long i = 0;
short int* spl;
short int* spr;
int xl;
int xr;
printf( "Region after Sort (NE=%ld) : ymin=%d, ymax=%d, xmin=%d, xmax=%d\n",
callcount, el->ymin, el->ymax, el->xmin, el->xmax);
for ( i=0; i<((el->ymax)-(el->ymin)); i++ ) {
spl = el->xvalues;
if ( el->link != NULL ) {
spr = el->link->xvalues;
xl = spl[i];
xr = spr[i];
printf( "Region after Sort (NE=%ld): y=%ld xleft=%d, xright=%d\n",
callcount, el->ymin + i, xl, xr);
}
else {
printf( "Region after Sort (NE=%ld): y=%ld xval=%d\n",
callcount, el->ymin + i, spl[i]);
}
}
if ( el->link != 0 )
el = el->link->link;
else
break;
}
}
++callcount;
*/
return 0;
}
/*
:h2.Sorting Edges
:h3.SortSwath() - Vertically Sort an Edge into a Region
This routine sorts an edge or a pair of edges into a growing region,
so that the region maintains its top-to-bottom, left-to-right form.
The rules for sorting horizontally may vary depending on what you
are doing, but the rules for vertical sorting are always the same.
This routine is passed an argument that is a function that will
perform the horizontal sort on demand (for example, swathxsort() or
SwathUnion()).
This is a recursive routine. A new edge (or edge pair) may overlap
the list I am building in strange and wonderful ways. Edges may
cross. When this happens, my strategy is to split the incoming edge
(or the growing list) in two at that point, execute the actual sort on
the top part of the split, and recursively call myself to figure out
exactly where the bottom part belongs.
*/
#define TOP(e) ((e)->ymin) /* the top of an edge (for readability */
#define BOTTOM(e) ((e)->ymax) /* the bottom of an edge (for readability */
struct edgelist *SortSwath(anchor, edge, swathfcn)
struct edgelist *anchor; /* list being built */
register struct edgelist *edge; /* incoming edge or pair of edges */
struct edgelist *(*swathfcn)(); /* horizontal sorter */
{
register struct edgelist *before,*after;
struct edgelist base;
if (RegionDebug > 0) {
if (RegionDebug > 2) {
IfTrace3(TRUE,"SortSwath(anchor=%p, edge=%p, fcn=%p)\n",
anchor, edge, swathfcn);
}
else {
IfTrace3(TRUE,"SortSwath(anchor=%p, edge=%p, fcn=%p)\n",
anchor, edge, swathfcn);
}
}
if (anchor == NULL)
return(edge);
before = &base;
before->ymin = before->ymax = MINPEL;
before->link = after = anchor;
/*
If the incoming edge is above the current list, we connect the current
list to the bottom of the incoming edge. One slight complication is
if the incoming edge overlaps into the current list. Then, we
first split the incoming edge in two at the point of overlap and recursively
call ourselves to sort the bottom of the split into the current list:
*/
if (TOP(edge) < TOP(after)) {
if (BOTTOM(edge) > TOP(after)) {
after = SortSwath(after, splitedge(edge, TOP(after)), swathfcn);
}
vertjoin(edge, after);
return(edge);
}
/*
At this point the top of edge is not higher than the top of the list,
which we keep in 'after'. We move the 'after' point down the list,
until the top of the edge occurs in the swath beginning with 'after'.
If the bottom of 'after' is below the bottom of the edge, we have to
split the 'after' swath into two parts, at the bottom of the edge.
If the bottom of 'after' is above the bottom of the swath,
*/
while (VALIDEDGE(after)) {
if (TOP(after) == TOP(edge)) {
if (BOTTOM(after) > BOTTOM(edge))
vertjoin(after, splitedge(after, BOTTOM(edge)));
else if (BOTTOM(after) < BOTTOM(edge)) {
after = SortSwath(after,
splitedge(edge, BOTTOM(after)), swathfcn);
}
break;
}
else if (TOP(after) > TOP(edge)) {
IfTrace0((BOTTOM(edge) < TOP(after) && RegionDebug > 0),
"SortSwath: disjoint edges\n");
if (BOTTOM(edge) > TOP(after)) {
after = SortSwath(after,
splitedge(edge, TOP(after)), swathfcn);
}
break;
}
else if (BOTTOM(after) > TOP(edge))
vertjoin(after, splitedge(after, TOP(edge)));
before = after;
after = after->link;
}
/*
At this point 'edge' exactly corresponds in height to the current
swath pointed to by 'after'.
*/
if (after != NULL && TOP(after) == TOP(edge)) {
before = (*swathfcn)(before, edge);
after = before->link;
}
/*
At this point 'after' contains all the edges after 'edge', and 'before'
contains all the edges before. Whew! A simple matter now of adding
'edge' to the linked list in its rightful place:
*/
before->link = edge;
if (RegionDebug > 1) {
IfTrace3(TRUE,"SortSwath: in between %p and %p are %p",
before, after, edge);
while (edge->link != NULL) {
edge = edge->link;
IfTrace1(TRUE," and %p", edge);
}
IfTrace0(TRUE,"\n");
}
else
for (; edge->link != NULL; edge = edge->link) { ; }
edge->link = after;
return base.link;
}
/*
:h3.splitedge() - Split an Edge or Swath in Two at a Given Y Value
This function returns the edge or swath beginning at the Y value, and
is guaranteed not to change the address of the old swath while splitting
it.
*/
static struct edgelist *splitedge(list, y)
struct edgelist *list; /* area to split */
register pel y; /* Y value to split list at */
{
register struct edgelist *new; /* anchor for newly built list */
register struct edgelist *last=NULL; /* end of newly built list */
register struct edgelist *r; /* temp pointer to new structure */
register struct edgelist *lastlist; /* temp pointer to last 'list' value */
IfTrace2((RegionDebug > 1),"splitedge of %p at %d ", list, (LONG) y);
lastlist = new = NULL;
while (list != NULL) {
if (y < list->ymin)
break;
if (y >= list->ymax)
abort("splitedge: above top of list", 33);
if (y == list->ymin)
abort("splitedge: would be null", 34);
r = (struct edgelist *)Allocate(sizeof(struct edgelist), list, 0);
/*
At this point 'r' points to a copy of the single structure at 'list'.
We will make 'r' be the new split 'edgelist'--the lower half.
We don't bother to correct 'xmin' and 'xmax', we'll take the
the pessimistic answer that results from using the old values.
*/
r->ymin = y;
r->xvalues = list->xvalues + (y - list->ymin);
/*
Update the fpx values so that ApplyContinuity() will continue
to work. Note that high precision is a fake, here!
*/
r->fpx1 = (r->xvalues[0]) << FRACTBITS;
r->fpx2 = (list->xvalues[list->ymax - list->ymin - 1]) << FRACTBITS;
list->fpx2 = (list->xvalues[y - list->ymin -1]) << FRACTBITS;
/*
Note that we do not need to allocate new memory for the X values,
they can remain with the old "edgelist" structure. We do have to
update that old structure so it is not as high:
*/
list->ymax = y;
/*
Insert 'r' in the subpath chain:
*/
r->subpath = list->subpath;
list->subpath = r;
/*
Now attach 'r' to the list we are building at 'new', and advance
'list' to point to the next element in the old list:
*/
if (new == NULL) {
new = r;
}
else
last->link = r;
last = r;
lastlist = list;
list = list->link;
}
/*
At this point we have a new list built at 'new'. We break the old
list at 'lastlist', and add the broken off part to the end of 'new'.
Then, we return the caller a pointer to 'new':
*/
if (new == NULL)
abort("null splitedge", 35);
lastlist->link = NULL;
last->link = list;
IfTrace1((RegionDebug > 1),"yields %p\n", new);
return(new);
}
/*
:h3.vertjoin() - Join Two Disjoint Edge Lists Vertically
The two edges must be disjoint vertically.
*/
static int vertjoin(top, bottom)
register struct edgelist *top; /* uppermost region */
register struct edgelist *bottom; /* bottommost region */
{
if (BOTTOM(top) > TOP(bottom))
abort("vertjoin not disjoint", 36);
for (; top->link != NULL; top=top->link) { ; }
top->link = bottom;
return(0);
}
/*
:h3.swathxsort() - Sorting by X Values
We need to sort 'edge' into its rightful
place in the swath by X value, taking care that we do not accidentally
advance to the next swath while searching for the correct X value. Like
all swath functions, this function returns a pointer to the edge
BEFORE the given edge in the sort.
*/
struct edgelist *swathxsort(before0, edge)
register struct edgelist *before0; /* edge before this swath */
register struct edgelist *edge; /* input edge */
{
register struct edgelist *before;
register struct edgelist *after;
register pel y=0;
before = before0;
after = before->link;
while (after != NULL && TOP(after) == TOP(edge)) {
register pel *x1,*x2;
y = TOP(edge);
x1 = after->xvalues;
x2 = edge->xvalues;
while (y < BOTTOM(edge) && *x1 == *x2) {
x1++; x2++; y++;
}
if (y >= BOTTOM(edge)) {
edge->flag |= ISAMBIGUOUS(ON);
after->flag |= ISAMBIGUOUS(ON);
break;
}
if (*x1 >= *x2)
break;
before = after;
after = after->link;
}
/*
At this point, 'edge' is between 'before' and 'after'. If 'edge' didn't
cross either of those other edges, we would be done. We check for
crossing. If it does cross, we split the problem up by calling SortSwath
recursively with the part of the edge that is below the crossing point:
*/
{
register int h0,h; /* height of edge--number of scans */
h0 = h = BOTTOM(edge) - y;
y -= TOP(edge);
if (h0 <= 0) {
IfTrace0((RegionDebug>0),"swathxsort: exactly equal edges\n");
return(before);
}
if (TOP(before) == TOP(edge))
h -= crosses(h, &before->xvalues[y], &edge->xvalues[y]);
if (after != NULL && TOP(after) == TOP(edge))
h -= crosses(h, &edge->xvalues[y], &after->xvalues[y]);
if (h < h0) {
SortSwath(before0->link,
splitedge(edge, TOP(edge) + y + h),
swathxsort);
}
}
return(before);
}
/*
:h3.SwathUnion() - Union Two Edges by X Value
We have a left and right edge that must be unioned into a growing
swath. If they are totally disjoint, they are just added in. The
fun comes in they overlap the existing edges. Then some edges
will disappear.
*/
struct edgelist *SwathUnion(before0, edge)
register struct edgelist *before0; /* edge before the swath */
register struct edgelist *edge; /* list of two edges to be unioned */
{
register int h; /* saves height of edge */
register struct edgelist *rightedge; /* saves right edge of 'edge' */
register struct edgelist *before,*after; /* edge before and after */
int h0; /* saves initial height */
IfTrace2((RegionDebug > 1),"SwathUnion entered, before=%p, edge=%p\n",
before0, edge);
h0 = h = edge->ymax - edge->ymin;
if (h <= 0)
abort("SwathUnion: 0 height swath?", 37);
before = before0;
after = before->link;
while (after != NULL && TOP(after) == TOP(edge)) {
register struct edgelist *right;
right = after->link;
if (right->xvalues[0] >= edge->xvalues[0])
break;
before = right;
after = before->link;
}
/*
This is the picture at this point. 'L' indicates a left hand edge,
'R' indicates the right hand edge.
'<--->' indicates the degree of uncertainty as to its placement
relative to other edges:
:xmp atomic.
before after
R <---L----> R L R L R
<---L---> <------R-------------------------->
edge
:exmp.
In case the left of 'edge' touches 'before', we need to reduce
the height by that amount.
*/
if (TOP(before) == TOP(edge))
h -= touches(h, before->xvalues, edge->xvalues);
rightedge = edge->link;
if (after == NULL || TOP(after) != TOP(edge) ||
after->xvalues[0] > rightedge->xvalues[0]) {
IfTrace2((RegionDebug > 1),
"SwathUnion starts disjoint: before=%p after=%p\n",
before, after);
/*
On this side of the the above 'if', the new edge is disjoint from the
existing edges in the swath. This is the picture:
:xmp atomic.
before after
R L R L R L R
L R
edge
:exmp.
We will verify it remains disjoint for the entire height. If the
situation changes somewhere down the edge, we split the edge at that
point and recursively call ourselves (through 'SortSwath') to figure
out the new situation:
*/
if (after != NULL && TOP(after) == TOP(edge))
h -= touches(h, rightedge->xvalues, after->xvalues);
if (h < h0)
SortSwath(before0->link, splitedge(edge, edge->ymin + h), t1_SwathUnion);
/* go to "return" this edge pair; it is totally disjoint */
}
else {
/*
At this point, at the 'else', we know that the
new edge overlaps one or more pairs in the existing swath. Here is
a picture of our knowledge and uncertainties:
:xmp atomic.
before after
R L R L R L R
<---L---> <---R------------------->
edge
:exmp.
We need to move 'after' along until it is to the right of the
right of 'edge'. ('After' should always point to a left edge of a pair:)
*/
register struct edgelist *left; /* variable to keep left edge in */
do {
left = after;
after = (after->link)->link;
} while (after != NULL && TOP(after) == TOP(edge)
&& after->xvalues[0] <= rightedge->xvalues[0]);
/*
At this point this is the picture:
:xmp atomic.
before left after
R L R L R L R
<---L---> <---R--->
edge
:exmp.
We need to verify that the situation stays like this all the way
down the edge. Again, if the
situation changes somewhere down the edge, we split the edge at that
point and recursively call ourselves (through 'SortSwath') to figure
out the new situation:
*/
h -= crosses(h, left->xvalues, rightedge->xvalues);
h -= crosses(h, edge->xvalues, ((before->link)->link)->xvalues);
if (after != NULL && TOP(after) == TOP(edge))
h -= touches(h, rightedge->xvalues, after->xvalues);
IfTrace3((RegionDebug > 1),
"SwathUnion is overlapped until %d: before=%p after=%p\n",
(LONG) TOP(edge) + h, before, after);
/*
OK, if we touched either of our neighbors we need to split at that point
and recursively sort the split edge onto the list. One tricky part
is that when we recursively sort, 'after' will change if it was not
in our current swath:
*/
if (h < h0) {
SortSwath(before0->link,
splitedge(edge, edge->ymin + h),
t1_SwathUnion);
if (after == NULL || TOP(after) != TOP(edge))
for (after = before0->link;
TOP(after) == TOP(edge);
after = after->link) { ; }
}
/*
Now we need to augment 'edge' by the left and right of the overlapped
swath, and to discard all edges between before and after, because they
were overlapped and have been combined with the new incoming 'edge':
*/
edge->xmin = TYPE1_MIN(edge->xmin, (before->link)->xmin);
edge->xmax = TYPE1_MIN(edge->xmax, (before->link)->xmax);
edgemin(h, edge->xvalues, (before->link)->xvalues);
rightedge->xmin = TYPE1_MAX(rightedge->xmin, (left->link)->xmin);
rightedge->xmax = TYPE1_MAX(rightedge->xmax, (left->link)->xmax);
edgemax(h, rightedge->xvalues, (left->link)->xvalues);
discard(before, after);
}
return(before);
}
/*
:h3.swathrightmost() - Simply Sorts New Edge to Rightmost of Swath
Like all swath functions, this function returns a pointer to the edge
BEFORE the given edge in the sort.
*/
struct edgelist *swathrightmost(before, edge)
register struct edgelist *before; /* edge before this swath */
register struct edgelist *edge; /* input edge */
{
register struct edgelist *after;
after = before->link;
while (after != NULL && TOP(after) == TOP(edge)) {
before = after;
after = after->link;
}
return(before);
}
/*
:h3.touches() - Returns the Remaining Height When Two Edges Touch
So, it will return 0 if they never touch. Allows incredibly(?) mnemonic
if (touches(...)) construct.
*/
static int touches(h, left, right)
register int h;
register pel *left,*right;
{
for (; h > 0; h--)
if (*left++ >= *right++)
break;
return(h);
}
/*
:h3.crosses() - Returns the Remaining Height When Two Edges Cross
So, it will return 0 if they never cross.
*/
static int crosses(h, left, right)
register int h;
register pel *left,*right;
{
for (; h > 0; h--)
if (*left++ > *right++)
break;
return(h);
}
/*
:h3.cedgemin() - Stores the Mininum of an Edge and an X Value
*/
static int cedgemin(h, e1, x)
register int h;
register pel *e1;
register pel x;
{
for (; --h >= 0; e1++)
if (*e1 > x)
*e1 = x;
return(0);
}
/*
:h3.cedgemax() - Stores the Maximum of an Edge and an X Value
*/
static int cedgemax(h, e1, x)
register int h;
register pel *e1;
register pel x;
{
for (; --h >= 0; e1++)
if (*e1 < x)
*e1 = x;
return(0);
}
/*
:h3.edgemin() - Stores the Mininum of Two Edges in First Edge
*/
static int edgemin(h, e1, e2)
register int h;
register pel *e1,*e2;
{
for (; --h >= 0; e1++,e2++)
if (*e1 > *e2)
*e1 = *e2;
return(0);
}
/*
:h3.edgemax() - Stores the Maximum of Two Edges in First Edge
*/
static int edgemax(h, e1, e2)
register int h;
register pel *e1,*e2;
{
for (; --h >= 0; e1++,e2++)
if (*e1 < *e2)
*e1 = *e2;
return(0);
}
/*
:h3 id=discard.discard() - Discard All Edges Between Two Edges
At first glance it would seem that we could discard an edgelist
structure merely by unlinking it from the list and freeing it. You are
wrong, region-breath! For performance, the X values associated with an
edge are allocated contiguously with it. So, we free the X values when
we free a structure. However, once an edge has been split, we are no
longer sure which control block actually is part of the memory block
that contains the edges. Rather than trying to decide, we play it safe
and never free part of a region.
So, to mark a 'edgelist' structure as discarded, we move it to the end
of the list and set ymin=ymax.
*/
static int discard(left, right)
register struct edgelist *left,*right; /* all edges between here exclusive */
/* should be discarded */
{
register struct edgelist *beg,*end,*p;
IfTrace2((RegionDebug > 0),"discard: l=%p, r=%p\n", left, right);
beg = left->link;
if (beg == right)
return(0);
for (p = beg; p != right; p = p->link) {
if (p->link == NULL && right != NULL)
abort("discard(): ran off end", 38);
IfTrace1((RegionDebug > 0),"discarding %p\n", p);
p->ymin = p->ymax = 32767;
end = p;
}
/*
* now put the chain beg/end at the end of right, if it is not
* already there:
*/
if (right != NULL) {
left->link = right;
while (right->link != NULL)
right = right->link;
right->link = beg;
}
end->link = NULL;
return(0);
}
/*
:h2.Changing the Representation of Regions
For convenience and/or performance, we sometimes like to change the way
regions are represented. This does not change the object itself, just
the representation, so these transformations can be made on a permanent
region.
*/
void MoveEdges(R, dx, dy)
register struct region *R; /* region to modify */
register fractpel dx,dy; /* delta X and Y to move edge list by */
{
register struct edgelist *edge; /* for looping through edges */
R->origin.x += dx;
R->origin.y += dy;
R->ending.x += dx;
R->ending.y += dy;
if (R->thresholded != NULL) {
R->thresholded->origin.x -= dx;
R->thresholded->origin.y -= dy;
}
/*
From now on we will deal with dx and dy as integer pel values:
*/
dx = NEARESTPEL(dx);
dy = NEARESTPEL(dy);
if (dx == 0 && dy == 0)
return;
R->xmin += dx;
R->xmax += dx;
R->ymin += dy;
R->ymax += dy;
for (edge = R->anchor; VALIDEDGE(edge); edge = edge->link) {
edge->ymin += dy;
edge->ymax += dy;
if (dx != 0) {
register int h; /* loop index; height of edge */
register pel *Xp; /* loop pointer to X values */
edge->xmin += dx;
edge->xmax += dx;
for (Xp = edge->xvalues, h = edge->ymax - edge->ymin;
--h >= 0; )
*Xp++ += dx;
}
}
}
/*
:h3.UnJumble() - Sort a Region Top to Bottom
It is an open question whether it pays in general to do this.
*/
void UnJumble(region)
struct region *region; /* region to sort */
{
register struct edgelist *anchor; /* new lists built here */
register struct edgelist *edge; /* edge pointer for loop */
register struct edgelist *next; /* ditto */
anchor = NULL;
for (edge=region->anchor; VALIDEDGE(edge); edge=next) {
if (edge->link == NULL)
abort("UnJumble: unpaired edge?", 39);
next = edge->link->link;
edge->link->link = NULL;
anchor = SortSwath(anchor, edge, t1_SwathUnion);
}
if (edge != NULL)
vertjoin(anchor, edge);
region->anchor = anchor;
region->flag &= ~ISJUMBLED(ON);
}
/*
*/
#undef NEED_OPTIMZEREGION
#ifdef NEED_OPTIMZEREGION
static int OptimizeRegion(R)
struct region *R; /* region to optimize */
{
register pel *xP; /* pel pointer for inner loop */
register int x; /* holds X value */
register int xmin,xmax; /* holds X range */
register int h; /* loop counter */
register struct edgelist *e; /* edgelist pointer for loop */
R->flag |= ISRECTANGULAR(ON);
for (e = R->anchor; VALIDEDGE(e); e=e->link) {
xmin = MAXPEL;
xmax = MINPEL;
for (h = e->ymax - e->ymin, xP = e->xvalues; --h >= 0;) {
x = *xP++;
if (x < xmin) xmin = x;
if (x > xmax) xmax = x;
}
if (xmin != xmax || (xmin != R->xmin && xmax != R->xmax))
R->flag &= ~ISRECTANGULAR(ON);
if (xmin < e->xmin || xmax > e->xmax)
abort("Tighten: existing edge bound was bad", 40);
if (xmin < R->xmin || xmax > R->xmax)
abort("Tighten: existing region bound was bad", 41);
e->xmin = xmin;
e->xmax = xmax;
}
R->flag |= ISOPTIMIZED(ON);
return(0);
}
#endif /* This function is not used */
/*
:h2.Miscelaneous Routines
:h3.MoreWorkArea() - Allocate New Space for "edge"
Our strategy is to temporarily allocate an array to hold this
unexpectedly large edge. ChangeDirection frees this array any time
it gets a shorter 'dy'.
*/
/*ARGSUSED*/
void MoreWorkArea(R, x1, y1, x2, y2)
struct region *R; /* region we are generating */
fractpel x1,y1; /* starting point of line */
fractpel x2,y2; /* ending point of line */
{
register int idy; /* integer dy of line */
idy = NEARESTPEL(y1) - NEARESTPEL(y2);
if (idy < 0) idy = - idy;
/*
* we must add one to the delta for the number of run ends we
* need to store:
*/
if (++idy > currentsize) {
IfTrace1((RegionDebug > 0),"Allocating edge of %d pels\n", idy);
if (currentworkarea != workedge)
NonObjectFree(currentworkarea);
currentworkarea = (pel *)Allocate(0, NULL, idy * sizeof(pel));
currentsize = idy;
}
ChangeDirection(CD_CONTINUE, R, x1, y1, y2 - y1, x2, y2);
}
/*
:h3.BoxClip() - Clip a Region to a Rectangle
BoxClip also duplicates the region if it is permanent. Note the
clipping box is specified in REGION coordinates, that is, in
coordinates relative to the region (0,0) point
*/
struct region *BoxClip(R, xmin, ymin, xmax, ymax)
register struct region *R; /* region to clip */
register pel xmin,ymin; /* upper left hand corner of rectangle */
register pel xmax,ymax; /* lower right hand corner */
{
struct edgelist anchor; /* pretend edgelist to facilitate discards */
register struct edgelist *e,*laste;
IfTrace1((OffPageDebug),"BoxClip of %p:\n", R);
R = UniqueRegion(R);
if (xmin > R->xmin) {
IfTrace2((OffPageDebug),"BoxClip: left clip old %d new %d\n",
(LONG) R->xmin, (LONG) xmin);
R->xmin = xmin;
}
if (xmax < R->xmax) {
IfTrace2((OffPageDebug),"BoxClip: right clip old %d new %d\n",
(LONG) R->xmax, (LONG) xmax);
R->xmax = xmax;
}
if (ymin > R->ymin) {
IfTrace2((OffPageDebug),"BoxClip: top clip old %d new %d\n",
(LONG) R->ymin, (LONG) ymin);
R->ymin = ymin;
}
if (ymax < R->ymax) {
IfTrace2((OffPageDebug),"BoxClip: bottom clip old %d new %d\n",
(LONG) R->ymax, (LONG) ymax);
R->ymax = ymax;
}
laste = &anchor;
anchor.link = R->anchor;
for (e = R->anchor; VALIDEDGE(e); e = e->link) {
if (TOP(e) < ymin) {
e->xvalues += ymin - e->ymin;
e->ymin = ymin;
}
if (BOTTOM(e) > ymax)
e->ymax = ymax;
if (TOP(e) >= BOTTOM(e)) {
discard(laste, e->link->link);
e = laste;
continue;
}
if (e->xmin < xmin) {
cedgemax(BOTTOM(e) - TOP(e), e->xvalues, xmin);
e->xmin = xmin;
e->xmax = TYPE1_MAX(e->xmax, xmin);
}
if (e->xmax > xmax) {
cedgemin(BOTTOM(e) - TOP(e), e->xvalues, xmax);
e->xmin = TYPE1_MIN(e->xmin, xmax);
e->xmax = xmax;
}
laste = e;
}
R->anchor = anchor.link;
return(R);
}
#ifdef notdef
/*
:h3.CoerceRegion() - Force a TextPath Structure to Become a Region
We also save the newly created region in the textpath structure, if the
structure was permanent. Then we don't have to do this again. Why not
save it all the time? Well, we certainly could, but I suspect it
wouldn't pay. We would have to make this region permanent (because we
couldn't have it be consumed) and this would probably require
unnecessary CopyRegions in most cases.
*/
struct region *CoerceRegion(tp)
register struct textpath *tp; /* input TEXTTYPE */
{
struct segment *path; /* temporary character path */
struct region *R; /* returned region */
R = Interior(path, EVENODDRULE);
return(R);
}
#endif
/*
:h3.RegionBounds() - Returns Bounding Box of a Region
*/
struct segment *RegionBounds(R)
register struct region *R;
{
register struct segment *path; /* returned path */
path = BoxPath(IDENTITY, R->ymax - R->ymin, R->xmax - R->xmin);
path = Join(PathSegment(MOVETYPE, R->origin.x + TOFRACTPEL(R->xmin),
R->origin.y + TOFRACTPEL(R->ymin) ),
path);
return(path);
}
/*
:h2.Formatting/Dump Routines for Debug
:h3.DumpArea() - Display a Region
*/
void DumpArea(area)
register struct region *area;
{
IfTrace1(TRUE,"Dumping area %p,", area);
IfTrace4(TRUE," X %d:%d Y %d:%d;", (LONG) area->xmin,
(LONG) area->xmax, (LONG) area->ymin,(LONG) area->ymax);
IfTrace4(TRUE," origin=(%d,%d), ending=(%d,%d)\n",
area->origin.x, area->origin.y, area->ending.x, area->ending.y);
DumpEdges(area->anchor);
}
#define INSWATH(p, y0, y1) (p != NULL && p->ymin == y0 && p->ymax == y1)
/*
:h3.DumpEdges() - Display Run End Lists (Edge Lists)
*/
static pel RegionDebugYMin = MINPEL;
static pel RegionDebugYMax = MAXPEL;
void DumpEdges(edges)
register struct edgelist *edges;
{
register struct edgelist *p,*p2;
register pel ymin = MINPEL;
register pel ymax = MINPEL;
register int y;
if (edges == NULL) {
IfTrace0(TRUE," NULL area.\n");
return;
}
if (RegionDebug <= 1) {
for (p=edges; p != NULL; p = p->link) {
edgecheck(p, ymin, ymax);
ymin = p->ymin; ymax = p->ymax;
IfTrace3(TRUE,". at %p type=%d flag=%x",
p, (LONG) p->type,(LONG) p->flag);
IfTrace4(TRUE," bounding box HxW is %dx%d at (%d,%d)\n",
(LONG) ymax - ymin, (LONG) p->xmax - p->xmin,
(LONG) p->xmin, (LONG) ymin);
}
}
else {
for (p2=edges; p2 != NULL; ) {
edgecheck(p2, ymin, ymax);
ymin = p2->ymin;
ymax = p2->ymax;
if (RegionDebug > 3 || (ymax > RegionDebugYMin
&& ymin < RegionDebugYMax)) {
IfTrace2 (TRUE,". Swath from %d to %d:\n",
ymin, ymax);
for (p=p2; INSWATH(p,ymin,ymax); p = p->link) {
IfTrace4(TRUE,". . at %p[%x] range %d:%d, ",
p, (LONG) p->flag,
(LONG) p->xmin, (LONG)p->xmax);
IfTrace1(TRUE, "subpath=%p,\n", p->subpath);
}
}
for (y=TYPE1_MAX(ymin,RegionDebugYMin); y < TYPE1_MIN(ymax, RegionDebugYMax); y++) {
IfTrace1(TRUE,". . . Y[%5d] ", (LONG) y);
for (p=p2; INSWATH(p,ymin,ymax); p = p->link)
IfTrace1(TRUE,"%5d ",
(LONG) p->xvalues[y - ymin]);
IfTrace0(TRUE,"\n");
}
while (INSWATH(p2, ymin, ymax))
p2 = p2->link;
}
}
}
/*
:h3.edgecheck() - For Debug, Verify that an Edge Obeys the Rules
*/
/*ARGSUSED*/
static int edgecheck(edge, oldmin, oldmax)
struct edgelist *edge;
int oldmin,oldmax;
{
if (edge->type != EDGETYPE)
abort("EDGE ERROR: non EDGETYPE in list", 42);
/*
The following check is not valid if the region is jumbled so I took it
out:
*/
/* if (edge->ymin < oldmax && edge->ymin != oldmin)
abort("EDGE ERROR: overlapping swaths", 43); */
return(0);
}
|