@zyro-sec/merman 0.0.0-stage → 1.0.2
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.
- package/package.json +29 -4
- package/script/layout-audit.ts +98 -0
- package/src/core/canvas.test.ts +203 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.test.ts +24 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.test.ts +116 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.test.ts +112 -0
- package/src/core/geometry.ts +347 -0
- package/src/core/mermaid.ts +38 -0
- package/src/core/render-grid.ts +51 -0
- package/src/core/spatial.test.ts +87 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.test.ts +33 -0
- package/src/core/text.ts +44 -0
- package/src/detect.ts +17 -0
- package/src/diagnostics.ts +16 -0
- package/src/flowchart/drawing.ts +340 -0
- package/src/flowchart/flowchart.test.ts +2800 -0
- package/src/flowchart/labels.test.ts +127 -0
- package/src/flowchart/labels.ts +144 -0
- package/src/flowchart/layout.ts +1427 -0
- package/src/flowchart/options.ts +12 -0
- package/src/flowchart/parser.ts +467 -0
- package/src/flowchart/render.ts +10 -0
- package/src/flowchart/routing.test.ts +368 -0
- package/src/flowchart/routing.ts +1542 -0
- package/src/flowchart/style.ts +79 -0
- package/src/flowchart/types.ts +65 -0
- package/src/gantt/diagram.test.ts +124 -0
- package/src/gantt/diagram.ts +8 -0
- package/src/gantt/drawing.ts +191 -0
- package/src/gantt/parser.ts +161 -0
- package/src/gantt/render-grid.ts +17 -0
- package/src/gantt/style.ts +36 -0
- package/src/gantt/types.ts +44 -0
- package/src/gitgraph/diagram.test.ts +183 -0
- package/src/gitgraph/diagram.ts +8 -0
- package/src/gitgraph/drawing.ts +234 -0
- package/src/gitgraph/parser.ts +219 -0
- package/src/gitgraph/render-grid.ts +17 -0
- package/src/gitgraph/style.ts +37 -0
- package/src/gitgraph/types.ts +36 -0
- package/src/layout-audit.test.ts +92 -0
- package/src/markdown.ts +336 -0
- package/src/palette.test.ts +103 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.test.ts +757 -0
- package/src/sequence/diagram.ts +8 -0
- package/src/sequence/drawing.ts +287 -0
- package/src/sequence/options.ts +8 -0
- package/src/sequence/parser.ts +282 -0
- package/src/sequence/placement.test.ts +219 -0
- package/src/sequence/placement.ts +684 -0
- package/src/sequence/render-grid.ts +22 -0
- package/src/sequence/style.ts +81 -0
- package/src/sequence/types.ts +74 -0
- package/src/state/diagram.test.ts +1386 -0
- package/src/state/diagram.ts +8 -0
- package/src/state/drawing.ts +360 -0
- package/src/state/endpoint.ts +15 -0
- package/src/state/layout.test.ts +314 -0
- package/src/state/layout.ts +1134 -0
- package/src/state/note.ts +59 -0
- package/src/state/options.ts +11 -0
- package/src/state/parser.ts +224 -0
- package/src/state/render-grid.ts +22 -0
- package/src/state/routing.test.ts +693 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.test.ts +113 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.test.ts +84 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagnostics.test.ts +129 -0
- package/src/test/diagram.ts +32 -0
- package/src/test/layout-audit/fixtures.ts +538 -0
- package/src/test/layout-audit/harness.ts +522 -0
- package/src/test/markdown.test.ts +474 -0
- package/src/timeline/diagram.test.ts +188 -0
- package/src/timeline/diagram.ts +8 -0
- package/src/timeline/drawing.ts +109 -0
- package/src/timeline/parser.ts +119 -0
- package/src/timeline/render-grid.ts +17 -0
- package/src/timeline/style.ts +36 -0
- package/src/timeline/types.ts +31 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { BorderChars } from "@opentui/core"
|
|
3
|
+
import { DiagramCanvas } from "./canvas.js"
|
|
4
|
+
import {
|
|
5
|
+
diagramArrowHead,
|
|
6
|
+
drawDiagramDiamond,
|
|
7
|
+
drawDiagramFrame,
|
|
8
|
+
drawOrthogonalPath,
|
|
9
|
+
mergeDiagramLineGlyph,
|
|
10
|
+
} from "./drawing.js"
|
|
11
|
+
|
|
12
|
+
describe("diagram drawing", () => {
|
|
13
|
+
test("merges line glyphs with square and rounded corners", () => {
|
|
14
|
+
expect(mergeDiagramLineGlyph("─", "│")).toBe("┼")
|
|
15
|
+
expect(mergeDiagramLineGlyph("─", "│", "rounded")).toBe("┼")
|
|
16
|
+
expect(mergeDiagramLineGlyph("─", "╭", "rounded")).toBe("┬")
|
|
17
|
+
expect(mergeDiagramLineGlyph("a", "│")).toBeUndefined()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test("draws frames through caller-provided cell writers", () => {
|
|
21
|
+
const canvas = new DiagramCanvas<"frame">(8, 4)
|
|
22
|
+
drawDiagramFrame(
|
|
23
|
+
{ left: 1, top: 0, width: 6, height: 4, centerX: 4, centerY: 2 },
|
|
24
|
+
BorderChars.rounded,
|
|
25
|
+
(x, y, char) => canvas.setCell(x, y, char, "frame"),
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
expect(canvas.toString({ trimBottom: true })).toBe(" ╭────╮\n │ │\n │ │\n ╰────╯")
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test("draws diamond frames through caller-provided cell writers", () => {
|
|
32
|
+
const canvas = new DiagramCanvas<"frame">(9, 5)
|
|
33
|
+
drawDiagramDiamond({ left: 0, top: 0, width: 9, height: 5, centerX: 4, centerY: 2 }, (x, y, char) =>
|
|
34
|
+
canvas.setCell(x, y, char, "frame"),
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
expect(canvas.toString({ trimBottom: true })).toBe(" ╭───╮\n╭─╯ ╰─╮\n│ │\n╰─╮ ╭─╯\n ╰───╯")
|
|
38
|
+
expect(canvas.toString()).not.toMatch(/[╱╲\\/]/)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test("draws orthogonal paths with selected corner style", () => {
|
|
42
|
+
const canvas = new DiagramCanvas<"edge">(7, 4)
|
|
43
|
+
drawOrthogonalPath(
|
|
44
|
+
[
|
|
45
|
+
{ x: 0, y: 0 },
|
|
46
|
+
{ x: 3, y: 0 },
|
|
47
|
+
{ x: 3, y: 2 },
|
|
48
|
+
{ x: 6, y: 2 },
|
|
49
|
+
],
|
|
50
|
+
(x, y, char) => canvas.setCell(x, y, char, "edge"),
|
|
51
|
+
{ cornerStyle: "rounded" },
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
expect(canvas.toString({ trimBottom: true })).toBe("───╮\n │\n ╰──")
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test("draws orthogonal paths with heavy line style", () => {
|
|
58
|
+
const canvas = new DiagramCanvas<"edge">(7, 4)
|
|
59
|
+
drawOrthogonalPath(
|
|
60
|
+
[
|
|
61
|
+
{ x: 0, y: 0 },
|
|
62
|
+
{ x: 3, y: 0 },
|
|
63
|
+
{ x: 3, y: 2 },
|
|
64
|
+
{ x: 6, y: 2 },
|
|
65
|
+
],
|
|
66
|
+
(x, y, char) => canvas.setCell(x, y, char, "edge"),
|
|
67
|
+
{ lineStyle: "heavy" },
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
expect(canvas.toString({ trimBottom: true })).toBe("━━━┓\n ┃\n ┗━━")
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test("draws orthogonal paths with dashed line style", () => {
|
|
74
|
+
const canvas = new DiagramCanvas<"edge">(9, 1)
|
|
75
|
+
drawOrthogonalPath(
|
|
76
|
+
[
|
|
77
|
+
{ x: 0, y: 0 },
|
|
78
|
+
{ x: 8, y: 0 },
|
|
79
|
+
],
|
|
80
|
+
(x, y, char) => canvas.setCell(x, y, char, "edge"),
|
|
81
|
+
{ lineStyle: "dashed" },
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
expect(canvas.toString({ trimBottom: true })).toBe("─ ─ ─ ─")
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test("keeps container frame policy separate from edge drawing policy", () => {
|
|
88
|
+
const canvas = new DiagramCanvas<"group" | "edge">(8, 5, {
|
|
89
|
+
mergeCell: (existing, incoming) => {
|
|
90
|
+
if (incoming.style === "edge") return incoming
|
|
91
|
+
return existing.char === " " ? incoming : existing
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
drawDiagramFrame(
|
|
96
|
+
{ left: 1, top: 0, width: 6, height: 5, centerX: 4, centerY: 2 },
|
|
97
|
+
BorderChars.rounded,
|
|
98
|
+
(x, y, char) => canvas.setCell(x, y, char, "group"),
|
|
99
|
+
)
|
|
100
|
+
drawOrthogonalPath(
|
|
101
|
+
[
|
|
102
|
+
{ x: 0, y: 2 },
|
|
103
|
+
{ x: 7, y: 2 },
|
|
104
|
+
],
|
|
105
|
+
(x, y, char) => canvas.setCell(x, y, char, "edge"),
|
|
106
|
+
)
|
|
107
|
+
canvas.setCell(7, 2, "▶", "edge")
|
|
108
|
+
|
|
109
|
+
expect(canvas.toString({ trimBottom: true })).toBe(" ╭────╮\n │ │\n───────▶\n │ │\n ╰────╯")
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test("selects filled and line arrow heads", () => {
|
|
113
|
+
expect(diagramArrowHead("right")).toBe("▶")
|
|
114
|
+
expect(diagramArrowHead("left", "line")).toBe("←")
|
|
115
|
+
})
|
|
116
|
+
})
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { BorderChars, type BorderCharacters } from "@opentui/core"
|
|
2
|
+
import {
|
|
3
|
+
directionBetween,
|
|
4
|
+
walkOrthogonalSegment,
|
|
5
|
+
type DiagramBounds,
|
|
6
|
+
type DiagramDirection,
|
|
7
|
+
type DiagramPoint,
|
|
8
|
+
} from "./geometry.js"
|
|
9
|
+
|
|
10
|
+
export type DiagramLineCornerStyle = "square" | "rounded"
|
|
11
|
+
export type DiagramLineStyle = "single" | "heavy" | "dashed"
|
|
12
|
+
export type DiagramArrowHeadStyle = "filled" | "line"
|
|
13
|
+
|
|
14
|
+
export interface DiagramDiamondCharacters {
|
|
15
|
+
topLeft: string
|
|
16
|
+
topRight: string
|
|
17
|
+
upperLeft: string
|
|
18
|
+
upperLeftJoin: string
|
|
19
|
+
upperRightJoin: string
|
|
20
|
+
upperRight: string
|
|
21
|
+
vertical: string
|
|
22
|
+
lowerLeft: string
|
|
23
|
+
lowerLeftJoin: string
|
|
24
|
+
lowerRightJoin: string
|
|
25
|
+
lowerRight: string
|
|
26
|
+
bottomLeft: string
|
|
27
|
+
bottomRight: string
|
|
28
|
+
horizontal: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const DIAGRAM_ARROW_HEADS = new Set(["▶", "◀", "▼", "▲", "→", "←", "↓", "↑"])
|
|
32
|
+
const HEAVY_LINE_GLYPHS = new Set(Object.values(BorderChars.heavy))
|
|
33
|
+
|
|
34
|
+
export const DIAGRAM_DIAMOND_CHARS = {
|
|
35
|
+
topLeft: "╭",
|
|
36
|
+
topRight: "╮",
|
|
37
|
+
upperLeft: "╭",
|
|
38
|
+
upperLeftJoin: "╯",
|
|
39
|
+
upperRightJoin: "╰",
|
|
40
|
+
upperRight: "╮",
|
|
41
|
+
vertical: "│",
|
|
42
|
+
lowerLeft: "╰",
|
|
43
|
+
lowerLeftJoin: "╮",
|
|
44
|
+
lowerRightJoin: "╭",
|
|
45
|
+
lowerRight: "╯",
|
|
46
|
+
bottomLeft: "╰",
|
|
47
|
+
bottomRight: "╯",
|
|
48
|
+
horizontal: "─",
|
|
49
|
+
} as const satisfies DiagramDiamondCharacters
|
|
50
|
+
|
|
51
|
+
export function diagramDiamondCharactersFromBorder(chars: BorderCharacters): DiagramDiamondCharacters {
|
|
52
|
+
return {
|
|
53
|
+
topLeft: chars.topLeft,
|
|
54
|
+
topRight: chars.topRight,
|
|
55
|
+
upperLeft: chars.topLeft,
|
|
56
|
+
upperLeftJoin: chars.bottomRight,
|
|
57
|
+
upperRightJoin: chars.bottomLeft,
|
|
58
|
+
upperRight: chars.topRight,
|
|
59
|
+
vertical: chars.vertical,
|
|
60
|
+
lowerLeft: chars.bottomLeft,
|
|
61
|
+
lowerLeftJoin: chars.topRight,
|
|
62
|
+
lowerRightJoin: chars.topLeft,
|
|
63
|
+
lowerRight: chars.bottomRight,
|
|
64
|
+
bottomLeft: chars.bottomLeft,
|
|
65
|
+
bottomRight: chars.bottomRight,
|
|
66
|
+
horizontal: chars.horizontal,
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function lineDirections(char: string): readonly DiagramDirection[] | undefined {
|
|
71
|
+
switch (char) {
|
|
72
|
+
case "─":
|
|
73
|
+
return ["left", "right"]
|
|
74
|
+
case "│":
|
|
75
|
+
return ["up", "down"]
|
|
76
|
+
case "━":
|
|
77
|
+
return ["left", "right"]
|
|
78
|
+
case "┃":
|
|
79
|
+
return ["up", "down"]
|
|
80
|
+
case "┌":
|
|
81
|
+
case "╭":
|
|
82
|
+
case "┏":
|
|
83
|
+
return ["right", "down"]
|
|
84
|
+
case "┐":
|
|
85
|
+
case "╮":
|
|
86
|
+
case "┓":
|
|
87
|
+
return ["left", "down"]
|
|
88
|
+
case "└":
|
|
89
|
+
case "╰":
|
|
90
|
+
case "┗":
|
|
91
|
+
return ["up", "right"]
|
|
92
|
+
case "┘":
|
|
93
|
+
case "╯":
|
|
94
|
+
case "┛":
|
|
95
|
+
return ["up", "left"]
|
|
96
|
+
case "├":
|
|
97
|
+
case "┣":
|
|
98
|
+
return ["up", "down", "right"]
|
|
99
|
+
case "┤":
|
|
100
|
+
case "┫":
|
|
101
|
+
return ["up", "down", "left"]
|
|
102
|
+
case "┬":
|
|
103
|
+
case "┳":
|
|
104
|
+
return ["left", "right", "down"]
|
|
105
|
+
case "┴":
|
|
106
|
+
case "┻":
|
|
107
|
+
return ["left", "right", "up"]
|
|
108
|
+
case "┼":
|
|
109
|
+
case "╋":
|
|
110
|
+
return ["up", "down", "left", "right"]
|
|
111
|
+
default:
|
|
112
|
+
return undefined
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function diagramLineGlyph(
|
|
117
|
+
directions: ReadonlySet<DiagramDirection>,
|
|
118
|
+
cornerStyle: DiagramLineCornerStyle = "square",
|
|
119
|
+
lineStyle: DiagramLineStyle = "single",
|
|
120
|
+
): string {
|
|
121
|
+
const up = directions.has("up")
|
|
122
|
+
const down = directions.has("down")
|
|
123
|
+
const left = directions.has("left")
|
|
124
|
+
const right = directions.has("right")
|
|
125
|
+
if (lineStyle === "heavy") {
|
|
126
|
+
const chars = BorderChars.heavy
|
|
127
|
+
if (up && down && left && right) return chars.cross
|
|
128
|
+
if (up && down && right) return chars.leftT
|
|
129
|
+
if (up && down && left) return chars.rightT
|
|
130
|
+
if (left && right && down) return chars.topT
|
|
131
|
+
if (left && right && up) return chars.bottomT
|
|
132
|
+
if (up && right) return chars.bottomLeft
|
|
133
|
+
if (up && left) return chars.bottomRight
|
|
134
|
+
if (down && right) return chars.topLeft
|
|
135
|
+
if (down && left) return chars.topRight
|
|
136
|
+
if (up || down) return chars.vertical
|
|
137
|
+
return chars.horizontal
|
|
138
|
+
}
|
|
139
|
+
if (up && down && left && right) return "┼"
|
|
140
|
+
if (up && down && right) return "├"
|
|
141
|
+
if (up && down && left) return "┤"
|
|
142
|
+
if (left && right && down) return "┬"
|
|
143
|
+
if (left && right && up) return "┴"
|
|
144
|
+
if (up && right) return cornerStyle === "rounded" ? "╰" : "└"
|
|
145
|
+
if (up && left) return cornerStyle === "rounded" ? "╯" : "┘"
|
|
146
|
+
if (down && right) return cornerStyle === "rounded" ? "╭" : "┌"
|
|
147
|
+
if (down && left) return cornerStyle === "rounded" ? "╮" : "┐"
|
|
148
|
+
if (up || down) return "│"
|
|
149
|
+
return "─"
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isHeavyLineGlyph(char: string): boolean {
|
|
153
|
+
return HEAVY_LINE_GLYPHS.has(char)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function segmentGlyph(direction: DiagramDirection, lineStyle: DiagramLineStyle | undefined): string {
|
|
157
|
+
const directions = new Set<DiagramDirection>(
|
|
158
|
+
direction === "left" || direction === "right" ? ["left", "right"] : ["up", "down"],
|
|
159
|
+
)
|
|
160
|
+
return diagramLineGlyph(directions, "square", lineStyle === "heavy" ? "heavy" : "single")
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function mergeDiagramLineGlyph(
|
|
164
|
+
existing: string,
|
|
165
|
+
incoming: string,
|
|
166
|
+
cornerStyle: DiagramLineCornerStyle = "square",
|
|
167
|
+
): string | undefined {
|
|
168
|
+
const existingDirections = lineDirections(existing)
|
|
169
|
+
const incomingDirections = lineDirections(incoming)
|
|
170
|
+
if (!existingDirections || !incomingDirections) return undefined
|
|
171
|
+
|
|
172
|
+
return diagramLineGlyph(
|
|
173
|
+
new Set([...existingDirections, ...incomingDirections]),
|
|
174
|
+
cornerStyle,
|
|
175
|
+
isHeavyLineGlyph(existing) && isHeavyLineGlyph(incoming) ? "heavy" : "single",
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function diagramArrowHead(direction: DiagramDirection, style: DiagramArrowHeadStyle = "filled"): string {
|
|
180
|
+
if (style === "line") {
|
|
181
|
+
if (direction === "right") return "→"
|
|
182
|
+
if (direction === "left") return "←"
|
|
183
|
+
if (direction === "up") return "↑"
|
|
184
|
+
return "↓"
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (direction === "right") return "▶"
|
|
188
|
+
if (direction === "left") return "◀"
|
|
189
|
+
if (direction === "up") return "▲"
|
|
190
|
+
return "▼"
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function diagramArrowHeadBetween(
|
|
194
|
+
from: DiagramPoint,
|
|
195
|
+
to: DiagramPoint,
|
|
196
|
+
style: DiagramArrowHeadStyle = "filled",
|
|
197
|
+
): string {
|
|
198
|
+
const direction = directionBetween(from, to)
|
|
199
|
+
return direction ? diagramArrowHead(direction, style) : diagramArrowHead("right", style)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function drawDiagramFrame(
|
|
203
|
+
bounds: DiagramBounds,
|
|
204
|
+
chars: BorderCharacters,
|
|
205
|
+
setCell: (x: number, y: number, char: string) => void,
|
|
206
|
+
): void {
|
|
207
|
+
setCell(bounds.left, bounds.top, chars.topLeft)
|
|
208
|
+
setCell(bounds.left + bounds.width - 1, bounds.top, chars.topRight)
|
|
209
|
+
setCell(bounds.left, bounds.top + bounds.height - 1, chars.bottomLeft)
|
|
210
|
+
setCell(bounds.left + bounds.width - 1, bounds.top + bounds.height - 1, chars.bottomRight)
|
|
211
|
+
for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) {
|
|
212
|
+
setCell(x, bounds.top, chars.horizontal)
|
|
213
|
+
setCell(x, bounds.top + bounds.height - 1, chars.horizontal)
|
|
214
|
+
}
|
|
215
|
+
for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
|
|
216
|
+
setCell(bounds.left, y, chars.vertical)
|
|
217
|
+
setCell(bounds.left + bounds.width - 1, y, chars.vertical)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function fillDiagramFrameInterior(bounds: DiagramBounds, setCell: (x: number, y: number) => void): void {
|
|
222
|
+
for (let y = bounds.top + 1; y < bounds.top + bounds.height - 1; y++) {
|
|
223
|
+
for (let x = bounds.left + 1; x < bounds.left + bounds.width - 1; x++) setCell(x, y)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function drawDiagramDiamond(
|
|
228
|
+
bounds: DiagramBounds,
|
|
229
|
+
setCell: (x: number, y: number, char: string) => void,
|
|
230
|
+
chars: DiagramDiamondCharacters = DIAGRAM_DIAMOND_CHARS,
|
|
231
|
+
): void {
|
|
232
|
+
const left = bounds.left
|
|
233
|
+
const right = bounds.left + bounds.width - 1
|
|
234
|
+
const top = bounds.top
|
|
235
|
+
const bottom = bounds.top + bounds.height - 1
|
|
236
|
+
const capInset = Math.min(2, Math.max(1, Math.floor((bounds.width - 1) / 2)))
|
|
237
|
+
const capLeft = left + capInset
|
|
238
|
+
const capRight = right - capInset
|
|
239
|
+
|
|
240
|
+
setCell(capLeft, top, chars.topLeft)
|
|
241
|
+
for (let x = capLeft + 1; x < capRight; x++) setCell(x, top, chars.horizontal)
|
|
242
|
+
setCell(capRight, top, chars.topRight)
|
|
243
|
+
|
|
244
|
+
setCell(left, top + 1, chars.upperLeft)
|
|
245
|
+
for (let x = left + 1; x < capLeft; x++) setCell(x, top + 1, chars.horizontal)
|
|
246
|
+
setCell(capLeft, top + 1, chars.upperLeftJoin)
|
|
247
|
+
setCell(capRight, top + 1, chars.upperRightJoin)
|
|
248
|
+
for (let x = capRight + 1; x < right; x++) setCell(x, top + 1, chars.horizontal)
|
|
249
|
+
setCell(right, top + 1, chars.upperRight)
|
|
250
|
+
|
|
251
|
+
for (let y = top + 2; y < bottom - 1; y++) {
|
|
252
|
+
setCell(left, y, chars.vertical)
|
|
253
|
+
setCell(right, y, chars.vertical)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
setCell(left, bottom - 1, chars.lowerLeft)
|
|
257
|
+
for (let x = left + 1; x < capLeft; x++) setCell(x, bottom - 1, chars.horizontal)
|
|
258
|
+
setCell(capLeft, bottom - 1, chars.lowerLeftJoin)
|
|
259
|
+
setCell(capRight, bottom - 1, chars.lowerRightJoin)
|
|
260
|
+
for (let x = capRight + 1; x < right; x++) setCell(x, bottom - 1, chars.horizontal)
|
|
261
|
+
setCell(right, bottom - 1, chars.lowerRight)
|
|
262
|
+
|
|
263
|
+
setCell(capLeft, bottom, chars.bottomLeft)
|
|
264
|
+
for (let x = capLeft + 1; x < capRight; x++) setCell(x, bottom, chars.horizontal)
|
|
265
|
+
setCell(capRight, bottom, chars.bottomRight)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function drawOrthogonalPath(
|
|
269
|
+
points: readonly DiagramPoint[],
|
|
270
|
+
setCell: (x: number, y: number, char: string) => void,
|
|
271
|
+
options: { cornerStyle?: DiagramLineCornerStyle; lineStyle?: DiagramLineStyle } = {},
|
|
272
|
+
): void {
|
|
273
|
+
for (let index = 1; index < points.length; index++) {
|
|
274
|
+
const from = points[index - 1]!
|
|
275
|
+
const to = points[index]!
|
|
276
|
+
const direction = directionBetween(from, to)
|
|
277
|
+
if (!direction) continue
|
|
278
|
+
const glyph = segmentGlyph(direction, options.lineStyle)
|
|
279
|
+
let step = index === 1 ? 0 : 1
|
|
280
|
+
walkOrthogonalSegment(from, to, index === 1, (point) => {
|
|
281
|
+
if (options.lineStyle !== "dashed" || step % 2 === 0) setCell(point.x, point.y, glyph)
|
|
282
|
+
step += 1
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (let index = 1; index < points.length - 1; index++) {
|
|
287
|
+
const previous = points[index - 1]!
|
|
288
|
+
const current = points[index]!
|
|
289
|
+
const next = points[index + 1]!
|
|
290
|
+
const fromDirection = directionBetween(current, previous)
|
|
291
|
+
const toDirection = directionBetween(current, next)
|
|
292
|
+
const directions = new Set<DiagramDirection>()
|
|
293
|
+
if (fromDirection) directions.add(fromDirection)
|
|
294
|
+
if (toDirection) directions.add(toDirection)
|
|
295
|
+
setCell(
|
|
296
|
+
current.x,
|
|
297
|
+
current.y,
|
|
298
|
+
diagramLineGlyph(directions, options.cornerStyle, options.lineStyle === "heavy" ? "heavy" : "single"),
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import {
|
|
3
|
+
diagramBoundsFromBounds,
|
|
4
|
+
diagramBoundsFromPoints,
|
|
5
|
+
diagramBoundsFromRect,
|
|
6
|
+
boundsSidePoint,
|
|
7
|
+
directionBetween,
|
|
8
|
+
lane,
|
|
9
|
+
orthogonalPath,
|
|
10
|
+
orthogonalPathPoints,
|
|
11
|
+
pathThrough,
|
|
12
|
+
pathViaLane,
|
|
13
|
+
point,
|
|
14
|
+
translateDiagramBounds,
|
|
15
|
+
walkOrthogonalSegment,
|
|
16
|
+
} from "./geometry.js"
|
|
17
|
+
|
|
18
|
+
describe("diagram geometry", () => {
|
|
19
|
+
test("directions are only defined for orthogonal point pairs", () => {
|
|
20
|
+
expect(directionBetween(point(1, 2), point(5, 2))).toBe("right")
|
|
21
|
+
expect(directionBetween(point(1, 2), point(1, 0))).toBe("up")
|
|
22
|
+
expect(directionBetween(point(1, 2), point(5, 4))).toBeUndefined()
|
|
23
|
+
expect(directionBetween(point(1, 2), point(1, 2))).toBeUndefined()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test("bounds side points describe border and outside ports", () => {
|
|
27
|
+
const bounds = { left: 10, top: 4, width: 8, height: 5, centerX: 14, centerY: 6 }
|
|
28
|
+
|
|
29
|
+
expect(boundsSidePoint(bounds, "left", "border")).toEqual(point(10, 6))
|
|
30
|
+
expect(boundsSidePoint(bounds, "left")).toEqual(point(9, 6))
|
|
31
|
+
expect(boundsSidePoint(bounds, "right", "border")).toEqual(point(17, 6))
|
|
32
|
+
expect(boundsSidePoint(bounds, "right")).toEqual(point(18, 6))
|
|
33
|
+
expect(boundsSidePoint(bounds, "top", "border")).toEqual(point(14, 4))
|
|
34
|
+
expect(boundsSidePoint(bounds, "bottom")).toEqual(point(14, 9))
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test("bounds helpers create, translate, and union bounds", () => {
|
|
38
|
+
const bounds = diagramBoundsFromRect(2, 3, 5, 4)
|
|
39
|
+
|
|
40
|
+
expect(bounds).toEqual({ left: 2, top: 3, width: 5, height: 4, centerX: 4, centerY: 5 })
|
|
41
|
+
translateDiagramBounds(bounds, 3, -1)
|
|
42
|
+
expect(bounds).toEqual({ left: 5, top: 2, width: 5, height: 4, centerX: 7, centerY: 4 })
|
|
43
|
+
expect(diagramBoundsFromBounds([bounds, diagramBoundsFromRect(0, 0, 2, 2)])).toEqual({
|
|
44
|
+
left: 0,
|
|
45
|
+
top: 0,
|
|
46
|
+
width: 10,
|
|
47
|
+
height: 6,
|
|
48
|
+
centerX: 5,
|
|
49
|
+
centerY: 3,
|
|
50
|
+
})
|
|
51
|
+
expect(diagramBoundsFromPoints([point(2, 2), point(4, 5)])).toEqual({
|
|
52
|
+
left: 2,
|
|
53
|
+
top: 2,
|
|
54
|
+
width: 3,
|
|
55
|
+
height: 4,
|
|
56
|
+
centerX: 3,
|
|
57
|
+
centerY: 4,
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test("paths compose through lanes while removing duplicate joints", () => {
|
|
62
|
+
expect(pathThrough([point(0, 0), point(0, 0), point(3, 0)])).toEqual([point(0, 0), point(3, 0)])
|
|
63
|
+
expect(pathViaLane(point(0, 0), lane("x", 4), point(8, 3))).toEqual([
|
|
64
|
+
point(0, 0),
|
|
65
|
+
point(4, 0),
|
|
66
|
+
point(4, 3),
|
|
67
|
+
point(8, 3),
|
|
68
|
+
])
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test("composed paths do not alias caller-owned points", () => {
|
|
72
|
+
const start = point(0, 0)
|
|
73
|
+
const end = point(8, 3)
|
|
74
|
+
const firstPath = pathViaLane(start, lane("x", 4), end)
|
|
75
|
+
const secondPath = pathViaLane(start, lane("x", 5), end)
|
|
76
|
+
|
|
77
|
+
expect(firstPath[0]).not.toBe(start)
|
|
78
|
+
expect(firstPath[firstPath.length - 1]).not.toBe(end)
|
|
79
|
+
expect(firstPath[0]).not.toBe(secondPath[0])
|
|
80
|
+
|
|
81
|
+
firstPath[0]!.x += 10
|
|
82
|
+
firstPath[firstPath.length - 1]!.y += 10
|
|
83
|
+
|
|
84
|
+
expect(start).toEqual(point(0, 0))
|
|
85
|
+
expect(end).toEqual(point(8, 3))
|
|
86
|
+
expect(secondPath[0]).toEqual(point(0, 0))
|
|
87
|
+
expect(secondPath[secondPath.length - 1]).toEqual(point(8, 3))
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test("orthogonal paths choose a terminal lane on the dominant axis", () => {
|
|
91
|
+
expect(orthogonalPath(point(0, 0), point(10, 4))).toEqual([point(0, 0), point(6, 0), point(6, 4), point(10, 4)])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test("orthogonal segment walkers exclude endpoints", () => {
|
|
95
|
+
const visited: Array<{ x: number; y: number }> = []
|
|
96
|
+
walkOrthogonalSegment(point(0, 0), point(3, 0), false, (next) => {
|
|
97
|
+
visited.push(next)
|
|
98
|
+
})
|
|
99
|
+
expect(visited).toEqual([point(1, 0), point(2, 0)])
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test("orthogonal path points include endpoints without duplicating joints", () => {
|
|
103
|
+
expect(orthogonalPathPoints([point(0, 0), point(3, 0), point(3, 2)])).toEqual([
|
|
104
|
+
point(0, 0),
|
|
105
|
+
point(1, 0),
|
|
106
|
+
point(2, 0),
|
|
107
|
+
point(3, 0),
|
|
108
|
+
point(3, 1),
|
|
109
|
+
point(3, 2),
|
|
110
|
+
])
|
|
111
|
+
})
|
|
112
|
+
})
|