otConverters: in _NamedTupleConverter only dump to XML non-default values

so we don't see varIdx='0' dumped all the time, even when there are no variations in font
This commit is contained in:
Cosimo Lupo 2020-03-09 16:15:33 +00:00
parent 82c2b3632f
commit b2a0d62295
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642

View File

@ -15,6 +15,7 @@ from .otTables import (lookupTypes, AATStateTable, AATState, AATAction,
ContextualMorphAction, LigatureMorphAction, ContextualMorphAction, LigatureMorphAction,
InsertionMorphAction, MorxSubtable, VariableFloat, InsertionMorphAction, MorxSubtable, VariableFloat,
VariableInt, ExtendMode as _ExtendMode) VariableInt, ExtendMode as _ExtendMode)
from itertools import zip_longest
from functools import partial from functools import partial
import struct import struct
import logging import logging
@ -1616,6 +1617,7 @@ class _NamedTupleConverter(BaseConverter):
klass(name=name, repeat=None, aux=None) klass(name=name, repeat=None, aux=None)
for name, klass in zip(self.tupleClass._fields, self.converterClasses) for name, klass in zip(self.tupleClass._fields, self.converterClasses)
] ]
self.convertersByName = {conv.name: conv for conv in self.converters}
# returned by getRecordSize method # returned by getRecordSize method
self.staticSize = sum(c.staticSize for c in self.converters) self.staticSize = sum(c.staticSize for c in self.converters)
@ -1634,18 +1636,33 @@ class _NamedTupleConverter(BaseConverter):
def xmlWrite(self, xmlWriter, font, value, name, attrs): def xmlWrite(self, xmlWriter, font, value, name, attrs):
assert value is not None assert value is not None
defaults = value.__new__.__defaults__ or ()
assert len(self.converters) >= len(defaults)
values = {}
required = object()
for conv, default in zip_longest(
reversed(self.converters),
reversed(defaults),
fillvalue=required,
):
v = getattr(value, conv.name)
if default is required or v != default:
values[conv.name] = conv.toString(v)
if attrs is None: if attrs is None:
attrs = [] attrs = []
for conv in self.converters: attrs.extend(
v = getattr(value, conv.name) (conv.name, values[conv.name])
attrs.append((conv.name, conv.toString(v))) for conv in self.converters
if conv.name in values
)
xmlWriter.simpletag(name, attrs) xmlWriter.simpletag(name, attrs)
xmlWriter.newline() xmlWriter.newline()
def xmlRead(self, attrs, content, font): def xmlRead(self, attrs, content, font):
converters = self.convertersByName
kwargs = { kwargs = {
conv.name: conv.fromString(attrs[conv.name]) k: converters[k].fromString(v)
for conv in self.converters for k, v in attrs.items()
} }
return self.tupleClass(**kwargs) return self.tupleClass(**kwargs)