@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
@@ -15,10 +15,15 @@ import { SLACKNODE, EDGE_LABEL } from './rank.js';
15
15
  import { selfRightSpace } from '../../common/splines-selfedge.js';
16
16
  import { lateInt } from '../../common/nodeinit.js';
17
17
  import { cround } from '../../common/arith.js';
18
+ import { agerrorf } from '../../common/agerr.js';
19
+ import { printfFixed } from '../../util/printf-round.js';
18
20
 
19
21
  /** @see lib/common/const.h:CL_OFFSET */
20
22
  export const CL_OFFSET = 8;
21
23
 
24
+ /** C `INT_MAX`: the longest aux edge `make_aux_edge` accepts. */
25
+ const INT_MAX = 2147483647;
26
+
22
27
  /**
23
28
  * Cluster margin = `late_int(g, G_margin, CL_OFFSET, 0)`. The `margin` attribute
24
29
  * is resolved up the subgraph chain (matching `agxget` inheritance), so a
@@ -104,12 +109,27 @@ export function graphRanksep(g: Graph): number { return g.info.ranksep ?? 0; }
104
109
  * Agedgeinfo_t it allocates here and never writes them. Its attributes are the
105
110
  * shared empty map: C binds no attribute record to an aux edge, only the
106
111
  * Agedgeinfo_t (issue #145).
112
+ *
113
+ * A length above INT_MAX is refused: the error prints and the result is null,
114
+ * with no edge linked into the fast lists (graphviz 16.1.1, upstream
115
+ * f86899fcc; 16.1.0 clamped the length). make_LR_constraints and
116
+ * make_edge_pairs test the result and fail the layout; every other caller
117
+ * ignores it, as C does, and simply has no edge there. The `%f` is
118
+ * `printfFixed`: `toFixed` goes exponential at 1e21, and corpus 2781's length
119
+ * is 7.2e25.
107
120
  * @see lib/dotgen/position.c:make_aux_edge
108
121
  */
