@knowvah/dot-engine 2.12.3 → 2.13.0

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 (92) hide show
  1. package/dist/api.js +13 -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/htmltable-emit.d.ts +9 -6
  6. package/dist/common/htmltable-emit.d.ts.map +1 -1
  7. package/dist/common/layers.d.ts +9 -0
  8. package/dist/common/layers.d.ts.map +1 -1
  9. package/dist/common/poly-gencode.d.ts.map +1 -1
  10. package/dist/common/xlabels-place.d.ts.map +1 -1
  11. package/dist/errors.d.ts +11 -3
  12. package/dist/errors.d.ts.map +1 -1
  13. package/dist/gvc/anchor.d.ts +17 -19
  14. package/dist/gvc/anchor.d.ts.map +1 -1
  15. package/dist/gvc/context.d.ts +7 -0
  16. package/dist/gvc/context.d.ts.map +1 -1
  17. package/dist/gvc/device-cluster.d.ts +9 -0
  18. package/dist/gvc/device-cluster.d.ts.map +1 -1
  19. package/dist/gvc/device.d.ts +2 -1
  20. package/dist/gvc/device.d.ts.map +1 -1
  21. package/dist/gvc/emit-walk.d.ts +7 -0
  22. package/dist/gvc/emit-walk.d.ts.map +1 -1
  23. package/dist/gvc/job.d.ts +13 -6
  24. package/dist/gvc/job.d.ts.map +1 -1
  25. package/dist/gvc/viewport.d.ts +14 -0
  26. package/dist/gvc/viewport.d.ts.map +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +631 -498
  29. package/dist/index.js.map +4 -4
  30. package/dist/layout/dot/classify.d.ts.map +1 -1
  31. package/dist/layout/dot/compound.d.ts +0 -4
  32. package/dist/layout/dot/compound.d.ts.map +1 -1
  33. package/dist/layout/dot/ns-range.d.ts.map +1 -1
  34. package/dist/layout/fdp/cluster-edges.d.ts.map +1 -1
  35. package/dist/model/cgraph-ops.d.ts +7 -0
  36. package/dist/model/cgraph-ops.d.ts.map +1 -1
  37. package/dist/model/graph.d.ts +12 -2
  38. package/dist/model/graph.d.ts.map +1 -1
  39. package/dist/parser/builder.d.ts +11 -10
  40. package/dist/parser/builder.d.ts.map +1 -1
  41. package/dist/parser/graph-attr-syms.d.ts +35 -0
  42. package/dist/parser/graph-attr-syms.d.ts.map +1 -0
  43. package/dist/render/dot/agwrite.d.ts +6 -7
  44. package/dist/render/dot/agwrite.d.ts.map +1 -1
  45. package/dist/render/dot/attrs.d.ts +5 -0
  46. package/dist/render/dot/attrs.d.ts.map +1 -1
  47. package/dist/render/json-cluster-edge.d.ts +3 -2
  48. package/dist/render/json-cluster-edge.d.ts.map +1 -1
  49. package/dist/render/json.d.ts +7 -1
  50. package/dist/render/json.d.ts.map +1 -1
  51. package/dist/render/map.d.ts +14 -10
  52. package/dist/render/map.d.ts.map +1 -1
  53. package/dist/render/svg-graph.d.ts.map +1 -1
  54. package/dist/render/svg-id.d.ts +9 -0
  55. package/dist/render/svg-id.d.ts.map +1 -1
  56. package/dist/render.js +713 -581
  57. package/dist/render.js.map +4 -4
  58. package/dist/util/printf-round.d.ts +8 -0
  59. package/dist/util/printf-round.d.ts.map +1 -1
  60. package/package.json +1 -1
  61. package/src/common/clust-map.ts +58 -0
  62. package/src/common/htmltable-emit.ts +87 -7
  63. package/src/common/layers.ts +25 -4
  64. package/src/common/poly-gencode.ts +3 -11
  65. package/src/common/xlabels-place.ts +22 -4
  66. package/src/errors.ts +13 -3
  67. package/src/gvc/anchor.ts +18 -30
  68. package/src/gvc/context.ts +21 -6
  69. package/src/gvc/device-cluster.ts +16 -0
  70. package/src/gvc/device.ts +20 -20
  71. package/src/gvc/emit-walk.ts +11 -1
  72. package/src/gvc/job.ts +17 -9
  73. package/src/gvc/viewport.ts +27 -6
  74. package/src/index.ts +1 -0
  75. package/src/layout/dot/classify.ts +23 -1
  76. package/src/layout/dot/compound.ts +1 -21
  77. package/src/layout/dot/ns-range.ts +41 -5
  78. package/src/layout/fdp/cluster-edges.ts +2 -23
  79. package/src/model/cgraph-ops.ts +12 -0
  80. package/src/model/graph.ts +13 -2
  81. package/src/parser/builder.ts +67 -100
  82. package/src/parser/graph-attr-syms.ts +62 -0
  83. package/src/render/dot/agwrite.ts +12 -12
  84. package/src/render/dot/attrs.ts +6 -2
  85. package/src/render/json-cluster-edge.ts +8 -4
  86. package/src/render/json.ts +15 -8
  87. package/src/render/map.ts +49 -58
  88. package/src/render/svg-cluster.ts +3 -3
  89. package/src/render/svg-graph.ts +1 -3
  90. package/src/render/svg-helpers.ts +6 -6
  91. package/src/render/svg-id.ts +23 -5
  92. package/src/util/printf-round.ts +11 -0
