Created
September 19, 2022 12:55
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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