@zyro-sec/merman 1.0.2 → 1.0.3

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 (91) hide show
  1. package/package.json +2 -2
  2. package/script/layout-audit.ts +98 -98
  3. package/src/core/canvas.ts +228 -228
  4. package/src/core/color/style.ts +56 -56
  5. package/src/core/drawing.ts +301 -301
  6. package/src/core/geometry.ts +347 -347
  7. package/src/core/mermaid.ts +38 -38
  8. package/src/core/render-grid.ts +51 -51
  9. package/src/core/spatial.ts +243 -243
  10. package/src/core/text-lines.ts +32 -32
  11. package/src/core/text.ts +44 -44
  12. package/src/detect.ts +17 -17
  13. package/src/diagnostics.ts +16 -16
  14. package/src/flowchart/drawing.ts +340 -340
  15. package/src/flowchart/labels.ts +144 -144
  16. package/src/flowchart/layout.ts +1427 -1427
  17. package/src/flowchart/options.ts +12 -12
  18. package/src/flowchart/parser.ts +467 -467
  19. package/src/flowchart/render.ts +10 -10
  20. package/src/flowchart/routing.ts +1542 -1542
  21. package/src/flowchart/style.ts +79 -79
  22. package/src/flowchart/types.ts +65 -65
  23. package/src/gantt/diagram.ts +8 -8
  24. package/src/gantt/drawing.ts +191 -191
  25. package/src/gantt/parser.ts +161 -161
  26. package/src/gantt/render-grid.ts +17 -17
  27. package/src/gantt/style.ts +36 -36
  28. package/src/gantt/types.ts +44 -44
  29. package/src/gitgraph/diagram.ts +8 -8
  30. package/src/gitgraph/drawing.ts +234 -234
  31. package/src/gitgraph/parser.ts +219 -219
  32. package/src/gitgraph/render-grid.ts +17 -17
  33. package/src/gitgraph/style.ts +37 -37
  34. package/src/gitgraph/types.ts +36 -36
  35. package/src/markdown.ts +336 -336
  36. package/src/palette.ts +75 -75
  37. package/src/plugin.ts +15 -15
  38. package/src/sequence/diagram.ts +8 -8
  39. package/src/sequence/drawing.ts +287 -287
  40. package/src/sequence/options.ts +8 -8
  41. package/src/sequence/parser.ts +282 -282
  42. package/src/sequence/placement.ts +684 -684
  43. package/src/sequence/render-grid.ts +22 -22
  44. package/src/sequence/style.ts +81 -81
  45. package/src/sequence/types.ts +74 -74
  46. package/src/state/diagram.ts +8 -8
  47. package/src/state/drawing.ts +360 -360
  48. package/src/state/endpoint.ts +15 -15
  49. package/src/state/layout.ts +1134 -1134
  50. package/src/state/note.ts +59 -59
  51. package/src/state/options.ts +11 -11
  52. package/src/state/parser.ts +224 -224
  53. package/src/state/render-grid.ts +22 -22
  54. package/src/state/routing.ts +1522 -1522
  55. package/src/state/search.ts +103 -103
  56. package/src/state/style.ts +52 -52
  57. package/src/state/types.ts +63 -63
  58. package/src/state/visible-model.ts +77 -77
  59. package/src/test/diagram.ts +32 -32
  60. package/src/test/layout-audit/fixtures.ts +538 -538
  61. package/src/test/layout-audit/harness.ts +522 -522
  62. package/src/timeline/diagram.ts +8 -8
  63. package/src/timeline/drawing.ts +109 -109
  64. package/src/timeline/parser.ts +119 -119
  65. package/src/timeline/render-grid.ts +17 -17
  66. package/src/timeline/style.ts +36 -36
  67. package/src/timeline/types.ts +31 -31
  68. package/tsconfig.json +10 -10
  69. package/src/core/canvas.test.ts +0 -203
  70. package/src/core/color/style.test.ts +0 -24
  71. package/src/core/drawing.test.ts +0 -116
  72. package/src/core/geometry.test.ts +0 -112
  73. package/src/core/spatial.test.ts +0 -87
  74. package/src/core/text.test.ts +0 -33
  75. package/src/flowchart/flowchart.test.ts +0 -2800
  76. package/src/flowchart/labels.test.ts +0 -127
  77. package/src/flowchart/routing.test.ts +0 -368
  78. package/src/gantt/diagram.test.ts +0 -124
  79. package/src/gitgraph/diagram.test.ts +0 -183
  80. package/src/layout-audit.test.ts +0 -92
  81. package/src/palette.test.ts +0 -103
  82. package/src/sequence/diagram.test.ts +0 -757
  83. package/src/sequence/placement.test.ts +0 -219
  84. package/src/state/diagram.test.ts +0 -1386
  85. package/src/state/layout.test.ts +0 -314
  86. package/src/state/routing.test.ts +0 -693
  87. package/src/state/style.test.ts +0 -113
  88. package/src/state/visible-model.test.ts +0 -84
  89. package/src/test/diagnostics.test.ts +0 -129
  90. package/src/test/markdown.test.ts +0 -474
  91. package/src/timeline/diagram.test.ts +0 -188
