summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite/src/segment/GrPassActionCode.cpp
blob: 83c58b27e48cb9084eaa287f30f2e5e38304fc88 (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
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: GrPassActionCode.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
    A continution of the GrPass file, including functions to run the action commands.
    
This comment could create a merge conflict. It can be removed.
----------------------------------------------------------------------------------------------*/

//:>********************************************************************************************
//:>	Include files
//:>********************************************************************************************
#include "Main.h"

#ifdef _MSC_VER
#pragma hdrstop
#endif
#undef THIS_FILE
DEFINE_THIS_FILE

//:End Ignore

//:>********************************************************************************************
//:>	Forward declarations
//:>********************************************************************************************

//:>********************************************************************************************
//:>	Local type definitions
//:>********************************************************************************************

typedef signed   char Int8;		// signed value
typedef unsigned char Uint8;	// unsigned value


namespace gr
{

//:>********************************************************************************************
//:>	Methods
//:>********************************************************************************************

/*----------------------------------------------------------------------------------------------
	The main function to run a block of action or constraint code and return a result.

	@param ptman			- table manager, for generating new slots; NULL when
								running constraints
	@param pbStart			- address of the first command
	@param fConstraints		- true if we are running constraint tests;
								false if we are running rule actions
	@param psstrmIn			- input stream
	@param psstrmOut		- output stream; when running constraints, used to access items
								in the precontext
	@param islot			- slot being processed relative to the start of the rule;
								used only for constraints; must be 0 for rule actions
----------------------------------------------------------------------------------------------*/
int GrPass::RunCommandCode(GrTableManager * ptman,
	byte * pbStart, bool fConstraints,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut, int islot)
{
	byte * pbNext = pbStart;

//	std::vector<int> vnStack;
//	vnStack.EnsureSpace(128);	// make it nice and big
    m_vnStack.clear();

	gAssert(fConstraints || islot == 0);

	//	There are two states: fMustGet = true means we need to get a slot to work on from
	//	the input; fMustGet = false means we have a slot and we need to do a Next before
	//	we can get the next one.
	bool fMustGet = !fConstraints;

	bool fInserting = false;	// set to true by an Insert command

	int nRet = 0;

	while (true)
	{
		ActionCommand op = ActionCommand(*pbNext++);
		StackMachineFlag smf;
		nRet = RunOneCommand(ptman, fConstraints, op,
			&pbNext, &fMustGet, &fInserting,
			psstrmIn, psstrmOut, islot,
			m_vnStack, &smf);

		if (smf == ksmfDone)
			return nRet;

		if (smf == ksmfUnderflow)
		{
			Warn("Underflow in Graphite stack machine");
			gAssert(false);
			FontException fexptn;
			fexptn.version = -1;
			fexptn.subVersion = -1;
            fexptn.errorCode = kferrUnknown;
			throw fexptn; // disastrous error in rendering; fall back to dumb rendering
		}
	}
	return nRet; // not needed, but to avoid compiler warning
}

/*----------------------------------------------------------------------------------------------
	Perform a single command. Specifically, read the arguments from the byte buffer and the
	run the appropriate functions.

	@param ptman			- table manager, for generating new slots; or NULL
	@param fConstraints		- true if we are running constraints; false if we are running
								rule actions
	@param op				- command operator
	@param ppbArg			- pointer to first argument
	@param pfMustGet		- state: getting input or processing it; when running constraints,
								always false
	@param pfInserting		- true if current item is to be inserted, or was inserted
	@param psstrmIn			- input stream
	@param psstrmOut		- output stream
	@param islot			- the slot being processed relative to the beginning of the rule;
								only relevant for constraint testing, must be 0 for rule actions
	@param vnStack			- the stack of values being manipulated
	@param psmf				- error code, done flag, or continue flag
----------------------------------------------------------------------------------------------*/
int GrPass::RunOneCommand(GrTableManager * ptman, bool fConstraints,
	ActionCommand op, byte ** ppbArg, bool * pfMustGet, bool * pfInserting,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut, int islot,
	std::vector<int> & vnStack, StackMachineFlag * psmf)
{
	*psmf = ksmfContinue;

	//	General purpose variables:
	int arg1, arg2, arg3, arg4;
	int nSlotRef;
	int nInputClass;
	int nOutputClass;
	int c;
	int islotArg;
	int nIndex;
	int nGlyphAttr;
	int nAttLevel;
	int nFeat;
	int nPState;
	SlotAttrName slat;

	int i;
	std::vector<int> vnTmp;

	byte * pbNext = *ppbArg;

	int nRet = 0;

	switch (op)
	{
	case kopNop:
		break;

	case kopPushByte:
		arg1 = Int8(*pbNext++);
		vnStack.push_back(arg1);
		break;
	case kopPushByteU:
		arg1 = Uint8(*pbNext++);
		vnStack.push_back(arg1);
		break;
	case kopPushShort:
		arg1 = Int8(*pbNext++);
		arg2 = Uint8(*pbNext++);
		vnStack.push_back((arg1 << 8) + arg2);
		break;
	case kopPushShortU:
		arg1 = Uint8(*pbNext++);
		arg2 = Uint8(*pbNext++);
		vnStack.push_back((arg1 << 8) + arg2);
		break;
	case kopPushLong:
		arg1 = Int8(*pbNext++);
		arg2 = Uint8(*pbNext++);
		arg3 = Uint8(*pbNext++);
		arg4 = Uint8(*pbNext++);
		vnStack.push_back((arg1 << 24) + (arg2 << 16) + (arg3 << 8) + arg4);
		break;

	case kopNeg:
	case kopTrunc8:	case kopTrunc16:
	case kopNot:
		DoStackArithmetic1Arg(op, vnStack, psmf);
		if (*psmf != ksmfContinue)
			return 0;
		break;

	case kopAdd:	case kopSub:
	case kopMul:	case kopDiv:
	case kopMin:	case kopMax:
	case kopAnd:	case kopOr:
	case kopEqual:	case kopNotEq:
	case kopLess:	case kopGtr:
	case kopLessEq:	case kopGtrEq:
		DoStackArithmetic2Args(op, vnStack, psmf);
		if (*psmf != ksmfContinue)
			return 0;
		break;

	case kopCond:
		DoConditional(vnStack, psmf);
		if (*psmf != ksmfContinue)
			return 0;
		break;

	case kopNext:
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(!*pfMustGet);
			DoNext(ptman, 1, psstrmIn, psstrmOut);
			*pfMustGet = true;
			*pfInserting = false;
		}
		break;
	case kopNextN:
		c = Int8(*pbNext++);
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(false);	// for now we don't allow anything other than moving one slot
			gAssert(!*pfMustGet);									// forward at a time
			DoNext(ptman, c, psstrmIn, psstrmOut);
			*pfMustGet = true;
		}
		break;
	case kopPutGlyph8bitObs:
	case kopPutGlyph:
		if (op == kopPutGlyph8bitObs)
            nOutputClass = Uint8(*pbNext++);
		else
		{
			nOutputClass = (Uint8(*pbNext++) << 8);
			nOutputClass += Uint8(*pbNext++);
		}
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			DoPutGlyph(ptman, *pfInserting, nOutputClass, psstrmIn, psstrmOut);
			*pfMustGet = false;
		}
		break;
	case kopPutCopy:
		nSlotRef = Int8(*pbNext++);
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			DoPutCopy(ptman, *pfInserting, nSlotRef, psstrmIn, psstrmOut);
			*pfMustGet = false;
		}
		break;
	case kopPutSubs8bitObs:
	case kopPutSubs:
		nSlotRef = Int8(*pbNext++);
		if (op == kopPutSubs8bitObs)
		{
			nInputClass = Uint8(*pbNext++);
			nOutputClass = Uint8(*pbNext++);
		}
		else
		{
			nInputClass = (Uint8(*pbNext++) << 8);
			nInputClass += Uint8(*pbNext++);
			nOutputClass = (Uint8(*pbNext++) << 8);
			nOutputClass += Uint8(*pbNext++);
		}
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			DoPutSubs(ptman, *pfInserting, nSlotRef, nInputClass, nOutputClass,
				psstrmIn, psstrmOut);
			*pfMustGet = false;
		}
		break;
	case kopCopyNext:
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			gAssert(!*pfInserting);		// 0 means copy this slot
			DoPutCopy(ptman, *pfInserting, 0, psstrmIn, psstrmOut);
			DoNext(ptman, 1, psstrmIn, psstrmOut);
			// *pfMustGet still = true
		}
		break;
	case kopInsert:
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			*pfInserting = true;
			// *pfMustGet still = true

			if (ptman->LoggingTransduction())
				m_pzpst->RecordInsertionAt(psstrmOut->WritePos());
		}
		break;
	case kopDelete:
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(*pfMustGet);
			DoDelete(ptman, psstrmIn, psstrmOut);
			*pfMustGet = false;
		}
		break;
	case kopAssoc:
		c = Uint8(*pbNext++);
		// std::vector<int> vnTmp;
		for (i = 0; i < c; i++)
			vnTmp.push_back(Int8(*pbNext++));
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(!*pfMustGet);
			DoAssoc(c, vnTmp, *pfInserting, psstrmIn, psstrmOut);
		}
		break;
	case kopCntxtItem:
		islotArg = Int8(*pbNext++);
		c = Uint8(*pbNext++);
		if (fConstraints)
		{
			//	Old approach:
			//	If this is NOT the relevant item, push TRUE, which causes the subsequent tests
			//	(which are eventually ORed with it) to become irrelevant.
			//vnStack.Push((int)(islotArg != islot));

			//	If this is not the relevant item, skip the specified number of bytes.
			if (islotArg != islot)
			{
				pbNext += c;
				vnStack.push_back(1);
			}
		}
		else
		{
			gAssert(false);
		}
		break;

	case kopAttrSet:
	case kopAttrAdd:
	case kopAttrSub:
	case kopAttrSetSlot:
		slat = SlotAttrName(Uint8(*pbNext++));	// slot attribute ID
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(!*pfMustGet);
			DoSetAttr(ptman, op, *pfInserting, slat, -1, vnStack, psstrmIn, psstrmOut);
			if (*psmf != ksmfContinue)
				return 0;
		}
		break;
	case kopIAttrSet:
	case kopIAttrAdd:
	case kopIAttrSub:
	case kopIAttrSetSlot:
		slat = SlotAttrName(Uint8(*pbNext++));	// slot attribute ID
		nIndex = Uint8(*pbNext++); // index; eg, global ID for component
		if (fConstraints)
		{
			gAssert(false);
		}
		else
		{
			gAssert(!*pfMustGet);
			DoSetAttr(ptman, op, *pfInserting, slat, nIndex, vnStack,
				psstrmIn, psstrmOut);
			if (*psmf != ksmfContinue)
				return 0;
		}
		break;

	case kopPushSlotAttr:
		slat = SlotAttrName(Uint8(*pbNext++));
		nSlotRef = Int8(*pbNext++);
		if (fConstraints)
			nSlotRef += islot + 1;	// +1 to peek ahead to a slot we haven't "got" yet
		DoPushSlotAttr(ptman, nSlotRef, *pfInserting, slat, -1, vnStack, psstrmIn, psstrmOut);
		if (*psmf != ksmfContinue)
			return 0;
		break;
	case kopPushISlotAttr:
		slat = SlotAttrName(Uint8(*pbNext++));
		nSlotRef = Int8(*pbNext++);
		nIndex = Uint8(*pbNext++);
		if (fConstraints)
			nSlotRef += islot + 1;	// +1 to peek ahead to a slot we haven't "got" yet
		DoPushSlotAttr(ptman, nSlotRef, *pfInserting, slat, nIndex, vnStack, psstrmIn, psstrmOut);
		if (*psmf != ksmfContinue)
			return 0;
		break;

	case kopPushGlyphAttrObs:
	case kopPushAttToGAttrObs:
	case kopPushGlyphAttr:
	case kopPushAttToGlyphAttr:
		if (op == kopPushGlyphAttrObs || op == kopPushAttToGAttrObs)
            nGlyphAttr = Uint8(*pbNext++);
		else {
			nGlyphAttr = (Uint8(*pbNext++) << 8);
			nGlyphAttr += Uint8(*pbNext++);
		}
		nSlotRef = Int8(*pbNext++);
		if (fConstraints)
			nSlotRef += islot + 1;	// +1 to peek ahead to a slot we haven't "got" yet
		if (op == kopPushAttToGlyphAttr || op == kopPushAttToGAttrObs)
			DoPushAttToGlyphAttr(ptman, nSlotRef, *pfInserting, nGlyphAttr,
				vnStack, psstrmIn, psstrmOut);
		else
			DoPushGlyphAttr(ptman, nSlotRef, *pfInserting, nGlyphAttr, vnStack,
				psstrmIn, psstrmOut);
		if (*psmf != ksmfContinue)
			return 0;
		break;
	case kopPushGlyphMetric:
	case kopPushAttToGlyphMetric:
		nGlyphAttr = Uint8(*pbNext++);
		nSlotRef = Int8(*pbNext++);
		nAttLevel = Int8(*pbNext++);
		if (fConstraints)
			nSlotRef += islot + 1;	// +1 to peek ahead to a slot we haven't "got" yet
		if (op == kopPushAttToGlyphMetric)
			DoPushAttToGlyphMetric(ptman, nSlotRef, *pfInserting, nGlyphAttr, nAttLevel,
				vnStack, psstrmIn, psstrmOut);
		else
			DoPushGlyphMetric(ptman, nSlotRef, *pfInserting, nGlyphAttr, nAttLevel,
				vnStack, psstrmIn, psstrmOut);
		if (*psmf != ksmfContinue)
			return 0;
		break;
	case kopPushFeat:
		nFeat = Uint8(*pbNext++);
		nSlotRef = Int8(*pbNext++);
		if (fConstraints)
			nSlotRef += islot + 1;	// +1 to peek ahead to a slot we haven't "got" yet
