@sccharts-lab/diagram 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +277 -0
  2. package/README.md +169 -2
  3. package/dist/dataflow.d.ts +18 -0
  4. package/dist/dataflow.js +216 -0
  5. package/dist/dataflow.js.map +1 -0
  6. package/dist/diagram.css +521 -0
  7. package/dist/elk-types.d.ts +50 -0
  8. package/dist/elk-types.js +5 -0
  9. package/dist/elk-types.js.map +1 -0
  10. package/dist/elk-worker.d.ts +10 -0
  11. package/dist/elk-worker.js +18 -0
  12. package/dist/elk-worker.js.map +1 -0
  13. package/dist/font-metrics.d.ts +13 -0
  14. package/dist/font-metrics.js +23 -0
  15. package/dist/font-metrics.js.map +1 -0
  16. package/dist/fonts/atkinson-hyperlegible-mono-latin-ext-wght-normal.woff2 +0 -0
  17. package/dist/fonts/atkinson-hyperlegible-mono-latin-wght-normal.woff2 +0 -0
  18. package/dist/fonts/atkinson-hyperlegible-mono.LICENSE.txt +93 -0
  19. package/dist/fonts/atkinson-hyperlegible-next-latin-ext-wght-normal.woff2 +0 -0
  20. package/dist/fonts/atkinson-hyperlegible-next-latin-wght-normal.woff2 +0 -0
  21. package/dist/fonts/atkinson-hyperlegible-next.LICENSE.txt +93 -0
  22. package/dist/fonts.css +42 -0
  23. package/dist/geometry.d.ts +24 -0
  24. package/dist/geometry.js +115 -0
  25. package/dist/geometry.js.map +1 -0
  26. package/dist/highlight.d.ts +35 -0
  27. package/dist/highlight.js +83 -0
  28. package/dist/highlight.js.map +1 -0
  29. package/dist/index.d.ts +16 -0
  30. package/dist/index.js +32 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/layout.d.ts +6 -0
  33. package/dist/layout.js +838 -0
  34. package/dist/layout.js.map +1 -0
  35. package/dist/measure.d.ts +23 -0
  36. package/dist/measure.js +73 -0
  37. package/dist/measure.js.map +1 -0
  38. package/dist/render.d.ts +3 -0
  39. package/dist/render.js +327 -0
  40. package/dist/render.js.map +1 -0
  41. package/dist/types.d.ts +221 -0
  42. package/dist/types.js +4 -0
  43. package/dist/types.js.map +1 -0
  44. package/package.json +70 -3
