Last active
June 6, 2023 10:22
-
-
Save anthrotype/17d1c8af5e8dbfde9cb77e6628b13ab7 to your computer and use it in GitHub Desktop.
Script to append a new axis to fvar and related variation tables
This file contains hidden or 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 sys | |
import argparse | |
import logging | |
from fontTools.ttLib import TTFont | |
from fontTools.ttLib.tables._f_v_a_r import Axis | |
from fontTools.varLib.builder import buildVarRegionAxis | |
log = logging.getLogger("add_fvar_axis.py") | |
def main(): | |
parser = argparse.ArgumentParser(description="Add a new axis to a variable font.") | |
parser.add_argument("font", help="The font to modify") | |
parser.add_argument("axis", help="The axis tag=min,default,max", nargs="+") | |
parser.add_argument("-o", "--output", help="The output font") | |
parser.add_argument("-v", "--verbose", action="store_true") | |
args = parser.parse_args() | |
logging.basicConfig(level="DEBUG" if args.verbose else "INFO") | |
font = TTFont(args.font, lazy=False) | |
font.ensureDecompiled() | |
fvar = font["fvar"] | |
name = font["name"] | |
for axis in args.axis: | |
tag, values = axis.split("=") | |
values = [float(v) for v in values.split(",")] | |
axis = Axis() | |
axis.axisTag = tag | |
axis.minValue, axis.defaultValue, axis.maxValue = values | |
axis.axisNameID = name.addName(tag, platforms=((3, 1, 0x409),)) | |
fvar.axes.append(axis) | |
for instance in fvar.instances: | |
instance.coordinates[tag] = axis.defaultValue | |
avar = font.get("avar") | |
if avar is not None: | |
segments = avar.segments | |
for axis in fvar.axes: | |
if axis.axisTag not in segments: | |
segments[tag] = {-1.0: -1.0, 0.0: 0.0, 1.0: 1.0} | |
for tableTag in font.keys(): | |
table = font[tableTag] | |
if ( | |
getattr(table, "table", None) is None | |
or getattr(table.table, "VarStore", None) is None | |
): | |
continue | |
log.info(f"Updating {tableTag}.VarStore") | |
varStore = table.table.VarStore | |
for region in varStore.VarRegionList.Region: | |
for _ in range(len(args.axis)): | |
region.VarRegionAxis.append(buildVarRegionAxis((0, 0, 0))) | |
if args.output: | |
font.save(args.output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment