scriptbase.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __read = (this && this.__read) || function (o, n) {
  18. var m = typeof Symbol === "function" && o[Symbol.iterator];
  19. if (!m) return o;
  20. var i = m.call(o), r, ar = [], e;
  21. try {
  22. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  23. }
  24. catch (error) { e = { error: error }; }
  25. finally {
  26. try {
  27. if (r && !r.done && (m = i["return"])) m.call(i);
  28. }
  29. finally { if (e) throw e.error; }
  30. }
  31. return ar;
  32. };
  33. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  34. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  35. if (ar || !(i in from)) {
  36. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  37. ar[i] = from[i];
  38. }
  39. }
  40. return to.concat(ar || Array.prototype.slice.call(from));
  41. };
  42. var __values = (this && this.__values) || function(o) {
  43. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  44. if (m) return m.call(o);
  45. if (o && typeof o.length === "number") return {
  46. next: function () {
  47. if (o && i >= o.length) o = void 0;
  48. return { value: o && o[i++], done: !o };
  49. }
  50. };
  51. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  52. };
  53. Object.defineProperty(exports, "__esModule", { value: true });
  54. exports.CommonScriptbaseMixin = void 0;
  55. var MmlNode_js_1 = require("../../../core/MmlTree/MmlNode.js");
  56. function CommonScriptbaseMixin(Base) {
  57. var _a;
  58. return _a = (function (_super) {
  59. __extends(class_1, _super);
  60. function class_1() {
  61. var args = [];
  62. for (var _i = 0; _i < arguments.length; _i++) {
  63. args[_i] = arguments[_i];
  64. }
  65. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  66. _this.baseScale = 1;
  67. _this.baseIc = 0;
  68. _this.baseRemoveIc = false;
  69. _this.baseIsChar = false;
  70. _this.baseHasAccentOver = null;
  71. _this.baseHasAccentUnder = null;
  72. _this.isLineAbove = false;
  73. _this.isLineBelow = false;
  74. _this.isMathAccent = false;
  75. var core = _this.baseCore = _this.getBaseCore();
  76. if (!core)
  77. return _this;
  78. _this.setBaseAccentsFor(core);
  79. _this.baseScale = _this.getBaseScale();
  80. _this.baseIc = _this.getBaseIc();
  81. _this.baseIsChar = _this.isCharBase();
  82. _this.isMathAccent = _this.baseIsChar &&
  83. (_this.scriptChild && !!_this.scriptChild.coreMO().node.getProperty('mathaccent'));
  84. _this.checkLineAccents();
  85. _this.baseRemoveIc = !_this.isLineAbove && !_this.isLineBelow &&
  86. (!_this.constructor.useIC || _this.isMathAccent);
  87. return _this;
  88. }
  89. Object.defineProperty(class_1.prototype, "baseChild", {
  90. get: function () {
  91. return this.childNodes[this.node.base];
  92. },
  93. enumerable: false,
  94. configurable: true
  95. });
  96. Object.defineProperty(class_1.prototype, "scriptChild", {
  97. get: function () {
  98. return this.childNodes[1];
  99. },
  100. enumerable: false,
  101. configurable: true
  102. });
  103. class_1.prototype.getBaseCore = function () {
  104. var core = this.getSemanticBase() || this.childNodes[0];
  105. while (core &&
  106. ((core.childNodes.length === 1 &&
  107. (core.node.isKind('mrow') ||
  108. (core.node.isKind('TeXAtom') && core.node.texClass !== MmlNode_js_1.TEXCLASS.VCENTER) ||
  109. core.node.isKind('mstyle') || core.node.isKind('mpadded') ||
  110. core.node.isKind('mphantom') || core.node.isKind('semantics'))) ||
  111. (core.node.isKind('munderover') && core.isMathAccent))) {
  112. this.setBaseAccentsFor(core);
  113. core = core.childNodes[0];
  114. }
  115. if (!core) {
  116. this.baseHasAccentOver = this.baseHasAccentUnder = false;
  117. }
  118. return core || this.childNodes[0];
  119. };
  120. class_1.prototype.setBaseAccentsFor = function (core) {
  121. if (core.node.isKind('munderover')) {
  122. if (this.baseHasAccentOver === null) {
  123. this.baseHasAccentOver = !!core.node.attributes.get('accent');
  124. }
  125. if (this.baseHasAccentUnder === null) {
  126. this.baseHasAccentUnder = !!core.node.attributes.get('accentunder');
  127. }
  128. }
  129. };
  130. class_1.prototype.getSemanticBase = function () {
  131. var fence = this.node.attributes.getExplicit('data-semantic-fencepointer');
  132. return this.getBaseFence(this.baseChild, fence);
  133. };
  134. class_1.prototype.getBaseFence = function (fence, id) {
  135. var e_1, _a;
  136. if (!fence || !fence.node.attributes || !id) {
  137. return null;
  138. }
  139. if (fence.node.attributes.getExplicit('data-semantic-id') === id) {
  140. return fence;
  141. }
  142. try {
  143. for (var _b = __values(fence.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  144. var child = _c.value;
  145. var result = this.getBaseFence(child, id);
  146. if (result) {
  147. return result;
  148. }
  149. }
  150. }
  151. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  152. finally {
  153. try {
  154. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  155. }
  156. finally { if (e_1) throw e_1.error; }
  157. }
  158. return null;
  159. };
  160. class_1.prototype.getBaseScale = function () {
  161. var child = this.baseCore;
  162. var scale = 1;
  163. while (child && child !== this) {
  164. var bbox = child.getOuterBBox();
  165. scale *= bbox.rscale;
  166. child = child.parent;
  167. }
  168. return scale;
  169. };
  170. class_1.prototype.getBaseIc = function () {
  171. return this.baseCore.getOuterBBox().ic * this.baseScale;
  172. };
  173. class_1.prototype.getAdjustedIc = function () {
  174. var bbox = this.baseCore.getOuterBBox();
  175. return (bbox.ic ? 1.05 * bbox.ic + .05 : 0) * this.baseScale;
  176. };
  177. class_1.prototype.isCharBase = function () {
  178. var base = this.baseCore;
  179. return (((base.node.isKind('mo') && base.size === null) ||
  180. base.node.isKind('mi') || base.node.isKind('mn')) &&
  181. base.bbox.rscale === 1 && Array.from(base.getText()).length === 1);
  182. };
  183. class_1.prototype.checkLineAccents = function () {
  184. if (!this.node.isKind('munderover'))
  185. return;
  186. if (this.node.isKind('mover')) {
  187. this.isLineAbove = this.isLineAccent(this.scriptChild);
  188. }
  189. else if (this.node.isKind('munder')) {
  190. this.isLineBelow = this.isLineAccent(this.scriptChild);
  191. }
  192. else {
  193. var mml = this;
  194. this.isLineAbove = this.isLineAccent(mml.overChild);
  195. this.isLineBelow = this.isLineAccent(mml.underChild);
  196. }
  197. };
  198. class_1.prototype.isLineAccent = function (script) {
  199. var node = script.coreMO().node;
  200. return (node.isToken && node.getText() === '\u2015');
  201. };
  202. class_1.prototype.getBaseWidth = function () {
  203. var bbox = this.baseChild.getOuterBBox();
  204. return bbox.w * bbox.rscale - (this.baseRemoveIc ? this.baseIc : 0) + this.font.params.extra_ic;
  205. };
  206. class_1.prototype.computeBBox = function (bbox, recompute) {
  207. if (recompute === void 0) { recompute = false; }
  208. var w = this.getBaseWidth();
  209. var _a = __read(this.getOffset(), 2), x = _a[0], y = _a[1];
  210. bbox.append(this.baseChild.getOuterBBox());
  211. bbox.combine(this.scriptChild.getOuterBBox(), w + x, y);
  212. bbox.w += this.font.params.scriptspace;
  213. bbox.clean();
  214. this.setChildPWidths(recompute);
  215. };
  216. class_1.prototype.getOffset = function () {
  217. return [0, 0];
  218. };
  219. class_1.prototype.baseCharZero = function (n) {
  220. var largeop = !!this.baseCore.node.attributes.get('largeop');
  221. var scale = this.baseScale;
  222. return (this.baseIsChar && !largeop && scale === 1 ? 0 : n);
  223. };
  224. class_1.prototype.getV = function () {
  225. var bbox = this.baseCore.getOuterBBox();
  226. var sbox = this.scriptChild.getOuterBBox();
  227. var tex = this.font.params;
  228. var subscriptshift = this.length2em(this.node.attributes.get('subscriptshift'), tex.sub1);
  229. return Math.max(this.baseCharZero(bbox.d * this.baseScale + tex.sub_drop * sbox.rscale), subscriptshift, sbox.h * sbox.rscale - (4 / 5) * tex.x_height);
  230. };
  231. class_1.prototype.getU = function () {
  232. var bbox = this.baseCore.getOuterBBox();
  233. var sbox = this.scriptChild.getOuterBBox();
  234. var tex = this.font.params;
  235. var attr = this.node.attributes.getList('displaystyle', 'superscriptshift');
  236. var prime = this.node.getProperty('texprimestyle');
  237. var p = prime ? tex.sup3 : (attr.displaystyle ? tex.sup1 : tex.sup2);
  238. var superscriptshift = this.length2em(attr.superscriptshift, p);
  239. return Math.max(this.baseCharZero(bbox.h * this.baseScale - tex.sup_drop * sbox.rscale), superscriptshift, sbox.d * sbox.rscale + (1 / 4) * tex.x_height);
  240. };
  241. class_1.prototype.hasMovableLimits = function () {
  242. var display = this.node.attributes.get('displaystyle');
  243. var mo = this.baseChild.coreMO().node;
  244. return (!display && !!mo.attributes.get('movablelimits'));
  245. };
  246. class_1.prototype.getOverKU = function (basebox, overbox) {
  247. var accent = this.node.attributes.get('accent');
  248. var tex = this.font.params;
  249. var d = overbox.d * overbox.rscale;
  250. var t = tex.rule_thickness * tex.separation_factor;
  251. var delta = (this.baseHasAccentOver ? t : 0);
  252. var T = (this.isLineAbove ? 3 * tex.rule_thickness : t);
  253. var k = (accent ? T : Math.max(tex.big_op_spacing1, tex.big_op_spacing3 - Math.max(0, d))) - delta;
  254. return [k, basebox.h * basebox.rscale + k + d];
  255. };
  256. class_1.prototype.getUnderKV = function (basebox, underbox) {
  257. var accent = this.node.attributes.get('accentunder');
  258. var tex = this.font.params;
  259. var h = underbox.h * underbox.rscale;
  260. var t = tex.rule_thickness * tex.separation_factor;
  261. var delta = (this.baseHasAccentUnder ? t : 0);
  262. var T = (this.isLineBelow ? 3 * tex.rule_thickness : t);
  263. var k = (accent ? T : Math.max(tex.big_op_spacing2, tex.big_op_spacing4 - h)) - delta;
  264. return [k, -(basebox.d * basebox.rscale + k + h)];
  265. };
  266. class_1.prototype.getDeltaW = function (boxes, delta) {
  267. var e_2, _a, e_3, _b;
  268. if (delta === void 0) { delta = [0, 0, 0]; }
  269. var align = this.node.attributes.get('align');
  270. var widths = boxes.map(function (box) { return box.w * box.rscale; });
  271. widths[0] -= (this.baseRemoveIc && !this.baseCore.node.attributes.get('largeop') ? this.baseIc : 0);
  272. var w = Math.max.apply(Math, __spreadArray([], __read(widths), false));
  273. var dw = [];
  274. var m = 0;
  275. try {
  276. for (var _c = __values(widths.keys()), _d = _c.next(); !_d.done; _d = _c.next()) {
  277. var i = _d.value;
  278. dw[i] = (align === 'center' ? (w - widths[i]) / 2 :
  279. align === 'right' ? w - widths[i] : 0) + delta[i];
  280. if (dw[i] < m) {
  281. m = -dw[i];
  282. }
  283. }
  284. }
  285. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  286. finally {
  287. try {
  288. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  289. }
  290. finally { if (e_2) throw e_2.error; }
  291. }
  292. if (m) {
  293. try {
  294. for (var _e = __values(dw.keys()), _f = _e.next(); !_f.done; _f = _e.next()) {
  295. var i = _f.value;
  296. dw[i] += m;
  297. }
  298. }
  299. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  300. finally {
  301. try {
  302. if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
  303. }
  304. finally { if (e_3) throw e_3.error; }
  305. }
  306. }
  307. [1, 2].map(function (i) { return dw[i] += (boxes[i] ? boxes[i].dx * boxes[0].scale : 0); });
  308. return dw;
  309. };
  310. class_1.prototype.getDelta = function (noskew) {
  311. if (noskew === void 0) { noskew = false; }
  312. var accent = this.node.attributes.get('accent');
  313. var _a = this.baseCore.getOuterBBox(), sk = _a.sk, ic = _a.ic;
  314. return ((accent && !noskew ? sk : 0) + this.font.skewIcFactor * ic) * this.baseScale;
  315. };
  316. class_1.prototype.stretchChildren = function () {
  317. var e_4, _a, e_5, _b, e_6, _c;
  318. var stretchy = [];
  319. try {
  320. for (var _d = __values(this.childNodes), _e = _d.next(); !_e.done; _e = _d.next()) {
  321. var child = _e.value;
  322. if (child.canStretch(2)) {
  323. stretchy.push(child);
  324. }
  325. }
  326. }
  327. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  328. finally {
  329. try {
  330. if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
  331. }
  332. finally { if (e_4) throw e_4.error; }
  333. }
  334. var count = stretchy.length;
  335. var nodeCount = this.childNodes.length;
  336. if (count && nodeCount > 1) {
  337. var W = 0;
  338. var all = (count > 1 && count === nodeCount);
  339. try {
  340. for (var _f = __values(this.childNodes), _g = _f.next(); !_g.done; _g = _f.next()) {
  341. var child = _g.value;
  342. var noStretch = (child.stretch.dir === 0);
  343. if (all || noStretch) {
  344. var _h = child.getOuterBBox(noStretch), w = _h.w, rscale = _h.rscale;
  345. if (w * rscale > W)
  346. W = w * rscale;
  347. }
  348. }
  349. }
  350. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  351. finally {
  352. try {
  353. if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
  354. }
  355. finally { if (e_5) throw e_5.error; }
  356. }
  357. try {
  358. for (var stretchy_1 = __values(stretchy), stretchy_1_1 = stretchy_1.next(); !stretchy_1_1.done; stretchy_1_1 = stretchy_1.next()) {
  359. var child = stretchy_1_1.value;
  360. child.coreMO().getStretchedVariant([W / child.bbox.rscale]);
  361. }
  362. }
  363. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  364. finally {
  365. try {
  366. if (stretchy_1_1 && !stretchy_1_1.done && (_c = stretchy_1.return)) _c.call(stretchy_1);
  367. }
  368. finally { if (e_6) throw e_6.error; }
  369. }
  370. }
  371. };
  372. return class_1;
  373. }(Base)),
  374. _a.useIC = true,
  375. _a;
  376. }
  377. exports.CommonScriptbaseMixin = CommonScriptbaseMixin;
  378. //# sourceMappingURL=scriptbase.js.map