summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/math/math-tweaks.tex
blob: 6a775688d204602e871be1fb27819d861bb20129 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
% language=us runpath=texruns:manuals/math macros=mkvi

% \enabletrackers[math.tweaks]

% \mapfontsize[pagella][script]      [.30] % a test
% \mapfontsize[pagella][scriptscript][.45] % a test

\hyphenation{ere-whon}

\enableexperiments[fonts.compact] % gives different sized outline !

% \definefontfeature[stixtwo:mathextra][kern=yes,script=dflt,language=dflt]

\startluacode
local list = {
     { "cambria",    "cambria-math"    },
     { "modern",     "modern-math"     },
     { "pagella",    "pagella-math"    },
     { "termes",     "termes-math"     },
     { "schola",     "schola-math"     },
     { "bonum",      "bonum-math"      },
     { "dejavu",     "dejavu-math"     },
     { "erewhon",    "erewhon-math"    },
     { "concrete",   "concrete-math"   },
     { "kpfonts",    "kpfonts-math"    },
     { "lucida",     "lucida-math"     },
     { "stixtwo",    "stixtwo-math"    },
     { "ebgaramond", "ebgaramond-math" },
     { "libertinus", "libertinus-math" },
     { "xcharter",   "xcharter-math"   },
}

local loaded = { }
local used   = table.setmetatableindex("table")

