@knowvah/dot-engine 2.12.2 → 2.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/dist/api.js +12 -2
  2. package/dist/api.js.map +2 -2
  3. package/dist/common/clust-map.d.ts +16 -0
  4. package/dist/common/clust-map.d.ts.map +1 -0
  5. package/dist/common/compass-port.d.ts.map +1 -1
  6. package/dist/common/graph-init.d.ts.map +1 -1
  7. package/dist/common/htmltable-emit.d.ts +9 -6
  8. package/dist/common/htmltable-emit.d.ts.map +1 -1
  9. package/dist/common/htmltable-label.d.ts.map +1 -1
  10. package/dist/common/htmltable-lex.d.ts +3 -1
  11. package/dist/common/htmltable-lex.d.ts.map +1 -1
  12. package/dist/common/htmltable-parse-image.d.ts +23 -0
  13. package/dist/common/htmltable-parse-image.d.ts.map +1 -0
  14. package/dist/common/htmltable-parse-state.d.ts +4 -0
  15. package/dist/common/htmltable-parse-state.d.ts.map +1 -1
  16. package/dist/common/htmltable-parse.d.ts +33 -2
  17. package/dist/common/htmltable-parse.d.ts.map +1 -1
  18. package/dist/common/layers.d.ts +9 -0
  19. package/dist/common/layers.d.ts.map +1 -1
  20. package/dist/common/poly-gencode.d.ts.map +1 -1
  21. package/dist/common/poly-inside.d.ts.map +1 -1
  22. package/dist/common/splines-geom.d.ts +2 -1
  23. package/dist/common/splines-geom.d.ts.map +1 -1
  24. package/dist/common/splines-routespl.d.ts +3 -0
  25. package/dist/common/splines-routespl.d.ts.map +1 -1
  26. package/dist/common/xlabels-place.d.ts.map +1 -1
  27. package/dist/gvc/anchor.d.ts +17 -19
  28. package/dist/gvc/anchor.d.ts.map +1 -1
  29. package/dist/gvc/device-cluster.d.ts +9 -0
  30. package/dist/gvc/device-cluster.d.ts.map +1 -1
  31. package/dist/gvc/device.d.ts +2 -1
  32. package/dist/gvc/device.d.ts.map +1 -1
  33. package/dist/gvc/emit-walk.d.ts +7 -0
  34. package/dist/gvc/emit-walk.d.ts.map +1 -1
  35. package/dist/gvc/job.d.ts +13 -6
  36. package/dist/gvc/job.d.ts.map +1 -1
  37. package/dist/gvc/viewport.d.ts +14 -0
  38. package/dist/gvc/viewport.d.ts.map +1 -1
  39. package/dist/index.js +21767 -21607
  40. package/dist/index.js.map +4 -4
  41. package/dist/layout/dot/classify.d.ts.map +1 -1
  42. package/dist/layout/dot/compound.d.ts +0 -4
  43. package/dist/layout/dot/compound.d.ts.map +1 -1
  44. package/dist/layout/dot/edge-route-chain-views.d.ts +35 -0
  45. package/dist/layout/dot/edge-route-chain-views.d.ts.map +1 -0
  46. package/dist/layout/dot/edge-route-chain.d.ts +20 -16
  47. package/dist/layout/dot/edge-route-chain.d.ts.map +1 -1
  48. package/dist/layout/dot/edge-route-faithful.d.ts.map +1 -1
  49. package/dist/layout/dot/flat.d.ts +22 -7
  50. package/dist/layout/dot/flat.d.ts.map +1 -1
  51. package/dist/layout/dot/mincross-flat.d.ts +10 -1
  52. package/dist/layout/dot/mincross-flat.d.ts.map +1 -1
  53. package/dist/layout/dot/mincross.d.ts +6 -1
  54. package/dist/layout/dot/mincross.d.ts.map +1 -1
  55. package/dist/layout/dot/ns-subtree.d.ts +9 -6
  56. package/dist/layout/dot/ns-subtree.d.ts.map +1 -1
  57. package/dist/layout/dot/pack-components.d.ts +4 -3
  58. package/dist/layout/dot/pack-components.d.ts.map +1 -1
  59. package/dist/layout/dot/position-aux.d.ts +62 -20
  60. package/dist/layout/dot/position-aux.d.ts.map +1 -1
  61. package/dist/layout/dot/position-bbox.d.ts.map +1 -1
  62. package/dist/layout/dot/position-ycoords.d.ts.map +1 -1
  63. package/dist/layout/dot/position.d.ts.map +1 -1
  64. package/dist/layout/dot/rank-dot2.d.ts +7 -25
  65. package/dist/layout/dot/rank-dot2.d.ts.map +1 -1
  66. package/dist/layout/dot/self-loop.d.ts +11 -4
  67. package/dist/layout/dot/self-loop.d.ts.map +1 -1
  68. package/dist/layout/dot/splines-flat-multi.d.ts +11 -3
  69. package/dist/layout/dot/splines-flat-multi.d.ts.map +1 -1
  70. package/dist/layout/dot/splines-route.d.ts +6 -3
  71. package/dist/layout/dot/splines-route.d.ts.map +1 -1
  72. package/dist/layout/dot/straight-edges.d.ts.map +1 -1
  73. package/dist/layout/fdp/cluster-edges.d.ts.map +1 -1
  74. package/dist/layout/fdp/tlayout.d.ts +3 -0
  75. package/dist/layout/fdp/tlayout.d.ts.map +1 -1
  76. package/dist/layout/neato/__fixtures__/hier-imds.d.ts +5 -4
  77. package/dist/layout/neato/__fixtures__/hier-imds.d.ts.map +1 -1
  78. package/dist/layout/neato/__fixtures__/hier-majorization.d.ts +6 -5
  79. package/dist/layout/neato/__fixtures__/hier-majorization.d.ts.map +1 -1
  80. package/dist/layout/neato/conjgrad.d.ts +7 -6
  81. package/dist/layout/neato/conjgrad.d.ts.map +1 -1
  82. package/dist/layout/neato/matrix-ops-dense.d.ts +1 -6
  83. package/dist/layout/neato/matrix-ops-dense.d.ts.map +1 -1
  84. package/dist/layout/neato/matrix-ops.d.ts +0 -6
  85. package/dist/layout/neato/matrix-ops.d.ts.map +1 -1
  86. package/dist/layout/neato/quad-prog-vpsc.d.ts.map +1 -1
  87. package/dist/model/cgraph-ops.d.ts +7 -0
  88. package/dist/model/cgraph-ops.d.ts.map +1 -1
  89. package/dist/model/graph.d.ts +12 -2
  90. package/dist/model/graph.d.ts.map +1 -1
  91. package/dist/model/rankEntry.d.ts +3 -1
  92. package/dist/model/rankEntry.d.ts.map +1 -1
  93. package/dist/ortho/index.d.ts +0 -1
  94. package/dist/ortho/index.d.ts.map +1 -1
  95. package/dist/ortho/partition.d.ts +2 -4
  96. package/dist/ortho/partition.d.ts.map +1 -1
  97. package/dist/parser/builder.d.ts +11 -10
  98. package/dist/parser/builder.d.ts.map +1 -1
  99. package/dist/parser/graph-attr-syms.d.ts +35 -0
  100. package/dist/parser/graph-attr-syms.d.ts.map +1 -0
  101. package/dist/pathplan/route.d.ts +93 -0
  102. package/dist/pathplan/route.d.ts.map +1 -1
  103. package/dist/render/dot/agwrite.d.ts +27 -16
  104. package/dist/render/dot/agwrite.d.ts.map +1 -1
  105. package/dist/render/dot/attrs.d.ts +5 -0
  106. package/dist/render/dot/attrs.d.ts.map +1 -1
  107. package/dist/render/dot/types.d.ts +7 -2
  108. package/dist/render/dot/types.d.ts.map +1 -1
  109. package/dist/render/json-cluster-edge.d.ts +3 -2
  110. package/dist/render/json-cluster-edge.d.ts.map +1 -1
  111. package/dist/render/json.d.ts +7 -1
  112. package/dist/render/json.d.ts.map +1 -1
  113. package/dist/render/map.d.ts +14 -10
  114. package/dist/render/map.d.ts.map +1 -1
  115. package/dist/render/svg-graph.d.ts.map +1 -1
  116. package/dist/render/svg-id.d.ts +9 -0
  117. package/dist/render/svg-id.d.ts.map +1 -1
  118. package/dist/render.js +22009 -21849
  119. package/dist/render.js.map +4 -4
  120. package/dist/util/overflow.d.ts +37 -0
  121. package/dist/util/overflow.d.ts.map +1 -0
  122. package/dist/util/printf-round.d.ts +8 -0
  123. package/dist/util/printf-round.d.ts.map +1 -1
  124. package/package.json +3 -2
  125. package/src/common/clust-map.ts +58 -0
  126. package/src/common/compass-port.ts +36 -20
  127. package/src/common/graph-init.ts +11 -1
  128. package/src/common/htmltable-emit.ts +87 -7
  129. package/src/common/htmltable-label.ts +9 -4
  130. package/src/common/htmltable-lex.ts +5 -2
  131. package/src/common/htmltable-parse-image.ts +69 -0
  132. package/src/common/htmltable-parse-state.ts +6 -0
  133. package/src/common/htmltable-parse.ts +34 -44
  134. package/src/common/layers.ts +25 -4
  135. package/src/common/poly-gencode.ts +3 -11
  136. package/src/common/poly-inside.ts +11 -5
  137. package/src/common/splines-geom.ts +24 -6
  138. package/src/common/splines-routespl.ts +32 -0
  139. package/src/common/xlabels-place.ts +22 -4
  140. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.1 +19 -19
  141. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.2 +19 -19
  142. package/src/gvc/__fixtures__/rerender/abom-shift-pack.dot.plain.nofree.3 +19 -19
  143. package/src/gvc/__fixtures__/rerender/abom-shift-parity.dot.plain.nofree.2 +18 -18
  144. package/src/gvc/__fixtures__/rerender/abom-shift-parity.dot.plain.nofree.3 +12 -12
  145. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.1 +20 -20
  146. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.2 +31 -31
  147. package/src/gvc/__fixtures__/rerender/flat-rev-stale.dot.plain.nofree.3 +33 -33
  148. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.1 +10 -10
  149. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.2 +28 -28
  150. package/src/gvc/__fixtures__/rerender/multi-edge.dot.plain.nofree.3 +28 -28
  151. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.1 +21 -28
  152. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.2 +100 -102
  153. package/src/gvc/__fixtures__/rerender/multi-edge.dot.svg.nofree.3 +137 -139
  154. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.1 +1 -1
  155. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.2 +1 -1
  156. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.3 +1 -1
  157. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.1 +3 -3
  158. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.2 +3 -3
  159. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.3 +3 -3
  160. package/src/gvc/__fixtures__/rerender/regen.sh +6 -0
  161. package/src/gvc/anchor.ts +18 -30
  162. package/src/gvc/device-cluster.ts +16 -0
  163. package/src/gvc/device.ts +20 -20
  164. package/src/gvc/emit-walk.ts +11 -1
  165. package/src/gvc/job.ts +17 -9
  166. package/src/gvc/viewport.ts +27 -6
  167. package/src/layout/dot/classify.ts +5 -2
  168. package/src/layout/dot/compound.ts +1 -21
  169. package/src/layout/dot/edge-route-chain-views.ts +120 -0
  170. package/src/layout/dot/edge-route-chain.ts +103 -117
  171. package/src/layout/dot/edge-route-faithful.ts +20 -5
  172. package/src/layout/dot/flat.ts +38 -17
  173. package/src/layout/dot/mincross-flat.ts +15 -5
  174. package/src/layout/dot/mincross.ts +6 -2
  175. package/src/layout/dot/ns-subtree.ts +11 -7
  176. package/src/layout/dot/pack-components.ts +6 -4
  177. package/src/layout/dot/position-aux.ts +112 -39
  178. package/src/layout/dot/position-bbox.ts +14 -1
  179. package/src/layout/dot/position-ycoords.ts +7 -1
  180. package/src/layout/dot/position.ts +4 -1
  181. package/src/layout/dot/rank-dot2.ts +13 -94
  182. package/src/layout/dot/self-loop.ts +29 -11
  183. package/src/layout/dot/splines-flat-multi.ts +12 -4
  184. package/src/layout/dot/splines-route.ts +10 -11
  185. package/src/layout/dot/straight-edges.ts +5 -3
  186. package/src/layout/fdp/cluster-edges.ts +2 -23
  187. package/src/layout/fdp/tlayout.ts +6 -5
  188. package/src/layout/neato/__fixtures__/conjgrad-d.json +1 -0
  189. package/src/layout/neato/__fixtures__/dim/hier-big-dim3.dot +141 -141
  190. package/src/layout/neato/__fixtures__/dim/hier-big-dim3.xdot +339 -339
  191. package/src/layout/neato/__fixtures__/dim/hier-big-dim4.dot +140 -140
  192. package/src/layout/neato/__fixtures__/dim/hier-big-dim4.xdot +323 -323
  193. package/src/layout/neato/__fixtures__/dim/hier-big-subset-dim3.dot +141 -141
  194. package/src/layout/neato/__fixtures__/dim/hier-big-subset-dim3.xdot +339 -339
  195. package/src/layout/neato/__fixtures__/dim/hier-big110-dim3.dot +251 -251
  196. package/src/layout/neato/__fixtures__/dim/hier-big110-dim3.xdot +535 -535
  197. package/src/layout/neato/__fixtures__/dim/hier-circuit-dim3.dot +14 -14
  198. package/src/layout/neato/__fixtures__/dim/hier-circuit-dim3.xdot +32 -32
  199. package/src/layout/neato/__fixtures__/dim/hier-dim4.dot +8 -8
  200. package/src/layout/neato/__fixtures__/dim/hier-dim4.xdot +14 -14
  201. package/src/layout/neato/__fixtures__/dim/hier-len-dim3.dot +17 -17
  202. package/src/layout/neato/__fixtures__/dim/hier-len-dim3.xdot +35 -35
  203. package/src/layout/neato/__fixtures__/dim/hier-noedge-big.dot +2 -2
  204. package/src/layout/neato/__fixtures__/dim/hier-noedge-big.xdot +2 -2
  205. package/src/layout/neato/__fixtures__/dim/hier-ring-dim3.dot +11 -11
  206. package/src/layout/neato/__fixtures__/dim/hier-ring-dim3.xdot +21 -21
  207. package/src/layout/neato/__fixtures__/dim/hier-star-dim3.dot +14 -14
  208. package/src/layout/neato/__fixtures__/dim/hier-star-dim3.xdot +26 -26
  209. package/src/layout/neato/__fixtures__/dim/hier-stresswt1-dim4.dot +13 -13
  210. package/src/layout/neato/__fixtures__/dim/hier-stresswt1-dim4.xdot +30 -30
  211. package/src/layout/neato/__fixtures__/dim/hier-subset-dim3.dot +10 -10
  212. package/src/layout/neato/__fixtures__/dim/hier-subset-dim3.xdot +19 -19
  213. package/src/layout/neato/__fixtures__/dim/smart-units.json +1 -1
  214. package/src/layout/neato/__fixtures__/hier/cases/dirnone.plain +10 -10
  215. package/src/layout/neato/__fixtures__/hier/cases/tdist.plain +12 -12
  216. package/src/layout/neato/__fixtures__/hier/hier-probe.plain +1 -1
  217. package/src/layout/neato/__fixtures__/hier/hier-probe.xdot +19 -19
  218. package/src/layout/neato/__fixtures__/hier-imds.ts +14 -13
  219. package/src/layout/neato/__fixtures__/hier-majorization.ts +24 -23
  220. package/src/layout/neato/__fixtures__/ipsep/ipsep-grid.xdot +20 -20
  221. package/src/layout/neato/__fixtures__/ipsep/ipsep-k6.xdot +8 -8
  222. package/src/layout/neato/__fixtures__/ipsep/ipsep-rand-len.xdot +34 -34
  223. package/src/layout/neato/__fixtures__/ipsep/ipsep-rand.xdot +32 -32
  224. package/src/layout/neato/__fixtures__/ipsep/ipsep-tree.xdot +18 -18
  225. package/src/layout/neato/__fixtures__/ipsep/kd-ipsep-len.xdot +13 -13
  226. package/src/layout/neato/__fixtures__/ipsep/kd-ipsep.xdot +12 -12
  227. package/src/layout/neato/__fixtures__/models/hier-circuit.xdot +33 -33
  228. package/src/layout/neato/__fixtures__/models/hier-default.xdot +30 -30
  229. package/src/layout/neato/__fixtures__/models/hier-mds-nolen.xdot +30 -30
  230. package/src/layout/neato/__fixtures__/models/hier-pin-disc-circuit.xdot +35 -35
  231. package/src/layout/neato/__fixtures__/models/hier-shortpath.xdot +30 -30
  232. package/src/layout/neato/conjgrad.ts +38 -35
  233. package/src/layout/neato/matrix-ops-dense.ts +2 -20
  234. package/src/layout/neato/matrix-ops.ts +0 -20
  235. package/src/layout/neato/quad-prog-vpsc.ts +7 -5
  236. package/src/layout/neato/smart-ini-x.ts +15 -15
  237. package/src/layout/neato/stress-subspace.ts +5 -5
  238. package/src/layout/sfdp/__fixtures__/dim/dim3-dimen3.dot +6 -6
  239. package/src/layout/sfdp/__fixtures__/dim/dim3.dot +6 -6
  240. package/src/layout/sfdp/__fixtures__/dim/dim4-dimen4.dot +6 -6
  241. package/src/layout/sfdp/__fixtures__/dim/overlap-prism-dim3.dot +6 -6
  242. package/src/layout/sfdp/__fixtures__/dim/regen.sh +2 -0
  243. package/src/model/cgraph-ops.ts +12 -0
  244. package/src/model/graph.ts +13 -2
  245. package/src/model/rankEntry.ts +3 -1
  246. package/src/ortho/index.ts +0 -1
  247. package/src/ortho/partition.ts +10 -22
  248. package/src/parser/builder.ts +67 -100
  249. package/src/parser/graph-attr-syms.ts +62 -0
  250. package/src/pathplan/route.ts +14 -4
  251. package/src/render/dot/agwrite.ts +47 -27
  252. package/src/render/dot/attrs.ts +6 -2
  253. package/src/render/dot/types.ts +7 -2
  254. package/src/render/json-cluster-edge.ts +8 -4
  255. package/src/render/json.ts +15 -8
  256. package/src/render/map.ts +49 -58
  257. package/src/render/svg-cluster.ts +3 -3
  258. package/src/render/svg-graph.ts +1 -3
  259. package/src/render/svg-helpers.ts +6 -6
  260. package/src/render/svg-id.ts +23 -5
  261. package/src/util/overflow.ts +74 -0
  262. package/src/util/printf-round.ts +11 -0
