@knowvah/dot-engine 1.8.1 → 2.0.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 (181) hide show
  1. package/README.md +232 -30
  2. package/dist/api/builder.d.ts +3 -0
  3. package/dist/api/builder.d.ts.map +1 -1
  4. package/dist/api/edge-ops.d.ts +7 -0
  5. package/dist/api/edge-ops.d.ts.map +1 -1
  6. package/dist/api/geometry.d.ts +5 -2
  7. package/dist/api/geometry.d.ts.map +1 -1
  8. package/dist/api.js +159 -31
  9. package/dist/api.js.map +3 -3
  10. package/dist/async/collect.d.ts +49 -0
  11. package/dist/async/collect.d.ts.map +1 -0
  12. package/dist/async/fonts.d.ts +20 -0
  13. package/dist/async/fonts.d.ts.map +1 -0
  14. package/dist/async/render-async.d.ts +91 -0
  15. package/dist/async/render-async.d.ts.map +1 -0
  16. package/dist/async/render-into.d.ts +38 -0
  17. package/dist/async/render-into.d.ts.map +1 -0
  18. package/dist/async/sanitize.d.ts +28 -0
  19. package/dist/async/sanitize.d.ts.map +1 -0
  20. package/dist/common/css-font.d.ts +9 -0
  21. package/dist/common/css-font.d.ts.map +1 -0
  22. package/dist/common/htmltable-types.d.ts +3 -3
  23. package/dist/common/htmltable-types.d.ts.map +1 -1
  24. package/dist/common/make-label.d.ts.map +1 -1
  25. package/dist/common/poly-shapes.d.ts.map +1 -1
  26. package/dist/common/textmeasure-factory.d.ts +2 -0
  27. package/dist/common/textmeasure-factory.d.ts.map +1 -1
  28. package/dist/common/textmeasure.d.ts +11 -1
  29. package/dist/common/textmeasure.d.ts.map +1 -1
  30. package/dist/common/utils-inputscale.d.ts +19 -0
  31. package/dist/common/utils-inputscale.d.ts.map +1 -0
  32. package/dist/errors.d.ts +61 -5
  33. package/dist/errors.d.ts.map +1 -1
  34. package/dist/gvc/context.d.ts +36 -4
  35. package/dist/gvc/context.d.ts.map +1 -1
  36. package/dist/gvc/device.d.ts +5 -2
  37. package/dist/gvc/device.d.ts.map +1 -1
  38. package/dist/gvc/image-resolver.d.ts +16 -16
  39. package/dist/gvc/image-resolver.d.ts.map +1 -1
  40. package/dist/gvc/job.d.ts +1 -9
  41. package/dist/gvc/job.d.ts.map +1 -1
  42. package/dist/gvc/usershape.d.ts +2 -12
  43. package/dist/gvc/usershape.d.ts.map +1 -1
  44. package/dist/index.d.ts +27 -8
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +7745 -5550
  47. package/dist/index.js.map +4 -4
  48. package/dist/label/index.d.ts.map +1 -1
  49. package/dist/label/node.d.ts +0 -6
  50. package/dist/label/node.d.ts.map +1 -1
  51. package/dist/label/rectangle.d.ts +1 -7
  52. package/dist/label/rectangle.d.ts.map +1 -1
  53. package/dist/layout/circo/circular.d.ts +8 -5
  54. package/dist/layout/circo/circular.d.ts.map +1 -1
  55. package/dist/layout/dot/pack-components.d.ts +0 -19
  56. package/dist/layout/dot/pack-components.d.ts.map +1 -1
  57. package/dist/layout/dot/position.d.ts +7 -2
  58. package/dist/layout/dot/position.d.ts.map +1 -1
  59. package/dist/layout/fdp/derive.d.ts.map +1 -1
  60. package/dist/layout/fdp/index.d.ts.map +1 -1
  61. package/dist/layout/fdp/init.d.ts.map +1 -1
  62. package/dist/layout/fdp/layout.d.ts.map +1 -1
  63. package/dist/layout/fdp/ports.d.ts +0 -10
  64. package/dist/layout/fdp/ports.d.ts.map +1 -1
  65. package/dist/layout/fdp/xlayout.d.ts +0 -16
  66. package/dist/layout/fdp/xlayout.d.ts.map +1 -1
  67. package/dist/layout/neato/adjust-info.d.ts +117 -0
  68. package/dist/layout/neato/adjust-info.d.ts.map +1 -0
  69. package/dist/layout/neato/cdt-surface.d.ts.map +1 -1
  70. package/dist/layout/neato/constraint-adjust.d.ts +40 -0
  71. package/dist/layout/neato/constraint-adjust.d.ts.map +1 -0
  72. package/dist/layout/neato/edge-len.d.ts +20 -0
  73. package/dist/layout/neato/edge-len.d.ts.map +1 -0
  74. package/dist/layout/neato/fdp-adjust.d.ts +32 -4
  75. package/dist/layout/neato/fdp-adjust.d.ts.map +1 -1
  76. package/dist/layout/neato/index.d.ts +9 -6
  77. package/dist/layout/neato/index.d.ts.map +1 -1
  78. package/dist/layout/neato/init.d.ts +4 -11
  79. package/dist/layout/neato/init.d.ts.map +1 -1
  80. package/dist/layout/neato/kk-paths.d.ts +50 -0
  81. package/dist/layout/neato/kk-paths.d.ts.map +1 -0
  82. package/dist/layout/neato/kk-solve.d.ts +14 -0
  83. package/dist/layout/neato/kk-solve.d.ts.map +1 -0
  84. package/dist/layout/neato/kk.d.ts +44 -0
  85. package/dist/layout/neato/kk.d.ts.map +1 -0
  86. package/dist/layout/neato/multispline-router.d.ts.map +1 -1
  87. package/dist/layout/neato/poly.d.ts +50 -0
  88. package/dist/layout/neato/poly.d.ts.map +1 -0
  89. package/dist/layout/neato/sgd-dijkstra.d.ts +20 -0
  90. package/dist/layout/neato/sgd-dijkstra.d.ts.map +1 -0
  91. package/dist/layout/neato/sgd.d.ts +14 -9
  92. package/dist/layout/neato/sgd.d.ts.map +1 -1
  93. package/dist/layout/neato/start.d.ts +51 -0
  94. package/dist/layout/neato/start.d.ts.map +1 -0
  95. package/dist/layout/neato/vpsc-adjust.d.ts +21 -0
  96. package/dist/layout/neato/vpsc-adjust.d.ts.map +1 -0
  97. package/dist/layout/sfdp/index.d.ts.map +1 -1
  98. package/dist/layout/sfdp/init.d.ts +0 -9
  99. package/dist/layout/sfdp/init.d.ts.map +1 -1
  100. package/dist/layout/sfdp/spring-driver.d.ts.map +1 -1
  101. package/dist/layout/twopi/circle.d.ts +0 -7
  102. package/dist/layout/twopi/circle.d.ts.map +1 -1
  103. package/dist/ortho/ortho-parallel.d.ts.map +1 -1
  104. package/dist/ortho/trap-query.d.ts.map +1 -1
  105. package/dist/parser/index.d.ts +9 -5
  106. package/dist/parser/index.d.ts.map +1 -1
  107. package/dist/render/index.d.ts +2 -0
  108. package/dist/render/index.d.ts.map +1 -1
  109. package/dist/render/public.d.ts +9 -3
  110. package/dist/render/public.d.ts.map +1 -1
  111. package/dist/render/xdot-public.d.ts +7 -2
  112. package/dist/render/xdot-public.d.ts.map +1 -1
  113. package/dist/render.js +7548 -5588
  114. package/dist/render.js.map +4 -4
  115. package/dist/util/xml.d.ts.map +1 -1
  116. package/dist/vpsc/Solver.d.ts +1 -0
  117. package/dist/vpsc/Solver.d.ts.map +1 -1
  118. package/package.json +1 -1
  119. package/src/api/builder.ts +73 -5
  120. package/src/api/edge-ops.ts +19 -0
  121. package/src/api/geometry.ts +22 -5
  122. package/src/async/collect.ts +204 -0
  123. package/src/async/fonts.ts +61 -0
  124. package/src/async/render-async.ts +205 -0
  125. package/src/async/render-into.ts +115 -0
  126. package/src/async/sanitize.ts +150 -0
  127. package/src/common/css-font.ts +109 -0
  128. package/src/common/htmltable-types.ts +4 -5
  129. package/src/common/make-label.ts +10 -1
  130. package/src/common/poly-shapes.ts +4 -1
  131. package/src/common/textmeasure-factory.ts +11 -0
  132. package/src/common/textmeasure.ts +41 -8
  133. package/src/common/utils-inputscale.ts +31 -0
  134. package/src/errors.ts +188 -5
  135. package/src/gvc/context.ts +96 -17
  136. package/src/gvc/device.ts +11 -6
  137. package/src/gvc/image-resolver.ts +25 -2
  138. package/src/gvc/job.ts +3 -1
  139. package/src/gvc/usershape.ts +6 -0
  140. package/src/index.ts +59 -43
  141. package/src/label/index.ts +6 -2
  142. package/src/label/node.ts +2 -1
  143. package/src/label/rectangle.ts +4 -2
  144. package/src/layout/circo/circular.ts +9 -6
  145. package/src/layout/dot/pack-components.ts +6 -2
  146. package/src/layout/dot/position.ts +21 -3
  147. package/src/layout/fdp/derive.ts +7 -6
  148. package/src/layout/fdp/index.ts +45 -5
  149. package/src/layout/fdp/init.ts +7 -6
  150. package/src/layout/fdp/layout.ts +2 -1
  151. package/src/layout/fdp/ports.ts +3 -2
  152. package/src/layout/fdp/xlayout.ts +3 -1
  153. package/src/layout/neato/adjust-info.ts +332 -0
  154. package/src/layout/neato/cdt-surface.ts +50 -30
  155. package/src/layout/neato/constraint-adjust.ts +466 -0
  156. package/src/layout/neato/edge-len.ts +36 -0
  157. package/src/layout/neato/fdp-adjust.ts +117 -12
  158. package/src/layout/neato/index.ts +36 -54
  159. package/src/layout/neato/init.ts +21 -36
  160. package/src/layout/neato/kk-paths.ts +146 -0
  161. package/src/layout/neato/kk-solve.ts +64 -0
  162. package/src/layout/neato/kk.ts +322 -0
  163. package/src/layout/neato/multispline-router.ts +4 -3
  164. package/src/layout/neato/poly.ts +493 -0
  165. package/src/layout/neato/sgd-dijkstra.ts +125 -0
  166. package/src/layout/neato/sgd.ts +63 -40
  167. package/src/layout/neato/start.ts +222 -0
  168. package/src/layout/neato/vpsc-adjust.ts +93 -0
  169. package/src/layout/sfdp/index.ts +4 -5
  170. package/src/layout/sfdp/init.ts +40 -4
  171. package/src/layout/sfdp/spring-driver.ts +30 -1
  172. package/src/layout/twopi/circle.ts +2 -1
  173. package/src/ortho/ortho-parallel.ts +2 -1
  174. package/src/ortho/trap-query.ts +2 -1
  175. package/src/parser/index.ts +11 -11
  176. package/src/render/index.ts +5 -0
  177. package/src/render/public.ts +24 -24
  178. package/src/render/svg.ts +1 -1
  179. package/src/render/xdot-public.ts +19 -18
  180. package/src/util/xml.ts +24 -30
  181. package/src/vpsc/Solver.ts +5 -3
