aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_portable_fonts.py (2597B)


      1 """Compare the AO monochrome font path against Windows GDI using local Verdana."""
      2 import pathlib
      3 import subprocess
      4 
      5 
      6 root = pathlib.Path(__file__).resolve().parents[2]
      7 decompiled = root / "client"
      8 work = root / "local/client/portable-fonts"
      9 work.mkdir(parents=True, exist_ok=True)
     10 reference = work / "reference.exe"
     11 subprocess.run([
     12     str(root / "toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"),
     13     "-O2", "tests/fonts.c", "-o", str(reference),
     14 ], cwd=decompiled, check=True)
     15 expected = subprocess.check_output([str(reference), str(work / "reference.metrics"), str(work / "reference.bits")], text=True, timeout=30)
     16 linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:]
     17 linux_work = "/mnt/" + work.drive[0].lower() + work.as_posix()[2:]
     18 wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"]
     19 subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/fonts"], check=True)
     20 actual = subprocess.check_output(wsl + ["./build/linux/fonts", linux_work + "/native.metrics", linux_work + "/native.bits"], text=True, timeout=30)
     21 assert actual == expected, (expected, actual)
     22 assert (work / "reference.metrics").read_bytes() == (work / "native.metrics").read_bytes(), "font metrics"
     23 reference_data = (work / "reference.bits").read_bytes()
     24 native_data = (work / "native.bits").read_bytes()
     25 assert len(reference_data) == len(native_data) == 420 * 512
     26 
     27 # GDI and FreeType can round an outline edge to adjacent pixels. Require every
     28 # ink pixel to be within one pixel of reference ink, in both directions; isolated
     29 # stray pixels or missing strokes fail. Measurements above remain byte-exact.
     30 different_pixels = different_glyphs = maximum_difference = 0
     31 for glyph in range(420):
     32     masks = []
     33     for data in [reference_data, native_data]:
     34         masks.append({(x, y) for y in range(64) for x in range(64)
     35                       if not (data[glyph * 512 + y * 8 + x // 8] & (0x80 >> (x % 8)))})
     36     difference = len(masks[0] ^ masks[1])
     37     different_pixels += difference
     38     different_glyphs += difference != 0
     39     maximum_difference = max(maximum_difference, difference)
     40     for source, target in [(masks[0], masks[1]), (masks[1], masks[0])]:
     41         for x, y in source - target:
     42             assert any((x + dx, y + dy) in target for dx in [-1, 0, 1] for dy in [-1, 0, 1]), (glyph, x, y)
     43 print(actual.strip())
     44 print("All metrics match Windows exactly; glyphs pass the one-pixel outline comparison.")
     45 print("Different ink pixels:", different_pixels, "across", different_glyphs, "glyphs; maximum per glyph:", maximum_difference)