mtext.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2019-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 CommonMtext wrapper mixin for the MmlMtext object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. /*****************************************************************/
  24. /**
  25. * The CommonMtext interface
  26. */
  27. export interface CommonMtext extends AnyWrapper {
  28. }
  29. /**
  30. * Shorthand for the CommonMtext constructor
  31. */
  32. export type MtextConstructor = Constructor<CommonMtext>;
  33. /*****************************************************************/
  34. /**
  35. * The CommonMtext wrapper mixin for the MmlMtext object
  36. *
  37. * @template T The Wrapper class constructor type
  38. */
  39. export function CommonMtextMixin<T extends WrapperConstructor>(Base: T): MtextConstructor & T {
  40. return class extends Base {
  41. /**
  42. * The font-family, weight, and style to use for the variants when mtextInheritFont
  43. * is true or mtextFont is specified. If not in this list, then the font's
  44. * getCssFont() is called. When the font family is not specified (as in these four),
  45. * the inherited or specified font is used.
  46. */
  47. public static INHERITFONTS = {
  48. normal: ['', false, false],
  49. bold: ['', false, true],
  50. italic: ['', true, false],
  51. 'bold-italic': ['', true, true]
  52. };
  53. /**
  54. * @override
  55. */
  56. protected getVariant() {
  57. const options = this.jax.options;
  58. const data = this.jax.math.outputData;
  59. //
  60. // If the font is to be inherited from the surrounding text, check the mathvariant
  61. // and see if it allows for inheritance. If so, set the variant appropriately,
  62. // otherwise get the usual variant.
  63. //
  64. const merror = ((!!data.merrorFamily || !!options.merrorFont) && this.node.Parent.isKind('merror'));
  65. if (!!data.mtextFamily || !!options.mtextFont || merror) {
  66. const variant = this.node.attributes.get('mathvariant') as string;
  67. const font = (this.constructor as any).INHERITFONTS[variant] || this.jax.font.getCssFont(variant);
  68. const family = font[0] || (merror ? data.merrorFamily || options.merrorFont :
  69. data.mtextFamily || options.mtextFont);
  70. this.variant = this.explicitVariant(family, font[2] ? 'bold' : '', font[1] ? 'italic' : '');
  71. return;
  72. }
  73. super.getVariant();
  74. }
  75. };
  76. }