From cb5237aaa991d80553b080b09b15e5754acbc94a Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 15:30:20 -0500 Subject: [PATCH] ws-arch2: docs/guide sections (archive: LHA / ARJ / Zoo, encodings, disk images; font: bitmap fonts, Type 1 / EOT / Hershey) with tested snippets; tools/fixtures/docs_arch2.py inputs --- docs/guide/archive.md | 70 ++++++++++++++++++++++++++++++++++++ docs/guide/font.md | 50 +++++++++++++++++++++++++- tools/fixtures/docs_arch2.py | 70 ++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 tools/fixtures/docs_arch2.py diff --git a/docs/guide/archive.md b/docs/guide/archive.md index edda82c..4eeceed 100644 --- a/docs/guide/archive.md +++ b/docs/guide/archive.md @@ -272,6 +272,65 @@ assert(by["bin"].dir and by["bin/run.sh"].mode == 493 and by["run"].link == "bin assert(F.archive.load(isoBytes, { prefer = "joliet" }).meta.iso.tree == "joliet") ``` +## LHA, ARJ and Zoo + +DOS-era archives read with their own compressors (`codecs/compress/Lzh`): LHA / LZH at header levels 0 to 3 with +lh0, lh1, lh4 to lh7, lhx and LArc's lzs / lz5 (Unix modes, symlinks, CRC-16 checked on `read`), ARJ methods 0 to 4 +(CRC-32 checked) and Zoo's stored, LZW and lzh entries (deleted entries skipped). LHA also writes (level 2 headers, +stored data); ARJ and Zoo are read only. + +```luau +local lzh = assert(F.archive.load(lzhBytes)) +assert(lzh.format == "lha" and lzh:list()[1].method == "lh0") -- small files are stored +assert(buffer.tostring(assert(lzh:read("readme.txt"))) == "formats-luau retro archives\n") +local arj = assert(F.archive.load(arjBytes)) +assert(arj:read("src/main.c")) +local zoo = assert(F.archive.load(zooBytes)) +assert(zoo:list()[1].method == "lzw" and zoo:read("readme.txt")) +local stored = assert(F.save(lzh, nil, { format = "lha" })) -- level 2, lh0 +assert(F.archive.load(stored):list()[1].method == "lh0") +``` + +## Encodings + +Text and Mac encodings load as archives too: uuencode / xxencode / `begin-base64` blocks (`uu`), yEnc (multi-part +postings in one text are joined), BinHex 4.0, MacBinary I / II / III and AppleSingle / AppleDouble. All of them read +and write. Mac files keep the data fork as the entry `name` and a resource fork as `name .. ".rsrc"` +(`fork = "resource"`), with `macType`, `macCreator` and `finderFlags`. A single-file encoding is a stream, so a +uuencoded `.tar.gz` loads as the tar. + +```luau +local uu = assert(F.archive.load(uuText)) +assert(uu:list()[1].name == "readme.txt" and uu:list()[1].mode == 420) -- 420 = 0644 +local a = F.format("binhex").new() +a:add("Read Me", "the data fork") +a:add("Read Me.rsrc", "resources") +a._byName["Read Me.rsrc"].fork = "resource" +local hqx = assert(a:write()) +local back = assert(F.archive.load(hqx)) +assert(back:list()[2].fork == "resource" and buffer.tostring(back:read("Read Me")) == "the data fork") +local y = assert(F.save(uu, nil, { format = "yenc" })) +assert(string.sub(buffer.tostring(y), 1, 8) == "=ybegin ") +``` + +## Disk images + +Floppy and disk images list their files: Commodore D64 / D71 / D81 (PETSCII names as VICE's c1541 shows them, +`type` = `prg` / `seq` / `usr` / `rel`, disk name and id in `meta`), Amiga ADF (OFS / FFS, international and +directory-cache modes, protection bits, comments, links) and FAT12 / FAT16 / FAT32 images (VFAT long names; a +hard-disk image opens its first FAT partition, `partition` picks another). D64 and FAT floppies also write. + +```luau +local d64 = assert(F.archive.load(d64Bytes)) +assert(d64.meta.disk == "games" and d64:list()[1].type == "prg") +local adf = assert(F.archive.load(adfBytes)) +assert(adf.meta.volume == "Work" and adf.meta.fs == "ffs" and adf:read("s/readme")) +local fat = assert(F.archive.load(floppyBytes)) +assert(fat.meta.fat == 12 and fat.meta.label == "DOCS" and fat:read("src/main.c")) +local floppy = assert(F.save(adf, nil, { format = "fat", size = 720 })) -- copy the ADF's files to a FAT floppy +assert(F.archive.load(floppy):read("s/readme")) +``` + ## Formats | Format | Read | Write | Notes | @@ -290,6 +349,17 @@ assert(F.archive.load(isoBytes, { prefer = "joliet" }).meta.iso.tree == "joliet" | compress (`.Z`, `.taz`) | yes | yes | single stream; 9..16-bit LZW | | LZ4 (`.lz4`) | yes | yes | single stream; frames, legacy frames, XXH32 checks; no dictionaries | | ISO 9660 (`.iso`) | yes | - | Joliet, Rock Ridge, El Torito, multi-extent; no UDF | +| LHA (`.lzh`, `.lha`, `.lzs`) | yes | yes | levels 0-3; lh0 / lh1 / lh4-lh7 / lhx / lzs / lz5; writes stored level 2 | +| ARJ (`.arj`) | yes | - | methods 0-4; no garbled entries, no multi-volume | +| Zoo (`.zoo`) | yes | - | stored / LZW / lzh | +| uuencode / xxencode (`.uu`, `.xxe`) | yes | yes | several blocks per text; GNU `begin-base64` too | +| yEnc (`.yenc`) | yes | yes | multi-part postings in one text | +| BinHex 4.0 (`.hqx`) | yes | yes | data and resource forks | +| MacBinary (`.bin`) | yes | yes | I / II / III; writes III | +| AppleSingle / AppleDouble | yes | yes | versions 1 and 2 | +| C64 disks (`.d64`, `.d71`, `.d81`) | yes | yes | PETSCII names; no GEOS VLIR | +| Amiga disks (`.adf`) | yes | - | OFS / FFS; no long-name (DOS\\6 / 7) volumes | +| FAT images (`.img`, `.ima`) | yes | yes | FAT12 / 16 / 32, VFAT names, MBR partitions; writes FAT12 floppies | Full capability list: [FORMATS.md](../FORMATS.md). diff --git a/docs/guide/font.md b/docs/guide/font.md index d784a96..a02df94 100644 --- a/docs/guide/font.md +++ b/docs/guide/font.md @@ -232,6 +232,46 @@ local bm = assert(F.font.load(fnt, { loadPage = function(file) return pages[file assert(bm:rasterize(bm:glyphIndex(string.byte("H")))) ``` +## Bitmap fonts + +BDF, PCF, PSF (Linux console) and Windows FNT / FON fonts load as the same Font, with their glyph bitmaps in +`font.bitmap` (`size`, `ascent`, `descent`, `glyphs[i] = { codepoint, name, width, height, left, top, advance, +pixels }`, one byte per pixel, 0 or 255). Units are pixels: `unitsPerEm` is the pixel size, `rasterize` returns the +glyph as stored (its `px` argument only sets `img.meta.scale`), `outline` is nil. `.pcf.gz` and `.psfu.gz` load +through the gzip. BDF and PSF also write, from any Font: a bitmap font as it is, an outline font rasterized at `px`. + +```luau +local bdf = assert(F.font.load(bdfText)) +assert(bdf.unitsPerEm == 8 and bdf.bitmap.glyphs[1].pixels) +local g = bdf:rasterize(bdf:glyphIndex(65), 8) +assert(g.format == "alpha8" and g.meta.advance == 5) +local pcf = assert(F.font.load(pcfBytes)) +assert(pcf:glyphIndex(65) == bdf:glyphIndex(65)) +local psf = assert(F.font.load(psfBytes)) +assert(psf.meta.width == 8 and psf.meta.height == 16 and psf:glyphIndex(65) > 0) +local fnt = assert(F.font.load(fntBytes)) +assert(fnt.unitsPerEm == 16) +local console = assert(F.save(bdf, nil, { format = "psf" })) -- BDF -> PSF 2 +assert(F.font.load(console).meta.source.format == "psf") +``` + +## Type 1, EOT and Hershey + +PostScript Type 1 fonts (PFB and PFA) decrypt and run their charstrings into the same outlines as TrueType ones +(glyph names map to Unicode through the Adobe Glyph List). EOT files unwrap the TrueType font inside (MTX-compressed +ones are unsupported) and write. Hershey JHF fonts are stroke fonts: `outline` gives open paths (`meta.strokes`), +`rasterize` strokes them. + +```luau +local t1 = assert(F.font.load(pfbBytes)) +assert(t1.unitsPerEm == 1000 and t1.info.family == "LMRoman10") +assert(t1:rasterize(t1:glyphIndex(string.byte("g")), 32).width > 5) +local eot = assert(F.font.load(eotBytes)) +assert(eot.meta.eot.familyName == "DejaVu Sans Mono" and eot:glyphIndex(65) > 0) +local jhf = assert(F.font.load(jhfText)) +assert(jhf.meta.strokes and jhf:outline(1)[1].op == "moveTo") +``` + ## Formats | Format | Read | Write | Notes | @@ -240,13 +280,21 @@ assert(bm:rasterize(bm:glyphIndex(string.byte("H")))) | woff | yes | - | | | woff2 | yes | - | | | bmfont (`.fnt`) | yes | yes | text, XML and binary descriptors; `bake` from any outline font | +| bdf (`.bdf`) | yes | yes | BDF 2.1; writes Unicode BDF from any font | +| pcf (`.pcf`, `.pcf.gz`) | yes | - | every glyph pad / bit / byte order | +| psf (`.psf`, `.psfu`) | yes | yes | PSF 1 / 2 with Unicode tables | +| fon (`.fon`, `.fnt`) | yes | - | Windows raster FNT 2 / 3, NE / PE .fon with several sizes | +| type1 (`.pfb`, `.pfa`) | yes | - | Type 1 charstrings (flex, seac), StandardEncoding / custom encodings | +| eot (`.eot`) | yes | yes | EOT 1.0 / 2.1 / 2.2, XOR; no MTX | +| hershey (`.jhf`) | yes | - | stroke fonts | See [FORMATS.md](../FORMATS.md) for the capability matrix. ## Gotchas - **No hinting, no shaping.** Glyphs are rendered from the outlines without hinting; there is no GSUB (ligatures, - contextual forms), no variable fonts (CFF2 / gvar return `UNSUPPORTED`) and no colour or bitmap glyphs. + contextual forms), no variable fonts (CFF2 / gvar return `UNSUPPORTED`) and no colour glyphs or embedded bitmap + strikes in TrueType fonts (bitmap font formats are separate: see above). - **`px` is the em size**, not the line height. The line height is `(ascent - descent + lineGap) * px / unitsPerEm`. - **Units differ by kind.** Outline fonts measure in font units; BMFont in pixels at its baked size. Use `font.unitsPerEm` for both: for a BMFont, `px / unitsPerEm` is the scale relative to the baked size. diff --git a/tools/fixtures/docs_arch2.py b/tools/fixtures/docs_arch2.py new file mode 100644 index 0000000..28e9b37 --- /dev/null +++ b/tools/fixtures/docs_arch2.py @@ -0,0 +1,70 @@ +# Docs inputs for the archive page's encodings / disk images / LHA-ARJ-Zoo sections and the font page's bitmap and +# Type 1 sections (tools/fixtures.py; tools/docs_check.py explains the inputs): a uuencoded file (sharutils), an +# .lzh (LHa for UNIX), an .arj (arj), a .zoo (built as tools/fixtures/zoo.py does), a D64 (VICE c1541), an ADF +# (amitools xdftool), a FAT floppy (mkfs.fat + mtools), BDF / PSF from Spleen, a PCF by bdftopcf, a .fnt by FontForge, +# Latin Modern's lmr10.pfb, an EOT around DejaVu Sans Mono (built as tools/fixtures/eot.py does) and a JHF text. +# tests/gen/docs_arch2.luau { files = { [name] = bytes }, vars = { uuText = "notes.uu", ... } } +import importlib.util, json, os, shutil, subprocess + +HERE = os.path.dirname(os.path.abspath(__file__)) +INPUTS = ["tools/docs_check.py", "tools/fixtures/_arch2.py", "tools/fixtures/zoo.py", "tools/fixtures/eot.py", "tools/fixtures/hershey.py"] + + +def mod(name, path): + spec = importlib.util.spec_from_file_location(name, path) + m = importlib.util.module_from_spec(spec) + spec.loader.exec_module(m) + return m + + +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 generate(ctx): + dc = mod("docs_check", os.path.join(ctx.root, "tools", "docs_check.py")) + A = mod("_arch2", os.path.join(HERE, "_arch2.py")) + zoo = mod("zoo_fx", os.path.join(HERE, "zoo.py")) + eot = mod("eot_fx", os.path.join(HERE, "eot.py")) + hershey = mod("hershey_fx", os.path.join(HERE, "hershey.py")) + os.environ["MTOOLS_SKIP_CHECK"] = "1" + os.environ["TZ"] = "UTC" + files = {} + tmp = A.tmpdir() + try: + tree = [("readme.txt", b"formats-luau retro archives\n"), ("src/main.c", b"int main(void) { return 0; }\n")] + A.tree(tree, tmp) + files["notes.uu"] = A.tool(["sharutils"], ["uuencode", "readme.txt", "readme.txt"], cwd=tmp) + A.tool([], ["env", "NIXPKGS_ALLOW_UNFREE=1", "nix", "shell", "--impure", "nixpkgs#lha", "-c", "lha", "-a2o5q", "tree.lzh", "readme.txt", "src"], cwd=tmp) + files["tree.lzh"] = open(os.path.join(tmp, "tree.lzh"), "rb").read() + A.tool(["arj"], ["arj", "a", "-m1", "-r", "-y", "tree.arj", "readme.txt", "src"], cwd=tmp) + files["tree.arj"] = open(os.path.join(tmp, "tree.arj"), "rb").read() + data = tree[0][1] + files["tree.zoo"] = zoo.archive([("readme.txt", "", 1, zoo.lzw(data), len(data), zoo.crc16(data), 2, 0, None)]) + A.tool(["vice"], ["c1541", "-format", "games,gd", "d64", "games.d64", "-write", "readme.txt", "readme"], cwd=tmp) + files["games.d64"] = open(os.path.join(tmp, "games.d64"), "rb").read() + A.tool(["uv"], ["uvx", "--from", "amitools==0.8.1", "xdftool", "work.adf", "-f", "create", "size=880K", "+", "format", "Work", "ffs", + "+", "makedir", "s", "+", "write", "readme.txt", "s/readme"], cwd=tmp) + files["work.adf"] = open(os.path.join(tmp, "work.adf"), "rb").read() + A.tool(["dosfstools"], ["mkfs.fat", "-C", "-F", "12", "-n", "DOCS", "floppy.img", "720"], cwd=tmp) + A.tool(["mtools"], ["mcopy", "-s", "-m", "-i", "floppy.img", "readme.txt", "src", "::/"], cwd=tmp) + files["floppy.img"] = open(os.path.join(tmp, "floppy.img"), "rb").read() + spleen = os.path.join(store("spleen"), "share", "fonts", "misc") + files["spleen-5x8.bdf"] = open(os.path.join(spleen, "spleen-5x8.bdf"), "rb").read() + A.tool(["xorg.bdftopcf"], ["bdftopcf", "-o", os.path.join(tmp, "spleen.pcf"), os.path.join(spleen, "spleen-5x8.bdf")]) + files["spleen-5x8.pcf"] = open(os.path.join(tmp, "spleen.pcf"), "rb").read() + files["spleen-8x16.psfu"] = open(os.path.join(store("spleen"), "share", "consolefonts", "spleen-8x16.psfu"), "rb").read() + A.tool(["fontforge"], ["fontforge", "-lang=ff", "-c", 'Open($1); Generate($2, "fnt")', os.path.join(spleen, "spleen-8x16.bdf"), os.path.join(tmp, "spleen.fnt")]) + fnt = [f for f in os.listdir(tmp) if f.endswith(".fnt")][0] + files["spleen.fnt"] = open(os.path.join(tmp, fnt), "rb").read() + files["lmr10.pfb"] = open(os.path.join(store("lmodern"), "share", "fonts", "type1", "public", "lm", "lmr10.pfb"), "rb").read() + dejavu = os.path.join(store("dejavu_fonts"), "share", "fonts", "truetype", "DejaVuSansMono.ttf") + facts = json.loads(ctx.python(["fonttools"], eot.CODE, input=json.dumps([dejavu]).encode()))[0] + files["mono.eot"] = eot.eot(open(dejavu, "rb").read(), facts, 0x00020001) + files["roman.jhf"] = (hershey.record(699, -8, 8, []) + "\n" + hershey.record(501, -9, 9, [[(0, -12), (-8, 9)], [(0, -12), (8, 9)], [(-5, 2), (5, 2)]]) + "\n").encode() + finally: + A.rmtree(tmp) + dc.emit_inputs(ctx, files, {"uuText": "notes.uu", "lzhBytes": "tree.lzh", "arjBytes": "tree.arj", "zooBytes": "tree.zoo", + "d64Bytes": "games.d64", "adfBytes": "work.adf", "floppyBytes": "floppy.img", + "bdfText": "spleen-5x8.bdf", "pcfBytes": "spleen-5x8.pcf", "psfBytes": "spleen-8x16.psfu", + "fntBytes": "spleen.fnt", "pfbBytes": "lmr10.pfb", "eotBytes": "mono.eot", "jhfText": "roman.jhf"})