1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
#!/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"): one scene in four chapters, each
# building its parts in place, so its regions are the two columns: the program column (the
# address space, then the texts beside and the code, then the aligned code and the frame's
# source and disassembly) and the state column (the bytes, the registers, the stack).
"intro": {"program": (64, 48, 880, 840), "state": (976, 48, 880, 840), **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()")
|