@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
package/src/render/json.ts
CHANGED
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
* conformance bar is the semantic `compare-json` comparator (±0.01, colors /
|
|
29
29
|
* fonts canonicalized). `stoj`/`ind` are retained as the C-faithful string
|
|
30
30
|
* helpers referenced by the unit tests.
|
|
31
|
+
* A non-finite draw coordinate prints as C's `%.03f` token (`inf`, `-inf`,
|
|
32
|
+
* `nan`), so such a document is not valid JSON, exactly as native's is not.
|
|
31
33
|
*
|
|
32
34
|
* @see plugin/core/gvrender_core_json.c
|
|
33
35
|
*/
|
|
@@ -41,14 +43,16 @@ import type { RendererPlugin } from '../gvc/context.js';
|
|
|
41
43
|
import type { RenderJob } from '../gvc/job.js';
|
|
42
44
|
import type { XdotOp, XdotColor } from '../xdot/types.js';
|
|
43
45
|
import type { TextlabelT, FieldT } from '../common/types.js';
|
|
44
|
-
import {
|
|
45
|
-
import { attachAttrs } from './dot/write-back.js';
|
|
46
|
+
import { gfmt5, XdotRenderer, type XdotDraws } from './dot.js';
|
|
47
|
+
import { attachAttrs, nodeLayoutAttrs } from './dot/write-back.js';
|
|
48
|
+
import { attachBeforeWriteWithClusterEdges as attachFirst, jsonNodes, labelSubgs, lookupCluster } from './json-cluster-edge.js';
|
|
46
49
|
import { attrsWithoutLayoutBb } from '../gvc/layout-bb.js';
|
|
47
50
|
import { CHAR_LATIN1 } from '../common/graph-init.js';
|
|
48
51
|
import { parse } from '../parser/index.js';
|
|
49
52
|
import { render as deviceRender } from '../gvc/device.js';
|
|
50
53
|
import { createDefaultContext } from '../gvc/default-context.js';
|
|
51
54
|
import { parseXDot } from '../xdot/index.js';
|
|
55
|
+
import { stringifyJsonTree } from './json-nonfinite.js';
|
|
52
56
|
import { isHtmlValue, htmlValueContent } from '../common/html-string.js';
|
|
53
57
|
|
|
54
58
|
// ---------------------------------------------------------------------------
|
|
@@ -218,10 +222,10 @@ function canonicalRootEdges(g: Graph): Array<{ e: Edge; key: string }> {
|
|
|
218
222
|
const counter = new Map<string, number>();
|
|
219
223
|
for (const [, n] of g.nodes) {
|
|
220
224
|
for (const e of n.outEdges(g)) {
|
|
221
|
-
const base = e.tail.name + '
|
|
225
|
+
const base = e.tail.name + '\0' + e.head.name;
|
|
222
226
|
const occ = counter.get(base) ?? 0;
|
|
223
227
|
counter.set(base, occ + 1);
|
|
224
|
-
out.push({ e, key: base + '
|
|
228
|
+
out.push({ e, key: base + '\0' + occ });
|
|
225
229
|
}
|
|
226
230
|
}
|
|
227
231
|
return out;
|
|
@@ -262,14 +266,11 @@ function collectDrawLookup(g: Graph): DrawLookup | undefined {
|
|
|
262
266
|
const draws = xr.drawStringsByObject();
|
|
263
267
|
const node = new Map<string, Map<string, string>>();
|
|
264
268
|
for (const [nm, xn] of xg.nodes) node.set(nm, new Map(xn.attrs));
|
|
265
|
-
// Correlate each
|
|
266
|
-
//
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
// subgraphs), so when a g-child has no same-named xg-child at this level it
|
|
271
|
-
// is a filtered non-cluster (or a cluster nested under one): descend g but
|
|
272
|
-
// hold the xg position, matching xg's flattened cluster tree (1436).
|
|
269
|
+
// Correlate each cluster with its xdot-re-render attrs by a PARALLEL walk
|
|
270
|
+
// keyed on the cluster OBJECT, not its name: a bare-name Map collides when two
|
|
271
|
+
// clusters share a name at different depths (graphs-b7). xg holds only the
|
|
272
|
+
// drawable clusters, so a g-child with no same-named xg-child here is a
|
|
273
|
+
// filtered non-cluster (or nested under one): descend g, hold xg (1436).
|
|
273
274
|
const cluster = new Map<Graph, Map<string, string>>();
|
|
274
275
|
const correlate = (gSub: Graph, xgSub: Graph): void => {
|
|
275
276
|
for (const [nm, gChild] of gSub.subgraphs) {
|
|
@@ -298,7 +299,7 @@ function collectDrawLookup(g: Graph): DrawLookup | undefined {
|
|
|
298
299
|
for (const [nm, gn] of g.nodes) applyDraws(node.get(nm), draws.get(gn));
|
|
299
300
|
for (const [gc, attrs] of cluster) applyDraws(attrs, draws.get(gc));
|
|
300
301
|
for (const [ge, attrs] of edge) applyDraws(attrs, draws.get(ge));
|
|
301
|
-
return { graph: graphAttrs, node, cluster, edge, attach: () => xr.attach(g) };
|
|
302
|
+
return { graph: graphAttrs, node, cluster, edge, attach: attachFirst(g, () => xr.attach(g)) };
|
|
302
303
|
}
|
|
303
304
|
|
|
304
305
|
// ---------------------------------------------------------------------------
|
|
@@ -312,7 +313,7 @@ interface BuildCtx {
|
|
|
312
313
|
subgGid: Map<Graph, number>;
|
|
313
314
|
nodeGid: Map<Node, number>;
|
|
314
315
|
edgeGid: Map<Edge, number>;
|
|
315
|
-
/**
|
|
316
|
+
/** The subgraphs below the rendered graph in DFS preorder (the flat top-level "objects" ordering). */
|
|
316
317
|
dfsSubs: Graph[];
|
|
317
318
|
/** True once any edge (or edge default) declares `label`: cgraph then carries
|
|
318
319
|
* a global `label` edge symbol, and write_attrs emits `label` (its value or
|
|
@@ -360,26 +361,19 @@ function isEdgeLabelDeclared(g: Graph): boolean {
|
|
|
360
361
|
return walk(g);
|
|
361
362
|
}
|
|
362
363
|
|
|
363
|
-
/** DFS preorder over the subgraph tree. @see label_subgs recursion order */
|
|
364
|
-
function dfsSubgraphs(g: Graph, out: Graph[]): void {
|
|
365
|
-
for (const [, sg] of g.subgraphs) {
|
|
366
|
-
out.push(sg);
|
|
367
|
-
dfsSubgraphs(sg, out);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
364
|
/** Assign every object its `_gvid`, mirroring write_graph's top-level pass:
|
|
372
365
|
* subgraphs DFS-preorder 0..sgcnt-1, then nodes sgcnt.., then edges 0.. in
|
|
373
366
|
* agfstnode/agfstout order. @see plugin/core/gvrender_core_json.c:write_graph */
|
|
374
367
|
function assignIds(g: Graph, doXDot: boolean, draws?: DrawLookup): BuildCtx {
|
|
375
368
|
const dfsSubs: Graph[] = [];
|
|
376
|
-
|
|
369
|
+
const clusters = new Map<string, number>();
|
|
370
|
+
const sgcnt = labelSubgs(g, 0, clusters, dfsSubs);
|
|
377
371
|
const subgGid = new Map<Graph, number>();
|
|
378
372
|
dfsSubs.forEach((sg, i) => subgGid.set(sg, i));
|
|
379
|
-
|
|
373
|
+
if (dfsSubs[0] === g) dfsSubs.shift(); // a subgraph render: label_subgs numbers g, write_subgs lists below it
|
|
380
374
|
const nodeGid = new Map<Node, number>();
|
|
381
375
|
let ncnt = 0;
|
|
382
|
-
for (const [, n] of g.nodes) nodeGid.set(n, sgcnt + ncnt++);
|
|
376
|
+
for (const [, n] of g.nodes) nodeGid.set(n, n.info.clustnode ? lookupCluster(clusters, n.name) : sgcnt + ncnt++);
|
|
383
377
|
const edgeGid = new Map<Edge, number>();
|
|
384
378
|
let ecnt = 0;
|
|
385
379
|
for (const { e } of canonicalRootEdges(g)) edgeGid.set(e, ecnt++);
|
|
@@ -399,9 +393,9 @@ function assignIds(g: Graph, doXDot: boolean, draws?: DrawLookup): BuildCtx {
|
|
|
399
393
|
* Emit an object's attributes into `o`, mirroring C's write_attrs: `agxget`
|
|
400
394
|
* returns HTML-string content with the marker stripped, empty non-`label`
|
|
401
395
|
* values are skipped, and xdot draw attrs are re-serialized as op arrays.
|
|
402
|
-
* `base`
|
|
403
|
-
*
|
|
404
|
-
* on conflict.
|
|
396
|
+
* `base` is the object's own attrs: user/inherited values and the draw strings the input or an
|
|
397
|
+
* earlier render stored, which an object this render does not draw prints as agxget would.
|
|
398
|
+
* `overlay` (the xdot re-render: `pos`/`width`/`height`/`bb`/draw strings) wins on conflict.
|
|
405
399
|
*/
|
|
406
400
|
function emitAttrs(
|
|
407
401
|
o: JObj,
|
|
@@ -524,11 +518,10 @@ function buildNodeObject(n: Node, ctx: BuildCtx): JObj {
|
|
|
524
518
|
const eff = nodeEffectiveAttrs(n);
|
|
525
519
|
const xd = ctx.draws?.node.get(n.name);
|
|
526
520
|
if (xd === undefined) {
|
|
527
|
-
// Fallback (json0 / hand-built graph):
|
|
528
|
-
//
|
|
529
|
-
|
|
530
|
-
eff.set('
|
|
531
|
-
eff.set('height', printNum(n.info.height));
|
|
521
|
+
// Fallback (json0 / hand-built graph): no xdot render supplies the layout
|
|
522
|
+
// attrs, so take the strings attach_attrs writes (output.c:296-309).
|
|
523
|
+
const layout = nodeLayoutAttrs(n);
|
|
524
|
+
eff.set('pos', layout.pos).set('width', layout.width).set('height', layout.height);
|
|
532
525
|
}
|
|
533
526
|
emitAttrs(o, eff, xd, ctx);
|
|
534
527
|
attachNodeRects(o, n);
|
|
@@ -607,10 +600,10 @@ function buildGraphObject(g: Graph, top: boolean, ctx: BuildCtx): JObj {
|
|
|
607
600
|
if (top) {
|
|
608
601
|
// "objects": all subgraphs (DFS preorder) then all node objects. Emitted
|
|
609
602
|
// only when the graph has any subgraph or node. @see write_subgs/write_nodes
|
|
610
|
-
if (ctx.dfsSubs.length > 0 || g.
|
|
603
|
+
if (ctx.dfsSubs.length > 0 || jsonNodes(g).length > 0) {
|
|
611
604
|
const objects: JObj[] = [];
|
|
612
605
|
for (const sg of ctx.dfsSubs) objects.push(buildGraphObject(sg, false, ctx));
|
|
613
|
-
for (const
|
|
606
|
+
for (const n of jsonNodes(g)) objects.push(buildNodeObject(n, ctx));
|
|
614
607
|
o.objects = objects;
|
|
615
608
|
}
|
|
616
609
|
} else {
|
|
@@ -619,10 +612,7 @@ function buildGraphObject(g: Graph, top: boolean, ctx: BuildCtx): JObj {
|
|
|
619
612
|
for (const [, sg] of g.subgraphs) childGids.push(ctx.subgGid.get(sg)!);
|
|
620
613
|
if (childGids.length > 0) o.subgraphs = childGids;
|
|
621
614
|
const nodeGids: number[] = [];
|
|
622
|
-
for (const
|
|
623
|
-
const gid = ctx.nodeGid.get(n);
|
|
624
|
-
if (gid !== undefined) nodeGids.push(gid);
|
|
625
|
-
}
|
|
615
|
+
for (const n of jsonNodes(g)) nodeGids.push(ctx.nodeGid.get(n)!);
|
|
626
616
|
if (nodeGids.length > 0) o.nodes = nodeGids;
|
|
627
617
|
const eGids = memberEdgeGids(g, ctx);
|
|
628
618
|
if (eGids.length > 0) o.edges = eGids;
|
|
@@ -658,14 +648,15 @@ function latin1Reencode(s: string): string {
|
|
|
658
648
|
export function buildJson(g: Graph, doXDot: boolean, preDraws?: DrawLookup): string {
|
|
659
649
|
const draws = doXDot ? (preDraws ?? collectDrawLookup(g)) : undefined;
|
|
660
650
|
const ctx = assignIds(g, doXDot, draws);
|
|
661
|
-
|
|
662
|
-
return JSON.stringify(root, null, 2) + '\n';
|
|
651
|
+
return stringifyJsonTree(buildGraphObject(g, true, ctx)) + '\n';
|
|
663
652
|
}
|
|
664
653
|
|
|
665
654
|
/** Everything the JSON writers leave to the graph walk: they write once, at the end. */
|
|
666
655
|
abstract class JsonRendererBase {
|
|
667
656
|
/** @see plugin/core/gvplugin_core.c registration table */
|
|
668
657
|
readonly quality = 0;
|
|
658
|
+
/** @see plugin/core/gvrender_core_json.c:745 render_features_json (default pad) */
|
|
659
|
+
readonly defaultPad = 0;
|
|
669
660
|
beginNode(_n: Node, _job: RenderJob): void { /* no-op */ }
|
|
670
661
|
endNode(_n: Node, _job: RenderJob): void { /* no-op */ }
|
|
671
662
|
beginEdge(_e: Edge, _job: RenderJob): void { /* no-op */ }
|
|
@@ -692,7 +683,10 @@ export class Json0Renderer extends JsonRendererBase implements RendererPlugin {
|
|
|
692
683
|
beginGraph(_g: Graph, _job: RenderJob): void { /* no-op */ }
|
|
693
684
|
|
|
694
685
|
/** C's json_begin_graph runs attach_attrs; buildJson overlays the same values. */
|
|
695
|
-
endGraph(g: Graph, job: RenderJob): void {
|
|
686
|
+
endGraph(g: Graph, job: RenderJob): void {
|
|
687
|
+
const later = attachFirst(g, () => attachAttrs(g));
|
|
688
|
+
job.write(buildJson(g, false)); later();
|
|
689
|
+
}
|
|
696
690
|
}
|
|
697
691
|
|
|
698
692
|
// ---------------------------------------------------------------------------
|
package/src/render/map.ts
CHANGED
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
import { initJobViewportZoom, parseDrawingSize } from '../gvc/viewport.js';
|
|
38
38
|
import { escapeXml } from './svg-helpers.js';
|
|
39
39
|
import { escapeXmlTitle } from './xml-escape.js';
|
|
40
|
+
import { maxOutputline } from './dot/agwrite-linelength.js';
|
|
40
41
|
|
|
41
42
|
// ---------------------------------------------------------------------------
|
|
42
43
|
// Avoid Lizard quote-tracker bug: never put " in string literals.
|
|
@@ -145,11 +146,6 @@ export function plainCoord(v: number): string {
|
|
|
145
146
|
/** must agree with scan.l @see lib/cgraph/write.c:120 tokenlist */
|
|
146
147
|
const CANON_KEYWORDS = ['node', 'edge', 'strict', 'graph', 'digraph', 'subgraph'];
|
|
147
148
|
|
|
148
|
-
/** Line-break threshold. agwrite may override via `linelength` but always
|
|
149
|
-
* restores this value, so the plain path (direct agstrcanon, no agwrite)
|
|
150
|
-
* observes the default. @see lib/cgraph/write.c:44,676,692 */
|
|
151
|
-
const MAX_OUTPUTLINE = 128;
|
|
152
|
-
|
|
153
149
|
function isDigitByte(b: number): boolean { return b >= 0x30 && b <= 0x39; }
|
|
154
150
|
|
|
155
151
|
function isAlnumByte(b: number): boolean {
|
|
@@ -228,7 +224,7 @@ export function agstrcanonText(arg: string): string {
|
|
|
228
224
|
const next = bytes[i + 1];
|
|
229
225
|
// Long-string line breaking: only after a non-id, non-backslash output
|
|
230
226
|
// char where the next input char is an id char. @see write.c:170-190
|
|
231
|
-
if (next !== undefined) {
|
|
227
|
+
if (next !== undefined && maxOutputline()) {
|
|
232
228
|
const last = out[out.length - 1]!;
|
|
233
229
|
const canBreak = !(isIdCharByte(last) || last === 0x5c) && isIdCharByte(next);
|
|
234
230
|
if (backslashPending && canBreak) {
|
|
@@ -236,7 +232,7 @@ export function agstrcanonText(arg: string): string {
|
|
|
236
232
|
needsQuotes = true;
|
|
237
233
|
backslashPending = false;
|
|
238
234
|
cnt = 0;
|
|
239
|
-
} else if (cnt >=
|
|
235
|
+
} else if (cnt >= maxOutputline()) {
|
|
240
236
|
if (canBreak) {
|
|
241
237
|
out.push(0x5c, 0x0a);
|
|
242
238
|
needsQuotes = true;
|
|
@@ -544,6 +540,7 @@ export function mapOutputImap(
|
|
|
544
540
|
export class PlainRenderer implements RendererPlugin {
|
|
545
541
|
readonly type = 'plain';
|
|
546
542
|
readonly quality = 0;
|
|
543
|
+
readonly defaultPad = 0; // @see plugin/core/gvrender_core_dot.c:734 render_features_dot
|
|
547
544
|
|
|
548
545
|
beginGraph(_g: Graph, _job: RenderJob): void { /* no-op */ }
|
|
549
546
|
endGraph(g: Graph, job: RenderJob): void { writePlain(g, job, false); }
|
|
@@ -566,6 +563,7 @@ export class PlainRenderer implements RendererPlugin {
|
|
|
566
563
|
export class PlainExtRenderer implements RendererPlugin {
|
|
567
564
|
readonly type = 'plain-ext';
|
|
568
565
|
readonly quality = 0;
|
|
566
|
+
readonly defaultPad = 0; // @see plugin/core/gvrender_core_dot.c:734 render_features_dot
|
|
569
567
|
|
|
570
568
|
beginGraph(_g: Graph, _job: RenderJob): void { /* no-op */ }
|
|
571
569
|
endGraph(g: Graph, job: RenderJob): void { writePlain(g, job, true); }
|
|
@@ -641,6 +639,7 @@ function anchorOf(url: string | null, tooltip: string | null, target: string | n
|
|
|
641
639
|
abstract class MapRendererBase implements RendererPlugin {
|
|
642
640
|
abstract readonly type: string;
|
|
643
641
|
readonly quality = 0;
|
|
642
|
+
readonly defaultPad = 4; // @see plugin/core/gvrender_core_map.c:244 render_features_map
|
|
644
643
|
protected abstract readonly isCmapx: boolean;
|
|
645
644
|
protected abstract readonly mapPolygon: boolean;
|
|
646
645
|
private mapCtx: MapCtx | null = null;
|
package/src/render/svg-graph.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { createObjState, ObjType } from '../gvc/job.js';
|
|
|
18
18
|
import { FillType } from '../gvc/context.js';
|
|
19
19
|
import { escapeXml, escapeXmlTitle, svgComment } from './svg-helpers.js';
|
|
20
20
|
import { resolveRenderColor, colorPaint } from './color-resolve.js';
|
|
21
|
-
import {
|
|
21
|
+
import { svgGraphClass, svgGraphId } from './svg-id.js';
|
|
22
22
|
import { parseGradientSpec } from '../common/htmltable-emit-fill.js';
|
|
23
23
|
import { findStopColor, parseStyleFlags } from '../common/style-resolve.js';
|
|
24
24
|
import { croundNarrow32 } from '../common/arith.js';
|
|
@@ -259,16 +259,17 @@ function writeGradientPolygon(clip: Box, id: string, job: RenderJob): void {
|
|
|
259
259
|
* Emit the gradient background for a graph with a two-color bgcolor.
|
|
260
260
|
* Pushes a temporary ObjState, emits the <defs> gradient, writes the polygon.
|
|
261
261
|
*
|
|
262
|
-
* The gradient id is prefixed
|
|
263
|
-
* when obj->id is set: graph0_l_0 / graph0_r_0
|
|
262
|
+
* The gradient id is prefixed with the graph's object id to match C's
|
|
263
|
+
* svg_gradstyle behaviour when obj->id is set: graph0_l_0 / graph0_r_0 for a
|
|
264
|
+
* root, graph<AGSEQ>_l_0 for a subgraph render.
|
|
264
265
|
*
|
|
265
266
|
* @see lib/common/emit.c:emit_background:1503-1519
|
|
266
267
|
* @see plugin/core/gvrender_core_svg.c:svg_gradstyle:572
|
|
267
268
|
* @see plugin/core/gvrender_core_svg.c:svg_rgradstyle:608
|
|
268
269
|
*/
|
|
269
|
-
function emitGradientBackground(clip: Box, spec: BgGradientSpec, job: RenderJob): void {
|
|
270
|
+
function emitGradientBackground(g: Graph, clip: Box, spec: BgGradientSpec, job: RenderJob): void {
|
|
270
271
|
const obj = createObjState(ObjType.RootGraph);
|
|
271
|
-
obj.id =
|
|
272
|
+
obj.id = svgGraphId(g, job);
|
|
272
273
|
obj.fillColor = resolveRenderColor(spec.fillColor);
|
|
273
274
|
obj.stopColor = resolveRenderColor(spec.stopColor);
|
|
274
275
|
obj.gradientFrac = spec.frac;
|
|
@@ -277,10 +278,10 @@ function emitGradientBackground(clip: Box, spec: BgGradientSpec, job: RenderJob)
|
|
|
277
278
|
job.pushObj(obj);
|
|
278
279
|
let gid: string;
|
|
279
280
|
if (spec.radial) {
|
|
280
|
-
gid = gradientId(
|
|
281
|
+
gid = gradientId(obj.id, 'r', job.radialGradId++);
|
|
281
282
|
emitRadialGradient(job, gid);
|
|
282
283
|
} else {
|
|
283
|
-
gid = gradientId(
|
|
284
|
+
gid = gradientId(obj.id, 'l', job.linearGradId++);
|
|
284
285
|
emitLinearGradient(job, bgGradientPts(clip), gid);
|
|
285
286
|
}
|
|
286
287
|
job.popObj();
|
|
@@ -313,7 +314,7 @@ function emitBgcolorBackground(g: Graph, clip: Box, bgcolorAttr: string | undefi
|
|
|
313
314
|
}
|
|
314
315
|
const spec = resolveBgGradientSpec(g, bgcolorAttr);
|
|
315
316
|
if (spec !== null) {
|
|
316
|
-
emitGradientBackground(clip, spec, job);
|
|
317
|
+
emitGradientBackground(g, clip, spec, job);
|
|
317
318
|
return;
|
|
318
319
|
}
|
|
319
320
|
emitGraphBackground(clip, resolveGraphBgcolor(bgcolorAttr), job);
|
package/src/render/svg-id.ts
CHANGED
|
@@ -50,9 +50,18 @@ export function svgClusterId(sg: Graph, job: RenderJob): string {
|
|
|
50
50
|
return job.objId(sg.attrs.get('id'), 'clust' + sg.seq);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Object id of the graph being emitted, as getObjId builds it for an AGRAPH:
|
|
55
|
+
* the DOT `id` attr wins; else `graph<AGSEQ>`. The graph being emitted is
|
|
56
|
+
* job->gvc->g, i.e. `root == obj`, so neither the `<gid>_` prefix nor the
|
|
57
|
+
* "clust" prefix applies. The root's AGSEQ is 0; a subgraph rendered after
|
|
58
|
+
* its own layout carries its creation sequence among subgraphs. It names the
|
|
59
|
+
* top group, the gradients, the graph anchor and map area (obj->id) and the
|
|
60
|
+
* base of the graph label's HTML anchor ids.
|
|
61
|
+
* @see lib/common/emit.c:getObjId
|
|
62
|
+
*/
|
|
54
63
|
export function svgGraphId(g: Graph, job: RenderJob): string {
|
|
55
|
-
return job.objId(g.attrs.get('id'), '
|
|
64
|
+
return job.objId(g.attrs.get('id'), 'graph' + g.seq, true);
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
/** `class="node[ <class>]"`, reading the inherited DOT `class` attribute. */
|
package/src/render/svg.ts
CHANGED
|
@@ -53,6 +53,7 @@ import {
|
|
|
53
53
|
emitParallelEdgePaths,
|
|
54
54
|
escapeXml,
|
|
55
55
|
emitPoints,
|
|
56
|
+
SVG_PAD,
|
|
56
57
|
} from './svg-helpers.js';
|
|
57
58
|
import { emitArrowOps } from './svg-arrow-ops.js';
|
|
58
59
|
import { resolveRenderColor, colorOpacity, colorPaint } from './color-resolve.js';
|
|
@@ -93,6 +94,8 @@ export class SvgRenderer implements RendererPlugin {
|
|
|
93
94
|
readonly type = 'svg';
|
|
94
95
|
/** @see plugin/core/gvplugin_core.c registration table */
|
|
95
96
|
readonly quality = 0;
|
|
97
|
+
/** @see plugin/core/gvrender_core_svg.c:810 render_features_svg (default pad) */
|
|
98
|
+
readonly defaultPad = SVG_PAD;
|
|
96
99
|
/** Whether the current edge's <g> group was opened (deferred-emit state). */
|
|
97
100
|
private edgeGroupOpen = false;
|
|
98
101
|
|
|
@@ -107,16 +110,16 @@ export class SvgRenderer implements RendererPlugin {
|
|
|
107
110
|
beginNode(n: Node, job: RenderJob): void { svgBeginNode(n, job); }
|
|
108
111
|
endNode(_n: Node, job: RenderJob): void { svgEndNode(job); }
|
|
109
112
|
beginEdge(e: Edge, job: RenderJob): void {
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
this.edgeGroupOpen = edgeHasDrawableContent(e, job.
|
|
113
|
+
// emit_edge gates all edge drawing on edge_in_box(e, job->clip).
|
|
114
|
+
// @see lib/common/emit.c:emit_edge
|
|
115
|
+
this.edgeGroupOpen = edgeHasDrawableContent(e, job.clip);
|
|
113
116
|
if (this.edgeGroupOpen) svgBeginEdge(e, job);
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
/**
|
|
117
120
|
* Close the edge group. Edge graphics (path + arrowhead) are emitted here
|
|
118
121
|
* because the full spline is available on e.info.spl only after layout has
|
|
119
|
-
*
|
|
122
|
+
* routed the edges.
|
|
120
123
|
*
|
|
121
124
|
* For multi-color edges (color="c1:c2:…"), emits parallel offset Béziers
|
|
122
125
|
* (one per color, SEP=2.0 apart) then arrows in headcolor/tailcolor.
|
package/src/util/bsd-qsort.ts
CHANGED
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
* (`lib/util/list.c:gv_list_sort_`). libc `qsort` is **not stable**: it reorders
|
|
9
9
|
* comparator-equal elements in an implementation-defined permutation. Most port
|
|
10
10
|
* sort sites can use JS's stable `Array.prototype.sort` because their comparator
|
|
11
|
-
* never reports a tie (e.g. `
|
|
11
|
+
* never reports a tie (e.g. `routeCurvedGroup`'s sort of distinct originals by
|
|
12
|
+
* creation seq, `splines-groups.ts`). Where ties DO occur AND the
|
|
12
13
|
* downstream result depends on their order, a stable sort diverges from the C
|
|
13
14
|
* oracle.
|
|
14
15
|
*
|
package/src/util/printf-round.ts
CHANGED
|
@@ -80,6 +80,17 @@ function roundHalfEvenDrop(value: bigint, dropCount: number): bigint {
|
|
|
80
80
|
return q;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* What C's printf prints for a non-finite double under `%f`, `%g` and `%e`:
|
|
85
|
+
* `inf`, `-inf` or `nan`. The reference libc (macOS) prints no sign on a NaN
|
|
86
|
+
* whose sign bit is set.
|
|
87
|
+
* @see lib/gvc/gvdevice.c:gvprintdouble · lib/common/output.c:71 ("%.5g")
|
|
88
|
+
*/
|
|
89
|
+
function printfNonFinite(v: number): string {
|
|
90
|
+
if (Number.isNaN(v)) return 'nan';
|
|
91
|
+
return v > 0 ? 'inf' : '-inf';
|
|
92
|
+
}
|
|
93
|
+
|
|
83
94
|
/**
|
|
84
95
|
* Format `v` as C's `%.<decimals>f` would (fixed decimal places, rounded
|
|
85
96
|
* half-to-even on exact ties). Mirrors `Number.prototype.toFixed(decimals)`
|
|
@@ -89,11 +100,8 @@ function roundHalfEvenDrop(value: bigint, dropCount: number): bigint {
|
|
|
89
100
|
*/
|
|
90
101
|
export function printfFixed(v: number, decimals: number): string {
|
|
91
102
|
// Non-finite: the exact-decimal decomposition below reads the IEEE-754
|
|
92
|
-
// bit pattern as a finite mantissa, so
|
|
93
|
-
|
|
94
|
-
// it returns "Infinity"/"-Infinity"/"NaN" verbatim (canonicalized to the
|
|
95
|
-
// oracle's C printf "inf"/"nan" by the xdot comparator).
|
|
96
|
-
if (!Number.isFinite(v)) return v.toFixed(decimals);
|
|
103
|
+
// bit pattern as a finite mantissa, so print C's text instead.
|
|
104
|
+
if (!Number.isFinite(v)) return printfNonFinite(v);
|
|
97
105
|
if (v === 0) return decimals > 0 ? '0.' + '0'.repeat(decimals) : '0';
|
|
98
106
|
const sign = v < 0 ? '-' : '';
|
|
99
107
|
const { digits, exponent10 } = exactDecimalDigits(Math.abs(v));
|
|
@@ -124,11 +132,9 @@ export function printfFixed(v: number, decimals: number): string {
|
|
|
124
132
|
* @see lib/common/output.c:71 (agxbprint "%.5g")
|
|
125
133
|
*/
|
|
126
134
|
export function printfSig(v: number, sig: number): string {
|
|
127
|
-
// Non-finite:
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
// but the contract must hold for every caller.)
|
|
131
|
-
if (!Number.isFinite(v)) return v.toPrecision(sig);
|
|
135
|
+
// Non-finite: the bit-decomposition below is only valid for finite
|
|
136
|
+
// magnitudes, so print C's text instead.
|
|
137
|
+
if (!Number.isFinite(v)) return printfNonFinite(v);
|
|
132
138
|
if (v === 0) return sig > 1 ? '0.' + '0'.repeat(sig - 1) : '0';
|
|
133
139
|
const sign = v < 0 ? '-' : '';
|
|
134
140
|
const { digits, exponent10 } = exactDecimalDigits(Math.abs(v));
|
package/src/xdot/parse.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
XdotRadialGrad, XdotColorStop, OpFunctions,
|
|
13
13
|
} from "./types.js";
|
|
14
14
|
import { XDOT_PARSE_ERROR } from "./types.js";
|
|
15
|
+
import { isSpace, scanStrtod } from "./strtod.js";
|
|
15
16
|
|
|
16
17
|
/** ULONG_MAX on an LP64 target: strtoul saturation value. */
|
|
17
18
|
const ULONG_MAX = 0xffff_ffff_ffff_ffffn;
|
|
@@ -21,8 +22,9 @@ type OpResult = { op: XdotOp; pos: number } | "error";
|
|
|
21
22
|
type OpMaybeResult = OpResult | null;
|
|
22
23
|
|
|
23
24
|
class XdotParser {
|
|
25
|
+
/** @see lib/util/gv_ctype.h:gv_isspace */
|
|
24
26
|
static isWs(c: string): boolean {
|
|
25
|
-
return c
|
|
27
|
+
return isSpace(c);
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
static skipWs(s: string, pos: number): number {
|
|
@@ -35,25 +37,13 @@ class XdotParser {
|
|
|
35
37
|
return i;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
return i;
|
|
45
|
-
}
|
|
46
|
-
|
|
40
|
+
/**
|
|
41
|
+
* C reads every xdot double with strtod, so a number may be a decimal, a
|
|
42
|
+
* hex float, inf/infinity or nan/nan(...); null is C's `p == s`.
|
|
43
|
+
* @see lib/xdot/xdot.c:parseReal
|
|
44
|
+
*/
|
|
47
45
|
static parseReal(s: string, pos: number): PR<number> {
|
|
48
|
-
|
|
49
|
-
const start = i;
|
|
50
|
-
if (i < s.length && (s[i] === "-" || s[i] === "+")) i++;
|
|
51
|
-
const digStart = i;
|
|
52
|
-
i = XdotParser.consumeDigits(s, i);
|
|
53
|
-
if (i < s.length && s[i] === ".") { i++; i = XdotParser.consumeDigits(s, i); }
|
|
54
|
-
i = XdotParser.consumeExponent(s, i);
|
|
55
|
-
if (i === start || i === digStart) return null;
|
|
56
|
-
return { val: parseFloat(s.slice(start, i)), pos: i };
|
|
46
|
+
return scanStrtod(s, pos);
|
|
57
47
|
}
|
|
58
48
|
|
|
59
49
|
static parseInt_(s: string, pos: number): PR<number> {
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// SPDX-License-Identifier: EPL-2.0
|
|
2
|
+
/**
|
|
3
|
+
* The number scan C's xdot parser gets from libc strtod. parseReal, parseRect
|
|
4
|
+
* and parsePolyline all read their doubles with strtod, so an xdot string may
|
|
5
|
+
* hold anything strtod accepts: a decimal, a hexadecimal float, `inf` or
|
|
6
|
+
* `infinity`, and `nan` or `nan(...)`. The grammar is the oracle platform's
|
|
7
|
+
* (macOS libc), which takes any characters between `nan(` and the first `)`.
|
|
8
|
+
* @see lib/xdot/xdot.c:parseReal
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** A scanned number and the offset just past it, or null when none starts here. */
|
|
12
|
+
export type Scan = { val: number; pos: number } | null;
|
|
13
|
+
|
|
14
|
+
const INF_WORD = "inf";
|
|
15
|
+
const INF_TAIL = "inity";
|
|
16
|
+
const NAN_WORD = "nan";
|
|
17
|
+
|
|
18
|
+
/** Bits in a double's significand, the smallest and largest binary exponents. */
|
|
19
|
+
const SIGNIFICAND_BITS = 53;
|
|
20
|
+
const MIN_SUBNORMAL_EXP = -1074;
|
|
21
|
+
const MAX_BINARY_EXP = 1023;
|
|
22
|
+
const MIN_NORMAL_EXP = -1022;
|
|
23
|
+
const EXP_BIAS = 1023;
|
|
24
|
+
/** 2^52: an exponent field sits above the 52 fraction bits. */
|
|
25
|
+
const FRACTION_SCALE = 2n ** 52n;
|
|
26
|
+
const HEX_DIGIT_BITS = 4n;
|
|
27
|
+
|
|
28
|
+
/** @see lib/util/gv_ctype.h:gv_isspace */
|
|
29
|
+
export function isSpace(c: string): boolean {
|
|
30
|
+
return c === " " || (c >= "\t" && c <= "\r");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isDecDigit(c: string): boolean {
|
|
34
|
+
return c >= "0" && c <= "9";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isHexDigit(c: string): boolean {
|
|
38
|
+
return isDecDigit(c) || (c >= "a" && c <= "f") || (c >= "A" && c <= "F");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function skipWhile(s: string, i: number, pred: (c: string) => boolean): number {
|
|
42
|
+
while (i < s.length && pred(s[i])) i++;
|
|
43
|
+
return i;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function matchesWord(s: string, i: number, word: string): boolean {
|
|
47
|
+
return s.slice(i, i + word.length).toLowerCase() === word;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The end of an exponent starting at `i`, or `i` when the marker is missing
|
|
52
|
+
* or no digit follows it: strtod("1e+") is 1 and stops at the `e`.
|
|
53
|
+
*/
|
|
54
|
+
function exponentEnd(s: string, i: number, marker: string): number {
|
|
55
|
+
if (i >= s.length || s[i].toLowerCase() !== marker) return i;
|
|
56
|
+
let j = i + 1;
|
|
57
|
+
if (s[j] === "+" || s[j] === "-") j++;
|
|
58
|
+
const end = skipWhile(s, j, isDecDigit);
|
|
59
|
+
return end === j ? i : end;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 2^q as a double, exact for every q a double can hold (-1074..1023). */
|
|
63
|
+
function pow2(q: number): number {
|
|
64
|
+
let bits = BigInt(q + EXP_BIAS) * FRACTION_SCALE;
|
|
65
|
+
if (q < MIN_NORMAL_EXP) bits = 2n ** BigInt(q - MIN_SUBNORMAL_EXP);
|
|
66
|
+
return new Float64Array(new BigUint64Array([bits]).buffer)[0];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** m / 2^shift rounded to nearest, ties to even; exact when shift <= 0. */
|
|
70
|
+
function shiftRoundHalfEven(m: bigint, shift: bigint): bigint {
|
|
71
|
+
if (shift <= 0n) return m << -shift;
|
|
72
|
+
const kept = m >> shift;
|
|
73
|
+
const rem = m - (kept << shift);
|
|
74
|
+
const half = 1n << (shift - 1n);
|
|
75
|
+
const roundUp = rem > half || (rem === half && (kept & 1n) === 1n);
|
|
76
|
+
return roundUp ? kept + 1n : kept;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* m * 2^e2 as the nearest double, ties to even, as strtod rounds a hex float:
|
|
81
|
+
* 53 bits for a normal result, fewer in the subnormal range, infinity above
|
|
82
|
+
* the largest double and zero below half the smallest.
|
|
83
|
+
*/
|
|
84
|
+
function hexToDouble(m: bigint, e2: bigint): number {
|
|
85
|
+
if (m === 0n) return 0;
|
|
86
|
+
// The exponent of the leading bit; bigint, since the text may carry an
|
|
87
|
+
// exponent of any length.
|
|
88
|
+
const top = BigInt(m.toString(2).length - 1) + e2;
|
|
89
|
+
if (top > BigInt(MAX_BINARY_EXP)) return Infinity;
|
|
90
|
+
if (top < BigInt(MIN_SUBNORMAL_EXP - 1)) return 0;
|
|
91
|
+
const q = Math.max(Number(top) - (SIGNIFICAND_BITS - 1), MIN_SUBNORMAL_EXP);
|
|
92
|
+
// A significand rounded up to 2^53 at the top exponent overflows to
|
|
93
|
+
// infinity in the multiplication, as strtod does.
|
|
94
|
+
return Number(shiftRoundHalfEven(m, BigInt(q) - e2)) * pow2(q);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A hexadecimal float whose "0x" starts at `i`: hex digits, an optional `.`
|
|
99
|
+
* and hex digits, an optional `p[sign]digits`. Null when no hex digit follows
|
|
100
|
+
* the prefix; strtod then reads the leading "0" as a decimal.
|
|
101
|
+
*/
|
|
102
|
+
function scanHex(s: string, i: number): Scan {
|
|
103
|
+
const intStart = i + 2;
|
|
104
|
+
const intEnd = skipWhile(s, intStart, isHexDigit);
|
|
105
|
+
const hasDot = s[intEnd] === ".";
|
|
106
|
+
const end = hasDot ? skipWhile(s, intEnd + 1, isHexDigit) : intEnd;
|
|
107
|
+
const digits = s.slice(intStart, intEnd) + (hasDot ? s.slice(intEnd + 1, end) : "");
|
|
108
|
+
if (digits.length === 0) return null;
|
|
109
|
+
const fracDigits = BigInt(hasDot ? end - intEnd - 1 : 0);
|
|
110
|
+
const expEnd = exponentEnd(s, end, "p");
|
|
111
|
+
const exp = expEnd > end ? BigInt(s.slice(end + 1, expEnd)) : 0n;
|
|
112
|
+
const val = hexToDouble(BigInt(`0x${digits}`), exp - HEX_DIGIT_BITS * fracDigits);
|
|
113
|
+
return { val, pos: expEnd };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* A decimal: digits with an optional `.`, at least one digit in all, and an
|
|
118
|
+
* optional `e[sign]digits`. parseFloat rounds the scanned text correctly, as
|
|
119
|
+
* strtod does, and overflows to infinity.
|
|
120
|
+
*/
|
|
121
|
+
function scanDecimal(s: string, i: number): Scan {
|
|
122
|
+
const intEnd = skipWhile(s, i, isDecDigit);
|
|
123
|
+
const hasDot = s[intEnd] === ".";
|
|
124
|
+
const mantEnd = hasDot ? skipWhile(s, intEnd + 1, isDecDigit) : intEnd;
|
|
125
|
+
const digitCount = mantEnd - i - (hasDot ? 1 : 0);
|
|
126
|
+
if (digitCount === 0) return null;
|
|
127
|
+
const end = exponentEnd(s, mantEnd, "e");
|
|
128
|
+
return { val: parseFloat(s.slice(i, end)), pos: end };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* `inf` or `infinity`, and `nan` or `nan(...)`, case-insensitive. The
|
|
133
|
+
* parenthesized part is taken only when a `)` closes it.
|
|
134
|
+
*/
|
|
135
|
+
function scanSpecial(s: string, i: number): Scan {
|
|
136
|
+
if (matchesWord(s, i, INF_WORD)) {
|
|
137
|
+
const tail = i + INF_WORD.length;
|
|
138
|
+
return { val: Infinity, pos: matchesWord(s, tail, INF_TAIL) ? tail + INF_TAIL.length : tail };
|
|
139
|
+
}
|
|
140
|
+
if (!matchesWord(s, i, NAN_WORD)) return null;
|
|
141
|
+
const end = i + NAN_WORD.length;
|
|
142
|
+
const close = s[end] === "(" ? s.indexOf(")", end) : -1;
|
|
143
|
+
return { val: NaN, pos: close === -1 ? end : close + 1 };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function scanMagnitude(s: string, i: number): Scan {
|
|
147
|
+
if (s[i] === "0" && (s[i + 1] === "x" || s[i + 1] === "X")) {
|
|
148
|
+
const hex = scanHex(s, i);
|
|
149
|
+
if (hex !== null) return hex;
|
|
150
|
+
}
|
|
151
|
+
return scanDecimal(s, i) ?? scanSpecial(s, i);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* strtod(s + pos, &end): skips whitespace, takes an optional sign, then the
|
|
156
|
+
* longest number strtod accepts. Null when no conversion is possible, which
|
|
157
|
+
* is C's `end == s`.
|
|
158
|
+
* @see lib/xdot/xdot.c:parseReal
|
|
159
|
+
*/
|
|
160
|
+
export function scanStrtod(s: string, pos: number): Scan {
|
|
161
|
+
let i = skipWhile(s, pos, isSpace);
|
|
162
|
+
const negative = s[i] === "-";
|
|
163
|
+
if (negative || s[i] === "+") i++;
|
|
164
|
+
const mag = scanMagnitude(s, i);
|
|
165
|
+
if (mag === null) return null;
|
|
166
|
+
return { val: negative ? -mag.val : mag.val, pos: mag.pos };
|
|
167
|
+
}
|