package/src/gvc/device.ts CHANGED
@@ -25,7 +25,7 @@ import { resolveEdgeAnchor, resolveObjAnchor, beginAnchorIf } from './anchor.js'
25
25
  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
- import { walkNodesAndEdges } from './emit-walk.js';
28
+ import { walkNodesAndEdges, clusterShown } from './emit-walk.js';
29
29
  import { initBb } from '../common/emit-bb.js';
30
30
  import { firstLayer, nextLayer, numPhysicalLayers, validLayer } from './first-layer.js';
31
31
  import { polyInit } from '../common/poly-init.js';
@@ -49,14 +49,14 @@ import {
49
49
  setRendererKnownColors,
50
50
  } from '../render/color-resolve.js';
51
51
  import { emitRoundedBezier } from '../common/poly-shapes.js';
52
- import { applyClusterObjState, clusterAttr, clusterStyle, clusterPeripheries, isStripedCluster, renderStripedClusterBox } from './device-cluster.js';
52
+ import { applyClusterObjState, clusterAttr, clusterStyle, clusterPeripheries, isStripedCluster, renderStripedClusterBox, beginClusterAnchor } from './device-cluster.js';
53
53
  import { transformPoint } from './device-transform.js';
54
54
  import { deviceClip, emitXdot } from './emit-xdot.js';
55
55
  import { gvrenderSetStyle, edgeStyleResets } from './gvrender-set-style.js';
56
56
  import { renderOneLabel, renderNodeXLabel, renderClusterLabel, reportEdgeLabelColors } from './device-labels.js';
57
57
  export { transformPoint, applyRotation, applyScale, buildPoint } from './device-transform.js';
58
58
  export { renderOneLabel, renderNodeXLabel, renderClusterLabel } from './device-labels.js';
59
- import { svgNodeId, svgEdgeId, svgClusterId, svgGraphId } from '../render/svg-id.js';
59
+ import { svgNodeId, svgEdgeId, svgClusterId, svgGraphId, drawingIdOf } from '../render/svg-id.js';
60
60
  import { nodeInBox, emitNodeComments, emitEdgeComments } from './emit-gates.js';
61
61
 
62
62
  // ---------------------------------------------------------------------------
