#!/usr/bin/env python3 """The designer's motion instrument (design/motion/motion.py: timing chart, rule checks, contact sheets), told where canim's panels are now and what each scene's captions say. chart.py chart|auto|sheet|onion|track VIDEO ... as motion.py takes them The video's file name (fib-1080.mp4 -> fib) picks its regions, and its captions: the strings `scenes/NAME.zig` says (`says.line`), in order. The CAPTION check (motion.py's) gets one more line here: a caption up for less than its reading time, 90 f and 3.6 f a character as shown. """ import os, re, sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "..", "design", "motion")) import numpy as np import motion W = motion.W # The stage (design/README.md, "5. The frame"; theme.layout.split): the lens band across the # top as two panels, its drawing over the program column and its view over the state column; # the program column (source over disassembly) and the state column (registers over stack). # A panel of n rows is 32 + 24 n. The clock is over the caption. def ph(rows): return 32 + 24 * rows def split(drawing, view, source, disasm, regs, backtrace=0): """Layout A: the disassembly under the source; the registers as tall as the scene's rows (`regs`), the stack the state column's rest above the backtrace, whole slots, any rest above it.""" out = {} if drawing: out["drawing"] = (64, 48, 880, drawing) if view: out["view"] = (976, 48, 880, view) left = 48 + (drawing + 12 if drawing else 0) right = 48 + (view + 12 if view else 0) out["source"] = (64, left, 880, ph(source)) out["disasm"] = (64, left + ph(source) + 12, 880, ph(disasm)) out["registers"] = (976, right, 880, ph(regs)) bottom = 888 - (ph(backtrace) + 12 if backtrace else 0) slots = (bottom - (right + ph(regs) + 12) - 32) // 24 out["stack"] = (976, bottom - ph(slots), 880, ph(slots)) if backtrace: out["backtrace"] = (976, 888 - ph(backtrace), 880, ph(backtrace)) return out # The words that are not panels (motion.WORDS): the clock (its count, ranged right at 1856, # changes in a frame by design) and the caption (its 1400 from the margin). The caption's box # starts under the clock's count and the run's step counter (912 to 934). words = {"clock": (0, 888, 1920, 46), "caption": (0, 940, 1464, 140)} # The splits (theme.layout.split), each scene's registers panel as tall as its rows (the # registers its steps change, the rest folded three to a row: `context.movedBy`). none = lambda regs: split(0, 0, 14, 17, regs, 4) beside = lambda regs: split(152, 152, 8, 17, regs) subject = lambda regs: split(296, 200, 8, 11, regs) # The `blocks` lens: the graph alone over the program column (880 by 344, no view), the source # six lines and the disassembly eleven under it; the registers and the stack from the top. blocks = lambda regs: split(344, 0, 6, 11, regs) # Each pane's minimap a region of its own (motion.strips), secondary action: the source's, the # disassembly's, the stack's, and the view's where it is a dump (not the calls lens's backtrace, # not the syscalls lens's log). def strips(panes, view): out = motion.strips(panes) if not view and "view strip" in out: del out["view strip"] out["view"] = panes["view"] return out motion.REGIONS.update({ "fib": {**strips(subject(10), False), **words}, "sort": {**strips(blocks(11), True), **words}, # The band is the calls so far; the disassembly a descent. "syscalls": {**strips(beside(14), False), **words}, "musl": {**strips(beside(15), False), **words}, "heap": {**strips(subject(14), True), **words}, "list": {**strips(subject(9), True), **words}, # The band's drawing is the backtrace, then the kernel's side; its view a dump. It folds none. "sqlite": {**strips(subject(17), True), **words}, "zoom": {**strips(none(9), False), **words}, "gallery": {**strips(beside(13), True), **words}, # The pair (src/pair.zig, `split`): the source shared across both columns, then each run's disassembly, # registers and frame as bytes on its side of the seam; no strips. ASan's stderr comes in over its # registers and its disassembly's lower rows once it has stopped. "asan": {"source": (64, 48, 1792, ph(5)), "plain disasm": (64, 212, 880, ph(9)), "asan disasm": (976, 212, 880, ph(5)), "plain registers": (64, 472, 880, ph(4)), "asan stderr": (976, 376, 880, ph(6)), "plain frame": (64, 612, 880, ph(10)), "asan frame": (976, 612, 880, ph(10)), **words}, # The introduction (motion.md, "A scene: the introduction"), chapter I: the address space in the # program column, the dump in the state column (the texts beside the address space there while # the dump is on the shelf); the camera at x2 on the dump for its first three beats. "intro_memory": {"space": (64, 48, 880, 840), "dump": (976, 48, 880, 840), **words}, # Chapter II: the texts on the left (the program column), main's rows and reverse's first under them, # the registers built over the stack. "intro_stack": {"text": (64, 48, 880, 536), "code": (64, 584, 880, 304), "registers": (976, 48, 880, 236), "stack": (976, 296, 880, 592), **words}, # Chapter III: the aligned panel across both columns under its text above, then the working frame # (the nodes lens as the list scene's), then the map. "intro_code": {"text": (64, 48, 1792, 120), "aligned": (64, 168, 1792, 720), **words}, }) # `cut`: the gallery is the catalogue's pages, cut from one to the next (scenes/gallery.zig: 3, 6, 9.6, 13, 16, # 19.8, 21 and 24 s), not a scene: a page's first frame changes everything at once by design. PAGES = {180, 360, 576, 780, 960, 1188, 1260, 1440} motion.EXEMPT["cut"] = lambda name, t, moved: lint.scene == "gallery" and any(abs(t - p) <= 1 for p in PAGES) _regions_for = motion.regions_for scene = lambda video: os.path.basename(video).split("-")[0].split(".")[0] motion.regions_for = lambda video, name: _regions_for(video, name or scene(video)) STRING = r'"((?:[^"\\]|\\.)*)"' def captions(name): """The scene's captions in the order it says them: each `says.line(s, "...")`, and each string of an array a `for` loop says.""" path = os.path.join(HERE, "..", "scenes", name + ".zig") if not os.path.exists(path): return None src = open(path).read() arrays = {m.group(1): re.findall(STRING, m.group(2), re.S) for m in re.finditer(r"const (\w+) = \[_\]\[\]const u8\{(.*?)\};", src, re.S)} out = [] for m in re.finditer(r"says\.line\(s, " + STRING + r"\)|for \((\w+)\)", src): if m.group(1) is not None: out.append(m.group(1)) elif m.group(2) in arrays: out.extend(arrays[m.group(2)]) return out def shown(text): """Characters as the caption shows them: no backticks, no line break, an escape one.""" text = re.sub(r"\\(.)", r"\1", text.replace("\\n", "")) return len(text.replace("`", "")) # A scene that opens on the map dives (scenes/list.zig: at 2.5 s, 150 f): the map's columns go into their # strips over the caption, which does not change. Its frames are no caption's arrival or leaving. DIVES = {"list": (150, 300)} _lint = motion.lint def lint(names, counts, soft, sizes, fps, ev): if lint.scene in DIVES and "caption" in names: a, b = DIVES[lint.scene] counts, soft = counts.copy(), soft.copy() counts[a:b, names.index("caption")] = 0 soft[a:b, names.index("caption")] = 0 out = [line for line in _lint(names, counts, soft, sizes, fps, ev) if line != " none"] said = captions(lint.scene) if lint.scene else None cap = names.index("caption") if "caption" in names else None if said is not None and cap is not None: on = motion.busy(counts, soft) changes = [(a, b) for a, b in motion.runs(on[:, cap]) if b - a >= 3] ends = [c[0] for c in changes[1:]] + [len(on)] # An arrival is a change after which the caption is up longer than the gap between leaving and arriving. arrivals = [(b, nxt - b - 1) for (a, b), nxt in zip(changes, ends) if nxt - b - 1 > 6] if len(arrivals) != len(said): out.append(f" CAPTION ? {len(arrivals)} captions arrive in the video, the scene says {len(said)}: reading times not checked") else: short = [f"{b / fps:.2f} ({up} f of {90 + 3.6 * shown(t):.0f})" for (b, up), t in zip(arrivals, said) if up < 90 + 3.6 * shown(t)] if short: out.append(f" CAPTION {len(short):3d} up under its reading time, 90 f + 3.6 f a character: " + ", ".join(short[:4]) + (" ..." if len(short) > 4 else "")) return out or [" none"] motion.lint = lint lint.scene = None if __name__ == "__main__": video = next((a for a in sys.argv[2:] if a.endswith(".mp4")), None) lint.scene = scene(video) if video else None motion.main() if hasattr(motion, "main") else sys.exit("design/motion/motion.py has no main()")