semantics.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 SVGsemantics wrapper for the MmlSemantics object
  19. * and the associated wrappers for annotations
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. import {SVGWrapper, SVGConstructor} from '../Wrapper.js';
  24. import {CommonSemanticsMixin} from '../../common/Wrappers/semantics.js';
  25. import {BBox} from '../../../util/BBox.js';
  26. import {MmlSemantics, MmlAnnotation, MmlAnnotationXML} from '../../../core/MmlTree/MmlNodes/semantics.js';
  27. import {XMLNode} from '../../../core/MmlTree/MmlNode.js';
  28. import {StyleList} from '../../../util/StyleList.js';
  29. /*****************************************************************/
  30. /**
  31. * The SVGsemantics wrapper for the MmlSemantics object
  32. *
  33. * @template N The HTMLElement node class
  34. * @template T The Text node class
  35. * @template D The Document class
  36. */
  37. // @ts-ignore
  38. export class SVGsemantics<N, T, D> extends
  39. CommonSemanticsMixin<SVGConstructor<any, any, any>>(SVGWrapper) {
  40. /**
  41. * The semantics wrapper
  42. */
  43. public static kind = MmlSemantics.prototype.kind;
  44. /**
  45. * @override
  46. */
  47. public toSVG(parent: N) {
  48. const svg = this.standardSVGnode(parent);
  49. if (this.childNodes.length) {
  50. this.childNodes[0].toSVG(svg);
  51. }
  52. }
  53. }
  54. /*****************************************************************/
  55. /**
  56. * The SVGannotation wrapper for the MmlAnnotation object
  57. *
  58. * @template N The HTMLElement node class
  59. * @template T The Text node class
  60. * @template D The Document class
  61. */
  62. export class SVGannotation<N, T, D> extends SVGWrapper<N, T, D> {
  63. /**
  64. * The annotation wrapper
  65. */
  66. public static kind = MmlAnnotation.prototype.kind;
  67. /**
  68. * @override
  69. */
  70. public toSVG(parent: N) {
  71. // FIXME: output as plain text
  72. super.toSVG(parent);
  73. }
  74. /**
  75. * @override
  76. */
  77. public computeBBox() {
  78. // FIXME: compute using the DOM, if possible
  79. return this.bbox;
  80. }
  81. }
  82. /*****************************************************************/
  83. /**
  84. * The SVGannotationXML wrapper for the MmlAnnotationXML object
  85. *
  86. * @template N The HTMLElement node class
  87. * @template T The Text node class
  88. * @template D The Document class
  89. */
  90. export class SVGannotationXML<N, T, D> extends SVGWrapper<N, T, D> {
  91. /**
  92. * The annotation-xml wrapper
  93. */
  94. public static kind = MmlAnnotationXML.prototype.kind;
  95. /**
  96. * @override
  97. */
  98. public static styles: StyleList = {
  99. 'foreignObject[data-mjx-xml]': {
  100. 'font-family': 'initial',
  101. 'line-height': 'normal',
  102. overflow: 'visible'
  103. }
  104. };
  105. }
  106. /*****************************************************************/
  107. /**
  108. * The SVGxml wrapper for the XMLNode object
  109. *
  110. * @template N The HTMLElement node class
  111. * @template T The Text node class
  112. * @template D The Document class
  113. */
  114. export class SVGxml<N, T, D> extends SVGWrapper<N, T, D> {
  115. /**
  116. * The XMLNode wrapper
  117. */
  118. public static kind = XMLNode.prototype.kind;
  119. /**
  120. * Don't include inline-block CSS for this element
  121. */
  122. public static autoStyle = false;
  123. /**
  124. * @override
  125. */
  126. public toSVG(parent: N) {
  127. const xml = this.adaptor.clone((this.node as XMLNode).getXML() as N);
  128. const em = this.jax.math.metrics.em * this.jax.math.metrics.scale;
  129. const scale = this.fixed(1 / em);
  130. const {w, h, d} = this.getBBox();
  131. this.element = this.adaptor.append(parent, this.svg('foreignObject', {
  132. 'data-mjx-xml': true,
  133. y: this.jax.fixed(-h * em) + 'px',
  134. width: this.jax.fixed(w * em) + 'px',
  135. height: this.jax.fixed((h + d) * em) + 'px',
  136. transform: `scale(${scale}) matrix(1 0 0 -1 0 0)`
  137. }, [xml]));
  138. }
  139. /**
  140. * @override
  141. */
  142. public computeBBox(bbox: BBox, _recompute: boolean = false) {
  143. const {w, h, d} = this.jax.measureXMLnode((this.node as XMLNode).getXML() as N);
  144. bbox.w = w;
  145. bbox.h = h;
  146. bbox.d = d;
  147. }
  148. /**
  149. * @override
  150. */
  151. protected getStyles() {}
  152. /**
  153. * @override
  154. */
  155. protected getScale() {}
  156. /**
  157. * @override
  158. */
  159. protected getVariant() {}
  160. }