//		else
//			nSlotRef = 0;
		DoPushFeatValue(ptman, nSlotRef, *pfInserting, nFeat, vnStack, psstrmIn, psstrmOut);
		if (*psmf != ksmfContinue)
			return 0;
		break;
	case kopPushProcState:
		nPState = Uint8(*pbNext++);
		gAssert(fConstraints);
		DoPushProcState(ptman, nPState, vnStack);
		break;
	case kopPushVersion:
		vnStack.push_back(kRuleVersion);
		break;
	case kopPopRet:
		if ((*psmf = CheckStack(vnStack, 1)) != ksmfContinue)
			return 0;
		nRet = vnStack.back();
		vnStack.pop_back();
		if (vnStack.size() != 0)
		{
			gAssert(false);
			*psmf = ksmfStackNotEmptied;
		}
		else
			*psmf = ksmfDone;
		break;
	case kopRetZero:
		nRet = 0;
		if (vnStack.size() != 0)
		{
			gAssert(false);
			*psmf = ksmfStackNotEmptied;
		}
		else
			*psmf = ksmfDone;
		break;
	case kopRetTrue:
		nRet = 1;
		if (vnStack.size() != 0)
		{
			gAssert(false);
			*psmf = ksmfStackNotEmptied;
		}
		else
			*psmf = ksmfDone;
		break;

	default:
		gAssert(false);
	}

	*ppbArg = pbNext;
	return nRet;
}

