mo.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  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. Object.defineProperty(exports, "__esModule", { value: true });
  45. exports.SVGmo = void 0;
  46. var Wrapper_js_1 = require("../Wrapper.js");
  47. var mo_js_1 = require("../../common/Wrappers/mo.js");
  48. var mo_js_2 = require("../../../core/MmlTree/MmlNodes/mo.js");
  49. var VFUZZ = 0.1;
  50. var HFUZZ = 0.1;
  51. var SVGmo = (function (_super) {
  52. __extends(SVGmo, _super);
  53. function SVGmo() {
  54. return _super !== null && _super.apply(this, arguments) || this;
  55. }
  56. SVGmo.prototype.toSVG = function (parent) {
  57. var attributes = this.node.attributes;
  58. var symmetric = attributes.get('symmetric') && this.stretch.dir !== 2;
  59. var stretchy = this.stretch.dir !== 0;
  60. if (stretchy && this.size === null) {
  61. this.getStretchedVariant([]);
  62. }
  63. var svg = this.standardSVGnode(parent);
  64. if (stretchy && this.size < 0) {
  65. this.stretchSVG();
  66. }
  67. else {
  68. var u = (symmetric || attributes.get('largeop') ? this.fixed(this.getCenterOffset()) : '0');
  69. var v = (this.node.getProperty('mathaccent') ? this.fixed(this.getAccentOffset()) : '0');
  70. if (u !== '0' || v !== '0') {
  71. this.adaptor.setAttribute(svg, 'transform', "translate(".concat(v, " ").concat(u, ")"));
  72. }
  73. this.addChildren(svg);
  74. }
  75. };
  76. SVGmo.prototype.stretchSVG = function () {
  77. var stretch = this.stretch.stretch;
  78. var variants = this.getStretchVariants();
  79. var bbox = this.getBBox();
  80. if (this.stretch.dir === 1) {
  81. this.stretchVertical(stretch, variants, bbox);
  82. }
  83. else {
  84. this.stretchHorizontal(stretch, variants, bbox);
  85. }
  86. };
  87. SVGmo.prototype.getStretchVariants = function () {
  88. var e_1, _a;
  89. var c = this.stretch.c || this.getText().codePointAt(0);
  90. var variants = [];
  91. try {
  92. for (var _b = __values(this.stretch.stretch.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
  93. var i = _c.value;
  94. variants[i] = this.font.getStretchVariant(c, i);
  95. }
  96. }
  97. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  98. finally {
  99. try {
  100. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  101. }
  102. finally { if (e_1) throw e_1.error; }
  103. }
  104. return variants;
  105. };
  106. SVGmo.prototype.stretchVertical = function (stretch, variant, bbox) {
  107. var h = bbox.h, d = bbox.d, w = bbox.w;
  108. var T = this.addTop(stretch[0], variant[0], h, w);
  109. var B = this.addBot(stretch[2], variant[2], d, w);
  110. if (stretch.length === 4) {
  111. var _a = __read(this.addMidV(stretch[3], variant[3], w), 2), H = _a[0], D = _a[1];
  112. this.addExtV(stretch[1], variant[1], h, 0, T, H, w);
  113. this.addExtV(stretch[1], variant[1], 0, d, D, B, w);
  114. }
  115. else {
  116. this.addExtV(stretch[1], variant[1], h, d, T, B, w);
  117. }
  118. };
  119. SVGmo.prototype.stretchHorizontal = function (stretch, variant, bbox) {
  120. var w = bbox.w;
  121. var L = this.addLeft(stretch[0], variant[0]);
  122. var R = this.addRight(stretch[2], variant[2], w);
  123. if (stretch.length === 4) {
  124. var _a = __read(this.addMidH(stretch[3], variant[3], w), 2), x1 = _a[0], x2 = _a[1];
  125. var w2 = w / 2;
  126. this.addExtH(stretch[1], variant[1], w2, L, w2 - x1);
  127. this.addExtH(stretch[1], variant[1], w2, x2 - w2, R, w2);
  128. }
  129. else {
  130. this.addExtH(stretch[1], variant[1], w, L, R);
  131. }
  132. };
  133. SVGmo.prototype.getChar = function (n, variant) {
  134. var char = this.font.getChar(variant, n) || [0, 0, 0, null];
  135. return [char[0], char[1], char[2], char[3] || {}];
  136. };
  137. SVGmo.prototype.addGlyph = function (n, variant, x, y, parent) {
  138. if (parent === void 0) { parent = null; }
  139. return this.placeChar(n, x, y, parent || this.element, variant);
  140. };
  141. SVGmo.prototype.addTop = function (n, v, H, W) {
  142. if (!n)
  143. return 0;
  144. var _a = __read(this.getChar(n, v), 3), h = _a[0], d = _a[1], w = _a[2];
  145. this.addGlyph(n, v, (W - w) / 2, H - h);
  146. return h + d;
  147. };
  148. SVGmo.prototype.addExtV = function (n, v, H, D, T, B, W) {
  149. var _this = this;
  150. if (!n)
  151. return;
  152. T = Math.max(0, T - VFUZZ);
  153. B = Math.max(0, B - VFUZZ);
  154. var adaptor = this.adaptor;
  155. var _a = __read(this.getChar(n, v), 3), h = _a[0], d = _a[1], w = _a[2];
  156. var Y = H + D - T - B;
  157. var s = 1.5 * Y / (h + d);
  158. var y = (s * (h - d) - Y) / 2;
  159. if (Y <= 0)
  160. return;
  161. var svg = this.svg('svg', {
  162. width: this.fixed(w), height: this.fixed(Y),
  163. y: this.fixed(B - D), x: this.fixed((W - w) / 2),
  164. viewBox: [0, y, w, Y].map(function (x) { return _this.fixed(x); }).join(' ')
  165. });
  166. this.addGlyph(n, v, 0, 0, svg);
  167. var glyph = adaptor.lastChild(svg);
  168. adaptor.setAttribute(glyph, 'transform', "scale(1,".concat(this.jax.fixed(s), ")"));
  169. adaptor.append(this.element, svg);
  170. };
  171. SVGmo.prototype.addBot = function (n, v, D, W) {
  172. if (!n)
  173. return 0;
  174. var _a = __read(this.getChar(n, v), 3), h = _a[0], d = _a[1], w = _a[2];
  175. this.addGlyph(n, v, (W - w) / 2, d - D);
  176. return h + d;
  177. };
  178. SVGmo.prototype.addMidV = function (n, v, W) {
  179. if (!n)
  180. return [0, 0];
  181. var _a = __read(this.getChar(n, v), 3), h = _a[0], d = _a[1], w = _a[2];
  182. var y = (d - h) / 2 + this.font.params.axis_height;
  183. this.addGlyph(n, v, (W - w) / 2, y);
  184. return [h + y, d - y];
  185. };
  186. SVGmo.prototype.addLeft = function (n, v) {
  187. return (n ? this.addGlyph(n, v, 0, 0) : 0);
  188. };
  189. SVGmo.prototype.addExtH = function (n, v, W, L, R, x) {
  190. var _this = this;
  191. if (x === void 0) { x = 0; }
  192. if (!n)
  193. return;
  194. R = Math.max(0, R - HFUZZ);
  195. L = Math.max(0, L - HFUZZ);
  196. var adaptor = this.adaptor;
  197. var _a = __read(this.getChar(n, v), 3), h = _a[0], d = _a[1], w = _a[2];
  198. var X = W - L - R;
  199. var Y = h + d + 2 * VFUZZ;
  200. var s = 1.5 * (X / w);
  201. var D = -(d + VFUZZ);
  202. if (X <= 0)
  203. return;
  204. var svg = this.svg('svg', {
  205. width: this.fixed(X), height: this.fixed(Y),
  206. x: this.fixed(x + L), y: this.fixed(D),
  207. viewBox: [(s * w - X) / 2, D, X, Y].map(function (x) { return _this.fixed(x); }).join(' ')
  208. });
  209. this.addGlyph(n, v, 0, 0, svg);
  210. var glyph = adaptor.lastChild(svg);
  211. adaptor.setAttribute(glyph, 'transform', 'scale(' + this.jax.fixed(s) + ',1)');
  212. adaptor.append(this.element, svg);
  213. };
  214. SVGmo.prototype.addRight = function (n, v, W) {
  215. if (!n)
  216. return 0;
  217. var w = this.getChar(n, v)[2];
  218. return this.addGlyph(n, v, W - w, 0);
  219. };
  220. SVGmo.prototype.addMidH = function (n, v, W) {
  221. if (!n)
  222. return [0, 0];
  223. var w = this.getChar(n, v)[2];
  224. this.addGlyph(n, v, (W - w) / 2, 0);
  225. return [(W - w) / 2, (W + w) / 2];
  226. };
  227. SVGmo.kind = mo_js_2.MmlMo.prototype.kind;
  228. return SVGmo;
  229. }((0, mo_js_1.CommonMoMixin)(Wrapper_js_1.SVGWrapper)));
  230. exports.SVGmo = SVGmo;
  231. //# sourceMappingURL=mo.js.map