Files
rivet-typst/src/render/structure.typ
T

68 lines
1.6 KiB
Typst

#import "../cetz.typ": draw
#let draw-track(config, schema, struct, pos) = draw.group(name: "track", {
let colors = schema.at("colors", default: (:))
let total-width = struct.bits * config.bit-width
let bit-colors = (:)
if struct.name in colors {
for (s, col) in colors.at(struct.name) {
let (start, end) = rng.parse-span(s)
for i in range(start, end + 1) {
bit-colors.insert(str(i), col)
}
}
}
let boundaries = ()
for r in struct.ranges.values() {
boundaries.push(if config.ltr-bits {r.end} else {r.start})
boundaries.push(if config.ltr-bits {r.start - 1} else {r.end + 1})
}
draw.group(name: "bits", {
for i in range(struct.bits) {
let rank = struct.start + i
let j = if config.ltr-bits {i} else {struct.bits - i - 1}
let col = bit-colors.at(str(rank), default: config.background)
// Background color
draw.rect(
(rel: (j * config.bit-width, 0), to: pos),
(rel: (config.bit-width, config.bit-height)),
fill: col,
stroke: none,
name: str(rank)
)
// Range boundaries
if rank in boundaries and j != struct.bits - 1 {
draw.line(
str(rank) + ".north-east",
str(rank) + ".south-east",
stroke: config.border-color
)
}
}
})
// Frame
draw.rect(
"bits.north-west", "bits.south-east",
name: "frame",
stroke: config.border-color + 2pt
)
// Names
})
#let render(
config,
schema,
struct,
pos: (0, 0),
) = draw.group(name: struct.name, {
draw-track(config, schema, struct, pos)
})