summaryrefslogtreecommitdiff
path: root/design/mock/map.py
blob: 65b6c88094e26134a70cb5a76e5e4ec800980867 (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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env python3
"""The map, the minimap, and the working frame they belong to, from one fresh recording of targets/list.

    python3 -B map.py     # 09, 10, 11 (the working frame), 19 (the map), 20 (the dive), 21 (a loupe open)

The working frame is the 1920 stage (context.py's blocks, as mocks 9 to 14 always drew them). The map is the same
moment unfolded on 3840 by 2160: every panel at its full length, packed into four columns by content. The minimap
is the map at a twentieth, drawn from the map itself: every run of text a stroke, the marks, the windows. The data
is `list-dbg.txt` (registers, the stack from 128 bytes under rsp, the heap) and `list.dis`, as dense.py reads them.
"""
import subprocess

from PIL import Image, ImageDraw, ImageFont

import gen
import dense as D
import context as C
from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, VERM, READ, HEAP, STACK, A, S, ROW, TAG,
                     PAD, MONO, b, ph, tw, mix, Svg, H_)

RSP, UNDER = D.RSP, 16                     # the map's stack starts 16 words above rsp
ROLES = {0: "saved rbp", 3: "saved rbx", 4: "saved rbp", 26: "saved rbp", 38: "saved rbp", 42: "argc",
         43: "argv[0]", 44: "argv end", 45: "envp end", 46: "auxv"}
FRAMES = [("reverse", 0, 1), ("main", 2, 5), ("libc+27781", 6, 27), ("__libc_start_main", 28, 39), ("_start", 40, 999)]


def stage(map_):
    """Set the blocks' globals for the map (3840) or the working frame (1920)."""
    gen.W, gen.H = (3840, 2160) if map_ else (1920, 1080)
    C.W = gen.W
    C.TOP, C.BOTTOM = (96, 1784) if map_ else (48, 888)
    C.LANE_LINK, C.LANE_VIEW = (1912, 1924) if map_ else (950, 962)


class Rec(Svg):
    """An Svg that keeps what it drew, so the minimap can be drawn from it."""
    def __init__(self):
        super().__init__()
        self.log = []

    def t(self, x, y, s, size=30, fill=VELLUM, font="mono", anchor="start"):
        super().t(x, y, s, size, fill, font, anchor)
        w = len(s) * 0.6 * size if font.startswith("mono") else tw(s, size, font)
        x0 = x - w if anchor == "end" else x - w / 2 if anchor == "middle" else x
        self.log.append(("t", x0, y, w, size, fill))

    def rect(self, x, y, w, h, fill="none", stroke=None, sw=2, dash=None):
        super().rect(x, y, w, h, fill, stroke, sw, dash)
        self.log.append(("r", x, y, w, h, fill, stroke))


# ---- the map --------------------------------------------------------------------------------------------
def slot_rows(first, n, step_free=True):
    """Stack rows from slot `first` (0 is 16 words above rsp): the free ones above rsp, then the stack."""
    rows = []
    for i in range(first, first + n):
        a = RSP + 8 * (i - UNDER)
        w = D.word(a)
        if i < UNDER:
            rows.append((a, [], [], ("int", w), {"free": True}))
            continue
        links, end = D.value(w)
        rows.append((a, ["rbp", "rsp"] if a == RSP else [], links, end))
    return rows


def roles_from(first, n):
    return {i - first: (t, DIM) for j, t in ROLES.items() for i in [j + UNDER] if first <= i < first + n}


def frames_from(first, n):
    out = []
    for name, a, z in FRAMES:
        a, z = a + UNDER - first, z + UNDER - first
        if z >= 0 and a < n:
            out.append((name, a if a >= 0 else -1, z))
    return out


def node_marks(step):
    return {"tick": [0x10040b8]} | ({"read": 0x1004098} if step else {})


def pins(step):
    return {0x10040b0: ["rcx"], 0x1004090: ["rax"] + ([] if step else ["rdi"]), 0x1004070: ["rdi"] if step else []}


