-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglyphgrep.py
More file actions
80 lines (67 loc) · 2.38 KB
/
Copy pathglyphgrep.py
File metadata and controls
80 lines (67 loc) · 2.38 KB
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
79
80
"""Search font files to find those containing a particular glyph."""
import re
import argparse
import pathlib
import logging
from fontTools.ttLib import TTFont, TTCollection
def _find_glyph(path, glyph):
if path.suffix == '.ttc':
fonts = TTCollection(path)
else:
fonts = [TTFont(path)]
for n, font in enumerate(fonts):
def _inspect():
for table in font['cmap'].tables:
if glyph in table.cmap:
return (n,
(table.platformID, table.platEncID),
table.cmap[glyph])
result = _inspect()
if result is not None:
yield result
def _parse_codepoint(string):
lower = string.lower()
if lower.startswith('u+'):
_, hex_part = lower.split('+')
return int(hex_part, base=16)
elif re.match(r'[0-9]+', lower):
return int(lower)
elif len(lower) == 1:
return ord(lower)
else:
raise argparse.ArgumentTypeError(
f'Could not interpret codepoint: {string}')
def _main():
parser = argparse.ArgumentParser(
description='Search font files for a glyph')
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--recursive', '-R', action='store_true')
parser.add_argument('codepoint', type=_parse_codepoint)
parser.add_argument('file', nargs='+')
args = parser.parse_args()
logging.getLogger('fontTools').setLevel(
logging.DEBUG if args.verbose else logging.ERROR)
to_scan = []
for path in args.file:
path = pathlib.Path(path)
if path.is_dir():
if args.recursive:
for ext in ['*.ttf', '*.otf', '*.ttc']:
to_scan.extend(path.rglob(ext))
else:
raise Exception(f'Path is not a file: {path}')
elif path.is_file():
to_scan.append(path)
for path in to_scan:
for font_num, flavor, description in _find_glyph(path, args.codepoint):
flavor_txt = f'platformID {flavor[0]}; platEncId {flavor[1]}'
if path.suffix == '.ttc':
print(f'{path} (font {font_num}; {flavor_txt}):',
'{description}')
else:
print(f'{path} ({flavor_txt}): {description}')
if __name__ == '__main__':
try:
_main()
except KeyboardInterrupt:
pass