Files
JIANT/tools/gen_px_font.py
T
Claude 224fe41a21 0.9.0: flash: the faces' glyph tables packed (-2.6 KB)
A face's glyph table was an aag_t {off, adv, bx, by, bw, bh} (8 B) for every glyph and phase: 420 entries in S and
M (105 glyphs x 4 phases), the advance repeated in every phase, the offsets 16 bits each. Now (tools/aa_raster.py
glyph_tables, for gen_aa_font.py and gen_px_font.py) AF_<N>_GH {off, adv} a glyph (its phases' lowest data offset,
the advance) and AF_<N>_GP 32 bits a glyph and phase: off - GH.off (10 bits), bx + 1 (3), by (6), bw (7), bh (6);
the generator asserts every face fits (and that a glyph's phases advance alike). src/gfx.c ag() unpacks an entry
into an aag_t; text_run (the phase test: phase 1's offset against phase 0's, as before), text_w, cv_text_up,
icons.c cv_trk and ui_menu.c's pen read it through ag() or GH.

S 3360 -> 2100 B, M 3360 -> 2100 B, L 656 -> 492 B, X 752 B as before: image 545784 -> 543224 B (-2560 with the
code). Every entry unpacks to exactly the old one (S M L X compared field by field); tests/text_ref_test.c's
reference unpacks the table on its own (0 pens, 0 pixels differ), tests/theme_test.c's probe face and
tests/text_spacing_test.py read the new layout; every UI render bit for bit as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-10-17 12:47:31 +00:00

176 lines
10 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
"""(0.8.5) The UI's smallest face, AF_X: a crisp 5 x 7 mono pixel font (run by tools/build.py generate()).
gen_px_font.py OUT.h
The approved LENTE mock-up sets its card labels, level tabs, breadcrumb and chips in ~8 px mono capitals; the
antialiased faces start at S (12 px, capitals 9 px tall). AF_X is a 5 x 7 cell on a 6 px pitch (capitals 7 px), the
classic character-LCD shapes, one bit a pixel: no ramp, no phases, no kerning. Capitals, figures and punctuation
and lower case (0x20 .. 0x7A: the units' "ms", "Hz", "dB" as the mock-up writes them), '|', the middle dot (0xB7) and
the ellipsis (0x85).
A glyph is trimmed to its ink box and packed MSB first, a byte boundary per glyph; gfx.c text_run draws it with
cv_bits (aafont_t.bits = 1). The same aafont_t as the other faces, so text_w, cv_text_in, text_fit and the host lint
work unchanged. Line 9 px: the capitals rows 1 .. 7, a descender row 8 (, ; g j p q y).
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
import aa_raster as ar # noqa: E402
PITCH = 6 # advance, px
H, ASC, CAP_Y = 9, 8, 1
G = {
' ': [],
'!': ["..#..", "..#..", "..#..", "..#..", "..#..", ".....", "..#.."],
'"': [".#.#.", ".#.#.", ".#.#."],
'#': [".#.#.", ".#.#.", "#####", ".#.#.", "#####", ".#.#.", ".#.#."],
'$': ["..#..", ".####", "#.#..", ".###.", "..#.#", "####.", "..#.."],
'%': ["##...", "##..#", "...#.", "..#..", ".#...", "#..##", "...##"],
'&': [".##..", "#..#.", "#.#..", ".#...", "#.#.#", "#..#.", ".##.#"],
"'": ["..#..", "..#..", ".#..."],
'(': ["...#.", "..#..", ".#...", ".#...", ".#...", "..#..", "...#."],
')': [".#...", "..#..", "...#.", "...#.", "...#.", "..#..", ".#..."],
'*': [".....", "..#..", "#.#.#", ".###.", "#.#.#", "..#..", "....."],
'+': [".....", "..#..", "..#..", "#####", "..#..", "..#..", "....."],
',': [".....", ".....", ".....", ".....", ".....", ".##..", "..#..", ".#..."],
'-': [".....", ".....", ".....", ".###.", ".....", ".....", "....."],
'.': [".....", ".....", ".....", ".....", ".....", ".##..", ".##.."],
'/': [".....", "....#", "...#.", "..#..", ".#...", "#....", "....."],
'0': [".###.", "#...#", "#..##", "#.#.#", "##..#", "#...#", ".###."],
'1': ["..#..", ".##..", "..#..", "..#..", "..#..", "..#..", ".###."],
'2': [".###.", "#...#", "....#", "...#.", "..#..", ".#...", "#####"],
'3': ["#####", "...#.", "..#..", "...#.", "....#", "#...#", ".###."],
'4': ["...#.", "..##.", ".#.#.", "#..#.", "#####", "...#.", "...#."],
'5': ["#####", "#....", "####.", "....#", "....#", "#...#", ".###."],
'6': ["..##.", ".#...", "#....", "####.", "#...#", "#...#", ".###."],
'7': ["#####", "....#", "...#.", "..#..", ".#...", ".#...", ".#..."],
'8': [".###.", "#...#", "#...#", ".###.", "#...#", "#...#", ".###."],
'9': [".###.", "#...#", "#...#", ".####", "....#", "...#.", ".##.."],
':': [".....", ".##..", ".##..", ".....", ".##..", ".##..", "....."],
';': [".....", ".##..", ".##..", ".....", ".##..", "..#..", ".#..."],
'<': ["...#.", "..#..", ".#...", "#....", ".#...", "..#..", "...#."],
'=': [".....", ".....", "#####", ".....", "#####", ".....", "....."],
'>': [".#...", "..#..", "...#.", "....#", "...#.", "..#..", ".#..."],
'?': [".###.", "#...#", "....#", "...#.", "..#..", ".....", "..#.."],
'@': [".###.", "#...#", "....#", ".##.#", "#.#.#", "#.#.#", ".###."],
'A': [".###.", "#...#", "#...#", "#...#", "#####", "#...#", "#...#"],
'B': ["####.", "#...#", "#...#", "####.", "#...#", "#...#", "####."],
'C': [".###.", "#...#", "#....", "#....", "#....", "#...#", ".###."],
'D': ["###..", "#..#.", "#...#", "#...#", "#...#", "#..#.", "###.."],
'E': ["#####", "#....", "#....", "####.", "#....", "#....", "#####"],
'F': ["#####", "#....", "#....", "####.", "#....", "#....", "#...."],
'G': [".###.", "#...#", "#....", "#.###", "#...#", "#...#", ".####"],
'H': ["#...#", "#...#", "#...#", "#####", "#...#", "#...#", "#...#"],
'I': [".###.", "..#..", "..#..", "..#..", "..#..", "..#..", ".###."],
'J': ["..###", "...#.", "...#.", "...#.", "...#.", "#..#.", ".##.."],
'K': ["#...#", "#..#.", "#.#..", "##...", "#.#..", "#..#.", "#...#"],
'L': ["#....", "#....", "#....", "#....", "#....", "#....", "#####"],
'M': ["#...#", "##.##", "#.#.#", "#.#.#", "#...#", "#...#", "#...#"],
'N': ["#...#", "#...#", "##..#", "#.#.#", "#..##", "#...#", "#...#"],
'O': [".###.", "#...#", "#...#", "#...#", "#...#", "#...#", ".###."],
'P': ["####.", "#...#", "#...#", "####.", "#....", "#....", "#...."],
'Q': [".###.", "#...#", "#...#", "#...#", "#.#.#", "#..#.", ".##.#"],
'R': ["####.", "#...#", "#...#", "####.", "#.#..", "#..#.", "#...#"],
'S': [".####", "#....", "#....", ".###.", "....#", "....#", "####."],
'T': ["#####", "..#..", "..#..", "..#..", "..#..", "..#..", "..#.."],
'U': ["#...#", "#...#", "#...#", "#...#", "#...#", "#...#", ".###."],
'V': ["#...#", "#...#", "#...#", "#...#", "#...#", ".#.#.", "..#.."],
'W': ["#...#", "#...#", "#...#", "#.#.#", "#.#.#", "#.#.#", ".#.#."],
'X': ["#...#", "#...#", ".#.#.", "..#..", ".#.#.", "#...#", "#...#"],
'Y': ["#...#", "#...#", "#...#", ".#.#.", "..#..", "..#..", "..#.."],
'Z': ["#####", "....#", "...#.", "..#..", ".#...", "#....", "#####"],
'[': [".###.", ".#...", ".#...", ".#...", ".#...", ".#...", ".###."],
'\\': [".....", "#....", ".#...", "..#..", "...#.", "....#", "....."],
']': [".###.", "...#.", "...#.", "...#.", "...#.", "...#.", ".###."],
'^': ["..#..", ".#.#.", "#...#"],
'_': [".....", ".....", ".....", ".....", ".....", ".....", "#####"],
'a': ['.....', '.....', '.###.', '....#', '.####', '#...#', '.####'],
'b': ['#....', '#....', '#.##.', '##..#', '#...#', '#...#', '####.'],
'c': ['.....', '.....', '.###.', '#....', '#....', '#...#', '.###.'],
'd': ['....#', '....#', '.##.#', '#..##', '#...#', '#...#', '.####'],
'e': ['.....', '.....', '.###.', '#...#', '#####', '#....', '.###.'],
'f': ['..##.', '.#..#', '.#...', '###..', '.#...', '.#...', '.#...'],
'g': ['.....', '.....', '.####', '#...#', '#...#', '.####', '....#', '.###.'],
'h': ['#....', '#....', '#.##.', '##..#', '#...#', '#...#', '#...#'],
'i': ['..#..', '.....', '.##..', '..#..', '..#..', '..#..', '.###.'],
'j': ['...#.', '.....', '..##.', '...#.', '...#.', '...#.', '#..#.', '.##..'],
'k': ['#....', '#....', '#..#.', '#.#..', '##...', '#.#..', '#..#.'],
'l': ['.##..', '..#..', '..#..', '..#..', '..#..', '..#..', '.###.'],
'm': ['.....', '.....', '##.#.', '#.#.#', '#.#.#', '#...#', '#...#'],
'n': ['.....', '.....', '#.##.', '##..#', '#...#', '#...#', '#...#'],
'o': ['.....', '.....', '.###.', '#...#', '#...#', '#...#', '.###.'],
'p': ['.....', '.....', '####.', '#...#', '#...#', '####.', '#....', '#....'],
'q': ['.....', '.....', '.####', '#...#', '#...#', '.####', '....#', '....#'],
'r': ['.....', '.....', '#.##.', '##..#', '#....', '#....', '#....'],
's': ['.....', '.....', '.###.', '#....', '.###.', '....#', '####.'],
't': ['.#...', '.#...', '###..', '.#...', '.#...', '.#..#', '..##.'],
'u': ['.....', '.....', '#...#', '#...#', '#...#', '#..##', '.##.#'],
'v': ['.....', '.....', '#...#', '#...#', '#...#', '.#.#.', '..#..'],
'w': ['.....', '.....', '#...#', '#...#', '#.#.#', '#.#.#', '.#.#.'],
'x': ['.....', '.....', '#...#', '.#.#.', '..#..', '.#.#.', '#...#'],
'y': ['.....', '.....', '#...#', '#...#', '#...#', '.####', '....#', '.###.'],
'z': ['.....', '.....', '#####', '...#.', '..#..', '.#...', '#####'],
'`': ['.#...', '..#..', '...#.'],
}
EXTRA = {
0x7C: ["..#..", "..#..", "..#..", "..#..", "..#..", "..#..", "..#.."], # |
0xB7: [".....", ".....", ".....", "..#..", ".....", ".....", "....."], # the middle dot
0x85: [".....", ".....", ".....", ".....", ".....", ".....", "#.#.#"], # the ellipsis
}
def glyph(rows):
"""(bx, by, bw, bh, bytes) of a glyph's ink box, rows top down (row 0 = the capitals' top)"""
ink = [(x, y) for y, r in enumerate(rows) for x, c in enumerate(r) if c == '#']
if not ink:
return 0, 0, 0, 0, b""
x0, x1 = min(p[0] for p in ink), max(p[0] for p in ink)
y0, y1 = min(p[1] for p in ink), max(p[1] for p in ink)
bits = [1 if rows[y][x] == '#' else 0 for y in range(y0, y1 + 1) for x in range(x0, x1 + 1)]
data = bytearray()
for i in range(0, len(bits), 8):
b = 0
for k, v in enumerate(bits[i:i + 8]):
b |= v << (7 - k)
data.append(b)
return x0, y0 + CAP_Y, x1 - x0 + 1, y1 - y0 + 1, bytes(data)
def main():
out = Path(sys.argv[1])
codes = list(range(0x20, 0x7B))
ex = sorted(EXTRA)
tab, data = [], bytearray()
for c in codes + ex:
rows = G[chr(c)] if c < 0x7B else EXTRA[c]
for r in rows:
assert len(r) == 5, (chr(c), r)
bx, by, bw, bh, d = glyph(rows)
tab.append((len(data), PITCH * 16, bx, by, bw, bh))
data += d
data += b"\0\0"
hb = [r for r in G['H']]
assert glyph(hb)[1] == CAP_Y and glyph(hb)[3] == 7
lines = ["/* generated by tools/gen_px_font.py: AF_X, the 5 x 7 mono pixel face (1 bit a pixel) */",
f"static const uint8_t AF_X_DATA[{len(data)}] = {{"]
for i in range(0, len(data), 24):
lines.append(" " + ", ".join(str(b) for b in data[i:i + 24]) + ",")
lines.append("};")
gtext, gsize = ar.glyph_tables("X", tab, 1) # (0.9.0: packed, as the other faces')
lines.append(gtext)
lines.append(f"static const uint8_t AF_X_EX[{len(ex)}] = {{{', '.join(str(c) for c in ex)}}};")
lines.append(f"static const aafont_t AF_X = {{{H}, {ASC}, 32, 122, {len(ex)}, 0, AF_X_GH, AF_X_GP, AF_X_DATA, 0, 0, AF_X_EX, 0, 0, 1}};")
lines.append(f"#define AF_X_CAP_Y {CAP_Y} /* the ink of H: rows {CAP_Y} .. {CAP_Y + 6} of the line */")
lines.append("#define AF_X_CAP_H 7")
lines.append(f"#define AF_X_PITCH {PITCH}")
out.write_text("\n".join(lines) + "\n")
size = len(data) + gsize + len(ex)
print(f"face X: 5x7 pixel mono, {len(tab)} glyphs, data {len(data)} B + table {gsize} B = {size} B -> {out}")
if __name__ == "__main__":
main()