@knowvah/dot-engine 2.11.0 → 2.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/common/agerr.d.ts +12 -0
  2. package/dist/common/agerr.d.ts.map +1 -1
  3. package/dist/common/edge-in-box.d.ts +6 -20
  4. package/dist/common/edge-in-box.d.ts.map +1 -1
  5. package/dist/common/emit-bb.d.ts +36 -0
  6. package/dist/common/emit-bb.d.ts.map +1 -0
  7. package/dist/gvc/__fixtures__/subg-renders/subg-renders.d.ts +10 -0
  8. package/dist/gvc/__fixtures__/subg-renders/subg-renders.d.ts.map +1 -0
  9. package/dist/gvc/device.d.ts.map +1 -1
  10. package/dist/gvc/emit-xdot.d.ts +9 -0
  11. package/dist/gvc/emit-xdot.d.ts.map +1 -1
  12. package/dist/index.d.ts +2 -0
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +7357 -7187
  15. package/dist/index.js.map +4 -4
  16. package/dist/layout/dot/decomp.d.ts.map +1 -1
  17. package/dist/layout/dot/edge-route-chain.d.ts.map +1 -1
  18. package/dist/layout/dot/edge-route.d.ts +3 -8
  19. package/dist/layout/dot/edge-route.d.ts.map +1 -1
  20. package/dist/layout/dot/init.d.ts +25 -4
  21. package/dist/layout/dot/init.d.ts.map +1 -1
  22. package/dist/layout/dot/splines.d.ts +5 -3
  23. package/dist/layout/dot/splines.d.ts.map +1 -1
  24. package/dist/layout/neato/fdp-adjust.d.ts.map +1 -1
  25. package/dist/layout/neato/sgd.d.ts.map +1 -1
  26. package/dist/layout/sfdp/spring-electrical.d.ts.map +1 -1
  27. package/dist/render/dot/agwrite-linelength.d.ts +25 -0
  28. package/dist/render/dot/agwrite-linelength.d.ts.map +1 -0
  29. package/dist/render/dot/agwrite.d.ts +4 -7
  30. package/dist/render/dot/agwrite.d.ts.map +1 -1
  31. package/dist/render/dot/attrs.d.ts.map +1 -1
  32. package/dist/render/dot/write-back.d.ts +10 -0
  33. package/dist/render/dot/write-back.d.ts.map +1 -1
  34. package/dist/render/dot.d.ts +1 -2
  35. package/dist/render/dot.d.ts.map +1 -1
  36. package/dist/render/json-cluster-edge.d.ts +22 -0
  37. package/dist/render/json-cluster-edge.d.ts.map +1 -0
  38. package/dist/render/json.d.ts.map +1 -1
  39. package/dist/render/map.d.ts.map +1 -1
  40. package/dist/render/svg-graph.d.ts.map +1 -1
  41. package/dist/render.js +7360 -7196
  42. package/dist/render.js.map +4 -4
  43. package/dist/util/printf-round.d.ts.map +1 -1
  44. package/package.json +1 -1
  45. package/src/common/agerr.ts +49 -1
  46. package/src/common/edge-in-box.ts +9 -24
  47. package/src/common/emit-bb.ts +128 -0
  48. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.1 +45 -0
  49. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.2 +45 -0
  50. package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.3 +45 -0
  51. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.1 +74 -0
  52. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.2 +74 -0
  53. package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.3 +76 -0
  54. package/src/gvc/__fixtures__/rerender/nestedclust.gv +4 -0
  55. package/src/gvc/__fixtures__/rerender/regen.sh +5 -0
  56. package/src/gvc/__fixtures__/subg-renders/subg-renders.ts +48 -0
  57. package/src/gvc/device.ts +26 -27
  58. package/src/gvc/emit-gates.ts +1 -1
  59. package/src/gvc/emit-xdot.ts +5 -6
  60. package/src/index.ts +4 -0
  61. package/src/layout/dot/decomp.ts +23 -2
  62. package/src/layout/dot/edge-route-chain.ts +79 -7
  63. package/src/layout/dot/edge-route.ts +8 -37
  64. package/src/layout/dot/init.ts +36 -11
  65. package/src/layout/dot/splines-flat.ts +2 -2
  66. package/src/layout/dot/splines-groups.ts +2 -2
  67. package/src/layout/dot/splines-route.ts +2 -2
  68. package/src/layout/dot/splines.ts +10 -10
  69. package/src/layout/fdp/index.ts +2 -2
  70. package/src/layout/neato/conjgrad.ts +2 -2
  71. package/src/layout/neato/fdp-adjust.ts +1 -16
  72. package/src/layout/neato/sgd.ts +3 -9
  73. package/src/layout/sfdp/spring-electrical.ts +5 -3
  74. package/src/render/dot/agwrite-linelength.ts +58 -0
  75. package/src/render/dot/agwrite.ts +13 -13
  76. package/src/render/dot/attrs.ts +27 -1
  77. package/src/render/dot/write-back.ts +21 -0
  78. package/src/render/dot/xdot-ops.ts +1 -1
  79. package/src/render/dot.ts +11 -17
  80. package/src/render/json-cluster-edge.ts +50 -0
  81. package/src/render/json.ts +16 -19
  82. package/src/render/map.ts +3 -7
  83. package/src/render/svg-graph.ts +22 -9
  84. package/src/render/svg.ts +3 -3
  85. package/src/util/printf-round.ts +16 -10
  86. package/dist/layout/dot/edge-order.d.ts +0 -27
  87. package/dist/layout/dot/edge-order.d.ts.map +0 -1
  88. package/src/layout/dot/edge-order.ts +0 -62