@@ -1,340 +1,340 @@
1
- import { BorderChars, TextAttributes, type BorderCharacters, type BorderStyle } from "@opentui/core"
2
- import { walkOrthogonalSegment, type DiagramDirection } from "../core/geometry.js"
3
- import { DiagramCanvas, type DiagramCanvasCell } from "../core/canvas.js"
4
- import { DIAGRAM_LABEL_PADDING_X, parseDiagramTextLines } from "../core/text.js"
5
- import type { DiagramTextRun } from "../core/text-lines.js"
6
- import {
7
- DIAGRAM_ARROW_HEADS,
8
- diagramArrowHeadBetween,
9
- diagramDiamondCharactersFromBorder,
10
- diagramLineGlyph,
11
- drawDiagramDiamond,
12
- drawDiagramFrame,
13
- fillDiagramFrameInterior,
14
- drawOrthogonalPath,
15
- mergeDiagramLineGlyph,
16
- } from "../core/drawing.js"
17
- import { layoutFlowchartDiagram, visualLength } from "./layout.js"
18
- import { flowchartRouteLabelLayout } from "./labels.js"
19
- import type { FlowchartDiagramRenderOptions } from "./options.js"
20
- import { flowchartDirectionBetween, flowchartSourceConnector } from "./routing.js"
21
- import {
22
- DATABASE_EDGE_FADE_STYLES,
23
- NODE_EDGE_FADE_STYLES,
24
- type FlowchartCellStyle,
25
- type FlowchartCellMetadata,
26
- type FlowchartEdgeFadeStyle,
27
- type FlowchartGrid,
28
- } from "./style.js"
29
- import type {
30
- FlowchartDiagram,
31
- FlowchartEdgeRoute,
32
- FlowchartEndpointBounds,
33
- FlowchartNode,
34
- FlowchartNodeBounds,
35
- FlowchartPoint,
36
- FlowchartSubgraphBounds,
37
- } from "./types.js"
38
-
39
- export const DEFAULT_BORDER_STYLE = "rounded" satisfies BorderStyle
40
- function mergeFlowchartCell(
41
- existing: DiagramCanvasCell<FlowchartCellStyle>,
42
- incoming: DiagramCanvasCell<FlowchartCellStyle>,
43
- ): DiagramCanvasCell<FlowchartCellStyle> {
44
- if (incoming.style !== "edge") return incoming
45
- if (existing.style === "label") return existing
46
- if (incoming.char === " ") return existing
47
- if (existing.style !== "edge" || existing.char === " ") return incoming
48
- if (DIAGRAM_ARROW_HEADS.has(existing.char)) return existing
49
- if (DIAGRAM_ARROW_HEADS.has(incoming.char)) return incoming
50
-
51
- return {
52
- ...incoming,
53
- char: mergeDiagramLineGlyph(existing.char, incoming.char, "rounded") ?? incoming.char,
54
- } as DiagramCanvasCell<FlowchartCellStyle>
55
- }
56
-
57
- function setRichText(
58
- grid: FlowchartGrid,
59
- x: number,
60
- y: number,
61
- runs: readonly DiagramTextRun[],
62
- style: FlowchartCellStyle,
63
- ): number {
64
- let offset = 0
65
- for (const run of runs) {
66
- grid.setText(x + offset, y, run.text, style, run.italic ? { attributes: TextAttributes.ITALIC } : undefined)
67
- offset += visualLength(run.text)
68
- }
69
- return offset
70
- }
71
-
72
- function drawNode(
73
- grid: FlowchartGrid,
74
- node: FlowchartNode,
75
- bounds: FlowchartNodeBounds,
76
- borderStyle: BorderStyle,
77
- ): void {
78
- const chars = BorderChars[borderStyle]
79
- const style: FlowchartCellStyle = node.shape === "database" ? "database" : "node"
80
- const border: FlowchartCellStyle = node.shape === "database" ? "databaseBorder" : "nodeBorder"
81
-
82
- if (node.shape === "decision") {
83
- drawDiagramDiamond(
84
- bounds,
85
- (x, y, char) => grid.setCell(x, y, char, border),
86
- diagramDiamondCharactersFromBorder(chars),
87
- )
88
- } else if (node.shape === "subroutine") {
89
- fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
90
- drawSubroutineNode(grid, bounds, chars, border)
91
- } else if (node.shape === "database") {
92
- fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
93
- drawDatabaseNode(grid, bounds, chars, border)
94
- } else {
95
- fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
96
- drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, border))
97
- }
98
-
99
- const textTop =
100
- node.shape === "decision"
101
- ? bounds.top + Math.floor((bounds.height - bounds.lines.length) / 2)
102
- : node.shape === "database"
103
- ? bounds.top + 2
104
- : bounds.top + 1
105
- const lines = parseDiagramTextLines(node.label)
106
- for (const [index, line] of bounds.lines.entries()) {
107
- const lineX =
108
- node.shape === "subroutine"
109
- ? bounds.left + 3
110
- : bounds.left + Math.max(1, Math.floor((bounds.width - visualLength(line)) / 2))
111
- setRichText(grid, lineX, textTop + index, lines[index]!.runs, style)
112
- }
113
- }
114
-
115
- function drawSubroutineNode(
116
- grid: FlowchartGrid,
117
- bounds: FlowchartNodeBounds,
118
- chars: BorderCharacters,
119
- style: FlowchartCellStyle,
120
- ): void {
121
- drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, style))
122
- const leftRailX = bounds.left + 2
123
- const rightRailX = bounds.left + bounds.width - 3
124
- grid.setCell(leftRailX, bounds.top, chars.topT, style)
125
- grid.setCell(rightRailX, bounds.top, chars.topT, style)
126
- grid.setCell(leftRailX, bounds.top + bounds.height - 1, chars.bottomT, style)
127
- grid.setCell(rightRailX, bounds.top + bounds.height - 1, chars.bottomT, style)
128
- for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
129
- grid.setCell(leftRailX, y, chars.vertical, style)
130
- grid.setCell(rightRailX, y, chars.vertical, style)
131
- }
132
- }
133
-
134
- function drawDatabaseNode(
135
- grid: FlowchartGrid,
136
- bounds: FlowchartNodeBounds,
137
- chars: BorderCharacters,
138
- style: FlowchartCellStyle,
139
- ): void {
140
- drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, style))
141
- const topRailY = bounds.top + 1
142
- const bottomRailY = bounds.top + bounds.height - 2
143
- for (const y of [topRailY, bottomRailY]) {
144
- grid.setCell(bounds.left, y, chars.leftT, style)
145
- grid.setCell(bounds.left + bounds.width - 1, y, chars.rightT, style)
146
- for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) {
147
- grid.setCell(x, y, chars.horizontal, style)
148
- }
149
- }
150
- }
151
-
152
- function drawSubgraphFrame(grid: FlowchartGrid, bounds: FlowchartSubgraphBounds, borderStyle: BorderStyle): void {
153
- const chars = BorderChars[borderStyle]
154
- drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, "group"))
155
- }
156
-
157
- function drawSubgraphLabel(grid: FlowchartGrid, bounds: FlowchartSubgraphBounds): void {
158
- if (bounds.label) {
159
- const lines = parseDiagramTextLines(bounds.label)
160
- const labelY = bounds.labelSide === "top" ? bounds.top : bounds.top + bounds.height - lines.length
161
- for (const [index, line] of lines.entries()) {
162
- grid.setText(bounds.left + 2, labelY + index, " ", "groupLabel")
163
- const width = setRichText(grid, bounds.left + 3, labelY + index, line.runs, "groupLabel")
164
- grid.setText(bounds.left + width + 3, labelY + index, " ", "groupLabel")
165
- }
166
- }
167
- }
168
-
169
- function drawEdgeLabel(grid: FlowchartGrid, route: FlowchartEdgeRoute, style: FlowchartCellStyle): void {
170
- const label = flowchartRouteLabelLayout(route, visualLength)
171
- const padding = " ".repeat(DIAGRAM_LABEL_PADDING_X)
172
- for (const [index, line] of parseDiagramTextLines(route.edge.label).entries()) {
173
- grid.setText(label.point.x, label.point.y + index, padding, style)
174
- const width = setRichText(grid, label.point.x + DIAGRAM_LABEL_PADDING_X, label.point.y + index, line.runs, style)
175
- grid.setText(label.point.x + width + DIAGRAM_LABEL_PADDING_X, label.point.y + index, padding, style)
176
- }
177
- }
178
-
179
- function drawRoutedEdge(grid: FlowchartGrid, route: FlowchartEdgeRoute): void {
180
- const { edge, points } = route
181
- if (points.length < 2) return
182
- const style: FlowchartCellStyle = "edge"
183
-
184
- drawOrthogonalPath(points, (x, y, char) => grid.setCell(x, y, char, style), {
185
- cornerStyle: "rounded",
186
- lineStyle: edge.style === "thick" ? "heavy" : "single",
187
- })
188
- if (edge.arrowhead !== false) {
189
- const end = points[points.length - 1]!
190
- const arrowFrom = points[points.length - 2]!
191
- grid.setCell(end.x, end.y, diagramArrowHeadBetween(arrowFrom, end), style)
192
- } else {
193
- const end = points[points.length - 1]!
194
- const endDirection = flowchartDirectionBetween(points[points.length - 2]!, end)
195
- if (endDirection) grid.setCell(end.x, end.y, diagramLineGlyph(new Set([endDirection])), style)
196
- }
197
- if (edge.label) {
198
- drawEdgeLabel(grid, route, "label")
199
- }
200
- }
201
-
202
- function flowchartNodeStyle(node: FlowchartNode | undefined): "node" | "database" {
203
- return node?.shape === "database" ? "database" : "node"
204
- }
205
-
206
- function sourceFadeStyles(sourceStyle: "node" | "database"): readonly FlowchartEdgeFadeStyle[] {
207
- return sourceStyle === "database" ? DATABASE_EDGE_FADE_STYLES : NODE_EDGE_FADE_STYLES
208
- }
209
-
210
- function styleExistingEdgeCell(grid: FlowchartGrid, x: number, y: number, style: FlowchartEdgeFadeStyle): void {
211
- const cell = grid.getCell(x, y)
212
- if (cell) grid.setCell(x, y, cell.char, style)
213
- }
214
-
215
- function routeCellOccupancy(routes: readonly FlowchartEdgeRoute[]): Map<string, number> {
216
- const occupancy = new Map<string, number>()
217
- for (const route of routes) {
218
- const routeCells = new Set<string>()
219
- for (let index = 1; index < route.points.length; index++) {
220
- walkOrthogonalSegment(route.points[index - 1]!, route.points[index]!, index === 1, (point) => {
221
- routeCells.add(`${point.x}:${point.y}`)
222
- })
223
- }
224
- for (const key of routeCells) occupancy.set(key, (occupancy.get(key) ?? 0) + 1)
225
- }
226
- return occupancy
227
- }
228
-
229
- function fadeSourcePath(
230
- grid: FlowchartGrid,
231
- connector: FlowchartPoint,
232
- points: FlowchartPoint[],
233
- styles: readonly FlowchartEdgeFadeStyle[],
234
- occupancy: ReadonlyMap<string, number>,
235
- ): void {
236
- const from = points[0]
237
- const to = points[1]
238
- if (!from || !to) return
239
- const privateCells = [connector]
240
-
241
- walkOrthogonalSegment(from, to, true, (point) => {
242
- const key = `${point.x}:${point.y}`
243
- const cell = grid.getCell(point.x, point.y)
244
- if (occupancy.get(key) !== 1 || !cell || !"─━│┃".includes(cell.char) || cell.style === "label") return false
245
- privateCells.push(point)
246
- })
247
-
248
- for (const [index, point] of privateCells.entries()) {
249
- const styleIndex = Math.min(
250
- styles.length - 1,
251
- Math.floor(((index + 1) * styles.length) / (privateCells.length + 1)),
252
- )
253
- styleExistingEdgeCell(grid, point.x, point.y, styles[styleIndex]!)
254
- }
255
- }
256
-
257
- function drawSourceConnectors(
258
- grid: FlowchartGrid,
259
- diagram: FlowchartDiagram,
260
- endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
261
- routes: readonly FlowchartEdgeRoute[],
262
- ): void {
263
- const nodesById = new Map(diagram.nodes.map((node) => [node.id, node]))
264
- const occupancy = routeCellOccupancy(routes)
265
- const sourceJunctions = new Map<string, { directions: Set<DiagramDirection>; heavy: boolean }>()
266
- for (const route of routes) {
267
- const sourcePoint = route.points[0]
268
- const next = route.points[1]
269
- if (!sourcePoint || !next) continue
270
- const key = `${route.edge.from}:${sourcePoint.x}:${sourcePoint.y}`
271
- const direction = flowchartDirectionBetween(sourcePoint, next)
272
- const junction = sourceJunctions.get(key) ?? { directions: new Set<DiagramDirection>(), heavy: true }
273
- if (direction) junction.directions.add(direction)
274
- junction.heavy &&= route.edge.style === "thick"
275
- sourceJunctions.set(key, junction)
276
- }
277
-
278
- for (const route of routes) {
279
- const from = endpoints.get(route.edge.from)
280
- const sourcePoint = route.points[0]
281
- if (!from || !sourcePoint) continue
282
- const styles = sourceFadeStyles(flowchartNodeStyle(nodesById.get(route.edge.from)))
283
- const connector = flowchartSourceConnector(from, sourcePoint)
284
- grid.setCell(connector.x, connector.y, connector.char, "edge")
285
- const connectorDirection = flowchartDirectionBetween(sourcePoint, connector)
286
- if (connectorDirection) {
287
- const cell = grid.getCell(sourcePoint.x, sourcePoint.y)
288
- if (cell && cell.style !== "label" && !DIAGRAM_ARROW_HEADS.has(cell.char)) {
289
- const key = `${route.edge.from}:${sourcePoint.x}:${sourcePoint.y}`
290
- const junction = sourceJunctions.get(key)
291
- const directions = new Set(junction?.directions)
292
- directions.add(connectorDirection)
293
- grid.replaceCell(
294
- sourcePoint.x,
295
- sourcePoint.y,
296
- diagramLineGlyph(
297
- directions,
298
- "rounded",
299
- (junction?.heavy ?? route.edge.style === "thick") ? "heavy" : "single",
300
- ),
301
- "edge",
302
- )
303
- }
304
- }
305
- fadeSourcePath(grid, connector, route.points, styles, occupancy)
306
- if (route.edge.sourceArrowhead && route.points[1]) {
307
- grid.setCell(sourcePoint.x, sourcePoint.y, diagramArrowHeadBetween(route.points[1], sourcePoint), "edge")
308
- }
309
- }
310
- }
311
-
312
- export function drawFlowchartDiagramGrid(
313
- diagram: FlowchartDiagram,
314
- options: FlowchartDiagramRenderOptions = {},
315
- ): FlowchartGrid {
316
- const borderStyle = options.borderStyle ?? DEFAULT_BORDER_STYLE
317
- const layout = layoutFlowchartDiagram(diagram, options)
318
- const { bounds, routes, subgraphBounds, width, height } = layout
319
- diagram = layout.diagram
320
- const grid = new DiagramCanvas<FlowchartCellStyle, FlowchartCellMetadata>(width, height, {
321
- mergeCell: mergeFlowchartCell,
322
- })
323
- for (const subgraph of diagram.subgraphs ?? []) {
324
- const bound = subgraphBounds.get(subgraph.id)
325
- if (bound) drawSubgraphFrame(grid, bound, borderStyle)
326
- }
327
- for (const route of routes) drawRoutedEdge(grid, route)
328
- for (const node of diagram.nodes) {
329
- const bound = bounds.get(node.id)
330
- if (bound) drawNode(grid, node, bound, borderStyle)
331
- }
332
- const endpoints = new Map<string, FlowchartEndpointBounds>([...subgraphBounds, ...bounds])
333
- drawSourceConnectors(grid, diagram, endpoints, routes)
334
- for (const subgraph of diagram.subgraphs ?? []) {
335
- const bound = subgraphBounds.get(subgraph.id)
336
- if (bound) drawSubgraphLabel(grid, bound)
337
- }
338
-
339
- return grid
340
- }
1
+ import { BorderChars, TextAttributes, type BorderCharacters, type BorderStyle } from "@opentui/core"
2
+ import { walkOrthogonalSegment, type DiagramDirection } from "../core/geometry.js"
3
+ import { DiagramCanvas, type DiagramCanvasCell } from "../core/canvas.js"
4
+ import { DIAGRAM_LABEL_PADDING_X, parseDiagramTextLines } from "../core/text.js"
5
+ import type { DiagramTextRun } from "../core/text-lines.js"
6
+ import {
7
+ DIAGRAM_ARROW_HEADS,
8
+ diagramArrowHeadBetween,
9
+ diagramDiamondCharactersFromBorder,
10
+ diagramLineGlyph,
11
+ drawDiagramDiamond,
12
+ drawDiagramFrame,
13
+ fillDiagramFrameInterior,
14
+ drawOrthogonalPath,
15
+ mergeDiagramLineGlyph,
16
+ } from "../core/drawing.js"
17
+ import { layoutFlowchartDiagram, visualLength } from "./layout.js"
18
+ import { flowchartRouteLabelLayout } from "./labels.js"
19
+ import type { FlowchartDiagramRenderOptions } from "./options.js"
20
+ import { flowchartDirectionBetween, flowchartSourceConnector } from "./routing.js"
21
+ import {
22
+ DATABASE_EDGE_FADE_STYLES,
23
+ NODE_EDGE_FADE_STYLES,
24
+ type FlowchartCellStyle,
25
+ type FlowchartCellMetadata,
26
+ type FlowchartEdgeFadeStyle,
27
+ type FlowchartGrid,
28
+ } from "./style.js"
29
+ import type {
30
+ FlowchartDiagram,
31
+ FlowchartEdgeRoute,
32
+ FlowchartEndpointBounds,
33
+ FlowchartNode,
34
+ FlowchartNodeBounds,
35
+ FlowchartPoint,
36
+ FlowchartSubgraphBounds,
37
+ } from "./types.js"
38
+
39
+ export const DEFAULT_BORDER_STYLE = "rounded" satisfies BorderStyle
40
+ function mergeFlowchartCell(
41
+ existing: DiagramCanvasCell<FlowchartCellStyle>,
42
+ incoming: DiagramCanvasCell<FlowchartCellStyle>,
43
+ ): DiagramCanvasCell<FlowchartCellStyle> {
44
+ if (incoming.style !== "edge") return incoming
45
+ if (existing.style === "label") return existing
46
+ if (incoming.char === " ") return existing
47
+ if (existing.style !== "edge" || existing.char === " ") return incoming
48
+ if (DIAGRAM_ARROW_HEADS.has(existing.char)) return existing
49
+ if (DIAGRAM_ARROW_HEADS.has(incoming.char)) return incoming
50
+
51
+ return {
52
+ ...incoming,
53
+ char: mergeDiagramLineGlyph(existing.char, incoming.char, "rounded") ?? incoming.char,
54
+ } as DiagramCanvasCell<FlowchartCellStyle>
55
+ }
56
+
57
+ function setRichText(
58
+ grid: FlowchartGrid,
59
+ x: number,
60
+ y: number,
61
+ runs: readonly DiagramTextRun[],
62
+ style: FlowchartCellStyle,
63
+ ): number {
64
+ let offset = 0
65
+ for (const run of runs) {
66
+ grid.setText(x + offset, y, run.text, style, run.italic ? { attributes: TextAttributes.ITALIC } : undefined)
67
+ offset += visualLength(run.text)
68
+ }
69
+ return offset
70
+ }
71
+
72
+ function drawNode(
73
+ grid: FlowchartGrid,
74
+ node: FlowchartNode,
75
+ bounds: FlowchartNodeBounds,
76
+ borderStyle: BorderStyle,
77
+ ): void {
78
+ const chars = BorderChars[borderStyle]
79
+ const style: FlowchartCellStyle = node.shape === "database" ? "database" : "node"
80
+ const border: FlowchartCellStyle = node.shape === "database" ? "databaseBorder" : "nodeBorder"
81
+
82
+ if (node.shape === "decision") {
83
+ drawDiagramDiamond(
84
+ bounds,
85
+ (x, y, char) => grid.setCell(x, y, char, border),
86
+ diagramDiamondCharactersFromBorder(chars),
87
+ )
88
+ } else if (node.shape === "subroutine") {
89
+ fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
90
+ drawSubroutineNode(grid, bounds, chars, border)
91
+ } else if (node.shape === "database") {
92
+ fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
93
+ drawDatabaseNode(grid, bounds, chars, border)
94
+ } else {
95
+ fillDiagramFrameInterior(bounds, (x, y) => grid.setCell(x, y, " ", style))
96
+ drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, border))
97
+ }
98
+
99
+ const textTop =
100
+ node.shape === "decision"
101
+ ? bounds.top + Math.floor((bounds.height - bounds.lines.length) / 2)
102
+ : node.shape === "database"
103
+ ? bounds.top + 2
104
+ : bounds.top + 1
105
+ const lines = parseDiagramTextLines(node.label)
106
+ for (const [index, line] of bounds.lines.entries()) {
107
+ const lineX =
108
+ node.shape === "subroutine"
109
+ ? bounds.left + 3
110
+ : bounds.left + Math.max(1, Math.floor((bounds.width - visualLength(line)) / 2))
111
+ setRichText(grid, lineX, textTop + index, lines[index]!.runs, style)
112
+ }
113
+ }
114
+
115
+ function drawSubroutineNode(
116
+ grid: FlowchartGrid,
117
+ bounds: FlowchartNodeBounds,
118
+ chars: BorderCharacters,
119
+ style: FlowchartCellStyle,
120
+ ): void {
121
+ drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, style))
122
+ const leftRailX = bounds.left + 2
123
+ const rightRailX = bounds.left + bounds.width - 3
124
+ grid.setCell(leftRailX, bounds.top, chars.topT, style)
125
+ grid.setCell(rightRailX, bounds.top, chars.topT, style)
126
+ grid.setCell(leftRailX, bounds.top + bounds.height - 1, chars.bottomT, style)
127
+ grid.setCell(rightRailX, bounds.top + bounds.height - 1, chars.bottomT, style)
128
+ for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
129
+ grid.setCell(leftRailX, y, chars.vertical, style)
130
+ grid.setCell(rightRailX, y, chars.vertical, style)
131
+ }
132
+ }
133
+
134
+ function drawDatabaseNode(
135
+ grid: FlowchartGrid,
136
+ bounds: FlowchartNodeBounds,
137
+ chars: BorderCharacters,
138
+ style: FlowchartCellStyle,
139
+ ): void {
140
+ drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, style))
141
+ const topRailY = bounds.top + 1
142
+ const bottomRailY = bounds.top + bounds.height - 2
143
+ for (const y of [topRailY, bottomRailY]) {
144
+ grid.setCell(bounds.left, y, chars.leftT, style)
145
+ grid.setCell(bounds.left + bounds.width - 1, y, chars.rightT, style)
146
+ for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) {
147
+ grid.setCell(x, y, chars.horizontal, style)
148
+ }
149
+ }
150
+ }
151
+
152
+ function drawSubgraphFrame(grid: FlowchartGrid, bounds: FlowchartSubgraphBounds, borderStyle: BorderStyle): void {
153
+ const chars = BorderChars[borderStyle]
154
+ drawDiagramFrame(bounds, chars, (x, y, char) => grid.setCell(x, y, char, "group"))
155
+ }
156
+
157
+ function drawSubgraphLabel(grid: FlowchartGrid, bounds: FlowchartSubgraphBounds): void {
158
+ if (bounds.label) {
159
+ const lines = parseDiagramTextLines(bounds.label)
160
+ const labelY = bounds.labelSide === "top" ? bounds.top : bounds.top + bounds.height - lines.length
161
+ for (const [index, line] of lines.entries()) {
162
+ grid.setText(bounds.left + 2, labelY + index, " ", "groupLabel")
163
+ const width = setRichText(grid, bounds.left + 3, labelY + index, line.runs, "groupLabel")
164
+ grid.setText(bounds.left + width + 3, labelY + index, " ", "groupLabel")
165
+ }
166
+ }
167
+ }
168
+
169
+ function drawEdgeLabel(grid: FlowchartGrid, route: FlowchartEdgeRoute, style: FlowchartCellStyle): void {
170
+ const label = flowchartRouteLabelLayout(route, visualLength)
171
+ const padding = " ".repeat(DIAGRAM_LABEL_PADDING_X)
172
+ for (const [index, line] of parseDiagramTextLines(route.edge.label).entries()) {
173
+ grid.setText(label.point.x, label.point.y + index, padding, style)
174
+ const width = setRichText(grid, label.point.x + DIAGRAM_LABEL_PADDING_X, label.point.y + index, line.runs, style)
175
+ grid.setText(label.point.x + width + DIAGRAM_LABEL_PADDING_X, label.point.y + index, padding, style)
176
+ }
177
+ }
178
+
179
+ function drawRoutedEdge(grid: FlowchartGrid, route: FlowchartEdgeRoute): void {
180
+ const { edge, points } = route
181
+ if (points.length < 2) return
182
+ const style: FlowchartCellStyle = "edge"
183
+
184
+ drawOrthogonalPath(points, (x, y, char) => grid.setCell(x, y, char, style), {
185
+ cornerStyle: "rounded",
186
+ lineStyle: edge.style === "thick" ? "heavy" : "single",
187
+ })
188
+ if (edge.arrowhead !== false) {
189
+ const end = points[points.length - 1]!
190
+ const arrowFrom = points[points.length - 2]!
191
+ grid.setCell(end.x, end.y, diagramArrowHeadBetween(arrowFrom, end), style)
192
+ } else {
193
+ const end = points[points.length - 1]!
194
+ const endDirection = flowchartDirectionBetween(points[points.length - 2]!, end)
195
+ if (endDirection) grid.setCell(end.x, end.y, diagramLineGlyph(new Set([endDirection])), style)
196
+ }
197
+ if (edge.label) {
198
+ drawEdgeLabel(grid, route, "label")
199
+ }
200
+ }
201
+
202
+ function flowchartNodeStyle(node: FlowchartNode | undefined): "node" | "database" {
203
+ return node?.shape === "database" ? "database" : "node"
204
+ }
205
+
206
+ function sourceFadeStyles(sourceStyle: "node" | "database"): readonly FlowchartEdgeFadeStyle[] {
207
+ return sourceStyle === "database" ? DATABASE_EDGE_FADE_STYLES : NODE_EDGE_FADE_STYLES
208
+ }
209
+
210
+ function styleExistingEdgeCell(grid: FlowchartGrid, x: number, y: number, style: FlowchartEdgeFadeStyle): void {
211
+ const cell = grid.getCell(x, y)
212
+ if (cell) grid.setCell(x, y, cell.char, style)
213
+ }
214
+
215
+ function routeCellOccupancy(routes: readonly FlowchartEdgeRoute[]): Map<string, number> {
216
+ const occupancy = new Map<string, number>()
217
+ for (const route of routes) {
218
+ const routeCells = new Set<string>()
219
+ for (let index = 1; index < route.points.length; index++) {
220
+ walkOrthogonalSegment(route.points[index - 1]!, route.points[index]!, index === 1, (point) => {
221
+ routeCells.add(`${point.x}:${point.y}`)
222
+ })
223
+ }
224
+ for (const key of routeCells) occupancy.set(key, (occupancy.get(key) ?? 0) + 1)
225
+ }
226
+ return occupancy
227
+ }
228
+
229
+ function fadeSourcePath(
230
+ grid: FlowchartGrid,
231
+ connector: FlowchartPoint,
232
+ points: FlowchartPoint[],
233
+ styles: readonly FlowchartEdgeFadeStyle[],
234
+ occupancy: ReadonlyMap<string, number>,
235
+ ): void {
236
+ const from = points[0]
237
+ const to = points[1]
238
+ if (!from || !to) return
239
+ const privateCells = [connector]
240
+
241
+ walkOrthogonalSegment(from, to, true, (point) => {
242
+ const key = `${point.x}:${point.y}`
243
+ const cell = grid.getCell(point.x, point.y)
244
+ if (occupancy.get(key) !== 1 || !cell || !"─━│┃".includes(cell.char) || cell.style === "label") return false
245
+ privateCells.push(point)
246
+ })
247
+
248
+ for (const [index, point] of privateCells.entries()) {
249
+ const styleIndex = Math.min(
250
+ styles.length - 1,
251
+ Math.floor(((index + 1) * styles.length) / (privateCells.length + 1)),
252
+ )
253
+ styleExistingEdgeCell(grid, point.x, point.y, styles[styleIndex]!)
254
+ }
255
+ }
256
+
257
+ function drawSourceConnectors(
258
+ grid: FlowchartGrid,
259
+ diagram: FlowchartDiagram,
260
+ endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
261
+ routes: readonly FlowchartEdgeRoute[],
262
+ ): void {
263
+ const nodesById = new Map(diagram.nodes.map((node) => [node.id, node]))
264
+ const occupancy = routeCellOccupancy(routes)
265
+ const sourceJunctions = new Map<string, { directions: Set<DiagramDirection>; heavy: boolean }>()
266
+ for (const route of routes) {
267
+ const sourcePoint = route.points[0]
268
+ const next = route.points[1]
269
+ if (!sourcePoint || !next) continue
270
+ const key = `${route.edge.from}:${sourcePoint.x}:${sourcePoint.y}`
271
+ const direction = flowchartDirectionBetween(sourcePoint, next)
272
+ const junction = sourceJunctions.get(key) ?? { directions: new Set<DiagramDirection>(), heavy: true }
273
+ if (direction) junction.directions.add(direction)
274
+ junction.heavy &&= route.edge.style === "thick"
275
+ sourceJunctions.set(key, junction)
276
+ }
277
+
278
+ for (const route of routes) {
279
+ const from = endpoints.get(route.edge.from)
280
+ const sourcePoint = route.points[0]
281
+ if (!from || !sourcePoint) continue
282
+ const styles = sourceFadeStyles(flowchartNodeStyle(nodesById.get(route.edge.from)))
283
+ const connector = flowchartSourceConnector(from, sourcePoint)
284
+ grid.setCell(connector.x, connector.y, connector.char, "edge")
285
+ const connectorDirection = flowchartDirectionBetween(sourcePoint, connector)
286
+ if (connectorDirection) {
287
+ const cell = grid.getCell(sourcePoint.x, sourcePoint.y)
288
+ if (cell && cell.style !== "label" && !DIAGRAM_ARROW_HEADS.has(cell.char)) {
289
+ const key = `${route.edge.from}:${sourcePoint.x}:${sourcePoint.y}`
290
+ const junction = sourceJunctions.get(key)
291
+ const directions = new Set(junction?.directions)
292
+ directions.add(connectorDirection)
293
+ grid.replaceCell(
294
+ sourcePoint.x,
295
+ sourcePoint.y,
296
+ diagramLineGlyph(
297
+ directions,
298
+ "rounded",
299
+ (junction?.heavy ?? route.edge.style === "thick") ? "heavy" : "single",
300
+ ),
301
+ "edge",
302
+ )
303
+ }
304
+ }
305
+ fadeSourcePath(grid, connector, route.points, styles, occupancy)
306
+ if (route.edge.sourceArrowhead && route.points[1]) {
307
+ grid.setCell(sourcePoint.x, sourcePoint.y, diagramArrowHeadBetween(route.points[1], sourcePoint), "edge")
308
+ }
309
+ }
310
+ }
311
+
312
+ export function drawFlowchartDiagramGrid(
313
+ diagram: FlowchartDiagram,
314
+ options: FlowchartDiagramRenderOptions = {},
315
+ ): FlowchartGrid {
316
+ const borderStyle = options.borderStyle ?? DEFAULT_BORDER_STYLE
317
+ const layout = layoutFlowchartDiagram(diagram, options)
318
+ const { bounds, routes, subgraphBounds, width, height } = layout
319
+ diagram = layout.diagram
320
+ const grid = new DiagramCanvas<FlowchartCellStyle, FlowchartCellMetadata>(width, height, {
321
+ mergeCell: mergeFlowchartCell,
322
+ })
323
+ for (const subgraph of diagram.subgraphs ?? []) {
324
+ const bound = subgraphBounds.get(subgraph.id)
325
+ if (bound) drawSubgraphFrame(grid, bound, borderStyle)
326
+ }
327
+ for (const route of routes) drawRoutedEdge(grid, route)
328
+ for (const node of diagram.nodes) {
329
+ const bound = bounds.get(node.id)
330
+ if (bound) drawNode(grid, node, bound, borderStyle)
331
+ }
332
+ const endpoints = new Map<string, FlowchartEndpointBounds>([...subgraphBounds, ...bounds])
333
+ drawSourceConnectors(grid, diagram, endpoints, routes)
334
+ for (const subgraph of diagram.subgraphs ?? []) {
335
+ const bound = subgraphBounds.get(subgraph.id)
336
+ if (bound) drawSubgraphLabel(grid, bound)
337
+ }
338
+
339
+ return grid
340
+ }