summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite-engine/src/segment/GrFSM.cpp
blob: c36670c96340cfb12799aefa761ef31ddec2b351 (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
/*--------------------------------------------------------------------*//*: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: GrFSM.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
	Implements the finite state machine.
----------------------------------------------------------------------------------------------*/

//:>********************************************************************************************
//:>	   Include files
//:>********************************************************************************************
#include "Main.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
// any other headers (not precompiled)

#undef THIS_FILE
DEFINE_THIS_FILE

//:End Ignore

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

//:>********************************************************************************************
//:>	   Local Constants and static variables
//:>********************************************************************************************

namespace gr
{

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

/*----------------------------------------------------------------------------------------------
	Fill in the FSM by reading from the font stream.
	Assumes the stream is in the correct position.
----------------------------------------------------------------------------------------------*/
bool GrFSM::ReadFromFont(GrIStream & grstrm, int fxdVersion)
{
	short snTmp;
	
	//	number of FSM states
	snTmp = grstrm.ReadShortFromFont();
	m_crow = snTmp;
	//	number of transitional states
	snTmp = grstrm.ReadShortFromFont();
	int crowTransitional = snTmp;
	//	number of success states
	snTmp = grstrm.ReadShortFromFont();
	int crowSuccess = snTmp;
	m_crowFinal = m_crow - crowTransitional;
	m_crowNonAcpt = m_crow - crowSuccess;
	m_rowFinalMin = crowTransitional;
	//	number of columns
	snTmp = grstrm.ReadShortFromFont();
	m_ccol = snTmp;

	//	Sanity checks.
	if (crowTransitional > m_crow || crowSuccess > m_crow)
		return false; // bad table
	//	TODO: add a sanity check for m_ccol.

	//	number of FSM glyph ranges and search constants
	snTmp = grstrm.ReadShortFromFont();
	m_cmcr = snTmp;
	snTmp = grstrm.ReadShortFromFont();
	m_dimcrInit = snTmp;
	snTmp = grstrm.ReadShortFromFont();
	m_cLoop = snTmp;
	snTmp = grstrm.ReadShortFromFont();
	m_imcrStart = snTmp;

	m_prgmcr = new GrFSMClassRange[m_cmcr];
	for (int imcr = 0; imcr < m_cmcr; imcr++)
	{
		m_prgmcr[imcr].m_chwFirst = grstrm.ReadUShortFromFont();
		m_prgmcr[imcr].m_chwLast = grstrm.ReadUShortFromFont();
		m_prgmcr[imcr].m_col = grstrm.ReadUShortFromFont();
	}

	// rule map and offsets (extra item at end gives final offset, ie, total)
	m_prgirulnMin = new data16[crowSuccess + 1];
	data16 * pchw = m_prgirulnMin;
	int i;
	for (i = 0; i < (crowSuccess + 1); i++, pchw++)
	{
		*pchw = grstrm.ReadUShortFromFont();
	}

	//	Last offset functions as the total length of the rule list.
	//	Note that the number of rules in the map is not necessarily equal to the number of
	//	rules in the pass; some rules may be listed multiply, if they are matched by more
	//	than one state.
	int crulInMap = m_prgirulnMin[crowSuccess];
	m_prgrulnMatched = new data16[crulInMap];
	m_crulnMatched = crulInMap;
	pchw = m_prgrulnMatched;
	for (i = 0; i < crulInMap; i++, pchw++)
	{
		*pchw = grstrm.ReadUShortFromFont();
	}

	//	min rule pre-context number of items
	byte bTmp = grstrm.ReadByteFromFont();
	m_critMinRulePreContext = bTmp;
	//	max rule pre-context number of items
	bTmp = grstrm.ReadByteFromFont();
	m_critMaxRulePreContext = bTmp;

	if (m_critMinRulePreContext > kMaxSlotsPerRule || m_critMaxRulePreContext > kMaxSlotsPerRule)
		return false; // bad table

	int cStartStates = m_critMaxRulePreContext - m_critMinRulePreContext + 1;

	//	start states
	m_prgrowStartStates = new short[cStartStates];
	short * psn = m_prgrowStartStates;
	for (int ic = 0; ic < cStartStates;	ic++, psn++)
	{
		*psn = grstrm.ReadShortFromFont();
	}

	return true;
}

/*----------------------------------------------------------------------------------------------
	Fill in the FSM's state table by reading from the font stream.
	Assumes the stream is in the correct position.
----------------------------------------------------------------------------------------------*/
bool GrFSM::ReadStateTableFromFont(GrIStream & grstrm, int fxdVersion)
{
	int cCells = ((m_crow - m_crowFinal) * m_ccol);
	m_prgrowTransitions = new short[cCells];
	short * psn = m_prgrowTransitions;
	for (int iCell = 0; iCell < cCells; iCell++, psn++)
	{
		*psn = grstrm.ReadShortFromFont();
	}

	return true;
}

/*----------------------------------------------------------------------------------------------
	Use the transition table to find a rule or rules that match; then run the contraint
	code to see if the rule is active. Return the rule number of the rule to be applied.

	@param ppass		- pass, which knows how to run constraint code
	@param psstrmIn		- input stream
	@param psstrmOut	- output stream (from which to get recently output slots)
----------------------------------------------------------------------------------------------*/
int GrFSM::GetRuleToApply(GrTableManager * ptman, GrPass * ppass,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut)
{
	//	List of accepting states and corresponding count of slots that were
	//	matched by each.
	int prgrowAccepting[kMaxSlotsPerRule];
	int prgcslotMatched[kMaxSlotsPerRule];

	//	Run the transition table until it jams.
	int crowAccepting = RunTransitionTable(ppass, psstrmIn, psstrmOut,
		prgrowAccepting, prgcslotMatched);

	//	Nothing matched; fail quickly.
	if (crowAccepting == 0)
		return -1;

	int * prow   = prgrowAccepting + crowAccepting - 1;
	int * pcslot = prgcslotMatched + crowAccepting - 1; // # of slots matched AFTER the curr pos

	//	Quick test for common case of exactly one rule matched.
	if (crowAccepting == 1 &&
		(m_prgirulnMin[*prow - m_crowNonAcpt + 1] - m_prgirulnMin[*prow - m_crowNonAcpt]) == 1)
	{
		int iruln = m_prgirulnMin[*prow - m_crowNonAcpt];
		int ruln = m_prgrulnMatched[iruln];
		if (RunConstraintAndRecordLog(ptman, ppass, ruln, psstrmIn, psstrmOut,
			ppass->PreModContextForRule(ruln), *pcslot))
		{
			return ruln;
		}
		else
			return -1;
	}

	//	Several rules were matched. We need to sort them and then run the constraints
	//	for each rule, accepting the first one whose constraints succeed.

	//	Count up the number of rules that matched and allocate a buffer of the right size.
	int crulMatched = 0;
	while (prow >= prgrowAccepting)
	{
		crulMatched +=
			m_prgirulnMin[*prow - m_crowNonAcpt + 1] - m_prgirulnMin[*prow - m_crowNonAcpt];
		--prow;
	}
	MatchedRule * prgmr = new MatchedRule[crulMatched];

	prow = prgrowAccepting + crowAccepting - 1;

	//	Do a dumb insertion sort, ordering primarily by sort key (largest first)
	//	and secondarily by the order in the original file (rule number).
	//	Review: is this kind of sort fast enough? We assuming that we usually only
	//	have a handful of matched rules.
	int cmr = 0;
	while (prow >= prgrowAccepting)
	{
		int iruln = m_prgirulnMin[*prow - m_crowNonAcpt];
		//	Get all the rules matches by a single state.
		while (iruln < m_prgirulnMin[*prow - m_crowNonAcpt + 1])
		{
			int ruln = m_prgrulnMatched[iruln];
			int nSortKey = ppass->SortKeyForRule(ruln);
			int cslot = *pcslot;
			int imr;
			for (imr = 0; imr < cmr; imr++)
			{
				if (nSortKey > prgmr[imr].nSortKey ||
					(nSortKey == prgmr[imr].nSortKey && ruln < prgmr[imr].ruln))
				{
					//	Insert here.
					memmove(prgmr+imr+1, prgmr+imr, (isizeof(MatchedRule) * (cmr - imr)));
					prgmr[imr].ruln = ruln;
					prgmr[imr].nSortKey = nSortKey;
					prgmr[imr].cslot = cslot;
					break;
				}
			}
			if (imr >= cmr)
			{
				prgmr[cmr].ruln = ruln;
				prgmr[cmr].nSortKey = nSortKey;
				prgmr[cmr].cslot = cslot;
			}
			cmr++;

			iruln++;
		}
		--prow;
		--pcslot;
	}


	for (int imr = 0; imr < cmr; imr++)
	{
		int ruln = prgmr[imr].ruln;
		if (RunConstraintAndRecordLog(ptman, ppass, ruln, psstrmIn, psstrmOut,
			ppass->PreModContextForRule(ruln), prgmr[imr].cslot))
		{
			//	Success!
			delete[] prgmr;
			return ruln;
		}
	}

	//	No rule matched or the constraints failed.
	delete[] prgmr;
	return -1;
}

/*----------------------------------------------------------------------------------------------
	Run the transition table, generating a list of rows corresponding to accepting states.

	@param psstrmIn			- input stream
	@param prgrowAccepting	- array to fill in with accepting rows
	@param prgcslotMatched	- corresponding array with number of slots matched
----------------------------------------------------------------------------------------------*/
int GrFSM::RunTransitionTable(GrPass * ppass, GrSlotStream * psstrmIn, GrSlotStream * psstrmOut,
	int * prgrowAccepting, int * prgcslotMatched)
{
	int ipass = ppass->PassNumber();

	// Remember we're reading the pre-context items from the output stream, not the input.
	int cslotPreContextAvail = psstrmOut->WritePos();

	if (cslotPreContextAvail < m_critMinRulePreContext)
		return 0;	// not enough input to match any rule

	//	Look backwards by the number of pre-context items for this pass, but not earlier
	//	than the beginning of the stream.
	int cslot = -1 * min(cslotPreContextAvail, m_critMaxRulePreContext);

	//	Normally the start state is state 0. But if we are near the beginning of the stream,
	//	we may need to adjust due to the fact that we don't have enough input yet to
	//	match the pre-context items in all the rules. So in effect we skip some states
	//	corresponding to the earlier items that aren't present.
	int row = m_prgrowStartStates[max(0, m_critMaxRulePreContext - cslotPreContextAvail)];

	int * prowTop = prgrowAccepting;
	int * pcslotTop = prgcslotMatched;
	while (true)
	{
		if (row >= m_rowFinalMin)
			//	final state--jammed
			return (prowTop - prgrowAccepting);

		if (psstrmIn->SlotsPendingInContext() <= cslot)
			//	no more input
			return (prowTop - prgrowAccepting);

		//	Figure out what column the input glyph belong in.
		//	Since this is a time-critical routine, we cache the results for the current pass.
		GrSlotState * pslot = (cslot < 0) ?
			psstrmOut->PeekBack(cslot) :
			psstrmIn->Peek(cslot);
		int col;
		if (pslot->PassNumberForColumn() == ipass)
		{
			col = pslot->FsmColumn();
		}
		else
		{
			gid16 chwGlyphID = (cslot < 0) ?
				psstrmOut->PeekBack(cslot)->GlyphID() :
				psstrmIn->Peek(cslot)->GlyphID();
			col = FindColumn(chwGlyphID);
			pslot->CacheFsmColumn(ipass, col);
		}
		int rowNext;
		if (col < 0)
			rowNext = 0;
		else
			rowNext = m_prgrowTransitions[(row * m_ccol) + col];

		if (rowNext == 0)	// jammed
			return (prowTop - prgrowAccepting);

		cslot++;
		if (rowNext >= m_crowNonAcpt)	// or (rowNext < 0)
		{
			//	Accepting state--push it on the stack.
			//rowNext *= -1;
			gAssert((prowTop - prgrowAccepting) < kMaxSlotsPerRule);
			*prowTop++ = rowNext;
			*pcslotTop++ = cslot;
		}
		row = rowNext;

		gAssert((prowTop - prgrowAccepting) == (pcslotTop - prgcslotMatched));
	}
}

/*----------------------------------------------------------------------------------------------
	Run the transition table, generating a list of rows corresponding to accepting states.
	Optimized to avoid matrix arithmetic for non-accepting states.

	Review: do we need to use this version?

	@param psstrmIn			- input stream
	@param prgrowAccepting	- array to fill in with accepting rows
	@param prgcslotMatched	- corresponding array with number of slots matched
----------------------------------------------------------------------------------------------*/
#if 0

int GrFSM::RunTransitionTableOptimized(GrSlotStream * psstrmIn, GrSlotStream * psstrmOut,
	int * prgrowAccepting, int * prgcslotMatched)
{
	int cslot = 0;
	int ichwRowOffset = 0;
	int ichwRowOffsetLim = m_crowFinal * m_ccol;	// number of cells
	int * prowTop = prgrowAccepting;
	int * pcslotTop = prgcslotMatched;
	while (true)
	{
		if (ichwRowOffset >= ichwRowOffsetLim)
			//	final state--jammed
			return (prowTop - prgrowAccepting);

		if (psstrmIn->SlotsPendingInContext() < cslot)
			//	no more input
			return (prowTop - prgrowAccepting);

		gid16 chwGlyphID = psstrmIn->Peek(cslot)->GlyphID();
		int col = FindColumn(chwGlyphID);
		short cellValue = *(m_prgprgrowXitions + ichwRowOffset + col);

		if (cellValue == 0)	// jammed
			return (prowTop - prgrowAccepting);

		cslot++;
		if (cellValue < 0)
		{
			//	Accepting state--push it on the stack.
			int rowNext = cellValue * -1;
			gAssert((prowTop - prgrowAccepting) < kMaxSlotsPerRule);
			*prowTop++ = rowNext;
			*pcslotTop++ = cslot;
			ichwRowOffset = rowNext * m_ccol;
		}
		else
			ichwRowOffset = cellValue;
		
		gAssert((prowTop - prgrowAccepting) == (pcslotTop - prgcslotMatched));
	}
}

#endif // 0


/*----------------------------------------------------------------------------------------------
	Run all the constraints for the rules matched by the state that we have reached.
	The rules are ordered as they were in the GDL file. When we hit a constraint that
	matches, return the corresponding rule number. Return -1 if no rule passed.

	@param ppass			- pass, which knows how to actually run the constraints
	@param psstrmIn			- input stream
	@param cslotMatched		- number of slots matched; constraints must be run for each of these
----------------------------------------------------------------------------------------------*/
//:Ignore
int GrFSM::RunConstraints_Obsolete(GrTableManager * ptman, GrPass * ppass, int row,
	GrSlotStream * psstrmIn, int cslotMatched)
{
	int iruln = m_prgirulnMin[row - m_crowNonAcpt];
	while (iruln < m_prgirulnMin[row - m_crowNonAcpt + 1])
	{
		int ruln = m_prgrulnMatched[iruln];
		bool fSuccess = ppass->RunConstraint(ptman, ruln, psstrmIn, NULL, 0, cslotMatched);
		if (fSuccess)
		{
			if (ptman->LoggingTransduction())
				ppass->RecordRuleFired(psstrmIn->ReadPosForNextGet(), ruln);
			return ruln;
		}
		else
		{
			if (ptman->LoggingTransduction())
				ppass->RecordRuleFailed(psstrmIn->ReadPosForNextGet(), ruln);
		}

		iruln++;
	}
	return -1;	// no rule succeeded
}
//:End Ignore

/*----------------------------------------------------------------------------------------------
	Run the constraints for the given rule, and record whether it failed or succeeded in the
	transduction log.

	@param ppass				- pass, which knows how to actually run the constraints
	@param psstrmIn				- input stream
	@param cslotPreModContext	- number of slots before the current position that must be tested
	@param islotLimMatched		- number of slots matched AFTER the current position

	@return True if the constraints succeeded.
----------------------------------------------------------------------------------------------*/
bool GrFSM::RunConstraintAndRecordLog(GrTableManager * ptman, GrPass * ppass, int ruln,
	GrSlotStream * psstrmIn, GrSlotStream * psstrmOut,
	int cslotPreModContext, int islotLimMatched)
{
	bool fSuccess = ppass->RunConstraint(ptman, ruln, psstrmIn, psstrmOut,
		cslotPreModContext, islotLimMatched);

	if (ptman->LoggingTransduction())
	{
		if (fSuccess)
			ppass->RecordRuleFired(psstrmIn->ReadPosForNextGet(), ruln);
		else
			ppass->RecordRuleFailed(psstrmIn->ReadPosForNextGet(), ruln);
	}

	return fSuccess;
}

/*----------------------------------------------------------------------------------------------
	Search for the class range containing the glyph ID, and return the matching FSM column.
	Does a fast binary search that uses only shifts, no division.
----------------------------------------------------------------------------------------------*/
int GrFSM::FindColumn(gid16 chwGlyphID)
{
	#ifdef _DEBUG
		//	Check that the pre-calculated constants are correct.
		int nPowerOf2 = 1;
		while (nPowerOf2 <= m_cmcr)
			nPowerOf2 <<= 1;
		nPowerOf2 >>= 1;
		//	Now nPowerOf2 is the max power of 2 <= m_cclsrg.
		//	m_cLoop is not needed for our purposes, but it is included because it is part of
		//	the TrueType standard.
		gAssert(1 << m_cLoop == nPowerOf2);		// m_cloop == log2(nPowerOf2)
		gAssert(m_dimcrInit == nPowerOf2);
		gAssert(m_imcrStart == m_cmcr - m_dimcrInit);
	#endif

	int dimcrCurr = m_dimcrInit;

	GrFSMClassRange * pmcrCurr = m_prgmcr + m_imcrStart;
	while (dimcrCurr > 0) 
	{
		int nTest;
		if (pmcrCurr < m_prgmcr)
			nTest = -1;
		else
		{
			nTest = pmcrCurr->m_chwFirst - chwGlyphID;
			if (nTest < 0 && chwGlyphID <= pmcrCurr->m_chwLast)
				//nTest = 0;
				// This is a tiny bit more efficient:
				return pmcrCurr->m_col;
		}

		if (nTest == 0)
			//	found it
			return pmcrCurr->m_col;

		dimcrCurr >>= 1;	// divide by 2
		if (nTest < 0)
			pmcrCurr += dimcrCurr;
		else // (nTest > 0)
			pmcrCurr -= dimcrCurr;
	}

	return -1;
}

} // namespace gr