-
Notifications
You must be signed in to change notification settings - Fork 207
/
BaseMethods.ts
1607 lines (1483 loc) · 51.2 KB
/
BaseMethods.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************
*
* Copyright (c) 2017 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview The Basic Parse methods.
*
* @author [email protected] (Volker Sorge)
*/
import * as sitem from './BaseItems.js';
import {StackItem, EnvList} from '../StackItem.js';
import {Macro} from '../Symbol.js';
import {ParseMethod} from '../Types.js';
import NodeUtil from '../NodeUtil.js';
import TexError from '../TexError.js';
import TexParser from '../TexParser.js';
import {TexConstant} from '../TexConstants.js';
import ParseUtil from '../ParseUtil.js';
import {MmlNode, TEXCLASS} from '../../../core/MmlTree/MmlNode.js';
import {MmlMsubsup} from '../../../core/MmlTree/MmlNodes/msubsup.js';
import {MmlMunderover} from '../../../core/MmlTree/MmlNodes/munderover.js';
import {Label} from '../Tags.js';
import {em} from '../../../util/lengths.js';
import {entities} from '../../../util/Entities.js';
import {lookup} from '../../../util/Options.js';
// Namespace
let BaseMethods: Record<string, ParseMethod> = {};
const P_HEIGHT = 1.2 / .85; // cmex10 height plus depth over .85
const MmlTokenAllow: {[key: string]: number} = {
fontfamily: 1, fontsize: 1, fontweight: 1, fontstyle: 1,
color: 1, background: 1,
id: 1, 'class': 1, href: 1, style: 1
};
/**
* Handle LaTeX tokens.
*/
/**
* Handle {
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Open = function(parser: TexParser, _c: string) {
// @test Identifier Font, Prime, Prime with subscript
parser.Push(parser.itemFactory.create('open'));
};
/**
* Handle }
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Close = function(parser: TexParser, _c: string) {
// @test Identifier Font, Prime, Prime with subscript
parser.Push(parser.itemFactory.create('close'));
};
/**
* Handle tilde and spaces.
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Tilde = function(parser: TexParser, _c: string) {
// @test Tilde, Tilde2
parser.Push(parser.create('token', 'mtext', {}, entities.nbsp));
};
/**
* Handling space, by doing nothing.
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Space = function(_parser: TexParser, _c: string) {};
/**
* Handle ^
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Superscript = function(parser: TexParser, _c: string) {
if (parser.GetNext().match(/\d/)) {
// don't treat numbers as a unit
parser.string = parser.string.substr(0, parser.i + 1) +
' ' + parser.string.substr(parser.i + 1);
}
let primes: MmlNode;
let base: MmlNode | void;
const top = parser.stack.Top();
if (top.isKind('prime')) {
// @test Prime on Prime
[base, primes] = top.Peek(2);
parser.stack.Pop();
} else {
// @test Empty base2, Square, Cube
base = parser.stack.Prev();
if (!base) {
// @test Empty base
base = parser.create('token', 'mi', {}, '');
}
}
const movesupsub = NodeUtil.getProperty(base, 'movesupsub');
let position = NodeUtil.isType(base, 'msubsup') ? (base as MmlMsubsup).sup :
(base as MmlMunderover).over;
if ((NodeUtil.isType(base, 'msubsup') && !NodeUtil.isType(base, 'msup') &&
NodeUtil.getChildAt(base, (base as MmlMsubsup).sup)) ||
(NodeUtil.isType(base, 'munderover') && !NodeUtil.isType(base, 'mover') &&
NodeUtil.getChildAt(base, (base as MmlMunderover).over) &&
!NodeUtil.getProperty(base, 'subsupOK'))) {
// @test Double-super-error, Double-over-error
throw new TexError('DoubleExponent', 'Double exponent: use braces to clarify');
}
if (!NodeUtil.isType(base, 'msubsup') || NodeUtil.isType(base, 'msup')) {
if (movesupsub) {
// @test Move Superscript, Large Operator
if (!NodeUtil.isType(base, 'munderover') || NodeUtil.isType(base, 'mover') ||
NodeUtil.getChildAt(base, (base as MmlMunderover).over)) {
// @test Large Operator
base = parser.create('node', 'munderover', [base], {movesupsub: true});
}
position = (base as MmlMunderover).over;
} else {
// @test Empty base, Empty base2, Square, Cube
base = parser.create('node', 'msubsup', [base]);
position = (base as MmlMsubsup).sup;
}
}
parser.Push(
parser.itemFactory.create('subsup', base).setProperties({
position: position, primes: primes, movesupsub: movesupsub
}) );
};
/**
* Handle _
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Subscript = function(parser: TexParser, _c: string) {
if (parser.GetNext().match(/\d/)) {
// don't treat numbers as a unit
parser.string =
parser.string.substr(0, parser.i + 1) + ' ' +
parser.string.substr(parser.i + 1);
}
let primes, base;
const top = parser.stack.Top();
if (top.isKind('prime')) {
// @test Prime on Sub
[base, primes] = top.Peek(2);
parser.stack.Pop();
} else {
base = parser.stack.Prev();
if (!base) {
// @test Empty Base Index
base = parser.create('token', 'mi', {}, '');
}
}
const movesupsub = NodeUtil.getProperty(base, 'movesupsub');
let position = NodeUtil.isType(base, 'msubsup') ?
(base as MmlMsubsup).sub : (base as MmlMunderover).under;
if ((NodeUtil.isType(base, 'msubsup') && !NodeUtil.isType(base, 'msup') &&
NodeUtil.getChildAt(base, (base as MmlMsubsup).sub)) ||
(NodeUtil.isType(base, 'munderover') && !NodeUtil.isType(base, 'mover') &&
NodeUtil.getChildAt(base, (base as MmlMunderover).under) &&
!NodeUtil.getProperty(base, 'subsupOK'))) {
// @test Double-sub-error, Double-under-error
throw new TexError('DoubleSubscripts', 'Double subscripts: use braces to clarify');
}
if (!NodeUtil.isType(base, 'msubsup') || NodeUtil.isType(base, 'msup')) {
if (movesupsub) {
// @test Large Operator, Move Superscript
if (!NodeUtil.isType(base, 'munderover') || NodeUtil.isType(base, 'mover') ||
NodeUtil.getChildAt(base, (base as MmlMunderover).under)) {
// @test Move Superscript
base = parser.create('node', 'munderover', [base], {movesupsub: true});
}
position = (base as MmlMunderover).under;
} else {
// @test Empty Base Index, Empty Base Index2, Index
base = parser.create('node', 'msubsup', [base]);
position = (base as MmlMsubsup).sub;
}
}
parser.Push(
parser.itemFactory.create('subsup', base).setProperties({
position: position, primes: primes, movesupsub: movesupsub
}) );
};
/**
* Handle '
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Prime = function(parser: TexParser, c: string) {
// @test Prime
let base = parser.stack.Prev();
if (!base) {
// @test PrimeSup, PrePrime, Prime on Sup
base = parser.create('node', 'mi');
}
if (NodeUtil.isType(base, 'msubsup') && !NodeUtil.isType(base, 'msup') &&
NodeUtil.getChildAt(base, (base as MmlMsubsup).sup)) {
// @test Double Prime Error
throw new TexError('DoubleExponentPrime',
'Prime causes double exponent: use braces to clarify');
}
let sup = '';
parser.i--;
do {
// @test Prime, PrimeSup, Double Prime, PrePrime
sup += entities.prime; parser.i++, c = parser.GetNext();
} while (c === '\'' || c === entities.rsquo);
sup = ['', '\u2032', '\u2033', '\u2034', '\u2057'][sup.length] || sup;
const node = parser.create('token', 'mo', {variantForm: true}, sup);
parser.Push(
parser.itemFactory.create('prime', base, node) );
};
/**
* Handle comments
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Comment = function(parser: TexParser, _c: string) {
while (parser.i < parser.string.length && parser.string.charAt(parser.i) !== '\n') {
parser.i++;
}
};
/**
* Handle hash marks outside of definitions
* @param {TexParser} parser The calling parser.
* @param {string} c The parsed character.
*/
BaseMethods.Hash = function(_parser: TexParser, _c: string) {
// @test Hash Error
throw new TexError('CantUseHash1',
'You can\'t use \'macro parameter character #\' in math mode');
};
/**
*
* Handle LaTeX Macros
*
*/
/**
* Handle \mathrm, \mathbf, etc, allowing for multi-letter runs to be one <mi>.
*/
BaseMethods.MathFont = function(parser: TexParser, name: string, variant: string) {
const text = parser.GetArgument(name);
let mml = new TexParser(text, {
...parser.stack.env,
font: variant,
multiLetterIdentifiers: true
}, parser.configuration).mml();
parser.Push(parser.create('node', 'TeXAtom', [mml]));
};
/**
* Setting font, e.g., via \\rm, \\bf etc.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} font The font name.
*/
BaseMethods.SetFont = function(parser: TexParser, _name: string, font: string) {
parser.stack.env['font'] = font;
};
/**
* Setting style, e.g., via \\displaystyle, \\textstyle, etc.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} texStyle The tex style name: D, T, S, SS
* @param {boolean} style True if we are in displaystyle.
* @param {string} level The nesting level for scripts.
*/
BaseMethods.SetStyle = function(parser: TexParser, _name: string,
texStyle: string, style: boolean,
level: string) {
parser.stack.env['style'] = texStyle;
parser.stack.env['level'] = level;
parser.Push(
parser.itemFactory.create('style').setProperty(
'styles', {displaystyle: style, scriptlevel: level}));
};
/**
* Setting size of an expression, e.g., \\small, \\huge.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {number} size The size value.
*/
BaseMethods.SetSize = function(parser: TexParser, _name: string, size: number) {
parser.stack.env['size'] = size;
parser.Push(
parser.itemFactory.create('style').setProperty('styles', {mathsize: em(size)}));
};
/**
* Setting explicit spaces, e.g., via commata or colons.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} space The space value.
*/
BaseMethods.Spacer = function(parser: TexParser, _name: string, space: number) {
// @test Positive Spacing, Negative Spacing
const node = parser.create('node', 'mspace', [], {width: em(space)});
const style = parser.create('node', 'mstyle', [node], {scriptlevel: 0});
parser.Push(style);
};
/**
* Parses left/right fenced expressions.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.LeftRight = function(parser: TexParser, name: string) {
// @test Fenced, Fenced3
const first = name.substr(1);
parser.Push(parser.itemFactory.create(first, parser.GetDelimiter(name), parser.stack.env.color));
};
/**
* Handle a named math function, e.g., \\sin, \\cos
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} id Alternative string representation of the function.
*/
BaseMethods.NamedFn = function(parser: TexParser, name: string, id: string) {
// @test Named Function
if (!id) {
id = name.substr(1);
}
const mml = parser.create('token', 'mi', {texClass: TEXCLASS.OP}, id);
parser.Push(parser.itemFactory.create('fn', mml));
};
/**
* Handle a named math operator, e.g., \\min, \\lim
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} id Alternative string representation of the operator.
*/
BaseMethods.NamedOp = function(parser: TexParser, name: string, id: string) {
// @test Limit
if (!id) {
id = name.substr(1);
}
id = id.replace(/ /, '\u2006');
const mml = parser.create('token', 'mo', {
movablelimits: true,
movesupsub: true,
form: TexConstant.Form.PREFIX,
texClass: TEXCLASS.OP
}, id);
parser.Push(mml);
};
/**
* Handle a limits command for math operators.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} limits The limits arguments.
*/
BaseMethods.Limits = function(parser: TexParser, _name: string, limits: string) {
// @test Limits
let op = parser.stack.Prev(true);
// Get the texclass for the core operator.
if (!op || (NodeUtil.getTexClass(NodeUtil.getCoreMO(op)) !== TEXCLASS.OP &&
NodeUtil.getProperty(op, 'movesupsub') == null)) {
// @test Limits Error
throw new TexError('MisplacedLimits', '%1 is allowed only on operators', parser.currentCS);
}
const top = parser.stack.Top();
let node;
if (NodeUtil.isType(op, 'munderover') && !limits) {
// @test Limits UnderOver
node = parser.create('node', 'msubsup');
NodeUtil.copyChildren(op, node);
op = top.Last = node;
} else if (NodeUtil.isType(op, 'msubsup') && limits) {
// @test Limits SubSup
// node = parser.create('node', 'munderover', NodeUtil.getChildren(op), {});
// Needs to be copied, otherwise we get an error in MmlNode.appendChild!
node = parser.create('node', 'munderover');
NodeUtil.copyChildren(op, node);
op = top.Last = node;
}
NodeUtil.setProperty(op, 'movesupsub', limits ? true : false);
NodeUtil.setProperties(NodeUtil.getCoreMO(op), {'movablelimits': false});
if (NodeUtil.getAttribute(op, 'movablelimits') ||
NodeUtil.getProperty(op, 'movablelimits')) {
NodeUtil.setProperties(op, {'movablelimits': false});
}
};
/**
* Handle over commands.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} open The open delimiter in case of a "withdelim" version.
* @param {string} close The close delimiter.
*/
BaseMethods.Over = function(parser: TexParser, name: string, open: string, close: string) {
// @test Over
const mml = parser.itemFactory.create('over').setProperty('name', parser.currentCS) ;
if (open || close) {
// @test Choose
mml.setProperty('open', open);
mml.setProperty('close', close);
} else if (name.match(/withdelims$/)) {
// @test Over With Delims, Above With Delims
mml.setProperty('open', parser.GetDelimiter(name));
mml.setProperty('close', parser.GetDelimiter(name));
}
if (name.match(/^\\above/)) {
// @test Above, Above With Delims
mml.setProperty('thickness', parser.GetDimen(name));
}
else if (name.match(/^\\atop/) || open || close) {
// @test Choose
mml.setProperty('thickness', 0);
}
parser.Push(mml);
};
/**
* Parses a fraction.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Frac = function(parser: TexParser, name: string) {
// @test Frac
const num = parser.ParseArg(name);
const den = parser.ParseArg(name);
const node = parser.create('node', 'mfrac', [num, den]);
parser.Push(node);
};
/**
* Parses a square root element.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Sqrt = function(parser: TexParser, name: string) {
const n = parser.GetBrackets(name);
let arg = parser.GetArgument(name);
if (arg === '\\frac') {
arg += '{' + parser.GetArgument(arg) + '}{' + parser.GetArgument(arg) + '}';
}
let mml = new TexParser(arg, parser.stack.env, parser.configuration).mml();
if (!n) {
// @test Square Root
mml = parser.create('node', 'msqrt', [mml]);
} else {
// @test General Root
mml = parser.create('node', 'mroot', [mml, parseRoot(parser, n)]);
}
parser.Push(mml);
};
// Utility
/**
* Parse a general root.
* @param {TexParser} parser The calling parser.
* @param {string} n The index of the root.
*/
function parseRoot(parser: TexParser, n: string) {
// @test General Root, Explicit Root
const env = parser.stack.env;
const inRoot = env['inRoot'];
env['inRoot'] = true;
const newParser = new TexParser(n, env, parser.configuration);
let node = newParser.mml();
const global = newParser.stack.global;
if (global['leftRoot'] || global['upRoot']) {
// @test Tweaked Root
const def: EnvList = {};
if (global['leftRoot']) {
def['width'] = global['leftRoot'];
}
if (global['upRoot']) {
def['voffset'] = global['upRoot'];
def['height'] = global['upRoot'];
}
node = parser.create('node', 'mpadded', [node], def);
}
env['inRoot'] = inRoot;
return node;
}
/**
* Parse a general root.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Root = function(parser: TexParser, name: string) {
const n = parser.GetUpTo(name, '\\of');
const arg = parser.ParseArg(name);
const node = parser.create('node', 'mroot', [arg, parseRoot(parser, n)]);
parser.Push(node);
};
/**
* Parses a movable index element in a root, e.g. \\uproot, \\leftroot
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} id Argument which should be a string representation of an integer.
*/
BaseMethods.MoveRoot = function(parser: TexParser, name: string, id: string) {
// @test Tweaked Root
if (!parser.stack.env['inRoot']) {
// @test Misplaced Move Root
throw new TexError('MisplacedMoveRoot', '%1 can appear only within a root', parser.currentCS);
}
if (parser.stack.global[id]) {
// @test Multiple Move Root
throw new TexError('MultipleMoveRoot', 'Multiple use of %1', parser.currentCS);
}
let n = parser.GetArgument(name);
if (!n.match(/-?[0-9]+/)) {
// @test Incorrect Move Root
throw new TexError('IntegerArg', 'The argument to %1 must be an integer', parser.currentCS);
}
n = (parseInt(n, 10) / 15) + 'em';
if (n.substr(0, 1) !== '-') {
n = '+' + n;
}
parser.stack.global[id] = n;
};
/**
* Handle accents.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} accent The accent.
* @param {boolean} stretchy True if accent is stretchy.
*/
BaseMethods.Accent = function(parser: TexParser, name: string, accent: string, stretchy: boolean) {
// @test Vector
const c = parser.ParseArg(name);
// @test Vector Font
const def = {...ParseUtil.getFontDef(parser), accent: true, mathaccent: true};
const entity = NodeUtil.createEntity(accent);
const moNode = parser.create('token', 'mo', def, entity);
const mml = moNode;
NodeUtil.setAttribute(mml, 'stretchy', stretchy ? true : false);
// @test Vector Op, Vector
const mo = (NodeUtil.isEmbellished(c) ? NodeUtil.getCoreMO(c) : c);
if (NodeUtil.isType(mo, 'mo')) {
// @test Vector Op
NodeUtil.setProperties(mo, {'movablelimits': false});
}
const muoNode = parser.create('node', 'munderover');
// This is necessary to get the empty element into the children.
NodeUtil.setChild(muoNode, 0, c);
NodeUtil.setChild(muoNode, 1, null);
NodeUtil.setChild(muoNode, 2, mml);
let texAtom = parser.create('node', 'TeXAtom', [muoNode]);
parser.Push(texAtom);
};
/**
* Handles stacked elements.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} c Character to stack.
* @param {boolean} stack True if stacked operator.
*/
BaseMethods.UnderOver = function(parser: TexParser, name: string, c: string, stack: boolean) {
const entity = NodeUtil.createEntity(c);
const mo = parser.create('token', 'mo', {stretchy: true, accent: true}, entity);
const pos = (name.charAt(1) === 'o' ? 'over' : 'under');
const base = parser.ParseArg(name);
parser.Push(ParseUtil.underOver(parser, base, mo, pos, stack));
};
/**
* Handles overset.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Overset = function(parser: TexParser, name: string) {
// @test Overset
const top = parser.ParseArg(name);
const base = parser.ParseArg(name);
if (NodeUtil.getAttribute(base, 'movablelimits') || NodeUtil.getProperty(base, 'movablelimits')) {
NodeUtil.setProperties(base, {'movablelimits': false});
}
const node = parser.create('node', 'mover', [base, top]);
parser.Push(node);
};
/**
* Handles underset.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Underset = function(parser: TexParser, name: string) {
// @test Underset
const bot = parser.ParseArg(name);
const base = parser.ParseArg(name);
if (NodeUtil.isType(base, 'mo') || NodeUtil.getProperty(base, 'movablelimits')) {
// @test Overline Sum
NodeUtil.setProperties(base, {'movablelimits': false});
}
const node = parser.create('node', 'munder', [base, bot]);
parser.Push(node);
};
/**
* Handles overunderset.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Overunderset = function(parser: TexParser, name: string) {
const top = parser.ParseArg(name);
const bot = parser.ParseArg(name);
const base = parser.ParseArg(name);
if (NodeUtil.isType(base, 'mo') || NodeUtil.getProperty(base, 'movablelimits')) {
NodeUtil.setProperties(base, {'movablelimits': false});
}
const node = parser.create('node', 'munderover', [base, bot, top]);
parser.Push(node);
};
/**
* Creates TeXAtom, when class of element is changed explicitly.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {number} mclass The new TeX class.
*/
BaseMethods.TeXAtom = function(parser: TexParser, name: string, mclass: number) {
let def: EnvList = {texClass: mclass};
let mml: StackItem | MmlNode;
let node: MmlNode;
let parsed: MmlNode;
if (mclass === TEXCLASS.OP) {
def['movesupsub'] = def['movablelimits'] = true;
const arg = parser.GetArgument(name);
const match = arg.match(/^\s*\\rm\s+([a-zA-Z0-9 ]+)$/);
if (match) {
// @test Mathop
def['mathvariant'] = TexConstant.Variant.NORMAL;
node = parser.create('token', 'mi', def, match[1]);
} else {
// @test Mathop Cal
parsed = new TexParser(arg, parser.stack.env, parser.configuration).mml();
node = parser.create('node', 'TeXAtom', [parsed], def);
}
mml = parser.itemFactory.create('fn', node);
} else {
// @test Mathrel
parsed = parser.ParseArg(name);
mml = parser.create('node', 'TeXAtom', [parsed], def);
}
parser.Push(mml);
};
/**
* Creates mmltoken elements. Used in Macro substitutions.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.MmlToken = function(parser: TexParser, name: string) {
// @test Modulo
const kind = parser.GetArgument(name);
let attr = parser.GetBrackets(name, '').replace(/^\s+/, '');
const text = parser.GetArgument(name);
const def: EnvList = {};
let node: MmlNode;
try {
node = parser.create('node', kind);
} catch (e) {
node = null;
}
if (!node || !node.isToken) {
// @test Token Illegal Type, Token Wrong Type
throw new TexError('NotMathMLToken', '%1 is not a token element', kind);
}
while (attr !== '') {
const match = attr.match(/^([a-z]+)\s*=\s*('[^']*'|"[^"]*"|[^ ,]*)\s*,?\s*/i);
if (!match) {
// @test Token Invalid Attribute
throw new TexError('InvalidMathMLAttr', 'Invalid MathML attribute: %1', attr);
}
if (!node.attributes.hasDefault(match[1]) && !MmlTokenAllow[match[1]]) {
// @test Token Unknown Attribute, Token Wrong Attribute
throw new TexError('UnknownAttrForElement',
'%1 is not a recognized attribute for %2',
match[1], kind);
}
let value: string | boolean = ParseUtil.MmlFilterAttribute(
parser, match[1], match[2].replace(/^(['"])(.*)\1$/, '$2'));
if (value) {
if (value.toLowerCase() === 'true') {
value = true;
}
else if (value.toLowerCase() === 'false') {
value = false;
}
def[match[1]] = value;
}
attr = attr.substr(match[0].length);
}
const textNode = parser.create('text', text);
node.appendChild(textNode);
NodeUtil.setProperties(node, def);
parser.Push(node);
};
/**
* Handle strut.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Strut = function(parser: TexParser, _name: string) {
// @test Strut
const row = parser.create('node', 'mrow');
const padded = parser.create('node', 'mpadded', [row],
{height: '8.6pt', depth: '3pt', width: 0});
parser.Push(padded);
};
/**
* Handle phantom commands.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} v Vertical size.
* @param {string} h Horizontal size.
*/
BaseMethods.Phantom = function(parser: TexParser, name: string, v: string, h: string) {
// @test Phantom
let box = parser.create('node', 'mphantom', [parser.ParseArg(name)]);
if (v || h) {
// TEMP: Changes here
box = parser.create('node', 'mpadded', [box]);
if (h) {
// @test Horizontal Phantom
NodeUtil.setAttribute(box, 'height', 0);
NodeUtil.setAttribute(box, 'depth', 0);
}
if (v) {
// @test Vertical Phantom
NodeUtil.setAttribute(box, 'width', 0);
}
}
const atom = parser.create('node', 'TeXAtom', [box]);
parser.Push(atom);
};
/**
* Handle smash.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Smash = function(parser: TexParser, name: string) {
// @test Smash, Smash Top, Smash Bottom
const bt = ParseUtil.trimSpaces(parser.GetBrackets(name, ''));
const smash = parser.create('node', 'mpadded', [parser.ParseArg(name)]);
// TEMP: Changes here:
switch (bt) {
case 'b': NodeUtil.setAttribute(smash, 'depth', 0); break;
case 't': NodeUtil.setAttribute(smash, 'height', 0); break;
default:
NodeUtil.setAttribute(smash, 'height', 0);
NodeUtil.setAttribute(smash, 'depth', 0);
}
const atom = parser.create('node', 'TeXAtom', [smash]);
parser.Push(atom);
};
/**
* Handle rlap and llap commands.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Lap = function(parser: TexParser, name: string) {
// @test Llap, Rlap
const mml = parser.create('node', 'mpadded', [parser.ParseArg(name)], {width: 0});
if (name === '\\llap') {
// @test Llap
NodeUtil.setAttribute(mml, 'lspace', '-1width');
}
const atom = parser.create('node', 'TeXAtom', [mml]);
parser.Push(atom);
};
/**
* Handle raise and lower commands.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.RaiseLower = function(parser: TexParser, name: string) {
// @test Raise, Lower, Raise Negative, Lower Negative
let h = parser.GetDimen(name);
let item =
parser.itemFactory.create('position').setProperties({name: parser.currentCS, move: 'vertical'}) ;
// TEMP: Changes here:
if (h.charAt(0) === '-') {
// @test Raise Negative, Lower Negative
h = h.slice(1);
name = name.substr(1) === 'raise' ? '\\lower' : '\\raise';
}
if (name === '\\lower') {
// @test Raise, Raise Negative
item.setProperty('dh', '-' + h);
item.setProperty('dd', '+' + h);
} else {
// @test Lower, Lower Negative
item.setProperty('dh', '+' + h);
item.setProperty('dd', '-' + h);
}
parser.Push(item);
};
/**
* Handle moveleft, moveright commands
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.MoveLeftRight = function(parser: TexParser, name: string) {
// @test Move Left, Move Right, Move Left Negative, Move Right Negative
let h = parser.GetDimen(name);
let nh = (h.charAt(0) === '-' ? h.slice(1) : '-' + h);
if (name === '\\moveleft') {
let tmp = h;
h = nh;
nh = tmp;
}
parser.Push(
parser.itemFactory.create('position').setProperties({
name: parser.currentCS, move: 'horizontal',
left: parser.create('node', 'mspace', [], {width: h}),
right: parser.create('node', 'mspace', [], {width: nh})}) );
};
/**
* Handle horizontal spacing commands.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Hskip = function(parser: TexParser, name: string) {
// @test Modulo
const node = parser.create('node', 'mspace', [],
{width: parser.GetDimen(name)});
parser.Push(node);
};
/**
* Handle removal of spaces in script modes
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.Nonscript = function(parser: TexParser, _name: string) {
parser.Push(parser.itemFactory.create('nonscript'));
};
/**
* Handle Rule and Space command
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} style The style of the rule spacer.
*/
BaseMethods.Rule = function(parser: TexParser, name: string, style: string) {
// @test Rule 3D, Space 3D
const w = parser.GetDimen(name),
h = parser.GetDimen(name),
d = parser.GetDimen(name);
let def: EnvList = {width: w, height: h, depth: d};
if (style !== 'blank') {
def['mathbackground'] = (parser.stack.env['color'] || 'black');
}
const node = parser.create('node', 'mspace', [], def);
parser.Push(node);
};
/**
* Handle rule command.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.rule = function(parser: TexParser, name: string) {
// @test Rule 2D
const v = parser.GetBrackets(name),
w = parser.GetDimen(name),
h = parser.GetDimen(name);
let mml = parser.create('node', 'mspace', [], {
width: w, height: h,
mathbackground: (parser.stack.env['color'] || 'black') });
if (v) {
mml = parser.create('node', 'mpadded', [mml], {voffset: v});
if (v.match(/^\-/)) {
NodeUtil.setAttribute(mml, 'height', v);
NodeUtil.setAttribute(mml, 'depth', '+' + v.substr(1));
} else {
NodeUtil.setAttribute(mml, 'height', '+' + v);
}
}
parser.Push(mml);
};
/**
* Handle big command sequences, e.g., \\big, \\Bigg.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {number} mclass The TeX class of the element.
* @param {number} size The em size.
*/
BaseMethods.MakeBig = function(parser: TexParser, name: string, mclass: number, size: number) {
// @test Choose, Over With Delims, Above With Delims
size *= P_HEIGHT;
let sizeStr = String(size).replace(/(\.\d\d\d).+/, '$1') + 'em';
const delim = parser.GetDelimiter(name, true);
const mo = parser.create('token', 'mo', {
minsize: sizeStr, maxsize: sizeStr,
fence: true, stretchy: true, symmetric: true
}, delim);
const node = parser.create('node', 'TeXAtom', [mo], {texClass: mclass});
parser.Push(node);
};
/**
* Handle buildrel command.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
*/
BaseMethods.BuildRel = function(parser: TexParser, name: string) {
// @test BuildRel, BuildRel Expression
const top = parser.ParseUpTo(name, '\\over');
const bot = parser.ParseArg(name);
const node = parser.create('node', 'munderover');
// This is necessary to get the empty element into the children.
NodeUtil.setChild(node, 0, bot);
NodeUtil.setChild(node, 1, null);
NodeUtil.setChild(node, 2, top);
const atom = parser.create('node', 'TeXAtom', [node], {texClass: TEXCLASS.REL});
parser.Push(atom);
};
/**
* Handle horizontal boxes.
* @param {TexParser} parser The calling parser.
* @param {string} name The macro name.
* @param {string} style Box style.
* @param {string} font The mathvariant to use
*/