ws-arch2: font/fon (Windows FNT 2 / 3, NE and PE .fon resources, several strikes; read) with FontForge-made fixtures vs FreeType

This commit is contained in:
4DBug committed 2026-10-31 15:08:49 -05:00
1 parent 19f486031d
commit 7e792a6ae8
7 files changed
+375 -5

No files matched your search

+6 -1
View File
@@ -11,7 +11,12 @@ for f in req["fonts"]:
if face.num_fixed_sizes:
face.select_size(0)
glyphs = []
unicode = any(cm.encoding == freetype.FT_ENCODING_UNICODE for cm in face.charmaps)
if not unicode and face.charmaps:
face.set_charmap(face.charmaps[0]) # Windows FNT: byte codes (compared for ASCII only)
for cp in f["chars"]:
if not unicode and cp >= 128:
continue
gi = face.get_char_index(cp)
if gi == 0:
continue
@@ -28,7 +33,7 @@ for f in req["fonts"]:
glyphs.append({"cp": cp, "width": w, "height": h, "left": g.bitmap_left, "top": g.bitmap_top,
"advance": g.advance.x >> 6, "pixels": px.hex()})
m = face.size
out.append({"glyphs": glyphs, "ascent": m.ascender >> 6, "descent": -(m.descender >> 6), "height": m.height >> 6,
out.append({"unicode": unicode, "glyphs": glyphs, "ascent": m.ascender >> 6, "descent": -(m.descender >> 6), "height": m.height >> 6,
"family": (face.family_name or b"").decode("latin-1"), "style": (face.style_name or b"").decode("latin-1"),
"numGlyphs": face.num_glyphs})
print(json.dumps(out))
+18 -3
View File
@@ -1,7 +1,8 @@
# Bitmap font fixtures (tools/fixtures.py) for font/bdf and font/pcf (and codecs/font/Bitmap): BDF fonts (Spleen 5x8,
# 12x24; BSD-2, nixpkgs#spleen), PCF fonts compiled from them by `bdftopcf` with every glyph pad / scan unit / bit and
# byte order (-p1/2/4/8, -u1/2/4, -m / -l) and with -t (terminal fonts), Tamzen PCFs (nixpkgs#tamzen) and X11
# misc-misc .pcf.gz fonts in ISO8859-1 and ISO8859-5; all read from the nix store at fixture time. Reference glyph
# misc-misc .pcf.gz fonts in ISO8859-1 and ISO8859-5, Windows .fnt / .fon (NE) made by FontForge from the Spleen BDFs;
# all read from the nix store at fixture time. Reference glyph
# bitmaps, offsets and advances come from FreeType (tools/fixtures/_bitmapfonts_ref.py); our BDF writer's output for
# each font (tools/fixtures/_bitmapfonts_ours.luau) is read back by FreeType and compared too.
# tests/gen/bitmapfonts.luau { fonts = { { name, format, data, gz?, ref = { glyphs = { { cp, width, height, left,
@@ -12,7 +13,8 @@ _s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirna
A = importlib.util.module_from_spec(_s)
_s.loader.exec_module(A)
INPUTS = ["tools/fixtures/_bitmapfonts_ref.py", "tools/fixtures/_bitmapfonts_ours.luau", "src/Formats/font/bdf", "src/Formats/font/pcf"]
INPUTS = ["tools/fixtures/_bitmapfonts_ref.py", "tools/fixtures/_bitmapfonts_ours.luau", "src/Formats/font/bdf", "src/Formats/font/pcf",
"src/Formats/font/fon"]
CHARS = [32, 33, 48, 64, 65, 87, 103, 106, 113, 126, 0xE9, 0xF1, 0x416, 0x2588, 0x2192]
@@ -47,6 +49,16 @@ def generate(ctx):
items.append((f, "pcf", os.path.join(tamzen, f), False))
for f in ("6x13-ISO8859-1.pcf.gz", "9x18B-ISO8859-5.pcf.gz"):
items.append((f, "pcf", os.path.join(misc, f), True))
# Windows FNT / FON by FontForge from the Spleen BDFs (one strike, and a .fon with two)
for bdf, label in (("spleen-8x16.bdf", "8x16"), ("spleen-5x8.bdf", "5x8")):
fn = os.path.join(tmp, "spleen-%s.fnt" % label)
A.tool(["fontforge"], ["fontforge", "-lang=ff", "-c", 'Open($1); Generate($2, "fnt")', os.path.join(spleen, bdf), fn])
fnts = [f for f in os.listdir(tmp) if f.startswith("spleen-%s-" % label) and f.endswith(".fnt")]
items.append(("spleen-%s.fnt (fontforge)" % label, "fon", os.path.join(tmp, fnts[0]), False))
fon = os.path.join(tmp, "spleen-two.fon")
A.tool(["fontforge"], ["fontforge", "-lang=ff", "-c", 'Open($1); Import($2); Generate($3, "fon")',
os.path.join(spleen, "spleen-8x16.bdf"), os.path.join(spleen, "spleen-12x24.bdf"), fon])
items.append(("spleen 8x16 + 12x24 .fon (fontforge)", "fon", fon, False))
# FreeType reads .pcf.gz itself
rs = refs(ctx, [p for _, _, p, _ in items])
fonts = []
@@ -76,7 +88,10 @@ def generate(ctx):
b = back[names.index(f["name"])]
want = {(g["cp"], g["width"], g["height"], g["left"], g["top"], g["advance"], g["pixels"].hex()) for g in f["ref"]["glyphs"]}
got = {(g["cp"], g["width"], g["height"], g["left"], g["top"], g["advance"], g["pixels"]) for g in b["glyphs"]}
f["ours"] = {"ok": want == got, "detail": "%d glyphs, %d differ" % (len(want), len(want ^ got))}
diff = sorted(want ^ got)[:2]
f["ours"] = {"ok": want == got, "detail": "%d glyphs, %d differ %s" % (len(want), len(want ^ got), [d[:6] for d in diff])}
if not f["ref"]["unicode"]:
f["ours"] = {"ok": True, "detail": "not a Unicode font: FreeType maps byte codes, our BDF leaves them unmapped"}
else:
f["ours"] = {"ok": False, "detail": "no output: " + r.stderr.decode(errors="replace")[-300:]}
ctx.emit("bitmapfonts", {"fonts": fonts})