mfrac.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 SVGmfrac wrapper for the MmlMfrac object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {SVGWrapper, SVGConstructor} from '../Wrapper.js';
  23. import {CommonMfracMixin} from '../../common/Wrappers/mfrac.js';
  24. import {MmlMfrac} from '../../../core/MmlTree/MmlNodes/mfrac.js';
  25. import {SVGmo} from './mo.js';
  26. /*****************************************************************/
  27. /**
  28. * The SVGmfrac wrapper for the MmlMfrac object
  29. *
  30. * @template N The HTMLElement node class
  31. * @template T The Text node class
  32. * @template D The Document class
  33. */
  34. export class SVGmfrac<N, T, D> extends CommonMfracMixin<SVGConstructor<any, any, any>>(SVGWrapper) {
  35. /**
  36. * The mfrac wrapper
  37. */
  38. public static kind = MmlMfrac.prototype.kind;
  39. /**
  40. * An mo element used to render bevelled fractions
  41. */
  42. public bevel: SVGmo<N, T, D>;
  43. /************************************************/
  44. /**
  45. * @override
  46. */
  47. public toSVG(parent: N) {
  48. this.standardSVGnode(parent);
  49. const {linethickness, bevelled} = this.node.attributes.getList('linethickness', 'bevelled');
  50. const display = this.isDisplay();
  51. if (bevelled) {
  52. this.makeBevelled(display);
  53. } else {
  54. const thickness = this.length2em(String(linethickness), .06);
  55. if (thickness === 0) {
  56. this.makeAtop(display);
  57. } else {
  58. this.makeFraction(display, thickness);
  59. }
  60. }
  61. }
  62. /************************************************/
  63. /**
  64. * @param {boolean} display True when fraction is in display mode
  65. * @param {number} t The rule line thickness
  66. */
  67. protected makeFraction(display: boolean, t: number) {
  68. const svg = this.element;
  69. const {numalign, denomalign} = this.node.attributes.getList('numalign', 'denomalign');
  70. const [num, den] = this.childNodes;
  71. const nbox = num.getOuterBBox();
  72. const dbox = den.getOuterBBox();
  73. const tex = this.font.params;
  74. const a = tex.axis_height;
  75. const d = .1; // line's extra left- and right-padding
  76. const pad = (this.node.getProperty('withDelims') ? 0 : tex.nulldelimiterspace);
  77. const W = Math.max((nbox.L + nbox.w + nbox.R) * nbox.rscale,
  78. (dbox.L + dbox.w + dbox.R) * dbox.rscale);
  79. const nx = this.getAlignX(W, nbox, numalign as string) + d + pad;
  80. const dx = this.getAlignX(W, dbox, denomalign as string) + d + pad;
  81. const {T, u, v} = this.getTUV(display, t);
  82. num.toSVG(svg);
  83. num.place(nx, a + T + Math.max(nbox.d * nbox.rscale, u));
  84. den.toSVG(svg);
  85. den.place(dx, a - T - Math.max(dbox.h * dbox.rscale, v));
  86. this.adaptor.append(svg, this.svg('rect', {
  87. width: this.fixed(W + 2 * d), height: this.fixed(t),
  88. x: this.fixed(pad), y: this.fixed(a - t / 2)
  89. }));
  90. }
  91. /************************************************/
  92. /**
  93. * @param {boolean} display True when fraction is in display mode
  94. */
  95. protected makeAtop(display: boolean) {
  96. const svg = this.element;
  97. const {numalign, denomalign} = this.node.attributes.getList('numalign', 'denomalign');
  98. const [num, den] = this.childNodes;
  99. const nbox = num.getOuterBBox();
  100. const dbox = den.getOuterBBox();
  101. const tex = this.font.params;
  102. const pad = (this.node.getProperty('withDelims') ? 0 : tex.nulldelimiterspace);
  103. const W = Math.max((nbox.L + nbox.w + nbox.R) * nbox.rscale,
  104. (dbox.L + dbox.w + dbox.R) * dbox.rscale);
  105. const nx = this.getAlignX(W, nbox, numalign as string) + pad;
  106. const dx = this.getAlignX(W, dbox, denomalign as string) + pad;
  107. const {u, v} = this.getUVQ(display);
  108. num.toSVG(svg);
  109. num.place(nx, u);
  110. den.toSVG(svg);
  111. den.place(dx, -v);
  112. }
  113. /************************************************/
  114. /**
  115. * @param {boolean} display True when fraction is in display mode
  116. */
  117. protected makeBevelled(display: boolean) {
  118. const svg = this.element;
  119. const [num, den] = this.childNodes;
  120. const {u, v, delta, nbox, dbox} = this.getBevelData(display);
  121. const w = (nbox.L + nbox.w + nbox.R) * nbox.rscale;
  122. num.toSVG(svg);
  123. this.bevel.toSVG(svg);
  124. den.toSVG(svg);
  125. num.place(nbox.L * nbox.rscale, u);
  126. this.bevel.place(w - delta / 2, 0);
  127. den.place(w + this.bevel.getOuterBBox().w + dbox.L * dbox.rscale - delta, v);
  128. }
  129. }