Skip to content

Commit

Permalink
Merge pull request #21 from googlefonts/add-instances
Browse files Browse the repository at this point in the history
Add (fvar) instances, if they are not given
  • Loading branch information
justvanrossum authored Apr 24, 2024
2 parents ff19e1b + 0064840 commit d8c286b
Show file tree
Hide file tree
Showing 5 changed files with 1,364 additions and 1,074 deletions.
45 changes: 45 additions & 0 deletions src/fontra_compile/compile_fontmake_action.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import os
import pathlib
import subprocess
Expand All @@ -10,6 +11,7 @@
from fontra.backends.copy import copyFont
from fontra.core.protocols import ReadableFontBackend
from fontra.workflow.actions import OutputActionProtocol, registerActionClass
from fontTools.designspaceLib import DesignSpaceDocument


@registerActionClass("compile-fontmake")
Expand Down Expand Up @@ -46,6 +48,8 @@ async def process(
async with aclosing(dsBackend):
await copyFont(self.input, dsBackend, continueOnError=continueOnError)

addInstances(designspacePath)

command = [
"fontmake",
"-m",
Expand All @@ -62,3 +66,44 @@ async def process(
command.append(value)

subprocess.run(command, check=True)


def addInstances(designspacePath):
dsDoc = DesignSpaceDocument.fromfile(designspacePath)
if dsDoc.instances:
# There are instances
return

# We will make up instances based on the axis value labels

sortOrder = {
"wght": 0,
"wdth": 1,
"ital": 2,
"slnt": 3,
}
axes = sorted(dsDoc.axes, key=lambda axis: sortOrder.get(axis.tag, 100))

elidedFallbackName = dsDoc.elidedFallbackName or "Regular"
dsDoc.elidedFallbackName = elidedFallbackName

axisLabels = [
[
(axis.name, label.name if not label.elidable else None, label.userValue)
for label in axis.axisLabels
]
for axis in axes
]

for items in itertools.product(*axisLabels):
location = {name: value for (name, valueLabel, value) in items}
nameParts = [valueLabel for (name, valueLabel, value) in items if valueLabel]
if not nameParts:
nameParts = [elidedFallbackName]
styleName = " ".join(nameParts)

# TODO: styleName seems to be ignored, and the instance names are derived
# from axis labels elsewhere. Figure out where this happens.
dsDoc.addInstanceDescriptor(styleName=styleName, userLocation=location)

dsDoc.write(designspacePath)
Loading

0 comments on commit d8c286b

Please sign in to comment.