-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.js
More file actions
1690 lines (1371 loc) · 58.5 KB
/
expression.js
File metadata and controls
1690 lines (1371 loc) · 58.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
/*
Fōrmulæ calculus package. Module for expression definition & visualization.
Copyright (C) 2015-2026 Laurence R. Ugalde
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
export class CalculusPackage extends Formulae.ExpressionPackage {};
const SIGN_GAP_FRAC = 0.00; // gap right of ∫ sign, as fraction of widthSymbol
const SIGN_GAP_FRAC_CLOSED_DOMAIN = 0.30; // wider gap for closed-domain; the oval protrudes right of the sign column
const D_GAP_FRAC = 0.30; // gap between integrand and differential d, as fraction of widthSymbol
const LIMIT_GAP_FRAC = 0.08; // gap between ∫ sign and limit expressions, as fraction of widthSymbol
const LIMIT_SIZE = -4; // font-size delta for limit subexpressions
const TYPE_EVAL_BAR = 1; // f(x) |_a^b
const TYPE_EVAL_BRACKET = 2; // [f(x)]_a^b
const EVAL_GAP_FRAC = 0.10; // gap between expression and bar/bracket, as fraction of fontSize
const EVAL_SERIF_FRAC = 0.25; // bracket serif length as fraction of fontSize
const CIRCLE_CX_FRAC = 0.45; // oval center as fraction of widthSymbol from sign left edge
const CIRCLE_RADIUS_SINGLE = 0.30; // r for dims=1, as fraction of widthSymbol
const CIRCLE_RADIUS_MULTI = 0.35; // r for dims≥2, as fraction of widthSymbol
const CIRCLE_STROKE_WIDTH = 0.06; // oval lineWidth as fraction of widthSymbol
// Path data from Wikipedia's integral SVG.
// Original viewBox: 0 0 52 75; <g> transform: translate(0,75) scale(0.1,-0.1)
// The path uses PostScript y-up coordinates; the transform is re-applied in drawIntegralSign.
const INTEGRAL_SIGN_PATH = new Path2D(
"M342 648 c-24 -29 -49 -130 -87 -343 c-19 -107 -33 -155 -46 -155 " +
"c-5 0 -8 3 -7 7 c2 5 0 9 -4 11 c-5 1 -8 0 -8 -3 c0 -3 0 -9 0 -15 " +
"c0 -5 9 -10 20 -10 c32 0 48 47 90 262 c40 203 59 271 67 236 " +
"c6 -23 26 -23 21 -1 c-4 22 -31 28 -46 11 z"
);
// Tight bounding box of the sign within the 52×75 SVG viewport.
// The path leaves large margins; these constants clip them so the sign
// fills the target box exactly.
const SIGN_SVG_X = 19; // left edge of sign in SVG space
const SIGN_SVG_Y = 8; // top edge of sign in SVG space
const SIGN_SVG_W = 21; // width of sign in SVG space (right edge ≈ 40)
const SIGN_SVG_H = 53; // height of sign in SVG space (bottom edge ≈ 61)
// Renders the ∫ sign in a target box (x, y, w, h) using the current fillStyle.
// Applies both the tight-bounds clipping and the original <g> transform.
function drawIntegralSign(context, x, y, w, h) {
context.save();
context.translate(x, y);
context.scale(w / SIGN_SVG_W, h / SIGN_SVG_H);
context.translate(-SIGN_SVG_X, -SIGN_SVG_Y);
context.translate(0, 75);
context.scale(0.1, -0.1);
context.fill(INTEGRAL_SIGN_PATH);
context.restore();
}
// Draws a stadium (two semicircles joined by horizontal tangents) centered
// vertically at cy. For dims=1 the two centers coincide and it degenerates
// to a full circle. strokeStyle is set to match the current fillStyle so
// the oval color tracks selection highlights and theme changes.
function drawClosedIntegralOval(context, cx1, cy, cx2, r, lineWidth) {
context.save();
context.lineWidth = lineWidth;
context.strokeStyle = context.fillStyle;
context.beginPath();
context.arc(cx1, cy, r, Math.PI / 2, -Math.PI / 2, false); // left semicircle
context.lineTo(cx2, cy - r); // top tangent
context.arc(cx2, cy, r, -Math.PI / 2, Math.PI / 2, false); // right semicircle
context.closePath(); // bottom tangent
context.stroke();
context.restore();
}
// ─── Indefinite integral: ∫ f dx ────────────────────────────────────────────
const IndefiniteIntegral = class extends Expression {
getTag() { return "Calculus.Integral.IndefiniteIntegral"; }
getName() { return CalculusPackage.messages.nameIndefiniteIntegral; }
canHaveChildren(count) { return count === 2; }
getChildName(index) { return CalculusPackage.messages.childrenIndefiniteIntegral[index]; }
prepareDisplay(context) {
this.heightSymbol = Math.floor(context.fontInfo.size * 3);
this.widthSymbol = Math.round(this.heightSymbol * SIGN_SVG_W / SIGN_SVG_H);
let signGap = Math.round(this.widthSymbol * SIGN_GAP_FRAC);
let dGap = Math.round(this.widthSymbol * D_GAP_FRAC);
let ch0 = this.children[0]; // integrand
let ch1 = this.children[1]; // variable
ch0.prepareDisplay(context);
ch1.prepareDisplay(context);
this.dWidth = Math.ceil(context.measureText("d").width);
// Vertical: center the ∫ sign at horzBaseline; accommodate both children
this.horzBaseline = Math.max(
Math.floor(this.heightSymbol / 2),
ch0.horzBaseline,
ch1.horzBaseline
);
this.height = this.horzBaseline + Math.max(
Math.ceil(this.heightSymbol / 2),
ch0.height - ch0.horzBaseline,
ch1.height - ch1.horzBaseline
);
// Horizontal: [∫] [signGap] [integrand] [dGap] [d] [variable]
ch0.x = this.widthSymbol + signGap;
ch0.y = this.horzBaseline - ch0.horzBaseline;
this.dX = ch0.x + ch0.width + dGap;
ch1.x = this.dX + this.dWidth;
ch1.y = this.horzBaseline - ch1.horzBaseline;
this.width = ch1.x + ch1.width;
this.vertBaseline = Math.floor(this.width / 2);
}
display(context, x, y) {
drawIntegralSign(
context,
x,
y + this.horzBaseline - Math.floor(this.heightSymbol / 2),
this.widthSymbol,
this.heightSymbol
);
let ch0 = this.children[0];
ch0.display(context, x + ch0.x, y + ch0.y);
let bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
super.drawText(context, "d", x + this.dX, y + this.horzBaseline + Math.round(context.fontInfo.size / 2));
context.fontInfo.setItalic(context, bkpItalic);
let ch1 = this.children[1];
ch1.display(context, x + ch1.x, y + ch1.y);
}
moveAcross(i, direction) {
if (direction === Expression.NEXT && i === 0) return this.children[1].moveTo(direction);
if (direction === Expression.PREVIOUS && i === 1) return this.children[0].moveTo(direction);
return this.moveOut(direction);
}
moveTo(direction) {
return direction === Expression.PREVIOUS
? this.children[1].moveTo(direction)
: this.children[0].moveTo(direction);
}
};
// ─── Definite integral: ∫_a^b f dx ─────────────────────────────────────────
const DefiniteIntegral = class extends Expression {
getTag() { return "Calculus.Integral.DefiniteIntegral"; }
getName() { return CalculusPackage.messages.nameDefiniteIntegral; }
canHaveChildren(count) { return count === 4; }
getChildName(index) { return CalculusPackage.messages.childrenDefiniteIntegral[index]; }
prepareDisplay(context) {
this.heightSymbol = Math.floor(context.fontInfo.size * 3);
this.widthSymbol = Math.round(this.heightSymbol * SIGN_SVG_W / SIGN_SVG_H);
let signGap = Math.round(this.widthSymbol * SIGN_GAP_FRAC);
let dGap = Math.round(this.widthSymbol * D_GAP_FRAC);
let limitGap = Math.round(this.widthSymbol * LIMIT_GAP_FRAC);
let ch0 = this.children[0]; // integrand
let ch1 = this.children[1]; // variable
let ch2 = this.children[2]; // lower bound
let ch3 = this.children[3]; // upper bound
ch0.prepareDisplay(context);
ch1.prepareDisplay(context);
{
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
ch2.prepareDisplay(context);
ch3.prepareDisplay(context);
context.fontInfo.setSizeAbsolute(context, bkp);
}
this.dWidth = Math.ceil(context.measureText("d").width);
// Sign column is wide enough to center the sign, lower, and upper
this.widthSignColumn = Math.max(this.widthSymbol, ch2.width, ch3.width);
this.symbolX = Math.floor((this.widthSignColumn - this.widthSymbol) / 2);
let spaceTop = ch3.height + limitGap;
let spaceBottom = ch2.height + limitGap;
// Vertical: sign center at horzBaseline; must leave room for upper bound above
this.horzBaseline = Math.max(
spaceTop + Math.floor(this.heightSymbol / 2),
ch0.horzBaseline,
ch1.horzBaseline
);
this.symbolY = this.horzBaseline - Math.floor(this.heightSymbol / 2);
// Limits centered within sign column, tight against sign top/bottom
ch3.x = Math.floor((this.widthSignColumn - ch3.width) / 2);
ch3.y = this.symbolY - limitGap - ch3.height;
ch2.x = Math.floor((this.widthSignColumn - ch2.width) / 2);
ch2.y = this.symbolY + this.heightSymbol + limitGap;
// Integrand and variable to the right of the sign column
ch0.x = this.widthSignColumn + signGap;
ch0.y = this.horzBaseline - ch0.horzBaseline;
this.dX = ch0.x + ch0.width + dGap;
ch1.x = this.dX + this.dWidth;
ch1.y = this.horzBaseline - ch1.horzBaseline;
this.width = ch1.x + ch1.width;
this.height = this.horzBaseline + Math.max(
Math.ceil(this.heightSymbol / 2) + spaceBottom,
ch0.height - ch0.horzBaseline,
ch1.height - ch1.horzBaseline
);
this.vertBaseline = Math.floor(this.width / 2);
}
display(context, x, y) {
drawIntegralSign(
context,
x + this.symbolX,
y + this.symbolY,
this.widthSymbol,
this.heightSymbol
);
let ch0 = this.children[0];
ch0.display(context, x + ch0.x, y + ch0.y);
let bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
super.drawText(context, "d", x + this.dX, y + this.horzBaseline + Math.round(context.fontInfo.size / 2));
context.fontInfo.setItalic(context, bkpItalic);
let ch1 = this.children[1];
ch1.display(context, x + ch1.x, y + ch1.y);
let bkpSize = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
let ch2 = this.children[2];
ch2.display(context, x + ch2.x, y + ch2.y);
let ch3 = this.children[3];
ch3.display(context, x + ch3.x, y + ch3.y);
context.fontInfo.setSizeAbsolute(context, bkpSize);
}
moveAcross(i, direction) {
switch (direction) {
case Expression.NEXT:
if (i === 0) return this.children[1].moveTo(direction);
if (i === 2 || i === 3) return this.children[0].moveTo(direction);
break;
case Expression.PREVIOUS:
if (i === 1) return this.children[0].moveTo(direction);
break;
case Expression.UP:
if (i === 0 || i === 1) return this.children[3].moveTo(direction);
if (i === 2) return this.children[0].moveTo(direction);
break;
case Expression.DOWN:
if (i === 0 || i === 1) return this.children[2].moveTo(direction);
if (i === 3) return this.children[0].moveTo(direction);
break;
}
return this.moveOut(direction);
}
moveTo(direction) {
switch (direction) {
case Expression.UP: return this.children[2].moveTo(direction);
case Expression.DOWN: return this.children[3].moveTo(direction);
case Expression.PREVIOUS: return this.children[1].moveTo(direction);
default: return this.children[0].moveTo(direction);
}
}
};
// ─── Definite integral over domain: ∫∫_D f dA ──────────────────────────────
const MULTI_SIGN_STEP = 0.80; // < 1.0 so consecutive ∫ signs overlap slightly, giving the stacked-integral look
const DefiniteIntegralOverDomain = class extends Expression {
getTag() { return "Calculus.Integral.DefiniteIntegralOverDomain"; }
getName() { return CalculusPackage.messages.nameDefiniteIntegralOverDomain; }
canHaveChildren(count) { return count === 3; }
getChildName(index) { return CalculusPackage.messages.childrenDefiniteIntegralOverDomain[index]; }
getSerializationNames() { return [ "Dimensions", "ClosedDomain" ]; }
async getSerializationStrings() { return [ String(this.dimensions), this.closedDomain ? "True" : "False" ]; }
setSerializationStrings(strings, promises) { this.dimensions = parseInt(strings[0]); this.closedDomain = strings[1] === "True"; }
prepareDisplay(context) {
let dims = this.dimensions || 1;
this.heightSymbol = Math.floor(context.fontInfo.size * 3);
this.widthSymbol = Math.round(this.heightSymbol * SIGN_SVG_W / SIGN_SVG_H);
this.signStep = Math.round(this.widthSymbol * MULTI_SIGN_STEP);
this.signGroupWidth = this.widthSymbol + (dims - 1) * this.signStep;
let signGap = Math.round(this.widthSymbol * (this.closedDomain ? SIGN_GAP_FRAC_CLOSED_DOMAIN : SIGN_GAP_FRAC));
let dGap = Math.round(this.widthSymbol * D_GAP_FRAC);
let limitGap = Math.round(this.widthSymbol * LIMIT_GAP_FRAC);
let ch0 = this.children[0]; // integrand
let ch1 = this.children[1]; // domain
let ch2 = this.children[2]; // differential element
ch0.prepareDisplay(context);
ch2.prepareDisplay(context);
this.dWidth = Math.ceil(context.measureText("d").width);
{
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
ch1.prepareDisplay(context);
context.fontInfo.setSizeAbsolute(context, bkp);
}
// Sign column: wide enough for sign group and domain label
this.widthSignColumn = Math.max(this.signGroupWidth, ch1.width);
this.symbolGroupX = Math.floor((this.widthSignColumn - this.signGroupWidth) / 2);
let spaceBottom = ch1.height + limitGap;
this.horzBaseline = Math.max(
Math.floor(this.heightSymbol / 2),
ch0.horzBaseline,
ch2.horzBaseline
);
this.symbolY = this.horzBaseline - Math.floor(this.heightSymbol / 2);
ch1.x = Math.floor((this.widthSignColumn - ch1.width) / 2);
ch1.y = this.symbolY + this.heightSymbol + limitGap;
ch0.x = this.widthSignColumn + signGap;
ch0.y = this.horzBaseline - ch0.horzBaseline;
this.dX = ch0.x + ch0.width + dGap;
ch2.x = this.dX + this.dWidth;
ch2.y = this.horzBaseline - ch2.horzBaseline;
this.width = ch2.x + ch2.width;
this.height = this.horzBaseline + Math.max(
Math.ceil(this.heightSymbol / 2) + spaceBottom,
ch0.height - ch0.horzBaseline,
ch2.height - ch2.horzBaseline
);
this.vertBaseline = Math.floor(this.width / 2);
}
display(context, x, y) {
let dims = this.dimensions || 1;
for (let i = 0; i < dims; i++) {
drawIntegralSign(
context,
x + this.symbolGroupX + i * this.signStep,
y + this.symbolY,
this.widthSymbol,
this.heightSymbol
);
}
if (this.closedDomain) {
let rFrac = dims === 1 ? CIRCLE_RADIUS_SINGLE : CIRCLE_RADIUS_MULTI;
let r = Math.round(this.widthSymbol * rFrac);
let cxOff = Math.round(this.widthSymbol * CIRCLE_CX_FRAC);
drawClosedIntegralOval(
context,
x + this.symbolGroupX + cxOff,
y + this.horzBaseline,
x + this.symbolGroupX + (dims - 1) * this.signStep + cxOff,
r,
Math.max(1, Math.round(this.widthSymbol * CIRCLE_STROKE_WIDTH))
);
}
let bkpSize = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
let ch1 = this.children[1];
ch1.display(context, x + ch1.x, y + ch1.y);
context.fontInfo.setSizeAbsolute(context, bkpSize);
let ch0 = this.children[0];
ch0.display(context, x + ch0.x, y + ch0.y);
let bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
super.drawText(context, "d", x + this.dX, y + this.horzBaseline + Math.round(context.fontInfo.size / 2));
context.fontInfo.setItalic(context, bkpItalic);
let ch2 = this.children[2];
ch2.display(context, x + ch2.x, y + ch2.y);
}
moveAcross(i, direction) {
switch (direction) {
case Expression.NEXT:
if (i === 0) return this.children[2].moveTo(direction);
if (i === 1) return this.children[0].moveTo(direction);
break;
case Expression.PREVIOUS:
if (i === 2) return this.children[0].moveTo(direction);
break;
case Expression.DOWN:
if (i === 0 || i === 2) return this.children[1].moveTo(direction);
break;
case Expression.UP:
if (i === 1) return this.children[0].moveTo(direction);
break;
}
return this.moveOut(direction);
}
moveTo(direction) {
switch (direction) {
case Expression.UP: return this.children[1].moveTo(direction);
case Expression.PREVIOUS: return this.children[2].moveTo(direction);
default: return this.children[0].moveTo(direction);
}
}
};
// ─── Limit: lim, lim inf, lim sup ───────────────────────────────────────────
const WORD_GAP_FRAC = 0.10; // gap below word to subscript, as fraction of fontSize
const EXPR_GAP_FRAC = 0.20; // gap right of sign column to expression, as fraction of fontSize
const LimitBase = class extends Expression {
getWord() { return "lim"; }
canHaveChildren(count) { return count === 3; }
getChildName(index) { return CalculusPackage.messages.childrenLimit[index]; }
getSerializationNames() { return [ "ApproachDirection" ]; }
async getSerializationStrings() { return [ this.approachDirection || "Both" ]; }
setSerializationStrings(strings, promises) {
this.approachDirection = strings[0] || "Both";
}
prepareDisplay(context) {
let dir = this.approachDirection || "Both";
this.approachChar = dir === "Left" ? "⁻" : dir === "Right" ? "⁺" : "";
let ch0 = this.children[0]; // expression
let ch1 = this.children[1]; // variable
let ch2 = this.children[2]; // limit point
ch0.prepareDisplay(context);
let fontSize = context.fontInfo.size;
this.wordWidth = Math.ceil(context.measureText(this.getWord()).width);
let subArrowWidth, subApproachWidth, subBaseline;
{
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
ch1.prepareDisplay(context);
ch2.prepareDisplay(context);
subArrowWidth = Math.ceil(context.measureText("→").width);
subApproachWidth = this.approachChar
? Math.ceil(context.measureText(this.approachChar).width) : 0;
subBaseline = Math.max(ch1.horzBaseline, ch2.horzBaseline);
context.fontInfo.setSizeAbsolute(context, bkp);
}
this.subArrowWidth = subArrowWidth;
this.subBaseline = subBaseline;
let subWidth = ch1.width + subArrowWidth + ch2.width + subApproachWidth;
// Sign column: wide enough to hold both the word and the subscript
this.signColumnWidth = Math.max(this.wordWidth, subWidth);
this.wordX = Math.floor((this.signColumnWidth - this.wordWidth) / 2);
let subLeft = Math.floor((this.signColumnWidth - subWidth) / 2);
let wordGap = Math.round(fontSize * WORD_GAP_FRAC);
let exprGap = Math.round(fontSize * EXPR_GAP_FRAC);
// Vertical: word baseline at horzBaseline; subscript hangs below
this.horzBaseline = Math.max(Math.floor(fontSize / 2), ch0.horzBaseline);
this.subY = this.horzBaseline + Math.ceil(fontSize / 2) + wordGap;
// Subscript children (baseline-aligned within subscript area)
ch1.x = subLeft;
ch1.y = this.subY + (subBaseline - ch1.horzBaseline);
this.arrowX = subLeft + ch1.width;
ch2.x = this.arrowX + subArrowWidth;
ch2.y = this.subY + (subBaseline - ch2.horzBaseline);
this.approachCharX = ch2.x + ch2.width;
// Expression to the right of the sign column
ch0.x = this.signColumnWidth + exprGap;
ch0.y = this.horzBaseline - ch0.horzBaseline;
let subHeight = subBaseline + Math.max(
ch1.height - ch1.horzBaseline,
ch2.height - ch2.horzBaseline
);
this.width = ch0.x + ch0.width;
this.height = Math.max(
this.subY + subHeight,
this.horzBaseline + (ch0.height - ch0.horzBaseline)
);
this.vertBaseline = Math.floor(this.width / 2);
}
display(context, x, y) {
super.drawText(context, this.getWord(),
x + this.wordX,
y + this.horzBaseline + Math.round(context.fontInfo.size / 2)
);
let bkpSize = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
let subTextY = y + this.subY + this.subBaseline + Math.round(context.fontInfo.size / 2);
let ch1 = this.children[1]; // variable
ch1.display(context, x + ch1.x, y + ch1.y);
super.drawText(context, "→", x + this.arrowX, subTextY);
let ch2 = this.children[2]; // limit point
ch2.display(context, x + ch2.x, y + ch2.y);
if (this.approachChar) {
super.drawText(context, this.approachChar, x + this.approachCharX, subTextY);
}
context.fontInfo.setSizeAbsolute(context, bkpSize);
let ch0 = this.children[0]; // expression
ch0.display(context, x + ch0.x, y + ch0.y);
}
moveTo(direction) {
// PREVIOUS enters from right → expression (rightmost); all others → variable (leftmost)
return (direction === Expression.PREVIOUS)
? this.children[0].moveTo(direction)
: this.children[1].moveTo(direction);
}
moveAcross(i, direction) {
switch (direction) {
case Expression.NEXT:
if (i === 1) return this.children[2].moveTo(direction); // variable → limit point
if (i === 2) return this.children[0].moveTo(direction); // limit point → expression
break;
case Expression.PREVIOUS:
if (i === 0) return this.children[2].moveTo(direction); // expression → limit point
if (i === 2) return this.children[1].moveTo(direction); // limit point → variable
break;
case Expression.DOWN:
if (i === 0) return this.children[1].moveTo(direction); // expression → subscript
break;
case Expression.UP:
if (i === 1 || i === 2) return this.children[0].moveTo(direction); // subscript → expression
break;
}
return this.moveOut(direction);
}
};
const Limit = class extends LimitBase {
getTag() { return "Calculus.Limit.Limit"; }
getName() { return CalculusPackage.messages.nameLimit; }
};
const LimitInferior = class extends LimitBase {
getTag() { return "Calculus.Limit.LimitInferior"; }
getName() { return CalculusPackage.messages.nameLimitInferior; }
getWord() { return "lim inf"; }
};
const LimitSuperior = class extends LimitBase {
getTag() { return "Calculus.Limit.LimitSuperior"; }
getName() { return CalculusPackage.messages.nameLimitSuperior; }
getWord() { return "lim sup"; }
};
// ─── Differential: TotalDerivative, TotalDerivativeWithoutVariables, PartialDerivative ──
const TYPE_TOTAL_LEIBNIZ_QUOTIENT = 1;
const TYPE_TOTAL_LEIBNIZ_OPERATOR = 2;
const TYPE_TOTAL_EULER = 3;
const TYPE_TOTAL_SUBSCRIPT = 4;
const TYPE_TOTAL_NOVAR_LAGRANGE = 1;
const TYPE_TOTAL_NOVAR_NEWTON = 2;
const TYPE_PARTIAL_LEIBNIZ_QUOTIENT = 1;
const TYPE_PARTIAL_LEIBNIZ_OPERATOR = 2;
CalculusPackage.styleTotal = TYPE_TOTAL_LEIBNIZ_OPERATOR;
CalculusPackage.styleTotalWithoutVariables = TYPE_TOTAL_NOVAR_LAGRANGE;
CalculusPackage.stylePartial = TYPE_PARTIAL_LEIBNIZ_OPERATOR;
CalculusPackage.styleEvaluationBar = TYPE_EVAL_BAR;
const LQ_FUNC_GAP_FRAC = 0.10; // gap between d^n (or ∂^n) and the function in the quotient numerator, as fraction of fontSize
const LO_FUNC_GAP_FRAC = 0.20; // gap between the operator fraction and the function to its right, as fraction of fontSize
const LEIBNIZ_DEN_GAP_FRAC = 0.15; // gap between d·var groups in the Leibniz denominator, as fraction of fontSize
function derivativeOrder(expression) {
let total = 0;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
if (
ch.getTag() === "Math.Arithmetic.Exponentiation" &&
ch.children.length === 2 &&
ch.children[1].getTag() === "Math.Number"
) {
let n = parseInt(ch.children[1].get("Value"));
total += (Number.isInteger(n) && n >= 2) ? n : 1;
} else {
total += 1;
}
}
return total;
}
function prepareDisplayLeibnizQuotient(expression, context, total) {
const fontSize = context.fontInfo.size;
const symHorzBaseline = Math.round(fontSize / 2);
const sym = total ? "d" : "∂";
const order = derivativeOrder(expression);
const symWidth = Math.ceil(context.measureText(sym).width);
let ordWidth = 0, ordHorzBaseline = 0;
if (order > 1) {
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
ordWidth = Math.ceil(context.measureText(String(order)).width);
ordHorzBaseline = Math.round(context.fontInfo.size / 2);
context.fontInfo.setSizeAbsolute(context, bkp);
}
let ch0 = expression.children[0];
ch0.prepareDisplay(context);
for (let i = 1; i < expression.children.length; i++) {
expression.children[i].prepareDisplay(context);
}
// Numerator: [sym^order] [ch0], aligned at horzBaseline
// d^n block uses Superscript layout; ordHorzBaseline=0 when order≤1 so formula is uniform
const dnHorzBaseline = ordHorzBaseline + symHorzBaseline;
const numHorzBaseline = Math.max(dnHorzBaseline, ch0.horzBaseline);
const numHeight = numHorzBaseline + Math.max(
fontSize - symHorzBaseline,
ch0.height - ch0.horzBaseline
);
const funcGap = Math.round(fontSize * LQ_FUNC_GAP_FRAC);
const numWidth = symWidth + ordWidth + funcGap + ch0.width;
// Denominator: [sym var₁] [gap] [sym var₂] …
const denGap = Math.round(fontSize * LEIBNIZ_DEN_GAP_FRAC);
let denHorzBaseline = symHorzBaseline;
let denHeightBelow = fontSize - symHorzBaseline;
let denWidth = 0;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
if (i > 1) denWidth += denGap;
denWidth += symWidth + ch.width;
denHorzBaseline = Math.max(denHorzBaseline, ch.horzBaseline);
denHeightBelow = Math.max(denHeightBelow, ch.height - ch.horzBaseline);
}
const denHeight = denHorzBaseline + denHeightBelow;
// Fraction (Division class pattern)
const fracWidth = 3 + Math.max(numWidth, denWidth) + 3;
const fracVertBaseline = Math.round(fracWidth / 2);
const numX = fracVertBaseline - Math.round(numWidth / 2);
const denX = fracVertBaseline - Math.round(denWidth / 2);
const denY = numHeight + 3;
// Child positions (numY = 0)
ch0.x = numX + symWidth + ordWidth + funcGap;
ch0.y = numHorzBaseline - ch0.horzBaseline;
let curX = 0;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
if (i > 1) curX += denGap;
ch.x = denX + curX + symWidth;
ch.y = denY + denHorzBaseline - ch.horzBaseline;
curX += symWidth + ch.width;
}
// Display data
expression.lqSym = sym;
expression.lqOrder = order;
expression.lqNumSymX = numX;
expression.lqNumSymTextY = numHorzBaseline + Math.round(fontSize / 2);
if (order > 1) {
expression.lqNumOrdX = numX + symWidth;
expression.lqNumOrdTextY = (numHorzBaseline - symHorzBaseline) + ordHorzBaseline;
}
expression.lqDenSymTextY = denY + denHorzBaseline + Math.round(fontSize / 2);
expression.lqDenSymXs = [];
curX = 0;
for (let i = 1; i < expression.children.length; i++) {
if (i > 1) curX += denGap;
expression.lqDenSymXs.push(denX + curX);
curX += symWidth + expression.children[i].width;
}
expression.width = fracWidth;
expression.height = numHeight + 3 + denHeight;
expression.horzBaseline = numHeight + 2;
expression.vertBaseline = fracVertBaseline;
}
function displayLeibnizQuotient(expression, context, x, y, total) {
// Fraction bar
context.beginPath();
context.moveTo(x, y - 0.5 + expression.horzBaseline);
context.lineTo(x + expression.width, y - 0.5 + expression.horzBaseline);
context.stroke();
// Numerator sym (italic d or ∂)
let bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
expression.drawText(context, expression.lqSym,
x + expression.lqNumSymX,
y + expression.lqNumSymTextY
);
context.fontInfo.setItalic(context, bkpItalic);
// Order superscript
if (expression.lqOrder > 1) {
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
expression.drawText(context, String(expression.lqOrder),
x + expression.lqNumOrdX,
y + expression.lqNumOrdTextY
);
context.fontInfo.setSizeAbsolute(context, bkp);
}
// Function child
let ch0 = expression.children[0];
ch0.display(context, x + ch0.x, y + ch0.y);
// Denominator: italic sym then variable child, for each variable
for (let i = 1; i < expression.children.length; i++) {
bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
expression.drawText(context, expression.lqSym,
x + expression.lqDenSymXs[i - 1],
y + expression.lqDenSymTextY
);
context.fontInfo.setItalic(context, bkpItalic);
let ch = expression.children[i];
ch.display(context, x + ch.x, y + ch.y);
}
}
function prepareDisplayLeibnizOperator(expression, context, total) {
const fontSize = context.fontInfo.size;
const symHorzBaseline = Math.round(fontSize / 2);
const sym = total ? "d" : "∂";
const order = derivativeOrder(expression);
const symWidth = Math.ceil(context.measureText(sym).width);
let ordWidth = 0, ordHorzBaseline = 0;
if (order > 1) {
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
ordWidth = Math.ceil(context.measureText(String(order)).width);
ordHorzBaseline = Math.round(context.fontInfo.size / 2);
context.fontInfo.setSizeAbsolute(context, bkp);
}
let ch0 = expression.children[0];
ch0.prepareDisplay(context);
for (let i = 1; i < expression.children.length; i++) {
expression.children[i].prepareDisplay(context);
}
// Numerator: d^n only (no function)
const dnHorzBaseline = ordHorzBaseline + symHorzBaseline;
const numHorzBaseline = dnHorzBaseline;
const numHeight = numHorzBaseline + (fontSize - symHorzBaseline);
const numWidth = symWidth + ordWidth;
// Denominator: [sym var₁] [gap] [sym var₂] …
const denGap = Math.round(fontSize * LEIBNIZ_DEN_GAP_FRAC);
let denHorzBaseline = symHorzBaseline;
let denHeightBelow = fontSize - symHorzBaseline;
let denWidth = 0;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
if (i > 1) denWidth += denGap;
denWidth += symWidth + ch.width;
denHorzBaseline = Math.max(denHorzBaseline, ch.horzBaseline);
denHeightBelow = Math.max(denHeightBelow, ch.height - ch.horzBaseline);
}
const denHeight = denHorzBaseline + denHeightBelow;
// Fraction (Division class pattern)
const fracWidth = 3 + Math.max(numWidth, denWidth) + 3;
const fracVertBaseline = Math.round(fracWidth / 2);
const numX = fracVertBaseline - Math.round(numWidth / 2);
const denX = fracVertBaseline - Math.round(denWidth / 2);
const denY = numHeight + 3;
const fracHorzBaseline = numHeight + 2;
const fracHeight = numHeight + 3 + denHeight;
// Function to the right of the fraction
const operatorGap = Math.round(fontSize * LO_FUNC_GAP_FRAC);
ch0.x = fracWidth + operatorGap;
ch0.y = fracHorzBaseline - ch0.horzBaseline;
// Variable children in denominator
let curX = 0;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
if (i > 1) curX += denGap;
ch.x = denX + curX + symWidth;
ch.y = denY + denHorzBaseline - ch.horzBaseline;
curX += symWidth + ch.width;
}
// Display data
expression.loSym = sym;
expression.loOrder = order;
expression.loFracWidth = fracWidth;
expression.loNumSymX = numX;
expression.loNumSymTextY = numHorzBaseline + Math.round(fontSize / 2);
if (order > 1) {
expression.loNumOrdX = numX + symWidth;
expression.loNumOrdTextY = (numHorzBaseline - symHorzBaseline) + ordHorzBaseline;
}
expression.loDenSymTextY = denY + denHorzBaseline + Math.round(fontSize / 2);
expression.loDenSymXs = [];
curX = 0;
for (let i = 1; i < expression.children.length; i++) {
if (i > 1) curX += denGap;
expression.loDenSymXs.push(denX + curX);
curX += symWidth + expression.children[i].width;
}
expression.width = fracWidth + operatorGap + ch0.width;
expression.horzBaseline = fracHorzBaseline;
expression.height = Math.max(fracHeight, fracHorzBaseline + (ch0.height - ch0.horzBaseline));
expression.vertBaseline = Math.round(expression.width / 2);
}
function displayLeibnizOperator(expression, context, x, y, total) {
// Fraction bar — spans only the fraction, not the full expression
context.beginPath();
context.moveTo(x, y - 0.5 + expression.horzBaseline);
context.lineTo(x + expression.loFracWidth, y - 0.5 + expression.horzBaseline);
context.stroke();
// Numerator sym (italic d or ∂)
let bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
expression.drawText(context, expression.loSym,
x + expression.loNumSymX,
y + expression.loNumSymTextY
);
context.fontInfo.setItalic(context, bkpItalic);
// Order superscript
if (expression.loOrder > 1) {
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
expression.drawText(context, String(expression.loOrder),
x + expression.loNumOrdX,
y + expression.loNumOrdTextY
);
context.fontInfo.setSizeAbsolute(context, bkp);
}
// Denominator: italic sym + variable child, for each variable
for (let i = 1; i < expression.children.length; i++) {
bkpItalic = context.fontInfo.italic;
context.fontInfo.setItalic(context, true);
expression.drawText(context, expression.loSym,
x + expression.loDenSymXs[i - 1],
y + expression.loDenSymTextY
);
context.fontInfo.setItalic(context, bkpItalic);
let ch = expression.children[i];
ch.display(context, x + ch.x, y + ch.y);
}
// Function to the right of the fraction
let ch0 = expression.children[0];
ch0.display(context, x + ch0.x, y + ch0.y);
}
function prepareDisplayEuler(expression, context) {
const fontSize = context.fontInfo.size;
const dHorzBaseline = Math.round(fontSize / 2);
const dWidth = Math.ceil(context.measureText("D").width);
const order = derivativeOrder(expression);
let ordWidth = 0, ordHorzBaseline_sm = 0, smallFontSize = 0;
if (order > 1) {
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
smallFontSize = context.fontInfo.size;
ordWidth = Math.ceil(context.measureText(String(order)).width);
ordHorzBaseline_sm = Math.round(smallFontSize / 2);
context.fontInfo.setSizeAbsolute(context, bkp);
}
let ch0 = expression.children[0];
ch0.prepareDisplay(context);
let subWidth = 0, subHorzBaseline = 0, subHeightBelow = 0;
{
let bkp = context.fontInfo.size;
context.fontInfo.setSizeRelative(context, LIMIT_SIZE);
for (let i = 1; i < expression.children.length; i++) {
expression.children[i].prepareDisplay(context);
}
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
subWidth += ch.width;
subHorzBaseline = Math.max(subHorzBaseline, ch.horzBaseline);
subHeightBelow = Math.max(subHeightBelow, ch.height - ch.horzBaseline);
}
context.fontInfo.setSizeAbsolute(context, bkp);
}
// ordHorzBaseline_sm = 0 when order ≤ 1, so this formula is uniform
const signHorzBaseline = ordHorzBaseline_sm + dHorzBaseline;
const exprGap = Math.round(fontSize * EXPR_GAP_FRAC);
expression.horzBaseline = Math.max(signHorzBaseline, ch0.horzBaseline);
const signOffset = expression.horzBaseline - signHorzBaseline;
// Subscript y: standard Subscript-class formula (sub center at D's bottom)
const subY = expression.horzBaseline - dHorzBaseline + fontSize - subHorzBaseline;
// child positions
const signColumnWidth = dWidth + Math.max(ordWidth, subWidth);
ch0.x = signColumnWidth + exprGap;
ch0.y = expression.horzBaseline - ch0.horzBaseline;
let curSubX = dWidth;
for (let i = 1; i < expression.children.length; i++) {
let ch = expression.children[i];
ch.x = curSubX;
ch.y = subY + (subHorzBaseline - ch.horzBaseline);
curSubX += ch.width;
}
// display data
expression.euOrder = order;
expression.euDTextY = expression.horzBaseline + Math.round(fontSize / 2);
expression.euOrdX = dWidth;
expression.euOrdTextY = signOffset + ordHorzBaseline_sm + Math.round(smallFontSize / 2);
expression.width = ch0.x + ch0.width;
expression.height = Math.max(
expression.horzBaseline - dHorzBaseline + fontSize + subHeightBelow,
expression.horzBaseline + (ch0.height - ch0.horzBaseline)
);