Skip to content

Commit

Permalink
[VARC] Implement XML read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad committed Dec 20, 2023
1 parent 33da6e9 commit 2e057ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
10 changes: 3 additions & 7 deletions Lib/fontTools/ttLib/tables/otConverters.py
Expand Up @@ -1948,13 +1948,9 @@ def write(self, writer, font, tableDict, values, repeatIndex=None):

def xmlRead(self, attrs, content, font):
if self._itemClass is not None:
lst = []
content = [t for t in content if isinstance(t, tuple)]
for eltName, eltAttrs, eltContent in content:
obj = self._itemClass()
obj.fromXML(eltName, eltAttrs, eltContent, font)
lst.append(obj)
return lst
obj = self._itemClass()
obj.fromXML(None, attrs, content, font)
return obj
elif self._converter is not None:
return self._converter.xmlRead(attrs, content, font)
else:
Expand Down
50 changes: 39 additions & 11 deletions Lib/fontTools/ttLib/tables/otTables.py
Expand Up @@ -253,23 +253,50 @@ def toXML(self, writer, ttFont, attrs):

if self.axisIndicesIndex is not None:
attrs.append(("axisIndicesIndex", self.axisIndicesIndex))
attrs.append(("axisValues", self.axisValues))

# XXX TODO
if self.axisValuesVarIndex != NO_VARIATION_INDEX:
attrs.append(("axisValuesVarIndex", self.axisValuesVarIndex))
if self.transformVarIndex != NO_VARIATION_INDEX:
attrs.append(("transformVarIndex", self.transformVarIndex))

for attr_name, mapping in VAR_TRANSFORM_MAPPING.items():
v = getattr(self.transform, attr_name)
if v != mapping.defaultValue:
attrs.append((attr_name, fl2str(v, mapping.fractionalBits)))

writer.simpletag("VarComponent", attrs)
writer.newline()

def fromXML(self, name, attrs, content, ttFont):
self.flags = safeEval(attrs["flags"]) & VarComponentFlags.RETAIN_FLAGS
self.flags = safeEval(attrs["flags"])
self.glyphName = attrs["glyphName"]

# XXX TODO
assert ("AxisIndicesIndex" in attrs) == ("AxisValuesIndex" in attrs)
if "AxisIndicesIndex" in attrs:
self.AxisIndicesIndex = safeEval(attrs["AxisIndicesIndex"])
self.AxisValuesIndex = safeEval(attrs["AxisValuesIndex"])
if "TransformIndex" in attrs:
self.TransformIndex = safeEval(attrs["TransformIndex"])
assert ("axisIndicesIndex" in attrs) == ("axisValues" in attrs)
if "axisIndicesIndex" in attrs:
self.axisIndicesIndex = safeEval(attrs["axisIndicesIndex"])
self.axisValues = safeEval(attrs["axisValues"])
else:
self.axisIndicesIndex = None
self.axisValues = ()

if "axisValuesVarIndex" in attrs:
self.axisValuesVarIndex = safeEval(attrs["axisValuesVarIndex"])
else:
self.axisValuesVarIndex = NO_VARIATION_INDEX
if "transformVarIndex" in attrs:
self.transformVarIndex = safeEval(attrs["transformVarIndex"])
else:
self.transformVarIndex = NO_VARIATION_INDEX

self.transform = DecomposedTransform()
for attr_name, mapping in VAR_TRANSFORM_MAPPING.items():
if attr_name in attrs:
setattr(
self.transform,
attr_name,
safeEval(attrs[attr_name]),
)

def applyTransformDeltas(self, deltas):
i = 0
Expand Down Expand Up @@ -305,7 +332,6 @@ def __ne__(self, other):


class VarCompositeGlyph:

def __init__(self, components=None):
self.components = components if components is not None else []

Expand All @@ -331,7 +357,9 @@ def toXML(self, xmlWriter, font, attrs, name):
xmlWriter.newline()

def fromXML(self, name, attrs, content, font):
if name == "VarComponent":
content = [c for c in content if isinstance(c, tuple)]
for name, attrs, content in content:
assert name == "VarComponent"
component = VarComponent()
component.fromXML(name, attrs, content, font)
self.components.append(component)
Expand Down

0 comments on commit 2e057ba

Please sign in to comment.