@@ -13,7 +13,7 @@
13
13
 
14
14
  import type { Graph } from '../../model/graph.js';
15
15
  import type { Edge as GraphEdge } from '../../model/edge.js';
16
- import type { Point, Box, Port } from '../../model/geom.js';
16
+ import type { Point, Box } from '../../model/geom.js';
17
17
  import type { Node } from '../../model/node.js';
18
18
  import { VIRTUAL, NORMAL } from './fastgr.js';
19
19
  import type { NodeBox } from './edge-route-geom.js';
@@ -24,10 +24,13 @@ import { beginPath } from '../../common/splines-path-begin.js';
24
24
  import { endPath } from '../../common/splines-path-end.js';
25
25
  import { routeRegularByType } from './splines-route-type.js';
26
26
  import { edgeType, EDGETYPE_LINE, resolveOrigEdge, swapEndsP, swapEdgeSpline } from './splines.js';
27
+ import { chainEnds, makeFwdEdge } from './edge-route-chain-views.js';
27
28
  import { clipAndInstall } from '../../common/splines-clip.js';
28
29
  import { buildDotSinfo } from './self-loop.js';
29
30
  import { TOP, BOTTOM, REGULAREDGE } from '../../common/splines-constants.js';
30
- import { splineMerge, makeLineEdge, resizeVn, straightLen, straightPath } from './splines-route.js';
31
+ import {
32
+ splineMerge, makeLineEdge, resizeVn, straightLen, straightPath, nodeOutSize,
33
+ } from './splines-route.js';
31
34
  import { EDGE_LABEL } from './rank.js';
