From 28cc28b38adbf9d121518437cec6022aead9d8d8 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sat, 16 Dec 2023 12:16:29 +0100 Subject: [PATCH] Add type annotations, soe minor tweaks --- src/fontra/core/instancer.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/fontra/core/instancer.py b/src/fontra/core/instancer.py index ba4df7e32..70574a98f 100644 --- a/src/fontra/core/instancer.py +++ b/src/fontra/core/instancer.py @@ -3,7 +3,7 @@ from dataclasses import dataclass, replace from enum import Enum from functools import cached_property, partial, singledispatch -from typing import Any +from typing import Any, Iterable from fontTools.misc.transform import DecomposedTransform, Transform from fontTools.varLib.models import ( @@ -51,7 +51,9 @@ async def getGlyphInstancer( self.glyphInstancers[glyphName] = glyphInstancer return glyphInstancer - async def _ensureComponentLocationCompatibility(self, glyph): + async def _ensureComponentLocationCompatibility( + self, glyph: VariableGlyph + ) -> VariableGlyph: layerGlyphs = { source.layerName: glyph.layers[source.layerName].glyph for source in glyph.sources @@ -87,10 +89,12 @@ async def _ensureComponentLocationCompatibility(self, glyph): ) -def _areComponentLocationsCompatible(glyphs): +def _areComponentLocationsCompatible( + glyphs: Iterable[StaticGlyph], +) -> tuple[bool, list[set[str]]]: ok = True numComponents = None - componentAxisNames = None + componentAxisNames: list[Any] | None = None for glyph in glyphs: if numComponents is None: @@ -106,12 +110,17 @@ def _areComponentLocationsCompatible(glyphs): ok = False componentAxisNames[i] |= axisNames + assert componentAxisNames is not None + return ok, componentAxisNames def _fixComponentLocationsCompatibility( - glyph, layerGlyphs, componentAxisNames, axisDefaults -): + glyph: VariableGlyph, + layerGlyphs: dict, + componentAxisNames: list[set[str]], + axisDefaults, +) -> VariableGlyph: return replace( glyph, layers={ @@ -131,12 +140,14 @@ def _fixComponentLocationsCompatibility( ) -def _fixComponentLocation(component, axisNames, axisDefaults): +def _fixComponentLocation( + component: Component, axisNames: set[str], axisDefaults: dict[str, float] +): return replace( component, location={ axisName: component.location.get(axisName, axisDefaults.get(axisName, 0)) - for axisName in axisNames + for axisName in sorted(axisNames) }, )