@@ -0,0 +1,216 @@
1
+ // Dataflow regions as a small circuit, in the spirit of KIELER's EquationSynthesis: every variable read is an
2
+ // input (a pentagon), every assigned variable an output, every operator a box with one input port per operand,
3
+ // and every referenced chart instance (`ref Other A`) an actor box with its inputs on the left and outputs on
4
+ // the right, wired left to right with orthogonal edges. KIELER's full synthesis (inlining, sequential
5
+ // constraints, skins) is richer; this covers the equations the guide teaches.
6
+ import { formatExpression } from "@sccharts-lab/engine/format";
7
+ import { r2 } from "./geometry.js";
8
+ import { lineMetrics } from "./measure.js";
9
+ const TEXT = { size: 10 };
10
+ const OP = { size: 10, bold: true };
11
+ const PORT = { size: 8 };
12
+ const OP_TEXT = { neg: '-', '?:': '?', pre: 'pre', val: 'val' };
13
+ export function buildCircuit(region, { measure, format, chartOf }) {
14
+ const nodes = [];
15
+ const edges = [];
16
+ const inputs = new Map();
17
+ const outputs = new Map();
18
+ const actors = new Map();
19
+ let counter = 0;
20
+ const base = region.id;
21
+ const metrics = lineMetrics(TEXT);
22
+ const portMetrics = lineMetrics(PORT);
23
+ function add(kind, text, style = TEXT) {
24
+ const node = { id: `${base}~${kind}${counter++}`, kind, text, style, inputs: [], outputs: [], width: 0, height: 0 };
25
+ nodes.push(node);
26
+ return node;
27
+ }
28
+ const port = (node, side, name) => {
29
+ const list = side === 'in' ? node.inputs : node.outputs;
30
+ if (!list.includes(name))
31
+ list.push(name);
32
+ return `${node.id}.${side}.${name}`;
33
+ };
34
+ const wire = (from, to) => edges.push({ id: `${base}~wire${edges.length}`, sources: [from], targets: [to] });
35
+ /** The referenced chart instance a reference names, if it is one (`ref Other A`). */
36
+ function actorOf(ref) {
37
+ const found = ref.decl ? format.lookup?.(ref.decl) : undefined;
38
+ if (found?.declaration.declKind !== 'reference')
39
+ return undefined;
40
+ let actor = actors.get(ref.name);
41
+ if (!actor) {
42
+ const target = found.declaration.referenceTarget;
43
+ // KIELER labels the instance box with the instance's name.
44
+ actor = add('actor', ref.name);
45
+ const chart = target ? chartOf?.(target) : undefined;
46
+ if (chart) {
47
+ // Ports in interface order, as KIELER lists them.
48
+ for (const d of chart.declarations)
49
+ for (const vo of d.valuedObjects) {
50
+ if (d.input)
51
+ port(actor, 'in', vo.name);
52
+ if (d.output)
53
+ port(actor, 'out', vo.name);
54
+ }
55
+ }
56
+ actors.set(ref.name, actor);
57
+ }
58
+ return actor;
59
+ }
60
+ /** Builds the node(s) computing `e`; returns the port that carries its value. */
61
+ function build(e) {
62
+ switch (e.kind) {
63
+ case 'ref': {
64
+ const actor = actorOf(e);
65
+ if (actor && e.sub)
66
+ return port(actor, 'out', e.sub.name);
67
+ const text = formatExpression(e, format);
68
+ let node = inputs.get(text);
69
+ if (!node)
70
+ inputs.set(text, (node = add('input', text)));
71
+ return port(node, 'out', 'value');
72
+ }
73
+ case 'op': {
74
+ const node = add('operator', OP_TEXT[e.op] ?? e.op, OP);
75
+ e.args.forEach((arg, i) => wire(build(arg), port(node, 'in', String(i))));
76
+ return port(node, 'out', 'value');
77
+ }
78
+ case 'call': {
79
+ const node = add('operator', e.name);
80
+ e.args.forEach((arg, i) => wire(build(arg), port(node, 'in', String(i))));
81
+ return port(node, 'out', 'value');
82
+ }
83
+ default: {
84
+ const node = add('constant', formatExpression(e, format));
85
+ return port(node, 'out', 'value');
86
+ }
87
+ }
88
+ }
89
+ for (const eq of region.equations) {
90
+ const actor = actorOf(eq.target);
91
+ if (actor && eq.target.sub) {
92
+ // `A.I = e`: into the instance's input I.
93
+ wire(build(eq.expression), port(actor, 'in', eq.target.sub.name));
94
+ }
95
+ else if (actor && eq.expression.kind === 'vector') {
96
+ // `A = {x, _, y}`: the instance's inputs in order; `_` leaves one unconnected.
97
+ eq.expression.values.forEach((value, i) => {
98
+ if (value.kind === 'ignore')
99
+ return;
100
+ const name = actor.inputs[i] ?? String(i);
101
+ wire(build(value), port(actor, 'in', name));
102
+ });
103
+ }
104
+ else {
105
+ const target = formatExpression(eq.target, format);
106
+ let out = outputs.get(target);
107
+ if (!out)
108
+ outputs.set(target, (out = add('output', target)));
109
+ wire(build(eq.expression), port(out, 'in', 'value'));
110
+ }
111
+ }
112
+ // Sizes.
113
+ for (const n of nodes) {
114
+ const w = measure(n.text, n.style);
115
+ if (n.kind === 'input' || n.kind === 'output') {
116
+ n.width = r2(Math.max(22, w + 16));
117
+ n.height = r2(metrics.lineHeight + 4);
118
+ }
119
+ else if (n.kind === 'actor') {
120
+ const widest = (names) => Math.max(0, ...names.map(p => measure(p, PORT)));
121
+ const rows = Math.max(n.inputs.length, n.outputs.length, 1);
122
+ n.width = r2(Math.max(w + 12, widest(n.inputs) + widest(n.outputs) + 20, 40));
123
+ n.height = r2(metrics.lineHeight + 6 + rows * (portMetrics.lineHeight + 2) + 4);
124
+ }
125
+ else {
126
+ n.width = r2(Math.max(20, w + 10));
127
+ n.height = r2(Math.max(20, 6 * n.inputs.length + 8));
128
+ }
129
+ }
130
+ const elkNodes = nodes.map(n => {
131
+ const ports = [];
132
+ const actorPortY = (i) => r2(metrics.lineHeight + 6 + i * (portMetrics.lineHeight + 2) + (portMetrics.lineHeight + 2) / 2);
133
+ n.inputs.forEach((name, i) => {
134
+ const p = { id: `${n.id}.in.${name}`, width: 0, height: 0, layoutOptions: { 'org.eclipse.elk.port.side': 'WEST', 'org.eclipse.elk.port.index': String(n.inputs.length - 1 - i) } };
135
+ if (n.kind === 'actor') {
136
+ p.x = 0;
137
+ p.y = actorPortY(i);
138
+ }
139
+ ports.push(p);
140
+ });
141
+ n.outputs.forEach((name, i) => {
142
+ const p = { id: `${n.id}.out.${name}`, width: 0, height: 0, layoutOptions: { 'org.eclipse.elk.port.side': 'EAST', 'org.eclipse.elk.port.index': String(i) } };
143
+ if (n.kind === 'actor') {
144
+ p.x = n.width;
145
+ p.y = actorPortY(i);
146
+ }
147
+ ports.push(p);
148
+ });
149
+ const layoutOptions = { 'org.eclipse.elk.portConstraints': n.kind === 'actor' ? 'FIXED_POS' : 'FIXED_ORDER' };
150
+ if (n.kind === 'input')
151
+ layoutOptions['org.eclipse.elk.layered.layering.layerConstraint'] = 'FIRST';
152
+ if (n.kind === 'output')
153
+ layoutOptions['org.eclipse.elk.layered.layering.layerConstraint'] = 'LAST';
154
+ const labels = [];
155
+ return { id: n.id, width: n.width, height: n.height, ports, layoutOptions, labels };
156
+ });
157
+ return {
158
+ nodes: elkNodes,
159
+ edges,
160
+ layoutOptions: {
161
+ 'org.eclipse.elk.direction': 'RIGHT',
162
+ 'org.eclipse.elk.edgeRouting': 'ORTHOGONAL',
163
+ 'org.eclipse.elk.spacing.nodeNode': '8',
164
+ 'org.eclipse.elk.layered.spacing.nodeNodeBetweenLayers': '18',
165
+ 'org.eclipse.elk.layered.spacing.edgeNodeBetweenLayers': '8',
166
+ 'org.eclipse.elk.layered.cycleBreaking.strategy': 'GREEDY',
167
+ },
168
+ read(node, ox, oy) {
169
+ const byId = new Map((node.children ?? []).map(c => [c.id, c]));
170
+ const out = { nodes: [], wires: [] };
171
+ const line = (text, style, x, top, height) => {
172
+ const m = lineMetrics(style);
173
+ const w = measure(text, style);
174
+ const l = {
175
+ x: r2(x), y: r2(top + (height - m.lineHeight) / 2 + m.baseline), width: r2(w), size: style.size,
176
+ runs: [{ text, dx: 0, width: r2(w), format: 'text' }], role: 'equation',
177
+ };
178
+ if (style.bold)
179
+ l.bold = true;
180
+ return l;
181
+ };
182
+ for (const n of nodes) {
183
+ const en = byId.get(n.id);
184
+ if (!en)
185
+ continue;
186
+ const x = ox + (en.x ?? 0), y = oy + (en.y ?? 0);
187
+ const w = en.width ?? n.width, h = en.height ?? n.height;
188
+ const tw = measure(n.text, n.style);
189
+ const lines = [];
190
+ if (n.kind === 'actor') {
191
+ lines.push(line(n.text, n.style, x + (w - tw) / 2, y + 2, metrics.lineHeight + 2));
192
+ const rowH = portMetrics.lineHeight + 2;
193
+ n.inputs.forEach((p, i) => lines.push(line(p, PORT, x + 4, y + metrics.lineHeight + 6 + i * rowH, rowH)));
194
+ n.outputs.forEach((p, i) => lines.push(line(p, PORT, x + w - 4 - measure(p, PORT), y + metrics.lineHeight + 6 + i * rowH, rowH)));
195
+ }
196
+ else {
197
+ // Pentagons point right; their text sits in the rectangular part.
198
+ const textX = n.kind === 'input' ? x + (w - 6 - tw) / 2 : n.kind === 'output' ? x + 6 + (w - 6 - tw) / 2 : x + (w - tw) / 2;
199
+ lines.push(line(n.text, n.style, textX, y, h));
200
+ }
201
+ out.nodes.push({ id: n.id, kind: n.kind, text: n.text, x: r2(x), y: r2(y), width: r2(w), height: r2(h), lines });
202
+ }
203
+ for (const e of node.edges ?? []) {
204
+ const points = [];
205
+ for (const s of e.sections ?? []) {
206
+ if (!points.length)
207
+ points.push(s.startPoint);
208
+ points.push(...(s.bendPoints ?? []), s.endPoint);
209
+ }
210
+ out.wires.push({ id: e.id, points: points.map(p => ({ x: r2(ox + p.x), y: r2(oy + p.y) })) });
211
+ }
212
+ return out;
213
+ },
214
+ };
215
+ }
216
+ //# sourceMappingURL=dataflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dataflow.js","sourceRoot":"","sources":["../src/dataflow.ts"],"names":[],"mappings":"AAAA,8GAA8G;AAC9G,+GAA+G;AAC/G,8GAA8G;AAC9G,sGAAsG;AACtG,8EAA8E;AAG9E,OAAO,EAAE,gBAAgB,EAAsB,oCAAmC;AAElF,OAAO,EAAE,EAAE,EAAE,sBAAkB;AAC/B,OAAO,EAAE,WAAW,EAAqC,qBAAiB;AAG1E,MAAM,IAAI,GAAc,EAAE,IAAI,EAAE,EAAE,EAAE,CAAA;AACpC,MAAM,EAAE,GAAc,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;AAC9C,MAAM,IAAI,GAAc,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;AAqBnC,MAAM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,GAAG,CAAC,CAAC,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAA;gBACpD,CAAC;gBACD,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAA;YACjG,CAAC;YACD,OAAO,GAAG,CAAA;QACd,CAAC;KACJ,CAAA;AACL,CAAC","sourcesContent":["// Dataflow regions as a small circuit, in the spirit of KIELER's EquationSynthesis: every variable read is an\n// input (a pentagon), every assigned variable an output, every operator a box with one input port per operand,\n// and every referenced chart instance (`ref Other A`) an actor box with its inputs on the left and outputs on\n// the right, wired left to right with orthogonal edges. KIELER's full synthesis (inlining, sequential\n// constraints, skins) is richer; this covers the equations the guide teaches.\n\nimport type { ElkExtendedEdge, ElkLabel, ElkNode, ElkPort } from './elk-types'\nimport { formatExpression, type FormatContext } from '@sccharts-lab/engine/format'\nimport type { Expression, Reference, Region, State } from '@sccharts-lab/engine/types'\nimport { r2 } from './geometry'\nimport { lineMetrics, type TextMeasurer, type TextStyle } from './measure'\nimport type { DataflowLayout, DataflowNodeLayout, Point, TextLine } from './types'\n\nconst TEXT: TextStyle = { size: 10 }\nconst OP: TextStyle = { size: 10, bold: true }\nconst PORT: TextStyle = { size: 8 }\n\nexport interface CircuitBuild {\n nodes: ElkNode[]\n edges: ElkExtendedEdge[]\n layoutOptions: Record<string, string>\n read(node: ElkNode, ox: number, oy: number): DataflowLayout\n}\n\ninterface CNode {\n id: string\n kind: DataflowNodeLayout['kind']\n text: string\n style: TextStyle\n width: number\n height: number\n /** Input ports in order (operators: one per operand; actors: by interface name). */\n inputs: string[]\n outputs: string[]\n}\n\nconst OP_TEXT: Record<string, string> = { neg: '-', '?:': '?', pre: 'pre', val: 'val' }\n\nexport interface CircuitContext {\n measure: TextMeasurer\n format: FormatContext\n /** Finds a chart by name, to name the ports of referenced instances by their interface. */\n chartOf?: (name: string) => State | undefined\n}\n\nexport function buildCircuit(region: Region, { measure, format, chartOf }: CircuitContext): CircuitBuild {\n const nodes: CNode[] = []\n const edges: ElkExtendedEdge[] = []\n const inputs = new Map<string, CNode>()\n const outputs = new Map<string, CNode>()\n const actors = new Map<string, CNode>()\n let counter = 0\n const base = region.id\n const metrics = lineMetrics(TEXT)\n const portMetrics = lineMetrics(PORT)\n\n function add(kind: CNode['kind'], text: string, style: TextStyle = TEXT): CNode {\n const node: CNode = { id: `${base}~${kind}${counter++}`, kind, text, style, inputs: [], outputs: [], width: 0, height: 0 }\n nodes.push(node)\n return node\n }\n const port = (node: CNode, side: 'in' | 'out', name: string) => {\n const list = side === 'in' ? node.inputs : node.outputs\n if (!list.includes(name)) list.push(name)\n return `${node.id}.${side}.${name}`\n }\n const wire = (from: string, to: string) => edges.push({ id: `${base}~wire${edges.length}`, sources: [from], targets: [to] })\n\n /** The referenced chart instance a reference names, if it is one (`ref Other A`). */\n function actorOf(ref: Reference): CNode | undefined {\n const found = ref.decl ? format.lookup?.(ref.decl) : undefined\n if (found?.declaration.declKind !== 'reference') return undefined\n let actor = actors.get(ref.name)\n if (!actor) {\n const target = found.declaration.referenceTarget\n // KIELER labels the instance box with the instance's name.\n actor = add('actor', ref.name)\n const chart = target ? chartOf?.(target) : undefined\n if (chart) {\n // Ports in interface order, as KIELER lists them.\n for (const d of chart.declarations) for (const vo of d.valuedObjects) {\n if (d.input) port(actor, 'in', vo.name)\n if (d.output) port(actor, 'out', vo.name)\n }\n }\n actors.set(ref.name, actor)\n }\n return actor\n }\n\n /** Builds the node(s) computing `e`; returns the port that carries its value. */\n function build(e: Expression): string {\n switch (e.kind) {\n case 'ref': {\n const actor = actorOf(e)\n if (actor && e.sub) return port(actor, 'out', e.sub.name)\n const text = formatExpression(e, format)\n let node = inputs.get(text)\n if (!node) inputs.set(text, (node = add('input', text)))\n return port(node, 'out', 'value')\n }\n case 'op': {\n const node = add('operator', OP_TEXT[e.op] ?? e.op, OP)\n e.args.forEach((arg, i) => wire(build(arg), port(node, 'in', String(i))))\n return port(node, 'out', 'value')\n }\n case 'call': {\n const node = add('operator', e.name)\n e.args.forEach((arg, i) => wire(build(arg), port(node, 'in', String(i))))\n return port(node, 'out', 'value')\n }\n default: {\n const node = add('constant', formatExpression(e, format))\n return port(node, 'out', 'value')\n }\n }\n }\n\n for (const eq of region.equations) {\n const actor = actorOf(eq.target)\n if (actor && eq.target.sub) {\n // `A.I = e`: into the instance's input I.\n wire(build(eq.expression), port(actor, 'in', eq.target.sub.name))\n } else if (actor && eq.expression.kind === 'vector') {\n // `A = {x, _, y}`: the instance's inputs in order; `_` leaves one unconnected.\n eq.expression.values.forEach((value, i) => {\n if (value.kind === 'ignore') return\n const name = actor.inputs[i] ?? String(i)\n wire(build(value), port(actor, 'in', name))\n })\n } else {\n const target = formatExpression(eq.target, format)\n let out = outputs.get(target)\n if (!out) outputs.set(target, (out = add('output', target)))\n wire(build(eq.expression), port(out, 'in', 'value'))\n }\n }\n\n // Sizes.\n for (const n of nodes) {\n const w = measure(n.text, n.style)\n if (n.kind === 'input' || n.kind === 'output') {\n n.width = r2(Math.max(22, w + 16))\n n.height = r2(metrics.lineHeight + 4)\n } else if (n.kind === 'actor') {\n const widest = (names: string[]) => Math.max(0, ...names.map(p => measure(p, PORT)))\n const rows = Math.max(n.inputs.length, n.outputs.length, 1)\n n.width = r2(Math.max(w + 12, widest(n.inputs) + widest(n.outputs) + 20, 40))\n n.height = r2(metrics.lineHeight + 6 + rows * (portMetrics.lineHeight + 2) + 4)\n } else {\n n.width = r2(Math.max(20, w + 10))\n n.height = r2(Math.max(20, 6 * n.inputs.length + 8))\n }\n }\n\n const elkNodes: ElkNode[] = nodes.map(n => {\n const ports: ElkPort[] = []\n const actorPortY = (i: number) => r2(metrics.lineHeight + 6 + i * (portMetrics.lineHeight + 2) + (portMetrics.lineHeight + 2) / 2)\n n.inputs.forEach((name, i) => {\n const p: ElkPort = { id: `${n.id}.in.${name}`, width: 0, height: 0, layoutOptions: { 'org.eclipse.elk.port.side': 'WEST', 'org.eclipse.elk.port.index': String(n.inputs.length - 1 - i) } }\n if (n.kind === 'actor') { p.x = 0; p.y = actorPortY(i) }\n ports.push(p)\n })\n n.outputs.forEach((name, i) => {\n const p: ElkPort = { id: `${n.id}.out.${name}`, width: 0, height: 0, layoutOptions: { 'org.eclipse.elk.port.side': 'EAST', 'org.eclipse.elk.port.index': String(i) } }\n if (n.kind === 'actor') { p.x = n.width; p.y = actorPortY(i) }\n ports.push(p)\n })\n const layoutOptions: Record<string, string> = { 'org.eclipse.elk.portConstraints': n.kind === 'actor' ? 'FIXED_POS' : 'FIXED_ORDER' }\n if (n.kind === 'input') layoutOptions['org.eclipse.elk.layered.layering.layerConstraint'] = 'FIRST'\n if (n.kind === 'output') layoutOptions['org.eclipse.elk.layered.layering.layerConstraint'] = 'LAST'\n const labels: ElkLabel[] = []\n return { id: n.id, width: n.width, height: n.height, ports, layoutOptions, labels }\n })\n\n return {\n nodes: elkNodes,\n edges,\n layoutOptions: {\n 'org.eclipse.elk.direction': 'RIGHT',\n 'org.eclipse.elk.edgeRouting': 'ORTHOGONAL',\n 'org.eclipse.elk.spacing.nodeNode': '8',\n 'org.eclipse.elk.layered.spacing.nodeNodeBetweenLayers': '18',\n 'org.eclipse.elk.layered.spacing.edgeNodeBetweenLayers': '8',\n 'org.eclipse.elk.layered.cycleBreaking.strategy': 'GREEDY',\n },\n read(node, ox, oy) {\n const byId = new Map((node.children ?? []).map(c => [c.id, c]))\n const out: DataflowLayout = { nodes: [], wires: [] }\n const line = (text: string, style: TextStyle, x: number, top: number, height: number): TextLine => {\n const m = lineMetrics(style)\n const w = measure(text, style)\n const l: TextLine = {\n x: r2(x), y: r2(top + (height - m.lineHeight) / 2 + m.baseline), width: r2(w), size: style.size,\n runs: [{ text, dx: 0, width: r2(w), format: 'text' }], role: 'equation',\n }\n if (style.bold) l.bold = true\n return l\n }\n for (const n of nodes) {\n const en = byId.get(n.id)\n if (!en) continue\n const x = ox + (en.x ?? 0), y = oy + (en.y ?? 0)\n const w = en.width ?? n.width, h = en.height ?? n.height\n const tw = measure(n.text, n.style)\n const lines: TextLine[] = []\n if (n.kind === 'actor') {\n lines.push(line(n.text, n.style, x + (w - tw) / 2, y + 2, metrics.lineHeight + 2))\n const rowH = portMetrics.lineHeight + 2\n n.inputs.forEach((p, i) => lines.push(line(p, PORT, x + 4, y + metrics.lineHeight + 6 + i * rowH, rowH)))\n n.outputs.forEach((p, i) => lines.push(line(p, PORT, x + w - 4 - measure(p, PORT), y + metrics.lineHeight + 6 + i * rowH, rowH)))\n } else {\n // Pentagons point right; their text sits in the rectangular part.\n const textX = n.kind === 'input' ? x + (w - 6 - tw) / 2 : n.kind === 'output' ? x + 6 + (w - 6 - tw) / 2 : x + (w - tw) / 2\n lines.push(line(n.text, n.style, textX, y, h))\n }\n out.nodes.push({ id: n.id, kind: n.kind, text: n.text, x: r2(x), y: r2(y), width: r2(w), height: r2(h), lines })\n }\n for (const e of node.edges ?? []) {\n const points: Point[] = []\n for (const s of e.sections ?? []) {\n if (!points.length) points.push(s.startPoint)\n points.push(...(s.bendPoints ?? []), s.endPoint)\n }\n out.wires.push({ id: e.id, points: points.map(p => ({ x: r2(ox + p.x), y: r2(oy + p.y) })) })\n }\n return out\n },\n }\n}\n"]}