forked from cpitclaudel/monospacifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
allchars.py
executable file
·78 lines (68 loc) · 2.33 KB
/
allchars.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/env python3
# -*- coding: utf-8 -*-
# pylint: disable=too-few-public-methods
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Find all glyphs covered by a given font.
Example use: ./allchars.py test.ttf`"""
import sys
try:
import fontforge
except ImportError:
print("This program requires FontForge's python bindings:")
print(" git clone https://github.com/fontforge/fontforge")
print(" cd fontforge")
print(" mkdir build; cd build")
print(" cmake -GNinja .. -Wno-dev")
print(" ninja")
print(" sudo ninja install")
print(" sudo ldconfig")
raise
def supported_chars(path):
ft = fontforge.open(path)
try:
for glyph in ft.glyphs():
if glyph.unicode >= 0:
yield chr(glyph.unicode)
finally:
ft.close()
def charmap(chars, width=80):
buf = []
for char in chars:
buf.append(char)
if len(buf) == width:
yield u"".join(buf)
buf = []
if len(buf) > 0:
yield u"".join(buf)
def compare(f1, f2):
gl1, gl2 = set(supported_chars(f1)), set(supported_chars(f2))
only1, only2 = (sorted(gl1 - gl2), sorted(gl2 - gl1))
# print("{} has {} glyphs".format(f1, len(gl1)))
# print("{} has {} glyphs".format(f2, len(gl2)))
print("\n# Only in {}:".format(f1))
print("\n".join(charmap(only1)))
print("\n# Only in {}:".format(f2))
print("\n".join(charmap(only2)))
def main():
if len(sys.argv) == 2:
print("\n".join(charmap(supported_chars(sys.argv[1]))))
elif len(sys.argv) == 3:
compare(sys.argv[1], sys.argv[2])
else:
print("Needs 1-2 arguments (paths to font files)")
if __name__ == '__main__':
main()
# Local Variables:
# python-shell-interpreter: "python3"
# End: