UnicodeConfiguration.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 unicode package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import {EnvList} from '../StackItem.js';
  24. import TexParser from '../TexParser.js';
  25. import TexError from '../TexError.js';
  26. import {CommandMap} from '../SymbolMap.js';
  27. import {ParseMethod} from '../Types.js';
  28. import ParseUtil from '../ParseUtil.js';
  29. import NodeUtil from '../NodeUtil.js';
  30. import {numeric} from '../../../util/Entities.js';
  31. // Namespace
  32. export let UnicodeMethods: Record<string, ParseMethod> = {};
  33. let UnicodeCache: {[key: number]: [number, number, string, number]} = {};
  34. /**
  35. * Parse function for unicode macro.
  36. * @param {TexParser} parser The current tex parser.
  37. * @param {string} name The name of the macro.
  38. */
  39. UnicodeMethods.Unicode = function(parser: TexParser, name: string) {
  40. let HD = parser.GetBrackets(name);
  41. let HDsplit = null;
  42. let font = null;
  43. if (HD) {
  44. if (HD.replace(/ /g, '').
  45. match(/^(\d+(\.\d*)?|\.\d+),(\d+(\.\d*)?|\.\d+)$/)) {
  46. HDsplit = HD.replace(/ /g, '').split(/,/);
  47. font = parser.GetBrackets(name);
  48. } else {
  49. font = HD;
  50. }
  51. }
  52. let n = ParseUtil.trimSpaces(parser.GetArgument(name)).replace(/^0x/, 'x');
  53. if (!n.match(/^(x[0-9A-Fa-f]+|[0-9]+)$/)) {
  54. throw new TexError('BadUnicode', 'Argument to \\unicode must be a number');
  55. }
  56. let N = parseInt(n.match(/^x/) ? '0' + n : n);
  57. if (!UnicodeCache[N]) {
  58. UnicodeCache[N] = [800, 200, font, N];
  59. } else if (!font) {
  60. font = UnicodeCache[N][2];
  61. }
  62. if (HDsplit) {
  63. UnicodeCache[N][0] = Math.floor(parseFloat(HDsplit[0]) * 1000);
  64. UnicodeCache[N][1] = Math.floor(parseFloat(HDsplit[1]) * 1000);
  65. }
  66. let variant = parser.stack.env.font as string;
  67. let def: EnvList = {};
  68. if (font) {
  69. UnicodeCache[N][2] = def.fontfamily = font.replace(/'/g, '\'');
  70. if (variant) {
  71. if (variant.match(/bold/)) {
  72. def.fontweight = 'bold';
  73. }
  74. if (variant.match(/italic|-mathit/)) {
  75. def.fontstyle = 'italic';
  76. }
  77. }
  78. } else if (variant) {
  79. def.mathvariant = variant;
  80. }
  81. let node = parser.create('token', 'mtext', def, numeric(n));
  82. NodeUtil.setProperty(node, 'unicode', true);
  83. parser.Push(node);
  84. };
  85. new CommandMap('unicode', {unicode: 'Unicode'}, UnicodeMethods);
  86. export const UnicodeConfiguration = Configuration.create(
  87. 'unicode', {handler: {macro: ['unicode']}}
  88. );