/*----------------------------------------------------------------------------------------------
	Perform arithmetic functions that take two arguments.
----------------------------------------------------------------------------------------------*/
void GrPass::DoStackArithmetic2Args(ActionCommand op, std::vector<int> & vnStack,
	StackMachineFlag * psmf)
{
	if ((*psmf = CheckStack(vnStack, 2)) != ksmfContinue)
		return;

	int nArg2 = vnStack.back();
	vnStack.pop_back();
	int nArg1 = vnStack.back();
	vnStack.pop_back();

	int nResult;

	switch (op)
	{
	case kopAdd:	nResult = nArg1 + nArg2;						break;
	case kopSub:	nResult = nArg1 - nArg2;						break;
	case kopMul:	nResult = nArg1 * nArg2;						break;
	case kopDiv:	nResult = nArg1 / nArg2;						break;
	case kopMin:	nResult = min(nArg1, nArg2);					break;
	case kopMax:	nResult = max(nArg1, nArg2);					break;
	case kopAnd:	nResult = (nArg1 != 0 && nArg2 != 0)? 1: 0;		break;
	case kopOr:		nResult = (nArg1 != 0 || nArg2 != 0)? 1: 0;		break;
	case kopEqual:	nResult = (nArg1 == nArg2)? 1: 0;				break;
	case kopNotEq:	nResult = (nArg1 != nArg2)? 1: 0;				break;
	case kopLess:	nResult = (nArg1 <  nArg2)? 1: 0;				break;
	case kopLessEq:	nResult = (nArg1 <= nArg2)? 1: 0;				break;
	case kopGtr:	nResult = (nArg1 >  nArg2)? 1: 0;				break;
	case kopGtrEq:	nResult = (nArg1 >= nArg2)? 1: 0;				break;
	default:
		Assert(false);
	}

	vnStack.push_back(nResult);
}

/*----------------------------------------------------------------------------------------------
	Perform arithmetic functions that take one argument.
----------------------------------------------------------------------------------------------*/
void GrPass::DoStackArithmetic1Arg(ActionCommand op, std::vector<int> & vnStack,
	StackMachineFlag * psmf)
{
	if ((*psmf = CheckStack(vnStack, 1)) != ksmfContinue)
		return;

	int nArg = vnStack.back();
	vnStack.pop_back();
	int nResult;	

	switch (op)
	{
	case kopNeg:		nResult = nArg * -1;			break;
	case kopTrunc8:		nResult = nArg & 0xFF;			break;
	case kopTrunc16:	nResult = nArg & 0xFFFF;		break;
	case kopNot:		nResult = (nArg == 0)? 1: 0;	break;
	default:
		Assert(false);
	}

	vnStack.push_back(nResult);
}

/*----------------------------------------------------------------------------------------------
	Perform the conditional statement.
----------------------------------------------------------------------------------------------*/
void GrPass::DoConditional(std::vector<int> & vnStack, StackMachineFlag * psmf)
{
	if ((*psmf = CheckStack(vnStack, 3)) != ksmfContinue)
		return;

	int nArg3 = vnStack.back();
	vnStack.pop_back();
	int nArg2 = vnStack.back();
	vnStack.pop_back();
	int nArg1 = vnStack.back();
	vnStack.pop_back();

	if (nArg1 == 0)
		vnStack.push_back(nArg3);
	else
		vnStack.push_back(nArg2);
}

/*----------------------------------------------------------------------------------------------
	Check to make sure the stack has the appropriate number of items on it; if not,
	return a stack error flag.
----------------------------------------------------------------------------------------------*/
GrPass::StackMachineFlag GrPass::CheckStack(std::vector<int> & vnStack, int cn)
{
	if (signed(vnStack.size()) < cn)
		return ksmfUnderflow;
	else
		return ksmfContinue;
}

/*----------------------------------------------------------------------------------------------
	We are finished processing a slot; go on to the next slot, or possibly go backwards.
----------------------------------------------------------------------------------------------*/
void GrPass::DoNext(GrTableManager * ptman,
	int cslot, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	gAssert(cslot == 1);	// for now anyway
}

