OSDN Git Service

update at 12/23/2018
[sawarabi-fonts/sawarabi-fonts.git] / script / fontparser.py
1 # -*- coding: utf-8 -*-
2
3 # Author: mshio <mshio@users.sourceforge.jp>
4
5 __version__ = '0.11'
6
7 import fontforge
8 from types import MethodType
9
10
11 class FontParser:
12     """
13     An object in order to parse the characters in the specified font.
14     """
15     def __init__(self, font_path):
16         self.forge = fontforge.open(font_path)
17
18     def parse(self, method):
19         self.proc = MethodType(method, self, FontParser)
20         f = self.forge
21         for g in f:
22             if g[0] != '.' and f[g].unicode > 0:
23                 self.proc(f[g])
24
25 class FontDiffParser:
26     def __init__(self, old_font_path, new_font_path):
27         self.font_path = [old_font_path, new_font_path]
28
29     def get_diff(self):
30         buf = []
31         old = fontforge.open(self.font_path[0])
32
33         def collect(self, glyph_obj):
34             if glyph_obj.glyphname not in old:
35                 buf.append(glyph_obj.unicode)
36
37         FontParser(self.font_path[1]).parse(collect)
38
39         return buf
40
41 class KanjiParser:
42     def __init__(self, font_path):
43         self.font_path = font_path
44
45     def get_list(self):
46         buf = []
47
48         def collect(self, glyph):
49             code = glyph.unicode
50             # roughly
51             if code >= 0x4e00 and code <= 0x9fff:
52               buf.append(code)
53
54         FontParser(self.font_path).parse(collect)
55
56         return buf
57