@@ -0,0 +1,48 @@
1
+ // SPDX-License-Identifier: EPL-2.0
2
+ // The renders part-11 T7's harness compares with native (harness/T7/port-vs-native.ts):
3
+ // subg-after-free.c's modes on harness/T0/subg.gv, fdp and osage, xdot and svg.
4
+ // root: layout root, render, free; layout subgraph, render, free; layout root, render, free
5
+ // nofree: layout root, render; layout subgraph (no free), render, free
6
+ // Native refs: harness/T7b/noaddr (fdp's settled, ND_id-ordered class; regen.sh).
7
+ import { readFileSync } from 'node:fs';
8
+ import { parse } from '../../../parser/index.js';
9
+ import type { EngineName } from '../../context.js';
10
+ import { createDefaultContext } from '../../default-context.js';
11
+ import { render as deviceRender } from '../../device.js';
12
+
13
+ const HARNESS = new URL('../../../../plans/post-16-1-followups-11/harness/', import.meta.url);
14
+ const read = (name: string): string => readFileSync(new URL(name, HARNESS), 'latin1');
15
+
16
+ export type SubgMode = 'root' | 'nofree';
17
+ export type SubgFmt = 'xdot' | 'svg';
18
+
19
+ /** Native's renders of one case, in order (harness/T7b/noaddr). */
20
+ export function nativeSubgRenders(engine: EngineName, sub: string, mode: SubgMode, fmt: SubgFmt): string[] {
21
+ return read(`T7b/noaddr/subg-${engine}.${sub}.${mode}.${fmt}`).split(/^== render \d+\n/m).slice(1);
22
+ }
23
+
24
+ /** The port's renders of one case, in the harness's order. */
25
+ export function portSubgRenders(engine: EngineName, sub: string, mode: SubgMode, fmt: SubgFmt): string[] {
26
+ const g = parse(read('T0/subg.gv'));
27
+ const s = g.subgraphs.get(sub);
28
+ if (s === undefined) throw new Error(`no subgraph ${sub}`);
29
+ const ctx = createDefaultContext();
30
+ const out: string[] = [];
31
+ ctx.layout(g, engine);
32
+ out.push(deviceRender(ctx, g, fmt));
33
+ if (mode !== 'nofree') ctx.freeLayout(g, engine);
34
+ ctx.layout(s, engine);
35
+ out.push(deviceRender(ctx, s, fmt));
36
+ ctx.freeLayout(s, engine);
37
+ if (mode === 'root') {
38
+ ctx.layout(g, engine);
39
+ out.push(deviceRender(ctx, g, fmt));
40
+ ctx.freeLayout(g, engine);
41
+ }
42
+ return out;
43
+ }
44
+
45
+ /** svg's generator banner and `<svg>` tag layout differ from native's on every render; drop them. */
46
+ export function svgBody(svg: string): string {
47
+ return svg.slice(svg.indexOf('<g id="graph'));
48
+ }
package/src/gvc/device.ts CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  import { POINTS_PER_INCH } from '../model/geom.js';
16
16
  import { lateDouble } from '../common/nodeinit.js';
17
- import { parseDrawingSize, initJobViewportZoom, parseLandscape, parseGraphPad, parseGraphMargin, setupPageClip } from './viewport.js';
17
+ import { parseDrawingSize, initJobViewportZoom, parseLandscape, parseGraphPad, parseGraphMargin } from './viewport.js';
18
18
  import type { Graph } from '../model/graph.js';
19
19
  import type { Node } from '../model/node.js';
20
20
  import type { Edge } from '../model/edge.js';
@@ -26,6 +26,7 @@ import type { ShapeDesc, TextlabelT } from '../common/types.js';
26
26
  import { type LayerInfo, parseLayers } from '../common/layers.js';
27
27
  import { RenderJob, EMIT_CLUSTERS_LAST, createObjState, ObjType, EmitState } from './job.js';
28
28
  import { walkNodesAndEdges } from './emit-walk.js';
29
+ import { initBb } from '../common/emit-bb.js';
29
30
  import { firstLayer, nextLayer, numPhysicalLayers, validLayer } from './first-layer.js';
30
31
  import { polyInit } from '../common/poly-init.js';
