Wrapper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  18. if (k2 === undefined) k2 = k;
  19. var desc = Object.getOwnPropertyDescriptor(m, k);
  20. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  21. desc = { enumerable: true, get: function() { return m[k]; } };
  22. }
  23. Object.defineProperty(o, k2, desc);
  24. }) : (function(o, m, k, k2) {
  25. if (k2 === undefined) k2 = k;
  26. o[k2] = m[k];
  27. }));
  28. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  29. Object.defineProperty(o, "default", { enumerable: true, value: v });
  30. }) : function(o, v) {
  31. o["default"] = v;
  32. });
  33. var __importStar = (this && this.__importStar) || function (mod) {
  34. if (mod && mod.__esModule) return mod;
  35. var result = {};
  36. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  37. __setModuleDefault(result, mod);
  38. return result;
  39. };
  40. var __values = (this && this.__values) || function(o) {
  41. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  42. if (m) return m.call(o);
  43. if (o && typeof o.length === "number") return {
  44. next: function () {
  45. if (o && i >= o.length) o = void 0;
  46. return { value: o && o[i++], done: !o };
  47. }
  48. };
  49. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  50. };
  51. var __read = (this && this.__read) || function (o, n) {
  52. var m = typeof Symbol === "function" && o[Symbol.iterator];
  53. if (!m) return o;
  54. var i = m.call(o), r, ar = [], e;
  55. try {
  56. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  57. }
  58. catch (error) { e = { error: error }; }
  59. finally {
  60. try {
  61. if (r && !r.done && (m = i["return"])) m.call(i);
  62. }
  63. finally { if (e) throw e.error; }
  64. }
  65. return ar;
  66. };
  67. var _a;
  68. Object.defineProperty(exports, "__esModule", { value: true });
  69. exports.CHTMLWrapper = exports.SPACE = exports.FONTSIZE = void 0;
  70. var LENGTHS = __importStar(require("../../util/lengths.js"));
  71. var Wrapper_js_1 = require("../common/Wrapper.js");
  72. var BBox_js_1 = require("../../util/BBox.js");
  73. exports.FONTSIZE = {
  74. '70.7%': 's',
  75. '70%': 's',
  76. '50%': 'ss',
  77. '60%': 'Tn',
  78. '85%': 'sm',
  79. '120%': 'lg',
  80. '144%': 'Lg',
  81. '173%': 'LG',
  82. '207%': 'hg',
  83. '249%': 'HG'
  84. };
  85. exports.SPACE = (_a = {},
  86. _a[LENGTHS.em(2 / 18)] = '1',
  87. _a[LENGTHS.em(3 / 18)] = '2',
  88. _a[LENGTHS.em(4 / 18)] = '3',
  89. _a[LENGTHS.em(5 / 18)] = '4',
  90. _a[LENGTHS.em(6 / 18)] = '5',
  91. _a);
  92. var CHTMLWrapper = (function (_super) {
  93. __extends(CHTMLWrapper, _super);
  94. function CHTMLWrapper() {
  95. var _this = _super !== null && _super.apply(this, arguments) || this;
  96. _this.chtml = null;
  97. return _this;
  98. }
  99. CHTMLWrapper.prototype.toCHTML = function (parent) {
  100. var e_1, _a;
  101. var chtml = this.standardCHTMLnode(parent);
  102. try {
  103. for (var _b = __values(this.childNodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  104. var child = _c.value;
  105. child.toCHTML(chtml);
  106. }
  107. }
  108. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  109. finally {
  110. try {
  111. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  112. }
  113. finally { if (e_1) throw e_1.error; }
  114. }
  115. };
  116. CHTMLWrapper.prototype.standardCHTMLnode = function (parent) {
  117. this.markUsed();
  118. var chtml = this.createCHTMLnode(parent);
  119. this.handleStyles();
  120. this.handleVariant();
  121. this.handleScale();
  122. this.handleColor();
  123. this.handleSpace();
  124. this.handleAttributes();
  125. this.handlePWidth();
  126. return chtml;
  127. };
  128. CHTMLWrapper.prototype.markUsed = function () {
  129. this.jax.wrapperUsage.add(this.kind);
  130. };
  131. CHTMLWrapper.prototype.createCHTMLnode = function (parent) {
  132. var href = this.node.attributes.get('href');
  133. if (href) {
  134. parent = this.adaptor.append(parent, this.html('a', { href: href }));
  135. }
  136. this.chtml = this.adaptor.append(parent, this.html('mjx-' + this.node.kind));
  137. return this.chtml;
  138. };
  139. CHTMLWrapper.prototype.handleStyles = function () {
  140. if (!this.styles)
  141. return;
  142. var styles = this.styles.cssText;
  143. if (styles) {
  144. this.adaptor.setAttribute(this.chtml, 'style', styles);
  145. var family = this.styles.get('font-family');
  146. if (family) {
  147. this.adaptor.setStyle(this.chtml, 'font-family', 'MJXZERO, ' + family);
  148. }
  149. }
  150. };
  151. CHTMLWrapper.prototype.handleVariant = function () {
  152. if (this.node.isToken && this.variant !== '-explicitFont') {
  153. this.adaptor.setAttribute(this.chtml, 'class', (this.font.getVariant(this.variant) || this.font.getVariant('normal')).classes);
  154. }
  155. };
  156. CHTMLWrapper.prototype.handleScale = function () {
  157. this.setScale(this.chtml, this.bbox.rscale);
  158. };
  159. CHTMLWrapper.prototype.setScale = function (chtml, rscale) {
  160. var scale = (Math.abs(rscale - 1) < .001 ? 1 : rscale);
  161. if (chtml && scale !== 1) {
  162. var size = this.percent(scale);
  163. if (exports.FONTSIZE[size]) {
  164. this.adaptor.setAttribute(chtml, 'size', exports.FONTSIZE[size]);
  165. }
  166. else {
  167. this.adaptor.setStyle(chtml, 'fontSize', size);
  168. }
  169. }
  170. return chtml;
  171. };
  172. CHTMLWrapper.prototype.handleSpace = function () {
  173. var e_2, _a;
  174. try {
  175. for (var _b = __values([[this.bbox.L, 'space', 'marginLeft'],
  176. [this.bbox.R, 'rspace', 'marginRight']]), _c = _b.next(); !_c.done; _c = _b.next()) {
  177. var data = _c.value;
  178. var _d = __read(data, 3), dimen = _d[0], name_1 = _d[1], margin = _d[2];
  179. if (dimen) {
  180. var space = this.em(dimen);
  181. if (exports.SPACE[space]) {
  182. this.adaptor.setAttribute(this.chtml, name_1, exports.SPACE[space]);
  183. }
  184. else {
  185. this.adaptor.setStyle(this.chtml, margin, space);
  186. }
  187. }
  188. }
  189. }
  190. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  191. finally {
  192. try {
  193. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  194. }
  195. finally { if (e_2) throw e_2.error; }
  196. }
  197. };
  198. CHTMLWrapper.prototype.handleColor = function () {
  199. var attributes = this.node.attributes;
  200. var mathcolor = attributes.getExplicit('mathcolor');
  201. var color = attributes.getExplicit('color');
  202. var mathbackground = attributes.getExplicit('mathbackground');
  203. var background = attributes.getExplicit('background');
  204. if (mathcolor || color) {
  205. this.adaptor.setStyle(this.chtml, 'color', mathcolor || color);
  206. }
  207. if (mathbackground || background) {
  208. this.adaptor.setStyle(this.chtml, 'backgroundColor', mathbackground || background);
  209. }
  210. };
  211. CHTMLWrapper.prototype.handleAttributes = function () {
  212. var e_3, _a, e_4, _b;
  213. var attributes = this.node.attributes;
  214. var defaults = attributes.getAllDefaults();
  215. var skip = CHTMLWrapper.skipAttributes;
  216. try {
  217. for (var _c = __values(attributes.getExplicitNames()), _d = _c.next(); !_d.done; _d = _c.next()) {
  218. var name_2 = _d.value;
  219. if (skip[name_2] === false || (!(name_2 in defaults) && !skip[name_2] &&
  220. !this.adaptor.hasAttribute(this.chtml, name_2))) {
  221. this.adaptor.setAttribute(this.chtml, name_2, attributes.getExplicit(name_2));
  222. }
  223. }
  224. }
  225. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  226. finally {
  227. try {
  228. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  229. }
  230. finally { if (e_3) throw e_3.error; }
  231. }
  232. if (attributes.get('class')) {
  233. var names = attributes.get('class').trim().split(/ +/);
  234. try {
  235. for (var names_1 = __values(names), names_1_1 = names_1.next(); !names_1_1.done; names_1_1 = names_1.next()) {
  236. var name_3 = names_1_1.value;
  237. this.adaptor.addClass(this.chtml, name_3);
  238. }
  239. }
  240. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  241. finally {
  242. try {
  243. if (names_1_1 && !names_1_1.done && (_b = names_1.return)) _b.call(names_1);
  244. }
  245. finally { if (e_4) throw e_4.error; }
  246. }
  247. }
  248. };
  249. CHTMLWrapper.prototype.handlePWidth = function () {
  250. if (this.bbox.pwidth) {
  251. if (this.bbox.pwidth === BBox_js_1.BBox.fullWidth) {
  252. this.adaptor.setAttribute(this.chtml, 'width', 'full');
  253. }
  254. else {
  255. this.adaptor.setStyle(this.chtml, 'width', this.bbox.pwidth);
  256. }
  257. }
  258. };
  259. CHTMLWrapper.prototype.setIndent = function (chtml, align, shift) {
  260. var adaptor = this.adaptor;
  261. if (align === 'center' || align === 'left') {
  262. var L = this.getBBox().L;
  263. adaptor.setStyle(chtml, 'margin-left', this.em(shift + L));
  264. }
  265. if (align === 'center' || align === 'right') {
  266. var R = this.getBBox().R;
  267. adaptor.setStyle(chtml, 'margin-right', this.em(-shift + R));
  268. }
  269. };
  270. CHTMLWrapper.prototype.drawBBox = function () {
  271. var _a = this.getBBox(), w = _a.w, h = _a.h, d = _a.d, R = _a.R;
  272. var box = this.html('mjx-box', { style: {
  273. opacity: .25, 'margin-left': this.em(-w - R)
  274. } }, [
  275. this.html('mjx-box', { style: {
  276. height: this.em(h),
  277. width: this.em(w),
  278. 'background-color': 'red'
  279. } }),
  280. this.html('mjx-box', { style: {
  281. height: this.em(d),
  282. width: this.em(w),
  283. 'margin-left': this.em(-w),
  284. 'vertical-align': this.em(-d),
  285. 'background-color': 'green'
  286. } })
  287. ]);
  288. var node = this.chtml || this.parent.chtml;
  289. var size = this.adaptor.getAttribute(node, 'size');
  290. if (size) {
  291. this.adaptor.setAttribute(box, 'size', size);
  292. }
  293. var fontsize = this.adaptor.getStyle(node, 'fontSize');
  294. if (fontsize) {
  295. this.adaptor.setStyle(box, 'fontSize', fontsize);
  296. }
  297. this.adaptor.append(this.adaptor.parent(node), box);
  298. this.adaptor.setStyle(node, 'backgroundColor', '#FFEE00');
  299. };
  300. CHTMLWrapper.prototype.html = function (type, def, content) {
  301. if (def === void 0) { def = {}; }
  302. if (content === void 0) { content = []; }
  303. return this.jax.html(type, def, content);
  304. };
  305. CHTMLWrapper.prototype.text = function (text) {
  306. return this.jax.text(text);
  307. };
  308. CHTMLWrapper.prototype.char = function (n) {
  309. return this.font.charSelector(n).substr(1);
  310. };
  311. CHTMLWrapper.kind = 'unknown';
  312. CHTMLWrapper.autoStyle = true;
  313. return CHTMLWrapper;
  314. }(Wrapper_js_1.CommonWrapper));
  315. exports.CHTMLWrapper = CHTMLWrapper;
  316. //# sourceMappingURL=Wrapper.js.map