109
- export function makeAuxEdge(u: Node, v: Node, len: number, wt: number): Edge {
122
+ export function makeAuxEdge(u: Node, v: Node, len: number, wt: number): Edge | null {
123
+ if (len > INT_MAX) {
124
+ agerrorf(
125
+ `Edge length ${printfFixed(len, 6)} larger than maximum ${INT_MAX} allowed.\n`
126
+ + 'Check for overwide node(s).',
127
+ );
128
+ return null;
129
+ }
110
130
  const e = new EdgeClass(u, v, '', true, true);
111
131
  // C ROUND() is half away from zero. @see lib/dotgen/position.c:195
112
- e.info.minlen = cround(Math.min(len, 2147483647));
132
+ e.info.minlen = cround(len);
113
133
  e.info.weight = wt;
114
134
  fastEdge(e);
115
135
  return e;
@@ -154,8 +174,11 @@ export function lrSep(g: Graph, rankIdx: number): number {
154
174
  return (rankIdx + (g.info.abomShift ?? 0)) & 1 ? 5 : nodesep;
155
175
  }
156
176
 
157
- /** @see lib/dotgen/position.c:make_LR_constraints (single flat-edge constraint) */
158
- export function processFlatEdge(g: Graph, u: Node, e: Edge): void {
177
+ /**
178
+ * Returns -1 when the aux edge is refused (16.1.1, upstream e62e35be5), else 0.
179
+ * @see lib/dotgen/position.c:make_LR_constraints (single flat-edge constraint)
180
+ */
181
+ export function processFlatEdge(g: Graph, u: Node, e: Edge): number {
159
182
  const nodesep = graphNodesep(g);
160
183
  const isOrdered = nodeOrder(u) < nodeOrder(e.head);
161
184
  const t0 = isOrdered ? e.tail : e.head;
@@ -175,44 +198,61 @@ export function processFlatEdge(g: Graph, u: Node, e: Edge): void {
175
198
  ex.info.minlen = Math.max(ex.info.minlen ?? 1, m0);
176
199
  ex.info.weight = Math.max(ex.info.weight ?? 0, edgeWeight(e));
177
200
  } else if (!e.info.label) {
178
- makeAuxEdge(t0, h0, m0, edgeWeight(e));
201
+ if (makeAuxEdge(t0, h0, m0, edgeWeight(e)) === null) return -1;
179
202
  }
203
+ return 0;
180
204
  }
181
205
 
182
- /** @see lib/dotgen/position.c:make_LR_constraints (flat-edge endpoint loop) */
183
- export function makeFlatEdgeConstraints(g: Graph, u: Node): void {
206
+ /**
207
+ * Returns -1 at the first refused aux edge, else 0.
208
+ * @see lib/dotgen/position.c:make_LR_constraints (flat-edge endpoint loop)
209
+ */
210
+ export function makeFlatEdgeConstraints(g: Graph, u: Node): number {
184
211
  const flatOut = u.info.flat_out;
185
- if (!flatOut) return;
212
+ if (!flatOut) return 0;
186
213
  for (let k = 0; k < flatOut.size; k++) {
187
- processFlatEdge(g, u, flatOut.list[k]);
214
+ if (processFlatEdge(g, u, flatOut.list[k]) !== 0) return -1;
188
215
  }
216
+ return 0;
189
217
  }
190
218
 
191
- /** @see lib/dotgen/position.c:make_LR_constraints (flat-label constraint body) */
219
+ /**
220
+ * Returns -1 when either aux edge is refused (16.1.1, upstream e62e35be5); the
221
+ * second is not tried after the first fails. Else 0.
222
+ * @see lib/dotgen/position.c:make_LR_constraints (flat-label constraint body)
223
+ */
192
224
  export function applyFlatLabel(
193
225
  g: Graph,
194
226
  e: Edge,
195
227
  e0raw: Edge,
196
228
  e1raw: Edge,
197
- ): void {
229
+ ): number {
198
230
  const nodesep = graphNodesep(g);
199
231
  const e0 = nodeOrder(e0raw.head) <= nodeOrder(e1raw.head) ? e0raw : e1raw;
200
232
  const e1 = e0 === e0raw ? e1raw : e0raw;
201
233
  // C: int m0 (position.c:222,276), so the half truncates
202
234
  const m0 = Math.trunc(edgeMinlen(e) * nodesep / 2);
203
235
  const m1a = m0 + nodeRw(e0.head) + nodeLw(e0.tail);
204
- if (!canReach(e0.tail, e0.head)) makeAuxEdge(e0.head, e0.tail, m1a, edgeWeight(e));
236
+ if (!canReach(e0.tail, e0.head)) {
237
+ if (makeAuxEdge(e0.head, e0.tail, m1a, edgeWeight(e)) === null) return -1;
238
+ }
205
239
  const m1b = m0 + nodeRw(e1.tail) + nodeLw(e1.head);
206
- if (!canReach(e1.head, e1.tail)) makeAuxEdge(e1.tail, e1.head, m1b, edgeWeight(e));
240
+ if (!canReach(e1.head, e1.tail)) {
241
+ if (makeAuxEdge(e1.tail, e1.head, m1b, edgeWeight(e)) === null) return -1;
242
+ }
243
+ return 0;
207
244
  }
208
245
 
209
- /** @see lib/dotgen/position.c:make_LR_constraints (flat-label constraints via ND_alg) */
210
- export function makeFlatLabelConstraints(g: Graph, u: Node): void {
246
+ /**
247
+ * Returns -1 when a label aux edge is refused, else 0.
248
+ * @see lib/dotgen/position.c:make_LR_constraints (flat-label constraints via ND_alg)
249
+ */
250
+ export function makeFlatLabelConstraints(g: Graph, u: Node): number {
211
251
  const e = u.info.posAlg;
212
- if (!e) return;
252
+ if (!e) return 0;
213
253
  const so = u.info.save_out;
214
- if (!so || so.size < 2) return;
215
- applyFlatLabel(g, e, so.list[0], so.list[1]);
254
+ if (!so || so.size < 2) return 0;
255
+ return applyFlatLabel(g, e, so.list[0], so.list[1]);
216
256
  }
217
257
 
218
258
  /** @see lib/dotgen/position.c:go */
@@ -243,11 +283,16 @@ export function selfWidth(u: Node): number {
243
283
  return sw;
244
284
  }
245
285
 
246
- /** @see lib/dotgen/position.c:make_LR_constraints (per-rank inner loop, one node pair) */
247
- export function lrRankPair(g: Graph, r: number, u: Node, v: Node, last: number): number {
286
+ /**
287
+ * Returns the next `last`, or null when the adjacent-pair aux edge is refused
288
+ * (16.1.1, upstream e62e35be5, 92ef8d508). C tests the edge before the
289
+ * `ND_rank(v)` assignment, so `v`'s rank is not written on failure.
290
+ * @see lib/dotgen/position.c:make_LR_constraints (per-rank inner loop, one node pair)
291
+ */
292
+ export function lrRankPair(g: Graph, r: number, u: Node, v: Node, last: number): number | null {
248
293
  const nodesep = lrSep(g, r);
249
294
  const width = nodeRw(u) + nodeLw(v) + nodesep;
250
- makeAuxEdge(u, v, width, 0);
295
+ if (makeAuxEdge(u, v, width, 0) === null) return null;
251
296
  /* C: `last = (ND_rank(v) = last + width)`. ND_rank is an int, so the
252
297
  * double `last + width` truncates toward zero on assignment, and the
253
298
  * truncated *integer* flows back into `last` — positions accumulate as
@@ -261,10 +306,16 @@ export function lrRankPair(g: Graph, r: number, u: Node, v: Node, last: number):
261
306
  return next;
262
307
  }
263
308
 
264
- /** @see lib/dotgen/position.c:make_LR_constraints (per-rank inner loop) */
265
- export function makeLrRankConstraints(g: Graph, r: number): void {
309
+ /**
310
+ * 16.1.1 skips a rank with no leader (`rank[i].v[0] == NULL` → `continue`).
311
+ * The per-rank body is its own function here, so C's `continue` is a `return`.
312
+ * @see lib/dotgen/position.c:make_LR_constraints (per-rank inner loop;
313
+ * 16.1.1 :240-243, d49e3a529 "anticipate empty ranks")
314
+ */
315
+ export function makeLrRankConstraints(g: Graph, r: number): number {
266
316
  const rank = g.info.rank!;
267
317
  const rk = rank[r];
318
+ if (rk.v[0] == null) return 0;
268
319
  let last = 0;
269
320
  for (let j = 0; j < rk.n; j++) {
270
321
  const u = rk.v[j];
@@ -272,26 +323,40 @@ export function makeLrRankConstraints(g: Graph, r: number): void {
272
323
  u.info.rw = nodeRw(u) + selfWidth(u);
273
324
  if (rk.v[0]) u.info.rank = last;
274
325
  const v = rk.v[j + 1];
275
- if (v) last = lrRankPair(g, r, u, v, last);
276
- makeFlatLabelConstraints(g, u);
277
- makeFlatEdgeConstraints(g, u);
326
+ if (v) {
327
+ const next = lrRankPair(g, r, u, v, last);
328
+ if (next === null) return -1;
329
+ last = next;
330
+ }
331
+ if (makeFlatLabelConstraints(g, u) !== 0) return -1;
332
+ if (makeFlatEdgeConstraints(g, u) !== 0) return -1;
278
333
  }
279
334
  if (rk.n > 0) rk.v[0].info.rank = 0;
335
+ return 0;
280
336
  }
281
337
 
282
- /** @see lib/dotgen/position.c:make_LR_constraints */
283
- export function makeLrConstraints(g: Graph): void {
338
+ /**
339
+ * Returns -1 at the first refused aux edge (C's `return -1` from inside the
340
+ * rank loops), else 0.
341
+ * @see lib/dotgen/position.c:make_LR_constraints
342
+ */
343
+ export function makeLrConstraints(g: Graph): number {
284
344
  for (let i = (g.info.minrank ?? 0); i <= (g.info.maxrank ?? 0); i++) {
285
- makeLrRankConstraints(g, i);
345
+ if (makeLrRankConstraints(g, i) !== 0) return -1;
286
346
  }
347
+ return 0;
287
348
  }
288
349
 
289
350
  // ---------------------------------------------------------------------------
290
351
  // make_edge_pairs — @see lib/dotgen/position.c:make_edge_pairs
291
352
  // ---------------------------------------------------------------------------
292
353
 
293
- /** @see lib/dotgen/position.c:make_edge_pairs (per-edge) */
294
- export function addEdgePair(g: Graph, e: Edge): void {
354
+ /**
355
+ * Returns -1 when either aux edge is refused (16.1.1, upstream 73c1fd4d1,
356
+ * f0d837f19), leaving the slack node unranked. Else 0.
357
+ * @see lib/dotgen/position.c:make_edge_pairs (per-edge)
358
+ */
359
+ export function addEdgePair(g: Graph, e: Edge): number {
295
360
  const sn = makeSlackNode(g);
296
361
  const hp = e.info.head_port.p;
297
362
  const tp = e.info.tail_port.p;
@@ -303,20 +368,25 @@ export function addEdgePair(g: Graph, e: Edge): void {
303
368
  const d = Math.trunc(hp.x - tp.x);
304
369
  const m0 = Math.max(0, d);
305
370
  const m1 = Math.max(0, -d);
306
- makeAuxEdge(sn, e.tail, m0 + 1, edgeWeight(e));
307
- makeAuxEdge(sn, e.head, m1 + 1, edgeWeight(e));
371
+ if (makeAuxEdge(sn, e.tail, m0 + 1, edgeWeight(e)) === null) return -1;
372
+ if (makeAuxEdge(sn, e.head, m1 + 1, edgeWeight(e)) === null) return -1;
308
373
  sn.info.rank = Math.min(nodeRank(e.tail) - m0 - 1, nodeRank(e.head) - m1 - 1);
374
+ return 0;
309
375
  }
310
376
 
311
- /** @see lib/dotgen/position.c:make_edge_pairs */
312
- export function makeEdgePairs(g: Graph): void {
377
+ /**
378
+ * Returns -1 at the first refused aux edge, else 0.
379
+ * @see lib/dotgen/position.c:make_edge_pairs
380
+ */
381
+ export function makeEdgePairs(g: Graph): number {
313
382
  for (let n = g.info.nlist; n !== undefined; n = n.info.next) {
314
383
  const so = n.info.save_out;
315
384
  if (!so) continue;
316
385
  for (let i = 0; i < so.size; i++) {
317
- addEdgePair(g, so.list[i]);
386
+ if (addEdgePair(g, so.list[i]) !== 0) return -1;
318
387
  }
319
388
  }
389
+ return 0;
320
390
  }
321
391
 
322
392
  /** Create a SLACKNODE virtual node and prepend to nlist. */
@@ -340,12 +410,15 @@ export function createAuxEdges(
340
410
  g: Graph,
341
411
  posClusters: (g: Graph) => void,
342
412
  compressGraph: (g: Graph) => void,
343
- ): void {
413
+ ): number {
344
414
  allocateAuxEdges(g);
345
- makeLrConstraints(g);
346
- makeEdgePairs(g);
415
+ const lrRc = makeLrConstraints(g);
416
+ if (lrRc !== 0) return lrRc;
417
+ const pairRc = makeEdgePairs(g);
418
+ if (pairRc !== 0) return pairRc;
347
419
  posClusters(g);
348
420
  compressGraph(g);
421
+ return 0;
349
422
  }
350
423
 
351
424
  /** @see lib/dotgen/position.c:remove_aux_edges (SLACKNODE removal loop) */
@@ -82,7 +82,20 @@ export function dotComputeBb(g: Graph, root: Graph): void {
82
82
  }
83
83
  const rankArr = root.info.rank!;
84
84
  const lly = rankArr[graphMaxrank(g)].v[0].info.coord.y - graphHt1(g);
85
- const ury = rankArr[graphMinrank(g)].v[0].info.coord.y + graphHt2(g);
85
+ // 16.1.1 takes UR.y from the first rank that is not empty, where 16.1.0 read
86
+ // the min rank's leader outright. C leaves UR.y uninitialised when no rank
87
+ // qualifies; that cannot be read here, because LL.y above has already
88
+ // dereferenced the max rank's leader, so at least the max rank qualifies
89
+ // whenever its `n` is nonzero. NaN marks the value as never meant to be used.
90
+ // @see lib/dotgen/position.c:dot_compute_bb (16.1.1 :885-894, 8e06a6261
91
+ // "anticipate min rank being empty")
92
+ let ury = Number.NaN;
93
+ for (let r = graphMinrank(g); r <= graphMaxrank(g); ++r) {
94
+ if (rankArr[r].n === 0) continue;
95
+ if (rankArr[r].v[0] == null) continue;
96
+ ury = rankArr[r].v[0].info.coord.y + graphHt2(g);
97
+ break;
98
+ }
86
99
  g.info.bb = { ll: { x: llx, y: lly }, ur: { x: urx, y: ury } };
87
100
  }
88
101
 
@@ -175,7 +175,13 @@ export function setYcoordsInitial(g: Graph, lbl: boolean): void {
175
175
  const d1 = rankArr[r + 1].ht2 + rankArr[r].ht1 + CL_OFFSET;
176
176
  const delta = Math.max(d0, d1);
177
177
  if (rankArr[r].n > 0) {
178
- rankArr[r].v[0].info.coord.y = rankArr[r + 1].v[0].info.coord.y + delta;
178
+ // 16.1.1: assign only when the rank below has a leader (`!= NULL`; the
179
+ // port's empty ranks hold null or nothing at v[0], hence `!= null`).
180
+ // @see lib/dotgen/position.c:set_ycoords (16.1.1 :793-797, 9fca64d0f
181
+ // "anticipate empty ranks")
182
+ if (rankArr[r + 1].v[0] != null) {
183
+ rankArr[r].v[0].info.coord.y = rankArr[r + 1].v[0].info.coord.y + delta;
184
+ }
179
185
  }
180
186
  maxht = Math.max(maxht, delta);
181
187
  }
@@ -188,7 +188,10 @@ export function dotPosition(g: Graph): number {
188
188
  }
189
189
  expandLeaves(g);
190
190
  if (flatEdges(g)) setYcoords(g);
191
- createAuxEdges(g, posClusters, compressGraph);
191
+ // 16.1.1 (upstream f444e5192): an aux edge longer than INT_MAX fails the
192
+ // layout here, ahead of the x-coordinate network simplex.
193
+ const auxRc = createAuxEdges(g, posClusters, compressGraph);
194
+ if (auxRc !== 0) return auxRc;
192
195
  g.info.rankIsXCoord = true;
193
196
  if (rank(g, 2, nsiter2(g))) {
194
197
  connectGraph(g);
@@ -17,15 +17,11 @@ import { nodesInSeq } from './decomp.js';
17
17
  // Constants @see lib/dotgen/rank.c
18
18
  // ---------------------------------------------------------------------------
19
19
 
20
- const BACKWARD_PENALTY = 1000;
21
- const STRONG_CLUSTER_WEIGHT = 1000;
22
20
  const NORANK = 6;
23
21
  const ROOT_NAME = '\x7froot';
24
- const TOPNODE_NAME = '\x7ftop';
25
- const BOTNODE_NAME = '\x7fbot';
26
22
 
27
- /** Mutable state replacing C statics Last_node and id in weak(). */
28
- export interface XgState { lastNode: Node | undefined; weakId: number; }
23
+ /** Mutable state replacing the C static Last_node. */
24
+ export interface XgState { lastNode: Node | undefined; }
29
25
 
30
26
  // ---------------------------------------------------------------------------
31
27
  // Accessor helpers — extract ?? and ?. so callers stay within CCN ≤ 10
@@ -79,17 +75,15 @@ export function d2unionAll(g: Graph): Node | undefined {
79
75
  // could later widen a node's membership out of creation order (the exact
80
76
  // precondition that makes `g.nodes.values()` diverge from AGSEQ elsewhere).
81
77
  // Xg's node-creation order is itself already driven by `nodesInSeq(g)`
82
- // (compileNodes/compileEdges/compileClusters, converted above), so
78
+ // (compileNodes, converted above; connectComponents adds ROOT last), so
83
79
  // `Xg.nodes.values()` is provably already AGSEQ-equivalent order.
84
80
  // ---------------------------------------------------------------------------
85
81
 
86
82
  /** @see lib/dotgen/rank.c:makeXnode — C's `agnode(G, name, 1)` returns the
87
- * existing node when one with `name` is present, so the fixed-name helper nodes
88
- * TOP/BOT are a single shared node across all strong clusters (compile_clusters
89
- * calls makeXnode(Xg, TOPNODE) once per cluster). Creating a fresh node per call
90
- * gives each cluster its own TOP/BOT, splitting the STRONG_CLUSTER_WEIGHT
91
- * constraint and derailing the rank solve. Real/weak node names are unique, so
92
- * this dedup only affects TOP/BOT/ROOT — exactly as in C. */
83
+ * existing node when one with `name` is present. Since graphviz 16.1.1 removed
84
+ * compile_clusters and its shared TOP/BOT nodes, the callers are compile_nodes
85
+ * (one call per set leader, each name unique) and connect_components (ROOT, at
86
+ * most once per Xg), so the dedup matters only for ROOT. */
93
87
  export function makeXnode(Xg: Graph, name: string, st: XgState): Node {
94
88
  const existing = Xg.nodes.get(name);
95
89
  if (existing) return existing;
@@ -212,30 +206,6 @@ export function xgStrong(Xg: Graph, t: Node, h: Node, orig: Edge): void {
212
206
  xgMerge(e, eMinlen(orig), eWeight(orig));
213
207
  }
214
208
 
215
- /** @see lib/dotgen/rank.c:weak — find existing weak diamond v→t, v→h */
216
- export function xgWeakExists(Xg: Graph, t: Node, h: Node): boolean {
217
- for (const e of Xg.edges) {
218
- if (e.head !== t) continue;
219
- const v = e.tail;
220
- for (const f of Xg.edges) { if (f.tail === v && f.head === h) return true; }
221
- }
222
- return false;
223
- }
224
-
225
- export function xgWeakSetWeights(e: Edge, f: Edge, origW: number, origML: number): void {
226
- e.info.minlen = Math.max(eMinlen(e), 0);
227
- e.info.weight = eWeight(e) + origW * BACKWARD_PENALTY;
228
- f.info.minlen = Math.max(eMinlen(f), origML);
229
- f.info.weight = eWeight(f) + origW;
230
- }
231
-
232
- /** @see lib/dotgen/rank.c:weak */
233
- export function xgWeak(Xg: Graph, t: Node, h: Node, orig: Edge, st: XgState): void {
234
- if (xgWeakExists(Xg, t, h)) return;
235
- const v = makeXnode(Xg, `_weak_${st.weakId++}`, st);
236
- xgWeakSetWeights(xgAddEdge(Xg, v, t), xgAddEdge(Xg, v, h), eWeight(orig), eMinlen(orig));
237
- }
238
-
239
209
  /** @see lib/dotgen/rank.c:is_nonconstraint */
240
210
  export function isNonConstraint(e: Edge): boolean {
241
211
  const v = e.attrs.get('constraint');
@@ -243,11 +213,6 @@ export function isNonConstraint(e: Edge): boolean {
243
213
  return !mapbool(v);
244
214
  }
245
215
 
246
- /** @see lib/dotgen/rank.c:is_a_strong_cluster */
247
- export function isAStrongCluster(g: Graph): boolean {
248
- return mapbool(g.attrs.get('compact') ?? '');
249
- }
250
-
251
216
  /** @see lib/dotgen/rank.c:dot_lca */
252
217
  export function dotLca(c0: Graph, c1: Graph): Graph {
253
218
  let a = c0; let b = c1;
@@ -283,66 +248,21 @@ export function ceInternal(Xg: Graph, e: Edge, Xt: Node, Xh: Node): void {
283
248
  }
284
249
 
285
250
  /** @see lib/dotgen/rank.c:compile_edges inner body */
286
- export function compileEdgeItem(Xg: Graph, e: Edge, Xt: Node, st: XgState): void {
251
+ export function compileEdgeItem(Xg: Graph, e: Edge, Xt: Node): void {
287
252
  if (isNonConstraint(e)) return;
288
253
  const Xh = d2find(e.head).info.rep;
289
254
  if (!Xh || Xt === Xh) return;
290
255
  if (isInternalToCluster(e)) { ceInternal(Xg, e, Xt, Xh); return; }
291
- const tc = e.tail.info.clust; const hc = e.head.info.clust;
292
- if ((tc && isAStrongCluster(tc)) || (hc && isAStrongCluster(hc))) xgWeak(Xg, Xt, Xh, e, st);
293
- else xgStrong(Xg, Xt, Xh, e);
256
+ xgStrong(Xg, Xt, Xh, e);
294
257
  }
295
258
 
296
259
  /** @see lib/dotgen/rank.c:compile_edges */
297
- export function compileEdges(g: Graph, Xg: Graph, st: XgState): void {
260
+ export function compileEdges(g: Graph, Xg: Graph): void {
298
261
  for (const n of nodesInSeq(g)) {
299
262
  const Xt = n.info.rep;
300
263
  if (!Xt) continue;
301
- for (const e of n.outEdges(g)) compileEdgeItem(Xg, e, Xt, st);
302
- }
303
- }
304
-
305
- // ---------------------------------------------------------------------------
306
- // compile_clusters helpers
307
- // ---------------------------------------------------------------------------
308
-
309
- interface ClusterBounds { top: Node | undefined; bot: Node | undefined; }
310
-
311
- export function ccProcessNode(g: Graph, Xg: Graph, st: XgState, n: Node, b: ClusterBounds): void {
312
- const rep = d2find(n).info.rep;
313
- if (!rep) return;
314
- if (n.inEdges(g).length === 0) {
315
- if (!b.top) b.top = makeXnode(Xg, TOPNODE_NAME, st);
316
- xgAddEdge(Xg, b.top, rep);
317
- }
318
- if (n.outEdges(g).length === 0) {
319
- if (!b.bot) b.bot = makeXnode(Xg, BOTNODE_NAME, st);
320
- xgAddEdge(Xg, rep, b.bot);
321
- }
322
- }
323
-
324
- export function ccStrongCluster(
325
- g: Graph, Xg: Graph, st: XgState, top: Node | undefined, bot: Node | undefined
326
- ): ClusterBounds {
327
- const b: ClusterBounds = { top, bot };
328
- for (const n of nodesInSeq(g)) ccProcessNode(g, Xg, st, n, b);
329
- // Xg is Agstrictdirected, so C's `agedge(Xg, top, bot)` returns the one shared
330
- // TOP->BOT edge (TOP/BOT dedup in makeXnode) and merge() accumulates onto it.
331
- // Mirror strict agedge with find-or-add so the weight sums instead of forking.
332
- if (b.top && b.bot) xgMerge(xgFindEdge(Xg, b.top, b.bot) ?? xgAddEdge(Xg, b.top, b.bot), 0, STRONG_CLUSTER_WEIGHT);
333
- return b;
334
- }
335
-
336
- /** @see lib/dotgen/rank.c:compile_clusters */
337
- export function compileClusters(
338
- g: Graph, Xg: Graph, st: XgState, top: Node | undefined, bot: Node | undefined
339
- ): void {
340
- let t = top; let b = bot;
341
- if (isACluster(g) && isAStrongCluster(g)) {
342
- const bounds = ccStrongCluster(g, Xg, st, t, b);
343
- t = bounds.top; b = bounds.bot;
264
+ for (const e of n.outEdges(g)) compileEdgeItem(Xg, e, Xt);
344
265
  }
345
- for (const sub of g.subgraphs.values()) compileClusters(sub, Xg, st, t, b);
346
266
  }
347
267
 
348
268
  // ---------------------------------------------------------------------------
@@ -507,7 +427,7 @@ export function readoutLevels(g: Graph, Xg: Graph, ncc: number): void {
507
427
 
508
428
  /** @see lib/dotgen/rank.c:dot2_rank */
509
429
  export function dot2Rank(g: Graph): void {
510
- const st: XgState = { lastNode: undefined, weakId: 0 };
430
+ const st: XgState = { lastNode: undefined };
511
431
  const Xg = new Graph('level assignment constraints', 'strict-directed');
512
432
  edgelabelRanks(g);
513
433
  const nslimit1 = g.attrs.get('nslimit1');
@@ -518,8 +438,7 @@ export function dot2Rank(g: Graph): void {
518
438
  const ssize = ssStr ? parseInt(ssStr, 10) : -1;
519
439
  compileSamerank(g, undefined);
520
440
  compileNodes(g, Xg, st);
521
- compileEdges(g, Xg, st);
522
- compileClusters(g, Xg, st, undefined, undefined);
441
+ compileEdges(g, Xg);
523
442
  breakCycles(Xg);
524
443
  const ncc = connectComponents(Xg, st);
525
444
  addFastEdges(Xg);
@@ -71,19 +71,30 @@ export function collectOtherEdges(n: Node, edges: Edge[]): void {
71
71
 
72
72
  // ---------------------------------------------------------------------------
73
73
  // computeSizey — rank-gap geometry for self-loop sizing
74
- // @see lib/dotgen/dotsplines.c:389-403
74
+ // @see lib/dotgen/dotsplines.c:dot_splines_ (16.1.1 :396-411)
75
75
  // ---------------------------------------------------------------------------
76
76
 
77
+ /** C's `GD_rank(g)[r].v[0] != NULL`: rank r is not empty. The port's empty
78
+ * ranks carry `v[0]` as null or as absent (`v` is `[]`), so the test is loose.
79
+ * @see lib/dotgen/dotsplines.c:dot_splines_ (16.1.1, ca3353d80, 1a77ac9df) */
80
+ function rankHasLeader(g: Graph, r: number): boolean {
81
+ return g.info.rank![r].v[0] != null;
82
+ }
83
+
77
84
  /** y-coord of first node in rank r. @see lib/dotgen/dotsplines.c:394 */
78
85
  function rankY(g: Graph, r: number): number {
79
86
  return g.info.rank![r].v[0].info.coord?.y ?? 0;
80
87
  }
81
88
 
82
- /** sizey when n is at the maximum rank. C's `r > 0` is a literal index test;
83
- * after abomination C's rank is r - abomShift (AD-2 renumbers +1 where C uses
84
- * index -1). @see lib/dotgen/dotsplines.c:392-396 */
89
+ /** sizey when n is at the maximum rank, or the rank below it is empty. C's
90
+ * `r > 0` is a literal index test; after abomination C's rank is
91
+ * r - abomShift (AD-2 renumbers +1 where C uses index -1). 16.1.1 also wants
92
+ * a leader on the rank above: `r > 0 && GD_rank(g)[r - 1].v[0] != NULL`.
93
+ * @see lib/dotgen/dotsplines.c:dot_splines_ (16.1.1 :400-404, 1a77ac9df) */
85
94
  function sizeyAtMaxrank(g: Graph, n: Node, r: number): number {
86
- if (r - (g.info.abomShift ?? 0) > 0) return rankY(g, r - 1) - (n.info.coord?.y ?? 0);
95
+ if (r - (g.info.abomShift ?? 0) > 0 && rankHasLeader(g, r - 1)) {
96
+ return rankY(g, r - 1) - (n.info.coord?.y ?? 0);
97
+ }
87
98
  return n.info.ht ?? 0;
88
99
  }
89
100
 
@@ -103,19 +114,26 @@ function sizeyAtMidrank(g: Graph, n: Node, r: number): number {
103
114
  /**
104
115
  * Compute the vertical sizing parameter for self-loops on node n.
105
116
  *
106
- * Mirrors the C branch exactly:
107
- * if r == maxrank: sizey = rank[r-1].v[0].y - n.y (or ht if r==0)
108
- * elif r == minrank: sizey = n.y - rank[r+1].v[0].y
117
+ * Mirrors the C branch exactly (16.1.1 guards an empty neighbouring rank):
118
+ * if r == maxrank || rank[r+1].v[0] == NULL:
119
+ * sizey = rank[r-1].v[0].y - n.y (or ht if r==0 or rank[r-1] is empty)
120
+ * elif r == minrank || rank[r-1].v[0] == NULL:
121
+ * sizey = n.y - rank[r+1].v[0].y
109
122
  * else: sizey = min(rank[r-1].v[0].y - n.y, n.y - rank[r+1].v[0].y)
110
123
  *
111
- * @see lib/dotgen/dotsplines.c:389-403
124
+ * The `||` order is C's: the rank table has no entry past either end, so the
125
+ * neighbour is read only once `r` is known not to be that end.
126
+ *
127
+ * @see lib/dotgen/dotsplines.c:dot_splines_ (16.1.1 :396-411; ca3353d80
128
+ * "anticipate an empty rank above", 1a77ac9df "anticipate an empty rank
129
+ * below")
112
130
  */
113
131
  export function computeSizey(g: Graph, n: Node): number {
114
132
  const r = n.info.rank ?? 0;
115
133
  const minrank = g.info.minrank ?? 0;
116
134
  const maxrank = g.info.maxrank ?? 0;
117
- if (r === maxrank) return sizeyAtMaxrank(g, n, r);
118
- if (r === minrank) return sizeyAtMinrank(g, n, r);
135
+ if (r === maxrank || !rankHasLeader(g, r + 1)) return sizeyAtMaxrank(g, n, r);
136
+ if (r === minrank || !rankHasLeader(g, r - 1)) return sizeyAtMinrank(g, n, r);
119
137
  return sizeyAtMidrank(g, n, r);
120
138
  }
121
139
 
@@ -89,9 +89,17 @@ function samePortsAs(x: Edge, lo: Node, key: { loP: Port; hiP: Port }): boolean
89
89
 
90
90
  /**
91
91
  * Collect all unrouted non-adjacent same-rank side-port flat edges between e's
92
- * endpoints that share e's tail/head ports (C portcmp), ordered so group[0].tail
93
- * is the lower-order (left) node, ties by seq (AD-3). Mirrors edge-route.ts:
94
- * collectAdjacentFlatGroup with the non-adjacent + identical-port group key.
92
+ * endpoints that share e's tail/head ports (C portcmp), in C's edgecmp order.
93
+ * Inside one make_flat_edge run every edgecmp key before the last is equal
94
+ * (same main edge, so the same rank and x difference; the same ports through
95
+ * makefwdedge; the same graph type and label), so the order is AGSEQ's,
96
+ * whichever node is on the left. make_flat_edge gives edge i the (i+1)-th
97
+ * route out. This follows C where "left tail first" differed from it
98
+ * (nonadjacent-flat-multi AD-3: "if C's order differs, follow C"; issue #158,
99
+ * 700813). `lo` keys the port match only, not the order. Mirrors
100
+ * edge-route.ts: collectAdjacentFlatGroup with the non-adjacent +
101
+ * identical-port group key.
102
+ * @see lib/dotgen/dotsplines.c:edgecmp
95
103
  * @see lib/dotgen/dotsplines.c:dot_splines_ (portcmp 370-373), make_flat_edge
96
104
  */
97
105
  export function collectNonAdjacentFlatGroup(e: Edge, g: Graph): Edge[] {
@@ -106,7 +114,7 @@ export function collectNonAdjacentFlatGroup(e: Edge, g: Graph): Edge[] {
106
114
  const group = g.edges.filter(
107
115
  x => sharesPair(x) && isNonAdjGroupable(x, g) && samePortsAs(x, lo, key)
108
116
  && x.info.label === e.info.label);
109
- group.sort((a, b) => Number(b.tail === lo) - Number(a.tail === lo) || a.seq - b.seq);
117
+ group.sort((a, b) => a.seq - b.seq);
110
118
  return group;
111
119
  }
112
120
 
@@ -89,19 +89,18 @@ export function isStraightContinuation(v: Node, n: Node): boolean {
89
89
  return nodeCoordX(v) === nodeCoordX(n);
90
90
  }
91
91
 
92
- /** Advance one step along the straight chain; return undefined if done. */
93
- export function straightNext(v: Node): Node | undefined {
94
- const e = v.info.out?.list[0];
95
- return e ? e.head : undefined;
96
- }
97
-
98
- /** @see lib/dotgen/dotsplines.c:straight_len */
92
+ /**
93
+ * Count of vertically aligned edges starting at n. The walk stops at a node
94
+ * with no out edge: 16.1.1 loops on `ND_out(v).size > 0`, where 16.1.0 read
95
+ * `ND_out(v).list[0]` of a virtual node that has none (upstream issue 2765).
96
+ * @see lib/dotgen/dotsplines.c:straight_len (16.1.1, 5649d079c)
97
+ */
99
98
  export function straightLen(n: Node): number {
100
99
  let cnt = 0;
101
- let v: Node | undefined = n;
102
- for (;;) {
103
- v = straightNext(v);
104
- if (!v || !isStraightContinuation(v, n)) break;
100
+ let v: Node = n;
101
+ while (nodeOutSize(v) > 0) {
102
+ v = v.info.out!.list[0].head;
103
+ if (!isStraightContinuation(v, n)) break;
105
104
  cnt++;
106
105
  }
107
106
  return cnt;
@@ -30,6 +30,7 @@ import { midPointf } from './compound-geom.js';
30
30
  import { placePortlabel, updateBB } from './splines-label.js';
31
31
  import { dotRoot } from './mincross-utils.js';
32
32
  import { nodesInSeq } from './decomp.js';
33
+ import { fdivOverflow } from '../../util/overflow.js';
33
34
 
34
35
  // Edge-type values, mirroring the constants in splines.ts. Defined locally to
35
36
  // avoid a straight-edges ↔ splines import cycle (splines.ts imports
@@ -174,10 +175,10 @@ function getCycleCentroid(g: Graph, edge: Edge): Point {
174
175
 
175
176
  // ---------------------------------------------------------------------------
176
177
  // bend — pull the two interior control points toward the centroid
177
- // @see lib/common/routespl.c:933-952
178
+ // @see lib/common/routespl.c:937-951
178
179
  // ---------------------------------------------------------------------------
179
180
 
180
- /** @see lib/common/routespl.c:933-952 (bend) */
181
+ /** @see lib/common/routespl.c:937-951 (bend) */
181
182
  function bend(spl: Point[], centroid: Point): void {
182
183
  const midpt = midPointf(spl[0], spl[3]);
183
184
  const distVal = dist(spl[3], spl[0]); // DIST(spl[3], spl[0])
@@ -185,7 +186,8 @@ function bend(spl: Point[], centroid: Point): void {
185
186
  const vX = centroid.x - midpt.x;
186
187
  const vY = centroid.y - midpt.y;
187
188
  const magV = Math.hypot(vX, vY);
188
- if (magV === 0) return; // midpoint == centroid: don't divide by zero
189
+ // avoid out of range division results
190
+ if (fdivOverflow(vX, magV) === null || fdivOverflow(vY, magV) === null) return;
189
191
  const ax = midpt.x - (vX / magV) * r; // + would be closest point
190
192
  const ay = midpt.y - (vY / magV) * r;
191
193
  // this can be improved