OSDN Git Service

convert all of fontparser classes into functions
[sawarabi-fonts/sawarabi-fonts.git] / script / new_glyphs.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # $Id$
5 # Author: mshio <mshio@users.osdn.me>
6
7 __version__ = '0.12'
8
9 import argparse
10 import os
11 import tarfile
12 from fontparser import font_diff
13 from listprinter import SimpleListPrinter
14
15
16 def print_new_glyph(archive, newfile, printer=None):
17   def fontfilepath_in_archive(archive_path, weight, ext='ttf'):
18     file_name = archive_path.split('/')[-1]
19     family_name, type_name, _ = file_name.split('-', 2)
20
21     return '{family}-{type_name}/{family}-{type_name}-{weight}.{ext}'.format(
22       family=family_name, type_name=type_name, weight=weight, ext=ext
23     )
24
25   def expand_archive(archive_path, fontfile_path):
26     f = tarfile.open(archive_path, 'r:gz')
27     f.extract(fontfile_path)
28
29   def clean_up(fontfile_path):
30     os.remove(fontfile_path)
31     os.rmdir(fontfile_path.split('/')[0])
32
33   def main(archive, newfile, printer):
34     font_path = fontfilepath_in_archive(archive, 'medium')
35     expand_archive(archive, font_path)
36
37     diff = font_diff(font_path, newfile)
38     clean_up(font_path)
39
40     p = printer
41     if p == None: p = SimpleListPrinter(delimiter=0x3001)
42     p.output(diff)
43     print >>p.out
44     print >>p.out, "%d char(s)" % len(diff)
45
46   main(archive, newfile, printer)
47
48 def parse_args():
49   parser = argparse.ArgumentParser()
50   parser.add_argument('archive_file', help='path of an archived font file')
51   parser.add_argument('font_file', help='path of a font file')
52
53   return parser.parse_args()
54
55 if __name__ == '__main__':
56   args = parse_args()
57   print_new_glyph(args.archive_file, args.font_file)