@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/render/map.ts CHANGED
@@ -27,14 +27,13 @@ import type { RendererPlugin } from '../gvc/context.js';
27
27
  import type { ObjState, RenderJob } from '../gvc/job.js';
28
28
  import { MapShape, EMIT_CLUSTERS_LAST } from '../gvc/job.js';
29
29
  import {
30
- type MapCtx,
31
- computeNodeUrlMap,
32
- computeGraphUrlMap,
33
- computeClusterUrlMap,
34
- computeLabelRectMap,
35
- computeEdgeSplineMaps,
30
+ type MapCtx, computeNodeUrlMap, computeGraphUrlMap, computeClusterUrlMap,
31
+ computeLabelRectMap, computeEdgeSplineMaps,
36
32
  } from '../gvc/anchor.js';
37
- import { initJobViewportZoom, parseDrawingSize } from '../gvc/viewport.js';
33
+ import { printfFixed0 } from '../util/printf-round.js';
34
+ import {
35
+ initJobViewportZoom, parseDrawingSize, setupPageBox, setupPageClip, setupPageSize,
36
+ } from '../gvc/viewport.js';
38
37
  import { escapeXml } from './svg-helpers.js';
39
38
  import { escapeXmlTitle } from './xml-escape.js';
40
39
  import { maxOutputline } from './dot/agwrite-linelength.js';
@@ -466,23 +465,22 @@ export function cmapxShape(shape: MapShape): string {
466
465
  return 'poly';
467
466
  }
468
467
 
469
- /** CMAPX coords for a rectangle (UL→LR in Y-down space). */
468
+ /** CMAPX coords for a rectangle (UL→LR in Y-down space), each printed "%.0f"
469
+ * (`-0` at the page top, ties to even). @see gvrender_core_map.c:map_output_shape */
470
470
  export function cmapxCoordsRect(pts: Point[]): string {
471
- return String(Math.round(pts[0]!.x)) + ',' + String(Math.round(pts[1]!.y))
472
- + ',' + String(Math.round(pts[1]!.x)) + ',' + String(Math.round(pts[0]!.y));
471
+ return printfFixed0(pts[0]!.x) + ',' + printfFixed0(pts[1]!.y)
472
+ + ',' + printfFixed0(pts[1]!.x) + ',' + printfFixed0(pts[0]!.y);
473
473
  }
474
474
 
475
- /** CMAPX coords for a circle: `cx,cy,r`. */
475
+ /** CMAPX coords for a circle: `cx,cy,r`, "%.0f". @see map_output_shape */
476
476
  export function cmapxCoordsCircle(pts: Point[]): string {
477
- const cx = Math.round(pts[0]!.x);
478
- const cy = Math.round(pts[0]!.y);
479
- const r = Math.round(pts[1]!.x - pts[0]!.x);
480
- return String(cx) + ',' + String(cy) + ',' + String(r);
477
+ return printfFixed0(pts[0]!.x) + ',' + printfFixed0(pts[0]!.y)
478
+ + ',' + printfFixed0(pts[1]!.x - pts[0]!.x);
481
479
  }
482
480
 
483
- /** CMAPX coords for a polygon: `x0,y0,x1,y1,...`. */
481
+ /** CMAPX coords for a polygon: `x0,y0,x1,y1,...`, "%.0f". @see map_output_shape */
484
482
  export function cmapxCoordsPoly(pts: Point[]): string {
485
- return pts.map(p => String(Math.round(p.x)) + ',' + String(Math.round(p.y))).join(',');
483
+ return pts.map(p => printfFixed0(p.x) + ',' + printfFixed0(p.y)).join(',');
486
484
  }
487
485
 
488
486
  /** Dispatch to the correct CMAPX coordinate formatter. */
@@ -513,21 +511,19 @@ export function mapOutputCmapx(
513
511
  out.push(isXml ? '/>\n' : '>\n');
514
512
  }
515
513
 
