-
Notifications
You must be signed in to change notification settings - Fork 8
/
dumpkerning.py
executable file
·73 lines (57 loc) · 1.95 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
'''
Wrapper script for all the getKerningPairsFromXXX scripts.
'''
from getKerningPairsFromFEA import FEAKernReader
from getKerningPairsFromOTF import OTFKernReader
from getKerningPairsFromUFO import UFOkernReader
from pathlib import Path
import defcon
import argparse
def dumpKerning(kernDict, fileName):
output = [f"{g_1} {g_2} {value}" for (g_1, g_2), value in sorted(kernDict.items())]
with open(fileName, "w") as blob:
blob.write('\n'.join(output))
def extractKerning(input_file):
if input_file.suffix in [".ttf", ".otf"]:
otfKern = OTFKernReader(input_file)
return otfKern.kerningPairs
elif input_file.suffix == ".ufo":
ufoKern = UFOkernReader(defcon.Font(input_file), includeZero=True)
return ufoKern.allKerningPairs
else:
# assume .fea
feaOrgKern = FEAKernReader(input_file)
return feaOrgKern.flatKerningPairs
def get_args(args=None):
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 file(s) to extract kerning from'
)
parser.add_argument(
'-o', '--output',
dest='outputDir'
)
return parser.parse_args(args)
def main(args=None):
args = get_args(args)
for source in args.sourceFiles:
input_file = Path(source)
new_suffix = input_file.suffix + ".kerndump"
output_file = input_file.with_suffix(new_suffix)
print(f"extracting kerning from {input_file.name}")
kerning = extractKerning(input_file)
if args.outputDir:
output_dir = Path(args.outputDir)
output_dir.mkdir(exist_ok=True)
output_file = output_dir / output_file.name
dumpKerning(kerning, output_file)
if __name__ == "__main__":
main()