/*----------------------------------------------------------------------------------------------
	Set the next glyph to be the given class. There should only be one member of the class,
	but if there happens to be more than one, just use the first member.
	All the slot attributes remain unchanged from the current input slot; associations are
	neutralized.

	@param ptman				- table manager, for generating new slots
	@param fInserting			- true if this slot is being inserted, not copied
	@param nReplacementClass	- class from which to take replacement glyph (corresponding to 
									selector slot's glyph's index in selector class)
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutGlyph(GrTableManager * ptman, bool fInserting, int nReplacementClass,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	EngineState * pengst = ptman->State();

	// If we are exactly at the segment boundary, pass the information on to the output stream.
	// Note that inserted items always go outside the segment, if they are at the boundary
	// (you DON'T want them between the segment boundary and the LB slot!).
	bool fSetSegMin = psstrmIn->AtSegMin() && !fInserting;
	bool fSetSegLim = psstrmIn->AtSegLim();

	//	Slot to copy features and text properties from:
	GrSlotState * pslotNextInput;
	if (psstrmIn->AtEndOfContext())
	{
		gAssert(fInserting);
		pslotNextInput = psstrmIn->RuleInputSlot(0, psstrmOut);
	}
	else
		pslotNextInput = (fInserting)? psstrmIn->Peek(): psstrmIn->NextGet();

	gid16 nGlyphReplacement = ptman->GetClassGlyphIDAt(nReplacementClass, 0);

	GrSlotState * pslotNew;
	if (fInserting)
	{
		pengst->NewSlot(nGlyphReplacement, pslotNextInput, m_ipass, &pslotNew);
		// leave associations empty; eventually they will be "neutralized"
	}
	else
	{
		pengst->NewSlotCopy(pslotNextInput, m_ipass, &pslotNew);
		pslotNew->SetGlyphID(nGlyphReplacement);
		ptman->SetSlotAttrsFromGlyphAttrs(pslotNew);
	}

	if (fSetSegMin)
		psstrmOut->SetSegMinToWritePos(false);
	if (fSetSegLim)
		psstrmOut->SetSegLimToWritePos(false);
	psstrmOut->NextPut(pslotNew);
}

/*----------------------------------------------------------------------------------------------
	Copy a glyph from the given slot in the input to the output.  All the associations and slot
	attributes are copied from the specified slot as well. Consume a slot from the input
	(which may or may not be the slot we're copying).

	@param ptman				- table manager, for generating new slots
	@param fInserting			- true if this slot is being inserted, not copied
	@param cslotCopyFrom		- slot to copy from
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutCopy(GrTableManager * ptman, bool fInserting, int cslotCopyFrom,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	EngineState * pengst = ptman->State();

	// If we are exactly at the segment boundary, pass the information on to the output stream.
	// Note that inserted items always go outside the segment, if they are at the boundary.
	bool fSetSegMin = psstrmIn->AtSegMin() && !fInserting;
	bool fSetSegLim = psstrmIn->AtSegLim();

	if (!fInserting)
	{
		gAssert(!psstrmIn->AtEndOfContext());
		//	Absorb next slot
		psstrmIn->NextGet();
	}

	GrSlotState * pslotCopyFrom = psstrmIn->RuleInputSlot(cslotCopyFrom, psstrmOut);

	GrSlotState * pslotNew;
	pengst->NewSlotCopy(pslotCopyFrom, m_ipass, &pslotNew);

	if (fSetSegMin)
		psstrmOut->SetSegMinToWritePos(false);
	if (fSetSegLim)
		psstrmOut->SetSegLimToWritePos(false);
	psstrmOut->NextPut(pslotNew);
}

/*----------------------------------------------------------------------------------------------
	Copy the current slot from the input to the output, substituting the corresponding glyph
	in the output class. All the associations and slot attributes remain unchanged from the
	current input slot.

	@param ptman				- table manager, for generating new slots
	@param fInserting			- true if this slot is being inserted, not copied
	@param cslotSel				- selector slot, relative to current slot
	@param nSelectorClass		- class of selector slot
	@param nReplacementClass	- class from which to take replacement glyph (corresponding to 
									selector slot's glyph's index in selector class)
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutSubs(GrTableManager * ptman, bool fInserting,
	int cslotSel, int nSelClass, int nReplacementClass,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	bool fAtSegMin, fAtSegLim;
	GrSlotState * pslotNextInput;
	DoPutSubsInit(psstrmIn, psstrmOut, fInserting, &pslotNextInput, &fAtSegMin, &fAtSegLim);

	GrSlotState * pslotInSelector = psstrmIn->RuleInputSlot(cslotSel, psstrmOut);

	gid16 gidSelector = pslotInSelector->GlyphID();
	int nSelIndex = ptman->GetIndexInGlyphClass(nSelClass, gidSelector);
	gAssert(nSelIndex != -1);
	gid16 gidReplacement = (nSelIndex == -1)?
		gidSelector:
		ptman->GetClassGlyphIDAt(nReplacementClass, nSelIndex);

	DoPutSubsAux(ptman, fInserting, gidReplacement, psstrmIn, psstrmOut, pslotNextInput,
		fAtSegMin, fAtSegLim);
}

/*----------------------------------------------------------------------------------------------
	Copy the current slot from the input to the output, substituting the corresponding glyph
	in the output class based on 2 input classes. All the associations and slot attributes
	remain unchanged from the current input slot.

	@param ptman				- table manager, for generating new slots
	@param fInserting			- true if this slot is being inserted, not copied
	@param cslotSel				- selector slot, relative to current slot
	@param nSelectorClass		- class of selector slot
	@param nReplacementClass	- class from which to take replacement glyph (corresponding to 
									selector slot's glyph's index in selector class)
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutSubs2(GrTableManager * ptman, bool fInserting,
	int cslotSel1, int nSelClass1, int cslotSel2, int nSelClass2, int nReplacementClass,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	bool fAtSegMin, fAtSegLim;
	GrSlotState * pslotNextInput;
	DoPutSubsInit(psstrmIn, psstrmOut, fInserting, &pslotNextInput, &fAtSegMin, &fAtSegLim);

	GrSlotState * pslotInSelector1 = psstrmIn->RuleInputSlot(cslotSel1, psstrmOut);
	gid16 gidSelector1 = pslotInSelector1->GlyphID();
	int nSelIndex1 = ptman->GetIndexInGlyphClass(nSelClass1, gidSelector1);
	gAssert(nSelIndex1 != -1);
	//size_t cClassLen1 = ptman->NumberOfGlyphsInClass(nSelClass1);

	GrSlotState * pslotInSelector2 = psstrmIn->RuleInputSlot(cslotSel2, psstrmOut);
	gid16 gidSelector2 = pslotInSelector2->GlyphID();
	int nSelIndex2 = ptman->GetIndexInGlyphClass(nSelClass2, gidSelector2);
	gAssert(nSelIndex2 != -1);
	size_t cClassLen2 = ptman->NumberOfGlyphsInClass(nSelClass2);

	int nSelIndex = (nSelIndex1 == -1 || nSelIndex2 == -1) ?
		-1 :
		(nSelIndex1 * static_cast<int>(cClassLen2)) + nSelIndex2;

	gid16 gidReplacement = (nSelIndex == -1)?
		gidSelector1:
		ptman->GetClassGlyphIDAt(nReplacementClass, nSelIndex);

	DoPutSubsAux(ptman, fInserting, gidReplacement, psstrmIn, psstrmOut, pslotNextInput,
		fAtSegMin, fAtSegLim);
}

/*----------------------------------------------------------------------------------------------
	Copy the current slot from the input to the output, substituting the corresponding glyph
	in the output class based on 2 input classes. All the associations and slot attributes
	remain unchanged from the current input slot.

	@param ptman				- table manager, for generating new slots
	@param fInserting			- true if this slot is being inserted, not copied
	@param cslotSel				- selector slot, relative to current slot
	@param nSelectorClass		- class of selector slot
	@param nReplacementClass	- class from which to take replacement glyph (corresponding to 
									selector slot's glyph's index in selector class)
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutSubs3(GrTableManager * ptman, bool fInserting,
	int cslotSel1, int nSelClass1, int cslotSel2, int nSelClass2, int cslotSel3, int nSelClass3,
	int nReplacementClass,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	bool fAtSegMin, fAtSegLim;
	GrSlotState * pslotNextInput;
	DoPutSubsInit(psstrmIn, psstrmOut, fInserting, &pslotNextInput, &fAtSegMin, &fAtSegLim);

	GrSlotState * pslotInSelector1 = psstrmIn->RuleInputSlot(cslotSel1, psstrmOut);
	gid16 gidSelector1 = pslotInSelector1->GlyphID();
	int nSelIndex1 = ptman->GetIndexInGlyphClass(nSelClass1, gidSelector1);
	gAssert(nSelIndex1 != -1);
	//size_t cClassLen1 = ptman->NumberOfGlyphsInClass(nSelClass1);

	GrSlotState * pslotInSelector2 = psstrmIn->RuleInputSlot(cslotSel2, psstrmOut);
	gid16 gidSelector2 = pslotInSelector2->GlyphID();
	int nSelIndex2 = ptman->GetIndexInGlyphClass(nSelClass2, gidSelector2);
	gAssert(nSelIndex2 != -1);
	size_t cClassLen2 = ptman->NumberOfGlyphsInClass(nSelClass2);

//	GrSlotState * pslotInSelector3 = psstrmIn->RuleInputSlot(cslotSel3, psstrmOut);
//	gid16 gidSelector3 = pslotInSelector3->GlyphID();
	int nSelIndex3 = ptman->GetIndexInGlyphClass(nSelClass3, gidSelector2);
	gAssert(nSelIndex3 != -1);
	size_t cClassLen3 = ptman->NumberOfGlyphsInClass(nSelClass3);

	int nSelIndex = (nSelIndex1 == -1 || nSelIndex2 == -1 || nSelIndex3 == -1) ?
		-1 :
		(((nSelIndex1 * static_cast<int>(cClassLen2)) + nSelIndex2) * static_cast<int>(cClassLen3)) + nSelIndex3;

	gid16 gidReplacement = (nSelIndex == -1)?
		gidSelector1:
		ptman->GetClassGlyphIDAt(nReplacementClass, nSelIndex);

	DoPutSubsAux(ptman, fInserting, gidReplacement, psstrmIn, psstrmOut, pslotNextInput,
		fAtSegMin, fAtSegLim);
}


/*----------------------------------------------------------------------------------------------
	Initial common part of all the DoPutSubs... methods.
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutSubsInit(GrSlotStream * psstrmIn, GrSlotStream * psstrmOut, bool fInserting,
	GrSlotState ** ppslotNextInput, bool * pfAtSegMin, bool * pfAtSegLim)
{
	// Do this before reading from stream.
	*pfAtSegMin = psstrmIn->AtSegMin();
	*pfAtSegLim = psstrmIn->AtSegLim();

	//	Slot to copy features and text properties from:
	if (psstrmIn->AtEndOfContext())
	{
		gAssert(fInserting);
		*ppslotNextInput = psstrmIn->RuleInputSlot(0, psstrmOut);
	}
	else
		*ppslotNextInput = (fInserting)? psstrmIn->Peek(): psstrmIn->NextGet();
}

/*----------------------------------------------------------------------------------------------
	Common part of all the DoPutSubs... methods.
----------------------------------------------------------------------------------------------*/
void GrPass::DoPutSubsAux(GrTableManager * ptman, bool fInserting, gid16 nGlyphReplacement,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut, GrSlotState * pslotNextInput,
	bool fAtSegMin, bool fAtSegLim)
{
	EngineState * pengst = ptman->State();

	// If we are exactly at the segment boundary, pass the information on to the output stream.
	// Note that inserted items always go outside the segment, if they are at the boundary.
	bool fSetSegMin = fAtSegMin && !fInserting;
	bool fSetSegLim = fAtSegLim;

	GrSlotState * pslotNew;
	if (fInserting)
	{
		pengst->NewSlot(nGlyphReplacement, pslotNextInput, m_ipass, &pslotNew);
		// leave associations empty; eventually they will be "neutralized"
	}
	else
	{
		pengst->NewSlotCopy(pslotNextInput, m_ipass, &pslotNew);
		pslotNew->SetGlyphID(nGlyphReplacement);
		ptman->SetSlotAttrsFromGlyphAttrs(pslotNew);
	}

	if (fSetSegMin)
		psstrmOut->SetSegMinToWritePos(false);
	if (fSetSegLim)
		psstrmOut->SetSegLimToWritePos(false);
	psstrmOut->NextPut(pslotNew);
}

