@fengwk/kchart 0.0.0-stage → 0.1.1
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,4046 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { DIRECTIONS, FONT as FONT$1, fallbackMeasure, maxWidthOf, warning, wrapText } from "./support.js";
|
|
3
|
+
import { placeGraphLabels, repairRoutes } from "./graph-route.js";
|
|
4
|
+
import { flowOutlineName, outlinePort, shapeGlyphBox, shapeOutline, shapeSize } from "./shapes.js";
|
|
5
|
+
// src/diagram/graph-layout.js
|
|
6
|
+
var graph_layout_exports = /* @__PURE__ */ __exportAll({ layoutGraph: () => layoutGraph });
|
|
7
|
+
var FONT = {
|
|
8
|
+
...FONT$1,
|
|
9
|
+
frame: {
|
|
10
|
+
size: 13,
|
|
11
|
+
weight: 600
|
|
12
|
+
},
|
|
13
|
+
erHead: {
|
|
14
|
+
size: 14,
|
|
15
|
+
weight: 600
|
|
16
|
+
},
|
|
17
|
+
erAttr: {
|
|
18
|
+
size: 13,
|
|
19
|
+
weight: 400
|
|
20
|
+
},
|
|
21
|
+
erKey: {
|
|
22
|
+
size: 12,
|
|
23
|
+
weight: 400
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var FRAME_PAD = 14;
|
|
27
|
+
var MARGIN = 16;
|
|
28
|
+
var markOf = (cls) => cls === "new" ? "+ " : cls === "info" ? "~ " : cls === "ok" ? "= " : "";
|
|
29
|
+
function labelBlock(item, rect) {
|
|
30
|
+
const titleH = item.titleLines.length * 19;
|
|
31
|
+
const descH = item.descLines.length * 17;
|
|
32
|
+
const gap = item.descLines.length ? 7 : 0;
|
|
33
|
+
const top = rect.y + Math.max(10, (rect.h - titleH - gap - descH) / 2);
|
|
34
|
+
return {
|
|
35
|
+
titleY: top + titleH / 2,
|
|
36
|
+
descY: item.descLines.length ? top + titleH + gap + descH / 2 : null
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
var hgap = (a, b) => {
|
|
40
|
+
if (a.kind === "real" && b.kind === "real") return 28;
|
|
41
|
+
if (a.kind === "label" || b.kind === "label") return 18;
|
|
42
|
+
return 20;
|
|
43
|
+
};
|
|
44
|
+
function collapseFrames(model) {
|
|
45
|
+
const frames = new Map(model.subgraphs.map((frame) => [frame.id, frame]));
|
|
46
|
+
if (![...frames.values()].some((frame) => frame.view === "collapsed")) return model;
|
|
47
|
+
const byId = new Map([...model.nodes, ...model.subgraphs].map((item) => [item.id, item]));
|
|
48
|
+
const redirects = /* @__PURE__ */ new Map();
|
|
49
|
+
for (const item of byId.values()) {
|
|
50
|
+
let collapsed = item.view === "collapsed" ? item.id : null;
|
|
51
|
+
for (let parent = item.parent; parent; parent = frames.get(parent)?.parent) if (frames.get(parent)?.view === "collapsed") collapsed = parent;
|
|
52
|
+
if (collapsed != null) redirects.set(item.id, collapsed);
|
|
53
|
+
}
|
|
54
|
+
const nodes = model.nodes.filter((node) => !redirects.has(node.id));
|
|
55
|
+
for (const frame of frames.values()) if (redirects.get(frame.id) === frame.id) nodes.push({
|
|
56
|
+
...frame,
|
|
57
|
+
label: frame.title,
|
|
58
|
+
shape: "rounded",
|
|
59
|
+
kind: "collapsed"
|
|
60
|
+
});
|
|
61
|
+
const subgraphs = model.subgraphs.filter((frame) => !redirects.has(frame.id));
|
|
62
|
+
const edges = model.edges.flatMap((edge) => {
|
|
63
|
+
const from = redirects.get(edge.from) ?? edge.from, to = redirects.get(edge.to) ?? edge.to;
|
|
64
|
+
if (from === to && redirects.has(from)) return [];
|
|
65
|
+
return [{
|
|
66
|
+
...edge,
|
|
67
|
+
from,
|
|
68
|
+
to,
|
|
69
|
+
fromFrame: edge.fromFrame && !redirects.has(from),
|
|
70
|
+
toFrame: edge.toFrame && !redirects.has(to)
|
|
71
|
+
}];
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
...model,
|
|
75
|
+
nodes,
|
|
76
|
+
subgraphs,
|
|
77
|
+
edges
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function crossingCount(geometry) {
|
|
81
|
+
const side = (p, q, r) => (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
|
|
82
|
+
const runs = geometry.edges.filter((edge) => edge.style !== "invisible").map((edge) => edge.segments);
|
|
83
|
+
let count = 0;
|
|
84
|
+
for (let i = 0; i < runs.length; i += 1) for (let j = i + 1; j < runs.length; j += 1) for (const a of runs[i]) for (const b of runs[j]) if (side(a.p0, a.p1, b.p0) * side(a.p0, a.p1, b.p1) < -1e-6 && side(b.p0, b.p1, a.p0) * side(b.p0, b.p1, a.p1) < -1e-6) count += 1;
|
|
85
|
+
return count;
|
|
86
|
+
}
|
|
87
|
+
function outsideKeys(geometry, members, ids) {
|
|
88
|
+
const lr = geometry.direction === "LR" || geometry.direction === "RL";
|
|
89
|
+
const centers = new Map([...geometry.nodes, ...geometry.frames].map((item) => [item.id, lr ? item.y + item.h / 2 : item.x + item.w / 2]));
|
|
90
|
+
return new Map(ids.map((id) => {
|
|
91
|
+
const outside = geometry.model.edges.flatMap((edge) => edge.from === id && !members.has(edge.to) ? [edge.to] : edge.to === id && !members.has(edge.from) ? [edge.from] : []).map((other) => centers.get(other)).filter(Number.isFinite);
|
|
92
|
+
return [id, outside.length ? outside.reduce((sum, value) => sum + value, 0) / outside.length : centers.get(id)];
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
var ORDER_SEARCH_NODES = 60;
|
|
96
|
+
var ORDER_SEARCH_TRIES = 12;
|
|
97
|
+
function orderBlocks(geometry, settle) {
|
|
98
|
+
if (!geometry.blocks?.length || geometry.nodes.length > ORDER_SEARCH_NODES) return geometry;
|
|
99
|
+
const seqOf = new Map(geometry.model.nodes.map((node) => [node.id, node.seq]));
|
|
100
|
+
let best = geometry, score = crossingCount(geometry), hints = /* @__PURE__ */ new Map(), tries = 0;
|
|
101
|
+
for (const block of geometry.blocks) {
|
|
102
|
+
if (!score) break;
|
|
103
|
+
const ids = block.members.filter((id) => seqOf.has(id));
|
|
104
|
+
if (ids.length < 2) continue;
|
|
105
|
+
const keys = outsideKeys(best, new Set(block.members), ids);
|
|
106
|
+
const seqs = ids.map((id) => seqOf.get(id)).sort((a, b) => a - b);
|
|
107
|
+
const tried = /* @__PURE__ */ new Set([ids.slice().sort((a, b) => (hints.get(a) ?? seqOf.get(a)) - (hints.get(b) ?? seqOf.get(b))).join("\n")]);
|
|
108
|
+
for (const tie of [1, -1]) {
|
|
109
|
+
const order = ids.slice().sort((a, b) => keys.get(a) - keys.get(b) || tie * (seqOf.get(a) - seqOf.get(b)));
|
|
110
|
+
if (tried.has(order.join("\n")) || tries >= ORDER_SEARCH_TRIES) continue;
|
|
111
|
+
tried.add(order.join("\n"));
|
|
112
|
+
tries += 1;
|
|
113
|
+
const next = new Map(hints);
|
|
114
|
+
order.forEach((id, index) => next.set(id, seqs[index]));
|
|
115
|
+
const candidate = settle(next);
|
|
116
|
+
if (!candidate.blocks) continue;
|
|
117
|
+
const count = crossingCount(candidate);
|
|
118
|
+
if (count < score) ({best, score, hints} = {
|
|
119
|
+
best: candidate,
|
|
120
|
+
score: count,
|
|
121
|
+
hints: next
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return best;
|
|
126
|
+
}
|
|
127
|
+
function layoutGraph(source, options = {}, info = {}) {
|
|
128
|
+
const settle = (hints) => {
|
|
129
|
+
const arranged = arrangeGraph(source, options, info, true, hints);
|
|
130
|
+
repairRoutes(arranged);
|
|
131
|
+
return arranged;
|
|
132
|
+
};
|
|
133
|
+
const geometry = source.type === "er" ? arrangeGraph(source, options, info) : orderBlocks(settle(null), settle);
|
|
134
|
+
if (source.type !== "er") placeGraphLabels(geometry, options.measure ?? fallbackMeasure);
|
|
135
|
+
delete geometry.blocks;
|
|
136
|
+
if (source.type !== "er" && (source.nodes.length > 60 || geometry.width > (options.width ?? Infinity) * 2)) geometry.errors = [warning("LARGE_DIAGRAM", options.lang ?? info.lang, info.headerLine ?? 1, "图较大,可在框内滚动", "Large diagram; scroll within the frame", "考虑拆成多张图", "Consider splitting the diagram")];
|
|
137
|
+
return geometry;
|
|
138
|
+
}
|
|
139
|
+
function arrangeGraph(source, options, info, allowCompact = true, hints = null) {
|
|
140
|
+
const model = collapseFrames(source);
|
|
141
|
+
const frames = new Map(model.subgraphs.map((frame) => [frame.id, frame]));
|
|
142
|
+
const childRegions = new Set(model.subgraphs.filter((frame) => frame.kind === "divider").map((frame) => frame.parent));
|
|
143
|
+
const hidden = /* @__PURE__ */ new Set(), localEdges = /* @__PURE__ */ new Set(), blocks = /* @__PURE__ */ new Map();
|
|
144
|
+
const nodes = [...model.nodes], edges = [...model.edges];
|
|
145
|
+
const measure = options.measure ?? fallbackMeasure;
|
|
146
|
+
const isMember = (item, frame) => {
|
|
147
|
+
for (let parent = item.parent; parent; parent = frames.get(parent)?.parent) if (parent === frame.id) return true;
|
|
148
|
+
return false;
|
|
149
|
+
};
|
|
150
|
+
const all = [...nodes, ...model.subgraphs];
|
|
151
|
+
const occupied = new Set(all.map((item) => item.parent));
|
|
152
|
+
const compact = allowCompact && model.type === "flowchart" && nodes.length > 0 && nodes.every((item) => item.parent != null);
|
|
153
|
+
const flowConfig = Object.entries(info.config ?? {}).find(([key]) => key.toLowerCase() === "flowchart")?.[1];
|
|
154
|
+
const inheritDir = Object.entries(flowConfig ?? {}).some(([key, value]) => key.toLowerCase() === "inheritdir" && value === true);
|
|
155
|
+
const projected = /* @__PURE__ */ new Map();
|
|
156
|
+
for (const frame of [...frames.values()].sort((a, b) => a.depth - b.depth || a.seq - b.seq)) {
|
|
157
|
+
const wholeBlock = compact && !frame.parent;
|
|
158
|
+
if (hidden.has(frame.id) || occupied.has(frame.id) && !(wholeBlock || frame.dir || inheritDir || frame.kind === "composite" || frame.kind === "divider" || childRegions.has(frame.id))) continue;
|
|
159
|
+
const members = new Set(occupied.has(frame.id) ? all.filter((item) => isMember(item, frame)).map((item) => item.id) : []);
|
|
160
|
+
const external = edges.some((edge) => members.has(edge.from) !== members.has(edge.to));
|
|
161
|
+
if (external && !wholeBlock) continue;
|
|
162
|
+
for (const id of members) projected.set(id, frame.id);
|
|
163
|
+
const topLevel = (item) => item.parent === frame.id ? {
|
|
164
|
+
...item,
|
|
165
|
+
parent: null
|
|
166
|
+
} : item;
|
|
167
|
+
const hinted = (item) => hints?.has(item.id) ? {
|
|
168
|
+
...item,
|
|
169
|
+
orderSeq: hints.get(item.id)
|
|
170
|
+
} : item;
|
|
171
|
+
const inside = edges.filter((edge) => members.has(edge.from) && members.has(edge.to));
|
|
172
|
+
const innerDirection = !external && frame.dir ? frame.dir : model.type === "flowchart" && (inheritDir || external) ? model.direction : "TB";
|
|
173
|
+
const inner = arrangeGraph({
|
|
174
|
+
...model,
|
|
175
|
+
direction: innerDirection,
|
|
176
|
+
nodes: nodes.filter((item) => members.has(item.id)).map(topLevel).map(hinted),
|
|
177
|
+
subgraphs: model.subgraphs.filter((item) => members.has(item.id)).map(topLevel),
|
|
178
|
+
edges: inside
|
|
179
|
+
}, options, info, false);
|
|
180
|
+
const lines = frame.title ? wrapText(frame.title, 200, FONT.frame, measure) : frame.icon ? [""] : [];
|
|
181
|
+
const descLines = (frame.desc ?? []).flatMap((line) => wrapText(line, 180, FONT.body, measure));
|
|
182
|
+
const titleW = Math.max(maxWidthOf(lines, FONT.frame, measure) + (frame.icon ? 22 : 0), maxWidthOf(descLines, FONT.body, measure));
|
|
183
|
+
const titleH = lines.length * 17 + (descLines.length ? 4 + descLines.length * 16 : 0);
|
|
184
|
+
const titleBand = titleH ? titleH + 8 : 0;
|
|
185
|
+
const w = Math.max(inner.width, titleW + 32), h = inner.height + titleBand;
|
|
186
|
+
blocks.set(frame.id, {
|
|
187
|
+
frame,
|
|
188
|
+
inner,
|
|
189
|
+
lines,
|
|
190
|
+
descLines,
|
|
191
|
+
titleW,
|
|
192
|
+
titleH,
|
|
193
|
+
titleBand,
|
|
194
|
+
w,
|
|
195
|
+
h,
|
|
196
|
+
members,
|
|
197
|
+
whole: wholeBlock
|
|
198
|
+
});
|
|
199
|
+
for (const id of members) hidden.add(id);
|
|
200
|
+
for (const edge of inside) localEdges.add(edge);
|
|
201
|
+
}
|
|
202
|
+
if (!blocks.size) return layoutCore(model, options);
|
|
203
|
+
const proxy = (id) => blocks.has(id);
|
|
204
|
+
const redirects = /* @__PURE__ */ new Map();
|
|
205
|
+
const outerEdges = edges.filter((edge) => !localEdges.has(edge)).map((edge) => {
|
|
206
|
+
const from = projected.get(edge.from) ?? edge.from, to = projected.get(edge.to) ?? edge.to;
|
|
207
|
+
if (from !== edge.from || to !== edge.to) redirects.set(edge.seq, edge);
|
|
208
|
+
return {
|
|
209
|
+
...edge,
|
|
210
|
+
from,
|
|
211
|
+
to,
|
|
212
|
+
fromFrame: edge.fromFrame && !proxy(from),
|
|
213
|
+
toFrame: edge.toFrame && !proxy(to)
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
const visible = nodes.filter((item) => !hidden.has(item.id));
|
|
217
|
+
const proxies = [...blocks.values()].map(({ frame, w, h }) => ({
|
|
218
|
+
...frame,
|
|
219
|
+
label: "",
|
|
220
|
+
block: {
|
|
221
|
+
w,
|
|
222
|
+
h
|
|
223
|
+
},
|
|
224
|
+
shape: "rect"
|
|
225
|
+
}));
|
|
226
|
+
const strip = !visible.length && !outerEdges.length && proxies.every((p) => p.kind === "divider" && !p.parent);
|
|
227
|
+
const horizontal = model.direction === "TB" || model.direction === "BT";
|
|
228
|
+
let at = MARGIN;
|
|
229
|
+
const compartments = strip ? proxies.sort((a, b) => a.seq - b.seq).map(({ id, block: { w, h } }) => {
|
|
230
|
+
const item = {
|
|
231
|
+
id,
|
|
232
|
+
x: horizontal ? at : MARGIN,
|
|
233
|
+
y: horizontal ? MARGIN : at,
|
|
234
|
+
w,
|
|
235
|
+
h
|
|
236
|
+
};
|
|
237
|
+
at += (horizontal ? w : h) + MARGIN;
|
|
238
|
+
return item;
|
|
239
|
+
}) : [];
|
|
240
|
+
const outer = strip ? {
|
|
241
|
+
type: model.type,
|
|
242
|
+
direction: model.direction,
|
|
243
|
+
edges: [],
|
|
244
|
+
frames: [],
|
|
245
|
+
labels: [],
|
|
246
|
+
ranks: [],
|
|
247
|
+
nodes: compartments,
|
|
248
|
+
width: horizontal ? at : Math.max(...compartments.map((p) => p.w)) + 32,
|
|
249
|
+
height: horizontal ? Math.max(...compartments.map((p) => p.h)) + 32 : at
|
|
250
|
+
} : layoutCore({
|
|
251
|
+
...model,
|
|
252
|
+
nodes: [...visible, ...proxies],
|
|
253
|
+
subgraphs: model.subgraphs.filter((frame) => !hidden.has(frame.id) && !proxy(frame.id)),
|
|
254
|
+
edges: outerEdges
|
|
255
|
+
}, options);
|
|
256
|
+
const retained = [];
|
|
257
|
+
for (const node of outer.nodes) {
|
|
258
|
+
const block = blocks.get(node.id);
|
|
259
|
+
if (!block) {
|
|
260
|
+
retained.push(node);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const { frame, inner, lines, descLines, titleW, titleH, titleBand, w, h } = block;
|
|
264
|
+
const dx = node.x + (w - inner.width) / 2, dy = node.y + titleBand;
|
|
265
|
+
const move = (p) => p && {
|
|
266
|
+
...p,
|
|
267
|
+
x: p.x + dx,
|
|
268
|
+
y: p.y + dy
|
|
269
|
+
};
|
|
270
|
+
const attach = (item) => item.parent == null ? frame.id : item.parent;
|
|
271
|
+
outer.frames.push({
|
|
272
|
+
...frame,
|
|
273
|
+
x: node.x,
|
|
274
|
+
y: node.y,
|
|
275
|
+
w,
|
|
276
|
+
h,
|
|
277
|
+
lines,
|
|
278
|
+
descLines,
|
|
279
|
+
titleRect: {
|
|
280
|
+
x: node.x + 16,
|
|
281
|
+
y: node.y + 10,
|
|
282
|
+
w: titleW,
|
|
283
|
+
h: titleH
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
outer.frames.push(...inner.frames.map((item) => ({
|
|
287
|
+
...move(item),
|
|
288
|
+
parent: attach(item),
|
|
289
|
+
titleRect: move(item.titleRect)
|
|
290
|
+
})));
|
|
291
|
+
retained.push(...inner.nodes.map((item) => ({
|
|
292
|
+
...move(item),
|
|
293
|
+
parent: attach(item),
|
|
294
|
+
titleY: item.titleY + dy,
|
|
295
|
+
descY: item.descY == null ? null : item.descY + dy
|
|
296
|
+
})));
|
|
297
|
+
outer.labels.push(...inner.labels.map(move));
|
|
298
|
+
outer.edges.push(...inner.edges.map((edge) => ({
|
|
299
|
+
...edge,
|
|
300
|
+
start: move(edge.start),
|
|
301
|
+
end: move(edge.end),
|
|
302
|
+
labelRect: move(edge.labelRect),
|
|
303
|
+
segments: edge.segments.map(({ p0, c1, c2, p1 }) => ({
|
|
304
|
+
p0: move(p0),
|
|
305
|
+
c1: move(c1),
|
|
306
|
+
c2: move(c2),
|
|
307
|
+
p1: move(p1)
|
|
308
|
+
}))
|
|
309
|
+
})));
|
|
310
|
+
}
|
|
311
|
+
for (const edge of outer.edges) {
|
|
312
|
+
if (proxy(edge.from)) edge.fromFrame = true;
|
|
313
|
+
if (proxy(edge.to)) edge.toFrame = true;
|
|
314
|
+
}
|
|
315
|
+
const result = {
|
|
316
|
+
...outer,
|
|
317
|
+
nodes: retained,
|
|
318
|
+
model
|
|
319
|
+
};
|
|
320
|
+
if (redirects.size) {
|
|
321
|
+
repairRoutes(result, redirects);
|
|
322
|
+
if ([...redirects].some(([seq, original]) => !result.edges.some((edge) => edge.id === seq && edge.from === original.from && edge.to === original.to))) return arrangeGraph(source, options, info, false);
|
|
323
|
+
}
|
|
324
|
+
if (compact) result.blocks = [...blocks.values()].filter((block) => block.whole).map(({ frame, members }) => ({
|
|
325
|
+
id: frame.id,
|
|
326
|
+
members: [...members]
|
|
327
|
+
}));
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
function layoutCore(model, { measure = fallbackMeasure, lang, ctx, renderOptions } = {}) {
|
|
331
|
+
const direction = DIRECTIONS.includes(model.direction) ? model.direction : "TB";
|
|
332
|
+
const isLR = direction === "LR" || direction === "RL";
|
|
333
|
+
const clusters = /* @__PURE__ */ new Map();
|
|
334
|
+
const root = {
|
|
335
|
+
id: null,
|
|
336
|
+
model: null,
|
|
337
|
+
parent: null,
|
|
338
|
+
children: [],
|
|
339
|
+
seq: -1,
|
|
340
|
+
depth: 0,
|
|
341
|
+
title: "",
|
|
342
|
+
titleLines: [],
|
|
343
|
+
descLines: [],
|
|
344
|
+
titleW: 0,
|
|
345
|
+
titleBand: 0
|
|
346
|
+
};
|
|
347
|
+
const realItems = [];
|
|
348
|
+
const realById = /* @__PURE__ */ new Map();
|
|
349
|
+
const selfEdges = /* @__PURE__ */ new Map();
|
|
350
|
+
const warnings = [];
|
|
351
|
+
for (const edge of model.edges) if (edge.from === edge.to && !edge.fromFrame && !edge.toFrame) {
|
|
352
|
+
if (!selfEdges.has(edge.from)) selfEdges.set(edge.from, []);
|
|
353
|
+
selfEdges.get(edge.from).push(edge);
|
|
354
|
+
}
|
|
355
|
+
const measureNode = (node) => {
|
|
356
|
+
const flowShape = model.type === "flowchart" && flowOutlineName(node.shape);
|
|
357
|
+
const glyph = flowShape && shapeGlyphBox(flowShape, 1, 1, direction);
|
|
358
|
+
const titleText = !glyph || node.explicitLabel || node.label !== node.id ? (model.mermaid ? "" : markOf(node.class)) + (node.label ?? node.id) : "";
|
|
359
|
+
const titleLines = !titleText ? [] : wrapText(titleText, node.kind === "er" ? 180 : 200, FONT.title, measure);
|
|
360
|
+
const descLines = [];
|
|
361
|
+
for (const desc of node.desc || []) descLines.push(...wrapText(desc, 200, FONT.body, measure));
|
|
362
|
+
let tw;
|
|
363
|
+
let th;
|
|
364
|
+
let er = null;
|
|
365
|
+
if (node.kind === "start") {
|
|
366
|
+
tw = 14;
|
|
367
|
+
th = 14;
|
|
368
|
+
} else if (node.kind === "end") {
|
|
369
|
+
tw = 18;
|
|
370
|
+
th = 18;
|
|
371
|
+
} else if (node.kind === "choice") {
|
|
372
|
+
tw = 34;
|
|
373
|
+
th = 34;
|
|
374
|
+
} else if (node.kind === "fork" || node.kind === "join") {
|
|
375
|
+
tw = isLR ? 10 : 70;
|
|
376
|
+
th = isLR ? 70 : 10;
|
|
377
|
+
} else if (node.kind === "er") {
|
|
378
|
+
const pad = 12;
|
|
379
|
+
const headLines = titleLines;
|
|
380
|
+
const headW = maxWidthOf(headLines, FONT.erHead, measure);
|
|
381
|
+
const typeW = Math.max(36, ...node.fields.map((f) => maxWidthOf(wrapText(f.type, 80, FONT.erAttr, measure), FONT.erAttr, measure)));
|
|
382
|
+
const keyW = Math.max(24, ...node.fields.map((f) => maxWidthOf([f.keys.join(" ")], FONT.erKey, measure)));
|
|
383
|
+
const nameMax = Math.max(60, 216 - typeW - 24 - keyW);
|
|
384
|
+
const rows = node.fields.map((f) => {
|
|
385
|
+
const typeLines = wrapText(f.type, 80, FONT.erAttr, measure);
|
|
386
|
+
const nameLines = wrapText(f.name, nameMax, FONT.erAttr, measure);
|
|
387
|
+
const keyText = f.keys.join(" ");
|
|
388
|
+
const rowH = Math.max(typeLines.length, nameLines.length) * 17 + 8;
|
|
389
|
+
return {
|
|
390
|
+
...f,
|
|
391
|
+
typeLines,
|
|
392
|
+
nameLines,
|
|
393
|
+
keyText,
|
|
394
|
+
rowH
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
const nameW = Math.max(60, ...rows.map((r) => maxWidthOf(r.nameLines, FONT.erAttr, measure)));
|
|
398
|
+
tw = Math.max(132, 24 + Math.max(headW, typeW + 12 + nameW + 12 + keyW));
|
|
399
|
+
const headerH = headLines.length * 19 + 14;
|
|
400
|
+
th = headerH + rows.reduce((sum, row) => sum + row.rowH, 0) + 6;
|
|
401
|
+
er = {
|
|
402
|
+
headLines,
|
|
403
|
+
typeW,
|
|
404
|
+
nameW,
|
|
405
|
+
keyW,
|
|
406
|
+
headerH,
|
|
407
|
+
rows,
|
|
408
|
+
pad
|
|
409
|
+
};
|
|
410
|
+
} else {
|
|
411
|
+
const titleW = maxWidthOf(titleLines, FONT.title, measure);
|
|
412
|
+
const descW = maxWidthOf(descLines, FONT.body, measure);
|
|
413
|
+
tw = Math.max(64, Math.max(titleW, descW) + 28);
|
|
414
|
+
th = 20 + titleLines.length * 19 + (descLines.length ? 5 + descLines.length * 17 : 0);
|
|
415
|
+
th = Math.max(40, th) + (node.kind === "collapsed" ? 20 : 0) + (node.icon ? (node.iconHeight ?? 16) + 8 : 0);
|
|
416
|
+
if (node.icon && node.iconHeight) tw = Math.max(tw, node.iconHeight + 28);
|
|
417
|
+
if (flowShape) {
|
|
418
|
+
[tw, th] = shapeSize(flowShape, tw, th, direction, titleLines.length > 0);
|
|
419
|
+
if (glyph && titleLines.length) th = Math.max(th, titleLines.length * 19 + 12);
|
|
420
|
+
} else if (node.shape === "diamond") {
|
|
421
|
+
tw *= 1.7;
|
|
422
|
+
th *= 1.7;
|
|
423
|
+
} else if (node.shape === "circle") {
|
|
424
|
+
const d = Math.max(48, Math.max(tw, th) * 1.3);
|
|
425
|
+
tw = d;
|
|
426
|
+
th = d;
|
|
427
|
+
} else if (node.shape === "hexagon") tw += 34;
|
|
428
|
+
else if (node.shape === "cylinder") th += 12;
|
|
429
|
+
}
|
|
430
|
+
if (node.block) ({w: tw, h: th} = node.block);
|
|
431
|
+
const loops = selfEdges.get(node.id) || [];
|
|
432
|
+
let loopExtra = 0;
|
|
433
|
+
let loopLabels = [];
|
|
434
|
+
if (loops.length) {
|
|
435
|
+
const labelWidths = loops.map((edge) => edge.label ? maxWidthOf(wrapText(edge.label, 120, FONT.label, measure), FONT.label, measure) : 0);
|
|
436
|
+
loopExtra = 26 + Math.max(0, ...labelWidths) + Math.max(0, loops.length - 1) * 12;
|
|
437
|
+
loopLabels = loops.map((edge, index) => ({
|
|
438
|
+
edge,
|
|
439
|
+
width: labelWidths[index],
|
|
440
|
+
lines: edge.label ? wrapText(edge.label, 120, FONT.label, measure) : [],
|
|
441
|
+
index
|
|
442
|
+
}));
|
|
443
|
+
}
|
|
444
|
+
const cross = isLR ? th : tw;
|
|
445
|
+
const flow = isLR ? tw : th;
|
|
446
|
+
return {
|
|
447
|
+
kind: "real",
|
|
448
|
+
node,
|
|
449
|
+
id: node.id,
|
|
450
|
+
seq: node.orderSeq ?? node.seq,
|
|
451
|
+
tw,
|
|
452
|
+
th,
|
|
453
|
+
bw: cross + loopExtra,
|
|
454
|
+
bh: flow,
|
|
455
|
+
offsetCross: loopExtra ? -loopExtra / 2 : 0,
|
|
456
|
+
titleLines,
|
|
457
|
+
descLines,
|
|
458
|
+
er,
|
|
459
|
+
loopExtra,
|
|
460
|
+
loopLabels,
|
|
461
|
+
selfLoops: loops,
|
|
462
|
+
parent: node.parent || null,
|
|
463
|
+
cluster: null,
|
|
464
|
+
up: [],
|
|
465
|
+
down: [],
|
|
466
|
+
rank: 0
|
|
467
|
+
};
|
|
468
|
+
};
|
|
469
|
+
for (const node of model.nodes) {
|
|
470
|
+
const item = measureNode(node);
|
|
471
|
+
realItems.push(item);
|
|
472
|
+
realById.set(node.id, item);
|
|
473
|
+
}
|
|
474
|
+
const nodeById = new Map(model.nodes.map((node) => [node.id, node]));
|
|
475
|
+
if (!realItems.length) return {
|
|
476
|
+
type: model.type,
|
|
477
|
+
direction,
|
|
478
|
+
width: 32,
|
|
479
|
+
height: 32,
|
|
480
|
+
nodes: [],
|
|
481
|
+
labels: [],
|
|
482
|
+
frames: [],
|
|
483
|
+
edges: [],
|
|
484
|
+
ranks: [],
|
|
485
|
+
warnings: ["图里没有节点"],
|
|
486
|
+
model
|
|
487
|
+
};
|
|
488
|
+
const layoutClusterTitle = (cluster, maxWidth) => {
|
|
489
|
+
cluster.titleLines = wrapText(cluster.title, maxWidth, FONT.frame, measure);
|
|
490
|
+
if (!cluster.titleLines.length && cluster.model.icon) cluster.titleLines = [""];
|
|
491
|
+
cluster.titleW = maxWidthOf(cluster.titleLines, FONT.frame, measure) + (cluster.model.icon ? 22 : 0);
|
|
492
|
+
cluster.titleBand = 10 + cluster.titleLines.length * 17 + (cluster.descLines.length ? 4 + cluster.descLines.length * 16 : 0) + 8;
|
|
493
|
+
};
|
|
494
|
+
for (const sg of model.subgraphs) {
|
|
495
|
+
const cluster = {
|
|
496
|
+
id: sg.id,
|
|
497
|
+
model: sg,
|
|
498
|
+
parent: null,
|
|
499
|
+
children: [],
|
|
500
|
+
seq: sg.seq,
|
|
501
|
+
depth: sg.depth,
|
|
502
|
+
title: sg.title ?? sg.id,
|
|
503
|
+
titleLines: [],
|
|
504
|
+
descLines: [],
|
|
505
|
+
class: sg.class || null
|
|
506
|
+
};
|
|
507
|
+
for (const desc of sg.desc || []) cluster.descLines.push(...wrapText(desc, 180, FONT.body, measure));
|
|
508
|
+
cluster.descW = maxWidthOf(cluster.descLines, FONT.body, measure);
|
|
509
|
+
layoutClusterTitle(cluster, 200);
|
|
510
|
+
clusters.set(cluster.id, cluster);
|
|
511
|
+
}
|
|
512
|
+
for (const cluster of clusters.values()) {
|
|
513
|
+
cluster.parent = (cluster.model.parent ? clusters.get(cluster.model.parent) : root) || root;
|
|
514
|
+
cluster.parent.children.push(cluster);
|
|
515
|
+
}
|
|
516
|
+
const sortChildren = (item) => {
|
|
517
|
+
item.children.sort((a, b) => a.seq - b.seq);
|
|
518
|
+
for (const child of item.children) sortChildren(child);
|
|
519
|
+
};
|
|
520
|
+
sortChildren(root);
|
|
521
|
+
for (const item of realItems) item.cluster = item.parent ? clusters.get(item.parent) || root : root;
|
|
522
|
+
const isDescendantOf = (cluster, ancestor) => {
|
|
523
|
+
for (let current = cluster; current; current = current.parent) if (current === ancestor) return true;
|
|
524
|
+
return false;
|
|
525
|
+
};
|
|
526
|
+
const membersOf = (frame) => realItems.filter((item) => isDescendantOf(item.cluster, frame));
|
|
527
|
+
const isFrameId = (id) => clusters.has(id) && !nodeById.has(id);
|
|
528
|
+
const clusterChain = (cluster) => {
|
|
529
|
+
const chain = [];
|
|
530
|
+
for (let current = cluster; current; current = current.parent) chain.push(current);
|
|
531
|
+
return chain;
|
|
532
|
+
};
|
|
533
|
+
const frameEntry = (frame) => {
|
|
534
|
+
const members = membersOf(frame);
|
|
535
|
+
if (!members.length) return null;
|
|
536
|
+
const start = members.find((item) => item.node.kind === "start" && item.parent === frame.id);
|
|
537
|
+
if (start) return start;
|
|
538
|
+
const memberIds = new Set(members.map((item) => item.id));
|
|
539
|
+
const hasInternalIn = (id) => model.edges.some((edge) => edge.to === id && memberIds.has(edge.from) && !edge.toFrame && !edge.fromFrame);
|
|
540
|
+
return members.find((item) => !hasInternalIn(item.id)) || members[0];
|
|
541
|
+
};
|
|
542
|
+
const frameExit = (frame) => {
|
|
543
|
+
const members = membersOf(frame);
|
|
544
|
+
if (!members.length) return null;
|
|
545
|
+
const end = members.find((item) => item.node.kind === "end" && item.parent === frame.id);
|
|
546
|
+
if (end) return end;
|
|
547
|
+
const memberIds = new Set(members.map((item) => item.id));
|
|
548
|
+
const hasInternalOut = (id) => model.edges.some((edge) => edge.from === id && memberIds.has(edge.to) && !edge.toFrame && !edge.fromFrame);
|
|
549
|
+
const reversed = [...members].reverse();
|
|
550
|
+
return reversed.find((item) => !hasInternalOut(item.id)) || reversed[0];
|
|
551
|
+
};
|
|
552
|
+
const layoutEdges = [];
|
|
553
|
+
for (const edge of model.edges) {
|
|
554
|
+
const fromFrame = isFrameId(edge.from);
|
|
555
|
+
const toFrame = isFrameId(edge.to);
|
|
556
|
+
edge.fromFrame = fromFrame;
|
|
557
|
+
edge.toFrame = toFrame;
|
|
558
|
+
if (edge.from === edge.to && fromFrame && toFrame) {
|
|
559
|
+
edge.skip = true;
|
|
560
|
+
warnings.push(`自环子图 ${edge.from} 暂不绘制`);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
if (edge.from === edge.to && !fromFrame && !toFrame) continue;
|
|
564
|
+
const fromItem = fromFrame ? frameExit(clusters.get(edge.from)) : realById.get(edge.from);
|
|
565
|
+
const toItem = toFrame ? frameEntry(clusters.get(edge.to)) : realById.get(edge.to);
|
|
566
|
+
if (!fromItem || !toItem) {
|
|
567
|
+
edge.skip = true;
|
|
568
|
+
if (fromFrame && !membersOf(clusters.get(edge.from)).length) warnings.push(`空子图 ${edge.from} 的连线已跳过`);
|
|
569
|
+
if (toFrame && !membersOf(clusters.get(edge.to)).length) warnings.push(`空子图 ${edge.to} 的连线已跳过`);
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
if (fromItem === toItem) {
|
|
573
|
+
edge.skip = true;
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
const fromChain = fromFrame ? clusterChain(clusters.get(edge.from).parent) : clusterChain(realById.get(edge.from).cluster);
|
|
577
|
+
const toChain = toFrame ? clusterChain(clusters.get(edge.to).parent) : clusterChain(realById.get(edge.to).cluster);
|
|
578
|
+
const ownerSet = new Set(toChain);
|
|
579
|
+
const owner = fromChain.find((cluster) => ownerSet.has(cluster)) || root;
|
|
580
|
+
layoutEdges.push({
|
|
581
|
+
edge,
|
|
582
|
+
fromItem,
|
|
583
|
+
toItem,
|
|
584
|
+
fromFrame,
|
|
585
|
+
toFrame,
|
|
586
|
+
owner,
|
|
587
|
+
minlen: 1 + (edge.extra || 0),
|
|
588
|
+
reversed: false,
|
|
589
|
+
chain: null,
|
|
590
|
+
start: null,
|
|
591
|
+
end: null,
|
|
592
|
+
segments: []
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
const allItems = [...realItems];
|
|
596
|
+
for (const frame of clusters.values()) if (!realItems.some((item) => isDescendantOf(item.cluster, frame))) warnings.push(`空子图 ${frame.id} 未绘制`);
|
|
597
|
+
const out = new Map(realItems.map((item) => [item, []]));
|
|
598
|
+
const inn = new Map(realItems.map((item) => [item, []]));
|
|
599
|
+
for (const le of layoutEdges) {
|
|
600
|
+
out.get(le.fromItem).push(le);
|
|
601
|
+
inn.get(le.toItem).push(le);
|
|
602
|
+
}
|
|
603
|
+
const color = /* @__PURE__ */ new Map();
|
|
604
|
+
const visit = (item) => {
|
|
605
|
+
color.set(item, 1);
|
|
606
|
+
for (const le of out.get(item)) {
|
|
607
|
+
const target = le.toItem;
|
|
608
|
+
const state = color.get(target) || 0;
|
|
609
|
+
if (state === 1) le.reversed = true;
|
|
610
|
+
else if (state === 0) visit(target);
|
|
611
|
+
}
|
|
612
|
+
color.set(item, 2);
|
|
613
|
+
};
|
|
614
|
+
for (const item of realItems) if (!color.get(item)) visit(item);
|
|
615
|
+
for (const le of layoutEdges) if (le.reversed) {
|
|
616
|
+
const swap = le.fromItem;
|
|
617
|
+
le.fromItem = le.toItem;
|
|
618
|
+
le.toItem = swap;
|
|
619
|
+
}
|
|
620
|
+
for (const item of realItems) {
|
|
621
|
+
out.set(item, []);
|
|
622
|
+
inn.set(item, []);
|
|
623
|
+
}
|
|
624
|
+
for (const le of layoutEdges) {
|
|
625
|
+
out.get(le.fromItem).push(le);
|
|
626
|
+
inn.get(le.toItem).push(le);
|
|
627
|
+
}
|
|
628
|
+
const indeg = new Map(realItems.map((item) => [item, 0]));
|
|
629
|
+
for (const le of layoutEdges) indeg.set(le.toItem, indeg.get(le.toItem) + 1);
|
|
630
|
+
const queue = realItems.filter((item) => indeg.get(item) === 0);
|
|
631
|
+
const topo = [];
|
|
632
|
+
while (queue.length) {
|
|
633
|
+
const item = queue.shift();
|
|
634
|
+
topo.push(item);
|
|
635
|
+
for (const le of out.get(item)) {
|
|
636
|
+
indeg.set(le.toItem, indeg.get(le.toItem) - 1);
|
|
637
|
+
if (indeg.get(le.toItem) === 0) queue.push(le.toItem);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (topo.length < realItems.length) {
|
|
641
|
+
for (const item of realItems) if (!topo.includes(item)) topo.push(item);
|
|
642
|
+
}
|
|
643
|
+
const rank = new Map(realItems.map((item) => [item, 0]));
|
|
644
|
+
for (const item of topo) for (const le of out.get(item)) rank.set(le.toItem, Math.max(rank.get(le.toItem), rank.get(item) + le.minlen));
|
|
645
|
+
const LABELLED_RANK_WEIGHT = 2;
|
|
646
|
+
const edgeRankWeight = (le) => le.edge.label ? LABELLED_RANK_WEIGHT : 1;
|
|
647
|
+
/** 加权中位数;累计权重正好落在中点时取两侧值的平均,等权时即普通中位数;调用方再四舍五入为整数层号。 */
|
|
648
|
+
const weightedRankMedian = (entries) => {
|
|
649
|
+
if (!entries.length) return 0;
|
|
650
|
+
const sorted = [...entries].sort((a, b) => a.value - b.value || a.weight - b.weight);
|
|
651
|
+
const total = sorted.reduce((sum, entry) => sum + entry.weight, 0);
|
|
652
|
+
let acc = 0;
|
|
653
|
+
let lower = sorted[0].value;
|
|
654
|
+
for (const entry of sorted) {
|
|
655
|
+
acc += entry.weight;
|
|
656
|
+
if (acc >= total / 2 - 1e-9) {
|
|
657
|
+
lower = entry.value;
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
acc = 0;
|
|
662
|
+
let upper = sorted[sorted.length - 1].value;
|
|
663
|
+
for (const entry of sorted) {
|
|
664
|
+
acc += entry.weight;
|
|
665
|
+
if (acc > total / 2 + 1e-9) {
|
|
666
|
+
upper = entry.value;
|
|
667
|
+
break;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return (lower + upper) / 2;
|
|
671
|
+
};
|
|
672
|
+
for (let iter = 0; iter < 4; iter += 1) {
|
|
673
|
+
const order = iter % 2 ? [...topo].reverse() : topo;
|
|
674
|
+
for (const item of order) {
|
|
675
|
+
const preds = inn.get(item).map((le) => rank.get(le.fromItem) + le.minlen);
|
|
676
|
+
const succs = out.get(item).map((le) => rank.get(le.toItem) - le.minlen);
|
|
677
|
+
const lower = preds.length ? Math.max(...preds) : 0;
|
|
678
|
+
const upper = succs.length ? Math.min(...succs) : Infinity;
|
|
679
|
+
if (upper < lower) continue;
|
|
680
|
+
const entries = [...inn.get(item).map((le) => ({
|
|
681
|
+
value: rank.get(le.fromItem) + le.minlen,
|
|
682
|
+
weight: edgeRankWeight(le)
|
|
683
|
+
})), ...out.get(item).map((le) => ({
|
|
684
|
+
value: rank.get(le.toItem) - le.minlen,
|
|
685
|
+
weight: edgeRankWeight(le)
|
|
686
|
+
}))];
|
|
687
|
+
const desired = Math.round(weightedRankMedian(entries));
|
|
688
|
+
const next = Math.max(lower, Math.min(upper, Number.isFinite(desired) ? desired : lower));
|
|
689
|
+
rank.set(item, next);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
let minRank = Math.min(...realItems.map((item) => rank.get(item)));
|
|
693
|
+
if (!Number.isFinite(minRank)) minRank = 0;
|
|
694
|
+
for (const item of realItems) item.rank = (rank.get(item) - minRank) * 2;
|
|
695
|
+
const frameRankRanges = /* @__PURE__ */ new Map();
|
|
696
|
+
for (const cluster of clusters.values()) {
|
|
697
|
+
const members = realItems.filter((item) => isDescendantOf(item.cluster, cluster));
|
|
698
|
+
if (!members.length) continue;
|
|
699
|
+
frameRankRanges.set(cluster.id, {
|
|
700
|
+
top: Math.min(...members.map((item) => item.rank)),
|
|
701
|
+
bottom: Math.max(...members.map((item) => item.rank))
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
let dummySeq = 1e5;
|
|
705
|
+
const dummyItems = [];
|
|
706
|
+
for (const le of layoutEdges) {
|
|
707
|
+
const a = le.fromItem;
|
|
708
|
+
const b = le.toItem;
|
|
709
|
+
const span = b.rank - a.rank;
|
|
710
|
+
if (span <= 0) {
|
|
711
|
+
le.edge.skip = true;
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
const chain = [a];
|
|
715
|
+
let labelRank = le.edge.label || le.edge.icon ? a.rank + Math.max(1, Math.floor(span / 2)) : -1;
|
|
716
|
+
if (le.edge.label && (le.fromFrame || le.toFrame)) {
|
|
717
|
+
const candidates = [];
|
|
718
|
+
const lowIsFrame = le.reversed ? le.toFrame : le.fromFrame;
|
|
719
|
+
const highIsFrame = le.reversed ? le.fromFrame : le.toFrame;
|
|
720
|
+
const lowFrameId = le.reversed ? le.edge.to : le.edge.from;
|
|
721
|
+
const highFrameId = le.reversed ? le.edge.from : le.edge.to;
|
|
722
|
+
if (lowIsFrame) {
|
|
723
|
+
const range = frameRankRanges.get(lowFrameId);
|
|
724
|
+
if (range && b.rank < range.top) candidates.push(b.rank + Math.max(1, Math.floor((range.top - b.rank) / 2)));
|
|
725
|
+
else if (range && b.rank > range.bottom) candidates.push(range.bottom + Math.max(1, Math.floor((b.rank - range.bottom) / 2)));
|
|
726
|
+
}
|
|
727
|
+
if (highIsFrame) {
|
|
728
|
+
const range = frameRankRanges.get(highFrameId);
|
|
729
|
+
if (range && a.rank < range.top) candidates.push(a.rank + Math.max(1, Math.floor((range.top - a.rank) / 2)));
|
|
730
|
+
else if (range && a.rank > range.bottom) candidates.push(range.bottom + Math.max(1, Math.floor((a.rank - range.bottom) / 2)));
|
|
731
|
+
}
|
|
732
|
+
const valid = candidates.filter((rank) => rank > a.rank && rank < b.rank);
|
|
733
|
+
if (valid.length) labelRank = valid[0];
|
|
734
|
+
}
|
|
735
|
+
for (let r = a.rank + 1; r < b.rank; r += 1) {
|
|
736
|
+
const isLabel = r === labelRank;
|
|
737
|
+
let tw = 4;
|
|
738
|
+
let th = 4;
|
|
739
|
+
let labelLines = [];
|
|
740
|
+
if (isLabel) {
|
|
741
|
+
labelLines = wrapText(le.edge.label, 160, FONT.label, measure);
|
|
742
|
+
if (!labelLines.length && le.edge.icon) labelLines = [""];
|
|
743
|
+
tw = Math.max(26, maxWidthOf(labelLines, FONT.label, measure) + 10 + (le.edge.icon ? 22 : 0));
|
|
744
|
+
th = Math.max(18, labelLines.length * 16 + 6);
|
|
745
|
+
}
|
|
746
|
+
const item = {
|
|
747
|
+
kind: isLabel ? "label" : "dummy",
|
|
748
|
+
id: `@@${le.edge.seq}:${r}`,
|
|
749
|
+
seq: dummySeq++,
|
|
750
|
+
rank: r,
|
|
751
|
+
edge: le.edge,
|
|
752
|
+
owner: le.owner,
|
|
753
|
+
cluster: le.owner,
|
|
754
|
+
tw,
|
|
755
|
+
th,
|
|
756
|
+
bw: isLR ? th : tw,
|
|
757
|
+
bh: isLR ? tw : th,
|
|
758
|
+
labelLines,
|
|
759
|
+
offsetCross: 0,
|
|
760
|
+
up: [],
|
|
761
|
+
down: []
|
|
762
|
+
};
|
|
763
|
+
dummyItems.push(item);
|
|
764
|
+
chain.push(item);
|
|
765
|
+
allItems.push(item);
|
|
766
|
+
}
|
|
767
|
+
chain.push(b);
|
|
768
|
+
le.chain = chain;
|
|
769
|
+
for (let i = 0; i < chain.length - 1; i += 1) {
|
|
770
|
+
chain[i].down.push(chain[i + 1]);
|
|
771
|
+
chain[i + 1].up.push(chain[i]);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
const rankItems = /* @__PURE__ */ new Map();
|
|
775
|
+
for (const item of allItems) {
|
|
776
|
+
if (!rankItems.has(item.rank)) rankItems.set(item.rank, []);
|
|
777
|
+
rankItems.get(item.rank).push(item);
|
|
778
|
+
}
|
|
779
|
+
for (const [r, items] of rankItems) {
|
|
780
|
+
items.sort((a, b) => a.seq - b.seq);
|
|
781
|
+
rankItems.set(r, items);
|
|
782
|
+
}
|
|
783
|
+
const maxRank = Math.max(...rankItems.keys());
|
|
784
|
+
const clusterItemsAtRank = (cluster, r) => (rankItems.get(r) || []).filter((item) => isDescendantOf(item.cluster, cluster));
|
|
785
|
+
const childPresentAt = (child, r) => {
|
|
786
|
+
const range = frameRankRanges.get(child.id);
|
|
787
|
+
return Boolean(range) && r >= range.top && r <= range.bottom;
|
|
788
|
+
};
|
|
789
|
+
const buildOrder = (cluster, r, bc) => {
|
|
790
|
+
const direct = (rankItems.get(r) || []).filter((item) => item.cluster === cluster);
|
|
791
|
+
const children = cluster.children.filter((child) => childPresentAt(child, r));
|
|
792
|
+
const childBC = /* @__PURE__ */ new Map();
|
|
793
|
+
for (const child of children) {
|
|
794
|
+
const childItems = clusterItemsAtRank(child, r);
|
|
795
|
+
if (childItems.length) {
|
|
796
|
+
childBC.set(child, childItems.reduce((sum, item) => sum + (bc.get(item) ?? 0), 0) / childItems.length);
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
const range = frameRankRanges.get(child.id);
|
|
800
|
+
let fallback = 0;
|
|
801
|
+
for (let delta = 1; range && delta <= range.bottom - range.top; delta += 1) {
|
|
802
|
+
const found = [r - delta, r + delta].filter((rank) => rank >= range.top && rank <= range.bottom).map((rank) => (rankItems.get(rank) || []).filter((item) => isDescendantOf(item.cluster, child))).find((list) => list.length > 0);
|
|
803
|
+
if (!found) continue;
|
|
804
|
+
const known = found.map((item) => bc.get(item)).filter((value) => value != null);
|
|
805
|
+
if (known.length) fallback = known.reduce((sum, value) => sum + value, 0) / known.length;
|
|
806
|
+
else {
|
|
807
|
+
const list = rankItems.get(found[0].rank) || [];
|
|
808
|
+
fallback = found.reduce((sum, item) => sum + list.indexOf(item), 0) / found.length;
|
|
809
|
+
}
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
childBC.set(child, fallback);
|
|
813
|
+
}
|
|
814
|
+
direct.sort((a, b) => (bc.get(a) ?? 0) - (bc.get(b) ?? 0) || a.seq - b.seq);
|
|
815
|
+
const gaps = Array.from({ length: children.length + 1 }, () => []);
|
|
816
|
+
for (const item of direct) {
|
|
817
|
+
let gap = 0;
|
|
818
|
+
for (const child of children) if ((bc.get(item) ?? 0) > (childBC.get(child) ?? 0)) gap += 1;
|
|
819
|
+
item.gapIndex = gap;
|
|
820
|
+
gaps[gap].push(item);
|
|
821
|
+
}
|
|
822
|
+
const result = [];
|
|
823
|
+
for (let g = 0; g <= children.length; g += 1) {
|
|
824
|
+
result.push(...gaps[g]);
|
|
825
|
+
if (g < children.length) result.push(...buildOrder(children[g], r, bc));
|
|
826
|
+
}
|
|
827
|
+
return result;
|
|
828
|
+
};
|
|
829
|
+
const orderRank = (r, side) => {
|
|
830
|
+
const items = rankItems.get(r) || [];
|
|
831
|
+
if (!items.length) return;
|
|
832
|
+
const current = new Map(items.map((item, index) => [item, index]));
|
|
833
|
+
const adjacent = rankItems.get(side === "up" ? r - 1 : r + 1) || [];
|
|
834
|
+
const adjacentIndex = new Map(adjacent.map((item, index) => [item, index]));
|
|
835
|
+
const bc = /* @__PURE__ */ new Map();
|
|
836
|
+
for (const item of items) {
|
|
837
|
+
const neighbors = (side === "up" ? item.up : item.down).map((neighbor) => adjacentIndex.get(neighbor)).filter((index) => index != null);
|
|
838
|
+
bc.set(item, neighbors.length ? neighbors.reduce((sum, index) => sum + index, 0) / neighbors.length : current.get(item) ?? 0);
|
|
839
|
+
}
|
|
840
|
+
const next = buildOrder(root, r, bc);
|
|
841
|
+
if (next.length !== items.length) return;
|
|
842
|
+
rankItems.set(r, next);
|
|
843
|
+
};
|
|
844
|
+
for (let iter = 0; iter < 4; iter += 1) {
|
|
845
|
+
for (let r = 1; r <= maxRank; r += 1) orderRank(r, "up");
|
|
846
|
+
for (let r = maxRank - 1; r >= 0; r -= 1) orderRank(r, "down");
|
|
847
|
+
}
|
|
848
|
+
const linksBetween = (r) => {
|
|
849
|
+
const top = rankItems.get(r) || [];
|
|
850
|
+
const bottom = rankItems.get(r + 1) || [];
|
|
851
|
+
const topIndex = new Map(top.map((item, index) => [item, index]));
|
|
852
|
+
const bottomIndex = new Map(bottom.map((item, index) => [item, index]));
|
|
853
|
+
const links = [];
|
|
854
|
+
for (const item of top) for (const neighbor of item.down) if (neighbor.rank === r + 1) links.push([topIndex.get(item), bottomIndex.get(neighbor)]);
|
|
855
|
+
return links;
|
|
856
|
+
};
|
|
857
|
+
const crossings = (r) => {
|
|
858
|
+
if (r < 0 || r >= maxRank) return 0;
|
|
859
|
+
const links = linksBetween(r);
|
|
860
|
+
let count = 0;
|
|
861
|
+
for (let i = 0; i < links.length; i += 1) for (let j = i + 1; j < links.length; j += 1) if ((links[i][0] - links[j][0]) * (links[i][1] - links[j][1]) < 0) count += 1;
|
|
862
|
+
return count;
|
|
863
|
+
};
|
|
864
|
+
for (let pass = 0; pass < 2; pass += 1) for (let r = 0; r <= maxRank; r += 1) {
|
|
865
|
+
const items = rankItems.get(r);
|
|
866
|
+
if (!items) continue;
|
|
867
|
+
for (let i = 0; i < items.length - 1; i += 1) {
|
|
868
|
+
const a = items[i];
|
|
869
|
+
const b = items[i + 1];
|
|
870
|
+
if (a.cluster !== b.cluster || (a.gapIndex || 0) !== (b.gapIndex || 0)) continue;
|
|
871
|
+
const before = crossings(r - 1) + crossings(r);
|
|
872
|
+
items[i] = b;
|
|
873
|
+
items[i + 1] = a;
|
|
874
|
+
if (crossings(r - 1) + crossings(r) < before) {} else {
|
|
875
|
+
items[i] = a;
|
|
876
|
+
items[i + 1] = b;
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
for (let pass = 0; pass < 2; pass += 1) for (let r = 0; r <= maxRank; r += 1) {
|
|
881
|
+
const items = rankItems.get(r);
|
|
882
|
+
if (!items) continue;
|
|
883
|
+
for (let i = 0; i < items.length - 1; i += 1) {
|
|
884
|
+
const a = items[i];
|
|
885
|
+
const b = items[i + 1];
|
|
886
|
+
if (a.kind !== "node" || b.kind !== "node") continue;
|
|
887
|
+
if (a.cluster !== b.cluster || (a.gapIndex || 0) !== (b.gapIndex || 0)) continue;
|
|
888
|
+
if (a.seq < b.seq) continue;
|
|
889
|
+
const before = crossings(r - 1) + crossings(r);
|
|
890
|
+
items[i] = b;
|
|
891
|
+
items[i + 1] = a;
|
|
892
|
+
if (crossings(r - 1) + crossings(r) <= before) continue;
|
|
893
|
+
items[i] = a;
|
|
894
|
+
items[i + 1] = b;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
const clusterGap = 20;
|
|
898
|
+
const rankSegments = (cluster, r) => {
|
|
899
|
+
const active = [];
|
|
900
|
+
cluster.children.forEach((child, index) => {
|
|
901
|
+
if (childPresentAt(child, r)) active.push(index);
|
|
902
|
+
});
|
|
903
|
+
const segments = [];
|
|
904
|
+
for (let s = 0; s <= active.length; s += 1) segments.push({
|
|
905
|
+
gStart: s === 0 ? 0 : active[s - 1] + 1,
|
|
906
|
+
gEnd: s === active.length ? cluster.children.length : active[s],
|
|
907
|
+
leftChild: s === 0 ? null : active[s - 1],
|
|
908
|
+
rightChild: s === active.length ? null : active[s]
|
|
909
|
+
});
|
|
910
|
+
return segments;
|
|
911
|
+
};
|
|
912
|
+
const segmentNatural = (list) => list.reduce((sum, item, index) => sum + item.bw + (index ? hgap(list[index - 1], item) : 0), 0);
|
|
913
|
+
const computeWidths = (cluster) => {
|
|
914
|
+
for (const child of cluster.children) computeWidths(child);
|
|
915
|
+
const children = cluster.children;
|
|
916
|
+
const gapWidths = Array.from({ length: children.length + 1 }, () => 0);
|
|
917
|
+
const singles = [];
|
|
918
|
+
const multis = [];
|
|
919
|
+
for (const [r, items] of rankItems) {
|
|
920
|
+
const segments = rankSegments(cluster, r);
|
|
921
|
+
const byGap = /* @__PURE__ */ new Map();
|
|
922
|
+
for (const item of items) {
|
|
923
|
+
if (item.cluster !== cluster) continue;
|
|
924
|
+
const gap = item.gapIndex || 0;
|
|
925
|
+
if (!byGap.has(gap)) byGap.set(gap, []);
|
|
926
|
+
byGap.get(gap).push(item);
|
|
927
|
+
}
|
|
928
|
+
for (const [gap, list] of byGap) {
|
|
929
|
+
const entry = {
|
|
930
|
+
...segments[gap] || segments[segments.length - 1],
|
|
931
|
+
natural: segmentNatural(list)
|
|
932
|
+
};
|
|
933
|
+
if (entry.gStart === entry.gEnd) singles.push(entry);
|
|
934
|
+
else multis.push(entry);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
for (const seg of singles) gapWidths[seg.gStart] = Math.max(gapWidths[seg.gStart], seg.natural);
|
|
938
|
+
const spanOf = (seg) => {
|
|
939
|
+
let width = 0;
|
|
940
|
+
let bands = 0;
|
|
941
|
+
for (let g = seg.gStart; g <= seg.gEnd; g += 1) {
|
|
942
|
+
if (gapWidths[g] <= 0) continue;
|
|
943
|
+
width += gapWidths[g];
|
|
944
|
+
bands += 1;
|
|
945
|
+
}
|
|
946
|
+
for (let c = seg.gStart; c < seg.gEnd; c += 1) {
|
|
947
|
+
width += children[c].width;
|
|
948
|
+
bands += 1;
|
|
949
|
+
}
|
|
950
|
+
return width + clusterGap * Math.max(0, bands - 1);
|
|
951
|
+
};
|
|
952
|
+
const ordered = multis.map((seg) => ({
|
|
953
|
+
...seg,
|
|
954
|
+
span: spanOf(seg)
|
|
955
|
+
})).sort((a, b) => a.span - b.span || a.gStart - b.gStart || a.gEnd - b.gEnd || a.natural - b.natural);
|
|
956
|
+
for (const seg of ordered) for (let guard = 0; guard < 8; guard += 1) {
|
|
957
|
+
const span = spanOf(seg);
|
|
958
|
+
if (seg.natural <= span + 1e-9) break;
|
|
959
|
+
const count = seg.gEnd - seg.gStart + 1;
|
|
960
|
+
const add = (seg.natural - span) / count;
|
|
961
|
+
for (let g = seg.gStart; g <= seg.gEnd; g += 1) gapWidths[g] += add;
|
|
962
|
+
}
|
|
963
|
+
const slots = [];
|
|
964
|
+
for (let g = 0; g <= children.length; g += 1) {
|
|
965
|
+
if (gapWidths[g] > 0) slots.push({
|
|
966
|
+
kind: "gap",
|
|
967
|
+
gap: g,
|
|
968
|
+
width: gapWidths[g]
|
|
969
|
+
});
|
|
970
|
+
if (g < children.length) slots.push({
|
|
971
|
+
kind: "child",
|
|
972
|
+
child: children[g],
|
|
973
|
+
width: children[g].width
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
const contentWidth = slots.reduce((sum, slot, index) => sum + slot.width + (index ? clusterGap : 0), 0);
|
|
977
|
+
if (cluster !== root && contentWidth > 200) layoutClusterTitle(cluster, contentWidth);
|
|
978
|
+
let width;
|
|
979
|
+
if (cluster === root) width = contentWidth;
|
|
980
|
+
else {
|
|
981
|
+
const crossTitle = isLR ? cluster.titleBand : 0;
|
|
982
|
+
const titleMin = isLR || contentWidth > 200 ? 0 : cluster.titleW + 28;
|
|
983
|
+
width = Math.max(contentWidth + 28 + crossTitle, titleMin);
|
|
984
|
+
}
|
|
985
|
+
cluster.contentWidth = contentWidth;
|
|
986
|
+
cluster.gapWidths = gapWidths;
|
|
987
|
+
cluster.slots = slots;
|
|
988
|
+
cluster.width = width;
|
|
989
|
+
};
|
|
990
|
+
computeWidths(root);
|
|
991
|
+
const placeCluster = (cluster, left) => {
|
|
992
|
+
cluster.left = left;
|
|
993
|
+
const pad = cluster === root ? 0 : FRAME_PAD;
|
|
994
|
+
const titleBand = isLR && cluster !== root ? cluster.titleBand : 0;
|
|
995
|
+
const innerLeft = left + pad + titleBand;
|
|
996
|
+
const innerWidth = cluster.width - pad * 2 - titleBand;
|
|
997
|
+
cluster.innerLeft = innerLeft;
|
|
998
|
+
let cursor = innerLeft + Math.max(0, (innerWidth - cluster.contentWidth) / 2);
|
|
999
|
+
const contentLeft = cursor;
|
|
1000
|
+
const contentRight = contentLeft + cluster.contentWidth;
|
|
1001
|
+
cluster.slots.forEach((slot, index) => {
|
|
1002
|
+
slot.left = cursor;
|
|
1003
|
+
if (slot.kind === "child") placeCluster(slot.child, cursor);
|
|
1004
|
+
cursor += slot.width + (index < cluster.slots.length - 1 ? clusterGap : 0);
|
|
1005
|
+
});
|
|
1006
|
+
for (const [r, items] of rankItems) {
|
|
1007
|
+
const segments = rankSegments(cluster, r);
|
|
1008
|
+
const byGap = /* @__PURE__ */ new Map();
|
|
1009
|
+
for (const item of items) {
|
|
1010
|
+
if (item.cluster !== cluster) continue;
|
|
1011
|
+
const gap = item.gapIndex || 0;
|
|
1012
|
+
if (!byGap.has(gap)) byGap.set(gap, []);
|
|
1013
|
+
byGap.get(gap).push(item);
|
|
1014
|
+
}
|
|
1015
|
+
for (const [gap, list] of byGap) {
|
|
1016
|
+
const seg = segments[gap] || segments[segments.length - 1];
|
|
1017
|
+
const leftChild = seg.leftChild == null ? null : cluster.children[seg.leftChild];
|
|
1018
|
+
const rightChild = seg.rightChild == null ? null : cluster.children[seg.rightChild];
|
|
1019
|
+
const slotLeft = leftChild ? leftChild.left + leftChild.width + clusterGap : contentLeft;
|
|
1020
|
+
const slotRight = Math.max(slotLeft, rightChild ? rightChild.left - clusterGap : contentRight);
|
|
1021
|
+
const natural = segmentNatural(list);
|
|
1022
|
+
let x = slotLeft + Math.max(0, (slotRight - slotLeft - natural) / 2);
|
|
1023
|
+
list.forEach((item, index) => {
|
|
1024
|
+
item.slotLeft = slotLeft;
|
|
1025
|
+
item.slotRight = slotRight;
|
|
1026
|
+
item.centerX = x + item.bw / 2;
|
|
1027
|
+
if (index < list.length - 1) x += item.bw + hgap(item, list[index + 1]);
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
placeCluster(root, 0);
|
|
1033
|
+
const anchorX = (item) => item.centerX + (item.offsetCross || 0);
|
|
1034
|
+
const segmentGroups = [];
|
|
1035
|
+
for (const [r, items] of rankItems) {
|
|
1036
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1037
|
+
for (const item of items) {
|
|
1038
|
+
const key = `${item.cluster.id ?? "@root"}#${item.gapIndex || 0}`;
|
|
1039
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
1040
|
+
groups.get(key).push(item);
|
|
1041
|
+
}
|
|
1042
|
+
for (const list of groups.values()) segmentGroups.push({
|
|
1043
|
+
rank: r,
|
|
1044
|
+
items: list
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
for (let iter = 0; iter < 8; iter += 1) {
|
|
1048
|
+
const down = iter % 2 === 0;
|
|
1049
|
+
const ranks = down ? [...rankItems.keys()].sort((a, b) => a - b) : [...rankItems.keys()].sort((a, b) => b - a);
|
|
1050
|
+
for (const r of ranks) for (const group of segmentGroups) {
|
|
1051
|
+
if (group.rank !== r) continue;
|
|
1052
|
+
const items = group.items;
|
|
1053
|
+
const desired = items.map((item) => {
|
|
1054
|
+
const neighbors = (down ? item.up : item.down).map(anchorX);
|
|
1055
|
+
if (!neighbors.length) return item.centerX;
|
|
1056
|
+
return neighbors.reduce((sum, value) => sum + value, 0) / neighbors.length - (item.offsetCross || 0);
|
|
1057
|
+
});
|
|
1058
|
+
let cursor = items[0].slotLeft;
|
|
1059
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
1060
|
+
const item = items[i];
|
|
1061
|
+
const min = (i === 0 ? item.slotLeft : cursor) + item.bw / 2;
|
|
1062
|
+
const max = item.slotRight - item.bw / 2;
|
|
1063
|
+
let center = Number.isFinite(desired[i]) ? desired[i] : min;
|
|
1064
|
+
center = Math.max(min, Math.min(max, center));
|
|
1065
|
+
item.centerX = center;
|
|
1066
|
+
cursor = center + item.bw / 2 + (i < items.length - 1 ? hgap(item, items[i + 1]) : 0);
|
|
1067
|
+
}
|
|
1068
|
+
let limit = items[items.length - 1].slotRight;
|
|
1069
|
+
for (let i = items.length - 1; i >= 0; i -= 1) {
|
|
1070
|
+
const item = items[i];
|
|
1071
|
+
const max = (i === items.length - 1 ? item.slotRight : limit) - item.bw / 2;
|
|
1072
|
+
const min = item.slotLeft + item.bw / 2;
|
|
1073
|
+
item.centerX = Math.max(min, Math.min(max, item.centerX));
|
|
1074
|
+
limit = item.centerX - item.bw / 2 - (i > 0 ? hgap(items[i - 1], item) : 0);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
const omegaOf = (a, b) => {
|
|
1079
|
+
const aVirtual = a.kind !== "real";
|
|
1080
|
+
const bVirtual = b.kind !== "real";
|
|
1081
|
+
if (aVirtual && bVirtual) return 8;
|
|
1082
|
+
if (aVirtual || bVirtual) return 2;
|
|
1083
|
+
return 1;
|
|
1084
|
+
};
|
|
1085
|
+
const segmentEntry = /* @__PURE__ */ new Map();
|
|
1086
|
+
for (const group of segmentGroups) group.items.forEach((item, index) => segmentEntry.set(item, {
|
|
1087
|
+
group,
|
|
1088
|
+
index
|
|
1089
|
+
}));
|
|
1090
|
+
const freeInterval = (item) => {
|
|
1091
|
+
const entry = segmentEntry.get(item);
|
|
1092
|
+
const items = entry.group.items;
|
|
1093
|
+
let min = item.slotLeft + item.bw / 2;
|
|
1094
|
+
let max = item.slotRight - item.bw / 2;
|
|
1095
|
+
if (entry.index > 0) {
|
|
1096
|
+
const prev = items[entry.index - 1];
|
|
1097
|
+
min = Math.max(min, prev.centerX + prev.bw / 2 + hgap(prev, item) + item.bw / 2);
|
|
1098
|
+
}
|
|
1099
|
+
if (entry.index < items.length - 1) {
|
|
1100
|
+
const next = items[entry.index + 1];
|
|
1101
|
+
max = Math.min(max, next.centerX - next.bw / 2 - hgap(item, next) - item.bw / 2);
|
|
1102
|
+
}
|
|
1103
|
+
return min <= max ? {
|
|
1104
|
+
min,
|
|
1105
|
+
max
|
|
1106
|
+
} : {
|
|
1107
|
+
min: item.centerX,
|
|
1108
|
+
max: item.centerX
|
|
1109
|
+
};
|
|
1110
|
+
};
|
|
1111
|
+
const weightedMedian = (entries) => {
|
|
1112
|
+
const sorted = [...entries].sort((a, b) => a.value - b.value || b.weight - a.weight);
|
|
1113
|
+
const total = sorted.reduce((sum, entry) => sum + entry.weight, 0);
|
|
1114
|
+
let acc = 0;
|
|
1115
|
+
for (const entry of sorted) {
|
|
1116
|
+
acc += entry.weight;
|
|
1117
|
+
if (acc >= total / 2 - 1e-9) return entry.value;
|
|
1118
|
+
}
|
|
1119
|
+
return sorted[sorted.length - 1].value;
|
|
1120
|
+
};
|
|
1121
|
+
const clampTo = (value, interval) => Math.max(interval.min, Math.min(interval.max, value));
|
|
1122
|
+
const incidentCost = (item, centerX) => {
|
|
1123
|
+
const anchor = centerX + (item.offsetCross || 0);
|
|
1124
|
+
let cost = 0;
|
|
1125
|
+
for (const neighbor of item.up) cost += omegaOf(item, neighbor) * Math.abs(anchor - anchorX(neighbor));
|
|
1126
|
+
for (const neighbor of item.down) cost += omegaOf(item, neighbor) * Math.abs(anchor - anchorX(neighbor));
|
|
1127
|
+
return cost;
|
|
1128
|
+
};
|
|
1129
|
+
const chainCost = (chain, overrides) => {
|
|
1130
|
+
let cost = 0;
|
|
1131
|
+
for (let i = 0; i < chain.length - 1; i += 1) {
|
|
1132
|
+
const a = chain[i];
|
|
1133
|
+
const b = chain[i + 1];
|
|
1134
|
+
const ax = overrides && overrides.has(a) ? overrides.get(a) : anchorX(a);
|
|
1135
|
+
const bx = overrides && overrides.has(b) ? overrides.get(b) : anchorX(b);
|
|
1136
|
+
cost += omegaOf(a, b) * Math.abs(ax - bx);
|
|
1137
|
+
}
|
|
1138
|
+
return cost;
|
|
1139
|
+
};
|
|
1140
|
+
const trySingleMove = (item) => {
|
|
1141
|
+
const neighbors = [...item.up, ...item.down];
|
|
1142
|
+
if (!neighbors.length) return false;
|
|
1143
|
+
const median = weightedMedian(neighbors.map((neighbor) => ({
|
|
1144
|
+
value: anchorX(neighbor),
|
|
1145
|
+
weight: omegaOf(item, neighbor)
|
|
1146
|
+
})));
|
|
1147
|
+
const target = clampTo(median - (item.offsetCross || 0), freeInterval(item));
|
|
1148
|
+
if (Math.abs(target - item.centerX) < .01) return false;
|
|
1149
|
+
const before = incidentCost(item, item.centerX);
|
|
1150
|
+
if (incidentCost(item, target) > before - .01) return false;
|
|
1151
|
+
item.centerX = target;
|
|
1152
|
+
return true;
|
|
1153
|
+
};
|
|
1154
|
+
const runInterval = (items) => {
|
|
1155
|
+
let min = -Infinity;
|
|
1156
|
+
let max = Infinity;
|
|
1157
|
+
for (const item of items) {
|
|
1158
|
+
const interval = freeInterval(item);
|
|
1159
|
+
min = Math.max(min, interval.min);
|
|
1160
|
+
max = Math.min(max, interval.max);
|
|
1161
|
+
if (min > max + 1e-9) return null;
|
|
1162
|
+
}
|
|
1163
|
+
return {
|
|
1164
|
+
min,
|
|
1165
|
+
max
|
|
1166
|
+
};
|
|
1167
|
+
};
|
|
1168
|
+
const tryChainMove = (le) => {
|
|
1169
|
+
const chain = le.chain;
|
|
1170
|
+
if (!chain || chain.length < 3) return false;
|
|
1171
|
+
const members = chain.slice(1, -1);
|
|
1172
|
+
if (!members.every((item) => segmentEntry.has(item))) return false;
|
|
1173
|
+
const before = chainCost(chain, null);
|
|
1174
|
+
const all = runInterval(members);
|
|
1175
|
+
if (all) {
|
|
1176
|
+
const low = anchorX(chain[0]);
|
|
1177
|
+
const high = anchorX(chain[chain.length - 1]);
|
|
1178
|
+
const mean = members.reduce((sum, item) => sum + anchorX(item), 0) / members.length;
|
|
1179
|
+
const best = {
|
|
1180
|
+
min: Math.max(all.min, Math.min(low, high)),
|
|
1181
|
+
max: Math.min(all.max, Math.max(low, high))
|
|
1182
|
+
};
|
|
1183
|
+
let target;
|
|
1184
|
+
if (best.min <= best.max) target = clampTo(mean, best);
|
|
1185
|
+
else if (all.max < Math.min(low, high)) target = all.max;
|
|
1186
|
+
else target = all.min;
|
|
1187
|
+
const overrides = new Map(members.map((item) => [item, target]));
|
|
1188
|
+
if (chainCost(chain, overrides) > before - .01) return false;
|
|
1189
|
+
for (const item of members) item.centerX = target;
|
|
1190
|
+
return true;
|
|
1191
|
+
}
|
|
1192
|
+
let prefix = 0;
|
|
1193
|
+
while (prefix < members.length && runInterval(members.slice(0, prefix + 1))) prefix += 1;
|
|
1194
|
+
let suffix = 0;
|
|
1195
|
+
while (suffix < members.length && runInterval(members.slice(members.length - suffix - 1))) suffix += 1;
|
|
1196
|
+
if (prefix + suffix > members.length) {
|
|
1197
|
+
if (prefix >= suffix) suffix = members.length - prefix;
|
|
1198
|
+
else prefix = members.length - suffix;
|
|
1199
|
+
}
|
|
1200
|
+
let moved = false;
|
|
1201
|
+
const alignRun = (items, outside) => {
|
|
1202
|
+
if (items.length < 2) return false;
|
|
1203
|
+
const interval = runInterval(items);
|
|
1204
|
+
if (!interval) return false;
|
|
1205
|
+
const leftmost = members.indexOf(items[0]);
|
|
1206
|
+
const nextItem = leftmost > 0 ? members[leftmost - 1] : members[leftmost + items.length];
|
|
1207
|
+
const entries = [{
|
|
1208
|
+
value: anchorX(outside),
|
|
1209
|
+
weight: 2
|
|
1210
|
+
}];
|
|
1211
|
+
if (nextItem) entries.push({
|
|
1212
|
+
value: anchorX(nextItem),
|
|
1213
|
+
weight: 8
|
|
1214
|
+
});
|
|
1215
|
+
const target = clampTo(weightedMedian(entries), interval);
|
|
1216
|
+
const overrides = new Map(items.map((item) => [item, target]));
|
|
1217
|
+
if (chainCost(chain, overrides) > chainCost(chain, null) - .01) return false;
|
|
1218
|
+
for (const item of items) item.centerX = target;
|
|
1219
|
+
return true;
|
|
1220
|
+
};
|
|
1221
|
+
if (prefix >= 2) moved = alignRun(members.slice(0, prefix), chain[0]) || moved;
|
|
1222
|
+
if (suffix >= 2) moved = alignRun(members.slice(members.length - suffix), chain[chain.length - 1]) || moved;
|
|
1223
|
+
return moved;
|
|
1224
|
+
};
|
|
1225
|
+
for (let pass = 0; pass < 40; pass += 1) {
|
|
1226
|
+
let accepted = false;
|
|
1227
|
+
for (const [, items] of [...rankItems.entries()].sort((a, b) => a[0] - b[0])) for (const item of items) if (trySingleMove(item)) accepted = true;
|
|
1228
|
+
for (const le of layoutEdges) if (tryChainMove(le)) accepted = true;
|
|
1229
|
+
if (!accepted) break;
|
|
1230
|
+
}
|
|
1231
|
+
const edgeOfItem = /* @__PURE__ */ new Map();
|
|
1232
|
+
for (const le of layoutEdges) {
|
|
1233
|
+
if (!le.chain) continue;
|
|
1234
|
+
for (const item of le.chain) edgeOfItem.set(item, le);
|
|
1235
|
+
}
|
|
1236
|
+
const realAtRank = /* @__PURE__ */ new Map();
|
|
1237
|
+
for (const item of realItems) {
|
|
1238
|
+
if (!realAtRank.has(item.rank)) realAtRank.set(item.rank, []);
|
|
1239
|
+
realAtRank.get(item.rank).push(item);
|
|
1240
|
+
}
|
|
1241
|
+
const separateGap = (items) => {
|
|
1242
|
+
if (!items.length) return;
|
|
1243
|
+
let cursor = items[0].slotLeft;
|
|
1244
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
1245
|
+
const item = items[i];
|
|
1246
|
+
const min = (i === 0 ? item.slotLeft : cursor) + item.bw / 2;
|
|
1247
|
+
const max = item.slotRight - item.bw / 2;
|
|
1248
|
+
item.centerX = Math.max(min, Math.min(max, item.centerX));
|
|
1249
|
+
cursor = item.centerX + item.bw / 2 + (i < items.length - 1 ? hgap(item, items[i + 1]) : 0);
|
|
1250
|
+
}
|
|
1251
|
+
let limit = items[items.length - 1].slotRight;
|
|
1252
|
+
for (let i = items.length - 1; i >= 0; i -= 1) {
|
|
1253
|
+
const item = items[i];
|
|
1254
|
+
const max = (i === items.length - 1 ? item.slotRight : limit) - item.bw / 2;
|
|
1255
|
+
const min = item.slotLeft + item.bw / 2;
|
|
1256
|
+
item.centerX = Math.max(min, Math.min(max, item.centerX));
|
|
1257
|
+
limit = item.centerX - item.bw / 2 - (i > 0 ? hgap(items[i - 1], item) : 0);
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
1261
|
+
for (const dummy of dummyItems) {
|
|
1262
|
+
if (dummy.kind !== "dummy") continue;
|
|
1263
|
+
for (const rank of [dummy.rank - 1, dummy.rank + 1]) {
|
|
1264
|
+
const neighborsAtRank = (rank < dummy.rank ? dummy.up : dummy.down).filter((item) => item.rank === rank);
|
|
1265
|
+
if (!neighborsAtRank.length) continue;
|
|
1266
|
+
for (const node of realAtRank.get(rank) || []) {
|
|
1267
|
+
if (neighborsAtRank.includes(node)) continue;
|
|
1268
|
+
const center = node.centerX + (node.offsetCross || 0);
|
|
1269
|
+
const half = (isLR ? node.th : node.tw) / 2 + 14;
|
|
1270
|
+
if (Math.abs(dummy.centerX - center) >= half) continue;
|
|
1271
|
+
const target = neighborsAtRank.map((item) => item.centerX + (item.offsetCross || 0)).reduce((sum, value) => sum + value, 0) / neighborsAtRank.length < center ? center - half : center + half;
|
|
1272
|
+
const le = edgeOfItem.get(dummy);
|
|
1273
|
+
const members = le && le.chain ? le.chain.slice(1, -1) : [dummy];
|
|
1274
|
+
const interval = runInterval(members);
|
|
1275
|
+
if (!interval) continue;
|
|
1276
|
+
const x = clampTo(target, interval);
|
|
1277
|
+
if (Math.abs(x - dummy.centerX) < .01) continue;
|
|
1278
|
+
for (const member of members) member.centerX = x;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
for (const group of segmentGroups) separateGap(group.items);
|
|
1283
|
+
}
|
|
1284
|
+
if (model.type === "er") for (let pass = 0; pass < 40; pass += 1) {
|
|
1285
|
+
let accepted = false;
|
|
1286
|
+
for (const [, items] of [...rankItems.entries()].sort((a, b) => a[0] - b[0])) for (const item of items) if (trySingleMove(item)) accepted = true;
|
|
1287
|
+
for (const le of layoutEdges) if (tryChainMove(le)) accepted = true;
|
|
1288
|
+
if (!accepted) break;
|
|
1289
|
+
}
|
|
1290
|
+
else {
|
|
1291
|
+
const beforeAlign = /* @__PURE__ */ new Map();
|
|
1292
|
+
for (const items of rankItems.values()) for (const item of items) beforeAlign.set(item, item.centerX);
|
|
1293
|
+
{
|
|
1294
|
+
const linkKey = (a, b) => `${a.seq}>${b.seq}`;
|
|
1295
|
+
const linkInfo = /* @__PURE__ */ new Map();
|
|
1296
|
+
for (const le of layoutEdges) {
|
|
1297
|
+
if (!le.chain) continue;
|
|
1298
|
+
for (let i = 0; i + 1 < le.chain.length; i += 1) linkInfo.set(linkKey(le.chain[i], le.chain[i + 1]), le);
|
|
1299
|
+
}
|
|
1300
|
+
const isBackLink = (le) => Boolean(le && le.reversed);
|
|
1301
|
+
const styleRank = (le) => {
|
|
1302
|
+
const style = le && le.edge ? le.edge.style : null;
|
|
1303
|
+
if (style === "dotted") return 2;
|
|
1304
|
+
if (style === "dashed") return 1;
|
|
1305
|
+
return 0;
|
|
1306
|
+
};
|
|
1307
|
+
const ranksAsc = [...rankItems.keys()].sort((a, b) => a - b);
|
|
1308
|
+
const upsOf = (item) => item.up.filter((up) => up.rank === item.rank - 1);
|
|
1309
|
+
const downsOf = (item) => item.down.filter((down) => down.rank === item.rank + 1);
|
|
1310
|
+
const level = /* @__PURE__ */ new Map();
|
|
1311
|
+
for (const r of ranksAsc) for (const item of rankItems.get(r) || []) {
|
|
1312
|
+
const ups = upsOf(item);
|
|
1313
|
+
level.set(item, ups.length ? Math.max(...ups.map((up) => level.get(up) + 1)) : 0);
|
|
1314
|
+
}
|
|
1315
|
+
const height = /* @__PURE__ */ new Map();
|
|
1316
|
+
for (const r of [...ranksAsc].reverse()) for (const item of rankItems.get(r) || []) {
|
|
1317
|
+
const downs = downsOf(item);
|
|
1318
|
+
height.set(item, downs.length ? Math.max(...downs.map((down) => height.get(down) + 1)) : 0);
|
|
1319
|
+
}
|
|
1320
|
+
const totalSpan = Math.max(0, ...height.values());
|
|
1321
|
+
const onLongest = (up, item) => level.get(up) + 1 + height.get(item) >= totalSpan ? 0 : 1;
|
|
1322
|
+
const primaryUp = /* @__PURE__ */ new Map();
|
|
1323
|
+
const balancedIn = /* @__PURE__ */ new Map();
|
|
1324
|
+
for (const r of ranksAsc) {
|
|
1325
|
+
if (r === 0) continue;
|
|
1326
|
+
for (const item of rankItems.get(r) || []) {
|
|
1327
|
+
const ups = upsOf(item);
|
|
1328
|
+
if (!ups.length) {
|
|
1329
|
+
primaryUp.set(item, null);
|
|
1330
|
+
continue;
|
|
1331
|
+
}
|
|
1332
|
+
const scored = ups.filter((up) => !isBackLink(linkInfo.get(linkKey(up, item)))).map((up) => {
|
|
1333
|
+
const le = linkInfo.get(linkKey(up, item));
|
|
1334
|
+
return {
|
|
1335
|
+
up,
|
|
1336
|
+
style: styleRank(le),
|
|
1337
|
+
crit: onLongest(up, item),
|
|
1338
|
+
x: anchorX(up),
|
|
1339
|
+
seq: up.seq
|
|
1340
|
+
};
|
|
1341
|
+
});
|
|
1342
|
+
if (!scored.length) {
|
|
1343
|
+
primaryUp.set(item, null);
|
|
1344
|
+
continue;
|
|
1345
|
+
}
|
|
1346
|
+
const bestStyle = Math.min(...scored.map((s) => s.style));
|
|
1347
|
+
const byStyle = scored.filter((s) => s.style === bestStyle);
|
|
1348
|
+
const bestCrit = Math.min(...byStyle.map((s) => s.crit));
|
|
1349
|
+
const pool = byStyle.filter((s) => s.crit === bestCrit);
|
|
1350
|
+
if (pool.length >= 4 && pool.length % 2 === 0) {
|
|
1351
|
+
const row = rankItems.get(r - 1) || [];
|
|
1352
|
+
balancedIn.set(item, pool.map((s) => s.up).sort((a, b) => row.indexOf(a) - row.indexOf(b)));
|
|
1353
|
+
primaryUp.set(item, null);
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
const xs = pool.map((s) => s.x).sort((a, b) => a - b);
|
|
1357
|
+
const mid = xs.length % 2 ? xs[(xs.length - 1) / 2] : (xs[xs.length / 2 - 1] + xs[xs.length / 2]) / 2;
|
|
1358
|
+
pool.sort((a, b) => Math.abs(a.x - mid) - Math.abs(b.x - mid) || a.seq - b.seq);
|
|
1359
|
+
primaryUp.set(item, pool[0].up);
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
const orderIndex = /* @__PURE__ */ new Map();
|
|
1363
|
+
for (const r of ranksAsc) (rankItems.get(r) || []).forEach((item, index) => orderIndex.set(item, index));
|
|
1364
|
+
const pairs = [];
|
|
1365
|
+
const hasChild = /* @__PURE__ */ new Set();
|
|
1366
|
+
const siblingRank = /* @__PURE__ */ new Map();
|
|
1367
|
+
const balanced = /* @__PURE__ */ new Map();
|
|
1368
|
+
for (const r of ranksAsc) {
|
|
1369
|
+
if (r === 0) continue;
|
|
1370
|
+
const entries = (rankItems.get(r) || []).map((item) => {
|
|
1371
|
+
const up = primaryUp.get(item);
|
|
1372
|
+
const le = up ? linkInfo.get(linkKey(up, item)) : null;
|
|
1373
|
+
return {
|
|
1374
|
+
item,
|
|
1375
|
+
up: up && !isBackLink(le) ? up : null,
|
|
1376
|
+
style: styleRank(le),
|
|
1377
|
+
crit: up ? onLongest(up, item) : 1
|
|
1378
|
+
};
|
|
1379
|
+
});
|
|
1380
|
+
const siblings = /* @__PURE__ */ new Map();
|
|
1381
|
+
for (const entry of entries) {
|
|
1382
|
+
if (!entry.up) continue;
|
|
1383
|
+
if (!siblings.has(entry.up)) siblings.set(entry.up, []);
|
|
1384
|
+
siblings.get(entry.up).push(entry);
|
|
1385
|
+
}
|
|
1386
|
+
for (const [up, list] of siblings) {
|
|
1387
|
+
const crit = Math.min(...list.map((entry) => entry.crit));
|
|
1388
|
+
const style = Math.min(...list.filter((entry) => entry.crit === crit).map((entry) => entry.style));
|
|
1389
|
+
const equal = list.filter((entry) => entry.crit === crit && entry.style === style).sort((a, b) => orderIndex.get(a.item) - orderIndex.get(b.item));
|
|
1390
|
+
if (equal.length >= 4 && equal.length % 2 === 0) {
|
|
1391
|
+
balanced.set(up, equal.map((entry) => entry.item));
|
|
1392
|
+
continue;
|
|
1393
|
+
}
|
|
1394
|
+
const middle = (equal.length - 1) / 2;
|
|
1395
|
+
equal.forEach((entry, index) => siblingRank.set(entry.item, Math.abs(index - middle)));
|
|
1396
|
+
}
|
|
1397
|
+
entries.sort((a, b) => a.crit - b.crit || a.style - b.style || anchorX(a.up || a.item) - anchorX(b.up || b.item) || (siblingRank.get(a.item) ?? 0) - (siblingRank.get(b.item) ?? 0) || a.item.seq - b.item.seq);
|
|
1398
|
+
for (const entry of entries) {
|
|
1399
|
+
const { item, up } = entry;
|
|
1400
|
+
if (!up || hasChild.has(up) || balanced.has(up)) continue;
|
|
1401
|
+
const ui = orderIndex.get(up);
|
|
1402
|
+
const vi = orderIndex.get(item);
|
|
1403
|
+
let crossing = false;
|
|
1404
|
+
for (const [pu, pv] of pairs) {
|
|
1405
|
+
if (pv.rank !== item.rank) continue;
|
|
1406
|
+
if ((orderIndex.get(pu) - ui) * (orderIndex.get(pv) - vi) < 0) {
|
|
1407
|
+
crossing = true;
|
|
1408
|
+
break;
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
if (crossing) continue;
|
|
1412
|
+
pairs.push([up, item]);
|
|
1413
|
+
hasChild.add(up);
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
const alignUp = new Map(pairs.map(([up, item]) => [item, up]));
|
|
1417
|
+
const blockOf = /* @__PURE__ */ new Map();
|
|
1418
|
+
for (const r of ranksAsc) for (const item of rankItems.get(r) || []) {
|
|
1419
|
+
const up = alignUp.get(item);
|
|
1420
|
+
if (up && blockOf.has(up)) {
|
|
1421
|
+
const block = blockOf.get(up);
|
|
1422
|
+
block.items.push(item);
|
|
1423
|
+
blockOf.set(item, block);
|
|
1424
|
+
} else blockOf.set(item, {
|
|
1425
|
+
id: blockOf.size,
|
|
1426
|
+
items: [item]
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
const crossRect = (item) => ({
|
|
1430
|
+
x: item.centerX + (item.offsetCross || 0) - (isLR ? item.th : item.tw) / 2,
|
|
1431
|
+
w: isLR ? item.th : item.tw
|
|
1432
|
+
});
|
|
1433
|
+
const corridorBlocked = (block) => {
|
|
1434
|
+
const xs = block.items.map(anchorX).sort((a, b) => a - b);
|
|
1435
|
+
const mid = xs.length % 2 ? xs[(xs.length - 1) / 2] : (xs[xs.length / 2 - 1] + xs[xs.length / 2]) / 2;
|
|
1436
|
+
const ranks = block.items.map((item) => item.rank);
|
|
1437
|
+
const lo = Math.min(...ranks);
|
|
1438
|
+
const hi = Math.max(...ranks);
|
|
1439
|
+
for (const item of realItems) {
|
|
1440
|
+
if (item.rank <= lo || item.rank >= hi) continue;
|
|
1441
|
+
if (block.items.includes(item)) continue;
|
|
1442
|
+
const rect = crossRect(item);
|
|
1443
|
+
if (mid > rect.x - 8 && mid < rect.x + rect.w + 8) return true;
|
|
1444
|
+
}
|
|
1445
|
+
return false;
|
|
1446
|
+
};
|
|
1447
|
+
for (const block of [...new Set(blockOf.values())]) {
|
|
1448
|
+
if (block.items.length < 2 || !corridorBlocked(block)) continue;
|
|
1449
|
+
for (const item of block.items) blockOf.set(item, {
|
|
1450
|
+
id: blockOf.size,
|
|
1451
|
+
items: [item]
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
for (const le of layoutEdges) {
|
|
1455
|
+
if (!le.chain || le.chain.length < 3) continue;
|
|
1456
|
+
let run = [];
|
|
1457
|
+
const flush = () => {
|
|
1458
|
+
if (run.length > 1) {
|
|
1459
|
+
const first = blockOf.get(run[0]);
|
|
1460
|
+
for (const item of run.slice(1)) {
|
|
1461
|
+
const block = blockOf.get(item);
|
|
1462
|
+
if (!block || block === first) continue;
|
|
1463
|
+
for (const member of block.items) {
|
|
1464
|
+
blockOf.set(member, first);
|
|
1465
|
+
first.items.push(member);
|
|
1466
|
+
}
|
|
1467
|
+
block.items = [];
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
run = [];
|
|
1471
|
+
};
|
|
1472
|
+
for (const item of le.chain.slice(1, -1)) {
|
|
1473
|
+
if (item.kind !== "dummy") {
|
|
1474
|
+
flush();
|
|
1475
|
+
continue;
|
|
1476
|
+
}
|
|
1477
|
+
if (run.length && item.rank !== run[run.length - 1].rank + 1) flush();
|
|
1478
|
+
run.push(item);
|
|
1479
|
+
}
|
|
1480
|
+
flush();
|
|
1481
|
+
}
|
|
1482
|
+
const blocks = [...new Set(blockOf.values())];
|
|
1483
|
+
const blockIndex = new Map(blocks.map((block, index) => [block, index]));
|
|
1484
|
+
const constraintMap = /* @__PURE__ */ new Map();
|
|
1485
|
+
for (const group of segmentGroups) {
|
|
1486
|
+
const order = [];
|
|
1487
|
+
for (const item of group.items) {
|
|
1488
|
+
const block = blockOf.get(item);
|
|
1489
|
+
if (!block) continue;
|
|
1490
|
+
if (order.length && order[order.length - 1].block === block) continue;
|
|
1491
|
+
order.push({
|
|
1492
|
+
block,
|
|
1493
|
+
item
|
|
1494
|
+
});
|
|
1495
|
+
}
|
|
1496
|
+
for (let i = 1; i < order.length; i += 1) {
|
|
1497
|
+
const a = order[i - 1];
|
|
1498
|
+
const b = order[i];
|
|
1499
|
+
const sep = a.item.bw / 2 + hgap(a.item, b.item) + b.item.bw / 2;
|
|
1500
|
+
const key = `${blockIndex.get(a.block)}>${blockIndex.get(b.block)}`;
|
|
1501
|
+
const prev = constraintMap.get(key);
|
|
1502
|
+
if (!prev) constraintMap.set(key, {
|
|
1503
|
+
a: a.block,
|
|
1504
|
+
b: b.block,
|
|
1505
|
+
sep
|
|
1506
|
+
});
|
|
1507
|
+
else prev.sep = Math.max(prev.sep, sep);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
const constraints = [...constraintMap.values()];
|
|
1511
|
+
const spanOf = (block) => {
|
|
1512
|
+
let min = -Infinity;
|
|
1513
|
+
let max = Infinity;
|
|
1514
|
+
const xs = [];
|
|
1515
|
+
for (const item of block.items) {
|
|
1516
|
+
if (item.cluster && item.cluster !== root) {
|
|
1517
|
+
min = Math.max(min, item.slotLeft + item.bw / 2 - (item.offsetCross || 0));
|
|
1518
|
+
max = Math.min(max, item.slotRight - item.bw / 2 - (item.offsetCross || 0));
|
|
1519
|
+
}
|
|
1520
|
+
xs.push(anchorX(item));
|
|
1521
|
+
}
|
|
1522
|
+
if (min > max + .01) return null;
|
|
1523
|
+
xs.sort((a, b) => a - b);
|
|
1524
|
+
const mid = xs.length % 2 ? xs[(xs.length - 1) / 2] : (xs[xs.length / 2 - 1] + xs[xs.length / 2]) / 2;
|
|
1525
|
+
return {
|
|
1526
|
+
min,
|
|
1527
|
+
max,
|
|
1528
|
+
mid
|
|
1529
|
+
};
|
|
1530
|
+
};
|
|
1531
|
+
const spans = /* @__PURE__ */ new Map();
|
|
1532
|
+
for (const block of blocks) {
|
|
1533
|
+
const s = spanOf(block);
|
|
1534
|
+
if (!s) {
|
|
1535
|
+
for (const item of block.items) blockOf.delete(item);
|
|
1536
|
+
continue;
|
|
1537
|
+
}
|
|
1538
|
+
spans.set(block, s);
|
|
1539
|
+
}
|
|
1540
|
+
const pos = /* @__PURE__ */ new Map();
|
|
1541
|
+
for (const [block, s] of spans) pos.set(block, Math.max(s.min, Math.min(s.max, s.mid)));
|
|
1542
|
+
const alive = constraints.filter((c) => spans.has(c.a) && spans.has(c.b) && c.a !== c.b);
|
|
1543
|
+
const move = (block, value) => {
|
|
1544
|
+
const s = spans.get(block);
|
|
1545
|
+
if (!s) return;
|
|
1546
|
+
pos.set(block, Math.max(s.min, Math.min(s.max, value)));
|
|
1547
|
+
};
|
|
1548
|
+
const nexts = new Map(blocks.map((block) => [block, []]));
|
|
1549
|
+
const indeg = new Map(blocks.map((block) => [block, 0]));
|
|
1550
|
+
for (const c of alive) {
|
|
1551
|
+
nexts.get(c.a).push(c);
|
|
1552
|
+
indeg.set(c.b, indeg.get(c.b) + 1);
|
|
1553
|
+
}
|
|
1554
|
+
const queue = blocks.filter((block) => spans.has(block) && indeg.get(block) === 0);
|
|
1555
|
+
queue.sort((a, b) => pos.get(a) - pos.get(b));
|
|
1556
|
+
const order = [];
|
|
1557
|
+
while (queue.length) {
|
|
1558
|
+
const block = queue.shift();
|
|
1559
|
+
order.push(block);
|
|
1560
|
+
for (const c of nexts.get(block)) {
|
|
1561
|
+
indeg.set(c.b, indeg.get(c.b) - 1);
|
|
1562
|
+
if (indeg.get(c.b) === 0) queue.push(c.b);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
if (order.length < blocks.length) {
|
|
1566
|
+
const rest = blocks.filter((block) => !order.includes(block));
|
|
1567
|
+
rest.sort((a, b) => pos.get(a) - pos.get(b));
|
|
1568
|
+
order.push(...rest);
|
|
1569
|
+
}
|
|
1570
|
+
for (const block of order) {
|
|
1571
|
+
let want = pos.get(block);
|
|
1572
|
+
for (const c of alive) if (c.b === block) want = Math.max(want, pos.get(c.a) + c.sep);
|
|
1573
|
+
move(block, want);
|
|
1574
|
+
}
|
|
1575
|
+
for (let i = order.length - 1; i >= 0; i -= 1) {
|
|
1576
|
+
const block = order[i];
|
|
1577
|
+
const s = spans.get(block);
|
|
1578
|
+
if (!s) continue;
|
|
1579
|
+
const clamped = Math.max(s.min, Math.min(s.max, pos.get(block)));
|
|
1580
|
+
if (clamped === pos.get(block)) continue;
|
|
1581
|
+
pos.set(block, clamped);
|
|
1582
|
+
for (const c of alive) {
|
|
1583
|
+
if (c.b !== block) continue;
|
|
1584
|
+
pos.set(c.a, Math.min(pos.get(c.a), clamped - c.sep));
|
|
1585
|
+
move(c.a, pos.get(c.a));
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
for (const [block, s] of spans) {
|
|
1589
|
+
const x = Math.max(s.min, Math.min(s.max, pos.get(block)));
|
|
1590
|
+
for (const item of block.items) item.centerX = x - (item.offsetCross || 0);
|
|
1591
|
+
}
|
|
1592
|
+
for (const r of ranksAsc) for (const parent of rankItems.get(r) || []) {
|
|
1593
|
+
const children = balanced.get(parent) || balancedIn.get(parent);
|
|
1594
|
+
const block = children && blockOf.get(parent);
|
|
1595
|
+
if (!block || !spans.has(block)) continue;
|
|
1596
|
+
const half = children.length / 2;
|
|
1597
|
+
const target = (anchorX(children[half - 1]) + anchorX(children[half])) / 2;
|
|
1598
|
+
const s = spans.get(block);
|
|
1599
|
+
let min = s.min;
|
|
1600
|
+
let max = s.max;
|
|
1601
|
+
for (const item of block.items) {
|
|
1602
|
+
const { group, index } = segmentEntry.get(item) || {};
|
|
1603
|
+
if (!group) continue;
|
|
1604
|
+
const prev = group.items.slice(0, index).reverse().find((other) => !block.items.includes(other));
|
|
1605
|
+
const next = group.items.slice(index + 1).find((other) => !block.items.includes(other));
|
|
1606
|
+
const offset = item.offsetCross || 0;
|
|
1607
|
+
if (prev) min = Math.max(min, prev.centerX + prev.bw / 2 + hgap(prev, item) + item.bw / 2 + offset);
|
|
1608
|
+
if (next) max = Math.min(max, next.centerX - next.bw / 2 - hgap(item, next) - item.bw / 2 + offset);
|
|
1609
|
+
}
|
|
1610
|
+
if (min > max + .01) continue;
|
|
1611
|
+
const x = Math.max(min, Math.min(max, target));
|
|
1612
|
+
for (const item of block.items) item.centerX = x - (item.offsetCross || 0);
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
if ([...rankItems.values()].some((items) => {
|
|
1616
|
+
const reals = items.filter((item) => item.kind === "real").map((item) => ({
|
|
1617
|
+
x: item.centerX + (item.offsetCross || 0),
|
|
1618
|
+
w: isLR ? item.th : item.tw
|
|
1619
|
+
})).sort((a, b) => a.x - b.x);
|
|
1620
|
+
return reals.some((item, index) => index > 0 && item.x - reals[index - 1].x < (item.w + reals[index - 1].w) / 2 + 2);
|
|
1621
|
+
})) for (const [item, x] of beforeAlign) item.centerX = x;
|
|
1622
|
+
}
|
|
1623
|
+
const ortho = model.type === "er" ? null : (() => {
|
|
1624
|
+
const clampTo = (value, min, max) => Math.max(min, Math.min(max, value));
|
|
1625
|
+
const R_CORNER = 6;
|
|
1626
|
+
const HOP_R = 5;
|
|
1627
|
+
const PORT_GAP = 16;
|
|
1628
|
+
const SIDE_STUB = 10;
|
|
1629
|
+
const LOOP_EXT = 24;
|
|
1630
|
+
const KAPPA = .5522847498;
|
|
1631
|
+
const isFlowSide = (side) => side === "top" || side === "bottom";
|
|
1632
|
+
const tbRectOf = (item) => {
|
|
1633
|
+
if (item.kind === "real") {
|
|
1634
|
+
const cross = isLR ? item.th : item.tw;
|
|
1635
|
+
const flow = isLR ? item.tw : item.th;
|
|
1636
|
+
return {
|
|
1637
|
+
x: item.centerX + (item.offsetCross || 0) - cross / 2,
|
|
1638
|
+
y: item.cy - flow / 2,
|
|
1639
|
+
w: cross,
|
|
1640
|
+
h: flow
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
return {
|
|
1644
|
+
x: item.centerX - item.bw / 2,
|
|
1645
|
+
y: item.cy - item.bh / 2,
|
|
1646
|
+
w: item.bw,
|
|
1647
|
+
h: item.bh
|
|
1648
|
+
};
|
|
1649
|
+
};
|
|
1650
|
+
const shapeOf = (item) => {
|
|
1651
|
+
const node = item.node || {};
|
|
1652
|
+
if (node.kind === "choice") return "diamond";
|
|
1653
|
+
if (node.kind === "note") return "flow:note";
|
|
1654
|
+
if (node.kind === "start" || node.kind === "end") return "circle";
|
|
1655
|
+
const flowShape = model.type === "flowchart" && flowOutlineName(node.shape);
|
|
1656
|
+
return flowShape ? `flow:${flowShape}` : node.shape || "rect";
|
|
1657
|
+
};
|
|
1658
|
+
const isDiamondNode = (item) => Boolean(item) && [
|
|
1659
|
+
"diamond",
|
|
1660
|
+
"flow:diam",
|
|
1661
|
+
"flow:choice"
|
|
1662
|
+
].includes(shapeOf(item));
|
|
1663
|
+
const outlines = /* @__PURE__ */ new Map();
|
|
1664
|
+
const routedOutline = (shape, w, h) => {
|
|
1665
|
+
const key = `${shape}:${w}:${h}`;
|
|
1666
|
+
if (!outlines.has(key)) {
|
|
1667
|
+
const outline = shapeOutline(shape, isLR ? h : w, isLR ? w : h, direction);
|
|
1668
|
+
outlines.set(key, outline.map(([x, y]) => {
|
|
1669
|
+
if (direction === "BT") return [x, h - y];
|
|
1670
|
+
if (direction === "LR") return [y, x];
|
|
1671
|
+
if (direction === "RL") return [y, h - x];
|
|
1672
|
+
return [x, y];
|
|
1673
|
+
}));
|
|
1674
|
+
}
|
|
1675
|
+
return outlines.get(key);
|
|
1676
|
+
};
|
|
1677
|
+
/** 端口落在边框上;上下边的 cross 是绝对 x,左右边的 cross 是相对 cy 的偏移。 */
|
|
1678
|
+
const borderPort = (rect, shape, side, cross) => {
|
|
1679
|
+
if (shape.startsWith("flow:")) {
|
|
1680
|
+
const offset = isFlowSide(side) ? cross - rect.x : cross + rect.h / 2;
|
|
1681
|
+
const port = outlinePort(routedOutline(shape.slice(5), rect.w, rect.h), side, offset);
|
|
1682
|
+
return {
|
|
1683
|
+
x: rect.x + port.x,
|
|
1684
|
+
y: rect.y + port.y
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
const cx = rect.x + rect.w / 2;
|
|
1688
|
+
const cy = rect.y + rect.h / 2;
|
|
1689
|
+
const hw = rect.w / 2;
|
|
1690
|
+
const hh = rect.h / 2;
|
|
1691
|
+
if (isFlowSide(side)) {
|
|
1692
|
+
const x = clampTo(cross, cx - hw * .96, cx + hw * .96);
|
|
1693
|
+
let y = side === "top" ? rect.y : rect.y + rect.h;
|
|
1694
|
+
if (shape === "diamond") {
|
|
1695
|
+
const dx = Math.min(hw * .96, Math.abs(x - cx));
|
|
1696
|
+
y = cy + (side === "top" ? -1 : 1) * hh * (1 - dx / hw);
|
|
1697
|
+
} else if (shape === "circle") {
|
|
1698
|
+
const dx = Math.min(hw * .96, Math.abs(x - cx));
|
|
1699
|
+
y = cy + (side === "top" ? -1 : 1) * hh * Math.sqrt(Math.max(0, 1 - (dx / hw) ** 2));
|
|
1700
|
+
} else if (shape === "hexagon") {
|
|
1701
|
+
const flat = hw - Math.min(16, hw * .35);
|
|
1702
|
+
const dx = Math.abs(x - cx);
|
|
1703
|
+
if (dx > flat) {
|
|
1704
|
+
const t = Math.min(1, (dx - flat) / Math.max(1, hw - flat));
|
|
1705
|
+
y = cy + (side === "top" ? -1 : 1) * hh * t;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return {
|
|
1709
|
+
x,
|
|
1710
|
+
y
|
|
1711
|
+
};
|
|
1712
|
+
}
|
|
1713
|
+
const dy = clampTo(cross, -hh * .96, hh * .96);
|
|
1714
|
+
let x = side === "left" ? rect.x : rect.x + rect.w;
|
|
1715
|
+
if (shape === "diamond") {
|
|
1716
|
+
const d = Math.min(hh * .82, Math.abs(dy));
|
|
1717
|
+
x = cx + (side === "left" ? -1 : 1) * hw * (1 - d / hh);
|
|
1718
|
+
} else if (shape === "circle") {
|
|
1719
|
+
const d = Math.min(hh * .84, Math.abs(dy));
|
|
1720
|
+
x = cx + (side === "left" ? -1 : 1) * hw * Math.sqrt(Math.max(0, 1 - (d / hh) ** 2));
|
|
1721
|
+
} else if (shape === "hexagon") {
|
|
1722
|
+
const cut = Math.min(16, hh * .35);
|
|
1723
|
+
const t = Math.abs(dy) / Math.max(1, hh);
|
|
1724
|
+
x = side === "left" ? rect.x + cut * t : rect.x + rect.w - cut * t;
|
|
1725
|
+
}
|
|
1726
|
+
return {
|
|
1727
|
+
x,
|
|
1728
|
+
y: cy + dy
|
|
1729
|
+
};
|
|
1730
|
+
};
|
|
1731
|
+
/** 侧向端口在 TB 里的 x:只依赖形状与相对偏移,不依赖所在层的 y。 */
|
|
1732
|
+
const sidePortX = (rect, shape, side, cross) => borderPort({
|
|
1733
|
+
x: rect.x,
|
|
1734
|
+
y: -rect.h / 2,
|
|
1735
|
+
w: rect.w,
|
|
1736
|
+
h: rect.h
|
|
1737
|
+
}, shape, side, cross).x;
|
|
1738
|
+
const frameRectOf = (cluster) => cluster.rect || (cluster.left != null ? {
|
|
1739
|
+
x: cluster.left,
|
|
1740
|
+
y: 0,
|
|
1741
|
+
w: cluster.width || 1,
|
|
1742
|
+
h: 1
|
|
1743
|
+
} : null);
|
|
1744
|
+
const endRect = (end) => end.isFrame ? frameRectOf(end.cluster) : tbRectOf(end.item);
|
|
1745
|
+
const endCenterX = (end) => {
|
|
1746
|
+
const rect = endRect(end);
|
|
1747
|
+
return rect.x + rect.w / 2;
|
|
1748
|
+
};
|
|
1749
|
+
const routes = [];
|
|
1750
|
+
for (const le of layoutEdges) {
|
|
1751
|
+
if (!le.chain || !le.chain.length) continue;
|
|
1752
|
+
const chain = le.chain;
|
|
1753
|
+
const lowItem = chain[0];
|
|
1754
|
+
const highItem = chain[chain.length - 1];
|
|
1755
|
+
const lowIsFrame = le.reversed ? le.toFrame : le.fromFrame;
|
|
1756
|
+
const highIsFrame = le.reversed ? le.fromFrame : le.toFrame;
|
|
1757
|
+
const lowId = le.reversed ? le.edge.to : le.edge.from;
|
|
1758
|
+
const highId = le.reversed ? le.edge.from : le.edge.to;
|
|
1759
|
+
const lowRange = lowIsFrame ? frameRankRanges.get(lowId) : null;
|
|
1760
|
+
const highRange = highIsFrame ? frameRankRanges.get(highId) : null;
|
|
1761
|
+
if (lowIsFrame && !lowRange) continue;
|
|
1762
|
+
if (highIsFrame && !highRange) continue;
|
|
1763
|
+
const makeEnd = (isFrame, id, item, range) => {
|
|
1764
|
+
const cluster = isFrame ? clusters.get(id) : null;
|
|
1765
|
+
if (isFrame && !frameRectOf(cluster)) return null;
|
|
1766
|
+
return {
|
|
1767
|
+
isFrame,
|
|
1768
|
+
id,
|
|
1769
|
+
item,
|
|
1770
|
+
cluster,
|
|
1771
|
+
shape: isFrame ? "rect" : shapeOf(item),
|
|
1772
|
+
rankTop: isFrame ? range.top : item.rank,
|
|
1773
|
+
rankBottom: isFrame ? range.bottom : item.rank
|
|
1774
|
+
};
|
|
1775
|
+
};
|
|
1776
|
+
const lowEnd = makeEnd(lowIsFrame, lowId, lowItem, lowRange);
|
|
1777
|
+
const highEnd = makeEnd(highIsFrame, highId, highItem, highRange);
|
|
1778
|
+
if (!lowEnd || !highEnd) continue;
|
|
1779
|
+
lowEnd.down = true;
|
|
1780
|
+
highEnd.down = false;
|
|
1781
|
+
const insideRange = (rank, range) => rank >= range.top && rank <= range.bottom;
|
|
1782
|
+
const insideForeignFrame = (mid) => {
|
|
1783
|
+
for (const cluster of clusters.values()) {
|
|
1784
|
+
const range = frameRankRanges.get(cluster.id);
|
|
1785
|
+
if (!range) continue;
|
|
1786
|
+
if (isDescendantOf(le.owner, cluster)) continue;
|
|
1787
|
+
if (!insideRange(mid.rank, range)) continue;
|
|
1788
|
+
return true;
|
|
1789
|
+
}
|
|
1790
|
+
return false;
|
|
1791
|
+
};
|
|
1792
|
+
const route = {
|
|
1793
|
+
le,
|
|
1794
|
+
lowEnd,
|
|
1795
|
+
highEnd,
|
|
1796
|
+
mids: chain.slice(1, -1).filter((item) => {
|
|
1797
|
+
if (lowIsFrame && insideRange(item.rank, lowRange)) return false;
|
|
1798
|
+
if (highIsFrame && insideRange(item.rank, highRange)) return false;
|
|
1799
|
+
if (insideForeignFrame(item)) return false;
|
|
1800
|
+
return true;
|
|
1801
|
+
}),
|
|
1802
|
+
kind: le.reversed ? "back" : "forward",
|
|
1803
|
+
srcEnd: le.reversed ? highEnd : lowEnd,
|
|
1804
|
+
tgtEnd: le.reversed ? lowEnd : highEnd,
|
|
1805
|
+
stops: [],
|
|
1806
|
+
lanes: [],
|
|
1807
|
+
tracks: [],
|
|
1808
|
+
seq: le.edge.seq
|
|
1809
|
+
};
|
|
1810
|
+
routes.push(route);
|
|
1811
|
+
}
|
|
1812
|
+
const outRoutes = /* @__PURE__ */ new Map();
|
|
1813
|
+
const inRoutes = /* @__PURE__ */ new Map();
|
|
1814
|
+
const pushMap = (map, key, value) => {
|
|
1815
|
+
if (!map.has(key)) map.set(key, []);
|
|
1816
|
+
map.get(key).push(value);
|
|
1817
|
+
};
|
|
1818
|
+
for (const route of routes) {
|
|
1819
|
+
if (route.kind !== "forward") continue;
|
|
1820
|
+
if (!route.srcEnd.isFrame) pushMap(outRoutes, route.srcEnd.item, route);
|
|
1821
|
+
if (!route.tgtEnd.isFrame) pushMap(inRoutes, route.tgtEnd.item, route);
|
|
1822
|
+
}
|
|
1823
|
+
const plainMids = (route) => route.mids.filter((item) => item.kind !== "label");
|
|
1824
|
+
const adjacentX = (route, role) => {
|
|
1825
|
+
const mids = plainMids(route);
|
|
1826
|
+
if (role === "src") {
|
|
1827
|
+
const mid = mids[0];
|
|
1828
|
+
return mid ? mid.centerX : endCenterX(route.tgtEnd);
|
|
1829
|
+
}
|
|
1830
|
+
const mid = mids[mids.length - 1];
|
|
1831
|
+
return mid ? mid.centerX : endCenterX(route.srcEnd);
|
|
1832
|
+
};
|
|
1833
|
+
const frameSideOf = (frameEnd, otherEnd) => {
|
|
1834
|
+
if (otherEnd.rankBottom < frameEnd.rankTop) return "top";
|
|
1835
|
+
if (otherEnd.rankTop > frameEnd.rankBottom) return "bottom";
|
|
1836
|
+
return endCenterX(otherEnd) < endCenterX(frameEnd) ? "left" : "right";
|
|
1837
|
+
};
|
|
1838
|
+
const frameCross = (frameEnd, side, adjacent) => {
|
|
1839
|
+
if (!isFlowSide(side)) return 0;
|
|
1840
|
+
const rect = endRect(frameEnd);
|
|
1841
|
+
return clampTo(adjacent, rect.x + 12, rect.x + rect.w - 12);
|
|
1842
|
+
};
|
|
1843
|
+
for (const route of routes) {
|
|
1844
|
+
const src = route.srcEnd;
|
|
1845
|
+
const tgt = route.tgtEnd;
|
|
1846
|
+
if (route.kind === "back") {
|
|
1847
|
+
const sideCost = (side) => {
|
|
1848
|
+
const sign = side === "right" ? 1 : -1;
|
|
1849
|
+
let score = 0;
|
|
1850
|
+
for (const end of [src, tgt]) {
|
|
1851
|
+
const gap = end === src ? end.rankTop - .5 : end.rankBottom + .5;
|
|
1852
|
+
const cx = endCenterX(end);
|
|
1853
|
+
for (const other of routes) {
|
|
1854
|
+
if (other.kind !== "forward" || other.srcEnd.rankBottom >= gap || other.tgtEnd.rankTop <= gap) continue;
|
|
1855
|
+
const stops = [
|
|
1856
|
+
{
|
|
1857
|
+
rank: other.srcEnd.rankBottom,
|
|
1858
|
+
x: endCenterX(other.srcEnd)
|
|
1859
|
+
},
|
|
1860
|
+
...plainMids(other).map((mid) => ({
|
|
1861
|
+
rank: mid.rank,
|
|
1862
|
+
x: mid.centerX
|
|
1863
|
+
})),
|
|
1864
|
+
{
|
|
1865
|
+
rank: other.tgtEnd.rankTop,
|
|
1866
|
+
x: endCenterX(other.tgtEnd)
|
|
1867
|
+
}
|
|
1868
|
+
];
|
|
1869
|
+
for (let i = 1; i < stops.length; i += 1) {
|
|
1870
|
+
if (stops[i - 1].rank >= gap || stops[i].rank <= gap) continue;
|
|
1871
|
+
if (Math.max(sign * (stops[i - 1].x - cx), sign * (stops[i].x - cx)) > 8) score += 1;
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
if (side === "right" && end.item?.selfLoops?.length) score += 2;
|
|
1875
|
+
}
|
|
1876
|
+
return score;
|
|
1877
|
+
};
|
|
1878
|
+
const side = sideCost("left") < sideCost("right") ? "left" : "right";
|
|
1879
|
+
src.side = side;
|
|
1880
|
+
tgt.side = side;
|
|
1881
|
+
src.pref = 0;
|
|
1882
|
+
tgt.pref = 0;
|
|
1883
|
+
route.backSide = side;
|
|
1884
|
+
continue;
|
|
1885
|
+
}
|
|
1886
|
+
if (src.isFrame) {
|
|
1887
|
+
src.side = frameSideOf(src, tgt);
|
|
1888
|
+
src.pref = frameCross(src, src.side, adjacentX(route, "src"));
|
|
1889
|
+
} else if (isDiamondNode(src.item)) {
|
|
1890
|
+
const rect = endRect(src);
|
|
1891
|
+
const lane = endCenterX(tgt);
|
|
1892
|
+
if (lane > rect.x + 16 && lane < rect.x + rect.w - 16) {
|
|
1893
|
+
src.side = "bottom";
|
|
1894
|
+
src.hardCross = rect.x + rect.w / 2;
|
|
1895
|
+
src.pref = adjacentX(route, "src");
|
|
1896
|
+
} else {
|
|
1897
|
+
src.side = lane < rect.x + rect.w / 2 ? "left" : "right";
|
|
1898
|
+
src.hardCross = 0;
|
|
1899
|
+
src.pref = 0;
|
|
1900
|
+
}
|
|
1901
|
+
} else {
|
|
1902
|
+
src.side = "bottom";
|
|
1903
|
+
src.pref = adjacentX(route, "src");
|
|
1904
|
+
}
|
|
1905
|
+
if (tgt.isFrame) {
|
|
1906
|
+
tgt.side = frameSideOf(tgt, src);
|
|
1907
|
+
tgt.pref = frameCross(tgt, tgt.side, adjacentX(route, "tgt"));
|
|
1908
|
+
} else if (isDiamondNode(tgt.item)) {
|
|
1909
|
+
const rect = endRect(tgt);
|
|
1910
|
+
const lane = endCenterX(src);
|
|
1911
|
+
const cx = rect.x + rect.w / 2;
|
|
1912
|
+
const srcRect = src.isFrame ? null : endRect(src);
|
|
1913
|
+
if (!!srcRect && isDiamondNode(src.item) && (src.side === "right" && cx > srcRect.x + srcRect.w + 16 || src.side === "left" && cx < srcRect.x - 16) || lane > rect.x + 16 && lane < rect.x + rect.w - 16) {
|
|
1914
|
+
tgt.side = "top";
|
|
1915
|
+
tgt.hardCross = cx;
|
|
1916
|
+
tgt.pref = adjacentX(route, "tgt");
|
|
1917
|
+
} else {
|
|
1918
|
+
tgt.side = lane < rect.x + rect.w / 2 ? "left" : "right";
|
|
1919
|
+
tgt.hardCross = 0;
|
|
1920
|
+
tgt.pref = 0;
|
|
1921
|
+
}
|
|
1922
|
+
} else {
|
|
1923
|
+
tgt.side = "top";
|
|
1924
|
+
tgt.pref = adjacentX(route, "tgt");
|
|
1925
|
+
}
|
|
1926
|
+
}
|
|
1927
|
+
const stubLane = (end) => sidePortX(endRect(end), end.shape, end.side, 0) + (end.side === "left" ? -10 : SIDE_STUB);
|
|
1928
|
+
for (const route of routes) {
|
|
1929
|
+
if (route.kind !== "forward" || plainMids(route).length) continue;
|
|
1930
|
+
if (!isFlowSide(route.srcEnd.side) && route.tgtEnd.side === "top" && route.tgtEnd.hardCross == null) {
|
|
1931
|
+
route.tgtEnd.pref = stubLane(route.srcEnd);
|
|
1932
|
+
route.tgtEnd.outward = route.srcEnd.side === "left" ? -1 : 1;
|
|
1933
|
+
}
|
|
1934
|
+
if (!isFlowSide(route.tgtEnd.side) && route.srcEnd.side === "bottom") route.srcEnd.pref = stubLane(route.tgtEnd);
|
|
1935
|
+
}
|
|
1936
|
+
for (const [item, list] of inRoutes) {
|
|
1937
|
+
const rect = tbRectOf(item);
|
|
1938
|
+
const top = list.filter((route) => route.tgtEnd.side === "top");
|
|
1939
|
+
const capacity = Math.max(1, Math.floor((rect.w - 4) / PORT_GAP) + 1);
|
|
1940
|
+
if (top.length <= capacity) continue;
|
|
1941
|
+
const cx = rect.x + rect.w / 2;
|
|
1942
|
+
top.sort((a, b) => Math.abs(adjacentX(a, "tgt") - cx) - Math.abs(adjacentX(b, "tgt") - cx) || a.seq - b.seq);
|
|
1943
|
+
top.slice(capacity).forEach((route) => {
|
|
1944
|
+
route.tgtEnd.side = adjacentX(route, "tgt") < cx ? "left" : "right";
|
|
1945
|
+
route.tgtEnd.hardCross = 0;
|
|
1946
|
+
route.tgtEnd.pref = 0;
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1949
|
+
/** 端口优先级:数字越小越该保住自己的车道。 */
|
|
1950
|
+
const portPriority = (route, end) => {
|
|
1951
|
+
if (end && end.hardCross != null) return 0;
|
|
1952
|
+
if (end && end.isFrame) return 1;
|
|
1953
|
+
if (route.kind === "back") return 6;
|
|
1954
|
+
const style = route.le.edge.style || "solid";
|
|
1955
|
+
const chain = Boolean(route.mids && route.mids.length);
|
|
1956
|
+
if (style === "solid" || style === "bold") return chain ? 2 : 3;
|
|
1957
|
+
return chain ? 4 : 5;
|
|
1958
|
+
};
|
|
1959
|
+
const requests = [];
|
|
1960
|
+
for (const route of routes) for (const end of [route.srcEnd, route.tgtEnd]) requests.push({
|
|
1961
|
+
route,
|
|
1962
|
+
end,
|
|
1963
|
+
side: end.side,
|
|
1964
|
+
isFrame: end.isFrame,
|
|
1965
|
+
rect: endRect(end),
|
|
1966
|
+
id: end.id,
|
|
1967
|
+
seq: route.seq,
|
|
1968
|
+
prio: portPriority(route, end)
|
|
1969
|
+
});
|
|
1970
|
+
const loops = [];
|
|
1971
|
+
for (const item of realItems) for (const edge of item.selfLoops || []) {
|
|
1972
|
+
const loop = {
|
|
1973
|
+
item,
|
|
1974
|
+
edge,
|
|
1975
|
+
index: loops.length
|
|
1976
|
+
};
|
|
1977
|
+
const rect = tbRectOf(item);
|
|
1978
|
+
const shape = shapeOf(item);
|
|
1979
|
+
loop.labelLines = edge.label ? wrapText(edge.label, 120, FONT.label, measure) : edge.icon ? [""] : [];
|
|
1980
|
+
loop.labelW = loop.labelLines.length ? maxWidthOf(loop.labelLines, FONT.label, measure) + 10 + (edge.icon ? 22 : 0) : 0;
|
|
1981
|
+
loop.labelH = loop.labelLines.length ? Math.max(18, loop.labelLines.length * 16 + 6) : 0;
|
|
1982
|
+
const halfSpan = Math.max(8, (loop.labelH + 6) / 2);
|
|
1983
|
+
loop.upper = {
|
|
1984
|
+
loop,
|
|
1985
|
+
side: "right",
|
|
1986
|
+
isFrame: false,
|
|
1987
|
+
rect,
|
|
1988
|
+
shape,
|
|
1989
|
+
id: item.id,
|
|
1990
|
+
want: -halfSpan,
|
|
1991
|
+
seq: edge.seq,
|
|
1992
|
+
order: 0
|
|
1993
|
+
};
|
|
1994
|
+
loop.lower = {
|
|
1995
|
+
loop,
|
|
1996
|
+
side: "right",
|
|
1997
|
+
isFrame: false,
|
|
1998
|
+
rect,
|
|
1999
|
+
shape,
|
|
2000
|
+
id: item.id,
|
|
2001
|
+
want: halfSpan,
|
|
2002
|
+
seq: edge.seq,
|
|
2003
|
+
order: 1
|
|
2004
|
+
};
|
|
2005
|
+
requests.push(loop.upper, loop.lower);
|
|
2006
|
+
loops.push(loop);
|
|
2007
|
+
}
|
|
2008
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2009
|
+
for (const request of requests) {
|
|
2010
|
+
const key = `${request.isFrame ? "F" : "N"}${request.id}|${request.side}`;
|
|
2011
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
2012
|
+
groups.get(key).push(request);
|
|
2013
|
+
}
|
|
2014
|
+
const endSlots = /* @__PURE__ */ new Map();
|
|
2015
|
+
const placeSide = (list) => {
|
|
2016
|
+
const side = list[0].side;
|
|
2017
|
+
const rect = list[0].rect;
|
|
2018
|
+
const flowSide = isFlowSide(side);
|
|
2019
|
+
const len = flowSide ? rect.w : rect.h;
|
|
2020
|
+
const margin = list[0].isFrame ? 12 : 0;
|
|
2021
|
+
const half = Math.max(0, Math.min((flowSide ? .3 : .4) * len, len / 2 - margin));
|
|
2022
|
+
const center = flowSide ? rect.x + rect.w / 2 : 0;
|
|
2023
|
+
const min = center - half;
|
|
2024
|
+
const max = center + half;
|
|
2025
|
+
const n = list.length;
|
|
2026
|
+
const gap = n > 1 ? Math.min(PORT_GAP, 2 * half / Math.max(1, n - 1)) : PORT_GAP;
|
|
2027
|
+
const items = list.map((request) => ({
|
|
2028
|
+
request,
|
|
2029
|
+
fixed: request.end && request.end.hardCross != null ? request.end.hardCross : null,
|
|
2030
|
+
want: request.end ? request.end.hardCross != null ? request.end.hardCross : request.end.pref == null ? center : request.end.pref : request.want,
|
|
2031
|
+
pos: null,
|
|
2032
|
+
pinned: false
|
|
2033
|
+
}));
|
|
2034
|
+
if (n > 1) {
|
|
2035
|
+
const fixedGroups = /* @__PURE__ */ new Map();
|
|
2036
|
+
for (const item of items) {
|
|
2037
|
+
if (item.fixed == null) continue;
|
|
2038
|
+
const key = Math.round(item.fixed * 2) / 2;
|
|
2039
|
+
fixedGroups.set(key, (fixedGroups.get(key) || []).concat(item));
|
|
2040
|
+
}
|
|
2041
|
+
for (const group of fixedGroups.values()) {
|
|
2042
|
+
if (group.length < 2) continue;
|
|
2043
|
+
for (const item of group) {
|
|
2044
|
+
item.fixed = null;
|
|
2045
|
+
item.want = item.request.end ? item.request.end.pref == null ? center : item.request.end.pref : item.request.want;
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
const fit = (value) => clampTo(value, min, max);
|
|
2050
|
+
const clearOf = (value, self) => items.every((item) => item === self || item.pos == null || Math.abs(item.pos - value) >= gap - .01);
|
|
2051
|
+
const heading = (item) => !flowSide && item.request.end ? item.request.end.down ? 1 : -1 : 0;
|
|
2052
|
+
const towardHeading = (item) => (a, b) => heading(item) ? (b - a) * heading(item) : a - b;
|
|
2053
|
+
for (const item of items) {
|
|
2054
|
+
if (item.fixed == null) continue;
|
|
2055
|
+
item.pos = fit(item.fixed);
|
|
2056
|
+
item.pinned = true;
|
|
2057
|
+
}
|
|
2058
|
+
const ranked = items.filter((item) => item.fixed == null).sort((a, b) => (a.request.prio ?? 3) - (b.request.prio ?? 3) || a.want - b.want || a.request.seq - b.request.seq || (a.request.order ?? 0) - (b.request.order ?? 0));
|
|
2059
|
+
for (const item of ranked) {
|
|
2060
|
+
if (item.want < min - .01 || item.want > max + .01) continue;
|
|
2061
|
+
if (!clearOf(item.want, item)) continue;
|
|
2062
|
+
item.pos = item.want;
|
|
2063
|
+
item.pinned = true;
|
|
2064
|
+
}
|
|
2065
|
+
let crowded = false;
|
|
2066
|
+
for (const item of ranked) {
|
|
2067
|
+
if (item.pos != null) continue;
|
|
2068
|
+
const slots = [
|
|
2069
|
+
fit(item.want),
|
|
2070
|
+
min,
|
|
2071
|
+
max
|
|
2072
|
+
];
|
|
2073
|
+
for (const other of items) {
|
|
2074
|
+
if (other.pos == null) continue;
|
|
2075
|
+
slots.push(fit(other.pos - gap), fit(other.pos + gap));
|
|
2076
|
+
}
|
|
2077
|
+
let open = slots.filter((value) => clearOf(value, item));
|
|
2078
|
+
const dir = item.request.end && item.request.end.outward;
|
|
2079
|
+
if (dir) {
|
|
2080
|
+
const vertex = item.want - dir * SIDE_STUB;
|
|
2081
|
+
const crosses = (value) => items.some((other) => other !== item && other.pos != null && (other.pos - item.want) * dir > .01 && (value - other.pos) * dir > .01);
|
|
2082
|
+
const ahead = open.filter((value) => (value - item.want) * dir >= -.01);
|
|
2083
|
+
const clean = ahead.filter((value) => !crosses(value));
|
|
2084
|
+
const behind = open.filter((value) => (value - item.want) * dir < -.01 && (value - vertex) * dir >= 3.99);
|
|
2085
|
+
if (clean.length) open = clean;
|
|
2086
|
+
else if (behind.length) open = behind;
|
|
2087
|
+
else if (ahead.length) open = ahead;
|
|
2088
|
+
}
|
|
2089
|
+
if (!open.length) {
|
|
2090
|
+
crowded = true;
|
|
2091
|
+
break;
|
|
2092
|
+
}
|
|
2093
|
+
open.sort((a, b) => Math.abs(a - item.want) - Math.abs(b - item.want) || towardHeading(item)(a, b));
|
|
2094
|
+
item.pos = open[0];
|
|
2095
|
+
}
|
|
2096
|
+
if (crowded) {
|
|
2097
|
+
const order = items.slice().sort((a, b) => a.want - b.want || heading(a) - heading(b) || a.request.seq - b.request.seq);
|
|
2098
|
+
const packedHalf = Math.min(half, PORT_GAP * (n - 1) / 2);
|
|
2099
|
+
order.forEach((item, index) => {
|
|
2100
|
+
item.pos = n === 1 ? fit(center) : center - packedHalf + 2 * packedHalf * index / (n - 1);
|
|
2101
|
+
});
|
|
2102
|
+
}
|
|
2103
|
+
for (const item of items) if (item.pos == null) item.pos = fit(item.want);
|
|
2104
|
+
const clashes = /* @__PURE__ */ new Map();
|
|
2105
|
+
for (const item of items) {
|
|
2106
|
+
const key = Math.round(item.pos);
|
|
2107
|
+
clashes.set(key, (clashes.get(key) || []).concat(item));
|
|
2108
|
+
}
|
|
2109
|
+
for (const group of clashes.values()) {
|
|
2110
|
+
if (group.length < 2) continue;
|
|
2111
|
+
const step = Math.min(PORT_GAP, 2 * half / Math.max(1, group.length - 1));
|
|
2112
|
+
const start = center - step * (group.length - 1) / 2;
|
|
2113
|
+
group.slice().sort((a, b) => a.want - b.want || heading(a) - heading(b) || a.request.seq - b.request.seq).forEach((item, order) => {
|
|
2114
|
+
item.pos = fit(start + order * step);
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
if (!flowSide && n > 1) {
|
|
2118
|
+
const sideX = side === "left" ? rect.x : rect.x + rect.w;
|
|
2119
|
+
const laneOf = (item) => {
|
|
2120
|
+
const { route, end, loop } = item.request;
|
|
2121
|
+
if (loop || !route || route.kind !== "forward") return null;
|
|
2122
|
+
return Math.abs((end === route.srcEnd ? adjacentX(route, "src") : adjacentX(route, "tgt")) - sideX);
|
|
2123
|
+
};
|
|
2124
|
+
for (const dir of [-1, 1]) {
|
|
2125
|
+
const group = items.filter((item) => item.fixed == null && heading(item) === dir && laneOf(item) != null);
|
|
2126
|
+
if (group.length < 2) continue;
|
|
2127
|
+
const slots = group.map((item) => item.pos).sort((a, b) => (a - b) * dir);
|
|
2128
|
+
group.sort((a, b) => laneOf(b) - laneOf(a) || a.request.seq - b.request.seq).forEach((item, index) => {
|
|
2129
|
+
item.pos = slots[index];
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
if (flowSide && n > 1 && items.some((a) => items.some((b) => a.want < b.want - .5 && a.pos > b.pos + .01))) {
|
|
2134
|
+
const order = items.slice().sort((a, b) => a.want - b.want || a.request.seq - b.request.seq || (a.request.order ?? 0) - (b.request.order ?? 0));
|
|
2135
|
+
const runs = [];
|
|
2136
|
+
let run = [];
|
|
2137
|
+
let lo = min;
|
|
2138
|
+
for (const item of order) {
|
|
2139
|
+
if (item.fixed == null) {
|
|
2140
|
+
run.push(item);
|
|
2141
|
+
continue;
|
|
2142
|
+
}
|
|
2143
|
+
runs.push({
|
|
2144
|
+
items: run,
|
|
2145
|
+
lo,
|
|
2146
|
+
hi: item.pos - gap
|
|
2147
|
+
});
|
|
2148
|
+
run = [];
|
|
2149
|
+
lo = item.pos + gap;
|
|
2150
|
+
}
|
|
2151
|
+
runs.push({
|
|
2152
|
+
items: run,
|
|
2153
|
+
lo,
|
|
2154
|
+
hi: max
|
|
2155
|
+
});
|
|
2156
|
+
if (runs.some((part) => part.items.length && part.lo + (part.items.length - 1) * gap > part.hi + .01)) {
|
|
2157
|
+
const packedHalf = Math.min(half, gap * (n - 1) / 2);
|
|
2158
|
+
order.forEach((item, index) => {
|
|
2159
|
+
item.pos = center - packedHalf + 2 * packedHalf * index / (n - 1);
|
|
2160
|
+
});
|
|
2161
|
+
} else for (const part of runs) {
|
|
2162
|
+
const count = part.items.length;
|
|
2163
|
+
if (!count) continue;
|
|
2164
|
+
const pools = [];
|
|
2165
|
+
part.items.forEach((item, index) => {
|
|
2166
|
+
pools.push({
|
|
2167
|
+
sum: item.want - index * gap,
|
|
2168
|
+
count: 1
|
|
2169
|
+
});
|
|
2170
|
+
while (pools.length > 1) {
|
|
2171
|
+
const last = pools[pools.length - 1];
|
|
2172
|
+
const prev = pools[pools.length - 2];
|
|
2173
|
+
if (prev.sum / prev.count <= last.sum / last.count) break;
|
|
2174
|
+
pools.pop();
|
|
2175
|
+
prev.sum += last.sum;
|
|
2176
|
+
prev.count += last.count;
|
|
2177
|
+
}
|
|
2178
|
+
});
|
|
2179
|
+
const top = part.hi - (count - 1) * gap;
|
|
2180
|
+
let index = 0;
|
|
2181
|
+
for (const pool of pools) {
|
|
2182
|
+
const base = Math.max(part.lo, Math.min(top, pool.sum / pool.count));
|
|
2183
|
+
for (let k = 0; k < pool.count; k += 1, index += 1) part.items[index].pos = base + index * gap;
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
for (const item of items) {
|
|
2188
|
+
item.request.pos = item.pos;
|
|
2189
|
+
if (item.request.end) {
|
|
2190
|
+
item.request.end.pos = item.pos;
|
|
2191
|
+
endSlots.set(item.request.end, {
|
|
2192
|
+
items,
|
|
2193
|
+
gap,
|
|
2194
|
+
min,
|
|
2195
|
+
max,
|
|
2196
|
+
item
|
|
2197
|
+
});
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
};
|
|
2201
|
+
for (const list of groups.values()) placeSide(list);
|
|
2202
|
+
const JOG_MIN = 16.5;
|
|
2203
|
+
const relocate = (end, value) => {
|
|
2204
|
+
const slot = endSlots.get(end);
|
|
2205
|
+
if (!slot || end.isFrame || end.hardCross != null || !isFlowSide(end.side)) return false;
|
|
2206
|
+
if (!Number.isFinite(value) || value < slot.min - .01 || value > slot.max + .01) return false;
|
|
2207
|
+
for (const other of slot.items) {
|
|
2208
|
+
if (other === slot.item || other.pos == null) continue;
|
|
2209
|
+
if (Math.abs(other.pos - value) < slot.gap - .01) return false;
|
|
2210
|
+
if (other.want < slot.item.want - .5 && other.pos > value || other.want > slot.item.want + .5 && other.pos < value) return false;
|
|
2211
|
+
}
|
|
2212
|
+
slot.item.pos = value;
|
|
2213
|
+
end.pos = value;
|
|
2214
|
+
return true;
|
|
2215
|
+
};
|
|
2216
|
+
const straighten = (route) => {
|
|
2217
|
+
if (route.kind !== "forward" || route.lanes.length < 2) return false;
|
|
2218
|
+
const last = route.lanes.length - 1;
|
|
2219
|
+
const pairs = [{
|
|
2220
|
+
end: route.srcEnd,
|
|
2221
|
+
lane: route.lanes[1],
|
|
2222
|
+
mate: route.tgtEnd
|
|
2223
|
+
}, {
|
|
2224
|
+
end: route.tgtEnd,
|
|
2225
|
+
lane: route.lanes[last - 1],
|
|
2226
|
+
mate: route.srcEnd
|
|
2227
|
+
}];
|
|
2228
|
+
let moved = false;
|
|
2229
|
+
for (const { end, lane, mate } of pairs) {
|
|
2230
|
+
if (!isFlowSide(end.side)) continue;
|
|
2231
|
+
const delta = lane - end.pos;
|
|
2232
|
+
if (Math.abs(delta) < .01) continue;
|
|
2233
|
+
if (relocate(end, lane)) {
|
|
2234
|
+
moved = true;
|
|
2235
|
+
continue;
|
|
2236
|
+
}
|
|
2237
|
+
if (Math.abs(delta) >= 16.49) continue;
|
|
2238
|
+
if (relocate(end, end.pos - Math.sign(delta) * (JOG_MIN - Math.abs(delta)))) {
|
|
2239
|
+
moved = true;
|
|
2240
|
+
continue;
|
|
2241
|
+
}
|
|
2242
|
+
if (route.lanes.length === 2 && relocate(mate, end.pos)) moved = true;
|
|
2243
|
+
}
|
|
2244
|
+
return moved;
|
|
2245
|
+
};
|
|
2246
|
+
const laneXOfEnd = (end) => {
|
|
2247
|
+
if (isFlowSide(end.side)) return end.pos;
|
|
2248
|
+
return sidePortX(endRect(end), end.shape, end.side, end.pos) + (end.side === "left" ? -10 : SIDE_STUB);
|
|
2249
|
+
};
|
|
2250
|
+
const backSlots = [];
|
|
2251
|
+
const channelFor = (route) => {
|
|
2252
|
+
const side = route.backSide;
|
|
2253
|
+
const lo = Math.min(route.tgtEnd.rankTop, route.srcEnd.rankBottom);
|
|
2254
|
+
const hi = Math.max(route.tgtEnd.rankBottom, route.srcEnd.rankTop);
|
|
2255
|
+
let edgeX = side === "right" ? -Infinity : Infinity;
|
|
2256
|
+
for (const item of allItems) {
|
|
2257
|
+
if (item.rank < lo || item.rank > hi) continue;
|
|
2258
|
+
const rect = tbRectOf(item);
|
|
2259
|
+
if (!Number.isFinite(rect.x) || !Number.isFinite(rect.w)) continue;
|
|
2260
|
+
edgeX = side === "right" ? Math.max(edgeX, rect.x + rect.w) : Math.min(edgeX, rect.x);
|
|
2261
|
+
}
|
|
2262
|
+
for (const cluster of clusters.values()) {
|
|
2263
|
+
const frameRect = frameRectOf(cluster);
|
|
2264
|
+
if (!frameRect || !Number.isFinite(frameRect.x) || !Number.isFinite(frameRect.w)) continue;
|
|
2265
|
+
if (cluster.bottomRank < lo || cluster.topRank > hi) continue;
|
|
2266
|
+
edgeX = side === "right" ? Math.max(edgeX, frameRect.x + frameRect.w) : Math.min(edgeX, frameRect.x);
|
|
2267
|
+
}
|
|
2268
|
+
if (!Number.isFinite(edgeX)) edgeX = 0;
|
|
2269
|
+
const need = Math.max(12, route.mids.reduce((best, mid) => mid.kind === "label" ? Math.max(best, mid.bw / 2 + 2) : best, 0));
|
|
2270
|
+
let x = edgeX + (side === "right" ? 14 : -14);
|
|
2271
|
+
for (let pass = 0; pass < 8; pass += 1) {
|
|
2272
|
+
let moved = false;
|
|
2273
|
+
for (const slot of backSlots) {
|
|
2274
|
+
if (slot.side !== side || slot.hi <= lo || slot.lo >= hi) continue;
|
|
2275
|
+
if (Math.abs(slot.x - x) < need + slot.need) {
|
|
2276
|
+
x = slot.x + (side === "right" ? 1 : -1) * (need + slot.need);
|
|
2277
|
+
moved = true;
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
if (!moved) break;
|
|
2281
|
+
}
|
|
2282
|
+
backSlots.push({
|
|
2283
|
+
side,
|
|
2284
|
+
lo,
|
|
2285
|
+
hi,
|
|
2286
|
+
x,
|
|
2287
|
+
need
|
|
2288
|
+
});
|
|
2289
|
+
return x;
|
|
2290
|
+
};
|
|
2291
|
+
const needsLabelLane = (route, item) => {
|
|
2292
|
+
const a = laneXOfEnd(route.srcEnd);
|
|
2293
|
+
const b = laneXOfEnd(route.tgtEnd);
|
|
2294
|
+
const detour = Math.max(0, item.centerX - Math.max(a, b)) + Math.max(0, Math.min(a, b) - item.centerX);
|
|
2295
|
+
if (!routes.some((other) => other !== route && other.lowEnd.id === route.lowEnd.id && other.highEnd.id === route.highEnd.id) && detour > item.bw * 2) return false;
|
|
2296
|
+
if (Math.abs(a - b) < item.bw + 2 && detour <= item.bw && routes.some((other) => other.kind === "back" && other.tgtEnd.rankBottom < item.rank && other.srcEnd.rankTop > item.rank)) return true;
|
|
2297
|
+
for (const end of [route.srcEnd, route.tgtEnd]) {
|
|
2298
|
+
if (!isFlowSide(end.side)) continue;
|
|
2299
|
+
for (const other of routes) {
|
|
2300
|
+
if (other === route || other.kind !== "forward") continue;
|
|
2301
|
+
for (const mate of [other.srcEnd, other.tgtEnd]) {
|
|
2302
|
+
if (mate.id !== end.id || mate.side !== end.side) continue;
|
|
2303
|
+
if (Math.abs(mate.pos - end.pos) < item.bw / 2 + 5) return true;
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
return false;
|
|
2308
|
+
};
|
|
2309
|
+
for (const route of routes) if (route.kind === "forward") route.stops = [
|
|
2310
|
+
{ end: route.srcEnd },
|
|
2311
|
+
...route.mids.filter((item) => item.kind !== "label" || needsLabelLane(route, item)).map((item) => ({ mid: item })),
|
|
2312
|
+
{ end: route.tgtEnd }
|
|
2313
|
+
];
|
|
2314
|
+
else {
|
|
2315
|
+
route.channelX = channelFor(route);
|
|
2316
|
+
route.stops = [
|
|
2317
|
+
{ end: route.srcEnd },
|
|
2318
|
+
{ channel: route.channelX },
|
|
2319
|
+
{ end: route.tgtEnd }
|
|
2320
|
+
];
|
|
2321
|
+
}
|
|
2322
|
+
const assignLanes = () => {
|
|
2323
|
+
for (const route of routes) {
|
|
2324
|
+
route.lanes = route.stops.map((stop) => {
|
|
2325
|
+
if (stop.end) return laneXOfEnd(stop.end);
|
|
2326
|
+
if (stop.mid) return stop.mid.centerX;
|
|
2327
|
+
return stop.channel;
|
|
2328
|
+
});
|
|
2329
|
+
if (route.kind === "forward" && route.lanes.length === 2 && !isFlowSide(route.srcEnd.side) && !isFlowSide(route.tgtEnd.side)) {
|
|
2330
|
+
const [a, b] = route.lanes;
|
|
2331
|
+
if ((route.srcEnd.side === "right" ? b > a : b < a) && Math.abs(a - b) < PORT_GAP) route.lanes[0] = b;
|
|
2332
|
+
}
|
|
2333
|
+
if (route.kind === "forward" && route.lanes.length === 2 && !isFlowSide(route.srcEnd.side) && isFlowSide(route.tgtEnd.side) && !route.srcEnd.isFrame) {
|
|
2334
|
+
const [a, b] = route.lanes;
|
|
2335
|
+
const outward = route.srcEnd.side === "right" ? b > a : b < a;
|
|
2336
|
+
const lo = Math.min(a, b);
|
|
2337
|
+
const hi = Math.max(a, b);
|
|
2338
|
+
const vertex = route.tgtEnd.side === "top" && !route.tgtEnd.isFrame && isDiamondNode(route.tgtEnd.item);
|
|
2339
|
+
const blocked = (vertex ? allItems : realItems).some((item) => {
|
|
2340
|
+
if (item === route.srcEnd.item || item.rank !== route.srcEnd.item.rank) return false;
|
|
2341
|
+
const rect = tbRectOf(item);
|
|
2342
|
+
return rect.x < hi + 8 && rect.x + rect.w > lo - 8;
|
|
2343
|
+
});
|
|
2344
|
+
if (outward && (hi - lo <= 32 || vertex) && !blocked) route.lanes[0] = b;
|
|
2345
|
+
const sign = route.srcEnd.side === "right" ? 1 : -1;
|
|
2346
|
+
const vertexX = a - sign * SIDE_STUB;
|
|
2347
|
+
if (!outward && (b - vertexX) * sign >= 3.99) route.lanes[0] = b;
|
|
2348
|
+
}
|
|
2349
|
+
if (route.kind === "forward" && route.lanes.length >= 2 && isFlowSide(route.srcEnd.side) && !isFlowSide(route.tgtEnd.side) && !route.tgtEnd.isFrame) {
|
|
2350
|
+
const last = route.lanes.length - 1;
|
|
2351
|
+
const a = route.lanes[last - 1];
|
|
2352
|
+
const b = route.lanes[last];
|
|
2353
|
+
const outward = route.tgtEnd.side === "right" ? a > b : a < b;
|
|
2354
|
+
const lo = Math.min(a, b);
|
|
2355
|
+
const hi = Math.max(a, b);
|
|
2356
|
+
const blocked = allItems.some((item) => {
|
|
2357
|
+
if (item === route.tgtEnd.item || item.rank !== route.tgtEnd.item.rank) return false;
|
|
2358
|
+
const rect = tbRectOf(item);
|
|
2359
|
+
return rect.x < hi + 8 && rect.x + rect.w > lo - 8;
|
|
2360
|
+
});
|
|
2361
|
+
if (outward && !blocked) route.lanes[last] = a;
|
|
2362
|
+
}
|
|
2363
|
+
if (route.kind === "back") {
|
|
2364
|
+
for (const index of [0, route.lanes.length - 1]) if (Math.abs(route.lanes[index] - route.channelX) < PORT_GAP) route.lanes[index] = route.channelX;
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
for (const route of routes) {
|
|
2368
|
+
if (route.kind !== "forward") continue;
|
|
2369
|
+
const interior = route.stops.slice(1, -1);
|
|
2370
|
+
if (!interior.length || interior.some((stop) => !stop.mid || stop.mid.kind !== "dummy")) continue;
|
|
2371
|
+
const old = route.lanes[1];
|
|
2372
|
+
if (route.lanes.slice(1, -1).some((x) => Math.abs(x - old) > .01)) continue;
|
|
2373
|
+
const choices = [route.lanes.at(-1), route.lanes[0]];
|
|
2374
|
+
for (const x of choices) {
|
|
2375
|
+
if (Math.abs(x - old) < .01 || Math.abs(x - old) >= PORT_GAP) continue;
|
|
2376
|
+
const blocked = realItems.some((item) => {
|
|
2377
|
+
if (item.rank <= route.srcEnd.rankBottom || item.rank >= route.tgtEnd.rankTop) return false;
|
|
2378
|
+
const rect = tbRectOf(item);
|
|
2379
|
+
return x > rect.x - 8 && x < rect.x + rect.w + 8;
|
|
2380
|
+
});
|
|
2381
|
+
const outside = interior.some(({ mid }) => mid.cluster && mid.cluster !== root && (x < mid.slotLeft || x > mid.slotRight));
|
|
2382
|
+
if (blocked || outside) continue;
|
|
2383
|
+
for (let i = 1; i < route.lanes.length - 1; i += 1) route.lanes[i] = x;
|
|
2384
|
+
break;
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
};
|
|
2388
|
+
assignLanes();
|
|
2389
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
2390
|
+
let moved = false;
|
|
2391
|
+
for (const route of routes) if (straighten(route)) moved = true;
|
|
2392
|
+
if (!moved) break;
|
|
2393
|
+
assignLanes();
|
|
2394
|
+
}
|
|
2395
|
+
const gapTracks = /* @__PURE__ */ new Map();
|
|
2396
|
+
const trackAt = (gap, route, index) => {
|
|
2397
|
+
if (!Number.isFinite(gap) || gap < 0 || gap > maxRank - 1) return null;
|
|
2398
|
+
if (!gapTracks.has(gap)) gapTracks.set(gap, []);
|
|
2399
|
+
const track = {
|
|
2400
|
+
gap,
|
|
2401
|
+
route,
|
|
2402
|
+
index,
|
|
2403
|
+
y: null
|
|
2404
|
+
};
|
|
2405
|
+
gapTracks.get(gap).push(track);
|
|
2406
|
+
return track;
|
|
2407
|
+
};
|
|
2408
|
+
const gapBelow = (route, stop) => {
|
|
2409
|
+
if (stop.end) {
|
|
2410
|
+
if (stop.end.isFrame) return stop.end === route.srcEnd ? stop.end.rankBottom : stop.end.rankTop - 1;
|
|
2411
|
+
return stop.end.item.rank;
|
|
2412
|
+
}
|
|
2413
|
+
return stop.mid.rank;
|
|
2414
|
+
};
|
|
2415
|
+
for (const route of routes) {
|
|
2416
|
+
route.tracks = [];
|
|
2417
|
+
if (route.kind === "forward") for (let i = 0; i + 1 < route.lanes.length; i += 1) {
|
|
2418
|
+
if (Math.abs(route.lanes[i + 1] - route.lanes[i]) < .01) {
|
|
2419
|
+
route.tracks.push(null);
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
route.tracks.push(trackAt(gapBelow(route, route.stops[i]), route, i));
|
|
2423
|
+
}
|
|
2424
|
+
else {
|
|
2425
|
+
const srcGap = route.srcEnd.rankTop - 1;
|
|
2426
|
+
const tgtGap = route.tgtEnd.rankBottom;
|
|
2427
|
+
let srcTrack;
|
|
2428
|
+
let tgtTrack;
|
|
2429
|
+
if (srcGap === tgtGap) {
|
|
2430
|
+
tgtTrack = trackAt(tgtGap, route, 1);
|
|
2431
|
+
srcTrack = trackAt(srcGap, route, 0);
|
|
2432
|
+
} else {
|
|
2433
|
+
srcTrack = trackAt(srcGap, route, 0);
|
|
2434
|
+
tgtTrack = trackAt(tgtGap, route, 1);
|
|
2435
|
+
}
|
|
2436
|
+
route.tracks = [srcTrack, tgtTrack];
|
|
2437
|
+
if (srcGap === tgtGap && srcTrack && tgtTrack) srcTrack.after = tgtTrack;
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
const trackShape = (track) => {
|
|
2441
|
+
const { route, index } = track;
|
|
2442
|
+
const lanes = route.lanes;
|
|
2443
|
+
if (route.kind === "forward") return {
|
|
2444
|
+
up: lanes[index],
|
|
2445
|
+
down: lanes[index + 1]
|
|
2446
|
+
};
|
|
2447
|
+
return index === 0 ? {
|
|
2448
|
+
up: lanes[1],
|
|
2449
|
+
down: lanes[0]
|
|
2450
|
+
} : {
|
|
2451
|
+
up: lanes[2],
|
|
2452
|
+
down: lanes[1]
|
|
2453
|
+
};
|
|
2454
|
+
};
|
|
2455
|
+
const orderTracks = (list) => {
|
|
2456
|
+
const n = list.length;
|
|
2457
|
+
if (n < 2) return;
|
|
2458
|
+
const shapes = list.map((track) => {
|
|
2459
|
+
const { up, down } = trackShape(track);
|
|
2460
|
+
return {
|
|
2461
|
+
up,
|
|
2462
|
+
down,
|
|
2463
|
+
lo: Math.min(up, down),
|
|
2464
|
+
hi: Math.max(up, down)
|
|
2465
|
+
};
|
|
2466
|
+
});
|
|
2467
|
+
const inside = (x, shape) => x > shape.lo + .5 && x < shape.hi - .5;
|
|
2468
|
+
const cost = shapes.map((a) => shapes.map((b) => a === b ? 0 : Number(inside(b.up, a)) + Number(inside(a.down, b))));
|
|
2469
|
+
const position = new Map(list.map((track, index) => [track, index]));
|
|
2470
|
+
const must = list.map((track) => track.after && position.has(track.after) ? position.get(track.after) : -1);
|
|
2471
|
+
const total = (order) => order.reduce((sum, a, i) => sum + order.slice(i + 1).reduce((acc, b) => acc + cost[a][b], 0), 0);
|
|
2472
|
+
const identity = list.map((_, index) => index);
|
|
2473
|
+
if (total(identity) === 0) return;
|
|
2474
|
+
let order;
|
|
2475
|
+
if (n <= 12) {
|
|
2476
|
+
const full = (1 << n) - 1;
|
|
2477
|
+
const best = new Float64Array(1 << n).fill(Infinity);
|
|
2478
|
+
best[full] = 0;
|
|
2479
|
+
const placeable = (set, t) => !(set & 1 << t) && (must[t] < 0 || set & 1 << must[t]);
|
|
2480
|
+
const added = (set, t) => {
|
|
2481
|
+
let sum = 0;
|
|
2482
|
+
for (let s = 0; s < n; s += 1) if (set & 1 << s) sum += cost[s][t];
|
|
2483
|
+
return sum;
|
|
2484
|
+
};
|
|
2485
|
+
for (let set = full - 1; set >= 0; set -= 1) for (let t = 0; t < n; t += 1) {
|
|
2486
|
+
if (!placeable(set, t)) continue;
|
|
2487
|
+
best[set] = Math.min(best[set], added(set, t) + best[set | 1 << t]);
|
|
2488
|
+
}
|
|
2489
|
+
order = [];
|
|
2490
|
+
for (let set = 0; set !== full;) {
|
|
2491
|
+
let next = -1;
|
|
2492
|
+
for (let t = 0; t < n && next < 0; t += 1) if (placeable(set, t) && added(set, t) + best[set | 1 << t] === best[set]) next = t;
|
|
2493
|
+
if (next < 0) return;
|
|
2494
|
+
order.push(next);
|
|
2495
|
+
set |= 1 << next;
|
|
2496
|
+
}
|
|
2497
|
+
} else {
|
|
2498
|
+
order = identity.slice();
|
|
2499
|
+
for (let pass = 0, swapped = true; swapped && pass < n; pass += 1) {
|
|
2500
|
+
swapped = false;
|
|
2501
|
+
for (let i = 0; i + 1 < order.length; i += 1) {
|
|
2502
|
+
const a = order[i];
|
|
2503
|
+
const b = order[i + 1];
|
|
2504
|
+
if (must[b] === a || cost[b][a] >= cost[a][b]) continue;
|
|
2505
|
+
order[i] = b;
|
|
2506
|
+
order[i + 1] = a;
|
|
2507
|
+
swapped = true;
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
if (total(order) >= total(identity)) return;
|
|
2512
|
+
const sorted = order.map((index) => list[index]);
|
|
2513
|
+
list.splice(0, n, ...sorted);
|
|
2514
|
+
};
|
|
2515
|
+
for (const list of gapTracks.values()) orderTracks(list);
|
|
2516
|
+
const lineSeg = (a, b) => ({
|
|
2517
|
+
p0: a,
|
|
2518
|
+
p1: b,
|
|
2519
|
+
c1: {
|
|
2520
|
+
x: a.x + (b.x - a.x) / 3,
|
|
2521
|
+
y: a.y + (b.y - a.y) / 3
|
|
2522
|
+
},
|
|
2523
|
+
c2: {
|
|
2524
|
+
x: a.x + (b.x - a.x) * 2 / 3,
|
|
2525
|
+
y: a.y + (b.y - a.y) * 2 / 3
|
|
2526
|
+
},
|
|
2527
|
+
kind: "line"
|
|
2528
|
+
});
|
|
2529
|
+
const arcSeg = (a, b, c1, c2, kind) => ({
|
|
2530
|
+
p0: a,
|
|
2531
|
+
p1: b,
|
|
2532
|
+
c1,
|
|
2533
|
+
c2,
|
|
2534
|
+
kind
|
|
2535
|
+
});
|
|
2536
|
+
const simplifyPoints = (points) => {
|
|
2537
|
+
const out = [];
|
|
2538
|
+
for (const point of points) {
|
|
2539
|
+
const last = out[out.length - 1];
|
|
2540
|
+
if (last && Math.abs(last.x - point.x) < .01 && Math.abs(last.y - point.y) < .01) continue;
|
|
2541
|
+
out.push(point);
|
|
2542
|
+
while (out.length >= 3) {
|
|
2543
|
+
const c = out[out.length - 1];
|
|
2544
|
+
const b = out[out.length - 2];
|
|
2545
|
+
const a = out[out.length - 3];
|
|
2546
|
+
const sameX = Math.abs(a.x - b.x) < .01 && Math.abs(b.x - c.x) < .01;
|
|
2547
|
+
const sameY = Math.abs(a.y - b.y) < .01 && Math.abs(b.y - c.y) < .01;
|
|
2548
|
+
if (!sameX && !sameY) break;
|
|
2549
|
+
out.splice(out.length - 2, 1);
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
return out;
|
|
2553
|
+
};
|
|
2554
|
+
const runsOf = (points) => {
|
|
2555
|
+
const runs = [];
|
|
2556
|
+
for (let i = 0; i + 1 < points.length; i += 1) {
|
|
2557
|
+
const a = points[i];
|
|
2558
|
+
const b = points[i + 1];
|
|
2559
|
+
const horizontal = Math.abs(a.y - b.y) < .01;
|
|
2560
|
+
const vertical = Math.abs(a.x - b.x) < .01;
|
|
2561
|
+
if (!horizontal && !vertical) continue;
|
|
2562
|
+
if (Math.hypot(b.x - a.x, b.y - a.y) < .01) continue;
|
|
2563
|
+
runs.push({
|
|
2564
|
+
horizontal,
|
|
2565
|
+
a,
|
|
2566
|
+
b
|
|
2567
|
+
});
|
|
2568
|
+
}
|
|
2569
|
+
return runs;
|
|
2570
|
+
};
|
|
2571
|
+
/** 折线 → 三次段:直线段三等分,拐角 6px 圆角,跨线点拱起 HOP_R。 */
|
|
2572
|
+
const segmentsOf = (points, hops) => {
|
|
2573
|
+
const out = [];
|
|
2574
|
+
const pushStraight = (a, b) => {
|
|
2575
|
+
const len = Math.hypot(b.x - a.x, b.y - a.y);
|
|
2576
|
+
if (len < .01) return;
|
|
2577
|
+
if (!(Math.abs(a.y - b.y) < .01) || !hops || !hops.length) {
|
|
2578
|
+
out.push(lineSeg(a, b));
|
|
2579
|
+
return;
|
|
2580
|
+
}
|
|
2581
|
+
const dir = Math.sign(b.x - a.x) || 1;
|
|
2582
|
+
const along = (p) => (p.x - a.x) * dir;
|
|
2583
|
+
const list = hops.filter((hop) => Math.abs(hop.y - a.y) < .01 && along(hop) > 11 && along(hop) < len - HOP_R - 6).sort((p, q) => along(p) - along(q));
|
|
2584
|
+
let cursor = a;
|
|
2585
|
+
for (const hop of list) {
|
|
2586
|
+
if (along(cursor) > along(hop) - HOP_R - 1) continue;
|
|
2587
|
+
const before = {
|
|
2588
|
+
x: hop.x - dir * HOP_R,
|
|
2589
|
+
y: hop.y
|
|
2590
|
+
};
|
|
2591
|
+
const after = {
|
|
2592
|
+
x: hop.x + dir * HOP_R,
|
|
2593
|
+
y: hop.y
|
|
2594
|
+
};
|
|
2595
|
+
const top = {
|
|
2596
|
+
x: hop.x,
|
|
2597
|
+
y: hop.y - HOP_R
|
|
2598
|
+
};
|
|
2599
|
+
out.push(lineSeg(cursor, before));
|
|
2600
|
+
out.push(arcSeg(before, top, {
|
|
2601
|
+
x: before.x,
|
|
2602
|
+
y: before.y - HOP_R * KAPPA
|
|
2603
|
+
}, {
|
|
2604
|
+
x: top.x - dir * HOP_R * KAPPA,
|
|
2605
|
+
y: top.y
|
|
2606
|
+
}, "jump"));
|
|
2607
|
+
out.push(arcSeg(top, after, {
|
|
2608
|
+
x: top.x + dir * HOP_R * KAPPA,
|
|
2609
|
+
y: top.y
|
|
2610
|
+
}, {
|
|
2611
|
+
x: after.x,
|
|
2612
|
+
y: after.y - HOP_R * KAPPA
|
|
2613
|
+
}, "jump"));
|
|
2614
|
+
cursor = after;
|
|
2615
|
+
}
|
|
2616
|
+
if (Math.hypot(b.x - cursor.x, b.y - cursor.y) > .01) out.push(lineSeg(cursor, b));
|
|
2617
|
+
};
|
|
2618
|
+
const pts = simplifyPoints(points);
|
|
2619
|
+
if (pts.length < 2) return out;
|
|
2620
|
+
let cursor = pts[0];
|
|
2621
|
+
for (let i = 1; i + 1 < pts.length; i += 1) {
|
|
2622
|
+
const p = pts[i];
|
|
2623
|
+
const q = pts[i + 1];
|
|
2624
|
+
const inLen = Math.hypot(p.x - cursor.x, p.y - cursor.y);
|
|
2625
|
+
const outLen = Math.hypot(q.x - p.x, q.y - p.y);
|
|
2626
|
+
if (inLen < .01 || outLen < .01) continue;
|
|
2627
|
+
const r = Math.min(R_CORNER, inLen / 2, outLen / 2);
|
|
2628
|
+
if (r < .5) {
|
|
2629
|
+
pushStraight(cursor, p);
|
|
2630
|
+
cursor = p;
|
|
2631
|
+
continue;
|
|
2632
|
+
}
|
|
2633
|
+
const uIn = {
|
|
2634
|
+
x: (p.x - cursor.x) / inLen,
|
|
2635
|
+
y: (p.y - cursor.y) / inLen
|
|
2636
|
+
};
|
|
2637
|
+
const uOut = {
|
|
2638
|
+
x: (q.x - p.x) / outLen,
|
|
2639
|
+
y: (q.y - p.y) / outLen
|
|
2640
|
+
};
|
|
2641
|
+
const a = {
|
|
2642
|
+
x: p.x - uIn.x * r,
|
|
2643
|
+
y: p.y - uIn.y * r
|
|
2644
|
+
};
|
|
2645
|
+
const b = {
|
|
2646
|
+
x: p.x + uOut.x * r,
|
|
2647
|
+
y: p.y + uOut.y * r
|
|
2648
|
+
};
|
|
2649
|
+
pushStraight(cursor, a);
|
|
2650
|
+
out.push(arcSeg(a, b, {
|
|
2651
|
+
x: a.x + uIn.x * r * KAPPA,
|
|
2652
|
+
y: a.y + uIn.y * r * KAPPA
|
|
2653
|
+
}, {
|
|
2654
|
+
x: b.x - uOut.x * r * KAPPA,
|
|
2655
|
+
y: b.y - uOut.y * r * KAPPA
|
|
2656
|
+
}, "corner"));
|
|
2657
|
+
cursor = b;
|
|
2658
|
+
}
|
|
2659
|
+
pushStraight(cursor, pts[pts.length - 1]);
|
|
2660
|
+
return out;
|
|
2661
|
+
};
|
|
2662
|
+
const tbPortPoint = (end, other) => {
|
|
2663
|
+
const rect = endRect(end);
|
|
2664
|
+
if (end.isFrame && !isFlowSide(end.side)) {
|
|
2665
|
+
const otherRect = other ? endRect(other) : null;
|
|
2666
|
+
const wantY = otherRect ? otherRect.y + otherRect.h / 2 : rect.y + rect.h / 2;
|
|
2667
|
+
const half = Math.max(0, rect.h / 2 - 16);
|
|
2668
|
+
return borderPort(rect, end.shape, end.side, clampTo(wantY - (rect.y + rect.h / 2), -half, half));
|
|
2669
|
+
}
|
|
2670
|
+
return borderPort(rect, end.shape, end.side, end.pos);
|
|
2671
|
+
};
|
|
2672
|
+
const stopY = (stop, other) => {
|
|
2673
|
+
if (stop.end) return tbPortPoint(stop.end, other).y;
|
|
2674
|
+
if (stop.mid) return stop.mid.cy;
|
|
2675
|
+
return null;
|
|
2676
|
+
};
|
|
2677
|
+
/** 最后一道保险:端口被边框限制后与车道错开时插拐点,保证不出现斜线 */
|
|
2678
|
+
const axisAlign = (points) => {
|
|
2679
|
+
const out = [];
|
|
2680
|
+
for (const point of points) {
|
|
2681
|
+
const last = out[out.length - 1];
|
|
2682
|
+
if (last && Math.abs(last.x - point.x) > .01 && Math.abs(last.y - point.y) > .01) out.push({
|
|
2683
|
+
x: last.x,
|
|
2684
|
+
y: point.y
|
|
2685
|
+
});
|
|
2686
|
+
out.push(point);
|
|
2687
|
+
}
|
|
2688
|
+
return out;
|
|
2689
|
+
};
|
|
2690
|
+
/** 端口 / 虚拟点 → 直角折线;横移一律走本层间的轨道(R6)。 */
|
|
2691
|
+
const pathOf = (route) => {
|
|
2692
|
+
const pts = [];
|
|
2693
|
+
const start = tbPortPoint(route.srcEnd, route.tgtEnd);
|
|
2694
|
+
const end = tbPortPoint(route.tgtEnd, route.srcEnd);
|
|
2695
|
+
pts.push(start);
|
|
2696
|
+
let cursor = route.lanes[0];
|
|
2697
|
+
if (!isFlowSide(route.srcEnd.side)) pts.push({
|
|
2698
|
+
x: cursor,
|
|
2699
|
+
y: start.y
|
|
2700
|
+
});
|
|
2701
|
+
for (let i = 0; i + 1 < route.lanes.length; i += 1) {
|
|
2702
|
+
const next = Math.abs(route.lanes[i + 1] - cursor) < 1 ? cursor : route.lanes[i + 1];
|
|
2703
|
+
if (Math.abs(next - cursor) > .01) {
|
|
2704
|
+
const track = route.tracks[i];
|
|
2705
|
+
let y = track ? track.y : null;
|
|
2706
|
+
if (y == null) {
|
|
2707
|
+
const otherOf = (stop) => stop.end ? stop.end === route.srcEnd ? route.tgtEnd : route.srcEnd : null;
|
|
2708
|
+
const a = stopY(route.stops[i], otherOf(route.stops[i]));
|
|
2709
|
+
const b = stopY(route.stops[i + 1], otherOf(route.stops[i + 1]));
|
|
2710
|
+
y = a != null && b != null ? (a + b) / 2 : (a ?? b ?? 0) + 20;
|
|
2711
|
+
}
|
|
2712
|
+
pts.push({
|
|
2713
|
+
x: cursor,
|
|
2714
|
+
y
|
|
2715
|
+
});
|
|
2716
|
+
pts.push({
|
|
2717
|
+
x: next,
|
|
2718
|
+
y
|
|
2719
|
+
});
|
|
2720
|
+
}
|
|
2721
|
+
cursor = next;
|
|
2722
|
+
}
|
|
2723
|
+
if (!isFlowSide(route.tgtEnd.side)) pts.push({
|
|
2724
|
+
x: cursor,
|
|
2725
|
+
y: end.y
|
|
2726
|
+
});
|
|
2727
|
+
else if (Math.abs(end.x - cursor) > .01 && Math.abs(end.x - cursor) < 1.5) end.x = cursor;
|
|
2728
|
+
pts.push(end);
|
|
2729
|
+
return simplifyPoints(axisAlign(pts));
|
|
2730
|
+
};
|
|
2731
|
+
const tbTitleRect = (cluster) => {
|
|
2732
|
+
const vr = cluster.visualRect;
|
|
2733
|
+
if (!vr) return null;
|
|
2734
|
+
const titleH = cluster.titleLines.length * 17 + (cluster.descLines.length ? 4 + cluster.descLines.length * 16 : 0);
|
|
2735
|
+
const rect = {
|
|
2736
|
+
x: vr.x + FRAME_PAD,
|
|
2737
|
+
y: vr.y + 10,
|
|
2738
|
+
w: Math.max(cluster.titleW, cluster.descW || 0, 1),
|
|
2739
|
+
h: Math.max(titleH, 1)
|
|
2740
|
+
};
|
|
2741
|
+
if (direction === "BT") return {
|
|
2742
|
+
x: rect.x,
|
|
2743
|
+
y: H - rect.y - rect.h,
|
|
2744
|
+
w: rect.w,
|
|
2745
|
+
h: rect.h
|
|
2746
|
+
};
|
|
2747
|
+
if (direction === "LR") return {
|
|
2748
|
+
x: rect.y,
|
|
2749
|
+
y: rect.x,
|
|
2750
|
+
w: rect.h,
|
|
2751
|
+
h: rect.w
|
|
2752
|
+
};
|
|
2753
|
+
if (direction === "RL") return {
|
|
2754
|
+
x: rect.y,
|
|
2755
|
+
y: W - rect.x - rect.w,
|
|
2756
|
+
w: rect.h,
|
|
2757
|
+
h: rect.w
|
|
2758
|
+
};
|
|
2759
|
+
return rect;
|
|
2760
|
+
};
|
|
2761
|
+
const hitsRun = (rect, points) => runsOf(points).some((run) => {
|
|
2762
|
+
const x1 = Math.min(run.a.x, run.b.x);
|
|
2763
|
+
const x2 = Math.max(run.a.x, run.b.x);
|
|
2764
|
+
const y1 = Math.min(run.a.y, run.b.y);
|
|
2765
|
+
const y2 = Math.max(run.a.y, run.b.y);
|
|
2766
|
+
if (x2 < rect.x + .5 || x1 > rect.x + rect.w - .5) return false;
|
|
2767
|
+
if (y2 < rect.y + .5 || y1 > rect.y + rect.h - .5) return false;
|
|
2768
|
+
return true;
|
|
2769
|
+
});
|
|
2770
|
+
return {
|
|
2771
|
+
routes,
|
|
2772
|
+
loops,
|
|
2773
|
+
gapTracks,
|
|
2774
|
+
build: () => {
|
|
2775
|
+
for (const route of routes) route.points = pathOf(route);
|
|
2776
|
+
const loopResults = loops.map((loop) => {
|
|
2777
|
+
const rect = tbRectOf(loop.item);
|
|
2778
|
+
const shape = shapeOf(loop.item);
|
|
2779
|
+
const upper = borderPort(rect, shape, "right", loop.upper.pos ?? -8);
|
|
2780
|
+
const lower = borderPort(rect, shape, "right", loop.lower.pos ?? 8);
|
|
2781
|
+
const ext = Math.max(LOOP_EXT, loop.labelW / 2 + 12);
|
|
2782
|
+
const outerX = Math.max(rect.x + rect.w + ext, upper.x + ext, lower.x + ext);
|
|
2783
|
+
const points = simplifyPoints([
|
|
2784
|
+
upper,
|
|
2785
|
+
{
|
|
2786
|
+
x: outerX,
|
|
2787
|
+
y: upper.y
|
|
2788
|
+
},
|
|
2789
|
+
{
|
|
2790
|
+
x: outerX,
|
|
2791
|
+
y: lower.y
|
|
2792
|
+
},
|
|
2793
|
+
lower
|
|
2794
|
+
]);
|
|
2795
|
+
const lines = loop.labelLines;
|
|
2796
|
+
let labelRect = null;
|
|
2797
|
+
if (lines.length) labelRect = {
|
|
2798
|
+
x: outerX - loop.labelW / 2,
|
|
2799
|
+
y: (upper.y + lower.y) / 2 - loop.labelH / 2,
|
|
2800
|
+
w: loop.labelW,
|
|
2801
|
+
h: loop.labelH
|
|
2802
|
+
};
|
|
2803
|
+
loop.points = points;
|
|
2804
|
+
loop.labelRect = labelRect;
|
|
2805
|
+
return loop;
|
|
2806
|
+
});
|
|
2807
|
+
const runList = [];
|
|
2808
|
+
for (const route of routes) for (const run of runsOf(route.points)) runList.push({
|
|
2809
|
+
run,
|
|
2810
|
+
key: route
|
|
2811
|
+
});
|
|
2812
|
+
for (const loop of loopResults) for (const run of runsOf(loop.points)) runList.push({
|
|
2813
|
+
run,
|
|
2814
|
+
key: loop
|
|
2815
|
+
});
|
|
2816
|
+
const hopsOf = /* @__PURE__ */ new Map();
|
|
2817
|
+
const allEnds = [];
|
|
2818
|
+
for (const entry of runList) allEnds.push(entry.run.a, entry.run.b);
|
|
2819
|
+
for (const { run, key } of runList) {
|
|
2820
|
+
if (!run.horizontal) continue;
|
|
2821
|
+
const lo = Math.min(run.a.x, run.b.x);
|
|
2822
|
+
const hi = Math.max(run.a.x, run.b.x);
|
|
2823
|
+
const y = run.a.y;
|
|
2824
|
+
const list = [];
|
|
2825
|
+
for (const other of runList) {
|
|
2826
|
+
if (other.key === key || other.run.horizontal) continue;
|
|
2827
|
+
const x = other.run.a.x;
|
|
2828
|
+
if (x < lo + HOP_R + 14 || x > hi - HOP_R - 14) continue;
|
|
2829
|
+
const y1 = Math.min(other.run.a.y, other.run.b.y);
|
|
2830
|
+
const y2 = Math.max(other.run.a.y, other.run.b.y);
|
|
2831
|
+
if (y < y1 + HOP_R + 10 || y > y2 - HOP_R - 10) continue;
|
|
2832
|
+
if (allEnds.some((point) => Math.hypot(point.x - x, point.y - y) < 20)) continue;
|
|
2833
|
+
list.push({
|
|
2834
|
+
x,
|
|
2835
|
+
y
|
|
2836
|
+
});
|
|
2837
|
+
}
|
|
2838
|
+
if (!list.length) continue;
|
|
2839
|
+
const previous = hopsOf.get(key) || [];
|
|
2840
|
+
hopsOf.set(key, previous.concat(list));
|
|
2841
|
+
}
|
|
2842
|
+
const routeBySeq = new Map(routes.map((route) => [route.le.edge.seq, route]));
|
|
2843
|
+
const labelRoute = /* @__PURE__ */ new Map();
|
|
2844
|
+
for (const item of allItems) {
|
|
2845
|
+
if (item.kind !== "label") continue;
|
|
2846
|
+
const route = routeBySeq.get(item.edge.seq);
|
|
2847
|
+
if (route) labelRoute.set(item, route);
|
|
2848
|
+
}
|
|
2849
|
+
const labelRects = /* @__PURE__ */ new Map();
|
|
2850
|
+
const cubicAt = (segment, t) => {
|
|
2851
|
+
const u = 1 - t;
|
|
2852
|
+
const a = u * u * u;
|
|
2853
|
+
const b = 3 * u * u * t;
|
|
2854
|
+
const c = 3 * u * t * t;
|
|
2855
|
+
const d = t * t * t;
|
|
2856
|
+
return {
|
|
2857
|
+
x: a * segment.p0.x + b * segment.c1.x + c * segment.c2.x + d * segment.p1.x,
|
|
2858
|
+
y: a * segment.p0.y + b * segment.c1.y + c * segment.c2.y + d * segment.p1.y
|
|
2859
|
+
};
|
|
2860
|
+
};
|
|
2861
|
+
const renderedByRoute = new Map(routes.map((route) => [route, segmentsOf(route.points, hopsOf.get(route) || [])]));
|
|
2862
|
+
const frameRects = [...clusters.values()].map((cluster) => cluster.rect).filter(Boolean);
|
|
2863
|
+
const titleRects = [];
|
|
2864
|
+
for (const cluster of clusters.values()) {
|
|
2865
|
+
const title = tbTitleRect(cluster);
|
|
2866
|
+
if (title) titleRects.push(title);
|
|
2867
|
+
}
|
|
2868
|
+
const fits = (item, route, rect) => {
|
|
2869
|
+
for (const node of realItems) if (boxesOverlap(rect, tbRectOf(node), 2)) return false;
|
|
2870
|
+
for (const [other, otherRect] of labelRects) if (other !== item && boxesOverlap(rect, otherRect, 3)) return false;
|
|
2871
|
+
for (const loop of loopResults) if (loop.labelRect && boxesOverlap(rect, loop.labelRect, 3)) return false;
|
|
2872
|
+
for (const title of titleRects) if (boxesOverlap(rect, title, 2)) return false;
|
|
2873
|
+
for (const frame of frameRects) {
|
|
2874
|
+
if (!boxesOverlap(rect, frame, 1)) continue;
|
|
2875
|
+
if (!(rect.x >= frame.x - .5 && rect.y >= frame.y - .5 && rect.x + rect.w <= frame.x + frame.w + .5 && rect.y + rect.h <= frame.y + frame.h + .5)) return false;
|
|
2876
|
+
}
|
|
2877
|
+
for (const other of routes) {
|
|
2878
|
+
if (other === route) continue;
|
|
2879
|
+
if (hitsRun(rect, other.points)) return false;
|
|
2880
|
+
for (const segment of renderedByRoute.get(other) || []) {
|
|
2881
|
+
if (segment.kind === "line") continue;
|
|
2882
|
+
for (let i = 0; i <= 12; i += 1) {
|
|
2883
|
+
const point = cubicAt(segment, i / 12);
|
|
2884
|
+
if (point.x > rect.x - 1 && point.x < rect.x + rect.w + 1 && point.y > rect.y - 1 && point.y < rect.y + rect.h + 1) return false;
|
|
2885
|
+
}
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
for (const loop of loopResults) if (hitsRun(rect, loop.points)) return false;
|
|
2889
|
+
return true;
|
|
2890
|
+
};
|
|
2891
|
+
for (const [item, route] of labelRoute) {
|
|
2892
|
+
const width = item.bw;
|
|
2893
|
+
const height = item.bh;
|
|
2894
|
+
const pref = {
|
|
2895
|
+
x: route.kind === "back" ? route.channelX : item.centerX,
|
|
2896
|
+
y: item.cy
|
|
2897
|
+
};
|
|
2898
|
+
let best = null;
|
|
2899
|
+
let bestDistance = Infinity;
|
|
2900
|
+
let fallback = null;
|
|
2901
|
+
let fallbackDistance = Infinity;
|
|
2902
|
+
const corners = route.points.slice(1, -1);
|
|
2903
|
+
for (const run of runsOf(route.points)) {
|
|
2904
|
+
const len = Math.hypot(run.b.x - run.a.x, run.b.y - run.a.y);
|
|
2905
|
+
const steps = Math.max(1, Math.ceil(len / 6));
|
|
2906
|
+
for (let step = 0; step <= steps; step += 1) {
|
|
2907
|
+
const t = step / steps;
|
|
2908
|
+
const px = run.a.x + (run.b.x - run.a.x) * t;
|
|
2909
|
+
const py = run.a.y + (run.b.y - run.a.y) * t;
|
|
2910
|
+
const nearCorner = corners.some((corner) => Math.hypot(corner.x - px, corner.y - py) < R_CORNER + height / 2 + 2);
|
|
2911
|
+
const distance = Math.hypot(px - pref.x, py - pref.y) + (nearCorner ? 14 : 0);
|
|
2912
|
+
const candidate = {
|
|
2913
|
+
x: px - width / 2,
|
|
2914
|
+
y: py - height / 2,
|
|
2915
|
+
w: width,
|
|
2916
|
+
h: height
|
|
2917
|
+
};
|
|
2918
|
+
if (distance < fallbackDistance) {
|
|
2919
|
+
fallback = candidate;
|
|
2920
|
+
fallbackDistance = distance;
|
|
2921
|
+
}
|
|
2922
|
+
if (distance >= bestDistance) continue;
|
|
2923
|
+
if (!fits(item, route, candidate)) continue;
|
|
2924
|
+
best = candidate;
|
|
2925
|
+
bestDistance = distance;
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
const chosen = best || fallback;
|
|
2929
|
+
if (!chosen) continue;
|
|
2930
|
+
const segments = renderedByRoute.get(route) || [];
|
|
2931
|
+
let nearestPoint = null;
|
|
2932
|
+
let nearestDistance = Infinity;
|
|
2933
|
+
for (const segment of segments) {
|
|
2934
|
+
const span = Math.hypot(segment.p1.x - segment.p0.x, segment.p1.y - segment.p0.y);
|
|
2935
|
+
const steps = Math.max(8, Math.min(80, Math.ceil(span / 2)));
|
|
2936
|
+
for (let step = 0; step <= steps; step += 1) {
|
|
2937
|
+
const point = cubicAt(segment, step / steps);
|
|
2938
|
+
const distance = Math.hypot(point.x - (chosen.x + width / 2), point.y - (chosen.y + height / 2));
|
|
2939
|
+
if (distance < nearestDistance) {
|
|
2940
|
+
nearestDistance = distance;
|
|
2941
|
+
nearestPoint = point;
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
if (nearestPoint && nearestDistance > .05 && nearestDistance < 8) {
|
|
2946
|
+
chosen.x += nearestPoint.x - (chosen.x + width / 2);
|
|
2947
|
+
chosen.y += nearestPoint.y - (chosen.y + height / 2);
|
|
2948
|
+
}
|
|
2949
|
+
labelRects.set(item, chosen);
|
|
2950
|
+
}
|
|
2951
|
+
for (const [item, route] of labelRoute) {
|
|
2952
|
+
const rect = labelRects.get(item);
|
|
2953
|
+
if (!rect) continue;
|
|
2954
|
+
const segments = renderedByRoute.get(route) || [];
|
|
2955
|
+
let point = null;
|
|
2956
|
+
let distance = Infinity;
|
|
2957
|
+
for (const segment of segments) {
|
|
2958
|
+
const span = Math.hypot(segment.p1.x - segment.p0.x, segment.p1.y - segment.p0.y);
|
|
2959
|
+
const steps = Math.max(8, Math.min(80, Math.ceil(span / 2)));
|
|
2960
|
+
for (let step = 0; step <= steps; step += 1) {
|
|
2961
|
+
const at = cubicAt(segment, step / steps);
|
|
2962
|
+
const gap = Math.hypot(at.x - (rect.x + rect.w / 2), at.y - (rect.y + rect.h / 2));
|
|
2963
|
+
if (gap < distance) {
|
|
2964
|
+
distance = gap;
|
|
2965
|
+
point = at;
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
if (!point || distance <= .05 || distance > 24) continue;
|
|
2970
|
+
rect.x += point.x - (rect.x + rect.w / 2);
|
|
2971
|
+
rect.y += point.y - (rect.y + rect.h / 2);
|
|
2972
|
+
}
|
|
2973
|
+
return {
|
|
2974
|
+
routes: routes.map((route) => ({
|
|
2975
|
+
le: route.le,
|
|
2976
|
+
start: route.points[0],
|
|
2977
|
+
end: route.points[route.points.length - 1],
|
|
2978
|
+
points: route.points,
|
|
2979
|
+
segments: segmentsOf(route.points, hopsOf.get(route) || [])
|
|
2980
|
+
})),
|
|
2981
|
+
loops: loopResults.map((loop) => ({
|
|
2982
|
+
edge: loop.edge,
|
|
2983
|
+
item: loop.item,
|
|
2984
|
+
start: loop.points[0],
|
|
2985
|
+
end: loop.points[loop.points.length - 1],
|
|
2986
|
+
points: loop.points,
|
|
2987
|
+
segments: segmentsOf(loop.points, hopsOf.get(loop) || []),
|
|
2988
|
+
labelRect: loop.labelRect,
|
|
2989
|
+
labelLines: loop.labelLines
|
|
2990
|
+
})),
|
|
2991
|
+
labels: labelRects
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
};
|
|
2995
|
+
})();
|
|
2996
|
+
const rankHeight = (r) => (rankItems.get(r) || []).reduce((best, item) => Math.max(best, item.bh), 0);
|
|
2997
|
+
const rankReal = (r) => (rankItems.get(r) || []).some((item) => item.kind === "real");
|
|
2998
|
+
const rankLabel = (r) => (rankItems.get(r) || []).some((item) => item.kind === "label");
|
|
2999
|
+
const ER_STUB = 18;
|
|
3000
|
+
const baseGap = (a, b) => {
|
|
3001
|
+
if (model.type === "er" && (rankReal(a) || rankReal(b))) return rankReal(a) && rankReal(b) ? 60 : 38;
|
|
3002
|
+
if (rankReal(a) && rankReal(b)) return 34;
|
|
3003
|
+
if (rankReal(a) || rankReal(b)) return 20;
|
|
3004
|
+
if (rankLabel(a) || rankLabel(b)) return 16;
|
|
3005
|
+
return 16;
|
|
3006
|
+
};
|
|
3007
|
+
const topExt = (cluster) => {
|
|
3008
|
+
if (cluster === root) return 0;
|
|
3009
|
+
if (isLR) return 12;
|
|
3010
|
+
return cluster.titleBand;
|
|
3011
|
+
};
|
|
3012
|
+
const bottomExt = (cluster) => cluster === root ? 0 : 12;
|
|
3013
|
+
for (const cluster of clusters.values()) {
|
|
3014
|
+
const items = realItems.filter((item) => isDescendantOf(item.cluster, cluster));
|
|
3015
|
+
if (!items.length) {
|
|
3016
|
+
cluster.topRank = Infinity;
|
|
3017
|
+
cluster.bottomRank = -Infinity;
|
|
3018
|
+
continue;
|
|
3019
|
+
}
|
|
3020
|
+
cluster.topRank = Math.min(...items.map((item) => item.rank));
|
|
3021
|
+
cluster.bottomRank = Math.max(...items.map((item) => item.rank));
|
|
3022
|
+
}
|
|
3023
|
+
const stackTop = (cluster) => {
|
|
3024
|
+
let nested = 0;
|
|
3025
|
+
for (const child of cluster.children) if (child.topRank === cluster.topRank && Number.isFinite(child.topRank)) nested = Math.max(nested, stackTop(child));
|
|
3026
|
+
return topExt(cluster) + nested;
|
|
3027
|
+
};
|
|
3028
|
+
const stackBottom = (cluster) => {
|
|
3029
|
+
let nested = 0;
|
|
3030
|
+
for (const child of cluster.children) if (child.bottomRank === cluster.bottomRank && Number.isFinite(child.bottomRank)) nested = Math.max(nested, stackBottom(child));
|
|
3031
|
+
return bottomExt(cluster) + nested;
|
|
3032
|
+
};
|
|
3033
|
+
const gapExtra = /* @__PURE__ */ new Map();
|
|
3034
|
+
for (let r = 0; r < maxRank; r += 1) {
|
|
3035
|
+
const topNeed = [...clusters.values()].filter((cluster) => cluster.topRank === r + 1).reduce((best, cluster) => Math.max(best, stackTop(cluster)), 0);
|
|
3036
|
+
const bottomNeed = [...clusters.values()].filter((cluster) => cluster.bottomRank === r).reduce((best, cluster) => Math.max(best, stackBottom(cluster)), 0);
|
|
3037
|
+
gapExtra.set(r, Math.max(topNeed, bottomNeed));
|
|
3038
|
+
}
|
|
3039
|
+
const boundaryPairs = /* @__PURE__ */ new Map();
|
|
3040
|
+
for (const le of layoutEdges) {
|
|
3041
|
+
if (!le.fromItem || !le.toItem) continue;
|
|
3042
|
+
const from = Math.min(le.fromItem.rank, le.toItem.rank);
|
|
3043
|
+
const to = Math.max(le.fromItem.rank, le.toItem.rank);
|
|
3044
|
+
const fromId = le.edge.from;
|
|
3045
|
+
const toId = le.edge.to;
|
|
3046
|
+
const key = fromId < toId ? `${fromId}<->${toId}` : `${toId}<->${fromId}`;
|
|
3047
|
+
for (let r = from; r < to; r += 1) {
|
|
3048
|
+
if (!boundaryPairs.has(r)) boundaryPairs.set(r, /* @__PURE__ */ new Map());
|
|
3049
|
+
const counts = boundaryPairs.get(r);
|
|
3050
|
+
counts.set(key, (counts.get(key) || 0) + 1);
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
for (const [r, counts] of boundaryPairs) {
|
|
3054
|
+
const maxParallel = Math.max(...counts.values());
|
|
3055
|
+
if (maxParallel <= 3) continue;
|
|
3056
|
+
gapExtra.set(r, Math.max(gapExtra.get(r) || 0, (maxParallel - 3) * 24));
|
|
3057
|
+
}
|
|
3058
|
+
const y = /* @__PURE__ */ new Map([[0, 0]]);
|
|
3059
|
+
for (let r = 1; r <= maxRank; r += 1) y.set(r, y.get(r - 1) + rankHeight(r - 1) / 2 + baseGap(r - 1, r) + (gapExtra.get(r - 1) || 0) + rankHeight(r) / 2);
|
|
3060
|
+
for (const item of allItems) item.cy = y.get(item.rank);
|
|
3061
|
+
const computeFrameRect = (cluster) => {
|
|
3062
|
+
for (const child of cluster.children) computeFrameRect(child);
|
|
3063
|
+
if (cluster === root) {
|
|
3064
|
+
cluster.rect = null;
|
|
3065
|
+
return;
|
|
3066
|
+
}
|
|
3067
|
+
const items = allItems.filter((item) => isDescendantOf(item.cluster, cluster));
|
|
3068
|
+
if (!items.length) {
|
|
3069
|
+
cluster.rect = null;
|
|
3070
|
+
return;
|
|
3071
|
+
}
|
|
3072
|
+
let top = Math.min(...items.map((item) => item.cy - item.bh / 2));
|
|
3073
|
+
let bottom = Math.max(...items.map((item) => item.cy + item.bh / 2));
|
|
3074
|
+
for (const child of cluster.children) {
|
|
3075
|
+
if (!child.rect) continue;
|
|
3076
|
+
top = Math.min(top, child.rect.y);
|
|
3077
|
+
bottom = Math.max(bottom, child.rect.y + child.rect.h);
|
|
3078
|
+
}
|
|
3079
|
+
cluster.rect = {
|
|
3080
|
+
x: cluster.left,
|
|
3081
|
+
y: top - topExt(cluster),
|
|
3082
|
+
w: cluster.width,
|
|
3083
|
+
h: bottom + bottomExt(cluster) - (top - topExt(cluster))
|
|
3084
|
+
};
|
|
3085
|
+
};
|
|
3086
|
+
computeFrameRect(root);
|
|
3087
|
+
if (ortho) {
|
|
3088
|
+
const trackWindow = (gap) => {
|
|
3089
|
+
let lo = -Infinity;
|
|
3090
|
+
let hi = Infinity;
|
|
3091
|
+
for (const item of rankItems.get(gap) || []) lo = Math.max(lo, item.cy + item.bh / 2);
|
|
3092
|
+
for (const item of rankItems.get(gap + 1) || []) hi = Math.min(hi, item.cy - item.bh / 2);
|
|
3093
|
+
for (const cluster of clusters.values()) {
|
|
3094
|
+
if (!cluster.rect) continue;
|
|
3095
|
+
if (cluster.topRank === gap + 1) hi = Math.min(hi, cluster.rect.y - 6);
|
|
3096
|
+
if (cluster.bottomRank === gap) lo = Math.max(lo, cluster.rect.y + cluster.rect.h + 6);
|
|
3097
|
+
}
|
|
3098
|
+
return {
|
|
3099
|
+
lo,
|
|
3100
|
+
hi
|
|
3101
|
+
};
|
|
3102
|
+
};
|
|
3103
|
+
const neededOf = (list) => (list.length - 1) * 12 + 32;
|
|
3104
|
+
for (let pass = 0; pass < 10; pass += 1) {
|
|
3105
|
+
let grown = false;
|
|
3106
|
+
for (let gap = 0; gap < maxRank; gap += 1) {
|
|
3107
|
+
const list = ortho.gapTracks.get(gap);
|
|
3108
|
+
if (!list || !list.length) continue;
|
|
3109
|
+
const window = trackWindow(gap);
|
|
3110
|
+
const need = neededOf(list);
|
|
3111
|
+
if (window.hi - window.lo >= need - 1e-6) continue;
|
|
3112
|
+
gapExtra.set(gap, (gapExtra.get(gap) || 0) + (need - (window.hi - window.lo)) + 2);
|
|
3113
|
+
grown = true;
|
|
3114
|
+
y.set(0, 0);
|
|
3115
|
+
for (let r = 1; r <= maxRank; r += 1) y.set(r, y.get(r - 1) + rankHeight(r - 1) / 2 + baseGap(r - 1, r) + (gapExtra.get(r - 1) || 0) + rankHeight(r) / 2);
|
|
3116
|
+
for (const item of allItems) item.cy = y.get(item.rank);
|
|
3117
|
+
computeFrameRect(root);
|
|
3118
|
+
}
|
|
3119
|
+
if (!grown) break;
|
|
3120
|
+
}
|
|
3121
|
+
const gapWindow = /* @__PURE__ */ new Map();
|
|
3122
|
+
for (const [gap, list] of ortho.gapTracks) {
|
|
3123
|
+
const window = trackWindow(gap);
|
|
3124
|
+
gapWindow.set(gap, window);
|
|
3125
|
+
const step = Math.max(12, Math.min(16, (window.hi - window.lo - 32) / Math.max(1, list.length - 1)));
|
|
3126
|
+
const span = (list.length - 1) * step;
|
|
3127
|
+
const start = window.lo + Math.max(16, (window.hi - window.lo - span) / 2);
|
|
3128
|
+
list.forEach((track, index) => {
|
|
3129
|
+
track.y = start + index * step;
|
|
3130
|
+
});
|
|
3131
|
+
}
|
|
3132
|
+
const allTracks = [];
|
|
3133
|
+
for (const [gap, list] of ortho.gapTracks) for (const track of list) allTracks.push({
|
|
3134
|
+
gap,
|
|
3135
|
+
track
|
|
3136
|
+
});
|
|
3137
|
+
allTracks.sort((a, b) => a.track.y - b.track.y);
|
|
3138
|
+
for (let i = 1; i < allTracks.length; i += 1) {
|
|
3139
|
+
const prev = allTracks[i - 1].track;
|
|
3140
|
+
const current = allTracks[i];
|
|
3141
|
+
const need = 12 - (current.track.y - prev.y);
|
|
3142
|
+
if (need <= .01) continue;
|
|
3143
|
+
const window = gapWindow.get(current.gap);
|
|
3144
|
+
if (!window) continue;
|
|
3145
|
+
const next = Math.min(window.hi - 6, current.track.y + need);
|
|
3146
|
+
current.track.y = Math.max(current.track.y, next);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
const tbBoxes = [];
|
|
3150
|
+
for (const item of realItems) {
|
|
3151
|
+
const cross = isLR ? item.th : item.tw;
|
|
3152
|
+
const flow = isLR ? item.tw : item.th;
|
|
3153
|
+
item.tbVisual = {
|
|
3154
|
+
x: item.centerX + (item.offsetCross || 0) - cross / 2,
|
|
3155
|
+
y: item.cy - flow / 2,
|
|
3156
|
+
w: cross,
|
|
3157
|
+
h: flow
|
|
3158
|
+
};
|
|
3159
|
+
tbBoxes.push(item.tbVisual);
|
|
3160
|
+
}
|
|
3161
|
+
const labelItems = dummyItems.filter((item) => item.kind === "label");
|
|
3162
|
+
for (const item of labelItems) {
|
|
3163
|
+
const cross = isLR ? item.th : item.tw;
|
|
3164
|
+
const flow = isLR ? item.tw : item.th;
|
|
3165
|
+
item.tbVisual = {
|
|
3166
|
+
x: item.centerX - cross / 2,
|
|
3167
|
+
y: item.cy - flow / 2,
|
|
3168
|
+
w: cross,
|
|
3169
|
+
h: flow
|
|
3170
|
+
};
|
|
3171
|
+
tbBoxes.push(item.tbVisual);
|
|
3172
|
+
}
|
|
3173
|
+
for (const item of allItems) tbBoxes.push({
|
|
3174
|
+
x: item.centerX - item.bw / 2,
|
|
3175
|
+
y: item.cy - item.bh / 2,
|
|
3176
|
+
w: item.bw,
|
|
3177
|
+
h: item.bh
|
|
3178
|
+
});
|
|
3179
|
+
for (const cluster of clusters.values()) if (cluster.rect) tbBoxes.push(cluster.rect);
|
|
3180
|
+
let tbMinX = Math.min(...tbBoxes.map((box) => box.x));
|
|
3181
|
+
let tbMinY = Math.min(...tbBoxes.map((box) => box.y));
|
|
3182
|
+
let tbMaxX = Math.max(...tbBoxes.map((box) => box.x + box.w));
|
|
3183
|
+
let tbMaxY = Math.max(...tbBoxes.map((box) => box.y + box.h));
|
|
3184
|
+
const shiftX = -tbMinX;
|
|
3185
|
+
const shiftY = -tbMinY;
|
|
3186
|
+
const W = tbMaxX - tbMinX;
|
|
3187
|
+
const H = tbMaxY - tbMinY;
|
|
3188
|
+
const transformPoint = (x, yy) => {
|
|
3189
|
+
const nx = x + shiftX;
|
|
3190
|
+
const ny = yy + shiftY;
|
|
3191
|
+
if (direction === "BT") return {
|
|
3192
|
+
x: nx,
|
|
3193
|
+
y: H - ny
|
|
3194
|
+
};
|
|
3195
|
+
if (direction === "LR") return {
|
|
3196
|
+
x: ny,
|
|
3197
|
+
y: nx
|
|
3198
|
+
};
|
|
3199
|
+
if (direction === "RL") return {
|
|
3200
|
+
x: W - ny,
|
|
3201
|
+
y: nx
|
|
3202
|
+
};
|
|
3203
|
+
return {
|
|
3204
|
+
x: nx,
|
|
3205
|
+
y: ny
|
|
3206
|
+
};
|
|
3207
|
+
};
|
|
3208
|
+
const transformRect = (rect) => {
|
|
3209
|
+
const x1 = rect.x + shiftX;
|
|
3210
|
+
const y1 = rect.y + shiftY;
|
|
3211
|
+
x1 + rect.w;
|
|
3212
|
+
const y2 = y1 + rect.h;
|
|
3213
|
+
if (direction === "BT") return {
|
|
3214
|
+
x: x1,
|
|
3215
|
+
y: H - y2,
|
|
3216
|
+
w: rect.w,
|
|
3217
|
+
h: rect.h
|
|
3218
|
+
};
|
|
3219
|
+
if (direction === "LR") return {
|
|
3220
|
+
x: y1,
|
|
3221
|
+
y: x1,
|
|
3222
|
+
w: rect.h,
|
|
3223
|
+
h: rect.w
|
|
3224
|
+
};
|
|
3225
|
+
if (direction === "RL") return {
|
|
3226
|
+
x: W - y2,
|
|
3227
|
+
y: x1,
|
|
3228
|
+
w: rect.h,
|
|
3229
|
+
h: rect.w
|
|
3230
|
+
};
|
|
3231
|
+
return {
|
|
3232
|
+
x: x1,
|
|
3233
|
+
y: y1,
|
|
3234
|
+
w: rect.w,
|
|
3235
|
+
h: rect.h
|
|
3236
|
+
};
|
|
3237
|
+
};
|
|
3238
|
+
const visualNodes = /* @__PURE__ */ new Map();
|
|
3239
|
+
for (const item of realItems) {
|
|
3240
|
+
const rect = transformRect(item.tbVisual);
|
|
3241
|
+
item.visualRect = rect;
|
|
3242
|
+
const anchor = transformPoint(item.centerX + (item.offsetCross || 0), item.cy);
|
|
3243
|
+
item.vx = anchor.x;
|
|
3244
|
+
item.vy = anchor.y;
|
|
3245
|
+
visualNodes.set(item.node.id, {
|
|
3246
|
+
item,
|
|
3247
|
+
rect
|
|
3248
|
+
});
|
|
3249
|
+
}
|
|
3250
|
+
const visualLabels = /* @__PURE__ */ new Map();
|
|
3251
|
+
for (const item of labelItems) {
|
|
3252
|
+
const rect = transformRect(item.tbVisual);
|
|
3253
|
+
item.visualRect = rect;
|
|
3254
|
+
const anchor = transformPoint(item.centerX, item.cy);
|
|
3255
|
+
item.vx = anchor.x;
|
|
3256
|
+
item.vy = anchor.y;
|
|
3257
|
+
visualLabels.set(item, {
|
|
3258
|
+
item,
|
|
3259
|
+
rect
|
|
3260
|
+
});
|
|
3261
|
+
}
|
|
3262
|
+
for (const item of dummyItems) {
|
|
3263
|
+
if (item.kind === "label") continue;
|
|
3264
|
+
const anchor = transformPoint(item.centerX, item.cy);
|
|
3265
|
+
item.vx = anchor.x;
|
|
3266
|
+
item.vy = anchor.y;
|
|
3267
|
+
}
|
|
3268
|
+
const visualFrames = /* @__PURE__ */ new Map();
|
|
3269
|
+
for (const cluster of clusters.values()) {
|
|
3270
|
+
if (!cluster.rect) continue;
|
|
3271
|
+
const rect = transformRect(cluster.rect);
|
|
3272
|
+
const titleH = cluster.titleLines.length * 17 + (cluster.descLines.length ? 4 + cluster.descLines.length * 16 : 0);
|
|
3273
|
+
const titleRect = {
|
|
3274
|
+
x: rect.x + FRAME_PAD,
|
|
3275
|
+
y: rect.y + 10,
|
|
3276
|
+
w: Math.max(cluster.titleW, cluster.descW || 0, 1),
|
|
3277
|
+
h: Math.max(titleH, 1)
|
|
3278
|
+
};
|
|
3279
|
+
cluster.visualRect = rect;
|
|
3280
|
+
cluster.visualTitleRect = titleRect;
|
|
3281
|
+
visualFrames.set(cluster.id, {
|
|
3282
|
+
cluster,
|
|
3283
|
+
rect,
|
|
3284
|
+
titleRect
|
|
3285
|
+
});
|
|
3286
|
+
}
|
|
3287
|
+
let selfResults = [];
|
|
3288
|
+
const clampValue = (value, min, max) => Math.max(min, Math.min(max, value));
|
|
3289
|
+
const centerOfRect = (rect) => ({
|
|
3290
|
+
x: rect.x + rect.w / 2,
|
|
3291
|
+
y: rect.y + rect.h / 2
|
|
3292
|
+
});
|
|
3293
|
+
const boxesOverlap = (a, b, pad = 0) => a.x < b.x + b.w + pad && a.x + a.w + pad > b.x && a.y < b.y + b.h + pad && a.y + a.h + pad > b.y;
|
|
3294
|
+
if (ortho) {
|
|
3295
|
+
const built = ortho.build();
|
|
3296
|
+
const toVisual = (point) => transformPoint(point.x, point.y);
|
|
3297
|
+
const toVisualSegments = (segments) => segments.map((seg) => ({
|
|
3298
|
+
p0: toVisual(seg.p0),
|
|
3299
|
+
p1: toVisual(seg.p1),
|
|
3300
|
+
c1: toVisual(seg.c1),
|
|
3301
|
+
c2: toVisual(seg.c2),
|
|
3302
|
+
kind: seg.kind
|
|
3303
|
+
}));
|
|
3304
|
+
for (const route of built.routes) {
|
|
3305
|
+
const { le } = route;
|
|
3306
|
+
le.start = toVisual(route.start);
|
|
3307
|
+
le.end = toVisual(route.end);
|
|
3308
|
+
le.points = route.points.map(toVisual);
|
|
3309
|
+
le.segments = toVisualSegments(route.segments);
|
|
3310
|
+
}
|
|
3311
|
+
for (const [item, rect] of built.labels) {
|
|
3312
|
+
item.visualRect = transformRect(rect);
|
|
3313
|
+
item.vx = item.visualRect.x + item.visualRect.w / 2;
|
|
3314
|
+
item.vy = item.visualRect.y + item.visualRect.h / 2;
|
|
3315
|
+
}
|
|
3316
|
+
selfResults = built.loops.map((loop) => ({
|
|
3317
|
+
edge: loop.edge,
|
|
3318
|
+
item: loop.item,
|
|
3319
|
+
start: toVisual(loop.start),
|
|
3320
|
+
end: toVisual(loop.end),
|
|
3321
|
+
points: loop.points.map(toVisual),
|
|
3322
|
+
segments: toVisualSegments(loop.segments),
|
|
3323
|
+
labelRect: loop.labelRect ? transformRect(loop.labelRect) : null,
|
|
3324
|
+
labelLines: loop.labelLines
|
|
3325
|
+
}));
|
|
3326
|
+
} else {
|
|
3327
|
+
const endpointSide = (center, adjacent) => {
|
|
3328
|
+
if (isLR) return adjacent.x >= center.x ? "right" : "left";
|
|
3329
|
+
return adjacent.y >= center.y ? "bottom" : "top";
|
|
3330
|
+
};
|
|
3331
|
+
const nodePort = (item, side, cross) => {
|
|
3332
|
+
const rect = item.visualRect;
|
|
3333
|
+
const cx = rect.x + rect.w / 2;
|
|
3334
|
+
const cy = rect.y + rect.h / 2;
|
|
3335
|
+
const halfW = rect.w / 2;
|
|
3336
|
+
const halfH = rect.h / 2;
|
|
3337
|
+
const shape = item.node.shape;
|
|
3338
|
+
const diamond = shape === "diamond" || item.node.kind === "choice";
|
|
3339
|
+
const circle = shape === "circle" || item.node.kind === "start" || item.node.kind === "end";
|
|
3340
|
+
if (side === "top" || side === "bottom") {
|
|
3341
|
+
const x = clampValue(cross, cx - halfW * .96, cx + halfW * .96);
|
|
3342
|
+
let y = side === "top" ? rect.y : rect.y + rect.h;
|
|
3343
|
+
if (diamond) {
|
|
3344
|
+
const dx = Math.min(halfW * .96, Math.abs(x - cx));
|
|
3345
|
+
y = cy + (side === "top" ? -1 : 1) * halfH * (1 - dx / halfW);
|
|
3346
|
+
} else if (circle) {
|
|
3347
|
+
const dx = Math.min(halfW * .96, Math.abs(x - cx));
|
|
3348
|
+
const ry = halfH * Math.sqrt(Math.max(0, 1 - (dx / halfW) ** 2));
|
|
3349
|
+
y = cy + (side === "top" ? -1 : 1) * ry;
|
|
3350
|
+
} else if (shape === "hexagon") {
|
|
3351
|
+
const flat = halfW - Math.min(16, halfW * .35);
|
|
3352
|
+
const dx = Math.abs(x - cx);
|
|
3353
|
+
if (dx > flat) {
|
|
3354
|
+
const t = Math.min(1, (dx - flat) / Math.max(1, halfW - flat));
|
|
3355
|
+
y = cy + (side === "top" ? -1 : 1) * halfH * t;
|
|
3356
|
+
}
|
|
3357
|
+
}
|
|
3358
|
+
return {
|
|
3359
|
+
x,
|
|
3360
|
+
y
|
|
3361
|
+
};
|
|
3362
|
+
}
|
|
3363
|
+
const y = clampValue(cross, cy - halfH * .96, cy + halfH * .96);
|
|
3364
|
+
let x = side === "left" ? rect.x : rect.x + rect.w;
|
|
3365
|
+
if (diamond) {
|
|
3366
|
+
const dy = Math.min(halfH * .82, Math.abs(y - cy));
|
|
3367
|
+
x = cx + (side === "left" ? -1 : 1) * halfW * (1 - dy / halfH);
|
|
3368
|
+
} else if (circle) {
|
|
3369
|
+
const dy = Math.min(halfH * .84, Math.abs(y - cy));
|
|
3370
|
+
const rx = halfW * Math.sqrt(Math.max(0, 1 - (dy / halfH) ** 2));
|
|
3371
|
+
x = cx + (side === "left" ? -1 : 1) * rx;
|
|
3372
|
+
} else if (shape === "hexagon") {
|
|
3373
|
+
const cut = Math.min(16, halfH * .35);
|
|
3374
|
+
const t = Math.abs(y - cy) / Math.max(1, halfH);
|
|
3375
|
+
x = side === "left" ? rect.x + cut * t : rect.x + rect.w - cut * t;
|
|
3376
|
+
}
|
|
3377
|
+
return {
|
|
3378
|
+
x,
|
|
3379
|
+
y
|
|
3380
|
+
};
|
|
3381
|
+
};
|
|
3382
|
+
const framePort = (rect, side, cross) => {
|
|
3383
|
+
if (side === "top" || side === "bottom") {
|
|
3384
|
+
const cx = rect.x + rect.w / 2;
|
|
3385
|
+
return {
|
|
3386
|
+
x: clampValue(cross, cx - rect.w * .48, cx + rect.w * .48),
|
|
3387
|
+
y: side === "top" ? rect.y : rect.y + rect.h
|
|
3388
|
+
};
|
|
3389
|
+
}
|
|
3390
|
+
const cy = rect.y + rect.h / 2;
|
|
3391
|
+
const y = clampValue(cross, cy - rect.h * .48, cy + rect.h * .48);
|
|
3392
|
+
return {
|
|
3393
|
+
x: side === "left" ? rect.x : rect.x + rect.w,
|
|
3394
|
+
y
|
|
3395
|
+
};
|
|
3396
|
+
};
|
|
3397
|
+
const frameSide = (id, otherRank, adjacent, center) => {
|
|
3398
|
+
const range = frameRankRanges.get(id);
|
|
3399
|
+
if (!range) return endpointSide(center, adjacent);
|
|
3400
|
+
if (otherRank < range.top) return isLR ? "left" : "top";
|
|
3401
|
+
if (otherRank > range.bottom) return isLR ? "right" : "bottom";
|
|
3402
|
+
return endpointSide(center, adjacent);
|
|
3403
|
+
};
|
|
3404
|
+
const portRequests = [];
|
|
3405
|
+
for (const le of layoutEdges) {
|
|
3406
|
+
if (!le.chain) continue;
|
|
3407
|
+
const chainStart = le.chain[0];
|
|
3408
|
+
const chainEnd = le.chain[le.chain.length - 1];
|
|
3409
|
+
const lowFrame = le.reversed ? le.toFrame : le.fromFrame;
|
|
3410
|
+
const highFrame = le.reversed ? le.fromFrame : le.toFrame;
|
|
3411
|
+
const lowId = le.reversed ? le.edge.to : le.edge.from;
|
|
3412
|
+
const highId = le.reversed ? le.edge.from : le.edge.to;
|
|
3413
|
+
const lowRange = lowFrame ? frameRankRanges.get(lowId) : null;
|
|
3414
|
+
const highRange = highFrame ? frameRankRanges.get(highId) : null;
|
|
3415
|
+
const rawMids = le.chain.slice(1, -1);
|
|
3416
|
+
const lowCenter = lowFrame ? centerOfRect(visualFrames.get(lowId).rect) : {
|
|
3417
|
+
x: chainStart.visualRect.x + chainStart.visualRect.w / 2,
|
|
3418
|
+
y: chainStart.visualRect.y + chainStart.visualRect.h / 2
|
|
3419
|
+
};
|
|
3420
|
+
const highCenter = highFrame ? centerOfRect(visualFrames.get(highId).rect) : {
|
|
3421
|
+
x: chainEnd.visualRect.x + chainEnd.visualRect.w / 2,
|
|
3422
|
+
y: chainEnd.visualRect.y + chainEnd.visualRect.h / 2
|
|
3423
|
+
};
|
|
3424
|
+
const lowAdjacent0 = rawMids.length ? {
|
|
3425
|
+
x: rawMids[0].vx,
|
|
3426
|
+
y: rawMids[0].vy
|
|
3427
|
+
} : {
|
|
3428
|
+
x: chainEnd.vx,
|
|
3429
|
+
y: chainEnd.vy
|
|
3430
|
+
};
|
|
3431
|
+
const highAdjacent0 = rawMids.length ? {
|
|
3432
|
+
x: rawMids[rawMids.length - 1].vx,
|
|
3433
|
+
y: rawMids[rawMids.length - 1].vy
|
|
3434
|
+
} : {
|
|
3435
|
+
x: chainStart.vx,
|
|
3436
|
+
y: chainStart.vy
|
|
3437
|
+
};
|
|
3438
|
+
const lowSide = lowFrame ? frameSide(lowId, chainEnd.rank, lowAdjacent0, lowCenter) : endpointSide(lowCenter, lowAdjacent0);
|
|
3439
|
+
const highSide = highFrame ? frameSide(highId, chainStart.rank, highAdjacent0, highCenter) : endpointSide(highCenter, highAdjacent0);
|
|
3440
|
+
const mids = rawMids.filter((item) => {
|
|
3441
|
+
if (lowFrame) {
|
|
3442
|
+
if ((lowSide === "top" || lowSide === "left") && item.rank >= lowRange.top) return false;
|
|
3443
|
+
if ((lowSide === "bottom" || lowSide === "right") && item.rank <= lowRange.bottom) return false;
|
|
3444
|
+
}
|
|
3445
|
+
if (highFrame) {
|
|
3446
|
+
if ((highSide === "top" || highSide === "left") && item.rank >= highRange.top) return false;
|
|
3447
|
+
if ((highSide === "bottom" || highSide === "right") && item.rank <= highRange.bottom) return false;
|
|
3448
|
+
}
|
|
3449
|
+
return true;
|
|
3450
|
+
});
|
|
3451
|
+
le.filteredMids = mids;
|
|
3452
|
+
const lowAdjacent = mids.length ? {
|
|
3453
|
+
x: mids[0].vx,
|
|
3454
|
+
y: mids[0].vy
|
|
3455
|
+
} : {
|
|
3456
|
+
x: chainEnd.vx,
|
|
3457
|
+
y: chainEnd.vy
|
|
3458
|
+
};
|
|
3459
|
+
const highAdjacent = mids.length ? {
|
|
3460
|
+
x: mids[mids.length - 1].vx,
|
|
3461
|
+
y: mids[mids.length - 1].vy
|
|
3462
|
+
} : {
|
|
3463
|
+
x: chainStart.vx,
|
|
3464
|
+
y: chainStart.vy
|
|
3465
|
+
};
|
|
3466
|
+
const lowCross = lowSide === "top" || lowSide === "bottom" ? lowAdjacent.x : lowAdjacent.y;
|
|
3467
|
+
const highCross = highSide === "top" || highSide === "bottom" ? highAdjacent.x : highAdjacent.y;
|
|
3468
|
+
le.lowReq = {
|
|
3469
|
+
le,
|
|
3470
|
+
role: "low",
|
|
3471
|
+
id: lowId,
|
|
3472
|
+
isFrame: lowFrame,
|
|
3473
|
+
side: lowSide,
|
|
3474
|
+
cross: lowCross,
|
|
3475
|
+
item: lowFrame ? null : chainStart,
|
|
3476
|
+
rect: lowFrame ? visualFrames.get(lowId).rect : null,
|
|
3477
|
+
center: lowCenter,
|
|
3478
|
+
edgeSeq: le.edge.seq
|
|
3479
|
+
};
|
|
3480
|
+
le.highReq = {
|
|
3481
|
+
le,
|
|
3482
|
+
role: "high",
|
|
3483
|
+
id: highId,
|
|
3484
|
+
isFrame: highFrame,
|
|
3485
|
+
side: highSide,
|
|
3486
|
+
cross: highCross,
|
|
3487
|
+
item: highFrame ? null : chainEnd,
|
|
3488
|
+
rect: highFrame ? visualFrames.get(highId).rect : null,
|
|
3489
|
+
center: highCenter,
|
|
3490
|
+
edgeSeq: le.edge.seq
|
|
3491
|
+
};
|
|
3492
|
+
portRequests.push(le.lowReq, le.highReq);
|
|
3493
|
+
}
|
|
3494
|
+
const portGroups = /* @__PURE__ */ new Map();
|
|
3495
|
+
for (const request of portRequests) {
|
|
3496
|
+
const key = `${request.isFrame ? "F" : "N"}:${request.id}:${request.side}`;
|
|
3497
|
+
if (!portGroups.has(key)) portGroups.set(key, []);
|
|
3498
|
+
portGroups.get(key).push(request);
|
|
3499
|
+
}
|
|
3500
|
+
const PORT_SPACING = model.type === "er" ? 28 : 12;
|
|
3501
|
+
const growth = /* @__PURE__ */ new Map();
|
|
3502
|
+
for (const group of portGroups.values()) {
|
|
3503
|
+
const first = group[0];
|
|
3504
|
+
if (first.isFrame || group.length < 2) continue;
|
|
3505
|
+
const item = first.item;
|
|
3506
|
+
if (item.cluster && item.cluster !== root) continue;
|
|
3507
|
+
const vertical = first.side === "left" || first.side === "right";
|
|
3508
|
+
const current = vertical ? item.visualRect.h : item.visualRect.w;
|
|
3509
|
+
const required = Math.max(current, (group.length - 1) * PORT_SPACING / .96);
|
|
3510
|
+
const need = growth.get(item) || {
|
|
3511
|
+
w: item.visualRect.w,
|
|
3512
|
+
h: item.visualRect.h
|
|
3513
|
+
};
|
|
3514
|
+
if (vertical) need.h = Math.max(need.h, required);
|
|
3515
|
+
else need.w = Math.max(need.w, required);
|
|
3516
|
+
growth.set(item, need);
|
|
3517
|
+
}
|
|
3518
|
+
for (const [item, need] of growth) {
|
|
3519
|
+
const rect = item.visualRect;
|
|
3520
|
+
if (need.w <= rect.w + .1 && need.h <= rect.h + .1) continue;
|
|
3521
|
+
const next = {
|
|
3522
|
+
x: rect.x - (need.w - rect.w) / 2,
|
|
3523
|
+
y: rect.y - (need.h - rect.h) / 2,
|
|
3524
|
+
w: need.w,
|
|
3525
|
+
h: need.h
|
|
3526
|
+
};
|
|
3527
|
+
if (!realItems.some((other) => other !== item && boxesOverlap(next, other.visualRect, 4))) item.visualRect = next;
|
|
3528
|
+
}
|
|
3529
|
+
for (const request of portRequests) {
|
|
3530
|
+
if (request.isFrame) continue;
|
|
3531
|
+
request.center = centerOfRect(request.item.visualRect);
|
|
3532
|
+
}
|
|
3533
|
+
for (const group of portGroups.values()) {
|
|
3534
|
+
group.sort((a, b) => a.cross - b.cross || a.edgeSeq - b.edgeSeq || (a.role === "low" ? -1 : 1));
|
|
3535
|
+
const count = group.length;
|
|
3536
|
+
const first = group[0];
|
|
3537
|
+
const sideLength = first.isFrame ? first.side === "top" || first.side === "bottom" ? first.rect.w : first.rect.h : first.side === "top" || first.side === "bottom" ? first.item.visualRect.w : first.item.visualRect.h;
|
|
3538
|
+
const centerCross = first.side === "top" || first.side === "bottom" ? first.center.x : first.center.y;
|
|
3539
|
+
const span = count === 1 ? 0 : Math.min(sideLength * .96, PORT_SPACING * (count - 1));
|
|
3540
|
+
group.forEach((request, index) => {
|
|
3541
|
+
const cross = count === 1 ? centerCross : centerCross - span / 2 + span * index / (count - 1);
|
|
3542
|
+
request.port = request.isFrame ? framePort(request.rect, request.side, cross) : nodePort(request.item, request.side, cross);
|
|
3543
|
+
});
|
|
3544
|
+
}
|
|
3545
|
+
const buildSegments = (points) => {
|
|
3546
|
+
const segments = [];
|
|
3547
|
+
for (let i = 0; i < points.length - 1; i += 1) {
|
|
3548
|
+
const p0 = points[i];
|
|
3549
|
+
const p1 = points[i + 1];
|
|
3550
|
+
if (Math.hypot(p1.x - p0.x, p1.y - p0.y) < .01) continue;
|
|
3551
|
+
let c1;
|
|
3552
|
+
let c2;
|
|
3553
|
+
if (isLR) {
|
|
3554
|
+
const mx = (p0.x + p1.x) / 2;
|
|
3555
|
+
c1 = {
|
|
3556
|
+
x: mx,
|
|
3557
|
+
y: p0.y
|
|
3558
|
+
};
|
|
3559
|
+
c2 = {
|
|
3560
|
+
x: mx,
|
|
3561
|
+
y: p1.y
|
|
3562
|
+
};
|
|
3563
|
+
} else {
|
|
3564
|
+
const my = (p0.y + p1.y) / 2;
|
|
3565
|
+
c1 = {
|
|
3566
|
+
x: p0.x,
|
|
3567
|
+
y: my
|
|
3568
|
+
};
|
|
3569
|
+
c2 = {
|
|
3570
|
+
x: p1.x,
|
|
3571
|
+
y: my
|
|
3572
|
+
};
|
|
3573
|
+
}
|
|
3574
|
+
segments.push({
|
|
3575
|
+
p0,
|
|
3576
|
+
p1,
|
|
3577
|
+
c1,
|
|
3578
|
+
c2
|
|
3579
|
+
});
|
|
3580
|
+
}
|
|
3581
|
+
return segments;
|
|
3582
|
+
};
|
|
3583
|
+
const stubOf = (port, side) => ({
|
|
3584
|
+
x: port.x + (side === "right" ? ER_STUB : side === "left" ? -18 : 0),
|
|
3585
|
+
y: port.y + (side === "bottom" ? ER_STUB : side === "top" ? -18 : 0)
|
|
3586
|
+
});
|
|
3587
|
+
const straight = (p0, p1) => ({
|
|
3588
|
+
p0,
|
|
3589
|
+
p1,
|
|
3590
|
+
c1: {
|
|
3591
|
+
x: p0.x + (p1.x - p0.x) / 3,
|
|
3592
|
+
y: p0.y + (p1.y - p0.y) / 3
|
|
3593
|
+
},
|
|
3594
|
+
c2: {
|
|
3595
|
+
x: p0.x + (p1.x - p0.x) * 2 / 3,
|
|
3596
|
+
y: p0.y + (p1.y - p0.y) * 2 / 3
|
|
3597
|
+
}
|
|
3598
|
+
});
|
|
3599
|
+
for (const le of layoutEdges) {
|
|
3600
|
+
if (!le.chain || !le.lowReq) continue;
|
|
3601
|
+
const mids = le.filteredMids.map((item) => ({
|
|
3602
|
+
x: item.vx,
|
|
3603
|
+
y: item.vy
|
|
3604
|
+
}));
|
|
3605
|
+
if (model.type === "er") {
|
|
3606
|
+
const lowStub = stubOf(le.lowReq.port, le.lowReq.side);
|
|
3607
|
+
const highStub = stubOf(le.highReq.port, le.highReq.side);
|
|
3608
|
+
const segments = [
|
|
3609
|
+
straight(le.lowReq.port, lowStub),
|
|
3610
|
+
...buildSegments([
|
|
3611
|
+
lowStub,
|
|
3612
|
+
...mids,
|
|
3613
|
+
highStub
|
|
3614
|
+
]),
|
|
3615
|
+
straight(highStub, le.highReq.port)
|
|
3616
|
+
];
|
|
3617
|
+
le.start = le.reversed ? le.highReq.port : le.lowReq.port;
|
|
3618
|
+
le.end = le.reversed ? le.lowReq.port : le.highReq.port;
|
|
3619
|
+
le.segments = le.reversed ? segments.reverse().map(({ p0, p1, c1, c2 }) => ({
|
|
3620
|
+
p0: p1,
|
|
3621
|
+
p1: p0,
|
|
3622
|
+
c1: c2,
|
|
3623
|
+
c2: c1
|
|
3624
|
+
})) : segments;
|
|
3625
|
+
continue;
|
|
3626
|
+
}
|
|
3627
|
+
const lowPoints = [
|
|
3628
|
+
le.lowReq.port,
|
|
3629
|
+
...mids,
|
|
3630
|
+
le.highReq.port
|
|
3631
|
+
];
|
|
3632
|
+
const points = le.reversed ? [...lowPoints].reverse() : lowPoints;
|
|
3633
|
+
le.start = points[0];
|
|
3634
|
+
le.end = points[points.length - 1];
|
|
3635
|
+
le.segments = buildSegments(points);
|
|
3636
|
+
}
|
|
3637
|
+
selfResults = [];
|
|
3638
|
+
for (const item of realItems) {
|
|
3639
|
+
if (!item.selfLoops.length) continue;
|
|
3640
|
+
for (const edge of item.selfLoops) {
|
|
3641
|
+
const rect = item.visualRect;
|
|
3642
|
+
const cx = rect.x + rect.w / 2;
|
|
3643
|
+
const cy = rect.y + rect.h / 2;
|
|
3644
|
+
const labelWidth = edge.label ? maxWidthOf(wrapText(edge.label, 120, FONT.label, measure), FONT.label, measure) : 0;
|
|
3645
|
+
const labelLines = edge.label ? wrapText(edge.label, 120, FONT.label, measure) : [];
|
|
3646
|
+
const labelHeight = labelLines.length ? labelLines.length * 16 + 6 : 0;
|
|
3647
|
+
const loop = direction === "LR" || direction === "RL" ? Math.max(16, labelLines.length ? (labelHeight + 12) / 1.5 : 16) : Math.max(16, labelLines.length ? (labelWidth + 22) / 1.5 : 16);
|
|
3648
|
+
let start;
|
|
3649
|
+
let end;
|
|
3650
|
+
let segments;
|
|
3651
|
+
let outer;
|
|
3652
|
+
if (direction === "LR" || direction === "RL") {
|
|
3653
|
+
const below = direction === "LR";
|
|
3654
|
+
const side = below ? "bottom" : "top";
|
|
3655
|
+
const ySide = below ? rect.y + rect.h : rect.y;
|
|
3656
|
+
start = nodePort(item, side, cx - 8);
|
|
3657
|
+
end = nodePort(item, side, cx + 8);
|
|
3658
|
+
outer = below ? ySide + loop * .75 : ySide - loop * .75;
|
|
3659
|
+
segments = [{
|
|
3660
|
+
p0: start,
|
|
3661
|
+
p1: end,
|
|
3662
|
+
c1: {
|
|
3663
|
+
x: cx - 8,
|
|
3664
|
+
y: below ? ySide + loop : ySide - loop
|
|
3665
|
+
},
|
|
3666
|
+
c2: {
|
|
3667
|
+
x: cx + 8,
|
|
3668
|
+
y: below ? ySide + loop : ySide - loop
|
|
3669
|
+
}
|
|
3670
|
+
}];
|
|
3671
|
+
} else {
|
|
3672
|
+
const right = rect.x + rect.w;
|
|
3673
|
+
start = nodePort(item, "right", cy - 8);
|
|
3674
|
+
end = nodePort(item, "right", cy + 8);
|
|
3675
|
+
outer = right + loop * .75;
|
|
3676
|
+
segments = [{
|
|
3677
|
+
p0: start,
|
|
3678
|
+
p1: end,
|
|
3679
|
+
c1: {
|
|
3680
|
+
x: right + loop,
|
|
3681
|
+
y: cy - 8
|
|
3682
|
+
},
|
|
3683
|
+
c2: {
|
|
3684
|
+
x: right + loop,
|
|
3685
|
+
y: cy + 8
|
|
3686
|
+
}
|
|
3687
|
+
}];
|
|
3688
|
+
}
|
|
3689
|
+
let labelRect = null;
|
|
3690
|
+
if (labelLines.length) {
|
|
3691
|
+
if (direction === "LR" || direction === "RL") labelRect = {
|
|
3692
|
+
x: cx - (labelWidth + 10) / 2,
|
|
3693
|
+
y: outer - labelHeight / 2,
|
|
3694
|
+
w: labelWidth + 10,
|
|
3695
|
+
h: labelHeight
|
|
3696
|
+
};
|
|
3697
|
+
else labelRect = {
|
|
3698
|
+
x: outer - (labelWidth + 10) / 2,
|
|
3699
|
+
y: cy - labelHeight / 2,
|
|
3700
|
+
w: labelWidth + 10,
|
|
3701
|
+
h: labelHeight
|
|
3702
|
+
};
|
|
3703
|
+
}
|
|
3704
|
+
selfResults.push({
|
|
3705
|
+
edge,
|
|
3706
|
+
item,
|
|
3707
|
+
start,
|
|
3708
|
+
end,
|
|
3709
|
+
segments,
|
|
3710
|
+
labelRect,
|
|
3711
|
+
labelLines
|
|
3712
|
+
});
|
|
3713
|
+
}
|
|
3714
|
+
}
|
|
3715
|
+
const labelEdgeMap = /* @__PURE__ */ new Map();
|
|
3716
|
+
for (const le of layoutEdges) {
|
|
3717
|
+
const item = le.chain ? le.chain.find((part) => part.kind === "label") : null;
|
|
3718
|
+
if (item) labelEdgeMap.set(item, le);
|
|
3719
|
+
}
|
|
3720
|
+
const pointOnSegment = (segment, t) => {
|
|
3721
|
+
const u = 1 - t;
|
|
3722
|
+
return {
|
|
3723
|
+
x: u * u * u * segment.p0.x + 3 * u * u * t * segment.c1.x + 3 * u * t * t * segment.c2.x + t * t * t * segment.p1.x,
|
|
3724
|
+
y: u * u * u * segment.p0.y + 3 * u * u * t * segment.c1.y + 3 * u * t * t * segment.c2.y + t * t * t * segment.p1.y
|
|
3725
|
+
};
|
|
3726
|
+
};
|
|
3727
|
+
const allSegments = [];
|
|
3728
|
+
for (const le of layoutEdges) if (le.segments && le.segments.length) allSegments.push({
|
|
3729
|
+
le,
|
|
3730
|
+
segments: le.segments
|
|
3731
|
+
});
|
|
3732
|
+
for (const loop of selfResults) allSegments.push({
|
|
3733
|
+
loop,
|
|
3734
|
+
segments: loop.segments
|
|
3735
|
+
});
|
|
3736
|
+
const labelFits = (rect, item, ownLe) => {
|
|
3737
|
+
for (const node of realItems) if (boxesOverlap(rect, node.visualRect, 2)) return false;
|
|
3738
|
+
for (const other of labelItems) if (other !== item && boxesOverlap(rect, other.visualRect, 2)) return false;
|
|
3739
|
+
for (const loop of selfResults) if (loop.labelRect && boxesOverlap(rect, loop.labelRect, 2)) return false;
|
|
3740
|
+
for (const frame of visualFrames.values()) if (frame.titleRect && boxesOverlap(rect, frame.titleRect, 2)) return false;
|
|
3741
|
+
for (const entry of allSegments) {
|
|
3742
|
+
if (entry.le === ownLe || entry.loop) continue;
|
|
3743
|
+
for (const segment of entry.segments) for (let step = 0; step <= 48; step += 1) {
|
|
3744
|
+
const point = pointOnSegment(segment, step / 48);
|
|
3745
|
+
if (point.x > rect.x + .5 && point.x < rect.x + rect.w - .5 && point.y > rect.y + .5 && point.y < rect.y + rect.h - .5) return false;
|
|
3746
|
+
}
|
|
3747
|
+
}
|
|
3748
|
+
return true;
|
|
3749
|
+
};
|
|
3750
|
+
if (!ortho) {
|
|
3751
|
+
const labelOrder = [...labelItems].sort((a, b) => a.edge.seq - b.edge.seq);
|
|
3752
|
+
for (const item of labelOrder) {
|
|
3753
|
+
const le = labelEdgeMap.get(item);
|
|
3754
|
+
if (!le || !le.segments || !le.segments.length) continue;
|
|
3755
|
+
const original = item.visualRect;
|
|
3756
|
+
if (labelFits(original, item, le)) continue;
|
|
3757
|
+
const width = original.w;
|
|
3758
|
+
const height = original.h;
|
|
3759
|
+
const candidates = [];
|
|
3760
|
+
for (const segment of le.segments) for (let step = 6; step <= 44; step += 1) {
|
|
3761
|
+
const point = pointOnSegment(segment, step / 50);
|
|
3762
|
+
candidates.push({
|
|
3763
|
+
x: point.x - width / 2,
|
|
3764
|
+
y: point.y - height / 2,
|
|
3765
|
+
w: width,
|
|
3766
|
+
h: height
|
|
3767
|
+
});
|
|
3768
|
+
}
|
|
3769
|
+
let best = null;
|
|
3770
|
+
let bestDistance = Infinity;
|
|
3771
|
+
for (const candidate of candidates) {
|
|
3772
|
+
if (!labelFits(candidate, item, le)) continue;
|
|
3773
|
+
const distance = Math.hypot(candidate.x - original.x, candidate.y - original.y);
|
|
3774
|
+
if (distance < bestDistance) {
|
|
3775
|
+
best = candidate;
|
|
3776
|
+
bestDistance = distance;
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
if (best) {
|
|
3780
|
+
item.visualRect = best;
|
|
3781
|
+
item.vx = best.x + width / 2;
|
|
3782
|
+
item.vy = best.y + height / 2;
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
}
|
|
3786
|
+
}
|
|
3787
|
+
const visualBoxes = [];
|
|
3788
|
+
for (const item of realItems) visualBoxes.push(item.visualRect);
|
|
3789
|
+
for (const item of labelItems) visualBoxes.push(item.visualRect);
|
|
3790
|
+
for (const cluster of clusters.values()) {
|
|
3791
|
+
if (cluster.visualRect) visualBoxes.push(cluster.visualRect);
|
|
3792
|
+
if (cluster.visualTitleRect) visualBoxes.push(cluster.visualTitleRect);
|
|
3793
|
+
}
|
|
3794
|
+
for (const le of layoutEdges) {
|
|
3795
|
+
if (!le.segments) continue;
|
|
3796
|
+
for (const seg of le.segments) {
|
|
3797
|
+
visualBoxes.push({
|
|
3798
|
+
x: seg.p0.x,
|
|
3799
|
+
y: seg.p0.y,
|
|
3800
|
+
w: 0,
|
|
3801
|
+
h: 0
|
|
3802
|
+
});
|
|
3803
|
+
visualBoxes.push({
|
|
3804
|
+
x: seg.p1.x,
|
|
3805
|
+
y: seg.p1.y,
|
|
3806
|
+
w: 0,
|
|
3807
|
+
h: 0
|
|
3808
|
+
});
|
|
3809
|
+
visualBoxes.push({
|
|
3810
|
+
x: seg.c1.x,
|
|
3811
|
+
y: seg.c1.y,
|
|
3812
|
+
w: 0,
|
|
3813
|
+
h: 0
|
|
3814
|
+
});
|
|
3815
|
+
visualBoxes.push({
|
|
3816
|
+
x: seg.c2.x,
|
|
3817
|
+
y: seg.c2.y,
|
|
3818
|
+
w: 0,
|
|
3819
|
+
h: 0
|
|
3820
|
+
});
|
|
3821
|
+
}
|
|
3822
|
+
}
|
|
3823
|
+
for (const loop of selfResults) {
|
|
3824
|
+
for (const seg of loop.segments) {
|
|
3825
|
+
visualBoxes.push({
|
|
3826
|
+
x: seg.p0.x,
|
|
3827
|
+
y: seg.p0.y,
|
|
3828
|
+
w: 0,
|
|
3829
|
+
h: 0
|
|
3830
|
+
});
|
|
3831
|
+
visualBoxes.push({
|
|
3832
|
+
x: seg.p1.x,
|
|
3833
|
+
y: seg.p1.y,
|
|
3834
|
+
w: 0,
|
|
3835
|
+
h: 0
|
|
3836
|
+
});
|
|
3837
|
+
visualBoxes.push({
|
|
3838
|
+
x: seg.c1.x,
|
|
3839
|
+
y: seg.c1.y,
|
|
3840
|
+
w: 0,
|
|
3841
|
+
h: 0
|
|
3842
|
+
});
|
|
3843
|
+
visualBoxes.push({
|
|
3844
|
+
x: seg.c2.x,
|
|
3845
|
+
y: seg.c2.y,
|
|
3846
|
+
w: 0,
|
|
3847
|
+
h: 0
|
|
3848
|
+
});
|
|
3849
|
+
}
|
|
3850
|
+
if (loop.labelRect) visualBoxes.push(loop.labelRect);
|
|
3851
|
+
}
|
|
3852
|
+
const minX = Math.min(...visualBoxes.map((box) => box.x));
|
|
3853
|
+
const minY = Math.min(...visualBoxes.map((box) => box.y));
|
|
3854
|
+
const maxX = Math.max(...visualBoxes.map((box) => box.x + box.w));
|
|
3855
|
+
const maxY = Math.max(...visualBoxes.map((box) => box.y + box.h));
|
|
3856
|
+
const offsetX = MARGIN - minX;
|
|
3857
|
+
const offsetY = MARGIN - minY;
|
|
3858
|
+
const width = Math.ceil(maxX - minX + 32);
|
|
3859
|
+
const height = Math.ceil(maxY - minY + 32);
|
|
3860
|
+
const movePoint = (point) => ({
|
|
3861
|
+
x: point.x + offsetX,
|
|
3862
|
+
y: point.y + offsetY
|
|
3863
|
+
});
|
|
3864
|
+
const moveRect = (rect) => ({
|
|
3865
|
+
x: rect.x + offsetX,
|
|
3866
|
+
y: rect.y + offsetY,
|
|
3867
|
+
w: rect.w,
|
|
3868
|
+
h: rect.h
|
|
3869
|
+
});
|
|
3870
|
+
for (const item of realItems) {
|
|
3871
|
+
item.visualRect = moveRect(item.visualRect);
|
|
3872
|
+
const anchor = movePoint({
|
|
3873
|
+
x: item.vx,
|
|
3874
|
+
y: item.vy
|
|
3875
|
+
});
|
|
3876
|
+
item.vx = anchor.x;
|
|
3877
|
+
item.vy = anchor.y;
|
|
3878
|
+
}
|
|
3879
|
+
for (const item of labelItems) {
|
|
3880
|
+
item.visualRect = moveRect(item.visualRect);
|
|
3881
|
+
const anchor = movePoint({
|
|
3882
|
+
x: item.vx,
|
|
3883
|
+
y: item.vy
|
|
3884
|
+
});
|
|
3885
|
+
item.vx = anchor.x;
|
|
3886
|
+
item.vy = anchor.y;
|
|
3887
|
+
}
|
|
3888
|
+
for (const item of dummyItems) {
|
|
3889
|
+
if (item.kind === "label") continue;
|
|
3890
|
+
const anchor = movePoint({
|
|
3891
|
+
x: item.vx,
|
|
3892
|
+
y: item.vy
|
|
3893
|
+
});
|
|
3894
|
+
item.vx = anchor.x;
|
|
3895
|
+
item.vy = anchor.y;
|
|
3896
|
+
}
|
|
3897
|
+
for (const cluster of clusters.values()) {
|
|
3898
|
+
if (cluster.visualRect) cluster.visualRect = moveRect(cluster.visualRect);
|
|
3899
|
+
if (cluster.visualTitleRect) cluster.visualTitleRect = moveRect(cluster.visualTitleRect);
|
|
3900
|
+
}
|
|
3901
|
+
for (const le of layoutEdges) {
|
|
3902
|
+
le.start = le.start ? movePoint(le.start) : null;
|
|
3903
|
+
le.end = le.end ? movePoint(le.end) : null;
|
|
3904
|
+
le.segments = le.segments.map((seg) => ({
|
|
3905
|
+
p0: movePoint(seg.p0),
|
|
3906
|
+
p1: movePoint(seg.p1),
|
|
3907
|
+
c1: movePoint(seg.c1),
|
|
3908
|
+
c2: movePoint(seg.c2),
|
|
3909
|
+
...seg.kind === void 0 ? {} : { kind: seg.kind }
|
|
3910
|
+
}));
|
|
3911
|
+
}
|
|
3912
|
+
for (const loop of selfResults) {
|
|
3913
|
+
loop.start = movePoint(loop.start);
|
|
3914
|
+
loop.end = movePoint(loop.end);
|
|
3915
|
+
loop.segments = loop.segments.map((seg) => ({
|
|
3916
|
+
p0: movePoint(seg.p0),
|
|
3917
|
+
p1: movePoint(seg.p1),
|
|
3918
|
+
c1: movePoint(seg.c1),
|
|
3919
|
+
c2: movePoint(seg.c2),
|
|
3920
|
+
...seg.kind === void 0 ? {} : { kind: seg.kind }
|
|
3921
|
+
}));
|
|
3922
|
+
if (loop.labelRect) loop.labelRect = moveRect(loop.labelRect);
|
|
3923
|
+
}
|
|
3924
|
+
const nodes = realItems.map((item) => {
|
|
3925
|
+
const text = labelBlock(item, item.visualRect);
|
|
3926
|
+
return {
|
|
3927
|
+
id: item.node.id,
|
|
3928
|
+
kind: item.node.kind,
|
|
3929
|
+
shape: item.node.shape,
|
|
3930
|
+
direction,
|
|
3931
|
+
class: item.node.class,
|
|
3932
|
+
label: item.node.label,
|
|
3933
|
+
lines: item.titleLines,
|
|
3934
|
+
descLines: item.descLines,
|
|
3935
|
+
er: item.er,
|
|
3936
|
+
parent: item.parent,
|
|
3937
|
+
x: item.visualRect.x,
|
|
3938
|
+
y: item.visualRect.y,
|
|
3939
|
+
w: item.visualRect.w,
|
|
3940
|
+
h: item.visualRect.h,
|
|
3941
|
+
titleY: text.titleY,
|
|
3942
|
+
descY: text.descY
|
|
3943
|
+
};
|
|
3944
|
+
});
|
|
3945
|
+
const labels = labelItems.map((item) => ({
|
|
3946
|
+
id: item.id,
|
|
3947
|
+
edge: item.edge.seq,
|
|
3948
|
+
lines: item.labelLines,
|
|
3949
|
+
x: item.visualRect.x,
|
|
3950
|
+
y: item.visualRect.y,
|
|
3951
|
+
w: item.visualRect.w,
|
|
3952
|
+
h: item.visualRect.h
|
|
3953
|
+
}));
|
|
3954
|
+
for (const loop of selfResults) {
|
|
3955
|
+
if (!loop.labelRect) continue;
|
|
3956
|
+
labels.push({
|
|
3957
|
+
id: `@@self:${loop.edge.seq}`,
|
|
3958
|
+
edge: loop.edge.seq,
|
|
3959
|
+
lines: loop.labelLines,
|
|
3960
|
+
x: loop.labelRect.x,
|
|
3961
|
+
y: loop.labelRect.y,
|
|
3962
|
+
w: loop.labelRect.w,
|
|
3963
|
+
h: loop.labelRect.h
|
|
3964
|
+
});
|
|
3965
|
+
}
|
|
3966
|
+
const frames = [...clusters.values()].filter((cluster) => cluster.visualRect).map((cluster) => ({
|
|
3967
|
+
id: cluster.id,
|
|
3968
|
+
title: cluster.title,
|
|
3969
|
+
class: cluster.class,
|
|
3970
|
+
kind: cluster.model.kind,
|
|
3971
|
+
parent: cluster.model.parent || null,
|
|
3972
|
+
lines: cluster.titleLines,
|
|
3973
|
+
descLines: cluster.descLines,
|
|
3974
|
+
x: cluster.visualRect.x,
|
|
3975
|
+
y: cluster.visualRect.y,
|
|
3976
|
+
w: cluster.visualRect.w,
|
|
3977
|
+
h: cluster.visualRect.h,
|
|
3978
|
+
titleRect: cluster.visualTitleRect
|
|
3979
|
+
}));
|
|
3980
|
+
const edges = [];
|
|
3981
|
+
for (const le of layoutEdges) {
|
|
3982
|
+
const labelItem = le.chain.find((item) => item.kind === "label");
|
|
3983
|
+
edges.push({
|
|
3984
|
+
id: le.edge.seq,
|
|
3985
|
+
from: le.edge.from,
|
|
3986
|
+
to: le.edge.to,
|
|
3987
|
+
fromFrame: le.fromFrame,
|
|
3988
|
+
toFrame: le.toFrame,
|
|
3989
|
+
label: le.edge.label,
|
|
3990
|
+
reversed: le.reversed,
|
|
3991
|
+
style: le.edge.style,
|
|
3992
|
+
startArrow: le.edge.startArrow,
|
|
3993
|
+
endArrow: le.edge.endArrow,
|
|
3994
|
+
startMark: le.edge.startMark,
|
|
3995
|
+
endMark: le.edge.endMark,
|
|
3996
|
+
animated: le.edge.anim?.animate,
|
|
3997
|
+
startCard: le.edge.startCard || null,
|
|
3998
|
+
endCard: le.edge.endCard || null,
|
|
3999
|
+
start: le.start,
|
|
4000
|
+
end: le.end,
|
|
4001
|
+
segments: le.segments,
|
|
4002
|
+
labelRect: labelItem ? labelItem.visualRect : null,
|
|
4003
|
+
labelLines: labelItem ? labelItem.labelLines : [],
|
|
4004
|
+
self: false
|
|
4005
|
+
});
|
|
4006
|
+
}
|
|
4007
|
+
for (const loop of selfResults) edges.push({
|
|
4008
|
+
id: loop.edge.seq,
|
|
4009
|
+
from: loop.edge.from,
|
|
4010
|
+
to: loop.edge.to,
|
|
4011
|
+
fromFrame: false,
|
|
4012
|
+
toFrame: false,
|
|
4013
|
+
label: loop.edge.label,
|
|
4014
|
+
style: loop.edge.style,
|
|
4015
|
+
startArrow: loop.edge.startArrow,
|
|
4016
|
+
endArrow: loop.edge.endArrow,
|
|
4017
|
+
startMark: loop.edge.startMark,
|
|
4018
|
+
endMark: loop.edge.endMark,
|
|
4019
|
+
animated: loop.edge.anim?.animate,
|
|
4020
|
+
startCard: null,
|
|
4021
|
+
endCard: null,
|
|
4022
|
+
start: loop.start,
|
|
4023
|
+
end: loop.end,
|
|
4024
|
+
segments: loop.segments,
|
|
4025
|
+
labelRect: loop.labelRect,
|
|
4026
|
+
labelLines: loop.labelLines,
|
|
4027
|
+
self: true
|
|
4028
|
+
});
|
|
4029
|
+
return {
|
|
4030
|
+
type: model.type,
|
|
4031
|
+
direction,
|
|
4032
|
+
width,
|
|
4033
|
+
height,
|
|
4034
|
+
nodes,
|
|
4035
|
+
labels,
|
|
4036
|
+
frames,
|
|
4037
|
+
edges,
|
|
4038
|
+
ranks: [...rankItems.entries()].sort((a, b) => a[0] - b[0]).map(([r, items]) => ({
|
|
4039
|
+
rank: r,
|
|
4040
|
+
items: items.map((item) => item.id)
|
|
4041
|
+
})),
|
|
4042
|
+
warnings,
|
|
4043
|
+
model
|
|
4044
|
+
};
|
|
4045
|
+
}
|
|
4046
|
+
export { graph_layout_exports, layoutGraph };
|