Remove and inline {Var,}ColorIndex

This commit is contained in:
Cosimo Lupo 2021-07-20 19:51:47 +01:00
parent 40897a1508
commit 0eb27e9878
3 changed files with 48 additions and 41 deletions

View File

@ -89,16 +89,16 @@ def _beforeBuildPaintRadialGradient(paint, source):
return _beforeBuildPaintVarRadialGradient(paint, source, lambda v: v.value) return _beforeBuildPaintVarRadialGradient(paint, source, lambda v: v.value)
def _defaultColorIndex(): def _defaultColorStop():
colorIndex = ot.ColorIndex() colorStop = ot.ColorStop()
colorIndex.Alpha = _DEFAULT_ALPHA.value colorStop.Alpha = _DEFAULT_ALPHA.value
return colorIndex return colorStop
def _defaultVarColorIndex(): def _defaultVarColorStop():
colorIndex = ot.VarColorIndex() colorStop = ot.VarColorStop()
colorIndex.Alpha = _DEFAULT_ALPHA colorStop.Alpha = _DEFAULT_ALPHA.value
return colorIndex return colorStop
def _defaultColorLine(): def _defaultColorLine():
@ -113,6 +113,12 @@ def _defaultVarColorLine():
return colorLine return colorLine
def _defaultPaintSolid():
paint = ot.Paint()
paint.Alpha = _DEFAULT_ALPHA.value
return paint
def _buildPaintCallbacks(): def _buildPaintCallbacks():
return { return {
( (
@ -125,10 +131,13 @@ def _buildPaintCallbacks():
ot.Paint, ot.Paint,
ot.PaintFormat.PaintVarRadialGradient, ot.PaintFormat.PaintVarRadialGradient,
): _beforeBuildPaintVarRadialGradient, ): _beforeBuildPaintVarRadialGradient,
(BuildCallback.CREATE_DEFAULT, ot.ColorIndex): _defaultColorIndex, (BuildCallback.CREATE_DEFAULT, ot.ColorStop): _defaultColorStop,
(BuildCallback.CREATE_DEFAULT, ot.VarColorIndex): _defaultVarColorIndex, (BuildCallback.CREATE_DEFAULT, ot.VarColorStop): _defaultVarColorStop,
(BuildCallback.CREATE_DEFAULT, ot.ColorLine): _defaultColorLine, (BuildCallback.CREATE_DEFAULT, ot.ColorLine): _defaultColorLine,
(BuildCallback.CREATE_DEFAULT, ot.VarColorLine): _defaultVarColorLine, (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,
} }

View File

@ -39,7 +39,7 @@ class BuildCallback(enum.Enum):
""" """
AFTER_BUILD = enum.auto() AFTER_BUILD = enum.auto()
"""Keyed on (CREATE_DEFAULT, class). """Keyed on (CREATE_DEFAULT, class[, Format if available]).
Receives no arguments. Receives no arguments.
Should return a new instance of class. 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) return isinstance(value, collections.abc.Sequence) and not isinstance(value, str)
def _set_format(dest, source): def _split_format(cls, source):
if _isNonStrSequence(source): if _isNonStrSequence(source):
assert len(source) > 0, f"{type(dest)} needs at least format from {source}" assert len(source) > 0, f"{cls} needs at least format from {source}"
dest.Format = source[0] fmt, remainder = source[0], source[1:]
source = source[1:]
elif isinstance(source, collections.abc.Mapping): elif isinstance(source, collections.abc.Mapping):
assert "Format" in source, f"{type(dest)} needs at least Format from {source}" assert "Format" in source, f"{cls} needs at least Format from {source}"
dest.Format = source["Format"] remainder = source.copy()
fmt = remainder.pop("Format")
else: 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( assert isinstance(
dest.Format, collections.abc.Hashable fmt, collections.abc.Hashable
), f"{type(dest)} Format is not hashable: {dest.Format}" ), f"{cls} Format is not hashable: {fmt!r}"
assert ( assert (
dest.Format in dest.convertersByName fmt in cls.convertersByName
), f"{dest.Format} invalid Format of {cls}" ), f"{cls} invalid Format: {fmt!r}"
return source return fmt, remainder
class TableBuilder: class TableBuilder:
@ -140,6 +140,11 @@ class TableBuilder:
return source return source
callbackKey = (cls,) callbackKey = (cls,)
fmt = None
if issubclass(cls, FormatSwitchingBaseTable):
fmt, source = _split_format(cls, source)
callbackKey = (cls, fmt)
dest = self._callbackTable.get( dest = self._callbackTable.get(
(BuildCallback.CREATE_DEFAULT,) + callbackKey, lambda: cls() (BuildCallback.CREATE_DEFAULT,) + callbackKey, lambda: cls()
)() )()
@ -150,11 +155,9 @@ class TableBuilder:
# For format switchers we need to resolve converters based on format # For format switchers we need to resolve converters based on format
if issubclass(cls, FormatSwitchingBaseTable): if issubclass(cls, FormatSwitchingBaseTable):
source = _set_format(dest, source) dest.Format = fmt
convByName = _assignable(convByName[dest.Format]) convByName = _assignable(convByName[dest.Format])
skippedFields.add("Format") skippedFields.add("Format")
callbackKey = (cls, dest.Format)
# Convert sequence => mapping so before thunk only has to handle one format # Convert sequence => mapping so before thunk only has to handle one format
if _isNonStrSequence(source): if _isNonStrSequence(source):

View File

@ -1627,23 +1627,15 @@ otData = [
('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'), ('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', [ ('ColorStop', [
('F2Dot14', 'StopOffset', None, None, ''), ('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', [ ('VarColorStop', [
('F2Dot14', 'StopOffset', None, None, ''), ('F2Dot14', 'StopOffset', None, None, 'VarIndexBase + 0'),
('VarColorIndex', 'Color', None, None, ''), ('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.'), ('uint32', 'VarIndexBase', None, None, 'Base index into DeltaSetIndexMap.'),
]), ]),
@ -1668,12 +1660,15 @@ otData = [
# PaintSolid # PaintSolid
('PaintFormat2', [ ('PaintFormat2', [
('uint8', 'PaintFormat', None, None, 'Format identifier-format = 2'), ('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 # PaintVarSolid
('PaintFormat3', [ ('PaintFormat3', [
('uint8', 'PaintFormat', None, None, 'Format identifier-format = 3'), ('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 # PaintLinearGradient