EmpheqUtil.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  19. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  20. if (ar || !(i in from)) {
  21. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  22. ar[i] = from[i];
  23. }
  24. }
  25. return to.concat(ar || Array.prototype.slice.call(from));
  26. };
  27. var __values = (this && this.__values) || function(o) {
  28. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  29. if (m) return m.call(o);
  30. if (o && typeof o.length === "number") return {
  31. next: function () {
  32. if (o && i >= o.length) o = void 0;
  33. return { value: o && o[i++], done: !o };
  34. }
  35. };
  36. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  37. };
  38. var __importDefault = (this && this.__importDefault) || function (mod) {
  39. return (mod && mod.__esModule) ? mod : { "default": mod };
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. exports.EmpheqUtil = void 0;
  43. var ParseUtil_js_1 = __importDefault(require("../ParseUtil.js"));
  44. var TexParser_js_1 = __importDefault(require("../TexParser.js"));
  45. exports.EmpheqUtil = {
  46. environment: function (parser, env, func, args) {
  47. var name = args[0];
  48. var item = parser.itemFactory.create(name + '-begin').setProperties({ name: env, end: name });
  49. parser.Push(func.apply(void 0, __spreadArray([parser, item], __read(args.slice(1)), false)));
  50. },
  51. splitOptions: function (text, allowed) {
  52. if (allowed === void 0) { allowed = null; }
  53. return ParseUtil_js_1.default.keyvalOptions(text, allowed, true);
  54. },
  55. columnCount: function (table) {
  56. var e_1, _a;
  57. var m = 0;
  58. try {
  59. for (var _b = __values(table.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  60. var row = _c.value;
  61. var n = row.childNodes.length - (row.isKind('mlabeledtr') ? 1 : 0);
  62. if (n > m)
  63. m = n;
  64. }
  65. }
  66. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  67. finally {
  68. try {
  69. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  70. }
  71. finally { if (e_1) throw e_1.error; }
  72. }
  73. return m;
  74. },
  75. cellBlock: function (tex, table, parser, env) {
  76. var e_2, _a;
  77. var mpadded = parser.create('node', 'mpadded', [], { height: 0, depth: 0, voffset: '-1height' });
  78. var result = new TexParser_js_1.default(tex, parser.stack.env, parser.configuration);
  79. var mml = result.mml();
  80. if (env && result.configuration.tags.label) {
  81. result.configuration.tags.currentTag.env = env;
  82. result.configuration.tags.getTag(true);
  83. }
  84. try {
  85. for (var _b = __values((mml.isInferred ? mml.childNodes : [mml])), _c = _b.next(); !_c.done; _c = _b.next()) {
  86. var child = _c.value;
  87. mpadded.appendChild(child);
  88. }
  89. }
  90. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  91. finally {
  92. try {
  93. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  94. }
  95. finally { if (e_2) throw e_2.error; }
  96. }
  97. mpadded.appendChild(parser.create('node', 'mphantom', [
  98. parser.create('node', 'mpadded', [table], { width: 0 })
  99. ]));
  100. return mpadded;
  101. },
  102. topRowTable: function (original, parser) {
  103. var table = ParseUtil_js_1.default.copyNode(original, parser);
  104. table.setChildren(table.childNodes.slice(0, 1));
  105. table.attributes.set('align', 'baseline 1');
  106. return original.factory.create('mphantom', {}, [parser.create('node', 'mpadded', [table], { width: 0 })]);
  107. },
  108. rowspanCell: function (mtd, tex, table, parser, env) {
  109. mtd.appendChild(parser.create('node', 'mpadded', [
  110. this.cellBlock(tex, ParseUtil_js_1.default.copyNode(table, parser), parser, env),
  111. this.topRowTable(table, parser)
  112. ], { height: 0, depth: 0, voffset: 'height' }));
  113. },
  114. left: function (table, original, left, parser, env) {
  115. var e_3, _a;
  116. if (env === void 0) { env = ''; }
  117. table.attributes.set('columnalign', 'right ' + (table.attributes.get('columnalign') || ''));
  118. table.attributes.set('columnspacing', '0em ' + (table.attributes.get('columnspacing') || ''));
  119. var mtd;
  120. try {
  121. for (var _b = __values(table.childNodes.slice(0).reverse()), _c = _b.next(); !_c.done; _c = _b.next()) {
  122. var row = _c.value;
  123. mtd = parser.create('node', 'mtd');
  124. row.childNodes.unshift(mtd);
  125. mtd.parent = row;
  126. if (row.isKind('mlabeledtr')) {
  127. row.childNodes[0] = row.childNodes[1];
  128. row.childNodes[1] = mtd;
  129. }
  130. }
  131. }
  132. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  133. finally {
  134. try {
  135. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  136. }
  137. finally { if (e_3) throw e_3.error; }
  138. }
  139. this.rowspanCell(mtd, left, original, parser, env);
  140. },
  141. right: function (table, original, right, parser, env) {
  142. if (env === void 0) { env = ''; }
  143. if (table.childNodes.length === 0) {
  144. table.appendChild(parser.create('node', 'mtr'));
  145. }
  146. var m = exports.EmpheqUtil.columnCount(table);
  147. var row = table.childNodes[0];
  148. while (row.childNodes.length < m)
  149. row.appendChild(parser.create('node', 'mtd'));
  150. var mtd = row.appendChild(parser.create('node', 'mtd'));
  151. exports.EmpheqUtil.rowspanCell(mtd, right, original, parser, env);
  152. table.attributes.set('columnalign', (table.attributes.get('columnalign') || '').split(/ /).slice(0, m).join(' ') + ' left');
  153. table.attributes.set('columnspacing', (table.attributes.get('columnspacing') || '').split(/ /).slice(0, m - 1).join(' ') + ' 0em');
  154. },
  155. adjustTable: function (empheq, parser) {
  156. var left = empheq.getProperty('left');
  157. var right = empheq.getProperty('right');
  158. if (left || right) {
  159. var table = empheq.Last;
  160. var original = ParseUtil_js_1.default.copyNode(table, parser);
  161. if (left)
  162. this.left(table, original, left, parser);
  163. if (right)
  164. this.right(table, original, right, parser);
  165. }
  166. },
  167. allowEnv: {
  168. equation: true,
  169. align: true,
  170. gather: true,
  171. flalign: true,
  172. alignat: true,
  173. multline: true
  174. },
  175. checkEnv: function (env) {
  176. return this.allowEnv.hasOwnProperty(env.replace(/\*$/, '')) || false;
  177. }
  178. };
  179. //# sourceMappingURL=EmpheqUtil.js.map