Skip to content

Commit

Permalink
Add type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Dec 16, 2023
1 parent d4a8c11 commit b8c7a7c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/fontra/backends/opentype.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 []
Expand All @@ -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
Expand Down

0 comments on commit b8c7a7c

Please sign in to comment.