Created
August 15, 2025 16:10
-
-
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
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 | |
| 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