BoldsymbolConfiguration.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Configuration file for the boldsymbol package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {MmlNode} from '../../../core/MmlTree/MmlNode.js';
  23. import {Configuration} from '../Configuration.js';
  24. import NodeUtil from '../NodeUtil.js';
  25. import TexParser from '../TexParser.js';
  26. import {TexConstant} from '../TexConstants.js';
  27. import {CommandMap} from '../SymbolMap.js';
  28. import {ParseMethod} from '../Types.js';
  29. import {NodeFactory} from '../NodeFactory.js';
  30. import ParseOptions from '../ParseOptions.js';
  31. let BOLDVARIANT: {[key: string]: string} = {};
  32. BOLDVARIANT[TexConstant.Variant.NORMAL] = TexConstant.Variant.BOLD;
  33. BOLDVARIANT[TexConstant.Variant.ITALIC] = TexConstant.Variant.BOLDITALIC;
  34. BOLDVARIANT[TexConstant.Variant.FRAKTUR] = TexConstant.Variant.BOLDFRAKTUR;
  35. BOLDVARIANT[TexConstant.Variant.SCRIPT] = TexConstant.Variant.BOLDSCRIPT;
  36. BOLDVARIANT[TexConstant.Variant.SANSSERIF] = TexConstant.Variant.BOLDSANSSERIF;
  37. BOLDVARIANT['-tex-calligraphic'] = '-tex-bold-calligraphic';
  38. BOLDVARIANT['-tex-oldstyle'] = '-tex-bold-oldstyle';
  39. BOLDVARIANT['-tex-mathit'] = TexConstant.Variant.BOLDITALIC;
  40. // Namespace
  41. export let BoldsymbolMethods: Record<string, ParseMethod> = {};
  42. /**
  43. * Parse function for boldsymbol macro.
  44. * @param {TexParser} parser The current tex parser.
  45. * @param {string} name The name of the macro.
  46. */
  47. BoldsymbolMethods.Boldsymbol = function(parser: TexParser, name: string) {
  48. let boldsymbol = parser.stack.env['boldsymbol'];
  49. parser.stack.env['boldsymbol'] = true;
  50. let mml = parser.ParseArg(name);
  51. parser.stack.env['boldsymbol'] = boldsymbol;
  52. parser.Push(mml);
  53. };
  54. new CommandMap('boldsymbol', {boldsymbol: 'Boldsymbol'}, BoldsymbolMethods);
  55. /**
  56. * Creates token nodes in bold font if possible.
  57. * @param {NodeFactory} factory The current node factory.
  58. * @param {string} kind The type of token node to create.
  59. * @param {any} def Properties for the node.
  60. * @param {string} text The text content.
  61. * @return {MmlNode} The generated token node.
  62. */
  63. export function createBoldToken(factory: NodeFactory, kind: string,
  64. def: any, text: string): MmlNode {
  65. let token = NodeFactory.createToken(factory, kind, def, text);
  66. if (kind !== 'mtext' &&
  67. factory.configuration.parser.stack.env['boldsymbol']) {
  68. NodeUtil.setProperty(token, 'fixBold', true);
  69. factory.configuration.addNode('fixBold', token);
  70. }
  71. return token;
  72. }
  73. /**
  74. * Postprocessor to rewrite token nodes to bold font, if possible.
  75. * @param {ParseOptions} data The parse options.
  76. */
  77. export function rewriteBoldTokens(arg: {data: ParseOptions}) {
  78. for (let node of arg.data.getList('fixBold')) {
  79. if (NodeUtil.getProperty(node, 'fixBold')) {
  80. let variant = NodeUtil.getAttribute(node, 'mathvariant') as string;
  81. if (variant == null) {
  82. NodeUtil.setAttribute(node, 'mathvariant', TexConstant.Variant.BOLD);
  83. } else {
  84. NodeUtil.setAttribute(node,
  85. 'mathvariant', BOLDVARIANT[variant] || variant);
  86. }
  87. NodeUtil.removeProperties(node, 'fixBold');
  88. }
  89. }
  90. }
  91. export const BoldsymbolConfiguration = Configuration.create(
  92. 'boldsymbol', {
  93. handler: {macro: ['boldsymbol']},
  94. nodes: {'token': createBoldToken},
  95. postprocessors: [rewriteBoldTokens]
  96. }
  97. );