Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Created March 26, 2020 11:23
Show Gist options
  • Select an option

  • Save anthrotype/e6165a7a10b9bb26f6b0d948282a3d8e to your computer and use it in GitHub Desktop.

Select an option

Save anthrotype/e6165a7a10b9bb26f6b0d948282a3d8e to your computer and use it in GitHub Desktop.
from fontTools import designspaceLib
import os.path
# define axes names, tags and min/default/max
AXES = [
dict(name="Weight", tag="wght", minimum=400, default=400, maximum=700),
dict(name="Width", tag="wdth", minimum=50, default=100, maximum=100),
]
# assign each master a location in the designspace. Ensure that at least
# one master is at the default location along all the axes as defined above;
# this will be treated as the 'neutral' or default master.
# XXX Replace the font filenames with actual ones.
MASTERS_PATHS_AND_LOCATIONS = [
("MyFont-1.ttf", {"Weight": 400, "Width": 100}), # this is the base master
("MyFont-2.ttf", {"Weight": 700, "Width": 100}),
("MyFont-3.ttf", {"Weight": 400, "Width": 50}),
("MyFont-4.ttf", {"Weight": 700, "Width": 50}),
]
# optionally, define the location and style name of named instances
NAMED_INSTANCES = [
("Regular", {"Weight": 400, "Width": 100}),
# ("Medium", {"Weight": 500, "Width": 100}),
# ("SemiBold", {"Weight": 600, "Width": 100}),
# ("Bold", {"Weight": 700, "Width": 100}),
# ("Condensed", {"Weight": 400, "Width": 75}),
# ("Condensed Medium", {"Weight": 500, "Width": 75}),
# ("Condensed SemiBold", {"Weight": 600, "Width": 75}),
# ("Condensed Bold", {"Weight": 700, "Width": 75}),
# ("UltraCondensed", {"Weight": 400, "Width": 50}),
# ("UltraCondensed Medium", {"Weight": 500, "Width": 50}),
# ("UltraCondensed SemiBold", {"Weight": 600, "Width": 50}),
# ("UltraCondensed Bold", {"Weight": 700, "Width": 50}),
]
def build_designspace(axes, masters, instances=None):
# create a new designspace document
designspace = designspaceLib.DesignSpaceDocument()
for axis_data in axes:
designspace.addAxisDescriptor(**axis_data)
for source_path, location in masters:
# the source.name is an optional identifier only used for error messages
source_name = os.path.basename(os.path.splitext(source_path)[0])
designspace.addSourceDescriptor(
name=source_name, location=location, path=source_path
)
if instances is not None:
for style_name, location in instances:
designspace.addInstanceDescriptor(styleName=style_name, location=location)
return designspace
if __name__ == "__main__":
import sys
designspace = build_designspace(AXES, MASTERS_PATHS_AND_LOCATIONS, NAMED_INSTANCES)
try:
output_path = sys.argv[1]
except IndexError:
sys.exit(f"usage: {sys.argv[0]} MyFont.designspace")
designspace.write(output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment