colorLib: add type annotations
This commit is contained in:
parent
acfae6721b
commit
7a2f68a317
@ -1,46 +1,45 @@
|
|||||||
from fontTools.ttLib import newTable
|
from typing import Dict, List, Tuple
|
||||||
|
from fontTools.ttLib.tables.C_O_L_R_ import LayerRecord, table_C_O_L_R_
|
||||||
|
from fontTools.ttLib.tables.C_P_A_L_ import Color, table_C_P_A_L_
|
||||||
from .errors import ColorLibError
|
from .errors import ColorLibError
|
||||||
|
|
||||||
|
|
||||||
def buildCOLR(colorLayers):
|
def buildCOLR(colorLayers: Dict[str, List[Tuple[str, int]]]) -> table_C_O_L_R_:
|
||||||
"""Build COLR table from color layers mapping.
|
"""Build COLR table from color layers mapping.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
colorLayers: Dict[str, List[Tuple[str, int]]]: map of base glyph names (str)
|
colorLayers: : map of base glyph names to lists of (layer glyph names,
|
||||||
to lists of layer glyph names (str) and palette indices (int) tuples.
|
palette indices) tuples.
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
A new COLRv0 table.
|
A new COLRv0 table.
|
||||||
"""
|
"""
|
||||||
from fontTools.ttLib.tables.C_O_L_R_ import LayerRecord
|
|
||||||
|
|
||||||
colorLayerLists = {}
|
colorLayerLists = {}
|
||||||
for baseGlyphName, layers in colorLayers.items():
|
for baseGlyphName, layers in colorLayers.items():
|
||||||
colorLayerLists[baseGlyphName] = [
|
colorLayerLists[baseGlyphName] = [
|
||||||
LayerRecord(layerGlyphName, colorID) for layerGlyphName, colorID in layers
|
LayerRecord(layerGlyphName, colorID) for layerGlyphName, colorID in layers
|
||||||
]
|
]
|
||||||
|
|
||||||
colr = newTable("COLR")
|
colr = table_C_O_L_R_()
|
||||||
colr.version = 0
|
colr.version = 0
|
||||||
colr.ColorLayers = colorLayerLists
|
colr.ColorLayers = colorLayerLists
|
||||||
return colr
|
return colr
|
||||||
|
|
||||||
|
|
||||||
def buildCPAL(palettes):
|
def buildCPAL(
|
||||||
|
palettes: List[List[Tuple[float, float, float, float]]]
|
||||||
|
) -> table_C_P_A_L_:
|
||||||
"""Build CPAL table from list of color palettes.
|
"""Build CPAL table from list of color palettes.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
palettes: List[List[Tuple[float, float, float, float]]]: list of lists
|
palettes: : list of lists of colors encoded as tuples of (R, G, B, A) floats.
|
||||||
colors encoded as tuples of (R, G, B, A) floats.
|
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
A new CPALv0 table.
|
A new CPALv0 table.
|
||||||
"""
|
"""
|
||||||
from fontTools.ttLib.tables.C_P_A_L_ import Color
|
|
||||||
|
|
||||||
if len({len(p) for p in palettes}) != 1:
|
if len({len(p) for p in palettes}) != 1:
|
||||||
raise ColorLibError("color palettes have different lengths")
|
raise ColorLibError("color palettes have different lengths")
|
||||||
cpal = newTable("CPAL")
|
cpal = table_C_P_A_L_()
|
||||||
# TODO(anthotype): Support version 1 with palette types, labels and entry labels.
|
# TODO(anthotype): Support version 1 with palette types, labels and entry labels.
|
||||||
cpal.version = 0
|
cpal.version = 0
|
||||||
cpal.numPaletteEntries = len(palettes[0])
|
cpal.numPaletteEntries = len(palettes[0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user