516
- /** Write one IMAP shape line; skipped when url is empty. */
517
- export function mapOutputImap(
518
- shape: MapShape, pts: Point[], url: string, out: string[],
519
- ): void {
514
+ /** Write one IMAP shape line ("%.0f" coordinates); skipped when url is empty.
515
+ * @see plugin/core/gvrender_core_map.c:map_output_shape (FORMAT_IMAP) */
516
+ export function mapOutputImap(shape: MapShape, pts: Point[], url: string, out: string[]): void {
520
517
  if (!url) return;
521
518
  if (shape === MapShape.Rectangle) {
522
519
  out.push('rect ' + url + ' '
523
- + Math.round(pts[0]!.x) + ',' + Math.round(pts[1]!.y)
524
- + ' ' + Math.round(pts[1]!.x) + ',' + Math.round(pts[0]!.y) + '\n');
520
+ + printfFixed0(pts[0]!.x) + ',' + printfFixed0(pts[1]!.y)
521
+ + ' ' + printfFixed0(pts[1]!.x) + ',' + printfFixed0(pts[0]!.y) + '\n');
525
522
  } else if (shape === MapShape.Circle) {
526
- const r = Math.round(pts[1]!.x - pts[0]!.x);
527
- out.push('circle ' + url + ' '
528
- + Math.round(pts[0]!.x) + ',' + Math.round(pts[0]!.y) + ',' + String(r) + '\n');
523
+ out.push('circle ' + url + ' ' + printfFixed0(pts[0]!.x) + ',' + printfFixed0(pts[0]!.y)
524
+ + ',' + printfFixed0(pts[1]!.x - pts[0]!.x) + '\n');
529
525
  } else {
530
- const pairs = pts.map(p => Math.round(p.x) + ',' + Math.round(p.y)).join(' ');
526
+ const pairs = pts.map(p => printfFixed0(p.x) + ',' + printfFixed0(p.y)).join(' ');
531
527
  out.push('poly ' + url + ' ' + pairs + '\n');
532
528
  }
533
529
  }
@@ -606,22 +602,26 @@ export function graphMapUrl(g: Graph): string | null {
606
602
  return raw === undefined ? null : substObjAnchor(raw, g);
607
603
  }
608
604
 
609
- /** Build the imagemap coordinate context. The map device's default dpi is 96
610
- * (dpi/resolution attrs override); zoom fits the drawing into `size=` exactly
611
- * as the SVG path (initJobViewportZoom). @see gvrender_core_map.c device_features_map */
605
+ /** Build the imagemap coordinate context: setup_page's pageBox and translation
606
+ * (non-rotated, GVRENDER_Y_GOES_DOWN; canvasBox.LL is the graph `margin=`, the
607
+ * map device having no default page size to centre in) and emit_graph's
608
+ * devscale, negated on y. The map device's default dpi is 96 (dpi/resolution
609
+ * attrs override); zoom fits the drawing into `size=` (initJobViewportZoom).
610
+ * @see lib/common/emit.c:setup_page (1515-1555) ; emit_graph (3631-3633) */
612
611
  export function buildMapCtx(g: Graph, job: RenderJob, mapPolygon: boolean): MapCtx {
613
612
  const drawingDpi = lateDouble(g.attrs.get('dpi'), lateDouble(g.attrs.get('resolution'), 0, 0), 0);
614
- const mapDpi = drawingDpi > 0 ? drawingDpi : 96;
615
- const devscale = mapDpi / POINTS_PER_INCH;
616
- const z = initJobViewportZoom(job.bb, parseDrawingSize(g.attrs.get('size')), job.pad);
617
- // The graph `margin=` enters device space as `margin * dpi/72` (independent
618
- // of zoom), added after the scaled transform. @see emit.c:setup_page.
619
- const marginOff = { x: job.margin.x * devscale, y: job.margin.y * devscale };
620
- return { bb: job.bb, pad: job.pad, scale: z * devscale, marginOff, mapPolygon };
613
+ const devscale = (drawingDpi > 0 ? drawingDpi : 96) / POINTS_PER_INCH;
614
+ const size = parseDrawingSize(g.attrs.get('size'));
615
+ const z = initJobViewportZoom(job.bb, size, job.pad);
616
+ const clip = setupPageClip(job.bb, size, job.pad, z);
617
+ const pageBox = setupPageBox(setupPageSize(job.bb, size, job.pad, z), job.pad);
618
+ const translation = { x: -clip.ll.x + job.margin.x / z, y: -clip.ur.y - job.margin.y / z };
619
+ return { pageBox, translation, scale: { x: z * devscale, y: z * -devscale }, mapPolygon };
621
620
  }
622
621
 
