From 105ff67b4589fcd52c128caa6e4b133d6f09b742 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 30 May 2024 16:15:31 +0100 Subject: [PATCH] Intuit instance names better, fixes #966 --- .../builder/recipeproviders/googlefonts.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Lib/gftools/builder/recipeproviders/googlefonts.py b/Lib/gftools/builder/recipeproviders/googlefonts.py index 70069fc5..f733c475 100644 --- a/Lib/gftools/builder/recipeproviders/googlefonts.py +++ b/Lib/gftools/builder/recipeproviders/googlefonts.py @@ -5,8 +5,10 @@ from tempfile import NamedTemporaryFile import yaml +from fontTools.designspaceLib import DesignSpaceDocument, InstanceDescriptor from strictyaml import load, YAMLValidationError +from gftools.builder.file import File from gftools.builder.recipeproviders import RecipeProviderBase from gftools.builder.schema import ( GOOGLEFONTS_SCHEMA, @@ -122,7 +124,9 @@ def _vf_filename(self, source, suffix="", extension="ttf"): return os.path.join(directory, f"{sourcebase}[{axis_tags}].{extension}") - def _static_filename(self, instance, suffix="", extension="ttf"): + def _static_filename( + self, instance: InstanceDescriptor, suffix: str = "", extension: str = "ttf" + ): """Determine the file name for a static font.""" if extension == "ttf": outdir = self.config["ttDir"] @@ -256,17 +260,24 @@ def build_all_statics(self): if self.config["buildOTF"]: self.build_a_static(source, instance, output="otf") - def build_a_static(self, source, instance, output): + def build_a_static(self, source: File, instance: InstanceDescriptor, output): target = self._static_filename(instance, extension=output) steps = [ {"source": source.path}, ] if not source.is_ufo: + instancename = instance.name + if instancename is None: + if not instance.familyName or not instance.styleName: + raise ValueError( + f"Instance {instance.filename} must have a name, or familyName and styleName" + ) + instancename = instance.familyName + " " + instance.styleName steps.append( { "operation": "instantiateUfo", - "instance_name": instance.name, + "instance_name": instancename, "glyphData": self.config.get("glyphData"), } )