-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-font-mapping.py
executable file
·78 lines (57 loc) · 1.71 KB
/
generate-font-mapping.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
74
75
76
77
78
#!/usr/bin/python3
from argparse import ArgumentParser
from collections import OrderedDict
import yaml
from fontTools.ttLib import TTFont
def represent_dictionary_order(self, dict_data):
return self.represent_mapping('tag:yaml.org,2002:map', dict_data.items())
def setup_yaml():
yaml.add_representer(OrderedDict, represent_dictionary_order)
setup_yaml()
def is_character_valid(char):
order = ord(char)
return 31 < order < 127 or order > 159
def get_font_characters(name, input_file, output_file):
with TTFont(input_file) as font:
mapping = [
chr(y[0])
for x in font["cmap"].tables
for y in x.cmap.items()
]
characters = {
x: ''
for x in mapping
if is_character_valid(x)
}
with open(output_file, 'w', encoding='utf-8') as file:
yml = OrderedDict()
yml['name'] = name
yml['v'] = '1.0.1'
yml['charmap'] = characters
yaml.dump(yml, file, allow_unicode=True)
def default_args():
default_parser = ArgumentParser()
default_parser.add_argument(
"-f", "--font",
help="Font Name",
required=True
)
default_parser.add_argument(
"-s", "--source",
help="Font source file",
required=True
)
default_parser.add_argument(
"-d", "--destination",
help="Destination mapping file",
required=True
)
return default_parser.parse_args()
def main():
parsed_args = default_args()
name = parsed_args.font
input_file = parsed_args.source
output_file = parsed_args.destination
get_font_characters(name, input_file, output_file)
if __name__ == '__main__':
main()