-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfont-to-text.py
executable file
·63 lines (56 loc) · 1.75 KB
/
font-to-text.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
#!/usr/bin/python
# coding=utf8
import binascii,sys
if __name__=='__main__':
if len(sys.argv)==3:
file_i=sys.argv[1] # исходный файл
file_o=sys.argv[2] # файл шрифта
else:
print ('Не заданы параметры!')
print ('Пример: '+sys.argv[0]+' file.bin font.txt')
sys.exit(1)
# Переменные
offset='1BBF' # смещение
length=2048 # длина
table=[]; # таблица
line_end='\r\n' # конец строки
replace_0='.' # замена для 0
replace_1='@' # замена для 1
offset_text='Смещение'
code_text='Код'
# Чтение исходного файла
file_in=open(file_i,'rb')
data_out=b''
file_in.seek(int(offset,base=16))
data_in=file_in.read(1)
while length:
code_hex=binascii.b2a_hex(data_in)
code_hex=code_hex.upper()
table.append(code_hex);
length=length-1
data_in=file_in.read(1)
file_in.close()
# Шрифт
s=0
for i in range(0,2047,8):
i_hex=('{0:x}'.format(int(s))).upper()
s_hex=('{0:x}'.format(int(s*8))).upper()
if s<=16:
i_hex='0x0'+i_hex
else:
i_hex='0x'+i_hex
if sys.version_info[0] < 3:
# Python 2
data_out=data_out+offset_text+': '+'0x'+str(s_hex)+' ('+str (s*8)+ ') '+code_text+': '+i_hex+' ('+str(s)+')'+line_end+'_12345678'+line_end
else:
# Python 3
data_out=data_out+(offset_text+': '+'0x'+str(s_hex)+' ('+str (s*8)+ ') '+code_text+': '+i_hex+' ('+str(s)+')'+line_end+'_12345678'+line_end).encode()
for t in range (0,8):
string_of_pixels=((bin(int(table[i+t],16))[2:].zfill(8)).replace('0',replace_0)).replace('1',replace_1)
data_out=data_out+(str(t+1)+string_of_pixels+line_end).encode()
data_out=data_out+line_end.encode()
s=s+1
# Запись в файл
file_out=open(file_o,'wb')
file_out.write(data_out)
file_out.close()