@syncfusion/ej2-office-common 35.1.37 → 35.1.43
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/dist/ej2-office-common.umd.min.js +2 -2
- package/dist/ej2-office-common.umd.min.js.map +1 -1
- package/dist/es6/ej2-office-common.es2015.js +5421 -6017
- package/dist/es6/ej2-office-common.es2015.js.map +1 -1
- package/dist/es6/ej2-office-common.es5.js +6270 -7203
- package/dist/es6/ej2-office-common.es5.js.map +1 -1
- package/dist/global/ej2-office-common.min.js +2 -2
- package/dist/global/ej2-office-common.min.js.map +1 -1
- package/dist/global/index.d.ts +1 -1
- package/office-common.js +4 -0
- package/package.json +11 -7
- package/src/index.d.ts +3 -17
- package/src/index.js +3 -18
- package/src/office-common/features/index.d.ts +6 -0
- package/src/office-common/features/index.js +6 -0
- package/src/office-common/features/mathml/index.d.ts +25 -0
- package/src/office-common/features/mathml/index.js +22 -0
- package/src/office-common/features/mathml/latex/latex-generator.d.ts +8 -0
- package/src/office-common/features/mathml/latex/latex-generator.js +357 -0
- package/src/office-common/features/mathml/latex/latex-parser.d.ts +87 -0
- package/src/office-common/features/mathml/latex/latex-parser.js +1206 -0
- package/src/office-common/features/mathml/latex/latex-symbols.d.ts +13 -0
- package/src/office-common/features/mathml/latex/latex-symbols.js +681 -0
- package/src/office-common/features/mathml/math-settings.d.ts +54 -0
- package/src/office-common/features/mathml/math-settings.js +174 -0
- package/src/office-common/features/mathml/office-math/adapter.d.ts +59 -0
- package/src/office-common/features/mathml/office-math/adapter.js +6 -0
- package/src/office-common/features/mathml/office-math/basic.d.ts +91 -0
- package/src/office-common/features/mathml/office-math/basic.js +93 -0
- package/src/office-common/features/mathml/office-math/break.d.ts +30 -0
- package/src/office-common/features/mathml/office-math/break.js +75 -0
- package/src/office-common/features/mathml/office-math/collection.d.ts +104 -0
- package/src/office-common/features/mathml/office-math/collection.js +170 -0
- package/src/office-common/features/mathml/office-math/entity.d.ts +12 -0
- package/src/office-common/features/mathml/office-math/entity.js +3 -0
- package/src/office-common/features/mathml/office-math/enums.d.ts +97 -0
- package/src/office-common/features/mathml/office-math/enums.js +108 -0
- package/src/office-common/features/mathml/office-math/format.d.ts +28 -0
- package/src/office-common/features/mathml/office-math/format.js +90 -0
- package/src/office-common/features/mathml/office-math/function-base.d.ts +27 -0
- package/src/office-common/features/mathml/office-math/function-base.js +69 -0
- package/src/office-common/features/mathml/office-math/function-collection.d.ts +29 -0
- package/src/office-common/features/mathml/office-math/function-collection.js +76 -0
- package/src/office-common/features/mathml/office-math/functions-basic.d.ts +118 -0
- package/src/office-common/features/mathml/office-math/functions-basic.js +331 -0
- package/src/office-common/features/mathml/office-math/functions-multi.d.ts +156 -0
- package/src/office-common/features/mathml/office-math/functions-multi.js +407 -0
- package/src/office-common/features/mathml/office-math/index.d.ts +27 -0
- package/src/office-common/features/mathml/office-math/index.js +25 -0
- package/src/office-common/features/mathml/office-math/keys.d.ts +6 -0
- package/src/office-common/features/mathml/office-math/keys.js +20 -0
- package/src/office-common/features/mathml/office-math/math-clone.d.ts +20 -0
- package/src/office-common/features/mathml/office-math/math-clone.js +203 -0
- package/src/office-common/features/mathml/office-math/math-collection.d.ts +19 -0
- package/src/office-common/features/mathml/office-math/math-collection.js +41 -0
- package/src/office-common/features/mathml/office-math/math-equality.d.ts +21 -0
- package/src/office-common/features/mathml/office-math/math-equality.js +115 -0
- package/src/office-common/features/mathml/office-math/math-paragraph.d.ts +58 -0
- package/src/office-common/features/mathml/office-math/math-paragraph.js +126 -0
- package/src/office-common/features/mathml/office-math/math-traversal.d.ts +23 -0
- package/src/office-common/features/mathml/office-math/math-traversal.js +95 -0
- package/src/office-common/features/mathml/office-math/matrix-collections.d.ts +54 -0
- package/src/office-common/features/mathml/office-math/matrix-collections.js +113 -0
- package/src/office-common/features/mathml/office-math/matrix-column.d.ts +43 -0
- package/src/office-common/features/mathml/office-math/matrix-column.js +96 -0
- package/src/office-common/features/mathml/office-math/matrix-row.d.ts +38 -0
- package/src/office-common/features/mathml/office-math/matrix-row.js +71 -0
- package/src/office-common/features/mathml/office-math/matrix.d.ts +62 -0
- package/src/office-common/features/mathml/office-math/matrix.js +158 -0
- package/src/office-common/features/mathml/office-math/office-math.d.ts +55 -0
- package/src/office-common/features/mathml/office-math/office-math.js +116 -0
- package/src/office-common/features/mathml/office-math/owner-holder.d.ts +42 -0
- package/src/office-common/features/mathml/office-math/owner-holder.js +87 -0
- package/src/office-common/features/mathml/office-math/property-store.d.ts +66 -0
- package/src/office-common/features/mathml/office-math/property-store.js +99 -0
- package/src/office-common/features/mathml/office-math/run-element.d.ts +31 -0
- package/src/office-common/features/mathml/office-math/run-element.js +97 -0
- package/src/office-common/features/mathml/office-math/unknown-element.d.ts +37 -0
- package/src/office-common/features/mathml/office-math/unknown-element.js +62 -0
- package/src/office-common/features/mathml/office-math/xml-equal.d.ts +13 -0
- package/src/office-common/features/mathml/office-math/xml-equal.js +24 -0
- package/src/office-common/features/mathml/parse/office-math-parser.d.ts +24 -0
- package/src/office-common/features/mathml/parse/office-math-parser.js +396 -0
- package/src/office-common/features/mathml/parse/office-math-properties.d.ts +165 -0
- package/src/office-common/features/mathml/parse/office-math-properties.js +432 -0
- package/src/office-common/features/mathml/parse/parse-options.d.ts +22 -0
- package/src/office-common/features/mathml/parse/parse-options.js +3 -0
- package/src/office-common/features/mathml/serialize/office-math-properties-writer.d.ts +38 -0
- package/src/office-common/features/mathml/serialize/office-math-properties-writer.js +267 -0
- package/src/office-common/features/mathml/serialize/office-math-serializer.d.ts +23 -0
- package/src/office-common/features/mathml/serialize/office-math-serializer.js +306 -0
- package/src/office-common/features/mathml/serialize/omml-writer.d.ts +123 -0
- package/src/office-common/features/mathml/serialize/omml-writer.js +135 -0
- package/src/office-common/index.d.ts +10 -0
- package/src/office-common/index.js +10 -0
- package/.eslintrc.json +0 -263
- package/.prettierrc.json +0 -5
- package/api-extractor.json +0 -35
- package/core.js +0 -4
- package/encryption.js +0 -4
- package/features.js +0 -4
- package/global.json +0 -6
- package/nuget.config +0 -7
- package/src/all.d.ts +0 -41
- package/src/all.js +0 -38
- package/src/features/index.d.ts +0 -11
- package/src/features/index.js +0 -11
- package/src/features/mathml/index.d.ts +0 -30
- package/src/features/mathml/index.js +0 -22
- package/src/features/mathml/math-factory.d.ts +0 -26
- package/src/features/mathml/math-factory.js +0 -568
- package/src/features/mathml/model/controls.d.ts +0 -896
- package/src/features/mathml/model/controls.js +0 -1541
- package/src/features/mathml/model/math-element.d.ts +0 -173
- package/src/features/mathml/model/math-element.js +0 -315
- package/src/features/mathml/model/roots.d.ts +0 -82
- package/src/features/mathml/model/roots.js +0 -177
- package/src/features/mathml/parse/math-parser.d.ts +0 -43
- package/src/features/mathml/parse/math-parser.js +0 -636
- package/src/features/mathml/serialize/math-serializer.d.ts +0 -34
- package/src/features/mathml/serialize/math-serializer.js +0 -749
- package/tsconfig.build.json +0 -13
- package/tsconfig.harness.json +0 -32
- package/tslint.json +0 -111
- /package/src/{core → office-common/core}/index.d.ts +0 -0
- /package/src/{core → office-common/core}/index.js +0 -0
- /package/src/{core → office-common/core}/xml/index.d.ts +0 -0
- /package/src/{core → office-common/core}/xml/index.js +0 -0
- /package/src/{core → office-common/core}/xml/model.d.ts +0 -0
- /package/src/{core → office-common/core}/xml/model.js +0 -0
- /package/src/{encryption → office-common/encryption}/agile-constants.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/agile-constants.js +0 -0
- /package/src/{encryption → office-common/encryption}/agile-encryption-info.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/agile-encryption-info.js +0 -0
- /package/src/{encryption → office-common/encryption}/agile-password.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/agile-password.js +0 -0
- /package/src/{encryption → office-common/encryption}/cfb-reader.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/cfb-reader.js +0 -0
- /package/src/{encryption → office-common/encryption}/crypto-primitives.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/crypto-primitives.js +0 -0
- /package/src/{encryption → office-common/encryption}/decrypt-encrypted-package.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/decrypt-encrypted-package.js +0 -0
- /package/src/{encryption → office-common/encryption}/encryption-info-xml.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/encryption-info-xml.js +0 -0
- /package/src/{encryption → office-common/encryption}/errors.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/errors.js +0 -0
- /package/src/{encryption → office-common/encryption}/index.d.ts +0 -0
- /package/src/{encryption → office-common/encryption}/index.js +0 -0
- /package/src/{features → office-common/features}/diagrams/constants.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/constants.js +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-data-model.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-data-model.js +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-parsers.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-parsers.js +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-serializers.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-serializers.js +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-views.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/diagram-views.js +0 -0
- /package/src/{features → office-common/features}/diagrams/element-tree.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/element-tree.js +0 -0
- /package/src/{features → office-common/features}/diagrams/errors.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/errors.js +0 -0
- /package/src/{features → office-common/features}/diagrams/index.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/index.js +0 -0
- /package/src/{features → office-common/features}/diagrams/smart-art-catalog.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/smart-art-catalog.js +0 -0
- /package/src/{features → office-common/features}/diagrams/smart-art-diagram.d.ts +0 -0
- /package/src/{features → office-common/features}/diagrams/smart-art-diagram.js +0 -0
- /package/src/{features → office-common/features}/ink/constants.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/constants.js +0 -0
- /package/src/{features → office-common/features}/ink/index.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/index.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-effects.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-effects.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-model.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-model.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-parser.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-parser.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-serializer.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-serializer.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-units.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-units.js +0 -0
- /package/src/{features → office-common/features}/ink/ink-xml.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/ink-xml.js +0 -0
- /package/src/{features → office-common/features}/ink/trace-codec.d.ts +0 -0
- /package/src/{features → office-common/features}/ink/trace-codec.js +0 -0
- /package/src/{features → office-common/features}/mathml/constants.d.ts +0 -0
- /package/src/{features → office-common/features}/mathml/constants.js +0 -0
- /package/src/{features → office-common/features}/mathml/errors.d.ts +0 -0
- /package/src/{features → office-common/features}/mathml/errors.js +0 -0
- /package/src/{features → office-common/features}/mathml/model/enums.d.ts +0 -0
- /package/src/{features → office-common/features}/mathml/model/enums.js +0 -0
- /package/src/{features → office-common/features}/mathml/openxml.d.ts +0 -0
- /package/src/{features → office-common/features}/mathml/openxml.js +0 -0
- /package/src/{features → office-common/features}/smart-art/diagram-xml.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/diagram-xml.js +0 -0
- /package/src/{features → office-common/features}/smart-art/errors.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/errors.js +0 -0
- /package/src/{features → office-common/features}/smart-art/index.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/index.js +0 -0
- /package/src/{features → office-common/features}/smart-art/minimal-samples.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/minimal-samples.js +0 -0
- /package/src/{features → office-common/features}/smart-art/mutators.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/mutators.js +0 -0
- /package/src/{features → office-common/features}/smart-art/namespaces.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/namespaces.js +0 -0
- /package/src/{features → office-common/features}/smart-art/package-graph.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/package-graph.js +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-colors.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-colors.js +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-data.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-data.js +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-layout.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-layout.js +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-rel-ids.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-rel-ids.js +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-style.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/parse-style.js +0 -0
- /package/src/{features → office-common/features}/smart-art/point-text.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/point-text.js +0 -0
- /package/src/{features → office-common/features}/smart-art/register.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/register.js +0 -0
- /package/src/{features → office-common/features}/smart-art/serialize.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/serialize.js +0 -0
- /package/src/{features → office-common/features}/smart-art/types.d.ts +0 -0
- /package/src/{features → office-common/features}/smart-art/types.js +0 -0
|
@@ -0,0 +1,1206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LaTeX → Office Math model parser.
|
|
3
|
+
*
|
|
4
|
+
* Port of compression `LaTeXConverter.ParseMathPara` (Syncfusion.Office MathML).
|
|
5
|
+
* The scanning rules intentionally mirror the .NET implementation (including
|
|
6
|
+
* Microsoft Word's tolerant handling of malformed input, which degrades to plain
|
|
7
|
+
* text runs).
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
import { MathFontType, MathFractionType, MathFunctionType, MathHorizontalAlignment, MathLimitType, MathScriptType, MathStyleType } from '../office-math/enums';
|
|
12
|
+
import { OfficeMathParagraph } from '../office-math/math-paragraph';
|
|
13
|
+
import { getLatexSymbol } from './latex-symbols';
|
|
14
|
+
// Built at runtime: Unicode property escapes need the `u` flag, which ES5 compile targets reject in literals.
|
|
15
|
+
var LETTER = new RegExp('^\\p{L}$', 'u');
|
|
16
|
+
var LETTER_OR_DIGIT = new RegExp('^[\\p{L}\\p{Nd}]$', 'u');
|
|
17
|
+
var GROUP_START = '{';
|
|
18
|
+
var GROUP_END = '}';
|
|
19
|
+
var SQUARE_START = '[';
|
|
20
|
+
var SQUARE_END = ']';
|
|
21
|
+
var CONTROL_START = '\\';
|
|
22
|
+
var SPACE = ' ';
|
|
23
|
+
var MATRIX_ROW_END = '\\\\';
|
|
24
|
+
var MATRIX_COL_END = '&';
|
|
25
|
+
var SUPER = '^';
|
|
26
|
+
var SUB = '_';
|
|
27
|
+
var LOW_LIMIT = '\\below';
|
|
28
|
+
var ACCENTS = {
|
|
29
|
+
'\\dot': 775, '\\ddot': 776, '\\dddot': 8411, '\\hat': 770, '\\widehat': 770, '\\check': 780, '\\acute': 769,
|
|
30
|
+
'\\grave': 768, '\\breve': 774, '\\widetilde': 771, '\\bar': 773, '\\vec': 8407, '\\hvec': 8401,
|
|
31
|
+
};
|
|
32
|
+
var FUNCTIONS = ['\\sin', '\\cos', '\\tan', '\\cot', '\\sec', '\\csc', '\\sinh', '\\cosh', '\\tanh', '\\coth', '\\sech', '\\csch',
|
|
33
|
+
'\\arcsin', '\\arccos', '\\arctan', '\\log', '\\lim', '\\min', '\\max', '\\ln'];
|
|
34
|
+
var NARY = {
|
|
35
|
+
'\\sum': 8721, '\\int': 8747, '\\iint': 8748, '\\iiint': 8749, '\\oint': 8750, '\\oiint': 8751, '\\oiiint': 8752,
|
|
36
|
+
'\\cwint': 8753, '\\coint': 8754, '\\aoint': 8755, '\\prod': 8719, '\\amalg': 8720, '\\bigcap': 8898, '\\bigcup': 8899,
|
|
37
|
+
'\\bigwedge': 8896, '\\bigvee': 8897, '\\bigodot': 10752, '\\bigotimes': 10754, '\\bigoplus': 10753,
|
|
38
|
+
'\\biguplus': 10756, '\\bigudot': 10755,
|
|
39
|
+
};
|
|
40
|
+
var DELIMITERS = {
|
|
41
|
+
'\\lfloor': '\u230A', '\\rfloor': '\u230B', '\\lceil': '\u2308', '\\rceil': '\u2309', '\\langle': '\u2329', '\\rangle': '\u232A',
|
|
42
|
+
'.': '', '|': '|', '(': '(', ')': ')', '[': '[', ']': ']', '{': '{', '}': '}',
|
|
43
|
+
};
|
|
44
|
+
var FONT_COMMANDS = {
|
|
45
|
+
'\\mathbb': MathFontType.DoubleStruck, '\\mathfrak': MathFontType.Fraktur, '\\mathtt': MathFontType.Monospace,
|
|
46
|
+
'\\mathsf': MathFontType.SansSerif, '\\mathcal': MathFontType.Script, '\\mathscr': MathFontType.Script,
|
|
47
|
+
};
|
|
48
|
+
var STYLE_COMMANDS = {
|
|
49
|
+
'\\mathbit': MathStyleType.BoldItalic, '\\mathbf': MathStyleType.Bold,
|
|
50
|
+
};
|
|
51
|
+
/** Mutable run accumulated while scanning; merged while formatting is unchanged. */
|
|
52
|
+
var RunBuf = /** @class */ (function () {
|
|
53
|
+
function RunBuf(text, sty, scr, nor) {
|
|
54
|
+
this.text = text;
|
|
55
|
+
this.sty = sty;
|
|
56
|
+
this.scr = scr;
|
|
57
|
+
this.nor = nor;
|
|
58
|
+
}
|
|
59
|
+
return RunBuf;
|
|
60
|
+
}());
|
|
61
|
+
function finalize(seq, target) {
|
|
62
|
+
seq.forEach(function (i) { return (i instanceof RunBuf ? addRun(i, target) : i.make(target)); });
|
|
63
|
+
}
|
|
64
|
+
function addRun(r, target) {
|
|
65
|
+
var run = target.functions.add(MathFunctionType.RunElement);
|
|
66
|
+
run.text = r.text;
|
|
67
|
+
if (r.sty !== undefined) {
|
|
68
|
+
run.mathFormat.style = r.sty;
|
|
69
|
+
}
|
|
70
|
+
if (r.scr !== undefined) {
|
|
71
|
+
run.mathFormat.font = r.scr;
|
|
72
|
+
}
|
|
73
|
+
if (r.nor) {
|
|
74
|
+
run.mathFormat.hasNormalText = true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function add(target, type) {
|
|
78
|
+
return target.functions.add(type);
|
|
79
|
+
}
|
|
80
|
+
function chr(code) {
|
|
81
|
+
return String.fromCharCode(code);
|
|
82
|
+
}
|
|
83
|
+
function nz(value) {
|
|
84
|
+
return value === null ? '' : value;
|
|
85
|
+
}
|
|
86
|
+
function hasScriptPair(s) {
|
|
87
|
+
return (s.startsWith(SUB) && s.includes(SUPER)) || (s.startsWith(SUPER) && s.includes(SUB));
|
|
88
|
+
}
|
|
89
|
+
function trimBackslash(s) {
|
|
90
|
+
return s.replace(/^\\+|\\+$/g, '');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Stateful LaTeX scanner/parser. One instance per conversion.
|
|
94
|
+
*
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
var LatexParser = /** @class */ (function () {
|
|
98
|
+
function LatexParser() {
|
|
99
|
+
this.latex = '';
|
|
100
|
+
this.pos = 0;
|
|
101
|
+
this.cur = null;
|
|
102
|
+
this.useExisting = false;
|
|
103
|
+
this.current = null;
|
|
104
|
+
this.roots = [];
|
|
105
|
+
this.nor = false;
|
|
106
|
+
}
|
|
107
|
+
/** Appends one `m:oMath` per top-level equation of `latex` to `target`, through its collections and adapter. */
|
|
108
|
+
LatexParser.prototype.populate = function (latex, target) {
|
|
109
|
+
var _this = this;
|
|
110
|
+
this.latex = latex;
|
|
111
|
+
this.parseEquation();
|
|
112
|
+
target.suspendMutations(function () { return _this.roots.forEach(function (r) { return finalize(r, target.maths.add()); }); });
|
|
113
|
+
};
|
|
114
|
+
// ─── Scanner ──────────────────────────────────────────────────────────────
|
|
115
|
+
LatexParser.prototype.next = function () {
|
|
116
|
+
if (this.pos > this.latex.length - 1) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
var el = this.latex[this.pos];
|
|
120
|
+
this.pos++;
|
|
121
|
+
this.cur = el;
|
|
122
|
+
if (el === CONTROL_START) {
|
|
123
|
+
while (this.pos < this.latex.length && (LETTER.test(this.latex[this.pos]) || this.latex[this.pos] === ';')) {
|
|
124
|
+
el += this.latex[this.pos];
|
|
125
|
+
this.pos++;
|
|
126
|
+
}
|
|
127
|
+
this.cur = el;
|
|
128
|
+
}
|
|
129
|
+
return this.cur;
|
|
130
|
+
};
|
|
131
|
+
LatexParser.prototype.ensure = function () {
|
|
132
|
+
if (this.current === null) {
|
|
133
|
+
this.current = [];
|
|
134
|
+
this.roots.push(this.current);
|
|
135
|
+
}
|
|
136
|
+
return this.current;
|
|
137
|
+
};
|
|
138
|
+
LatexParser.prototype.sub = function (text, target) {
|
|
139
|
+
if (text === null) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
var saved = { latex: this.latex, current: this.current, pos: this.pos, cur: this.cur, use: this.useExisting };
|
|
143
|
+
this.latex = text;
|
|
144
|
+
this.current = target;
|
|
145
|
+
this.pos = 0;
|
|
146
|
+
this.cur = null;
|
|
147
|
+
this.useExisting = false;
|
|
148
|
+
this.parseEquation();
|
|
149
|
+
this.latex = saved.latex;
|
|
150
|
+
this.current = saved.current;
|
|
151
|
+
this.pos = saved.pos;
|
|
152
|
+
this.cur = saved.cur;
|
|
153
|
+
this.useExisting = saved.use;
|
|
154
|
+
};
|
|
155
|
+
LatexParser.prototype.fill = function (text) {
|
|
156
|
+
var seq = [];
|
|
157
|
+
this.sub(text, seq);
|
|
158
|
+
return seq;
|
|
159
|
+
};
|
|
160
|
+
LatexParser.prototype.add = function (make) {
|
|
161
|
+
this.ensure().push({ make: make });
|
|
162
|
+
};
|
|
163
|
+
LatexParser.prototype.setRun = function (text) {
|
|
164
|
+
if (text.length === 0) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
var seq = this.ensure();
|
|
168
|
+
var t = text.startsWith('\\') ? getLatexSymbol(text) : text;
|
|
169
|
+
var last = seq[seq.length - 1];
|
|
170
|
+
if (last instanceof RunBuf && last.sty === this.sty && last.scr === this.scr && last.nor === this.nor) {
|
|
171
|
+
last.text += t;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
seq.push(new RunBuf(t, this.sty, this.scr, this.nor));
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
LatexParser.prototype.fallbackRun = function (startPos) {
|
|
178
|
+
this.setRun(this.latex.substring(startPos, this.pos));
|
|
179
|
+
};
|
|
180
|
+
// ─── Group helpers ────────────────────────────────────────────────────────
|
|
181
|
+
LatexParser.prototype.equationString = function () {
|
|
182
|
+
this.cur = this.next();
|
|
183
|
+
var result = null;
|
|
184
|
+
var valid = false;
|
|
185
|
+
var notInGroup = false;
|
|
186
|
+
if (this.cur === GROUP_START) {
|
|
187
|
+
var g = this.group();
|
|
188
|
+
valid = g.ok;
|
|
189
|
+
result = g.text;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
notInGroup = true;
|
|
193
|
+
}
|
|
194
|
+
if (notInGroup && this.cur !== null && this.cur.length === 1 && (LETTER_OR_DIGIT.test(this.cur) || this.cur === SPACE)) {
|
|
195
|
+
result = this.cur;
|
|
196
|
+
}
|
|
197
|
+
else if (!valid) {
|
|
198
|
+
result = null;
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
};
|
|
202
|
+
LatexParser.prototype.group = function () {
|
|
203
|
+
if (this.cur !== GROUP_START) {
|
|
204
|
+
return { ok: false, text: '' };
|
|
205
|
+
}
|
|
206
|
+
var text = '';
|
|
207
|
+
var depth = 1;
|
|
208
|
+
while (depth > 0 && this.pos < this.latex.length) {
|
|
209
|
+
this.cur = this.next();
|
|
210
|
+
if (this.cur === GROUP_START) {
|
|
211
|
+
depth++;
|
|
212
|
+
text += this.cur;
|
|
213
|
+
}
|
|
214
|
+
else if (this.cur === GROUP_END) {
|
|
215
|
+
if (depth !== 1) {
|
|
216
|
+
text += this.cur;
|
|
217
|
+
}
|
|
218
|
+
depth--;
|
|
219
|
+
}
|
|
220
|
+
else if (this.cur === '\\left') {
|
|
221
|
+
var d = this.delimiter();
|
|
222
|
+
text += d.text;
|
|
223
|
+
if (!d.ok) {
|
|
224
|
+
return { ok: false, text: text };
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
text += nz(this.cur);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return { ok: depth === 0, text: text };
|
|
232
|
+
};
|
|
233
|
+
LatexParser.prototype.squareGroup = function () {
|
|
234
|
+
if (this.cur !== SQUARE_START) {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
var text = '';
|
|
238
|
+
var depth = 1;
|
|
239
|
+
while (depth > 0 && this.pos < this.latex.length) {
|
|
240
|
+
this.cur = this.next();
|
|
241
|
+
if (this.cur === SQUARE_START) {
|
|
242
|
+
depth++;
|
|
243
|
+
text += this.cur;
|
|
244
|
+
}
|
|
245
|
+
else if (this.cur === SQUARE_END) {
|
|
246
|
+
if (depth !== 1) {
|
|
247
|
+
text += this.cur;
|
|
248
|
+
}
|
|
249
|
+
depth--;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
text += nz(this.cur);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return text;
|
|
256
|
+
};
|
|
257
|
+
// ─── Main loop ────────────────────────────────────────────────────────────
|
|
258
|
+
LatexParser.prototype.parseEquation = function () {
|
|
259
|
+
while (this.pos < this.latex.length || this.useExisting) {
|
|
260
|
+
if (this.useExisting) {
|
|
261
|
+
this.useExisting = false;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
this.cur = this.next();
|
|
265
|
+
}
|
|
266
|
+
this.dispatch(nz(this.cur), this.cur);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
LatexParser.prototype.dispatch = function (c, raw) {
|
|
270
|
+
if (raw === null) {
|
|
271
|
+
this.parseDefault();
|
|
272
|
+
}
|
|
273
|
+
else if (ACCENTS[c] !== undefined) {
|
|
274
|
+
this.parseAccent();
|
|
275
|
+
}
|
|
276
|
+
else if (c === '\\overline' || c === '\\underline') {
|
|
277
|
+
this.parseBar();
|
|
278
|
+
}
|
|
279
|
+
else if (c === '\\box') {
|
|
280
|
+
this.parseBox();
|
|
281
|
+
}
|
|
282
|
+
else if (c === '\\fbox' || c === '\\boxed') {
|
|
283
|
+
this.parseBorderBox();
|
|
284
|
+
}
|
|
285
|
+
else if (c === '\\left') {
|
|
286
|
+
this.parseDelimiter();
|
|
287
|
+
}
|
|
288
|
+
else if (c === '\\eqarray') {
|
|
289
|
+
this.parseEqArray();
|
|
290
|
+
}
|
|
291
|
+
else if (c === '\\frac' || c === '\\sfrac' || c === '\\binom') {
|
|
292
|
+
this.parseFraction();
|
|
293
|
+
}
|
|
294
|
+
else if (FUNCTIONS.indexOf(c) !== -1) {
|
|
295
|
+
this.parseFunction();
|
|
296
|
+
}
|
|
297
|
+
else if (c === '\\overbrace' || c === '\\underbrace') {
|
|
298
|
+
this.parseGroupChar();
|
|
299
|
+
}
|
|
300
|
+
else if (c === '\\begin') {
|
|
301
|
+
this.parseMatrix();
|
|
302
|
+
}
|
|
303
|
+
else if (NARY[c] !== undefined) {
|
|
304
|
+
this.parseNary();
|
|
305
|
+
}
|
|
306
|
+
else if (c === '\\sqrt') {
|
|
307
|
+
this.parseRadical();
|
|
308
|
+
}
|
|
309
|
+
else if (c === GROUP_START) {
|
|
310
|
+
this.parseGroupStart();
|
|
311
|
+
}
|
|
312
|
+
else if (STYLE_COMMANDS[c] !== undefined) {
|
|
313
|
+
this.parseFormat(c, 'sty', STYLE_COMMANDS[c]);
|
|
314
|
+
}
|
|
315
|
+
else if (FONT_COMMANDS[c] !== undefined) {
|
|
316
|
+
this.parseFormat(c, 'scr', FONT_COMMANDS[c]);
|
|
317
|
+
}
|
|
318
|
+
else if (c === '\\mathrm') {
|
|
319
|
+
this.parseFormat(c, 'nor', true);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
this.parseDefault();
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
LatexParser.prototype.parseDefault = function () {
|
|
326
|
+
var startPos = this.pos;
|
|
327
|
+
var previous = nz(this.cur);
|
|
328
|
+
this.cur = this.next();
|
|
329
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
330
|
+
this.parseScript(previous, startPos - previous.length);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (this.cur !== null) {
|
|
334
|
+
this.useExisting = true;
|
|
335
|
+
}
|
|
336
|
+
if (this.cur === SPACE) {
|
|
337
|
+
if (previous === '\\') {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (previous.startsWith('\\') && getLatexSymbol(previous) !== previous) {
|
|
341
|
+
this.useExisting = false;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
this.setRun(previous);
|
|
345
|
+
};
|
|
346
|
+
LatexParser.prototype.parseFormat = function (start, kind, value) {
|
|
347
|
+
var laTexPos = this.pos;
|
|
348
|
+
this.cur = this.next();
|
|
349
|
+
if (this.cur !== GROUP_START) {
|
|
350
|
+
this.setRun(start);
|
|
351
|
+
this.useExisting = true;
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
var g = this.group();
|
|
355
|
+
if (!g.ok) {
|
|
356
|
+
this.fallbackRun(laTexPos - start.length);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
var old = { sty: this.sty, scr: this.scr, nor: this.nor };
|
|
360
|
+
if (kind === 'sty' && this.sty === undefined) {
|
|
361
|
+
this.sty = value;
|
|
362
|
+
}
|
|
363
|
+
else if (kind === 'scr' && this.scr === undefined) {
|
|
364
|
+
this.scr = value;
|
|
365
|
+
}
|
|
366
|
+
else if (kind === 'nor') {
|
|
367
|
+
this.nor = true;
|
|
368
|
+
}
|
|
369
|
+
this.sub(g.text, this.current);
|
|
370
|
+
this.sty = old.sty;
|
|
371
|
+
this.scr = old.scr;
|
|
372
|
+
this.nor = old.nor;
|
|
373
|
+
};
|
|
374
|
+
/** `\cmd{...}` / `\cmd x` argument shared by accent, bar, box, border box, group char. */
|
|
375
|
+
LatexParser.prototype.oneArg = function () {
|
|
376
|
+
var at = this.pos;
|
|
377
|
+
var start = nz(this.cur);
|
|
378
|
+
var text = this.equationString();
|
|
379
|
+
if (text === SPACE && this.cur === SPACE) {
|
|
380
|
+
text = this.equationString();
|
|
381
|
+
}
|
|
382
|
+
return { start: start, at: at, text: text };
|
|
383
|
+
};
|
|
384
|
+
LatexParser.prototype.parseAccent = function () {
|
|
385
|
+
var _a = this.oneArg(), start = _a.start, at = _a.at, text = _a.text;
|
|
386
|
+
if (text === null && this.cur !== null) {
|
|
387
|
+
this.fallbackRun(at - start.length);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
var chrCode = chr(ACCENTS[start]);
|
|
391
|
+
var body = text;
|
|
392
|
+
if (body && body.startsWith('\\bar{') && body.endsWith(GROUP_END) && chrCode === chr(773)) {
|
|
393
|
+
chrCode = chr(831);
|
|
394
|
+
body = body.substring(5, body.length - 1);
|
|
395
|
+
}
|
|
396
|
+
var inner = body ? this.fill(body) : [];
|
|
397
|
+
this.add(function (t) {
|
|
398
|
+
var f = add(t, MathFunctionType.Accent);
|
|
399
|
+
f.accentCharacter = chrCode;
|
|
400
|
+
finalize(inner, f.equation);
|
|
401
|
+
});
|
|
402
|
+
};
|
|
403
|
+
LatexParser.prototype.parseBar = function () {
|
|
404
|
+
var _a = this.oneArg(), start = _a.start, at = _a.at, text = _a.text;
|
|
405
|
+
if (text === null && this.cur !== null) {
|
|
406
|
+
this.fallbackRun(at - start.length);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
this.cur = this.next();
|
|
410
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
411
|
+
this.parseScript("" + start + GROUP_START + nz(text) + GROUP_END, at);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (this.cur !== null) {
|
|
415
|
+
this.useExisting = true;
|
|
416
|
+
}
|
|
417
|
+
var inner = text ? this.fill(text) : [];
|
|
418
|
+
var pos = start === '\\overline' ? 'top' : 'bot';
|
|
419
|
+
this.add(function (t) {
|
|
420
|
+
var f = add(t, MathFunctionType.Bar);
|
|
421
|
+
f.barTop = pos === 'top';
|
|
422
|
+
finalize(inner, f.equation);
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
LatexParser.prototype.parseBox = function () {
|
|
426
|
+
var _a = this.oneArg(), start = _a.start, at = _a.at, text = _a.text;
|
|
427
|
+
if (text === null && this.cur !== null) {
|
|
428
|
+
this.fallbackRun(at - start.length);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
var inner = text ? this.fill(text) : [];
|
|
432
|
+
this.add(function (t) {
|
|
433
|
+
var f = add(t, MathFunctionType.Box);
|
|
434
|
+
f.noBreak = false;
|
|
435
|
+
finalize(inner, f.equation);
|
|
436
|
+
});
|
|
437
|
+
};
|
|
438
|
+
LatexParser.prototype.parseBorderBox = function () {
|
|
439
|
+
var _a = this.oneArg(), start = _a.start, at = _a.at, text = _a.text;
|
|
440
|
+
if (text === null && this.cur !== null) {
|
|
441
|
+
this.fallbackRun(at - start.length);
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.cur = this.next();
|
|
445
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
446
|
+
this.parseScript("" + start + GROUP_START + nz(text) + GROUP_END, at);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (this.cur !== null) {
|
|
450
|
+
this.useExisting = true;
|
|
451
|
+
}
|
|
452
|
+
var inner = text ? this.fill(text) : [];
|
|
453
|
+
this.add(function (t) { return finalize(inner, add(t, MathFunctionType.BorderBox).equation); });
|
|
454
|
+
};
|
|
455
|
+
// ─── Delimiter / eqArray ──────────────────────────────────────────────────
|
|
456
|
+
LatexParser.prototype.delimiterChar = function (eq) {
|
|
457
|
+
this.cur = this.next();
|
|
458
|
+
eq.s += nz(this.cur);
|
|
459
|
+
if (this.cur === CONTROL_START) {
|
|
460
|
+
this.cur = this.next();
|
|
461
|
+
eq.s += nz(this.cur);
|
|
462
|
+
}
|
|
463
|
+
var key = nz(this.cur);
|
|
464
|
+
return Object.prototype.hasOwnProperty.call(DELIMITERS, key) ? DELIMITERS[key] : null;
|
|
465
|
+
};
|
|
466
|
+
LatexParser.prototype.delimiter = function () {
|
|
467
|
+
var r = { ok: false, begin: null, end: null, sep: null, list: [], text: '' };
|
|
468
|
+
var eq = { s: nz(this.cur) };
|
|
469
|
+
var depth = 1;
|
|
470
|
+
r.begin = this.delimiterChar(eq);
|
|
471
|
+
if (r.begin === null) {
|
|
472
|
+
r.text = eq.s;
|
|
473
|
+
return r;
|
|
474
|
+
}
|
|
475
|
+
var between = '';
|
|
476
|
+
while (depth > 0 && this.pos < this.latex.length) {
|
|
477
|
+
var el = nz(this.next());
|
|
478
|
+
eq.s += el;
|
|
479
|
+
if (el === '\\left') {
|
|
480
|
+
between += el;
|
|
481
|
+
depth++;
|
|
482
|
+
}
|
|
483
|
+
else if (el === '\\right') {
|
|
484
|
+
if (depth === 1) {
|
|
485
|
+
r.list.push(between.trim());
|
|
486
|
+
r.end = this.delimiterChar(eq);
|
|
487
|
+
if (r.end === null) {
|
|
488
|
+
r.text = eq.s;
|
|
489
|
+
return r;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
between += el;
|
|
494
|
+
}
|
|
495
|
+
depth--;
|
|
496
|
+
}
|
|
497
|
+
else if (el === '\\middle') {
|
|
498
|
+
if (depth === 1) {
|
|
499
|
+
var mid = this.delimiterChar(eq);
|
|
500
|
+
if (mid === null) {
|
|
501
|
+
r.text = eq.s;
|
|
502
|
+
return r;
|
|
503
|
+
}
|
|
504
|
+
if (r.sep === null) {
|
|
505
|
+
r.sep = mid;
|
|
506
|
+
}
|
|
507
|
+
r.list.push(between.trim());
|
|
508
|
+
between = '';
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
between += el;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
between += el;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
r.ok = depth === 0;
|
|
519
|
+
r.text = eq.s;
|
|
520
|
+
return r;
|
|
521
|
+
};
|
|
522
|
+
LatexParser.prototype.parseDelimiter = function () {
|
|
523
|
+
var _this = this;
|
|
524
|
+
var at = this.pos;
|
|
525
|
+
var start = nz(this.cur);
|
|
526
|
+
var d = this.delimiter();
|
|
527
|
+
if (!d.ok) {
|
|
528
|
+
this.setRun(this.latex.substring(at - start.length, this.pos).replace(/\\ /g, ' '));
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
this.cur = this.next();
|
|
532
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
533
|
+
this.parseScript(d.text, at);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (this.cur !== null) {
|
|
537
|
+
this.useExisting = true;
|
|
538
|
+
}
|
|
539
|
+
var args = d.list.map(function (e) { return _this.fill(e); });
|
|
540
|
+
var props = { begChr: nz(d.begin), endChr: nz(d.end) };
|
|
541
|
+
if (d.sep !== null) {
|
|
542
|
+
props.sepChr = d.sep;
|
|
543
|
+
}
|
|
544
|
+
this.add(function (t) {
|
|
545
|
+
var f = add(t, MathFunctionType.Delimiter);
|
|
546
|
+
f.beginCharacter = props.begChr;
|
|
547
|
+
f.endCharacter = props.endChr;
|
|
548
|
+
if (props.sepChr !== undefined) {
|
|
549
|
+
f.separator = props.sepChr;
|
|
550
|
+
}
|
|
551
|
+
args.forEach(function (a) { return finalize(a, f.equations.add()); });
|
|
552
|
+
});
|
|
553
|
+
};
|
|
554
|
+
LatexParser.prototype.splitEqArray = function (text) {
|
|
555
|
+
var list = [];
|
|
556
|
+
var between = '';
|
|
557
|
+
if (text !== null) {
|
|
558
|
+
for (var p = 0; p < text.length; p++) {
|
|
559
|
+
if (text[p] === '@') {
|
|
560
|
+
if (text.substring(p).includes('&') || text.substring(p + 1).includes('@')) {
|
|
561
|
+
list.push(between.trim());
|
|
562
|
+
between = '';
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
between += text[p];
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
list.push(between.trim());
|
|
570
|
+
}
|
|
571
|
+
return list;
|
|
572
|
+
};
|
|
573
|
+
LatexParser.prototype.parseEqArray = function () {
|
|
574
|
+
var _this = this;
|
|
575
|
+
var at = this.pos;
|
|
576
|
+
var start = nz(this.cur);
|
|
577
|
+
var text = this.equationString();
|
|
578
|
+
if (text === null && this.cur !== null) {
|
|
579
|
+
this.fallbackRun(at - start.length);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
var parts = this.splitEqArray(text).map(function (e) { return _this.fill(e); });
|
|
583
|
+
this.add(function (t) {
|
|
584
|
+
var f = add(t, MathFunctionType.EquationArray);
|
|
585
|
+
if (parts.length === 0) {
|
|
586
|
+
f.equations.add();
|
|
587
|
+
}
|
|
588
|
+
parts.forEach(function (p) { return finalize(p, f.equations.add()); });
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
// ─── Fraction / function / group char / limit ─────────────────────────────
|
|
592
|
+
LatexParser.prototype.parseFraction = function () {
|
|
593
|
+
var at = this.pos;
|
|
594
|
+
var start = nz(this.cur);
|
|
595
|
+
var num = this.equationString();
|
|
596
|
+
if (this.cur === SPACE && num === SPACE) {
|
|
597
|
+
num = this.equationString();
|
|
598
|
+
}
|
|
599
|
+
var den = '';
|
|
600
|
+
if ((this.cur === SPACE && num === SPACE) || (this.cur === null && num === null)) {
|
|
601
|
+
num = '';
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
den = this.equationString();
|
|
605
|
+
}
|
|
606
|
+
if (num === null || den === null) {
|
|
607
|
+
this.fallbackRun(at - start.length);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
this.cur = this.next();
|
|
611
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
612
|
+
this.parseScript("" + start + GROUP_START + num + GROUP_END + GROUP_START + den + GROUP_END, at);
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
if (this.cur !== null) {
|
|
616
|
+
this.useExisting = true;
|
|
617
|
+
}
|
|
618
|
+
if (start === '\\binom') {
|
|
619
|
+
this.binomial(num, den);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
var type = start === '\\sfrac' ? MathFractionType.SkewedFractionBar : MathFractionType.NormalFractionBar;
|
|
623
|
+
this.fraction(type, num, den);
|
|
624
|
+
};
|
|
625
|
+
LatexParser.prototype.fraction = function (type, num, den) {
|
|
626
|
+
var n = num !== '' ? this.fill(num) : [];
|
|
627
|
+
var d = den !== '' ? this.fill(den) : [];
|
|
628
|
+
this.add(function (t) {
|
|
629
|
+
var f = add(t, MathFunctionType.Fraction);
|
|
630
|
+
f.fractionType = type;
|
|
631
|
+
finalize(n, f.numerator);
|
|
632
|
+
finalize(d, f.denominator);
|
|
633
|
+
});
|
|
634
|
+
};
|
|
635
|
+
/** `\binom{n}{k}`: a parenthesis delimiter around a no-bar fraction (Word's binomial coefficient). */
|
|
636
|
+
LatexParser.prototype.binomial = function (num, den) {
|
|
637
|
+
var n = num !== '' ? this.fill(num) : [];
|
|
638
|
+
var d = den !== '' ? this.fill(den) : [];
|
|
639
|
+
this.add(function (t) {
|
|
640
|
+
var delimiter = add(t, MathFunctionType.Delimiter);
|
|
641
|
+
delimiter.beginCharacter = '(';
|
|
642
|
+
delimiter.endCharacter = ')';
|
|
643
|
+
var f = add(delimiter.equations.add(), MathFunctionType.Fraction);
|
|
644
|
+
f.fractionType = MathFractionType.NoFractionBar;
|
|
645
|
+
finalize(n, f.numerator);
|
|
646
|
+
finalize(d, f.denominator);
|
|
647
|
+
});
|
|
648
|
+
};
|
|
649
|
+
LatexParser.prototype.parseNoFractionBar = function (numerator, startPos) {
|
|
650
|
+
if (this.cur !== '\\atop') {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
var den = this.equationString();
|
|
654
|
+
if (this.cur === den) {
|
|
655
|
+
den = null;
|
|
656
|
+
}
|
|
657
|
+
if (den === null) {
|
|
658
|
+
this.fallbackRun(startPos);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
this.fraction(MathFractionType.NoFractionBar, numerator, den);
|
|
662
|
+
};
|
|
663
|
+
LatexParser.prototype.parseFunction = function () {
|
|
664
|
+
var at = this.pos;
|
|
665
|
+
var start = nz(this.cur);
|
|
666
|
+
var body = this.equationString();
|
|
667
|
+
var scriptKind = null;
|
|
668
|
+
var scriptText = null;
|
|
669
|
+
if (this.cur === SUPER || this.cur === SUB || this.cur === LOW_LIMIT) {
|
|
670
|
+
scriptKind = this.cur === SUPER ? 'sup' : this.cur === SUB ? 'sub' : 'limit';
|
|
671
|
+
scriptText = this.equationString();
|
|
672
|
+
if (scriptText === this.cur) {
|
|
673
|
+
body = this.equationString();
|
|
674
|
+
if (body !== null && body === this.cur) {
|
|
675
|
+
body = '';
|
|
676
|
+
this.useExisting = true;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
else {
|
|
680
|
+
body = this.equationString();
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (body === null) {
|
|
684
|
+
this.fallbackRun(at - start.length);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
var name = trimBackslash(start);
|
|
688
|
+
var nameSeq = [];
|
|
689
|
+
var make;
|
|
690
|
+
if (scriptKind === 'sup' || scriptKind === 'sub') {
|
|
691
|
+
var base_1 = this.fill(name);
|
|
692
|
+
this.regular(base_1);
|
|
693
|
+
var script_1 = scriptText !== null ? this.fill(scriptText) : [];
|
|
694
|
+
make = function (f) {
|
|
695
|
+
var sc = add(f.functionName, MathFunctionType.SubSuperscript);
|
|
696
|
+
sc.scriptType = scriptKind === 'sup' ? MathScriptType.Superscript : MathScriptType.Subscript;
|
|
697
|
+
finalize(base_1, sc.equation);
|
|
698
|
+
finalize(script_1, sc.script);
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
else if (scriptKind === 'limit') {
|
|
702
|
+
var base_2 = this.fill(name);
|
|
703
|
+
this.regular(base_2);
|
|
704
|
+
var lim_1 = scriptText !== null ? this.fill(scriptText) : [];
|
|
705
|
+
make = function (f) {
|
|
706
|
+
var l = add(f.functionName, MathFunctionType.Limit);
|
|
707
|
+
l.limitType = MathLimitType.LowerLimit;
|
|
708
|
+
finalize(base_2, l.equation);
|
|
709
|
+
finalize(lim_1, l.limit);
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
else {
|
|
713
|
+
var saved = this.current;
|
|
714
|
+
this.current = nameSeq;
|
|
715
|
+
this.setRun(name);
|
|
716
|
+
this.current = saved;
|
|
717
|
+
this.regular(nameSeq);
|
|
718
|
+
make = function (f) { return finalize(nameSeq, f.functionName); };
|
|
719
|
+
}
|
|
720
|
+
this.ensure();
|
|
721
|
+
var inner = body !== '' ? this.fill(body) : [];
|
|
722
|
+
this.add(function (t) {
|
|
723
|
+
var f = add(t, MathFunctionType.Function);
|
|
724
|
+
make(f);
|
|
725
|
+
finalize(inner, f.equation);
|
|
726
|
+
});
|
|
727
|
+
};
|
|
728
|
+
LatexParser.prototype.regular = function (seq) {
|
|
729
|
+
var first = seq[0];
|
|
730
|
+
if (first instanceof RunBuf) {
|
|
731
|
+
first.sty = MathStyleType.Regular;
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
LatexParser.prototype.parseGroupChar = function () {
|
|
735
|
+
var _a = this.oneArg(), start = _a.start, at = _a.at, text = _a.text;
|
|
736
|
+
if (text === null && this.cur !== null) {
|
|
737
|
+
this.fallbackRun(at - start.length);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
var inner = text ? this.fill(text) : [];
|
|
741
|
+
var over = start === '\\overbrace';
|
|
742
|
+
this.add(function (t) {
|
|
743
|
+
var f = add(t, MathFunctionType.GroupCharacter);
|
|
744
|
+
if (over) {
|
|
745
|
+
f.groupCharacter = chr(9182);
|
|
746
|
+
f.hasAlignTop = false;
|
|
747
|
+
f.hasCharacterTop = true;
|
|
748
|
+
}
|
|
749
|
+
finalize(inner, f.equation);
|
|
750
|
+
});
|
|
751
|
+
};
|
|
752
|
+
/** Compares the current element without TypeScript narrowing across mutating calls. */
|
|
753
|
+
LatexParser.prototype.isCurrent = function (value) {
|
|
754
|
+
return this.cur === value;
|
|
755
|
+
};
|
|
756
|
+
LatexParser.prototype.parseLimit = function (equation, startPos) {
|
|
757
|
+
if (!this.isCurrent(LOW_LIMIT)) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
var lim = this.equationString();
|
|
761
|
+
if (lim === SPACE && this.isCurrent(SPACE)) {
|
|
762
|
+
lim = this.equationString();
|
|
763
|
+
}
|
|
764
|
+
if (lim === null) {
|
|
765
|
+
this.fallbackRun(startPos);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
var base = this.fill(equation);
|
|
769
|
+
var limit = lim !== SPACE ? this.fill(lim) : [];
|
|
770
|
+
this.add(function (t) {
|
|
771
|
+
var f = add(t, MathFunctionType.Limit);
|
|
772
|
+
f.limitType = MathLimitType.LowerLimit;
|
|
773
|
+
finalize(base, f.equation);
|
|
774
|
+
finalize(limit, f.limit);
|
|
775
|
+
});
|
|
776
|
+
};
|
|
777
|
+
// ─── Matrix ───────────────────────────────────────────────────────────────
|
|
778
|
+
LatexParser.prototype.parseMatrix = function () {
|
|
779
|
+
var _this = this;
|
|
780
|
+
var at = this.pos;
|
|
781
|
+
var start = nz(this.cur);
|
|
782
|
+
this.cur = this.next();
|
|
783
|
+
if (this.cur !== GROUP_START) {
|
|
784
|
+
this.setRun(start);
|
|
785
|
+
this.useExisting = true;
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
var g = this.group();
|
|
789
|
+
var m = g.ok && g.text === 'matrix' ? this.matrixRows() : null;
|
|
790
|
+
if (m === null) {
|
|
791
|
+
this.fallbackRun(at - start.length);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
this.cur = this.next();
|
|
795
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
796
|
+
this.parseScript(this.latex.substring(at - start.length, this.pos - 1), at);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
if (this.cur !== null) {
|
|
800
|
+
this.useExisting = true;
|
|
801
|
+
}
|
|
802
|
+
var rows = m.rows.map(function (cols) {
|
|
803
|
+
var cells = [];
|
|
804
|
+
for (var i = 0; i < m.maxCols; i++) {
|
|
805
|
+
cells.push(i < cols.length ? _this.fill(cols[i]) : []);
|
|
806
|
+
}
|
|
807
|
+
return cells;
|
|
808
|
+
});
|
|
809
|
+
var columnCount = m.maxCols;
|
|
810
|
+
this.add(function (t) {
|
|
811
|
+
var f = add(t, MathFunctionType.Matrix);
|
|
812
|
+
for (var i = 0; i < columnCount; i++) {
|
|
813
|
+
f.columns.add().horizontalAlignment = MathHorizontalAlignment.Center;
|
|
814
|
+
}
|
|
815
|
+
rows.forEach(function (r) {
|
|
816
|
+
var row = f.rows.add();
|
|
817
|
+
r.forEach(function (c, i) { return finalize(c, row.arguments.getItem(i)); });
|
|
818
|
+
});
|
|
819
|
+
});
|
|
820
|
+
};
|
|
821
|
+
LatexParser.prototype.matrixRows = function () {
|
|
822
|
+
var rows = [];
|
|
823
|
+
var maxCols = 0;
|
|
824
|
+
var depth = 1;
|
|
825
|
+
var between = '';
|
|
826
|
+
var cols = [];
|
|
827
|
+
var endRow = function () {
|
|
828
|
+
cols.push(between);
|
|
829
|
+
between = '';
|
|
830
|
+
maxCols = Math.max(maxCols, cols.length);
|
|
831
|
+
rows.push(cols);
|
|
832
|
+
cols = [];
|
|
833
|
+
};
|
|
834
|
+
while (depth > 0 && this.pos < this.latex.length) {
|
|
835
|
+
this.cur = this.next();
|
|
836
|
+
while (this.cur === SPACE) {
|
|
837
|
+
this.cur = this.next();
|
|
838
|
+
}
|
|
839
|
+
if (this.cur === CONTROL_START) {
|
|
840
|
+
this.cur = this.next();
|
|
841
|
+
if (this.cur === CONTROL_START) {
|
|
842
|
+
this.cur += CONTROL_START;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
var c = this.cur;
|
|
846
|
+
if (c === '\\begin') {
|
|
847
|
+
between += c;
|
|
848
|
+
var startEl = nz((this.cur = this.next()));
|
|
849
|
+
var g = this.group();
|
|
850
|
+
if (g.ok && g.text === 'matrix') {
|
|
851
|
+
depth++;
|
|
852
|
+
}
|
|
853
|
+
between += startEl + g.text + nz(this.cur);
|
|
854
|
+
}
|
|
855
|
+
else if (c === '\\end') {
|
|
856
|
+
var startEl = c + nz((this.cur = this.next()));
|
|
857
|
+
var g = this.group();
|
|
858
|
+
if (g.ok && g.text === 'matrix' && depth === 1) {
|
|
859
|
+
if (cols.length !== 0 || between !== '') {
|
|
860
|
+
endRow();
|
|
861
|
+
}
|
|
862
|
+
depth--;
|
|
863
|
+
}
|
|
864
|
+
else if (depth === 1) {
|
|
865
|
+
return null;
|
|
866
|
+
}
|
|
867
|
+
else {
|
|
868
|
+
between += startEl + g.text + nz(this.cur);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
else if (c === MATRIX_COL_END) {
|
|
872
|
+
if (depth === 1) {
|
|
873
|
+
cols.push(between);
|
|
874
|
+
between = '';
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
between += c;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
else if (c === MATRIX_ROW_END) {
|
|
881
|
+
if (depth === 1) {
|
|
882
|
+
endRow();
|
|
883
|
+
}
|
|
884
|
+
else {
|
|
885
|
+
between += c;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
between += nz(c);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return depth === 0 ? { rows: rows, maxCols: maxCols } : null;
|
|
893
|
+
};
|
|
894
|
+
// ─── N-ary ────────────────────────────────────────────────────────────────
|
|
895
|
+
LatexParser.prototype.parseNary = function () {
|
|
896
|
+
var at = this.pos;
|
|
897
|
+
var start = nz(this.cur);
|
|
898
|
+
var eq = this.equationString();
|
|
899
|
+
var subText = null;
|
|
900
|
+
var supText = null;
|
|
901
|
+
for (var i = 0; i < 2; i++) {
|
|
902
|
+
if (eq === SPACE && this.cur === SPACE) {
|
|
903
|
+
eq = this.equationString();
|
|
904
|
+
}
|
|
905
|
+
else {
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
if (this.cur === SUB) {
|
|
910
|
+
subText = this.equationString();
|
|
911
|
+
}
|
|
912
|
+
else if (this.cur === SUPER) {
|
|
913
|
+
supText = this.equationString();
|
|
914
|
+
}
|
|
915
|
+
if (subText !== null) {
|
|
916
|
+
eq = this.equationString();
|
|
917
|
+
if (this.cur === SUPER) {
|
|
918
|
+
supText = this.equationString();
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
else if (supText !== null) {
|
|
922
|
+
eq = this.equationString();
|
|
923
|
+
if (this.cur === SUB) {
|
|
924
|
+
subText = this.equationString();
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
if (subText !== null && supText !== null) {
|
|
928
|
+
eq = this.equationString();
|
|
929
|
+
}
|
|
930
|
+
if (eq !== null) {
|
|
931
|
+
eq = this.naryTail(eq);
|
|
932
|
+
}
|
|
933
|
+
if (eq === null && this.cur !== null) {
|
|
934
|
+
this.fallbackRun(at - start.length);
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
var sub = subText !== null ? this.fill(subText) : [];
|
|
938
|
+
var sup = supText !== null ? this.fill(supText) : [];
|
|
939
|
+
var base = eq !== SPACE ? this.fill(eq) : [];
|
|
940
|
+
var character = chr(NARY[start]);
|
|
941
|
+
this.add(function (t) {
|
|
942
|
+
var f = add(t, MathFunctionType.NArray);
|
|
943
|
+
f.nArrayCharacter = character;
|
|
944
|
+
if (subText === null) {
|
|
945
|
+
f.hideLowerLimit = true;
|
|
946
|
+
}
|
|
947
|
+
if (supText === null) {
|
|
948
|
+
f.hideUpperLimit = true;
|
|
949
|
+
}
|
|
950
|
+
if (subText === null && supText === null) {
|
|
951
|
+
f.subSuperscriptLimit = false;
|
|
952
|
+
}
|
|
953
|
+
finalize(sub, f.subscript);
|
|
954
|
+
finalize(sup, f.superscript);
|
|
955
|
+
finalize(base, f.equation);
|
|
956
|
+
});
|
|
957
|
+
};
|
|
958
|
+
/** Greedily absorbs trailing script groups into the N-ary operand (mirrors compression). */
|
|
959
|
+
LatexParser.prototype.naryTail = function (first) {
|
|
960
|
+
var eq = first;
|
|
961
|
+
var isLeft = false;
|
|
962
|
+
if (this.cur === GROUP_END && hasScriptPair(eq)) {
|
|
963
|
+
eq = GROUP_START + eq + GROUP_END;
|
|
964
|
+
isLeft = true;
|
|
965
|
+
}
|
|
966
|
+
var part = this.equationString();
|
|
967
|
+
var check = true;
|
|
968
|
+
if (part !== null && isLeft) {
|
|
969
|
+
eq += GROUP_START + part + GROUP_END;
|
|
970
|
+
}
|
|
971
|
+
else if (part === null && (this.cur === SUPER || this.cur === SUB)) {
|
|
972
|
+
eq += this.cur + GROUP_START + nz(this.equationString()) + GROUP_END;
|
|
973
|
+
}
|
|
974
|
+
else if (part !== null && hasScriptPair(part)) {
|
|
975
|
+
eq += GROUP_START + part + GROUP_END;
|
|
976
|
+
}
|
|
977
|
+
else {
|
|
978
|
+
this.useExisting = true;
|
|
979
|
+
check = false;
|
|
980
|
+
if (part !== null && this.cur !== part) {
|
|
981
|
+
this.cur = part;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
while (check) {
|
|
985
|
+
part = this.equationString();
|
|
986
|
+
if (part === null && (this.cur === SUPER || this.cur === SUB)) {
|
|
987
|
+
eq += this.cur + GROUP_START + nz(this.equationString()) + GROUP_END;
|
|
988
|
+
}
|
|
989
|
+
else if (part !== null && hasScriptPair(part)) {
|
|
990
|
+
eq += GROUP_START + part + GROUP_END;
|
|
991
|
+
}
|
|
992
|
+
else {
|
|
993
|
+
this.useExisting = true;
|
|
994
|
+
check = false;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return eq;
|
|
998
|
+
};
|
|
999
|
+
// ─── Radical ──────────────────────────────────────────────────────────────
|
|
1000
|
+
LatexParser.prototype.parseRadical = function () {
|
|
1001
|
+
var at = this.pos;
|
|
1002
|
+
var start = nz(this.cur);
|
|
1003
|
+
this.cur = this.next();
|
|
1004
|
+
if (this.cur === SPACE) {
|
|
1005
|
+
this.cur = this.next();
|
|
1006
|
+
}
|
|
1007
|
+
var degree = this.squareGroup();
|
|
1008
|
+
if (degree !== null) {
|
|
1009
|
+
this.cur = this.next();
|
|
1010
|
+
}
|
|
1011
|
+
var equation = null;
|
|
1012
|
+
var valid = false;
|
|
1013
|
+
var notInGroup = false;
|
|
1014
|
+
if (this.cur === GROUP_START) {
|
|
1015
|
+
var g = this.group();
|
|
1016
|
+
valid = g.ok;
|
|
1017
|
+
equation = g.text;
|
|
1018
|
+
}
|
|
1019
|
+
else {
|
|
1020
|
+
notInGroup = true;
|
|
1021
|
+
}
|
|
1022
|
+
if (notInGroup && this.cur !== null && this.cur.length === 1 && (LETTER_OR_DIGIT.test(this.cur) || this.cur === SPACE)) {
|
|
1023
|
+
equation = this.cur;
|
|
1024
|
+
}
|
|
1025
|
+
if (!(notInGroup || valid)) {
|
|
1026
|
+
this.fallbackRun(at - start.length);
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
this.cur = this.next();
|
|
1030
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
1031
|
+
this.parseScript(this.latex.substring(at - start.length, this.pos - 1), at);
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
if (this.cur !== null) {
|
|
1035
|
+
this.useExisting = true;
|
|
1036
|
+
}
|
|
1037
|
+
var hide = !(degree !== null && equation !== null);
|
|
1038
|
+
var deg = !hide ? this.fill(degree) : [];
|
|
1039
|
+
var body = [];
|
|
1040
|
+
if (equation !== null && equation !== SPACE) {
|
|
1041
|
+
body = this.fill(equation);
|
|
1042
|
+
}
|
|
1043
|
+
else if (equation === null) {
|
|
1044
|
+
this.useExisting = true;
|
|
1045
|
+
if (degree !== null) {
|
|
1046
|
+
this.setRun("[" + degree + "]");
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
this.add(function (t) {
|
|
1050
|
+
var f = add(t, MathFunctionType.Radical);
|
|
1051
|
+
if (hide) {
|
|
1052
|
+
f.hideDegree = true;
|
|
1053
|
+
}
|
|
1054
|
+
finalize(deg, f.degree);
|
|
1055
|
+
finalize(body, f.equation);
|
|
1056
|
+
});
|
|
1057
|
+
};
|
|
1058
|
+
// ─── Groups and scripts ───────────────────────────────────────────────────
|
|
1059
|
+
LatexParser.prototype.parseGroupStart = function () {
|
|
1060
|
+
var startPos = this.pos - GROUP_START.length;
|
|
1061
|
+
var g = this.group();
|
|
1062
|
+
if (!g.ok) {
|
|
1063
|
+
this.setRun(GROUP_START);
|
|
1064
|
+
this.sub(g.text, this.current);
|
|
1065
|
+
return;
|
|
1066
|
+
}
|
|
1067
|
+
if (hasScriptPair(g.text)) {
|
|
1068
|
+
this.parseLeftScript(g.text, startPos);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
this.cur = this.next();
|
|
1072
|
+
if (this.cur === SUPER || this.cur === SUB) {
|
|
1073
|
+
this.parseScript(g.text, startPos);
|
|
1074
|
+
}
|
|
1075
|
+
else if (this.cur === LOW_LIMIT) {
|
|
1076
|
+
this.parseLimit(g.text, startPos);
|
|
1077
|
+
}
|
|
1078
|
+
else if (this.cur === '\\atop') {
|
|
1079
|
+
this.parseNoFractionBar(g.text, startPos);
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
this.sub(g.text, this.current);
|
|
1083
|
+
this.useExisting = true;
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
LatexParser.prototype.parseScript = function (equation, startPos) {
|
|
1087
|
+
if (!(this.cur === SUPER || this.cur === SUB)) {
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
var isSup = this.cur === SUPER;
|
|
1091
|
+
var script = this.equationString();
|
|
1092
|
+
if (script === null && this.cur !== null) {
|
|
1093
|
+
this.fallbackRun(startPos);
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
if (this.cur !== null) {
|
|
1097
|
+
this.cur = this.next();
|
|
1098
|
+
if ((isSup && this.cur === SUB) || (!isSup && this.cur === SUPER)) {
|
|
1099
|
+
this.parseRightScript(equation, script, isSup, startPos);
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
this.useExisting = true;
|
|
1103
|
+
}
|
|
1104
|
+
this.ensure();
|
|
1105
|
+
var base = this.fill(equation);
|
|
1106
|
+
var scriptSeq = script ? this.fill(script) : [];
|
|
1107
|
+
this.add(function (t) {
|
|
1108
|
+
var f = add(t, MathFunctionType.SubSuperscript);
|
|
1109
|
+
f.scriptType = isSup ? MathScriptType.Superscript : MathScriptType.Subscript;
|
|
1110
|
+
finalize(base, f.equation);
|
|
1111
|
+
finalize(scriptSeq, f.script);
|
|
1112
|
+
});
|
|
1113
|
+
};
|
|
1114
|
+
LatexParser.prototype.parseRightScript = function (equation, first, isSup, startPos) {
|
|
1115
|
+
var second = this.equationString();
|
|
1116
|
+
if (second === null && this.cur !== null) {
|
|
1117
|
+
this.fallbackRun(startPos);
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
var keep = function (s) { return (s !== null && s !== SPACE ? s : null); };
|
|
1121
|
+
var base = this.fill(keep(equation));
|
|
1122
|
+
var sup = this.fill(keep(isSup ? first : second));
|
|
1123
|
+
var sub = this.fill(keep(isSup ? second : first));
|
|
1124
|
+
this.add(function (t) {
|
|
1125
|
+
var f = add(t, MathFunctionType.RightSubSuperscript);
|
|
1126
|
+
finalize(base, f.equation);
|
|
1127
|
+
finalize(sub, f.subscript);
|
|
1128
|
+
finalize(sup, f.superscript);
|
|
1129
|
+
});
|
|
1130
|
+
};
|
|
1131
|
+
LatexParser.prototype.parseLeftScript = function (group, startPos) {
|
|
1132
|
+
var sub = null;
|
|
1133
|
+
var sup = null;
|
|
1134
|
+
var saved = { latex: this.latex, pos: this.pos, cur: this.cur };
|
|
1135
|
+
this.latex = group;
|
|
1136
|
+
this.pos = 1;
|
|
1137
|
+
if (group.startsWith(SUB)) {
|
|
1138
|
+
sub = this.equationString();
|
|
1139
|
+
}
|
|
1140
|
+
else {
|
|
1141
|
+
sup = this.equationString();
|
|
1142
|
+
}
|
|
1143
|
+
if (sub !== null) {
|
|
1144
|
+
this.cur = this.next();
|
|
1145
|
+
if (this.cur === SUPER) {
|
|
1146
|
+
sup = this.equationString();
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
else if (sup !== null) {
|
|
1150
|
+
this.cur = this.next();
|
|
1151
|
+
if (this.cur === SUB) {
|
|
1152
|
+
sub = this.equationString();
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
this.latex = saved.latex;
|
|
1156
|
+
this.pos = saved.pos;
|
|
1157
|
+
this.cur = saved.cur;
|
|
1158
|
+
var eq = null;
|
|
1159
|
+
if (sub !== null && sup !== null) {
|
|
1160
|
+
eq = this.equationString();
|
|
1161
|
+
}
|
|
1162
|
+
if (eq === null && this.cur !== null) {
|
|
1163
|
+
this.fallbackRun(startPos);
|
|
1164
|
+
return;
|
|
1165
|
+
}
|
|
1166
|
+
var keep = function (s) { return (s !== SPACE ? s : null); };
|
|
1167
|
+
var subSeq = this.fill(keep(sub));
|
|
1168
|
+
var supSeq = this.fill(keep(sup));
|
|
1169
|
+
var base = eq !== null && eq !== SPACE ? this.fill(eq) : [];
|
|
1170
|
+
this.add(function (t) {
|
|
1171
|
+
var f = add(t, MathFunctionType.LeftSubSuperscript);
|
|
1172
|
+
finalize(subSeq, f.subscript);
|
|
1173
|
+
finalize(supSeq, f.superscript);
|
|
1174
|
+
finalize(base, f.equation);
|
|
1175
|
+
});
|
|
1176
|
+
};
|
|
1177
|
+
return LatexParser;
|
|
1178
|
+
}());
|
|
1179
|
+
export { LatexParser };
|
|
1180
|
+
/**
|
|
1181
|
+
* Convert a LaTeX equation string to an Office Math paragraph.
|
|
1182
|
+
*
|
|
1183
|
+
* Mirrors Microsoft Word's "linear LaTeX" behavior as implemented by Syncfusion
|
|
1184
|
+
* `LaTeXConverter.ParseMathPara`: unsupported or malformed constructs degrade to
|
|
1185
|
+
* plain text runs instead of throwing.
|
|
1186
|
+
*
|
|
1187
|
+
* @param latex - LaTeX source (for example `\frac{a}{b}` or `\sqrt[3]{x}`).
|
|
1188
|
+
* @returns An {@link OfficeMathParagraph} holding one {@link OfficeMath} per top-level equation.
|
|
1189
|
+
* @public
|
|
1190
|
+
*/
|
|
1191
|
+
export function parseLatexToMath(latex, adapter) {
|
|
1192
|
+
var paragraph = new OfficeMathParagraph(adapter);
|
|
1193
|
+
parseLatexIntoMathParagraph(latex, paragraph);
|
|
1194
|
+
return paragraph;
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Appends the equations parsed from a LaTeX string to an existing Math paragraph.
|
|
1198
|
+
* Runs are created through the target's document adapter when it has one.
|
|
1199
|
+
*
|
|
1200
|
+
* @param latex - LaTeX source.
|
|
1201
|
+
* @param target - The paragraph to populate; it keeps its identity, owner, adapter and listener.
|
|
1202
|
+
* @public
|
|
1203
|
+
*/
|
|
1204
|
+
export function parseLatexIntoMathParagraph(latex, target) {
|
|
1205
|
+
new LatexParser().populate(latex, target);
|
|
1206
|
+
}
|