Skip to content

Commit

Permalink
Get static building working again
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Nov 20, 2024
1 parent 97f71a9 commit 8f99e62
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
5 changes: 1 addition & 4 deletions Lib/gftools/builder/fontc.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ def modify_config(self, config: dict):
extra_args = config.get("extraFontmakeArgs") or ""
extra_args += " --no-production-names --drop-implied-oncurves"
config["extraFontmakeArgs"] = extra_args
# override config to turn not build instances if we're variable
if self.will_build_variable_font(config):
config["buildStatic"] = False
# if the font doesn't explicitly request CFF, just build TT outlines
# if the font _only_ wants CFF outlines, we will try to build them
# ( but fail on fontc for now) (but is this even a thing?)
elif config.get("buildTTF", True):
if config.get("buildTTF", True):
config["buildOTF"] = False
if self.simple_output_path:
output_dir = str(self.simple_output_path)
Expand Down
5 changes: 5 additions & 0 deletions Lib/gftools/builder/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ def get(self, operation_name: str):

return FontcAddSubset

if operation_name == "instantiateUfo":
from .fontc.fontcInstantiateUfo import FontcInstantiateUFO

return FontcInstantiateUFO

return self.known_operations.get(operation_name)


Expand Down
70 changes: 70 additions & 0 deletions Lib/gftools/builder/operations/fontc/fontcInstantiateUfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from pathlib import Path
from typing import List
from gftools.builder.file import File
from gftools.builder.operations import FontmakeOperationBase
import glyphsLib
import os
import gftools.builder
from functools import cached_property
from glyphsLib.builder import UFOBuilder
from ninja.ninja_syntax import Writer, escape_path
from fontTools.designspaceLib import InstanceDescriptor


class FontcInstantiateUFO(FontmakeOperationBase):
description = "Create instance UFOs from a Glyphs or designspace file"
rule = 'fontmake -i "$instance_name" -o ufo $fontmake_type $in $args'

def validate(self):
# Ensure there is an instance name
if "instance_name" not in self.original:
raise ValueError("No instance name specified")
# Ensure the instance is defined in the font
desired = self.original["instance_name"]
if "target" not in self.original and not self.relevant_instance:
raise ValueError(
f"Instance {desired} not found in {self.first_source.path}"
)

@cached_property
def relevant_instance(self):
desired = self.original["instance_name"]
relevant_instance = [
i
for i in self.first_source.instances
if i.name == desired or i.familyName + " " + i.styleName == desired
]
if len(relevant_instance) == 0:
return None
return relevant_instance[0]

@property
def instance_dir(self):
return Path(self.first_source.path).parent / "instance_ufos"

@property
def targets(self):
if "target" in self.original:
return [File(self.original["target"])]
instance = self.relevant_instance
assert instance is not None
assert instance.filename is not None
# if self.first_source.is_glyphs:
return [File(str(self.instance_dir / (os.path.basename(instance.filename))))]
# return [ File(instance.filename) ]

@property
def variables(self):
vars = super().variables
if self.first_source.is_glyphs:
vars["args"] += f"--instance-dir {escape_path(str(self.instance_dir))}"
else:
vars["args"] += f"--output-dir {escape_path(str(self.instance_dir))}"
vars["instance_name"] = self.original["instance_name"]
if self.original.get("glyphData") is not None:
for glyphData in self.original["glyphData"]:
vars["args"] += f" --glyph-data {escape_path(glyphData)}"
return vars

def set_target(self, target: File):
raise ValueError("Cannot set target on InstantiateUFO")
30 changes: 14 additions & 16 deletions Lib/gftools/builder/recipeproviders/googlefonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,22 +333,20 @@ def build_a_static(self, source: File, instance: InstanceDescriptor, output):
steps = [
{"source": source.path},
]
# if we're running fontc we skip conversion to UFO
if not source.is_ufo and not self.config.get("use_fontc", False):
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": instancename,
"glyphData": self.config.get("glyphData"),
}
)
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": instancename,
"glyphData": self.config.get("glyphData"),
}
)
steps += (
[
{
Expand Down
2 changes: 1 addition & 1 deletion Lib/gftools/builder/recipeproviders/noto.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def build_a_static(self, source, instance, output):
"instance_name": instance.name,
"target": "full-designspace/instance_ufos/"
+ os.path.basename(instance.filename)
+ ".json",
+ ("" if self.builder.fontc_args.use_fontc else ".json"),
},
{
"operation": "buildTTF" if output == "ttf" else "buildOTF",
Expand Down

0 comments on commit 8f99e62

Please sign in to comment.