From 29a2ebf813665fb9a2bbd773a925675505e9ee42 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 22 Jul 2021 19:25:29 +0100 Subject: [PATCH] omit default VarIndexBase 4294967295 (0xFFFFFFFF in decimal form) is not very memorable. Still, using hex notation for all VarIndexBases would make the non-default values less readable (when interpreted as an index into the DeltaSetIndexMap array, decimal makes more sense). Since 0xFFFFFFFF means 'no variation data', it makes sense to omit it from the ttx dump and write an empty element with no value. We also allow to build Var tables without needing to pass "VarIndexBase": 0xFFFFFFFF in the source dict. --- Lib/fontTools/colorLib/table_builder.py | 5 ++++ Lib/fontTools/ttLib/tables/otConverters.py | 16 ++++++++++++ Lib/fontTools/ttLib/tables/otData.py | 30 +++++++++++----------- Tests/colorLib/builder_test.py | 6 +---- Tests/ttLib/tables/C_O_L_R_test.py | 14 +++++----- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/Lib/fontTools/colorLib/table_builder.py b/Lib/fontTools/colorLib/table_builder.py index 98a27e479..763115b96 100644 --- a/Lib/fontTools/colorLib/table_builder.py +++ b/Lib/fontTools/colorLib/table_builder.py @@ -19,6 +19,7 @@ from fontTools.ttLib.tables.otConverters import ( UShort, IntValue, FloatValue, + OptionalValue, ) from fontTools.misc.roundTools import otRound @@ -171,6 +172,10 @@ class TableBuilder: # let's try as a 1-tuple dest = self.build(cls, (source,)) + for field, conv in convByName.items(): + if not hasattr(dest, field) and isinstance(conv, OptionalValue): + setattr(dest, field, conv.DEFAULT) + dest = self._callbackTable.get( (BuildCallback.AFTER_BUILD,) + callbackKey, lambda d: d )(dest) diff --git a/Lib/fontTools/ttLib/tables/otConverters.py b/Lib/fontTools/ttLib/tables/otConverters.py index ecc1057a4..058813a81 100644 --- a/Lib/fontTools/ttLib/tables/otConverters.py +++ b/Lib/fontTools/ttLib/tables/otConverters.py @@ -226,6 +226,18 @@ class SimpleValue(BaseConverter): def xmlRead(self, attrs, content, font): return self.fromString(attrs["value"]) +class OptionalValue(SimpleValue): + DEFAULT = None + def xmlWrite(self, xmlWriter, font, value, name, attrs): + if value != self.DEFAULT: + attrs.append(("value", self.toString(value))) + xmlWriter.simpletag(name, attrs) + xmlWriter.newline() + def xmlRead(self, attrs, content, font): + if "value" in attrs: + return self.fromString(attrs["value"]) + return self.DEFAULT + class IntValue(SimpleValue): @staticmethod def fromString(value): @@ -258,6 +270,9 @@ class Flags32(ULong): def toString(value): return "0x%08X" % value +class VarIndex(OptionalValue, ULong): + DEFAULT = 0xFFFFFFFF + class Short(IntValue): staticSize = 2 def read(self, reader, font, tableDict): @@ -1732,6 +1747,7 @@ converterMapping = { "uint32": ULong, "char64": Char64, "Flags32": Flags32, + "VarIndex": VarIndex, "Version": Version, "Tag": Tag, "GlyphID": GlyphID, diff --git a/Lib/fontTools/ttLib/tables/otData.py b/Lib/fontTools/ttLib/tables/otData.py index 056b68117..e33370829 100755 --- a/Lib/fontTools/ttLib/tables/otData.py +++ b/Lib/fontTools/ttLib/tables/otData.py @@ -1624,7 +1624,7 @@ otData = [ ('Fixed', 'yy', None, None, 'y-part of y basis vector'), ('Fixed', 'dx', None, None, 'Translation in x direction'), ('Fixed', 'dy', None, None, 'Translation in y direction'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), ('ColorStop', [ @@ -1636,7 +1636,7 @@ otData = [ ('F2Dot14', 'StopOffset', None, None, 'VarIndexBase + 0'), ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved. VarIndexBase + 1'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), ('ColorLine', [ @@ -1668,7 +1668,7 @@ otData = [ ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 3'), ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved. VarIndexBase + 0'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintLinearGradient @@ -1692,7 +1692,7 @@ otData = [ ('int16', 'y1', None, None, ''), ('int16', 'x2', None, None, ''), ('int16', 'y2', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintRadialGradient @@ -1716,7 +1716,7 @@ otData = [ ('int16', 'x1', None, None, ''), ('int16', 'y1', None, None, ''), ('uint16', 'r1', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintSweepGradient @@ -1736,7 +1736,7 @@ otData = [ ('int16', 'centerY', None, None, 'Center y coordinate.'), ('Angle', 'startAngle', None, None, 'Start of the angular range of the gradient.'), ('Angle', 'endAngle', None, None, 'End of the angular range of the gradient.'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintGlyph @@ -1778,7 +1778,7 @@ otData = [ ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarTranslate table) to Paint subtable.'), ('int16', 'dx', None, None, 'Translation in x direction.'), ('int16', 'dy', None, None, 'Translation in y direction.'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintScale @@ -1794,7 +1794,7 @@ otData = [ ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScale table) to Paint subtable.'), ('F2Dot14', 'scaleX', None, None, ''), ('F2Dot14', 'scaleY', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintScaleAroundCenter @@ -1814,7 +1814,7 @@ otData = [ ('F2Dot14', 'scaleY', None, None, ''), ('int16', 'centerX', None, None, ''), ('int16', 'centerY', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintScaleUniform @@ -1828,7 +1828,7 @@ otData = [ ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 21'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScaleUniform table) to Paint subtable.'), ('F2Dot14', 'scale', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintScaleUniformAroundCenter @@ -1846,7 +1846,7 @@ otData = [ ('F2Dot14', 'scale', None, None, ''), ('int16', 'centerX', None, None, ''), ('int16', 'centerY', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintRotate @@ -1860,7 +1860,7 @@ otData = [ ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 25'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarRotate table) to Paint subtable.'), ('Angle', 'angle', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintRotateAroundCenter @@ -1878,7 +1878,7 @@ otData = [ ('Angle', 'angle', None, None, ''), ('int16', 'centerX', None, None, ''), ('int16', 'centerY', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintSkew @@ -1894,7 +1894,7 @@ otData = [ ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarSkew table) to Paint subtable.'), ('Angle', 'xSkewAngle', None, None, ''), ('Angle', 'ySkewAngle', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintSkewAroundCenter @@ -1914,7 +1914,7 @@ otData = [ ('Angle', 'ySkewAngle', None, None, ''), ('int16', 'centerX', None, None, ''), ('int16', 'centerY', None, None, ''), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), + ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), # PaintComposite diff --git a/Tests/colorLib/builder_test.py b/Tests/colorLib/builder_test.py index 8d30ca5ee..987841aec 100644 --- a/Tests/colorLib/builder_test.py +++ b/Tests/colorLib/builder_test.py @@ -335,9 +335,7 @@ def test_buildVarColorLine_StopMap(): def checkBuildAffine2x3(cls, variable=False): - matrix = _build( - cls, (1.5, 0, 0.5, 2.0, 1.0, -3.0) + ((0xFFFFFFFF,) if variable else ()) - ) + matrix = _build(cls, (1.5, 0, 0.5, 2.0, 1.0, -3.0)) assert matrix.xx == 1.5 assert matrix.yx == 0.0 assert matrix.xy == 0.5 @@ -955,8 +953,6 @@ def checkBuildPaintRotate(fmt): if around_center: source["centerX"] = 127 source["centerY"] = 129 - if variable: - source["VarIndexBase"] = 0xFFFFFFFF paint = _build(ot.Paint, source) diff --git a/Tests/ttLib/tables/C_O_L_R_test.py b/Tests/ttLib/tables/C_O_L_R_test.py index 35075ced5..73ba74d75 100644 --- a/Tests/ttLib/tables/C_O_L_R_test.py +++ b/Tests/ttLib/tables/C_O_L_R_test.py @@ -248,11 +248,11 @@ COLR_V1_SAMPLE = ( (b"\x00\x00", "ColorLine.ColorStop[0].StopOffset.value (0.0)"), (b"\x00\x06", "ColorLine.ColorStop[0].PaletteIndex (6)"), (b"@\x00", "ColorLine.ColorStop[0].Alpha.value (1.0)"), - (b"\xff\xff\xff\xff", "VarIndexBase (0xFFFFFFFF)"), + (b"\x00\x00\x00\x00", "VarIndexBase (0)"), (b"@\x00", "ColorLine.ColorStop[1].StopOffset.value (1.0)"), (b"\x00\x07", "ColorLine.ColorStop[1].PaletteIndex (7)"), (b"\x19\x9a", "ColorLine.ColorStop[1].Alpha.value (0.4)"), - (b"\xff\xff\xff\xff", "VarIndexBase (0xFFFFFFFF)"), + (b"\x00\x00\x00\x01", "VarIndexBase (1)"), (b"\xff\xf3\x00\x00", "Affine2x3.xx (-13)"), (b"\x00\x0e\x00\x00", "Affine2x3.xy (14)"), @@ -351,7 +351,7 @@ COLR_V1_XML = [ ' ', ' ', ' ', - ' ', + ' ', " ", " ", " ", @@ -389,7 +389,7 @@ COLR_V1_XML = [ ' ', ' ', ' ', - ' ', + ' ', " ", ' ', " ", @@ -433,13 +433,13 @@ COLR_V1_XML = [ ' ', ' ', ' ', - ' ', + ' ', " ", ' ', ' ', ' ', ' ', - ' ', + ' ', " ", " ", ' ', @@ -448,7 +448,7 @@ COLR_V1_XML = [ ' ', ' ', ' ', - ' ', + ' ', " ", " ", ' ',