-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathChineseFont.py
48 lines (41 loc) · 1.52 KB
/
ChineseFont.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
class ChineseFont:
def __init__(self, file):
# open data file
self.__db_file = open(file, 'rb')
self.__load_map()
def get_font_size(self):
return self.__font_size
def is_exist(self, key):
return key in self.__map
def get_font_count(self):
return self.__font_count
# get bit map
def get_bit_map(self, key):
if not self.is_exist(key):
return bytearray()
sort = self.__map[key]
self.__db_file.seek(2 + 4 + int((self.__font_size * self.__font_size) / 8) * sort)
return self.__db_file.read(int((self.__font_size * self.__font_size) / 8))
def close(self):
self.__db_file.close()
def __load_map(self):
# font_size
font_size_byte = self.__db_file.read(2)
self.__font_size = int.from_bytes(font_size_byte, 'big')
# font_count
font_count_byte = self.__db_file.read(4)
self.__font_count = int.from_bytes(font_count_byte, 'big')
# seek
self.__db_file.seek(int((self.__font_size * self.__font_size * self.__font_count) / 8) + 4 + 2)
# load map
self.__map = {}
while True:
key_len = self.__db_file.read(1)
if key_len == b'':
break
if(key_len[0] == 1):
key = self.__db_file.read(1)
else:
key = self.__db_file.read(3)
data = self.__db_file.read(4)
self.__map[key.decode('utf-8')] = int.from_bytes(data, 'big')