set pre-initialized CountReference in VarRegionList.preWrite with fvar.axisCount

so we can reuse CountReference class, as suggested in https://github.com/fonttools/fonttools/pull/1752#issuecomment-547113944

The patch is no shorter though.
This commit is contained in:
Cosimo Lupo 2019-10-29 12:52:42 +00:00
parent e8f5bbcc6a
commit ebadcd96e6
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642
2 changed files with 18 additions and 14 deletions

View File

@ -498,11 +498,6 @@ class OTTableWriter(object):
return OverflowErrorRecord( (self.tableTag, LookupListIndex, SubTableIndex, itemName, itemIndex) )
class StaticCount(int):
"""A count value that should be taken literally, rather than recomputed on compile."""
pass
class CountReference(object):
"""A reference to a Count value, not a count of references."""
def __init__(self, table, name, size=None, value=None):
@ -518,6 +513,8 @@ class CountReference(object):
table[name] = value
else:
assert table[name] == value, (name, table[name], value)
def getValue(self):
return self.table[self.name]
def getCountData(self):
v = self.table[self.name]
if v is None: v = 0
@ -695,12 +692,16 @@ class BaseTable(object):
# table. We will later store it here.
# We add a reference: by the time the data is assembled
# the Count value will be filled in.
if not isinstance(value, StaticCount):
# we ignore the current count value since it's being recomputed,
# unless this is of type StaticCount, which is assumed to be correct.
value = None
ref = writer.writeCountReference(table, conv.name, conv.staticSize, value)
table[conv.name] = value
# We ignore the current count value since it will be recomputed,
# unless it's a CountReference that was already initialized in a custom preWrite.
if isinstance(value, CountReference):
ref = value
ref.size = conv.staticSize
writer.writeData(ref)
table[conv.name] = ref.getValue()
else:
ref = writer.writeCountReference(table, conv.name, conv.staticSize)
table[conv.name] = None
if conv.isPropagated:
writer[conv.name] = ref
elif conv.isLookupType:

View File

@ -7,7 +7,7 @@ converter objects from otConverters.py.
"""
from fontTools.misc.py23 import *
from fontTools.misc.textTools import pad, safeEval
from .otBase import BaseTable, FormatSwitchingBaseTable, ValueRecord, StaticCount
from .otBase import BaseTable, FormatSwitchingBaseTable, ValueRecord, CountReference
import logging
import struct
@ -699,8 +699,11 @@ class VarRegionList(BaseTable):
# https://github.com/khaledhosny/ots/pull/192
fvarTable = font.get("fvar")
if fvarTable:
self.RegionAxisCount = StaticCount(len(fvarTable.axes))
return self.__dict__.copy()
self.RegionAxisCount = len(fvarTable.axes)
return {
**self.__dict__,
"RegionAxisCount": CountReference(self.__dict__, "RegionAxisCount")
}
class SingleSubst(FormatSwitchingBaseTable):