Merge pull request #869 from anthrotype/varLib-builder-fixes
[varLib.builder] Fix roundtripping XML
This commit is contained in:
commit
9d5e38c439
@ -205,28 +205,6 @@ class VarIdxMap(BaseTable):
|
|||||||
mapping.append((outer << 16) | inner)
|
mapping.append((outer << 16) | inner)
|
||||||
|
|
||||||
|
|
||||||
class VarData(BaseTable):
|
|
||||||
|
|
||||||
def preWrite(self, font):
|
|
||||||
rawTable = self.__dict__.copy()
|
|
||||||
|
|
||||||
# TODO Remove this abstraction/optimization and move it varLib.builder?
|
|
||||||
|
|
||||||
numShorts = 0
|
|
||||||
count = len(self.VarRegionIndex)
|
|
||||||
for item in getattr(self, 'Item', []):
|
|
||||||
assert len(item) == count, ("Item length mismatch", len(item), count)
|
|
||||||
for i in range(count - 1, numShorts - 1, -1):
|
|
||||||
if not (-128 <= item[i] <= 127):
|
|
||||||
numShorts = i + 1
|
|
||||||
break
|
|
||||||
if numShorts == count:
|
|
||||||
break
|
|
||||||
|
|
||||||
rawTable['NumShorts'] = numShorts
|
|
||||||
return rawTable
|
|
||||||
|
|
||||||
|
|
||||||
class SingleSubst(FormatSwitchingBaseTable):
|
class SingleSubst(FormatSwitchingBaseTable):
|
||||||
|
|
||||||
def populateDefaults(self, propagator=None):
|
def populateDefaults(self, propagator=None):
|
||||||
|
@ -6,7 +6,7 @@ from fontTools.ttLib.tables import otTables as ot
|
|||||||
|
|
||||||
def buildVarRegionAxis(axisSupport):
|
def buildVarRegionAxis(axisSupport):
|
||||||
self = ot.VarRegionAxis()
|
self = ot.VarRegionAxis()
|
||||||
self.StartCoord, self.PeakCoord, self.EndCoord = axisSupport
|
self.StartCoord, self.PeakCoord, self.EndCoord = [float(v) for v in axisSupport]
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def buildVarRegion(support, axisTags):
|
def buildVarRegion(support, axisTags):
|
||||||
@ -20,7 +20,7 @@ def buildVarRegion(support, axisTags):
|
|||||||
|
|
||||||
def buildVarRegionList(supports, axisTags):
|
def buildVarRegionList(supports, axisTags):
|
||||||
self = ot.VarRegionList()
|
self = ot.VarRegionList()
|
||||||
self.AxisCount = len(axisTags)
|
self.RegionAxisCount = len(axisTags)
|
||||||
self.Region = []
|
self.Region = []
|
||||||
for support in supports:
|
for support in supports:
|
||||||
self.Region.append(buildVarRegion(support, axisTags))
|
self.Region.append(buildVarRegion(support, axisTags))
|
||||||
@ -39,8 +39,7 @@ def _reorderItem(lst, narrows):
|
|||||||
out.append(lst[i])
|
out.append(lst[i])
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def optimizeVarData(self):
|
def calculateNumShorts(self, optimize=True):
|
||||||
# Reorder columns such that all SHORT columns come before UINT8
|
|
||||||
count = self.VarRegionCount
|
count = self.VarRegionCount
|
||||||
items = self.Item
|
items = self.Item
|
||||||
narrows = set(range(count))
|
narrows = set(range(count))
|
||||||
@ -51,11 +50,15 @@ def optimizeVarData(self):
|
|||||||
break
|
break
|
||||||
if not narrows:
|
if not narrows:
|
||||||
break
|
break
|
||||||
|
if optimize:
|
||||||
|
# Reorder columns such that all SHORT columns come before UINT8
|
||||||
self.VarRegionIndex = _reorderItem(self.VarRegionIndex, narrows)
|
self.VarRegionIndex = _reorderItem(self.VarRegionIndex, narrows)
|
||||||
for i in range(self.ItemCount):
|
for i in range(self.ItemCount):
|
||||||
items[i] = _reorderItem(items[i], narrows)
|
items[i] = _reorderItem(items[i], narrows)
|
||||||
|
self.NumShorts = count - len(narrows)
|
||||||
|
else:
|
||||||
|
wides = set(range(count)) - narrows
|
||||||
|
self.NumShorts = 1+max(wides) if wides else 0
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def buildVarData(varRegionIndices, items, optimize=True):
|
def buildVarData(varRegionIndices, items, optimize=True):
|
||||||
@ -68,8 +71,7 @@ def buildVarData(varRegionIndices, items, optimize=True):
|
|||||||
assert len(item) == regionCount
|
assert len(item) == regionCount
|
||||||
records.append(list(item))
|
records.append(list(item))
|
||||||
self.ItemCount = len(self.Item)
|
self.ItemCount = len(self.Item)
|
||||||
if items and optimize:
|
calculateNumShorts(self, optimize=optimize)
|
||||||
optimizeVarData(self)
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
||||||
@ -119,8 +121,7 @@ class OnlineVarStoreBuilder(object):
|
|||||||
self._store.VarDataCount = len(self._store.VarData)
|
self._store.VarDataCount = len(self._store.VarData)
|
||||||
for data in self._store.VarData:
|
for data in self._store.VarData:
|
||||||
data.ItemCount = len(data.Item)
|
data.ItemCount = len(data.Item)
|
||||||
if optimize:
|
calculateNumShorts(data, optimize)
|
||||||
optimizeVarData(data)
|
|
||||||
return self._store
|
return self._store
|
||||||
|
|
||||||
def storeMasters(self, master_values):
|
def storeMasters(self, master_values):
|
||||||
|
@ -6,15 +6,16 @@
|
|||||||
<VarStore Format="1">
|
<VarStore Format="1">
|
||||||
<Format value="1"/>
|
<Format value="1"/>
|
||||||
<VarRegionList>
|
<VarRegionList>
|
||||||
|
<!-- RegionAxisCount=2 -->
|
||||||
<!-- RegionCount=5 -->
|
<!-- RegionCount=5 -->
|
||||||
<Region index="0">
|
<Region index="0">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
@ -23,46 +24,46 @@
|
|||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="-1.0"/>
|
<StartCoord value="-1.0"/>
|
||||||
<PeakCoord value="-1.0"/>
|
<PeakCoord value="-1.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="2">
|
<Region index="2">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="3">
|
<Region index="3">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="-1.0"/>
|
<StartCoord value="-1.0"/>
|
||||||
<PeakCoord value="-1.0"/>
|
<PeakCoord value="-1.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="4">
|
<Region index="4">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
@ -71,7 +72,7 @@
|
|||||||
<!-- VarDataCount=1 -->
|
<!-- VarDataCount=1 -->
|
||||||
<VarData index="0">
|
<VarData index="0">
|
||||||
<!-- ItemCount=0 -->
|
<!-- ItemCount=0 -->
|
||||||
<NumShorts value="None"/>
|
<NumShorts value="0"/>
|
||||||
<!-- VarRegionCount=5 -->
|
<!-- VarRegionCount=5 -->
|
||||||
<VarRegionIndex index="0" value="0"/>
|
<VarRegionIndex index="0" value="0"/>
|
||||||
<VarRegionIndex index="1" value="1"/>
|
<VarRegionIndex index="1" value="1"/>
|
||||||
@ -87,15 +88,16 @@
|
|||||||
<VarStore Format="1">
|
<VarStore Format="1">
|
||||||
<Format value="1"/>
|
<Format value="1"/>
|
||||||
<VarRegionList>
|
<VarRegionList>
|
||||||
|
<!-- RegionAxisCount=2 -->
|
||||||
<!-- RegionCount=5 -->
|
<!-- RegionCount=5 -->
|
||||||
<Region index="0">
|
<Region index="0">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
@ -104,46 +106,46 @@
|
|||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="-1.0"/>
|
<StartCoord value="-1.0"/>
|
||||||
<PeakCoord value="-1.0"/>
|
<PeakCoord value="-1.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="2">
|
<Region index="2">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="0"/>
|
<PeakCoord value="0.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="3">
|
<Region index="3">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="-1.0"/>
|
<StartCoord value="-1.0"/>
|
||||||
<PeakCoord value="-1.0"/>
|
<PeakCoord value="-1.0"/>
|
||||||
<EndCoord value="0"/>
|
<EndCoord value="0.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
</Region>
|
</Region>
|
||||||
<Region index="4">
|
<Region index="4">
|
||||||
<VarRegionAxis index="0">
|
<VarRegionAxis index="0">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
<VarRegionAxis index="1">
|
<VarRegionAxis index="1">
|
||||||
<StartCoord value="0"/>
|
<StartCoord value="0.0"/>
|
||||||
<PeakCoord value="1.0"/>
|
<PeakCoord value="1.0"/>
|
||||||
<EndCoord value="1.0"/>
|
<EndCoord value="1.0"/>
|
||||||
</VarRegionAxis>
|
</VarRegionAxis>
|
||||||
@ -152,7 +154,7 @@
|
|||||||
<!-- VarDataCount=1 -->
|
<!-- VarDataCount=1 -->
|
||||||
<VarData index="0">
|
<VarData index="0">
|
||||||
<!-- ItemCount=6 -->
|
<!-- ItemCount=6 -->
|
||||||
<NumShorts value="None"/>
|
<NumShorts value="0"/>
|
||||||
<!-- VarRegionCount=5 -->
|
<!-- VarRegionCount=5 -->
|
||||||
<VarRegionIndex index="0" value="0"/>
|
<VarRegionIndex index="0" value="0"/>
|
||||||
<VarRegionIndex index="1" value="1"/>
|
<VarRegionIndex index="1" value="1"/>
|
||||||
|
@ -97,9 +97,13 @@ class BuildTest(unittest.TestCase):
|
|||||||
varfont, model, _ = build(ds_path, finder)
|
varfont, model, _ = build(ds_path, finder)
|
||||||
|
|
||||||
tables = ['GDEF', 'HVAR', 'fvar', 'gvar']
|
tables = ['GDEF', 'HVAR', 'fvar', 'gvar']
|
||||||
#varfont.save(ds_path.replace('.designspace', suffix))
|
expected_ttx = self.get_test_input('BuildTest.ttx')
|
||||||
#varfont.saveXML(ds_path.replace('.designspace', '.ttx'), tables=tables)
|
self.expect_ttx(varfont, expected_ttx, tables)
|
||||||
self.expect_ttx(varfont, self.get_test_input('BuildTest.ttx'), tables)
|
|
||||||
|
# ensure the TTX dump is the same after saving and reloading font
|
||||||
|
varfont_path = os.path.join(self.tempdir, 'BuildTest.ttf')
|
||||||
|
varfont.save(varfont_path)
|
||||||
|
self.expect_ttx(TTFont(varfont_path), expected_ttx, tables)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user