[varLib.merger] set ClassDef{1,2}.Format using same logic as otTables.ClassDef.preWrite
This commit is contained in:
parent
e6125b353e
commit
23511fd302
@ -418,19 +418,19 @@ class ClassDef(FormatSwitchingBaseTable):
|
||||
assert 0, "unknown format: %s" % self.Format
|
||||
self.classDefs = classDefs
|
||||
|
||||
def preWrite(self, font):
|
||||
def _getClassRanges(self, font):
|
||||
classDefs = getattr(self, "classDefs", None)
|
||||
if classDefs is None:
|
||||
classDefs = self.classDefs = {}
|
||||
format = 2
|
||||
rawTable = {"ClassRangeRecord": []}
|
||||
self.classDefs = {}
|
||||
return
|
||||
getGlyphID = font.getGlyphID
|
||||
items = []
|
||||
for glyphName, cls in classDefs.items():
|
||||
if not cls: continue
|
||||
if not cls:
|
||||
continue
|
||||
items.append((getGlyphID(glyphName), glyphName, cls))
|
||||
items.sort()
|
||||
if items:
|
||||
items.sort()
|
||||
last, lastName, lastCls = items[0]
|
||||
ranges = [[lastCls, last, lastName]]
|
||||
for glyphID, glyphName, cls in items[1:]:
|
||||
@ -441,7 +441,13 @@ class ClassDef(FormatSwitchingBaseTable):
|
||||
lastName = glyphName
|
||||
lastCls = cls
|
||||
ranges[-1].extend([last, lastName])
|
||||
return ranges
|
||||
|
||||
def preWrite(self, font):
|
||||
format = 2
|
||||
rawTable = {"ClassRangeRecord": []}
|
||||
ranges = self._getClassRanges(font)
|
||||
if ranges:
|
||||
startGlyph = ranges[0][1]
|
||||
endGlyph = ranges[-1][3]
|
||||
glyphCount = endGlyph - startGlyph + 1
|
||||
|
@ -342,6 +342,18 @@ def _ClassDef_merge_classify(lst, allGlyphs=None):
|
||||
|
||||
return self, classes
|
||||
|
||||
def _ClassDef_calculate_Format(self, font):
|
||||
fmt = 2
|
||||
ranges = self._getClassRanges(font)
|
||||
if ranges:
|
||||
startGlyph = ranges[0][1]
|
||||
endGlyph = ranges[-1][3]
|
||||
glyphCount = endGlyph - startGlyph + 1
|
||||
if len(ranges) * 3 >= glyphCount + 1:
|
||||
# Format 1 is more compact
|
||||
fmt = 1
|
||||
self.Format = fmt
|
||||
|
||||
def _PairPosFormat2_merge(self, lst, merger):
|
||||
merger.mergeObjects(self, lst,
|
||||
exclude=('Coverage',
|
||||
@ -368,6 +380,7 @@ def _PairPosFormat2_merge(self, lst, merger):
|
||||
|
||||
# Align first classes
|
||||
self.ClassDef1, classes = _ClassDef_merge_classify([l.ClassDef1 for l in lst], allGlyphs=glyphSet)
|
||||
_ClassDef_calculate_Format(self.ClassDef1, merger.font)
|
||||
self.Class1Count = len(classes)
|
||||
new_matrices = []
|
||||
for l,matrix in zip(lst, matrices):
|
||||
@ -397,6 +410,7 @@ def _PairPosFormat2_merge(self, lst, merger):
|
||||
|
||||
# Align second classes
|
||||
self.ClassDef2, classes = _ClassDef_merge_classify([l.ClassDef2 for l in lst])
|
||||
_ClassDef_calculate_Format(self.ClassDef2, merger.font)
|
||||
self.Class2Count = len(classes)
|
||||
new_matrices = []
|
||||
for l,matrix in zip(lst, matrices):
|
||||
|
Loading…
x
Reference in New Issue
Block a user