Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Created August 15, 2025 16:10
Show Gist options
  • Select an option

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

Select an option

Save anthrotype/f637d268250295c87a7821e0fbfce10c to your computer and use it in GitHub Desktop.
Use fonttools to draw a variable glyph's advance width at a given location
import sys
from fontTools.pens.basePen import NullPen
from fontTools.ttLib import TTFont
if len(sys.argv) < 3:
print("usage: draw_glyph_advance.py fontfile.ttf glyphname [tag=value ...]")
sys.exit(1)
input_file, glyph_name = sys.argv[1:3]
print(f"glyph: {glyph_name}")
font = TTFont(input_file)
print("HVAR is present:", "yes" if "HVAR" in font else "no")
fvar = font["fvar"]
location = {a.axisTag: 0.0 for a in fvar.axes}
# axis coordinates format: 'tag=value'
for arg in sys.argv[3:]:
tag, value = arg.split("=")
if tag not in location:
sys.exit(f"unknown axis tag: {tag}")
location[tag] = float(value)
print(f"location: {location}")
glyph_set = font.getGlyphSet(location=location, normalized=True)
glyph = glyph_set[glyph_name]
pen = NullPen()
glyph.draw(pen)
print(f"width: {glyph.width}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment