chtml.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  29. if (k2 === undefined) k2 = k;
  30. var desc = Object.getOwnPropertyDescriptor(m, k);
  31. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  32. desc = { enumerable: true, get: function() { return m[k]; } };
  33. }
  34. Object.defineProperty(o, k2, desc);
  35. }) : (function(o, m, k, k2) {
  36. if (k2 === undefined) k2 = k;
  37. o[k2] = m[k];
  38. }));
  39. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  40. Object.defineProperty(o, "default", { enumerable: true, value: v });
  41. }) : function(o, v) {
  42. o["default"] = v;
  43. });
  44. var __importStar = (this && this.__importStar) || function (mod) {
  45. if (mod && mod.__esModule) return mod;
  46. var result = {};
  47. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  48. __setModuleDefault(result, mod);
  49. return result;
  50. };
  51. var __values = (this && this.__values) || function(o) {
  52. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  53. if (m) return m.call(o);
  54. if (o && typeof o.length === "number") return {
  55. next: function () {
  56. if (o && i >= o.length) o = void 0;
  57. return { value: o && o[i++], done: !o };
  58. }
  59. };
  60. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  61. };
  62. Object.defineProperty(exports, "__esModule", { value: true });
  63. exports.CHTML = void 0;
  64. var OutputJax_js_1 = require("./common/OutputJax.js");
  65. var StyleList_js_1 = require("../util/StyleList.js");
  66. var WrapperFactory_js_1 = require("./chtml/WrapperFactory.js");
  67. var Usage_js_1 = require("./chtml/Usage.js");
  68. var tex_js_1 = require("./chtml/fonts/tex.js");
  69. var LENGTHS = __importStar(require("../util/lengths.js"));
  70. var string_js_1 = require("../util/string.js");
  71. var CHTML = (function (_super) {
  72. __extends(CHTML, _super);
  73. function CHTML(options) {
  74. if (options === void 0) { options = null; }
  75. var _this = _super.call(this, options, WrapperFactory_js_1.CHTMLWrapperFactory, tex_js_1.TeXFont) || this;
  76. _this.chtmlStyles = null;
  77. _this.font.adaptiveCSS(_this.options.adaptiveCSS);
  78. _this.wrapperUsage = new Usage_js_1.Usage();
  79. return _this;
  80. }
  81. CHTML.prototype.escaped = function (math, html) {
  82. this.setDocument(html);
  83. return this.html('span', {}, [this.text(math.math)]);
  84. };
  85. CHTML.prototype.styleSheet = function (html) {
  86. if (this.chtmlStyles) {
  87. if (this.options.adaptiveCSS) {
  88. var styles = new StyleList_js_1.CssStyles();
  89. this.addWrapperStyles(styles);
  90. this.updateFontStyles(styles);
  91. this.adaptor.insertRules(this.chtmlStyles, styles.getStyleRules());
  92. }
  93. return this.chtmlStyles;
  94. }
  95. var sheet = this.chtmlStyles = _super.prototype.styleSheet.call(this, html);
  96. this.adaptor.setAttribute(sheet, 'id', CHTML.STYLESHEETID);
  97. this.wrapperUsage.update();
  98. return sheet;
  99. };
  100. CHTML.prototype.updateFontStyles = function (styles) {
  101. styles.addStyles(this.font.updateStyles({}));
  102. };
  103. CHTML.prototype.addWrapperStyles = function (styles) {
  104. var e_1, _a;
  105. if (!this.options.adaptiveCSS) {
  106. _super.prototype.addWrapperStyles.call(this, styles);
  107. return;
  108. }
  109. try {
  110. for (var _b = __values(this.wrapperUsage.update()), _c = _b.next(); !_c.done; _c = _b.next()) {
  111. var kind = _c.value;
  112. var wrapper = this.factory.getNodeClass(kind);
  113. wrapper && this.addClassStyles(wrapper, styles);
  114. }
  115. }
  116. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  117. finally {
  118. try {
  119. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  120. }
  121. finally { if (e_1) throw e_1.error; }
  122. }
  123. };
  124. CHTML.prototype.addClassStyles = function (wrapper, styles) {
  125. var _a;
  126. var CLASS = wrapper;
  127. if (CLASS.autoStyle && CLASS.kind !== 'unknown') {
  128. styles.addStyles((_a = {},
  129. _a['mjx-' + CLASS.kind] = {
  130. display: 'inline-block',
  131. 'text-align': 'left'
  132. },
  133. _a));
  134. }
  135. this.wrapperUsage.add(CLASS.kind);
  136. _super.prototype.addClassStyles.call(this, wrapper, styles);
  137. };
  138. CHTML.prototype.processMath = function (math, parent) {
  139. this.factory.wrap(math).toCHTML(parent);
  140. };
  141. CHTML.prototype.clearCache = function () {
  142. this.cssStyles.clear();
  143. this.font.clearCache();
  144. this.wrapperUsage.clear();
  145. this.chtmlStyles = null;
  146. };
  147. CHTML.prototype.reset = function () {
  148. this.clearCache();
  149. };
  150. CHTML.prototype.unknownText = function (text, variant, width) {
  151. if (width === void 0) { width = null; }
  152. var styles = {};
  153. var scale = 100 / this.math.metrics.scale;
  154. if (scale !== 100) {
  155. styles['font-size'] = this.fixed(scale, 1) + '%';
  156. styles.padding = LENGTHS.em(75 / scale) + ' 0 ' + LENGTHS.em(20 / scale) + ' 0';
  157. }
  158. if (variant !== '-explicitFont') {
  159. var c = (0, string_js_1.unicodeChars)(text);
  160. if (c.length !== 1 || c[0] < 0x1D400 || c[0] > 0x1D7FF) {
  161. this.cssFontStyles(this.font.getCssFont(variant), styles);
  162. }
  163. }
  164. if (width !== null) {
  165. var metrics = this.math.metrics;
  166. styles.width = Math.round(width * metrics.em * metrics.scale) + 'px';
  167. }
  168. return this.html('mjx-utext', { variant: variant, style: styles }, [this.text(text)]);
  169. };
  170. CHTML.prototype.measureTextNode = function (textNode) {
  171. var adaptor = this.adaptor;
  172. var text = adaptor.clone(textNode);
  173. adaptor.setStyle(text, 'font-family', adaptor.getStyle(text, 'font-family').replace(/MJXZERO, /g, ''));
  174. var style = { position: 'absolute', 'white-space': 'nowrap' };
  175. var node = this.html('mjx-measure-text', { style: style }, [text]);
  176. adaptor.append(adaptor.parent(this.math.start.node), this.container);
  177. adaptor.append(this.container, node);
  178. var w = adaptor.nodeSize(text, this.math.metrics.em)[0] / this.math.metrics.scale;
  179. adaptor.remove(this.container);
  180. adaptor.remove(node);
  181. return { w: w, h: .75, d: .2 };
  182. };
  183. CHTML.NAME = 'CHTML';
  184. CHTML.OPTIONS = __assign(__assign({}, OutputJax_js_1.CommonOutputJax.OPTIONS), { adaptiveCSS: true, matchFontHeight: true });
  185. CHTML.commonStyles = {
  186. 'mjx-container[jax="CHTML"]': { 'line-height': 0 },
  187. 'mjx-container [space="1"]': { 'margin-left': '.111em' },
  188. 'mjx-container [space="2"]': { 'margin-left': '.167em' },
  189. 'mjx-container [space="3"]': { 'margin-left': '.222em' },
  190. 'mjx-container [space="4"]': { 'margin-left': '.278em' },
  191. 'mjx-container [space="5"]': { 'margin-left': '.333em' },
  192. 'mjx-container [rspace="1"]': { 'margin-right': '.111em' },
  193. 'mjx-container [rspace="2"]': { 'margin-right': '.167em' },
  194. 'mjx-container [rspace="3"]': { 'margin-right': '.222em' },
  195. 'mjx-container [rspace="4"]': { 'margin-right': '.278em' },
  196. 'mjx-container [rspace="5"]': { 'margin-right': '.333em' },
  197. 'mjx-container [size="s"]': { 'font-size': '70.7%' },
  198. 'mjx-container [size="ss"]': { 'font-size': '50%' },
  199. 'mjx-container [size="Tn"]': { 'font-size': '60%' },
  200. 'mjx-container [size="sm"]': { 'font-size': '85%' },
  201. 'mjx-container [size="lg"]': { 'font-size': '120%' },
  202. 'mjx-container [size="Lg"]': { 'font-size': '144%' },
  203. 'mjx-container [size="LG"]': { 'font-size': '173%' },
  204. 'mjx-container [size="hg"]': { 'font-size': '207%' },
  205. 'mjx-container [size="HG"]': { 'font-size': '249%' },
  206. 'mjx-container [width="full"]': { width: '100%' },
  207. 'mjx-box': { display: 'inline-block' },
  208. 'mjx-block': { display: 'block' },
  209. 'mjx-itable': { display: 'inline-table' },
  210. 'mjx-row': { display: 'table-row' },
  211. 'mjx-row > *': { display: 'table-cell' },
  212. 'mjx-mtext': {
  213. display: 'inline-block'
  214. },
  215. 'mjx-mstyle': {
  216. display: 'inline-block'
  217. },
  218. 'mjx-merror': {
  219. display: 'inline-block',
  220. color: 'red',
  221. 'background-color': 'yellow'
  222. },
  223. 'mjx-mphantom': {
  224. visibility: 'hidden'
  225. },
  226. '_::-webkit-full-page-media, _:future, :root mjx-container': {
  227. 'will-change': 'opacity'
  228. }
  229. };
  230. CHTML.STYLESHEETID = 'MJX-CHTML-styles';
  231. return CHTML;
  232. }(OutputJax_js_1.CommonOutputJax));
  233. exports.CHTML = CHTML;
  234. //# sourceMappingURL=chtml.js.map