scriptbase.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 a base class for CHTMLmsubsup, CHTMLmunderover
  19. * and their relatives. (Since munderover can become msubsup
  20. * when movablelimits is set, munderover needs to be able to
  21. * do the same thing as msubsup in some cases.)
  22. *
  23. * @author dpvc@mathjax.org (Davide Cervone)
  24. */
  25. import {CHTMLWrapper, CHTMLConstructor} from '../Wrapper.js';
  26. import {CommonScriptbaseMixin} from '../../common/Wrappers/scriptbase.js';
  27. import {BBox} from '../../../util/BBox.js';
  28. import {StyleData} from '../../../util/StyleList.js';
  29. /*****************************************************************/
  30. /**
  31. * A base class for msup/msub/msubsup and munder/mover/munderover
  32. * wrapper implementations
  33. *
  34. * @template N The HTMLElement node class
  35. * @template T The Text node class
  36. * @template D The Document class
  37. */
  38. // @ts-ignore
  39. export class CHTMLscriptbase<N, T, D> extends
  40. CommonScriptbaseMixin<CHTMLWrapper<any, any, any>, CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
  41. /**
  42. * The scriptbase wrapper
  43. */
  44. public static kind = 'scriptbase';
  45. /**
  46. * This gives the common output for msub and msup. It is overridden
  47. * for all the others (msubsup, munder, mover, munderover).
  48. *
  49. * @override
  50. */
  51. public toCHTML(parent: N) {
  52. this.chtml = this.standardCHTMLnode(parent);
  53. const [x, v] = this.getOffset();
  54. const dx = x - (this.baseRemoveIc ? this.baseIc : 0);
  55. const style: StyleData = {'vertical-align': this.em(v)};
  56. if (dx) {
  57. style['margin-left'] = this.em(dx);
  58. }
  59. this.baseChild.toCHTML(this.chtml);
  60. this.scriptChild.toCHTML(this.adaptor.append(this.chtml, this.html('mjx-script', {style})) as N);
  61. }
  62. /**
  63. * @param {N[]} nodes The HTML elements to be centered in a stack
  64. * @param {number[]} dx The x offsets needed to center the elements
  65. */
  66. protected setDeltaW(nodes: N[], dx: number[]) {
  67. for (let i = 0; i < dx.length; i++) {
  68. if (dx[i]) {
  69. this.adaptor.setStyle(nodes[i], 'paddingLeft', this.em(dx[i]));
  70. }
  71. }
  72. }
  73. /**
  74. * @param {N} over The HTML element for the overscript
  75. * @param {BBox} overbox The bbox for the overscript
  76. */
  77. protected adjustOverDepth(over: N, overbox: BBox) {
  78. if (overbox.d >= 0) return;
  79. this.adaptor.setStyle(over, 'marginBottom', this.em(overbox.d * overbox.rscale));
  80. }
  81. /**
  82. * @param {N} under The HTML element for the underscript
  83. * @param {BBox} underbox The bbox for the underscript
  84. */
  85. protected adjustUnderDepth(under: N, underbox: BBox) {
  86. if (underbox.d >= 0) return;
  87. const adaptor = this.adaptor;
  88. const v = this.em(underbox.d);
  89. const box = this.html('mjx-box', {style: {'margin-bottom': v, 'vertical-align': v}});
  90. for (const child of adaptor.childNodes(adaptor.firstChild(under) as N) as N[]) {
  91. adaptor.append(box, child);
  92. }
  93. adaptor.append(adaptor.firstChild(under) as N, box);
  94. }
  95. /**
  96. * @param {N} base The HTML element for the base
  97. * @param {BBox} basebox The bbox for the base
  98. */
  99. protected adjustBaseHeight(base: N, basebox: BBox) {
  100. if (this.node.attributes.get('accent')) {
  101. const minH = this.font.params.x_height * basebox.scale;
  102. if (basebox.h < minH) {
  103. this.adaptor.setStyle(base, 'paddingTop', this.em(minH - basebox.h));
  104. basebox.h = minH;
  105. }
  106. }
  107. }
  108. }