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 <VarIndexBase/> element with no value.
We also allow to build Var tables without needing to pass "VarIndexBase": 0xFFFFFFFF in the source dict.
This commit is contained in:
Cosimo Lupo 2021-07-22 19:25:29 +01:00
parent 11871673ff
commit 29a2ebf813
5 changed files with 44 additions and 27 deletions

View File

@ -19,6 +19,7 @@ from fontTools.ttLib.tables.otConverters import (
UShort, UShort,
IntValue, IntValue,
FloatValue, FloatValue,
OptionalValue,
) )
from fontTools.misc.roundTools import otRound from fontTools.misc.roundTools import otRound
@ -171,6 +172,10 @@ class TableBuilder:
# let's try as a 1-tuple # let's try as a 1-tuple
dest = self.build(cls, (source,)) 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( dest = self._callbackTable.get(
(BuildCallback.AFTER_BUILD,) + callbackKey, lambda d: d (BuildCallback.AFTER_BUILD,) + callbackKey, lambda d: d
)(dest) )(dest)

View File

@ -226,6 +226,18 @@ class SimpleValue(BaseConverter):
def xmlRead(self, attrs, content, font): def xmlRead(self, attrs, content, font):
return self.fromString(attrs["value"]) 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): class IntValue(SimpleValue):
@staticmethod @staticmethod
def fromString(value): def fromString(value):
@ -258,6 +270,9 @@ class Flags32(ULong):
def toString(value): def toString(value):
return "0x%08X" % value return "0x%08X" % value
class VarIndex(OptionalValue, ULong):
DEFAULT = 0xFFFFFFFF
class Short(IntValue): class Short(IntValue):
staticSize = 2 staticSize = 2
def read(self, reader, font, tableDict): def read(self, reader, font, tableDict):
@ -1732,6 +1747,7 @@ converterMapping = {
"uint32": ULong, "uint32": ULong,
"char64": Char64, "char64": Char64,
"Flags32": Flags32, "Flags32": Flags32,
"VarIndex": VarIndex,
"Version": Version, "Version": Version,
"Tag": Tag, "Tag": Tag,
"GlyphID": GlyphID, "GlyphID": GlyphID,

View File

@ -1624,7 +1624,7 @@ otData = [
('Fixed', 'yy', None, None, 'y-part of y basis vector'), ('Fixed', 'yy', None, None, 'y-part of y basis vector'),
('Fixed', 'dx', None, None, 'Translation in x direction'), ('Fixed', 'dx', None, None, 'Translation in x direction'),
('Fixed', 'dy', None, None, 'Translation in y 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', [ ('ColorStop', [
@ -1636,7 +1636,7 @@ otData = [
('F2Dot14', 'StopOffset', None, None, 'VarIndexBase + 0'), ('F2Dot14', 'StopOffset', None, None, 'VarIndexBase + 0'),
('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'),
('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved. VarIndexBase + 1'), ('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', [ ('ColorLine', [
@ -1668,7 +1668,7 @@ otData = [
('uint8', 'PaintFormat', None, None, 'Format identifier-format = 3'), ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 3'),
('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'),
('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved. VarIndexBase + 0'), ('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 # PaintLinearGradient
@ -1692,7 +1692,7 @@ otData = [
('int16', 'y1', None, None, ''), ('int16', 'y1', None, None, ''),
('int16', 'x2', None, None, ''), ('int16', 'x2', None, None, ''),
('int16', 'y2', None, None, ''), ('int16', 'y2', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintRadialGradient # PaintRadialGradient
@ -1716,7 +1716,7 @@ otData = [
('int16', 'x1', None, None, ''), ('int16', 'x1', None, None, ''),
('int16', 'y1', None, None, ''), ('int16', 'y1', None, None, ''),
('uint16', 'r1', None, None, ''), ('uint16', 'r1', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintSweepGradient # PaintSweepGradient
@ -1736,7 +1736,7 @@ otData = [
('int16', 'centerY', None, None, 'Center y coordinate.'), ('int16', 'centerY', None, None, 'Center y coordinate.'),
('Angle', 'startAngle', None, None, 'Start of the angular range of the gradient.'), ('Angle', 'startAngle', None, None, 'Start of the angular range of the gradient.'),
('Angle', 'endAngle', None, None, 'End 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 # PaintGlyph
@ -1778,7 +1778,7 @@ otData = [
('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarTranslate table) to Paint subtable.'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarTranslate table) to Paint subtable.'),
('int16', 'dx', None, None, 'Translation in x direction.'), ('int16', 'dx', None, None, 'Translation in x direction.'),
('int16', 'dy', None, None, 'Translation in y 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 # PaintScale
@ -1794,7 +1794,7 @@ otData = [
('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScale table) to Paint subtable.'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScale table) to Paint subtable.'),
('F2Dot14', 'scaleX', None, None, ''), ('F2Dot14', 'scaleX', None, None, ''),
('F2Dot14', 'scaleY', None, None, ''), ('F2Dot14', 'scaleY', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintScaleAroundCenter # PaintScaleAroundCenter
@ -1814,7 +1814,7 @@ otData = [
('F2Dot14', 'scaleY', None, None, ''), ('F2Dot14', 'scaleY', None, None, ''),
('int16', 'centerX', None, None, ''), ('int16', 'centerX', None, None, ''),
('int16', 'centerY', None, None, ''), ('int16', 'centerY', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintScaleUniform # PaintScaleUniform
@ -1828,7 +1828,7 @@ otData = [
('uint8', 'PaintFormat', None, None, 'Format identifier-format = 21'), ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 21'),
('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScaleUniform table) to Paint subtable.'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarScaleUniform table) to Paint subtable.'),
('F2Dot14', 'scale', None, None, ''), ('F2Dot14', 'scale', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintScaleUniformAroundCenter # PaintScaleUniformAroundCenter
@ -1846,7 +1846,7 @@ otData = [
('F2Dot14', 'scale', None, None, ''), ('F2Dot14', 'scale', None, None, ''),
('int16', 'centerX', None, None, ''), ('int16', 'centerX', None, None, ''),
('int16', 'centerY', None, None, ''), ('int16', 'centerY', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintRotate # PaintRotate
@ -1860,7 +1860,7 @@ otData = [
('uint8', 'PaintFormat', None, None, 'Format identifier-format = 25'), ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 25'),
('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarRotate table) to Paint subtable.'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarRotate table) to Paint subtable.'),
('Angle', 'angle', None, None, ''), ('Angle', 'angle', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintRotateAroundCenter # PaintRotateAroundCenter
@ -1878,7 +1878,7 @@ otData = [
('Angle', 'angle', None, None, ''), ('Angle', 'angle', None, None, ''),
('int16', 'centerX', None, None, ''), ('int16', 'centerX', None, None, ''),
('int16', 'centerY', None, None, ''), ('int16', 'centerY', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintSkew # PaintSkew
@ -1894,7 +1894,7 @@ otData = [
('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarSkew table) to Paint subtable.'), ('Offset24', 'Paint', None, None, 'Offset (from beginning of PaintVarSkew table) to Paint subtable.'),
('Angle', 'xSkewAngle', None, None, ''), ('Angle', 'xSkewAngle', None, None, ''),
('Angle', 'ySkewAngle', None, None, ''), ('Angle', 'ySkewAngle', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintSkewAroundCenter # PaintSkewAroundCenter
@ -1914,7 +1914,7 @@ otData = [
('Angle', 'ySkewAngle', None, None, ''), ('Angle', 'ySkewAngle', None, None, ''),
('int16', 'centerX', None, None, ''), ('int16', 'centerX', None, None, ''),
('int16', 'centerY', None, None, ''), ('int16', 'centerY', None, None, ''),
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('VarIndex', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
# PaintComposite # PaintComposite

View File

@ -335,9 +335,7 @@ def test_buildVarColorLine_StopMap():
def checkBuildAffine2x3(cls, variable=False): def checkBuildAffine2x3(cls, variable=False):
matrix = _build( matrix = _build(cls, (1.5, 0, 0.5, 2.0, 1.0, -3.0))
cls, (1.5, 0, 0.5, 2.0, 1.0, -3.0) + ((0xFFFFFFFF,) if variable else ())
)
assert matrix.xx == 1.5 assert matrix.xx == 1.5
assert matrix.yx == 0.0 assert matrix.yx == 0.0
assert matrix.xy == 0.5 assert matrix.xy == 0.5
@ -955,8 +953,6 @@ def checkBuildPaintRotate(fmt):
if around_center: if around_center:
source["centerX"] = 127 source["centerX"] = 127
source["centerY"] = 129 source["centerY"] = 129
if variable:
source["VarIndexBase"] = 0xFFFFFFFF
paint = _build(ot.Paint, source) paint = _build(ot.Paint, source)

View File

@ -248,11 +248,11 @@ COLR_V1_SAMPLE = (
(b"\x00\x00", "ColorLine.ColorStop[0].StopOffset.value (0.0)"), (b"\x00\x00", "ColorLine.ColorStop[0].StopOffset.value (0.0)"),
(b"\x00\x06", "ColorLine.ColorStop[0].PaletteIndex (6)"), (b"\x00\x06", "ColorLine.ColorStop[0].PaletteIndex (6)"),
(b"@\x00", "ColorLine.ColorStop[0].Alpha.value (1.0)"), (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", "ColorLine.ColorStop[1].StopOffset.value (1.0)"),
(b"\x00\x07", "ColorLine.ColorStop[1].PaletteIndex (7)"), (b"\x00\x07", "ColorLine.ColorStop[1].PaletteIndex (7)"),
(b"\x19\x9a", "ColorLine.ColorStop[1].Alpha.value (0.4)"), (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"\xff\xf3\x00\x00", "Affine2x3.xx (-13)"),
(b"\x00\x0e\x00\x00", "Affine2x3.xy (14)"), (b"\x00\x0e\x00\x00", "Affine2x3.xy (14)"),
@ -351,7 +351,7 @@ COLR_V1_XML = [
' <yy value="1.0"/>', ' <yy value="1.0"/>',
' <dx value="300.0"/>', ' <dx value="300.0"/>',
' <dy value="0.0"/>', ' <dy value="0.0"/>',
' <VarIndexBase value="4294967295"/>', ' <VarIndexBase/>',
" </Transform>", " </Transform>",
" </BackdropPaint>", " </BackdropPaint>",
" </Paint>", " </Paint>",
@ -389,7 +389,7 @@ COLR_V1_XML = [
' <Paint Format="3"><!-- PaintVarSolid -->', ' <Paint Format="3"><!-- PaintVarSolid -->',
' <PaletteIndex value="2"/>', ' <PaletteIndex value="2"/>',
' <Alpha value="0.5"/>', ' <Alpha value="0.5"/>',
' <VarIndexBase value="4294967295"/>', ' <VarIndexBase/>',
" </Paint>", " </Paint>",
' <Glyph value="glyph00011"/>', ' <Glyph value="glyph00011"/>',
" </Paint>", " </Paint>",
@ -433,13 +433,13 @@ COLR_V1_XML = [
' <StopOffset value="0.0"/>', ' <StopOffset value="0.0"/>',
' <PaletteIndex value="6"/>', ' <PaletteIndex value="6"/>',
' <Alpha value="1.0"/>', ' <Alpha value="1.0"/>',
' <VarIndexBase value="4294967295"/>', ' <VarIndexBase value="0"/>',
" </ColorStop>", " </ColorStop>",
' <ColorStop index="1">', ' <ColorStop index="1">',
' <StopOffset value="1.0"/>', ' <StopOffset value="1.0"/>',
' <PaletteIndex value="7"/>', ' <PaletteIndex value="7"/>',
' <Alpha value="0.4"/>', ' <Alpha value="0.4"/>',
' <VarIndexBase value="4294967295"/>', ' <VarIndexBase value="1"/>',
" </ColorStop>", " </ColorStop>",
" </ColorLine>", " </ColorLine>",
' <x0 value="7"/>', ' <x0 value="7"/>',
@ -448,7 +448,7 @@ COLR_V1_XML = [
' <x1 value="10"/>', ' <x1 value="10"/>',
' <y1 value="11"/>', ' <y1 value="11"/>',
' <r1 value="12"/>', ' <r1 value="12"/>',
' <VarIndexBase value="4294967295"/>', ' <VarIndexBase/>',
" </Paint>", " </Paint>",
" <Transform>", " <Transform>",
' <xx value="-13.0"/>', ' <xx value="-13.0"/>',