From b8c7a7c990c1e408b57ce2bfa8798d5e827a6f23 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sat, 16 Dec 2023 14:21:59 +0100 Subject: [PATCH] Add type annotations --- src/fontra/backends/opentype.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/fontra/backends/opentype.py b/src/fontra/backends/opentype.py index ea02b2b24..fde3bbe92 100644 --- a/src/fontra/backends/opentype.py +++ b/src/fontra/backends/opentype.py @@ -1,5 +1,5 @@ from os import PathLike -from typing import Any +from typing import Any, Generator from fontTools.misc.psCharStrings import SimpleT2Decompiler from fontTools.pens.pointPen import GuessSmoothPointPen @@ -23,7 +23,7 @@ class OTFBackend: def fromPath(cls, path: PathLike) -> ReadableFontBackend: return cls(path=path) - def __init__(self, *, path): + def __init__(self, *, path: PathLike) -> None: self.path = path self.font = TTFont(path, lazy=True) self.globalAxes = unpackAxes(self.font) @@ -35,14 +35,14 @@ def __init__(self, *, path): else None ) self.characterMap = self.font.getBestCmap() - glyphMap = {} + glyphMap: dict[str, list[int]] = {} for glyphName in self.font.getGlyphOrder(): glyphMap[glyphName] = [] for code, glyphName in sorted(self.characterMap.items()): glyphMap[glyphName].append(code) self.glyphMap = glyphMap self.glyphSet = self.font.getGlyphSet() - self.variationGlyphSets = {} + self.variationGlyphSets: dict[str, Any] = {} def close(self): self.font.close() @@ -81,7 +81,7 @@ async def getGlyph(self, glyphName: str) -> VariableGlyph | None: glyph.sources = sources return glyph - def _getGlyphVariationLocations(self, glyphName): + def _getGlyphVariationLocations(self, glyphName: str) -> list[dict[str, float]]: # TODO/FIXME: This misses variations that only exist in HVAR/VVAR locations = set() if self.gvarVariations is not None: @@ -118,11 +118,13 @@ async def getCustomData(self) -> dict[str, Any]: return {} -def tuplifyLocation(loc): +def tuplifyLocation(loc: dict[str, float]) -> tuple: return tuple(sorted(loc.items())) -def getLocationsFromVarstore(varDataIndex, varStore, fvarAxes): +def getLocationsFromVarstore( + varDataIndex: int, varStore, fvarAxes +) -> Generator[dict[str, float], None, None]: regions = varStore.VarRegionList.Region for regionIndex in varStore.VarData[varDataIndex].VarRegionIndex: location = { @@ -133,7 +135,7 @@ def getLocationsFromVarstore(varDataIndex, varStore, fvarAxes): yield location -def unpackAxes(font): +def unpackAxes(font: TTFont) -> list[GlobalAxis | GlobalDiscreteAxis]: fvar = font.get("fvar") if fvar is None: return [] @@ -144,7 +146,7 @@ def unpackAxes(font): if avar is not None else {} ) - axisList = [] + axisList: list[GlobalAxis | GlobalDiscreteAxis] = [] for axis in fvar.axes: normMin = -1 if axis.minValue < axis.defaultValue else 0 normMax = 1 if axis.maxValue > axis.defaultValue else 0