/*----------------------------------------------------------------------------------------------
	A slot has just been inserted. Clear all the associations; eventually (unless we go on
	to set the associations explicitly) we will set its before-assoc to the slot after it
	and its after-assoc to the slot before it. This makes it basically unselectable.
	OBSOLETE - handled by slot initialization code
----------------------------------------------------------------------------------------------*/
void GrPass::SetNeutralAssocs(GrSlotState * pslotNew, GrSlotStream * psstrmIn)
{
	pslotNew->ClearAssocs();
}

/*----------------------------------------------------------------------------------------------
	Delete the next slot in the input.

	@param psstrmIn			- input stream
	@param psstrmOut		- output stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoDelete(GrTableManager * ptman, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	if (psstrmIn->AtSegMin())
		psstrmOut->SetSegMinToWritePos();
	if (psstrmIn->AtSegLim())
		psstrmOut->SetSegLimToWritePos();

	GrSlotState * pslot = psstrmIn->NextGet();
	pslot->MarkDeleted();

	if (ptman->LoggingTransduction())
		m_pzpst->RecordDeletionBefore(psstrmOut->WritePos());
}

/*----------------------------------------------------------------------------------------------
	Set the associations for the current slot. Any previous associations will be overwritten.

	@param cn					- number of associations
	@param vnAssoc				- list of associations
	@param fInserting			- whether current slot was inserted
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoAssoc(int cnAssocs, std::vector<int> & vnAssocs, bool fInserting,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	gAssert((unsigned)cnAssocs == vnAssocs.size());

	//	Sort the list of associations. It's okay to use a simple bubble sort
	//	since we expect the list to be very short. Ideally we should remove duplicates
	//	too but I don't think duplicates actually hurt anything.
	for (int i1 = 0; i1 < cnAssocs - 1; i1++)
	{
		for (int i2 = i1 + 1; i2 < cnAssocs; i2++)
		{
			if (vnAssocs[i2] < vnAssocs[i1])
			{
				int nTmp = vnAssocs[i2];
				vnAssocs[i2] = vnAssocs[i1];
				vnAssocs[i1] = nTmp;
			}
		}
	}

	std::vector<GrSlotState *> vpslotAssocs;
	vpslotAssocs.resize(cnAssocs);
	for (int i = 0; i < cnAssocs; i++)
		vpslotAssocs[i] = psstrmIn->RuleInputSlot(vnAssocs[i], psstrmOut);

	GrSlotState * pslot = psstrmOut->RuleOutputSlot();
	pslot->Associate(vpslotAssocs);
}

/*----------------------------------------------------------------------------------------------
	Set a slot attribute for the current slot; the value is on the stack.

	@param op					- command
	@param fInserting			- whether current slot was inserted
	@param slat					- slot attribute to set
	@param slati				- slot attribute index, or -1 for non-indexed attribute
	@param vnStack				- stack to read value from
	@param psstrmIn / Out		- input/output streams
----------------------------------------------------------------------------------------------*/
void GrPass::DoSetAttr(GrTableManager * ptman, ActionCommand op, bool fInserting,
	SlotAttrName slat, int slati, std::vector<int> & vnStack,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	int nVal = vnStack.back();
	vnStack.pop_back();

	if (slat == kslatUserDefnV1)
		slat = kslatUserDefn;

	if (slati != -1 && slat != kslatCompRef && slat != kslatUserDefn)
	{
		//	Invalid slot attribute index.
		gAssert(false);
		slati = -1;
	}

	if (slati == -1 && (slat == kslatCompRef || slat == kslatUserDefn))
	{
		//	Missing slot attribute index.
		gAssert(false);
		slati = 0;
	}

	if (slat == kslatPosX || slat == kslatPosY)
	{
		//	Doesn't make sense to set the pos attribute.
		gAssert(false);
		return;
	}

	GrSlotState * pslotIn = psstrmIn->RuleInputSlot(0, psstrmOut);

	GrSlotState * pslotComp;

	int nOldVal;

	if (op == kopAttrAdd || op == kopAttrSub || op == kopIAttrAdd || op == kopIAttrSub)
	{
		//	Incrementing or decrementing.

		gAssert(!fInserting);

		switch (slat)
		{
		case kslatAdvX:			nOldVal = pslotIn->AdvanceX(ptman);		break;
		case kslatAdvY:			nOldVal = pslotIn->AdvanceY(ptman);		break;
		case kslatShiftX:		nOldVal = pslotIn->ShiftX();			break;
		case kslatShiftY:		nOldVal = pslotIn->ShiftY();			break;

		case kslatAttAtX:		nOldVal = pslotIn->AttachAtX(ptman, psstrmIn);	break;
		case kslatAttAtY:		nOldVal = pslotIn->AttachAtY();					break;
		case kslatAttAtXoff:	nOldVal = pslotIn->AttachAtXOffset();			break;
		case kslatAttAtYoff:	nOldVal = pslotIn->AttachAtYOffset();			break;

		case kslatAttWithX:		nOldVal = pslotIn->AttachWithX(ptman, psstrmIn);	break;
		case kslatAttWithY:		nOldVal = pslotIn->AttachWithY();					break;
		case kslatAttWithXoff:	nOldVal = pslotIn->AttachWithXOffset();				break;
		case kslatAttWithYoff:	nOldVal = pslotIn->AttachWithYOffset();				break;

		case kslatUserDefn:		nOldVal = pslotIn->UserDefn(slati);		break;

		case kslatMeasureSol:	nOldVal = pslotIn->MeasureSol();	break;
		case kslatMeasureEol:	nOldVal = pslotIn->MeasureEol();	break;

		case kslatJStretch:		nOldVal = pslotIn->JStretch();	break;
		case kslatJShrink:		nOldVal = pslotIn->JShrink();	break;
		case kslatJStep:		nOldVal = pslotIn->JStep();		break;
		case kslatJWeight:		nOldVal = pslotIn->JWeight();	break;
		case kslatJWidth:		nOldVal = pslotIn->JWidth();	break;

		//	These are kind of odd, but maybe.
		case kslatAttLevel:		nOldVal = pslotIn->AttachLevel();		break;
		case kslatBreak:		nOldVal = pslotIn->BreakWeight();		break;
		case kslatDir:			nOldVal = pslotIn->Directionality();	break;

		default:
			//	Kind of attribute that it makes no sense to increment.
			gAssert(false);
			return;
		}

		if (op == kopAttrAdd || op == kopIAttrAdd)
			nVal = nOldVal + nVal;
		else
		{
			gAssert(op == kopAttrSub || op == kopIAttrSub);
			nVal = nOldVal - nVal;
		}
	}

	GrSlotState * pslotOut = psstrmOut->RuleOutputSlot();

	if (pslotOut->IsLineBreak(ptman->LBGlyphID()))
	{
		switch (slat)
		{
		case kslatAdvX:
		case kslatAdvY:
		case kslatShiftX:
		case kslatShiftY:

		case kslatAttTo:
		case kslatAttLevel:
		case kslatAttAtX:
		case kslatAttAtY:
		case kslatAttAtGpt:
		case kslatAttAtXoff:
		case kslatAttAtYoff:
		case kslatAttWithX:
		case kslatAttWithY:
		case kslatAttWithGpt:
		case kslatAttWithXoff:
		case kslatAttWithYoff:

		case kslatMeasureSol:
		case kslatMeasureEol:

		case kslatJStretch:
		case kslatJShrink:
		case kslatJStep:
		case kslatJWeight:
		case kslatJWidth:
			slat = kslatNoEffect;
			break;
		default:
			; // okay, do nothing
		}
	}

	nVal = GrGlyphSubTable::ConvertValueForVersion(nVal, slat, -1, m_fxdVersion);

	switch (slat)
	{
	case kslatAdvX:			pslotOut->SetAdvanceX(nVal);			break;
	case kslatAdvY:			pslotOut->SetAdvanceY(nVal);			break;
	case kslatShiftX:		pslotOut->SetShiftX(nVal);				break;
	case kslatShiftY:		pslotOut->SetShiftY(nVal);				break;

	case kslatAttTo:		pslotOut->SetAttachTo(nVal);			break;
	case kslatAttLevel:		pslotOut->SetAttachLevel(nVal);			break;
	case kslatAttAtX:		pslotOut->SetAttachAtX(nVal);			break;
	case kslatAttAtY:		pslotOut->SetAttachAtY(nVal);			break;
	case kslatAttAtGpt:		pslotOut->SetAttachAtGpoint(nVal);		break;
	case kslatAttAtXoff:	pslotOut->SetAttachAtXOffset(nVal);		break;
	case kslatAttAtYoff:	pslotOut->SetAttachAtYOffset(nVal);		break;
	case kslatAttWithX:		pslotOut->SetAttachWithX(nVal);			break;
	case kslatAttWithY:		pslotOut->SetAttachWithY(nVal);			break;
	case kslatAttWithGpt:	pslotOut->SetAttachWithGpoint(nVal);	break;
	case kslatAttWithXoff:	pslotOut->SetAttachWithXOffset(nVal);	break;
	case kslatAttWithYoff:	pslotOut->SetAttachWithYOffset(nVal);	break;

	case kslatUserDefn:		pslotOut->SetUserDefn(slati, nVal);		break;

	case kslatCompRef:
		gAssert(nVal - 1 >= psstrmIn->RuleStartReadPos() - psstrmIn->ReadPosForNextGet());
		pslotComp = psstrmIn->RuleInputSlot(nVal, psstrmOut);
		pslotOut->SetCompRefSlot(ptman, slati, pslotComp);		
		break;

	case kslatMeasureSol:	pslotOut->SetMeasureSol(nVal);	break;
	case kslatMeasureEol:	pslotOut->SetMeasureEol(nVal);	break;

	case kslatJStretch:		pslotOut->SetJStretch(nVal);	break;
	case kslatJShrink:		pslotOut->SetJShrink(nVal);		break;
	case kslatJStep:		pslotOut->SetJStep(nVal);		break;
	case kslatJWeight:		pslotOut->SetJWeight(nVal);		break;
	case kslatJWidth:		pslotOut->SetJWidth(nVal);		break;

	case kslatBreak:		pslotOut->SetBreakWeight(nVal);				break;
	case kslatDir:			pslotOut->SetDirectionality(DirCode(nVal)); break;
	case kslatInsert:		pslotOut->SetInsertBefore(bool(nVal));		break;

	case kslatNoEffect:
		// Do nothing.
		break;

	default:
		//	Kind of attribute that it makes no sense to set.
		gAssert(false);
		return;
	}
}

