-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzpl_font_encoder.py
75 lines (61 loc) · 2 KB
/
zpl_font_encoder.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
import base64
import zlib
class ZebraFontEncoder:
@staticmethod
def crc16(data: bytes, poly=0x8408):
"""
CRC-16-CCITT Algorithm
"""
data = bytearray(data)
crc = 0xFFFF
for b in data:
cur_byte = 0xFF & b
for _ in range(0, 8):
if (crc & 0x0001) ^ (cur_byte & 0x0001):
crc = (crc >> 1) ^ poly
else:
crc >>= 1
cur_byte >>= 1
crc = ~crc & 0xFFFF
crc = (crc << 8) | ((crc >> 8) & 0xFF)
bytes_ = crc & 0xFFFF
return f"{bytes_:#06x}"[2:]
def generate_command(
self,
font_path,
font_filename,
font_extension="TTF",
output_path="out.txt",
method="ascii",
):
with open(font_path, "rb") as f:
content = f.read()
encoder = {
"ascii": self.ascii_encode,
"b64": self.base64_encode,
"z64": self.z64_encode,
}[method]
data = encoder(content)
if method == "ascii":
head = f"~DUR:{font_filename}.{font_extension}"
else:
head = f"~DT{font_filename}"
output = f"{head},{len(content)},{data}"
with open(output_path, "w") as f:
f.write(output)
def ascii_encode(self, content):
return content.hex().upper()
def base64_encode(self, content):
base64_bytes = base64.b64encode(content)
base64_string = base64_bytes.decode("ascii")
crc = self.crc16(base64_bytes)
return f":B64:{base64_string}:{crc}"
def z64_encode(self, content):
compressed = zlib.compress(content)
base64_bytes = base64.b64encode(compressed)
base64_string = base64_bytes.decode("ascii")
crc = self.crc16(base64_bytes)
return f":Z64:{base64_string}:{crc}"
if __name__ == "__main__":
fonts = ZebraFontEncoder()
fonts.generate_command("./ROBOTO.TTF", font_filename="ROBOTO", method="ascii")