chtml.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements the CHTML OutputJax object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {CommonOutputJax} from './common/OutputJax.js';
  23. import {CommonWrapper} from './common/Wrapper.js';
  24. import {StyleList} from '../util/Styles.js';
  25. import {StyleList as CssStyleList, CssStyles} from '../util/StyleList.js';
  26. import {OptionList} from '../util/Options.js';
  27. import {MathDocument} from '../core/MathDocument.js';
  28. import {MathItem} from '../core/MathItem.js';
  29. import {MmlNode} from '../core/MmlTree/MmlNode.js';
  30. import {CHTMLWrapper} from './chtml/Wrapper.js';
  31. import {CHTMLWrapperFactory} from './chtml/WrapperFactory.js';
  32. import {CHTMLFontData} from './chtml/FontData.js';
  33. import {Usage} from './chtml/Usage.js';
  34. import {TeXFont} from './chtml/fonts/tex.js';
  35. import * as LENGTHS from '../util/lengths.js';
  36. import {unicodeChars} from '../util/string.js';
  37. /*****************************************************************/
  38. /**
  39. * Implements the CHTML class (extends AbstractOutputJax)
  40. *
  41. * @template N The HTMLElement node class
  42. * @template T The Text node class
  43. * @template D The Document class
  44. */
  45. export class CHTML<N, T, D> extends
  46. CommonOutputJax<N, T, D, CHTMLWrapper<N, T, D>, CHTMLWrapperFactory<N, T, D>, CHTMLFontData, typeof CHTMLFontData> {
  47. /**
  48. * The name of this output jax
  49. */
  50. public static NAME: string = 'CHTML';
  51. /**
  52. * @override
  53. */
  54. public static OPTIONS: OptionList = {
  55. ...CommonOutputJax.OPTIONS,
  56. adaptiveCSS: true, // true means only produce CSS that is used in the processed equations
  57. matchFontHeight: true, // true to match ex-height of surrounding font
  58. };
  59. /**
  60. * The default styles for CommonHTML
  61. */
  62. public static commonStyles: CssStyleList = {
  63. 'mjx-container[jax="CHTML"]': {'line-height': 0},
  64. 'mjx-container [space="1"]': {'margin-left': '.111em'},
  65. 'mjx-container [space="2"]': {'margin-left': '.167em'},
  66. 'mjx-container [space="3"]': {'margin-left': '.222em'},
  67. 'mjx-container [space="4"]': {'margin-left': '.278em'},
  68. 'mjx-container [space="5"]': {'margin-left': '.333em'},
  69. 'mjx-container [rspace="1"]': {'margin-right': '.111em'},
  70. 'mjx-container [rspace="2"]': {'margin-right': '.167em'},
  71. 'mjx-container [rspace="3"]': {'margin-right': '.222em'},
  72. 'mjx-container [rspace="4"]': {'margin-right': '.278em'},
  73. 'mjx-container [rspace="5"]': {'margin-right': '.333em'},
  74. 'mjx-container [size="s"]' : {'font-size': '70.7%'},
  75. 'mjx-container [size="ss"]': {'font-size': '50%'},
  76. 'mjx-container [size="Tn"]': {'font-size': '60%'},
  77. 'mjx-container [size="sm"]': {'font-size': '85%'},
  78. 'mjx-container [size="lg"]': {'font-size': '120%'},
  79. 'mjx-container [size="Lg"]': {'font-size': '144%'},
  80. 'mjx-container [size="LG"]': {'font-size': '173%'},
  81. 'mjx-container [size="hg"]': {'font-size': '207%'},
  82. 'mjx-container [size="HG"]': {'font-size': '249%'},
  83. 'mjx-container [width="full"]': {width: '100%'},
  84. 'mjx-box': {display: 'inline-block'},
  85. 'mjx-block': {display: 'block'},
  86. 'mjx-itable': {display: 'inline-table'},
  87. 'mjx-row': {display: 'table-row'},
  88. 'mjx-row > *': {display: 'table-cell'},
  89. //
  90. // These don't have Wrapper subclasses, so add their styles here
  91. //
  92. 'mjx-mtext': {
  93. display: 'inline-block'
  94. },
  95. 'mjx-mstyle': {
  96. display: 'inline-block'
  97. },
  98. 'mjx-merror': {
  99. display: 'inline-block',
  100. color: 'red',
  101. 'background-color': 'yellow'
  102. },
  103. 'mjx-mphantom': {
  104. visibility: 'hidden'
  105. },
  106. //
  107. // WebKit-specific CSS to handle bug with clipped characters.
  108. // (test found at https://browserstrangeness.bitbucket.io/css_hacks.html#safari)
  109. //
  110. '_::-webkit-full-page-media, _:future, :root mjx-container': {
  111. 'will-change': 'opacity'
  112. }
  113. };
  114. /**
  115. * The ID for the stylesheet element for the styles for the SVG output
  116. */
  117. public static STYLESHEETID = 'MJX-CHTML-styles';
  118. /**
  119. * Used to store the CHTMLWrapper factory.
  120. */
  121. public factory: CHTMLWrapperFactory<N, T, D>;
  122. /**
  123. * The usage information for the wrapper classes
  124. */
  125. public wrapperUsage: Usage<string>;
  126. /**
  127. * The CHTML stylesheet, once it is constructed
  128. */
  129. public chtmlStyles: N = null;
  130. /**
  131. * @override
  132. * @constructor
  133. */
  134. constructor(options: OptionList = null) {
  135. super(options, CHTMLWrapperFactory as any, TeXFont);
  136. this.font.adaptiveCSS(this.options.adaptiveCSS);
  137. this.wrapperUsage = new Usage<string>();
  138. }
  139. /**
  140. * @override
  141. */
  142. public escaped(math: MathItem<N, T, D>, html: MathDocument<N, T, D>) {
  143. this.setDocument(html);
  144. return this.html('span', {}, [this.text(math.math)]);
  145. }
  146. /**
  147. * @override
  148. */
  149. public styleSheet(html: MathDocument<N, T, D>) {
  150. if (this.chtmlStyles) {
  151. if (this.options.adaptiveCSS) {
  152. //
  153. // Update the style sheet rules
  154. //
  155. const styles = new CssStyles();
  156. this.addWrapperStyles(styles);
  157. this.updateFontStyles(styles);
  158. this.adaptor.insertRules(this.chtmlStyles, styles.getStyleRules());
  159. }
  160. return this.chtmlStyles; // stylesheet is already added to the document
  161. }
  162. const sheet = this.chtmlStyles = super.styleSheet(html);
  163. this.adaptor.setAttribute(sheet, 'id', CHTML.STYLESHEETID);
  164. this.wrapperUsage.update();
  165. return sheet;
  166. }
  167. /**
  168. * @param {CssStyles} styles The styles to update with newly used character styles
  169. */
  170. protected updateFontStyles(styles: CssStyles) {
  171. styles.addStyles(this.font.updateStyles({}));
  172. }
  173. /**
  174. * @override
  175. */
  176. protected addWrapperStyles(styles: CssStyles) {
  177. if (!this.options.adaptiveCSS) {
  178. super.addWrapperStyles(styles);
  179. return;
  180. }
  181. for (const kind of this.wrapperUsage.update()) {
  182. const wrapper = this.factory.getNodeClass(kind) as any as typeof CommonWrapper;
  183. wrapper && this.addClassStyles(wrapper, styles);
  184. }
  185. }
  186. /**
  187. * @override
  188. */
  189. protected addClassStyles(wrapper: typeof CommonWrapper, styles: CssStyles) {
  190. const CLASS = wrapper as typeof CHTMLWrapper;
  191. if (CLASS.autoStyle && CLASS.kind !== 'unknown') {
  192. styles.addStyles({
  193. ['mjx-' + CLASS.kind]: {
  194. display: 'inline-block',
  195. 'text-align': 'left'
  196. }
  197. });
  198. }
  199. this.wrapperUsage.add(CLASS.kind);
  200. super.addClassStyles(wrapper, styles);
  201. }
  202. /**
  203. * @param {MmlNode} math The MML node whose HTML is to be produced
  204. * @param {N} parent The HTML node to contain the HTML
  205. */
  206. protected processMath(math: MmlNode, parent: N) {
  207. this.factory.wrap(math).toCHTML(parent);
  208. }
  209. /**
  210. * Clear the cache of which items need their styles to be output
  211. */
  212. public clearCache() {
  213. this.cssStyles.clear();
  214. this.font.clearCache();
  215. this.wrapperUsage.clear();
  216. this.chtmlStyles = null;
  217. }
  218. /**
  219. * @override
  220. */
  221. public reset() {
  222. this.clearCache();
  223. }
  224. /*****************************************************************/
  225. /**
  226. * @override
  227. */
  228. public unknownText(text: string, variant: string, width: number = null) {
  229. const styles: StyleList = {};
  230. const scale = 100 / this.math.metrics.scale;
  231. if (scale !== 100) {
  232. styles['font-size'] = this.fixed(scale, 1) + '%';
  233. styles.padding = LENGTHS.em(75 / scale) + ' 0 ' + LENGTHS.em(20 / scale) + ' 0';
  234. }
  235. if (variant !== '-explicitFont') {
  236. const c = unicodeChars(text);
  237. if (c.length !== 1 || c[0] < 0x1D400 || c[0] > 0x1D7FF) {
  238. this.cssFontStyles(this.font.getCssFont(variant), styles);
  239. }
  240. }
  241. //
  242. // Work around Safari bug with the MJXZERO font by forcing the width.
  243. // (If MJXZERO can be made to work with Safari, then remove width parameter
  244. // and call to getBBox().w in TextNode.ts)
  245. //
  246. if (width !== null) {
  247. const metrics = this.math.metrics;
  248. styles.width = Math.round(width * metrics.em * metrics.scale) + 'px';
  249. }
  250. //
  251. return this.html('mjx-utext', {variant: variant, style: styles}, [this.text(text)]);
  252. }
  253. /**
  254. * Measure the width of a text element by placing it in the page
  255. * and looking up its size (fake the height and depth, since we can't measure that)
  256. *
  257. * @override
  258. */
  259. public measureTextNode(textNode: N) {
  260. const adaptor = this.adaptor;
  261. const text = adaptor.clone(textNode);
  262. //
  263. // Work arround Safari bug with the MJXZERO font.
  264. //
  265. adaptor.setStyle(text, 'font-family', adaptor.getStyle(text, 'font-family').replace(/MJXZERO, /g, ''));
  266. //
  267. const style = {position: 'absolute', 'white-space': 'nowrap'};
  268. const node = this.html('mjx-measure-text', {style}, [text]);
  269. adaptor.append(adaptor.parent(this.math.start.node), this.container);
  270. adaptor.append(this.container, node);
  271. let w = adaptor.nodeSize(text, this.math.metrics.em)[0] / this.math.metrics.scale;
  272. adaptor.remove(this.container);
  273. adaptor.remove(node);
  274. return {w: w, h: .75, d: .2};
  275. }
  276. }