623
- /** Head/tail/center label anchor bundle. */
624
- interface EdgeLabelAnchor { url: string | null; tooltip: string | null; target: string | null; explicit: boolean; }
622
+ /** Head/tail/center label anchor bundle; `suffix` is emit_edge_label's `type` (from lkind). */
623
+ interface EdgeLabelAnchor { url: string | null; tooltip: string | null; target: string | null; explicit: boolean;
624
+ suffix: 'label' | 'headlabel' | 'taillabel'; }
625
625
 
626
626
  /** Coalesce nullable anchor fields to '' (gvrender_begin_anchor passes ""). */
627
627
  function anchorOf(url: string | null, tooltip: string | null, target: string | null, id: string | null): AnchorCtx {
@@ -678,13 +678,6 @@ abstract class MapRendererBase implements RendererPlugin {
678
678
  if (this.mapCtx !== null && job.obj !== null) computeNodeUrlMap(n, job.obj, this.mapCtx);
679
679
  }
680
680
 
681
- beginCluster(sg: Graph, job: RenderJob): void {
682
- const bb = sg.info.bb;
683
- if (this.mapCtx !== null && job.obj !== null && bb !== undefined) {
684
- computeClusterUrlMap(bb, job.obj, this.mapCtx);
685
- }
686
- }
687
-
688
681
  endEdge(e: Edge, job: RenderJob): void {
689
682
  if (this.mapCtx !== null && job.obj !== null) this.emitEdge(e, job.obj, job);
690
683
  }
@@ -732,26 +725,24 @@ abstract class MapRendererBase implements RendererPlugin {
732
725
  this.emitShape(MapShape.Polygon, poly, anchor, job);
733
726
  }
734
727
  }
735
- const centerA: EdgeLabelAnchor = {
736
- url: obj.labelUrl, tooltip: obj.labelTooltip, target: obj.labelTarget, explicit: obj.explicitLabelTooltip,
737
- };
728
+ const centerA: EdgeLabelAnchor = { url: obj.labelUrl, tooltip: obj.labelTooltip,
729
+ target: obj.labelTarget, explicit: obj.explicitLabelTooltip, suffix: 'label' };
738
730
  this.emitEdgeLabel(e.info.label, centerA, obj, job);
739
731
  this.emitEdgeLabel(e.info.xlabel, centerA, obj, job);
740
- this.emitEdgeLabel(e.info.head_label,
741
- { url: obj.headUrl, tooltip: obj.headTooltip, target: obj.headTarget, explicit: obj.explicitHeadTooltip },
742
- obj, job);
743
- this.emitEdgeLabel(e.info.tail_label,
744
- { url: obj.tailUrl, tooltip: obj.tailTooltip, target: obj.tailTarget, explicit: obj.explicitTailTooltip },
745
- obj, job);
732
+ this.emitEdgeLabel(e.info.head_label, { url: obj.headUrl, tooltip: obj.headTooltip,
733
+ target: obj.headTarget, explicit: obj.explicitHeadTooltip, suffix: 'headlabel' }, obj, job);
734
+ this.emitEdgeLabel(e.info.tail_label, { url: obj.tailUrl, tooltip: obj.tailTooltip,
735
+ target: obj.tailTarget, explicit: obj.explicitTailTooltip, suffix: 'taillabel' }, obj, job);
746
736
  }
747
737
 
748
- /** One edge label hot spot (map_label rect), if placed and url/explicit-tip.
749
- * @see lib/common/emit.c:emit_edge_label */
738
+ /** One edge label hot spot (map_label rect), if placed and url/explicit-tip; its id is
739
+ * `<obj.id>-<suffix>` (C: `if (id)`, else NULL). @see lib/common/emit.c:emit_edge_label (2851-2876) */
750
740
  private emitEdgeLabel(lab: TextlabelT | undefined, la: EdgeLabelAnchor, obj: ObjState, job: RenderJob): void {
751
741
  const placed = lab !== undefined && lab.set && this.mapCtx !== null;
752
742
  if (!placed || (la.url === null && !la.explicit)) return;
753
743
  computeLabelRectMap(lab, obj, this.mapCtx!);
754
- this.emitShape(MapShape.Rectangle, obj.urlMapPts, anchorOf(la.url, la.tooltip, la.target, obj.id), job);
744
+ const id = obj.id === null ? null : `${obj.id}-${la.suffix}`;
745
+ this.emitShape(MapShape.Rectangle, obj.urlMapPts, anchorOf(la.url, la.tooltip, la.target, id), job);
755
746
  }