package/dist/api.js CHANGED
@@ -474,7 +474,91 @@ var Edge = class _Edge {
474
474
  }
475
475
  };
476
476
 
477
+ // src/errors.ts
478
+ var FRIENDLY_MESSAGES = {
479
+ SYNTAX_ERROR: "There is a syntax error in the DOT source.",
480
+ SYNTAX_UNEXPECTED_EOF: "The DOT source ended unexpectedly \u2014 a bracket or statement may be unclosed.",
481
+ EDGE_OP_DIRECTED_IN_UNDIRECTED: "A directed edge '->' was used in an undirected graph; use '--' instead.",
482
+ EDGE_OP_UNDIRECTED_IN_DIRECTED: "An undirected edge '--' was used in a directed graph; use '->' instead.",
483
+ HTML_PARSE_ERROR: "An HTML-like label could not be parsed.",
484
+ RENDER_ERROR: "The graph could not be laid out or rendered.",
485
+ INTERNAL_ERROR: "dot-engine hit an internal bug while processing the graph. Please report it with the DOT source that triggered it.",
486
+ UNKNOWN_LAYOUT: "The graph names a layout engine that is not available.",
487
+ UNSUPPORTED_FEATURE: "The graph uses a Graphviz feature that dot-engine does not support yet.",
488
+ GENERIC_ERROR: "An unexpected error occurred while rendering the graph."
489
+ };
490
+ function friendlyMessageFor(code) {
491
+ return FRIENDLY_MESSAGES[code];
492
+ }
493
+ var DotEngineError = class extends Error {
494
+ constructor(message, options) {
495
+ super(message, options);
496
+ }
497
+ };
498
+ var InternalError = class extends DotEngineError {
499
+ type = "render";
500
+ code = "INTERNAL_ERROR";
501
+ friendlyMessage = friendlyMessageFor("INTERNAL_ERROR");
502
+ constructor(message, options) {
503
+ super(message, options);
504
+ this.name = "InternalError";
505
+ }
506
+ };
507
+ var UsageTypeError = class extends TypeError {
508
+ code;
509
+ constructor(message, code) {
510
+ super(message);
511
+ this.code = code;
512
+ }
513
+ };
514
+ var UsageStateError = class extends Error {
515
+ code;
516
+ constructor(message, code) {
517
+ super(message);
518
+ this.code = code;
519
+ }
520
+ };
521
+ function describeReceived(v) {
522
+ if (v === null || v === void 0) return String(v);
523
+ switch (typeof v) {
524
+ case "string":
525
+ return `string ${JSON.stringify(v)}`;
526
+ case "number":
527
+ case "boolean":
528
+ case "bigint":
529
+ return `${typeof v} ${String(v)}`;
530
+ case "function":
531
+ return "a function";
532
+ case "symbol":
533
+ return "a symbol";
534
+ default: {
535
+ const ctor = v.constructor;
536
+ return typeof ctor?.name === "string" && ctor.name !== "" ? `an instance of ${ctor.name}` : "an object";
537
+ }
538
+ }
539
+ }
540
+ function invalidArgType(param, expected, actual) {
541
+ return new UsageTypeError(
542
+ `The "${param}" argument must be of type ${expected}. Received ${describeReceived(actual)}`,
543
+ "ERR_INVALID_ARG_TYPE"
544
+ );
545
+ }
546
+ function invalidArgValue(param, value, allowed) {
547
+ return new UsageTypeError(
548
+ `The argument "${param}" is invalid. Received ${JSON.stringify(value)}; allowed: ${allowed.join(", ")}`,
549
+ "ERR_INVALID_ARG_VALUE"
550
+ );
551
+ }
552
+ function invalidState(message) {
553
+ return new UsageStateError(message, "ERR_INVALID_STATE");
554
+ }
555
+
477
556
  // src/api/edge-ops.ts
