mglyph.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 CommonMglyph wrapper mixin for the MmlMglyph object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {AnyWrapper, WrapperConstructor, Constructor} from '../Wrapper.js';
  23. import {CommonTextNode} from './TextNode.js';
  24. import {TextNode} from '../../../core/MmlTree/MmlNode.js';
  25. import {BBox} from '../../../util/BBox.js';
  26. /*****************************************************************/
  27. /**
  28. * The CommonMglyph interface
  29. */
  30. export interface CommonMglyph extends AnyWrapper {
  31. /**
  32. * The image's width converted to em's
  33. */
  34. width: number;
  35. /**
  36. * The image's height converted to em's
  37. */
  38. height: number;
  39. /*
  40. * The image's valign values converted to em's
  41. */
  42. valign: number;
  43. /**
  44. * TextNode used for deprecated fontfamily/index use case
  45. */
  46. charWrapper: CommonTextNode;
  47. /**
  48. * Obtain the width, height, and valign.
  49. * Note: Currently, the width and height must be specified explicitly, or they default to 1em
  50. * Since loading the image may be asynchronous, it would require a restart.
  51. * A future extension could implement this either by subclassing this object, or
  52. * perhaps as a post-filter on the MathML input jax that adds the needed dimensions
  53. */
  54. getParameters(): void;
  55. }
  56. /**
  57. * Shorthand for the CommonMglyph constructor
  58. */
  59. export type MglyphConstructor = Constructor<CommonMglyph>;
  60. /*****************************************************************/
  61. /**
  62. * The CommonMglyph wrapper mixin for the MmlMglyph object
  63. *
  64. * @template T The Wrapper class constructor type
  65. */
  66. export function CommonMglyphMixin<T extends WrapperConstructor>(Base: T): MglyphConstructor & T {
  67. return class extends Base {
  68. /**
  69. * @override
  70. */
  71. public width: number;
  72. /**
  73. * @override
  74. */
  75. public height: number;
  76. /**
  77. * @override
  78. */
  79. public valign: number;
  80. /**
  81. * @override
  82. */
  83. public charWrapper: CommonTextNode;
  84. /**
  85. * @override
  86. * @constructor
  87. */
  88. constructor(...args: any[]) {
  89. super(...args);
  90. this.getParameters();
  91. }
  92. /**
  93. * @override
  94. */
  95. public getParameters() {
  96. const {width, height, valign, src, index} =
  97. this.node.attributes.getList('width', 'height', 'valign', 'src', 'index');
  98. if (src) {
  99. this.width = (width === 'auto' ? 1 : this.length2em(width));
  100. this.height = (height === 'auto' ? 1 : this.length2em(height));
  101. this.valign = this.length2em(valign || '0');
  102. } else {
  103. const text = String.fromCodePoint(parseInt(index as string));
  104. const mmlFactory = this.node.factory;
  105. this.charWrapper = this.wrap((mmlFactory.create('text') as TextNode).setText(text));
  106. this.charWrapper.parent = this as any as AnyWrapper;
  107. }
  108. }
  109. /**
  110. * @override
  111. */
  112. public computeBBox(bbox: BBox, _recompute: boolean = false) {
  113. if (this.charWrapper) {
  114. bbox.updateFrom(this.charWrapper.getBBox());
  115. } else {
  116. bbox.w = this.width;
  117. bbox.h = this.height + this.valign;
  118. bbox.d = -this.valign;
  119. }
  120. }
  121. };
  122. }