32
35
  import type { RankEntry } from '../../model/rankEntry.js';
33
36
  import { rankHt } from './edge-route-rank.js';
@@ -37,6 +40,9 @@ import {
37
40
  type BboxCtx,
38
41
  } from './edge-route-faithful.js';
39
42
 
43
+ // Re-export so callers that imported the forward view from this module keep working.
44
+ export { makeFwdEdge } from './edge-route-chain-views.js';
45
+
40
46
  // ---------------------------------------------------------------------------
41
47
  // Faithful multi-rank side-port routing (make_regular_edge hackflag path)
42
48
  // @see lib/dotgen/dotsplines.c:make_regular_edge (forward, abs(rankdiff) > 1)
@@ -85,6 +91,27 @@ function isChainVirtual(n: Node): boolean {
85
91
  return (n.info.node_type ?? NORMAL) === VIRTUAL && !splineMerge(n);
86
92
  }
87
93
 
94
+ /**
95
+ * True when n is a chain vnode with no out edge: where graphviz 16.1.1's
96
+ * make_regular_edge closes the path and drops the edge. `concentrate` leaves
97
+ * such a node at the end of an edge's `ED_to_virt` chain when it orphans the
98
+ * chain's first vnode (upstream issue 2765).
99
+ * @see lib/dotgen/dotsplines.c:make_regular_edge (16.1.1, 4f9ef1d9e)
100
+ */
101
+ function isChainDeadEnd(n: Node): boolean {
102
+ return isChainVirtual(n) && nodeOutSize(n) === 0;
103
+ }
104
+
105
+ /**
106
+ * True when the collected chain is one C routes: it reaches `end`, the real
107
+ * node at its far side, or it stops at a dead end. A chain that stops anywhere
108
+ * else is declined.
109
+ */
110
+ function chainEndsAt(segs: GraphEdge[], end: Node): boolean {
111
+ const tip = segs[segs.length - 1].head;
112
+ return tip === end || isChainDeadEnd(tip);
113
+ }
114
+
88
115
  /**
89
116
  * straight_len smode threshold: a collinear run is segmented once it is ≥ 5
90
117
  * (4+1) with edge labels on the root graph, else ≥ 3 (2+1).
@@ -186,6 +213,27 @@ function closeSmodeSeg(w: ChainWalk, segs: GraphEdge[], ci: number, sl: number,
186
213
  return next;
187
214
  }
188
215
 
216
+ /**
217
+ * Close the path at `segs[ei].head`, a chain vnode with no out edge, and drop
218
+ * the edge. 16.1.1 fails the loop body's `ND_out(hn).size > 0` test there and
219
+ * falls into the segment close with `ND_out(hn).list[0]` NULL: maximal_bbox
220
+ * (cl_bound takes the node's own cluster), endpath on the current edge with
221
+ * the end slope constrained, then the route. `straight_path(NULL, sl)` gives
222
+ * no next edge, so after recover_slack make_regular_edge returns before any
223
+ * clip_and_install. `sl` is always 0 here: straight_len of a node with no out
224
+ * edge is 0, so smode is never entered at a dead end. C's current edge is the
225
+ * one beginpath took until the walk has advanced. Always null: the route is
226
+ * discarded, and a failed route returns before recover_slack (C's `pn == 0`).
227
+ * @see lib/dotgen/dotsplines.c:make_regular_edge (16.1.1, 4f9ef1d9e, a79e42588)
228
+ */
229
+ function closeDeadEnd(
230
+ w: ChainWalk, beginEdge: GraphEdge, segs: GraphEdge[], ei: number, segFirst: number,
231
+ ): null {
232
+ if (!endSeg(w, ei === 0 ? beginEdge : segs[ei], segs[ei], undefined, true)) return null;
233
+ recoverSlack(segs.slice(segFirst), w.P);
234
+ return null;
235
+ }
236
+
189
237
  /** Route the final (post-loop) chain segment, ending at the real head node. */
