-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpy_font_test.py
151 lines (125 loc) · 4.46 KB
/
py_font_test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# encoding='utf-8'
from functools import cache
import io
import sys
import re
import time
import datetime
import os
import math
import random
import numpy as np
FONT_TEST_STR = "我的祖国繁荣昌盛"
FONT_BIN_FILE = "simsun.ttc-16.bin"
FONT_WIDTH = 32
FONT_HEIGHT = 32
FONT_START_CODE = 0x4E00
FONT_END_CODE = 0x9FFF
FONT_WIDTH_B = math.ceil(FONT_WIDTH / 8)
FONT_BIN_OFFSET = 0x4E00
FONT_HEADER_SIZE = (4 * 6) # sizeof(int32) * 6
# 8 * 64 byte, 64x64 bit
FONT_BYTE_SIZE = (FONT_WIDTH_B * FONT_HEIGHT)
def read_font_header():
global FONT_WIDTH
global FONT_HEIGHT
global FONT_BIN_OFFSET
global FONT_START_CODE
global FONT_END_CODE
global FONT_WIDTH_B
global FONT_BYTE_SIZE
'''
typedef struct font_bin_s
{
int32_t flag; //file flag, "FONT"
int32_t width; //font width, 64
int32_t height; //font height, 64
int32_t start; //font start, 0x4E00
int32_t end; //font end, 0x9FA5
int32_t mode; //font mode, 1
} font_bin_t;
'''
with open(FONT_BIN_FILE, 'rb') as f:
f.seek(0) # seek to header
rbuff = f.read(FONT_HEADER_SIZE)
flag = str(rbuff[0:4])
width = int.from_bytes(rbuff[4:8], byteorder='little')
height = int.from_bytes(rbuff[8:12], byteorder='little')
start = int.from_bytes(rbuff[12:16], byteorder='little')
end = int.from_bytes(rbuff[16:20], byteorder='little')
mode = int.from_bytes(rbuff[20:24], byteorder='little')
# print("flag: [%s]" % flag)
# print("width: [%d]" % width)
# print("height: [%d]" % height)
# print("start: [0x%X]" % start)
# print("end: [0x%X]" % end)
# print("mode: [%d]" % mode)
if flag != "b'FONT'" or width <=8 or height <=8:
print("font file header error")
return False
FONT_WIDTH = width
FONT_HEIGHT = height
FONT_BIN_OFFSET = start
FONT_START_CODE = start
FONT_END_CODE = end
FONT_WIDTH_B = math.ceil(FONT_WIDTH / 8)
FONT_BYTE_SIZE = (FONT_WIDTH_B * FONT_HEIGHT)
print("font width: [%d]" % FONT_WIDTH)
print("font height: [%d]" % FONT_HEIGHT)
print("font bin offset: [0x%X]" % FONT_BIN_OFFSET)
print("font start code: [0x%X]" % FONT_START_CODE)
print("font end code: [0x%X]" % FONT_END_CODE)
print("font width byte: [%d]" % FONT_WIDTH_B)
print("font byte size: [%d]" % FONT_BYTE_SIZE)
return True
def read_font_buff(fcode):
with open(FONT_BIN_FILE, 'rb') as f:
fseek = fcode - FONT_BIN_OFFSET
fseek = fseek * FONT_BYTE_SIZE
fseek = fseek + FONT_HEADER_SIZE # skip header
f.seek(fseek) # seek to font data
rbuff = f.read(FONT_BYTE_SIZE)
return rbuff
def show_font_byte(fbuff):
byten = 0
# for y in range(FONT_HEIGHT):
# print("%02X: " % y, end='')
# for x in range(FONT_WIDTH_B):
# bytei = y * FONT_WIDTH_B + x
# print("%02X " % fbuff[bytei], end='')
# print("")
for y in range(FONT_HEIGHT):
print("%02X: " % y, end='')
for x in range(FONT_WIDTH_B):
bytei = y * FONT_WIDTH_B + x
byten = fbuff[bytei]
for x0 in range(8):
col = byten & (1 << (7 - x0))
if col != 0:
print("*", end="")
else:
print(" ", end="")
print("")
if __name__ == "__main__":
if read_font_header() != True:
print("font header error")
exit(0)
# FONT_TEST_BYTE = []
# print("Font Test: {0}".format(FONT_TEST_STR))
# for s in FONT_TEST_STR:
# FONT_TEST_BYTE.append(ord(s))
FONT_TEST_BYTE = [x for x in range(FONT_START_CODE, FONT_END_CODE + 1)]
# print(FONT_TEST_BYTE)
findex = 0
fstring = ''
for fcode in FONT_TEST_BYTE:
try:
fbytes = fcode.to_bytes(2, byteorder='big')
fstring = fbytes.decode('utf-16-be')
print("============================== idx[%d] code[%X] chr[%s] ==============================" % (findex, fcode, fstring))
except:
fstring = 'XX'
print("============================== idx[%d] code[%X] chr[%s] ==============================" % (findex, fcode, fstring))
fbuff = read_font_buff(fcode)
show_font_byte(fbuff)
findex = findex + 1