@@ -279,12 +279,12 @@ function drawClusterInScheme(sg: Graph, renderer: RendererPlugin, job: RenderJob
279
279
  if (job.obj !== null) job.obj.id = svgClusterId(sg, job);
280
280
  // @see lib/common/emit.c:emit_begin_cluster / getObjId
281
281
  setHtmlAnchorObj(svgClusterId(sg, job), labelTextOf(sg.info.label), job.obj ?? undefined);
282
- // Resolve and open the cluster anchor around the box + label (closed before
283
- // the cluster's child nodes — sibling of them). @see emit.c:3803.
284
- if (job.obj !== null) {
285
- resolveObjAnchor(sg, labelTextOf(sg.info.label), svgClusterId(sg, job), job.obj);
286
- }
287
- const anchored = beginAnchorIf(renderer, job);
282
+ // Resolve the cluster anchor. Drawing devices open it around the box + label;
283
+ // the map device (EMIT_CLUSTERS_LAST) opens it after the label, so the
284
+ // cluster's <area> follows its label's cell areas. @see emit.c:3743, 3861
285
+ if (job.obj !== null) resolveObjAnchor(sg, labelTextOf(sg.info.label), svgClusterId(sg, job), job.obj);
286
+ const late = (job.flags & EMIT_CLUSTERS_LAST) !== 0;
287
+ const anchored = !late && beginClusterAnchor(sg, renderer, job);
288
288
  // C draws the boundary box only when peripheries != 0, or (peripheries == 0
289
289
  // and the cluster is filled). peripheries=0 + unfilled emits nothing.
290
290
  // style=invis sets PEN_NONE and gvrender_polygon skips the whole box
@@ -294,7 +294,7 @@ function drawClusterInScheme(sg: Graph, renderer: RendererPlugin, job: RenderJob
294
294
  renderClusterBox(sg, filled, renderer, job);
295
295
  }
296
296
  renderClusterLabel(sg, renderer, job);
297
- if (anchored) renderer.endAnchor?.(job);
297
+ if (anchored || (late && beginClusterAnchor(sg, renderer, job))) renderer.endAnchor?.(job);
298
298
  renderer.endCluster?.(sg, job);
299
299
  }
300
300
 
