mmultiscripts.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-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 SVGmmultiscripts wrapper for the MmlMmultiscripts object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {SVGWrapper, Constructor} from '../Wrapper.js';
  23. import {SVGmsubsup} from './msubsup.js';
  24. import {CommonMmultiscriptsMixin} from '../../common/Wrappers/mmultiscripts.js';
  25. import {MmlMmultiscripts} from '../../../core/MmlTree/MmlNodes/mmultiscripts.js';
  26. import {split} from '../../../util/string.js';
  27. /*****************************************************************/
  28. /**
  29. * A function taking two widths and returning an offset of the first in the second
  30. */
  31. export type AlignFunction = (w: number, W: number) => number;
  32. /**
  33. * Get the function for aligning scripts horizontally (left, center, right)
  34. */
  35. export function AlignX(align: string) {
  36. return ({
  37. left: (_w, _W) => 0,
  38. center: (w, W) => (W - w) / 2,
  39. right: (w, W) => W - w
  40. } as {[name: string]: AlignFunction})[align] || ((_w, _W) => 0) as AlignFunction;
  41. }
  42. /*****************************************************************/
  43. /**
  44. * The SVGmmultiscripts wrapper for the MmlMmultiscripts object
  45. *
  46. * @template N The HTMLElement node class
  47. * @template T The Text node class
  48. * @template D The Document class
  49. */
  50. // @ts-ignore
  51. export class SVGmmultiscripts<N, T, D> extends
  52. CommonMmultiscriptsMixin<SVGWrapper<any, any, any>, Constructor<SVGmsubsup<any, any, any>>>(SVGmsubsup) {
  53. /**
  54. * The mmultiscripts wrapper
  55. */
  56. public static kind = MmlMmultiscripts.prototype.kind;
  57. /**
  58. * @override
  59. */
  60. public toSVG(parent: N) {
  61. const svg = this.standardSVGnode(parent);
  62. const data = this.scriptData;
  63. //
  64. // Get the alignment for the scripts
  65. //
  66. const scriptalign = this.node.getProperty('scriptalign') || 'right left';
  67. const [preAlign, postAlign] = split(scriptalign + ' ' + scriptalign);
  68. //
  69. // Combine the bounding boxes of the pre- and post-scripts,
  70. // and get the resulting baseline offsets
  71. //
  72. const sub = this.combinePrePost(data.sub, data.psub);
  73. const sup = this.combinePrePost(data.sup, data.psup);
  74. const [u, v] = this.getUVQ(sub, sup);
  75. //
  76. // Place the pre-scripts, then the base, then the post-scripts
  77. //
  78. let x = 0; // scriptspace
  79. if (data.numPrescripts) {
  80. x = this.addScripts(.05, u, v, this.firstPrescript, data.numPrescripts, preAlign);
  81. }
  82. const base = this.baseChild;
  83. base.toSVG(svg);
  84. base.place(x, 0);
  85. x += base.getOuterBBox().w;
  86. if (data.numScripts) {
  87. this.addScripts(x, u, v, 1, data.numScripts, postAlign);
  88. }
  89. }
  90. /**
  91. * Create a table with the super and subscripts properly separated and aligned.
  92. *
  93. * @param {number} x The x offset of the scripts
  94. * @param {number} u The baseline offset for the superscripts
  95. * @param {number} v The baseline offset for the subscripts
  96. * @param {number} i The starting index for the scripts
  97. * @param {number} n The number of sub/super-scripts
  98. * @param {string} align The alignment for the scripts
  99. * @return {number} The right-hand offset of the scripts
  100. */
  101. protected addScripts(x: number, u: number, v: number, i: number, n: number, align: string): number {
  102. const adaptor = this.adaptor;
  103. const alignX = AlignX(align);
  104. const supRow = adaptor.append(this.element, this.svg('g'));
  105. const subRow = adaptor.append(this.element, this.svg('g'));
  106. this.place(x, u, supRow);
  107. this.place(x, v, subRow);
  108. let m = i + 2 * n;
  109. let dx = 0;
  110. while (i < m) {
  111. const [sub, sup] = [this.childNodes[i++], this.childNodes[i++]];
  112. const [subbox, supbox] = [sub.getOuterBBox(), sup.getOuterBBox()];
  113. const [subr, supr] = [subbox.rscale, supbox.rscale];
  114. const w = Math.max(subbox.w * subr, supbox.w * supr);
  115. sub.toSVG(subRow);
  116. sup.toSVG(supRow);
  117. sub.place(dx + alignX(subbox.w * subr, w), 0);
  118. sup.place(dx + alignX(supbox.w * supr, w), 0);
  119. dx += w;
  120. }
  121. return x + dx;
  122. }
  123. }