forked from adobe-type-tools/kern-dump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdumpkerning.py
executable file
·53 lines (43 loc) · 1.68 KB
/
dumpkerning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
from __future__ import print_function
import os
import defcon
from getKerningPairsFromFEA import FEAKernReader
from getKerningPairsFromOTF import OTFKernReader
from getKerningPairsFromUFO import UFOkernReader
def dumpKerning(kernDict, fileName):
f = open(fileName, "w")
for (g1, g2), v in sorted(kernDict.items()):
f.write("%s %s %s\n" % (g1, g2, v))
f.close()
def extractKerning(path):
path = os.path.normpath(path) # remove trailing slash for .ufo
base, ext = os.path.splitext(path)
ext = ext.lower()
if ext in [".ttf", ".otf"]:
otfKern = OTFKernReader(path)
return otfKern.kerningPairs
elif ext == ".ufo":
ufoKern = UFOkernReader(defcon.Font(path), includeZero=True)
return ufoKern.allKerningPairs
else:
# assume .fea
feaOrgKern = FEAKernReader([path])
return feaOrgKern.flatKerningPairs
def main(args):
import argparse
parser = argparse.ArgumentParser(description='Extract (flat) kerning from ufo, ttf, otf or fea and write it to a text file.')
parser.add_argument('sourceFiles', nargs='+', metavar="SOURCE",
help='source files to extract kerning from')
parser.add_argument('-o', '--output', dest='outputFolder')
args = parser.parse_args(args)
for source in args.sourceFiles:
print("extracting kerning from %s" % source)
kerning = extractKerning(source)
output = os.path.normpath(source) + ".kerndump"
if args.outputFolder:
output = os.path.join(args.outputFolder, os.path.basename(output))
dumpKerning(kerning, output)
if __name__ == "__main__":
import sys
main(sys.argv[1:])