local function identify(fontname,filename)
    local g = fonts.goodies.load(filename)
    if g then
        local tweaks = g.mathematics.tweaks
        if tweaks then
            local after = tweaks.aftercopying
            if after then
                local sequence = { }
                local usage    = { }
                local bigslots = false
                for i=1,#after do
                    local tweak = after[i].tweak
                    if tweak then
                        sequence[#sequence+1] = tweak
                        usage[tweak] = true
                        used[tweak][fontname] = true
                        if tweak == "bigslots" then
                            bigslots = bigslots
                        end
                    end
                end
                local t = {
                    sequence = sequence,
                    usage    = usage,
                    goodies  = g.mathematics,
                    bigslots = bigslots,
                }
                loaded[fontname] = t
            end
        else
            print("no tweaks",filename)
        end
    else
        print("no goodie file",filename)
    end
end

for i=1,#list do
    local l = list[i]
    identify(l[1],l[2])
    context.usebodyfont { l[1] }
    context.usebodyfont { l[1] .. "-nt" }
end

document.usedfonts   = list -- order
document.usedgoodies = loaded
document.usedtweaks  = used

function document.gettweakusage(name)
    local list = table.sortedkeys(document.usedtweaks[name])
    if #list > 0 then
        context("This tweak is used in: %, t.",list)
    else
        context("This tweak is used \\emphasized{nowhere}.")
    end
end

function document.showsomething(name)
    for i=1,#document.usedfonts do
        context[name](document.usedfonts[i][1])
    end
end

-- inspect(loaded)

\stopluacode

\def\ShowTweakUsage#1{\ctxlua{document.gettweakusage("#1")}}
\def\ShowSomething #1{\ctxlua{document.showsomething("#1")}}

\setupbodyfont[bonum]

\usemodule[abbreviations-logos]

\setuplayout
  [height=middle,
   width=middle,
   footer=0pt]

\setupwhitespace
  [big]

\setuphead
  [chapter]
  [header=high]

\starttext

% A riddle. Maybe use the path and the path randomizer.

\startMPpage
    fill Page enlarged 1mm withcolor darkred ;

    picture p ; p := lmt_outline [ text = "\textdollar" ] ;
    picture q ; q := lmt_outline [ text = "\texteuro" ] ;

    p := (p shifted - center p) randomized .7 ;
    q := (q shifted - center q) randomized .7 ;

    draw image ( draw p ; draw q ; )
        xysized (PaperWidth -4cm, PaperHeight -10cm)
        shifted center Page
        shifted (0,4cm)
        withcolor yellow
        withpen pencircle scaled 5mm ;

    draw textext ("tweaking")
        xsized (PaperWidth -4cm)
        shifted center bottomboundary Page
        shifted (0,5.5cm)
        withcolor white ;

    draw textext ("math fonts")
        xsized (PaperWidth -4cm)
        shifted center bottomboundary Page
        shifted (0,2.25cm)
        withcolor white ;

    setbounds currentpicture to Page ;
\stopMPpage

\starttitle[title=Contents]
    \startcolumns
        \placelist[chapter][before=,after=,alternative=a]
    \stopcolumns
\stoptitle

\startchapter[title=What is there to tweak]

We have written about our struggle to \OPENTYPE\ render math fonts well before,
but here we will do it from the perspective of \LUAMETATEX\ and \CONTEXT\ \LMTX\
combined with the fonts available in 2022. So what is the state of these fonts?
The answer to this question is that now the landscape has settled we have a
curious mix of old and new style fonts that has to be dealt with. Our answer to
this is an updated math engine combined with (in \CONTEXT) tweaking font
properties that we can (or will not) deal with in the engine: think of adapting
dimensions, adding missing shapes, variants and extensibles, normalizing anchors
and kerns. Before we discuss these in details let us look at what fonts we
have.

In 2022 we can distinguish several groups of math fonts. The first contains only
one font: Cambria by \MICROSOFT. This is the reference font that more or less
defines the standard and indicates how an \OPENTYPE\ math font is to be
constructed. It has been stable for a long time and we don't see any development
in it. In the meantime the specification has been improved but there is still
room for interpretation.

The second group consists if Latin Modern, Pagella, Termes, Bonum, Schola and
Dejavu. These are the outcome of the \TEX Gyre project funded by user groups and
conducted by the GUST foundation (Jerzy, Jacko and friends). In this project some
choices were made that are reflected in todays versions. It is a mix between old
school \TEX\ fonts (that were the starting point) and interpretation of the
\quote {standard} that at that time to some extend had to be deduced from
observing Cambria usage in MSWord.

The third group started with the release of the \TYPEONE\ \STIX\ fonts that had
been announced several times as the reference \UNICODE\ math coverage font. In
order to check its promises those fonts were quickly converted into a useable
\XITS\ \OPENTYPE\ math font by Khaled Hosny. It took a while before an official
\STIX\ (two) showed up. In principle that font can replace \XITS\ now because
\XITS\ is no longer maintained (but it remain useful as it is a good benchmark
for \STIX). We also inherit some bidirectional elements of \XITS\ that we can
inject into other fonts.

A fourth group contains Erewhon, Kepler, XCharter, Concrete and Euler. These are
existing fonts repackaged and fine-tuned by Daniel Flippo. At the time of writing
this they are a bit in flux and getting better by the day.

Then there are the commercial fonts. Most noticeable is Lucida by Biggelow and
Holmes which is font expanded, maintained and distributed by \TUG\ derived from
the original \TYPEONE\ fonts. When we were upgrading the math engine we were
happy to be awarded the opportunity to improve the metrics (and some minor)
details in order to bring down the quite extensive number of runtime tweaks
needed. As a commercial font it stands out because there is an update policy, so
we can trust fixes to be available to users. The complete \OPENTYPE\ Lucida font
family is also dirt cheap compared to fonts with such a broad coverage. Another
commercial math font is Minion Math by Johannes Küstler but we have limited
possibilities to tweak that because one needs the font as well as the text
minions. It doesn't make sense to spend much time on fonts that one can't use in
(sample) documents and one cannot expect us to invest in fonts we can't use
anyway, right? So where Lucida is extensively supported for other commercial
fonts we just apply default tweaks to those fonts which is probably good enough.

Group six contains the left-overs: Garamond and Libertinus. We also expect a Plex
Math companion. Originally Asana was in this group but eventually we decided that
it is unusable in practice, so we dropped support for that weird and visually
inconsistent mix of fonts. We have to see how Computer Modern Book will fit in
but as we can use Latin Modern with effects instead there is no hurry with that
one. The mentioned Garamond (that is meant to be combined with EBGaramond) is a
bit outlier in the sense that it has excessive amounts of variants and some
shapes that might look okay in text but not so in math, but thereby it makes a
good test case for tweaks.

There are some other setups that we take into account, like Euler over Pagella and
Pagella over Euler but these just use the tweaks that apply to either of them.

\stopchapter

\startchapter[title=Math fonts in \CONTEXT]

In \MKII\ math fonts are set up in the traditional way and because all available
fonts \TYPEONE\ are modelled after Computer Modern quite some definitions were
needed to populate the math families: the basic foursome: roman, italic, symbol,
extensible, as well as additional families that deal with additional alphabets.

In \MKIV\ it became simpler: we went \UNICODE. Traditional fonts can be assembled
into a virtual \UNICODE\ font at runtime. Loading now came down to just one font,
or two when bold math is required. Because \XITS\ introduced right-to-left math
we actually can kick in a a few more instances but it doesn't change the
principles.

In \LMTX\ we integrated bidirectional math in such a way that we now only need
one instance and the new \LUAMETATEX\ engine also provides an alternative to the
triple family model where text, script and scriptscript share the same font
instance which further limits loading time, which in turn means that we can waste
some runtime on tweaking.

So indeed in \MKIV\ and \LMTX\ we can tweak fonts runtime: we patch them before
passing them to the engine. What was still experimental in \MKIV\ has matured in
\LMTX, also because now considerable time was spent on trying out and configuring
tweaks. This made sense because by now we can consider the math fonts to be sort
of frozen: we have to accept that and have to rely on these patches. That we
divert a bit from the \quote {official} \OPENTYPE\ math approach doesn't bother
us much because after all, \TEX\ is still sort of setting the quality standard.

We start with showing some usage tables. When defining a typeface one or more
goodie files can be specified. This is what we set up by default:

\startluacode

context.starttabulate { "|l|l|" }
    context.NC() context("bodyfont")
    context.NC() context("goodie file")
    context.NC() context.NR()
    for i=1,#document.usedfonts do
        local name   = document.usedfonts[i][1]
        local goodie = document.usedfonts[i][2]
        context.NC() context(name)
        context.NC() context(goodie)
        context.NC() context.NR()
    end
context.stoptabulate()
\stopluacode

Tweaks are applied in a specific order and sometimes a tweak is applied several
times with different character lists and|/|or directives:

\startluacode
context.starttabulate { "|l|lp|" }
    context.NC() context("bodyfont")
    context.NC() context("tweak order")
    context.NC() context.NR()
    for i=1,#document.usedfonts do
        local name   = document.usedfonts[i][1]
        local goodie = document.usedfonts[i][2]
        local sequence = document.usedgoodies[name].sequence
        context.NC() context(name)
        context.NC() context("% t",sequence)
        context.NC() context.NR()
    end
context.stoptabulate()
\stopluacode

Keep in mind that some of these steps are driven by optional features or specific
versions of the font, so there are likely less applied than seen here. We can
condense this list to usage only:

\startluacode
context.starttabulate { "|l|lp|" }
    context.NC() context("bodyfont")
    context.NC() context("tweak usage")
    context.NC() context.NR()
    for i=1,#document.usedfonts do
        local name   = document.usedfonts[i][1]
        local goodie = document.usedfonts[i][2]
        local usage  = table.sortedkeys(document.usedgoodies[name].usage)
        context.NC() context(name)
        context.NC() context("% t",usage)
        context.NC() context.NR()
    end
context.stoptabulate()
\stopluacode

Finally we show what tweaks are actually applied. We have some more but these are
either experimental, diagnostic or in the meantime sort of obsolete.

\startluacode
context.starttabulate { "|l|lp|" }
    context.NC() context("tweak")
    context.NC() context("usage")
    context.NC() context.NR()
    for tweak, detail in table.sortedhash(document.usedtweaks) do
        context.NC() context(tweak)
        context.NC() context("% t",table.sortedkeys(detail))
        context.NC() context.NR()
    end
context.stoptabulate()
-- inspect(table.sortedkeys(mathematics.tweaks))
\stopluacode

In the next chapters we will discuss the possible tweaks where roughly group them. Tweaks
that are no longer used will be left out (even if they are still in \LMTX). One reason
for orphaning tweaks is that we (finally) decided to get rid of all italics.

(This is work in progress: we don't want to waste time on tweaks that became unused.)

\stopchapter

\startchapter[title=Math constants]

A math goodie file looks like this"

\starttyping
return {
    name        = "pagella-math",
    version     = "1.00",
    comment     = "Goodies that complement pagella.",
    author      = "Hans Hagen & Mikael Sundqvist",
    copyright   = "ConTeXt development team",
    mathematics = {
        parameters = {
            AccentTopShiftUp          =   10,
            FlattenedAccentTopShiftUp =   10,
            AccentBaseDepth           =   80,
            DelimiterPercent          =   90,
            DelimiterShortfall        =  400,
            DisplayOperatorMinHeight  = 1800, -- 1500 in font
            PrimeRaisePercent         =   75, --   50 default
            PrimeRaiseComposedPercent =   10, --   25 default
        },
        tweaks = {
            -- Here we have a list of tweaks. Just have a look at the file!
        },
    }
}
\stoptyping

In following chapters we discuss the  entries in the tweaks subtable, here we spend some
words on the parameters. In \OPENTYPE\ speak these are called constants, which is also the
term we use when passing a font from \LUA\ to the engine.

\definedescription[MathFP][alternative=serried,width=fit,headstyle=\bf\tt,align={flushleft}]

These are the mandate font parameters with their description straight from the specification:

\startMathFP {ScriptPercentScaleDown}                    Percentage of scaling down for level 1 superscripts and subscripts. Suggested value: 80 pct. \stopMathFP
\startMathFP {ScriptScriptPercentScaleDown}              Percentage of scaling down for level 2 (scriptscript) superscripts and subscripts. Suggested value: 60 pct. \stopMathFP
\startMathFP {DelimitedSubFormulaMinHeight}              Minimum height required for a delimited expression (contained within parentheses, etc.) to be treated as a sub-formula. Suggested value: normal line height times 1.5. \stopMathFP
\startMathFP {DisplayOperatorMinHeight}                  Minimum height of n-ary operators (such as integral and summation) for formulas in display mode (that is, appearing as standalone page elements, not embedded inline within text). \stopMathFP
\startMathFP {MathLeading}                               White space to be left between math formulas to ensure proper line spacing. For example, for applications that treat line gap as a part of line ascender, formulas with ink going above (\typ {os2.sTypoAscender} + \typ {os2.sTypoLineGap} - \typ {MathLeading}) or with ink going below \typ {os2.sTypoDescender} will result in increasing line height. \stopMathFP
\startMathFP {AxisHeight}                                Axis height of the font. In math typesetting, the term axis refers to a horizontal reference line used for positioning elements in a formula. The math axis is similar to but distinct from the baseline for regular text layout. For example, in a simple equation, a minus symbol or fraction rule would be on the axis, but a string for a variable name would be set on a baseline that is offset from the axis. The axisHeight value determines the amount of that offset. \stopMathFP
\startMathFP {AccentBaseHeight}                          Maximum (ink) height of accent base that does not require raising the accents. Suggested: x‑height of the font (\typ {os2.sxHeight}) plus any possible overshots. \stopMathFP
\startMathFP {FlattenedAccentBaseHeight}                 Maximum (ink) height of accent base that does not require flattening the accents. Suggested: cap height of the font (\typ {os2.sCapHeight}). \stopMathFP
\startMathFP {SubscriptShiftDown}                        The standard shift down applied to subscript elements. Positive for moving in the downward direction. Suggested: \typ {os2.ySubscriptYOffset}. \stopMathFP
\startMathFP {SubscriptTopMax}                           Maximum allowed height of the (ink) top of subscripts that does not require moving subscripts further down. Suggested: 4/5 \typ {xheigh}t. \stopMathFP
\startMathFP {SubscriptBaselineDropMin}                  Minimum allowed drop of the baseline of subscripts relative to the (ink) bottom of the base. Checked for bases that are treated as a box or extended shape. Positive for subscript baseline dropped below the base bottom. \stopMathFP
\startMathFP {SuperscriptShiftUp}                        Standard shift up applied to superscript elements. Suggested: \typ {os2.ySuperscriptYOffset}. \stopMathFP
\startMathFP {SuperscriptShiftUpCramped}                 Standard shift of superscripts relative to the base, in cramped style. \stopMathFP
\startMathFP {SuperscriptBottomMin}                      Minimum allowed height of the (ink) bottom of superscripts that does not require moving subscripts further up. Suggested: 1/4 times \typ {xheight}. \stopMathFP
\startMathFP {SuperscriptBaselineDropMax}                Maximum allowed drop of the baseline of superscripts relative to the (ink) top of the base. Checked for bases that are treated as a box or extended shape. Positive for superscript baseline below the base top. \stopMathFP
\startMathFP {SubSuperscriptGapMin}                      Minimum gap between the superscript and subscript ink. Suggested: 4 times default rule thickness. \stopMathFP
\startMathFP {SuperscriptBottomMaxWithSubscript}         The maximum level to which the (ink) bottom of superscript can be pushed to increase the gap between superscript and subscript, before subscript starts being moved down. Suggested: 4/5 times \typ {xheigh}t. \stopMathFP
\startMathFP {SpaceAfterScript}                          Extra white space to be added after each subscript and superscript. Suggested: 0.5 pt for a 12 pt font. (Note that, in some math layout implementations, a constant value, such as 0.5 pt, may be used for all text sizes. Some implementations may use a constant ratio of text size, such as 1/24 of \typ {emwidth}.) \stopMathFP
\startMathFP {UpperLimitGapMin}                          Minimum gap between the (ink) bottom of the upper limit, and the (ink) top of the base operator. \stopMathFP
\startMathFP {UpperLimitBaselineRiseMin}                 Minimum distance between baseline of upper limit and (ink) top of the base operator. \stopMathFP
\startMathFP {LowerLimitGapMin}                          Minimum gap between (ink) top of the lower limit, and (ink) bottom of the base operator. \stopMathFP
\startMathFP {LowerLimitBaselineDropMin}                 Minimum distance between baseline of the lower limit and (ink) bottom of the base operator. \stopMathFP
\startMathFP {StackTopShiftUp}                           Standard shift up applied to the top element of a stack. \stopMathFP
\startMathFP {StackTopDisplayStyleShiftUp}               Standard shift up applied to the top element of a stack in display style. \stopMathFP
\startMathFP {StackBottomShiftDown}                      Standard shift down applied to the bottom element of a stack. Positive for moving in the downward direction. \stopMathFP
\startMathFP {StackBottomDisplayStyleShiftDown}          Standard shift down applied to the bottom element of a stack in display style. Positive for moving in the downward direction. \stopMathFP
\startMathFP {StackGapMin}                               Minimum gap between (ink) bottom of the top element of a stack, and the (ink) top of the bottom element. Suggested: 3 times default rule thickness. \stopMathFP
\startMathFP {StackDisplayStyleGapMin}                   Minimum gap between (ink) bottom of the top element of a stack, and the (ink) top of the bottom element in display style. Suggested: 7 times default rule thickness. \stopMathFP
\startMathFP {StretchStackTopShiftUp}                    Standard shift up applied to the top element of the stretch stack. \stopMathFP
\startMathFP {StretchStackBottomShiftDown}               Standard shift down applied to the bottom element of the stretch stack. Positive for moving in the downward direction. \stopMathFP
\startMathFP {StretchStackGapAboveMin}                   Minimum gap between the ink of the stretched element, and the (ink) bottom of the element above. Suggested: same value as \typ {UpperLimitGapMin}. \stopMathFP
\startMathFP {StretchStackGapBelowMin}                   Minimum gap between the ink of the stretched element, and the (ink) top of the element below. Suggested: same value as \typ {LowerLimitGapMin}. \stopMathFP
\startMathFP {FractionNumeratorShiftUp}                  Standard shift up applied to the numerator. \stopMathFP
\startMathFP {FractionNumeratorDisplayStyleShiftUp}      Standard shift up applied to the numerator in display style. Suggested: same value as \typ {StackTopDisplayStyleShiftUp}. \stopMathFP
\startMathFP {FractionDenominatorShiftDown}              Standard shift down applied to the denominator. Positive for moving in the downward direction. \stopMathFP
\startMathFP {FractionDenominatorDisplayStyleShiftDown}  Standard shift down applied to the denominator in display style. Positive for moving in the downward direction. Suggested: same value as \typ {StackBottomDisplayStyleShiftDown}. \stopMathFP
\startMathFP {FractionNumeratorGapMin}                   Minimum tolerated gap between the (ink) bottom of the numerator and the ink of the fraction bar. Suggested: default rule thickness. \stopMathFP
\startMathFP {FractionNumDisplayStyleGapMin}             Minimum tolerated gap between the (ink) bottom of the numerator and the ink of the fraction bar in display style. Suggested: 3 times default rule thickness. \stopMathFP
\startMathFP {FractionRuleThickness}                     Thickness of the fraction bar. Suggested: default rule thickness. \stopMathFP
\startMathFP {FractionDenominatorGapMin}                 Minimum tolerated gap between the (ink) top of the denominator and the ink of the fraction bar. Suggested: default rule thickness. \stopMathFP
\startMathFP {FractionDenomDisplayStyleGapMin}           Minimum tolerated gap between the (ink) top of the denominator and the ink of the fraction bar in display style. Suggested: 3 times default rule thickness. \stopMathFP
\startMathFP {SkewedFractionHorizontalGap}               Horizontal distance between the top and bottom elements of a skewed fraction. \stopMathFP
\startMathFP {SkewedFractionVerticalGap}                 Vertical distance between the ink of the top and bottom elements of a skewed fraction. \stopMathFP
\startMathFP {OverbarVerticalGap}                        Distance between the overbar and the (ink) top of he base. Suggested: 3 times default rule thickness. \stopMathFP
\startMathFP {OverbarRuleThickness}                      Thickness of overbar. Suggested: default rule thickness. \stopMathFP
\startMathFP {OverbarExtraAscender}                      Extra white space reserved above the overbar. Suggested: default rule thickness. \stopMathFP
\startMathFP {UnderbarVerticalGap}                       Distance between underbar and (ink) bottom of the base. Suggested: 3 times default rule thickness. \stopMathFP
\startMathFP {UnderbarRuleThickness}                     Thickness of underbar. Suggested: default rule thickness. \stopMathFP
\startMathFP {UnderbarExtraDescender}                    Extra white space reserved below the underbar. Always positive. Suggested: default rule thickness. \stopMathFP
\startMathFP {RadicalVerticalGap}                        Space between the (ink) top of the expression and the bar over it. Suggested: 5/4 times default rule thickness. \stopMathFP
\startMathFP {RadicalDisplayStyleVerticalGap}            Space between the (ink) top of the expression and the bar over it. Suggested: default rule thickness + 1/2 timss \typ {xheight}. \stopMathFP
\startMathFP {RadicalRuleThickness}                      Thickness of the radical rule. This is the thickness of the rule in designed or constructed radical signs. Suggested: default rule thickness. \stopMathFP
\startMathFP {RadicalExtraAscender}                      Extra white space reserved above the radical. Suggested: same value as \typ {RadicalRuleThickness}. \stopMathFP
\startMathFP {RadicalKernBeforeDegree}                   Extra horizontal kern before the degree of a radical, if such is present. Suggested: 5/18 of \typ {emwidth}. \stopMathFP
\startMathFP {RadicalKernAfterDegree}                    Negative kern after the degree of a radical, if such is present. Suggested: −10/18 of \typ {emwidth}. \stopMathFP
\startMathFP {RadicalDegreeBottomRaisePercent}           Height of the bottom of the radical degree, if such is present, in proportion to the ascender of the radical sign. Suggested: 60 pct. \stopMathFP

All these parameters can be set in the goodie file and will then overload the
ones that are set already in the font. During \LUATEX\ and \LUAMETATEX\ development some
additional parameters have been added:

\startMathFP {MinConnectorOverlap}               to be described \stopMathFP
\startMathFP {SubscriptShiftDownWithSuperscript} to be described \stopMathFP
\startMathFP {FractionDelimiterSize}             to be described \stopMathFP
\startMathFP {FractionDelimiterDisplayStyleSize} to be described \stopMathFP
\startMathFP {NoLimitSubFactor}                  to be described \stopMathFP
\startMathFP {NoLimitSupFactor}                  to be described \stopMathFP
\startMathFP {AccentBaseDepth}                   to be described \stopMathFP
\startMathFP {FlattenedAccentBaseDepth}          to be described \stopMathFP
\startMathFP {SpaceBeforeScript}                 to be described \stopMathFP
\startMathFP {PrimeRaisePercent}                 to be described \stopMathFP
\startMathFP {PrimeShiftUp}                      to be described \stopMathFP
\startMathFP {PrimeShiftUpCramped}               to be described \stopMathFP
\startMathFP {PrimeSpaceAfter}                   to be described \stopMathFP
\startMathFP {PrimeBaselineDropMax}              to be described \stopMathFP
\startMathFP {PrimeWidthPercent}                 to be described \stopMathFP
\startMathFP {SkewedDelimiterTolerance}          to be described \stopMathFP
\startMathFP {AccentTopShiftUp}                  to be described \stopMathFP
\startMathFP {AccentBottomShiftDown}             to be described \stopMathFP
\startMathFP {AccentTopOvershoot}                to be described \stopMathFP
\startMathFP {AccentBottomOvershoot}             to be described \stopMathFP
\startMathFP {AccentSuperscriptDrop}             to be described \stopMathFP
\startMathFP {AccentSuperscriptPercent}          to be described \stopMathFP
\startMathFP {FlattenedAccentTopShiftUp}         to be described \stopMathFP
\startMathFP {FlattenedAccentBottomShiftDown}    to be described \stopMathFP
\startMathFP {DelimiterPercent}                  to be described \stopMathFP
\startMathFP {DelimiterShortfall}                to be described \stopMathFP

Whenever we thought that we should have some control we added a variable to play
with. There are more variables that control the engine and some are set in
\CONTEXT. Often a parameter can be set per style. We don't set them all.

\stopchapter

\startchapter[title=Font sizes]

For quite a while \CONTEXT\ \MKIV\ and \LMTX\ have been set up to use the script
and scriptscript scales from the \OPENTYPE\ fonts. However, the method used
didn't always work as expected with the configured sizes in bodyfont environments
that were based on initial usage of Computer Modern. That in itself is no big
problem but as we defined the smaller sizes of for instance five point to be that
value too, a conflict of namespace surfaced. It's a side effect of race
condition: we want fast loading for which we need a namespace (at every size) but
when the size in the \OPENTYPE\ file is non standard (read: the 10pt, 7pt, 5pt
ratio) we get the wrong scales from the hash. We need to know the scale before we
initialize the font. But as we don't want to consult the font data each time we
check a scale we have now moved the ratios to the typescripts. This also makes it
possible to overload them easier.

The following table shows the values as configured and you will notice that there
are differences. By moving them into the typescript we can also assure that
updates to the font have no side effects. As with most math parameters in the
font, these are not then well defined|/|explored anyway.

\def\MappedScriptSize      {\cldcontext{fonts.hashes.mathparameters[\the\fontid\scriptscriptfont0].ScriptPercentScaleDown}}
\def\MappedScriptScriptSize{\cldcontext{fonts.hashes.mathparameters[\the\fontid\scriptscriptfont0].ScriptScriptPercentScaleDown}}

\starttexdefinition ShowMappedFontSize #1
    \NC #1
    \NC \mappedfontsize{#1}{script}
    \NC \mappedfontsize{#1}{scriptscript}
    \NC \bgroup\switchtobodyfont[#1]\normalexpanded{\egroup\MappedScriptSize}
    \NC \bgroup\switchtobodyfont[#1]\normalexpanded{\egroup\MappedScriptScriptSize}
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|c|]
    \FL
    \NC
    \NS[1][c] user or typescript
    \NS[1][c] font parameter
    \NC \NR
    \NC bodyfont
    \NC script
    \NC scriptscript
    \NC script
    \NC scriptscript
    \NC \NR
    \ML
    \ShowSomething{ShowMappedFontSize}
    \LL
\stoptabulate

A user can do the following:

\starttyping
\mapfontsize[pagella][script]      [.75]
\mapfontsize[pagella][scriptscript][.65]
\stoptyping

before defining a typeface. In that case the definition in the typescript is
ignored.

Below we show the script and scriptscript shapes as available in the font. We
just show overlayed shaped, so this is not a formula with proper spacing.

\definecolor[tred]  [r=1,t=.5,a=1]
\definecolor[tgreen][g=1,t=.5,a=1]
\definecolor[tblue] [b=1,t=.5,a=1]

\starttexdefinition ShowSizes #1
    \startoverlay
        {\tred  \definedfont[MathRoman*math]#1}
        {\tgreen\definedfont[MathRoman*math-script]#1}
        {\tblue \definedfont[MathRoman*math-scriptscript]#1}
    \stopoverlay
\stoptexdefinition

\starttexdefinition ShowMathSizes #1
    \NC #1 \NC \scale [s=4] {\inframed[frame=off]
        {\switchtobodyfont[#1]\showglyphs%
         \dontleavehmode \strut
         \ShowSizes{2}\ShowSizes{+}
         \ShowSizes{𝐴}\ShowSizes{=}
         \ShowSizes{𝑔}\ShowSizes{−}
         \ShowSizes{𝜋}\ShowSizes{)}}}
    \NR \NR
\stoptexdefinition

\starttabulate[|l|l|]
    \ShowSomething{ShowMathSizes}
\stoptabulate

\stopchapter

\startchapter[title=Data structure]

A traditional \TEX\ engine only needs metrics, that is: the shape is not
relevant. The only measure that somewhat reflect the shape is the italic
correction but it actually is not so much a correction and more the anchor of the
subscript. This is also true for an engine that can deal with \OPENTYPE\ math.
However, there the shape is reflected in the staircase kern tables that define
the kerning at the corners.

In \LUAMETATEX\ each character has the following numeric properties (there cna be
more at the \LUA\ end: width, height, depth, italic, expansion, leftprotrusion
and rightprotrusion. An \OPENTYPE\ font only provides the width and boundingbox
so the later defines the height and depth. We have an status field that tells
what we're dealing with and some special treatments is needed and a pointer
(index) to a follow up glyph (the next in size in math.) There are an optional
kerning table and ligature table (pointers to allocated arrays) as well as an
optional pointer to a math specific table. At the \LUA\ end we have more fields
and some play a role in tweaks. For instance, we can redefine dimensions and
shift the shape around. These are \CONTEXT\ specific.

The optional math (sub)table of a character has many fields. Here we only mention
them because some will be referred to in later chapters: smaller, mirror, {\em
flat accent}, top anchor, bottom anchor, {\em four optional corner math kerns
arrays (with their size)}, {\em optional horizontal and vertical extensible
recipes}, four simple corner kerns, four edge margins, a top and bottom accent
overshoot, {\em italics for vertical and horizontal extensibles}, and an inner
location, x offset and y offset for e.g. degree anchoring. The cursive fields
come from \OPENTYPE\ MATH.

This means that a math character has more data in the engine and therefore uses
more memory. However, there are only a few math fonts loaded and not all
characters need the math information. One reason for splitting the data in
\LUAMETATEX\ is that we could save a lot of memory when huge e.g. \CJK\ fonts are
used: there we want to occupy as little memory as possible.

When reading the next chapters keep in mind that quite a bit fo the
implementation of tweaks is \CONTEXT\ specific. The engine should be able to do a
proper job without tweaks in which case one just has to live with the limitations
of and artifacts in fonts.

When making a manual like this a complication is that one has to load many math
fonts and also find a way to show both the tweaked and non-tweaked version. We
only demonstrate the core set of tweaked files so here is an example:

\starttexdefinition TweakTest #1
    \NC #1
    \NC \switchtobodyfont[#1]    $\showglyphs\showfontitalics f^2_2 + x + 1$
    \NC \switchtobodyfont[#1-nt] $\showglyphs\showfontitalics f^2_2 + x + 1$
    \NC \switchtobodyfont[#1]    $\showglyphs\showfontitalics\showmakeup[mathglue] f^2_2 + x + 1$
    \NC \switchtobodyfont[#1-nt] $\showglyphs\showfontitalics\showmakeup[mathglue] f^2_2 + x + 1$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|c|]
    \NC \NC tweaked \NC original \NC tweaked \NC original \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.TweakTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

You will notice that in the tweaked version we have no kerns that compensate for the
width. We will come to that later because these examples are more meant as calibrating
the loading.

\stopchapter

\startchapter[title=Script parameters]

Here we show the parameters that deal with script placement. This chapter was
added after we found that some values in Latin Modern were off. The tables show a
factor that relates the value to the x-height which is for some part of the
recommendation.

\starttabulate[|c|l|c|l|]
\NC       \NC SubscriptBaselineDropMin          \NC .10 \NC harmless, seldom triggered \NC \NR
\NC \star \NC SubscriptShiftDown                \NC .40 \NC by inspection in several files \NC \NR
\NC \star \NC SubscriptShiftDownWithSuperscript \NC .40 \NC as above \NC \NR
\NC       \NC SubscriptTopMax                   \NC .80 \NC Microsoft recommendation \NC \NR
\NC       \NC SuperscriptBaselineDropMax        \NC .10 \NC see Subscript..Min variant \NC \NR
\NC       \NC SuperscriptBottomMaxWithSubscript \NC .80 \NC Microsoft recommendation \NC \NR
\NC       \NC SuperscriptBottomMin              \NC .25 \NC Microsoft recommendation \NC \NR
\NC \star \NC SuperscriptShiftUp                \NC .65 \NC by inspection, a gamble \NC \NR
\NC \star \NC SuperscriptShiftUpCramped         \NC .65 \NC see above, non-\TEX \NC \NR
\stoptabulate

In the end only the ones marked with a star are really important. The other ones
are often small and the drop ones between .5 and 1.5 points with (to us) no clear
logic. We decided to not enable a tweak but explicitly check and set the (four)
values in the goodie file.

\starttexdefinition ShowScriptsValue #1#2#3
    \NC \csstring#2
    \NC \bgroup \switchtobodyfont[#1-nt]\normalexpanded{\egroup\fam0 \cldcontext{"\letterpercent0.3f",\number#2\textstyle/\number\exheight}}
    \NC \bgroup \switchtobodyfont[#1-nt]\normalexpanded{\egroup\fam0 \the#2\textstyle}
    \NC \bgroup \switchtobodyfont   [#1]\normalexpanded{\egroup\fam0 \the#2\textstyle}
    \NC \NR
\stoptexdefinition

\starttexdefinition ShowScripts #1
    \testpage[4]
    \starttabulate[|l|r|r|r|]
        \FL
        \NC \bf #1
        \NC factor
        \NC original
        \NC tweaked
        \NC \NR
        \ML
      % \ShowScriptsValue{#1}{\Umathaxis}           {MathAxis}
        \ShowScriptsValue{#1}{\Umathsubshiftdown}   {SubscriptShiftDown}
        \ShowScriptsValue{#1}{\Umathsubshiftdrop}   {SubscriptBaselineDropMin}
        \ShowScriptsValue{#1}{\Umathsubsupshiftdown}{SubscriptShiftDownWithSuperscript}
      % \ShowScriptsValue{#1}{\Umathsubsupvgap}     {}
        \ShowScriptsValue{#1}{\Umathsubtopmax}      {SubscriptTopMax}
        \ShowScriptsValue{#1}{\Umathsupbottommin}   {SuperscriptBottomMin}
        \ShowScriptsValue{#1}{\Umathsupshiftdrop}   {SuperscriptBaselineDropMax}
        \ShowScriptsValue{#1}{\Umathsupshiftup}     {SuperscriptShiftUp}
        \ShowScriptsValue{#1}{\Umathsupsubbottommax}{SuperscriptBaselineDropMax}
        \LL
    \stoptabulate
\stoptexdefinition

\ShowSomething{ShowScripts}

One of the pitfalls is that in \CONTEXT\ we actually set \type
{\Umathsubsupshiftdown} which the results in a bad parameter being used. This
also makes that we do need to fix the font. footnote {Other macro packages are
less likely to suffer from this as long as they don't set that parameter.} These
are issues that we easily spent days on checking, double checking and exploring
variants before settling on some approach. In this case we compared traditional
\PDFTEX\ output using Computer Modern with \OPENTYPE\ rendering using Latin
Modern which exposed the issue.

\stopchapter

\startchapter[title=primes]

A few words on primes, a pain in the butt symbol. In traditional \TEX\ (fonts) a
prime is a special symbol. Macro packages are set up in such a way that single
quotes become primes. In practice that then boils down to putting the symbol in
the superscript which means that at the \TEX\ level the smaller size is used.

This smaller size is somehow reflected in the \OPENTYPE\ math fonts: the script
and scriptscript sized (ssty variant 1 and 2) are basically meant to be
superscripted. The text size however often looks differently and is positioned
like an accent. Supposedly it is meant to be used without placement, as minute or
second indicator. This is one of the cases where a text, script and scriptscript
variant of the same glyphs looks pretty incompatible.

Combined with up to four primes being combined as well as up to three reverse
primes being provided this a rather messy implementation. In \CONTEXT\ we always
tweaked the dimensions to consistently suit our purpose.

When we experimented with primes as operator, implemented like we do with fourier
using right delimited radicals, we realized that hooking this into the text level
symbol gave inconsistent results so in the end we simplified the \type
{fixprimes} tweak in a way that suits both usage patterns: we just use the \type
{ssty}~1 variant (of course scaled to the right relative size) for all three
sizes: there is no need to be more granular and it safes us some trouble. We
thereby also sacrifice some detailed tweaking but also got rid of potential
visual incompatibilities as for instance in \STIX.

\stopchapter

\startchapter[title=Tweak: dimensions] \ShowTweakUsage{dimensions}

Usage of this tweak can best be observed (and experimented) in the goodie files
because there are many parameters. Most are fractions, for instance of the width.
It is important to realize than when you mess with the \type {width} and \type
{xoffset}, you need to set the \type {advance} to (most likely) the old width
when it hasn't yet be set. This tells the backend what the natural glyph
progression is.

\stopchapter

\startchapter[title=Tweak: topanchors]   \ShowTweakUsage{topanchors}

\startbuffer
\im{\hat{f}+\widehat{f}}
\stopbuffer

\starttabulate[|l|c|c|]
    \NC \NC tweaked \NC original \NC \NR
    \NC ebgaramond
    \NC \switchtobodyfont[ebgaramond]    \getbuffer
    \NC \switchtobodyfont[ebgaramond-nt] \getbuffer \NR
\stoptabulate

\stopchapter

\startchapter[title=Tweak: moveitalics]  \ShowTweakUsage{moveitalics}

By now we have written plenty about italic correction in math fonts. To summarize
it: in traditional \TEX\ fonts the width of characters is such that the subscript
sits nicely against the shape, and the italic correction determines where the
superscript ends up or when that is absent where the next item starts. It is also
used for positioning the limits on n-ary operators. In \OPENTYPE\ fonts kerning
happens with staircase kerns and italic correction is supposedly only used after
a run of italic shapes. There positioning the limits is also driven by that
correction.

The dual purpose is confusing. In traditional fonts the italic correction is
actually a kern and that is why we decided to just add it to the width and use
the same value for a negative bottom kern. An exception is the integral where as
mentioned the correction determines the limits.

With the fonts being some mix for old and new we also have to deal with the left
end of the shapes, especially in relation to the positioning of top accents which
can be somewhat erratic.

All this head lead us to a tweak that does several things at once: get rid of
weird left side bearings (so that for instance a math italic \type {f} doesn't
stick out in a way that makes it unusable), correct the advance width with the
italic correction, turn top accent values into top and bottom anchors, add top
and bottom right and left kerns based on all this. Finally we wipe the old italic
and top accent values and end up with a clean glyph that we can deal with properly
in several circumstances.

In the goodie files you can find these lines:

\starttyping
presets.moveitalics { correct = true },
presets.moveitalics { correct = true, letters = true },
\stoptyping

In the file \type {common-math.lfg} you can find how these are defined. The first
one does the alphabets and the second one all the letters. That second one is
sort of redundant but one can now comment the line in order to check for
left-overs. Because we do this for all fonts, we have defined these operations as
presets. so it makes sense to study this file. These lines are in fact function
calls that return a regular tweak table entry.

\stopchapter

\startchapter[title=Tweak: movelimits]   \ShowTweakUsage{movelimits}

This tweak turns the italic correction of integrals into anchors and right kerns,
something that the engine can deal with in a more advanced way than just some
fuzzy italic correction because kerns permeable through the renderer. Again we
have a preset:

\starttyping
presets.moveintegrals { factor = 1.5 }
\stoptyping

This is defined as:

\starttyping
moveintegrals = function(parameters)
    return {
        tweak  = "movelimits",
        factor = parameters.factor or 1,
        list   = mathematics.tweaks.subsets.integrals,
    }
end
\stoptyping

Watch how we pas a factor that does an extra shift on the top and bottom anchors.
For the list we use a predefined list of integrals (and there are plenty in
\UNICODE).

\stopchapter

\startchapter[title=Tweak: wipeanchors]  \ShowTweakUsage{wipeanchors}

This tweak wipes the top anchors (top accents) for the given (ranges) of
characters. There are fonts out there with weird ones and when wiped the centered
positioning that kicks in when no anchor is defined works better. In practice
only a few very sloped characters benefit from anchors. Interesting is that the
math italic f is sensitive for bad dimensions and anchors and as that character
is used a lot the impression can be that a whole font is bad if that one is.

\stopchapter

\startchapter[title=Tweak: wipeitalics]  \ShowTweakUsage{wipeitalics}

This tweak wipes the \quote {italic correction} fields that are left over after
applying \type {moveitalics} and \type {movelimits}. For instance some symbols
can have italic corrections that make no sense but might have resulted from some
automatic font generation workflow.

\stopchapter

\startchapter[title=Tweak: checkspacing] \ShowTweakUsage{checkspacing}

This is a rather harmless but still useful tweak. There are plenty of spacing characters
in \UNICODE\ and although in math mode spacing is handled automatically users might be
tempted to inject their own. Normally that happens with commands but using these direct
characters can be an option.

\starttexdefinition CheckSpacingA #1
    \start
    \switchtobodyfont[#1]
    \normalexpanded{\stop\ttx\withoutpt\emwidth}%
\stoptexdefinition

\starttexdefinition CheckSpacingB #1#2
    \vrule height 1.5\exheight depth 0.5\exheight width 1pt
    \start
    \switchtobodyfont[#1]%
    \char#2\relax
    \stop
    \vrule height 1.5\exheight depth 0.5\exheight width 1pt
\stoptexdefinition

\startluacode
    local list = mathematics.tweaks.datasets.checkspacing
    context.starttabulate { "|l|l|" .. string.rep("c|",#list) }
        context.NC() context.NC()
        for i=1,#list do
            context.NC() context.rotate( { rotation = "90" }, string.formatters["\\ttx %U"](list[i][1]) )
        end
        context.NC() context.NR()
        context.NC() context.NC()
        for i=1,#list do
            context.NC() context("\\ttx %s",list[i][2]) -- space quad char
        end
        context.NC() context.NR()
        for i=1,#document.usedfonts do
            local font = document.usedfonts[i][1]
            context.NC()
            context(font)
            context.NC()
            context.CheckSpacingA(font)
            for i=1,#list do
                context.NC()
                context.CheckSpacingB(font,list[i][1])
            end
            context.NC()
            context.NR()
        end
    context.stoptabulate()
\stopluacode

In this table the \type {s} indicates that the value is a spacer, like a \quote
{non breakable space}, the \type {q} that the value is derived from a quad, like
\quote {four per em space}, and the \type {c} means that we have a character
driven quantity, like a \quote {punctuation space}.

\stopchapter

\startchapter[title=Tweak: setovershoots] \ShowTweakUsage{setovershoots}

This tweak add some tolerance to characters which will make accents on top of
them look a bit better.

\starttyping
{
    tweak = "setovershoots",
    list  = {
        {
            target       = "uppercasescript",
            topovershoot = 0.05,
        },
        {
            target       = "uppercaseboldscript",
            topovershoot = 0.05,
        },
    },
}
\stoptyping

\stopchapter

\startchapter[title=Tweak: simplifykerns] \ShowTweakUsage{simplifykerns}

The native kerning mechanism in \OPENTYPE\ math fonts uses staircase kerns for
positioning scripts at each corner. When we looked into fonts in more detail we
noticed that when the first step was normally okay, the second of third was such
that the shape was actually touched. This made us add this tweak, where the
extremes of staircase kerns (first top and last bottom) are used instead. This is
possible because in \LUAMETATEX\ we have additional kerning features.

One can argue that this degrades the quality but one has to keep in mind that
\TEX\ will move script up and down anyway, so one seldom ends up in the more
dangerous zones. So it's best to just catch it and throw out the few that we
could benefit from.

\stopchapter

\startchapter[title=Tweak: kernpairs]     \ShowTweakUsage{kernpairs}

We use this tweak to optimize kerning between successive characters. Here are
some examples for bonum:

\startlines
{\showglyphs\switchtobodyfont   [bonum]$aj$\quad $fj$\quad $ij$}
{\showglyphs\switchtobodyfont[bonum-nt]$aj$\quad $fj$\quad $ij$}
\stoplines

It might take while before we have set them up; fortunately we can use
categories to set up a lot at the same time.

Some fonts have kerns, for instance \STIX\ but not for math. Borrowing the
default latin ones is no real option because it looks too inconsistent for use in
math. This is why for now we keep it explicit.

% \startlines
% {\showglyphs\switchtobodyfont   [stixtwo]$aj$\quad $fj$\quad $ij$\quad \char65 \char67}
% {\showglyphs\switchtobodyfont[stixtwo-nt]$aj$\quad $fj$\quad $ij$\quad \char65 \char67}
% \stoplines

\stopchapter

\startchapter[title=Tweak: kerns]         \ShowTweakUsage{kerns}

This tweak can be used to set the (simple) corner kerns.

\starttyping
{
    tweak = "kerns",
    list  = {
        [0x002F] = { topleft = -0.2, bottomright = -0.2 },
    }
}
\stoptyping

The numbers are a fraction of the width and valid fields are \typ {topleft}, \typ
{topright}, \typ {bottomleft} and \typ {bottomright}. These kerns are used in pre
and postscripts.

\starttexdefinition KernsTest #1
    \NC #1
    \NC \switchtobodyfont[#1]    $\showglyphs x^2/2$
    \NC \switchtobodyfont[#1-nt] $\showglyphs x^2/2$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|]
    \NC \NC tweaked \NC original \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB { "medium" }
            end
            context.KernsTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: margins]       \ShowTweakUsage{margins}

This tweak is similar to the kerns tweak but adds margins instead. it is a trick to
enforce larger and|/|or smaller accents (like for instance \type {\widetilde}.

\starttyping
{
    tweak = "margins",
    list  = {
        [0x1D7DC] = { left = -.1, right = -.1 }, -- doublestruck 4
    }
}
\stoptyping

The relevant fields are \type {left} and \type {right}, they specify the margins
as fraction of the width, and \type {top} and \type {bottom}, those specify
margins as fraction of the combined height and depth.

Keep in mind that these are not \OPENTYPE\ math properties but specific (and
native) to \LUAMETATEX.

\stopchapter

\startchapter[title=Tweak: staircase]     \ShowTweakUsage{staircase}

{\em This tweak is kind of obsolete and might go away some day.}

\stopchapter

\startchapter[title=Tweak: fixintegrals] \ShowTweakUsage{fixintegrals}

In the Computer Modern font there are only a few sizes for the integral sign
\type {U+222B} and they are very sloped. Nowadays fonts also have an extensible
in which case the shape is upright. In \UNICODE\ there are quite some characters
that can be used to construct extensibles (like braces) and there are also
snippets for constructing integrals: \type {0x2320}, \type {0x23AE} and \type
{0x2321}. The fact that these are there and that fonts then provide them also
makes it easy to provide extensibles: you just stack them:

\starttexdefinition TestIntegralSnippets #1
    \NC #1
    \NC \showglyphs \switchtobodyfont[#1-nt]%
        $ \displaystyle \integral { \quad \vcenter\bgroup
            \darkred
            \offinterlineskip
            \hpack{$\char"2320$}
            \hpack{$\char"23AE$}
            \hpack{$\char"2321$}
          \egroup } $
    \NC \showglyphs \switchtobodyfont[#1-nt]$\char"2320$
    \NC \showglyphs \switchtobodyfont[#1-nt]$\char"23AE$
    \NC \showglyphs \switchtobodyfont[#1-nt]$\char"2321$
    \NC \NR
\stoptexdefinition

\starttabulate[|||c|c|c|]
    \ShowSomething{TestIntegralSnippets}
\stoptabulate

This tweak can use these snippets to provide an extensible in case it is not part
of the integral glyph definition. In these examples we show the untweaked
integral that is choosen when we provide the one made from three snippets. The
red one is the constructed variant. Keep in mind that an extensible works best
when there is some overlap possible.

\stopchapter

\startchapter[title=Tweak: accentdimensions] \ShowTweakUsage{accentdimensions}

Positioning of accents is driven by \typ {AccentBaseHeight} and \typ
{AccentBaseDepth} combined with the height of what goes under an accent as we ll
as the height of the accent. Unfortunately the height of the accents is not
always consistent across the repertoire and within a variant list starting with
the initial character.

\starttexdefinition TestAccentDimension #1#2#3
    \NC \ifnum#3=1 #1\fi
    \NC \tttf U+\ifnum#2<"FFF 0\fi\tohexadecimal#2
    \NC \switchtobodyfont   [#1]\showglyphs$\char#2$
    \NC \switchtobodyfont   [#1]\showglyphs$\dorecurse{3}{\char\mathvariantcode #2 ##1\quad}\unskip$
    \NC \switchtobodyfont[#1-nt]\showglyphs$\char#2$
    \NC \switchtobodyfont[#1-nt]\showglyphs$\dorecurse{3}{\char\mathvariantcode #2 ##1\quad}\unskip$
    \NC \NR
\stoptexdefinition

\startluacode
    function document.TestAccentDimension(list)
        context.starttabulate { "|l|c|c|c|c|c|" }
        context.FL()
        context.NC() context("bodyfont")
        context.NC() context("unicode")
        context.NC() context("tweaked")
        context.NC()
        context.NC() context("original")
        context.NC()
        context.NC() context.NR()
        for i=1,#document.usedfonts do
            context.ML()
            local n = 0
            for k, v in table.sortedhash(mathematics.tweaks.datasets.accentdimensions[list]) do
                n = n + 1
                context.TestAccentDimension(document.usedfonts[i][1],k,n)
            end
        end
        context.LL()
        context.stoptabulate()
    end
\stopluacode

The over and under accents are not always present so other tweaks fill in these gaps.

\ctxlua{document.TestAccentDimension("over")}
\ctxlua{document.TestAccentDimension("under")}

We can specify what accents get processed but here we show the default set. In most cases o
treatment is needed.

\ctxlua{document.TestAccentDimension("accent")}

The specification can be simple, just the tweak name, but you can also specify
a detailed list:

\starttyping
{
    tweak = "accentdimensions",
    list  = { "over", "under" }, -- "accent"
}
\stoptyping

or even:

\starttyping
{
    tweak = "accentdimensions",
    list  = {
        [0x203E] = { factor = "over"  }, -- overbar
        [0x203E] = { factor = "under" }, -- underbar
        [0x23DE] = { factor = "over"  }, -- overbrace
        [0x23DF] = { factor = "under" }, -- underbrace
        [0x23DC] = { factor = "over"  }, -- overparent
        [0x23DD] = { factor = "under" }, -- underparent
        [0x23B4] = { factor = "over"  }, -- overbracket
        [0x23B5] = { factor = "under" }, -- underbracket
    }
}
\stoptyping

Instead of a named factor you can pass a number to be applied to the height or
depth. As with all tweaks, they are normally not applied at the user level so if
you have to you'd better check the source code to see what really happens in this
tweak (this is true for more tweaks).

\stopchapter

\startchapter[title=Tweak: copyaccents]      \ShowTweakUsage{copyaccents}

This tweak deals with a mixup of combining accents and wide accents where
recipes can be on the \quote {wrong} code point.

\startluacode
context.starttabulate { "|T|T|c|c|" }
context.FL()
context.NC() context("base")
context.NC() context("accent")
context.NC() context("scale")
context.NC() context.NR()
context.ML()
for k, v in table.sortedhash(mathematics.tweaks.datasets.copyaccents) do
    context.NC() context("%U",k)
    context.NC() context("%U",v[1])
    context.NC() context(v[2] and "yes" or "no")
    context.NC() context("$\\char %i$",v[1])
    context.NC() context.NR()
end
context.LL()
context.stoptabulate()
\stopluacode

\stopchapter

\startchapter[title=Tweak: fixaccents]       \ShowTweakUsage{fixaccents}

When the width of an accent is zero but it has an top (accent) anchor defined we
can fix the its width, which is what this somewhat obscure tweak does. It belongs
to the repertoire of weird tweaks that we came up with when trying to make
suboptimal fonts work well.

\stopchapter

\startchapter[title=Tweak: extendaccents]    \ShowTweakUsage{extendaccents}

When a wide accent is used the engine will try to find a best match from a
sequence of variants. When there are no more variants it will use an extensible
(recipe) when present. When that is not the case, one can decide to stretch the
last variant to fit, but that only works for a few accents: the scale column in
the table in the chapter about copying accents shows them. This tweak takes a few
optional parameters: \type{all} that is either \type {true} or a number
indicating after which variant stretching starts, and \type {force} that forces
stretch on all variants.

\stopchapter

% \startchapter[title=Tweak: fixanchors]       \ShowTweakUsage{fixanchors}
%   obsolete
% \stopchapter

\startchapter[title=Tweak: flattenaccents]   \ShowTweakUsage{flattenaccents}

An \OPENTYPE\ math font can have a flat variant of an accent that can kick in when we
run out of space. This feature will flatten regular accents. The same list is used as
for copying accents. There are optional parameters to control the flattening:

\starttabulate[||||]
\FL
\NC parameter \NC type     \NC default           \NC \NR
\ML
\NC force     \NC boolean  \NC false             \NC \NR
\NC height    \NC factor   \NC 0.8               \NC \NR
\NC offset    \NC factor   \NC 0.9 or calculated \NC \NR
\NC squeeze   \NC factor   \NC 0.1 or calculated \NC \NR
\LL
\stoptabulate

\stopchapter

\startchapter[title=Tweak: bigslots]         \ShowTweakUsage{bigslots}

This tweak can be applied as tweak or at a higher level in the mathematics goodie
table. When defined as tweak it is under font version control.

\starttexdefinition CheckBigSlotA #1#2
    \start
    \switchtobodyfont[#1]%
    \im{\char\mathvariantcode "7B #2}%
    \stop
    \hss
\stoptexdefinition

\starttexdefinition CheckBigSlotB #1#2
    \start
    \switchtobodyfont[#1]%
    \setbox\scratchbox\hbox{\im{\char\mathvariantcode "7B #2}}%
    \normalexpanded{\stop{\ttxx
        \cldcontext{"\letterpercent.3f",\number\htdp\scratchbox/\number\exheight}%
    }}\quad
\stoptexdefinition

\starttexdefinition CheckBigSlotC #1
    \start
    \switchtobodyfont[#1]%
    \normalexpanded{\stop{\ttxx\withoutpt\exheight}}%
\stoptexdefinition

\startluacode
context.starttabulate { "|l|l|l|l|l|" }
    context.NC() context("font")
 -- context.NC() context("tweak")
 -- context.NC() context("global")
    context.NC() context("sequence")
    context.NC() context("ex-height")
    context.NC() context("rendering")
    context.NC() context("ratio to ex")
    context.NC() context.NR()
    for i=1,#document.usedfonts do
        local font    = document.usedfonts[i][1]
        local goodies = document.usedgoodies[font]
        local llist   = goodies.bigslots
        local glist   = goodies.goodies.bigslots
        local list    = glist or llist
        context.NC() context(font)
     -- context.NC() context("% t",llist)
     -- context.NC() context("% t",glist)
        context.NC() if list then context("\\ttxx % t",list) else context("unset") end
        context.NC()
        if list then
            context.CheckBigSlotC(font)
        end
        context.NC()
        if list then
         -- context.switchtobodyfont { font }
         -- for i=1,#list do
         --     context(function()
         --         context.im(utf.char(mathematics.variantcode(0x7B,list[i]))) context.quad()
         --         context.im(utf.char(mathematics.variantcode(0x5D,list[i]))) context.quad()
         --     end)
         -- end
            context.hss()
            for i=1,#list do
                context.CheckBigSlotA(font,list[i])
            end
        end
        context.NC()
        if list then
            for i=1,#list do
                context.CheckBigSlotB(font,list[i])
            end
        end
        context.NC() context.NR()
    end
context.stoptabulate()
\stopluacode

\stopchapter

\startchapter[title=Tweak: wipevariants]     \ShowTweakUsage{wipevariants}

When a font has too many variants one can wipe them with this tweak. The entries
in the list specify the last variant. When a \type {*} will wipe all variants.

\starttyping % [style=\ttx]
{
    tweak   = "radicaldegreeanchors",
    list = {
        [0x221A] = 5, -- becomes last variant
    }
}
\stoptyping

\stopchapter

\startchapter[title=Tweak: radicaldegreeanchors] \ShowTweakUsage{radicaldegreeanchors}

The position of the radical degree is determined by some font parameters (todo)
but these are shared among the different sizes as well as the extensible. We
therefore provide a tweak that sets real anchors. These are not officially in
\UNICODE\ math but an engine feature. Here is the experimental setup we made for
Lucida. It demonstrates that we can set the base character, variants and the
extensible. The factors that default to one, are applied to the width and height
plus depth in order to get the coordinates.

\starttyping[style=\ttx]
{
    tweak   = "radicaldegreeanchors",
    list = {
        -- the base radical:
     -- [0x221A]                = { hfactor =  .05, vfactor =  .675 },
        -- all variants:
     -- ["0x221A.variants.*"]   = { hfactor =  .05, vfactor =  .6   },
        -- specific variants:
     -- ["0x221A.variants.1"]   = { hfactor =  .05, vfactor =  .65  },
     -- ["0x221A.variants.2"]   = { hfactor =  .1,  vfactor =  .65  },
        ["0x221A.variants.3"]   = { hfactor = 0,    vfactor =  .55  },
        ["0x221A.variants.4"]   = { hfactor = 0,    vfactor =  .50  },
     -- ["0x221A.variants.5"]   = { hfactor =  .05, vfactor =  .525 },
     -- ["0x221A.variants.5"]   = { hfactor =  .1,  vfactor =  .55  },
     -- ["0x221A.variants.6"]   = { hfactor =  .1,  vfactor =  .55  },
        -- extensibles where the setting of bottom wins over top:
     -- ["0x221A.parts.top"]    = { hfactor =  .1,  vfactor = 5.5   },
        ["0x221A.parts.bottom"] = { hfactor = 0,    vfactor =  .85  },
    }
}
\stoptyping

The \type {location} is not set here and defaults to \type {left}; it is
redundant because currently we only have left sided radical degrees (but we do
have right sided radicals).

\stopchapter

\startchapter[title=Tweak: fixradicals]          \ShowTweakUsage{fixradicals}

Because there is are limited number of heights and depths available in original
\TFM\ files and variants by their nature differ in height and depth some glyphs
like radicals have interesting dimensions. At the time of writing this only Latin
Modern has this property.

{\em ff image needed here}

\starttexdefinition TestFixRadical #1
    \dontleavehmode
    \NC {\srule height3ex depth 2ex \relax}#1
    \NC \showglyphs \switchtobodyfont[#1-nt]$\mathaxisbelow \char"221A + x$
    \NC \showglyphs \switchtobodyfont   [#1]$\mathaxisbelow \char"221A + x$
    \NC \showglyphs \switchtobodyfont   [#1]$\mathaxisbelow \char\mathvariantcode "221A 1 + x$
    \NC \showglyphs \switchtobodyfont   [#1]$\mathaxisbelow \sqrt{x}$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|c|]
    \NC bodyfont \NC untweaked \NC tweaked \NC first variant \NC example \NC \NR
    \ShowSomething{TestFixRadical}
\stoptabulate

% after this: \dorecurse{15}{ Nacht Na de Dam 2021 | Wende - Bloed in mijn Bloed }

\stopchapter

\startchapter[title=Tweak: fallbacks]        \ShowTweakUsage{fallbacks}

This tweak is not really a tweak but hooks into the fallbacks that a user
defined. However, when fallbacks are set, they need to be injected at the right
spot in the sequence of tweaks which is why it is defined in the goodie file.

\stopchapter

\startchapter[title=Tweak: replacealphabets] \ShowTweakUsage{replacealphabets}

This tweak is an important one. It is used to add or swap script and calligraphic
alphabets, inject right to left variants and configure replacements based on
features. The following fields can be set:

\starttabulate[||||]
\NC filename \NC string \NC source file \NC \NR
\NC feature  \NC string \NC feature \NC \NR
\NC source   \NC string \NC alphabet name \NC \NR
\NC          \NC table  \NC first source range \NC \NR
\NC target   \NC string \NC alphabet name \NC \NR
\NC          \NC table  \NC first source range \NC \NR
\NC rscale   \NC number \NC relative scale \NC \NR
\stoptabulate

When an effect (like boldening) is used it is also applied to the injected
alphabets. When not given, the target range is the same as the source. An example
of a more extensive specification is:

\starttyping
{
    tweak    = "replacealphabets",
    feature  = "euleroverpagella",
    filename = "euler.otf",
    list     = {
        { source = { first = 0x02100, last = 0x02BFF } },
        { source = { first = 0x1D400, last = 0x1D7FF } },
        { source = { first = 0x1D538, last = 0x1D550 } },
    },
}
\stoptyping

The feature at the outer level is a selector that can be set as follows:

\starttyping
\definefontfeature[lucida:mathextra][euleroverpagella=yes]
\stoptyping

This feature is not to be confused by the file related one that we access in for
instance for instance Lucida:

\starttyping
{
    tweak   = "replacealphabets",
    list    = {
        {
            source   = "uppercasescript",
            target   = "uppercasecalligraphic",
            feature  = "ss04",
        },
        {
            source   = "lowercasescript",
            target   = "lowercasecalligraphic",
            feature  = "ss04",
        },
        {
            source   = "uppercaseboldscript",
            target   = "uppercaseboldcalligraphic",
            feature  = "ss04",
        },
        -- No lowercase bold calligraphic/script in font
    },
},
\stoptyping

The calligraphic alphabets are not official \UNICODE\ but internal \CONTEXT\ ones
that are used to switch between script and calligraphic styles.

\stopchapter

\startchapter[title=Tweak: replace]     \ShowTweakUsage{replace}

You can replace a character in a font by another one:

\starttyping
{
    tweak   = "replace",
    list    = {
        [0x123] = 0x125
    },
},
\stoptyping

\stopchapter

\startchapter[title=Tweak: substitute]       \ShowTweakUsage{substitute}

You can substitute a character in a font by one from a substitution feature:

\starttyping
{
    tweak   = "substitute",
    list    = {
        [0x123] = "ss01"
    },
},
\stoptyping

\stopchapter

\startchapter[title=Tweak: fixprimes] \ShowTweakUsage{fixprimes}

\starttexdefinition PrimeTest #1
    \NC #1
    \NC \showglyphs \switchtobodyfont[#1]    $f'(x)+e^{g'(x)} - a''+ b''' + c''''$
    \NC \showglyphs \switchtobodyfont[#1-nt] $f'(x)+e^{g'(x)} - a''+ b''' + c''''$
    \NC \showglyphs \switchtobodyfont[#1]    $\primed{f}$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|]
    \NC \NC tweaked \NC original \NC primed \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.PrimeTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: fixslashes] \ShowTweakUsage{fixslashes}

This tweak makes sure that the regular slash \type {U+002F} behaves compatible
with the math one \type {U+2044}. Not all fonts provide the correct variants
chain.

% There is currently no need to show datasets.fixslashes as there is
% just one entry.

\stopchapter

\startchapter[title=Tweak: fixellipses] \ShowTweakUsage{fixellipses}

\startbuffer
\im{1 + 2+ \cdots + n} \im{1, 2, \ldots, n}
\stopbuffer

\starttabulate[|l|c|c|]
    \NC \NC tweaked \NC original \NC \NR
    \NC concrete
    \NC \switchtobodyfont[concrete]    \getbuffer
    \NC \switchtobodyfont[concrete-nt] \getbuffer \NR
\stoptabulate

\stopchapter

\startchapter[title=Tweak: wipecues]    \ShowTweakUsage{wipecues}

Cues are hidden directives that should not take space so we set their dimensions
to zero:

\startluacode
    local t = mathematics.tweaks.datasets.wipecues
    local c = characters.data
    context.starttabulate { "|c|c|l|" }
    for i=1,#t do
        local u = t[i]
        context.NC() context("%U",u)
        context.NC() context("$\\char %i$",u)
        context.NC() context(c[u].description)
        context.NC() context.NR()
    end
    context.stoptabulate()
\stopluacode

{\em Maybe some day we add tracers in which case they are visible but stil take
no space.}

\stopchapter

\startchapter[title=Tweak: addactuarian] \ShowTweakUsage{addactuarian}

This adds the annuity symbol \type {U+020E7} but as extensible because in itself
this symbol, if present in the font at all, is rather unusable. It uses the same
mechanism as radicals (roots) but from the other edge.

% {\em todo: realistic example by Mikael}
% From wiki: https://en.wikipedia.org/wiki/Actuarial_notation

{\red MPS: In the \typ{\rannuity{m}} example, I would like it to be without the top bar, but I cannot find that. Also, maybe the bar over $n$ should also be tighter.}


\starttexdefinition ActuarianTest #1
    \NC #1
    \NC \switchtobodyfont[#1]
    $\bar{A}^1_{x:\rannuity{n}}^^{2}__{\rannuity{m}}$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|]
    \NC \NC tweaked \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.ActuarianTest(document.usedfonts[i][1])
        end
    }
\stoptabulate



\stopchapter

\startchapter[title=Tweak: addarrows]    \ShowTweakUsage{addarrows}

Not all fonts have extensible arrows. Traditionally in \TEX\ these were made
from arrow heads and rules. This tweak makes proper extensibles.

\startluacode
    local t = mathematics.tweaks.datasets.addarrows
    local c = characters.data
    context.starttabulate { "|c|c|c|l|" }
    for i=1,#t do
        local u = t[i]
        context.NC() context("%U",u)
        context.NC() context("$\\char %i$",u)
        context.NC() context("$\\Umathaccent 0 0 %i{\\hskip3em}$",u)
        context.NC() context(c[u].description)
        context.NC() context.NR()
    end
    context.stoptabulate()
\stopluacode

\stopchapter

\startchapter[title=Tweak: addbars]      \ShowTweakUsage{addbars}

There is only one parameter to this tweak: \type {advance} which is a fraction
of the width.

\starttexdefinition BarTest #1
    \NC #1
    \NC \showglyphs \switchtobodyfont[#1]    $\char"007C\quad\char"2016\quad\char"2980$
    \NC \showglyphs \switchtobodyfont[#1-nt] $\char"007C\quad\char"2016\quad\char"2980$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|]
    \NC \NC tweaked \NC original \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.BarTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: addequals]    \ShowTweakUsage{addequals}

There is only one parameter to this tweak: \type {advance} which is a fraction
of the width.

\starttexdefinition EqualTest #1
    \NC #1
    \NC \showglyphs \switchtobodyfont[#1]    $\char"003D\quad\char"2A75\quad\char"2A76$
    \NC \showglyphs \switchtobodyfont[#1-nt] $\char"003D\quad\char"2A75\quad\char"2A76$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|]
    \NC \NC tweaked \NC original \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.EqualTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: addfourier]   \ShowTweakUsage{addfourier}

This adds the fourier rendering which uses the radical subsystem but with a right
hand symbol.

% {\em todo: realistic example by Mikael}
% From Folland - Real analysis:

\starttexdefinition FourierTest #1
    \NC #1
    \NC \switchtobodyfont[#1]
    $\partial^\alpha \hat{f} = \fourier{[(-2\pi i x )^{\alpha} f]}$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|]
    \NC \NC tweaked \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB()
            end
            context.FourierTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: addmirrors]   \ShowTweakUsage{addmirrors}

This adds symbols for right|-|to|-|left math by simply mirroring existing ones.
Deep down the engine doesn't really care about in what direction typesetting
happens (wrt spacing and building structures), and it is the backend that does
the real work. However, symbols need to be flipped.

\stopchapter

\startchapter[title=Tweak: addparts]     \ShowTweakUsage{addparts}

Here is an example of a (probably rarely used) low level extensible definition:

\starttyping
{
    tweak = "addparts",
    list  = {
        [0x21F4] = {
            horizontal = true,
            template   = 0x2192,
            sequence   = {
                { glyph = "first", factor = 2 },
                { glyph = 0x2218 },
                { glyph = "first", factor = 2 },
                { glyph = "last" },
            }
        }
    }
},
\stoptyping

In this example \type {first} is equivalent to the first part of the template, and
\type {last} to the last one. Like:

\blank \start \switchtobodyfont[stixtwo] \im {
    \char"21F4 \quad
    \Umathtopaccent 0 0 "21F4 {\mathtexttf{\strut one two three}}
} \stop \blank

\stopchapter

\startchapter[title=Tweak: addprivates]  \ShowTweakUsage{addprivates}

This tweak adds some characters to the repertoire. Currently we have only a few:

\starttexdefinition PrivatesTest #1
    \NC #1
    \NC \switchtobodyfont[#1] $\um  2$
    \NC \switchtobodyfont[#1] $\up  2$
    \NC \switchtobodyfont[#1] $\ump 2$
    \NC \switchtobodyfont[#1] $\upm 2$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|c|]
    \NC \NC \type {\um} \NC \type {\up} \NC \type {\ump} \NC \type {\upm} \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
%             if i > 1 then
%                 context.TB { "small" }
%             end
            context.PrivatesTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: addrules]     \ShowTweakUsage{addrules}

This adds a few (missing) rule based shapes to a font:

\starttexdefinition RulesTest #1
    \NC #1
    \NC \switchtobodyfont[#1] $\Umathaccent top    0 0 "23B4 {\hskip2cm}$
    \NC \switchtobodyfont[#1] $\Umathaccent top    0 0 "203E {\hskip2cm}$
    \NC \switchtobodyfont[#1] $\Umathaccent bottom 0 0 "23B5 {\hskip2cm}$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|]
    \NC \NC \type {0x23B4} \NC \type {0x203E} \NC \type {0x23B5} \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB { "small" }
            end
            context.RulesTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=Tweak: addscripts]   \ShowTweakUsage{addscripts}

This tweak adds high plus and minus shapes (we use them for indicating calculator
input):

\starttexdefinition ScriptsTest #1
    \NC #1
    \NC \switchtobodyfont[#1] $\char"207A x$
    \NC \switchtobodyfont[#1] $\char"207B x$
    \NC \switchtobodyfont[#1] $\char"208A x$
    \NC \switchtobodyfont[#1] $\char"208B x$
    \NC \NR
\stoptexdefinition

\starttabulate[|l|c|c|c|c|]
    \NC \NC \type {0x207A} \NC \type {0x2037B} \NC \type {0x208A} \NC \type {0x208B} \NC \NR
    \ctxlua {
        for i=1,#document.usedfonts do
            if i > 1 then
                context.TB { "small" }
            end
            context.ScriptsTest(document.usedfonts[i][1])
        end
    }
\stoptabulate

\stopchapter

\startchapter[title=What we ran into]

\startsubject[title=Introduction]

In this chapter we will show some of the characters that triggered adding a tweak
feature. It can be that in the meantime something changed (got fixed) but
nevertheless we think that by collecting some example here can help developers of
math fonts to play safe so that the engine can do a good job even without tweaks.

\stopsubject

\startsubject[title=Latin Modern: radical base]

In traditional \TEX\ fonts, for practical reasons (like implementation limits)
the base radical character sits quite a bit below the baseline. The engine will
automatically compensate for this when it is defined as radical but it makes
usage as stand alone character hard. It also doesn't play well with corner kerns
and degree anchoring.

\startlinecorrection
\startcombination
    {\externalfigure[lm-radical-base.png]    [width=.25\textwidth]} {base}
    {\externalfigure[lm-radical-variant1.png][width=.25\textwidth]} {variant 1}
\stopcombination
\stoplinecorrection

\stopsubject

\startsubject[title=Latin Modern: weird anchors]

The \TEX Gyre fonts have top accent anchors defined but they are (at least when
we write this) often positioned on the highest point of the shape, which is
probably a side effect of some automated heuristic. However, for some shapes that
works out bad. This is why we can wipe these anchors, just use the middle of the
width and provide our own when it makes sense.

\startlinecorrection
    \externalfigure[lm-7-anchor.png][width=.25\textwidth]
\stoplinecorrection

\stopsubject

\startsubject[title=StixTwo]

When trying to improve font metrics (aka tweaking) we sometimes take a look at
the shapes in FontForge. So for instance when we noticed that the integral mid
segment in StixTwo was too narrow we took that look. Then we also noticed that
the glyph was positioned at the edge instead of in the middle but that the
hinting was such that it ended up in the middle. Intrigued by this we looked into
it in more detail and noticed that the \OPENTYPE\ and \TRUETYPE\ files show up
quite different. So we fetching the source files from the git repository, and
played with the build script.

The starting point is a \TRUETYPE\ file that seems to provide all kind of tables,
that are then adapted using a dedicated glyph name file (basically many verbose
glyph names are replaced by (in the end sort of redundant) uni names as well as
plenty small glif files (we didn't check if these are different from what is in
the original ttf file). Intermediate files are generated and at some point auto
hinting happens, using different tools for the two formats.

At some point we figured out that the left side bearing in the head table was
somehow interfering and more experiments (we used for instance Lucida Math as
comparison) if became clear that the lsb bit in the head flags was the culprit.
By (un)setting it we could let \FONTFORGE\ show the \OPENTYPE\ and \TRUETYPE\
files in the same way of differently. We could also zero the lsb entries in the
hmtx table, which sort of fixed it independent of the bit. For the record we
show the effect here (this document is also a bit our archive).

\startlinecorrection
\startcombination[nx=3,ny=1]
    {\externalfigure[StixTwo23AE-original.png][width=.25\textwidth]} {original otf}
    {\externalfigure[StixTwo23AE-fixed.png]   [width=.25\textwidth]} {fixed otf}
    {\externalfigure[StixTwo23AE-ttf.png]     [width=.25\textwidth]} {ttf}
\stopcombination
\stoplinecorrection

The original variant:

\starttyping
<CharString name="uni23AE">
125 21 -21 hstem
301 102 vstem
403 hmoveto
650 -102 -650 vlineto
endchar
</CharString>
\stoptyping

The fixed version:

\starttyping
<CharString name="uni23AE">
125 0 650 -629 -21 hstemhm
301 102 hintmask 01100000
403 0 rmoveto
hintmask 10100000
0 650 rlineto
-102 0 rlineto
0 -650 rlineto
endchar
</CharString>
\stoptyping

It was this character that we looked at because we use it in a demo, but actually
many characters had this visual hinting side effect. So, one solution is to wipe
the hints and let FontForge add new ones, but easier is to just use the
\TRUETYPE\ files instead of the \OPENTYPE\ ones, either fixed or with hints
removed,as in:

\starttyping
<CharString name="uni23AE">
  125 403 0 rmoveto
  0 650 rlineto
  -102 0 rlineto
  0 -650 rlineto
  endchar
</CharString>
\stoptyping

This is the original \type {uni23A_E_.glif} file:

\starttyping
<?xml version='1.0' encoding='UTF-8'?>
<glyph name="uni23AE" format="2">
  <advance width="704"/>
  <unicode hex="23AE"/>
  <outline>
    <contour>
      <point x="403" y="0" type="line"/>
      <point x="403" y="650" type="line"/>
      <point x="301" y="650" type="line"/>
      <point x="301" y="0" type="line"/>
    </contour>
  </outline>
</glyph>
\stoptyping

In case one wonders why all this matters: occasionally we use the shape in
\METAPOST\ and there we don't apply hinting, which means that we don't want to
end up with the glyph positioned wrongly in the bounding box with hints moving
them to the right spot. So that is why we now prefer the \TRUETYPE\ variant
because then at least we can compare different programs visually. \footnote {In
the FontForge preview rendering in the overview, hinting is also not applied so
there glyphs are also positioned somewhat weird. In TypeTool3 the glyph doesn't
show up at all when we enable outline fill so it makes an interesting puzzle.}

\stopsubject

\stopchapter

% \startchapter[title=Left-overs]

%  "action",
%  "subsets",
%  "parameters",

%  "message",
%  "showinfo",
%  "setoptions",
%  "oldstylemath",
%  "fixoldschool",
%  "version",

% \stopchapter

\stoptext