31
32
  import {
@@ -50,7 +51,7 @@ import {
50
51
  import { emitRoundedBezier } from '../common/poly-shapes.js';
51
52
  import { applyClusterObjState, clusterAttr, clusterStyle, clusterPeripheries, isStripedCluster, renderStripedClusterBox } from './device-cluster.js';
52
53
  import { transformPoint } from './device-transform.js';
53
- import { emitXdot } from './emit-xdot.js';
54
+ import { deviceClip, emitXdot } from './emit-xdot.js';
54
55
  import { gvrenderSetStyle, edgeStyleResets } from './gvrender-set-style.js';
55
56
  import { renderOneLabel, renderNodeXLabel, renderClusterLabel, reportEdgeLabelColors } from './device-labels.js';
56
57
  export { transformPoint, applyRotation, applyScale, buildPoint } from './device-transform.js';
@@ -413,6 +414,15 @@ function renderPage(g: Graph, renderer: RendererPlugin, job: RenderJob, info: La
413
414
  // render — top-level convenience
414
415
  // ---------------------------------------------------------------------------
415
416
 
417
+ /** A job for `format` carrying the context's image hooks. */
418
+ function newRenderJob(ctx: GvcContext, format: string, inlineImages: boolean): RenderJob {
419
+ const job = new RenderJob(format, ctx.textMeasurer);
420
+ job.inlineImages = inlineImages;
421
+ if (ctx.imageResolver !== undefined) job.imageResolver = ctx.imageResolver;
422
+ if (ctx.imageSizer !== undefined) job.imageSizer = ctx.imageSizer;
423
+ return job;
424
+ }
425
+
416
426
  /**
417
427
  * Select renderer, create job, initialise bb from graph, then render.
418
428
  *
@@ -430,38 +440,27 @@ export function render(ctx: GvcContext, g: Graph, format: string, inlineImages =
430
440
  if (!(ctx instanceof GvcContext)) throw invalidArgType('ctx', 'GvcContext', ctx);
431
441
  if (typeof g !== 'object' || g === null) throw invalidArgType('g', 'object', g);
432
442
  const renderer = ctx.bestRenderer(format);
433
- const job = new RenderJob(format, ctx.textMeasurer);
434
- job.inlineImages = inlineImages;
435
- if (ctx.imageResolver !== undefined) job.imageResolver = ctx.imageResolver;
436
- if (ctx.imageSizer !== undefined) job.imageSizer = ctx.imageSizer;
443
+ const job = newRenderJob(ctx, format, inlineImages);
444
+ initBb(g); // spl->bb for edge_in_box, before init_gvc. @see lib/common/emit.c:4216
437
445
  // gvc->bb = GD_bb(g) verbatim -- no recompute fallback. Every layout engine
438
- // sets g.info.bb itself before render() runs (set_aspect for dot,
439
- // compute_bb-equivalent computeSubgraphBB calls in neato/circo/sfdp/fdp/
440
- // twopi/osage); when a layout phase aborts before that point (2825: dot_
441
- // position fails before set_aspect), C leaves GD_bb at its zero default and
442
- // the degenerate box propagates into job.bb/job.pad-derived clip below,
443
- // excluding every node via node_in_box -- do not paper over that with a
444
- // recompute, it is the mechanism that produces C's near-empty output.
446
+ // sets g.info.bb itself before render() runs; when a layout phase aborts
447
+ // before that point (2825: dot_position fails before set_aspect), C leaves
448
+ // GD_bb at its zero default and the degenerate box propagates into the clip
449
+ // below, excluding every node via node_in_box -- do not paper over that with
450
+ // a recompute, it is the mechanism that produces C's near-empty output.
445
451
  // @see lib/common/emit.c:3272 init_gvc (gvc->bb = GD_bb(g))
446
452
  job.bb = g.info.bb;
447
- // init_job_pad: resolve job.pad from the `pad=` graph attribute before the
448
- // size= fit is computed — C calls init_job_pad before init_job_viewport
449
- // (emit.c:4289-4291) because the fit uses the padded bb.
453
+ // init_job_pad: job.pad from `pad=`, before the size= fit, which uses the
454
+ // padded bb (C calls init_job_pad before init_job_viewport, emit.c:4289-4291).
450
455
  // @see lib/common/emit.c:3241-3251 (attr read); :3290-3304 (fallback)
451
456
  job.pad = parseGraphPad(g.attrs.get('pad'));
452
- // init_job_margin: resolve job.margin from the `margin=` graph attribute,
453
- // immediately after init_job_pad (C's call order, emit.c:4290-4291).
454
- // margin does NOT feed the size= zoom fit below (only pad does) -- it is
455
- // read here and consumed later, after Z is known, by emitSvgTag/svgBeginPage
456
- // (job.width/height and the page group translate), matching C's
457
- // init_job_margin -> init_job_dpi -> init_job_viewport -> init_job_pagination
458
- // order where margin is resolved before Z but only used after it.
457
+ // init_job_margin, right after init_job_pad (emit.c:4290-4291). margin does
458
+ // not feed the size= fit (only pad does); emitSvgTag/svgBeginPage use it after
459
+ // Z is known, as C resolves it before init_job_viewport and uses it after.
459
460
  // @see lib/common/emit.c:3229-3239 (attr read); :3309-3331 (fallback)
460
461
  job.margin = parseGraphMargin(g.attrs.get('margin'));
461
462
  // init_job_viewport: fit the drawing into size= via a zoom Z carried by the
462
- // SVG group transform (D1: parse here, not graph_init).
463
- //
464
- // Correction to D3: only the trailing `!` on size= sets the *filled* flag
463
+ // SVG group transform (D1: parse here, not graph_init). Correction to D3: only the trailing `!` on size= sets the *filled* flag
465
464
  // that init_job_viewport reads (input.c:694 GD_drawing->filled =
466
465
  // getdoubles2ptf). `ratio=fill` instead sets ratio_kind=R_FILL (setRatio),
467
466
  // which reshapes the *layout* (out of scope, D3) and does NOT feed
@@ -479,7 +478,7 @@ export function render(ctx: GvcContext, g: Graph, format: string, inlineImages =
479
478
  // not job->zoom. job.scale is read only by the SVG group/dims.
480
479
  const drawingSize = parseDrawingSize(g.attrs.get('size'));
481
480
  const z = initJobViewportZoom(job.bb, drawingSize, job.pad);
482
- job.clip = setupPageClip(job.bb, drawingSize, job.pad, z); // @see emit.c:setup_page
481
+ job.clip = deviceClip(g, renderer, job); // @see emit.c:setup_page (xdot's default pad is 0)
483
482
  // init_job_dpi (emit.c:3333): GD_drawing->dpi = late_double(dpi, resolution, 0)
484
483
  // (input.c:713). When >0 it becomes job->dpi; otherwise the SVG device's
485
484
  // default_dpi = 72 (gvrender_core_svg.c:814) applies. init_job_viewport then
@@ -81,7 +81,7 @@ export function emitNodeComments(n: Node, renderer: RendererPlugin, job: RenderJ
81
81
  * @see lib/common/emit.c:emit_edge (gvrender_comment x2)
82
82
  */
83
83
  export function emitEdgeComments(e: Edge, renderer: RendererPlugin, job: RenderJob): void {
84
- if (!edgeHasDrawableContent(e, job.bb)) return;
84
+ if (!edgeHasDrawableContent(e, job.clip)) return;
85
85
  const kind = e.head.root.kind; // agisdirected(agraphof(aghead(e)))
86
86
  const op = kind === 'directed' || kind === 'strict-directed' ? '->' : '--';
87
87
  gvrenderComment(renderer, job, e.tail.name + op + e.head.name);
@@ -30,16 +30,15 @@ const DEFAULT_FILL = 'lightgrey';
30
30
  const DEFAULT_LINE_STYLE: readonly string[] = ['solid', 'setlinewidth(1)'];
31
31
 
32
32
  /**
33
- * job->clip for this device. `job.clip` carries the SVG default pad of 4; the
34
- * xdot device's default is 0, so with no `pad` its clip is the bb, computed in
35
- * C's order, rounding included.
33
+ * job->clip for this device, computed in C's order, rounding included. svg's
34
+ * pad is `job.pad` (default 4); the xdot device's default is 0, so with no
35
+ * `pad` its clip is the bb.
36
36
  * @see plugin/core/gvrender_core_dot.c:739 render_features_dot
37
37
  * @see lib/common/emit.c:1558
38
38
  */
39
- function deviceClip(g: Graph, renderer: RendererPlugin, job: RenderJob): Box {
40
- if (renderer.type === 'svg') return job.clip;
39
+ export function deviceClip(g: Graph, renderer: RendererPlugin, job: RenderJob): Box {
41
40
  const attr = g.attrs.get('pad');
42
- const pad = attr ? parseGraphPad(attr) : { x: 0, y: 0 };
41
+ const pad = renderer.type === 'svg' ? job.pad : attr ? parseGraphPad(attr) : { x: 0, y: 0 };
43
42
  const size = parseDrawingSize(g.attrs.get('size'));
44
43
  return setupPageClip(job.bb, size, pad, initJobViewportZoom(job.bb, size, pad));
45
44
  }
package/src/index.ts CHANGED
@@ -143,6 +143,10 @@ export type {
143
143
  GvExpectation,
144
144
  RenderResult,
145
145
  } from './errors.js';
146
+ // Engine messages (graphviz's stderr): a caller-set function receives them
147
+ // in place of the console, as cgraph's agseterrf. @see lib/cgraph/agerror.c
148
+ export { agseterrf } from './common/agerr.js';
149
+ export type { AgUserErrF } from './common/agerr.js';
146
150
  export { setImageSizer } from './gvc/usershape.js';
147
151
  export type { ImageSizer } from './common/htmltable-types.js';
148
152
  export { setImageResolver } from './gvc/image-resolver.js';
@@ -140,6 +140,28 @@ export function decompSearch(g: Graph, start: Node, cmark: number): void {
140
140
  const _dc = { cmark: 0 };
141
141
  registerRenderState(() => { _dc.cmark = 0; });
142
142
 
143
+ /**
144
+ * The Cmark each graph's records last met, keyed by the root's record. Cmark is
145
+ * a process static, so a layout with no gvFreeLayout before it continues the
146
+ * count, and the ND_mark the last layout left can never equal the new Cmark.
147
+ * The port starts each layout as a new process (AD-4), so the count is carried
148
+ * with the records it is compared against: a free replaces the root's record
149
+ * (layout-reset.ts), and the next layout starts at the reset value, as the
150
+ * zeroed records of a new process do.
151
+ * @see lib/dotgen/decomp.c:Cmark
152
+ * @see lib/gvc/gvlayout.c:gvFreeLayout
153
+ */
154
+ const cmarkByRecord = new WeakMap<object, number>();
155
+
156
+ /** `++Cmark`, continuing the count `g`'s records last met. @see lib/dotgen/decomp.c:decompose */
157
+ function nextCmark(g: Graph): number {
158
+ const record = g.root.info;
159
+ _dc.cmark = Math.max(_dc.cmark, cmarkByRecord.get(record) ?? 0);
160
+ if (++_dc.cmark === 0) _dc.cmark = 1;
161
+ cmarkByRecord.set(record, _dc.cmark);
162
+ return _dc.cmark;
163
+ }
164
+
143
165
  // ONE private function immediately before decompose — the acyclic.ts pattern.
144
166
  function resolveNode(n: Node, pass: 0 | 1): Node | undefined {
145
167
  if (pass > 0) {
@@ -155,8 +177,7 @@ function resolveNode(n: Node, pass: 0 | 1): Node | undefined {
155
177
  * @see lib/dotgen/decomp.c:decompose
156
178
  */
157
179
  export function decompose(g: Graph, pass: 0 | 1): void {
158
- if (++_dc.cmark === 0) _dc.cmark = 1;
159
- const cmark = _dc.cmark;
180
+ const cmark = nextCmark(g);
160
181
  g.info.comp = [];
161
182
  for (const n of nodesInSeq(g)) {
162
183
  const v = resolveNode(n, pass);
@@ -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 } from '../../model/geom.js';
16
+ import type { Point, Box, Port } 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';
@@ -229,6 +229,77 @@ function routeChainSegmented(
229
229
  return finishChain(w, endEdge, segs[ei], segFirst, segs) ? w.pts : null;
230
230
  }
231
231
 
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
+ /** True when `e` runs forward across two or more ranks of a ranked graph. */
297
+ function spansRanksForward(g: Graph, e: GraphEdge): boolean {
298
+ const r = e.tail.info.rank;
299
+ const rh = e.head.info.rank;
300
+ return g.info.rank !== undefined && r !== undefined && rh !== undefined && rh > r + 1;
301
+ }
302
+
232
303
  /**
233
304
  * Route a forward edge spanning ≥2 ranks via its virtual-node chain through the
234
305
  * faithful pipeline (REGULAREDGE side boxes steer the port, routeSplines over
@@ -240,9 +311,7 @@ function routeChainSegmented(
240
311
  * @see lib/dotgen/dotsplines.c:make_regular_edge (hackflag forward path)
241
312
  */
242
313
  export function routeMultiRankEdgeFaithful(g: Graph, e: GraphEdge): Point[] | null | 'lost' {
243
- const r = e.tail.info.rank;
244
- const rh = e.head.info.rank;
245
- if (g.info.rank === undefined || r === undefined || rh === undefined || rh <= r + 1) return null;
314
+ if (!spansRanksForward(g, e)) return null;
246
315
  const et = edgeType(g);
247
316
  // EDGETYPE_LINE: C tries makeLineEdge first (direct tail→head segment),
248
317
  // falling back to the box corridor only when it declines (delr==1, or delr==2
@@ -261,8 +330,10 @@ export function routeMultiRankEdgeFaithful(g: Graph, e: GraphEdge): Point[] | nu
261
330
  // endpath with the LAST (each carries the orig's copied port, so a dyna
262
331
  // record port resolves toward the adjacent-rank vnode, not the far real
263
332
  // endpoint). @see lib/dotgen/dotsplines.c:make_regular_edge (tn/hn walk),
264
- // lib/dotgen/fastgr.c:new_virtual_edge (port copies)
265
- return routeChainSegmented(g, segs[0], segs[segs.length - 1], segs) ?? 'lost';
333
+ // lib/dotgen/fastgr.c:new_virtual_edge (port copies). An edge merged into
334
+ // another's chain takes the hackflag views instead (chainEnds).
335
+ const [beginEdge, endEdge] = chainEnds(e, segs);
336
+ return routeChainSegmented(g, beginEdge, endEdge, segs) ?? 'lost';
266
337
  }
267
338
 
268
339
  /**
@@ -415,7 +486,8 @@ export function routeBackEdge(e: GraphEdge, _tailBox: NodeBox, _headBox: NodeBox
415
486
  // (fastgr new_virtual_edge copy rules); fwdEdge remains the clip target so
416
487
  // bezier_clip keeps the load-bearing forward direction (1644).
417
488
  // @see lib/dotgen/dotsplines.c:make_regular_edge (segfirst/tn/hn walk)
418
- const fwd = routeChainSegmented(g, segs[0], segs[segs.length - 1], segs);
489
+ const [beginEdge, endEdge] = chainEnds(e, segs);
490
+ const fwd = routeChainSegmented(g, beginEdge, endEdge, segs);
419
491
  // Chain routing failed: C's make_regular_edge returns with no spline and
420
492
  // dot_splines_ never revisits the edge — mark it consumed so the backstop
421
493
  // sweep does not re-route (and re-report) it.
@@ -15,7 +15,6 @@
15
15
 
16
16
  import type { Graph } from '../../model/graph.js';
17
17
  import type { Edge as GraphEdge } from '../../model/edge.js';
18
- import { buildOutEdgeIndex } from '../../model/node.js';
19
18
  import type { Point } from '../../model/geom.js';
20
19
  import type { NodeBox } from './edge-route-geom.js';
21
20
  import type { PortRoute } from './edge-route-routing.js';
@@ -27,7 +26,6 @@ import { arrowDrawOpsForEnd } from './edge-route-arrow.js';
27
26
  import { linearBezier } from './edge-route-poly.js';
28
27
  import { rankEdgeInfoOf, routedThisPass } from './edge-route-rank.js';
29
28
  import { routeRegularEdgeFaithful } from './edge-route-faithful.js';
30
- import { edgeRouteCmp } from './edge-order.js';
31
29
  import { isFlatAdjacent, makeFlatAdjEdges } from './splines-flat.js';
32
30
  import { ShapeKind, type ShapeDesc } from '../../common/types.js';
33
31
  import {
@@ -138,7 +136,7 @@ export function hasValidCoords(e: GraphEdge): boolean {
138
136
  }
139
137
 
140
138
  // ---------------------------------------------------------------------------
141
- // routeOneEdge / routeDotEdges
139
+ // routeOneEdge / routeLoneEdge
142
140
  // @see lib/dotgen/dotsplines.c:dot_splines_
143
141
  // ---------------------------------------------------------------------------
144
142
 
@@ -231,10 +229,9 @@ function routeFaithfulRegularPlain(e: GraphEdge, g: Graph): boolean {
231
229
  * the faithful pipeline (AD-1). A back edge is the forward edge with swapped
232
230
  * ends (C `makefwdedge`): route the forward view as a plain adjacent edge, then
233
231
  * `clipAndInstall` installs the forward-geometry spline on the ORIGINAL edge
234
- * (makeFwdEdge sets `to_orig`/`edge_type`). `dotSplines_` runs `edgeNormalize`
235
- * *before* `routeDotEdges`, so the global `swapSpline` pass has already run for
236
- * this edge — apply the back-edge swap here so the spline runs tail→head with
237
- * the arrow at the head. Returns false when `e` is not an adjacent back edge.
232
+ * (makeFwdEdge sets `to_orig`/`edge_type`). Apply the back-edge swap here so
233
+ * the spline runs tail→head with the arrow at the head. Returns false when `e`
234
+ * is not an adjacent back edge.
238
235
  * @see lib/dotgen/dotsplines.c:make_regular_edge (BWDEDGE makefwdedge), edge_normalize
239
236
  */
240
237
  function routeFaithfulAdjacentBack(e: GraphEdge, g: Graph): boolean {
@@ -453,39 +450,17 @@ function routeForwardEdge(
453
450
  e.info.headArrowOps = arrowDrawOpsForEnd(e, 'head', result.arrowTip, result.arrowDir, pw);
454
451
  }
455
452
 
456
- /**
457
- * Collect every real out-edge (same set as `g.nodes.values()` × out-edges) in
458
- * C `dot_splines_` routing order: rank-major collection + `edgecmp` sort. Order
459
- * matters because `recover_slack` mutates shared virtual nodes that other edges
460
- * read as `maximal_bbox` neighbours (S1). When the graph is unranked, preserve
461
- * the legacy insertion order (the sort keys are rank-derived).
462
- * @see lib/dotgen/dotsplines.c:dot_splines_ (edges list build + edgecmp sort)
463
- */
464
- function orderedDotEdges(g: Graph): GraphEdge[] {
465
- const outIdx = buildOutEdgeIndex(g);
466
- const edges: GraphEdge[] = [];
467
- for (const n of g.nodes.values()) {
468
- for (const e of outIdx.get(n) ?? []) edges.push(e);
469
- }
470
- if (g.info.rank !== undefined) edges.sort(edgeRouteCmp);
471
- return edges;
472
- }
473
-
474
- /**
475
- * Compute and install edge splines for all unrouted edges in g.
476
- * @see lib/dotgen/dotsplines.c:dot_splines_
477
- */
478
453
  /**
479
454
  * Route one not-yet-routed edge, applying the `dot_splines_` skip guards. No-op
480
455
  * if the edge is already routed or ineligible (FLATORDER/IGNORED, self-loop, or
481
- * lacking valid coords). Used both by the unified `dotSplines_` loop — which
482
- * routes lone edges at their `edgecmp` position, interleaved with groups, as C
483
- * does — and by the `routeDotEdges` backstop sweep below.
456
+ * lacking valid coords). The `dotSplines_` group loop calls it for the edges it
457
+ * collected from the rank arrays, at their `edgecmp` position, as C does; an
458
+ * edge it did not collect keeps the route it had, as `ED_spl` does in C.
484
459
  * @see lib/dotgen/dotsplines.c:dot_splines_ (per-edge dispatch + 295 skip)
485
460
  */
486
461
  export function routeLoneEdge(e: GraphEdge, g: Graph): void {
487
462
  // A lost edge (routesplines failure, C semantics) is consumed: C routes each
488
- // edge exactly once, so the backstop sweep must not re-attempt it.
463
+ // edge exactly once, so a later group must not re-attempt it.
489
464
  if (routedThisPass(e) || e.info.lost === true) return;
490
465
  // C dot_splines_ skips FLATORDER/IGNORED edges when building the route list;
491
466
  // an IGNORED edge (e.g. a concentrate-merged parallel duplicate) must get no
@@ -507,7 +482,3 @@ export function routeLoneEdge(e: GraphEdge, g: Graph): void {
507
482
  if (!hasValidCoords(e)) return;
508
483
  routeOneEdge(e, g);
509
484
  }
510
-
511
- export function routeDotEdges(g: Graph): void {
512
- for (const e of orderedDotEdges(g)) routeLoneEdge(e, g);
513
- }
@@ -118,6 +118,28 @@ export function dotInitNode(n: Node): void {
118
118
  // dotInitEdge
119
119
  // ---------------------------------------------------------------------------
120
120
 
121
+ /**
122
+ * Which of dot_init_edge's attribute symbols are bound. C reads `constraint`,
123
+ * `minlen` and `group` through the globals E_constr, E_minlen and N_group
124
+ * (bound per graph by graph_init). make_flat_adj_edges' setState nulls all
125
+ * three for the aux layout and cleanupCloneGraph restores them, so an aux
126
+ * edge's copied `constraint=false` or `minlen`, and its clones' `group`, are
127
+ * never read: late_int(e, NULL, 1, 0) is 1 and late_string(n, NULL, "") "".
128
+ * @see lib/common/input.c:graph_init (E_constr, E_minlen, N_group)
129
+ * @see lib/dotgen/dotsplines.c:setState, cleanupCloneGraph
130
+ */
131
+ export interface InitEdgeSyms {
132
+ readonly constr: boolean;
133
+ readonly minlen: boolean;
134
+ readonly group: boolean;
135
+ }
136
+
137
+ /** The main graph's symbols, as graph_init binds them. */
138
+ export const DOT_INIT_SYMS: InitEdgeSyms = { constr: true, minlen: true, group: true };
139
+
140
+ /** The flat-adj aux graph's symbols. @see lib/dotgen/dotsplines.c:setState */
141
+ export const AUX_INIT_SYMS: InitEdgeSyms = { constr: false, minlen: false, group: false };
142
+
121
143
  /**
122
144
  * True when tail and head carry the same non-empty `group` attribute.
123
145
  * C compares interned refstr pointers (`tailgroup == headgroup`); cgraph's
@@ -125,7 +147,8 @@ export function dotInitNode(n: Node): void {
125
147
  * `tailgroup[0]` guard excludes the shared "" default.
126
148
  * @see lib/dotgen/dotinit.c:dot_init_edge (tailgroup/headgroup check)
127
149
  */
128
- export function sameNonemptyGroup(e: Edge): boolean {
150
+ export function sameNonemptyGroup(e: Edge, syms: InitEdgeSyms = DOT_INIT_SYMS): boolean {
151
+ if (!syms.group) return false;
129
152
  const tailgroup = nodeAttr(e.tail, e.tail.root, 'group') ?? '';
130
153
  const headgroup = nodeAttr(e.head, e.head.root, 'group') ?? '';
131
154
  return tailgroup !== '' && tailgroup === headgroup;
@@ -146,8 +169,8 @@ function initEdgeConstraint(e: Edge): void {
146
169
  * Applies the same-group penalty to xpenalty and weight.
147
170
  * @see lib/dotgen/dotinit.c:dot_init_edge (tailgroup/headgroup block)
148
171
  */
149
- function applyGroupPenalty(e: Edge): void {
150
- if (!sameNonemptyGroup(e)) return;
172
+ function applyGroupPenalty(e: Edge, syms: InitEdgeSyms): void {
173
+ if (!sameNonemptyGroup(e, syms)) return;
151
174
  e.info.xpenalty = CL_CROSS;
152
175
  e.info.weight = (e.info.weight ?? 1) * 100;
153
176
  }
@@ -168,8 +191,8 @@ function applyNonconstraintZero(e: Edge): void {
168
191
  *
169
192
  * @see lib/dotgen/dotinit.c:dot_init_edge
170
193
  */
171
- export function dotInitEdge(e: Edge): void {
172
- initEdgeConstraint(e);
194
+ export function dotInitEdge(e: Edge, syms: InitEdgeSyms = DOT_INIT_SYMS): void {
195
+ if (syms.constr) initEdgeConstraint(e);
173
196
  // C: ED_weight(e) = late_int(e, E_weight, 1, 0), the attribute only, on an aux
174
197
  // edge too: make_flat_adj_edges writes its 10000 with agxset. The group and
175
198
  // nonconstraint adjustments below then apply on top, matching C's order.
@@ -177,11 +200,11 @@ export function dotInitEdge(e: Edge): void {
177
200
  e.info.weight = lateInt(e.attrs.get('weight'), 1, 0);
178
201
  e.info.count = 1;
179
202
  e.info.xpenalty = 1;
180
- applyGroupPenalty(e);
203
+ applyGroupPenalty(e, syms);
181
204
  applyNonconstraintZero(e);
182
- // late_int semantics: default 1, minimum 0.
205
+ // late_int semantics: default 1, minimum 0; an unbound E_minlen gives 1.
183
206
  // @see lib/dotgen/dotinit.c:85 ED_minlen(e) = late_int(e, E_minlen, 1, 0)
184
- e.info.minlen = lateInt(e.attrs.get('minlen'), 1, 0);
207
+ e.info.minlen = lateInt(syms.minlen ? e.attrs.get('minlen') : undefined, 1, 0);
185
208
  // samehead / sametail group ids for shared-port merging. C reads these via
186
209
  // agxget(e, E_samehead/E_sametail) in dot_sameports and treats an empty
187
210
  // string (id[0] == '\0') as "no group". Without populating these, the ported
@@ -233,19 +256,21 @@ function clearFastGraphLists(n: Node): void {
233
256
 
234
257
  /**
235
258
  * Calls commonInitNode (shape-aware, label-driven sizing) then
236
- * dotInitNode for every node, then dotInitEdge for every edge.
259
+ * dotInitNode for every node, then dotInitEdge for every edge. `syms` stands
260
+ * for the global symbol state C's dot_init_edge reads (AUX_INIT_SYMS inside
261
+ * make_flat_adj_edges).
237
262
  *
238
263
  * @see lib/dotgen/dotinit.c:dot_init_node_edge
239
264
  * @see lib/dotgen/dotinit.c:dot_init_node (common_init_node + gv_nodesize)
240
265
  */
241
- export function dotInitNodeEdge(g: Graph): void {
266
+ export function dotInitNodeEdge(g: Graph, syms: InitEdgeSyms = DOT_INIT_SYMS): void {
242
267
  for (const n of nodesInSeq(g)) {
243
268
  commonInitNode(n, g);
244
269
  dotInitNode(n);
245
270
  }
246
271
  const measurer = g.root.info.gvc?.textMeasurer;
247
272
  for (const e of g.edges) {
248
- dotInitEdge(e);
273
+ dotInitEdge(e, syms);
249
274
  if (measurer) initEdgeLabels(e, g, measurer);
250
275
  }
251
276
  }
@@ -34,7 +34,7 @@ import { getSplineBounds, staleAdjacent } from './edge-route-rank.js';
34
34
  import { graphRanksep } from './position-aux.js';
35
35
  import { Edge as EdgeClass } from '../../model/edge.js';
36
36
  import { cloneNode, cloneEdge, cleanupCloneGraph, transformf } from './splines-clone.js';
37
- import { dotInitNodeEdge } from './init.js';
37
+ import { dotInitNodeEdge, AUX_INIT_SYMS } from './init.js';
38
38
  import { gvPostprocess } from '../../common/postproc.js';
39
39
  import { newSpline } from '../../common/splines-clip.js';
40
40
  import { mapArrowOpPoints } from '../../common/arrows-shapes-util.js';
@@ -367,7 +367,7 @@ export function makeFlatAdjEdges(g: Graph, edges: Edge[], cnt: number, et: numbe
367
367
  // C setEdgeType(auxg, et); inlined (index.ts's setEdgeType would be an import cycle).
368
368
  // @see lib/dotgen/dotsplines.c:make_flat_adj_edges
369
369
  aux.auxg.info.flags = (aux.auxg.info.flags & ~0xf) | (et & 0xf);
370
- dotInitNodeEdge(aux.auxg);
370
+ dotInitNodeEdge(aux.auxg, AUX_INIT_SYMS);
371
371
  dotRank(aux.auxg);
372
372
  if (dotMincross(aux.auxg) !== 0) return 1;
373
373
  if (dotPosition(aux.auxg) !== 0) return 1;
@@ -94,8 +94,8 @@ export function dedupByOrig(group: Edge[]): Edge[] {
94
94
  /**
95
95
  * Dispatch one edgecmp group: self-loop → routeSelfEdgeGroup; cross-rank cnt>1 →
96
96
  * routeParallelEdgeGroup (Multisep offsets); cross-rank cnt==1 → per-run or
97
- * whole-chain lone routing in-place (C order); same-rank flat → left for the
98
- * routeDotEdges sweep.
97
+ * whole-chain lone routing in-place (C order); same-rank flat → routed in place
98
+ * at its edgecmp position.
99
99
  * @see lib/dotgen/dotsplines.c:367-419
100
100
  */
101
101
  function dispatchEdgeGroup(g: Graph, group: Edge[], ctx: GroupRouteCtx): void {
@@ -387,8 +387,8 @@ export function routeParallelEdgeGroup(
387
387
  }) ?? edges[0]!;
388
388
  const base = baseSplineForGroup(g, baseEdge);
389
389
  // A lost group base consumes every member: C's dot_splines_ calls
390
- // make_regular_edge once per group and never re-routes its edges, so the
391
- // routeDotEdges backstop must not re-attempt (and re-report) them.
390
+ // make_regular_edge once per group and never re-routes its edges, so no
391
+ // later group may re-attempt (and re-report) them.
392
392
  // @see lib/dotgen/dotsplines.c:dot_splines_ (group loop, one call per group)
393
393
  if (base === 'lost') {
394
394
  for (const e of edges) resolveOrigEdge(e).info.lost = true;