From 0eb27e987880f53388e1ce945ffc799b9dc52f1e Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Tue, 20 Jul 2021 19:51:47 +0100 Subject: [PATCH] Remove and inline {Var,}ColorIndex --- Lib/fontTools/colorLib/builder.py | 29 +++++++++++++------- Lib/fontTools/colorLib/table_builder.py | 35 ++++++++++++++----------- Lib/fontTools/ttLib/tables/otData.py | 25 +++++++----------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/Lib/fontTools/colorLib/builder.py b/Lib/fontTools/colorLib/builder.py index e12e741ea..392fb106b 100644 --- a/Lib/fontTools/colorLib/builder.py +++ b/Lib/fontTools/colorLib/builder.py @@ -89,16 +89,16 @@ def _beforeBuildPaintRadialGradient(paint, source): return _beforeBuildPaintVarRadialGradient(paint, source, lambda v: v.value) -def _defaultColorIndex(): - colorIndex = ot.ColorIndex() - colorIndex.Alpha = _DEFAULT_ALPHA.value - return colorIndex +def _defaultColorStop(): + colorStop = ot.ColorStop() + colorStop.Alpha = _DEFAULT_ALPHA.value + return colorStop -def _defaultVarColorIndex(): - colorIndex = ot.VarColorIndex() - colorIndex.Alpha = _DEFAULT_ALPHA - return colorIndex +def _defaultVarColorStop(): + colorStop = ot.VarColorStop() + colorStop.Alpha = _DEFAULT_ALPHA.value + return colorStop def _defaultColorLine(): @@ -113,6 +113,12 @@ def _defaultVarColorLine(): return colorLine +def _defaultPaintSolid(): + paint = ot.Paint() + paint.Alpha = _DEFAULT_ALPHA.value + return paint + + def _buildPaintCallbacks(): return { ( @@ -125,10 +131,13 @@ def _buildPaintCallbacks(): ot.Paint, ot.PaintFormat.PaintVarRadialGradient, ): _beforeBuildPaintVarRadialGradient, - (BuildCallback.CREATE_DEFAULT, ot.ColorIndex): _defaultColorIndex, - (BuildCallback.CREATE_DEFAULT, ot.VarColorIndex): _defaultVarColorIndex, + (BuildCallback.CREATE_DEFAULT, ot.ColorStop): _defaultColorStop, + (BuildCallback.CREATE_DEFAULT, ot.VarColorStop): _defaultVarColorStop, (BuildCallback.CREATE_DEFAULT, ot.ColorLine): _defaultColorLine, (BuildCallback.CREATE_DEFAULT, ot.VarColorLine): _defaultVarColorLine, + (BuildCallback.CREATE_DEFAULT, ot.VarColorLine): _defaultVarColorLine, + (BuildCallback.CREATE_DEFAULT, ot.Paint, ot.PaintFormat.PaintSolid): _defaultPaintSolid, + (BuildCallback.CREATE_DEFAULT, ot.Paint, ot.PaintFormat.PaintVarSolid): _defaultPaintSolid, } diff --git a/Lib/fontTools/colorLib/table_builder.py b/Lib/fontTools/colorLib/table_builder.py index 6fba6b0fc..a9c994c39 100644 --- a/Lib/fontTools/colorLib/table_builder.py +++ b/Lib/fontTools/colorLib/table_builder.py @@ -39,7 +39,7 @@ class BuildCallback(enum.Enum): """ AFTER_BUILD = enum.auto() - """Keyed on (CREATE_DEFAULT, class). + """Keyed on (CREATE_DEFAULT, class[, Format if available]). Receives no arguments. Should return a new instance of class. """ @@ -62,25 +62,25 @@ def _isNonStrSequence(value): return isinstance(value, collections.abc.Sequence) and not isinstance(value, str) -def _set_format(dest, source): +def _split_format(cls, source): if _isNonStrSequence(source): - assert len(source) > 0, f"{type(dest)} needs at least format from {source}" - dest.Format = source[0] - source = source[1:] + assert len(source) > 0, f"{cls} needs at least format from {source}" + fmt, remainder = source[0], source[1:] elif isinstance(source, collections.abc.Mapping): - assert "Format" in source, f"{type(dest)} needs at least Format from {source}" - dest.Format = source["Format"] + assert "Format" in source, f"{cls} needs at least Format from {source}" + remainder = source.copy() + fmt = remainder.pop("Format") else: - raise ValueError(f"Not sure how to populate {type(dest)} from {source}") + raise ValueError(f"Not sure how to populate {cls} from {source}") assert isinstance( - dest.Format, collections.abc.Hashable - ), f"{type(dest)} Format is not hashable: {dest.Format}" + fmt, collections.abc.Hashable + ), f"{cls} Format is not hashable: {fmt!r}" assert ( - dest.Format in dest.convertersByName - ), f"{dest.Format} invalid Format of {cls}" + fmt in cls.convertersByName + ), f"{cls} invalid Format: {fmt!r}" - return source + return fmt, remainder class TableBuilder: @@ -140,6 +140,11 @@ class TableBuilder: return source callbackKey = (cls,) + fmt = None + if issubclass(cls, FormatSwitchingBaseTable): + fmt, source = _split_format(cls, source) + callbackKey = (cls, fmt) + dest = self._callbackTable.get( (BuildCallback.CREATE_DEFAULT,) + callbackKey, lambda: cls() )() @@ -150,11 +155,9 @@ class TableBuilder: # For format switchers we need to resolve converters based on format if issubclass(cls, FormatSwitchingBaseTable): - source = _set_format(dest, source) - + dest.Format = fmt convByName = _assignable(convByName[dest.Format]) skippedFields.add("Format") - callbackKey = (cls, dest.Format) # Convert sequence => mapping so before thunk only has to handle one format if _isNonStrSequence(source): diff --git a/Lib/fontTools/ttLib/tables/otData.py b/Lib/fontTools/ttLib/tables/otData.py index 31f2d0630..056b68117 100755 --- a/Lib/fontTools/ttLib/tables/otData.py +++ b/Lib/fontTools/ttLib/tables/otData.py @@ -1627,23 +1627,15 @@ otData = [ ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ]), - ('ColorIndex', [ - ('uint16', 'PaletteIndex', None, None, 'Index value to use with a selected color palette.'), - ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved'), - ]), - ('VarColorIndex', [ - ('uint16', 'PaletteIndex', None, None, 'Index value to use with a selected color palette.'), - ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved'), - ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), - ]), - ('ColorStop', [ ('F2Dot14', 'StopOffset', None, None, ''), - ('ColorIndex', 'Color', None, None, ''), + ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), + ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved'), ]), ('VarColorStop', [ - ('F2Dot14', 'StopOffset', None, None, ''), - ('VarColorIndex', 'Color', None, None, ''), + ('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.'), ]), @@ -1668,12 +1660,15 @@ otData = [ # PaintSolid ('PaintFormat2', [ ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 2'), - ('ColorIndex', 'Color', None, None, 'A solid color paint.'), + ('uint16', 'PaletteIndex', None, None, 'Index for a CPAL palette entry.'), + ('F2Dot14', 'Alpha', None, None, 'Values outsided [0.,1.] reserved'), ]), # PaintVarSolid ('PaintFormat3', [ ('uint8', 'PaintFormat', None, None, 'Format identifier-format = 3'), - ('VarColorIndex', 'Color', None, None, 'A solid color paint.'), + ('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.'), ]), # PaintLinearGradient