Multi-byte fonts: GB2312, Big5, Shift-JIS, KS C 5601, UTF-8

Font Creator output decoded (lead/trail ranges and gaps in the header, map codes = bytes as
little-endian u16 or Unicode code points) and round-tripped byte for byte; from_ttf(chars=)
builds them; the renderer splits text like the simulator (any byte >= 0x81 starts a pair,
missing characters advance one space) -- pixel-exact for all five encodings.
Intelligent glyph data is 8-byte aligned (was 4). CLI add-font --chars/--chars-file, MCP chars.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-16 13:42:50 +01:00
1 parent c102157ac2
commit 0138a0ba56
11 files changed
+137 -26

No files matched your search

+20 -2
View File
@@ -126,6 +126,24 @@ class Format4(unittest.TestCase):
self.assertEqual(blob[24 + aoff:], bytes([63, 17, 0, 15]) * 4 + bytes([63, 17, 0, 255, 0, 112]))
self.assertEqual(pictures.to_image(blob).getpixel((3, 3))[3], 126)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_multibyte_fonts(self):
# header layout of the Editor's Font Creator output (GB2312 / Shift-JIS / UTF-8)
gb = fonts.from_ttf(None, 16, "gb2312", chars="AB中文")
b = gb.pack()
self.assertEqual((b[4], b[5], b[8], b[9], b[10], b[11]), (2, 2, 0xA1, 0xF7, 0xA1, 0xFE))
self.assertEqual([g.code for g in gb.glyphs], [0x41, 0x42, 0xC4CE, 0xD0D6]) # bytes, little-endian
back = fonts.Font.parse(b)
self.assertEqual(back.codes("A中".encode("gb2312")), [0x41, 0xD0D6])
self.assertEqual(back.codes(bytes([0xE9, 0xFE])), [0xFEE9]) # any byte >= 0x81 starts a pair
sj = fonts.from_ttf(None, 16, "shift-jis", chars="A日").pack()
self.assertEqual((sj[1], sj[2], sj[0x1C], sj[0x1D]), (0x7E, 0x01, 0x9F, 0x40))
u8 = fonts.Font.parse(fonts.from_ttf(None, 16, "utf-8", chars="Aé中").pack())
self.assertEqual([g.code for g in u8.glyphs], [0x41, 0xE9, 0x4E2D]) # code points
self.assertEqual(u8.codes("é中".encode("utf-8")), [0xE9, 0x4E2D])
with self.assertRaises(ValueError):
fonts.from_ttf(None, 16, "big5") # needs chars=
def test_overhang_columns(self):
# Font Creator fonts: map bytes 3/4 = extra columns left/right of the advance width
rows = [[1, 0, 0, 1, 1]] * 2
@@ -139,7 +157,7 @@ class Format4(unittest.TestCase):
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_font_follows_project_direction(self):
# an Intelligent landscape project (0 degrees): glyphs row by row, header byte 3 = 0x0A,
# glyph data 4-byte aligned -- what the Editor converts a 90-degree font to
# glyph data 8-byte aligned -- what the Editor converts a 90-degree font to
from nxhmi import project
h = project.new_project("NX8048P050_011")
f = fonts.from_ttf(None, 16)
@@ -151,7 +169,7 @@ class Format4(unittest.TestCase):
self.assertEqual(back.glyph("0").pixels, fonts.from_ttf(None, 16).glyph("0").pixels)
self.assertEqual(back.pack(), blob)
m = 0x2C + blob[17]
self.assertEqual((blob[m + 5] | blob[m + 6] << 8) % 4, 0)
self.assertEqual((blob[m + 5] | blob[m + 6] << 8) % 8, 0)
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
def test_image_roundtrip(self):