mo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  45. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  46. if (ar || !(i in from)) {
  47. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  48. ar[i] = from[i];
  49. }
  50. }
  51. return to.concat(ar || Array.prototype.slice.call(from));
  52. };
  53. var __values = (this && this.__values) || function(o) {
  54. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  55. if (m) return m.call(o);
  56. if (o && typeof o.length === "number") return {
  57. next: function () {
  58. if (o && i >= o.length) o = void 0;
  59. return { value: o && o[i++], done: !o };
  60. }
  61. };
  62. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  63. };
  64. var _a;
  65. Object.defineProperty(exports, "__esModule", { value: true });
  66. exports.CommonMoMixin = exports.DirectionVH = void 0;
  67. var BBox_js_1 = require("../../../util/BBox.js");
  68. var string_js_1 = require("../../../util/string.js");
  69. var FontData_js_1 = require("../FontData.js");
  70. exports.DirectionVH = (_a = {},
  71. _a[1] = 'v',
  72. _a[2] = 'h',
  73. _a);
  74. function CommonMoMixin(Base) {
  75. return (function (_super) {
  76. __extends(class_1, _super);
  77. function class_1() {
  78. var args = [];
  79. for (var _i = 0; _i < arguments.length; _i++) {
  80. args[_i] = arguments[_i];
  81. }
  82. var _this = _super.apply(this, __spreadArray([], __read(args), false)) || this;
  83. _this.size = null;
  84. _this.isAccent = _this.node.isAccent;
  85. return _this;
  86. }
  87. class_1.prototype.computeBBox = function (bbox, _recompute) {
  88. if (_recompute === void 0) { _recompute = false; }
  89. this.protoBBox(bbox);
  90. if (this.node.attributes.get('symmetric') &&
  91. this.stretch.dir !== 2) {
  92. var d = this.getCenterOffset(bbox);
  93. bbox.h += d;
  94. bbox.d -= d;
  95. }
  96. if (this.node.getProperty('mathaccent') &&
  97. (this.stretch.dir === 0 || this.size >= 0)) {
  98. bbox.w = 0;
  99. }
  100. };
  101. class_1.prototype.protoBBox = function (bbox) {
  102. var stretchy = (this.stretch.dir !== 0);
  103. if (stretchy && this.size === null) {
  104. this.getStretchedVariant([0]);
  105. }
  106. if (stretchy && this.size < 0)
  107. return;
  108. _super.prototype.computeBBox.call(this, bbox);
  109. this.copySkewIC(bbox);
  110. };
  111. class_1.prototype.getAccentOffset = function () {
  112. var bbox = BBox_js_1.BBox.empty();
  113. this.protoBBox(bbox);
  114. return -bbox.w / 2;
  115. };
  116. class_1.prototype.getCenterOffset = function (bbox) {
  117. if (bbox === void 0) { bbox = null; }
  118. if (!bbox) {
  119. bbox = BBox_js_1.BBox.empty();
  120. _super.prototype.computeBBox.call(this, bbox);
  121. }
  122. return ((bbox.h + bbox.d) / 2 + this.font.params.axis_height) - bbox.h;
  123. };
  124. class_1.prototype.getVariant = function () {
  125. if (this.node.attributes.get('largeop')) {
  126. this.variant = (this.node.attributes.get('displaystyle') ? '-largeop' : '-smallop');
  127. return;
  128. }
  129. if (!this.node.attributes.getExplicit('mathvariant') &&
  130. this.node.getProperty('pseudoscript') === false) {
  131. this.variant = '-tex-variant';
  132. return;
  133. }
  134. _super.prototype.getVariant.call(this);
  135. };
  136. class_1.prototype.canStretch = function (direction) {
  137. if (this.stretch.dir !== 0) {
  138. return this.stretch.dir === direction;
  139. }
  140. var attributes = this.node.attributes;
  141. if (!attributes.get('stretchy'))
  142. return false;
  143. var c = this.getText();
  144. if (Array.from(c).length !== 1)
  145. return false;
  146. var delim = this.font.getDelimiter(c.codePointAt(0));
  147. this.stretch = (delim && delim.dir === direction ? delim : FontData_js_1.NOSTRETCH);
  148. return this.stretch.dir !== 0;
  149. };
  150. class_1.prototype.getStretchedVariant = function (WH, exact) {
  151. var e_1, _a;
  152. if (exact === void 0) { exact = false; }
  153. if (this.stretch.dir !== 0) {
  154. var D = this.getWH(WH);
  155. var min = this.getSize('minsize', 0);
  156. var max = this.getSize('maxsize', Infinity);
  157. var mathaccent = this.node.getProperty('mathaccent');
  158. D = Math.max(min, Math.min(max, D));
  159. var df = this.font.params.delimiterfactor / 1000;
  160. var ds = this.font.params.delimitershortfall;
  161. var m = (min || exact ? D : mathaccent ? Math.min(D / df, D + ds) : Math.max(D * df, D - ds));
  162. var delim = this.stretch;
  163. var c = delim.c || this.getText().codePointAt(0);
  164. var i = 0;
  165. if (delim.sizes) {
  166. try {
  167. for (var _b = __values(delim.sizes), _c = _b.next(); !_c.done; _c = _b.next()) {
  168. var d = _c.value;
  169. if (d >= m) {
  170. if (mathaccent && i) {
  171. i--;
  172. }
  173. this.variant = this.font.getSizeVariant(c, i);
  174. this.size = i;
  175. if (delim.schar && delim.schar[i]) {
  176. this.stretch = __assign(__assign({}, this.stretch), { c: delim.schar[i] });
  177. }
  178. return;
  179. }
  180. i++;
  181. }
  182. }
  183. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  184. finally {
  185. try {
  186. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  187. }
  188. finally { if (e_1) throw e_1.error; }
  189. }
  190. }
  191. if (delim.stretch) {
  192. this.size = -1;
  193. this.invalidateBBox();
  194. this.getStretchBBox(WH, this.checkExtendedHeight(D, delim), delim);
  195. }
  196. else {
  197. this.variant = this.font.getSizeVariant(c, i - 1);
  198. this.size = i - 1;
  199. }
  200. }
  201. };
  202. class_1.prototype.getSize = function (name, value) {
  203. var attributes = this.node.attributes;
  204. if (attributes.isSet(name)) {
  205. value = this.length2em(attributes.get(name), 1, 1);
  206. }
  207. return value;
  208. };
  209. class_1.prototype.getWH = function (WH) {
  210. if (WH.length === 0)
  211. return 0;
  212. if (WH.length === 1)
  213. return WH[0];
  214. var _a = __read(WH, 2), H = _a[0], D = _a[1];
  215. var a = this.font.params.axis_height;
  216. return (this.node.attributes.get('symmetric') ? 2 * Math.max(H - a, D + a) : H + D);
  217. };
  218. class_1.prototype.getStretchBBox = function (WHD, D, C) {
  219. var _a;
  220. if (C.hasOwnProperty('min') && C.min > D) {
  221. D = C.min;
  222. }
  223. var _b = __read(C.HDW, 3), h = _b[0], d = _b[1], w = _b[2];
  224. if (this.stretch.dir === 1) {
  225. _a = __read(this.getBaseline(WHD, D, C), 2), h = _a[0], d = _a[1];
  226. }
  227. else {
  228. w = D;
  229. }
  230. this.bbox.h = h;
  231. this.bbox.d = d;
  232. this.bbox.w = w;
  233. };
  234. class_1.prototype.getBaseline = function (WHD, HD, C) {
  235. var hasWHD = (WHD.length === 2 && WHD[0] + WHD[1] === HD);
  236. var symmetric = this.node.attributes.get('symmetric');
  237. var _a = __read((hasWHD ? WHD : [HD, 0]), 2), H = _a[0], D = _a[1];
  238. var _b = __read([H + D, 0], 2), h = _b[0], d = _b[1];
  239. if (symmetric) {
  240. var a = this.font.params.axis_height;
  241. if (hasWHD) {
  242. h = 2 * Math.max(H - a, D + a);
  243. }
  244. d = h / 2 - a;
  245. }
  246. else if (hasWHD) {
  247. d = D;
  248. }
  249. else {
  250. var _c = __read((C.HDW || [.75, .25]), 2), ch = _c[0], cd = _c[1];
  251. d = cd * (h / (ch + cd));
  252. }
  253. return [h - d, d];
  254. };
  255. class_1.prototype.checkExtendedHeight = function (D, C) {
  256. if (C.fullExt) {
  257. var _a = __read(C.fullExt, 2), extSize = _a[0], endSize = _a[1];
  258. var n = Math.ceil(Math.max(0, D - endSize) / extSize);
  259. D = endSize + n * extSize;
  260. }
  261. return D;
  262. };
  263. class_1.prototype.remapChars = function (chars) {
  264. var primes = this.node.getProperty('primes');
  265. if (primes) {
  266. return (0, string_js_1.unicodeChars)(primes);
  267. }
  268. if (chars.length === 1) {
  269. var parent_1 = this.node.coreParent().parent;
  270. var isAccent = this.isAccent && !parent_1.isKind('mrow');
  271. var map = (isAccent ? 'accent' : 'mo');
  272. var text = this.font.getRemappedChar(map, chars[0]);
  273. if (text) {
  274. chars = this.unicodeChars(text, this.variant);
  275. }
  276. }
  277. return chars;
  278. };
  279. return class_1;
  280. }(Base));
  281. }
  282. exports.CommonMoMixin = CommonMoMixin;
  283. //# sourceMappingURL=mo.js.map