/*----------------------------------------------------------------------------------------------
	Push value of a slot attribute onto the stack.

	@param nSlotRef				- slot whose attribute we are interested in
	@param fInserting			- whether current slot was inserted
	@param slat					- slot attribute to get
	@param slati				- slot attribute index, or -1 for non-indexed attribute
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushSlotAttr(GrTableManager * ptman,
	int nSlotRef, bool fInserting,
	SlotAttrName slat, int slati, std::vector<int> & vnStack,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	GrSlotState * pslot = psstrmIn->RuleInputSlot(nSlotRef, psstrmOut, true);
	if (pslot == NULL)
	{
		// Non-existent pre-context item.
		vnStack.push_back(0);
		return;
	}

	int nVal;
	int xyBogus;

	if (slat == kslatUserDefnV1)
		slat = kslatUserDefn;

	if (slati != -1 && slat != kslatCompRef && slat != kslatUserDefn)
	{
		//	Invalid slot attribute index.
		gAssert(false);
		slati = -1;
	}

	if (slati == -1 && (slat == kslatCompRef || slat == kslatUserDefn))
	{
		//	Missing slot attribute index.
		gAssert(false);
		slati = 0;
	}

	switch (slat)
	{
	case kslatAdvX:			nVal = pslot->AdvanceX(ptman);		break;
	case kslatAdvY:			nVal = pslot->AdvanceY(ptman);		break;
	case kslatShiftX:		nVal = pslot->ShiftX();				break;
	case kslatShiftY:		nVal = pslot->ShiftY();				break;

	case kslatPosX:	pslot->Position(ptman, psstrmIn, &nVal, &xyBogus); 	break;
	case kslatPosY:	pslot->Position(ptman, psstrmIn, &xyBogus, &nVal); 	break;

	case kslatAttTo:		nVal = pslot->AttachTo();			break;
	case kslatAttLevel:		nVal = pslot->AttachLevel();		break;
	case kslatAttAtX:		nVal = pslot->AttachAtX(ptman, psstrmIn); break;
	case kslatAttAtY:		nVal = pslot->AttachAtY();			break;
	case kslatAttAtGpt:		nVal = pslot->AttachAtGpoint();		break;
	case kslatAttAtXoff:	nVal = pslot->AttachAtXOffset();	break;
	case kslatAttAtYoff:	nVal = pslot->AttachAtYOffset();	break;
	case kslatAttWithX:		nVal = pslot->AttachWithX(ptman, psstrmIn); break;
	case kslatAttWithY:		nVal = pslot->AttachWithY();		break;
	case kslatAttWithGpt:	nVal = pslot->AttachWithGpoint();	break;
	case kslatAttWithXoff:	nVal = pslot->AttachWithXOffset();	break;
	case kslatAttWithYoff:	nVal = pslot->AttachWithYOffset();	break;

	case kslatMeasureSol:	nVal = pslot->MeasureSol();			break;
	case kslatMeasureEol:	nVal = pslot->MeasureEol();			break;

	case kslatJStretch:		nVal = pslot->JStretch();			break;
	case kslatJShrink:		nVal = pslot->JShrink();			break;
	case kslatJStep:		nVal = pslot->JStep();				break;
	case kslatJWeight:		nVal = pslot->JWeight();			break;
	case kslatJWidth:		nVal = pslot->JWidth();				break;

	case kslatBreak:		nVal = pslot->BreakWeight();		break;
	case kslatDir:			nVal = pslot->Directionality();		break;
	case kslatInsert:		nVal = pslot->InsertBefore();		break;

	case kslatUserDefn:		nVal = pslot->UserDefn(slati);		break;

	//	Currently no way to look up component.X.ref.

	default:
		//	Invalid attribute.
		gAssert(false);
		nVal = 0;
	}

	vnStack.push_back(nVal);
}

/*----------------------------------------------------------------------------------------------
	Push value of a glyph attribute onto the stack.

	@param nSlotRef				- slot whose attribute we are interested in
	@param fInserting			- whether current slot was inserted
	@param nGlyphAttr			- glyph attribute to get
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushGlyphAttr(GrTableManager * ptman, int nSlotRef, bool fInserting,
	int nGlyphAttr,
	std::vector<int> & vnStack, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	GrSlotState * pslot = psstrmIn->RuleInputSlot(nSlotRef, psstrmOut, true);
	if (pslot == NULL)
	{
		// Non-existent pre-context item.
		vnStack.push_back(0);
		return;
	}
	int nVal = pslot->GlyphAttrValueEmUnits(ptman, nGlyphAttr);
	vnStack.push_back(nVal);
}

/*----------------------------------------------------------------------------------------------
	Push value of a glyph attribute onto the stack. The slot of interest is the slot to
	which the current slot is attached.

	@param nSlotRef				- leaf slot
	@param fInserting			- whether current slot was inserted
	@param nGlyphAttr			- glyph attribute to get
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushAttToGlyphAttr(GrTableManager * ptman, int nSlotRef, bool fInserting,
	int nGlyphAttr, std::vector<int> & vnStack,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	//	Note that it is the slot in the output stream that has
	//	the relevant attach.to attribute set.
	GrSlotState * pslotLeaf = psstrmOut->RuleOutputSlot(0);
	int srAttTo = pslotLeaf->AttachTo();
	if (srAttTo == 0)
	{
		gAssert(false);
		vnStack.push_back(0);
		return;
	}

	GrSlotState * pslotRoot = psstrmIn->RuleInputSlot(nSlotRef + srAttTo, psstrmOut);
	int nVal = pslotRoot->GlyphAttrValueEmUnits(ptman, nGlyphAttr);
	vnStack.push_back(nVal);
}

/*----------------------------------------------------------------------------------------------
	Push value of a glyph metric onto the stack.

	@param nSlotRef				- slot whose attribute we are interested in
	@param fInserting			- whether current slot was inserted
	@param nGlyphAttr			- glyph attribute to get
	@param nAttLevel			- for accessing composite metrics
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushGlyphMetric(GrTableManager * ptman, int nSlotRef, bool fInserting,
	int nGlyphAttr, int nAttLevel,
	std::vector<int> & vnStack, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	GrSlotState * pslot = psstrmIn->RuleInputSlot(nSlotRef, psstrmOut, true);
	if (pslot == NULL)
	{
		// Non-existent pre-context item.
		vnStack.push_back(0);
		return;
	}

	DoPushGlyphMetricAux(ptman, pslot, nGlyphAttr, nAttLevel, vnStack, psstrmIn);
}

/*----------------------------------------------------------------------------------------------
	Push value of a glyph metric onto the stack. The slot of interest is the slot to
	which the current slot is attached.

	@param nSlotRef				- slot whose attribute we are interested in
	@param fInserting			- whether current slot was inserted
	@param nGlyphAttr			- glyph attribute to get
	@param nAttLevel			- for accessing composite metrics
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
	@param psstrmOut			- output stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushAttToGlyphMetric(GrTableManager * ptman, int nSlotRef, bool fInserting,
	int nGlyphAttr, int nAttLevel,
	std::vector<int> & vnStack,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	//	Note that it is the slot in the output stream that has
	//	the relevant attach.to attribute set.
	GrSlotState * pslotLeaf = psstrmOut->RuleOutputSlot(0);
	int srAttTo = pslotLeaf->AttachTo();
	if (srAttTo == 0)
	{
		gAssert(false);
		vnStack.push_back(0);
		return;
	}

	GrSlotState * pslot = psstrmIn->RuleInputSlot(nSlotRef + srAttTo, psstrmOut);

	DoPushGlyphMetricAux(ptman, pslot, nGlyphAttr, nAttLevel, vnStack, psstrmIn);
}

/*--------------------------------------------------------------------------------------------*/