557
+ function requireObject(param, v) {
558
+ if (typeof v !== "object" || v === null) {
559
+ throw invalidArgType(param, "object", v);
560
+ }
561
+ }
478
562
  function isUndirected(g) {
479
563
  const k = g.root.kind;
480
564
  return k === "undirected" || k === "strict-undirected";
@@ -500,6 +584,12 @@ function insertEdge(g, root, edge) {
500
584
  }
501
585
  }
502
586
  function addEdge(g, tail, head, name) {
587
+ requireObject("g", g);
588
+ requireObject("tail", tail);
589
+ requireObject("head", head);
590
+ if (name !== void 0 && typeof name !== "string") {
591
+ throw invalidArgType("name", "string or undefined", name);
592
+ }
503
593
  const root = g.root;
504
594
  const undirected = isUndirected(g);
505
595
  if (isStrict(g)) {
@@ -511,31 +601,6 @@ function addEdge(g, tail, head, name) {
511
601
  return edge;
512
602
  }
513
603
 
514
- // src/errors.ts
515
- var FRIENDLY_MESSAGES = {
516
- SYNTAX_ERROR: "There is a syntax error in the DOT source.",
517
- SYNTAX_UNEXPECTED_EOF: "The DOT source ended unexpectedly \u2014 a bracket or statement may be unclosed.",
518
- EDGE_OP_DIRECTED_IN_UNDIRECTED: "A directed edge '->' was used in an undirected graph; use '--' instead.",
519
- EDGE_OP_UNDIRECTED_IN_DIRECTED: "An undirected edge '--' was used in a directed graph; use '->' instead.",
520
- HTML_PARSE_ERROR: "An HTML-like label could not be parsed.",
521
- RENDER_ERROR: "The graph could not be laid out or rendered.",
522
- GENERIC_ERROR: "An unexpected error occurred while rendering the graph."
523
- };
524
- function friendlyMessageFor(code) {
525
- return FRIENDLY_MESSAGES[code];
526
- }
527
- var RenderError = class extends Error {
528
- type = "render";
529
- code;
530
- friendlyMessage;
531
- constructor(message, code = "RENDER_ERROR") {
532
- super(message);
533
- this.name = "RenderError";
534
- this.code = code;
535
- this.friendlyMessage = friendlyMessageFor(code);
536
- }
537
- };
538
-
539
604
  // src/common/html-string.ts
540
605
  var HTML_STRING_MARK = "";
541
606
 
@@ -550,12 +615,15 @@ var NodeHandle = class {
550
615
  return this._node.name;
551
616
  }
552
617
  setAttr(k, v) {
618
+ requireKeyValue(k, v);
553
619
  this._node.attrs.set(k, v);
554
620
  }
555
621
  setHtmlAttr(k, v) {
622
+ requireKeyValue(k, v);
556
623
  this._node.attrs.set(k, HTML_STRING_MARK + v);
557
624
  }
558
625
  getAttr(k) {
626
+ requireString("k", k);
559
627
  return this._node.attrs.get(k);
560
628
  }
561
629
  };
@@ -572,20 +640,59 @@ var EdgeHandle = class {
572
640
  return this._edge.head.name;
573
641
  }
574
642
  setAttr(k, v) {
643
+ requireKeyValue(k, v);
575
644
  this._edge.attrs.set(k, v);
576
645
  }
577
646
  setHtmlAttr(k, v) {
647
+ requireKeyValue(k, v);
578
648
  this._edge.attrs.set(k, HTML_STRING_MARK + v);
579
649
  }
580
650
  getAttr(k) {
651
+ requireString("k", k);
581
652
  return this._edge.attrs.get(k);
582
653
  }
583
654
  };
