[VarComponent] Nicer XML output

https://github.com/fonttools/fonttools/pull/3395#issuecomment-1895944021
This commit is contained in:
Behdad Esfahbod 2024-01-17 17:27:06 -07:00
parent e3ba7a7e0b
commit 5ad4045cec
2 changed files with 106 additions and 40 deletions

View File

@ -151,6 +151,9 @@ def _write_uint32var(v):
class VarComponent: class VarComponent:
def __init__(self): def __init__(self):
self.populateDefaults()
def populateDefaults(self, propagator=None):
self.flags = 0 self.flags = 0
self.glyphName = None self.glyphName = None
self.axisIndicesIndex = None self.axisIndicesIndex = None
@ -266,55 +269,63 @@ class VarComponent:
return _write_uint32var(flags) + bytesjoin(data) return _write_uint32var(flags) + bytesjoin(data)
def toXML(self, writer, ttFont, attrs): def toXML(self, writer, ttFont, attrs):
attrs.append(("glyphName", self.glyphName)) writer.begintag("VarComponent", attrs)
attrs.append(("flags", self.flags)) writer.newline()
def write(name, value):
if value is not None:
writer.simpletag(name, value=value)
writer.newline()
write("glyphName", self.glyphName)
write("flags", hex(self.flags))
if self.axisIndicesIndex is not None: if self.axisIndicesIndex is not None:
attrs.append(("axisIndicesIndex", self.axisIndicesIndex)) write("axisIndicesIndex", self.axisIndicesIndex)
attrs.append(("axisValues", self.axisValues)) write("axisValues", self.axisValues)
if self.axisValuesVarIndex != NO_VARIATION_INDEX: if self.axisValuesVarIndex != NO_VARIATION_INDEX:
attrs.append(("axisValuesVarIndex", self.axisValuesVarIndex)) write("axisValuesVarIndex", self.axisValuesVarIndex)
if self.transformVarIndex != NO_VARIATION_INDEX: if self.transformVarIndex != NO_VARIATION_INDEX:
attrs.append(("transformVarIndex", self.transformVarIndex)) write("transformVarIndex", self.transformVarIndex)
for attr_name, mapping in VAR_TRANSFORM_MAPPING.items(): for attr_name, mapping in VAR_TRANSFORM_MAPPING.items():
v = getattr(self.transform, attr_name) v = getattr(self.transform, attr_name)
if v != mapping.defaultValue: if v != mapping.defaultValue:
attrs.append((attr_name, fl2str(v, mapping.fractionalBits))) write(attr_name, fl2str(v, mapping.fractionalBits))
writer.simpletag("VarComponent", attrs) writer.endtag("VarComponent")
writer.newline() writer.newline()
def fromXML(self, name, attrs, content, ttFont): def fromXML(self, name, attrs, content, ttFont):
self.flags = safeEval(attrs["flags"]) content = [c for c in content if isinstance(c, tuple)]
self.glyphName = attrs["glyphName"]
assert ("axisIndicesIndex" in attrs) == ("axisValues" in attrs) self.populateDefaults()
if "axisIndicesIndex" in attrs:
self.axisIndicesIndex = safeEval(attrs["axisIndicesIndex"])
self.axisValues = safeEval(attrs["axisValues"])
else:
self.axisIndicesIndex = None
self.axisValues = ()
if "axisValuesVarIndex" in attrs: for name, attrs, content in content:
self.axisValuesVarIndex = safeEval(attrs["axisValuesVarIndex"]) assert not content
else: v = attrs["value"]
self.axisValuesVarIndex = NO_VARIATION_INDEX
if "transformVarIndex" in attrs:
self.transformVarIndex = safeEval(attrs["transformVarIndex"])
else:
self.transformVarIndex = NO_VARIATION_INDEX
self.transform = DecomposedTransform() if name == "glyphName":
for attr_name, mapping in VAR_TRANSFORM_MAPPING.items(): self.glyphName = v
if attr_name in attrs: elif name == "flags":
self.flags = safeEval(v)
elif name == "axisIndicesIndex":
self.axisIndicesIndex = safeEval(v)
elif name == "axisValues":
self.axisValues = safeEval(v)
elif name == "axisValuesVarIndex":
self.axisValuesVarIndex = safeEval(v)
elif name == "transformVarIndex":
self.transformVarIndex = safeEval(v)
elif name in VAR_TRANSFORM_MAPPING:
setattr( setattr(
self.transform, self.transform,
attr_name, name,
safeEval(attrs[attr_name]), safeEval(v),
) )
else:
assert False, name
def applyTransformDeltas(self, deltas): def applyTransformDeltas(self, deltas):
i = 0 i = 0