void GrPass::DoPushGlyphMetricAux(GrTableManager * ptman,
	GrSlotState * pslot, int nGlyphAttr, int nAttLevel,
	std::vector<int> & vnStack, GrSlotStream * psstrmIn)
{
	int mVal;
	GlyphMetric gmet = GlyphMetric(nGlyphAttr);
	if (nAttLevel == 0 || gmet == kgmetAscent || gmet == kgmetDescent)
	{
		mVal = pslot->GlyphMetricEmUnits(ptman, nGlyphAttr);
	}
	else
	{
		pslot->CalcCompositeMetrics(ptman, psstrmIn, nAttLevel, true);

		float xy;
		switch (gmet)
		{
		case kgmetLsb:			xy = pslot->ClusterLsb(psstrmIn);		break;
		case kgmetRsb:			xy = pslot->ClusterRsb(psstrmIn);		break;
		case kgmetBbTop:		xy = pslot->ClusterBbTop(psstrmIn);		break;
		case kgmetBbBottom:		xy = pslot->ClusterBbBottom(psstrmIn);	break;
		case kgmetBbLeft:		xy = pslot->ClusterBbLeft(psstrmIn);	break;
		case kgmetBbRight:		xy = pslot->ClusterBbRight(psstrmIn);	break;
		case kgmetBbHeight:		xy = pslot->ClusterBbHeight(psstrmIn);	break;
		case kgmetBbWidth:		xy = pslot->ClusterBbWidth(psstrmIn);	break;
//		case kgmetAdvHeight:	xy = pslot->ClusterAdvHeight(psstrmIn);	break;
		case kgmetAdvWidth:		xy = pslot->ClusterAdvWidth(psstrmIn);	break;
		default:
			gAssert(false);
			xy = 0;
		}
		mVal = ptman->LogToEmUnits(xy);
	}

	vnStack.push_back(mVal);
}

