Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add more typing to instantiator #697

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions Lib/fontmake/instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
import copy
import logging
import typing
from typing import Any, Dict, List, Mapping, Set, Tuple, Union
from typing import AbstractSet, Any, Dict, List, Mapping, Sequence, Set, Tuple, Union

import attr
import fontMath
import fontTools.designspaceLib as designspaceLib
import fontTools.misc.fixedTools
import fontTools.varLib as varLib
import ufoLib2
from fontTools.designspaceLib import RuleDescriptor

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -153,23 +154,27 @@ class InstantiatorError(Exception):
pass


def process_rules_swaps(rules, location, glyphNames):
""" Apply these rules at this location to these glyphnames
- rule order matters
def process_rules_swaps(
rules: Sequence[RuleDescriptor],
location: Location,
glyph_names: Union[Sequence[str], AbstractSet[str]],
) -> List[Tuple[str, str]]:
"""Apply these rules at this location to these glyphnames
- rule order matters

Return a list of (oldName, newName) in the same order as the rules.
Return a list of (oldName, newName) in the same order as the rules.
"""
swaps = []
glyphNames = set(glyphNames)
swaps: List[Tuple[str, str]] = []
glyph_names = set(glyph_names)
for rule in rules:
if designspaceLib.evaluateRule(rule, location):
for oldName, newName in rule.subs:
for name_old, name_new in rule.subs:
# Here I don't check if the new name is also in glyphNames...
# I guess it should be, so that we can swap, and if it isn't,
# then it's better to error out later when we try to swap,
# instead of silently ignoring the rule here.
if oldName in glyphNames:
swaps.append((oldName, newName))
if name_old in glyph_names:
swaps.append((name_old, name_new))
return swaps


Expand Down Expand Up @@ -198,7 +203,7 @@ def from_designspace(
cls,
designspace: designspaceLib.DesignSpaceDocument,
round_geometry: bool = True,
):
) -> "Instantiator":
"""Instantiates a new data class from a Designspace object."""
if designspace.default is None:
raise InstantiatorError(_error_msg_no_default(designspace))
Expand Down Expand Up @@ -348,7 +353,7 @@ def generate_instance(

# Lib
# 1. Copy the default lib to the instance.
font.lib = typing.cast(dict, copy.deepcopy(self.copy_lib))
font.lib = typing.cast(Dict[str, Any], copy.deepcopy(self.copy_lib))
# 2. Copy the Designspace's skipExportGlyphs list over to the UFO to
# make sure it wins over the default UFO one.
font.lib["public.skipExportGlyphs"] = [name for name in self.skip_export_glyphs]
Expand Down Expand Up @@ -607,7 +612,7 @@ def collect_glyph_masters(
return locations_and_masters


def width_class_from_wdth_value(wdth_user_value) -> int:
def width_class_from_wdth_value(wdth_user_value: int) -> int:
"""Return the OS/2 width class from the wdth axis user value.

The OpenType 1.8.3 specification states:
Expand All @@ -627,19 +632,19 @@ def width_class_from_wdth_value(wdth_user_value) -> int:
return fontTools.misc.fixedTools.otRound(width_user_value_mapped)


def weight_class_from_wght_value(wght_user_value) -> int:
def weight_class_from_wght_value(wght_user_value: int) -> int:
"""Return the OS/2 weight class from the wght axis user value."""
weight_user_value = min(max(wght_user_value, 1), 1000)
return fontTools.misc.fixedTools.otRound(weight_user_value)


def italic_angle_from_slnt_value(slnt_user_value) -> Union[int, float]:
def italic_angle_from_slnt_value(slnt_user_value: int) -> Union[int, float]:
"""Return the italic angle from the slnt axis user value."""
slant_user_value = min(max(slnt_user_value, -90), 90)
return slant_user_value


def swap_glyph_names(font: ufoLib2.Font, name_old: str, name_new: str):
def swap_glyph_names(font: ufoLib2.Font, name_old: str, name_new: str) -> None:
"""Swap two existing glyphs in the default layer of a font (outlines,
width, component references, kerning references, group membership).

Expand Down Expand Up @@ -739,7 +744,7 @@ class Variator:
@classmethod
def from_masters(
cls, items: List[Tuple[Location, FontMathObject]], axis_order: List[str]
):
) -> "Variator":
masters = []
master_locations = []
location_to_master = {}
Expand Down