Replies: 5 comments
-
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._f_v_a_r import Axis
import fontTools.ttLib.tables.TupleVariation as tv
import argparse
TupleVariation = tv.TupleVariation
parser = argparse.ArgumentParser(description="Add a TRAK axis")
parser.add_argument("-o", "--output", help="output font file")
parser.add_argument("font", help="font to add the axis to")
args = parser.parse_args()
font = TTFont(args.font)
if "fvar" not in font:
raise Exception("Font does not have fvar table")
var = font["gvar"].variations
for glyphname, value in var.items():
glyph = font["glyf"][glyphname]
numPointsInGlyph = font["gvar"].getNumPoints_(glyph)
minusValue = TupleVariation(
{"TRAK": (-1.0, -1.0, 0.0)}, [(0, 0)] * numPointsInGlyph
)
minusValue.coordinates[-3] = (-font["head"].unitsPerEm, 0)
plusValue = TupleVariation({"TRAK": (0.0, 1.0, 1.0)}, [(0, 0)] * numPointsInGlyph)
plusValue.coordinates[-3] = (font["head"].unitsPerEm, 0)
value.append(minusValue)
value.append(plusValue)
# HVAR is horrible and I hate it.
if "HVAR" in font:
del font["HVAR"]
# Add axis at the end after gvar is decompiled
trak = Axis()
trak.axisTag = "TRAK"
trak.minValue, trak.defaultValue, trak.maxValue = -1000, 0, 1000
trak.axisNameID = font["name"].addName("Tracking")
fvar = font["fvar"]
fvar.axes.append(trak)
# Each named instance needs to add the axis too, even though we're not using it there.
for instance in fvar.instances:
instance.coordinates["TRAK"] = 0
font.save(args.output) |
Beta Was this translation helpful? Give feedback.
-
That was a good chuckle. |
Beta Was this translation helpful? Give feedback.
-
excellent, thank you! |
Beta Was this translation helpful? Give feedback.
-
I’ll briefly clarify why this is useful: with instancer, I can this way »freeze« letter-spacing into an instance. And remix with other axes (wght, wdth for example) to simulate opsz :) |
Beta Was this translation helpful? Give feedback.
-
The snippet works, the output font works in Samsa & FontView & FontGoggles, but in Safari & Chrome changing any variations makes the glyphs disappear. |
Beta Was this translation helpful? Give feedback.
-
Could someone perhaps write a snippet that shows how to add a
TRAK
axis to an existing VF?-head.unitsPerEm
+head.unitsPerEm
gvar
-based (TT) fontsfvar
tag should beTRAK
and the name should beTracking
fvar
instancesI imagine that this could be done in quite optimized way, but I’m not sure how to approach this. I think it would be generally useful, and it’s something that is best added onto a finished font rather than on the source level.
Beta Was this translation helpful? Give feedback.
All reactions