-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathUnicodeConfiguration.ts
More file actions
201 lines (193 loc) · 6.01 KB
/
UnicodeConfiguration.ts
File metadata and controls
201 lines (193 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*************************************************************
*
* Copyright (c) 2018-2024 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file Configuration file for the unicode package.
*
* @author v.sorge@mathjax.org (Volker Sorge)
*/
import { HandlerType, ConfigurationType } from '../HandlerTypes.js';
import { Configuration } from '../Configuration.js';
import { EnvList } from '../StackItem.js';
import TexParser from '../TexParser.js';
import TexError from '../TexError.js';
import { CommandMap } from '../TokenMap.js';
import { ParseMethod } from '../Types.js';
import { UnitUtil } from '../UnitUtil.js';
import NodeUtil from '../NodeUtil.js';
import { numeric } from '../../../util/Entities.js';
import { Other } from '../base/BaseConfiguration.js';
const UnicodeCache: { [key: number]: [number, number, string, number] } = {};
// Namespace
const UnicodeMethods: { [key: string]: ParseMethod } = {
/**
* Parse function for unicode macro.
*
* @param {TexParser} parser The current tex parser.
* @param {string} name The name of the macro.
*/
Unicode(parser: TexParser, name: string) {
const HD = parser.GetBrackets(name);
let HDsplit = null;
let font = '';
if (HD) {
if (
HD.replace(/ /g, '').match(/^(\d+(\.\d*)?|\.\d+),(\d+(\.\d*)?|\.\d+)$/)
) {
HDsplit = HD.replace(/ /g, '').split(/,/);
font = parser.GetBrackets(name) || '';
} else {
font = HD;
}
}
if (font.match(/;/)) {
throw new TexError(
'BadFont',
"Font name for %1 can't contain semicolons",
parser.currentCS
);
}
const n = UnitUtil.trimSpaces(parser.GetArgument(name)).replace(/^0x/, 'x');
if (!n.match(/^(x[0-9A-Fa-f]+|[0-9]+)$/)) {
throw new TexError(
'BadUnicode',
'Argument to %1 must be a number',
parser.currentCS
);
}
const N = parseInt(n.match(/^x/) ? '0' + n : n);
if (!UnicodeCache[N]) {
UnicodeCache[N] = [800, 200, font, N];
} else if (!font) {
font = UnicodeCache[N][2];
}
if (HDsplit) {
UnicodeCache[N][0] = Math.floor(parseFloat(HDsplit[0]) * 1000);
UnicodeCache[N][1] = Math.floor(parseFloat(HDsplit[1]) * 1000);
}
const variant = parser.stack.env.font as string;
const def: EnvList = {};
if (font) {
UnicodeCache[N][2] = def.fontfamily = font.replace(/'/g, "'");
if (variant) {
if (variant.match(/bold/)) {
def.fontweight = 'bold';
}
if (variant.match(/italic|-mathit/)) {
def.fontstyle = 'italic';
}
}
} else if (variant) {
def.mathvariant = variant;
}
const node = parser.create('token', 'mtext', def, numeric(n));
NodeUtil.setProperty(node, 'unicode', true);
parser.Push(node);
},
/**
* Create a raw unicode character in TeX input.
*
* @param {TexParser} parser The current tex parser.
* @param {string} name The name of the macro.
*/
RawUnicode(parser: TexParser, name: string) {
const hex = parser.GetArgument(name).trim();
if (!hex.match(/^[0-9A-F]{1,6}$/)) {
throw new TexError(
'BadRawUnicode',
'Argument to %1 must a hexadecimal number with 1 to 6 digits',
parser.currentCS
);
}
const n = parseInt(hex, 16);
parser.string = String.fromCodePoint(n) + parser.string.substring(parser.i);
parser.i = 0;
},
/**
* Implements the \char control sequence.
*
* @param {TexParser} parser The current tex parser.
* @param {string} _name The name of the macro.
*/
Char(parser: TexParser, _name: string) {
let match;
const next = parser.GetNext();
let c = '';
const text = parser.string.substring(parser.i);
if (next === "'") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
match = text.match(/^'([0-7]{1,7}) ?/u);
if (match) {
c = String.fromCodePoint(parseInt(match[1], 8));
}
} else if (next === '"') {
match = text.match(/^"([0-9A-F]{1,6}) ?/);
if (match) {
c = String.fromCodePoint(parseInt(match[1], 16));
}
} else if (next === '`') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
match = text.match(/^`(?:(\\\S)|(.))/u);
if (match) {
if (match[2]) {
c = match[2];
} else {
parser.i += 2;
const cs = [...parser.GetCS()];
if (cs.length > 1) {
throw new TexError(
'InvalidAlphanumeric',
'Invalid alphanumeric constant for %1',
parser.currentCS
);
}
c = cs[0];
match = [''];
}
}
} else {
match = text.match(/^([0-9]{1,7}) ?/);
if (match) {
c = String.fromCodePoint(parseInt(match[1]));
}
}
if (!c) {
throw new TexError(
'MissingNumber',
'Missing numeric constant for %1',
parser.currentCS
);
}
parser.i += match[0].length;
if (c >= '0' && c <= '9') {
parser.Push(parser.create('token', 'mn', {}, c));
} else if (c.match(/[A-Za-z]/)) {
parser.Push(parser.create('token', 'mi', {}, c));
} else {
Other(parser, c);
}
},
};
new CommandMap('unicode', {
unicode: UnicodeMethods.Unicode,
U: UnicodeMethods.RawUnicode,
char: UnicodeMethods.Char,
});
export const UnicodeConfiguration = Configuration.create('unicode', {
[ConfigurationType.HANDLER]: { [HandlerType.MACRO]: ['unicode'] },
});