Skip to content

Instantly share code, notes, and snippets.

@davelab6
Forked from xen/pyft-vertical-metrics.py
Last active August 29, 2015 13:56
Show Gist options
  • Save davelab6/9061745 to your computer and use it in GitHub Desktop.
Save davelab6/9061745 to your computer and use it in GitHub Desktop.
#!/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['OS/2'].sTypoAscender = ascents
font['OS/2'].usWinAscent = ascents
font['hhea'].descent = descents
font['OS/2'].sTypoDescender = descents
font['OS/2'].usWinDescent = descents
font['hhea'].lineGap = linegaps
font['OS/2'].sTypoLineGap = 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("hhea asc:" + font['hhea'].ascent)
print("OS/2 asc:" + font['OS/2'].sTypoAscender)
print("OS/2 asc:" + font['OS/2'].usWinAscent)
print("hhea des:" + font['hhea'].descent)
print("OS/2 des:" + font['OS/2'].sTypoDescender)
print("OS/2 des:" + font['OS/2'].usWinDescent)
print("hhea lng:" + font['hhea'].lineGap)
print("OS/2 lng:" + font['OS/2'].sTypoLineGap)
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