@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,127 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { flowchartEdgeLabelLayout } from "./labels.js"
|
|
3
|
+
|
|
4
|
+
const measure = (text: string): number => text.length
|
|
5
|
+
|
|
6
|
+
describe("flowchart edge labels", () => {
|
|
7
|
+
test("places vertical-route labels beside the bus", () => {
|
|
8
|
+
expect(
|
|
9
|
+
flowchartEdgeLabelLayout(
|
|
10
|
+
[
|
|
11
|
+
{ x: 22, y: 3 },
|
|
12
|
+
{ x: 22, y: 7 },
|
|
13
|
+
],
|
|
14
|
+
"rollback",
|
|
15
|
+
measure,
|
|
16
|
+
).point,
|
|
17
|
+
).toEqual({ x: 23, y: 5 })
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test("places labels inline only when padded text fits with clearance", () => {
|
|
21
|
+
expect(
|
|
22
|
+
flowchartEdgeLabelLayout(
|
|
23
|
+
[
|
|
24
|
+
{ x: 0, y: 2 },
|
|
25
|
+
{ x: 13, y: 2 },
|
|
26
|
+
],
|
|
27
|
+
"rollback",
|
|
28
|
+
measure,
|
|
29
|
+
).point,
|
|
30
|
+
).toEqual({ x: 2, y: 2 })
|
|
31
|
+
|
|
32
|
+
expect(
|
|
33
|
+
flowchartEdgeLabelLayout(
|
|
34
|
+
[
|
|
35
|
+
{ x: 0, y: 2 },
|
|
36
|
+
{ x: 9, y: 2 },
|
|
37
|
+
],
|
|
38
|
+
"rollback",
|
|
39
|
+
measure,
|
|
40
|
+
).point,
|
|
41
|
+
).toEqual({ x: 2, y: 1 })
|
|
42
|
+
|
|
43
|
+
expect(
|
|
44
|
+
flowchartEdgeLabelLayout(
|
|
45
|
+
[
|
|
46
|
+
{ x: 0, y: 2 },
|
|
47
|
+
{ x: 7, y: 2 },
|
|
48
|
+
],
|
|
49
|
+
"rollback",
|
|
50
|
+
measure,
|
|
51
|
+
).point,
|
|
52
|
+
).toEqual({ x: 2, y: 1 })
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("uses vertical bus labels before short terminal branches", () => {
|
|
56
|
+
expect(
|
|
57
|
+
flowchartEdgeLabelLayout(
|
|
58
|
+
[
|
|
59
|
+
{ x: 155, y: 5 },
|
|
60
|
+
{ x: 150, y: 5 },
|
|
61
|
+
{ x: 150, y: 9 },
|
|
62
|
+
{ x: 146, y: 9 },
|
|
63
|
+
],
|
|
64
|
+
"rollback",
|
|
65
|
+
measure,
|
|
66
|
+
).point,
|
|
67
|
+
).toEqual({ x: 151, y: 7 })
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test("keeps side-route labels on the vertical bus when horizontal arms grow", () => {
|
|
71
|
+
expect(
|
|
72
|
+
flowchartEdgeLabelLayout(
|
|
73
|
+
[
|
|
74
|
+
{ x: 5, y: 2 },
|
|
75
|
+
{ x: 30, y: 2 },
|
|
76
|
+
{ x: 30, y: 10 },
|
|
77
|
+
{ x: 5, y: 10 },
|
|
78
|
+
],
|
|
79
|
+
"parallel label",
|
|
80
|
+
measure,
|
|
81
|
+
"y",
|
|
82
|
+
).point,
|
|
83
|
+
).toEqual({ x: 31, y: 6 })
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test("measures br-delimited edge label lines as a block", () => {
|
|
87
|
+
const layout = flowchartEdgeLabelLayout(
|
|
88
|
+
[
|
|
89
|
+
{ x: 0, y: 2 },
|
|
90
|
+
{ x: 20, y: 2 },
|
|
91
|
+
],
|
|
92
|
+
"first<br/>second line",
|
|
93
|
+
measure,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
expect(layout.lines).toEqual([" first ", " second line "])
|
|
97
|
+
expect(layout.width).toBe(13)
|
|
98
|
+
expect(layout.height).toBe(2)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test("places multiline horizontal edge labels outside the route row", () => {
|
|
102
|
+
const layout = flowchartEdgeLabelLayout(
|
|
103
|
+
[
|
|
104
|
+
{ x: 0, y: 5 },
|
|
105
|
+
{ x: 20, y: 5 },
|
|
106
|
+
],
|
|
107
|
+
"first<br/>second",
|
|
108
|
+
measure,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
expect(layout.point.y + layout.height).toBeLessThanOrEqual(5)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test("centers multiline vertical edge labels beside their route", () => {
|
|
115
|
+
const layout = flowchartEdgeLabelLayout(
|
|
116
|
+
[
|
|
117
|
+
{ x: 22, y: 2 },
|
|
118
|
+
{ x: 22, y: 10 },
|
|
119
|
+
],
|
|
120
|
+
"one<br/>two<br/>three",
|
|
121
|
+
measure,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
expect(layout.point).toEqual({ x: 23, y: 5 })
|
|
125
|
+
expect(layout.height).toBe(3)
|
|
126
|
+
})
|
|
127
|
+
})
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clampPoint,
|
|
3
|
+
centeredSpanStart,
|
|
4
|
+
insetSpan,
|
|
5
|
+
midpoint,
|
|
6
|
+
point,
|
|
7
|
+
pointOnSegment,
|
|
8
|
+
segmentBetween,
|
|
9
|
+
segmentSpan,
|
|
10
|
+
shiftPoint,
|
|
11
|
+
spanCapacity,
|
|
12
|
+
type DiagramSegment,
|
|
13
|
+
} from "../core/geometry.js"
|
|
14
|
+
import { measureDiagramLabelWidth, padDiagramLabelLine, splitDiagramLines } from "../core/text.js"
|
|
15
|
+
import type { FlowchartEdgeRoute, FlowchartPoint } from "./types.js"
|
|
16
|
+
|
|
17
|
+
const LABEL_BUS_CLEARANCE = 3
|
|
18
|
+
const LABEL_NODE_CLEARANCE = 2
|
|
19
|
+
const LABEL_LINE_CLEARANCE = 2
|
|
20
|
+
const LABEL_TERMINAL_CLEARANCE = 1
|
|
21
|
+
|
|
22
|
+
export interface FlowchartEdgeLabelLayout {
|
|
23
|
+
lines: string[]
|
|
24
|
+
point: FlowchartPoint
|
|
25
|
+
width: number
|
|
26
|
+
height: number
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function flowchartLabelText(label: string): string {
|
|
30
|
+
return padDiagramLabelLine(label)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function flowchartLabelWidth(label: string, measure: (text: string) => number): number {
|
|
34
|
+
return measureDiagramLabelWidth(label, measure)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function minimumInlineLabelLength(labelWidth: number): number {
|
|
38
|
+
return labelWidth + LABEL_LINE_CLEARANCE * 2 - 1
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function flowchartHorizontalLabelRankGap(labelWidth: number): number {
|
|
42
|
+
return minimumInlineLabelLength(labelWidth) + LABEL_BUS_CLEARANCE + 1
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function flowchartVerticalBranchLabelGap(labelWidth: number): number {
|
|
46
|
+
return minimumInlineLabelLength(labelWidth) + LABEL_BUS_CLEARANCE + LABEL_NODE_CLEARANCE
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function inlineLabelSlot(segment: DiagramSegment, labelWidth: number): { x: number; fits: boolean } {
|
|
50
|
+
const slot = insetSpan(segmentSpan(segment), LABEL_LINE_CLEARANCE)
|
|
51
|
+
return { x: centeredSpanStart(slot, labelWidth), fits: spanCapacity(slot) >= labelWidth }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function segmentLabelPoint(segment: DiagramSegment, labelWidth: number, labelHeight: number): FlowchartPoint {
|
|
55
|
+
if (segment.axis === "x") {
|
|
56
|
+
const slot = inlineLabelSlot(segment, labelWidth)
|
|
57
|
+
if (labelHeight === 1 && slot.fits) return point(slot.x, segment.from.y)
|
|
58
|
+
|
|
59
|
+
if (labelHeight > 1) {
|
|
60
|
+
return shiftPoint(point(slot.x, segment.from.y), "up", labelHeight)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return clampPoint(shiftPoint(shiftPoint(segment.from, segment.direction, LABEL_LINE_CLEARANCE), "up", labelHeight))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const slot = insetSpan(segmentSpan(segment), LABEL_TERMINAL_CLEARANCE)
|
|
67
|
+
if (spanCapacity(slot) >= labelHeight) {
|
|
68
|
+
return clampPoint(point(segment.from.x + 1, centeredSpanStart(slot, labelHeight)))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const center = shiftPoint(pointOnSegment(segment, midpoint(segmentSpan(segment))), "right")
|
|
72
|
+
return clampPoint(shiftPoint(center, "up", Math.floor((labelHeight - 1) / 2)))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function bestLabelSegment(
|
|
76
|
+
points: readonly FlowchartPoint[],
|
|
77
|
+
labelWidth: number,
|
|
78
|
+
preferredAxis?: DiagramSegment["axis"],
|
|
79
|
+
preferredSegment?: number,
|
|
80
|
+
): DiagramSegment | undefined {
|
|
81
|
+
const segments = points.slice(1).flatMap((to, index) => {
|
|
82
|
+
const segment = segmentBetween(points[index]!, to)
|
|
83
|
+
return segment ? [segment] : []
|
|
84
|
+
})
|
|
85
|
+
if (preferredSegment !== undefined && segments[preferredSegment]) return segments[preferredSegment]
|
|
86
|
+
const preferred = preferredAxis ? segments.find((segment) => segment.axis === preferredAxis) : undefined
|
|
87
|
+
if (preferred) return preferred
|
|
88
|
+
|
|
89
|
+
let roomyHorizontal: DiagramSegment | undefined
|
|
90
|
+
let verticalBus: DiagramSegment | undefined
|
|
91
|
+
let longest: DiagramSegment | undefined
|
|
92
|
+
|
|
93
|
+
for (const segment of segments) {
|
|
94
|
+
if (!roomyHorizontal && segment.axis === "x" && inlineLabelSlot(segment, labelWidth).fits) roomyHorizontal = segment
|
|
95
|
+
if (!verticalBus && segment.axis === "y") verticalBus = segment
|
|
96
|
+
if (!longest || segment.length > longest.length) longest = segment
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return roomyHorizontal ?? verticalBus ?? longest
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function flowchartLabelPoint(
|
|
103
|
+
points: readonly FlowchartPoint[],
|
|
104
|
+
labelWidth: number,
|
|
105
|
+
labelHeight: number,
|
|
106
|
+
preferredAxis?: DiagramSegment["axis"],
|
|
107
|
+
preferredSegment?: number,
|
|
108
|
+
): FlowchartPoint {
|
|
109
|
+
const segment = bestLabelSegment(points, labelWidth, preferredAxis, preferredSegment)
|
|
110
|
+
return segment ? segmentLabelPoint(segment, labelWidth, labelHeight) : (points[0] ?? point(0, 0))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function flowchartEdgeLabelLayout(
|
|
114
|
+
points: readonly FlowchartPoint[],
|
|
115
|
+
label: string,
|
|
116
|
+
measure: (text: string) => number,
|
|
117
|
+
preferredAxis?: DiagramSegment["axis"],
|
|
118
|
+
preferredSegment?: number,
|
|
119
|
+
): FlowchartEdgeLabelLayout {
|
|
120
|
+
const lines = splitDiagramLines(label).map(flowchartLabelText)
|
|
121
|
+
const width = flowchartLabelWidth(label, measure)
|
|
122
|
+
const height = lines.length
|
|
123
|
+
return {
|
|
124
|
+
lines,
|
|
125
|
+
point: flowchartLabelPoint(points, width, height, preferredAxis, preferredSegment),
|
|
126
|
+
width,
|
|
127
|
+
height,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function flowchartRouteLabelLayout(
|
|
132
|
+
route: Pick<FlowchartEdgeRoute, "edge" | "points" | "labelAxis" | "labelPoint">,
|
|
133
|
+
measure: (text: string) => number,
|
|
134
|
+
): FlowchartEdgeLabelLayout {
|
|
135
|
+
const lines = splitDiagramLines(route.edge.label).map(flowchartLabelText)
|
|
136
|
+
const width = flowchartLabelWidth(route.edge.label, measure)
|
|
137
|
+
const height = lines.length
|
|
138
|
+
return {
|
|
139
|
+
lines,
|
|
140
|
+
point: route.labelPoint ?? flowchartLabelPoint(route.points, width, height, route.labelAxis),
|
|
141
|
+
width,
|
|
142
|
+
height,
|
|
143
|
+
}
|
|
144
|
+
}
|