@@ -305,13 +305,13 @@ function drawClusterInScheme(sg: Graph, renderer: RendererPlugin, job: RenderJob
305
305
  * completes before recursing; sub-clusters are separate pushes on a clean stack.
306
306
  * @see lib/common/emit.c:emit_clusters:3777
307
307
  */
308
- function renderOneCluster(sg: Graph, renderer: RendererPlugin, job: RenderJob): void {
308
+ function renderOneCluster(sg: Graph, renderer: RendererPlugin, job: RenderJob, info?: LayerInfo): void {
309
309
  // EMIT_CLUSTERS_LAST (map device): recurse into sub-clusters BEFORE emitting
310
310
  // this cluster's anchor, so an inner cluster's <area> precedes its parent's
311
311
  // (child-first). Drawing devices recurse AFTER (parent-first), below.
312
312
  // @see lib/common/emit.c:emit_clusters (3795 vs 3940)
313
313
  const clustersLast = (job.flags & EMIT_CLUSTERS_LAST) !== 0;
314
- if (clustersLast) renderClusters(sg, renderer, job);
314
+ if (clustersLast) renderClusters(sg, renderer, job, info);
315
315
  // push_obj_state in emit_begin_cluster (line 3762), before beginCluster
316
316
  const obj = createObjState(ObjType.Cluster);
317
317
  obj.emitState = EmitState.CDraw;
@@ -328,7 +328,7 @@ function renderOneCluster(sg: Graph, renderer: RendererPlugin, job: RenderJob):
328
328
  // C recurses AFTER emit_end_cluster (line 3940-3941): sub-clusters are drawn
329
329
  // with the parent already popped — each has its own independent push/pop.
330
330
  // The map device recursed first (child-first), above.
331
- if (!clustersLast) renderClusters(sg, renderer, job);
331
+ if (!clustersLast) renderClusters(sg, renderer, job, info);
332
332
  }
333
333
 
334
334
  /**
@@ -337,13 +337,13 @@ function renderOneCluster(sg: Graph, renderer: RendererPlugin, job: RenderJob):
337
337
  * @see lib/gvc/gvrender.c:gvrender_box
338
338
  * @see lib/common/emit.c:emit_clusters
339
339
  */
340
- export function renderClusters(g: Graph, renderer: RendererPlugin, job: RenderJob): void {
340
+ export function renderClusters(g: Graph, renderer: RendererPlugin, job: RenderJob, info?: LayerInfo): void {
341
341
  const nClust = g.info.n_cluster ?? 0;
342
342
  const clust = g.info.clust;
343
343
  if (nClust === 0 || !clust) return;
344
344
  for (let c = 0; c < nClust; c++) {
345
345
  const sg = clust[c];
346
- if (sg) renderOneCluster(sg, renderer, job);
346
+ if (sg && clusterShown(info, job, sg)) renderOneCluster(sg, renderer, job, info);
347
347
  }
348
348
  }
349
349
 
@@ -404,9 +404,9 @@ function renderPage(g: Graph, renderer: RendererPlugin, job: RenderJob, info: La
404
404
  // device (EMIT_CLUSTERS_LAST) emits their <area> AFTER, so a node/edge area
405
405
  // precedes its enclosing cluster's. @see lib/common/emit.c:emit_view (3513-3565)
406
406
  const clustersLast = (job.flags & EMIT_CLUSTERS_LAST) !== 0;
407
- if (!clustersLast) renderClusters(g, renderer, job);
407
+ if (!clustersLast) renderClusters(g, renderer, job, info);
408
408
  walkNodesAndEdges(g, renderer, job, info);
409
- if (clustersLast) renderClusters(g, renderer, job);
409
+ if (clustersLast) renderClusters(g, renderer, job, info);
410
410
  renderer.endPage?.(g, job);
411
411
  }
412
412
 
@@ -497,12 +497,12 @@ export function render(ctx: GvcContext, g: Graph, format: string, inlineImages =
497
497
  const dpiScale = jobDpi / POINTS_PER_INCH;
498
498
  job.scale = { x: z * dpiScale, y: z * dpiScale };
499
499
  // init_job_viewport: job->rotation = gvc->rotation (= 90 for landscape, set in
500
- // emit_graph from GD_drawing->landscape). Emit-only — drives the SVG group
501
- // transform + dim swap (T2), never layout/routing (ADR-1). transformPoint is
502
- // guarded against the ptf rotation branch (ADR-2).
500
+ // emit_graph from GD_drawing->landscape). Emit-only: drives the SVG group
501
+ // transform + dim swap (T2), never layout/routing (ADR-1; ptf guard: ADR-2).
503
502
  // @see lib/common/emit.c:3260 (gvc->rotation=90); :3390 (job->rotation)
504
503
  job.rotation = parseLandscape(g) ? 90 : 0;
505
504
  job.renderer = renderer;
505
+ job.drawingId = drawingIdOf(g); // GD_drawing(g)->id. @see lib/common/input.c:graph_init
506
506
  setRendererKnownColors(renderer.type === 'svg'); // svg_knowncolors; others have none
507
507
  resetHtmlAnchorIds(); // C: fresh anchorId per dot invocation
508
508
  for (const n of g.nodes.values()) polyInit(n, g, job.measurer);
@@ -19,7 +19,7 @@ import type { Edge } from '../model/edge.js';
19
19
  import { buildOutEdgeIndex } from '../model/node.js';
20
20
  import type { RendererPlugin } from './context.js';
21
21
  import type { RenderJob } from './job.js';
22
- import { type LayerInfo, nodeInLayer, edgeInLayer } from '../common/layers.js';
22
+ import { type LayerInfo, nodeInLayer, edgeInLayer, clustInLayer } from '../common/layers.js';
23
23
  import { renderNode, renderEdge } from './device.js';
24
24
 
25
25
  /** Whether a node is shown in the current layer (always true off-layer). */
@@ -32,6 +32,16 @@ function edgeShown(info: LayerInfo | undefined, job: RenderJob, e: Edge): boolea
32
32
  return info === undefined || job.numLayers <= 1 || edgeInLayer(info, job.layerNum, e);
33
33
  }
34
34
 
35
+ /**
36
+ * Whether a cluster is drawn in the current layer (always true off-layer).
37
+ * C tests this in emit_clusters' loop, before drawing the cluster or
38
+ * recursing into it, so a skipped cluster's sub-clusters are skipped too.
39
+ * @see lib/common/emit.c:emit_clusters
40
+ */
41
+ export function clusterShown(info: LayerInfo | undefined, job: RenderJob, sg: Graph): boolean {
42
+ return info === undefined || job.numLayers <= 1 || clustInLayer(info, job.layerNum, sg);
43
+ }
44
+
35
45
  /**
36
46
  * Output order from the `outputorder` graph attr, mirroring C's chkOrder:
37
47
  * `nodesfirst` → EMIT_SORTED, `edgesfirst` → EMIT_EDGE_SORTED, else breadth-first.
package/src/gvc/job.ts CHANGED
@@ -309,20 +309,28 @@ export class RenderJob {
309
309
  /** GD_fontnames of the root graph — selects which PostScript-alias fields the
310
310
  * SVG emitter uses. @see plugin/core/gvrender_core_svg.c:464 */
311
311
  fontnames: FontnameKind = FontnameKind.NativeFonts;
312
- /** Root graph's `id` attribute (GD_drawing(root)->id), '' if unset. Used as
313
- * the `<gid>_` prefix for non-root object ids. @see lib/common/emit.c:getObjId */
312
+ /** GD_drawing(root)->id: the root graph's `id`, substituted, '' if unset. Set
313
+ * by render() for every renderer (drawingIdOf); the `<gid>_` prefix of
314
+ * non-root object ids. @see lib/common/emit.c:getObjId */
314
315
  drawingId: string = '';
315
316
 
316
317
  /**
317
- * Resolve an object's SVG id like C's getObjId (steps after layerPagePrefix):
318
- * a non-empty DOT `id` attr wins verbatim; otherwise non-root objects are
319
- * prefixed with `<drawingId>_` when the root graph has an id.
320
- * @see lib/common/emit.c:getObjId:209
318
+ * Resolve an object's id like C's getObjId. The layer half of
319
+ * layerPagePrefix comes first (`<layer>_` after the first layer), ahead of
320
+ * every other step. Then a non-empty DOT `id` attr wins verbatim; otherwise
321
+ * non-root objects are prefixed with `<drawingId>_` when the root graph has
322
+ * an id. The root's id gets the same prefix in emit_page (NotFirstPage).
323
+ * The page half (`page<x>,<y>_`) is left out: only ps and quartz page, and
324
+ * neither prints an id.
325
+ * @see lib/common/emit.c:getObjId
326
+ * @see lib/common/emit.c:layerPagePrefix
327
+ * @see lib/common/emit.c:emit_page
321
328
  */
322
329
  objId(idAttr: string | undefined, defaultId: string, isRoot = false): string {
323
- if (idAttr !== undefined && idAttr.length > 0) return idAttr;
324
- if (!isRoot && this.drawingId.length > 0) return this.drawingId + '_' + defaultId;
325
- return defaultId;
330
+ const pfx = this.idLayerPrefix();
331
+ if (idAttr !== undefined && idAttr.length > 0) return pfx + idAttr;
332
+ if (!isRoot && this.drawingId.length > 0) return pfx + this.drawingId + '_' + defaultId;
333
+ return pfx + defaultId;
326
334
  }
327
335
 
328
336
  /** Active renderer plugin; set by render() before walkNodes. */
@@ -190,6 +190,22 @@ export function initJobViewportZoom(bb: Box, size: DrawingSize | null, pad: Poin
190
190
  * @see lib/common/emit.c:init_job_viewport, init_job_pagination, setup_page
191
191
  */
192
192
  export function setupPageClip(bb: Box, size: DrawingSize | null, pad: Point, z: number): Box {
193
+ const focus = { x: 0.5 * (bb.ll.x + bb.ur.x), y: 0.5 * (bb.ll.y + bb.ur.y) };
194
+ const pageSize = setupPageSize(bb, size, pad, z);
195
+ const ll = {
196
+ x: focus.x + pageSize.x * (0 - 1 / 2.0),
197
+ y: focus.y + pageSize.y * (0 - 1 / 2.0),
198
+ };
199
+ return { ll, ur: { x: ll.x + pageSize.x, y: ll.y + pageSize.y } };
200
+ }
201
+
202
+ /**
203
+ * job->pageSize, the size of one page in graph units: imageSize / zoom, where
204
+ * imageSize = view = Z * sz and sz is the padded bb's extent (see
205
+ * setupPageClip). Not always sz: (Z * sz) / Z can round one ulp away from it.
206
+ * @see lib/common/emit.c:init_job_viewport, init_job_pagination
207
+ */
208
+ export function setupPageSize(bb: Box, size: DrawingSize | null, pad: Point, z: number): Point {
193
209
  const jbLL = { x: bb.ll.x - pad.x, y: bb.ll.y - pad.y };
194
210
  const jbUR = { x: bb.ur.x + pad.x, y: bb.ur.y + pad.y };
195
211
  const sz = { x: jbUR.x - jbLL.x, y: jbUR.y - jbLL.y };
@@ -197,11 +213,16 @@ export function setupPageClip(bb: Box, size: DrawingSize | null, pad: Point, z:
197
213
  if (sz.x <= 0.001) sz.x = size.x;
198
214
  if (sz.y <= 0.001) sz.y = size.y;
199
215
  }
200
- const focus = { x: 0.5 * (bb.ll.x + bb.ur.x), y: 0.5 * (bb.ll.y + bb.ur.y) };
201
- const pageSize = { x: (z * sz.x) / z, y: (z * sz.y) / z };
202
- const ll = {
203
- x: focus.x + pageSize.x * (0 - 1 / 2.0),
204
- y: focus.y + pageSize.y * (0 - 1 / 2.0),
205
- };
216
+ return { x: (z * sz.x) / z, y: (z * sz.y) / z };
217
+ }
218
+
219
+ /**
220
+ * job->pageBox for the single page (pagesArrayElem 0): LL = elem * pageSize -
221
+ * pad, UR = LL + pageSize. Its origin is -pad whatever the bb's is, and it is
222
+ * derived apart from job->clip, so the two can differ in their last bits.
223
+ * @see lib/common/emit.c:setup_page (1515-1518)
224
+ */
225
+ export function setupPageBox(pageSize: Point, pad: Point): Box {
226
+ const ll = { x: 0 * pageSize.x - pad.x, y: 0 * pageSize.y - pad.y };
206
227
  return { ll, ur: { x: ll.x + pageSize.x, y: ll.y + pageSize.y } };
207
228
  }
package/src/index.ts CHANGED
@@ -52,6 +52,7 @@ function classifyError(err: unknown): GvError {
52
52
  };
53
53
  if (gv.location !== undefined) out.location = gv.location;
54
54
  if (gv.expected !== undefined) out.expected = gv.expected;
55
+ if (gv.context !== undefined) out.context = { ...gv.context };
55
56
  return out;
56
57
  }
57
58
 
@@ -17,6 +17,8 @@ import type { Graph } from '../../model/graph.js';
17
17
  import type { Node } from '../../model/node.js';
18
18
  import { buildOutEdgeIndex } from '../../model/node.js';
19
19
  import type { Edge } from '../../model/edge.js';
20
+ import { agerrorf } from '../../common/agerr.js';
21
+ import { RenderError } from '../../errors.js';
20
22
  import {
21
23
  VIRTUAL,
22
24
  virtualNode, virtualEdge, fastNode, flatEdge,
@@ -68,6 +70,7 @@ export function nonconstraintEdge(e: Edge): boolean {
68
70
  const WNODE_ORDINARY = 0;
69
71
  const WNODE_SINGLETON = 1;
70
72
  const WNODE_VIRTUAL = 2;
73
+ const INT_MAX = 2147483647;
71
74
  const CLASSIFY_WEIGHT_TABLE = [[1, 1, 1], [1, 2, 2], [1, 2, 4]];
72
75
 
73
76
  function epClass(n: Node): number {
@@ -79,7 +82,26 @@ function epClass(n: Node): number {
79
82
  /** @see lib/dotgen/mincross.c:virtual_weight (inlined to avoid cycle) */
80
83
  function applyVirtualWeight(e: Edge): void {
81
84
  const t = CLASSIFY_WEIGHT_TABLE[epClass(e.tail)][epClass(e.head)];
82
- e.info.weight = (e.info.weight ?? 1) * t;
85
+ const w = e.info.weight ?? 1;
86
+ // C: INT_MAX / t < ED_weight(e), int division, before the multiply.
87
+ if (Math.trunc(INT_MAX / t) < w) throw virtualWeightOverflow(e, t);
88
+ e.info.weight = w * t;
89
+ }
90
+
91
+ /**
92
+ * C `agerrorf` + `graphviz_exit(EXIT_FAILURE)`: the stderr line verbatim,
93
+ * then the structured throw naming the original edge.
94
+ * @see lib/dotgen/mincross.c:virtual_weight
95
+ */
96
+ function virtualWeightOverflow(e: Edge, t: number): RenderError {
97
+ agerrorf('overflow when calculating virtual weight of edge');
98
+ const o = e.info.to_orig ?? e;
99
+ const name = `${o.tail.name}->${o.head.name}`;
100
+ return new RenderError(
101
+ `Edge "${name}" has a weight too large for the layout (weight x ${t} exceeds the 32-bit integer limit); lower its weight attribute.`,
102
+ 'RENDER_ERROR',
103
+ { context: { edge: name } },
104
+ );
83
105
  }
84
106
 
85
107
  // ---------------------------------------------------------------------------
@@ -15,6 +15,7 @@ import {
15
15
  import { arrowEndClip, tailArrowEndClip, arrowClipLength, distFused } from './edge-route-clip.js';
16
16
  import { arrowDrawOpsForEnd, edgeArrowName, edgeArrowsize } from './edge-route-arrow.js';
17
17
  import { edgePenwidthAttr, edgeRenderPenwidth } from './edge-route-helpers.js';
18
+ import { mkClustMap } from '../../common/clust-map.js';
18
19
 
19
20
  // Re-export geometry helpers so existing callers (tests) import from one place.
20
21
  export {
@@ -24,27 +25,6 @@ export {
24
25
  tryLeftSide, tryRightSide, tryBottomSide, tryTopSide, boxIntersectf,
25
26
  } from './compound-clip.js';
26
27
 
27
- // ---------------------------------------------------------------------------
28
- // Cluster map
29
- // ---------------------------------------------------------------------------
30
-
31
- /** Recursively populate name→cluster map. @see lib/common/utils.c:fillMap */
32
- export function fillClustMap(g: Graph, map: Map<string, Graph>): void {
33
- const nc = g.info.n_cluster ?? 0;
34
- for (let c = 1; c <= nc; c++) {
35
- const cl = g.info.clust![c - 1];
36
- if (!map.has(cl.name)) map.set(cl.name, cl);
37
- fillClustMap(cl, map);
38
- }
39
- }
40
-
41
- /** Build a name→cluster map for all clusters reachable from g. @see lib/common/utils.c:mkClustMap */
42
- export function mkClustMap(g: Graph): Map<string, Graph> {
43
- const map = new Map<string, Graph>();
44
- fillClustMap(g, map);
45
- return map;
46
- }
47
-
48
28
  // ---------------------------------------------------------------------------
49
29
  // ClipState — shared between head/tail clip helpers
50
30
  // ---------------------------------------------------------------------------
@@ -3,6 +3,8 @@
3
3
  import type { Node } from '../../model/node.js';
4
4
  import type { Edge } from '../../model/edge.js';
5
5
  import type { EdgeList } from '../../model/nodeInfo.js';
6
+ import { agerrorf } from '../../common/agerr.js';
7
+ import { RenderError } from '../../errors.js';
6
8
  import { seq, isTreeEdge } from './ns-core.js';
7
9
  import type { NsCtx } from './ns-core.js';
8
10
 
@@ -77,15 +79,49 @@ export function xVal(e: Edge, v: Node, dir: number): number {
77
79
  return d < 0 ? -rv : rv;
78
80
  }
79
81
 
82
+ const INT_MAX = 2147483647;
83
+ const INT_MIN = -2147483648;
84
+
85
+ /**
86
+ * `sadd_overflow` as x_cutval uses it: add in int32 range, report overflow.
87
+ * @see lib/common/ns.c:x_cutval
88
+ */
89
+ function sumOverflows(sum: number, add: number): boolean {
90
+ const s = sum + add;
91
+ return s > INT_MAX || s < INT_MIN;
92
+ }
93
+
94
+ /**
95
+ * C `agerrorf` + `graphviz_exit(EXIT_FAILURE)`: the stderr line verbatim,
96
+ * then the structured throw.
97
+ * @see lib/common/ns.c:x_cutval
98
+ */
99
+ function cutvalOverflow(v: Node): RenderError {
100
+ agerrorf('overflow when computing edge weight sum');
101
+ return new RenderError(
102
+ `Edge weights at node "${v.name}" sum past the 32-bit integer limit; lower the weight attribute values.`,
103
+ 'RENDER_ERROR',
104
+ { context: { node: v.name } },
105
+ );
106
+ }
107
+
108
+ /** Add one edge list's x_val terms to `sum`, in C order. @see x_cutval */
109
+ function sumXVals(list: EdgeList | undefined, v: Node, dir: number, sum: number): number {
110
+ if (!list) return sum;
111
+ for (let i = 0; i < list.size; i++) {
112
+ const add = xVal(list.list[i], v, dir);
113
+ if (sumOverflows(sum, add)) throw cutvalOverflow(v);
114
+ sum += add;
115
+ }
116
+ return sum;
117
+ }
118
+
80
119
  /** @see lib/common/ns.c:x_cutval */
81
120
  export function xCutval(f: Edge): void {
82
121
  const v = f.tail.info.par === f ? f.tail : f.head;
83
122
  const dir = f.tail.info.par === f ? 1 : -1;
84
- let sum = 0;
85
- const out = v.info.out;
86
- if (out) for (let i = 0; i < out.size; i++) sum += xVal(out.list[i], v, dir);
87
- const inp = v.info.in;
88
- if (inp) for (let i = 0; i < inp.size; i++) sum += xVal(inp.list[i], v, dir);
123
+ let sum = sumXVals(v.info.out, v, dir, 0);
124
+ sum = sumXVals(v.info.in, v, dir, sum);
89
125
  f.info.cutvalue = sum;
90
126
  }
91
127
 
@@ -27,33 +27,12 @@ import type { Graph } from '../../model/graph.js';
27
27
  import type { Node } from '../../model/node.js';
28
28
  import { Edge } from '../../model/edge.js';
29
29
  import { agnode, agsubnode } from '../../model/cgraph-ops.js';
30
+ import { mkClustMap } from '../../common/clust-map.js';
30
31
 
31
32
  // ---------------------------------------------------------------------------
32
- // Cluster name map (mkClustMap / fillMap / findCluster)
33
+ // Cluster name lookup (mapc); the map is common/clust-map.ts
33
34
  // ---------------------------------------------------------------------------
34
35
 
35
- /**
36
- * Map every cluster's name to its subgraph, recursing the cluster tree
37
- * (info.clust, built by mkClusters). A duplicate name keeps the first.
38
- * @see lib/common/utils.c:fillMap
39
- */
40
- function fillMap(g: Graph, map: Map<string, Graph>): void {
41
- const n = g.info.n_cluster ?? 0;
42
- for (let c = 0; c < n; c++) {
43
- const cl = g.info.clust![c]!;
44
- const s = cl.name;
45
- if (!map.has(s)) map.set(s, cl);
46
- fillMap(cl, map);
47
- }
48
- }
49
-
50
- /** @see lib/common/utils.c:mkClustMap */
51
- function mkClustMap(g: Graph): Map<string, Graph> {
52
- const map = new Map<string, Graph>();
53
- fillMap(g, map);
54
- return map;
55
- }
56
-
57
36
  /**
58
37
  * A node maps to a cluster iff its name starts with "cluster" AND a cluster of
59
38
  * that exact name exists. @see lib/common/utils.c:mapc
@@ -193,3 +193,15 @@ export function agGraphAttr(g: Graph, name: string): string | undefined {
193
193
  if (own !== undefined) return own;
194
194
  return g.root.declaredGraphAttrs.has(name) ? '' : undefined;
195
195
  }
196
+
197
+ /**
198
+ * The graph attributes of `g` that have a local dict symbol: `attrs` without
199
+ * the keys the builder seeded from the parent. Writers that copy a subgraph's
200
+ * own attributes use it, as `write_dict` prints local symbols only.
201
+ * @see lib/cgraph/attr.c:aglocaldictsym
202
+ */
203
+ export function localGraphAttrs(g: Graph): Map<string, string> {
204
+ const local = new Map(g.attrs);
205
+ for (const key of g.seededAttrs ?? []) local.delete(key);
206
+ return local;
207
+ }
@@ -108,8 +108,8 @@ export class Graph {
108
108
 
109
109
  /**
110
110
  * Keys in `attrs` the BUILDER seeded from `graphDefaultsSnapshot` rather than
111
- * the input declaring them in this scope (see builder.ts GRAPH_LABEL_INHERIT_
112
- * KEYS). cgraph distinguishes these structurally — a seeded value is inherited
111
+ * the input declaring them in this scope: every key of the snapshot the
112
+ * scope did not set (agmakeattrs). cgraph distinguishes these structurally — a seeded value is inherited
113
113
  * through the dict view and has no local symbol, while a local declaration
114
114
  * creates one even when its value equals the inherited value — so the
115
115
  * serializer needs the provenance to decide what write_dict would print.
@@ -118,6 +118,17 @@ export class Graph {
118
118
  */
119
119
  seededAttrs?: Set<string>;
120
120
 
121
+ /**
122
+ * Graph-attribute keys this subgraph holds a local symbol for without having
123
+ * declared them: `unviewsubgraphsattr` gives each direct subgraph one, with
124
+ * its current value, when the parent updates its own symbol. The value is
125
+ * the `attrs` entry (absent for the empty default). A subgraph opened in
126
+ * this one later copies that value, not a nearer ancestor's newer one.
127
+ * Undefined on the root and on a subgraph that was never pinned.
128
+ * @see lib/cgraph/attr.c:232 unviewsubgraphsattr
129
+ */
130
+ pinnedAttrs?: Set<string>;
131
+
121
132
  /**
122
133
  * Graph-attribute keys whose declaration in THIS scope was the first anywhere
123
134
  * in the graph. cgraph's `setattr` splits on exactly this: a key with no