math.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 CHTMLmath wrapper for the MmlMath object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {CHTMLWrapper, CHTMLConstructor} from '../Wrapper.js';
  23. import {CommonMathMixin} from '../../common/Wrappers/math.js';
  24. import {MmlMath} from '../../../core/MmlTree/MmlNodes/math.js';
  25. import {StyleList} from '../../../util/StyleList.js';
  26. import {BBox} from '../../../util/BBox.js';
  27. /*****************************************************************/
  28. /**
  29. * The CHTMLmath wrapper for the MmlMath object
  30. *
  31. * @template N The HTMLElement node class
  32. * @template T The Text node class
  33. * @template D The Document class
  34. */
  35. // @ts-ignore
  36. export class CHTMLmath<N, T, D> extends
  37. CommonMathMixin<CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
  38. /**
  39. * The math wrapper
  40. */
  41. public static kind = MmlMath.prototype.kind;
  42. /**
  43. * @override
  44. */
  45. public static styles: StyleList = {
  46. 'mjx-math': {
  47. 'line-height': 0,
  48. 'text-align': 'left',
  49. 'text-indent': 0,
  50. 'font-style': 'normal',
  51. 'font-weight': 'normal',
  52. 'font-size': '100%',
  53. 'font-size-adjust': 'none',
  54. 'letter-spacing': 'normal',
  55. 'border-collapse': 'collapse',
  56. 'word-wrap': 'normal',
  57. 'word-spacing': 'normal',
  58. 'white-space': 'nowrap',
  59. 'direction': 'ltr',
  60. 'padding': '1px 0'
  61. },
  62. 'mjx-container[jax="CHTML"][display="true"]': {
  63. display: 'block',
  64. 'text-align': 'center',
  65. margin: '1em 0'
  66. },
  67. 'mjx-container[jax="CHTML"][display="true"][width="full"]': {
  68. display: 'flex'
  69. },
  70. 'mjx-container[jax="CHTML"][display="true"] mjx-math': {
  71. padding: 0
  72. },
  73. 'mjx-container[jax="CHTML"][justify="left"]': {
  74. 'text-align': 'left'
  75. },
  76. 'mjx-container[jax="CHTML"][justify="right"]': {
  77. 'text-align': 'right'
  78. }
  79. };
  80. /**
  81. * @override
  82. */
  83. public toCHTML(parent: N) {
  84. super.toCHTML(parent);
  85. const chtml = this.chtml;
  86. const adaptor = this.adaptor;
  87. const display = (this.node.attributes.get('display') === 'block');
  88. if (display) {
  89. adaptor.setAttribute(chtml, 'display', 'true');
  90. adaptor.setAttribute(parent, 'display', 'true');
  91. this.handleDisplay(parent);
  92. } else {
  93. this.handleInline(parent);
  94. }
  95. adaptor.addClass(chtml, 'MJX-TEX');
  96. }
  97. /**
  98. * Handle displayed equations (set min-width, and so on).
  99. */
  100. protected handleDisplay(parent: N) {
  101. const adaptor = this.adaptor;
  102. const [align, shift] = this.getAlignShift();
  103. if (align !== 'center') {
  104. adaptor.setAttribute(parent, 'justify', align);
  105. }
  106. if (this.bbox.pwidth === BBox.fullWidth) {
  107. adaptor.setAttribute(parent, 'width', 'full');
  108. if (this.jax.table) {
  109. let {L, w, R} = this.jax.table.getOuterBBox();
  110. if (align === 'right') {
  111. R = Math.max(R || -shift, -shift);
  112. } else if (align === 'left') {
  113. L = Math.max(L || shift, shift);
  114. } else if (align === 'center') {
  115. w += 2 * Math.abs(shift);
  116. }
  117. const W = this.em(Math.max(0, L + w + R));
  118. adaptor.setStyle(parent, 'min-width', W);
  119. adaptor.setStyle(this.jax.table.chtml, 'min-width', W);
  120. }
  121. } else {
  122. this.setIndent(this.chtml, align, shift);
  123. }
  124. }
  125. /**
  126. * Handle in-line expressions
  127. */
  128. protected handleInline(parent: N) {
  129. //
  130. // Transfer right margin to container (for things like $x\hskip -2em y$)
  131. //
  132. const adaptor = this.adaptor;
  133. const margin = adaptor.getStyle(this.chtml, 'margin-right');
  134. if (margin) {
  135. adaptor.setStyle(this.chtml, 'margin-right', '');
  136. adaptor.setStyle(parent, 'margin-right', margin);
  137. adaptor.setStyle(parent, 'width', '0');
  138. }
  139. }
  140. /**
  141. * @override
  142. */
  143. public setChildPWidths(recompute: boolean, w: number = null, clear: boolean = true) {
  144. return (this.parent ? super.setChildPWidths(recompute, w, clear) : false);
  145. }
  146. }