Files
go-mxl-pattern-generator/scripts/gen_font.py
T
Dmitry Sergeev f48195a8bf WGPU is coming
2026-09-07 17:18:03 +03:00

54 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Bake an 8x16 1-bpp bitmap font (ASCII 32..127) from DejaVu Sans Mono 13px.
Output: Go source file with the atlas. One byte per glyph row, bit n = pixel
at column n (LSB = leftmost), matching the kernel's (row >> col) & 1 lookup.
"""
from PIL import Image, ImageDraw, ImageFont
FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
SIZE = 13
BASELINE = 12 # baseline row inside the 16px cell
THRESH = 110
font = ImageFont.truetype(FONT, SIZE)
atlas = []
for c in range(32, 128):
img = Image.new("L", (8, 16), 0)
d = ImageDraw.Draw(img)
d.text((0, BASELINE), chr(c), font=font, fill=255, anchor="ls")
px = img.load()
for y in range(16):
b = 0
for x in range(8):
if px[x, y] >= THRESH:
b |= 1 << x
atlas.append(b)
def art(c):
print(f"--- '{chr(c)}' ---")
for y in range(16):
row = atlas[(c - 32) * 16 + y]
print("".join("#" if (row >> x) & 1 else "." for x in range(8)))
for c in "AgM%m0(":
art(ord(c))
lines = []
for i in range(0, len(atlas), 16):
chunk = ", ".join(f"0x{b:02X}" for b in atlas[i:i+16])
lines.append(f"\t{chunk},")
body = "\n".join(lines)
src = f'''package generator
// 8x16 1-bpp bitmap font baked from DejaVu Sans Mono {SIZE}px (public-domain
// style console glyphs). ASCII 32..127, one byte per row, LSB = leftmost pixel.
var fontAtlas8x16 = [96 * 16]byte{{
{body}
}}
'''
with open("internal/generator/font.go", "w") as f:
f.write(src)
print("wrote internal/generator/font.go,", len(atlas), "bytes of glyph data")