@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
@@ -28,6 +28,14 @@
28
28
  * @see lib/gvc/gvdevice.c:gvprintdouble
29
29
  */
30
30
  export declare function printfFixed(v: number, decimals: number): string;
31
+ /**
32
+ * Format `v` as C's `%.0f` would: `printfFixed(v, 0)`, except that an exact
33
+ * negative zero keeps its sign and prints `-0`, as printf does. A negative
34
+ * value that rounds to zero already prints `-0` in both.
35
+ *
36
+ * @see plugin/core/gvrender_core_map.c:map_output_shape ("%.0f")
37
+ */
38
+ export declare function printfFixed0(v: number): string;
31
39
  /**
32
40
  * Format `v` with `sig` significant digits, using the same notational
33
41
  * contract as `Number.prototype.toPrecision(sig)` — fixed form when the
@@ -1 +1 @@
1
- {"version":3,"file":"printf-round.d.ts","sourceRoot":"","sources":["../../src/util/printf-round.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAuEH;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAkB/D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAmCxD"}
1
+ {"version":3,"file":"printf-round.d.ts","sourceRoot":"","sources":["../../src/util/printf-round.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAuEH;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAkB/D;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAmCxD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowvah/dot-engine",
3
- "version": "2.12.3",
3
+ "version": "2.13.0",
4
4
  "description": "Faithful TypeScript port of Graphviz (dot, neato, fdp, sfdp, circo, twopi, osage, patchwork) — no C: no native binary, no WASM. Oracle-verified against the C implementation, runs in the browser.",
5
5
  "keywords": [
6
6
  "graphviz",
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: EPL-2.0
2
+
3
+ /**
4
+ * The flat cluster-name map dot's compound edges and fdp's cluster edges look
5
+ * cluster endpoints up in. cgraph keeps subgraph names per parent, so two
6
+ * clusters in the tree can share a name; C keeps the first and warns.
7
+ *
8
+ * @see lib/common/utils.c:fillMap, mkClustMap (1498-1525)
9
+ * @see lib/cgraph/id.c:agnameof
10
+ */
11
+
12
+ import type { Graph } from '../model/graph.js';
13
+ import { LOCALNAMEPREFIX } from '../model/cgraph-ops.js';
14
+ import { agwarningf } from './agerr.js';
15
+
16
+ /**
17
+ * fillMap's state across the recursion. `anonKeyed` stands for C's key alias:
18
+ * agnameof returns one static buffer for every anonymous graph, and fillMap
19
+ * stores that pointer as the key, so once an anonymous cluster is in the map,
20
+ * every later anonymous cluster's dtmatch finds it. A cluster is anonymous
21
+ * here when its name starts with LOCALNAMEPREFIX, the test the dot writer
22
+ * uses; a source name `"%x"` is stored by cgraph and not aliased in C (#194).
23
+ * @see lib/cgraph/id.c:agnameof (static buf, 161-164)
24
+ */
25
+ interface ClustMapState { map: Map<string, Graph>; anonKeyed: boolean }
26
+
27
+ /**
28
+ * Map every cluster's name to its subgraph, preorder over the cluster tree
29
+ * (info.clust, 0-based here for C's GD_clust[1..n]). A second cluster with a
30
+ * name already in the map is ignored, with a warning.
31
+ * @see lib/common/utils.c:fillMap
32
+ */
33
+ function fillMap(g: Graph, st: ClustMapState): void {
34
+ const n = g.info.n_cluster ?? 0;
35
+ for (let c = 0; c < n; c++) {
36
+ const cl = g.info.clust![c]!;
37
+ const s = cl.name;
38
+ const anon = s.startsWith(LOCALNAMEPREFIX);
39
+ if (st.map.has(s) || (anon && st.anonKeyed)) {
40
+ agwarningf(`Two clusters named ${s} - the second will be ignored`);
41
+ } else {
42
+ st.map.set(s, cl);
43
+ if (anon) st.anonKeyed = true;
44
+ }
45
+ fillMap(cl, st);
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Build the name-to-cluster map for g's cluster tree. Assumes g's cluster
51
+ * tree (info.n_cluster, info.clust) is already built.
52
+ * @see lib/common/utils.c:mkClustMap
53
+ */
54
+ export function mkClustMap(g: Graph): Map<string, Graph> {
55
+ const st: ClustMapState = { map: new Map<string, Graph>(), anonKeyed: false };
56
+ fillMap(g, st);
57
+ return st.map;
58
+ }
@@ -4,7 +4,8 @@
4
4
  * HTML label rendering: emits a positioned HTML label (cell borders,
5
5
  * text runs, table border) through the renderer plugin.
6
6
  *
7
- * emitHtmlLabel is the port of emit_html_tbl.
7
+ * emitHtmlLabel is the port of emit_html_label (its obj push and pop).
8
+ * emitHtmlTbl is the port of emit_html_tbl.
8
9
  * emitHtmlCell is the port of emit_html_cell.
9
10
  *
10
11
  * Decoration (bgcolor fill, per-side borders, anchors, rules) fires
@@ -12,6 +13,9 @@
12
13
  * PlacedCell / PlacedHtml — mirroring C's conditional structure.
13
14
  * Undecorated tables emit identically to before T6.
14
15
  *
16
+ * @see lib/common/htmltable.c:emit_html_label
17
+ * @see lib/common/htmltable.c:allocObj
18
+ * @see lib/common/htmltable.c:freeObj
15
19
  * @see lib/common/htmltable.c:emit_html_tbl
16
20
  * @see lib/common/htmltable.c:emit_html_cell
17
21
  * @see lib/common/htmltable.c:emit_htextspans
@@ -23,8 +27,8 @@
23
27
  */
24
28
 
25
29
  import type { Point, Box } from '../model/geom.js';
26
- import type { RenderJob } from '../gvc/job.js';
27
- import { EMIT_CLUSTERS_LAST } from '../gvc/job.js';
30
+ import type { RenderJob, ObjState } from '../gvc/job.js';
31
+ import { EMIT_CLUSTERS_LAST, createObjState } from '../gvc/job.js';
28
32
  import type { RendererPlugin } from '../gvc/context.js';
29
33
  import type { TextSpan } from './emit-types.js';
30
34
  import type { PlacedHtml, PlacedCell, PlacedLine, PlacedImage } from './htmltable-pos.js';
@@ -244,7 +248,7 @@ export function emitHtmlCell(
244
248
  if (cell.nested !== undefined) {
245
249
  // C: emit_html_cell recurses emit_html_tbl for an HTML_TBL child.
246
250
  // @see lib/common/htmltable.c:emit_html_cell
247
- emitHtmlLabel(cell.nested, pos, renderer, job);
251
+ emitHtmlTbl(cell.nested, pos, renderer, job);
248
252
  } else if (cell.image !== undefined) {
249
253
  emitHtmlImg(cell.image, pos, renderer, job);
250
254
  } else {
@@ -254,7 +258,7 @@ export function emitHtmlCell(
254
258
  }
255
259
 
256
260
  // ---------------------------------------------------------------------------
257
- // emitHtmlLabel — port of emit_html_tbl
261
+ // emitHtmlTbl — port of emit_html_tbl
258
262
  // ---------------------------------------------------------------------------
259
263
 
260
264
  /** Emit vrule/hrule rules for all ruled cells. */
@@ -276,14 +280,15 @@ function emitTableRules(
276
280
  }
277
281
 
278
282
  /**
279
- * Emit a fully positioned HTML table label.
283
+ * Emit a fully positioned HTML table: a label's top-level table, or one
284
+ * nested in a cell.
280
285
  *
281
286
  * Draw order mirrors C emit_html_tbl:
282
287
  * 1. Open anchor 2. Table BGCOLOR 3. Rules 4. Border 5. Cells 6. Close anchor
283
288
  *
284
289
  * @see lib/common/htmltable.c:emit_html_tbl
285
290
  */
286
- export function emitHtmlLabel(
291
+ function emitHtmlTbl(
287
292
  placed: PlacedHtml,
288
293
  pos: Point,
289
294
  renderer: RendererPlugin,
@@ -322,3 +327,78 @@ export function emitHtmlLabel(
322
327
  endHtmlAnchor(renderer, job);
323
328
  }
324
329
  }
330
+
331
+ // ---------------------------------------------------------------------------
332
+ // emitHtmlLabel — port of emit_html_label
333
+ // ---------------------------------------------------------------------------
334
+
335
+ /**
336
+ * Push a new obj on the stack, used in common by all the label's html
337
+ * elements with anchors. It takes the paint fields push_obj_state defaults to
338
+ * the parent's, and inherits the parent's type, emit_state and object, and its
339
+ * url, tooltip, target and explicit_tooltip. Not its id, label or map points:
340
+ * a cell's emit_map_rect writes this obj's points, never the owner's.
341
+ * `parent` is job.obj (C reads it back as obj->parent), narrowed by the caller.
342
+ *
343
+ * @see lib/common/htmltable.c:allocObj
344
+ * @see lib/common/emit.c:push_obj_state
345
+ */
346
+ function allocObj(job: RenderJob, parent: ObjState): void {
347
+ const obj = createObjState(parent.type);
348
+ obj.parent = parent;
349
+ obj.penColor = parent.penColor;
350
+ obj.fillColor = parent.fillColor;
351
+ obj.pen = parent.pen;
352
+ obj.fill = parent.fill;
353
+ obj.penWidth = parent.penWidth;
354
+ obj.gradientAngle = parent.gradientAngle;
355
+ obj.stopColor = parent.stopColor;
356
+ obj.emitState = parent.emitState;
357
+ obj.graphObj = parent.graphObj;
358
+ obj.url = parent.url;
359
+ obj.tooltip = parent.tooltip;
360
+ obj.target = parent.target;
361
+ obj.explicitTooltip = parent.explicitTooltip;
362
+ job.pushObj(obj);
363
+ }
364
+
365
+ /**
366
+ * Pop the label's obj. C clears url, tooltip, target and id first so that
367
+ * pop_obj_state does not free the parent's strings; nothing to do here.
368
+ *
369
+ * @see lib/common/htmltable.c:freeObj
370
+ */
371
+ function freeObj(job: RenderJob): void {
372
+ job.popObj();
373
+ }
374
+
375
+ /**
376
+ * Emit a fully positioned HTML table label in its own obj state, so nothing
377
+ * the label writes on the obj (a cell's map rect) reaches the label's owner.
378
+ * The obj stack is as deep on return as on entry, even when the label throws.
379
+ *
380
+ * @see lib/common/htmltable.c:emit_html_label
381
+ */
382
+ export function emitHtmlLabel(
383
+ placed: PlacedHtml,
384
+ pos: Point,
385
+ renderer: RendererPlugin,
386
+ job: RenderJob,
387
+ ): void {
388
+ const parent = job.obj;
389
+ if (parent === null) {
390
+ // C always has an enclosing obj here (root, cluster, node or edge). The
391
+ // port's label emitters also run on a bare job with an empty obj stack
392
+ // (withLabelEmitState and gvrenderTextspan take `job.obj === null`; the
393
+ // renderOneLabel and renderClusterLabel cases of device.test.ts): with no
394
+ // owner there is nothing to shield, so emit without a push.
395
+ emitHtmlTbl(placed, pos, renderer, job);
396
+ return;
397
+ }
398
+ allocObj(job, parent);
399
+ try {
400
+ emitHtmlTbl(placed, pos, renderer, job);
401
+ } finally {
402
+ freeObj(job);
403
+ }
404
+ }
@@ -1,15 +1,17 @@
1
1
  // SPDX-License-Identifier: EPL-2.0
2
2
  //
3
3
  // Output-layer support, ported from lib/common/emit.c (parse_layers,
4
- // layer_index, selectedLayer, node_in_layer, edge_in_layer). A graph with a
5
- // `layers="a:b:c"` attribute renders once per layer; each node/edge appears
6
- // only in the layers it belongs to (its `layer` attribute, or all layers when
7
- // it has none). All of this is inert unless numLayers > 1.
4
+ // layer_index, selectedLayer, node_in_layer, edge_in_layer, clust_in_layer).
5
+ // A graph with a `layers="a:b:c"` attribute renders once per layer; each
6
+ // node/edge/cluster appears only in the layers it belongs to (its `layer`
7
+ // attribute, or all layers when it has none; a cluster with none follows its
8
+ // nodes). All of this is inert unless numLayers > 1.
8
9
 
9
10
  import { agwarningf } from './agerr.js';
10
11
  import type { Graph } from '../model/graph.js';
11
12
  import type { Node } from '../model/node.js';
12
13
  import type { Edge } from '../model/edge.js';
14
+ import { graphAttrInherited } from './attr-get.js';
13
15
 
14
16
  /** @see lib/common/const.h:82-83 */
15
17
  const DEFAULT_LAYERSEP = ':\t ';
@@ -121,6 +123,25 @@ export function edgeInLayer(info: LayerInfo, layerNum: number, e: Edge): boolean
121
123
  });
122
124
  }
123
125
 
126
+ /**
127
+ * Whether cluster `sg` is drawn in layer `layerNum`. Its own `layer` decides
128
+ * when it has one (C reads it with `agattr_text(sg, AGRAPH, "layer", 0)`: the
129
+ * cluster's value, or the default it was created under). With none, the
130
+ * cluster is in the layer when any of its nodes is, and `node_in_layer` is
131
+ * handed `sg`, so only the edges of the cluster itself count.
132
+ * @see lib/common/emit.c:1593 clust_in_layer
133
+ */
134
+ export function clustInLayer(info: LayerInfo, layerNum: number, sg: Graph): boolean {
135
+ if (info.numLayers <= 1) return true;
136
+ const pg = graphAttrInherited(sg, 'layer') ?? '';
137
+ if (selectedLayer(info, layerNum, pg)) return true;
138
+ if (pg !== '') return false;
139
+ for (const n of sg.nodes.values()) {
140
+ if (nodeInLayer(info, layerNum, n, sg)) return true;
141
+ }
142
+ return false;
143
+ }
144
+
124
145
  /**
125
146
  * Walk each layer index and keep those `selectedLayer` matches against `p`.
126
147
  * Returns `[n, l1..ln, numLayers + 1]`, or null (after a warning) when nothing
@@ -536,24 +536,16 @@ export function polyGencode(rawJob: unknown, rawNode: unknown): void {
536
536
  if (ctx === null) return;
537
537
  // EMIT_CLUSTERS_LAST (map device): the node's own anchor <area> is emitted
538
538
  // AFTER its shape and label, so an inner HTML cell's area precedes the
539
- // node's. Save the node hot spot first — HTML cells overwrite obj.urlMapPts
540
- // via emit_map_rect — and restore it for the deferred anchor.
539
+ // node's. The hot spot is still emit_begin_node's: the label's cells write
540
+ // their map rects on the label's own obj (allocObj), not on the node's.
541
541
  // @see lib/common/shapes.c:poly_gencode (2936, 3087-3092)
542
542
  const clustersLast = (job.flags & EMIT_CLUSTERS_LAST) !== 0;
543
- const savedShape = job.obj?.urlMapShape;
544
- const savedPts = job.obj?.urlMapPts;
545
543
  let inAnchor = clustersLast ? false : beginNodeAnchor(n, renderer, job);
546
544
  renderPeripheries(ctx.poly, ctx.coord, ctx.filled, ctx.ringCtx);
547
545
  // point_gencode has no usershape branch.
548
546
  if (!isPointNode(n)) renderUsershape(n, ctx);
549
547
  // point_gencode never emits a label (AD-3).
550
548
  if (!isPointNode(n)) renderNodeLabel(n, ctx.coord, renderer, job);
551
- if (clustersLast) {
552
- if (job.obj && savedPts !== undefined && savedShape !== undefined) {
553
- job.obj.urlMapShape = savedShape;
554
- job.obj.urlMapPts = savedPts;
555
- }
556
- inAnchor = beginNodeAnchor(n, renderer, job);
557
- }
549
+ if (clustersLast) inAnchor = beginNodeAnchor(n, renderer, job);
558
550
  if (inAnchor) renderer.endAnchor?.(job);
559
551
  }
@@ -12,6 +12,7 @@
12
12
  import type { Graph } from '../model/graph.js';
13
13
  import type { Point } from '../model/geom.js';
14
14
  import type { TextlabelT } from './types.js';
15
+ import { agwarningf } from './agerr.js';
15
16
  import {
16
17
  EDGE_LABEL, HEAD_LABEL, TAIL_LABEL, NODE_XLABEL, EDGE_XLABEL, mapbool,
17
18
  } from '../layout/dot/rank.js';
@@ -418,17 +419,25 @@ function buildArrays(
418
419
  return { ctx };
419
420
  }
420
421
 
421
- /** Write placed label positions back to the graph; update bb. */
422
- function writeBackLabels(gp: Graph, lbls: XLabelT[], nLbls: number): void {
422
+ /**
423
+ * Write placed label positions back to the graph; update bb. Returns C's
424
+ * `cnt`, the number of labels placed.
425
+ *
426
+ * @see lib/common/postproc.c:addXLabels (569-580)
427
+ */
428
+ function writeBackLabels(gp: Graph, lbls: XLabelT[], nLbls: number): number {
429
+ let cnt = 0;
423
430
  for (let i = 0; i < nLbls; i++) {
424
431
  const xlp = lbls[i];
425
432
  if (xlp.set) {
433
+ cnt++;
426
434
  const lp = xlp.lbl as TextlabelT;
427
435
  lp.set = true;
428
436
  lp.pos = centerPt(xlp);
429
437
  updateBBForLabel(gp, lp);
430
438
  }
431
439
  }
440
+ return cnt;
432
441
  }
433
442
 
434
443
  // ---------------------------------------------------------------------------
@@ -444,7 +453,13 @@ function needsXLabelWork(gp: Graph): boolean {
444
453
  return !!(h & EDGE_LABEL) && !(gp.info.edgeLabelsDone ?? false);
445
454
  }
446
455
 
447
- /** Run one placeLabels pass: count slots, build arrays, place, write back. */
456
+ /**
457
+ * Run one placeLabels pass: count slots, build arrays, place, write back, and
458
+ * warn when a label is left unplaced. Not ported: the `Verbose`
459
+ * `labels positioned` line; the port has no `Verbose`.
460
+ *
461
+ * @see lib/common/postproc.c:addXLabels (569-587)
462
+ */
448
463
  function runPlacement(gp: Graph): void {
449
464
  const hasLabels = gp.info.has_labels ?? 0;
450
465
  const et = edgeType(gp);
@@ -460,7 +475,10 @@ function runPlacement(gp: Graph): void {
460
475
  const force = forceStr === undefined ? true : mapbool(forceStr);
461
476
  const params: LabelParamsT = { bb: { ll: ctx.bb.ll, ur: ctx.bb.ur }, force };
462
477
  placeLabels(ctx.objs, ctx.lbls, params);
463
- writeBackLabels(gp, ctx.lbls, nLbls);
478
+ const cnt = writeBackLabels(gp, ctx.lbls, nLbls);
479
+ if (cnt !== nLbls) {
480
+ agwarningf(`${cnt} out of ${nLbls} exterior labels positioned.`);
481
+ }
464
482
  }
465
483
 
466
484
  /**
package/src/errors.ts CHANGED
@@ -45,6 +45,8 @@ export interface GvError {
45
45
  location?: { line: number; column: number; offset?: number };
46
46
  /** Peggy's discriminated union, passed through unmapped; SYNTAX_* only. */
47
47
  expected?: GvExpectation[];
48
+ /** Named values the message refers to (e.g. `{ node }`); string-valued. */
49
+ context?: Record<string, string>;
48
50
  }
49
51
 
50
52
  /** Result of a result-style render: `svg` XOR `errors` for v1. */
@@ -91,6 +93,11 @@ const SEMANTIC_RENDER_CODES: ReadonlySet<GvErrorCode> = new Set<GvErrorCode>([
91
93
  'UNSUPPORTED_FEATURE',
92
94
  ]);
93
95
 
96
+ /** `ErrorOptions` plus the optional structured {@link GvError.context}. */
97
+ export interface DotEngineErrorOptions extends ErrorOptions {
98
+ context?: Record<string, string>;
99
+ }
100
+
94
101
  /**
95
102
  * Abstract base of every library-originated error: `instanceof
96
103
  * DotEngineError` means dot-engine failed on this input (bad input, a fatal
@@ -101,9 +108,12 @@ export abstract class DotEngineError extends Error implements GvError {
101
108
  abstract readonly type: GvErrorType;
102
109
  abstract readonly code: GvErrorCode;
103
110
  abstract readonly friendlyMessage: string;
111
+ /** `declare`: no own property unless the caller supplied a context. */
112
+ declare context?: Record<string, string>;
104
113
 
105
- constructor(message: string, options?: ErrorOptions) {
114
+ constructor(message: string, options?: DotEngineErrorOptions) {
106
115
  super(message, options);
116
+ if (options?.context !== undefined) this.context = options.context;
107
117
  }
108
118
  }
109
119
 
@@ -113,7 +123,7 @@ export class InternalError extends DotEngineError {
113
123
  readonly code = 'INTERNAL_ERROR';
114
124
  readonly friendlyMessage = friendlyMessageFor('INTERNAL_ERROR');
115
125
 
116
- constructor(message: string, options?: ErrorOptions) {
126
+ constructor(message: string, options?: DotEngineErrorOptions) {
117
127
  super(message, options);
118
128
  this.name = 'InternalError';
119
129
  }
@@ -131,7 +141,7 @@ export class RenderError extends DotEngineError {
131
141
  constructor(
132
142
  message: string,
133
143
  code: GvErrorCode = 'RENDER_ERROR',
134
- options?: ErrorOptions,
144
+ options?: DotEngineErrorOptions,
135
145
  ) {
136
146
  super(message, options);
137
147
  this.name = 'RenderError';
package/src/gvc/anchor.ts CHANGED
@@ -24,6 +24,7 @@ import { bezier } from '../common/bezier.js';
24
24
  import { ptToLine2 } from '../common/splines-geom.js';
25
25
  import type { RendererPlugin } from './context.js';
26
26
  import { MapShape, type ObjState, type RenderJob } from './job.js';
27
+ import { applyScale } from './device-transform.js';
27
28
 
28
29
  /** Non-empty attr value, or undefined. C: `(s = agget(o, k)) && s[0]`. */
29
30
  function attr(attrs: Map<string, string>, key: string): string | undefined {
@@ -234,28 +235,21 @@ const DFLT_SAMPLE = 20;
234
235
  const MAP_HW = 2.0;
235
236
 
236
237
  /**
237
- * Coordinate-transform context for the imagemap device. `bb` is the unpadded
238
- * graph bounding box (GD_bb), `pad` the graph padding, `scale` the device
239
- * scale (`zoom * dpi / 72`). The imagemap device is GVRENDER_Y_GOES_DOWN with
240
- * no GVRENDER_DOES_TRANSFORM, so emit.c runs every map point through
241
- * gvrender_ptf.
238
+ * Coordinate-transform context for the imagemap device. `pageBox` is
239
+ * job->pageBox (the root graph's area). `translation` and `scale` are
240
+ * gvrender_ptf_A's: job->translation, and zoom * devscale with `scale.y`
241
+ * negative, because the map device is GVRENDER_Y_GOES_DOWN.
242
242
  */
243
- export interface MapCtx { bb: Box; pad: Point; scale: number; marginOff: Point; mapPolygon: boolean; }
243
+ export interface MapCtx { pageBox: Box; translation: Point; scale: Point; mapPolygon: boolean; }
244
244
 
245
245
  /**
246
- * gvrender_ptf for the imagemap device (single page, no rotation): the clip box
247
- * is the padded drawing, so translation.x = -(bb.ll.x - pad.x) and
248
- * translation.y = -(bb.ur.y + pad.y); devscale.y is negated for Y_GOES_DOWN.
249
- * setup_page also folds `canvasBox.LL / zoom` (the graph `margin=`) into the
250
- * translation, which after the `* scale` becomes `margin * dpi/72` in device
251
- * units (`marginOff`) — matching svgBeginPage's `+ margin / z` term.
252
- * @see lib/gvc/gvrender.c:gvrender_ptf ; lib/common/emit.c:setup_page (1566-1581)
246
+ * gvrender_ptf_A's no-rotation branch: (p + translation) * scale. The negative
247
+ * y scale makes the page top -0.0, which "%.0f" prints as `-0` (issue #175).
248
+ * buildMapCtx fills the context; rotate=90 is not ported for maps (#247).
249
+ * @see lib/gvc/gvrender.c:gvrender_ptf_A ; lib/common/emit.c:setup_page (1548-1555)
253
250
  */
254
251
  export function mapTransform(p: Point, ctx: MapCtx): Point {
255
- return {
256
- x: (p.x + ctx.pad.x - ctx.bb.ll.x) * ctx.scale + ctx.marginOff.x,
257
- y: (ctx.bb.ur.y + ctx.pad.y - p.y) * ctx.scale + ctx.marginOff.y,
258
- };
252
+ return applyScale(p, ctx.translation.x, ctx.translation.y, ctx.scale.x, ctx.scale.y);
259
253
  }
260
254
 
261
255
  /** isFilled: node style contains "filled". @see lib/common/emit.c:705 */
@@ -388,21 +382,15 @@ export function computeNodeUrlMap(n: Node, obj: ObjState, ctx: MapCtx): void {
388
382
  }
389
383
 
390
384
  /**
391
- * Graph-level (root) hot spot: the padded drawing rectangle (pageBox), matching
392
- * emit_page's map computation for GVRENDER_DOES_MAP_RECTANGLE devices.
393
- * @see lib/common/emit.c:emit_page (3623-3642)
385
+ * Graph-level (root) hot spot: job->pageBox, as emit_page maps it for
386
+ * GVRENDER_DOES_MAP_RECTANGLE devices. The page box starts at -pad (not at
387
+ * bb.LL - pad: fdp graph labels keep a negative bb.LL, and native url.gv fdp
388
+ * emits 0,-33,539,374), and the translation is built from job->clip, so under
389
+ * `size=` the corner can land a few ulps below zero and print `-0`.
390
+ * @see lib/common/emit.c:emit_page (3570-3589)
394
391
  */
395
392
  export function computeGraphUrlMap(obj: ObjState, ctx: MapCtx): void {
396
- // C uses job->clip — the 0-BASED page window ([-pad, (UR-LL)+pad] in graph
397
- // coords), not bb±pad. Identical when bb.LL is the origin, but engines that
398
- // keep a negative bb.LL (fdp graph labels: bb "0,-24.8,...") shift the root
399
- // rect: native url.gv fdp emits 0,-33,539,374 where bb±pad would give
400
- // 0,0,539,407. @see lib/common/emit.c:3652 emit_map_rect(job, job->clip)
401
- const ll = { x: -ctx.pad.x, y: -ctx.pad.y };
402
- const ur = {
403
- x: ctx.bb.ur.x - ctx.bb.ll.x + ctx.pad.x,
404
- y: ctx.bb.ur.y - ctx.bb.ll.y + ctx.pad.y,
405
- };
393
+ const { ll, ur } = ctx.pageBox;
406
394
  obj.urlMapShape = MapShape.Rectangle;
407
395
  obj.urlMapPts = [mapTransform(ll, ctx), mapTransform(ur, ctx)];
408
396
  }
@@ -27,6 +27,7 @@ import type { ImageSizer } from '../common/htmltable-types.js';
27
27
  import type { ImageResolver } from './image-resolver.js';
28
28
  import type { RenderJob } from './job.js'; // scaffold in T25; full class in T26
29
29
  import { resetRenderState } from '../common/render-state.js';
30
+ import { agerrorf } from '../common/agerr.js';
30
31
  import { RenderError, invalidArgType, invalidArgValue } from '../errors.js';
31
32
  import { bindDotRoot, resetFreedLayouts, undoRenderedClusterEdges } from './layout-reset.js';
32
33
  import { initXdot } from './init-xdot.js';
@@ -326,12 +327,7 @@ export class GvcContext {
326
327
  let engine = selected;
327
328
  if (attr !== undefined && attr !== '') {
328
329
  const override = this.layouts.get(attr);
329
- if (override === undefined) {
330
- throw new RenderError(
331
- `Layout type: "${attr}" not recognized`,
332
- 'UNKNOWN_LAYOUT',
333
- );
334
- }
330
+ if (override === undefined) throw this.unknownLayout(attr);
335
331
  engine = override;
336
332
  }
337
333
  undoRenderedClusterEdges(g);
@@ -352,6 +348,25 @@ export class GvcContext {
352
348
  if (g.info) g.info.laidOut = true;
353
349
  }
354
350
 
351
+ /**
352
+ * C's `agerrorf("Layout type: \"%s\" not recognized. Use one of:%s\n", ...)`
353
+ * (`gvplugin_list` writes each type as a leading space + name), then the
354
+ * structured error. The thrown wording may differ from the agerr text.
355
+ * @see lib/gvc/gvlayout.c:gvLayoutJobs
356
+ */
357
+ private unknownLayout(attr: string): RenderError {
358
+ const list = [...this.layouts.keys()]
359
+ .sort()
360
+ .map((name: string) => ` ${name}`)
361
+ .join('');
362
+ agerrorf(`Layout type: "${attr}" not recognized. Use one of:${list}`);
363
+ return new RenderError(
364
+ `Layout type: "${attr}" not recognized. Use one of:${list}.`,
365
+ 'UNKNOWN_LAYOUT',
366
+ { context: { layout: attr } },
367
+ );
368
+ }
369
+
355
370
  /**
356
371
  * Release engine layout state after rendering. The records themselves stay,
357
372
  * for getLayout, until the next layout replaces them (part-9 AD-11, see
@@ -20,6 +20,7 @@ import { gvrenderSetStyle, checkClusterStyleTokens } from './gvrender-set-style.
20
20
  import { resolveFillColorName, resolveRenderColor, reportColorNames } from '../render/color-resolve.js';
21
21
  import { parseSegs } from '../common/multicolor.js';
22
22
  import { graphAttrInherited } from '../common/attr-get.js';
23
+ import { beginAnchorIf } from './anchor.js';
23
24
 
24
25
  /**
25
26
  * Copy gradient fields from a linear/radial ResolvedFill onto obj.
@@ -188,3 +189,18 @@ function applyClusterFill(obj: ObjState, fillRes: ResolvedFill): boolean {
188
189
  applyClusterGradient(obj, fillRes);
189
190
  return true;
190
191
  }
192
+
193
+ /**
194
+ * `if (doAnchor) { emit_map_rect(job, GD_bb(sg)); gvrender_begin_anchor(...) }`:
195
+ * record the cluster box as the pending hot spot, then open the anchor.
196
+ * Drawing devices run it before the box, the map device (EMIT_CLUSTERS_LAST)
197
+ * after the label. Returns doAnchor (obj->url || obj->explicit_tooltip); when
198
+ * true the caller closes the anchor with renderer.endAnchor.
199
+ * @see lib/common/emit.c:emit_clusters:3743-3747, 3861-3868
200
+ */
201
+ export function beginClusterAnchor(sg: Graph, renderer: RendererPlugin, job: RenderJob): boolean {
202
+ const obj = job.obj;
203
+ if (obj === null || (obj.url === null && !obj.explicitTooltip)) return false;
204
+ renderer.emitMapRect?.(sg.info.bb!, job);
205
+ return beginAnchorIf(renderer, job);
206
+ }