diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 16:05:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 16:43:58 -0300 |
| commit | 165bb6c1344b98de7bef1d3c2b72147b4a40797a (patch) | |
| tree | 0e70e6222700063f604f78673f6ab3a5bfb9242d /anim/src/ui.zig | |
| parent | 66a2a7cf733f6abc1f79561c52d901ce67019fb1 (diff) | |
| download | computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.tar.gz computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.zip | |
canim: the renderer speaks the design language
The blocks, their states and the tokens carry the catalogue's names:
atoms in ui, chain/reg/slot in rows, insn with its line column, jump lane
and annotations (read, write, taken or not, a call with its arguments, a
return, a syscall) in disasm, bytes and hexdump with field paper and the
reading column, the three splits and relayout in stage, the ties (thread,
link and pin, projection, echo). A lens is a band over the whole context,
its drawing left and its bytes right. Frames' spans on the stack, each
instruction's line and a callee's parameter count come from the tracer.
A gallery scene draws every block; the test compares six of its pages
with pictures kept in test/gallery, drawn from a recording kept there
too. The scenes are ported; the motion checks report no pop and no
metronome in any of them.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'anim/src/ui.zig')
| -rw-r--r-- | anim/src/ui.zig | 508 |
1 files changed, 275 insertions, 233 deletions
diff --git a/anim/src/ui.zig b/anim/src/ui.zig index 9d92e28..48d2f64 100644 --- a/anim/src/ui.zig +++ b/anim/src/ui.zig @@ -1,7 +1,8 @@ //! What every component draws with, in the design's units (1920 by 1080; a //! component's origin is its own top left corner): rects and rules snapped -//! to pixels, text in the design's faces set on a baseline, hex in groups -//! of four, the panel, the marks (written, read, unknown), and arrows. +//! to pixels, text in the design's faces set on a baseline, and the +//! catalogue's atoms (design/README.md, "1. Atoms"): `address`, `value`, +//! `arrow`, `byte`, the `mark`s, `plaque`, `pin`, `bracket`, and a panel's `tag`. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); @@ -71,38 +72,32 @@ fn set(face: Face, size: f32) Face { }; } -/// `n` in hex, a space between groups of four from the right, into `out` -/// (20 bytes is enough): nobody reads twelve digits unbroken. -pub fn grouped(out: []u8, n: u64) []const u8 { - var buf: [16]u8 = undefined; - const digits = std.fmt.bufPrint(&buf, "{x}", .{n}) catch unreachable; - var at: usize = 0; - for (digits, 0..) |d, i| { - if (i > 0 and (digits.len - i) % 4 == 0) { - out[at] = ' '; - at += 1; - } - out[at] = d; - at += 1; - } - return out[0..at]; -} - /// Width of one mono character at `size`: the mono faces advance 0.6 em, a /// whole number of pixels at the design's sizes. pub fn advance(size: f32) f32 { return size * 3 / 5; } -/// The space between groups of four hex digits: half a character, a whole unit. -pub fn hexGap(size: f32) f32 { - return @ceil(advance(size) / 2); +/// How many leading hex digits every address of a column shares (an +/// address's `lead`), leaving at least two: the eye reads the rest. +pub fn shared(addrs: []const u64) usize { + if (addrs.len == 0) return 0; + var bufs: [2][16]u8 = undefined; + const first = std.fmt.bufPrint(&bufs[0], "{x}", .{addrs[0]}) catch unreachable; + var k: usize = first.len -| 2; + for (addrs[1..]) |a| { + const d = std.fmt.bufPrint(&bufs[1], "{x}", .{a}) catch unreachable; + if (d.len != first.len) return 0; + var same: usize = 0; + while (same < k and d[same] == first[same]) same += 1; + k = same; + } + return k; } -/// Width of `digits` hex digits in groups of four, half a character apart. -pub fn hexWidth(digits: usize, size: f32) f32 { - const groups: f32 = @floatFromInt((digits + 3) / 4); - return @as(f32, @floatFromInt(digits)) * advance(size) + (groups - 1) * hexGap(size); +/// The digits of an address, as long as it is. +pub fn digits(addr: u64) usize { + return if (addr == 0) 1 else (64 - @clz(addr) + 3) / 4; } pub const Align = enum { left, center, right }; @@ -130,6 +125,13 @@ pub const Ui = struct { return out; } + /// The same, its origin moved to `x, y`: a row or a panel drawn at its place. + pub fn moved(u: Ui, x: f32, y: f32) Ui { + var out = u; + out.at.xf = out.at.xf.mul(.translate(x, y)); + return out; + } + fn point(u: Ui, x: f32, y: f32) Vec { return u.at.xf.apply(.{ x, y }); } @@ -200,103 +202,150 @@ pub const Ui = struct { return u.f.format(fmt, args); } - /// `value` as `digits` hex digits, in groups of four half a character - /// apart, from `x` rightwards. - pub fn hex(u: Ui, size: f32, x: f32, baseline: f32, v: u64, digits: usize, ink: Ink) void { - var buf: [16]u8 = undefined; - const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{v}) catch unreachable; - const shown = all[16 - digits ..]; - // Leading zeros, but never the last digit. - var zeros: usize = 0; - while (zeros < digits - 1 and shown[zeros] == '0') zeros += 1; - const adv = advance(size); - var at_x = x; - var i: usize = 0; - while (i < digits) { - // A group ends where the digits left are a multiple of four. - const end = i + ((digits - i - 1) % 4) + 1; - const split = std.math.clamp(zeros, i, end); - u.text(ink.face, size, at_x, baseline, shown[i..split], ink.lead); - u.text(ink.face, size, at_x + @as(f32, @floatFromInt(split - i)) * adv, baseline, shown[split..end], ink.ink); - at_x += @as(f32, @floatFromInt(end - i)) * adv + hexGap(size); - i = end; - } + /// For tools/motion.py, which plots how much of each panel changes + /// from frame to frame: with CANIM_PANELS in the environment, every + /// frame tells where on the canvas (as fractions of it) `r` is. + pub fn region(u: Ui, name: []const u8, r: Rect) void { + if (u.f.host.env("CANIM_PANELS") == null) return; + const a = u.point(r.x, r.y); + const b = u.point(r.x + r.w, r.y + r.h); + const w = @floor(u.f.in.window_width); + const h = @floor(u.f.in.window_height); + std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name }); } - /// An address in a gutter: its last group is the end that changes from - /// row to row, so only that one is in full ink. - pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, digits: usize) void { - const size = theme.size.mono.small; - const adv = advance(size); - var buf: [16]u8 = undefined; - const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{addr}) catch unreachable; - const shown = all[16 - digits ..]; - var i: usize = 0; - var at_x = x; - while (i < digits) : (i += 4) { - u.text(.mono, size, at_x, baseline, shown[i .. i + 4], if (i + 4 == digits) theme.color.text.primary else theme.color.text.faint); - at_x += 4 * adv + hexGap(size); - } - } + // ---------------------------------------------------------- atoms - /// An acme window `w` by `h` at the origin: a tag one row high (a box, - /// gold where the narration is; the title; a note ranged right, or the - /// legend of the regions), and a rule round the body. Returns the body. - pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: bool = false }) Rect { + /// `tag`: a panel's title bar, 24 high, raised: a 12-unit box 8 in + /// (solid gold where the narration is, `focus`; a faint outline + /// elsewhere), the title in Go Medium 20, and ranged right a `note` + /// (Go 18, faint) or a `legend` (words in Go Medium 18, each in its + /// ink, 14 apart). With the rule round the body it is a panel, an acme + /// window `w` by `h` at the origin. Returns the body. + pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: []const Legend = &.{} }) Rect { u.region(title, .{ .x = 0, .y = 0, .w = w, .h = h }); const m = theme.metric.context; const t = theme.color.text; u.fill(.{ .x = 0, .y = 0, .w = w, .h = m.tag }, theme.color.raised); - u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, theme.color.rule, theme.stroke.rule); + u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, if (options.focus) t.dim else theme.color.rule, theme.stroke.rule); const box: Rect = .{ .x = 8, .y = 6, .w = 12, .h = 12 }; if (options.focus) u.fill(box, theme.mark(.current).solid) else u.frame(box, t.faint, theme.stroke.rule); - const base = theme.baseline(0, m.tag, theme.size.sans.tag); + const base = m.tag - 6; u.text(.sans_medium, theme.size.sans.tag, 28, base, title, if (options.focus) t.primary else t.dim); const micro = theme.size.sans.micro; - if (options.legend) { - // What an address's ink says, always on screen, as pwndbg's legend is. - const c = theme.color.region; - var x = w - 10; - inline for (.{ .{ "read-only", c.rodata }, .{ "rwx", c.code_ }, .{ "data", c.data }, .{ "code", c.code_ }, .{ "heap", c.heap }, .{ "stack", c.stack } }) |entry| { - x -= u.width(.sans_medium, micro, entry[0]); - u.text(.sans_medium, micro, x, base, entry[0], entry[1]); - if (comptime std.mem.eql(u8, entry[0], "rwx")) u.fill(.{ .x = x, .y = base + 3, .w = u.width(.sans_medium, micro, entry[0]), .h = theme.stroke.rule }, entry[1]); - x -= 16; + if (options.legend.len > 0) { + var x = w - 10 + 14; + for (options.legend) |entry| x -= u.width(.sans_medium, micro, entry.text) + 14; + for (options.legend) |entry| { + const ew = u.width(.sans_medium, micro, entry.text); + u.text(.sans_medium, micro, x, base, entry.text, entry.ink); + if (entry.under) u.fill(.{ .x = x, .y = base + 3, .w = ew, .h = theme.stroke.rule }, entry.ink); + x += ew + 14; } } else u.text(.sans, micro, w - 10 - u.width(.sans, micro, options.note), base, options.note, t.faint); return .{ .x = 0, .y = m.tag, .w = w, .h = h - m.tag }; } - /// For tools/motion.py, which plots how much of each panel changes - /// from frame to frame: with CANIM_PANELS in the environment, every - /// frame tells where on the canvas (as fractions of it) `r` is. - pub fn region(u: Ui, name: []const u8, r: Rect) void { - if (u.f.host.env("CANIM_PANELS") == null) return; - const a = u.point(r.x, r.y); - const b = u.point(r.x + r.w, r.y + r.h); - const w = @floor(u.f.in.window_width); - const h = @floor(u.f.in.window_height); - std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name }); + /// `address`: lower-case hex, unbroken, as long as it is, no `0x`, in + /// the ink of where it points. `lead`: the first n digits at 60% of + /// the ink toward the page (a column's shared digits). `name`: a code + /// address's `name+offset` after it, primary, 10 on. `rwx`: a 2-unit + /// underline. Returns where it ends (after the name). + pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, a: Address) f32 { + const size = theme.size.mono.context; + var buf: [16]u8 = undefined; + const d = std.fmt.bufPrint(&buf, "{x}", .{addr}) catch unreachable; + const lead = @min(a.lead, d.len -| 2); + const adv = advance(size); + if (lead > 0) u.text(a.face, size, x, baseline, d[0..lead], mix(a.ink, theme.color.page, theme.metric.context.lead_alpha)); + u.text(a.face, size, x + @as(f32, @floatFromInt(lead)) * adv, baseline, d[lead..], a.ink); + const end = x + @as(f32, @floatFromInt(d.len)) * adv; + if (a.rwx) u.fill(.{ .x = x, .y = baseline + 3, .w = end - x, .h = theme.stroke.rule }, a.ink); + if (a.name.len == 0) return end; + const name = symbol(u, a.name); + u.text(.mono, size, end + 10, baseline, name, theme.color.text.primary); + return end + 10 + @as(f32, @floatFromInt(name.len)) * adv; + } + + /// `value`: what is not an address, set by its class (`Value.of`), from + /// `x` at mono 20: a digit; a small number in hex and its decimal 8 + /// after it, faint; a large one in hex after `0x`; a small negative. + /// `pointer`: an address, in `ink`. Returns where its digits end (the + /// faint decimal apart: `Value.width` is what a mark goes behind). + pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 { + const size = theme.size.mono.context; + const shown = Value.of(v, pointer); + u.text(ink.face, size, x, baseline, u.print("{s}{s}", .{ shown.prefix, shown.digits() }), ink.ink); + const end = x + shown.width(); + if (shown.decimal) u.text(.mono, size, end + 8, baseline, u.print("{d}", .{v}), theme.color.text.faint); + return end; + } + + /// `arrow`: the sign between two things in a chain, 30 wide from `x` + /// on the row whose middle is `mid`, faint: grammar, not data. + /// Returns where it ends. + pub fn arrow(u: Ui, x: f32, mid: f32, kind: Arrow) f32 { + const c = theme.color.text.faint; + const head = theme.metric.context.arrow_head; + const pen = u.at.pen(.{ .fill = rgb(c) }); + switch (kind) { + .points, .more => { + u.fill(.{ .x = x + 6, .y = mid - 1, .w = 11, .h = 2 }, c); + sk.draw.polygon(u.f, &.{ .{ x + 24, mid }, .{ x + 24 - head[0], mid - head[1] / 2 }, .{ x + 24 - head[0], mid + head[1] / 2 } }, pen); + if (kind == .more) for (0..3) |d| u.fill(.{ .x = x + 32 + @as(f32, @floatFromInt(d)) * 8, .y = mid + 2, .w = 3, .h = 3 }, c); + }, + .holds => { + sk.draw.polygon(u.f, &.{ .{ x + 6, mid }, .{ x + 6 + head[0], mid - head[1] / 2 }, .{ x + 6 + head[0], mid + head[1] / 2 } }, pen); + u.fill(.{ .x = x + 13, .y = mid - 1, .w = 11, .h = 2 }, c); + }, + } + return x + theme.metric.context.arrow_w + @as(f32, if (kind == .more) 24 else 0); } - /// The ground of a thing a step wrote, as hot as `h` says: armed (a - /// frame, the old value still in it), struck (the fill coming in, - /// its edges pulsing out and back), solid, cooling to its ember with a - /// bar along the bottom, `ground` (null: nothing) once that has gone. - /// Returns what to set its digits in. - pub fn written(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink { + // ---------------------------------------------------------- marks + + /// `mark` on a value in the context (mono 20), as hot as `h` says: + /// `armed` (a 2-unit vermilion frame round the old value), `struck` + /// (solid vermilion behind it, 5 wider each side, its edges pulsing out + /// 3; the digits bold, in ink: the ink this returns), then cooled to + /// nothing, and `held`: a tick 4 wide at the value's left for three + /// steps. A held change never sits under the digits it marks, whose + /// ink is where they point. `r` is the digits: x, width, and the row; + /// the tick is `tick` units left of them (5 after a chain's arrow). + pub fn written(u: Ui, r: Rect, h: Heat, tick: f32) ?Ink { + const w = theme.mark(.write); + const m = theme.metric.context; + // The tick is what a strike cools to: not under it while it is solid. + const held = h.level * (1 - (if (h.level >= 1) h.solid * h.in else 0)); + if (held > 0) u.fill(.{ .x = r.x - tick, .y = r.y + 3, .w = m.tick, .h = r.h - 6 }, mix(theme.color.page, w.solid, held)); + if (h.arm > 0) u.faded(h.arm).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, w.solid, m.read_frame); + const solid = if (h.level >= 1) h.solid * h.in else 0; + if (solid <= 0) return null; + const out = @min(h.out, m.pulse_outset); + u.fill(.{ .x = r.x - m.strike_out - out, .y = r.y + 1 - out, .w = r.w + 2 * m.strike_out + 2 * out, .h = r.h - 2 + 2 * out }, mix(theme.color.page, w.solid, solid)); + return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + } + + /// `read` in the context: a 2-unit frame in read's colour, 6 out at + /// the sides, no fill; `shows` of it (0 to 1: it fades in and out). + pub fn read(u: Ui, r: Rect, shows: f32) void { + if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, theme.mark(.read).solid, theme.metric.context.read_frame); + } + + /// `mark` on a lens's cell: armed (a 3-unit frame), struck (a solid + /// fill coming in, its edges out 4 and back), cooled to its ember and + /// a vermilion bar 4 high along its bottom edge (`held`), `ground` + /// (null: nothing) once that has gone. Returns what to set its digits in. + pub fn cellWritten(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink { const w = theme.mark(.write); const under: Color = rgb(ground orelse theme.color.page); - var body = if (h.level < 1) mix(under, w.ember, h.level) else mix(w.ember, w.solid, h.solid); + // No mark under coloured digits but a strike's: it cools to its ground, and the bar stays. + var body = if (h.level < 1) under else mix(under, w.solid, h.solid); // Timing: a strike arrives over a tenth of a second, not in a frame. - if (h.in < 1) body = mix(if (h.from == .ember) rgb(w.ember) else under, body, h.in); - // The pulse is an outset, the same on a byte and on a register, - // and the digits do not move with it. - // shortcut: a neighbour drawn after covers its side of the outset; struck cells in two passes when that shows. + if (h.in < 1) body = mix(under, body, h.in); if (h.level > 0 or ground != null) u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, body); const bar = theme.metric.ember_bar; - if (h.level >= 1 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, w.solid); - // Anticipation: where the write will land, before it does. + if (h.level > 0 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, mix(under, w.solid, @min(h.level, 1))); if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, theme.stroke.line); // Bold ink on the solid. The face changes at the middle of the // cool, where the colour is moving fastest and hides it. @@ -306,91 +355,80 @@ pub const Ui = struct { return .{ .ink = mix(cooled.ink, w.on_solid, heat), .lead = mix(cooled.lead, w.on_solid, heat), .face = if (heat >= 0.5) .mono_bold else .mono }; } - /// A thing a step read: its ember inside a frame, `shows` of it (0 to - /// 1: it fades in and out, and is never armed or pulsed). - pub fn readMark(u: Ui, r: Rect, shows: f32) void { - if (shows <= 0) return; - const m = theme.mark(.read); - const v = u.faded(shows); - v.fill(r, m.ember); - v.frame(r, m.solid, theme.stroke.line); + /// `read` on a lens's cell: a 2-unit frame 2 out, no fill. + pub fn cellRead(u: Ui, r: Rect, shows: f32) void { + if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 2, .y = r.y - 2, .w = r.w + 4, .h = r.h + 4 }, theme.mark(.read).solid, theme.stroke.rule); } - /// The same in the context, at mono 20, where a value has no cell of - /// its own: armed is a thin frame; a strike is solid behind the value - /// (its edges out a little and back), and for as long as it is solid - /// "written now" is all the value says: the ink this returns, bold on - /// the solid. It cools to nothing, and what is left for three steps is - /// a tick in the gap at the value's left: a held change never sits - /// under the digits it marks, whose ink is where they point. - pub fn struck(u: Ui, r: Rect, h: Heat) ?Ink { - const w = theme.mark(.write); - const m = theme.metric.context; - if (h.level > 0) u.fill(.{ .x = r.x - m.tick - 2, .y = r.y + 1, .w = m.tick, .h = r.h - 2 }, mix(theme.color.page, w.solid, h.level)); - if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, m.read_frame); - const solid = if (h.level >= 1) h.solid * h.in else 0; - if (solid <= 0) return null; - const out = @min(h.out, m.pulse_outset); - u.fill(.{ .x = r.x - out, .y = r.y - out, .w = r.w + 2 * out, .h = r.h + 2 * out }, mix(theme.color.page, w.solid, solid)); - return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + /// `current` on a row: gold ember across `r`, and a gold plaque `w` + /// wide at its left, its edges out `out` units (the fire). + pub fn current(u: Ui, r: Rect, w: f32, out: f32) void { + const gold = theme.mark(.current); + u.fill(r, gold.ember); + u.fill(.{ .x = r.x - out, .y = r.y - out, .w = w + 2 * out, .h = r.h + 2 * out }, gold.solid); } - /// A thing a step read, in the context: a thin frame outside the digits, and no fill. - pub fn readFrame(u: Ui, r: Rect, shows: f32) void { - if (shows > 0) u.faded(shows).frame(r, theme.mark(.read).solid, theme.metric.context.read_frame); + /// `trail`: a 4-unit bar at a row's left edge, `k` steps old (0 newest). + pub fn trail(u: Ui, x: f32, y: f32, k: usize) void { + if (k < theme.color.trail.len) u.fill(.{ .x = x, .y = y + 1, .w = 4, .h = theme.metric.context.row - 2 }, theme.color.trail[k]); } - /// One arrow of a chain, `metric.context.arrow_w` wide from `x`, on the - /// row whose middle is `y`: pointing right ("points to") or back - /// ("holds"). Faint: it is grammar, not data. - pub fn chainArrow(u: Ui, x: f32, y: f32, back: bool) void { - const m = theme.metric.context; - const c = theme.color.text.faint; - const a = x + 5; - const b = x + m.arrow_w - 5; - const head = m.arrow_head; - const pen = u.at.pen(.{ .fill = rgb(c) }); - if (back) { - u.fill(.{ .x = a + head[0] - 1, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); - sk.draw.polygon(u.f, &.{ .{ a, y }, .{ a + head[0], y - head[1] / 2 }, .{ a + head[0], y + head[1] / 2 } }, pen); - } else { - u.fill(.{ .x = a, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); - sk.draw.polygon(u.f, &.{ .{ b, y }, .{ b - head[0], y - head[1] / 2 }, .{ b - head[0], y + head[1] / 2 } }, pen); - } + /// `unknown`: never observed: two dots where the digits would be, centred in `r`. + pub fn unknown(u: Ui, r: Rect) void { + const d = 4; + const y = r.y + (r.h - d) / 2; + u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint); + u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint); } - /// A value as its class is set (design/README.md, "What a value shows - /// as"), from `x` on `baseline` at mono 20: an address (`pointer`) as - /// long as it is, in `ink`; a number under 10 as the digit; up to - /// 0xffff as hex with its decimal after it, faint; larger, hex with - /// `0x`; within 4096 of 2^64, a small negative. Returns where it ends (the faint - /// decimal apart: `Value.width` is what a mark goes behind). - pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 { + /// `plaque` `chip`: raised, `r`, behind a set flag. + pub fn chip(u: Ui, r: Rect) void { + u.fill(r, theme.color.raised); + } + + /// `pin` `row`: a register's names at the row it points at, bold, + /// primary, then 5 on a head 9 by 12 in the target's region ink, + /// pointing right at the row's address. Three names, then a count. + pub fn pinRow(u: Ui, x: f32, y: f32, names: []const []const u8, ink: anytype) void { const size = theme.size.mono.context; - const adv = advance(size); - const shown = Value.of(v, pointer); - var at = x; - if (shown.prefix.len > 0) { - u.text(ink.face, size, at, baseline, shown.prefix, ink.ink); - at += @as(f32, @floatFromInt(shown.prefix.len)) * adv; - } - var i: usize = 0; - const digits = shown.digits(); - while (i < digits.len) { - // A group ends where the digits left are a multiple of four. - const end = if (shown.grouped) i + ((digits.len - i - 1) % 4) + 1 else digits.len; - u.text(ink.face, size, at, baseline, u.print("{s}", .{digits[i..end]}), ink.ink); - at += @as(f32, @floatFromInt(end - i)) * adv + @as(f32, if (end < digits.len) theme.metric.context.group_gap else 0); - i = end; - } - if (shown.decimal) { - u.text(.mono, size, at + theme.metric.context.group_gap, baseline, u.print("{d}", .{v}), theme.color.text.faint); - at += theme.metric.context.group_gap + @as(f32, @floatFromInt(std.fmt.count("{d}", .{v}))) * adv; - } - return at; + const p = theme.metric.pin; + const shown = joined(u, names); + u.text(.mono_bold, size, x, theme.baseline(y, theme.metric.context.row, size), shown, theme.color.text.primary); + const hx = x + u.width(.mono, size, shown) + p.row_gap; + const mid = y + theme.metric.context.row / 2; + sk.draw.polygon(u.f, &.{ .{ hx + p.row_head[0], mid }, .{ hx, mid - p.row_head[1] / 2 }, .{ hx, mid + p.row_head[1] / 2 } }, u.at.pen(.{ .fill = rgb(ink) })); } - /// The veil over what cannot be read at the pace: `amount` of it, 0 to 1. + /// `pin` `under`: a stub 3 wide rising `size` + 10 to a node's bottom + /// edge at (`cx`, `y`), a head 10 by 10 on it, the first name left of + /// the stub and the others right, bold, primary. + pub fn pinUnder(u: Ui, cx: f32, y: f32, names: []const []const u8, ink: anytype, size: f32) void { + const p = theme.metric.pin; + const rise = size + 10; + u.fill(.{ .x = cx - p.stub / 2, .y = y + p.under_head[0] - 1, .w = p.stub, .h = rise - p.under_head[0] + 1 }, ink); + sk.draw.polygon(u.f, &.{ .{ cx, y + 1 }, .{ cx - p.under_head[1] / 2, y + 1 + p.under_head[0] }, .{ cx + p.under_head[1] / 2, y + 1 + p.under_head[0] } }, u.at.pen(.{ .fill = rgb(ink) })); + if (names.len == 0) return; + const by = y + 8 + size * 0.86; + u.text(.mono_bold, size, cx - p.under_names - u.width(.mono, size, names[0]), by, names[0], theme.color.text.primary); + // One name a side: a node's neighbour has its own. The rest are a count. + if (names.len > 1) u.text(.mono_bold, size, cx + p.under_names, by, if (names.len > 2) u.print("{s} +{d}", .{ names[1], names.len - 2 }) else names[1], theme.color.text.primary); + } + + /// `bracket`: a span marked at its edge, a stroke down `y0 .. y1` at + /// `x` and a serif at each end toward the span (right of the stroke, + /// or left with `side` -1). `open`: no bottom serif (a frame that + /// runs on past the panel). + pub fn bracket(u: Ui, x: f32, y0: f32, y1: f32, ink: anytype, kind: [2]f32, side: f32, open: bool) void { + const sw = kind[0]; + const serif = kind[1]; + u.fill(.{ .x = x - sw / 2, .y = y0, .w = sw, .h = y1 - y0 }, ink); + const sx = if (side > 0) x - sw / 2 else x + sw / 2 - serif - sw; + u.fill(.{ .x = sx, .y = y0, .w = serif + sw, .h = sw }, ink); + if (!open) u.fill(.{ .x = sx, .y = y1 - sw, .w = serif + sw, .h = sw }, ink); + } + + /// The veil over what cannot be read at the pace, or is not the + /// subject: `amount` of it, 0 to 1, at `alpha` (`color.veil_alpha`). pub fn veil(u: Ui, r: Rect, amount: f32) void { if (amount > 0) u.fill(r, rgb(theme.color.veil).alphaFloat(theme.color.veil_alpha * std.math.clamp(amount, 0, 1))); } @@ -406,38 +444,12 @@ pub const Ui = struct { u.f.clip(null); } - /// Never observed: two dots where the digits would be, centred in `r`. - pub fn unknown(u: Ui, r: Rect) void { - const d = theme.stroke.dot; - const y = r.y + (r.h - d) / 2; - u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint); - u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint); - } - - /// Bytes that are there and not shown: three dots and what they are. - pub fn elision(u: Ui, x: f32, y: f32, note: []const u8) void { - const d = theme.stroke.dot; - const h = theme.metric.elision_h; - for (0..3) |i| u.fill(.{ .x = x + @as(f32, @floatFromInt(i)) * 14, .y = y + (h - d) / 2, .w = d, .h = d }, theme.color.text.faint); - u.text(.sans, theme.size.sans.micro, x + 56, theme.baseline(y, h, theme.size.sans.micro), note, theme.color.text.faint); - } - - /// A name in a box 40 tall at `x, y`: hollow (a value) with `fill` - /// null, else filled (a place, a tracked object). Returns its width. - pub fn plaque(u: Ui, x: f32, y: f32, face: Face, size: f32, bytes: []const u8, ink: anytype, fill_color: ?u24, stroke: ?u24) f32 { - const m = theme.metric.plaque; - const w = u.width(face, size, bytes) + 2 * m.pad; - const r: Rect = .{ .x = x, .y = y, .w = w, .h = m.h }; - if (fill_color) |c| u.fill(r, c); - if (stroke) |c| u.frame(r, c, theme.stroke.rule); - u.text(face, size, x + m.pad, theme.baseline(y, m.h, size), bytes, ink); - return w; - } - - /// A path through `points` with filleted corners, `progress` of its - /// length drawn (an arrow growing to its target), dashed or whole, with - /// a filled head at its end or none. - pub fn arrow(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1 }) void { + /// A path through `points` with corners of radius `radius`, `progress` + /// of its length drawn from its start (a tie drawing head first, a node's + /// arrow growing to its target), dashed or whole, with a filled head at + /// its end or none. `from`: the share of its length left undrawn at its + /// start (a tie drawing back into its pin). + pub fn route(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1, from: f32 = 0, radius: f32 = theme.stroke.elbow_radius }) void { if (points.len < 2 or options.progress <= 0) return; var path: std.ArrayList(Vec) = .empty; const gpa = u.f.scratch; @@ -447,7 +459,7 @@ pub const Ui = struct { const a = points[i - 1]; const b = points[i]; const z = points[i + 1]; - const r = @min(theme.stroke.elbow_radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2); + const r = @min(options.radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2); const from = b + sk.math.withLength(a - b, r); const to = b + sk.math.withLength(z - b, r); for (0..7) |k| { @@ -457,22 +469,27 @@ pub const Ui = struct { } path.append(gpa, points[points.len - 1]) catch return; - // Cut to `progress` of its length, and short of the head. + // Cut to `progress` of its length, short of the head, and from `from` of it. var total: f32 = 0; for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| total += sk.math.distance(a, b); const head_len: f32 = if (options.head) |h| h[0] else 0; const tip_at = total * std.math.clamp(options.progress, 0, 1); - var keep = @max(tip_at - @max(head_len - 2, 0), 0); + const keep = @max(tip_at - @max(head_len - 2, 0), 0); + const start_at = total * std.math.clamp(options.from, 0, 1); var tip = path.items[0]; var dir: Vec = sk.math.normalize(path.items[1] - path.items[0]); var body: std.ArrayList(Vec) = .empty; - body.append(gpa, path.items[0]) catch return; var walked: f32 = 0; for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| { const len = sk.math.distance(a, b); if (len == 0) continue; - if (keep > 0) body.append(gpa, if (keep >= len) b else sk.math.mix(a, b, keep / len)) catch return; - keep -= len; + // The part of this segment between `start_at` and `keep`. + const lo = std.math.clamp((start_at - walked) / len, 0, 1); + const hi = std.math.clamp((keep - walked) / len, 0, 1); + if (hi > lo) { + if (body.items.len == 0) body.append(gpa, sk.math.mix(a, b, lo)) catch return; + body.append(gpa, sk.math.mix(a, b, hi)) catch return; + } if (walked <= tip_at) { dir = sk.math.normalize(b - a); tip = if (tip_at - walked >= len) b else sk.math.mix(a, b, (tip_at - walked) / len); @@ -485,13 +502,13 @@ pub const Ui = struct { if (body.items.len > 1) { if (options.dashed) sk.draw.dashed(u.f, body.items, theme.stroke.dash, stroke) else sk.draw.polyline(u.f, body.items, stroke); } - if (options.head) |h| { + if (options.head) |h| if (tip_at > start_at) { const back = tip - sk.math.scale(dir, h[0]); const side = sk.math.scale(sk.math.perp(dir), h[1] / 2); var solid = pen; solid.stroke = null; sk.draw.polygon(u.f, &.{ tip, back + side, back - side }, solid); - } + }; } /// A straight line between two points (a tree's edge). @@ -509,11 +526,12 @@ pub const Value = struct { buf: [20]u8 = undefined, len: usize = 0, prefix: []const u8 = "", - /// In groups of four from the right. - grouped: bool = false, /// Its decimal follows, faint: a small number that may be either. decimal: bool = false, + /// The class decides: an address (`pointer`) is hex as long as it is; + /// a digit is itself; a small number is hex with `0x` and its decimal; + /// a large one, hex with `0x`; within 4096 of 2^64, a small negative. pub fn of(v: u64, pointer: bool) Value { var shown: Value = .{}; const negative = !pointer and v > std.math.maxInt(u64) - 4096; @@ -523,8 +541,6 @@ pub const Value = struct { shown.len = (std.fmt.bufPrint(&shown.buf, "{d}", .{v}) catch unreachable).len; } else { shown.len = (std.fmt.bufPrint(&shown.buf, "{x}", .{v}) catch unreachable).len; - // The user's direction: hex unbroken, as a debugger prints it (the design had groups of four). - shown.grouped = false; shown.decimal = !pointer and v <= 0xffff; if (!pointer) shown.prefix = "0x"; } @@ -537,12 +553,45 @@ pub const Value = struct { /// Units its digits take at mono 20 (the faint decimal apart). pub fn width(shown: *const Value) f32 { - const adv = advance(theme.size.mono.context); - const groups: f32 = if (shown.grouped) @floatFromInt((shown.len + 3) / 4) else 1; - return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * adv + (groups - 1) * theme.metric.context.group_gap; + return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * advance(theme.size.mono.context); } }; +/// `address`'s options. +pub const Address = struct { + ink: Color = rgb(theme.color.region.code_), + lead: usize = 0, + name: []const u8 = "", + rwx: bool = false, + face: Face = .mono, +}; + +/// The kinds of `arrow`: "which points to", "which holds", "and goes on". +pub const Arrow = enum { points, holds, more }; + +/// A word of a `tag`'s legend, in its ink. +pub const Legend = struct { text: []const u8, ink: Color, under: bool = false }; + +/// A code address's name as the catalogue writes it: `name+offset`, and +/// for an address with no symbol its module, `libc+27781` (the recording +/// says `libc.so.6!27781`). +pub fn symbol(u: Ui, name: []const u8) []const u8 { + const bang = std.mem.indexOfScalar(u8, name, '!') orelse return name; + const after = name[bang + 1 ..]; + // A symbol of the module's: its own name is enough. + for (after) |ch| if (!std.ascii.isHex(ch)) return after; + const module = name[0..bang]; + return u.print("{s}+{s}", .{ module[0 .. std.mem.indexOfScalar(u8, module, '.') orelse module.len], after }); +} + +/// Names joined by a space: three, then a count. +fn joined(u: Ui, names: []const []const u8) []const u8 { + var out: []const u8 = ""; + for (names[0..@min(names.len, 3)], 0..) |n, i| out = if (i == 0) n else u.print("{s} {s}", .{ out, n }); + if (names.len > 3) out = u.print("{s} +{d}", .{ out, names.len - 3 }); + return out; +} + /// How hot a written thing is, from `cursor.Now.heat`. pub const Heat = struct { /// 0 no mark, up to 1 the mark in full (less while it leaves). @@ -583,16 +632,9 @@ test "a value is set by its class" { try std.testing.expect(address.prefix.len == 0 and !address.decimal); } -test "hex is grouped in fours" { - var buf: [20]u8 = undefined; - try std.testing.expectEqualStrings("7fff ffff d8c0", grouped(&buf, 0x7fffffffd8c0)); - try std.testing.expectEqualStrings("8000", grouped(&buf, 0x8000)); - try std.testing.expectEqualStrings("1 0000", grouped(&buf, 0x10000)); -} - -test "hex widths are whole units at the design's sizes" { - try std.testing.expectEqual(@as(f32, 18), advance(theme.size.mono.data)); - try std.testing.expectEqual(@as(f32, 315), hexWidth(16, theme.size.mono.data)); - try std.testing.expectEqual(@as(f32, 196), hexWidth(12, theme.size.mono.small)); - try std.testing.expectEqual(@as(f32, 36), hexWidth(2, theme.size.mono.data)); +test "a column's addresses share their leading digits, leaving two" { + try std.testing.expectEqual(@as(usize, 5), shared(&.{ 0x1001910, 0x1001937 })); + try std.testing.expectEqual(@as(usize, 6), shared(&.{0x28b41070})); + try std.testing.expectEqual(@as(usize, 0), shared(&.{ 0x1001910, 0x7ffe0000 })); + try std.testing.expectEqual(@as(usize, 12), digits(0x7ffe970f17b0)); } |
