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

Instantiator support for non-default layer sources #980

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 35 additions & 17 deletions Lib/fontmake/instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"woffMinorVersion",
}

DEFAULT_LAYER_NAME = "foreground"
verbosus marked this conversation as resolved.
Show resolved Hide resolved


# Custom exception for this module
class InstantiatorError(Exception):
Expand Down Expand Up @@ -224,21 +226,33 @@ def from_designspace(
# because the math behind varLib and MutatorMath uses the default font as the
# point of reference for all data.
default_font = designspace.default.font
non_default_layer_name = designspace.default.layerName
non_default_layer_flag = (
verbosus marked this conversation as resolved.
Show resolved Hide resolved
non_default_layer_name is not None
and non_default_layer_name != DEFAULT_LAYER_NAME
verbosus marked this conversation as resolved.
Show resolved Hide resolved
)

glyph_names: Set[str] = set(default_font.keys())

for source in designspace.sources:
other_names = set(source.font.keys())
diff_names = other_names - glyph_names
if diff_names:
logger.warning(
"The source %s (%s) contains glyphs that are missing from the "
"default source, which will be ignored: %s. If this is unintended, "
"check that these glyphs have the exact same name as the "
"corresponding glyphs in the default source.",
source.name,
source.filename,
", ".join(sorted(diff_names)),
)
if non_default_layer_flag:
if non_default_layer_name in default_font.layers:
verbosus marked this conversation as resolved.
Show resolved Hide resolved
layer = default_font.layers[non_default_layer_name]
glyph_names = layer.keys()
logger.info(f"Building from layer {layer.name}")
else:
for source in designspace.sources:
other_names = set(source.font.keys())
diff_names = other_names - glyph_names
if diff_names:
logger.warning(
verbosus marked this conversation as resolved.
Show resolved Hide resolved
"The source %s (%s) contains glyphs that are missing from the "
"default source, which will be ignored: %s. If this is unintended, "
"check that these glyphs have the exact same name as the "
"corresponding glyphs in the default source.",
source.name,
source.filename,
", ".join(sorted(diff_names)),
)

# Construct Variators
axis_bounds: AxisBounds = {} # Design space!
Expand Down Expand Up @@ -517,9 +531,11 @@ def collect_info_masters(
) -> List[Tuple[Location, FontMathObject]]:
"""Return master Info objects wrapped by MathInfo."""
locations_and_masters = []

for source in designspace.sources:
if source.layerName is not None:
continue # No font info in source layers.
if source.layerName is not None and source.layerName != DEFAULT_LAYER_NAME:
if source != designspace.default:
verbosus marked this conversation as resolved.
Show resolved Hide resolved
continue # No font info in source layers.
verbosus marked this conversation as resolved.
Show resolved Hide resolved

normalized_location = varLib.models.normalizeLocation(
source.location, axis_bounds
Expand All @@ -541,9 +557,11 @@ def collect_kerning_masters(
groups = designspace.default.font.groups

locations_and_masters = []

for source in designspace.sources:
if source.layerName is not None:
continue # No kerning in source layers.
if source.layerName is not None and source.layerName != DEFAULT_LAYER_NAME:
if source != designspace.default:
verbosus marked this conversation as resolved.
Show resolved Hide resolved
continue # No kerning in source layers.

# If a source has groups, they should match the default's.
if source.font.groups and source.font.groups != groups:
Expand Down
29 changes: 29 additions & 0 deletions tests/data/MutatorSans/MutatorSans-non-default-layer.designspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version='1.0' encoding='UTF-8'?>
<designspace format="4.0">
<axes>
<axis tag="wdth" name="width" minimum="0" maximum="1000" default="0"/>
<axis tag="wght" name="weight" minimum="0" maximum="1000" default="0"/>
</axes>
<sources>
<source filename="MutatorSansLightCondensed.ufo" name="master.MutatorMathTest.LightCondensed.0" familyname="MutatorMathTest" stylename="LightCondensed" layer="support">
<lib copy="1"/>
<groups copy="1"/>
<features copy="1"/>
<info copy="1"/>
<location>
<dimension name="width" xvalue="0"/>
<dimension name="weight" xvalue="0"/>
</location>
</source>
</sources>
<instances>
<instance familyname="MutatorMathTest" stylename="LightCondensed" filename="instances/MutatorMathTest-LightCondensed.ufo" postscriptfontname="MutatorMathTest-LightCondensed">
<location>
<dimension name="width" xvalue="0"/>
<dimension name="weight" xvalue="0"/>
</location>
<kerning/>
<info/>
</instance>
</instances>
</designspace>
12 changes: 12 additions & 0 deletions tests/test_instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ def test_interpolation_masters_as_instances(data_dir):
assert instance_font["l"].width == 280


def test_non_default_layer(data_dir, caplog):
designspace = designspaceLib.DesignSpaceDocument.fromfile(
data_dir / "MutatorSans" / "MutatorSans-non-default-layer.designspace"
)
designspace.loadSourceFonts(ufoLib2.Font.open)
generator = fontmake.instantiator.Instantiator.from_designspace(
designspace, round_geometry=True
)
instance_font = generator.generate_instance(designspace.instances[0])
assert {g.name for g in instance_font} == {"A", "S", "W"}


def test_instance_attributes(data_dir):
designspace = designspaceLib.DesignSpaceDocument.fromfile(
data_dir / "DesignspaceTest" / "DesignspaceTest-instance-attrs.designspace"
Expand Down