Files
neoLegacy-mac/tests/test_reconstructed_gr_strokepath.py
2026-07-17 21:15:30 -05:00

343 lines
16 KiB
Python

"""Runtime tests for the reconstructed gr_strokepath translation unit.
RED STATE: gr_strokepath.c / gr_strokepath.h do not exist yet, so setUpClass
raises "missing gr_strokepath runtime inputs". Every assertion below is
grounded in a specific line of iggy-decompiled/raw/durango/gr_strokepath.c
(cited inline) so a faithful DURANGO reconstruction turns these green.
gr_strokepath is RAD's path-stroking / stroke-antialiasing renderer. It
converts a fill/shape path into a stroked outline, exports three functions
(iggy_stroke_path, iggy_stroke_path_for_antialiasing, iggy_stroke_path_free)
and owns exactly one named data global -- the antialias-width float
iggy_aawidth (0x3faccccd == 1.35f in every retail object's .data).
Struct offsets are the 64-bit DURANGO offsets read directly from the raw
decompilation; the dylib is built for the 64-bit host with the
IGGY_RECONSTRUCT_GR_STROKEPATH_DURANGO code path selected, exactly like the
sibling iggy_font_provider / stb_truetype_wrapper runtime tests.
Deep stroke geometry (the exact stroked-vertex output for a contour, join /
cap / miter tesselation, curve flattening, and the aawidth -> segment-density
effect) is intentionally NOT pinned here -- it is left for the
implementation+review phase. What is pinned is real, deterministic
collaborator behavior: the iggy_stroke_path_free release sequence, the
temp-alloc / temp-free lifecycle, the 9-slice set-stroke-xform dispatch, the
NaN early-out, and the iggy_aawidth default.
"""
import ctypes
import math
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed"
SOURCE = RECONSTRUCTED / "gr_strokepath.c"
HEADER = RECONSTRUCTED / "gr_strokepath.h"
SUPPORT = ROOT / "tests/support/gr_strokepath_test_support.c"
# 64-bit DURANGO context-struct field offsets (raw/durango cited inline).
CTX_SIZE = 0x3600
CTX_STROKE_WIDTH = 0x1E80 # *(ctx+0x1e80) squared into local_3fc (raw:2983)
CTX_9SLICE_ENABLE = 0x2298 # if != 0 -> 9-slice stroke path (raw:3003)
CTX_9SLICE_SUB = 0x229C # inner 9-slice branch selector (raw:3008)
CTX_TEMP_SIZE = 0x34DC # temp-arena byte count read by init_buffers (raw:3586)
# 64-bit contour-list (param_6) field: element count at +0x18 (raw:3011,3179).
CONTOUR_COUNT = 0x18
class ReconstructedGrStrokepathTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
missing = [str(path) for path in (SOURCE, HEADER, SUPPORT)
if not path.exists()]
if missing:
raise AssertionError(
"missing gr_strokepath runtime inputs: " + ", ".join(missing))
cls.temp = tempfile.TemporaryDirectory()
output = Path(cls.temp.name) / "gr_strokepath.dylib"
build = subprocess.run([
"clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra",
"-Werror", "-Wl,-undefined,dynamic_lookup",
"-DIGGY_RECONSTRUCT_GR_STROKEPATH_DURANGO=1",
"-I", str(RECONSTRUCTED),
str(SOURCE), str(SUPPORT), "-o", str(output),
], capture_output=True, text=True)
if build.returncode:
raise AssertionError(build.stdout + build.stderr)
cls.lib = ctypes.CDLL(str(output))
lib = cls.lib
# Harness observability.
lib.gr_strokepath_test_reset.argtypes = []
lib.gr_strokepath_test_fail_next_malloc.argtypes = []
lib.gr_strokepath_test_fail_next_temp_alloc.argtypes = []
for name in (
"gr_strokepath_test_malloc_count",
"gr_strokepath_test_free_alloc_count",
"gr_strokepath_test_temp_alloc_count",
"gr_strokepath_test_temp_free_count",
"gr_strokepath_test_last_malloc_size",
"gr_strokepath_test_last_temp_alloc_size",
"gr_strokepath_test_last_temp_free_size",
"gr_strokepath_test_slice_set_xform_count",
"gr_strokepath_test_slice_scale_count",
"gr_strokepath_test_isnan_count"):
getattr(lib, name).restype = ctypes.c_size_t
getattr(lib, name).argtypes = []
lib.gr_strokepath_test_last_malloc_tag.restype = ctypes.c_int
lib.gr_strokepath_test_last_malloc_tag.argtypes = []
for name in (
"gr_strokepath_test_last_temp_free_ctx",
"gr_strokepath_test_last_slice_set_ctx",
"gr_strokepath_test_last_slice_set_xform",
"gr_strokepath_test_last_slice_set_translate",
"gr_strokepath_test_last_slice_scale_ctx"):
getattr(lib, name).restype = ctypes.c_void_p
getattr(lib, name).argtypes = []
lib.gr_strokepath_test_freed_ctx.restype = ctypes.c_void_p
lib.gr_strokepath_test_freed_ctx.argtypes = [ctypes.c_size_t]
lib.gr_strokepath_test_freed_ptr.restype = ctypes.c_void_p
lib.gr_strokepath_test_freed_ptr.argtypes = [ctypes.c_size_t]
# Exported ABI (64-bit DURANGO signatures recovered from raw).
lib.iggy_stroke_path.restype = ctypes.c_int64
lib.iggy_stroke_path.argtypes = [
ctypes.c_void_p, # param_1 player/context
ctypes.c_void_p, # param_2 iggy context
ctypes.c_void_p, # param_3 xform (2x2, nullable)
ctypes.c_void_p, # param_4 translate
ctypes.c_void_p, # param_5
ctypes.c_void_p, # param_6 contour list
ctypes.c_float, # param_7 morph factor
ctypes.c_void_p, # param_8 morph target (nullable)
ctypes.c_int32, # param_9 flag
ctypes.c_int32, # param_10 flag
ctypes.c_void_p, # param_11
ctypes.c_void_p, # param_12
]
lib.iggy_stroke_path_for_antialiasing.restype = ctypes.c_int64
lib.iggy_stroke_path_for_antialiasing.argtypes = [
ctypes.c_void_p, # param_1 player/context
ctypes.c_void_p, # param_2 iggy context
ctypes.c_void_p, # param_3 xform (nullable)
ctypes.c_void_p, # param_4 translate (NaN-checked)
ctypes.c_float, # param_5 aawidth divisor
ctypes.c_void_p, # param_6 segment array
ctypes.c_int32, # param_7 contour count
ctypes.c_int32, # param_8 flag
ctypes.c_uint32, # param_9 direction flag
]
lib.iggy_stroke_path_free.restype = None
lib.iggy_stroke_path_free.argtypes = [
ctypes.c_void_p, ctypes.c_void_p]
@classmethod
def tearDownClass(cls):
cls.temp.cleanup()
def setUp(self):
self.lib.gr_strokepath_test_reset()
def make_ctx(self, temp_size=0x1000, slice_enable=0, slice_sub=0):
buf = (ctypes.c_uint8 * CTX_SIZE)()
addr = ctypes.addressof(buf)
ctypes.c_int32.from_address(addr + CTX_TEMP_SIZE).value = temp_size
ctypes.c_int32.from_address(addr + CTX_9SLICE_ENABLE).value = slice_enable
ctypes.c_int32.from_address(addr + CTX_9SLICE_SUB).value = slice_sub
ctypes.c_float.from_address(addr + CTX_STROKE_WIDTH).value = 1.0
return buf
def empty_contour(self):
buf = (ctypes.c_uint8 * 0x40)()
ctypes.c_int32.from_address(
ctypes.addressof(buf) + CONTOUR_COUNT).value = 0
return buf
# ------------------------------------------------------------------ #
# Owned global
# ------------------------------------------------------------------ #
def test_iggy_aawidth_default_is_one_point_three_five(self):
# durango .data holds cd cc ac 3f == 0x3faccccd == 1.35f; used as
# (iggy_aawidth / param_5) * 0.5 at raw/durango:3449.
aawidth = ctypes.c_float.in_dll(self.lib, "iggy_aawidth")
self.assertAlmostEqual(aawidth.value, 1.35, places=5)
# ------------------------------------------------------------------ #
# iggy_stroke_path_free -- exact release sequence
# ------------------------------------------------------------------ #
def test_stroke_path_free_releases_two_node_list_in_order(self):
# raw/durango:3558-3571 per node: free node[3], node[2], node itself;
# next = node[0]. 64-bit node is void*[]: [0]=next, [2]=bufA, [3]=bufB.
node_b = (ctypes.c_void_p * 4)(None, None, 0x2222, 0x3333)
node_a = (ctypes.c_void_p * 4)(
ctypes.addressof(node_b), None, 0xAAAA, 0xBBBB)
self._keep = (node_a, node_b)
ctx = 0x1234
self.lib.iggy_stroke_path_free(ctx, ctypes.addressof(node_a))
self.assertEqual(self.lib.gr_strokepath_test_free_alloc_count(), 6)
# Node A first: buf[3]=0xBBBB, buf[2]=0xAAAA, then the node itself.
self.assertEqual(self.lib.gr_strokepath_test_freed_ptr(0), 0xBBBB)
self.assertEqual(self.lib.gr_strokepath_test_freed_ptr(1), 0xAAAA)
self.assertEqual(
self.lib.gr_strokepath_test_freed_ptr(2), ctypes.addressof(node_a))
# Node B (reached via node_a[0]): buf[3]=0x3333, buf[2]=0x2222, node.
self.assertEqual(self.lib.gr_strokepath_test_freed_ptr(3), 0x3333)
self.assertEqual(self.lib.gr_strokepath_test_freed_ptr(4), 0x2222)
self.assertEqual(
self.lib.gr_strokepath_test_freed_ptr(5), ctypes.addressof(node_b))
# The context/tag (param_1) is passed straight through to every free.
self.assertEqual(self.lib.gr_strokepath_test_freed_ctx(0), ctx)
self.assertEqual(self.lib.gr_strokepath_test_freed_ctx(5), ctx)
def test_stroke_path_free_null_list_is_a_no_op(self):
# raw/durango:3563 the while-guard rejects a null head immediately.
self.lib.iggy_stroke_path_free(0, None)
self.assertEqual(self.lib.gr_strokepath_test_free_alloc_count(), 0)
# ------------------------------------------------------------------ #
# iggy_stroke_path -- lifecycle + 9-slice collaboration
# ------------------------------------------------------------------ #
def test_stroke_path_temp_alloc_failure_returns_zero_without_free(self):
# raw/durango:2999-3002 init_buffers' iggy_temp_alloc fails ->
# local_80 (builder+0x3c8) == 0 -> return 0 BEFORE the final
# iggy_temp_free at raw:3339 and before any 9-slice call.
ctx = self.make_ctx()
xform = (ctypes.c_float * 4)(1.0, 0.0, 0.0, 1.0)
translate = (ctypes.c_float * 2)(0.0, 0.0)
contour = self.empty_contour()
self.lib.gr_strokepath_test_fail_next_temp_alloc()
result = self.lib.iggy_stroke_path(
None, ctypes.addressof(ctx), ctypes.addressof(xform),
ctypes.addressof(translate), None, ctypes.addressof(contour),
0.0, None, 0, 0, None, None)
self.assertEqual(result, 0)
self.assertEqual(self.lib.gr_strokepath_test_temp_alloc_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_temp_free_count(), 0)
self.assertEqual(self.lib.gr_strokepath_test_slice_set_xform_count(), 0)
def test_stroke_path_9slice_enabled_calls_set_stroke_xform(self):
# raw/durango:3003-3006 with ctx+0x2298 != 0 the stroke path calls
# iggy_9slice_set_stroke_xform(ctx, param_3, param_4). With
# ctx+0x229c == 0 and an empty contour (count 0) no scale_forstroke /
# add_seg runs; finish_current_chunk allocates nothing (raw:1959-1998
# early-out) and iggy_temp_free(ctx, size) runs once (raw:3339).
ctx = self.make_ctx(slice_enable=1, slice_sub=0)
ctx_addr = ctypes.addressof(ctx)
xform = (ctypes.c_float * 4)(1.0, 0.0, 0.0, 1.0)
translate = (ctypes.c_float * 2)(5.0, 7.0)
contour = self.empty_contour()
result = self.lib.iggy_stroke_path(
None, ctx_addr, ctypes.addressof(xform),
ctypes.addressof(translate), None, ctypes.addressof(contour),
0.0, None, 0, 0, None, None)
self.assertEqual(result, 0)
self.assertEqual(self.lib.gr_strokepath_test_slice_set_xform_count(), 1)
self.assertEqual(
self.lib.gr_strokepath_test_last_slice_set_ctx(), ctx_addr)
self.assertEqual(
self.lib.gr_strokepath_test_last_slice_set_xform(),
ctypes.addressof(xform))
self.assertEqual(
self.lib.gr_strokepath_test_last_slice_set_translate(),
ctypes.addressof(translate))
self.assertEqual(self.lib.gr_strokepath_test_slice_scale_count(), 0)
self.assertEqual(self.lib.gr_strokepath_test_temp_alloc_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_temp_free_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_malloc_count(), 0)
def test_stroke_path_9slice_disabled_skips_set_stroke_xform(self):
# raw/durango:3003 ctx+0x2298 == 0 -> the 9-slice branch is skipped
# entirely; the temp-alloc/temp-free lifecycle still runs once each
# and an empty contour yields no output (return local_418 == 0).
ctx = self.make_ctx(slice_enable=0)
xform = (ctypes.c_float * 4)(1.0, 0.0, 0.0, 1.0)
translate = (ctypes.c_float * 2)(0.0, 0.0)
contour = self.empty_contour()
result = self.lib.iggy_stroke_path(
None, ctypes.addressof(ctx), ctypes.addressof(xform),
ctypes.addressof(translate), None, ctypes.addressof(contour),
0.0, None, 0, 0, None, None)
self.assertEqual(result, 0)
self.assertEqual(self.lib.gr_strokepath_test_slice_set_xform_count(), 0)
self.assertEqual(self.lib.gr_strokepath_test_temp_alloc_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_temp_free_count(), 1)
# ------------------------------------------------------------------ #
# iggy_stroke_path_for_antialiasing -- NaN guard + failure lifecycle
# ------------------------------------------------------------------ #
def test_for_antialiasing_nan_translate_returns_zero_no_alloc(self):
# raw/durango:3433-3434,3549 rr_isnan_f(param_4[0]) (or [1]) -> the
# whole body (init_buffers included) is skipped and 0 is returned.
ctx = self.make_ctx()
translate = (ctypes.c_float * 2)(float("nan"), 0.0)
result = self.lib.iggy_stroke_path_for_antialiasing(
None, ctypes.addressof(ctx), None, ctypes.addressof(translate),
1.0, None, 0, 0, 0)
self.assertEqual(result, 0)
self.assertGreaterEqual(self.lib.gr_strokepath_test_isnan_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_temp_alloc_count(), 0)
def test_for_antialiasing_temp_alloc_failure_returns_zero_no_free(self):
# raw/durango:3470-3473 finite translate passes the NaN guard;
# init_buffers' iggy_temp_alloc fails -> local_e0 == 0 -> local_478 = 0
# is returned and the else-branch iggy_temp_free (raw:3540) never runs.
ctx = self.make_ctx()
translate = (ctypes.c_float * 2)(0.0, 0.0)
self.lib.gr_strokepath_test_fail_next_temp_alloc()
result = self.lib.iggy_stroke_path_for_antialiasing(
None, ctypes.addressof(ctx), None, ctypes.addressof(translate),
1.0, None, 0, 0, 0)
self.assertEqual(result, 0)
self.assertEqual(self.lib.gr_strokepath_test_isnan_count(), 2)
self.assertEqual(self.lib.gr_strokepath_test_temp_alloc_count(), 1)
self.assertEqual(self.lib.gr_strokepath_test_temp_free_count(), 0)
def test_for_antialiasing_finite_translate_passes_nan_guard(self):
# raw/durango:3433-3434 both translate components are checked; a
# finite pair calls rr_isnan_f exactly twice and does not early-out
# (temp-alloc succeeds, so the arena lifecycle is entered).
ctx = self.make_ctx()
translate = (ctypes.c_float * 2)(2.0, 3.0)
self.lib.iggy_stroke_path_for_antialiasing(
None, ctypes.addressof(ctx), None, ctypes.addressof(translate),
1.0, None, 0, 0, 0)
self.assertEqual(self.lib.gr_strokepath_test_isnan_count(), 2)
self.assertGreaterEqual(
self.lib.gr_strokepath_test_temp_alloc_count(), 1)
def test_iggy_aawidth_divisor_is_finite_for_unit_scale(self):
# Sanity guard on the pinned constant: (iggy_aawidth / 1.0) * 0.5
# (raw/durango:3449) must be a normal finite float, matching the
# 1.35f default so the antialias-density math is well-defined.
aawidth = ctypes.c_float.in_dll(self.lib, "iggy_aawidth").value
density = (aawidth / 1.0) * 0.5
self.assertTrue(math.isfinite(density))
self.assertAlmostEqual(density, 0.675, places=4)
if __name__ == "__main__":
unittest.main()