-
Notifications
You must be signed in to change notification settings - Fork 4
/
elemMath.js
1202 lines (1086 loc) · 48.9 KB
/
elemMath.js
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
// @ts-check
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode:nil; c-basic-offset: 4 -*- */
/* vim: set ts=4 et sw=4 tw=80: */
/*
Copyright (c) 2020 Neil Soiffer, Talking Cat Software
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
* The basic idea is that each digit gets its own column in the resulting table
* There are lots of wrinkles on this, including getting alignment correct, drawing lines, etc.
* https://w3c.github.io/mathml/#stacks-of-characters-mstack
*
* The algorithm works by building a data structure that closely mirrors the resulting table.
* Once all the rows are processed, that data structure is turned into an HTML table.
*/
import { _MathTransforms, MATHML_NS } from '../common/math-transforms.js'
const ELEM_MATH_CSS = `
table.elem-math {
border-collapse: collapse;
border-spacing: 0px;
}
table.elem-math tr {
vertical-align: baseline;
}
td.curved-line {
position: absolute;
padding-top: 0em;
width: 0.75em;
border: 0.3ex solid; /* match border bottom */
transform: translate(0.48em, -0.15em);
border-radius: 70%;
clip-path: inset(0.1em 0 0 0.45em);
box-sizing: border-box;
margin-left: -0.85em;
margin-right: 0.75em;
}
mtd.precedes-separator {
padding-right: 0 !important; /* override an inline style */
}
mtd.separator {
padding-left: 0 !important; /* override an inline style */
padding-right: 0 !important; /* override an inline style */
}
.carry {
font-size: 60%;
line-height: 90%;
width: 1px;
overflow: visible;
}
.hidden-digit {
visibility: hidden;
}
.crossout-horiz, .crossout-vert, .crossout-up, .crossout-down{
position: relative;
display: inline-block;
}
.crossout-horiz:before {
content: '';
border-bottom: .3ex solid black;
width: 140%;
position: absolute;
right: -20%;
top: 40%;
}
.crossout-vert::before {
content: '';
border-left: .3ex solid black;
height: 100%;
position: absolute;
right: 35%;
top: 0%;
}
.crossout-up::before {
content: '';
width: 100%;
position: absolute;
right: 0;
top: 40%;
}
.crossout-up::before {
border-bottom: .2em solid black;
transform: skewY(-60deg);
}
.crossout-down::after {
content: '';
width: 100%;
position: absolute;
right: 0;
top: 40%;
}
.crossout-down::after {
border-bottom: .2em solid black;
transform: skewY(60deg);
}
`
// msline defined values
const MSLINETHICKNESS_THIN = '.1ex'
const MSLINETHICKNESS_MEDIUM = '.35ex'
const MSLINETHICKNESS_THICK = '.65ex'
// mstack defined charspacing values
const MSTACK_TIGHT = '0em'
const MSTACK_MEDIUM = '.2em'
const MSTACK_LOOSE = '.4em'
const NON_BREAKING_SPACE = '\u00A0';
const NO_SPACE = '\u200A'; // hair space (need something that is a char for use in carries)
class MathMLAttrs {
/**
* Call the constructor when an mstyle is found
* @param {Element} el
* @param {Object} [previousAttrs=null]
*/
constructor(el, previousAttrs) {
this.attrs = {};
if (!previousAttrs) {
while (el && el.tagName.toLowerCase() !== 'math') {
if (el.tagName.toLowerCase() === 'mstyle') {
this.addAttrs(el);
}
el = el.parentElement;
}
if (el && el.tagName.toLowerCase() === 'math') {
this.addAttrs(el);
}
} else {
this.attrs = Object.assign({}, previousAttrs);
if (el.tagName.toLowerCase() === 'mstyle') {
// Override any attr that is already present
for (let attr of el.attributes) {
this.attrs[attr.name] = attr.value;
}
}
}
}
/**
* Add an attr of 'el' if it isn't already present in 'this.attrs' (helper fn for the constructor)
* @param {Element} el
*/
addAttrs(el) {
for (let attr of el.attributes) {
if (!this.attrs[attr.name]) {
this.attrs[attr.name] = attr.value;
}
}
}
/**
*
* @param {Element} el
* @param {string} name
* @param {string} defaultVal
* @returns {string}
*/
getAttr(el, name, defaultVal) {
if (el.hasAttribute(name)) {
return el.getAttribute(name);
}
return this.attrs[name] ? this.attrs[name] : defaultVal;
}
}
class Carry {
/**
*
* @param {string} location
* @param {string} crossout
* @param {number} scriptsizemultiplier
*/
constructor(location, crossout, scriptsizemultiplier) {
this.location = location;
this.crossout = crossout;
this.scriptsizemultiplier = scriptsizemultiplier
}
}
class TableCell {
// Holds data to construct the actual <td>
/**
* @param {string | Element} [value] // contents (digit) of the cell
* @param {string} [style=''] // style info for the cell
* @param {Carry} [carry=null] // a single carry
*/
constructor(value, style, carry) {
if (carry) {
if (typeof value !== "object") {
throw new Error("Elementary math mscarry isn't an 'object'");
}
this.data = document.createElement((carry.location === 'n' || carry.location === 's') ? 'div' : 'span');
this.data.appendChild(value);
this.data.className = 'carry';
this.data.style.fontSize = Math.round(carry.scriptsizemultiplier).toString() + '%';
} else {
if (typeof value !== "string") {
throw new Error("Elementary math mscarry isn't a 'string'");
}
this.data = document.createTextNode(value);
}
this.carry = carry; // for multiple carries, 'data' is already built up -- value is last carry seen
this.style = style || '';
}
}
class TableRow {
// Holds data to construct the actual <tr>
/**
* @param {TableCell[]} data // all cells in the row
* @param {number} [digitsOnRight] // # of digits to the right of '.' (includes '.') (can be negative due to shift)
* @param {number} [shift] // # amount of shift (position) -- need to track because of underlines
*/
constructor(data, digitsOnRight, shift) {
if (shift === 0) {
this.data = data;
} else if (shift > 0) {
this.data = this.padOnRight(data, shift);
} else if (shift < 0) {
this.data = this.padOnLeft(data, -shift);
digitsOnRight -= shift;
}
this.nRight = digitsOnRight;
this.shift = shift;
this.style = '';
this.addSpacingAfterRow = false; // want to add a little spacing later on
this.alignAt = 0; // no alignment needed (-1 is last line; 1 is first line)
}
/**
* @param {string} lineUnderThickness
* @param {string} color
*/
addUnderline(lineUnderThickness, color) {
this.style += `border-bottom: ${lineUnderThickness} solid ${color};`;
this.addSpacingAfterRow = true;
}
/**
*
* @param {number} shift
* @param {number} length
* @param {string} thickness
* @param {string} color
*/
addUnderlineToCells(shift, length, thickness, color) {
// the underlines should act independently of the previous line
// however, to do the underline, we need to attach them as borders to the above the cells
// pad previous row on left/right if needed
// note: order of padding is important so that 'right' is correct)
// note: we create new TableCells because we will modify it by adding an underline
let nLeftOfDecimalPoint = this.data.length - this.nRight;
let right = nLeftOfDecimalPoint - shift;
if (shift + length > nLeftOfDecimalPoint) {
this.data = this.padOnLeft(this.data, shift + length - nLeftOfDecimalPoint);
right = length;
}
if (shift < -this.nRight) {
this.data = this.padOnRight(this.data, this.nRight - shift);
this.nRight -= shift;
right = this.data.length;
}
// now add the underlines
for (let i = right - length; i < right; i++) {
this.data[i].style += `border-bottom: ${thickness} solid ${color};`;
}
this.addSpacingAfterRow = true;
}
// two helper functions that adds padding to the left or right side of an array
/**
*
* @param {TableCell[]} arr
* @param {number} amount
* @returns TableCell[]
*/
padOnLeft(arr, amount) {
let newCells = Array(amount);
for (let i = 0; i < amount; i++) {
newCells[i] = new TableCell(NO_SPACE);
}
return newCells.concat(arr);
}
/**
*
* @param {TableCell[]} arr
* @param {number} amount
* @returns TableCell[]
*/
padOnRight(arr, amount) {
let newCells = Array(amount);
for (let i = 0; i < amount; i++) {
newCells[i] = new TableCell(NO_SPACE);
}
return arr.concat(newCells);
}
}
class ElemMath {
/**
* mstack and mlondiv
* Note: we do *not* store the rows of the stack in here because (potentially) mlongdiv has its own stack for divisor/result
* Instead, we pass the rows as arguments to the various methods
*
* @param {Element} mstackOrLongDiv
*/
constructor(mstackOrLongDiv) {
this.stack = mstackOrLongDiv;
this.attrs = new MathMLAttrs(mstackOrLongDiv);
this.stackAlign = this.getAttr(mstackOrLongDiv, 'stackalign', 'decimalpoint');
this.charAlign = this.getAttr(mstackOrLongDiv, 'charalign', 'right');
this.charSpacing = this.getAttr(mstackOrLongDiv, 'charspacing', 'medium');
if (this.charSpacing === 'loose') {
this.charSpacing = MSTACK_LOOSE;
} else if (this.charSpacing === 'medium') {
this.charSpacing = MSTACK_MEDIUM;
} else if (this.charSpacing === 'tight') {
this.charSpacing = MSTACK_TIGHT;
}
this.longdivstyle = mstackOrLongDiv.tagName === 'mstack' ? '' : this.getAttr(mstackOrLongDiv, 'longdivstyle', 'lefttop');
// FIX: todo -- not yet dealt with
this.align = this.getAttr(mstackOrLongDiv,'algin', 'baseline');
}
/**
*
* @param {Element} el
* @param {string} name
* @param {string} defaultVal
* @returns {string}
*/
getAttr(el, name, defaultVal) {
return this.attrs.getAttr(el, name, defaultVal);
}
/**
* Add another row to the stack.
* If the last row is a row of carries, then this row is merged with them so there is no new row
* If this row is a row of carries also, then the merging is done differently
* @param {TableRow[]} rows
* @param {TableRow} newRow
*/
add(rows, newRow) {
/**
*
* @param {TableCell} cell
* @param {string} crossoutStyle
* @returns {TableCell} (updated cell)
*/
function addCrossoutToData(cell, crossoutStyle) {
// some crossouts are handled with :before or :after
// since there can only be one of these, we create a nested span for each crossout
const crossouts = crossoutStyle.split(' ');
let result = cell.data;
crossouts.forEach( function(crossout) {
if (crossout === 'none' || crossout==='') { // '' -- happens when there are two or more spaces in a row
return; // nothing to do
}
let span = document.createElement("span");
span.appendChild(result);
switch (crossout) {
case 'updiagonalstrike':
span.className = 'crossout-up';
break;
case 'downdiagonalstrike':
span.className = 'crossout-down';
break;
case 'verticalstrike':
span.className = 'crossout-vert';
break;
case 'horizontalstrike':
span.className = 'crossout-horiz';
break;
default:
span.className = 'crossout-up'; // do something
console.log(`Unknown crossout type '${crossoutStyle}`);
break;
}
result = span;
} );
cell.data = result;
return cell;
}
/**
*
* @param {TableCell} cell
* @param {TableCell} previousCell
* @returns {TableCell} (updated data)
*/
function mergeCarryAndData(cell, previousCell) {
let data = cell.data;
if (data.textContent === NO_SPACE) {
let span = document.createElement('span');
span.appendChild(data);
data.textContent = '0'; // need digit width to get decent spacing/placement of the carry
span.className = "hidden-digit";
data = span;
}
let parent = document.createElement('span');
parent.appendChild(data);
switch (previousCell.carry.location) {
case 'n':
case 'w':
parent.prepend(previousCell.data);
break;
case 'nw': {
let newElement = document.createElement('sup');
newElement.appendChild(previousCell.data);
parent.prepend(newElement);
break;
}
case 'ne': {
let newElement = document.createElement('sup');
newElement.appendChild(previousCell.data);
parent.appendChild(newElement);
break;
}
case 'e':
case 's':
parent.appendChild(previousCell.data);
break;
case 'se': {
let newElement = document.createElement('sub');
newElement.appendChild(previousCell.data);
parent.appendChild(newElement);
break;
}
case 'sw': {
let newElement = document.createElement('sub');
newElement.appendChild(previousCell.data);
parent.prepend(newElement);
break;
}
default:
console.log(`Unknown crossout location '${previousCell.carry.location}`);
break;
}
cell.data = parent;
return cell;
}
let previousRow = rows[rows.length - 1];
// if the previous row is a carry row, then the non "fill" spots will have a carry -- just need to find one
if (rows.length === 0 ||
!previousRow.data.find( cell => cell.carry )) {
rows.push(newRow); // "normal" row -- just add it
return;
}
// have to merge the rows
// first make them the same size, padding on left/right if needed
const extraToAddOnLeft = (newRow.data.length - newRow.nRight) - (previousRow.data.length - previousRow.nRight);
if (extraToAddOnLeft !== 0) {
if (extraToAddOnLeft < 0) {
newRow.data = newRow.padOnLeft(newRow.data, -extraToAddOnLeft);
} else {
previousRow.data = previousRow.padOnLeft(previousRow.data, extraToAddOnLeft);
}
}
const extraToAddOnRight = newRow.nRight - previousRow.nRight;
if (extraToAddOnRight !== 0) {
if (extraToAddOnRight < 0) {
newRow.data = newRow.padOnRight(newRow.data, -extraToAddOnRight);
} else {
previousRow.data = previousRow.padOnRight(previousRow.data, extraToAddOnRight);
}
}
// merge the data now that the rows have the same number of elements
for (let i=0; i < newRow.data.length; i++) {
const prevCell = previousRow.data[i];
let cell = newRow.data[i];
if (prevCell.carry) {
cell = addCrossoutToData(cell, prevCell.carry.crossout);
cell = mergeCarryAndData(cell, prevCell);
cell.alignAt = prevCell.carry.location === 's' ? 1 : -1;
newRow.data[i] = cell;
}
}
rows[rows.length - 1] = newRow; // replace the carry row with the current row
}
/**
*
* @param {Element} msrow
* @returns {[TableCell[], number]}
*/
process_msrow(msrow) {
// The spec doesn't say how to determine decimal alignment in an msrow
// Here, we take the first 'mn' we find to be the determination of a '.'.
// Anything after the 'mn' is considered to be to the right of the '.'
let foundNumber = false;
let nDigitsRightOfDecimalPt = 0;
//** @type {TableCell[]} */
let cells = [];
for (let i=0; i<msrow.children.length; i++) {
const child = msrow.children[i];
if (child.tagName.toLowerCase() === 'mn') {
const chars = child.textContent.trim().split('');
cells = cells.concat( chars.map( c => new TableCell(c)) );
if (foundNumber) {
nDigitsRightOfDecimalPt += chars.length;
} else {
const iDecimalPt = child.textContent.trim().indexOf(this.getAttr(child, 'decimalpoint', '.'));
nDigitsRightOfDecimalPt = iDecimalPt < 0 ? 0 : chars.length - iDecimalPt;
foundNumber = true;
}
} else {
// everything should be in one column.
// FIX: the child might be something complex -- textContent might be inappropriate
cells.push( new TableCell(child.textContent.trim()) );
if (foundNumber) {
nDigitsRightOfDecimalPt += 1;
}
}
}
return [cells, this.stackAlign !== 'decimalpoint' ? 0 : nDigitsRightOfDecimalPt];
}
/**
*
* @param {Element} row
* @param {string} location
* @param {string} crossout
* @param {number} scriptsizemultiplier
* @returns {TableCell[]}
*/
process_mscarries(row, location, crossout, scriptsizemultiplier) {
let cells = [];
let child = row.children[0];
// children are pulled out of the row and put in the TableCell, so we can't use a standard 'for' loop
while (child) {
let nextChild = child.nextElementSibling; // do this before child is modified
let cellLocation = location;
let cellCrossout = crossout
if (child.tagName.toLowerCase() === 'mscarry') {
cellLocation = this.getAttr(child, 'location', 'n');
cellCrossout = this.getAttr(child, 'crossout', 'none');
// FIX: child could be any MathML construct -- currently only supporting a *leaf*
// the text content of the parent will match that of the *leaf* child, so nothing to change here
}
cells.push( new TableCell(child, '', new Carry(cellLocation, cellCrossout, scriptsizemultiplier)) );
child = nextChild;
}
return cells;
}
/**
* @param {Element} node
* @param {TableRow[]} rows
* @param {number} position
* @param {number} [rowShift=0]
* @returns {TableRow[]}
*/
processChildren(node, rows, position, rowShift) {
if (!node.children) {
return rows;
}
rowShift = rowShift || 0;
// Note: we only want to compute a decimal position (which is an align point) when stackAlign==='decimalpoint'; otherwise alignment will be off
for (let i= (node.tagName.toLowerCase() === 'mlongdiv' ? 2 : 0); i<node.children.length; i++) {
rows = this.processChild(node.children[i], rows, position);
position += rowShift; // non-zero when specified by msgroup; applies to 2nd and subsequent rows
}
return rows;
}
/**
* @param {Element} child
* @param {TableRow[]} rows
* @param {number} position
* @returns {TableRow[]}
*/
processChild(child, rows, position) {
let shift = position + parseInt(this.getAttr(child, 'position', '0'));
switch (child.tagName.toLowerCase()) {
case 'mn': {
const chars = child.textContent.trim().split('');
const iDecimalPt = child.textContent.trim().indexOf(this.getAttr(child, 'decimalpoint', '.'));
const nDigitsRightOfDecimalPt = (this.stackAlign !== 'decimalpoint' || iDecimalPt < 0) ? 0 : chars.length - iDecimalPt;
const cells = chars.map( c => new TableCell(c));
this.add(rows, new TableRow(cells, nDigitsRightOfDecimalPt, shift) );
break;
}
case 'msgroup':
rows = this.processChildren(child, rows, shift, parseInt(this.getAttr(child, 'shift', '0')));
break;
case 'msline': {
const length = parseInt(this.getAttr(child, 'length', '0'));
let thickness = this.getAttr(child, 'mslinethickness', 'medium');
if (thickness === 'medium') {
thickness = MSLINETHICKNESS_MEDIUM;
} else if (thickness === 'thin') {
thickness = MSLINETHICKNESS_THIN;
} else if (thickness === 'thick') {
thickness = MSLINETHICKNESS_THICK;
}
if (rows.length === 0) {
this.add(rows, new TableRow([], 0, 0) );
}
const previousRow = rows[rows.length-1];
const mathcolor = this.getAttr(child, 'mathcolor', 'black');
if (length === 0) {
previousRow.addUnderline(thickness, mathcolor);
} else {
previousRow.addUnderlineToCells(shift, length, thickness, mathcolor);
}
break;
}
case 'mscarries': {
let location = this.getAttr(child, 'location', 'n');
let crossout = this.getAttr(child, 'crossout', 'none');
let scriptsizemultiplier = parseFloat(this.getAttr(child, 'scriptsizemultiplier', '0.6'));
this.add(rows, new TableRow(this.process_mscarries(child, location, crossout, 100*scriptsizemultiplier), 0, shift) );
break;
}
case 'mstyle': {
const oldAttrs = this.attrs;
this.attrs = new MathMLAttrs(child, oldAttrs);
if (child.children.length === 1 && child.children[0].tagName.toLowerCase() === 'msline') {
// FIX: not legal according to spec, but should be able to wrap msline in mstyle to change mathcolor
// FIX: spec should be fixed
this.processChild(child.children[0], rows, shift);
} else {
let cells;
let nDigitsRightOfDecimalPt;
[cells, nDigitsRightOfDecimalPt] = this.process_msrow(child);
this.add(rows, new TableRow(cells, nDigitsRightOfDecimalPt, shift) );
this.attrs = oldAttrs;
}
}
break;
default: {
let cells;
let nDigitsRightOfDecimalPt = 0;
if (child.tagName.toLowerCase() == 'msrow') {
[cells, nDigitsRightOfDecimalPt] = this.process_msrow(child);
} else {
// FIX: this isn't right for non-leaf cells
// We are out of a MathML context inside of the table we are building, so we can't just stuff the MathML in it
cells = [new TableCell(child.textContent.trim())];
}
this.add(rows, new TableRow(cells, nDigitsRightOfDecimalPt, shift) );
break;
}
}
return rows;
}
/**
*
* @param {TableRow[]} rows
* @param {string} stackAlign
* @returns {TableRow[]}
*/
processShifts(rows, stackAlign) {
let maxLeftOfDecimalPt = 0;
let maxRightOfDecimalPt = 0; // only used when doing decimal alignment
// we want to fill out all the entries in each row
// when doing decimal alignment, we need to keep track of int and fractional part
// first, compute the max digits across all the rows
for (const row of rows) {
if (stackAlign === 'decimalpoint') {
maxLeftOfDecimalPt = Math.max(maxLeftOfDecimalPt, row.data.length - row.nRight);
maxRightOfDecimalPt = Math.max(maxRightOfDecimalPt, row.nRight);
} else {
maxLeftOfDecimalPt = Math.max(maxLeftOfDecimalPt, row.data.length);
}
}
// now pad each row
for (const row of rows) {
switch (stackAlign) {
case 'decimalpoint':
row.data = row.padOnLeft(row.data, maxLeftOfDecimalPt - (row.data.length - row.nRight));
row.data = row.padOnRight(row.data, maxRightOfDecimalPt - row.nRight);
row.nRight = maxRightOfDecimalPt;
break;
case 'left':
row.data = row.padOnRight(row.data, maxLeftOfDecimalPt - row.data.length);
break;
case 'center': {
const padding = maxLeftOfDecimalPt - row.data.length;
row.data = row.padOnRight(row.data, padding/2);
row.data = row.padOnLeft(row.data, padding - padding/2); // remainder after half fill above
break;
}
case 'right':
row.data = row.padOnLeft(row.data, maxLeftOfDecimalPt - row.data.length);
break;
default:
console.log(`Unknown mstack stackalign attr value: "${stackAlign}"`);
break;
}
}
return rows;
}
/**
*
* @param {Element} divisor
* @param {Element} result
* @param {TableRow[]} stackRows
* @returns {TableRow[]}
*/
addOnLongDivParts(divisor, result, stackRows) {
/**
* @param {TableRow} row
*/
function countPaddingOnRight(row) {
for (let i = row.data.length-1; i>=0; i--) {
const cell = row.data[i];
if (cell.data.textContent !== NO_SPACE) {
return row.data.length - 1 - i;
}
}
return row.data.length;
}
/**
* @param {TableRow} row
* @param {number} nKeep // number of padded cells to keep (if not enough cells, appropriate # is added)
* @returns {TableRow}
*/
function removePaddingOnRight(row, nKeep) {
let nDeletedRight = 0;
// delete empty cells from end
for (let i = row.data.length-1; i>=0; i--) {
const cell = row.data[i];
if (cell.data.textContent !== NO_SPACE) {
break;
}
if (nKeep > 0) {
nKeep--;
} else {
row.data.pop();
nDeletedRight++;
}
}
// add on any needed cells
for (let i=0; i<nKeep; i++) {
row.data.push( new TableCell(NO_SPACE) );
}
row.nRight -= nDeletedRight - nKeep;
return row;
}
const mathcolor = this.getAttr(this.stack, 'mathcolor', 'black');
// Note: we assure there are divisors, results and at least one row in the stack for layout by creating dummy entries if needed.
// For a few styles, a second row is needed -- those are handled in those cases.
if (stackRows.length == 0) {
stackRows.push( new TableRow( [new TableCell(NO_SPACE)], 0, 0 ) );
}
// FIX: this is broken for anything that is more than one row tall.
/** @type{TableRow[]} */
let divisorRows = divisor ? this.processChild(divisor, [], 0) : [new TableRow( [new TableCell(NO_SPACE)], 0, 0 )];
let divisorRow = divisorRows[0]; // FIX: currently can only handle one row
let iLastDivisorDigit = divisorRow.data.length-1;
let resultRows = result ? this.processChild(result, [], 0) : [new TableRow( [new TableCell(NO_SPACE)], 0, 0 )];
let resultRow = resultRows[0]; // FIX: currently can only handle one row
switch (this.longdivstyle) {
case 'left/\\right':
case 'left)(right': {
// Easy case -- everything goes on first line
const leftDelim = new TableCell( this.longdivstyle === 'left/\\right' ? '/' : ')' );
const rightDelim = new TableCell( this.longdivstyle === 'left/\\right' ? '\\' : '(' );
if (stackRows.length === 0) {
stackRows.push( new TableRow(
divisorRow.data.concat(
[leftDelim],
new TableCell(NO_SPACE),
[rightDelim],
resultRows[0].data)
));
} else {
stackRows[0].data = divisorRow.data.concat(
[leftDelim],
removePaddingOnRight(stackRows[0], 0).data,
[rightDelim],
resultRows[0].data)
stackRows[0].nRight += 1 + resultRows[0].data.length;
}
break;
}
case ':right=right': {
// Easy case -- everything goes on first line
if (stackRows.length === 0) {
stackRows.push( new TableRow(
[new TableCell(':')].concat(
divisorRow.data,
[new TableCell('=')],
resultRow.data)
));
} else {
stackRows[0].data = removePaddingOnRight(stackRows[0], 0).data.concat(
[new TableCell(':')],
divisorRow.data,
[new TableCell('=')],
resultRow.data)
stackRows[0].nRight += 2 + divisorRow.data.length + resultRow.data.length;
}
break;
}
case 'stackedrightright':
case 'mediumstackedrightright':
case 'shortstackedrightright': {
// mstack on left, vertical line down right side of mstack; divisor to the right of that, horizontal line, then result underneath
// FIX: this only works for *leaf* elements
// need to assure there are at least two rows in the stack (already made sure there is one)
if (stackRows.length == 1) {
stackRows.push( new TableRow( [new TableCell(NO_SPACE)], 0, 0 ) );
stackRows = this.processShifts(stackRows, this.stackAlign);
}
// First, add a row of padding on right and put a line down the right side of them
if (this.longdivstyle !== 'stackedrightright') {
// want to suck these lines in -- find out how much padding there is on each line and remove some
const nLine1Padding = countPaddingOnRight(stackRows[0]);
const nLine2Padding = countPaddingOnRight(stackRows[1]);
const nRemove = Math.min(nLine1Padding, nLine2Padding);
stackRows[0] = removePaddingOnRight(stackRows[0], nLine1Padding - nRemove);
stackRows[1] = removePaddingOnRight(stackRows[1], nLine2Padding - nRemove);
}
const verticalLineLength = this.longdivstyle === 'shortstackedrightright' ? 1 :
this.longdivstyle === 'mediumstackedrightright' ? 2 : stackRows.length;
for (let i = 0; i < verticalLineLength; i++) {
let newCell = new TableCell(NO_SPACE);
if (i < verticalLineLength) {
newCell.style += `border-right: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;
}
stackRows[i].data.push(newCell);
}
// Add some padding on the left to the divisor and result to separate them from the line
// FIX: unfortunately, this also adds space in the columns below for the non 'stackedrightright' cases.
// FIX: maybe there are some games to be played with columnspans...
if (this.longdivstyle === 'stackedrightright') {
divisorRow.data[0].style += 'padding-left: 0.5em;';
resultRow.data[0].style += 'padding-left: 0.5em;';
}
// Attach the divisor to the first line (note: the divisor and result are *not* decimal aligned)
const nCellsLargerResultThanDivisor = resultRow.data.length - divisorRow.data.length;
if (nCellsLargerResultThanDivisor > 0) {
divisorRow.data = divisorRow.padOnRight(divisorRow.data, nCellsLargerResultThanDivisor);
}
divisorRow.addUnderlineToCells(-divisorRow.nRight, divisorRow.data.length, MSLINETHICKNESS_MEDIUM, mathcolor);
divisorRow.addSpacingAfterRow = false;
stackRows[0].data = stackRows[0].data.concat(divisorRow.data);
stackRows[0].nRight += divisorRow.data.length
// Attach the result to the second line
stackRows[1].data = stackRows[1].data.concat(resultRow.data);
stackRows[1].nRight += resultRow.data.length
break;
}
case 'stackedleftleft': {
// mstack on right, vertical line down left side of mstack; divisor to the left of that, horizontal line, then result underneath
// we need at least two stack elements for this layout
if (stackRows.length == 1) {
stackRows.push( new TableRow( [new TableCell(NO_SPACE)], 0, 0 ) );
stackRows = this.processShifts(stackRows, this.stackAlign);
}
// FIX: this only works for *leaf* elements
// First, add a row of padding on left and put a line down the right side of them
for (let i = 0; i < stackRows.length; i++) {
let newCell = new TableCell('');
newCell.style += `border-left: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;
stackRows[i].data.unshift(newCell);
}
// Add some padding on the right to the divisor and result to separate them from the line
divisorRow.data[divisorRow.data.length-1].style += 'padding-right: 0.5em;';
resultRow.data[resultRow.data.length-1].style += 'padding-right: 0.5em;';
// Attach the divisor to the first line (note: the divisor and result are *not* decimal aligned)
const nCellsLargerResultThanDivisor = resultRow.data.length - divisorRow.data.length;
if (nCellsLargerResultThanDivisor > 0) {
divisorRow.data = divisorRow.padOnLeft(divisorRow.data, nCellsLargerResultThanDivisor);
}
divisorRow.addUnderlineToCells(-divisorRow.nRight, divisorRow.data.length, MSLINETHICKNESS_MEDIUM, mathcolor);
divisorRow.addSpacingAfterRow = false;
stackRows[0].data = divisorRow.data.concat(stackRows[0].data);
// Attach the result to the second line
stackRows[1].data = resultRow.data.concat(stackRows[1].data);
break;
}
case 'righttop': {
// First, put the result on top with a line underneath
resultRow.addUnderline(MSLINETHICKNESS_MEDIUM, mathcolor);
resultRow.addSpacingAfterRow = false; // don't want to add extra spacing
let mergedRows = resultRows.concat(stackRows);
stackRows = this.processShifts(mergedRows, this.stackAlign);
divisorRow.data[0].style += `border-left: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;
divisorRow.addUnderlineToCells(-divisorRow.nRight, divisorRow.data.length, MSLINETHICKNESS_MEDIUM, mathcolor);
stackRows[1].data = stackRows[1].data.concat(divisorRow.data);
stackRows[1].nRight += divisorRow.data.length;
break;
}
case 'lefttop':
case 'stackedleftlinetop':
default: {
// left top -- divisor to left of stack, result new row on top (part of stack and underlined)
// FIX: this only works for *leaf* elements
// First, put the result on top with a line underneath
resultRow.addUnderlineToCells(-resultRow.nRight, Math.max(resultRow.data.length, stackRows[0].data.length), MSLINETHICKNESS_MEDIUM, mathcolor);
resultRow.addSpacingAfterRow = false; // don't want to add extra spacing
let mergedRows = resultRows.concat(stackRows);
stackRows = this.processShifts(mergedRows, this.stackAlign);
if (this.longdivstyle === 'stackedleftlinetop') {
divisorRow.data[divisorRow.data.length-1].style += `border-right: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;
divisorRow.data[divisorRow.data.length-1].style += `border-right: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;divisorRow.data[divisorRow.data.length-1].data.style += 'position:relative';
divisorRow.addUnderlineToCells(-divisorRow.nRight, divisorRow.data.length, MSLINETHICKNESS_MEDIUM, mathcolor);
} else {
// add the ")" to the element (handled like a curved border with css)
divisorRow.data = divisorRow.padOnRight(divisorRow.data, 1)
iLastDivisorDigit += 1;
divisorRow.data[iLastDivisorDigit].class = 'curved-line';
divisorRow.data[iLastDivisorDigit].style = ''; // let CSS deal with it
}
stackRows[1].data = divisorRow.data.concat(stackRows[1].data);
break;
}
}
let answer = this.processShifts(stackRows, this.stackAlign);
if (this.longdivstyle === 'lefttop') {
// extend the line to the left one cell to be above the added ')'
stackRows[0].data[iLastDivisorDigit].style += `border-bottom: ${MSLINETHICKNESS_MEDIUM} solid ${mathcolor};`;
}
return answer;
}