mroot.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 CommonMroot wrapper mixin for the MmlMroot object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {Constructor} from '../../common/Wrapper.js';
  23. import {CommonMsqrt, MsqrtConstructor} from './msqrt.js';
  24. import {CommonMo} from './mo.js';
  25. import {BBox} from '../../../util/BBox.js';
  26. /*****************************************************************/
  27. /**
  28. * The CommonMroot interface
  29. */
  30. export interface CommonMroot extends CommonMsqrt {
  31. }
  32. /**
  33. * Shorthand for the CommonMroot constructor
  34. */
  35. export type MrootConstructor = Constructor<CommonMroot>;
  36. /*****************************************************************/
  37. /**
  38. * The CommonMroot wrapper mixin for the MmlMroot object (extends CommonMsqrt)
  39. *
  40. * @template T The Wrapper class constructor type
  41. */
  42. export function CommonMrootMixin<T extends MsqrtConstructor>(Base: T): MrootConstructor & T {
  43. return class extends Base {
  44. /**
  45. * @override
  46. */
  47. get surd() {
  48. return 2;
  49. }
  50. /**
  51. * @override
  52. */
  53. get root(): number {
  54. return 1;
  55. }
  56. /**
  57. * @override
  58. */
  59. public combineRootBBox(BBOX: BBox, sbox: BBox, H: number) {
  60. const bbox = this.childNodes[this.root].getOuterBBox();
  61. const h = this.getRootDimens(sbox, H)[1];
  62. BBOX.combine(bbox, 0, h);
  63. }
  64. /**
  65. * @override
  66. */
  67. public getRootDimens(sbox: BBox, H: number) {
  68. const surd = this.childNodes[this.surd] as CommonMo;
  69. const bbox = this.childNodes[this.root].getOuterBBox();
  70. const offset = (surd.size < 0 ? .5 : .6) * sbox.w;
  71. const {w, rscale} = bbox;
  72. const W = Math.max(w, offset / rscale);
  73. const dx = Math.max(0, W - w);
  74. const h = this.rootHeight(bbox, sbox, surd.size, H);
  75. const x = W * rscale - offset;
  76. return [x, h, dx];
  77. }
  78. /**
  79. * @param {BBox} rbox The bbox of the root
  80. * @param {BBox} sbox The bbox of the surd
  81. * @param {number} size The size of the surd
  82. * @param {number} H The height of the root as a whole
  83. * @return {number} The height of the root within the surd
  84. */
  85. public rootHeight(rbox: BBox, sbox: BBox, size: number, H: number): number {
  86. const h = sbox.h + sbox.d;
  87. const b = (size < 0 ? 1.9 : .55 * h) - (h - H);
  88. return b + Math.max(0, rbox.d * rbox.rscale);
  89. }
  90. };
  91. }