ws-arch2: font/psf (PSF 1 / 2 read + write, Unicode tables kept on round trip, .psfu.gz via the api); Types.BitmapStrike.map (extra code points per glyph, additive)
This commit is contained in:
1 parent
1b0bc158e8
commit
19f486031d
9 files changed
+545
-8
No files matched your search
@@ -1,10 +1,14 @@
|
||||
-- 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.
|
||||
-- tools/fixtures/_bitmapfonts_ours.luau: our bitmap font writers' output for the reference tools to read back (run by
|
||||
-- tools/fixtures/bitmapfonts.py / psf.py after they wrote the source fonts to tests/gen/<src>.luau:
|
||||
-- `luau _bitmapfonts_ours.luau -a <src> <format> [version]`): decodes each source font with its format, encodes it
|
||||
-- with <format> (default bdf) and prints "<case> <hex>" 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 srcName, outFormat, version = ...
|
||||
local Src = if srcName == "psf_src" then require("@tests/gen/psf_src") else require("@tests/gen/bitmapfonts_src")
|
||||
local enc = F.format(outFormat or "bdf") :: any
|
||||
|
||||
local function hex(b: buffer): string
|
||||
local t = table.create(buffer.len(b))
|
||||
@@ -21,8 +25,8 @@ for _, c in Src.fonts do
|
||||
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 font and enc then
|
||||
local out = enc.encode(font, { version = tonumber(version) })
|
||||
if out then
|
||||
print(c.name .. " " .. hex(out))
|
||||
end
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
# PSF fixtures (tools/fixtures.py): Linux console fonts from kbd (nixpkgs#kbd: PSF 2 with and without Unicode tables,
|
||||
# PSF 1 with 512 glyphs), Spleen / Tamzen / Terminus / Cozette console fonts, read from the nix store at fixture time.
|
||||
# FreeType does not read PSF: the reference is kbd's `psfgettable` (the Unicode table) and the independent parser
|
||||
# below (header and glyph bitmaps). Our PSF 2 and PSF 1 output (tools/fixtures/_bitmapfonts_ours.luau) is read back
|
||||
# by psfgettable and the parser.
|
||||
# tests/gen/psf.luau { fonts = { { name, data, gz?, version, count, width, height, glyphs = { { index, pixels } },
|
||||
# table = { { glyph, cp } }, ours2 = { ok, detail }, ours1? } } }
|
||||
import gzip, importlib.util, 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_ours.luau", "src/Formats/font/psf"]
|
||||
|
||||
|
||||
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 parse(b):
|
||||
"""(version, count, width, height, [glyph pixel bytes as 0/255]) of a PSF file"""
|
||||
if b[:4] == b"\x72\xb5\x4a\x86":
|
||||
_, hsize, flags, count, charsize, height, width = [int.from_bytes(b[4 + 4 * i:8 + 4 * i], "little") for i in range(7)]
|
||||
ver, at = 2, hsize
|
||||
else:
|
||||
mode, height = b[2], b[3]
|
||||
ver, at, width, count = 1, 4, 8, 512 if mode & 1 else 256
|
||||
stride = (width + 7) // 8
|
||||
glyphs = []
|
||||
for i in range(count):
|
||||
g = b[at + i * stride * height: at + (i + 1) * stride * height]
|
||||
px = bytearray(width * height)
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if g[y * stride + x // 8] >> (7 - x % 8) & 1:
|
||||
px[y * width + x] = 255
|
||||
glyphs.append(bytes(px))
|
||||
return ver, count, width, height, glyphs
|
||||
|
||||
|
||||
def table(path):
|
||||
out = A.tool(["kbd"], ["psfgettable", path]).decode()
|
||||
pairs = []
|
||||
for line in out.splitlines():
|
||||
if line.startswith("#") or not line.strip():
|
||||
continue
|
||||
parts = line.split()
|
||||
g = int(parts[0], 16)
|
||||
for p in parts[1:]:
|
||||
if p.startswith("U+") and "," not in p:
|
||||
pairs.append({"glyph": g, "cp": int(p[2:], 16)})
|
||||
return pairs
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
kbd = os.path.join(store("kbd"), "share", "consolefonts")
|
||||
items = [(os.path.join(kbd, f), f) for f in ("lat9w-16.psfu.gz", "default8x16.psfu.gz", "cp866-8x16.psf.gz", "eurlatgr.psfu.gz", "iso01-12x22.psfu.gz")]
|
||||
items += [(os.path.join(store("spleen"), "share", "consolefonts", "spleen-12x24.psfu"), "spleen-12x24.psfu"),
|
||||
(os.path.join(store("tamzen"), "share", "consolefonts", "Tamzen8x16.psf"), "Tamzen8x16.psf"),
|
||||
(os.path.join(store("terminus_font"), "share", "consolefonts", "ter-v16n.psf.gz"), "ter-v16n.psf.gz"),
|
||||
(os.path.join(store("cozette"), "share", "consolefonts", "cozette.psf"), "cozette.psf")]
|
||||
tmp = A.tmpdir()
|
||||
try:
|
||||
fonts = []
|
||||
for path, name in items:
|
||||
raw = open(path, "rb").read()
|
||||
data = gzip.decompress(raw) if name.endswith(".gz") else raw
|
||||
p = os.path.join(tmp, name.replace(".gz", ""))
|
||||
open(p, "wb").write(data)
|
||||
ver, count, width, height, glyphs = parse(data)
|
||||
sample = sorted({0, 1, 65, 97, count // 2, count - 1})
|
||||
e = {"name": name, "data": raw, "version": ver, "count": count, "width": width, "height": height,
|
||||
"glyphs": [{"index": i, "pixels": glyphs[i]} for i in sample], "table": table(p)}
|
||||
if name.endswith(".gz"):
|
||||
e["gz"] = True
|
||||
fonts.append(e)
|
||||
ctx.emit("psf_src", {"fonts": [{"name": f["name"], "format": "psf", "data": f["data"], **({"gz": True} if f.get("gz") else {})} 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")
|
||||
base = ([exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]) + [os.path.join(ctx.root, "tools", "fixtures", "_bitmapfonts_ours.luau"), "-a", "psf_src", "psf"]
|
||||
for ver in ("2", "1"):
|
||||
r = subprocess.run(base + [ver], capture_output=True, cwd=ctx.root)
|
||||
outs = dict(line.rsplit(" ", 1) for line in r.stdout.decode().splitlines())
|
||||
for f in fonts:
|
||||
key = "ours" + ver
|
||||
if f["name"] not in outs:
|
||||
if ver == "2" or (f["width"] <= 8):
|
||||
f[key] = {"ok": False, "detail": "no output: " + r.stderr.decode(errors="replace")[-300:]}
|
||||
continue
|
||||
data = bytes.fromhex(outs[f["name"]])
|
||||
p = os.path.join(tmp, "ours%s-%s" % (ver, f["name"]))
|
||||
open(p, "wb").write(data)
|
||||
over, ocount, ow, oh, og = parse(data)
|
||||
_, _, _, _, src = parse(gzip.decompress(f["data"]) if f.get("gz") else f["data"])
|
||||
same_glyphs = ow == f["width"] and oh == f["height"] and og[:f["count"]] == src
|
||||
same_table = sorted((x["glyph"], x["cp"]) for x in table(p)) == sorted((x["glyph"], x["cp"]) for x in f["table"])
|
||||
f[key] = {"ok": over == int(ver) and same_glyphs and same_table, "detail": "version %d, glyphs %s, table %s" % (over, same_glyphs, same_table)}
|
||||
ctx.emit("psf", {"fonts": fonts})
|
||||
finally:
|
||||
A.rmtree(tmp)
|
||||
Reference in new issue
Block a user