TextNode.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 CHTMLTextNode wrapper for the TextNode object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {TextNode} from '../../../core/MmlTree/MmlNode.js';
  23. import {CHTMLWrapper, CHTMLConstructor} from '../Wrapper.js';
  24. import {CommonTextNodeMixin} from '../../common/Wrappers/TextNode.js';
  25. import {StyleList} from '../../../util/StyleList.js';
  26. /*****************************************************************/
  27. /**
  28. * The CHTMLTextNode 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 CHTMLTextNode<N, T, D> extends
  36. CommonTextNodeMixin<CHTMLConstructor<any, any, any>>(CHTMLWrapper) {
  37. /**
  38. * The TextNode wrapper
  39. */
  40. public static kind = TextNode.prototype.kind;
  41. /**
  42. * @override
  43. */
  44. public static autoStyle = false;
  45. /**
  46. * @override
  47. */
  48. public static styles: StyleList = {
  49. 'mjx-c': {
  50. display: 'inline-block'
  51. },
  52. 'mjx-utext': {
  53. display: 'inline-block',
  54. padding: '.75em 0 .2em 0'
  55. }
  56. };
  57. /**
  58. * @override
  59. */
  60. public toCHTML(parent: N) {
  61. this.markUsed();
  62. const adaptor = this.adaptor;
  63. const variant = this.parent.variant;
  64. const text = (this.node as TextNode).getText();
  65. if (text.length === 0) return;
  66. if (variant === '-explicitFont') {
  67. adaptor.append(parent, this.jax.unknownText(text, variant, this.getBBox().w));
  68. } else {
  69. const chars = this.remappedText(text, variant);
  70. for (const n of chars) {
  71. const data = this.getVariantChar(variant, n)[3];
  72. const font = (data.f ? ' TEX-' + data.f : '');
  73. const node = (data.unknown ?
  74. this.jax.unknownText(String.fromCodePoint(n), variant) :
  75. this.html('mjx-c', {class: this.char(n) + font}));
  76. adaptor.append(parent, node);
  77. !data.unknown && this.font.charUsage.add([variant, n]);
  78. }
  79. }
  80. }
  81. }