Files
mandy/tests/test_pc_prompt_glyphs.py
ubiquitarianism 95bc33f6e2 Built-in keyboard prompts, and both prompt movies built for every player
tools/pc_prompt_glyphs.py draws its own key caps, arrow keys and mouse in
code when --glyphs is not given (no pictures, Pillow or font), with the keys
of the runtime's on-foot table, each inside the outline of the Xbox glyph it
replaces and covering no more of the canvas (measured on the user's movie,
numbers only). tools/prompt_glyph_movies.py builds pc_ and ps_prompt_glyphs.gfx
over the title's movie (standalone if data.rpf cannot be read) into
d3d12/target, where leonida.exe looks; play-nvidia.ps1 runs it before a start
and make_game.py in its runtime stage. A movie the player made is never
touched or shadowed; its own are stamped and rebuilt only when inputs change.
2026-11-29 21:49:50 -05:00

339 lines
18 KiB
Python

"""tools/pc_prompt_glyphs.py: pictures made here are traced into shapes that draw back to the same pictures, lines
that repeat are defined once, and the movie written from them reads back as what was traced. No artwork and no
game file: every picture is drawn by the tool's own rasteriser or written pixel by pixel below."""
import io
import math
import os
import re
import struct
import sys
import tempfile
import unittest
import zlib
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "tools"))
import pc_prompt_glyphs as pc # noqa: E402
import ps_prompt_glyphs as swf # noqa: E402
N = sorted(swf.GLYPHS)
PICTURES = pc.synthetic()
def filtered_png(w, h, rgba, kinds):
"""A PNG whose rows use the filter types `kinds` in turn (the tool's own writer uses none)."""
def chunk(kind, body):
return struct.pack(">I", len(body)) + kind + body + struct.pack(">I", zlib.crc32(kind + body))
raw, prev = bytearray(), bytes(w * 4)
for y in range(h):
row, kind = rgba[y * w * 4:(y + 1) * w * 4], kinds[y % len(kinds)]
out = bytearray([kind])
for i, v in enumerate(row):
a = row[i - 4] if i >= 4 else 0
b, c = prev[i], prev[i - 4] if i >= 4 else 0
p = a + b - c
paeth = a if abs(p - a) <= abs(p - b) and abs(p - a) <= abs(p - c) else b if abs(p - b) <= abs(p - c) else c
out.append((v - (0, a, b, (a + b) >> 1, paeth)[kind]) & 255)
raw += out
prev = row
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0))
+ chunk(b"IDAT", zlib.compress(bytes(raw))) + chunk(b"IEND", b""))
def points(contour):
"""The corners of a contour of straight edges."""
start, segs = contour
assert all(s[0] == "L" for s in segs), segs
assert segs[-1][1] == start, "the contour closes"
return [s[1] for s in segs]
def same_ring(a, b):
"""Whether two closed lists of points are one polygon, from any start."""
return len(a) == len(b) and any(a[k:] + a[:k] == b for k in range(len(a)))
class Png(unittest.TestCase):
def test_a_picture_survives_every_filter_type(self):
w, h, rgba = PICTURES["key"]
self.assertEqual(pc.read_png(pc.write_png(w, h, rgba)), (w, h, rgba))
for kinds in ([1], [2], [3], [4], [0, 1, 2, 3, 4]):
self.assertEqual(pc.read_png(filtered_png(w, h, rgba, kinds)), (w, h, rgba), kinds)
def test_what_is_not_a_picture_it_reads_is_refused(self):
with self.assertRaises(ValueError):
pc.read_png(b"GIF89a" + bytes(32))
sixteen_bit = struct.pack(">I4s", 13, b"IHDR") + struct.pack(">IIBBBBB", 1, 1, 16, 6, 0, 0, 0) + bytes(4)
with self.assertRaises(ValueError):
pc.read_png(b"\x89PNG\r\n\x1a\n" + sixteen_bit)
class Tracing(unittest.TestCase):
def test_an_edge_is_placed_by_its_pixel_s_coverage(self):
# A black box whose edges fall inside pixels: left at 20.25, right at 99.5, top at 30.75, bottom at 60 + 1/3.
size = 264
rows = []
for y in range(size):
cy = min(max(y + 1 - 30.75, 0.0), 1.0) - min(max(y + 1 - (60 + 1 / 3), 0.0), 1.0)
row = bytearray()
for x in range(size):
cx = min(max(x + 1 - 20.25, 0.0), 1.0) - min(max(x + 1 - 99.5, 0.0), 1.0)
row += bytes((0, 0, 0, round(255 * cx * cy)))
rows.append(bytes(row))
(piece,) = pc.pieces_of(size, size, b"".join(rows))
self.assertEqual((piece["colour"], piece["depth"], piece["cuts"]), ("K", 0, []))
for got, want in zip(pc.bounds(piece["pts"]), (20.25, 30.75, 99.5, 60 + 1 / 3)):
self.assertAlmostEqual(got, want, delta=0.01)
# Fitted: four straight edges through those places (in pixels here, no rounding to twips).
corners = points(pc.fit_outline(piece["pts"], 0.12))
self.assertEqual(len(corners), 4)
for x, y in corners:
self.assertAlmostEqual(min(abs(x - 20.25), abs(x - 99.5)), 0, delta=0.02)
self.assertAlmostEqual(min(abs(y - 30.75), abs(y - (60 + 1 / 3))), 0, delta=0.02)
def test_a_key_comes_back_corner_for_corner(self):
# The picture: an outline with 3-unit chamfers at (4, 6), a face with 1-unit chamfers 3 inside it, a triangle.
glyphs = pc.GlyphSet()
glyphs.add(4, *PICTURES["key"])
(k, (outline,)), (w, (face,)), (k2, (arrow,)) = glyphs.plan()[4]
self.assertEqual((k, w, k2), ("K", "W", "K"))
t = swf.TWIP
at = lambda pts: [((4 + x) * t, (6 + y) * t) for x, y in pts] # noqa: E731
key = [(3, 0), (29, 0), (32, 3), (32, 29), (29, 32), (3, 32), (0, 29), (0, 3)]
self.assertTrue(same_ring(points(outline), at(key)), points(outline))
lit = [(4, 3), (28, 3), (29, 4), (29, 28), (28, 29), (4, 29), (3, 28), (3, 4)]
self.assertTrue(same_ring(points(face), at(lit)), points(face))
self.assertTrue(same_ring(points(arrow), at([(16, 9), (24, 22), (8, 22)])), points(arrow))
# Every contour runs clockwise on screen: its inside is on the right, as fill style 1 wants.
for contour in (outline, face, arrow):
self.assertGreater(swf.area(points(contour)), 0)
def test_a_curve_stays_within_the_tolerance(self):
glyphs = pc.GlyphSet()
glyphs.add(4, *PICTURES["bar"])
(k, (disc,)), (g, (inner,)), (k2, (bar,)) = glyphs.plan()[4]
self.assertEqual((k, g, k2), ("K", "G", "K"))
for contour, radius in ((disc, 31), (inner, 28)):
start, segs = contour
self.assertTrue(all(s[0] == "Q" for s in segs) and 6 <= len(segs) <= 16, segs)
here, worst = start, 0.0
for _, control, end in segs:
for k in range(9):
u = k / 8
x = (1 - u) ** 2 * here[0] + 2 * u * (1 - u) * control[0] + u * u * end[0]
y = (1 - u) ** 2 * here[1] + 2 * u * (1 - u) * control[1] + u * u * end[1]
worst = max(worst, abs(math.hypot(x - 32 * swf.TWIP, y - 32 * swf.TWIP) - radius * swf.TWIP))
here = end
# The tolerance, half a twip of rounding, and what the tracing itself is out by.
self.assertLess(worst / swf.TWIP, pc.TOLERANCE + 0.025 + 0.03, (radius, worst))
# The bar's straight sides are straight edges, its round ends curves.
self.assertEqual(sum(1 for s in bar[1] if s[0] == "L"), 2)
def test_a_hole_through_the_picture_is_cut_out_not_painted(self):
w, h, rgba = PICTURES["ring"]
glyphs = pc.GlyphSet()
glyphs.add(4, w, h, rgba)
(k, ring), (white, (dot,)) = glyphs.plan()[4]
self.assertEqual((k, white, len(ring)), ("K", "W", 2))
outer, hole = ring
self.assertGreater(swf.area([s[-1] for s in outer[1]]), 0)
self.assertLess(swf.area([s[-1] for s in hole[1]]), 0, "the hole runs the other way round")
drawn = pc.draw([(pc.RGBA[c], cs) for c, cs in glyphs.plan()[4]], w, glyphs.scale)
centre = (pc.MARGIN + 32) * glyphs.scale
self.assertEqual(drawn.alpha[centre][centre], 0)
self.assertEqual(drawn.alpha[centre][centre + 20 * glyphs.scale], 255)
total, bad, worst, opaque = pc.difference(drawn, rgba)
self.assertLess(total, 0.004 * opaque)
self.assertEqual(bad, 0)
def test_a_line_that_repeats_is_defined_once(self):
glyphs = pc.GlyphSet()
for n, name in ((4, "key"), (5, "key unlit"), (6, "key moved"), (7, "key")):
glyphs.add(n, *PICTURES[name])
# Three lines in all: the outline, the face, the triangle; the moved key is the same three elsewhere.
self.assertEqual(len(glyphs.lib.items), 3)
movie = swf.Movie(compact=True)
placed = {n: glyphs.sprite(movie, n).items for n in (4, 5, 6, 7)}
visible = [key for key in movie.order if key[1][3]]
self.assertEqual(len(visible), 4, "outline, white face, grey face, triangle")
self.assertEqual(placed[4], placed[7])
by_id = lambda items: [i for i, _, _ in items] # noqa: E731
self.assertEqual(by_id(placed[4]), by_id(placed[6]), "the moved key places the same shapes")
moved = {(x6 - x4, y6 - y4) for (_, x4, y4), (_, x6, y6) in zip(placed[4][1:], placed[6][1:])}
self.assertEqual(moved, {(24 * swf.TWIP, 18 * swf.TWIP)})
self.assertEqual(by_id(placed[4])[1], by_id(placed[5])[1], "lit or not, one outline")
self.assertNotEqual(by_id(placed[4])[2], by_id(placed[5])[2], "the face is another colour")
def test_pictures_must_be_the_canvas_at_a_whole_scale(self):
glyphs = pc.GlyphSet()
with self.assertRaises(ValueError):
glyphs.add(4, 100, 100, bytes(100 * 100 * 4))
glyphs.add(4, *PICTURES["key"])
with self.assertRaises(ValueError):
glyphs.add(5, 132, 132, bytes(132 * 132 * 4))
class Movie(unittest.TestCase):
@classmethod
def setUpClass(cls):
names = sorted(PICTURES)
cls.pictures = {n: PICTURES[names[i % len(names)]] for i, n in enumerate(N)}
cls.glyphs = pc.trace(cls.pictures)
def test_a_shape_reads_back_as_it_was_written(self):
contours = [
((0, 0), [("L", (400, 0)), ("L", (400, 300)), ("Q", (410, 520), (200, 520)), ("L", (-3, 517)),
("L", (0, 0))]),
((100, 100), [("L", (100, 200)), ("L", (33000, 200)), ("L", (100, 100))]),
]
for compact in (False, True):
body = bytes(2) + swf.contour_bytes(contours, (1, 2, 3, 255), compact)
self.assertEqual(pc.read_shape(body), ((1, 2, 3, 255), contours))
# A level or upright edge takes the short record, and an edge of no length none at all.
def edge(end, compact):
return len(swf.contour_bytes([((0, 0), [("L", end)])], (0, 0, 0, 255), compact))
self.assertLess(edge((0, 250), True), edge((250, 250), True))
self.assertLess(edge((0, 0), True), edge((0, 250), True))
self.assertEqual(edge((250, 250), True), edge((250, 250), False))
def test_the_standalone_movie_draws_back_to_the_pictures(self):
data = pc.build(self.glyphs, pc.MOVIE_SIZE)
self.assertEqual(data[:4], b"GFX*")
self.assertEqual(struct.unpack_from("<I", data, 4)[0], len(data))
self.assertEqual(len(data), pc.MOVIE_SIZE)
placed = pc.read_movie(data)
self.assertEqual(sorted(placed), N)
plan = self.glyphs.plan()
for n in N:
w, h, rgba = self.pictures[n]
# What the movie holds is what was traced, shape for shape (after an invisible rectangle, if any).
seen = [(colour, contours) for colour, contours in placed[n] if colour[3]]
traced = [(pc.RGBA[c], [(tuple(s), [tuple(e) for e in segs]) for s, segs in cs]) for c, cs in plan[n]]
self.assertEqual(seen, traced, n)
total, bad, worst, opaque = pc.difference(pc.draw(placed[n], w, self.glyphs.scale), rgba)
self.assertLess(total, 0.004 * opaque, n)
self.assertEqual(bad, 0, n)
def test_a_sprite_is_never_smaller_than_the_glyph_it_stands_in_for(self):
placed = pc.read_movie(pc.build(self.glyphs))
for n in N:
xs = [p[0] for _, cs in placed[n] for start, segs in cs for p in [start] + [q for s in segs for q in s[1:]]]
ys = [p[1] for _, cs in placed[n] for start, segs in cs for p in [start] + [q for s in segs for q in s[1:]]]
x0, y0, x1, y1 = swf.BOUNDS.get(n, (0, 0, swf.BOX, swf.BOX))
self.assertTrue(min(xs) <= x0 + 2 and min(ys) <= y0 + 2 and max(xs) >= x1 - 2 and max(ys) >= y1 - 2, n)
def test_built_over_another_movie_only_its_drawings_change(self):
# Stand-in for the title's movie: the PlayStation tool's own, padded as the title's is long.
base = swf.pad_movie(swf.build(), pc.MOVIE_SIZE)
data = pc.build_over(base, self.glyphs)
self.assertEqual(len(data), len(base))
self.assertEqual(struct.unpack_from("<I", data, 4)[0], len(data))
other = lambda movie: [(c, b) for c, b in swf.read_tags(movie)[0] if c not in (32, 39, 77)] # noqa: E731
self.assertEqual(other(data), other(base), "exports, ActionScript and class table are kept as they are")
tags = swf.read_tags(data)[0]
self.assertEqual(swf.exports_of(tags), swf.exports_of(swf.read_tags(base)[0]), "the sprites keep their ids")
self.assertLessEqual(pc.used(data), pc.budget(base))
placed = pc.read_movie(data)
w, h, rgba = self.pictures[30]
total, bad, worst, opaque = pc.difference(pc.draw(placed[30], w, self.glyphs.scale), rgba)
self.assertLess(total, 0.004 * opaque)
def test_a_set_that_does_not_fit_is_refused(self):
# A movie with no drawings to give up: no shapes, and sprites that place nothing.
full = swf.build()
tags, start = swf.read_tags(full)
bare = [swf.tag(code, body[:4] + swf.tag(0, b"") if code == 39 else body) for code, body in tags if code != 32]
base = full[:4] + struct.pack("<I", start + sum(map(len, bare))) + full[8:start] + b"".join(bare)
self.assertLess(pc.budget(base), 400)
with self.assertRaises(ValueError):
pc.build_over(base, self.glyphs)
KBM = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "d3d12", "crates", "leonida-hle", "src",
"modules", "gameinput", "kbm.rs")
# The slot of each pad input of the runtime's keyboard table (docs/prompt_glyphs.md, "N to button").
SLOT_OF = {"A": 30, "B": 31, "X": 32, "Y": 33, "LEFT_SHOULDER": 34, "RIGHT_SHOULDER": 36, "LEFT_STICK": 16,
"RIGHT_STICK": 25, "MENU": 38, "VIEW": 39, "UP": 4, "DOWN": 5, "LEFT": 6, "RIGHT": 7,
"LeftTrigger": 35, "RightTrigger": 37}
# What the built-in set calls each key of that table.
KEY_LABEL = {"VK_SPACE": "SPACE", "VK_LCONTROL": "CTRL", "VK_LSHIFT": "SHIFT", "VK_TAB": "TAB", "VK_ESCAPE": "ESC",
"VK_ENTER_EXIT": "F", "VK_UP": pc.UP, "VK_DOWN": pc.DOWN, "VK_LEFT": pc.LEFT, "VK_RIGHT": pc.RIGHT,
"VK_MOUSE_LEFT": "left", "VK_MOUSE_RIGHT": "right"}
class Builtin(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.pictures = pc.builtin()
cls.glyphs = pc.trace(cls.pictures)
cls.movie = pc.build(cls.glyphs, pc.MOVIE_SIZE)
def test_every_slot_has_a_picture_at_the_tracer_s_size(self):
self.assertEqual(sorted(pc.BUILTIN), N)
self.assertEqual(sorted(self.pictures), N)
for n, (w, h, rgba) in self.pictures.items():
self.assertEqual((w, h, len(rgba)), (264, 264, 264 * 264 * 4), n)
self.assertTrue(any(rgba[3::4]), n)
# One drawing for the slots that show the same thing.
self.assertIs(self.pictures[17], self.pictures[58])
self.assertIs(self.pictures[21], self.pictures[59])
def test_each_picture_fits_the_xbox_glyph_it_stands_in_for(self):
for n, picture in self.pictures.items():
inside_all, covered, xbox = pc.footprint(n, *picture)
self.assertTrue(inside_all, "N %d leaves the Xbox glyph's outline" % n)
self.assertLessEqual(covered, xbox, n)
self.assertGreater(covered, 0.15 * xbox, n)
def test_the_labels_are_the_keys_of_the_runtime_s_on_foot_table(self):
with open(KBM, encoding="utf-8") as f:
source = f.read()
bound = {}
for table in ("SHARED", "FOOT_ONLY"):
body = source.split("const %s: &[Bind] = &[" % table, 1)[1].split("];", 1)[0]
for key, to in re.findall(r"\((VK_\w+|b'.'), To::(\w+(?:\(\w+\))?)\)", body):
pad = to[len("Button("):-1] if to.startswith("Button(") else to
label = KEY_LABEL.get(key, key[2:-1] if key.startswith("b'") else key)
bound.setdefault(pad, set()).add(label)
for pad, slot in SLOT_OF.items():
kind, *what = pc.BUILTIN[slot]
label = what[0]
self.assertIn(label, bound.get(pad, ()), "N %d (%s) shows %r, the table has %s"
% (slot, pad, label, sorted(bound.get(pad, ()))))
# The sticks: W/A/S/D and the mouse.
for slot in (12, 13, 14, 15, 17, 18, 19, 20, 58):
self.assertTrue(set(str(pc.BUILTIN[slot][1:])) & set("WASD"), slot)
for slot in (21, 22, 23, 24, 26, 27, 28, 29, 59):
self.assertEqual(pc.BUILTIN[slot][0], "mouse", slot)
def test_the_padded_movie_is_the_title_s_length_and_draws_back_to_the_pictures(self):
data = self.movie
self.assertEqual(len(data), pc.MOVIE_SIZE)
self.assertEqual(len(data), 28148)
self.assertEqual(data[:4], b"GFX*")
self.assertEqual(struct.unpack_from("<I", data, 4)[0], len(data))
self.assertLessEqual(pc.used(data), 15226, "fits where the title's own drawings were")
placed = pc.read_movie(data)
self.assertEqual(sorted(placed), N)
for n in N:
w, h, rgba = self.pictures[n]
total, bad, worst, opaque = pc.difference(pc.draw(placed[n], w, self.glyphs.scale), rgba)
self.assertLess(total, 0.01 * opaque, n)
self.assertLessEqual(bad, 2, n)
def test_the_command_line_builds_the_built_in_set_without_glyphs(self):
with tempfile.TemporaryDirectory() as tmp:
out = os.path.join(tmp, "pc_prompt_glyphs.gfx")
was, sys.stdout = sys.stdout, io.StringIO()
try:
pc.main(["--pad-inflated", str(pc.MOVIE_SIZE), "--out", out, "--quiet"])
finally:
sys.stdout = was
with open(out, "rb") as f:
self.assertEqual(f.read(), self.movie)
if __name__ == "__main__":
unittest.main()