rapier-math 2.6.1 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/index.mjs +1 -1
- package/dist/rapier-math.js +227 -122
- package/docs/font-source.md +27 -16
- package/docs/math-components.json +8 -8
- package/docs/math-resources.json +26 -26
- package/package.json +1 -1
- package/src/math/FONT-LICENSE.txt +1 -1
- package/src/math/build-font.py +36 -24
- package/src/math/classic.js +1 -1
- package/src/math/font.js +5 -5
- package/src/math/layout.js +68 -84
- package/src/math/mathml.js +53 -7
- package/src/math/parser.js +102 -25
- package/src/tools/build-math.mjs +3 -3
package/src/math/parser.js
CHANGED
|
@@ -12,6 +12,7 @@ class MathTexError extends Error {
|
|
|
12
12
|
|
|
13
13
|
function mathTexTokens(source, offset = 0) {
|
|
14
14
|
const tokens = [];
|
|
15
|
+
let count = 0;
|
|
15
16
|
for (let i = 0; i < source.length;) {
|
|
16
17
|
const start = i, value = String.fromCodePoint(source.codePointAt(i));
|
|
17
18
|
i += value.length;
|
|
@@ -26,8 +27,27 @@ function mathTexTokens(source, offset = 0) {
|
|
|
26
27
|
let command = '';
|
|
27
28
|
if (/[A-Za-z@]/.test(source[i])) {
|
|
28
29
|
while (i < source.length && /[A-Za-z@]/.test(source[i])) command += source[i++];
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
if (command === 'verb') {
|
|
31
|
+
// Verbatim changes TeX's character categories before its body is read.
|
|
32
|
+
while (source[i] === ' ' || source[i] === '\t') i++;
|
|
33
|
+
const starred = source[i] === '*';
|
|
34
|
+
if (starred) i++;
|
|
35
|
+
while (source[i] === ' ' || source[i] === '\t') i++;
|
|
36
|
+
const delimiter = i < source.length ? String.fromCodePoint(source.codePointAt(i)) : '';
|
|
37
|
+
if (!delimiter || /[\r\n]/.test(delimiter))
|
|
38
|
+
throw new MathTexError('unclosed-verb', 'A verbatim span needs a delimiter on the same line.', offset + start, 'verb');
|
|
39
|
+
i += delimiter.length;
|
|
40
|
+
const end = source.indexOf(delimiter, i), newline = source.slice(i, end < 0 ? undefined : end).search(/[\r\n]/);
|
|
41
|
+
if (end < 0 || newline >= 0)
|
|
42
|
+
throw new MathTexError('unclosed-verb', 'A verbatim span must close on the same line.', offset + start, 'verb');
|
|
43
|
+
if (count + end - i + 1 > 100000)
|
|
44
|
+
throw new MathTexError('resource-limit', 'This formula has too many tokens to render safely.', offset + start, 'verb');
|
|
45
|
+
tokens.push({kind: 'verbatim', value: source.slice(i, end), starred, delimiter, offset: offset + start});
|
|
46
|
+
i = end + delimiter.length;
|
|
47
|
+
} else {
|
|
48
|
+
tokens.push({kind: 'command', value: command, offset: offset + start});
|
|
49
|
+
while (i < source.length && /\s/.test(source[i])) i++;
|
|
50
|
+
}
|
|
31
51
|
} else {
|
|
32
52
|
command = String.fromCodePoint(source.codePointAt(i));
|
|
33
53
|
i += command.length;
|
|
@@ -37,7 +57,8 @@ function mathTexTokens(source, offset = 0) {
|
|
|
37
57
|
while (i < source.length && /\s/.test(source[i])) i++;
|
|
38
58
|
tokens.push({kind: 'space', value: ' ', offset: offset + start});
|
|
39
59
|
} else tokens.push({kind: 'char', value, offset: offset + start});
|
|
40
|
-
|
|
60
|
+
count += tokens.at(-1).kind === 'verbatim' ? 1 + tokens.at(-1).value.length : 1;
|
|
61
|
+
if (count > 100000)
|
|
41
62
|
throw new MathTexError('resource-limit', 'This formula has too many tokens to render safely.', offset + start);
|
|
42
63
|
}
|
|
43
64
|
return tokens;
|
|
@@ -93,6 +114,8 @@ const MATH_TEX_ACCENTS = Object.freeze({
|
|
|
93
114
|
overleftharpoon: ['↼', 'over', true], overrightharpoon: ['⇀', 'over', true], utilde: ['̃', 'under', true]
|
|
94
115
|
});
|
|
95
116
|
const MATH_TEX_LONG_ARROWS = {iff: 'Longleftrightarrow', implies: 'Longrightarrow', impliedby: 'Longleftarrow', And: 'And'};
|
|
117
|
+
// These unicode-math aliases do not expose a binary/relation mathchar to amsmath's raw-token lookahead.
|
|
118
|
+
const MATH_TEX_DOTS_LOW = new Set('circ bullet diamond lor land smallsetminus bigcirc dag ddag bigtriangledown le ge neq owns Join blacktriangleleft blacktriangleright therefore because coloneqq eqqcolon dblcolon gets to'.split(' '));
|
|
96
119
|
// mathtools: a colon or double colon joined to a relation, 1.2 mu apart.
|
|
97
120
|
const MATH_TEX_COLONS = {coloneq: '∶−', Coloneq: '∷−', Coloneqq: '∷=', Eqqcolon: '=∷', eqcolon: '−∶', Eqcolon: '−∷', colonapprox: '∶≈', Colonapprox: '∷≈', colonsim: '∶∼', Colonsim: '∷∼'};
|
|
98
121
|
// dvipsnames in CMYK percent (LaTeX graphics bundle), converted as xcolor does: each RGB channel is 1 − min(1, CMY + K).
|
|
@@ -128,18 +151,24 @@ const MATH_SI_ABBREVIATIONS = Object.freeze(Object.fromEntries(`f,p,n,u,m,,k:g p
|
|
|
128
151
|
}).filter(([name]) => name.length > 1 || /^[gmsAJLlVWNFHCTK]$/.test(name))));
|
|
129
152
|
const MATH_SI_PREFIXES = mathSITable('quecto=q ronto=r yocto=y zepto=z atto=a femto=f pico=p nano=n micro=µ milli=m centi=c deci=d deca=da deka=da hecto=h kilo=k mega=M giga=G tera=T peta=P exa=E zetta=Z yotta=Y ronna=R quetta=Q');
|
|
130
153
|
const MATH_TEX_PACKAGES = new Set(('ams amsmath amssymb amsfonts mathtools color newcommand unicode mhchem physics siunitx bbm bboldx dsfont cancel bbox boldsymbol').split(' '));
|
|
154
|
+
const MATH_TEX_PHYSICS = new Set('dv derivative pdv pderivative fdv functionalderivative dd differential qty quantity pqty bqty Bqty vqty abs absolutevalue norm bra ket braket innerproduct ip ketbra dyad outerproduct op expval expectationvalue ev mel matrixel matrixelement comm commutator acomm anticommutator pb poissonbracket vb va vu vectorbold vectorarrow vectorunit grad gradient divergence curl laplacian eval evaluated order Tr tr trace rank diag Res mqty pmqty bmqty Bmqty Pmqty vmqty smqty dmat diagonalmatrix admat antidiagonalmatrix imat identitymatrix zmat zeromatrix xmat xmatrix pmat paulimatrix qq qc qcc qif qthen qelse qotherwise qunless qgiven qusing qassume qsince qlet qfor qall qeven qodd qinteger qand qor qas qin'.split(' '));
|
|
131
155
|
const MATH_TEX_ENVIRONMENTS = new Set(('matrix pmatrix bmatrix Bmatrix vmatrix Vmatrix smallmatrix psmallmatrix bsmallmatrix Bsmallmatrix vsmallmatrix Vsmallmatrix array aligned alignedat align alignat flalign gathered lgathered rgathered gather split cases dcases rcases drcases subarray substack eqnarray multline multlined equation darray CD').split(' '));
|
|
156
|
+
const MATH_TABLE_DELIMITERS = Object.freeze({pmatrix: ['(', ')'], bmatrix: ['[', ']'], Bmatrix: ['{', '}'], vmatrix: ['|', '|'], Vmatrix: ['‖', '‖'], cases: ['{', ''], rcases: ['', '}']});
|
|
157
|
+
function mathTableDelimiters(environment) { return MATH_TABLE_DELIMITERS[environment.replace(/smallmatrix$/, 'matrix').replace(/^d(r?cases)$/, '$1')]; }
|
|
132
158
|
const MATH_ENCLOSE_NOTATIONS = new Set('box roundedbox circle left right top bottom horizontalstrike verticalstrike updiagonalstrike downdiagonalstrike updiagonalarrow longdiv actuarial madruwb radical phasorangle'.split(' '));
|
|
133
159
|
const MATH_TEX_LITERAL_CLASSES = Object.freeze({
|
|
134
160
|
'+': 'bin', '−': 'bin', '-': 'bin', '±': 'bin', '∓': 'bin', '×': 'bin', '÷': 'bin', '*': 'bin', '⋅': 'bin',
|
|
135
161
|
'=': 'rel', '<': 'rel', '>': 'rel', '≤': 'rel', '≥': 'rel', '≠': 'rel', ':': 'rel',
|
|
136
162
|
'(': 'open', '[': 'open', '{': 'open', '⟨': 'open', ')': 'close', ']': 'close', '}': 'close', '⟩': 'close', ',': 'punct', ';': 'punct',
|
|
163
|
+
'!': 'close',
|
|
137
164
|
'∑': 'op', '∏': 'op', '∫': 'op', '∮': 'op', '∬': 'op', '∭': 'op'
|
|
138
165
|
});
|
|
139
166
|
|
|
140
167
|
function mathTexTokenText(tokens) {
|
|
141
|
-
return tokens.map(token => token.kind === '
|
|
168
|
+
return tokens.map(token => token.kind === 'verbatim' ? '\\verb' + (token.starred ? '*' : '') + token.delimiter + token.value + token.delimiter :
|
|
169
|
+
token.kind === 'command' ? '\\' + token.value + (/^[A-Za-z@]+$/.test(token.value) ? ' ' : '') : token.value).join('');
|
|
142
170
|
}
|
|
171
|
+
function mathChoice(node, context) { return node.choices[context.level ? Math.min(context.level + 1, 3) : context.display ? 0 : 1]; }
|
|
143
172
|
// Whether braces in a token run pair up without closing below their start.
|
|
144
173
|
function mathTexBalanced(tokens) {
|
|
145
174
|
let depth = 0;
|
|
@@ -231,10 +260,12 @@ class MathTexParser {
|
|
|
231
260
|
if (token.kind === 'command' && (Object.hasOwn(MATH_TEX_DECLARATIONS, token.value) || token.value === 'color')) {
|
|
232
261
|
this.take();
|
|
233
262
|
const fields = token.value === 'color' ? {color: this.readColor()} : MATH_TEX_DECLARATIONS[token.value];
|
|
263
|
+
// A math style is an ordered list node: a selected mathchoice can change the style of later atoms.
|
|
264
|
+
if (fields.mathStyle) { children.push(this.node('style', {...fields, declaration: true, body: this.row([])})); continue; }
|
|
234
265
|
// A declaration styles each following atom; the atoms keep their own classes and spacing.
|
|
235
266
|
const rest = this.nested(() => this.parseRow(stop, allowFraction));
|
|
236
267
|
// The first one notes where the declaration stood: TeX keeps no italic correction across it.
|
|
237
|
-
for (const [at, body] of (rest.type === 'row' ? rest.children : [rest]).entries()) children.push(this.
|
|
268
|
+
for (const [at, body] of (rest.type === 'row' ? rest.children : [rest]).entries()) children.push(this.styledDeclaration(fields, body, at === 0));
|
|
238
269
|
break;
|
|
239
270
|
}
|
|
240
271
|
if (token.kind === 'command' && /^(over|atop|choose|brace|brack|above|overwithdelims|atopwithdelims|abovewithdelims)$/.test(token.value)) {
|
|
@@ -251,6 +282,7 @@ class MathTexParser {
|
|
|
251
282
|
}
|
|
252
283
|
if (token.kind === 'char' && (token.value === '^' || token.value === '_' || token.value === "'")) {
|
|
253
284
|
let base = children.pop() || this.row([]);
|
|
285
|
+
if (base.type === 'choice' || base.type === 'style' && base.mathStyle && base.declaration && !base.body.children.length) { children.push(base); base = this.row([]); }
|
|
254
286
|
children.push(this.readScripts(base));
|
|
255
287
|
continue;
|
|
256
288
|
}
|
|
@@ -264,12 +296,21 @@ class MathTexParser {
|
|
|
264
296
|
const atom = this.parseAtom();
|
|
265
297
|
// A symbol remembers where its source begins, so a tap on it can find its place in the source.
|
|
266
298
|
if (atom && (atom.type === 'glyph' || atom.type === 'text') && atom.at === undefined) atom.at = token.offset;
|
|
267
|
-
if (atom) children.push(this.readScripts(atom));
|
|
299
|
+
if (atom) children.push(atom.type === 'choice' ? atom : this.readScripts(atom));
|
|
268
300
|
}
|
|
269
301
|
// An equation tag follows its equation wherever \tag was written.
|
|
270
302
|
const tags = children.filter(child => child?.semanticRole === 'tag');
|
|
271
303
|
return this.row(tags.length ? [...children.filter(child => child?.semanticRole !== 'tag'), ...tags] : children);
|
|
272
304
|
}
|
|
305
|
+
styledDeclaration(fields, body, first) {
|
|
306
|
+
// Font/color assignments are group-local; math-style nodes remain in the list even through these assignments.
|
|
307
|
+
if (body.type === 'style' && body.mathStyle && body.declaration) return body;
|
|
308
|
+
if (body.type === 'choice') {
|
|
309
|
+
for (const branch of body.choices) branch.children = branch.children.map((child, index) => this.styledDeclaration(fields, child, first && index === 0));
|
|
310
|
+
return body;
|
|
311
|
+
}
|
|
312
|
+
return this.node('style', {...fields, body, ...(first ? {declaration: true} : {})});
|
|
313
|
+
}
|
|
273
314
|
readScripts(base) {
|
|
274
315
|
let result = base;
|
|
275
316
|
for (;;) {
|
|
@@ -371,6 +412,7 @@ class MathTexParser {
|
|
|
371
412
|
parseAtom() {
|
|
372
413
|
const token = this.take();
|
|
373
414
|
if (token.kind === 'eof') this.error('missing-argument', 'A mathematical expression is missing.', token);
|
|
415
|
+
if (token.kind === 'verbatim') return this.readVerb(token);
|
|
374
416
|
if (token.kind === 'command') return this.command(token);
|
|
375
417
|
if (token.value === '{') {
|
|
376
418
|
return this.nested(() => {
|
|
@@ -403,15 +445,14 @@ class MathTexParser {
|
|
|
403
445
|
}
|
|
404
446
|
command(token) {
|
|
405
447
|
const name = token.value;
|
|
448
|
+
if (name === 'mathchoice') return this.node('choice', {choices: Array.from({length: 4}, () => {
|
|
449
|
+
if (!this.is('{')) this.error('missing-argument', 'Each math choice needs a braced expression.');
|
|
450
|
+
return this.argument();
|
|
451
|
+
})});
|
|
406
452
|
if (Object.hasOwn(MATH_TEX_SPACES, name)) return this.node('space', {width: MATH_TEX_SPACES[name]});
|
|
407
453
|
// amsmath: \iff, \implies and \impliedby are a long double arrow between thick spaces, one relation.
|
|
408
454
|
if (Object.hasOwn(MATH_TEX_LONG_ARROWS, name)) return this.node('style', {class: 'rel', body: this.row([this.node('space', {width: 5 / 18}), this.node('glyph', MATH_SYMBOLS[MATH_TEX_LONG_ARROWS[name]]), this.node('space', {width: 5 / 18})])});
|
|
409
|
-
|
|
410
|
-
if (name === 'dots') {
|
|
411
|
-
const next = this.rawPeek(), after = next.kind === 'command' && Object.hasOwn(MATH_SYMBOLS, next.value) ? MATH_SYMBOLS[next.value] : null;
|
|
412
|
-
const centred = next.kind === 'char' ? '+-=<>*:'.includes(next.value) : !!after && (['bin', 'rel'].includes(after.class) || /^(?:int|iint|iiint|oint)$/.test(next.value));
|
|
413
|
-
if (centred) return this.node('glyph', MATH_SYMBOLS.cdots);
|
|
414
|
-
}
|
|
455
|
+
if (/^dots[bcimo]?$/.test(name)) return this.readDots(name);
|
|
415
456
|
// amsmath's \colon: a colon 2 mu after the left side and 6 mu before the right, with no punctuation space.
|
|
416
457
|
if (name === 'colon') return this.row([this.node('space', {width: 2 / 18}), this.node('glyph', {value: ':', class: 'ord', variant: 'roman', tokenKind: 'mo'}), this.node('space', {width: 6 / 18})]);
|
|
417
458
|
if (Object.hasOwn(MATH_SYMBOLS, name)) return this.node('glyph', MATH_SYMBOLS[name]);
|
|
@@ -444,12 +485,12 @@ class MathTexParser {
|
|
|
444
485
|
if (align && !/^[lr]$/.test(mathTexTokenText(align).trim())) this.error('invalid-option', 'Continued-fraction alignment must be l or r.', token);
|
|
445
486
|
return this.node('frac', {num: this.argument(), den: this.argument(), bar: true, ...(name === 'dfrac' || name === 'cfrac' ? {style: 'display'} : name === 'tfrac' ? {style: 'text'} : {}), ...(align ? {align: mathTexTokenText(align).trim()} : {}), ...(name === 'cfrac' ? {strut: true} : {})});
|
|
446
487
|
}
|
|
447
|
-
if (/^(binom|dbinom|tbinom)$/.test(name)) return this.node('frac', {num: this.argument(), den: this.argument(), bar: false, left: '(', right: ')', ...(name[0] === 'd' ? {style: 'display'} : name[0] === 't' ? {style: 'text'} : {})});
|
|
488
|
+
if (/^(binom|dbinom|tbinom)$/.test(name)) return this.node('frac', {num: this.argument(), den: this.argument(), bar: false, left: '(', right: ')', textDelimiters: true, ...(name[0] === 'd' ? {style: 'display'} : name[0] === 't' ? {style: 'text'} : {})});
|
|
448
489
|
if (name === 'genfrac') {
|
|
449
490
|
const left = this.delimiterArgument(), right = this.delimiterArgument(), thickness = this.plainArgument(), style = this.plainArgument();
|
|
450
491
|
if (!/^[0-3]?$/.test(style)) this.error('invalid-style', 'The fraction style must be 0, 1, 2 or 3.', token);
|
|
451
492
|
const barThickness = thickness ? this.dimension(thickness, token) : undefined;
|
|
452
|
-
return this.node('frac', {num: this.argument(), den: this.argument(), bar: barThickness !== 0, left, right, ...(barThickness === undefined ? {} : {barThickness}), ...(style ? {style: ['display', 'text', 'script', 'scriptscript'][Number(style)]} : {})});
|
|
493
|
+
return this.node('frac', {num: this.argument(), den: this.argument(), bar: barThickness !== 0, left, right, textDelimiters: true, ...(barThickness === undefined ? {} : {barThickness}), ...(style ? {style: ['display', 'text', 'script', 'scriptscript'][Number(style)]} : {})});
|
|
453
494
|
}
|
|
454
495
|
if (name === 'sqrt') {
|
|
455
496
|
const index = this.optionalRaw();
|
|
@@ -475,7 +516,7 @@ class MathTexParser {
|
|
|
475
516
|
return value ? this.literal(value, {class: 'rel', delimiter: true, variant: 'roman'}) : this.row([]);
|
|
476
517
|
}
|
|
477
518
|
if (/^(big|Big|bigg|Bigg)[lrm]?$/.test(name)) {
|
|
478
|
-
const value = this.
|
|
519
|
+
const value = this.delimiterArgument(), sizes = {big: 1.2, Big: 1.8, bigg: 2.4, Bigg: 3};
|
|
479
520
|
const base = name.replace(/[lrm]$/, '');
|
|
480
521
|
return this.literal(value, {delimiterSize: sizes[base], class: name.endsWith('l') ? 'open' : name.endsWith('r') ? 'close' : name.endsWith('m') ? 'rel' : 'ord', variant: 'roman'});
|
|
481
522
|
}
|
|
@@ -665,8 +706,13 @@ class MathTexParser {
|
|
|
665
706
|
return this.node('overunder', {base: this.argument(), [name === 'accentset' ? 'over' : 'under']: decoration, accentGap: true});
|
|
666
707
|
}
|
|
667
708
|
if (name === 'slashed') return this.node('accent', {base: this.argument(), value: '/', position: 'through'});
|
|
668
|
-
if (name === '
|
|
669
|
-
if (name === '
|
|
709
|
+
if (name === 'bmod') return this.row([this.node('space', {width: 5 / 18}), this.node('text', {value: 'mod', variant: 'roman', class: 'bin'}), this.node('space', {width: 5 / 18})]);
|
|
710
|
+
if (name === 'mod' || name === 'pmod' || name === 'pod') return this.row([
|
|
711
|
+
this.node('space', {width: (name === 'mod' ? 12 : 8) / 18, displayWidth: 1}),
|
|
712
|
+
...(name === 'mod' ? [] : [this.literal('(')]),
|
|
713
|
+
...(name === 'pod' ? [] : [this.node('text', {value: 'mod', variant: 'roman'}), this.node('space', {width: 1 / 3})]),
|
|
714
|
+
this.argument(), ...(name === 'mod' ? [] : [this.literal(')')])
|
|
715
|
+
]);
|
|
670
716
|
if (name === 'ce') return {...this.chemistry(this.rawArgument()), semanticRole: 'chemistry'};
|
|
671
717
|
if (name === 'pu') return {...this.units(this.rawArgument()), semanticRole: 'units'};
|
|
672
718
|
if (/^(SI|si|num|unit|ang|SIrange|numrange|qtyrange)$/.test(name)) return {...this.siunitx(name, token), semanticRole: 'units'};
|
|
@@ -687,7 +733,7 @@ class MathTexParser {
|
|
|
687
733
|
const integral = () => this.literal('∫', {large: true, limits: 'never', class: 'op'});
|
|
688
734
|
return this.node('style', {class: 'op', limits: 'never', body: this.row([integral(), this.node('space', {width: -1 / 6}), this.node('glyph', MATH_SYMBOLS.cdots), this.node('space', {width: -1 / 6}), integral()])});
|
|
689
735
|
}
|
|
690
|
-
if (
|
|
736
|
+
if (MATH_TEX_PHYSICS.has(name)) return {...this.physics(name, token), semanticRole: 'physics'};
|
|
691
737
|
if (name === 'mmlToken') return this.mathmlToken(token);
|
|
692
738
|
if (name === 'unicode' || name === 'char') return this.character(name, token);
|
|
693
739
|
if (name === 'TeX' || name === 'LaTeX') return this.node('text', {value: name, variant: 'roman'});
|
|
@@ -703,9 +749,34 @@ class MathTexParser {
|
|
|
703
749
|
if (!tokens.length) return '';
|
|
704
750
|
const parser = new MathTexParser(this.source, {tokens, scopes: this.scopes, budget: this.budget});
|
|
705
751
|
const delimiter = parser.readDelimiter();
|
|
706
|
-
if (parser.peek().kind !== 'eof') this.error('invalid-delimiter', 'A
|
|
752
|
+
if (parser.peek().kind !== 'eof') this.error('invalid-delimiter', 'A delimiter must be one symbol.', tokens[0]);
|
|
707
753
|
return delimiter;
|
|
708
754
|
}
|
|
755
|
+
readDots(name) {
|
|
756
|
+
let next = this.rawPeek();
|
|
757
|
+
if (name === 'dots' && next.kind === 'command' && next.value === 'boldsymbol') {
|
|
758
|
+
const frames = this.frames.map(frame => ({...frame}));
|
|
759
|
+
try { this.rawTake(); next = this.rawArgument()[0] || this.eof; }
|
|
760
|
+
finally { this.frames = frames; }
|
|
761
|
+
}
|
|
762
|
+
let macro = next.kind === 'command' && !next.builtin && this.macro(next.value);
|
|
763
|
+
if (macro?.body?.[0]?.builtin) { next = macro.body[0]; macro = null; }
|
|
764
|
+
const first = macro?.body?.[0], command = next.kind === 'command' ? next.value : '',
|
|
765
|
+
centered = macro ? first?.kind === 'command' && /^(mathbin|mathrel|not|DOTSB)$/.test(first.value) :
|
|
766
|
+
next.kind === 'char' ? ['bin', 'rel'].includes(MATH_TEX_LITERAL_CLASSES[next.value]) :
|
|
767
|
+
command === 'not' || Object.hasOwn(MATH_TEX_LONG_ARROWS, command) ||
|
|
768
|
+
!MATH_TEX_DOTS_LOW.has(command) && ['bin', 'rel'].includes(MATH_SYMBOLS[command]?.class),
|
|
769
|
+
integral = name === 'dotsi' || name === 'dots' && (command === 'idotsint' || first?.value === 'DOTSI'),
|
|
770
|
+
raised = integral || name === 'dotsb' || name === 'dotsm' || name === 'dots' && centered,
|
|
771
|
+
closing = next.kind === 'eof' || next.kind === 'char' && /^(\)|\]|\$)$/.test(next.value) ||
|
|
772
|
+
/^(rbrack|\}|rbrace|rangle|rceil|rfloor|rgroup|rmoustache|right|bigr|biggr|Bigr|Biggr)$/.test(command),
|
|
773
|
+
extra = !integral && (name === 'dots' ? !raised && closing : closing || next.kind === 'char' &&
|
|
774
|
+
(name === 'dotsc' ? /^[.;]$/.test(next.value) : /^[,.;]$/.test(next.value))),
|
|
775
|
+
dots = this.node('glyph', raised ? MATH_SYMBOLS.cdots : MATH_SYMBOLS.ldots);
|
|
776
|
+
return integral || extra ? this.node('style', {class: 'inner', body: this.row([
|
|
777
|
+
...(integral ? [this.node('space', {width: -1 / 6})] : []), dots, ...(extra ? [this.node('space', {width: 1 / 6})] : [])
|
|
778
|
+
])}) : dots;
|
|
779
|
+
}
|
|
709
780
|
readLink(token) {
|
|
710
781
|
const href = this.rawArgument().map(item => {
|
|
711
782
|
if (item.kind !== 'command' || /^[#$%&_]$/.test(item.value)) return item.value;
|
|
@@ -1094,6 +1165,14 @@ class MathTexParser {
|
|
|
1094
1165
|
this.frames.push({tokens: replacement, at: 0});
|
|
1095
1166
|
}
|
|
1096
1167
|
|
|
1168
|
+
readVerb(token) {
|
|
1169
|
+
// Raw spans are single tokens, but each copied character consumes the same admission budget as a glyph.
|
|
1170
|
+
this.reserveParts(token.value.length, token);
|
|
1171
|
+
// LaTeX places \verb in a text-size hbox, including when it occurs in a script.
|
|
1172
|
+
return this.node('style', {mathStyle: 'text', body: this.node('text', {
|
|
1173
|
+
value: token.value.replace(/[ \t]/g, token.starred ? '\u2423' : '\u00a0'), variant: 'monospace', verbatim: true, at: token.offset
|
|
1174
|
+
})});
|
|
1175
|
+
}
|
|
1097
1176
|
parseText(variant = 'roman') {
|
|
1098
1177
|
const children = [];
|
|
1099
1178
|
let value = '';
|
|
@@ -1102,6 +1181,7 @@ class MathTexParser {
|
|
|
1102
1181
|
for (;;) {
|
|
1103
1182
|
const token = this.take(false);
|
|
1104
1183
|
if (token.kind === 'eof') break;
|
|
1184
|
+
if (token.kind === 'verbatim') { flush(); children.push(this.readVerb(token)); continue; }
|
|
1105
1185
|
if (token.kind === 'space') { value += ' '; continue; }
|
|
1106
1186
|
if (token.kind === 'char') {
|
|
1107
1187
|
if (token.value === '{') {
|
|
@@ -1179,9 +1259,6 @@ class MathTexParser {
|
|
|
1179
1259
|
return this.row(children);
|
|
1180
1260
|
}
|
|
1181
1261
|
|
|
1182
|
-
physicsCommand(name) {
|
|
1183
|
-
return /^(dv|derivative|pdv|pderivative|fdv|functionalderivative|dd|differential|qty|quantity|pqty|bqty|Bqty|vqty|abs|absolutevalue|norm|bra|ket|braket|innerproduct|ip|ketbra|dyad|outerproduct|op|expval|expectationvalue|ev|mel|matrixel|matrixelement|comm|commutator|acomm|anticommutator|pb|poissonbracket|vb|va|vu|vectorbold|vectorarrow|vectorunit|grad|gradient|divergence|curl|laplacian|eval|evaluated|order|Tr|tr|trace|rank|diag|Res|mqty|pmqty|bmqty|Bmqty|Pmqty|vmqty|smqty|dmat|diagonalmatrix|admat|antidiagonalmatrix|imat|identitymatrix|zmat|zeromatrix|xmat|xmatrix|pmat|paulimatrix|qq|qc|qcc|q(?:if|then|else|otherwise|unless|given|using|assume|since|let|for|all|even|odd|integer|and|or|as|in))$/.test(name);
|
|
1184
|
-
}
|
|
1185
1262
|
physics(name, token) {
|
|
1186
1263
|
const starred = this.is('*'); if (starred) this.take();
|
|
1187
1264
|
if (/^(dv|derivative|pdv|pderivative|fdv|functionalderivative)$/.test(name)) {
|
|
@@ -1609,12 +1686,12 @@ function parseMathTexPreview(source, error, caret = source.length) {
|
|
|
1609
1686
|
// two neighbouring letters is one edit.
|
|
1610
1687
|
const MATH_TEX_STRUCTURE = 'enclose multicolumn hdotsfor intertext frac dfrac tfrac cfrac binom dbinom tbinom genfrac sqrt root left right middle big Big bigg Bigg bigl bigr Bigl Bigr begin end text textbf textit mathrm mathbf operatorname overset underset stackrel buildrel xrightarrow xleftarrow not phantom smash hspace quad qquad color textcolor mathcolor colorbox boxed cancel newcommand renewcommand def ce pu SI si num unit ang SIrange numrange qtyrange dv pdv qty abs norm bra ket braket substack tag label href';
|
|
1611
1688
|
// Every other command the parser accepts: a name being typed toward one of them is not a misspelling.
|
|
1612
|
-
const MATH_TEX_OTHER = 'TeX let char
|
|
1689
|
+
const MATH_TEX_OTHER = 'TeX let char over LaTeX Set unicode bbox matrix pmatrix bmatrix Bmatrix vmatrix Vmatrix smallmatrix cases atop choose brace brack above overwithdelims atopwithdelims abovewithdelims limits nolimits displaylimits long strut operatornamewithlimits mathop stackbin overunderset xlongequal hphantom vphantom mathstrut rlap llap clap mathrlap mathllap mathclap mspace kern mkern hskip mskip rule raise raisebox lower fcolorbox definecolor fbox bcancel xcancel sout cancelto providecommand gdef edef xdef DeclareMathOperator DeclarePairedDelimiter global outer require url notag nonumber relax allowbreak nobreak protect displaystylelimits mathopen mathclose mathbin mathrel mathord mathinner mathpunct Bra Ket Braket set nicefrac sfrac vcenter prescript sideset accentset underaccent slashed mod bmod pmod pod smashoperator mathmakebox idotsint mmlToken textbackslash textasciitilde textasciicircum mathchoice verb';
|
|
1613
1690
|
let mathTexNames;
|
|
1614
1691
|
function mathSuggest(name, environment = false) {
|
|
1615
1692
|
if (typeof name !== 'string' || name.length < 2 || name.length > 32) return undefined;
|
|
1616
1693
|
mathTexNames ||= {command: [...new Set([...MATH_TEX_STRUCTURE.split(' '), ...MATH_TEX_OTHER.split(' '), ...Object.keys(MATH_SYMBOLS), ...Object.keys(MATH_TEX_VARIANTS), ...Object.keys(MATH_TEX_TEXT_VARIANTS),
|
|
1617
|
-
...Object.keys(MATH_TEX_ACCENTS), ...MATH_NAMED_OPERATORS, ...MATH_LIMIT_OPERATORS, ...Object.keys(MATH_TEX_SPACES), ...Object.keys(MATH_TEX_LONG_ARROWS)])], environment: [...MATH_TEX_ENVIRONMENTS]};
|
|
1694
|
+
...Object.keys(MATH_TEX_ACCENTS), ...Object.keys(MATH_TEX_DECLARATIONS), ...Object.keys(MATH_TEX_COLONS), ...MATH_TEX_PHYSICS, ...MATH_NAMED_OPERATORS, ...MATH_LIMIT_OPERATORS, ...Object.keys(MATH_TEX_SPACES), ...Object.keys(MATH_TEX_LONG_ARROWS)])], environment: [...MATH_TEX_ENVIRONMENTS]};
|
|
1618
1695
|
const limit = name.length > 5 ? 2 : 1;
|
|
1619
1696
|
let best, bestDistance = limit + 1;
|
|
1620
1697
|
for (const candidate of mathTexNames[environment ? 'environment' : 'command']) {
|
|
@@ -1631,4 +1708,4 @@ function mathSuggest(name, environment = false) {
|
|
|
1631
1708
|
}
|
|
1632
1709
|
|
|
1633
1710
|
import {MATH_SYMBOLS, MATH_NAMED_OPERATORS, MATH_LIMIT_OPERATORS} from './symbols.js';
|
|
1634
|
-
export {parseMathTex, parseMathTexPreview, mathSuggest};
|
|
1711
|
+
export {parseMathTex, parseMathTexPreview, mathSuggest, mathChoice, mathTableDelimiters};
|
package/src/tools/build-math.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import {mathModules, mathClassicSource} from '../math/package-build.mjs';
|
|
|
12
12
|
import {mathReadme} from '../math/package-readme.mjs';
|
|
13
13
|
|
|
14
14
|
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
15
|
-
export const MATH_VERSION = '2.
|
|
15
|
+
export const MATH_VERSION = '2.7.0';
|
|
16
16
|
export const MATH_BUNDLE = 'rapier-math-' + MATH_VERSION + '.js';
|
|
17
17
|
export const MATH_SOURCES = ['font.js', 'symbols.js', 'parser.js', 'source.js', 'variants.js', 'layout.js', 'fallbacks.js', 'responsive.js', 'api.js', 'browser.js', 'mathml.js', 'semantic-api.js', 'combined.js', 'classic.js'].map(name => 'math/' + name);
|
|
18
18
|
export const MATH_RECIPE = ['math/build-font.py', 'math/FONT-SOURCE.md', 'math/FONT-LICENSE.txt', 'math/package-verify.mjs', 'tools/build-math.mjs', 'math/LICENSE-MIT', 'math/adoption-verify.mjs', 'math/package-build.mjs', 'math/package-layout.mjs', 'math/package-check.mjs', 'math/package-readme.mjs', 'math/README.md', 'math/CHANGELOG.md', 'math/SEMANTICS.md', 'math/semantic-verify.mjs', 'math/typeset-verify.mjs'];
|
|
@@ -30,14 +30,14 @@ const FONT = {
|
|
|
30
30
|
copyright: 'Copyright 2012--2014 for Latin Modern Math OTF by B. Jackowski, P. Strzelczyk and P. Pianowski (on behalf of TeX users groups). This work is released under the GUST Font License -- see http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt for details.',
|
|
31
31
|
licenseSource: 'https://mirror.apps.cam.ac.uk/pub/tex-archive/fonts/lm-math/doc/GUST-FONT-LICENSE.txt',
|
|
32
32
|
licenseFile: 'math/FONT-LICENSE.txt',
|
|
33
|
-
licenseSha256: '
|
|
33
|
+
licenseSha256: 'fb1f153e363d003180781b4a043f34ab76d17f1c338de9229c40149b52cfdf2f',
|
|
34
34
|
};
|
|
35
35
|
const MONO = {
|
|
36
36
|
name: 'Latin Modern Mono', version: '2.004', license: FONT.license,
|
|
37
37
|
source: 'https://ftp.riken.jp/CTAN/fonts/lm/fonts/opentype/public/lm/lmmono10-regular.otf',
|
|
38
38
|
sha256: '22deb6d3be3ffcb40b33f0e010afb256c677d97c2a2bdee1f5740cdc97751558',
|
|
39
39
|
copyright: 'Copyright 2003, 2009 B. Jackowski and J. M. Nowacki (on behalf of TeX users groups). This work is released under the GUST Font License -- see http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt for details.',
|
|
40
|
-
note: 'Monospace
|
|
40
|
+
note: 'Monospace text outlines and advances; complete license in math-font-LICENSE.txt',
|
|
41
41
|
};
|
|
42
42
|
const HOOK = {
|
|
43
43
|
name: 'Latin Modern Math Italic10 Regular', version: '2.003', license: FONT.license,
|