summaryrefslogtreecommitdiff
path: root/anim/tools/chart.py
blob: 7cc09253d2b00e9ba1aa09f6cc5600e712241ece (plain) (blame)
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
#!/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, slots, backtrace=0):
    out = {}
    if drawing:
        out["drawing"] = (64, 48, 880, drawing)
        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(17))
    out["stack"] = (976, right + ph(17) + 12, 880, ph(slots))
    if backtrace:
        out["backtrace"] = (976, right + ph(17) + 12 + ph(slots) + 12, 880, ph(backtrace))
    return out

# The words that are not panels (motion.WORDS): the clock (its count, ranged right at 1648,
# changes in a frame by design), the caption (its 1400 from the margin), and the minimap at
# the caption band's right end, secondary action. The caption's box starts under the clock's count
# and the run's step counter (912 to 934).
words = {"clock": (0, 888, 1656, 46), "caption": (0, 940, 1464, 140), "minimap": (1664, 912, 192, 108)}
none, beside, subject = split(0, 0, 14, 17, 8, 4), split(152, 152, 8, 17, 8), split(296, 200, 10, 9, 6)
# The `blocks` lens: the graph over the program column, the source alone under it; the disassembly
# (its view) over the state column, seven rows, the registers and six words under it.
blocks = {**subject, "view": (976, 48, 880, ph(7)), "source": (64, 356, 880, ph(20))}
del blocks["disasm"]
motion.REGIONS.update({
    "fib": {**subject, **words},
    "sort": {**blocks, **words},
    # The band is the calls so far; the disassembly a descent.
    "syscalls": {**beside, **words},
    "heap": {**subject, **words},
    "list": {**subject, **words},
    # The band's drawing is the backtrace, then the kernel's side.
    "sqlite": {**subject, **words},
    "zoom": {**none, **words},
    "gallery": {**beside, **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 falling map passes under 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()")