-
Notifications
You must be signed in to change notification settings - Fork 11
/
subTablesBase.py
455 lines (382 loc) · 18.8 KB
/
subTablesBase.py
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
from __future__ import print_function
import weakref
# ------------
# Base Classes
# ------------
class BaseSubTable(object):
"""
This object implents the base level subtable behavior
for GSUB and GPOS subtables. It establishes one private
attribute, _lookup, which is a weak reference to the
lookup that contains the subtable.
"""
__slots__ = ["_lookup"]
def __init__(self):
self._lookup = None
def loadFromFontTools(self, subtable, lookup):
self._lookup = weakref.ref(lookup)
return self
def process(self, processed, glyphRecords, featureTag):
if self._lookup is not None and hasattr(self._lookup(), "LookupType"):
lookupType = self._lookup().LookupType
else:
lookupType = "Unknown"
if hasattr(self, "SubstFormat"):
format = str(self.SubstFormat)
elif hasattr(self, "PosFormat"):
format = str(self.PosFormat)
else:
format = "Unknown"
className = self.__class__.__name__
print("[Compositor] %s skipping Lookup Type %s Format %s" % (className, lookupType, format))
return processed, glyphRecords, False
def _lookupFlagCoversGlyph(self, glyphName):
return self._lookup().LookupFlag.coversGlyph(glyphName)
def _nextRecord(self, glyphRecords):
nextRecord = None
nextRecordIndex = 0
while nextRecord is None:
for _nextRecord in glyphRecords:
_nextGlyph = _nextRecord.glyphName
if not self._lookupFlagCoversGlyph(_nextGlyph):
nextRecord = _nextRecord
break
nextRecordIndex += 1
break
return nextRecord, nextRecordIndex
class BaseContextSubTable(BaseSubTable):
__slots__ = []
def _processMatch(self, rule, processed, glyphRecords, inputGlyphCount, matchedIndexes, featureTag):
performedAction = False
if not rule._ActionCount:
performedAction = True
processed.extend(glyphRecords[:inputGlyphCount])
glyphRecords = glyphRecords[inputGlyphCount:]
else:
eligibleRecords = glyphRecords[:inputGlyphCount]
ineligibleRecords = glyphRecords[inputGlyphCount:]
for record in rule._ActionLookupRecord:
sequenceIndex = record.SequenceIndex
matchIndex = matchedIndexes[sequenceIndex]
backRecords = eligibleRecords[:matchIndex]
inputRecords = eligibleRecords[matchIndex:]
lookupListIndex = record.LookupListIndex
lookup = self._lookup()._lookupList().Lookup[lookupListIndex]
for subtable in lookup.SubTable:
backRecords, inputRecords, performedAction = subtable.process(backRecords, inputRecords, featureTag)
if performedAction:
break
if performedAction:
eligibleRecords = backRecords + inputRecords
processed.extend(eligibleRecords)
glyphRecords = ineligibleRecords
return processed, glyphRecords, performedAction
class BaseChainingContextSubTable(BaseContextSubTable):
__slots__ = []
def _testContext(self, testSource, testAgainst, matchCount, additionObjects=None):
# this procedure is common across all formats
# with the exception of evaluating if a particular
# glyph matches a position in the context.
# to handle this, the comparison is evaluated
# by a _evaluateContextItem method in each
# subclass. the speed penalty for this is negligible.
# the aditionalObjects arg will be ignored by
# all formats except format 2 which needs a ClassDef
# to perform the comparison.
completeRun = []
matchedIndexes = []
matched = 0
while matched < matchCount:
for recordIndex, glyphRecord in enumerate(testSource):
completeRun.append(glyphRecord)
glyphName = glyphRecord.glyphName
if not self._lookupFlagCoversGlyph(glyphName):
if not self._evaluateContextItem(glyphName, testAgainst[matched], additionObjects):
break
matched += 1
matchedIndexes.append(recordIndex)
if matched == matchCount:
break
break
return matched == matchCount, completeRun, matchedIndexes
class BaseContextFormat1SubTable(BaseContextSubTable):
__slots__ = []
def process(self, processed, glyphRecords, featureTag):
performedAction = False
currentRecord = glyphRecords[0]
currentGlyph = currentRecord.glyphName
if currentGlyph in self.Coverage:
if not self._lookupFlagCoversGlyph(currentGlyph):
coverageIndex = self.Coverage.index(currentGlyph)
ruleSet = self._RuleSet[coverageIndex]
for rule in ruleSet._Rule:
matchedIndexes = [0]
currentGlyphIndex = 1
for input in rule.Input:
glyphRecord, relativeIndex = self._nextRecord(glyphRecords[currentGlyphIndex:])
currentGlyphIndex += relativeIndex
if glyphRecord is not None:
glyphName = glyphRecord.glyphName
if glyphName != input:
break
else:
matchedIndexes.append(currentGlyphIndex)
currentGlyphIndex += 1
if len(matchedIndexes) == rule.GlyphCount:
inputGlyphCount = matchedIndexes[-1] + 1
processed, glyphRecords, performedAction = self._processMatch(rule, processed, glyphRecords, inputGlyphCount, matchedIndexes, featureTag)
if performedAction:
break
return processed, glyphRecords, performedAction
class BaseContextFormat2SubTable(BaseContextSubTable):
__slots__ = []
def process(self, processed, glyphRecords, featureTag):
performedAction = False
currentRecord = glyphRecords[0]
currentGlyph = currentRecord.glyphName
if currentGlyph in self.Coverage:
if not self._lookupFlagCoversGlyph(currentGlyph):
classIndex = self.ClassDef[currentGlyph]
classSet = self._ClassSet[classIndex]
if classSet is not None:
matchedIndexes = [0]
currentGlyphIndex = 1
for classRule in classSet._ClassRule:
for inputClass in classRule.Class:
glyphRecord, relativeIndex = self._nextRecord(glyphRecords[currentGlyphIndex:])
currentGlyphIndex += relativeIndex
if glyphRecord is not None:
glyphName = glyphRecord.glyphName
glyphClass = self.ClassDef[glyphName]
if glyphClass != inputClass:
break
else:
matchedIndexes.append(currentGlyphIndex)
currentGlyphIndex += 1
if len(matchedIndexes) == classRule.GlyphCount:
inputGlyphCount = matchedIndexes[-1] + 1
processed, glyphRecords, performedAction = self._processMatch(classRule, processed, glyphRecords, inputGlyphCount, matchedIndexes, featureTag)
return processed, glyphRecords, performedAction
class BaseContextFormat3SubTable(BaseContextSubTable):
__slots__ = []
def process(self, processed, glyphRecords, featureTag):
performedAction = False
matchedIndexes = []
currentGlyphIndex = 0
for coverage in self.Coverage:
glyphRecord, relativeIndex = self._nextRecord(glyphRecords[currentGlyphIndex:])
currentGlyphIndex += relativeIndex
currentGlyph = glyphRecord.glyphName
if currentGlyph not in coverage:
break
matchedIndexes.append(currentGlyphIndex)
currentGlyphIndex += 1
if len(matchedIndexes) == self.GlyphCount:
inputGlyphCount = matchedIndexes[-1] + 1
processed, glyphRecords, performedAction = self._processMatch(self, processed, glyphRecords, inputGlyphCount, matchedIndexes, featureTag)
return processed, glyphRecords, performedAction
class BaseChainingContextFormat1SubTable(BaseChainingContextSubTable):
__slots__ = []
def process(self, processed, glyphRecords, featureTag):
performedAction = False
currentRecord = glyphRecords[0]
currentGlyph = currentRecord.glyphName
if currentGlyph in self.Coverage:
for chainRuleSet in self._ChainRuleSet:
for chainRule in chainRuleSet._ChainRule:
# backtrack testing
backtrackCount = chainRule.BacktrackGlyphCount
if not backtrackCount:
backtrackMatch = True
else:
backtrackMatch, backtrack, backtrackMatchIndexes = self._testContext(reversed(processed), chainRule.Backtrack, backtrackCount)
if not backtrackMatch:
continue
# input testing
inputCount = chainRule.InputGlyphCount
if not inputCount:
inputMatch = True
else:
inputMatch, input, inputMatchIndexes = self._testContext(glyphRecords[1:], chainRule.Input, inputCount-1)
if not inputMatch:
continue
input = [currentRecord] + input
inputMatchIndexes = [0] + [i + 1 for i in inputMatchIndexes]
# look ahead testing
lookAheadCount = chainRule.LookAheadGlyphCount
if not lookAheadCount:
lookAheadMatch = True
else:
lookAheadMatch, lookAhead, lookAheadMatchIndexes = self._testContext(glyphRecords[len(input):], chainRule.LookAhead, lookAheadCount)
if not lookAheadMatch:
continue
# match. process.
if backtrackMatch and inputMatch and lookAheadMatch:
processed, glyphRecords, performedAction = self._processMatch(chainRule, processed, glyphRecords, len(input), inputMatchIndexes, featureTag)
if performedAction:
# break the chainRule loop
break
if performedAction:
# break the chainRuleSet loop
break
return processed, glyphRecords, performedAction
def _evaluateContextItem(self, glyphName, contextTest, additionalObject):
return glyphName == contextTest
class BaseChainingContextFormat2SubTable(BaseChainingContextSubTable):
__slots__ = []
def process(self, processed, glyphRecords, featureTag):
performedAction = False
currentRecord = glyphRecords[0]
currentGlyph = currentRecord.glyphName
if currentGlyph in self.Coverage:
if not self._lookupFlagCoversGlyph(currentGlyph):
classIndex = self.InputClassDef[currentGlyph]
chainClassSet = self._ChainClassSet[classIndex]
if chainClassSet is not None:
for chainClassRule in chainClassSet._ChainClassRule:
# backtrack testing
backtrackCount = chainClassRule.BacktrackGlyphCount
if not backtrackCount:
backtrackMatch = True
else:
backtrackMatch, backtrack, backtrackMatchIndexes = self._testContext(reversed(processed), chainClassRule.Backtrack, backtrackCount, self.BacktrackClassDef)
if not backtrackMatch:
continue
# input testing
inputCount = chainClassRule.InputGlyphCount
if not inputCount:
inputMatch = True
else:
inputMatch, input, inputMatchIndexes = self._testContext(glyphRecords[1:], chainClassRule.Input, inputCount-1, self.InputClassDef)
if not inputMatch:
continue
input = [currentRecord] + input
inputMatchIndexes = [0] + [i + 1 for i in inputMatchIndexes]
# look ahead testing
lookAheadCount = chainClassRule.LookAheadGlyphCount
if not lookAheadCount:
lookAheadMatch = True
else:
lookAheadMatch, lookAhead, lookAheadMatchIndexes = self._testContext(glyphRecords[len(input):], chainClassRule.LookAhead, lookAheadCount, self.LookAheadClassDef)
if not lookAheadMatch:
continue
# match. process.
if backtrackMatch and inputMatch and lookAheadMatch:
processed, glyphRecords, performedAction = self._processMatch(chainClassRule, processed, glyphRecords, len(input), inputMatchIndexes, featureTag)
if performedAction:
break
return processed, glyphRecords, performedAction
def _evaluateContextItem(self, glyphName, contextTest, additionalObject):
classDef = additionalObject
classIndex = classDef[glyphName]
return classIndex == contextTest
class BaseChainingContextFormat3SubTable(BaseChainingContextSubTable):
"""
This object implements chaining contextual format 3.
It is shared across GSUB and GPOS contextual subtables.
"""
__slots__ = ["BacktrackGlyphCount", "BacktrackCoverage", "InputGlyphCount",
"InputCoverage", "LookAheadGlyphCount", "LookAheadCoverage"]
def __init__(self):
super(BaseChainingContextFormat3SubTable, self).__init__()
self.BacktrackGlyphCount = 0
self.BacktrackCoverage = []
self.InputGlyphCount = 0
self.InputCoverage = []
self.LookAheadGlyphCount = 0
self.LookAheadCoverage = []
def loadFromFontTools(self, subtable, lookup):
super(BaseChainingContextFormat3SubTable, self).loadFromFontTools(subtable, lookup)
self.BacktrackGlyphCount = subtable.BacktrackGlyphCount
self.BacktrackCoverage = [Coverage().loadFromFontTools(coverage) for coverage in subtable.BacktrackCoverage]
self.InputGlyphCount = subtable.InputGlyphCount
self.InputCoverage = [Coverage().loadFromFontTools(coverage) for coverage in subtable.InputCoverage]
self.LookAheadGlyphCount = subtable.LookAheadGlyphCount
self.LookAheadCoverage = [Coverage().loadFromFontTools(coverage) for coverage in subtable.LookAheadCoverage]
return self
def process(self, processed, glyphRecords, featureTag):
performedAction = False
while 1:
# backtrack testing
backtrackCount = self.BacktrackGlyphCount
if not backtrackCount:
backtrackMatch = True
else:
backtrackMatch, backtrack, backtrackMatchIndexes = self._testContext(reversed(processed), self.BacktrackCoverage, backtrackCount)
if not backtrackMatch:
break
# input testing
inputCount = self.InputGlyphCount
if not inputCount:
inputMatch = True
else:
inputMatch, input, inputMatchIndexes = self._testContext(glyphRecords, self.InputCoverage, inputCount)
if not inputMatch:
break
# look ahead testing
lookAheadCount = self.LookAheadGlyphCount
if not lookAheadCount:
lookAheadMatch = True
else:
lookAheadMatch, lookAhead, lookAheadMatchIndexes = self._testContext(glyphRecords[len(input):], self.LookAheadCoverage, lookAheadCount)
if not lookAheadMatch:
break
# match. process.
if backtrackMatch and inputMatch and lookAheadMatch:
processed, glyphRecords, performedAction = self._processMatch(self, processed, glyphRecords, len(input), inputMatchIndexes, featureTag)
# break the while
break
return processed, glyphRecords, performedAction
def _evaluateContextItem(self, glyphName, contextTest, additionalObject):
return glyphName in contextTest
class BaseLookupRecord(object):
"""
This object implements the functionality of both
GSUB SubstLookupRecord and GPOS PosLookupRecord.
"""
__slots__ = ["SequenceIndex", "LookupListIndex"]
def __init__(self):
self.SequenceIndex = None
self.LookupListIndex = None
def loadFromFontTools(self, record):
self.SequenceIndex = record.SequenceIndex
self.LookupListIndex = record.LookupListIndex
return self
# --------
# Coverage
# --------
class Coverage(object):
"""
fontTools abstracts CoverageFormat1 and
CoverageFormat2 into a common Coverage
object. The same is done here. Consequently
the structure of this object does not closely
follow the specification. Instead, the basic
functionality is implemented through standard
dict methods.
To determine if a glyph is in the coverage:
>>> "x" in coverage
True
To get the index for a particular glyph:
>>> coverage.index("x")
330
"""
__slots__ = ["_glyphs"]
def __init__(self, coverage=None):
if coverage is not None:
coverage = list(coverage)
self._glyphs = coverage
def loadFromFontTools(self, coverage):
# the data coming in could be a fontTools
# Coverage object or a list of glyph names
if not isinstance(coverage, list):
coverage = coverage.glyphs
self._glyphs = list(coverage)
return self
def __contains__(self, glyphName):
return glyphName in self._glyphs
def index(self, glyphName):
return self._glyphs.index(glyphName)
def _get_Glyphs(self):
return list(self._glyphs)
Glyphs = property(_get_Glyphs, doc="This is for reference only. Not for use in processing.")