@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,347 +1,347 @@
1
- export type DiagramAxis = "x" | "y"
2
- export type DiagramDirection = "up" | "down" | "left" | "right"
3
- export type DiagramSide = "left" | "right" | "top" | "bottom"
4
-
5
- export interface DiagramPoint {
6
- x: number
7
- y: number
8
- }
9
-
10
- export interface DiagramBounds {
11
- left: number
12
- top: number
13
- width: number
14
- height: number
15
- centerX: number
16
- centerY: number
17
- }
18
-
19
- export interface DiagramSegment {
20
- from: DiagramPoint
21
- to: DiagramPoint
22
- axis: DiagramAxis
23
- direction: DiagramDirection
24
- length: number
25
- }
26
-
27
- export interface DiagramLane {
28
- axis: DiagramAxis
29
- coordinate: number
30
- }
31
-
32
- export interface DiagramSpan {
33
- start: number
34
- end: number
35
- }
36
-
37
- const DIRECTION_AXIS = {
38
- left: "x",
39
- right: "x",
40
- up: "y",
41
- down: "y",
42
- } as const satisfies Record<DiagramDirection, DiagramAxis>
43
-
44
- const DIRECTION_SIGN = {
45
- left: -1,
46
- right: 1,
47
- up: -1,
48
- down: 1,
49
- } as const satisfies Record<DiagramDirection, -1 | 1>
50
-
51
- const DIRECTION_SIDE = {
52
- left: "left",
53
- right: "right",
54
- up: "top",
55
- down: "bottom",
56
- } as const satisfies Record<DiagramDirection, DiagramSide>
57
-
58
- const OPPOSITE_SIDE = {
59
- left: "right",
60
- right: "left",
61
- top: "bottom",
62
- bottom: "top",
63
- } as const satisfies Record<DiagramSide, DiagramSide>
64
-
65
- export function point(x: number, y: number): DiagramPoint {
66
- return { x, y }
67
- }
68
-
69
- export function diagramBoundsFromRect(left: number, top: number, width: number, height: number): DiagramBounds {
70
- return {
71
- left,
72
- top,
73
- width,
74
- height,
75
- centerX: left + Math.floor(width / 2),
76
- centerY: top + Math.floor(height / 2),
77
- }
78
- }
79
-
80
- export function translateDiagramBounds(bounds: DiagramBounds, dx: number, dy: number): void {
81
- bounds.left += dx
82
- bounds.top += dy
83
- bounds.centerX += dx
84
- bounds.centerY += dy
85
- }
86
-
87
- export function diagramBoundsFromBounds(bounds: readonly DiagramBounds[]): DiagramBounds | undefined {
88
- if (bounds.length === 0) return undefined
89
- const left = Math.min(...bounds.map((bound) => bound.left))
90
- const top = Math.min(...bounds.map((bound) => bound.top))
91
- const right = Math.max(...bounds.map((bound) => bound.left + bound.width))
92
- const bottom = Math.max(...bounds.map((bound) => bound.top + bound.height))
93
- return diagramBoundsFromRect(left, top, right - left, bottom - top)
94
- }
95
-
96
- export function diagramBoundsFromPoints(points: readonly DiagramPoint[]): DiagramBounds | undefined {
97
- if (points.length === 0) return undefined
98
- const left = Math.min(...points.map((point) => point.x))
99
- const top = Math.min(...points.map((point) => point.y))
100
- const right = Math.max(...points.map((point) => point.x))
101
- const bottom = Math.max(...points.map((point) => point.y))
102
- return diagramBoundsFromRect(left, top, right - left + 1, bottom - top + 1)
103
- }
104
-
105
- export function coordinate(point: DiagramPoint, axis: DiagramAxis): number {
106
- return point[axis]
107
- }
108
-
109
- export function withCoordinate(point: DiagramPoint, axis: DiagramAxis, value: number): DiagramPoint {
110
- return axis === "x" ? { x: value, y: point.y } : { x: point.x, y: value }
111
- }
112
-
113
- export function shiftPoint(point: DiagramPoint, direction: DiagramDirection, distance = 1): DiagramPoint {
114
- switch (direction) {
115
- case "left":
116
- return { x: point.x - distance, y: point.y }
117
- case "right":
118
- return { x: point.x + distance, y: point.y }
119
- case "up":
120
- return { x: point.x, y: point.y - distance }
121
- case "down":
122
- return { x: point.x, y: point.y + distance }
123
- }
124
- }
125
-
126
- export function clampPoint(point: DiagramPoint, min: DiagramPoint = { x: 0, y: 0 }): DiagramPoint {
127
- return { x: Math.max(min.x, point.x), y: Math.max(min.y, point.y) }
128
- }
129
-
130
- export function samePoint(left: DiagramPoint, right: DiagramPoint): boolean {
131
- return left.x === right.x && left.y === right.y
132
- }
133
-
134
- export function directionAxis(direction: DiagramDirection): DiagramAxis {
135
- return DIRECTION_AXIS[direction]
136
- }
137
-
138
- export function directionSign(direction: DiagramDirection): -1 | 1 {
139
- return DIRECTION_SIGN[direction]
140
- }
141
-
142
- export function sideForDirection(direction: DiagramDirection): DiagramSide {
143
- return DIRECTION_SIDE[direction]
144
- }
145
-
146
- export function oppositeSide(side: DiagramSide): DiagramSide {
147
- return OPPOSITE_SIDE[side]
148
- }
149
-
150
- export function directionBetween(from: DiagramPoint, to: DiagramPoint): DiagramDirection | undefined {
151
- if (from.y === to.y) {
152
- if (to.x > from.x) return "right"
153
- if (to.x < from.x) return "left"
154
- }
155
- if (from.x === to.x) {
156
- if (to.y > from.y) return "down"
157
- if (to.y < from.y) return "up"
158
- }
159
- return undefined
160
- }
161
-
162
- export function boundsCenter(bounds: DiagramBounds): DiagramPoint {
163
- return point(bounds.centerX, bounds.centerY)
164
- }
165
-
166
- export function boundsSidePoint(
167
- bounds: DiagramBounds,
168
- side: DiagramSide,
169
- surface: "border" | "outside" = "outside",
170
- ): DiagramPoint {
171
- switch (side) {
172
- case "left":
173
- return point(bounds.left - (surface === "outside" ? 1 : 0), bounds.centerY)
174
- case "right":
175
- return point(bounds.left + bounds.width - (surface === "outside" ? 0 : 1), bounds.centerY)
176
- case "top":
177
- return point(bounds.centerX, bounds.top - (surface === "outside" ? 1 : 0))
178
- case "bottom":
179
- return point(bounds.centerX, bounds.top + bounds.height - (surface === "outside" ? 0 : 1))
180
- }
181
- }
182
-
183
- export function centerCoordinate(bounds: DiagramBounds, axis: DiagramAxis): number {
184
- return axis === "x" ? bounds.centerX : bounds.centerY
185
- }
186
-
187
- export function snapCoordinate(source: number, target: number, tolerance: number): number {
188
- return Math.abs(source - target) <= tolerance ? target : source
189
- }
190
-
191
- export function pathThrough(points: readonly DiagramPoint[]): DiagramPoint[] {
192
- const path: DiagramPoint[] = []
193
- for (const next of points) {
194
- if (!path.length || !samePoint(path[path.length - 1]!, next)) path.push(point(next.x, next.y))
195
- }
196
- return path
197
- }
198
-
199
- export function lane(axis: DiagramAxis, coordinate: number): DiagramLane {
200
- return { axis, coordinate }
201
- }
202
-
203
- export function pathViaLane(start: DiagramPoint, routeLane: DiagramLane, end: DiagramPoint): DiagramPoint[] {
204
- return pathThrough([
205
- start,
206
- withCoordinate(start, routeLane.axis, routeLane.coordinate),
207
- withCoordinate(end, routeLane.axis, routeLane.coordinate),
208
- end,
209
- ])
210
- }
211
-
212
- function dominantAxis(start: DiagramPoint, end: DiagramPoint): DiagramAxis {
213
- return Math.abs(end.x - start.x) >= Math.abs(end.y - start.y) ? "x" : "y"
214
- }
215
-
216
- function terminalLane(start: DiagramPoint, end: DiagramPoint, axis: DiagramAxis, terminalClearance: number): number {
217
- const startCoordinate = coordinate(start, axis)
218
- const endCoordinate = coordinate(end, axis)
219
- const delta = endCoordinate - startCoordinate
220
- const sign = Math.sign(delta)
221
- if (sign === 0) return endCoordinate
222
- return endCoordinate - sign * Math.min(terminalClearance, Math.max(1, Math.abs(delta) - 1))
223
- }
224
-
225
- export function orthogonalPath(
226
- start: DiagramPoint,
227
- end: DiagramPoint,
228
- options: { preferredAxis?: DiagramAxis; terminalClearance?: number } = {},
229
- ): DiagramPoint[] {
230
- if (start.x === end.x || start.y === end.y) return pathThrough([start, end])
231
-
232
- const laneAxis = options.preferredAxis ?? dominantAxis(start, end)
233
- return pathViaLane(start, lane(laneAxis, terminalLane(start, end, laneAxis, options.terminalClearance ?? 4)), end)
234
- }
235
-
236
- export function segmentBetween(from: DiagramPoint, to: DiagramPoint): DiagramSegment | undefined {
237
- const direction = directionBetween(from, to)
238
- if (!direction) return undefined
239
- return {
240
- from,
241
- to,
242
- axis: directionAxis(direction),
243
- direction,
244
- length: Math.abs(coordinate(to, directionAxis(direction)) - coordinate(from, directionAxis(direction))),
245
- }
246
- }
247
-
248
- export function walkOrthogonalSegment(
249
- from: DiagramPoint,
250
- to: DiagramPoint,
251
- includeStart: boolean,
252
- visit: (point: DiagramPoint) => boolean | void,
253
- ): void {
254
- const direction = directionBetween(from, to)
255
- if (!direction) return
256
-
257
- const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
258
- const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
259
- let cursor = includeStart ? from : point(from.x + dx, from.y + dy)
260
-
261
- while (!samePoint(cursor, to)) {
262
- if (visit(cursor) === false) return
263
- cursor = point(cursor.x + dx, cursor.y + dy)
264
- }
265
- }
266
-
267
- export function orthogonalPathPoints(points: readonly DiagramPoint[]): DiagramPoint[] {
268
- const path: DiagramPoint[] = []
269
- for (let index = 1; index < points.length; index++) {
270
- const from = points[index - 1]!
271
- const to = points[index]!
272
- const direction = directionBetween(from, to)
273
- if (!direction) continue
274
-
275
- const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
276
- const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
277
- let cursor = path.length === 0 ? point(from.x, from.y) : point(from.x + dx, from.y + dy)
278
- while (true) {
279
- path.push(cursor)
280
- if (samePoint(cursor, to)) break
281
- cursor = point(cursor.x + dx, cursor.y + dy)
282
- }
283
- }
284
- return path
285
- }
286
-
287
- export function orderedSpan(left: number, right: number): DiagramSpan {
288
- return left <= right ? { start: left, end: right } : { start: right, end: left }
289
- }
290
-
291
- export function segmentSpan(segment: DiagramSegment): DiagramSpan {
292
- return orderedSpan(coordinate(segment.from, segment.axis), coordinate(segment.to, segment.axis))
293
- }
294
-
295
- export function pointOnSegment(segment: DiagramSegment, coordinateValue: number): DiagramPoint {
296
- return withCoordinate(segment.from, segment.axis, coordinateValue)
297
- }
298
-
299
- export function insetSpan(span: DiagramSpan, amount: number): DiagramSpan {
300
- return { start: span.start + amount, end: span.end - amount }
301
- }
302
-
303
- export function spanCapacity(span: DiagramSpan): number {
304
- return Math.max(0, span.end - span.start + 1)
305
- }
306
-
307
- export function centeredSpanStart(span: DiagramSpan, width: number): number {
308
- return span.start + Math.floor((spanCapacity(span) - width) / 2)
309
- }
310
-
311
- export function midpoint(span: DiagramSpan): number {
312
- return Math.round((span.start + span.end) / 2)
313
- }
314
-
315
- export function advanceCoordinate(origin: number, direction: DiagramDirection, distance: number): number {
316
- return origin + directionSign(direction) * distance
317
- }
318
-
319
- export function beforeNearestCoordinate(
320
- points: readonly DiagramPoint[],
321
- axis: DiagramAxis,
322
- direction: DiagramDirection,
323
- clearance: number,
324
- ): number {
325
- const coordinates = points.map((point) => coordinate(point, axis))
326
- const nearest = directionSign(direction) > 0 ? Math.min(...coordinates) : Math.max(...coordinates)
327
- return advanceCoordinate(nearest, direction, -clearance)
328
- }
329
-
330
- export function afterFarthestCoordinate(
331
- points: readonly DiagramPoint[],
332
- axis: DiagramAxis,
333
- direction: DiagramDirection,
334
- clearance: number,
335
- ): number {
336
- const coordinates = points.map((point) => coordinate(point, axis))
337
- const farthest = directionSign(direction) > 0 ? Math.max(...coordinates) : Math.min(...coordinates)
338
- return advanceCoordinate(farthest, direction, clearance)
339
- }
340
-
341
- export function keepBefore(preferred: number, boundary: number, direction: DiagramDirection): number {
342
- return directionSign(direction) > 0 ? Math.min(preferred, boundary) : Math.max(preferred, boundary)
343
- }
344
-
345
- export function keepAfter(preferred: number, boundary: number, direction: DiagramDirection): number {
346
- return directionSign(direction) > 0 ? Math.max(preferred, boundary) : Math.min(preferred, boundary)
347
- }
1
+ export type DiagramAxis = "x" | "y"
2
+ export type DiagramDirection = "up" | "down" | "left" | "right"
3
+ export type DiagramSide = "left" | "right" | "top" | "bottom"
4
+
5
+ export interface DiagramPoint {
6
+ x: number
7
+ y: number
8
+ }
9
+
10
+ export interface DiagramBounds {
11
+ left: number
12
+ top: number
13
+ width: number
14
+ height: number
15
+ centerX: number
16
+ centerY: number
17
+ }
18
+
19
+ export interface DiagramSegment {
20
+ from: DiagramPoint
21
+ to: DiagramPoint
22
+ axis: DiagramAxis
23
+ direction: DiagramDirection
24
+ length: number
25
+ }
26
+
27
+ export interface DiagramLane {
28
+ axis: DiagramAxis
29
+ coordinate: number
30
+ }
31
+
32
+ export interface DiagramSpan {
33
+ start: number
34
+ end: number
35
+ }
36
+
37
+ const DIRECTION_AXIS = {
38
+ left: "x",
39
+ right: "x",
40
+ up: "y",
41
+ down: "y",
42
+ } as const satisfies Record<DiagramDirection, DiagramAxis>
43
+
44
+ const DIRECTION_SIGN = {
45
+ left: -1,
46
+ right: 1,
47
+ up: -1,
48
+ down: 1,
49
+ } as const satisfies Record<DiagramDirection, -1 | 1>
50
+
51
+ const DIRECTION_SIDE = {
52
+ left: "left",
53
+ right: "right",
54
+ up: "top",
55
+ down: "bottom",
56
+ } as const satisfies Record<DiagramDirection, DiagramSide>
57
+
58
+ const OPPOSITE_SIDE = {
59
+ left: "right",
60
+ right: "left",
61
+ top: "bottom",
62
+ bottom: "top",
63
+ } as const satisfies Record<DiagramSide, DiagramSide>
64
+
65
+ export function point(x: number, y: number): DiagramPoint {
66
+ return { x, y }
67
+ }
68
+
69
+ export function diagramBoundsFromRect(left: number, top: number, width: number, height: number): DiagramBounds {
70
+ return {
71
+ left,
72
+ top,
73
+ width,
74
+ height,
75
+ centerX: left + Math.floor(width / 2),
76
+ centerY: top + Math.floor(height / 2),
77
+ }
78
+ }
79
+
80
+ export function translateDiagramBounds(bounds: DiagramBounds, dx: number, dy: number): void {
81
+ bounds.left += dx
82
+ bounds.top += dy
83
+ bounds.centerX += dx
84
+ bounds.centerY += dy
85
+ }
86
+
87
+ export function diagramBoundsFromBounds(bounds: readonly DiagramBounds[]): DiagramBounds | undefined {
88
+ if (bounds.length === 0) return undefined
89
+ const left = Math.min(...bounds.map((bound) => bound.left))
90
+ const top = Math.min(...bounds.map((bound) => bound.top))
91
+ const right = Math.max(...bounds.map((bound) => bound.left + bound.width))
92
+ const bottom = Math.max(...bounds.map((bound) => bound.top + bound.height))
93
+ return diagramBoundsFromRect(left, top, right - left, bottom - top)
94
+ }
95
+
96
+ export function diagramBoundsFromPoints(points: readonly DiagramPoint[]): DiagramBounds | undefined {
97
+ if (points.length === 0) return undefined
98
+ const left = Math.min(...points.map((point) => point.x))
99
+ const top = Math.min(...points.map((point) => point.y))
100
+ const right = Math.max(...points.map((point) => point.x))
101
+ const bottom = Math.max(...points.map((point) => point.y))
102
+ return diagramBoundsFromRect(left, top, right - left + 1, bottom - top + 1)
103
+ }
104
+
105
+ export function coordinate(point: DiagramPoint, axis: DiagramAxis): number {
106
+ return point[axis]
107
+ }
108
+
109
+ export function withCoordinate(point: DiagramPoint, axis: DiagramAxis, value: number): DiagramPoint {
110
+ return axis === "x" ? { x: value, y: point.y } : { x: point.x, y: value }
111
+ }
112
+
113
+ export function shiftPoint(point: DiagramPoint, direction: DiagramDirection, distance = 1): DiagramPoint {
114
+ switch (direction) {
115
+ case "left":
116
+ return { x: point.x - distance, y: point.y }
117
+ case "right":
118
+ return { x: point.x + distance, y: point.y }
119
+ case "up":
120
+ return { x: point.x, y: point.y - distance }
121
+ case "down":
122
+ return { x: point.x, y: point.y + distance }
123
+ }
124
+ }
125
+
126
+ export function clampPoint(point: DiagramPoint, min: DiagramPoint = { x: 0, y: 0 }): DiagramPoint {
127
+ return { x: Math.max(min.x, point.x), y: Math.max(min.y, point.y) }
128
+ }
129
+
130
+ export function samePoint(left: DiagramPoint, right: DiagramPoint): boolean {
131
+ return left.x === right.x && left.y === right.y
132
+ }
133
+
134
+ export function directionAxis(direction: DiagramDirection): DiagramAxis {
135
+ return DIRECTION_AXIS[direction]
136
+ }
137
+
138
+ export function directionSign(direction: DiagramDirection): -1 | 1 {
139
+ return DIRECTION_SIGN[direction]
140
+ }
141
+
142
+ export function sideForDirection(direction: DiagramDirection): DiagramSide {
143
+ return DIRECTION_SIDE[direction]
144
+ }
145
+
146
+ export function oppositeSide(side: DiagramSide): DiagramSide {
147
+ return OPPOSITE_SIDE[side]
148
+ }
149
+
150
+ export function directionBetween(from: DiagramPoint, to: DiagramPoint): DiagramDirection | undefined {
151
+ if (from.y === to.y) {
152
+ if (to.x > from.x) return "right"
153
+ if (to.x < from.x) return "left"
154
+ }
155
+ if (from.x === to.x) {
156
+ if (to.y > from.y) return "down"
157
+ if (to.y < from.y) return "up"
158
+ }
159
+ return undefined
160
+ }
161
+
162
+ export function boundsCenter(bounds: DiagramBounds): DiagramPoint {
163
+ return point(bounds.centerX, bounds.centerY)
164
+ }
165
+
166
+ export function boundsSidePoint(
167
+ bounds: DiagramBounds,
168
+ side: DiagramSide,
169
+ surface: "border" | "outside" = "outside",
170
+ ): DiagramPoint {
171
+ switch (side) {
172
+ case "left":
173
+ return point(bounds.left - (surface === "outside" ? 1 : 0), bounds.centerY)
174
+ case "right":
175
+ return point(bounds.left + bounds.width - (surface === "outside" ? 0 : 1), bounds.centerY)
176
+ case "top":
177
+ return point(bounds.centerX, bounds.top - (surface === "outside" ? 1 : 0))
178
+ case "bottom":
179
+ return point(bounds.centerX, bounds.top + bounds.height - (surface === "outside" ? 0 : 1))
180
+ }
181
+ }
182
+
183
+ export function centerCoordinate(bounds: DiagramBounds, axis: DiagramAxis): number {
184
+ return axis === "x" ? bounds.centerX : bounds.centerY
185
+ }
186
+
187
+ export function snapCoordinate(source: number, target: number, tolerance: number): number {
188
+ return Math.abs(source - target) <= tolerance ? target : source
189
+ }
190
+
191
+ export function pathThrough(points: readonly DiagramPoint[]): DiagramPoint[] {
192
+ const path: DiagramPoint[] = []
193
+ for (const next of points) {
194
+ if (!path.length || !samePoint(path[path.length - 1]!, next)) path.push(point(next.x, next.y))
195
+ }
196
+ return path
197
+ }
198
+
199
+ export function lane(axis: DiagramAxis, coordinate: number): DiagramLane {
200
+ return { axis, coordinate }
201
+ }
202
+
203
+ export function pathViaLane(start: DiagramPoint, routeLane: DiagramLane, end: DiagramPoint): DiagramPoint[] {
204
+ return pathThrough([
205
+ start,
206
+ withCoordinate(start, routeLane.axis, routeLane.coordinate),
207
+ withCoordinate(end, routeLane.axis, routeLane.coordinate),
208
+ end,
209
+ ])
210
+ }
211
+
212
+ function dominantAxis(start: DiagramPoint, end: DiagramPoint): DiagramAxis {
213
+ return Math.abs(end.x - start.x) >= Math.abs(end.y - start.y) ? "x" : "y"
214
+ }
215
+
216
+ function terminalLane(start: DiagramPoint, end: DiagramPoint, axis: DiagramAxis, terminalClearance: number): number {
217
+ const startCoordinate = coordinate(start, axis)
218
+ const endCoordinate = coordinate(end, axis)
219
+ const delta = endCoordinate - startCoordinate
220
+ const sign = Math.sign(delta)
221
+ if (sign === 0) return endCoordinate
222
+ return endCoordinate - sign * Math.min(terminalClearance, Math.max(1, Math.abs(delta) - 1))
223
+ }
224
+
225
+ export function orthogonalPath(
226
+ start: DiagramPoint,
227
+ end: DiagramPoint,
228
+ options: { preferredAxis?: DiagramAxis; terminalClearance?: number } = {},
229
+ ): DiagramPoint[] {
230
+ if (start.x === end.x || start.y === end.y) return pathThrough([start, end])
231
+
232
+ const laneAxis = options.preferredAxis ?? dominantAxis(start, end)
233
+ return pathViaLane(start, lane(laneAxis, terminalLane(start, end, laneAxis, options.terminalClearance ?? 4)), end)
234
+ }
235
+
236
+ export function segmentBetween(from: DiagramPoint, to: DiagramPoint): DiagramSegment | undefined {
237
+ const direction = directionBetween(from, to)
238
+ if (!direction) return undefined
239
+ return {
240
+ from,
241
+ to,
242
+ axis: directionAxis(direction),
243
+ direction,
244
+ length: Math.abs(coordinate(to, directionAxis(direction)) - coordinate(from, directionAxis(direction))),
245
+ }
246
+ }
247
+
248
+ export function walkOrthogonalSegment(
249
+ from: DiagramPoint,
250
+ to: DiagramPoint,
251
+ includeStart: boolean,
252
+ visit: (point: DiagramPoint) => boolean | void,
253
+ ): void {
254
+ const direction = directionBetween(from, to)
255
+ if (!direction) return
256
+
257
+ const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
258
+ const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
259
+ let cursor = includeStart ? from : point(from.x + dx, from.y + dy)
260
+
261
+ while (!samePoint(cursor, to)) {
262
+ if (visit(cursor) === false) return
263
+ cursor = point(cursor.x + dx, cursor.y + dy)
264
+ }
265
+ }
266
+
267
+ export function orthogonalPathPoints(points: readonly DiagramPoint[]): DiagramPoint[] {
268
+ const path: DiagramPoint[] = []
269
+ for (let index = 1; index < points.length; index++) {
270
+ const from = points[index - 1]!
271
+ const to = points[index]!
272
+ const direction = directionBetween(from, to)
273
+ if (!direction) continue
274
+
275
+ const dx = direction === "right" ? 1 : direction === "left" ? -1 : 0
276
+ const dy = direction === "down" ? 1 : direction === "up" ? -1 : 0
277
+ let cursor = path.length === 0 ? point(from.x, from.y) : point(from.x + dx, from.y + dy)
278
+ while (true) {
279
+ path.push(cursor)
280
+ if (samePoint(cursor, to)) break
281
+ cursor = point(cursor.x + dx, cursor.y + dy)
282
+ }
283
+ }
284
+ return path
285
+ }
286
+
287
+ export function orderedSpan(left: number, right: number): DiagramSpan {
288
+ return left <= right ? { start: left, end: right } : { start: right, end: left }
289
+ }
290
+
291
+ export function segmentSpan(segment: DiagramSegment): DiagramSpan {
292
+ return orderedSpan(coordinate(segment.from, segment.axis), coordinate(segment.to, segment.axis))
293
+ }
294
+
295
+ export function pointOnSegment(segment: DiagramSegment, coordinateValue: number): DiagramPoint {
296
+ return withCoordinate(segment.from, segment.axis, coordinateValue)
297
+ }
298
+
299
+ export function insetSpan(span: DiagramSpan, amount: number): DiagramSpan {
300
+ return { start: span.start + amount, end: span.end - amount }
301
+ }
302
+
303
+ export function spanCapacity(span: DiagramSpan): number {
304
+ return Math.max(0, span.end - span.start + 1)
305
+ }
306
+
307
+ export function centeredSpanStart(span: DiagramSpan, width: number): number {
308
+ return span.start + Math.floor((spanCapacity(span) - width) / 2)
309
+ }
310
+
311
+ export function midpoint(span: DiagramSpan): number {
312
+ return Math.round((span.start + span.end) / 2)
313
+ }
314
+
315
+ export function advanceCoordinate(origin: number, direction: DiagramDirection, distance: number): number {
316
+ return origin + directionSign(direction) * distance
317
+ }
318
+
319
+ export function beforeNearestCoordinate(
320
+ points: readonly DiagramPoint[],
321
+ axis: DiagramAxis,
322
+ direction: DiagramDirection,
323
+ clearance: number,
324
+ ): number {
325
+ const coordinates = points.map((point) => coordinate(point, axis))
326
+ const nearest = directionSign(direction) > 0 ? Math.min(...coordinates) : Math.max(...coordinates)
327
+ return advanceCoordinate(nearest, direction, -clearance)
328
+ }
329
+
330
+ export function afterFarthestCoordinate(
331
+ points: readonly DiagramPoint[],
332
+ axis: DiagramAxis,
333
+ direction: DiagramDirection,
334
+ clearance: number,
335
+ ): number {
336
+ const coordinates = points.map((point) => coordinate(point, axis))
337
+ const farthest = directionSign(direction) > 0 ? Math.max(...coordinates) : Math.min(...coordinates)
338
+ return advanceCoordinate(farthest, direction, clearance)
339
+ }
340
+
341
+ export function keepBefore(preferred: number, boundary: number, direction: DiagramDirection): number {
342
+ return directionSign(direction) > 0 ? Math.min(preferred, boundary) : Math.max(preferred, boundary)
343
+ }
344
+
345
+ export function keepAfter(preferred: number, boundary: number, direction: DiagramDirection): number {
346
+ return directionSign(direction) > 0 ? Math.max(preferred, boundary) : Math.min(preferred, boundary)
347
+ }