semantic_node_factory.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SemanticNodeFactory = void 0;
  4. const semantic_meaning_js_1 = require("./semantic_meaning.js");
  5. const semantic_default_js_1 = require("./semantic_default.js");
  6. const semantic_default_js_2 = require("./semantic_default.js");
  7. const semantic_node_js_1 = require("./semantic_node.js");
  8. class SemanticNodeFactory {
  9. constructor() {
  10. this.leafMap = new semantic_default_js_2.SemanticNodeCollator();
  11. this.defaultMap = new semantic_default_js_1.SemanticDefault();
  12. this.idCounter_ = -1;
  13. }
  14. makeNode(id) {
  15. return this.createNode_(id);
  16. }
  17. makeUnprocessed(mml) {
  18. const node = this.createNode_();
  19. node.mathml = [mml];
  20. node.mathmlTree = mml;
  21. return node;
  22. }
  23. makeEmptyNode() {
  24. const node = this.createNode_();
  25. node.type = semantic_meaning_js_1.SemanticType.EMPTY;
  26. return node;
  27. }
  28. makeContentNode(content) {
  29. const node = this.createNode_();
  30. node.updateContent(content);
  31. return node;
  32. }
  33. makeMultipleContentNodes(num, content) {
  34. const nodes = [];
  35. for (let i = 0; i < num; i++) {
  36. nodes.push(this.makeContentNode(content));
  37. }
  38. return nodes;
  39. }
  40. makeLeafNode(content, font) {
  41. if (!content) {
  42. return this.makeEmptyNode();
  43. }
  44. const node = this.makeContentNode(content);
  45. node.font = font || node.font;
  46. const meaning = this.defaultMap.getNode(node);
  47. if (meaning) {
  48. node.type = meaning.type;
  49. node.role = meaning.role;
  50. node.font = meaning.font;
  51. }
  52. this.leafMap.addNode(node);
  53. return node;
  54. }
  55. makeBranchNode(type, children, contentNodes, opt_content) {
  56. const node = this.createNode_();
  57. if (opt_content) {
  58. node.updateContent(opt_content);
  59. }
  60. node.type = type;
  61. node.childNodes = children;
  62. node.contentNodes = contentNodes;
  63. children.concat(contentNodes).forEach(function (x) {
  64. x.parent = node;
  65. node.addMathmlNodes(x.mathml);
  66. });
  67. return node;
  68. }
  69. createNode_(id) {
  70. if (typeof id !== 'undefined') {
  71. this.idCounter_ = Math.max(this.idCounter_, id);
  72. }
  73. else {
  74. id = ++this.idCounter_;
  75. }
  76. return new semantic_node_js_1.SemanticNode(id);
  77. }
  78. }
  79. exports.SemanticNodeFactory = SemanticNodeFactory;