756
747
  }
757
748
 
@@ -11,10 +11,10 @@ import { escapeXmlTitle } from './svg-helpers.js';
11
11
  import { svgClusterId, svgClusterClass } from './svg-id.js';
12
12
 
13
13
  export function svgBeginCluster(sg: Graph, job: RenderJob): void {
14
- // Id from getObjId (DOT `id` attr / gid prefix / clust<seq>); layer prefix
15
- // only (no per-object suffix for graph/cluster ids). DOT `class` is appended.
14
+ // Id from getObjId (layer prefix / DOT `id` attr / gid prefix / clust<seq>);
15
+ // no per-object suffix for graph/cluster ids. DOT `class` is appended.
16
16
  // @see lib/common/emit.c:getObjId; plugin/core/gvrender_core_svg.c:svg_print_id_class
17
- job.write('<g id="' + escapeXmlTitle(job.idLayerPrefix() + svgClusterId(sg, job))
17
+ job.write('<g id="' + escapeXmlTitle(svgClusterId(sg, job))
18
18
  + '" ' + svgClusterClass(sg) + '>\n');
19
19
  job.write('<title>' + escapeXmlTitle(sg.name) + '</title>\n');
20
20
  }
@@ -338,8 +338,6 @@ export function svgBeginGraph(g: Graph, job: RenderJob): void {
338
338
  // C reads GD_fontnames(job->gvc->g) at each span; the port carries it on the
339
339
  // job, set here alongside the other root-graph-derived fields.
340
340
  job.fontnames = g.info.fontnames ?? FontnameKind.NativeFonts;
341
- // GD_drawing(root)->id — used as the `<gid>_` prefix for non-root object ids.
342
- job.drawingId = g.attrs.get('id') ?? '';
343
341
  job.write(SVG_XML_DECL);
344
342
  // C svg_begin_job emits the stylesheet PI (raw href) between the XML
345
343
  // declaration and the DOCTYPE when the graph has a `stylesheet` attribute.
@@ -409,7 +407,7 @@ export function svgBeginPage(g: Graph, job: RenderJob): void {
409
407
  : -clip.ll.x + job.margin.x / z;
410
408
  const ty = clip.ur.y + job.margin.y / z;
411
409
  emitGraphGroupOpen(
412
- job.idLayerPrefix() + svgGraphId(g, job),
410
+ svgGraphId(g, job),
413
411
  svgGraphClass(g),
414
412
  tx, ty, job,
415
413
  );
@@ -180,13 +180,13 @@ export {
180
180
  // ---------------------------------------------------------------------------
181
181
 
182
182
  export function svgBeginNode(n: Node, job: RenderJob): void {
183
- // Id from getObjId (DOT `id` attr / gid prefix / node<seq>); layer
184
- // prefix/suffix are empty unless rendering layers (layerNum>1). The DOT
183
+ // Id from getObjId (layer prefix / DOT `id` attr / gid prefix / node<seq>);
184
+ // the prefix and the suffix are empty unless layerNum>1. The DOT
185
185
  // `class` attr is appended to the SVG class string (svg_print_id_class).
186
186
  // @see lib/common/emit.c:getObjId; plugin/core/gvrender_core_svg.c:svg_begin_node
187
187
  // C's svg_print_id_class prints the assembled id through gvputs_xml, so the
188
188
  // DOT `id` attr (and user-controlled layer names) are XML-escaped at emission.
189
- job.write('<g id="' + escapeXmlTitle(job.idLayerPrefix() + svgNodeId(n, job) + job.idLayerSuffix())
189
+ job.write('<g id="' + escapeXmlTitle(svgNodeId(n, job) + job.idLayerSuffix())
190
190
  + '" ' + svgNodeClass(n) + '>\n');
191
191
  job.write('<title>' + escapeXmlTitle(n.name) + '</title>\n');
192
192
  }
@@ -223,9 +223,9 @@ export function svgBeginEdge(e: Edge, job: RenderJob): void {
223
223
  const hp = e.info.head_port?.name;
224
224
  const raw = e.tail.name + (tp ? ':' + tp : '')
225
225
  + sep + e.head.name + (hp ? ':' + hp : '');
226
- // Edges get the layer prefix only — no per-object suffix (svg_begin_edge
227
- // passes idx=NULL, unlike svg_begin_node). DOT `id`/`class` attrs apply.
228
- job.write('<g id="' + escapeXmlTitle(job.idLayerPrefix() + svgEdgeId(e, job))
226
+ // Edges get the layer prefix with the id (objId) — no per-object suffix
227
+ // (svg_begin_edge passes idx=NULL, unlike svg_begin_node). `class` applies.
228
+ job.write('<g id="' + escapeXmlTitle(svgEdgeId(e, job))
229
229
  + '" ' + svgEdgeClass(e) + '>\n');
230
230
  job.write('<title>' + escapeEdgeTitle(raw) + '</title>\n');
231
231
  }
@@ -2,11 +2,15 @@
2
2
  //
3
3
  // SVG object id + class resolution, faithful to C's getObjId
4
4
  // (lib/common/emit.c:209) and svg_print_id_class
5
- // (plugin/core/gvrender_core_svg.c). A non-empty DOT `id` attribute wins
6
- // verbatim; otherwise the generated `<pfx><seq>` id is used, with a `<gid>_`
7
- // prefix on non-root objects when the root graph carries an `id` attribute
8
- // (job.drawingId). The DOT `class` attribute, when present, is appended to the
9
- // SVG class string. Attribute reads use C's `agget` semantics (defaults
5
+ // (plugin/core/gvrender_core_svg.c). Every id starts with `<layer>_` on a
6
+ // layer after the first (layerPagePrefix, in job.objId). Then a non-empty DOT
7
+ // `id` attribute wins; otherwise the generated `<pfx><seq>` id is used, with a
8
+ // `<gid>_` prefix on non-root objects when the root graph carries an `id`
9
+ // attribute
10
+ // (job.drawingId, from drawingIdOf). That prefix belongs to the graph, not to
11
+ // the svg plugin: every renderer that prints an object id (svg, the imagemaps)
12
+ // gets the same one. The DOT `class` attribute, when present, is appended to
13
+ // the SVG class string. Attribute reads use C's `agget` semantics (defaults
10
14
  // inherited) via nodeAttr / clusterAttr.
11
15
 
12
16
  import type { Node } from '../model/node.js';
@@ -16,6 +20,20 @@ import type { RenderJob } from '../gvc/job.js';
16
20
  import { escapeXmlTitle } from './xml-escape.js';
17
21
  import { nodeAttr } from '../common/poly-init.js';
18
22
  import { clusterAttr } from '../gvc/device-cluster.js';
23
+ import { substObjAnchor } from '../common/subst.js';
24
+
25
+ /**
26
+ * C's `GD_drawing(root)->id`: the root graph's `id` attribute with its `\`
27
+ * escapes substituted (`\G` and the like), or '' when it is unset or empty.
28
+ * graph_init sets it once for every renderer; getObjId prefixes each non-root
29
+ * object id with it.
30
+ * @see lib/common/input.c:graph_init (786-788)
31
+ * @see lib/common/emit.c:getObjId
32
+ */
33
+ export function drawingIdOf(g: Graph): string {
34
+ const id = g.attrs.get('id');
35
+ return id !== undefined && id.length > 0 ? substObjAnchor(id, g) : '';
36
+ }
19
37
 
20
38
  /**
21
39
  * `class="<kind>"`, with the DOT `class` attribute appended (` <class>`,
@@ -118,6 +118,17 @@ export function printfFixed(v: number, decimals: number): string {
118
118
  return sign + intPart + '.' + fracPart;
119
119
  }
120
120
 
121
+ /**
122
+ * Format `v` as C's `%.0f` would: `printfFixed(v, 0)`, except that an exact
123
+ * negative zero keeps its sign and prints `-0`, as printf does. A negative
124
+ * value that rounds to zero already prints `-0` in both.
125
+ *
126
+ * @see plugin/core/gvrender_core_map.c:map_output_shape ("%.0f")
127
+ */
128
+ export function printfFixed0(v: number): string {
129
+ return Object.is(v, -0) ? '-0' : printfFixed(v, 0);
130
+ }
131
+
121
132
  /**
122
133
  * Format `v` with `sig` significant digits, using the same notational
123
134
  * contract as `Number.prototype.toPrecision(sig)` — fixed form when the