Skip to content

Instantly share code, notes, and snippets.

@justvanrossum
Created September 19, 2022 12:55
Show Gist options
  • Save justvanrossum/8a78bd4dc35b496fd09d37d85faf13f3 to your computer and use it in GitHub Desktop.
Save justvanrossum/8a78bd4dc35b496fd09d37d85faf13f3 to your computer and use it in GitHub Desktop.
Rebuild the named instances in a designspace 5 file based on the axis labels. Use with caution!
import argparse
from itertools import product
import sys
from fontTools.designspaceLib import DesignSpaceDocument
def main():
parser = argparse.ArgumentParser(
description="Makes new named instances based on the axis labels"
)
parser.add_argument("input", help="The source .designspace file")
parser.add_argument("output", help="The destination .designspace file")
args = parser.parse_args()
doc = DesignSpaceDocument.fromfile(args.input)
defaultSource = doc.findDefault()
familyName = defaultSource.familyName
doc.instances = []
allLabels = [[(axis.name, label) for label in axis.axisLabels] for axis in doc.axes]
for labels in product(*allLabels):
styleName = " ".join(label.name for _, label in labels if not label.elidable)
if not styleName:
styleName = doc.elidedFallbackName
location = {axisName: label.userValue for axisName, label in labels}
doc.addInstanceDescriptor(
familyName=familyName, styleName=styleName, userLocation=location
)
doc.write(args.output)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment