ws-arch2: font/bdf (read / write; FreeType reads our output identically) and font/pcf (every pad / unit / order, .pcf.gz via the api), shared bitmap-font fixtures vs FreeType
init.luau unwrap: a single-stream archive whose entry is a .pcf / .psf / .bdf loads as that font (category font).
This commit is contained in:
1 parent
f1c2c6ed06
commit
87cbc62fb6
11 files changed
+913
-2
No files matched your search
@@ -0,0 +1,30 @@
|
||||
-- tools/fixtures/_bitmapfonts_ours.luau: our bitmap font writers' output for FreeType to read back (run by
|
||||
-- tools/fixtures/bitmapfonts.py after it wrote tests/gen/bitmapfonts_src.luau): decodes each source font with its
|
||||
-- format and prints "<case> <hex of our BDF>" lines.
|
||||
local F = require("@formats")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Src = require("@tests/gen/bitmapfonts_src")
|
||||
|
||||
local function hex(b: buffer): string
|
||||
local t = table.create(buffer.len(b))
|
||||
for i = 0, buffer.len(b) - 1 do
|
||||
t[i + 1] = string.format("%02x", buffer.readu8(b, i))
|
||||
end
|
||||
return table.concat(t)
|
||||
end
|
||||
|
||||
for _, c in Src.fonts do
|
||||
local data = buffer.fromstring(c.data)
|
||||
if c.gz then
|
||||
data = Deflate.gunzip(data, 0, buffer.len(data)) :: buffer
|
||||
end
|
||||
local mod = F.format(c.format) :: any
|
||||
local font = if mod then mod.decode(Bytes.reader(data)) else nil
|
||||
if font and F.format("bdf") then
|
||||
local out = (F.format("bdf") :: any).encode(font)
|
||||
if out then
|
||||
print(c.name .. " " .. hex(out))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,34 @@
|
||||
# FreeType's view of bitmap fonts (run by tools/fixtures/bitmapfonts.py in a nix python with freetype-py): stdin is
|
||||
# JSON { fonts: [ { path, chars } ] }; prints JSON [ { glyphs: [ { cp, width, height, left, top, advance, pixels(hex) } ],
|
||||
# ascent, descent, family, style, height } ] from FT_LOAD_RENDER | FT_LOAD_TARGET_MONO on the first strike.
|
||||
import json, sys
|
||||
import freetype
|
||||
|
||||
req = json.load(sys.stdin)
|
||||
out = []
|
||||
for f in req["fonts"]:
|
||||
face = freetype.Face(f["path"])
|
||||
if face.num_fixed_sizes:
|
||||
face.select_size(0)
|
||||
glyphs = []
|
||||
for cp in f["chars"]:
|
||||
gi = face.get_char_index(cp)
|
||||
if gi == 0:
|
||||
continue
|
||||
face.load_glyph(gi, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
|
||||
g = face.glyph
|
||||
bm = g.bitmap
|
||||
w, h, pitch = bm.width, bm.rows, bm.pitch
|
||||
buf = bm.buffer
|
||||
px = bytearray(w * h)
|
||||
for y in range(h):
|
||||
for x in range(w):
|
||||
if (buf[y * pitch + x // 8] >> (7 - x % 8)) & 1:
|
||||
px[y * w + x] = 255
|
||||
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,
|
||||
"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))
|
||||
@@ -0,0 +1,84 @@
|
||||
# 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
|
||||
# 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,
|
||||
# top, advance, pixels } }, ascent, descent }, ours = { ok, detail } } } }
|
||||
import gzip, importlib.util, json, os, shutil, subprocess
|
||||
|
||||
_s = importlib.util.spec_from_file_location("_arch2", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_arch2.py"))
|
||||
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"]
|
||||
CHARS = [32, 33, 48, 64, 65, 87, 103, 106, 113, 126, 0xE9, 0xF1, 0x416, 0x2588, 0x2192]
|
||||
|
||||
|
||||
def store(pkg):
|
||||
return subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#" + pkg], capture_output=True, check=True, text=True).stdout.strip().split("\n")[-1]
|
||||
|
||||
|
||||
def refs(ctx, paths):
|
||||
code = open(os.path.join(ctx.root, "tools", "fixtures", "_bitmapfonts_ref.py")).read()
|
||||
raw = ctx.python(["freetype-py"], code, input=json.dumps({"fonts": [{"path": p, "chars": CHARS} for p in paths]}).encode())
|
||||
return json.loads(raw)
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
spleen = os.path.join(store("spleen"), "share", "fonts", "misc")
|
||||
tamzen = os.path.join(store("tamzen"), "share", "fonts", "misc")
|
||||
misc = os.path.join(store("xorg.fontmiscmisc"), "share", "fonts", "X11", "misc")
|
||||
tmp = A.tmpdir()
|
||||
try:
|
||||
items = [] # (name, format, path, gz)
|
||||
for f in ("spleen-5x8.bdf", "spleen-12x24.bdf"):
|
||||
items.append((f, "bdf", os.path.join(spleen, f), False))
|
||||
src = os.path.join(spleen, "spleen-5x8.bdf")
|
||||
for opts in (["-p1", "-u1", "-m"], ["-p2", "-u2", "-l"], ["-p4", "-u4", "-m"], ["-p8", "-u4", "-l"], ["-p4", "-u1", "-l", "-t"]):
|
||||
out = os.path.join(tmp, "spleen-5x8" + "".join(opts) + ".pcf")
|
||||
A.tool(["xorg.bdftopcf"], ["bdftopcf"] + opts + ["-o", out, src])
|
||||
items.append(("spleen-5x8 bdftopcf " + " ".join(opts), "pcf", out, False))
|
||||
out = os.path.join(tmp, "spleen-12x24.pcf")
|
||||
A.tool(["xorg.bdftopcf"], ["bdftopcf", "-o", out, os.path.join(spleen, "spleen-12x24.bdf")])
|
||||
items.append(("spleen-12x24 bdftopcf", "pcf", out, False))
|
||||
for f in ("Tamzen7x13r.pcf", "Tamzen10x20b.pcf"):
|
||||
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))
|
||||
# FreeType reads .pcf.gz itself
|
||||
rs = refs(ctx, [p for _, _, p, _ in items])
|
||||
fonts = []
|
||||
for (name, fmt, path, gz), ref in zip(items, rs):
|
||||
for g in ref["glyphs"]:
|
||||
g["pixels"] = bytes.fromhex(g["pixels"])
|
||||
e = {"name": name, "format": fmt, "data": open(path, "rb").read(), "ref": ref}
|
||||
if gz:
|
||||
e["gz"] = True
|
||||
fonts.append(e)
|
||||
# our BDF writer, read back by FreeType
|
||||
ctx.emit("bitmapfonts_src", {"fonts": [{k: v for k, v in f.items() if k != "ref"} for f in fonts]})
|
||||
subprocess.run(["python3", "-I", os.path.join(ctx.root, "tools", "gen_registry.py")], check=True, capture_output=True)
|
||||
exe = shutil.which("luau")
|
||||
argv = ([exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]) + [os.path.join(ctx.root, "tools", "fixtures", "_bitmapfonts_ours.luau")]
|
||||
r = subprocess.run(argv, capture_output=True, cwd=ctx.root)
|
||||
ours = {}
|
||||
for line in r.stdout.decode().splitlines():
|
||||
n, h = line.rsplit(" ", 1)
|
||||
p = os.path.join(tmp, "ours-%d.bdf" % len(ours))
|
||||
open(p, "wb").write(bytes.fromhex(h))
|
||||
ours[n] = p
|
||||
names = list(ours)
|
||||
back = refs(ctx, [ours[n] for n in names]) if names else []
|
||||
for f in fonts:
|
||||
if f["name"] in ours:
|
||||
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))}
|
||||
else:
|
||||
f["ours"] = {"ok": False, "detail": "no output: " + r.stderr.decode(errors="replace")[-300:]}
|
||||
ctx.emit("bitmapfonts", {"fonts": fonts})
|
||||
finally:
|
||||
A.rmtree(tmp)
|
||||
Reference in new issue
Block a user