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

This commit is contained in:
4DBug committed 2026-10-31 15:30:20 -05:00
1 parent 53b41c7275
commit cb5237aaa9
3 files changed
+189 -1

No files matched your search

+70
View File
@@ -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).
+49 -1
View File
@@ -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.