TextNode.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 SVGTextNode wrapper for the TextNode object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {TextNode} from '../../../core/MmlTree/MmlNode.js';
  23. import {SVGWrapper, SVGConstructor} from '../Wrapper.js';
  24. import {CommonTextNodeMixin} from '../../common/Wrappers/TextNode.js';
  25. import {StyleList} from '../../../util/StyleList.js';
  26. /*****************************************************************/
  27. /**
  28. * The SVGTextNode wrapper for the TextNode object
  29. *
  30. * @template N The HTMLElement node class
  31. * @template T The Text node class
  32. * @template D The Document class
  33. */
  34. // @ts-ignore
  35. export class SVGTextNode<N, T, D> extends
  36. CommonTextNodeMixin<SVGConstructor<any, any, any>>(SVGWrapper) {
  37. /**
  38. * The TextNode wrapper
  39. */
  40. public static kind = TextNode.prototype.kind;
  41. /**
  42. * @override
  43. */
  44. public static styles: StyleList = {
  45. 'mjx-container[jax="SVG"] path[data-c], mjx-container[jax="SVG"] use[data-c]': {
  46. 'stroke-width': 3
  47. }
  48. };
  49. /**
  50. * @override
  51. */
  52. public toSVG(parent: N) {
  53. const text = (this.node as TextNode).getText();
  54. const variant = this.parent.variant;
  55. if (text.length === 0) return;
  56. if (variant === '-explicitFont') {
  57. this.element = this.adaptor.append(parent, this.jax.unknownText(text, variant));
  58. } else {
  59. const chars = this.remappedText(text, variant);
  60. if (this.parent.childNodes.length > 1) {
  61. parent = this.element = this.adaptor.append(parent, this.svg('g', {'data-mml-node': 'text'}));
  62. }
  63. let x = 0;
  64. for (const n of chars) {
  65. x += this.placeChar(n, x, 0, parent, variant);
  66. }
  67. }
  68. }
  69. }