COLS = {}                                  # a pane's column on the map: (its parts, its window's first row, rows)


def draw_map(g, step, loupe, subject=False):
    """The map at one moment. Returns the outlines of the working frame's windows, in map units."""
    stage(True)
    sp = C.SUBJECT if subject else C.BESIDE
    col = D.COL
    win = {}
    # A: the lens's drawing at its subject size, the file whole, the backtrace whole, the key
    D.put(col[0])
    C.RX = col[2]
    C.lens(g, 96, 296, 0, D.NODES, pins(step), node_marks(step), 0x1004000, 0, subject=True, view_rows={}, ties=False)
    win["drawing"] = (col[0], 96, D.CW, 296)
    ys = C.source(g, 404, 42, D.SRC, 1, 21, [], "list.c", 42)
    first = 18 if subject else 19
    COLS["source"] = ([(col[0], 404 + TAG + PAD, 42)], first - 1, sp["src"])
    win["source"] = (col[0], 404 + TAG + PAD + (first - 1) * ROW, D.CW, sp["src"] * ROW)
    D.put(col[0])
    C.backtrace(g, 1456, ph(5), D.BT, x=col[0], note="newest at the bottom")
    key(g, col[0], 1620, step)
    # B: 69 instructions, the function and its neighbours
    D.put(col[1])
    rows = D.window(69, step)
    at = C.disasm(g, 96, rows, "reverse", 21)
    D.thread_across(g, ys, [(at[0x1001926], at[0x1001926] + ROW)])
    wn = [r[0] for r in D.window(sp["dis"], step)]
    win["disasm"] = (col[1], at[wn[0]], D.CW, sp["dis"] * ROW)
    COLS["disasm"] = ([(col[1], 96 + TAG + PAD, 69)], [r[0] for r in rows].index(wn[0]), sp["dis"])
    # C: memory, the registers, a loupe, the stack continued
    D.put(col[2])
    mem = C.fields(g, 96, ph(14), "the heap, round what the step reads", 0x1004000, 512,
                   D.heap_fields(0x1004000, 512, 0x1004098), D.MEM, HEAP, D.region, marks=(9,) if step else (),
                   title="memory", touched={0x1004098} if step else ())
    win["view"] = (col[2], mem[0x1004050 if subject else 0x1004070], D.CW, (7 if subject else 5) * ROW)
    COLS["view"] = ([(col[2], 96 + TAG + PAD, 14)], (win["view"][1] - 96 - TAG - PAD) // ROW, 7 if subject else 5)
    C.registers(g, 476, D.regs(step), D.FLAGS, {"rax": {0: "tick"}, "rcx": {0: "tick"},
                                                "rdi": {0: "strike" if step else "tick"}})
    win["registers"] = (col[2], 476, D.CW, ph(17))
    y = 476 + ph(17) + 12
    if loupe:
        lys = D.loupe(g, col[2], y, [("rdi", DIM), (" → ", FAINT), ("1004070", HEAP)], "heap · struct node, 16 bytes",
                      0x1004030, D.heap_fields(0x1004030, 144), [(0x1004070 - 0x1004030, 16)], 0)
        win["loupe"] = (col[2], lys[0x1004050], D.CW, 5 * ROW)
        y += ph(9) + 12
    n_c = (1784 - y - TAG - 2 * PAD) // ROW
    # D: the stack, 16 words above rsp and on; what does not fit continues at C's foot, then `more`
    D.put(col[3])
    C.stack(g, 96, slot_rows(0, 69), frames_from(0, 69), note="from rsp-80", rsp=RSP, roles=roles_from(0, 69))
    win["stack"] = (col[3], 96 + TAG + PAD + UNDER * ROW, D.CW, sp["words"] * ROW)
    COLS["stack"] = ([(col[3], 96 + TAG + PAD, 69), (col[2], y + TAG + PAD, n_c)], UNDER, sp["words"])
    D.put(col[2])
    C.stack(g, y, slot_rows(69, n_c - 1), frames_from(69, n_c - 1), note="continued", rsp=RSP,
            roles=roles_from(69, n_c - 1))
    left = (0x7ffffffff000 - (RSP + 8 * (69 + n_c - 1 - UNDER))) // 8
    g.rect(col[2] + 2, y + ph(n_c - 1) - 2, D.CW - 4, 2, PAGE)     # one row longer: the `more` row is the panel's
    g.frame(col[2], y, D.CW, ph(n_c), RULE, 2)
    yy = y + TAG + PAD + (n_c - 1) * ROW
    C.more(g, col[2] + 184 - 6, yy)
    g.t(col[2] + 184 + 66, b(yy), "%d words, to the stack's top" % left, S, FAINT, MONO)
    # the windows the working frame shows: an outline round each, 4 out
    for x, y0, w, h in win.values():
        g.frame(x - 4, y0 - 4, w + 8, h + 8, DIM, 2)
    return win


def key(g, x, y, step):
    """The map's key, at the foot of a column it leaves: what this is, and its marks."""
    y0 = C.panel(g, x, y, D.CW, 3, "the map", note="targets/list · moment %d of 4" % (3 if step else 2))   # its rows, no more
    g.t(x + 12, b(y0), "every panel whole; the working frame's windows outlined", 18, DIM, "sans")
    items = [("current", lambda xx, yy: C.current_row(g, xx, yy, 120, 24)), ("struck", lambda xx, yy: C.struck(g, xx, yy, 60)),
             ("read", lambda xx, yy: g.frame(xx, yy, 60, ROW, READ, 2)), ("held", lambda xx, yy: C.held(g, xx + 8, yy, 40)),
             ("window", lambda xx, yy: g.frame(xx, yy, 60, ROW, DIM, 2))]
    for i, (name, draw) in enumerate(items):
        xx, yy = x + 12 + (i % 3) * 290, y0 + (1 + i // 3) * ROW
        draw(xx, yy)
        g.t(xx + 136, b(yy), name, 18, DIM, "sans")


# ---- a pane's minimap ------------------------------------------------------------------------------------
MW, MGAP, MR = 40, 8, 4                    # its strip: 40 wide, 8 from the content; 4 units a row
KX = (MW - 4) / D.CW                       # a row's 880 to the strip's 36: a character two thirds of a unit


def column(log, parts):
    """A pane's column, from the map's log: for each row, its runs of text as (x from the panel's left, width,
    ink), and its marks (current, struck, read)."""
    rows, marks, n0 = {}, {}, 0
    for x0, y0, n in parts:
        for e in log:
            if e[0] == "t":
                _, x, y, w, size, fill = e
                r = int((y - 19.2 - y0 + 1) // ROW)
                if x0 <= x < x0 + D.CW and 0 <= r < n and fill not in (None, "none", PAGE):
                    rows.setdefault(n0 + r, []).append((x - x0, w, fill))
            else:
                _, x, y, w, h, fill, stroke = e
                r = int((y + h / 2 - y0) // ROW)
                if not (x0 <= x < x0 + D.CW and 0 <= r < n):
                    continue
                kind = ("current" if fill in (GOLD, C.WASH) and w >= 20 and h >= 20 else
                        "struck" if fill in (C.SOFT, VERM) else "read" if stroke == READ else None)
                if kind:
                    marks.setdefault(n0 + r, set()).add(kind)
        n0 += n
    return rows, marks, n0


def pane_minimap(g, log, name, x, y, h):
    """A pane's minimap: its whole column of the map, a row 4 units, in the strip at the pane's right (x, y, 40 by
    h). Each run of text a stroke 3 high in its ink 65% of the way from the page; the marks at the row's left: the current
    row gold, a strike vermilion, a read in read's colour; the pane's window framed 2 in `dim`. A column longer
    than the strip shows the rows round the window, and three dots at the end where it goes on."""
    parts, w0, wn = COLS[name]
    rows, marks, total = column(log, parts)
    g.rect(x, y, MW, h, C.WELL)
    cap = int((h - 6) // MR)
    first = 0 if total <= cap else min(max(w0 + wn // 2 - cap // 2, 0), total - cap)
    top = y + 3
    for r in range(first, min(first + cap, total)):
        ry = top + (r - first) * MR
        for rx, rw, ink in rows.get(r, []):
            g.rect(x + 2 + rx * KX, ry, max(rw * KX, 1), 3, mix(ink, PAGE, 0.65))
        for k, kind in enumerate(sorted(marks.get(r, ()), key=["current", "struck", "read"].index)):
            g.rect(x + 2 + 5 * k, ry - 0.5, 4, 4, {"current": GOLD, "struck": VERM, "read": READ}[kind])
    g.frame(x, top + (w0 - first) * MR - 3, MW, wn * MR + 5, VELLUM, 2)   # the pane's window
    for end, more in (("top", first > 0), ("foot", first + cap < total)):
        if more:
            yy = y + 1 if end == "top" else y + h - 3
            for d in range(3):
                g.rect(x + MW / 2 - 7 + d * 6, yy, 2, 2, FAINT)


def map_log(step, loupe, subject=False):
    stage(True)
    m = Rec()
    win = draw_map(m, step, loupe, subject)
    return m, win


# ---- the working frame -------------------------------------------------------------------------------------
BESIDE_VIEW = [7, 8, 9, 10, 11]            # the view's rows (of the heap's 32): 1004070 to 10040b0
SUBJECT_VIEW = [5, 6, 7, 8, 9, 10, 11]


def clock(g, moment, total):
    g.rect(64, 899, 1792, 2, RULE)
    g.rect(64, 898, 1792 * moment / total, 4, GOLD)
    g.t(1856, 934, "moment %d of %d" % (moment, total), S, FAINT, MONO, "end")


def strip(on):
    """Panes with a minimap: their content 48 narrower, their frame whole (context.tag)."""
    C.CW, C.MINIMAP = (D.CW - MW - MGAP, MW + MGAP) if on else (D.CW, 0)
    C.DX_NOTE = 540 if on else 564         # the annotation starts 24 sooner, so its room loses only 24


def working_panels(g, step, subject, wrap=lambda name: ("", ""), withhold=None):
    """The working frame's panels, each between the strings wrap(name) gives (a transform, for the dive)."""
    stage(False)
    sp = C.SUBJECT if subject else C.BESIDE
    out = {}

    def open_(name):
        o, c = wrap(name)
        g.o.append(o) if o else None
        return c

    y = 48 + sp["band"] + 12
    c = open_("source")
    strip(True)
    D.put(64)
    ys = C.source(g, y, sp["src"], D.SRC, 18 if subject else 19, 21, [], "list.c", 42)
    g.o.append(c) if c else None
    c = open_("disasm")
    at = C.disasm(g, y + ph(sp["src"]) + 12, D.window(sp["dis"], step), "reverse", 21)
    g.o.append(c) if c else None
    out["thread"] = (ys, at[0x1001926])
    yr = 48 + sp["view"] + 12
    c = open_("registers")
    strip(False)
    D.put(976)
    out["regs"] = C.registers(g, yr, D.regs(step), D.FLAGS, {"rax": {0: "tick"}, "rcx": {0: "tick"},
                                                            "rdi": {0: "strike" if step else "tick"}})
    g.o.append(c) if c else None
    strip(True)
    c = open_("stack")
    n = sp["words"]
    C.stack(g, yr + ph(17) + 12, slot_rows(UNDER, n), frames_from(UNDER, n), rsp=RSP, roles=roles_from(UNDER, n))
    g.o.append(c) if c else None
    c = open_("view")
    view = C.fields(g, 48, sp["view"], "the heap, %x to %x" % (0x1004000 + 16 * (SUBJECT_VIEW if subject else BESIDE_VIEW)[0],
                    0x10040bf), 0x1004000, 512, D.heap_fields(0x1004000, 512, 0x1004098), D.MEM, HEAP, D.region,
                    marks=(9,) if step else (), rows_shown=SUBJECT_VIEW if subject else BESIDE_VIEW, more_rows=False,
                    touched={0x1004098} if step else (), withhold=withhold)
    g.o.append(c) if c else None
    strip(False)
    out["view"] = view
    out["boxes"] = {"source": (64, y, ph(sp["src"])), "disasm": (64, y + ph(sp["src"]) + 12, ph(sp["dis"])),
                    "stack": (976, yr + ph(17) + 12, ph(n)), "view": (976, 48, sp["view"])}
    return out, sp


def working(name, step, subject=False, loupe=False, caption=None):
    """A working frame at 1920 by 1080, with its minimap."""
    m, win = map_log(step, loupe, subject)
    stage(False)
    g = Svg()
    out, sp = working_panels(g, step, subject, withhold=(0x1004050, 0x10040a0) if loupe else None)
    D.put(64)
    C.thread(g, out["thread"][0], [(out["thread"][1], out["thread"][1] + ROW)])
    D.put(64)
    C.RX = 976
    C.lens(g, 48, sp["band"], sp["view"], D.NODES, pins(step), node_marks(step), 0x1004000, 32,
           link_from=(out["regs"]["rdi"], "rdi") if step and not loupe else None, subject=subject, view_rows=out["view"],
           ties=not loupe)
    if loupe:                              # opened under its pointer, rdi's new value: the heap round 1004070
        ry = out["regs"]["rdi"]
        hop = 976 + 72
        lx, ly = 956, ry + ROW - 1         # it hangs from its pointer: its tag on the row's foot, the rows under rdi covered whole
        D.loupe(g, lx, ly, [("rdi", DIM), (" → ", FAINT), ("1004070", HEAP)], "heap · struct node, 16 bytes",
                0x1004050, D.heap_fields(0x1004050, 80), [(0x1004070 - 0x1004050, 16)], 1, rows=5)
        g.rect(hop - 2, ry + ROW - 3, 7 * A + 4, 4, HEAP)     # the stem, short: a bracket under the pointer's digits, on the tag
    minimaps(g, m.log, out["boxes"])
    clock(g, 3 if step else 2, 4)
    gen.caption(g, caption)
    g.save(name)


def minimaps(g, log, boxes, skip=()):
    """Each pane's minimap, in the strip its frame keeps at its right, over its body."""
    for name, (x, y, h) in boxes.items():
        if name not in skip:
            pane_minimap(g, log, name, x + D.CW - 2 - MW, y + TAG, h - TAG - 2)


# ---- the dive: the map coming in to the working frame, four instants -------------------------------------
def glide(t):
    """The glide curve (0.45, 0, 0.55, 1), solved for t."""
    lo, hi = 0.0, 1.0
    for _ in range(40):
        s = (lo + hi) / 2
        x = 3 * (1 - s) ** 2 * s * 0.45 + 3 * (1 - s) * s * s * 0.55 + s ** 3
        lo, hi = (s, hi) if x < t else (lo, s)
    return 3 * (1 - s) * s * s + s ** 3


FRAME_RECT = {"source": (64, 212, 880, ph(8)), "disasm": (64, 448, 880, ph(17)), "registers": (976, 212, 880, ph(17)),
              "stack": (976, 664, 880, ph(8)), "view": (976, 48, 880, 152), "drawing": (64, 48, 880, 152)}


def dive(name, e, veil, outline, fade_frame):
    """One instant of `dive`: e, the move's progress (glide applied); veil, on the map; outline, the windows' bright
    frames; fade_frame, how far the working frame's tags and minimaps have come in. Each window travels to its
    pane's place, its scale from the map's half to whole on its logarithm; each pane's column, the rest of what
    the map holds of it, squeezes into the pane's minimap strip on the same move; the rest of the map fades."""
    m, win = map_log(False, False)
    body = m.o[1:-1] if m.o[-1] == "</svg>" else m.o[1:]
    stage(False)
    g = Svg()
    clips = []
    # the map's parts no pane takes: faded in place as the move goes
    g.o.append('<g opacity="%.2f" transform="scale(0.5)">' % (1 - e))
    g.o += body
    if veil:
        g.o.append('<rect width="3840" height="2160" fill="%s" opacity="%.2f"/>' % (PAGE, veil))
    for x, y, w, h in win.values():
        if outline:
            g.o.append('<rect x="%.1f" y="%.1f" width="%.1f" height="%.1f" fill="none" stroke="%s" stroke-width="%.1f"/>'
                       % (x - 4, y - 4, w + 8, h + 8, BRIGHT, 2 + outline * 2))
    g.o.append("</g>")
    out, sp = working_panels(Svg(), False, False)          # where the panes and their strips will be
    if 0 < e < 1:                          # each column squeezing into its strip: a row from 24 units to 3
        for nm, (bx, by, bh) in out["boxes"].items():
            parts, w0, wn = COLS[nm]
            sx0, sy0 = bx + D.CW - 2 - MW + 2, by + TAG + 3
            cap = int((bh - TAG - 2 - 6) // MR)
            total = sum(p[2] for p in parts)
            first = 0 if total <= cap else min(max(w0 + wn // 2 - cap // 2, 0), total - cap)
            n0 = 0
            for k, (mx, my, n) in enumerate(parts):
                kx, ky = 0.5 ** (1 - e) * KX ** e, 0.5 ** (1 - e) * (MR / ROW) ** e   # on their logarithms
                tx = mx * 0.5 * (1 - e) + sx0 * e
                ty = my * 0.5 * (1 - e) + (sy0 + (n0 - first) * MR) * e
                cid = "c%s%d" % (nm, k)
                clips.append('<clipPath id="%s"><rect x="%d" y="%d" width="%d" height="%d"/></clipPath>'
                             % (cid, mx, my, D.CW, n * ROW))
                g.o.append('<g opacity="%.2f" transform="translate(%.2f,%.2f) scale(%.4f,%.4f) translate(%d,%d)">'
                           % (0.5 + 0.5 * e, tx, ty, kx, ky, -mx, -my))
                g.o.append('<g clip-path="url(#%s)">' % cid)
                g.o += body
                g.o.append("</g></g>")
                n0 += n
    g.o.insert(1, "<defs>" + "".join(clips) + "</defs>")
    map_rect = {"source": win["source"], "disasm": win["disasm"], "registers": win["registers"], "stack": win["stack"],
                "view": win["view"], "drawing": win["drawing"]}

    def wrap(nm):
        if nm not in FRAME_RECT:
            return "", ""
        fx, fy, fw, fh = FRAME_RECT[nm]
        mx, my, mw, mh = map_rect[nm]
        row0 = fy if nm in ("registers", "drawing") else fy + TAG + PAD
        sw = 0.5 ** (1 - e)
        ox, oy = (mx - fx) * 0.5 * (1 - e), (my - row0) * 0.5 * (1 - e)
        return '<g transform="translate(%.2f,%.2f) scale(%.4f)">' % (ox, oy, sw), "</g>"

    if e > 0:
        working_panels(g, False, False, wrap)
        o, c = wrap("drawing")
        g.o.append(o)
        D.put(64)
        C.RX = 976
        C.lens(g, 48, 152, 152, D.NODES, pins(False), node_marks(False), 0x1004000, 32, view_rows={0x1004090: 0},
               ties=False)
        g.o.append(c)
    if fade_frame > 0:
        g.o.append('<g opacity="%.2f">' % fade_frame)
        minimaps(g, m.log, out["boxes"])
        clock(g, 2, 4)
        g.o.append("</g>")
    gen.caption(g, [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)],
                    [("rdi", "mono"), (" points at a node; the word after its value is the next node.",)]])
    g.save(name)


def sheet(names, out, labels):
    """Four 1920 by 1080 instants on one 3840 by 2160 sheet, each at its own size."""
    tiles = []
    for n in names:
        subprocess.run(["resvg", n + ".svg", n + ".png"], check=True)
        tiles.append(Image.open(n + ".png").convert("RGB"))
    s = Image.new("RGB", (3840, 2160), "#000000")
    font = ImageFont.truetype("/usr/share/fonts/TTF/JetBrainsMonoNL-Medium.ttf", 28)
    d = ImageDraw.Draw(s)
    for i, (t, lab) in enumerate(zip(tiles, labels)):
        x, y = (i % 2) * 1920, (i // 2) * 1080
        s.paste(t, (x, y))
        d.rectangle((x + 8, y + 8, x + 8 + 18 * len(lab), y + 48), fill="#000000")
        d.text((x + 16, y + 12), lab, font=font, fill="#fff8e8")
    s.save(out)


def crop_sheet(src, out):
    """Mock 39: three panes' right ends from a working frame, at 1:1 (a 1080p pixel a unit), with what each minimap
    lets a viewer read named under it."""
    im = Image.open(src).convert("RGB")
    crops = [((800, 448, 944, 888), (24, 64), "the disassembly", ["69 rows: the function whole", "and its neighbours; the pc",
                                                                "gold, a read cyan; the", "window of 17 rows framed"]),
             ((1640, 40, 1860, 200), (380, 64), "its bytes", ["the heap's 14 rows on the map;", "the 5 shown framed; the read row"]),
             ((1640, 660, 1860, 888), (380, 360), "the stack", ["48 of its 103 rows, round the", "window; three dots: it goes on"])]
    s = Image.new("RGB", (720, 700), "#0a1030")
    d = ImageDraw.Draw(s)
    head = ImageFont.truetype("/usr/share/fonts/go/Go-Medium.ttf", 22)
    body = ImageFont.truetype("/usr/share/fonts/go/Go-Regular.ttf", 18)
    d.text((24, 18), "each pane's minimap at 1:1, a pixel of 1080p a unit", font=head, fill="#eee6d2")
    for box, (x, y), name, lines in crops:
        c = im.crop(box)
        s.paste(c, (x, y))
        d.text((x, y + c.height + 10), name, font=head, fill="#eee6d2")
        for k, t in enumerate(lines):
            d.text((x, y + c.height + 40 + 24 * k), t, font=body, fill="#9aa6d9")
    s.save(out)


if __name__ == "__main__":
    import os
    stage(True)
    g = Rec()
    draw_map(g, True, True)
    D.clock4(g, 3, 4)
    D.caption4(g, [[("The map: this moment of ",), ("targets/list", "mono"), (", every panel whole.",)],
                   [("The working frame's windows are outlined; a scene comes in from here.",)]])
    g.save("19-map")
    cap9 = [[("Stopped in ",), ("reverse", "mono"), (", about to load ",), ("head->next", "mono"), (".",)],
            [("rdi", "mono"), (" points at a node; the word after its value is the next node.",)]]
    cap10 = [[("The load read the word at ",), ("1004098", "mono"), (": the address of the next node.",)],
             [("rdi", "mono"), (" holds it now, and points at the node whose value is 9.",)]]
    working("09-context", False, caption=cap9)
    working("10-context-step", True, caption=cap10)
    working("11-context-lens", True, subject=True, caption=cap10)
    working("21-loupe", True, loupe=True,
            caption=[[("rdi", "mono"), (" holds ",), ("1004070", "mono"), ("; under it opens the heap round that node.",)],
                     [("Each pane's minimap shows where its rows are in the whole.",)]])
    crop_sheet("10-context-step.png", "39-minimap-1to1.png")
    names = ["20a", "20b", "20c", "20d"]
    dive("20a", 0, 0, 0, 0)
    dive("20b", 0, 0.5, 1, 0)
    dive("20c", glide(0.5), 0.5, 1, 0)
    dive("20d", 1, 0.5, 0, 1)
    sheet(names, "20-dive.png", ["frame 0: the map", "frame 24: windows bright, the rest veiled",
                                 "frame 60: halfway", "frame 96: the working frame"])
    for n in names:
        os.remove(n + ".svg")
        os.remove(n + ".png")