Files
jimmyandClaude Opus 5.5 9f84330b91 Move project to repo root and add Makefile
The Makefile wraps the build, USB and WiFi flashing, the serial monitor
and font generation, with toolchain paths that can be overridden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 12:45:11 +13:00

74 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Render DejaVu Sans Mono Bold into 1-bit C bitmaps for printable ASCII.
Usage: gen_font.py <out.h>
Each glyph row is packed MSB-first into ceil(width/8) bytes.
"""
import sys
from PIL import Image, ImageDraw, ImageFont
TTF = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"
FIRST, LAST = 32, 126
# (name, cell width, cell height, point size)
FONTS = [
("font16", 8, 16, 13),
("font48", 24, 48, 39),
("font96", 48, 96, 78),
]
def render(name, w, h, size):
font = ImageFont.truetype(TTF, size)
ascent, descent = font.getmetrics()
top = (h - (ascent + descent)) // 2
stride = (w + 7) // 8
out = []
for code in range(FIRST, LAST + 1):
img = Image.new("L", (w, h), 0)
d = ImageDraw.Draw(img)
adv = font.getlength(chr(code))
d.text(((w - adv) / 2, top), chr(code), font=font, fill=255)
rows = []
for y in range(h):
bits = 0
for x in range(stride * 8):
on = x < w and img.getpixel((x, y)) >= 110
bits = (bits << 1) | on
rows.append(bits.to_bytes(stride, "big"))
out.append(b"".join(rows))
data = b"".join(out)
lines = [f"static const uint8_t {name}_bits[] = {{"]
for i in range(0, len(data), 16):
lines.append(" " + ", ".join(f"0x{b:02x}" for b in data[i:i + 16]) + ",")
lines.append("};")
lines.append(f"static const font_t {name} = {{{w}, {h}, {stride}, {name}_bits}};")
return "\n".join(lines)
def main():
parts = [
"// Generated by tools/gen_font.py from DejaVu Sans Mono Bold. Do not edit.",
"// DejaVu fonts: Bitstream Vera licence (free to use and redistribute).",
"#pragma once",
"#include <stdint.h>",
"",
"typedef struct {",
" uint8_t w, h, stride;",
" const uint8_t *bits; // glyphs 32..126, h rows of `stride` bytes each",
"} font_t;",
"",
f"#define FONT_FIRST {FIRST}",
f"#define FONT_LAST {LAST}",
"",
]
for f in FONTS:
parts.append(render(*f))
parts.append("")
with open(sys.argv[1], "w") as fh:
fh.write("\n".join(parts))
if __name__ == "__main__":
main()