From 5249d7f5e62340e1f9270203db42d1bd021a89b3 Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Sun, 1 Nov 2026 15:15:46 -0500 Subject: [PATCH] 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. --- palm-client/Makefile | 2 +- palm-client/resources/smallfont.nfnt | Bin 0 -> 1566 bytes palm-client/resources/visormesh.rcp | 7 ++- palm-client/tools/gen_font.py | 82 ++++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 palm-client/resources/smallfont.nfnt diff --git a/palm-client/Makefile b/palm-client/Makefile index 3671955..245e0ad 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -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 $@ diff --git a/palm-client/resources/smallfont.nfnt b/palm-client/resources/smallfont.nfnt new file mode 100644 index 0000000000000000000000000000000000000000..3fb2cbe35460aa39fd3267573ced85faeef0902a GIT binary patch literal 1566 zcmdT^QA;CN6h7z77}F5kELiX%Y=>nk!A`*i3qJTT$lY{0q#-O)Lh&IiVJcP@AAGQ_ z4_OHkY_L*XD#ex{v+eY5j6xSGbfKZ01}kCL2Pr;SkYx}_?bHVgK3MD>vkQILKd|R; z&-dLs=iGDdJ(pPoP~ZYOAdn6S(d)vZ;BU#?UD3qoG%=T2bZvZ0N?ZS*>7EvtSUnl%$f@B4aKb5x#H zTe_I9%j-{V#(=7;&Mar?Va}bB-M1jXvDxtBWU40ZK9g_H znzbZj9iRLgyBA*h;^;j+on*P4TOo+*_esD1>VogLoKlh96x#|(1t!RB3XE{x`df}X z%`jUx(#M+7WJ+<|eLPn)Tg75EZQ6$#gc{axt*>Rzm3z%qkA*klf8OpcU4yH@zGKSQ zqF)DvU*)dOH^w=CkQTS+i)KW9-#E0a4=n4B?}>TJva~mYPA8xsB7}t75Ljx{E&)-f z1Iz}5+78zR4?aLW7uX;gZ#*LbTuMP${gPdpttSFxks%5(=!PDMi#Mu@)Ee}|01U#A z@D}Za6l7oovM>rc$io;+z$8q;G|Y%JMJPcT7GVjNVFfB+g9BCAfQPUNk3^L&Y(W!t zU>EjaUp!q8j^G$h;1tf_906qvV-#c9jXfAg1yxL-hW$8zgE)jbGBhxS863eZj$#h; zIEE8AiBmX@GeYm;9Da(Q;Sw(6SGbDb;sadA@9_ux5r4v8aO;KI#OJz$ySRt@cz_-r z;W3`zDW2gu0m&4mD8;CodMHi`sgxj%`e}d$X^3=UWKfDSG(uS#r5xpHj3#K3rf8aG zs6a(3QJEHLiI!=FDrA#GRobA3v`LSsPA+XxlXhsA_Gq6D$fF}VrV~1)Gdh<*lBKW| zm10u2)FZ_uMN*}Nq)GkKfHWu#NxH-&LrO^*(ZQEkFM@w!pz^cP=KnuY$HfZ- 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__':