/*----------------------------------------------------------------------------------------------
	Push a feature value onto the stack. For rule actions, the slot of interest is the
	current slot.

	@param nSlotRef				- slot being examined
	@param fInserting			- whether current slot was inserted
	@param nFeat				- feature of interest
	@param vnStack				- stack to push onto
	@param psstrmIn				- input stream
	@param psstrmOut			- output stream
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushFeatValue(GrTableManager * ptman, int nSlotRef, bool fInserting,
	int nFeat, std::vector<int> & vnStack, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	gAssert(!fInserting);

	GrSlotState * pslot = psstrmIn->RuleInputSlot(nSlotRef, psstrmOut, true);
	if (pslot == NULL)
	{
		// Non-existent pre-context item.
		vnStack.push_back(0);
		return;
	}

	int nVal = pslot->FeatureValue(nFeat);
	vnStack.push_back(nVal);
}

/*----------------------------------------------------------------------------------------------
	Push a processing-state value onto the stack.

	@param nFeat				- feature of interest
	@param vnStack				- stack to push onto
----------------------------------------------------------------------------------------------*/
void GrPass::DoPushProcState(GrTableManager * ptman, int nPState, std::vector<int> & vnStack)
{
	int nValue;
	if (nPState == kpstatJustifyMode)
	{
		// Convert from internal modes to user modes.
		int jmodi = ptman->InternalJustificationMode();
		switch (jmodi)
		{
		case kjmodiNormal:
		case kjmodiCanShrink:
			nValue = kjmoduNormal;
			break;
		case kjmodiMeasure:
			nValue = kjmoduMeasure;
			break;
		case kjmodiJustify:
			nValue = kjmoduJustify;
			break;
		default:
			gAssert(false);
			nValue = kjmoduNormal;
		}
	}
	else if (nPState == kpstatJustifyLevel)
		nValue = 1; // TODO: get justify level from ptman
	vnStack.push_back(nValue);
}

} // namespace gr