Files

59 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Generate an 8x8 bitmap font for Agila from a system monospace font."""
from PIL import Image, ImageDraw, ImageFont
font_names = [
"/System/Library/Fonts/Menlo.ttc",
"/System/Library/Fonts/Monaco.dfont",
"/Library/Fonts/Courier New.ttf",
"/System/Library/Fonts/Courier.dfont",
]
font = None
for name in font_names:
try:
font = ImageFont.truetype(name, 8)
print(f"Using font: {name}")
break
except Exception as e:
print(f"Failed {name}: {e}")
if font is None:
print("No suitable font found, using default")
font = ImageFont.load_default()
w, h = 8, 8
values = []
for code in range(32, 127):
ch = chr(code)
img = Image.new("L", (w, h), 0)
draw = ImageDraw.Draw(img)
draw.text((0, 0), ch, font=font, fill=255)
bytes_data = []
for col in range(w):
byte_val = 0
for row in range(h):
px = img.getpixel((col, row))
if px > 127:
byte_val |= 1 << row
bytes_data.append(byte_val)
values.extend(bytes_data)
with open("api/v/font5x7.fv", "w") as f:
f.write("// font5x7.fv - 8x8 bitmap font (ASCII 32-126)\n")
f.write("module agila\n\n")
f.write("const font5x7 = [u8(0x{:02x}), ".format(values[0]))
f.write(", ".join(f"0x{v:02x}" for v in values[1:]))
f.write("]\n\n")
f.write("pub fn glyph5x7(ch rune) []u8 {\n")
f.write("\tcode := int(ch) - 32\n")
f.write("\tif code < 0 || code > 94 {\n")
f.write("\t\treturn font5x7[0..8] // space\n")
f.write("\t}\n")
f.write("\toff := code * 8\n")
f.write("\treturn font5x7[off..off+8]\n")
f.write("}\n")
print(f"Generated {len(values)} bytes for {len(values)//8} glyphs")