Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Dev/devfont_to_hpp.py
1168 views
1
# Requires the Pillow dependency
2
import sys, math, itertools
3
from PIL import Image
4
from pathlib import Path
5
6
WIDTH = 8
7
HEIGHT = 8
8
CHARCOUNT = 128
9
COLCOUNT = 16
10
11
IMGWIDTH = (COLCOUNT if CHARCOUNT >= COLCOUNT else CHARCOUNT) * WIDTH
12
IMGHEIGHT = math.ceil(CHARCOUNT / COLCOUNT) * HEIGHT
13
14
def encode_font(image_path):
15
print(f"Opening {image_path}...")
16
17
img = Image.open(image_path).convert("1")
18
assert img.size == (IMGWIDTH, IMGHEIGHT), f"Image does not match expected size ({IMGWIDTH}x{IMGHEIGHT})"
19
20
print("Encoding pixel data...")
21
data = list()
22
23
row = 0
24
col = 0
25
for char in range(CHARCOUNT):
26
h = row * HEIGHT
27
for y in range(h, h + HEIGHT):
28
for x in range(WIDTH):
29
data.append("0x01" if img.getpixel((x + (col * WIDTH), y)) else "0x00")
30
col += 1
31
if col >= COLCOUNT:
32
col = 0
33
row += 1
34
35
return data
36
37
def main():
38
assert len(sys.argv) > 1, "No input file given"
39
40
img_file = Path(sys.argv[1])
41
data = encode_font(img_file)
42
43
# Using img_file.with_name() will save it in the same directory as img_file
44
output_file = Path(sys.argv[2]) if len(sys.argv) > 2 else img_file.with_name(img_file.stem + ".hpp")
45
print(f"Writing to {output_file}...")
46
try:
47
with output_file.open("w") as hpp_file:
48
hpp_file.write(f"#ifndef DEVFONT_H\n#define DEVFONT_H\n\n")
49
if CHARCOUNT == 128:
50
hpp_file.write("// First 128 characters of Code page 437 on old IBM computers\n")
51
hpp_file.write("uint8 devTextStencil[] = {\n ")
52
53
# Print the list while dividing it into lines of 24 values
54
array = []
55
for row in itertools.batched(data, 24):
56
array.append(", ".join(pixel for pixel in row) + ",")
57
hpp_file.write("\n ".join(array)[:-1])
58
59
hpp_file.write("\n};\n\n")
60
hpp_file.write("#endif")
61
62
print("Done!")
63
except Exception as e:
64
print(f"[ERROR] {e}")
65
66
if __name__ == "__main__":
67
main()
68
69