Skip to content

Commit

Permalink
Add type annotations, soe minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
justvanrossum committed Dec 16, 2023
1 parent b61c6c5 commit 28cc28b
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/fontra/core/instancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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={
Expand All @@ -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)
},
)

Expand Down

0 comments on commit 28cc28b

Please sign in to comment.