Skip to content

Instantly share code, notes, and snippets.

@connordavenport
Created May 26, 2020 19:33
Show Gist options
  • Save connordavenport/0639a51963b5633942c5f61a1da50a05 to your computer and use it in GitHub Desktop.
Save connordavenport/0639a51963b5633942c5f61a1da50a05 to your computer and use it in GitHub Desktop.
Add FVAR Instance
from fontTools.ttLib.tables._f_v_a_r import table__f_v_a_r, Axis, NamedInstance
from fontTools.ttLib.tables._n_a_m_e import table__n_a_m_e, NameRecord
from fontTools.ttLib import TTFont
def AddName(font, name):
nameTable = font.get("name")
if nameTable is None:
nameTable = font["name"] = table__n_a_m_e()
nameTable.names = []
namerec = NameRecord()
namerec.nameID = 1 + max([n.nameID for n in nameTable.names] + [256])
namerec.string = name.encode('mac_roman')
namerec.platformID, namerec.platEncID, namerec.langID = (1, 0, 0)
nameTable.names.append(namerec)
return namerec
fontPath = "path/to/font.ttf"
font = TTFont(fontPath)
instances = [("Light", 300),("ExtraSuperBold", 1000)]
fvarTable = font["fvar"]
for name, axisLocation in instances:
inst = NamedInstance()
inst.subfamilyNameID = AddName(font, name).nameID
inst.coordinates = {"wght": weight}
fvarTable.instances.append(inst)
font.save(fontPath.replace(".ttf","-newInstance.ttf"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment