OSDN Git Service

adds new_glyphs.py
[sawarabi-fonts/sawarabi-fonts.git] / script / new_glyphs.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import fontforge
5 import os
6 import sys
7 import tarfile
8
9 def get_fontfilename_in_archive(string):
10   name = string.split('/')[-1]
11   p = name.split('-')
12   family = p[0]
13   type = p[1]
14
15   return '%s-%s/%s-%s-medium.ttf' % (family, type, family, type)
16
17 def get_glyph_list(fontfile):
18   ret = []
19   font = fontforge.open(fontfile)
20   for g in font:
21     glyph = font[g]
22     ret.append(glyph.unicode)
23   font.close()
24   return ret
25
26 def remove_dep_item(list1, list2):
27   for l in list1:
28     list2.remove(l)
29
30   return list2
31
32 def print_result_list(list):
33   out = sys.stdout
34   col = 0
35   dec = 0
36   for n in list:
37     if n < 0 or n > sys.maxunicode:
38       continue
39     if col != 0:
40       out.write('、')
41     col = col + 1
42     out.write(unichr(n).encode('utf-8'))
43     if col == 10:
44       col = 0
45       dec = dec + 1
46       out.write('\n')
47   if col != 0:
48     out.write('\n')
49   out.write('%d chars\n' % (dec * 10 + col))
50
51
52 if __name__ == '__main__':
53   if len(sys.argv) != 3:
54     print 'usage: %s tar.gz-file font-file' % sys.argv[0]
55     sys.exit(1)
56
57   archive = sys.argv[1]
58   newfile = sys.argv[2]
59
60   fontfile = get_fontfilename_in_archive(archive)
61   f = tarfile.open(archive, 'r:gz')
62   f.extract(fontfile)
63
64   oldglyphs = get_glyph_list(fontfile)
65   newglyphs = get_glyph_list(newfile)
66
67   result = remove_dep_item(oldglyphs, newglyphs)
68   print_result_list(result)
69
70   os.remove(fontfile)
71   os.rmdir(fontfile.split('/')[0])