@knowvah/dot-engine 2.12.0 → 2.12.2
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.
- package/dist/api/builder.d.ts +7 -1
- package/dist/api/builder.d.ts.map +1 -1
- package/dist/api.js +14 -7
- package/dist/api.js.map +2 -2
- package/dist/common/edge-in-box.d.ts +6 -20
- package/dist/common/edge-in-box.d.ts.map +1 -1
- package/dist/common/edge-label-init.d.ts +26 -1
- package/dist/common/edge-label-init.d.ts.map +1 -1
- package/dist/common/emit-bb.d.ts +36 -0
- package/dist/common/emit-bb.d.ts.map +1 -0
- package/dist/common/nodeinit.d.ts +16 -2
- package/dist/common/nodeinit.d.ts.map +1 -1
- package/dist/common/splines-path-begin.d.ts.map +1 -1
- package/dist/common/splines-path-end.d.ts.map +1 -1
- package/dist/gvc/__fixtures__/subg-renders/subg-renders.d.ts +10 -0
- package/dist/gvc/__fixtures__/subg-renders/subg-renders.d.ts.map +1 -0
- package/dist/gvc/context.d.ts +8 -0
- package/dist/gvc/context.d.ts.map +1 -1
- package/dist/gvc/device.d.ts.map +1 -1
- package/dist/gvc/emit-gates.d.ts +5 -10
- package/dist/gvc/emit-gates.d.ts.map +1 -1
- package/dist/gvc/emit-xdot.d.ts +10 -0
- package/dist/gvc/emit-xdot.d.ts.map +1 -1
- package/dist/gvc/job.d.ts +5 -2
- package/dist/gvc/job.d.ts.map +1 -1
- package/dist/gvc/viewport.d.ts +10 -5
- package/dist/gvc/viewport.d.ts.map +1 -1
- package/dist/index.js +7617 -7334
- package/dist/index.js.map +4 -4
- package/dist/layout/dot/decomp.d.ts.map +1 -1
- package/dist/layout/dot/edge-route.d.ts +10 -11
- package/dist/layout/dot/edge-route.d.ts.map +1 -1
- package/dist/layout/dot/fastgr.d.ts +5 -3
- package/dist/layout/dot/fastgr.d.ts.map +1 -1
- package/dist/layout/dot/flat-adj-record.d.ts +22 -0
- package/dist/layout/dot/flat-adj-record.d.ts.map +1 -0
- package/dist/layout/dot/init.d.ts +65 -4
- package/dist/layout/dot/init.d.ts.map +1 -1
- package/dist/layout/dot/mincross-build.d.ts +6 -0
- package/dist/layout/dot/mincross-build.d.ts.map +1 -1
- package/dist/layout/dot/position-aux.d.ts +7 -1
- package/dist/layout/dot/position-aux.d.ts.map +1 -1
- package/dist/layout/dot/splines-clone.d.ts +2 -1
- package/dist/layout/dot/splines-clone.d.ts.map +1 -1
- package/dist/layout/dot/splines-flat-labeled.d.ts.map +1 -1
- package/dist/layout/dot/splines-flat.d.ts +18 -0
- package/dist/layout/dot/splines-flat.d.ts.map +1 -1
- package/dist/layout/dot/splines-groups.d.ts.map +1 -1
- package/dist/layout/dot/splines.d.ts +5 -3
- package/dist/layout/dot/splines.d.ts.map +1 -1
- package/dist/layout/fdp/ports.d.ts.map +1 -1
- package/dist/layout/neato/conjgrad-mkernel.d.ts.map +1 -1
- package/dist/layout/neato/fdp-adjust.d.ts.map +1 -1
- package/dist/layout/neato/sgd.d.ts.map +1 -1
- package/dist/layout/sfdp/spring-electrical.d.ts.map +1 -1
- package/dist/model/cgraph-ops.d.ts +10 -2
- package/dist/model/cgraph-ops.d.ts.map +1 -1
- package/dist/model/edge.d.ts +7 -2
- package/dist/model/edge.d.ts.map +1 -1
- package/dist/model/edgeInfo.d.ts +17 -2
- package/dist/model/edgeInfo.d.ts.map +1 -1
- package/dist/model/graph.d.ts +4 -3
- package/dist/model/graph.d.ts.map +1 -1
- package/dist/model/lazy-attrs.d.ts.map +1 -1
- package/dist/parser/builder.d.ts.map +1 -1
- package/dist/render/dot/agwrite-linelength.d.ts +25 -0
- package/dist/render/dot/agwrite-linelength.d.ts.map +1 -0
- package/dist/render/dot/agwrite.d.ts +9 -8
- package/dist/render/dot/agwrite.d.ts.map +1 -1
- package/dist/render/dot/attrs.d.ts +15 -4
- package/dist/render/dot/attrs.d.ts.map +1 -1
- package/dist/render/dot/write-back.d.ts +24 -0
- package/dist/render/dot/write-back.d.ts.map +1 -1
- package/dist/render/dot/xdot-ops.d.ts +20 -29
- package/dist/render/dot/xdot-ops.d.ts.map +1 -1
- package/dist/render/dot.d.ts +5 -4
- package/dist/render/dot.d.ts.map +1 -1
- package/dist/render/json-cluster-edge.d.ts +35 -0
- package/dist/render/json-cluster-edge.d.ts.map +1 -0
- package/dist/render/json-nonfinite.d.ts +17 -0
- package/dist/render/json-nonfinite.d.ts.map +1 -0
- package/dist/render/json.d.ts +4 -0
- package/dist/render/json.d.ts.map +1 -1
- package/dist/render/map.d.ts +3 -0
- package/dist/render/map.d.ts.map +1 -1
- package/dist/render/svg-graph.d.ts.map +1 -1
- package/dist/render/svg-id.d.ts +10 -1
- package/dist/render/svg-id.d.ts.map +1 -1
- package/dist/render/svg.d.ts +3 -1
- package/dist/render/svg.d.ts.map +1 -1
- package/dist/render.js +7657 -7374
- package/dist/render.js.map +4 -4
- package/dist/util/bsd-qsort.d.ts.map +1 -1
- package/dist/util/printf-round.d.ts.map +1 -1
- package/dist/xdot/parse.d.ts.map +1 -1
- package/dist/xdot/strtod.d.ts +23 -0
- package/dist/xdot/strtod.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/api/builder.ts +8 -2
- package/src/common/edge-in-box.ts +9 -24
- package/src/common/edge-label-init.ts +43 -17
- package/src/common/emit-bb.ts +128 -0
- package/src/common/nodeinit.ts +28 -8
- package/src/common/splines-path-begin.ts +4 -3
- package/src/common/splines-path-end.ts +4 -3
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.1 +45 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.2 +45 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.dot.nofree.3 +45 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.1 +74 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.2 +74 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.dot.xdot.nofree.3 +76 -0
- package/src/gvc/__fixtures__/rerender/nestedclust.gv +4 -0
- package/src/gvc/__fixtures__/rerender/regen.sh +5 -0
- package/src/gvc/__fixtures__/subg-renders/subg-renders.ts +48 -0
- package/src/gvc/context.ts +8 -0
- package/src/gvc/device.ts +28 -29
- package/src/gvc/emit-gates.ts +7 -16
- package/src/gvc/emit-xdot.ts +10 -12
- package/src/gvc/job.ts +5 -2
- package/src/gvc/viewport.ts +13 -7
- package/src/layout/dot/__fixtures__/flat-adj-aux-syms.native-16.1.json +1210 -0
- package/src/layout/dot/decomp.ts +23 -2
- package/src/layout/dot/edge-route.ts +52 -86
- package/src/layout/dot/fastgr.ts +9 -3
- package/src/layout/dot/flat-adj-record.ts +68 -0
- package/src/layout/dot/init.ts +84 -13
- package/src/layout/dot/mincross-build.ts +14 -1
- package/src/layout/dot/position-aux.ts +10 -3
- package/src/layout/dot/splines-clone.ts +8 -2
- package/src/layout/dot/splines-flat-labeled.ts +3 -2
- package/src/layout/dot/splines-flat.ts +11 -21
- package/src/layout/dot/splines-groups.ts +7 -5
- package/src/layout/dot/splines-route.ts +2 -2
- package/src/layout/dot/splines.ts +8 -8
- package/src/layout/fdp/ports.ts +6 -2
- package/src/layout/neato/conjgrad-mkernel.ts +6 -2
- package/src/layout/neato/conjgrad.ts +2 -2
- package/src/layout/neato/fdp-adjust.ts +1 -16
- package/src/layout/neato/sgd.ts +2 -1
- package/src/layout/sfdp/spring-electrical.ts +5 -3
- package/src/model/cgraph-ops.ts +4 -3
- package/src/model/edge.ts +11 -4
- package/src/model/edgeInfo.ts +18 -2
- package/src/model/graph.ts +4 -3
- package/src/model/lazy-attrs.ts +3 -1
- package/src/parser/builder.ts +12 -3
- package/src/render/dot/agwrite-linelength.ts +58 -0
- package/src/render/dot/agwrite.ts +23 -16
- package/src/render/dot/attrs.ts +45 -8
- package/src/render/dot/write-back.ts +48 -3
- package/src/render/dot/xdot-ops.ts +26 -57
- package/src/render/dot.ts +16 -20
- package/src/render/json-cluster-edge.ts +87 -0
- package/src/render/json-nonfinite.ts +51 -0
- package/src/render/json.ts +36 -42
- package/src/render/map.ts +6 -7
- package/src/render/svg-graph.ts +9 -8
- package/src/render/svg-id.ts +11 -2
- package/src/render/svg.ts +7 -4
- package/src/util/bsd-qsort.ts +2 -1
- package/src/util/printf-round.ts +16 -10
- package/src/xdot/parse.ts +9 -19
- package/src/xdot/strtod.ts +167 -0
- package/dist/layout/dot/edge-order.d.ts +0 -27
- package/dist/layout/dot/edge-order.d.ts.map +0 -1
- package/src/layout/dot/edge-order.ts +0 -62
|
@@ -17,16 +17,18 @@
|
|
|
17
17
|
import type { Graph } from '../../model/graph.js';
|
|
18
18
|
import type { Node } from '../../model/node.js';
|
|
19
19
|
import type { Edge } from '../../model/edge.js';
|
|
20
|
+
import { LOCALNAMEPREFIX, agNameOf } from '../../model/cgraph-ops.js';
|
|
20
21
|
import type { TextlabelT, FieldT, ShapeDesc } from '../../common/types.js';
|
|
21
22
|
import { agstrcanon, agstrcanonText } from '../map.js';
|
|
22
23
|
import { isHtmlValue } from '../../common/html-string.js';
|
|
23
|
-
import { XDOT_VERSION,
|
|
24
|
+
import { XDOT_VERSION, gfmt5, lpStr, xdotId } from './xdot-ops.js';
|
|
24
25
|
import {
|
|
25
26
|
COMPUTED_EDGE_ATTRS, COMPUTED_NODE_ATTRS, appendRecordRects, computedPart, dictParts,
|
|
26
27
|
echoAttr, echoGraphAttr, edgeAttrsAttached, edgeConnector, edgePosRaw,
|
|
27
28
|
effectiveEdgeDefaults, effectiveNodeDefaults, graphInputParts, graphLabelAttrs,
|
|
28
29
|
isDirected, nodeDictParts, nodePosRaw, nodeRecord, objInputParts,
|
|
29
30
|
} from './attrs.js';
|
|
31
|
+
import { withLinelength } from './agwrite-linelength.js';
|
|
30
32
|
import { nodesInSeq } from '../../layout/dot/decomp.js';
|
|
31
33
|
import type { XdotDraws, SerCtx } from './types.js';
|
|
32
34
|
|
|
@@ -40,16 +42,13 @@ export abstract class DotWriterBase {
|
|
|
40
42
|
protected abstract drawsOf(obj: Node | Edge | Graph): XdotDraws | undefined;
|
|
41
43
|
|
|
42
44
|
/**
|
|
43
|
-
* Emit `key
|
|
44
|
-
* `"` becomes `\"` unless
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* double-escaped). The byte-length prefix stays on the UNescaped text (the
|
|
48
|
-
* parser un-escapes before parseXDot re-reads it), matching native exactly.
|
|
49
|
-
* @see lib/cgraph/write.c:_agstrcanon (135-167)
|
|
45
|
+
* Emit `key=<canon value>` through `_agstrcanon`, as `agwrite` does for the xdot
|
|
46
|
+
* op attributes: `"` becomes `\"` unless part of an escape, and long strings
|
|
47
|
+
* wrap at Max_outputline. The byte-length prefix stays on the UNescaped text.
|
|
48
|
+
* @see lib/cgraph/write.c:_agstrcanon
|
|
50
49
|
*/
|
|
51
50
|
private drawAttr(key: string, value: string): string {
|
|
52
|
-
return key + '=
|
|
51
|
+
return key + '=' + agstrcanonText(value);
|
|
53
52
|
}
|
|
54
53
|
|
|
55
54
|
/** `llx,lly,urx,ury` from a graph's layout bb. */
|
|
@@ -79,10 +78,12 @@ export abstract class DotWriterBase {
|
|
|
79
78
|
level: 0,
|
|
80
79
|
};
|
|
81
80
|
this.subgdfs(g, 1, ctx.preorder);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
return withLinelength(g, () => {
|
|
82
|
+
this.writeHdr(g, true, ctx);
|
|
83
|
+
this.writeBody(g, ctx);
|
|
84
|
+
this.writeTrl(ctx);
|
|
85
|
+
return ctx.out.join('');
|
|
86
|
+
});
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
/** Preorder-number the subgraph tree. @see write.c:subgdfs */
|
|
@@ -93,9 +94,13 @@ export abstract class DotWriterBase {
|
|
|
93
94
|
return ix0 + 1;
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
/** A
|
|
97
|
+
/** A graph is anonymous when `agnameof` gives a `%` local name: a nameless
|
|
98
|
+
* root (`%1`), a nameless subgraph (the parser names it `%N`), or a name
|
|
99
|
+
* that itself starts with `%`. A graph named `""` is named: cgraph maps `""`
|
|
100
|
+
* as a name, so it keeps its header and is never inlined.
|
|
101
|
+
* @see write.c:is_anonymous ; write.c:write_hdr ; lib/cgraph/id.c:idmap */
|
|
97
102
|
private isAnonymous(g: Graph): boolean {
|
|
98
|
-
return g.
|
|
103
|
+
return agNameOf(g).startsWith(LOCALNAMEPREFIX);
|
|
99
104
|
}
|
|
100
105
|
|
|
101
106
|
/** A graph carries a `bb` attribute when it is the root or has a layout bb
|
|
@@ -155,7 +160,9 @@ export abstract class DotWriterBase {
|
|
|
155
160
|
if (top) {
|
|
156
161
|
const strict = g.kind === 'strict-directed' || g.kind === 'strict-undirected' ? 'strict ' : '';
|
|
157
162
|
const kw = isDirected(g) ? 'digraph' : 'graph';
|
|
158
|
-
|
|
163
|
+
// The root's name is '' both when it has none (agnameof: `%1`, not
|
|
164
|
+
// printed) and when it is named `""` (printed as `""`).
|
|
165
|
+
const nm = this.isAnonymous(g) ? '' : xdotId(g.name) + ' ';
|
|
159
166
|
ctx.out.push(strict + kw + ' ' + nm + '{\n');
|
|
160
167
|
ctx.level++;
|
|
161
168
|
} else {
|
package/src/render/dot/attrs.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { IGNORED } from '../../layout/dot/rank.js';
|
|
|
16
16
|
import { agstrcanon } from '../map.js';
|
|
17
17
|
import { POINTS_PER_INCH } from '../../model/geom.js';
|
|
18
18
|
import type { TextlabelT, FieldT } from '../../common/types.js';
|
|
19
|
-
import {
|
|
19
|
+
import { gfmt2, gfmt5, lpStr } from './xdot-ops.js';
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* C's `attach_attrs` edge loop skips IGNORED edges and edges with no spline
|
|
@@ -222,6 +222,32 @@ function computedGraphAttrs(emitDraws: boolean): Set<string> {
|
|
|
222
222
|
return emitDraws ? COMPUTED_GRAPH_ATTRS_XDOT : COMPUTED_GRAPH_ATTRS;
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
/** `COMPUTED_GRAPH_ATTRS` for a subgraph that is not a laid-out cluster. */
|
|
226
|
+
const COMPUTED_SUBGRAPH_ATTRS = new Set(['bb', 'lp', 'lwidth', 'lheight']);
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* The attributes a scope does not echo from its own dict: at the root,
|
|
230
|
+
* `computed`; at a subgraph scope, never xdotversion (xdot_end_graph agsafesets
|
|
231
|
+
* it on the render root, so a subgraph rendered as a root keeps it), and
|
|
232
|
+
* `_draw_`/`_ldraw_` only for a cluster in an ancestor's GD_clust, which
|
|
233
|
+
* emit_clusters draws and clusterAttrs prints. Any other subgraph prints its
|
|
234
|
+
* stored or input values.
|
|
235
|
+
* @see plugin/core/gvrender_core_dot.c:427 xdot_end_graph, :280 xdot_end_cluster
|
|
236
|
+
*/
|
|
237
|
+
function ownComputedAttrs(g: Graph, top: boolean, computed: Set<string>): ReadonlySet<string> {
|
|
238
|
+
if (top) return computed;
|
|
239
|
+
return inClusterTree(g) ? COMPUTED_GRAPH_ATTRS : COMPUTED_SUBGRAPH_ATTRS;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Whether an ancestor's GD_clust holds `g` (a cluster inside an anonymous
|
|
243
|
+
* subgraph belongs to the nearest cluster or root above it). */
|
|
244
|
+
function inClusterTree(g: Graph): boolean {
|
|
245
|
+
for (let a = g.parent; a !== null; a = a.parent) {
|
|
246
|
+
if ((a.info.clust ?? []).slice(0, a.info.n_cluster ?? 0).includes(g)) return true;
|
|
247
|
+
}
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
|
|
225
251
|
/** Walk to the root graph (cgraph `agroot`). */
|
|
226
252
|
export function rootOf(g: Graph): Graph {
|
|
227
253
|
let cur: Graph = g;
|
|
@@ -262,7 +288,7 @@ export function graphInputParts(g: Graph, top: boolean, emitDraws: boolean): str
|
|
|
262
288
|
const snap = g.graphDefaultsSnapshot;
|
|
263
289
|
const seeded = g.seededAttrs;
|
|
264
290
|
for (const [k, v] of g.attrs) {
|
|
265
|
-
if (computed.has(k)) continue;
|
|
291
|
+
if (ownComputedAttrs(g, top, computed).has(k)) continue;
|
|
266
292
|
if (!top && seeded !== undefined && seeded.has(k)) continue;
|
|
267
293
|
if (writeDictSkips(v, snap?.get(k))) continue;
|
|
268
294
|
parts.push(k + '=' + agstrcanon(v));
|
|
@@ -330,10 +356,19 @@ export function nodeDictParts(g: Graph, top: boolean): string[] {
|
|
|
330
356
|
return dictParts(defs);
|
|
331
357
|
}
|
|
332
358
|
|
|
359
|
+
/**
|
|
360
|
+
* A node or edge attribute the layout did not overwrite: the input's own value,
|
|
361
|
+
* written as agwrite writes every value, `name=` + `agstrcanon(value)` — an
|
|
362
|
+
* html value as `<...>`, a plain one quoted only when it needs it and broken
|
|
363
|
+
* with backslash-newline at `Max_outputline`. An absent or empty value prints
|
|
364
|
+
* nothing.
|
|
365
|
+
* @see lib/cgraph/write.c:write_nondefault_attrs (502-504)
|
|
366
|
+
* @see lib/cgraph/write.c:_write_canonstr
|
|
367
|
+
*/
|
|
333
368
|
export function echoAttr(attrs: Map<string, string>, key: string): string[] {
|
|
334
369
|
const v = attrs.get(key);
|
|
335
370
|
if (v === undefined || v.length === 0) return [];
|
|
336
|
-
return [key
|
|
371
|
+
return [`${key}=${agstrcanon(v)}`];
|
|
337
372
|
}
|
|
338
373
|
|
|
339
374
|
/**
|
|
@@ -359,16 +394,18 @@ export function writeDictSkips(v: string, inherited: string | undefined): boolea
|
|
|
359
394
|
|
|
360
395
|
/**
|
|
361
396
|
* `echoAttr` for a GRAPH attribute the layout did not overwrite — same as
|
|
362
|
-
* echoAttr
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
* (tests/share/KW91.gv).
|
|
397
|
+
* echoAttr (the value goes through `agstrcanon`, as write_dict's does) except
|
|
398
|
+
* an EMPTY value can still be emitted, per `writeDictSkips`. So a re-fed dot
|
|
399
|
+
* output whose `{rank=same}` block carries `bb=""`/`lp=""` under a boxed,
|
|
400
|
+
* labelled cluster echoes both back verbatim (tests/share/KW91.gv).
|
|
401
|
+
* @see lib/cgraph/write.c:write_dict (290-292)
|
|
402
|
+
* @see lib/cgraph/write.c:_write_canonstr
|
|
366
403
|
*/
|
|
367
404
|
export function echoGraphAttr(g: Graph, key: string): string[] {
|
|
368
405
|
const v = g.attrs.get(key);
|
|
369
406
|
if (v === undefined) return [];
|
|
370
407
|
if (writeDictSkips(v, g.graphDefaultsSnapshot?.get(key))) return [];
|
|
371
|
-
return [key
|
|
408
|
+
return [`${key}=${agstrcanon(v)}`];
|
|
372
409
|
}
|
|
373
410
|
|
|
374
411
|
/**
|
|
@@ -29,13 +29,37 @@ const POINTS_PER_INCH = 72;
|
|
|
29
29
|
/** The draw strings of one object, or undefined for an object drawn with none. */
|
|
30
30
|
type DrawsOf = (obj: Node | Edge | Graph) => XdotDraws | undefined;
|
|
31
31
|
|
|
32
|
+
/** The three strings attach_attrs writes on every node. */
|
|
33
|
+
export interface NodeLayoutAttrs {
|
|
34
|
+
readonly pos: string;
|
|
35
|
+
readonly height: string;
|
|
36
|
+
readonly width: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A laid-out node's `pos`, `height` and `width` as attach_attrs prints them:
|
|
41
|
+
* `%.5g` of ND_coord (every coordinate when `odim >= 3`), PS2INCH(ND_ht) and
|
|
42
|
+
* PS2INCH(ND_lw + ND_rw). Pure: the write-back and json0, which has no xdot
|
|
43
|
+
* render to read them from, both take their values here.
|
|
44
|
+
* @see lib/common/output.c:296-309
|
|
45
|
+
*/
|
|
46
|
+
export function nodeLayoutAttrs(n: Node): NodeLayoutAttrs {
|
|
47
|
+
const info = n.info;
|
|
48
|
+
return {
|
|
49
|
+
pos: nodePosRaw(n),
|
|
50
|
+
height: gfmt5(info.ht / POINTS_PER_INCH),
|
|
51
|
+
width: gfmt5((info.lw + info.rw) / POINTS_PER_INCH),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
32
55
|
/** @see lib/common/output.c:296-317 */
|
|
33
56
|
function attachNode(n: Node): void {
|
|
34
57
|
const attrs = ownAttrs(n);
|
|
35
58
|
const info = n.info;
|
|
36
|
-
|
|
37
|
-
attrs.set('
|
|
38
|
-
attrs.set('
|
|
59
|
+
const layout = nodeLayoutAttrs(n);
|
|
60
|
+
attrs.set('pos', layout.pos);
|
|
61
|
+
attrs.set('height', layout.height);
|
|
62
|
+
attrs.set('width', layout.width);
|
|
39
63
|
const xlabel = info.xlabel as TextlabelT | undefined;
|
|
40
64
|
if (xlabel?.set) attrs.set('xlp', lpStr(xlabel.pos));
|
|
41
65
|
if ((info.shape as ShapeDesc | undefined)?.name === 'record') {
|
|
@@ -100,6 +124,27 @@ export function storedDraws(attrs: Map<string, string>): XdotDraws {
|
|
|
100
124
|
};
|
|
101
125
|
}
|
|
102
126
|
|
|
127
|
+
/**
|
|
128
|
+
* The draw strings agwrite prints for `obj` after this render. C stores them in
|
|
129
|
+
* the attributes before agwrite: xdot_end_node and xdot_end_edge agxset only
|
|
130
|
+
* non-empty buffers, so an object this render did not draw (outside the clip,
|
|
131
|
+
* invisible, or outside a subgraph render) prints what an earlier render or the
|
|
132
|
+
* input stored; xdot_end_cluster sets a drawn cluster's `_draw_` even when empty.
|
|
133
|
+
* @see plugin/core/gvrender_core_dot.c:237 xdot_end_node, :251 xdot_end_edge
|
|
134
|
+
* @see plugin/core/gvrender_core_dot.c:280 xdot_end_cluster, :427 xdot_end_graph
|
|
135
|
+
*/
|
|
136
|
+
export function renderedDraws(obj: Node | Edge | Graph, d: XdotDraws | undefined, cluster: boolean): XdotDraws {
|
|
137
|
+
const st = storedDraws(obj.attrs);
|
|
138
|
+
return {
|
|
139
|
+
draw: cluster ? (d?.draw ?? '') : d?.draw || st.draw,
|
|
140
|
+
ldraw: d?.ldraw || st.ldraw,
|
|
141
|
+
hdraw: d?.hdraw || st.hdraw,
|
|
142
|
+
tdraw: d?.tdraw || st.tdraw,
|
|
143
|
+
hldraw: d?.hldraw || st.hldraw,
|
|
144
|
+
tldraw: d?.tldraw || st.tldraw,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
103
148
|
/** One object's non-empty draw strings; an empty buffer leaves the slot alone. */
|
|
104
149
|
function attachDraws(attrs: Map<string, string>, d: XdotDraws | undefined): void {
|
|
105
150
|
if (d?.draw) attrs.set('_draw_', d.draw);
|
|
@@ -13,6 +13,8 @@ import { colorxlate } from '../../common/color.js';
|
|
|
13
13
|
import { getGradientPoints } from '../svg-gradient.js';
|
|
14
14
|
import { EmitState, toFixed2HalfEven } from '../../gvc/job.js';
|
|
15
15
|
import { printfSig, printfFixed } from '../../util/printf-round.js';
|
|
16
|
+
import { isHtmlValue } from '../../common/html-string.js';
|
|
17
|
+
import { agstrcanon } from '../map.js';
|
|
16
18
|
|
|
17
19
|
// ---------------------------------------------------------------------------
|
|
18
20
|
// Constants
|
|
@@ -296,31 +298,34 @@ export function xdotStrOp(prefix: string, s: string): string {
|
|
|
296
298
|
}
|
|
297
299
|
|
|
298
300
|
/**
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
301
|
+
* Canonicalize a node, edge-endpoint or graph/subgraph name for `-Tdot`/`-Txdot`
|
|
302
|
+
* as agwrite does: `write_nodename` and `write_hdr` both print the name through
|
|
303
|
+
* `write_canonstr`, i.e. `agstrcanon` with the html check on. So a name is bare
|
|
304
|
+
* when it is an id or numeral, quoted when it is a keyword or holds any other
|
|
305
|
+
* byte, wrapped at `Max_outputline`, and angle-bracketed when it is html-like.
|
|
306
|
+
* A `\` is copied verbatim: it is already the start of a stored escape like
|
|
307
|
+
* `\n`/`\l`, so doubling it would change the name.
|
|
308
|
+
* @see lib/cgraph/write.c:write_nodename
|
|
309
|
+
* @see lib/cgraph/write.c:write_hdr
|
|
310
|
+
* @see lib/cgraph/write.c:write_canonstr
|
|
306
311
|
*
|
|
307
|
-
* ONE DELIBERATE DIVERGENCE from _agstrcanon: an ODD trailing backslash run
|
|
308
|
-
* padded to even. C copies it verbatim and appends the
|
|
309
|
-
* ending in a single `\` serializes as `"a\"` — the
|
|
310
|
-
* and the output does not reparse. The DOT lexer
|
|
311
|
-
* (source `"a\"` never terminates), so this is
|
|
312
|
-
* corpus id changes; it is reachable only
|
|
313
|
-
*
|
|
314
|
-
* `a\\` instead of `a\`, which beats
|
|
312
|
+
* ONE DELIBERATE DIVERGENCE from _agstrcanon: an ODD trailing backslash run on
|
|
313
|
+
* a non-html name is padded to even. C copies it verbatim and appends the
|
|
314
|
+
* closing quote, so a name ending in a single `\` serializes as `"a\"` — the
|
|
315
|
+
* backslash escapes the quote and the output does not reparse. The DOT lexer
|
|
316
|
+
* cannot produce such a name (source `"a\"` never terminates), so this is
|
|
317
|
+
* unreachable from any file and no corpus id changes; it is reachable only
|
|
318
|
+
* through the programmatic API, where a caller supplies the string directly.
|
|
319
|
+
* Padding costs a name that round-trips to `a\\` instead of `a\`, which beats
|
|
320
|
+
* emitting a document that cannot be parsed. An html name is never padded: C
|
|
321
|
+
* prints `<a\>` and that reparses.
|
|
315
322
|
* @see CodeQL "Incomplete string escaping or encoding" — the alert's own remedy
|
|
316
323
|
* (escape every `\`) is wrong here and breaks `\n`/`\l` parity.
|
|
317
324
|
*/
|
|
318
325
|
export function xdotId(s: string): string {
|
|
319
|
-
if (
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
const trailingBackslashes = /\\*$/.exec(body)![0].length;
|
|
323
|
-
return '"' + body + (trailingBackslashes % 2 === 1 ? '\\' : '') + '"';
|
|
326
|
+
if (isHtmlValue(s)) return agstrcanon(s);
|
|
327
|
+
const oddTrailingRun = /\\*$/.exec(s)![0].length % 2 === 1;
|
|
328
|
+
return agstrcanon(oddTrailingRun ? `${s}\\` : s);
|
|
324
329
|
}
|
|
325
330
|
|
|
326
331
|
/**
|
|
@@ -338,7 +343,7 @@ export function xdotId(s: string): string {
|
|
|
338
343
|
* @see lib/common/output.c:71 (agxbprint "%.5g")
|
|
339
344
|
*/
|
|
340
345
|
export function gfmt5(v: number): string {
|
|
341
|
-
if (!Number.isFinite(v)) return
|
|
346
|
+
if (!Number.isFinite(v)) return printfSig(v, 5); // inf, -inf, nan
|
|
342
347
|
if (v === 0) return '0';
|
|
343
348
|
let s = printfSig(v, 5);
|
|
344
349
|
const e = s.indexOf('e');
|
|
@@ -385,42 +390,6 @@ export function escBackslash(s: string): string {
|
|
|
385
390
|
return s.replace(/\\/g, '\\\\');
|
|
386
391
|
}
|
|
387
392
|
|
|
388
|
-
/** True if s[i..] starts an escape sequence agcanonStr keeps verbatim: a `\`
|
|
389
|
-
* followed by one of E G H L N T l n r \ ". @see lib/cgraph/write.c:is_escape */
|
|
390
|
-
export function isEscapeSeq(s: string, i: number): boolean {
|
|
391
|
-
if (s[i] !== '\\') return false;
|
|
392
|
-
const n = s[i + 1];
|
|
393
|
-
return n === 'E' || n === 'G' || n === 'H' || n === 'L' || n === 'N' || n === 'T'
|
|
394
|
-
|| n === 'l' || n === 'n' || n === 'r' || n === '\\' || n === '"';
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Escape a value for a DOT attribute exactly as agwrite's agcanonStr does: a `"`
|
|
399
|
-
* becomes `\"` ONLY when it is not already part of an escape sequence, so an
|
|
400
|
-
* existing `\"` or `\\` in the value is passed through verbatim rather than
|
|
401
|
-
* double-escaped. For an already-backslash-doubled value (node/edge/graph labels,
|
|
402
|
-
* post put_escaping_backslashes) this is identical to a naive `"`→`\"` — every
|
|
403
|
-
* `"` follows a doubled `\\`, so none are ever part_of_escape. For raw cluster
|
|
404
|
-
* labels (agset, no put_escaping) it preserves the source `\"`/`\\` unchanged.
|
|
405
|
-
* @see lib/cgraph/write.c:_agstrcanon (135-167)
|
|
406
|
-
*/
|
|
407
|
-
export function agcanonEscape(s: string): string {
|
|
408
|
-
let out = '';
|
|
409
|
-
let partOfEscape = false;
|
|
410
|
-
for (let i = 0; i < s.length; i++) {
|
|
411
|
-
const c = s[i]!;
|
|
412
|
-
if (c === '"' && !partOfEscape) {
|
|
413
|
-
out += '\\';
|
|
414
|
-
} else if (!partOfEscape && isEscapeSeq(s, i)) {
|
|
415
|
-
partOfEscape = true;
|
|
416
|
-
} else {
|
|
417
|
-
partOfEscape = false;
|
|
418
|
-
}
|
|
419
|
-
out += c;
|
|
420
|
-
}
|
|
421
|
-
return out;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
393
|
/** Trim a "%.3f" fixed string like C's agxbuf_trim_zeros (trailing 0s + dot). */
|
|
425
394
|
export function trimFixed3(v: number): string {
|
|
426
395
|
let s = v.toFixed(3);
|
package/src/render/dot.ts
CHANGED
|
@@ -20,12 +20,12 @@ import type { GVColor } from '../common/color.js';
|
|
|
20
20
|
import { resolveRenderColor } from './color-resolve.js';
|
|
21
21
|
import { edgeIsTapered } from './svg-tapered-edge.js';
|
|
22
22
|
import { findStopColor, parseStyleFlags } from '../common/style-resolve.js';
|
|
23
|
-
import {
|
|
23
|
+
import { deviceClip } from '../gvc/emit-xdot.js';
|
|
24
24
|
import { edgeHasDrawableContent } from './svg.js';
|
|
25
25
|
import type { Bezier } from '../model/geom.js';
|
|
26
26
|
import type { RendererPlugin } from '../gvc/context.js';
|
|
27
27
|
import { EdgeDrawBase } from './dot/edge-draw.js';
|
|
28
|
-
import { attachXdotAttrs, storedDraws } from './dot/write-back.js';
|
|
28
|
+
import { attachXdotAttrs, renderedDraws, storedDraws } from './dot/write-back.js';
|
|
29
29
|
export * from './dot/types.js';
|
|
30
30
|
export type { XdotDraws, SerCtx } from './dot/types.js';
|
|
31
31
|
import type { XdotDraws } from './dot/types.js';
|
|
@@ -68,6 +68,8 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
68
68
|
readonly type: string = 'xdot';
|
|
69
69
|
/** @see plugin/core/gvplugin_core.c registration table */
|
|
70
70
|
readonly quality = 0;
|
|
71
|
+
/** `-Tdot` too (`:734`). @see plugin/core/gvrender_core_dot.c:745 render_features_xdot */
|
|
72
|
+
readonly defaultPad = 0;
|
|
71
73
|
|
|
72
74
|
/**
|
|
73
75
|
* Whether this format attaches the xdot draw attributes before agwrite.
|
|
@@ -79,10 +81,10 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
79
81
|
*/
|
|
80
82
|
protected override readonly emitDraws: boolean = true;
|
|
81
83
|
|
|
82
|
-
/**
|
|
83
|
-
* attribute slots already hold. Single gate so the two paths cannot drift. */
|
|
84
|
+
/** The draw strings agwrite prints for `obj`: `-Tdot` computes none (renderedDraws). */
|
|
84
85
|
protected drawsOf(obj: Node | Edge | Graph): XdotDraws | undefined {
|
|
85
|
-
|
|
86
|
+
if (!this.emitDraws) return storedDraws(obj.attrs);
|
|
87
|
+
return renderedDraws(obj, this.draws.get(obj), this.clusters.includes(obj as Graph));
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
/** Per-render draw buffers. Indices 8/9 alias 1; 10/11 alias 5. */
|
|
@@ -104,8 +106,8 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
104
106
|
this.clusters = [];
|
|
105
107
|
}
|
|
106
108
|
|
|
107
|
-
/**
|
|
108
|
-
*
|
|
109
|
+
/** This render's draw strings by ORIGINAL object. `-Tjson` lays them over the re-parsed xdot text,
|
|
110
|
+
* which cannot represent draw text containing `"` (id 2239); stored strings reach it by that text. */
|
|
109
111
|
drawStringsByObject(): ReadonlyMap<Node | Edge | Graph, XdotDraws> {
|
|
110
112
|
return this.draws;
|
|
111
113
|
}
|
|
@@ -137,18 +139,12 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
137
139
|
// explicit `bgcolor=transparent` to white (a filled white canvas), unlike a
|
|
138
140
|
// truecolor device that paints nothing. @see lib/common/emit.c:1490
|
|
139
141
|
if (fillSpec === 'transparent') fillSpec = 'white';
|
|
140
|
-
// The canvas fill covers job->clip
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
const padAttr = g.attrs.get('pad');
|
|
145
|
-
const pad = padAttr !== undefined && padAttr !== '' ? parseGraphPad(padAttr) : { x: 0, y: 0 };
|
|
146
|
-
const bb = job.bb;
|
|
142
|
+
// The canvas fill covers job->clip, which setup_page derives from the focus
|
|
143
|
+
// and page size: bb ± pad (xdot's default pad is 0), and nan for an infinite
|
|
144
|
+
// bb. @see emit.c:1559-1562 setup_page · gvrender_core_dot.c:745 (default_pad)
|
|
145
|
+
const c = deviceClip(g, job);
|
|
147
146
|
const corners: Point[] = [
|
|
148
|
-
{ x:
|
|
149
|
-
{ x: bb.ll.x - pad.x, y: bb.ur.y + pad.y },
|
|
150
|
-
{ x: bb.ur.x + pad.x, y: bb.ur.y + pad.y },
|
|
151
|
-
{ x: bb.ur.x + pad.x, y: bb.ll.y - pad.y },
|
|
147
|
+
{ x: c.ll.x, y: c.ll.y }, { x: c.ll.x, y: c.ur.y }, { x: c.ur.x, y: c.ur.y }, { x: c.ur.x, y: c.ll.y },
|
|
152
148
|
];
|
|
153
149
|
const buf = this.bufs[EmitState.GDraw]!;
|
|
154
150
|
buf.push(xdotPenColor(resolveRenderColor('transparent')));
|
|
@@ -197,7 +193,7 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
197
193
|
// SVG renderer applies the same gate via edgeGroupOpen; the xdot renderer
|
|
198
194
|
// must too, else concentrate-merged edges (no spline, stale off-box label
|
|
199
195
|
// position) emit a spurious _ldraw_. @see lib/common/emit.c:emit_edge (3039)
|
|
200
|
-
this.edgeDrawable = edgeHasDrawableContent(e, job.
|
|
196
|
+
this.edgeDrawable = edgeHasDrawableContent(e, job.clip);
|
|
201
197
|
const spl = e.info.spl;
|
|
202
198
|
if (!this.edgeDrawable || spl === undefined) return;
|
|
203
199
|
const edraw = this.bufs[EmitState.EDraw]!;
|
|
@@ -307,7 +303,7 @@ export class XdotRenderer extends EdgeDrawBase implements RendererPlugin {
|
|
|
307
303
|
if (draw) set.draw = draw;
|
|
308
304
|
// Cluster labels are stored via plain agxset (xdot_end_cluster:286), NOT
|
|
309
305
|
// put_escaping_backslashes like node/edge/graph labels — so NO escBackslash.
|
|
310
|
-
// The source `\"`/`\\` in the label survive as-is through
|
|
306
|
+
// The source `\"`/`\\` in the label survive as-is through agstrcanonText on
|
|
311
307
|
// serialize (drawAttr). @see gvrender_core_dot.c:286
|
|
312
308
|
if (ldraw) set.ldraw = ldraw;
|
|
313
309
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EPL-2.0
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The cluster-edge half of the json writers. C's attach_attrs ends by undoing
|
|
5
|
+
* fdp's cluster edges, and both json formats run it before they write, so the
|
|
6
|
+
* proxy nodes `__<i>:<cluster>` never print. The port's writers otherwise read
|
|
7
|
+
* the proxies as they print and undo at the next layout (`layout-reset.ts`);
|
|
8
|
+
* json prints from the graph itself, so it takes the undo at C's point, through
|
|
9
|
+
* the same helper. A graph with no cluster edges keeps the usual order (attach
|
|
10
|
+
* after the write): nothing there can differ.
|
|
11
|
+
*
|
|
12
|
+
* Also the json cluster map: write_graph's top pass numbers the subgraphs and
|
|
13
|
+
* maps each cluster's name to its gid (label_subgs, insert), which is where a
|
|
14
|
+
* cluster node's gid comes from (lookup) and where a repeated cluster name is
|
|
15
|
+
* warned about.
|
|
16
|
+
*
|
|
17
|
+
* @see plugin/core/gvrender_core_json.c:json_begin_graph
|
|
18
|
+
* @see plugin/core/gvrender_core_json.c:write_graph
|
|
19
|
+
* @see lib/common/output.c:403 (if (HAS_CLUST_EDGE(g)) undoClusterEdges(g))
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { agwarningf } from '../common/agerr.js';
|
|
23
|
+
import { undoRenderedClusterEdges } from '../gvc/layout-reset.js';
|
|
24
|
+
import { isACluster } from '../layout/dot/rank.js';
|
|
25
|
+
import type { Graph } from '../model/graph.js';
|
|
26
|
+
import type { Node } from '../model/node.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* With cluster edges on `g`: runs `attach` (attach_attrs' write-back) and the
|
|
30
|
+
* undo now, and returns a no-op. Otherwise returns `attach`, for the caller to
|
|
31
|
+
* run after the write.
|
|
32
|
+
* @see lib/common/output.c:416 attach_attrs
|
|
33
|
+
*/
|
|
34
|
+
export function attachBeforeWriteWithClusterEdges(g: Graph, attach: () => void): () => void {
|
|
35
|
+
if (!g.info?.n_cluster_edges) return attach;
|
|
36
|
+
attach();
|
|
37
|
+
undoRenderedClusterEdges(g);
|
|
38
|
+
return () => undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The nodes json writes: C's write_nodes skips every IS_CLUST_NODE, the stand-in
|
|
43
|
+
* node `mapN` makes for a cluster an edge points at.
|
|
44
|
+
* @see plugin/core/gvrender_core_json.c:571 (if (IS_CLUST_NODE(n)) continue)
|
|
45
|
+
*/
|
|
46
|
+
export function jsonNodes(g: Graph): Node[] {
|
|
47
|
+
return [...g.nodes.values()].filter((n) => !n.info.clustnode);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The gid mapped to cluster `name`, or -1 when there is none.
|
|
52
|
+
* @see plugin/core/gvrender_core_json.c:lookup
|
|
53
|
+
*/
|
|
54
|
+
export function lookupCluster(map: ReadonlyMap<string, number>, name: string): number {
|
|
55
|
+
return map.get(name) ?? -1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Maps cluster `name` to gid `v`. The first entry of a name is kept; a later
|
|
60
|
+
* one with another value is warned about and dropped.
|
|
61
|
+
* @see plugin/core/gvrender_core_json.c:insert
|
|
62
|
+
*/
|
|
63
|
+
export function insertCluster(map: Map<string, number>, name: string, v: number): void {
|
|
64
|
+
const ip = map.get(name);
|
|
65
|
+
if (ip !== undefined) {
|
|
66
|
+
if (ip !== v) agwarningf(`Duplicate cluster name "${name}"`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
map.set(name, v);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Numbers `g`'s subgraphs in DFS preorder from `lbl` (C's `GD_gid(g) = lbl++`;
|
|
74
|
+
* here a subgraph's gid is its index in `out`) and maps each cluster's name to
|
|
75
|
+
* its gid. Returns the next label: the subgraph count when called on the root
|
|
76
|
+
* with 0.
|
|
77
|
+
* @see plugin/core/gvrender_core_json.c:label_subgs
|
|
78
|
+
*/
|
|
79
|
+
export function labelSubgs(g: Graph, lbl: number, map: Map<string, number>, out: Graph[]): number {
|
|
80
|
+
if (g !== g.root) {
|
|
81
|
+
const gid = lbl++;
|
|
82
|
+
out.push(g);
|
|
83
|
+
if (isACluster(g)) insertCluster(map, g.name, gid);
|
|
84
|
+
}
|
|
85
|
+
for (const [, sg] of g.subgraphs) lbl = labelSubgs(sg, lbl, map, out);
|
|
86
|
+
return lbl;
|
|
87
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* The json writers' serialization step. C prints every draw-op number with
|
|
4
|
+
* `%.03f`, and on the oracle platform (macOS libc) that prints a non-finite
|
|
5
|
+
* double as the bare token `inf`, `-inf` or `nan` (a negative NaN prints
|
|
6
|
+
* `nan` too). `JSON.stringify` would print `null`, so the tree is serialized
|
|
7
|
+
* with those tokens instead. A document that holds one is not valid JSON,
|
|
8
|
+
* exactly as native's is not; a tree with no non-finite number serializes
|
|
9
|
+
* byte for byte as `JSON.stringify(tree, null, 2)`.
|
|
10
|
+
* @see plugin/core/gvrender_core_json.c:write_polyline (%.03f)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const JSON_INDENT = 2;
|
|
14
|
+
|
|
15
|
+
const INF_TOKEN = 'inf';
|
|
16
|
+
const NEG_INF_TOKEN = '-inf';
|
|
17
|
+
const NAN_TOKEN = 'nan';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Brackets a token while it travels through JSON.stringify as a string. A C
|
|
21
|
+
* string cannot hold NUL, so no attribute value cgraph could store spells a
|
|
22
|
+
* bracketed token.
|
|
23
|
+
*/
|
|
24
|
+
const MARK = '\u0000';
|
|
25
|
+
/** MARK as JSON.stringify escapes it. */
|
|
26
|
+
const ESCAPED_MARK = JSON.stringify(MARK).slice(1, -1).replace(/\\/g, '\\\\');
|
|
27
|
+
/** A whole string value that is one bracketed token. */
|
|
28
|
+
const QUOTED_TOKEN = new RegExp(
|
|
29
|
+
`"${ESCAPED_MARK}(${INF_TOKEN}|${NEG_INF_TOKEN}|${NAN_TOKEN})${ESCAPED_MARK}"`, 'g',
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
/** What C's `%.03f` prints for a non-finite double. */
|
|
33
|
+
function nonFiniteToken(v: number): string {
|
|
34
|
+
if (Number.isNaN(v)) return NAN_TOKEN;
|
|
35
|
+
return v > 0 ? INF_TOKEN : NEG_INF_TOKEN;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* `JSON.stringify(tree, null, 2)`, except that a non-finite number prints as
|
|
40
|
+
* C's `%.03f` token.
|
|
41
|
+
* @see plugin/core/gvrender_core_json.c:write_polyline (%.03f)
|
|
42
|
+
*/
|
|
43
|
+
export function stringifyJsonTree(tree: unknown): string {
|
|
44
|
+
let sawNonFinite = false;
|
|
45
|
+
const text = JSON.stringify(tree, (_key, v: unknown) => {
|
|
46
|
+
if (typeof v !== 'number' || Number.isFinite(v)) return v;
|
|
47
|
+
sawNonFinite = true;
|
|
48
|
+
return `${MARK}${nonFiniteToken(v)}${MARK}`;
|
|
49
|
+
}, JSON_INDENT);
|
|
50
|
+
return sawNonFinite ? text.replace(QUOTED_TOKEN, '$1') : text;
|
|
51
|
+
}
|