190
238
  function finishChain(
191
239
  w: ChainWalk, portEdge: GraphEdge, lastEdge: GraphEdge, segFirst: number, segs: GraphEdge[],
@@ -203,7 +251,8 @@ function finishChain(
203
251
  * collinear vnode run is split into spline-top + straight-middle + spline-bottom
204
252
  * via successive beginpath/endpath/routesplines calls, hugging the corridor.
205
253
  * `e` carries the real end ports (first beginpath, final endpath); the chain
206
- * edges drive the box geometry.
254
+ * edges drive the box geometry. A chain that stops at a vnode with no out edge
255
+ * is routed to it and dropped (closeDeadEnd): null, as for a failed route.
207
256
  * @see lib/dotgen/dotsplines.c:make_regular_edge (1771-1873 smode loop)
208
257
  */
209
258
  function routeChainSegmented(
@@ -222,6 +271,8 @@ function routeChainSegmented(
222
271
  segFirst = ei; smode = false;
223
272
  continue;
224
273
  }
274
+ // 16.1.1 walks on only while hn has an out edge (`ND_out(hn).size > 0`).
275
+ if (nodeOutSize(segs[ei].head) === 0) return closeDeadEnd(w, beginEdge, segs, ei, segFirst);
225
276
  si = false;
226
277
  w.boxes.push(maximalBbox(w.ctx, segs[ei].head, segs[ei], segs[ei + 1]));
227
278
  ei++;
@@ -229,70 +280,6 @@ function routeChainSegmented(
229
280
  return finishChain(w, endEdge, segs[ei], segFirst, segs) ? w.pts : null;
230
281
  }
231
282
 
232
- // ---------------------------------------------------------------------------
233
- // Hackflag views for an edge routed on another edge's chain
234
- // @see lib/dotgen/dotsplines.c:make_regular_edge (abs(rank diff) > 1)
235
- // ---------------------------------------------------------------------------
236
-
237
- /** A port copied by value, as C's struct assignment copies `port`. */
238
- function copyPort(p: Port): Port {
239
- return { ...p, p: { ...p.p } };
240
- }
241
-
242
- /**
243
- * C's `fwdedgea`: a copy of `e` from its rank-low end to the first vnode of the
244
- * chain, head port undefined at (0,0), VIRTUAL with `to_orig = e`. beginpath's
245
- * side-port clip reset therefore lands on `e` itself.
246
- */
247
- function hackflagBeginView(e: GraphEdge, back: boolean, firstVnode: Node): GraphEdge {
248
- const tailPort = back ? e.info.head_port : e.info.tail_port;
249
- return {
250
- ...e,
251
- tail: back ? e.head : e.tail,
252
- head: firstVnode,
253
- info: {
254
- ...e.info,
255
- tail_port: copyPort(tailPort),
256
- head_port: { ...copyPort(e.info.head_port), defined: false, p: { x: 0, y: 0 } },
257
- edge_type: VIRTUAL,
258
- to_orig: e,
259
- },
260
- } as GraphEdge;
261
- }
262
-
263
- /**
264
- * C's `fwdedgeb`: for a back edge, `makefwdedge(e)` (VIRTUAL, `to_orig = e`);
265
- * otherwise a plain struct copy of `e` that stays NORMAL, so endpath's
266
- * side-port clip reset lands on the copy and `e` keeps its head clip.
267
- */
268
- function hackflagEndView(e: GraphEdge, back: boolean): GraphEdge {
269
- if (back) return makeFwdEdge(e);
270
- return {
271
- ...e,
272
- info: { ...e.info, tail_port: copyPort(e.info.tail_port), head_port: copyPort(e.info.head_port) },
273
- } as GraphEdge;
274
- }
275
-
276
- /**
277
- * The edges that drive the chain's first beginpath and last endpath. A chain
278
- * owned by `e` itself is C's ordinary walk from the first to the last fast
279
- * edge. A chain owned by another edge means class2 merged `e` into it
280
- * (`merge_chain`), so `e` sits in ND_other and C's group loop hands the real
281
- * edge to make_regular_edge, which takes the hackflag branch: beginpath on
282
- * `fwdedgea`, endpath on `fwdedgeb`. Driving those calls with the owner's
283
- * segments reset the owner's clip flags instead of `e`'s (plantuml-ts 41).
284
- * @see lib/dotgen/dotsplines.c:make_regular_edge (hackflag)
285
- * @see lib/dotgen/class2.c:class2 (merge_chain, other_edge)
286
- */
287
- function chainEnds(e: GraphEdge, segs: GraphEdge[]): [GraphEdge, GraphEdge] {
288
- const first = segs[0];
289
- const last = segs[segs.length - 1];
290
- const orig = resolveOrigEdge(e);
291
- if (resolveOrigEdge(first) === orig) return [first, last];
292
- const back = (orig.head.info.rank ?? 0) < (orig.tail.info.rank ?? 0);
293
- return [hackflagBeginView(orig, back, first.head), hackflagEndView(orig, back)];
294
- }
295
-
296
283
  /** True when `e` runs forward across two or more ranks of a ranked graph. */
297
284
  function spansRanksForward(g: Graph, e: GraphEdge): boolean {
298
285
  const r = e.tail.info.rank;
@@ -305,9 +292,10 @@ function spansRanksForward(g: Graph, e: GraphEdge): boolean {
305
292
  * faithful pipeline (REGULAREDGE side boxes steer the port, routeSplines over
306
293
  * the chain; adjustregularpath inter-rank widening finally fires). Returns
307
294
  * control points in graphviz-internal y-up, null when the edge is not a
308
- * multi-rank chain, or 'lost' when the chain walk was attempted and failed
309
- * (completeregularpath / routesplines failure): C's make_regular_edge returns
310
- * with no spline and the edge is never routed again.
295
+ * multi-rank chain, or 'lost' when the chain walk was attempted and gave no
296
+ * spline (a completeregularpath / routesplines failure, or a chain that ends
297
+ * at a vnode with no out edge): C's make_regular_edge returns with no spline
298
+ * and the edge is never routed again.
311
299
  * @see lib/dotgen/dotsplines.c:make_regular_edge (hackflag forward path)
312
300
  */
313
301
  export function routeMultiRankEdgeFaithful(g: Graph, e: GraphEdge): Point[] | null | 'lost' {
@@ -321,11 +309,12 @@ export function routeMultiRankEdgeFaithful(g: Graph, e: GraphEdge): Point[] | nu
321
309
  if (line !== null) return line;
322
310
  }
323
311
  const segs = chainSegments(e);
324
- // Route only a COMPLETE chain (last segment reaches e.head). segs.length===1 is
325
- // valid (concentrate may merge the whole chain to a direct tail->head segment);
326
- // a chain ending at a virtual node is broken — bail, don't crash the loop.
312
+ // Route a COMPLETE chain (last segment reaches e.head). segs.length===1 is
313
+ // valid (concentrate may merge the whole chain to a direct tail->head segment).
314
+ // A chain that stops at a vnode with no out edge is routed too, to that dead
315
+ // end, as 16.1.1 does; one that stops at any other virtual node is declined.
327
316
  // @see dotsplines.c:make_regular_edge
328
- if (segs.length === 0 || segs[segs.length - 1].head !== e.head) return null;
317
+ if (segs.length === 0 || !chainEndsAt(segs, e.head)) return null;
329
318
  // C's make_regular_edge drives beginpath with the FIRST chain fast edge and
330
319
  // endpath with the LAST (each carries the orig's copied port, so a dyna
331
320
  // record port resolves toward the adjacent-rank vnode, not the far real
@@ -339,17 +328,19 @@ export function routeMultiRankEdgeFaithful(g: Graph, e: GraphEdge): Point[] | nu
339
328
  /**
340
329
  * The merge-bounded run owned by one collected entry: the entry followed by
341
330
  * `out.list[0]` successors while the head is a plain (non-merge) virtual node —
342
- * C make_regular_edge's chain walk. Returns null when the entry is NOT a
343
- * partial run (neither end at a splineMerge node): plain single-chain entries
344
- * belong to the caller's whole-chain routers.
331
+ * C make_regular_edge's chain walk. The run also stops at a vnode with no out
332
+ * edge, 16.1.1's dead end. Returns null when the entry is NOT a partial run
333
+ * (neither end at a splineMerge node): plain single-chain entries belong to
334
+ * the caller's whole-chain routers.
345
335
  * @see lib/dotgen/dotsplines.c:make_regular_edge (while !splineMerge(hn) walk)
336
+ * @see lib/dotgen/dotsplines.c:make_regular_edge (16.1.1, 4f9ef1d9e)
346
337
  */
347
338
  function entryRunSegments(e: GraphEdge): GraphEdge[] | null {
348
339
  const segs: GraphEdge[] = [e];
349
340
  let hn = e.head;
350
341
  while ((hn.info.node_type ?? NORMAL) === VIRTUAL && !splineMerge(hn)) {
351
- const nxt = hn.info.out?.list[0];
352
- if (nxt === undefined) return null; // broken chain — leave to the caller
342
+ if (nodeOutSize(hn) === 0) break; // dead end: routeChainSegmented closes there
343
+ const nxt = hn.info.out!.list[0];
353
344
  segs.push(nxt);
354
345
  hn = nxt.head;
355
346
  }
@@ -369,8 +360,9 @@ function entryRunSegments(e: GraphEdge): GraphEdge[] | null {
369
360
  * orig) with the walk's final head node, exactly as C.
370
361
  *
371
362
  * Returns the resolved original edge when the entry was a partial run
372
- * (consumed — even if routing declined, so the caller never re-routes the
373
- * whole chain), or null when the entry is a plain full chain.
363
+ * (consumed — even if routing declined or the run stopped at a dead end, so
364
+ * the caller never re-routes the whole chain), or null when the entry is a
365
+ * plain full chain.
374
366
  * @see lib/dotgen/dotsplines.c:dot_splines_ (group loop), make_regular_edge
375
367
  */
376
368
  export function routeEntryRun(g: Graph, entry: GraphEdge): GraphEdge | null {
@@ -387,21 +379,35 @@ export function routeEntryRun(g: Graph, entry: GraphEdge): GraphEdge | null {
387
379
  * contains it (boxes run high-y to low-y). Label vnodes are anchored to the
388
380
  * box's right edge (center = box.ur.x + rw); plain vnodes are centred. This is
389
381
  * what aligns a flat-adj aux label vnode onto its spline so the label lands at
390
- * the dot position (DOT-12). @see lib/dotgen/dotsplines.c:recover_slack
382
+ * the dot position (DOT-12). @see lib/dotgen/dotsplines.c:recover_slack (resize_vn calls)
391
383
  */
392
384
  function resizeVnInBox(vn: Node, box: Box): void {
393
385
  if (vn.info.label != null) resizeVn(vn, box.ll.x, box.ur.x, box.ur.x + vn.info.rw);
394
386
  else resizeVn(vn, box.ll.x, (box.ll.x + box.ur.x) / 2, box.ur.x);
395
387
  }
396
388
 
397
- function recoverSlack(segs: GraphEdge[], P: Path): void {
389
+ /**
390
+ * Walk the routed segment's chain vnodes (`segs[i].head`, C's
391
+ * `vn = aghead(ND_out(vn).list[0])` step) and resize each into its box. The
392
+ * walk ends at a vnode with no out edge, whether or not a box reached it:
393
+ * 16.1.1's two `ND_out(vn).size == 0` exits, where 16.1.0 stepped through a
394
+ * NULL `list[0]` (upstream issue 2765). Exported for tests only: not part of
395
+ * the package's entry points.
396
+ * @see lib/dotgen/dotsplines.c:recover_slack (16.1.1, f3da7c569)
397
+ */
398
+ export function recoverSlack(segs: GraphEdge[], P: Path): void {
398
399
  let b = 0; // skip first rank box
399
400
  for (let i = 0; i < segs.length; i++) {
400
401
  const vn = segs[i].head;
401
- if ((vn.info.node_type ?? NORMAL) !== VIRTUAL || splineMerge(vn)) break;
402
+ if (!isChainVirtual(vn)) break;
402
403
  while (b < P.nbox && P.boxes[b].ll.y > vn.info.coord.y) b++;
403
404
  if (b >= P.nbox) break;
404
- if (P.boxes[b].ur.y >= vn.info.coord.y) resizeVnInBox(vn, P.boxes[b]);
405
+ if (P.boxes[b].ur.y < vn.info.coord.y) {
406
+ if (nodeOutSize(vn) === 0) break;
407
+ continue;
408
+ }
409
+ resizeVnInBox(vn, P.boxes[b]);
410
+ if (nodeOutSize(vn) === 0) break;
405
411
  }
406
412
  }
407
413
 
@@ -427,39 +433,17 @@ function backChainSegments(e: GraphEdge): GraphEdge[] {
427
433
  }
428
434
 
429
435
  /**
430
- * Forward view of any edge (C `makefwdedge`): tail/head swapped when the edge
431
- * runs high→low rank (a back edge), fresh portless ends. `beginPath`/`endPath`
432
- * read `e.tail`/`e.head`, so the chain must be driven by an edge that runs
433
- * low→high rank. Sets `to_orig`/`edge_type=VIRTUAL` so `clipAndInstall`→
434
- * `newSpline` installs the spline on the ORIGINAL edge (its `to_orig` loop) and
435
- * the post-routing `swapSpline` pass reverses it back to tail→head. Shares
436
- * `attrs`/`root`/`to_virt` with `e`. Consumed by T3 for parallel back-members.
437
- * @see lib/dotgen/dotsplines.c:makefwdedge
438
- * @see lib/common/splines.c:clip_and_install (to_orig install loop)
436
+ * True when routeBackEdge routes the collected chain: one of two or more
437
+ * segments that reaches `end` (the back edge's tail), or one of any length
438
+ * that stops at a dead end, which 16.1.1 routes to and drops.
439
+ * @see lib/dotgen/dotsplines.c:make_regular_edge (16.1.1, 4f9ef1d9e)
439
440
  */
440
- export function makeFwdEdge(e: GraphEdge): GraphEdge {
441
- const back = (e.head.info.rank ?? 0) < (e.tail.info.rank ?? 0);
442
- const [tail, head] = back ? [e.head, e.tail] : [e.tail, e.head];
443
- return {
444
- ...e,
445
- tail,
446
- head,
447
- info: {
448
- ...e.info,
449
- // C makefwdedge SWAPS the ports (does not strip them): a back edge keeps
450
- // its corner ports across the forward view so make_regular_edge curls it
451
- // (e.g. #241_0 aux back-edge clone sw/se → size 7). Stripping to an empty
452
- // Center port straightened every port-bearing back edge (size 4).
453
- // @see lib/dotgen/dotsplines.c:makefwdedge (ED_tail_port(new)=ED_head_port(old))
454
- tail_port: e.info.head_port ?? makePort(),
455
- head_port: e.info.tail_port ?? makePort(),
456
- to_orig: e,
457
- edge_type: VIRTUAL,
458
- },
459
- } as GraphEdge;
441
+ function isRoutableBackChain(segs: GraphEdge[], end: Node): boolean {
442
+ if (segs.length === 0) return false;
443
+ const tip = segs[segs.length - 1].head;
444
+ return tip === end ? segs.length >= 2 : isChainDeadEnd(tip);
460
445
  }
461
446
 
462
-
463
447
  /**
464
448
  * Route a multi-rank back-edge via its virtual node chain, mirroring C exactly:
465
449
  * make_regular_edge routes the makefwdedge (forward) view, clip_and_install
@@ -471,11 +455,13 @@ export function makeFwdEdge(e: GraphEdge): GraphEdge {
471
455
  * opposite parameter end), landing the 0.5-tolerance crossing ~0.26-0.33 off
472
456
  * (1644, dfa family). clipAndInstall resolves the spline onto `e` via the
473
457
  * fwdEdge's to_orig and gates arrows by ED_dir, so this also drops the bespoke
474
- * reverse/arrow code. @see lib/dotgen/dotsplines.c:make_regular_edge (hackflag)
458
+ * reverse/arrow code. A chain that stops at a vnode with no out edge is routed
459
+ * to that dead end and the edge is lost, as 16.1.1 does.
460
+ * @see lib/dotgen/dotsplines.c:make_regular_edge (hackflag)
475
461
  */
476
462
  export function routeBackEdge(e: GraphEdge, _tailBox: NodeBox, _headBox: NodeBox, g: Graph): void {
477
463
  const segs = backChainSegments(e);
478
- if (segs.length < 2 || segs[segs.length - 1].head !== e.tail) return;
464
+ if (!isRoutableBackChain(segs, e.tail)) return;
479
465
  const fwdEdge = makeFwdEdge(e);
480
466
  // C's make_regular_edge routes the collected FAST SEGMENT view: beginpath's
481
467
  // edge head is the adjacent chain vnode, so a dyna record port resolves
@@ -43,7 +43,7 @@ import { edgeType } from './splines.js';
43
43
  import { addBox } from '../../common/splines-path-shared.js';
44
44
  import { TOP, BOTTOM, REGULAREDGE } from '../../common/splines-constants.js';
45
45
  import { VIRTUAL, NORMAL } from './fastgr.js';
46
- import { neighbor, topBound, botBound, nodeType, splineMerge } from './splines-route.js';
46
+ import { neighbor, topBound, botBound, nodeType, splineMerge, nodeOutSize } from './splines-route.js';
47
47
  import { getSplineBounds, rankHt } from './edge-route-rank.js';
48
48
  import { graphRanksep } from './position-aux.js';
49
49
 
@@ -90,10 +90,12 @@ function isLabeledVirtual(n: Node): boolean {
90
90
  return nodeType(n) === VIRTUAL && n.info.label != null;
91
91
  }
92
92
 
93
- /** Tail/head clusters behind node n: its own cluster when NORMAL, else the
94
- * original edge's endpoint clusters. @see cl_bound (dotsplines.c:2135-2140) */
93
+ /** Tail/head clusters behind node n: its own cluster when NORMAL or when it
94
+ * has no out edge (a virtual node concentrate orphaned, upstream issue 2765),
95
+ * else the original edge's endpoint clusters.
96
+ * @see lib/dotgen/dotsplines.c:cl_bound (16.1.1, f8e3f2100) */
95
97
  function endpointClusters(n: Node): [Graph | undefined, Graph | undefined] {
96
- if (nodeType(n) === NORMAL) return [n.info.clust, n.info.clust];
98
+ if (nodeType(n) === NORMAL || nodeOutSize(n) === 0) return [n.info.clust, n.info.clust];
97
99
  const orig = n.info.out?.list[0]?.info.to_orig;
98
100
  return [orig?.tail.info.clust, orig?.head.info.clust];
99
101
  }
@@ -281,9 +283,22 @@ function pathBoundsReady(first: Edge, last: Edge): boolean {
281
283
  && !boundMissing(botBound(last, -1)) && !boundMissing(botBound(last, 1));
282
284
  }
283
285
 
284
- /** Grow rank boxes (even slots) / inter-rank boxes (odd) to a minimum width. */
286
+ /**
287
+ * Grow rank boxes (even slots) / inter-rank boxes (odd) to a minimum width.
288
+ *
289
+ * C runs `fb - 1 .. lb` whatever `P->nbox` is: when `add_box` dropped boxes,
290
+ * the loop reaches slots at or past `nbox` of the array dot_splines_
291
+ * preallocates (dotsplines.c:338) and widens them. Those writes are dead:
292
+ * every reader of a path's boxes stops at `nbox` (routesplines_, checkpath,
293
+ * printpath, recover_slack, this function's second loop), and `add_box`
294
+ * overwrites slot `nbox` before it counts it. The port's array holds no such
295
+ * slot (or a stale reference to a box that is live elsewhere, which C's
296
+ * struct copies cannot reach), so it skips them.
297
+ * @see lib/dotgen/dotsplines.c:adjustregularpath (first loop)
298
+ */
285
299
  function widenPathBoxes(P: Path, fb: number, lb: number): void {
286
300
  for (let i = fb - 1; i < lb + 1; i++) {
301
+ if (i >= P.nbox) continue;
287
302
  const bp = P.boxes[i];
288
303
  const even = (i - fb) % 2 === 0;
289
304
  const tooNarrow = even ? bp.ll.x >= bp.ur.x : bp.ll.x + MINW > bp.ur.x;
@@ -356,18 +356,21 @@ export function hasInterveningNode(rk: RankEntry, lo: number, hi: number): boole
356
356
  * `g` is the graph being laid out — C's `dot_root(tn)`, NOT `agroot(tn)`. See
357
357
  * `flatNode` above for why these differ under `pack`.
358
358
  *
359
+ * A self-loop is never adjacent: C's `for (i = lo + 1; i < hi; i++)` leaves
360
+ * `i == lo + 1` when `lo == hi`, so its `if (i == hi)` is false. dot_splines_'
361
+ * group loop reads the flag on loops too (dotsplines.c:366), so marking one
362
+ * would merge a node's loops across portcmp breaks.
363
+ *
359
364
  * @see lib/dotgen/flat.c:checkFlatAdjacent (rank = &GD_rank(dot_root(tn))[...])
365
+ * @see lib/dotgen/flat.c:checkFlatAdjacent (16.1.1) 224-236
360
366
  */
361
367
  export function checkFlatAdjacent(g: Graph, e: Edge): void {
362
368
  const tOrd = nodeOrder(e.tail);
363
369
  const hOrd = nodeOrder(e.head);
364
370
  const lo = Math.min(tOrd, hOrd);
365
371
  const hi = Math.max(tOrd, hOrd);
366
- let adjacent = hi - lo <= 1;
367
- if (!adjacent) {
368
- adjacent = !hasInterveningNode(g.info.rank![nodeRank(e.tail)], lo, hi);
369
- }
370
- if (!adjacent) return;
372
+ if (hi <= lo) return;
373
+ if (hasInterveningNode(g.info.rank![nodeRank(e.tail)], lo, hi)) return;
371
374
  for (let le: Edge | undefined = e; le; le = le.info.to_virt) le.info.adjacent = 1;
372
375
  }
373
376
 
@@ -401,30 +404,48 @@ export function markAdjacent(g: Graph): void {
401
404
  }
402
405
  }
403
406
 
407
+ /** True when `edges` holds a labeled edge that is not flat-adjacent.
408
+ * @see lib/dotgen/flat.c:flat_edges (16.1.1, 4e50c9abd) 277-289 */
409
+ function hasNonAdjacentLabel(edges: EdgeList | undefined): boolean {
410
+ if (!edges) return false;
411
+ for (let j = 0; j < edges.size; j++) {
412
+ if (isLabeledFlat(edges.list[j]) && !edges.list[j].info.adjacent) return true;
413
+ }
414
+ return false;
415
+ }
416
+
417
+ /**
418
+ * Per node of the rank, C scans `ND_flat_in` and then `ND_other` (the scan
419
+ * upstream 4e50c9abd added for gitlab #2756) for a labeled non-adjacent edge.
420
+ * The `ND_other` scan has no rank filter and no loop filter, so a labeled
421
+ * self-loop counts: checkFlatAdjacent never marks one.
422
+ * @see lib/dotgen/flat.c:flat_edges (16.1.1, 4e50c9abd) 276-290
423
+ */
404
424
  export function rankHasNonAdjacentLabel(rk: RankEntry): boolean {
405
425
  for (let i = 0; i < rk.n; i++) {
406
- const fo = rk.v[i].info.flat_out;
407
- if (!fo) continue;
408
- for (let j = 0; j < fo.size; j++) {
409
- if (isLabeledFlat(fo.list[j]) && !fo.list[j].info.adjacent) return true;
410
- }
426
+ const n = rk.v[i];
427
+ if (hasNonAdjacentLabel(n.info.flat_in)) return true;
428
+ if (hasNonAdjacentLabel(n.info.other)) return true;
411
429
  }
412
430
  return false;
413
431
  }
414
432
 
415
433
  /**
416
- * AD-1: C gates this on `GD_rank(g)[0].flat || GD_n_cluster(g) > 0` and then
417
- * scans for a labeled non-adjacent flat edge on rank mn. In this port the
418
- * `rank[mn].flat` adjacency matrix is unreliable at position-time (mincross
419
- * builds it before `flat_out` is populated), so we detect the rank-mn labeled
420
- * non-adjacent flat edge directly via `flat_out` — which is the real gate. This
421
- * only fires for a graph with such an edge on the lowest rank (none of the 115
422
- * goldens have one), so it is byte-safe. @see lib/dotgen/flat.c:flat_edges 279
434
+ * C's gate, `GD_rank(g)[0].flat || GD_n_cluster(g) > 0`, then the scan of the
435
+ * lowest rank. The matrix field is read as a flag: cleanup2 calls
436
+ * `free_matrix(GD_rank(g)[r].flat)` (mincross.c:864) and free_matrix
437
+ * (mincross.c:415-420) frees without nulling it, so here it is non-null
438
+ * exactly when some flat_breakcycles run on the root saw a node of the rank
439
+ * with a flat out-edge (FLATORDER edges included). `cleanup2Ranks` keeps the
440
+ * field for this read. Without the gate the `ND_other` scan would add a rank
441
+ * for a labeled rank-0 self-loop in a graph where C adds none.
442
+ * @see lib/dotgen/flat.c:flat_edges (16.1.1, 4e50c9abd) 274-291
423
443
  */
424
444
  export function needsAbomination(g: Graph): boolean {
425
445
  const mn = graphMinrank(g);
426
446
  const rank = g.info.rank;
427
447
  if (!rank || rank[mn] === undefined) return false;
448
+ if (rank[mn].flat === undefined && (g.info.n_cluster ?? 0) <= 0) return false;
428
449
  return rankHasNonAdjacentLabel(rank[mn]);
429
450
  }
430
451
 
@@ -150,15 +150,25 @@ export function postorder(g: Graph, v: Node, list: Node[], r: number): void {
150
150
  list.push(v);
151
151
  }
152
152
 
153
- // flat_reorder — @see lib/dotgen/mincross.c:flat_reorder
154
- export function flatReorderBuildTemprank(g: Graph, rk: RankEntry, temprank: Node[], flip: boolean): void {
153
+ /**
154
+ * Builds the reverse topological sort order of one rank in `temprank` and
155
+ * returns C's `valid_rank`: false as soon as the walk meets a node that is
156
+ * neither free of constraining flat edges nor an unmarked source (C's
157
+ * `break`), which tells the caller to leave the rank as mincross ordered it.
158
+ * Graphviz 16.1.1, upstream 9fb21f570.
159
+ *
160
+ * @see lib/dotgen/mincross.c:flat_reorder
161
+ */
162
+ export function flatReorderBuildTemprank(g: Graph, rk: RankEntry, temprank: Node[], flip: boolean): boolean {
155
163
  for (let i = 0; i < rk.n; i++) {
156
164
  const v = flip ? rankGet(rk, i) : rankGet(rk, rk.n - i - 1);
157
165
  const inCnt = v.info.flat_in !== undefined ? countConstraining(g, v.info.flat_in) : 0;
158
166
  const outCnt = v.info.flat_out !== undefined ? countConstraining(g, v.info.flat_out) : 0;
159
167
  if (inCnt === 0 && outCnt === 0) { temprank.push(v); continue; }
160
- if (!v.info.mark && inCnt === 0) postorder(g, v, temprank, v.info.rank !== undefined ? v.info.rank! : 0);
168
+ if (!v.info.mark && inCnt === 0) { postorder(g, v, temprank, v.info.rank !== undefined ? v.info.rank! : 0); continue; }
169
+ return false;
161
170
  }
171
+ return true;
162
172
  }
163
173
 
164
174
  export function flatReorderFixEdges(g: Graph, rk: RankEntry, flip: boolean): void {
@@ -188,8 +198,8 @@ export function flatReorderRank(g: Graph, rk: RankEntry, rootRank: RankEntry[],
188
198
  const baseOrder = rankGet(rk, 0).info.order !== undefined ? rankGet(rk, 0).info.order! : 0;
189
199
  for (let i = 0; i < rk.n; i++) rankGet(rk, i).info.mark = 0;
190
200
  const temprank: Node[] = [];
191
- flatReorderBuildTemprank(g, rk, temprank, flip);
192
- if (temprank.length > 0) {
201
+ const validRank = flatReorderBuildTemprank(g, rk, temprank, flip);
202
+ if (validRank && temprank.length > 0) {
193
203
  if (!flip) temprank.reverse();
194
204
  for (let i = 0; i < rk.n; i++) {
195
205
  rankSet(rk, i, temprank[i]!);
@@ -237,7 +237,12 @@ export function cleanup2(ctx: MincrossContext, g: Graph): void {
237
237
  cleanup2Ranks(g);
238
238
  }
239
239
 
240
- /** @see lib/dotgen/mincross.c:cleanup2 (rank loop) */
240
+ /**
241
+ * `rank[r].flat` is left set: C's `free_matrix(GD_rank(g)[r].flat)` frees the
242
+ * matrix without nulling the field (mincross.c:415-420), and flat_edges then
243
+ * reads `GD_rank(g)[0].flat` as a flag (flat.c:274, `needsAbomination`).
244
+ * @see lib/dotgen/mincross.c:cleanup2 (rank loop; 16.1.1 853-865)
245
+ */
241
246
  export function cleanup2Ranks(g: Graph): void {
242
247
  const rank = g.info.rank;
243
248
  if (!rank) return;
@@ -248,7 +253,6 @@ export function cleanup2Ranks(g: Graph): void {
248
253
  rank[r].v[i].info.order = i;
249
254
  removeFlatorderEdges(rank[r].v[i]);
250
255
  }
251
- rank[r].flat = undefined;
252
256
  }
253
257
  }
254
258
 
@@ -5,6 +5,7 @@ import type { Edge } from '../../model/edge.js';
5
5
  import { addTreeEdge, isTreeEdge, nsSlack } from './ns-core.js';
6
6
  import type { NsCtx } from './ns-core.js';
7
7
  import { initCutvalues } from './ns-range.js';
8
+ import { saddSaturate } from '../../util/overflow.js';
8
9
 
9
10
  // ---------------------------------------------------------------------------
10
11
  // Types
@@ -262,19 +263,22 @@ export function stExtractMin(heap: STheap): Subtree {
262
263
 
263
264
  /**
264
265
  * Add `delta` to ND_rank of every node in the tight tree reachable from `v`
265
- * (the parent back-edge to `from` excluded). C's `tree_adjust` is a recursive
266
- * DFS; the tight tree is acyclic, so each node is visited exactly once and the
267
- * mutation (`rank += delta`) is order-independent. Ported with an explicit
268
- * stack instead of recursion: deep trees (e.g. corpus 2646, a 7-deep quadtree
269
- * with thousands of nodes) overflow the JS call stack where C's larger native
270
- * stack does not. Outcome is identical to the recursive form.
266
+ * (the parent back-edge to `from` excluded), clamping each sum to
267
+ * [INT_MIN, INT_MAX] as C's `sadd_saturate` does (graphviz 16.1.1, upstream
268
+ * issue 2758). C's `tree_adjust` is a DFS; the tight tree is acyclic, so each
269
+ * node is visited exactly once and the mutation
270
+ * (`rank = sadd_saturate(rank, delta)`) depends only on the node's own rank,
271
+ * which makes it order-independent. Ported with an explicit stack instead of
272
+ * recursion: deep trees (e.g. corpus 2646, a 7-deep quadtree with thousands of
273
+ * nodes) overflow the JS call stack where C's larger native stack does not.
274
+ * Outcome is identical to the recursive form.
271
275
  * @see lib/common/ns.c:tree_adjust
272
276
  */
273
277
  export function treeAdjust(v: Node, from: Node | undefined, delta: number): void {
274
278
  const stack: Array<[Node, Node | undefined]> = [[v, from]];
275
279
  while (stack.length > 0) {
276
280
  const [n, f] = stack.pop()!;
277
- n.info.rank = (n.info.rank ?? 0) + delta;
281
+ n.info.rank = saddSaturate(n.info.rank ?? 0, delta);
278
282
  const ti = n.info.tree_in;
279
283
  if (ti) for (let i = 0; i < ti.size; i++) {
280
284
  const w = ti.list[i].tail;
@@ -216,9 +216,10 @@ export function initSubg(sg: Graph, root: Graph): void {
216
216
  * ranking reads them. Node sizing in `dotPhaseInit` reads `sg.root.info.flip`
217
217
  * (the real root), so it is correct regardless of this ordering.
218
218
  *
219
- * Returns the component's dot_splines status (non-zero = C's dotLayout(sg)
220
- * failing, which makes doDot return before packing). The mincross/position
221
- * statuses are not consulted on this arm.
219
+ * Returns the component's dot_position status when it is non-zero (16.1.1: an
220
+ * aux edge longer than INT_MAX, issue #162), else its dot_splines status. A
221
+ * non-zero status is C's dotLayout(sg) failing, which makes doDot return
222
+ * before packing. The mincross status is not consulted on this arm (#235).
222
223
  * @see lib/dotgen/dotinit.c:dotLayout (static)
223
224
  */
224
225
  export function dotLayoutComponent(sg: Graph, root: Graph): number {
@@ -248,7 +249,8 @@ export function dotLayoutComponent(sg: Graph, root: Graph): number {
248
249
  sg.info.has_labels = 0;
249
250
  dotRank(sg);
250
251
  dotMincross(sg);
251
- dotPosition(sg);
252
+ const posRc = dotPosition(sg);
253
+ if (posRc !== 0) return posRc;
252
254
  return dotPhasePostNoFinish(sg);
253
255
  }
254
256