View File

@ -304,28 +304,83 @@
</AxisIndicesList> </AxisIndicesList>
<VarCompositeGlyphs> <VarCompositeGlyphs>
<VarCompositeGlyph index="0"> <VarCompositeGlyph index="0">
<VarComponent index="0" glyphName="glyph00007" flags="0"/> <VarComponent index="0">
<VarComponent index="1" glyphName="glyph00003" flags="0"/> <glyphName value="glyph00007"/>
<flags value="0x0"/>
</VarComponent>
<VarComponent index="1">
<glyphName value="glyph00003"/>
<flags value="0x0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="1"> <VarCompositeGlyph index="1">
<VarComponent index="0" glyphName="glyph00005" flags="0"/> <VarComponent index="0">
<VarComponent index="1" glyphName="glyph00004" flags="0"/> <glyphName value="glyph00005"/>
<VarComponent index="2" glyphName="glyph00006" flags="0"/> <flags value="0x0"/>
</VarComponent>
<VarComponent index="1">
<glyphName value="glyph00004"/>
<flags value="0x0"/>
</VarComponent>
<VarComponent index="2">
<glyphName value="glyph00006"/>
<flags value="0x0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="2"> <VarCompositeGlyph index="2">
<VarComponent index="0" glyphName="glyph00008" flags="18" axisIndicesIndex="2" axisValues="(3537,)" translateX="-6.0"/> <VarComponent index="0">
<glyphName value="glyph00008"/>
<flags value="0x12"/>
<axisIndicesIndex value="2"/>
<axisValues value="(3537,)"/>
<translateX value="-6.0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="3"> <VarCompositeGlyph index="3">
<VarComponent index="0" glyphName="glyph00008" flags="22" axisIndicesIndex="0" axisValues="(-13184, 0, 7928)" axisValuesVarIndex="0" translateX="-3.0"/> <VarComponent index="0">
<glyphName value="glyph00008"/>
<flags value="0x16"/>
<axisIndicesIndex value="0"/>
<axisValues value="(-13184, 0, 7928)"/>
<axisValuesVarIndex value="0"/>
<translateX value="-3.0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="4"> <VarCompositeGlyph index="4">
<VarComponent index="0" glyphName="glyph00009" flags="62" axisIndicesIndex="1" axisValues="(-8847, 655, -8484, 0, 0)" axisValuesVarIndex="1" transformVarIndex="2" translateX="-5.0" translateY="24.0"/> <VarComponent index="0">
<glyphName value="glyph00009"/>
<flags value="0x3e"/>
<axisIndicesIndex value="1"/>
<axisValues value="(-8847, 655, -8484, 0, 0)"/>
<axisValuesVarIndex value="1"/>
<transformVarIndex value="2"/>
<translateX value="-5.0"/>
<translateY value="24.0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="5"> <VarCompositeGlyph index="5">
<VarComponent index="0" glyphName="glyph00010" flags="62" axisIndicesIndex="0" axisValues="(-12714, -5843, 0)" axisValuesVarIndex="3" transformVarIndex="4" translateX="53.0" translateY="-231.0"/> <VarComponent index="0">
<glyphName value="glyph00010"/>
<flags value="0x3e"/>
<axisIndicesIndex value="0"/>
<axisValues value="(-12714, -5843, 0)"/>
<axisValuesVarIndex value="3"/>
<transformVarIndex value="4"/>
<translateX value="53.0"/>
<translateY value="-231.0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
<VarCompositeGlyph index="6"> <VarCompositeGlyph index="6">
<VarComponent index="0" glyphName="glyph00009" flags="62" axisIndicesIndex="0" axisValues="(-4227, 819, -8484)" axisValuesVarIndex="5" transformVarIndex="6" translateX="-5.0" translateY="12.0"/> <VarComponent index="0">
<glyphName value="glyph00009"/>
<flags value="0x3e"/>
<axisIndicesIndex value="0"/>
<axisValues value="(-4227, 819, -8484)"/>
<axisValuesVarIndex value="5"/>
<transformVarIndex value="6"/>
<translateX value="-5.0"/>
<translateY value="12.0"/>
</VarComponent>
</VarCompositeGlyph> </VarCompositeGlyph>
</VarCompositeGlyphs> </VarCompositeGlyphs>
</VARC> </VARC>