655
+ function requireString(param, v) {
656
+ if (typeof v !== "string") throw invalidArgType(param, "string", v);
657
+ }
658
+ function requireAttrs(param, attrs) {
659
+ if (attrs === void 0) return;
660
+ requireObject(param, attrs);
661
+ if (Array.isArray(attrs)) throw invalidArgType(param, "plain object", attrs);
662
+ for (const [k, v] of Object.entries(attrs)) {
663
+ if (typeof v !== "string") {
664
+ throw invalidArgType(`${param}.${k}`, "string", v);
665
+ }
666
+ }
667
+ }
668
+ function requireNodeRef(param, ref) {
669
+ if (typeof ref === "string" || ref instanceof NodeHandle) return;
670
+ throw invalidArgType(param, "string or GvNode", ref);
671
+ }
672
+ function requireKeyValue(k, v) {
673
+ requireString("k", k);
674
+ requireString("v", v);
675
+ }
676
+ function requireGraphOptions(opts) {
677
+ if (opts === void 0) return;
678
+ requireObject("opts", opts);
679
+ const o = opts;
680
+ for (const key of ["directed", "strict"]) {
681
+ if (o[key] !== void 0 && typeof o[key] !== "boolean") {
682
+ throw invalidArgType(`opts.${key}`, "boolean or undefined", o[key]);
683
+ }
684
+ }
685
+ if (o["name"] !== void 0 && typeof o["name"] !== "string") {
686
+ throw invalidArgType("opts.name", "string or undefined", o["name"]);
687
+ }
688
+ }
584
689
  function resolveNode(g, ref) {
585
690
  if (typeof ref === "string") {
586
691
  const node = agnode(g, ref, true);
587
692
  if (node === null) {
588
- throw new RenderError(`Failed to resolve node '${ref}' in graph '${g.name}'`, "GENERIC_ERROR");
693
+ throw new InternalError(
694
+ `Failed to resolve node '${ref}' in graph '${g.name}'`
695
+ );
589
696
  }
590
697
  return node;
591
698
  }
@@ -611,6 +718,9 @@ var GraphBuilder = class _GraphBuilder {
611
718
  return addNodeToContext(this._graph, this._context, name, attrs);
612
719
  }
613
720
  addEdge(tail, head, attrs) {
721
+ requireNodeRef("tail", tail);
722
+ requireNodeRef("head", head);
723
+ requireAttrs("attrs", attrs);
614
724
  const tailNode = resolveNode(this._graph, tail);
615
725
  const headNode = resolveNode(this._graph, head);
616
726
  const edge = addEdge(this._graph, tailNode, headNode);
@@ -618,26 +728,33 @@ var GraphBuilder = class _GraphBuilder {
618
728
  return new EdgeHandle(edge);
619
729
  }
620
730
  addSubgraph(name, attrs) {
731
+ requireString("name", name);
732
+ requireAttrs("attrs", attrs);
621
733
  const sg = agsubg(this._context, name, true);
622
734
  if (sg === null) {
623
- throw new RenderError(`Failed to create subgraph '${name}'`, "GENERIC_ERROR");
735
+ throw new InternalError(`Failed to create subgraph '${name}'`);
624
736
  }
625
737
  applyAttrs(sg.attrs, attrs);
626
738
  return new _GraphBuilder(this._graph, sg);
627
739
  }
628
740
  setAttr(k, v) {
741
+ requireKeyValue(k, v);
629
742
  this._context.attrs.set(k, v);
630
743
  }
631
744
  setHtmlAttr(k, v) {
745
+ requireKeyValue(k, v);
632
746
  this._context.attrs.set(k, HTML_STRING_MARK + v);
633
747
  }
634
748
  getAttr(k) {
749
+ requireString("k", k);
635
750
  return this._context.attrs.get(k);
636
751
  }
637
752
  };
638
753
  function addNodeToContext(root, context, name, attrs) {
754
+ requireString("name", name);
755
+ requireAttrs("attrs", attrs);
639
756
  const node = agnode(root, name, true);
640
- if (node === null) throw new RenderError(`Failed to create node '${name}'`, "GENERIC_ERROR");
757
+ if (node === null) throw new InternalError(`Failed to create node '${name}'`);
641
758
  applyAttrs(node.attrs, attrs);
642
759
  if (context !== root) agsubnode(context, node, true);
643
760
  return new NodeHandle(node);
@@ -654,6 +771,7 @@ function resolveKind(opts) {
654
771
  return KIND_TABLE[`${d}:${s}`] ?? "directed";
655
772
  }
656
773
  function createGraph(opts) {
774
+ requireGraphOptions(opts);
657
775
  const g = new Graph(opts?.name ?? "", resolveKind(opts));
658
776
  return new GraphBuilder(g, g);
659
777
  }
@@ -747,11 +865,21 @@ function collectClusters(sg, yAxis, flipY, out) {
747
865
  collectClusters(c, yAxis, flipY, out);
748
866
  }
749
867
  }
868
+ var Y_AXES = ["up", "down"];
869
+ function checkGeometryOptions(opts) {
870
+ if (opts === void 0) return;
871
+ requireObject("opts", opts);
872
+ const y = opts.yAxis;
873
+ if (y !== void 0 && y !== "up" && y !== "down") {
874
+ throw invalidArgValue("opts.yAxis", y, Y_AXES);
875
+ }
876
+ }
750
877
  function getLayout(g, opts) {
878
+ requireObject("g", g);
879
+ checkGeometryOptions(opts);
751
880
  if (g.info?.laidOut !== true) {
752
- throw new RenderError(
753
- "getLayout requires a laid-out graph; run ctx.layout(g, engine) or render() first",
754
- "GENERIC_ERROR"
881
+ throw invalidState(
882
+ "getLayout requires a laid-out graph; run ctx.layout(g, engine) or render() first"
755
883
  );
756
884
  }
757
885
  const yAxis = opts?.yAxis ?? "down";