Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions Lib/ufo2ft/instantiator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from fontTools.ttLib.tables._n_a_m_e import NameRecord as ftNameRecord
from fontTools.ttLib.tables._n_a_m_e import _makeWindowsName

from ufo2ft.constants import OPENTYPE_CATEGORIES_KEY
from ufo2ft.util import (
_getNewGlyphFactory,
importUfoModule,
Expand Down Expand Up @@ -316,6 +317,10 @@ class Instantiator:
)
# (design location, openTypeOS2Panose) of each source that has its own fontinfo
source_panoses: List[Tuple[Location, List[int]]] = field(default_factory=list)
# (design location, public.openTypeCategories) of each source that defines them
source_opentype_categories: List[Tuple[Location, Dict[str, str]]] = field(
default_factory=list
)
# computed attributes (see __post_init__ below)
default_source_idx: int = field(init=False)
default_design_location: Location = field(init=False)
Expand Down Expand Up @@ -524,6 +529,20 @@ def from_designspace(

copy_lib: Mapping[str, Any] = default_font.lib if do_info else {}

# Likewise for the GDEF glyph categories, which can differ per source (e.g.
# glyphsLib infers them from each master's anchors). Sources that define
# none fall back to the default's, copied with the rest of its lib.
source_opentype_categories: List[Tuple[Location, Dict[str, str]]] = []
if do_info:
for source in designspace.sources:
if source.layerName is not None and source is not designspace.default:
continue # No lib in non-default source layers.
categories = source.font.lib.get(OPENTYPE_CATEGORIES_KEY)
if not categories:
continue
source_location = {**default_design_location, **source.location}
source_opentype_categories.append((source_location, categories))

# The list of glyphs-not-to-export-and-decompose-where-used-as-a-component is
# supposed to be taken from the Designspace when a Designspace is used as the
# starting point of the compilation process. It should be exported to all
Expand Down Expand Up @@ -551,6 +570,7 @@ def from_designspace(
designspace_root_lib_font_info,
special_axes,
source_panoses,
source_opentype_categories,
)

@property
Expand Down Expand Up @@ -614,6 +634,11 @@ def generate_instance(self, instance: designspaceLib.InstanceDescriptor) -> Font
# Lib
# 1. Copy the default lib to the instance.
font.lib = typing.cast(dict, copy.deepcopy(self.copy_lib))
# If the instance sits on a source with its own GDEF categories, use those.
for source_location, categories in self.source_opentype_categories:
if source_location == location:
font.lib[OPENTYPE_CATEGORIES_KEY] = copy.deepcopy(categories)
break
# 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
35 changes: 35 additions & 0 deletions tests/instantiator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,3 +1626,38 @@ def test_strict_math_glyph(ufo_module, data_dir):
glyph = fonts[0]["test"]
assert len(glyph.contours) == 1
assert len(glyph.contours[0].points) == 16


def test_opentype_categories_from_source_at_instance_location(ufo_module):
d = designspaceLib.DesignSpaceDocument()
d.addAxisDescriptor(
name="Weight", tag="wght", minimum=300, default=300, maximum=900
)
for weight, categories in [
(300, {"a": "base"}),
(600, None),
(900, {"a": "base", "b": "base"}),
]:
font = ufo_module.Font()
font.newGlyph("a")
font.newGlyph("b")
if categories is not None:
font.lib["public.openTypeCategories"] = categories
d.addSourceDescriptor(location={"Weight": weight}, font=font)
for weight in (300, 450, 600, 900):
d.addInstanceDescriptor(styleName=str(weight), location={"Weight": weight})

generator = ufo2ft.instantiator.Instantiator.from_designspace(d)
categories = [
generator.generate_instance(instance).lib["public.openTypeCategories"]
for instance in d.instances
]

# Only an instance on a source that defines categories takes them, the
# others get the default source's.
assert categories == [
{"a": "base"},
{"a": "base"},
{"a": "base"},
{"a": "base", "b": "base"},
]
Loading