Files
gui/tools/gen_vt_data.py
T

141 lines
5.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 D-Modem Authors
"""
Writes core/src/vt_data.c: the terminal emulator's data from the D-Modem
app's (D-Modem/app, shared/src/commonMain/kotlin/org/dmodem/app/terminal):
the home computers' character sets and Unscii's 8x8 and 8x16 glyphs
(LegacyCharsets.kt), the machines' character ROMs as SyncTERM draws them
(RomFonts.kt: Synchronet ciolib's, LGPL-2.0-or-later) and teletext's
mosaics (Mosaic.kt's, drawn the same way).
python3 tools/gen_vt_data.py APP_TERMINAL_DIR > core/src/vt_data.c
"""
import re
import sys
SETS = [
("C64_UPPER", "vt_set_c64_upper"), ("C64_LOWER", "vt_set_c64_lower"),
("VIC_UPPER", "vt_set_vic_upper"), ("VIC_LOWER", "vt_set_vic_lower"),
("PET_UPPER", "vt_set_pet_upper"), ("PET_LOWER", "vt_set_pet_lower"),
("ATARI_GRAPHICS", "vt_set_atari_graphics"),
("ATARI_INTERNATIONAL", "vt_set_atari_international"),
]
# RomFonts.kt's, in the order of vtint.h's ROM_* (SyncTERM's numbers)
ROMS = ["VGA", "VGA_THIN", "C64_UPPER", "C64_LOWER", "C128_UPPER",
"C128_LOWER", "ATARI", "POT_NOODLE", "MOSOUL", "MICROKNIGHT_PLUS",
"TOPAZ_PLUS", "MICROKNIGHT", "TOPAZ", "PRESTEL", "ATARI_ST",
"RIPTERM"]
def int_array(src, name):
m = re.search(r"val %s: IntArray = intArrayOf\((.*?)\)" % name, src, re.S)
v = [int(x, 16) for x in re.findall(r"0x([0-9A-Fa-f]+)", m.group(1))]
assert len(v) == 256, name
return v
def bitmap(src, name, height):
m = re.search(r"val %s: BitmapFont = BitmapFont.parse\(\s*%d,(.*?)\n \)" % (name, height), src, re.S)
body = "".join(re.findall(r'"([^"]*)"', m.group(1)))
out = []
for e in body.split(";"):
if not e:
continue
cp, rows = e.split(":")
out.append((int(cp, 16), [int(rows[2 * r:2 * r + 2], 16) for r in range(height)]))
out.sort()
return out
def rom(src, name):
m = re.search(r"val %s: RomFont = RomFont\((.*?)\n \)" % name, src, re.S)
sizes = {}
for h, body in re.findall(r"h(8|16) =\s*((?:\s*\"[0-9a-f]*\"\s*\+?)+)", m.group(1)):
hexs = "".join(re.findall(r'"([0-9a-f]*)"', body))
assert len(hexs) == 256 * int(h) * 2, (name, h)
sizes[int(h)] = bytes.fromhex(hexs)
return sizes
def mosaic():
out = []
for sep in (False, True):
for mask in range(1, 64):
rows = []
for y in range(9):
band = y // 3
r = 0
if not (sep and y % 3 == 2):
if mask & (1 << (band * 2)):
r |= 0b110000 if sep else 0b111000
if mask & (1 << (band * 2 + 1)):
r |= 0b000110 if sep else 0b000111
rows.append(r)
out.append((0xf1000 + mask + (0x40 if sep else 0), rows))
return out
def emit_bytes(name, data, static=True):
print("%sconst uint8_t %s[%d] = {" % ("static " if static else "", name, len(data)))
for k in range(0, len(data), 16):
print("\t" + ", ".join("0x%02x" % b for b in data[k:k + 16]) + ",")
print("};")
def emit_font(cname, font, w, h):
print("static const uint32_t %s_codes[%d] = {" % (cname, len(font)))
for k in range(0, len(font), 8):
print("\t" + ", ".join("0x%x" % cp for cp, _ in font[k:k + 8]) + ",")
print("};")
emit_bytes(cname + "_rows", [b for _, rows in font for b in rows])
print("const struct vt_font %s = { %d, %d, %d, %s_codes, %s_rows, NULL };\n"
% (cname, w, h, len(font), cname, cname))
def main():
d = sys.argv[1]
legacy = open(d + "/LegacyCharsets.kt", encoding="utf-8").read()
roms = open(sys.argv[2] if len(sys.argv) > 2 else d + "/RomFonts.kt", encoding="utf-8").read()
print("/*")
print(" * SPDX-License-Identifier: AGPL-3.0-only AND LGPL-2.0-or-later")
print(" * Generated by tools/gen_vt_data.py from the D-Modem app's")
print(" * LegacyCharsets.kt, RomFonts.kt and Mosaic.kt: don't edit by hand.")
print(" *")
print(" * The sets are Rebecca Bettencourt's Legacy Computing mappings; the")
print(" * 8x8 and 8x16 glyphs Unscii's (public domain); the character ROMs")
print(" * Synchronet ciolib's (src/conio/allfonts.c), LGPL-2.0-or-later,")
print(" * copyright Rob Swindell and its contributors")
print(" * (core/licenses/syncterm-fonts-LGPL-2.0.txt).")
print(" */")
print('#include "vtint.h"\n')
for kt, c in SETS:
v = int_array(legacy, kt)
print("const uint32_t %s[256] = {" % c)
for k in range(0, 256, 8):
print("\t" + ", ".join("0x%x" % x for x in v[k:k + 8]) + ",")
print("};\n")
emit_font("vt_font_8x8", bitmap(legacy, "GLYPHS", 8), 8, 8)
emit_font("vt_font_vga", bitmap(legacy, "VGA", 16), 8, 16)
emit_font("vt_font_mosaic", mosaic(), 6, 9)
names = []
for n in ROMS:
sizes = rom(roms, n)
r8 = r16 = "NULL"
if 8 in sizes:
r8 = "rom_%s_8" % n.lower()
emit_bytes(r8, sizes[8])
if 16 in sizes:
r16 = "rom_%s_16" % n.lower()
emit_bytes(r16, sizes[16])
names.append((r8, r16))
print()
print("const struct vt_rom vt_roms[ROM_COUNT] = {")
for r8, r16 in names:
print("\t{ %s, %s }," % (r8, r16))
print("};")
main()