Skip to content

Instantly share code, notes, and snippets.

@xen
Last active August 29, 2015 13:56
Show Gist options
  • Save xen/9059759 to your computer and use it in GitHub Desktop.
Save xen/9059759 to your computer and use it in GitHub Desktop.
Font ascents/descents fixes
#!/usr/bin/python
# -*- coding: utf-8 -*-
from fontTools import ttLib
def writeFont(font, filename):
font.save(filename)
print "Wrote font to", filename
def set_metrics(filename, ascents, descents, linegaps):
font = ttLib.TTFont(filename)
font['hhea'].ascent = ascents
font['hhea'].ascent = descents
font['hhea'].lineGap = linegaps
writeFont(font, filename)
def fix_metrics(filename):
font = ttLib.TTFont(filename)
# Code here
writeFont(font, filename)
def show_metrics(filename):
font = ttLib.TTFont(filename)
print(font['hhea'].ascent)
print(font['hhea'].descent)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
# $ python pyft-vertical-metrics.py font.ttf;
# 1000
# -200
# $ python pyft-vertical-metrics.py --ascents 2189 --descents 600 --linegaps 0 font.ttf;
# $ python pyft-vertical-metrics.py -a 2189 -d 600 -l 0 font.ttf;
# $ python pyft-vertical-metrics.py font.ttf;
# 2189
# -600
parser.add_argument('-a', '--ascents', type=int, help="Set new ascents value in 'Horizontal Header' table ('hhea')")
parser.add_argument('-d', '--descents', type=int, help="Set new descents value in 'Horizontal Header' table ('hhea')")
parser.add_argument('-l', '--linegaps', type=int, help="Set new linegaps value in 'Horizontal Header' table ('hhea')")
parser.add_argument('--autofix', type=int, help="Autofix font metrics, overite file")
parser.add_argument('filename', help="Font file in TTF format")
args = parser.parse_args()
if any([args.ascents, args.descents, args.linegaps]):
set_metrics(args.filename, args.ascents, args.descents, args.linegaps)
elif args.autofix:
fix_metrics(args.filename)
else:
show_metrics(args.filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment