palm-client: write the small font's NFNT directly (PilRC's was corrupt)

Font Preview showed garbled text: the bundled PilRC's FONT compiler writes
a broken NFNT header (first/last char words missing, later fields shifted,
height lost), reproduced with a two-glyph test font. gen_font.py now builds
the NFNT itself (header, glyph strip, location and offset/width tables),
decodes it back and checks every glyph, and PilRC includes it as a DATA
resource. Resources now rebuild when the font changes.
This commit is contained in:
rich committed 2026-11-01 15:15:46 -05:00
1 parent 1bae27fe9b
commit 5249d7f5e6
4 files changed
+86 -5

No files matched your search

+1 -1
View File
@@ -44,7 +44,7 @@ build/%.o: src/%.c src/*.h src/ui/*.h | build
build/$(APP): $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
build/res.stamp: resources/$(APP).rcp src/resources.h | build
build/res.stamp: resources/$(APP).rcp src/resources.h resources/smallfont.nfnt | build
rm -rf build/res && mkdir -p build/res
$(PILRC) -q -I src $< build/res
touch $@
Binary file not shown.
+5 -2
View File
@@ -3,8 +3,11 @@
VERSION ID 1 "0.1"
APPLICATIONICONNAME ID 1000 "VisorMesh"
/* Small UI font: generated by tools/gen_font.py, registered as app font 0x80 at launch */
FONT ID SmallFontRes FONTID 128 "resources/smallfont.txt"
/*
* Small UI font, registered as app font 0x80 at launch. tools/gen_font.py writes the
* NFNT itself: this PilRC's FONT compiler emits a corrupt header (garbled text).
*/
DATA "NFNT" ID SmallFontRes "resources/smallfont.nfnt"
MENU ID MainMenuBar
BEGIN
+80 -2
View File
@@ -3,7 +3,10 @@
public domain) plus hand-drawn UI symbols.
Outputs:
resources/smallfont.txt PilRC FONT source (compiled into an NFNT resource)
resources/smallfont.nfnt the NFNT resource, written here directly (the bundled
PilRC's FONT compiler emits a corrupt header) and
included by PilRC as a DATA resource
resources/smallfont.txt the same glyphs as text, for reading and diffs
src/ui/glyphs.h code points for the UI symbols
src/ui/fontmetrics.c per-character widths, so host tests lay text out exactly
@@ -12,10 +15,12 @@ Latin-1); the UI symbols go in 0x80-0x9F, reusing Windows-1252's slots where it
has the same symbol (... 0x85, < 0x8B, > 0x9B).
"""
import os
import struct
HERE = os.path.dirname(os.path.abspath(__file__))
BDF = os.path.join(HERE, 'fonts', '4x6.bdf')
OUT_FONT = os.path.join(HERE, '..', 'resources', 'smallfont.txt')
OUT_NFNT = os.path.join(HERE, '..', 'resources', 'smallfont.nfnt')
OUT_GLYPHS = os.path.join(HERE, '..', 'src', 'ui', 'glyphs.h')
OUT_METRICS = os.path.join(HERE, '..', 'src', 'ui', 'fontmetrics.c')
@@ -90,6 +95,71 @@ def parse_bdf(path):
return glyphs
def build_nfnt(font, missing):
"""
Palm OS FontType (Mac FONT layout, big-endian words):
fontType, firstChar, lastChar, maxWidth, kernMax, nDescent, fRectWidth,
fRectHeight, owTLoc, ascent, descent, leading, rowWords,
bitmap[rowWords * fRectHeight] all glyphs side by side, 1 bit per pixel
locTable[n + 2] x of each glyph in the strip, then the strip width
owTable[n + 2] (offset << 8 | width); 0xFFFF = not in font
where n = lastChar - firstChar + 1 and entry n is the missing-char glyph.
owTLoc is the distance in words from the owTLoc field to owTable.
"""
first, last = min(font), max(font)
n = last - first + 1
order = [font.get(c) for c in range(first, last + 1)] + [missing]
locs, x = [], 0
for rows in order:
locs.append(x)
x += len(rows[0]) if rows else 0
locs.append(x)
strip_w = x
row_words = (strip_w + 15) // 16
bitmap = bytearray(row_words * 2 * HEIGHT)
for rows, x0 in zip(order, locs):
if not rows:
continue
for y, row in enumerate(rows):
for dx, ch in enumerate(row):
if ch == '#':
bx = x0 + dx
bitmap[y * row_words * 2 + bx // 8] |= 0x80 >> (bx % 8)
ow = [(0 << 8 | len(rows[0])) if rows else 0xFFFF for rows in order] + [0xFFFF]
max_w = max(len(r[0]) for r in order if r)
header_words, ow_t_loc_index = 13, 8
ow_t_loc = header_words + row_words * HEIGHT + len(locs) - ow_t_loc_index
header = struct.pack('>13h', -28672, first, last, max_w, 0, 0, max_w, HEIGHT,
ow_t_loc, ASCENT, DESCENT, 0, row_words)
return header + bytes(bitmap) + struct.pack('>%dH' % len(locs), *locs) + struct.pack('>%dH' % len(ow), *ow)
def decode_nfnt(data):
"""Reads an NFNT back into {code: rows} (and 'missing'), to check build_nfnt."""
h = struct.unpack('>13h', data[:26])
first, last, height, ow_t_loc, row_words = h[1], h[2], h[7], h[8], h[12]
n = last - first + 1
bm_off = 26
loc_off = bm_off + row_words * 2 * height
locs = struct.unpack('>%dH' % (n + 2), data[loc_off:loc_off + 2 * (n + 2)])
ow_off = (ow_t_loc_index_bytes := 8 * 2) + ow_t_loc * 2
ow = struct.unpack('>%dH' % (n + 2), data[ow_off:ow_off + 2 * (n + 2)])
out = {}
for i in range(n + 1):
if ow[i] == 0xFFFF:
continue
x0, x1 = locs[i], locs[i + 1]
rows = []
for y in range(height):
row = ''
for x in range(x0, x1):
byte = data[bm_off + y * row_words * 2 + x // 8]
row += '#' if byte & (0x80 >> (x % 8)) else '-'
rows.append(row)
out['missing' if i == n else first + i] = rows
return out
def main():
bdf = parse_bdf(BDF)
font = {}
@@ -104,6 +174,14 @@ def main():
missing = bdf[0xFFFD][1] if 0xFFFD in bdf else ['####', '#--#', '#--#', '#--#', '####', '----']
os.makedirs(os.path.dirname(OUT_GLYPHS), exist_ok=True)
nfnt = build_nfnt(font, missing)
back = decode_nfnt(nfnt)
for code, rows in font.items():
assert back.get(code) == rows, 'NFNT round trip failed for 0x%02X' % code
assert back['missing'] == missing
with open(OUT_NFNT, 'wb') as f:
f.write(nfnt)
with open(OUT_FONT, 'w') as f:
f.write('fontType 36864\nascent %d\ndescent %d\n\n' % (ASCENT, DESCENT))
for code in sorted(font):
@@ -124,7 +202,7 @@ def main():
for r in range(0, 256, 16):
f.write('\t' + ', '.join(str(w) for w in widths[r:r + 16]) + ',\n')
f.write('};\n')
print('%d glyphs -> %s' % (len(font), os.path.relpath(OUT_FONT)))
print('%d glyphs -> %s (%d bytes, round trip OK)' % (len(font), os.path.relpath(OUT_NFNT), len(nfnt)))
if __name__ == '__main__':