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
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
|
/*
See license.txt in the root of this project.
*/
/*tex
Here is the main font API implementation for the original pascal parts. Stuff to watch out for:
\startitemize
\startitem
Knuth had a |null_character| that was used when a character could not be found by the
|fetch()| routine, to signal an error. This has been deleted, but it may mean that the
output of luatex is incompatible with TeX after |fetch()| has detected an error
condition.
\stopitem
\startitem
Knuth also had a |font_glue()| optimization. This has been removed because it was a bit
of dirty programming and it also was problematic |if 0 != null|.
\stopitem
\stopitemize
*/
# include "luametatex.h"
/*tex
Finally the base mode ligaturing and kerning code has also been made more consistent with the
rest: abstraction, more tight equality testing, helpers, merged some experiments, etc. It will
probably evolve a bit more; not that we use basemode frequently in \CONTEXT. Keep in mind that
it is not that hard to mess up the list when using \LUA\ but we do little checking here.
From now on base mode ligaturing and kerning will only be applied when |text_font_control|
have the |text_control_base_ligaturing| and |text_control_base_kerning| bits set.
*/
inline static halfword tex_aux_discretionary_node(halfword target, int location)
{
switch (location) {
case pre_break_code : return disc_pre_break_node(target);
case post_break_code: return disc_post_break_node(target);
case no_break_code : return disc_no_break_node(target);
default : return null;
}
}
inline static int tex_aux_same_font_properties(halfword a, halfword b) // also in kern
{
return node_type(a) == glyph_node && node_type(b) == glyph_node
&& glyph_font(a) == glyph_font(b)
&& glyph_x_scale(a) == glyph_x_scale(b)
&& glyph_y_scale(a) == glyph_y_scale(b)
&& glyph_scale(a) == glyph_scale(b);
}
inline static int tex_aux_apply_base_kerning(halfword n)
{
if (glyph_protected(n)) {
return 0;
} else {
halfword f = glyph_font(n);
if (f >= 0 && f <= lmt_font_state.font_data.ptr && lmt_font_state.fonts[f]) {
return has_font_text_control(f, text_control_base_kerning);
} else {
return 0;
}
}
}
inline static int tex_aux_apply_base_ligaturing(halfword n)
{
if (glyph_protected(n)) {
return 0;
} else {
halfword f = glyph_font(n);
if (f >= 0 && f <= lmt_font_state.font_data.ptr && lmt_font_state.fonts[f]) {
return has_font_text_control(f, text_control_base_ligaturing);
} else {
return 0;
}
}
}
/* */
inline static scaled tex_aux_font_x_scaled(scaled v)
{
return v ? scaledround(0.000001 * (glyph_scale_par ? glyph_scale_par : 1000) * (glyph_x_scale_par ? glyph_x_scale_par : 1000) * v) : 0;
}
inline static scaled tex_aux_font_y_scaled(scaled v)
{
return v ? scaledround(0.000001 * (glyph_scale_par ? glyph_scale_par : 1000) * (glyph_y_scale_par ? glyph_y_scale_par : 1000) * v) : 0;
}
inline static scaled tex_aux_glyph_x_scaled(halfword g, scaled v)
{
return v ? scaledround(0.000001 * (glyph_scale(g) ? glyph_scale(g) : 1000) * (glyph_x_scale(g) ? glyph_x_scale(g) : 1000) * v) : 0;
}
inline static scaled tex_aux_glyph_y_scaled(halfword g, scaled v)
{
return v ? scaledround(0.000001 * (glyph_scale(g) ? glyph_scale(g) : 1000) * (glyph_y_scale(g) ? glyph_y_scale(g) : 1000) * v) : 0;
}
font_state_info lmt_font_state = {
.fonts = NULL,
.adjust_stretch = 0,
.adjust_shrink = 0,
.adjust_step = 0,
.padding = 0,
.font_data = {
.minimum = min_font_size,
.maximum = max_font_size,
.size = memory_data_unset,
.step = stp_font_size,
.allocated = 0,
.itemsize = 1,
.top = 0,
.ptr = 0,
.initial = memory_data_unset,
.offset = 0,
},
};
/*tex
There can be holes in the font id range. And \unknown\ nullfont is special! Contrary
to other places, here we don't reallocate an array of records but one of pointers.
*/
void tex_initialize_fonts(void)
{
texfont **tmp = aux_allocate_clear_array(sizeof(texfont *), lmt_font_state.font_data.minimum, 0);
if (tmp) {
for (int i = 0; i < lmt_font_state.font_data.minimum; i++) {
tmp[i] = NULL;
}
lmt_font_state.fonts = tmp;
lmt_font_state.font_data.allocated += lmt_font_state.font_data.minimum * sizeof(texfont *);
lmt_font_state.font_data.top = lmt_font_state.font_data.minimum;
lmt_font_state.font_data.ptr = -1; /* we need to end up with id zero first */
tex_create_null_font();
} else {
tex_overflow_error("fonts", lmt_font_state.font_data.minimum);
}
}
/*tex If a slot is not used .. so be it. We want sequential numbers. */
int tex_new_font_id(void)
{
if (lmt_font_state.font_data.ptr < lmt_font_state.font_data.top) {
++lmt_font_state.font_data.ptr;
return lmt_font_state.font_data.ptr;
} else if (lmt_font_state.font_data.top < lmt_font_state.font_data.maximum) {
texfont **tmp ;
int top = lmt_font_state.font_data.top + lmt_font_state.font_data.step;
if (top > lmt_font_state.font_data.maximum) {
top = lmt_font_state.font_data.maximum;
}
tmp = aux_reallocate_array(lmt_font_state.fonts, sizeof(texfont *), top, 0);
if (tmp) {
for (int i = lmt_font_state.font_data.top + 1; i < top; i++) {
tmp[i] = NULL;
}
lmt_font_state.fonts = tmp;
lmt_font_state.font_data.allocated += ((size_t) top - lmt_font_state.font_data.top) * sizeof(texfont *);
lmt_font_state.font_data.top = top;
lmt_font_state.font_data.ptr += 1;
return lmt_font_state.font_data.ptr;
}
}
tex_overflow_error("fonts", lmt_font_state.font_data.maximum);
return 0;
}
int tex_get_font_max_id(void)
{
return lmt_font_state.font_data.ptr;
}
void tex_dump_font_data(dumpstream f) {
dump_int(f, lmt_font_state.font_data.ptr);
}
void tex_undump_font_data(dumpstream f) {
int x;
undump_int(f, x);
lmt_font_state.font_data.ptr = 0;
}
void tex_set_charinfo_extensible_recipe(charinfo *ci, extinfo *ext)
{
if (ci->math) {
extinfo *list = ci->math->extensible_recipe;
if (list) {
while (list) {
extinfo *c = list->next;
lmt_memory_free(list);
list = c;
}
}
ci->math->extensible_recipe = ext;
}
}
void tex_set_font_parameters(halfword f, int index)
{
int i = font_parameter_count(f);
if (index > i) {
/*tex If really needed this can be a calloc. */
int size = (index + 2) * (int) sizeof(int);
int *list = lmt_memory_realloc(font_parameter_base(f), (size_t) size);
if (list) {
lmt_font_state.font_data.allocated += (index - i + 1) * (int) sizeof(scaled);
font_parameter_base(f) = list;
font_parameter_count(f) = index;
while (i < index) {
font_parameter(f, ++i) = 0;
}
} else {
tex_overflow_error("font", size);
}
}
}
/*tex Most stuff is zero: */
int tex_new_font(void)
{
int size = sizeof(charinfo);
charinfo *ci = lmt_memory_calloc(1, (size_t) size);
if (ci) {
texfont *tf = NULL;
size = sizeof(texfont);
tf = lmt_memory_calloc(1, (size_t) size);
if (tf) {
sa_tree_item sa_value = { 0 };
int id = tex_new_font_id();
lmt_font_state.font_data.allocated += size;
lmt_font_state.fonts[id] = tf;
set_font_name(id, NULL);
set_font_original(id, NULL);
set_font_left_boundary(id, NULL);
set_font_right_boundary(id, NULL);
set_font_parameter_base(id, NULL);
set_font_math_parameter_base(id, NULL);
/*tex |ec = 0| */
set_font_first_character(id, 1);
set_font_hyphen_char(id, '-');
set_font_skew_char(id, -1);
/*tex allocate eight values including 0 */
tex_set_font_parameters(id, 7);
for (int i = 0; i <= 7; i++) {
tex_set_font_parameter(id, i, 0);
}
/*tex character info zero is reserved for |notdef|. The stack size 1, default item value 0. */
tf->characters = sa_new_tree(1, 4, sa_value);
tf->chardata = ci;
tf->chardata_size = 1;
return id;
}
}
tex_overflow_error("font", size);
return 0;
}
void tex_font_malloc_charinfo(halfword f, int index)
{
int glyph = lmt_font_state.fonts[f]->chardata_size;
int size = (glyph + index) * sizeof(charinfo);
charinfo *data = lmt_memory_realloc(lmt_font_state.fonts[f]->chardata , (size_t) size);
if (data) {
lmt_font_state.font_data.allocated += index * sizeof(charinfo);
lmt_font_state.fonts[f]->chardata = data;
memset(&data[glyph], 0, (size_t) index * sizeof(charinfo));
lmt_font_state.fonts[f]->chardata_size += index;
} else {
tex_overflow_error("font", size);
}
}
void tex_char_malloc_mathinfo(charinfo *ci)
{
int size = sizeof(mathinfo);
mathinfo *mi = lmt_memory_calloc(1, (size_t) size);
if (mi) {
mi->extensible_recipe = NULL;
/* */
mi->top_left_math_kern_array = NULL;
mi->top_right_math_kern_array = NULL;
mi->bottom_right_math_kern_array = NULL;
mi->bottom_left_math_kern_array = NULL;
/* zero annyway: */
mi->top_left_kern = 0;
mi->top_right_kern = 0;
mi->bottom_left_kern = 0;
mi->bottom_right_kern = 0;
/* */
mi->left_margin = 0;
mi->right_margin = 0;
mi->top_margin = 0;
mi->bottom_margin = 0;
/* */
mi->top_overshoot = INT_MIN;
mi->bottom_overshoot = INT_MIN;
if (ci->math) {
/*tex This seldom or probably never happens. */
tex_set_charinfo_extensible_recipe(ci, NULL);
set_charinfo_top_left_math_kern_array(ci, NULL);
set_charinfo_top_right_math_kern_array(ci, NULL);
set_charinfo_bottom_right_math_kern_array(ci, NULL);
set_charinfo_bottom_left_math_kern_array(ci, NULL);
lmt_memory_free(ci->math);
} else {
lmt_font_state.font_data.allocated += size;
}
ci->math = mi;
} else {
tex_overflow_error("font", size);
}
}
inline int aux_find_charinfo_id(halfword f, int c)
{
sa_tree_item item;
sa_get_item_4(lmt_font_state.fonts[f]->characters, c, &item);
return (int) item.int_value;
}
charinfo *tex_get_charinfo(halfword f, int c)
{
if (proper_char_index(f, c)) {
sa_tree_item item;
sa_get_item_4(lmt_font_state.fonts[f]->characters, c, &item);
int glyph = (int) item.int_value;
if (! glyph) {
sa_tree_item sa_value = { 0 };
int tglyph = ++lmt_font_state.fonts[f]->chardata_count;
if (tglyph >= lmt_font_state.fonts[f]->chardata_size) {
tex_font_malloc_charinfo(f, 256);
}
lmt_font_state.fonts[f]->chardata[tglyph].expansion = 1000;
sa_value.int_value = tglyph;
/*tex 1 means global */
sa_set_item_4(lmt_font_state.fonts[f]->characters, c, sa_value, 1);
glyph = tglyph;
}
return &(lmt_font_state.fonts[f]->chardata[glyph]);
} else if (c == left_boundary_char) {
if (! font_has_left_boundary(f)) {
int size = sizeof(charinfo);
charinfo *ci = lmt_memory_calloc(1, (size_t) size);
if (ci) {
lmt_font_state.font_data.allocated += size;
set_font_left_boundary(f, ci);
} else {
tex_overflow_error("font", size);
}
}
return font_left_boundary(f);
} else if (c == right_boundary_char) {
if (! font_has_right_boundary(f)) {
int size = sizeof(charinfo);
charinfo *ci = lmt_memory_calloc(1, (size_t) size);
if (ci) {
lmt_font_state.font_data.allocated += size;
set_font_right_boundary(f, ci);
} else {
tex_overflow_error("font", size);
}
}
return font_right_boundary(f);
} else {
return &(lmt_font_state.fonts[f]->chardata[0]);
}
}
static charinfo *tex_aux_char_info(halfword f, int c)
{
if (f > lmt_font_state.font_data.ptr) {
return NULL;
} else if (proper_char_index(f, c)) {
return &(lmt_font_state.fonts[f]->chardata[(int) aux_find_charinfo_id(f, c)]);
} else if (c == left_boundary_char) {
if (font_left_boundary(f)) {
return font_left_boundary(f);
}
} else if (c == right_boundary_char) {
if (font_right_boundary(f)) {
return font_right_boundary(f);
}
}
return &(lmt_font_state.fonts[f]->chardata[0]);
}
void tex_char_process(halfword f, int c)
{
if (tex_char_has_tag_from_font(f, c, callback_tag)) {
int callback_id = lmt_callback_defined(process_character_callback);
if (callback_id > 0) {
lmt_run_callback(lmt_lua_state.lua_instance, callback_id, "dd->", f, c);
}
tex_char_reset_tag_from_font(f, c, callback_tag);
}
}
int tex_char_exists(halfword f, int c)
{
if (f > lmt_font_state.font_data.ptr) {
return 0;
} else if (proper_char_index(f, c)) {
return (int) aux_find_charinfo_id(f, c);
} else if (c == left_boundary_char) {
if (font_has_left_boundary(f)) {
return 1;
}
} else if (c == right_boundary_char) {
if (font_has_right_boundary(f)) {
return 1;
}
}
return 0;
}
/*
static int check_math_char(halfword f, int c, int size)
{
int callback_id = lmt_callback_defined(get_math_char_callback);
if (callback_id > 0) {
halfword s = c;
lmt_run_callback(lua_state.lua_instance, callback_id, "ddd->d", f, c, size, &s);
if (s && proper_char_index(f, s) && aux_find_charinfo_id(f, s)) {
return s;
}
}
return c;
}
*/
int tex_math_char_exists(halfword f, int c, int size)
{
(void) size;
return (f > 0 && f <= lmt_font_state.font_data.ptr && proper_char_index(f, c));
}
/*tex
There is a bit overhead due to first fetching but we don't need to check again, so that saves
a little.
*/
int tex_get_math_char(halfword f, int c, int size, scaled *scale, int direction)
{
int id = aux_find_charinfo_id(f, c);
texfont *tf = lmt_font_state.fonts[f];
if (id) {
/* */
if (direction) {
charinfo *ci = &tf->chardata[id];
int m = ci->math->mirror;
if (m && proper_char_index(f, m)) {
int mid = aux_find_charinfo_id(f, m);
if (mid) {
id = mid;
c = m;
}
}
}
/* */
if (size && tf->compactmath) {
for (int i=1;i<=size;i++) {
charinfo *ci = &tf->chardata[id];
if (ci->math) {
int s = ci->math->smaller;
if (s && proper_char_index(f, s)) {
id = aux_find_charinfo_id(f, s);
if (id) {
/* todo: trace */
c = s;
} else {
break;
}
} else {
break;
}
} else {
break;
}
}
}
}
if (scale) {
*scale = tex_get_math_font_scale(f, size);
if (! *scale) {
*scale = 1000;
}
}
return c;
}
void tex_append_charinfo_extensible_recipe(charinfo *ci, int glyph, int startconnect, int endconnect, int advance, int extender)
{
if (ci->math) {
int size = sizeof(extinfo);
extinfo *ext = lmt_memory_malloc((size_t) size);
if (ext) {
extinfo *lst = ci->math->extensible_recipe;
ext->next = NULL;
ext->glyph = glyph;
ext->start_overlap = startconnect;
ext->end_overlap = endconnect;
ext->advance = advance;
ext->extender = extender;
if (lst) {
while (lst->next) {
lst = lst->next;
}
lst->next = ext;
} else {
ci->math->extensible_recipe = ext;
}
} else {
tex_overflow_error("font", size);
}
}
}
int tex_get_charinfo_math_kerns(charinfo *ci, int id)
{
/*tex All callers check for |result > 0|. */
if (ci->math) {
switch (id) {
case top_left_kern:
return ci->math->top_left_math_kerns;
case bottom_left_kern:
return ci->math->bottom_left_math_kerns;
case top_right_kern:
return ci->math->top_right_math_kerns;
case bottom_right_kern:
return ci->math->bottom_right_math_kerns;
default:
tex_confusion("weird math kern");
break;
}
}
return 0;
}
void tex_add_charinfo_math_kern(charinfo *ci, int id, scaled ht, scaled krn)
{
if (ci->math) {
int k = 0;
int s = 0;
scaled *a = NULL;
switch (id) {
case top_right_kern:
{
k = ci->math->top_right_math_kerns;
s = 2 * (k + 1) * (int) sizeof(scaled);
a = lmt_memory_realloc(ci->math->top_right_math_kern_array, (size_t) s);
if (a) {
ci->math->top_right_math_kern_array = a;
ci->math->top_right_math_kerns++;
}
break;
}
case bottom_right_kern:
{
k = ci->math->bottom_right_math_kerns;
s = 2 * (k + 1) * (int) sizeof(scaled);
a = lmt_memory_realloc(ci->math->bottom_right_math_kern_array, (size_t) s);
if (a) {
ci->math->bottom_right_math_kern_array = a;
ci->math->bottom_right_math_kerns++;
}
break;
}
case bottom_left_kern:
{
k = ci->math->bottom_left_math_kerns;
s = 2 * (k + 1) * (int) sizeof(scaled);
a = lmt_memory_realloc(ci->math->bottom_left_math_kern_array, (size_t) s);
if (a) {
ci->math->bottom_left_math_kern_array = a;
ci->math->bottom_left_math_kerns++;
}
break;
}
case top_left_kern:
{
k = ci->math->top_left_math_kerns;
s = 2 * (k + 1) * (int) sizeof(scaled);
a = lmt_memory_realloc(ci->math->top_left_math_kern_array, (size_t) s);
if (a) {
ci->math->top_left_math_kern_array = a;
ci->math->top_left_math_kerns++;
}
break;
}
default:
tex_confusion("add math kern");
return;
}
if (a) {
a[2 * k] = ht;
a[(2 * k) + 1] = krn;
} else {
tex_overflow_error("font", s);
}
}
}
/*tex
In \TEX, extensibles were fairly simple things. This function squeezes a \TFM\ extensible into
the vertical extender structures. |advance == 0| is a special case for \TFM\ fonts, because
finding the proper advance width during \TFM\ reading can be tricky.
A small complication arises if |rep| is the only non-zero: it needs to be doubled as a
non-repeatable to avoid mayhem.
\starttyping
void tex_set_charinfo_extensible(charinfo *ci, int top, int bottom, int middle, int extender)
{
if (ci->math) {
extinfo *ext;
tex_set_charinfo_extensible_recipe(ci, NULL);
if (bottom == 0 && top == 0 && middle == 0 && extender != 0) {
ext = tex_new_charinfo_extensible_step(extender, 0, 0, 0, math_extension_normal);
tex_add_charinfo_extensible_step(ci, ext);
ext = tex_new_charinfo_extensible_step(extender, 0, 0, 0, math_extension_repeat);
tex_add_charinfo_extensible_step(ci, ext);
} else {
if (bottom) {
ext = tex_new_charinfo_extensible_step(bottom, 0, 0, 0, math_extension_normal);
tex_add_charinfo_extensible_step(ci, ext);
}
if (extender) {
ext = tex_new_charinfo_extensible_step(extender, 0, 0, 0, math_extension_repeat);
tex_add_charinfo_extensible_step(ci, ext);
}
if (middle) {
ext = tex_new_charinfo_extensible_step(middle, 0, 0, 0, math_extension_normal);
tex_add_charinfo_extensible_step(ci, ext);
if (extender) {
ext = tex_new_charinfo_extensible_step(extender, 0, 0, 0, math_extension_repeat);
tex_add_charinfo_extensible_step(ci, ext);
}
}
if (top) {
ext = tex_new_charinfo_extensible_step(top, 0, 0, 0, math_extension_normal);
tex_add_charinfo_extensible_step(ci, ext);
}
}
}
}
\stoptyping
*/
/*tex why not just preallocate for all math otf parameters */
void tex_set_font_math_parameters(halfword f, int b)
{
int i = font_math_parameter_count(f);
if (i < b) {
size_t size = ((size_t) b + 2) * sizeof(scaled);
scaled *data = lmt_memory_realloc(font_math_parameter_base(f), size);
if (data) {
lmt_font_state.font_data.allocated += (int) (((size_t) b - i + 1) * sizeof(scaled));
font_math_parameter_base(f) = data;
font_math_parameter_count(f) = b;
while (i < b) {
++i; /* in macro, make the next a function */
// set_font_math_parameter(f, i, undefined_math_parameter);
font_math_parameter(f, i) = undefined_math_parameter;
}
} else {
tex_overflow_error("font", (int) size);
}
}
}
void tex_delete_font(int f)
{
if (lmt_font_state.fonts[f]) {
tex_set_font_name(f, NULL);
tex_set_font_original(f, NULL);
set_font_left_boundary(f, NULL);
set_font_right_boundary(f, NULL);
for (int i = font_first_character(f); i <= font_last_character(f); i++) {
if (tex_char_exists(f, i)) {
charinfo *co = tex_aux_char_info(f, i);
set_charinfo_kerns(co, NULL);
set_charinfo_ligatures(co, NULL);
if (co->math) {
tex_set_charinfo_extensible_recipe(co, NULL);
set_charinfo_top_left_math_kern_array(co, NULL);
set_charinfo_top_right_math_kern_array(co, NULL);
set_charinfo_bottom_right_math_kern_array(co, NULL);
set_charinfo_bottom_left_math_kern_array(co, NULL);
set_charinfo_math(co, NULL);
}
}
}
/*tex free |notdef| */
lmt_memory_free(lmt_font_state.fonts[f]->chardata);
sa_destroy_tree(lmt_font_state.fonts[f]->characters);
lmt_memory_free(font_parameter_base(f));
if (font_math_parameter_base(f)) {
lmt_memory_free(font_math_parameter_base(f));
}
lmt_memory_free(lmt_font_state.fonts[f]);
lmt_font_state.fonts[f] = NULL;
if (lmt_font_state.font_data.ptr == f) {
lmt_font_state.font_data.ptr--;
}
}
}
void tex_create_null_font(void)
{
int id = tex_new_font();
tex_set_font_name(id, "nullfont");
tex_set_font_original(id, "nullfont");
}
int tex_is_valid_font(halfword f)
{
return (f >= 0 && f <= lmt_font_state.font_data.ptr && lmt_font_state.fonts[f]);
}
int tex_checked_font(halfword f)
{
return (f >= 0 && f <= lmt_font_state.font_data.ptr && lmt_font_state.fonts[f]) ? f : null_font;
}
halfword tex_get_font_identifier(halfword fontspec)
{
if (fontspec) {
halfword fnt = font_spec_identifier(fontspec);
if ((fnt >= 0 && fnt <= lmt_font_state.font_data.ptr && lmt_font_state.fonts[fnt])) {
return fnt;
}
}
return null_font;
}
/*tex
Here come some subroutines to deal with expanded fonts. Returning 1 means that they are
identical.
*/
ligatureinfo tex_get_ligature(halfword f, int lc, int rc)
{
ligatureinfo t = { 0, 0, 0, 0 };
if (lc != non_boundary_char && rc != non_boundary_char && tex_has_ligature(f, lc)) {
int k = 0;
charinfo *co = tex_aux_char_info(f, lc);
while (1) {
ligatureinfo u = charinfo_ligature(co, k);
if (ligature_end(u)) {
break;
} else if (ligature_char(u) == rc) {
return ligature_disabled(u) ? t : u;
}
k++;
}
}
return t;
}
int tex_raw_get_kern(halfword f, int lc, int rc)
{
if (lc != non_boundary_char && rc != non_boundary_char) {
int k = 0;
charinfo *co = tex_aux_char_info(f, lc);
while (1) {
kerninfo u = charinfo_kern(co, k);
if (kern_end(u)) {
break;
} else if (kern_char(u) == rc) {
return kern_disabled(u) ? 0 : kern_kern(u);
}
k++;
}
}
return 0;
}
int tex_get_kern(halfword f, int lc, int rc)
{
if (lc == non_boundary_char || rc == non_boundary_char || (! tex_has_kern(f, lc))) {
return 0;
} else {
return tex_raw_get_kern(f, lc, rc);
}
}
scaled tex_valid_kern(halfword left, halfword right)
{
if (node_type(left) == glyph_node && node_type(right) == glyph_node) {
halfword fl = glyph_font(left);
halfword fr = glyph_font(right);
halfword cl = glyph_character(left);
halfword cr = glyph_character(right);
if (fl == fr && cl != non_boundary_char && cr != non_boundary_char && tex_has_kern(fl, cl) && ! tex_has_glyph_option(left, glyph_option_no_right_kern) && ! tex_has_glyph_option(right, glyph_option_no_left_kern)) {
return tex_raw_get_kern(fl, cl, cr);
}
}
return 0;
}
/*tex
Experiment:
*/
halfword tex_checked_font_adjust(halfword adjust_spacing, halfword adjust_spacing_step, halfword adjust_spacing_shrink, halfword adjust_spacing_stretch)
{
if (adjust_spacing >= adjust_spacing_full) {
if (adjust_spacing_step > 0) {
lmt_font_state.adjust_step = adjust_spacing_step;
lmt_font_state.adjust_shrink = adjust_spacing_shrink;
lmt_font_state.adjust_stretch = adjust_spacing_stretch;
if (lmt_font_state.adjust_step > 100) {
lmt_font_state.adjust_step = 100;
}
if (lmt_font_state.adjust_shrink < 0) {
lmt_font_state.adjust_shrink = 0;
} else if (lmt_font_state.adjust_shrink > 500) {
lmt_font_state.adjust_shrink = 500;
}
if (lmt_font_state.adjust_stretch < 0) {
lmt_font_state.adjust_stretch = 0;
} else if (lmt_font_state.adjust_stretch > 1000) {
lmt_font_state.adjust_stretch = 1000;
}
return adjust_spacing;
}
} else {
adjust_spacing = adjust_spacing_off;
}
lmt_font_state.adjust_step = 0;
lmt_font_state.adjust_shrink = 0;
lmt_font_state.adjust_stretch = 0;
return adjust_spacing;
}
/*tex This returns the multiple of |font_step(f)| that is nearest to |e|. */
int tex_fix_expand_value(halfword f, int e)
{
int max_expand, neg;
if (e == 0) {
return 0;
} else if (e < 0) {
e = -e;
neg = 1;
max_expand = font_max_shrink(f);
} else {
neg = 0;
max_expand = font_max_stretch(f);
}
if (e > max_expand) {
e = max_expand;
} else {
int step = font_step(f);
if (e % step > 0) {
e = step * tex_round_xn_over_d(e, 1, step);
}
}
return neg ? -e : e;
}
int tex_read_font_info(char *cnom, scaled s)
{
int callback_id = lmt_callback_defined(define_font_callback);
if (callback_id > 0) {
int f = 0;
lmt_run_callback(lmt_lua_state.lua_instance, callback_id, "Sd->d", cnom, s, &f);
if (tex_is_valid_font(f)) {
tex_set_font_original(f, (char *) cnom);
return f;
} else {
return 0;
}
} else {
tex_normal_warning("fonts","no font has been read, you need to enable or fix the callback");
return 0;
}
}
/*tex Abstraction: */
halfword tex_get_font_parameter(halfword f, halfword code) /* todo: math */
{
if (font_parameter_count(f) < code) {
tex_set_font_parameters(f, code);
}
return font_parameter(f, code);
}
void tex_set_font_parameter(halfword f, halfword code, scaled v)
{
if (font_parameter_count(f) < code) {
tex_set_font_parameters(f, code);
}
font_parameter(f, code) = v;
}
scaled tex_get_font_slant (halfword f) { return font_parameter(f, slant_code); }
scaled tex_get_font_space (halfword f) { return font_parameter(f, space_code); }
scaled tex_get_font_space_stretch (halfword f) { return font_parameter(f, space_stretch_code); }
scaled tex_get_font_space_shrink (halfword f) { return font_parameter(f, space_shrink_code); }
scaled tex_get_font_ex_height (halfword f) { return font_parameter(f, ex_height_code); }
scaled tex_get_font_em_width (halfword f) { return font_parameter(f, em_width_code); }
scaled tex_get_font_extra_space (halfword f) { return font_parameter(f, extra_space_code); }
scaled tex_get_scaled_slant (halfword f) { return font_parameter(f, slant_code); }
scaled tex_get_scaled_space (halfword f) { return tex_aux_font_x_scaled(font_parameter(f, space_code)); }
scaled tex_get_scaled_space_stretch (halfword f) { return tex_aux_font_x_scaled(font_parameter(f, space_stretch_code)); }
scaled tex_get_scaled_space_shrink (halfword f) { return tex_aux_font_x_scaled(font_parameter(f, space_shrink_code)); }
scaled tex_get_scaled_ex_height (halfword f) { return tex_aux_font_y_scaled(font_parameter(f, ex_height_code)); }
scaled tex_get_scaled_em_width (halfword f) { return tex_aux_font_x_scaled(font_parameter(f, em_width_code)); }
scaled tex_get_scaled_extra_space (halfword f) { return tex_aux_font_x_scaled(font_parameter(f, extra_space_code)); }
scaled tex_font_x_scaled (scaled v) { return tex_aux_font_x_scaled(v); }
scaled tex_font_y_scaled (scaled v) { return tex_aux_font_y_scaled(v); }
halfword tex_get_scaled_parameter(halfword f, halfword code) /* todo: math */
{
if (font_parameter_count(f) < code) {
tex_set_font_parameters(f, code);
}
switch (code) {
case slant_code:
return font_parameter(f, code);
case ex_height_code:
return tex_aux_font_y_scaled(font_parameter(f, code));
default:
return tex_aux_font_x_scaled(font_parameter(f, code));
}
}
void tex_set_scaled_parameter(halfword f, halfword code, scaled v)
{
if (font_parameter_count(f) < code) {
tex_set_font_parameters(f, code);
}
font_parameter(f, code) = tex_aux_font_x_scaled(v);
}
halfword tex_get_scaled_glue(halfword f)
{
halfword p = tex_new_glue_node(zero_glue, space_skip_glue);
glue_amount(p) = tex_aux_font_x_scaled(font_parameter(f, space_code));
glue_stretch(p) = tex_aux_font_x_scaled(font_parameter(f, space_stretch_code));
glue_shrink(p) = tex_aux_font_x_scaled(font_parameter(f, space_shrink_code));
glue_font(p) = f;
return p;
}
halfword tex_get_scaled_parameter_glue(quarterword p, quarterword s)
{
halfword n = tex_new_glue_node(zero_glue, s);
halfword g = glue_parameter(p);
// if (g) {
// memcpy((void *) (node_memory_state.nodes + n + 2), (void *) (node_memory_state.nodes + g + 2), (glue_spec_size - 2) * (sizeof(memoryword)));
// }
glue_amount(n) = tex_aux_font_x_scaled(glue_amount(g));
glue_stretch(n) = tex_aux_font_x_scaled(glue_stretch(g));
glue_shrink(n) = tex_aux_font_x_scaled(glue_shrink(g));
return n;
}
halfword tex_get_parameter_glue(quarterword p, quarterword s)
{
halfword n = tex_new_glue_node(zero_glue, s);
halfword g = glue_parameter(p);
if (g) {
memcpy((void *) (lmt_node_memory_state.nodes + n + 2), (void *) (lmt_node_memory_state.nodes + g + 2), (glue_spec_size - 2) * (sizeof(memoryword)));
}
return n;
}
/*tex Ligaturing starts here */
static void tex_aux_discretionary_append(halfword target, int location, halfword n)
{
halfword node = tex_aux_discretionary_node(target, location);
if (node_tail(node)) {
tex_couple_nodes(node_tail(node), n);
} else {
node_head(node) = n;
}
node_tail(node) = n;
}
static void tex_aux_discretionary_prepend(halfword target, int location, halfword n)
{
halfword node = tex_aux_discretionary_node(target, location);
if (node_head(node)) {
tex_couple_nodes(n, node_head(node));
} else {
node_tail(node) = n;
}
node_head(node) = n;
}
static void tex_aux_nesting_prepend_list(halfword target, int location, halfword n) /* n is prepended to target */
{
halfword node = tex_aux_discretionary_node(target, location);
halfword copy = tex_copy_node_list(n, null);
halfword tail = tex_tail_of_node_list(copy);
if (node_head(node)) {
tex_couple_nodes(tail, node_head(node));
} else {
node_tail(node) = tail;
}
node_head(node) = copy;
}
int tex_valid_ligature(halfword left, halfword right, int *slot)
{
if (node_type(left) != glyph_node) {
return -1;
} else if (glyph_font(left) != glyph_font(right)) {
return -1;
} else if (tex_has_glyph_option(left, glyph_option_no_right_ligature) || tex_has_glyph_option(right, glyph_option_no_left_ligature)) {
return -1;
} else {
ligatureinfo lig = tex_get_ligature(glyph_font(left), glyph_character(left), glyph_character(right));
if (ligature_is_valid(lig)) {
*slot = ligature_replacement(lig);
return ligature_type(lig);
} else {
return -1;
}
}
}
static int tex_aux_found_ligature(halfword left, halfword right)
{
if (! left || ! right) {
return 0;
} else if (node_type(left) != glyph_node || node_type(right) != glyph_node) {
return 0;
} else if (glyph_font(left) != glyph_font(right)) {
return 0;
} else if (tex_has_glyph_option(left, glyph_option_no_right_ligature) || tex_has_glyph_option(right, glyph_option_no_left_ligature)) {
return 0;
} else {
return ligature_is_valid(tex_get_ligature(glyph_font(left), glyph_character(left), glyph_character(right)));
}
}
/*tex
In principle we only support simple ligatures, i.e.\ |move_after|, |keep_right| and |keep_left|
are zero. At some point we might even drop special ones, including those with boundaries because
the likelyhood of encountering these in the \OPENTYPE\ arena is close to zero.
*/
static int tex_aux_try_ligature(halfword *first, halfword second, halfword *nextone)
{
halfword current = *first;
halfword slot;
halfword type = tex_valid_ligature(current, second, &slot);
if (type >= 0) {
int move_after = (type & 0x0C) >> 2;
int keep_right = (type & 0x01) != 0;
int keep_left = (type & 0x02) != 0;
halfword next = node_next(second);
if (keep_left && keep_right) {
halfword ligature = tex_copy_node(current);
glyph_character(ligature) = slot;
tex_couple_nodes(*first, ligature);
tex_couple_nodes(ligature, second);
if (nextone) {
*nextone = second;
}
} else if (keep_right) {
glyph_character(*first) = slot;
if (nextone) {
*nextone = second;
}
} else if (keep_left) {
glyph_character(second) = slot;
if (nextone) {
*nextone = second;
}
} else {
glyph_character(*first) = slot;
tex_uncouple_node(second);
tex_flush_node(second);
tex_try_couple_nodes(*first, next);
if (nextone) {
*nextone = *first;
}
}
/* untested */
if (nextone) {
while (move_after-- > 0 && *nextone) {
*nextone = node_next(*nextone);
}
}
return 1;
} else {
return 0;
}
}
static void tex_aux_handle_ligature_list(halfword target, int location)
{
halfword node = tex_aux_discretionary_node(target, location);
halfword head = node_head(node);
halfword tail = node_tail(node);
if (head && head != tail) {
halfword current = head;
while (node_next(current)) {
halfword next = node_next(current);
int ishead = current == head;
halfword nextone = next;
if (tex_aux_same_font_properties(current, next) && tex_aux_try_ligature(¤t, next, &nextone)) {
if (ishead) {
head = current;
node_head(node) = current;
}
current = nextone;
} else {
current = next;
}
}
node_tail(node) = current;
}
}
static void tex_aux_handle_ligature_pair(halfword target, int location)
{
halfword node = tex_aux_discretionary_node(target, location);
halfword head = node_head(node);
halfword tail = node_tail(node);
if (head && head != tail) {
halfword previous = node_prev(tail);
int ishead = previous == head;
if (tex_aux_same_font_properties(previous, tail) && tex_aux_try_ligature(&previous, tail, NULL)) {
if (ishead) {
head = previous;
node_head(node) = previous;
}
node_tail(node) = previous;
}
}
}
/*tex
In \LUATEX\ we have a chained variant of discretionaries (init and select) but that never really
works out ok. It was there for basemode to be compatible with original \TEX\ but it was also means
for border cases that in practice never occur. A least no \CONTEXT\ user ever complained about
ligatures and hyphenation of these border cases. Keep in mind that in node mode (which we normally
use) the select discs never showed up anyway. Another reason for dropping these discretionaries is
that by not using them we get more predictable (or at least easier) handling of node lists that do
have (any kind of) discretionaries. It is still on my agenda to look into nested discretionaries
i.e. discs nodes in disc fields but it might never result in useable code.
Todo: check this boundary mess (check for subtype too).
Todo: maybe get rid of weird ligatures, turn boundary into space and such.
*/
static halfword tex_aux_handle_ligature_word(halfword current)
{
halfword right = null;
halfword last = null; /* cf LuaTeX patch, an ancient border case buglet. */
if (node_type(current) == boundary_node) {
halfword previous = node_prev(current);
halfword next = node_next(current);
/*tex There is no need to uncouple |cur|, it is freed. */
tex_flush_node(current);
if (next) {
tex_couple_nodes(previous, next);
if (node_type(next) != glyph_node) {
return previous;
} else {
current = next;
}
} else {
node_next(previous) = next;
return previous;
}
} else if (node_type(current) == glyph_node && font_has_left_boundary(glyph_font(current))) {
halfword previous = node_prev(current);
halfword glyph = tex_new_glyph_node(glyph_unset_subtype, glyph_font(current), left_boundary_char, current);
tex_couple_nodes(previous, glyph);
tex_couple_nodes(glyph, current);
current = glyph;
}
if (node_type(current) == glyph_node && font_has_right_boundary(glyph_font(current))) {
right = tex_new_glyph_node(glyph_unset_subtype, glyph_font(current), right_boundary_char, current);
}
// tex_print_node_list(current, "GOING",max_integer, max_integer);
while (1) {
halfword currenttype = node_type(current);
/*tex A glyph followed by \unknown */
if (currenttype == glyph_node) {
if (tex_aux_apply_base_ligaturing(current)) {
halfword forward = node_next(current);
if (forward) {
halfword forwardtype = node_type(forward);
if (forwardtype == glyph_node) {
if (! tex_aux_apply_base_ligaturing(forward)) {
// break;
} else if (! tex_aux_same_font_properties(current, forward)) {
// break;
} else {
halfword nextone = current;
if (tex_aux_try_ligature(¤t, forward, &nextone)) {
current = nextone;
continue;
}
}
} else if (forwardtype == disc_node) {
/*tex a glyph followed by a disc */
halfword pre = disc_pre_break_head(forward);
halfword replace = disc_no_break_head(forward);
halfword next;
/*tex Check on: |a{b?}{?}{?}| and |a+b=>B| : |{B?}{?}{a?}| */
/*tex Check on: |a{?}{?}{b?}| and |a+b=>B| : |{a?}{?}{B?}| */
if (tex_aux_found_ligature(current, pre) || tex_aux_found_ligature(current, replace)) {
/*tex Move |cur| from before disc to skipped part */
halfword previous = node_prev(current);
tex_uncouple_node(current);
tex_couple_nodes(previous, forward);
tex_aux_discretionary_prepend(forward, no_break_code, current);
tex_aux_discretionary_prepend(forward, pre_break_code, tex_copy_node(current));
/*tex As we have removed cur, we need to start again. */
current = previous;
}
/*tex Check on: |a{?}{?}{}b| and |a+b=>B| : |{a?}{?b}{B}|. */
next = node_next(forward);
if (! replace && tex_aux_found_ligature(current, next)) {
/*tex Move |cur| from before |disc| to |no_break| part. */
halfword previous = node_prev(current);
halfword tail = node_next(next);
tex_uncouple_node(current);
tex_couple_nodes(previous, forward);
tex_aux_discretionary_prepend(forward, pre_break_code, tex_copy_node(current));
/*tex Move next from after disc to |no_break| part. */
tex_uncouple_node(next);
tex_try_couple_nodes(forward, tail);
/*tex We {\em know} this works. */
tex_couple_nodes(current, next);
/*tex Make sure the list is correct. */
tex_aux_discretionary_append(forward, post_break_code, tex_copy_node(next));
/*tex As we have removed cur, we need to start again. */
current = previous;
}
/*tex We are finished with the |pre_break|. */
tex_aux_handle_ligature_list(forward, pre_break_code);
} else if (forwardtype == boundary_node) {
halfword next = node_next(forward);
tex_try_couple_nodes(current, next);
tex_flush_node(forward);
if (right) {
/*tex Shame, didn't need it. */
tex_flush_node(right);
/*tex No need to reset |right|, we're going to leave the loop anyway. */
}
break;
} else if (right) {
tex_couple_nodes(current, right);
tex_couple_nodes(right, forward);
right = null;
continue;
} else {
break;
}
} else {
/*tex The last character of a paragraph. */
if (right) {
/*tex |par| prohibits the use of |couple_nodes| here. */
tex_try_couple_nodes(current, right);
right = null;
continue;
} else {
break;
}
}
/*tex A discretionary followed by \unknown */
}
} else if (currenttype == disc_node) {
/*tex If |{?}{x}{?}| or |{?}{?}{y}| then: */
if (disc_no_break_head(current) || disc_post_break_head(current)) {
/*tex Is this nesting okay (and needed)? */
halfword forward;
if (disc_post_break_head(current)) {
tex_aux_handle_ligature_list(current, post_break_code);
}
if (disc_no_break_head(current)) {
tex_aux_handle_ligature_list(current, no_break_code);
}
forward = node_next(current);
while (forward && node_type(forward) == glyph_node && tex_aux_apply_base_ligaturing(forward)) {
halfword replace = disc_no_break_tail(current);
halfword post = disc_post_break_tail(current);
if (tex_aux_found_ligature(replace, forward) || tex_aux_found_ligature(post, forward)) {
tex_try_couple_nodes(current, node_next(forward));
tex_uncouple_node(forward);
tex_aux_discretionary_append(current, no_break_code, tex_copy_node(forward));
tex_aux_handle_ligature_pair(current, no_break_code);
tex_aux_handle_ligature_pair(current, post_break_code);
forward = node_next(current);
} else {
break;
}
}
if (forward && node_type(forward) == disc_node) {
/*tex This only deals with simple pre-only discretionaries and a following glyph. */
halfword next = node_next(forward);
if (next
&& ! disc_no_break_head(forward)
&& ! disc_post_break_head(forward)
&& node_type(next) == glyph_node
&& tex_aux_apply_base_ligaturing(next)
&& ((disc_post_break_tail(current) && tex_aux_found_ligature(disc_post_break_tail(current), next)) ||
(disc_no_break_tail (current) && tex_aux_found_ligature(disc_no_break_tail (current), next)))) {
halfword last = node_next(next);
tex_uncouple_node(next);
tex_try_couple_nodes(forward, last);
/*tex Just a hidden flag, used for (base mode) experiments. */
if (hyphenation_permitted(hyphenation_mode_par, lazy_ligatures_hyphenation_mode)) {
/*tex f-f-i -> f-fi */
tex_aux_discretionary_append(current, no_break_code, tex_copy_node(next));
tex_aux_handle_ligature_pair(current,no_break_code);
tex_aux_discretionary_append(current, post_break_code, next);
tex_aux_handle_ligature_pair(current,post_break_code);
tex_try_couple_nodes(node_prev(forward), node_next(forward));
tex_flush_node(forward);
} else {
/*tex f-f-i -> ff-i : |{a-}{b}{AB} {-}{c}{}| => |{AB-}{c}{ABc}| */
tex_aux_discretionary_append(forward, post_break_code, tex_copy_node(next));
if (disc_no_break_head(current)) {
tex_aux_nesting_prepend_list(forward, no_break_code, disc_no_break_head(current));
tex_aux_discretionary_append(forward, no_break_code, next);
tex_aux_handle_ligature_pair(forward, no_break_code);
tex_aux_nesting_prepend_list(forward, pre_break_code, disc_no_break_head(current));
}
tex_try_couple_nodes(node_prev(current), node_next(current));
tex_flush_node(current);
current = forward;
}
}
}
}
} else {
/*tex We have glyph nor disc. */
return last;
}
/*tex Goto the next node, where |\par| allows |node_next(cur)| to be NULL. */
last = current;
current = node_next(current);
}
return current;
}
/*tex The return value is the new tail, head should be a dummy: */
halfword tex_handle_ligaturing(halfword head, halfword tail)
{
if (node_next(head)) {
/*tex A trick to allow explicit |node == null| tests. */
halfword save_tail = null;
halfword current, previous;
if (tail) {
save_tail = node_next(tail);
node_next(tail) = null;
}
previous = head;
current = node_next(previous);
while (current) {
switch(node_type(current)) {
case glyph_node:
if (tex_aux_apply_base_ligaturing(current)) {
current = tex_aux_handle_ligature_word(current);
}
break;
case disc_node:
case boundary_node:
current = tex_aux_handle_ligature_word(current);
break;
}
previous = current;
if (current) {
current = node_next(current);
}
}
if (! previous) {
previous = tex_tail_of_node_list(head);
}
tex_try_couple_nodes(previous, save_tail);
return previous;
} else {
return tail;
}
}
/*tex Kerning starts here: */
static halfword tex_aux_add_kern_before(halfword left, halfword right)
{
if (tex_aux_same_font_properties(left, right) &&
! tex_has_glyph_option(left, glyph_option_no_right_kern) &&
! tex_has_glyph_option(right, glyph_option_no_left_kern) &&
tex_has_kern(glyph_font(left), glyph_character(left))
) {
scaled k = tex_raw_get_kern(glyph_font(left), glyph_character(left), glyph_character(right));
if (k) {
scaled kern = tex_new_kern_node(k, font_kern_subtype);
halfword previous = node_prev(right);
tex_couple_nodes(previous, kern);
tex_couple_nodes(kern, right);
tex_attach_attribute_list_copy(kern, left);
return kern;
}
}
return null;
}
static halfword tex_aux_add_kern_after(halfword left, halfword right, halfword after)
{
if (tex_aux_same_font_properties(left, right) &&
! tex_has_glyph_option(left, glyph_option_no_right_kern) &&
! tex_has_glyph_option(right, glyph_option_no_left_kern) &&
tex_has_kern(glyph_font(left), glyph_character(left))
) {
scaled k = tex_raw_get_kern(glyph_font(left), glyph_character(left), glyph_character(right));
if (k) {
scaled kern = tex_new_kern_node(k, font_kern_subtype);
halfword next = node_next(after);
tex_couple_nodes(after, kern);
tex_try_couple_nodes(kern, next);
tex_attach_attribute_list_copy(kern, after);
return kern;
}
}
return null;
}
static halfword tex_aux_do_handle_kerning(halfword root, halfword init_left, halfword init_right);
static void tex_aux_handle_discretionary_kerning(halfword target, int location, halfword left, halfword right)
{
halfword node = tex_aux_discretionary_node(target, location);
if (node_head(node)) {
halfword kern = tex_aux_do_handle_kerning(node_head(node), left, right);
if (kern) {
node_head(node) = kern;
node_tail(node) = tex_tail_of_node_list(node_head(node));
}
}
}
static halfword tex_aux_do_handle_kerning(halfword root, halfword init_left, halfword init_right)
{
// halfword head = node_next(root); // todo: get rid of this one
halfword head = root; // todo: get rid of this one
halfword current = head;
halfword initial = null;
if (current) {
halfword left = null;
if (node_type(current) == glyph_node && tex_aux_apply_base_kerning(current)) {
if (init_left) {
halfword kern = tex_aux_add_kern_before(init_left, current);
if (current == head) {
initial = kern;
}
}
left = current;
}
current = node_next(current);
while (current) {
halfword currenttype = node_type(current);
if (currenttype == glyph_node) {
if (tex_aux_apply_base_kerning(current)) {
if (left) {
tex_aux_add_kern_before(left, current);
if (glyph_character(left) < 0) {
halfword previous = node_prev(left);
tex_couple_nodes(previous, current);
tex_flush_node(left);
}
}
left = current;
} else {
left = null;
}
} else {
if (currenttype == disc_node) {
halfword next = node_next(current);
halfword right = node_type(next) == glyph_node && tex_aux_apply_base_kerning(next) ? next : null;
tex_aux_handle_discretionary_kerning(current, pre_break_code, left, null);
tex_aux_handle_discretionary_kerning(current, post_break_code, null, right);
tex_aux_handle_discretionary_kerning(current, no_break_code, left, right);
}
if (left) {
if (glyph_character(left) < 0) { /* boundary ? */
halfword previous = node_prev(left);
tex_couple_nodes(previous, current);
tex_flush_node(left);
}
left = null;
}
}
current = node_next(current);
}
if (left) {
if (init_right) {
tex_aux_add_kern_after(left, init_right, left);
}
if (glyph_character(left) < 0) {
halfword previous = node_prev(left);
halfword next = node_next(left);
if (next) {
tex_couple_nodes(previous, next);
node_tail(root) = next;
} else if (previous != root) {
node_next(previous) = null;
node_tail(root) = previous;
} else {
node_next(root) = null;
node_tail(root) = null;
}
tex_flush_node(left);
}
}
} else if (init_left && init_right ) {
tex_aux_add_kern_after(init_left, init_right, root);
node_tail(root) = node_next(root);
}
return initial;
}
halfword tex_handle_kerning(halfword head, halfword tail)
{
halfword save_link = null;
if (tail) {
save_link = node_next(tail);
node_next(tail) = null;
node_tail(head) = tail;
tex_aux_do_handle_kerning(node_next(head), null, null); /*tex There is no need to check initial here. */
tail = node_tail(head);
if (tex_valid_node(save_link)) {
/* no need for check */
tex_try_couple_nodes(tail, save_link);
}
} else {
node_tail(head) = null;
tex_aux_do_handle_kerning(node_next(head), null, null); /*tex There is no need to check initial here. */
}
return tail;
}
/*tex The ligaturing and kerning \LUA\ interface: */
static halfword tex_aux_run_lua_ligkern_callback(lua_State *L, halfword head, halfword group, halfword direction, int callback_id)
{
int top = 0;
if (lmt_callback_okay(L, callback_id, &top)) {
int i;
lmt_node_list_to_lua(L, head);
lmt_push_group_code(L, group);
lua_pushinteger(L, direction);
i = lmt_callback_call(L, 3, 1, top);
if (i) {
lmt_callback_error(L, top, i);
} else {
head = lmt_node_list_from_lua(L, -1);
lmt_callback_wrapup(L, top);
}
}
return head;
}
halfword tex_handle_glyphrun(halfword head, halfword group, halfword direction)
{
if (head) {
int callback_id = lmt_callback_defined(glyph_run_callback);
if (callback_id) {
return tex_aux_run_lua_ligkern_callback(lmt_lua_state.lua_instance, head, group, direction, callback_id);
} else {
callback_id = lmt_callback_defined(ligaturing_callback);
if (callback_id) {
head = tex_aux_run_lua_ligkern_callback(lmt_lua_state.lua_instance, head, group, direction, callback_id);
} else {
// what if disc at start
tex_handle_ligaturing(head, null);
}
callback_id = lmt_callback_defined(kerning_callback);
if (callback_id) {
head = tex_aux_run_lua_ligkern_callback(lmt_lua_state.lua_instance, head, group, direction, callback_id);
} else {
halfword kern = tex_aux_do_handle_kerning(head, null, null);
if (kern) {
head = kern;
}
}
}
}
return head;
}
/*tex
When the user defines |\font\f|, say, \TEX\ assigns an internal number to the user's font |\f|.
Adding this number to |font_id_base| gives the |eqtb| location of a \quote {frozen} control
sequence that will always select the
font.
The variable |a| in the following code indicates the global nature of the value to be set. It's
used in the |define| macro. Here we're never global.
There's not much scanner code here because the other scanners are defined where they make most
sense.
*/
void tex_set_cur_font(halfword g, halfword f)
{
update_tex_font(g, f);
}
/*tex
Because we do fonts in \LUA\ we can decide to drop this one and assume a definition using the
token scanner. It also avoids the filename (split) mess.
*/
int tex_tex_def_font(int a)
{
if (! lmt_fileio_state.job_name) {
/*tex Avoid confusing |texput| with the font name. */
tex_open_log_file();
}
tex_get_r_token();
if (tex_define_permitted(cur_cs, a)) {
/*tex The user's font identifier. */
halfword u = cur_cs;
/*tex This runs through existing fonts. */
halfword f;
/*tex Stated 'at' size, or negative of scaled magnification. */
scaled s = -1000;
char *fn;
/*tex Here |a| determines if we define global or not. */
if (is_global(a)) {
update_tex_font_global(u, null_font);
} else {
update_tex_font_local(u, null_font);
}
fn = tex_read_file_name(1, NULL, NULL);
/*tex Scan the font size specification. */
lmt_fileio_state.name_in_progress = 1;
if (tex_scan_keyword("at")) {
/*tex Put the positive 'at' size into |s|. */
s = tex_scan_dimen(0, 0, 0, 0, NULL);
if ((s <= 0) || (s >= 01000000000)) {
tex_handle_error(
normal_error_type,
"Improper 'at' size (%D), replaced by 10pt",
s,
pt_unit,
"I can only handle fonts at positive sizes that are less than 2048pt, so I've\n"
"changed what you said to 10pt."
);
s = 10 * unity;
}
} else if (tex_scan_keyword("scaled")) {
s = tex_scan_int(0, NULL);
if ((s <= 0) || (s > 32768)) {
tex_handle_error(
normal_error_type,
"Illegal magnification has been changed to 1000 (%i)",
s,
"The magnification ratio must be between 1 and 32768."
);
s = -1000;
} else {
s = -s;
}
}
lmt_fileio_state.name_in_progress = 0;
f = tex_read_font_info(fn, s);
eq_value(u) = f;
lmt_memory_free(fn);
return 1;
} else {
return 0;
}
}
/*tex
When \TEX\ wants to typeset a character that doesn't exist, the character node is not created;
thus the output routine can assume that characters exist when it sees them. The following
procedure prints a warning message unless the user has suppressed it.
*/
void tex_char_warning(halfword f, int c)
{
if (tracing_lost_chars_par > 0) {
/*tex saved value of |tracing_online| */
int old_setting = tracing_online_par;
/*tex index to current digit; we assume that $0\L n<16^{22}$ */
if (tracing_lost_chars_par > 1) {
tracing_online_par = 1;
}
tex_begin_diagnostic();
tex_print_format("[font: missing character, character %c (%U), font '%s']", c, c, font_name(f));
tex_end_diagnostic();
tracing_online_par = old_setting;
}
}
/* Getters. */
scaled tex_char_width_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->width;
}
scaled tex_char_height_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->height;
}
scaled tex_char_depth_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->depth;
}
scaled tex_char_total_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->height + ci->depth;
}
scaled tex_char_italic_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->italic;
}
// scaled tex_char_options_from_font(halfword f, halfword c)
// {
// charinfo *ci = tex_aux_char_info(f, c);
// return ci->math ? ci->math->options : 0;
// }
//
// int tex_char_has_option_from_font(halfword f, halfword c, int option)
// {
// charinfo *ci = tex_aux_char_info(f, c);
// return ci->math ? math_font_option(ci->math->options, option) : 0;
// }
scaledwhd tex_char_whd_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return (scaledwhd) {
.wd = ci->width,
.ht = ci->height,
.dp = ci->depth,
.ic = ci->italic
};
}
scaled tex_char_ef_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->expansion;
}
scaled tex_char_cf_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->compression;
}
scaled tex_char_lp_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->leftprotrusion;
}
scaled tex_char_rp_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->rightprotrusion;
}
halfword tex_char_has_tag_from_font(halfword f, halfword c, halfword tag)
{
return (tex_aux_char_info(f, c)->tag & tag) == tag;
}
void tex_char_reset_tag_from_font(halfword f, halfword c, halfword tag)
{
charinfo *ci = tex_aux_char_info(f, c);
ci->tag = ci->tag & ~(tag);
}
halfword tex_char_tag_from_font(halfword f, halfword c)
{
return tex_aux_char_info(f, c)->tag;
}
int tex_char_checked_tag(halfword tag)
{
return tag & (horizontal_tag | vertical_tag | extend_last_tag | italic_tag | n_ary_tag | radical_tag | punctuation_tag);
}
halfword tex_char_next_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->next : -1;
}
halfword tex_char_extensible_italic_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->extensible_italic : INT_MIN;
}
halfword tex_char_unchecked_top_anchor_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->top_anchor : INT_MIN;
}
halfword tex_char_top_anchor_from_font(halfword f, halfword c)
{
scaled n = tex_char_unchecked_top_anchor_from_font(f, c);
return n == INT_MIN ? 0 : n;
}
halfword tex_char_unchecked_bottom_anchor_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->bottom_anchor : INT_MIN;
}
halfword tex_char_bottom_anchor_from_font(halfword f, halfword c)
{
scaled n = tex_char_unchecked_bottom_anchor_from_font(f, c);
return n == INT_MIN ? 0 : n;
}
halfword tex_char_flat_accent_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->flat_accent : INT_MIN;
}
scaled tex_char_top_left_kern_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->top_left_kern : 0;
}
scaled tex_char_top_right_kern_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->top_right_kern : 0;
}
scaled tex_char_bottom_left_kern_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->bottom_left_kern : 0;
}
scaled tex_char_bottom_right_kern_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->bottom_right_kern : 0;
}
extinfo *tex_char_extensible_recipe_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->extensible_recipe : NULL;
}
scaled tex_char_left_margin_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->left_margin : 0;
}
scaled tex_char_right_margin_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->right_margin : 0;
}
scaled tex_char_top_margin_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->top_margin : 0;
}
scaled tex_char_bottom_margin_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->bottom_margin : 0;
}
scaled tex_char_top_overshoot_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->top_overshoot : 0;
}
scaled tex_char_bottom_overshoot_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->bottom_overshoot : 0;
}
scaled tex_char_inner_x_offset_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->inner_x_offset : 0;
}
scaled tex_char_inner_y_offset_from_font(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci->math ? ci->math->inner_y_offset : 0;
}
/* Nodes */
scaled tex_char_width_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return tex_aux_glyph_x_scaled(g, ci->width);
}
scaled tex_char_height_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return tex_aux_glyph_y_scaled(g, ci->height);
}
scaled tex_char_depth_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return tex_aux_glyph_y_scaled(g, ci->depth);
}
scaled tex_char_total_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
scaled ht = ci->height;
scaled dp = ci->depth;
return tex_aux_glyph_y_scaled(g, (ht > 0 ? ht : 0) + (dp > 0 ? dp : 0)); /* so not progression */
}
scaled tex_char_italic_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return tex_aux_glyph_x_scaled(g, ci->italic);
}
// halfword tex_char_options_from_glyph(halfword g)
// {
// charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
// return ci->math ? ci->math->options : 0;
// }
// int tex_char_has_option_from_glyph(halfword g, int t)
// {
// if (node_type(g) == glyph_node) {
// charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
// return ci->math ? math_font_option(ci->math->options, t) : 0;
// } else {
// return 0;
// }
// }
scaledwhd tex_char_whd_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return (scaledwhd) {
.wd = tex_aux_glyph_x_scaled(g, ci->width),
.ht = tex_aux_glyph_y_scaled(g, ci->height),
.dp = tex_aux_glyph_y_scaled(g, ci->depth),
.ic = tex_aux_glyph_x_scaled(g, ci->italic)
};
}
scaled tex_char_width_italic_from_glyph(halfword g)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
return tex_aux_glyph_x_scaled(g, ci->width + ci->italic);
}
/* More */
scaled tex_calculated_char_width(halfword f, halfword c, halfword ex)
{
scaled wd = tex_aux_char_info(f, c)->width;
return ex ? tex_round_xn_over_d(wd, 1000 + ex, 1000) : wd;
}
scaled tex_calculated_glyph_width(halfword g, halfword ex)
{
charinfo *ci = tex_aux_char_info(glyph_font(g), glyph_character(g));
scaled wd = tex_aux_glyph_x_scaled(g, ci->width);
return ex ? tex_round_xn_over_d(wd, 1000 + ex, 1000) : wd;
}
/* Checkers: */
int tex_has_ligature(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci ? ci->ligatures != NULL : 0;
}
int tex_has_kern(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci ? ci->kerns != NULL : 0;
}
int tex_char_has_math(halfword f, halfword c)
{
charinfo *ci = tex_aux_char_info(f, c);
return ci ? ci->math != NULL : 0;
}
/* Setters: */
void tex_set_lpcode_in_font(halfword f, halfword c, halfword i)
{
charinfo *ci = tex_aux_char_info(f, c);
if (ci) {
ci->leftprotrusion = i;
}
}
void tex_set_rpcode_in_font(halfword f, halfword c, halfword i)
{
charinfo *ci = tex_aux_char_info(f, c);
if (ci) {
ci->rightprotrusion = i;
}
}
void tex_set_efcode_in_font(halfword f, halfword c, halfword i) {
charinfo *ci = tex_aux_char_info(f, c);
if (ci) {
ci->expansion = i;
}
}
void tex_set_cfcode_in_font(halfword f, halfword c, halfword i) {
charinfo *ci = tex_aux_char_info(f, c);
if (ci) {
ci->compression = i;
}
}
void tex_set_font_name(halfword f, const char *s)
{
if (font_name(f)) {
lmt_memory_free(font_name(f));
}
set_font_name(f, s ? lmt_memory_strdup(s) : NULL);
}
void tex_set_font_original(halfword f, const char *s)
{
if (font_original(f)) {
lmt_memory_free(font_original(f));
}
set_font_original(f, s ? lmt_memory_strdup(s) : NULL);
}
scaled tex_get_math_font_scale(halfword f, halfword size)
{
scaled scale = 1000;
switch (size) {
case 2: scale = lmt_font_state.fonts[f]->mathscales[2] ? lmt_font_state.fonts[f]->mathscales[2] : glyph_scriptscript_scale_par; break;
case 1: scale = lmt_font_state.fonts[f]->mathscales[1] ? lmt_font_state.fonts[f]->mathscales[1] : glyph_script_scale_par; break;
case 0: scale = lmt_font_state.fonts[f]->mathscales[0] ? lmt_font_state.fonts[f]->mathscales[0] : glyph_text_scale_par; break;
}
return scale ? scale : 1000;
}
/*tex
Experiment.
*/
void tex_run_font_spec(void)
{
update_tex_font_identifier(font_spec_identifier(cur_chr));
if (font_spec_scale(cur_chr) != unused_scale_value) {
update_tex_glyph_scale(font_spec_scale(cur_chr));
}
if (font_spec_x_scale(cur_chr) != unused_scale_value) {
update_tex_glyph_x_scale(font_spec_x_scale(cur_chr));
}
if (font_spec_y_scale(cur_chr) != unused_scale_value) {
update_tex_glyph_y_scale(font_spec_y_scale(cur_chr));
}
}
|