summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/math-ini.lmt
blob: 81138b2e230ea49088183de62213e6180cc72096 (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
if not modules then modules = { } end modules ['math-ini'] = {
    version   = 1.001,
    comment   = "companion to math-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- The way we do math in \CONTEXT\ \MKIV\ differs from other macro packages so you
-- should not mix the somewhat unique (and bit weird) approach here with the more
-- traditional (\TEX) approach. Also, we use char-def.lua as starting point and that
-- file is quite \CONTEXT\ specific. When we added math to that there was no
-- interest (quite the contrary) so we didn't went generic there which in retrospect
-- also gives us the freedom to add more information, something that happens
-- occasionally. Because that file is shared between \MKIV\ and \LMTX\ some
-- information is only used by \LMTX. We also have quite some runs over the math
-- list but that has been so since we started and performance will not degrade much
-- by it; after all math is not that demanding. More details can be found in the
-- manuals that discuss math. Most code (and concepts) date from 2005 so maybe I
-- will upgrade the lot some day, although it has been adapted on the way to the
-- changes in the engine(s).

local next, type = next, type
local formatters, find, nospaces = string.formatters, string.find, string.nospaces
local utfchar, utfbyte = utf.char, utf.byte
local sortedhash = table.sortedhash
local toboolean = toboolean

local context               = context
local commands              = commands
local implement             = interfaces.implement

local ctx_sprint            = context.sprint
local ctx_doifelsesomething = commands.doifelsesomething

local trace_defining        = false  trackers.register("math.defining", function(v) trace_defining = v end)

local report_math           = logs.reporter("mathematics","initializing")

mathematics                 = mathematics or { }
local mathematics           = mathematics

mathematics.extrabase       = fonts.privateoffsets.mathextrabase -- here we push some virtuals
mathematics.privatebase     = fonts.privateoffsets.mathbase      -- here we push the ex

local unsetvalue            = attributes.unsetvalue
local allocate              = utilities.storage.allocate
local chardata              = characters.data

local texsetattribute       = tex.setattribute
local setmathcode           = tex.setmathcode
local setdelcode            = tex.setdelcode
local texintegerdef         = tex.integerdef

setdelcode = function() end

-- This is relatively new and experimental:

do

    local dictionaries       = mathematics.dictionaries or { }
    mathematics.dictionaries = dictionaries

    local names    = dictionaries.names    or utilities.storage.allocate()
    local groups   = dictionaries.groups   or utilities.storage.allocate()
    local data     = dictionaries.data     or utilities.storage.allocate()
    local sets     = dictionaries.sets     or utilities.storage.allocate()
    local variants = dictionaries.variants or utilities.storage.allocate() -- todo: get from char-def
    local defaults = dictionaries.defaults or utilities.storage.allocate() -- todo: get from char-def

    storage.register("mathematics/dictionaries/names",    names,    "mathematics.dictionaries.names")
    storage.register("mathematics/dictionaries/groups",   groups,   "mathematics.dictionaries.groups")
    storage.register("mathematics/dictionaries/data",     data,     "mathematics.dictionaries.data")
    storage.register("mathematics/dictionaries/sets",     sets,     "mathematics.dictionaries.sets")
    storage.register("mathematics/dictionaries/variants", variants, "mathematics.dictionaries.variants")
    storage.register("mathematics/dictionaries/defaults", defaults, "mathematics.dictionaries.defaults")

    dictionaries.names    = dictionaries.names    or names
    dictionaries.groups   = dictionaries.groups   or groups
    dictionaries.data     = dictionaries.data     or data
    dictionaries.sets     = dictionaries.sets     or sets
    dictionaries.variants = dictionaries.variants or variants
    dictionaries.defaults = dictionaries.defaults or defaults

    if not sets.n then
        sets.n = 0
    end

    function dictionaries.registergroup(name)
        local group = rawget(names,name)
        if not group then
            group = #groups + 1
            names[name]   = group
            names[group]  = group
            groups[group] = name
            data[group]   = { }
            local csname  = "math" .. nospaces(name) .. "dictionary"
            texintegerdef(csname,group,"immutable")
        end
        return group
    end

    function dictionaries.registergroupset(name,set)
        local s = sets[name]
        if not s then
            local d = dictionaries.registergroup(name)
            local n = sets.n + 1
            local l = utilities.parsers.settings_to_array(set)
            local g = { }
            for i=1,#l do
                local n = names[l[i]]
                if n then
                    g[#g+1] = n -- ordered
                end
            end
            s = {
                names  = l,
                groups = g,
                group  = d,
            }
            sets[name] = s
            sets[d]    = s

        end
    end

    function dictionaries.groupset(name)
        return sets[name] or { }
    end

    function dictionaries.groupsetgroup(name)
        local s = sets[name]
        if s then
            return s.group
        else
            return names[name] or 0
        end
    end

    function dictionaries.registercharacter(group,index,description,class)
        local d = names[group] -- can be number or string
        if d then
            data[d][index] = description or true
            local v = variants[index]
            if not class then
                class = true
            end
            if v then
                v[d] = class
            else
                variants[index] = { [d] = class}
            end
            if not defaults[index] then
                defaults[index] = d
            end
        end
    end

    implement {
        name      = "registergroupset",
        arguments = "2 strings",
        actions   = dictionaries.registergroupset,
    }

    implement {
        name      = "groupsetgroup",
        arguments = "string",
        actions   = { dictionaries.groupsetgroup, context },
    }

    local fontchardata = fonts.hashes.characters
    local f_dictionary = false
    local register     = callback.register
    local whatdetail   = "all"

    local function trace(n,properties,group,index,font,char)
     -- local properties, group, index, font, char = nodes.nuts.getchardict(nodes.nuts.tonut(n))
        if properties ~= 0 or group ~= 0 or index ~= 0 then
            local char = fontchardata[font][char]
            if char or whatdetail == "all" then
                local unicode = char and char.unicode
                if unicode then
                    local groupname = groups[group]
                    local indexname = false
                    if groupname then
                        indexname = data[group][index] -- dictionaries.data
                    else
                        groupname = "unknown"
                    end
                    if not indexname or indexname == true then
                        indexname = chardata[unicode]
                        indexname = indexname and indexname.description or "unknown"
                    end
                    if not f_dictionary then
                        f_dictionary = formatters["properties [%04X:%04X:%04X] [%s] %U : %s"]
                    end
                    return f_dictionary(properties,group,index,groupname,unicode,indexname)
                end
            end
        end
    end

    trackers.register("math.dictionaries",function(v)
        whatdetail = v
        if whatdetail then
            register("get_math_dictionary",trace)
        else
            register("get_math_dictionary")
        end
    end)

    register("get_math_dictionary",trace)

    -- This is experimental and a prelude to the long pending "relate math rendering to
    -- some field" wish. In TeX characters and symbols are grouped by class but that is
    -- mostly related to spacing etc. while we actually want to group by meaning. A
    -- reasonable but incomplete starting point is:
    --
    -- https://www.w3.org/TR/MathML3/appendixa.html#parsing_DefEncAtt
    --
    -- But it has some weird short names mixed with long ones (and a strange suddenly
    -- uppercase: Differential-Operator) but we are not bound to that at all. We will
    -- probably remove and add categories anyway. This openmath stuff looks a bit
    -- abandoned but we can use it as a start and playground anyway.
    --
    -- The char-def.lua file will have mathgroup entries reflecting this.
    --
    -- This is a good one (with nice roll-overs too):
    --
    -- https://en.wikipedia.org/wiki/List_of_mathematical_symbols_by_subject

    if environment.initex then

        local registergroup = mathematics.dictionaries.registergroup

        registergroup("default")
        registergroup("binary arithmetic")
        registergroup("binary linear algebra")
        registergroup("binary logical")
        registergroup("binary relation")
        registergroup("binary set")
        registergroup("constant arithmetic")
        registergroup("constant set")
        registergroup("differential")
        registergroup("integral")
        registergroup("interval")
        registergroup("lambda")
        registergroup("limit")
        registergroup("nary arithmetic")
        registergroup("nary constructor")
        registergroup("nary functional")
        registergroup("nary linear algebra")
        registergroup("nary logical")
        registergroup("nary minmax")
        registergroup("nary relation")
        registergroup("nary set list")
        registergroup("nary set relation")
        registergroup("nary set")
        registergroup("nary statistics")
        registergroup("partial") -- partial differential
        registergroup("product")
        registergroup("quantifier")
        registergroup("unary arithmetic")
        registergroup("unary elementary")
        registergroup("unary functional")
        registergroup("unary linear algebra")
        registergroup("unary logical")
        registergroup("unary set")
        registergroup("unary vector")

    end

    -- \Umathdictdef\vdash 1 \mathbinarylogicaldictionary "22A2 \mathrelationcode 0 "22A2
    --
    -- \startluacode
    --     mathematics.dictionaries.registercharacter("binary logical",0x22A2,"implies")
    -- \stopluacode

end

-- These are different from mkiv with luatex.

local classes          = allocate { unset = 64 } -- or -1
local classnames       = allocate { }
local maxengineclass   = 63
local lastengineclass  = 0
local lastprivateclass = maxengineclass

for k, v in next, nodes.noadcodes do
    if type(k) == "string"then
        classes[k] = v
        local n = classnames[v]
        if not n or #k < #n then
            classnames[v] = k
        end
    elseif k > lastengineclass then
        lastengineclass = k
    end
end

local ordinary_class    = classes.ordinary
local operator_class    = classes.operator
local binary_class      = classes.binary
local relation_class    = classes.relation
local open_class        = classes.open
local close_class       = classes.close
local punctuation_class = classes.punctuation
local middle_class      = classes.middle
local accent_class      = classes.accent
local radical_class     = classes.radical
local fraction_class    = classes.fraction
local under_class       = classes.under
local over_class        = classes.over
local fenced_class      = classes.fenced
local ghost_class       = classes.ghost

classes.ord             = ordinary_class
classes.op              = operator_class
classes.bin             = binary_class
classes.rel             = relation_class
classes.opening         = open_class         -- will go
classes.closing         = close_class        -- will go
classes.punct           = punctuation_class
classes.frac            = fraction_class
classes.rad             = radical_class
classes.fen             = fenced_class
classes.gst             = ghost_class

classes.limop           = operator_class
classes.limoperator     = operator_class
classes.nolop           = operator_class
classes.nolimoperator   = operator_class
classes.large           = operator_class
classes.largeoperator   = operator_class

-- special in the engine   : variable active inner vcenter

local function registerengineclass(name,short)
    local class = classes[name]
    if not class then
        if lastengineclass < maxengineclass then
            lastengineclass = lastengineclass + 1
            class = lastengineclass
            classnames[class] = short or name
        else
            class = ordinary_class
        end
        classes[name] = class
    end
    return class
end

registerengineclass("explicit",        "xpl")
registerengineclass("imaginary",       "img")
registerengineclass("differential",    "dif")
registerengineclass("exponential",     "exp")
registerengineclass("ellipsis",        "ell")
registerengineclass("function",        "fnc")
registerengineclass("digit",           "dig")
local division_class =
registerengineclass("division",        "div")
registerengineclass("factorial",       "fac")
registerengineclass("wrapped",         "wra")
registerengineclass("construct",       "con")
registerengineclass("dimension",       "dim")
registerengineclass("unary",           "una")
registerengineclass("textpunctuation", "tpu")
registerengineclass("unspaced")
registerengineclass("experimental")
registerengineclass("fake")
registerengineclass("numbergroup",     "ngr")

registerengineclass("maybeordinary")
registerengineclass("mayberelation")
registerengineclass("maybebinary")

local specialclasses = tex.specialmathclasscodes

classes["all"]   = specialclasses["all"]    classnames[specialclasses["all"]  ] = "all"
classes["begin"] = specialclasses["begin"]  classnames[specialclasses["begin"]] = "begin"
classes["end"]   = specialclasses["end"]    classnames[specialclasses["end"]  ] = "end"

callback.register("get_noad_class", function(n) return classnames[n] end)

local function registerprivateclass(name,parent)
    local class = parent and classes[parent] or classes[name]
    if not class then
        lastprivateclass = lastprivateclass + 1
        class = lastprivateclass
        classes[name] = class
        -- also setup
    end
    return class
end

local function toengineclass(class)
    if type(class) == "string" then
        return classes[class] or ordinary_class
    elseif class > lastengineclass then
        return ordinary_class
    else
        return class
    end
end

implement {
    name      = "registerengineclass",
    public    = true,
    protected = true,
    arguments = { "optional", "optional" },
    actions   = registerengineclass,
}

local topaccent_class    = registerprivateclass("topaccent")
local bottomaccent_class = registerprivateclass("bottomaccent")
local delimiter_class    = registerprivateclass("delimiter")
local root_class         = registerprivateclass("root")
local prime_class        = registerprivateclass("prime")

registerprivateclass("botaccent","bottomaccent")

local accents = allocate {
    accent       = true, -- some can be both
    topaccent    = true,  [topaccent_class]    = true,
    bottomaccent = true,  [bottomaccent_class] = true,
    botaccent    = true,
    under        = true,  [under_class]        = true,
    over         = true,  [over_class]         = true,
    unknown      = false,
}

local integer_value = tokens.values.integer

implement {
    name      = "mathclassvalue",
 -- usage     = "value",
    public    = true,
    arguments = "string",
    actions = function(name)
     -- return integer_value, classes[name] or ordinary_class
        context(tostring(classes[name] or ordinary_class))
    end
}

-- used in math-tag: so there we need to make things ord etc to fit within
-- mathml

local codes = allocate {
    ordinary       = ordinary_class,    [ordinary_class]    = "ordinary",
    largeoperator  = operator_class,    [operator_class]    = "largeoperator",
    binaryoperator = binary_class,      [binary_class]      = "binaryoperator",
    relation       = relation_class,    [relation_class]    = "relation",
    openingsymbol  = open_class,        [open_class]        = "openingsymbol",
    closingsymbol  = close_class,       [close_class]       = "closingsymbol",
    punctuation    = punctuation_class, [punctuation_class] = "punctuation",
    middlesymbol   = middle_class,      [middle_class]      = "middlesymbol",
}

local extensibles = allocate {
           unknown    = 0,
    l = 1, left       = 1,
    r = 2, right      = 2,
    h = 3, horizontal = 3,-- lr or rl
    u = 5, up         = 4,
    d = 5, down       = 5,
    v = 6, vertical   = 6,-- ud or du
    m = 7, mixed      = 7,
}

table.setmetatableindex(extensibles,function(t,k) t[k] = 0 return 0 end)

local virtualized = allocate {
}

function mathematics.virtualize(unicode,virtual)

    local function virtualize(k,v)
        local c = virtualized[k]
        if c == v then
            report_math("character %C is already virtualized to %C",k,v)
        elseif c then
            report_math("character %C is already virtualized to %C, ignoring mapping to %C",k,c,v)
        else
            virtualized[k] = v
        end
    end

    if type(unicode) == "table" then
        for k, v in next, unicode do
            virtualize(k,v)
        end
    elseif type(unicode) == "number" and type(virtual) == "number" then
        virtualize(unicode,virtual)
 -- else
        -- error
    end
end

mathematics.extensibles   = extensibles
mathematics.classes       = classes
mathematics.toengineclass = toengineclass
mathematics.classnames    = classnames
mathematics.codes         = codes
-----------.accents       = codes
mathematics.virtualized   = virtualized

do

    local registercharacter = mathematics.dictionaries.registercharacter
    local groupnames        = mathematics.dictionaries.names

    local setmathcharacter = function(class,family,slot,unicode,mset,dset,group)
        if mset and class ~= ordinary_class then
            setmathcode("global",slot,class,family,unicode)
            mset = false
        end
        if dset and (class == open_class or class == close_class or class == middle_class or class == division_class) then
            setdelcode("global",slot,family,unicode,0,0)
            dset = false
        end
        if group then
            group = groupnames[group] or 0
            if group ~= 0 then
                -- which one
                registercharacter(group,unicode,nil,class)
             -- registercharacter(group,slot,nil,class)
            end
        end
        return mset, dset
    end

    local function report(class,engine,family,unicode,name)
        local nametype = type(name)
        if nametype == "string" then
            report_math("class %a, engine %a, family %a, char %C, name %a",class,engine,family,unicode,name)
        elseif nametype == "number" then
            report_math("class %a, engine %a, family %a, char %C, number %U",class,engine,family,unicode,name)
        else
            report_math("class %a, engine %a, family %a, char %C",class,engine,family,unicode)
        end
    end

    local f_accent            = formatters[ [[\defUmathtopaccent \%s{%X}{%X}{%X}]] ]
    local f_fixedtopaccent    = formatters[ [[\defUmathfixedtopaccent \%s{%X}{%X}{%X}]] ]
    local f_fixedbottomaccent = formatters[ [[\defUmathfixedbottomaccent \%s{%X}{%X}{%X}]] ]
    local f_topaccent         = formatters[ [[\defUmathtopaccent \%s{%X}{%X}{%X}]] ]
    local f_bottomaccent      = formatters[ [[\defUmathbottomaccent \%s{%X}{%X}{%X}]] ]
    local f_over              = formatters[ [[\defUdelimiterover \%s{%X}{%X}{%X}]] ]
    local f_under             = formatters[ [[\defUdelimiterunder\%s{%X}{%X}{%X}]] ]
    local f_fence             = formatters[ [[\defUdelimiter \%s{%X}{%X}{%X}]] ]
    local f_delimiter         = formatters[ [[\defUdelimiter \%s{%X}{%X}{%X}]] ]
    local f_radical           = formatters[ [[\defUradical \%s{%X}{%X}]]     ]
    local f_root              = formatters[ [[\defUroot \%s{%X}{%X}]]     ]
    local f_char              = formatters[ [[\defUmathchar \%s{%X}{%X}{%X}]] ]

    local texmathchardef = tex.mathchardef

    local setmathsymbol = function(name,class,engine,family,slot,stretch,group) -- hex is nicer for tracing
        if class == accent_class then
            ctx_sprint(f_topaccent(name,0,family,slot))
        elseif class == topaccent_class then
            -- only widegrave widehat widetilde : still needed?
            ctx_sprint((stretch and f_topaccent or f_fixedtopaccent)(name,0,family,slot))
        elseif class == bottomaccent_class then
            ctx_sprint((stretch and f_bottomaccent or f_fixedbottomaccent)(name,0,family,slot))
        elseif class == over_class then
            ctx_sprint(f_over(name,0,family,slot))
        elseif class == under_class then
            ctx_sprint(f_under(name,0,family,slot))
        elseif class == open_class or class == close_class or class == middle_class then
            setdelcode("global",slot,family,slot,0,0)
            ctx_sprint(f_fence(name,engine,family,slot))
        elseif class == delimiter_class then
            setdelcode("global",slot,family,slot,0,0)
            ctx_sprint(f_delimiter(name,0,family,slot))
        elseif class == radical_class then
            ctx_sprint(f_radical(name,family,slot))
        elseif class == root_class then
            ctx_sprint(f_root(name,family,slot))
        elseif texmathchardef then
            if group then
                group = groupnames[group] or 0
                if group ~= 0 then
                    texmathchardef(name,engine,family,slot,"permanent",0x1,group,slot)
                    return
                end
            end
            texmathchardef(name,engine,family,slot,"permanent")
        else
            -- beware, open/close and other specials should not end up here
            ctx_sprint(f_char(name,engine,family,slot))
        end
    end

    function mathematics.define()
        if trace_defining then
            logs.startfilelogging(report_math,"math defined from character definitions")
        end
        local family = 0
        local data   = characters.data
        --
        local function remap(first,last)
            for unicode=utfbyte(first),utfbyte(last) do
                setmathcode("global",unicode,ordinary_class,family,unicode)
            end
        end
        remap("0","9")
        remap("A","Z")
        remap("a","z")
        --
        setdelcode("global",0x2E,0,0,0,0) -- period is special
        --
        for unicode, character in sortedhash(data) do
            local symbol = character.mathsymbol
            local mset   = true
            local dset   = true
            if symbol then
                local other = data[symbol]
                local class = other.mathclass
                if class then
                    local engine = toengineclass(class)
                    if trace_defining then
                        report(class,engine,family,unicode,symbol)
                    end
                    mset, dset = setmathcharacter(engine,family,unicode,symbol,mset,dset,group)
                end
                local spec = other.mathspec
                if spec then
                    for i=1,#spec do
                        local m = spec[i]
                        local class = m.class
                        if class then
                            local engine = toengineclass(class)
                            -- todo: trace
                            mset, dset = setmathcharacter(engine,family,unicode,symbol,mset,dset,group)
                        end
                    end
                end
            end
            local class   = character.mathclass
            local spec    = character.mathspec
            local name    = character.mathname
            local stretch = character.mathstretch
            local group   = character.mathgroup
            if spec then
                local done = false
                if class then
                    if name then
                        report_math("fatal error, conflicting mathclass and mathspec for %C",unicode)
                        os.exit()
                    else
                        class = classes[class] or ordinary_class
                        local engine = toengineclass(class)
                        if trace_defining then
                            report(class,engine,family,unicode)
                        end
                        mset, dset = setmathcharacter(engine,family,unicode,unicode,mset,dset,group)
                        done = true
                    end
                end
                for i=1,#spec do
                    local m       = spec[i]
                    local name    = m.name
                    local class   = m.class   or class
                    local group   = m.group   or group
                    local stretch = m.stretch or stretch
                    if class then
                        class = classes[class] or ordinary_class
                    else
                        class = ordinary_class
                    end
                    if class then
                        local engine = toengineclass(class)
                        if name then
                            if trace_defining then
                                report(class,engine,family,unicode,name)
                            end
                            setmathsymbol(name,class,engine,family,unicode,stretch,group)
                        else
                            name = (class == classes.ordinary or class == classes.digit) and character.adobename -- bad
                            if name and trace_defining then
                                report(class,engine,family,unicode,name)
                            end
                        end
                        if not done then
                            mset, dset = setmathcharacter(engine,family,unicode,m.unicode or unicode,mset,dset,group) -- see solidus
                            done = true
                        end
                    end
                end
            else
                if class then
                    class = classes[class] or ordinary_class
                else
                    class = ordinary_class
                end
                if name ~= nil then
                    local engine = toengineclass(class)
                    if name == false then
                        if trace_defining then
                            report(class,engine,family,unicode,name)
                        end
                        mset, dset = setmathcharacter(engine,family,unicode,unicode,mset,dset,group)
                    else
                     -- if not name then
                     --     name = character.contextname -- too dangerous, we loose textslash and a few more
                     -- end
                        if name then
                            if trace_defining then
                                report(class,engine,family,unicode,name)
                            end
                            setmathsymbol(name,class,engine,family,unicode,stretch,group)
                        else
                            if trace_defining then
                                report(class,engine,family,unicode,character.adobename)
                            end
                        end
                        mset, dset = setmathcharacter(engine,family,unicode,unicode,mset,dset,group)
                    end
                elseif class ~= ordinary_class then
                    local engine = toengineclass(class)
                    if trace_defining then
                        report(class,engine,family,unicode,character.adobename)
                    end
                    mset, dset = setmathcharacter(engine,family,unicode,unicode,mset,dset,group)
                end
            end
        end
        --
        if trace_defining then
            logs.stopfilelogging()
        end
    end

end

-- needed for mathml analysis
-- string with # > 1 are invalid
-- we could cache

do

    local lpegmatch = lpeg.match
    local utf8byte  = lpeg.patterns.utf8byte * lpeg.P(-1)

    -- function somechar(c)
    --     local b = lpegmatch(utf8byte,c)
    --     return b and chardata[b]
    -- end

    local somechar = table.setmetatableindex(function(t,k)
        if k then
            local b = lpegmatch(utf8byte,k)
            local v = b and chardata[b] or false
            t[k] = v
            return v
        end
    end)

    local function utfmathclass(chr, default)
        local cd = somechar[chr]
        return cd and cd.mathclass or default or "unknown"
    end

    local function utfmathlimop(chr)
        local cd = somechar[chr]
        return cd and cd.mathclass == "limop" or false
    end

    local function utfmathaccent(chr,default,asked1,asked2)
        local cd = somechar[chr]
        if not cd then
            return default or false
        end
        if asked1 and asked1 ~= "" then
            local mc = cd.mathclass
            if mc and (mc == asked1 or mc == asked2) then
                return true
            end
            local ms = cd.mathspec
            if not ms then
                local mp = cd.mathparent
                if mp then
                    ms = chardata[mp].mathspec
                end
            end
            if ms then
                for i=1,#ms do
                    local msi = ms[i]
                    local mc = msi.class
                    if mc and (mc == asked1 or mc == asked2) then
                        return true
                    end
                end
            end
        else
            local mc = cd.mathclass
            if mc then
                return accents[mc] or default or false
            end
            local ms = cd.mathspec
            if ms then
                for i=1,#ms do
                    local msi = ms[i]
                    local mc = msi.class
                    if mc then
                        return accents[mc] or default or false
                    end
                end
            end
        end
        return default or false
    end

    local function utfmathstretch(chr,default) -- "h", "v", "b", ""
        local cd = somechar[chr]
        return cd and cd.mathstretch or default or ""
    end

    local function utfmathcommand(chr,default,asked1,asked2)
        local cd = somechar[chr]
        if not cd then
            return default or ""
        end
        if asked1 then
            local mn = cd.mathname
            local mc = cd.mathclass
            if mn and mc and (mc == asked1 or mc == asked2) then
                return mn
            end
            local ms = cd.mathspec
            if not ms then
                local mp = cd.mathparent
                if mp then
                    ms = chardata[mp].mathspec
                end
            end
            if ms then
                for i=1,#ms do
                    local msi = ms[i]
                    local mn = msi.name
                    if mn then
                        local mc = msi.class
                        if mc == asked1 or mc == asked2 then
                            return mn
                        end
                    end
                end
            end
        else
            local mn = cd.mathname
            if mn then
                return mn
            end
            local ms = cd.mathspec
            if ms then
                for i=1,#ms do
                    local msi = ms[i]
                    local mn = msi.name
                    if mn then
                        return mn
                    end
                end
            end
        end
        return default or ""
    end

    local function utfmathfiller(chr, default)
        local cd = somechar[chr]
        local cmd = cd and cd.mathfiller -- or cd.mathname
        return cmd or default or ""
    end

    mathematics.utfmathclass   = utfmathclass
    mathematics.utfmathstretch = utfmathstretch
    mathematics.utfmathcommand = utfmathcommand
    mathematics.utfmathfiller  = utfmathfiller
    mathematics.utfmathaccent  = utfmathaccent

    -- interfaced

    implement {
        name      = "utfmathclass",
        public    = true,
        actions   = { utfmathclass, context },
        arguments = "argument"
    }

    implement {
        name      = "utfmathstretch",
        public    = true,
        actions   = { utfmathstretch, context },
        arguments = "argument"
    }

    implement {
        name      = "utfmathcommand",
        public    = true,
        actions   = { utfmathcommand, context },
        arguments = "argument"
    }

    implement {
        name      = "utfmathfiller",
        public    = true,
        actions   = { utfmathfiller, context },
        arguments = "argument"
    }

    implement {
        name      = "utfmathcommandabove",
        public    = true,
        actions   = { utfmathcommand, context },
        arguments = { "argument", false, "'topaccent'","'over'" }
    }

    implement {
        name      = "utfmathcommandbelow",
        public    = true,
        actions   = { utfmathcommand, context },
        arguments = { "argument", false, "'bottomaccent'","'under'" }
    }

    implement {
        name      = "utfmathcommandfiller",
        public    = true,
        actions   = { utfmathfiller, context },
        arguments = "argument"
    }

    -- todo: make this a helper:

    implement {
        name      = "doifelseutfmathabove",
        public    = true,
        actions   = { utfmathaccent, ctx_doifelsesomething },
        arguments = { "argument", false, "'topaccent'", "'over'" }
    }

    implement {
        name      = "doifelseutfmathbelow",
        public    = true,
        actions   = { utfmathaccent, ctx_doifelsesomething },
        arguments = { "argument", false, "'bottomaccent'", "'under'" }
    }

    implement {
        name      = "doifelseutfmathaccent",
        public    = true,
        actions   = { utfmathaccent, ctx_doifelsesomething },
        arguments = "argument",
    }

    implement {
        name      = "doifelseutfmathfiller",
        public    = true,
        actions   = { utfmathfiller, ctx_doifelsesomething },
        arguments = "argument",
    }

    implement {
        name      = "doifelseutfmathlimop",
        public    = true,
        actions   = { utfmathlimop, ctx_doifelsesomething },
        arguments = "argument"
    }

end

-- helpers
--
-- 1: step 1
-- 2: step 2
-- 3: htdp * 1.33^n
-- 4: size * 1.33^n
-- 5: use lfg

function mathematics.big(tfmdata,unicode,n,method)
    local t = tfmdata.characters
    local c = t[unicode]
    if c and n > 0 then
        if method == 1 or method == 2 or method == 5 then
            if method == 5 then
                local b = tfmdata.bigslots
                if b then
                    n = (n > #b and b[#b]) or b[n] or n
                end
            elseif method == 2 then -- large steps
                n = n * 2
            end
            local next = c.next
            while next do
                if n <= 1 then
                    return next
                else
                    n = n - 1
                    local tn = t[next].next
                    if tn then
                        next = tn
                    else
                        return next
                    end
                end
            end
        elseif method >= 3 then
            local size = 1.33^n
            if method == 4 then
                size = tfmdata.parameters.size * size
            else -- if method == 3 then
                size = (c.height + c.depth) * size
            end
            local next = c.next
            while next do
                local cn = t[next]
                if (cn.height + cn.depth) >= size then
                    return next
                else
                    local tn = cn.next
                    if tn then
                        next = tn
                    else
                        return next
                    end
                end
            end
        end
    end
    return unicode
end

do -- experimental

    -- local categories = { } -- indexed + hashed
    --
    -- local a_mathcategory = attributes.private("mathcategory")
    --
    -- local function registercategory(category,tag,data) -- always same data for tag
    --     local c = categories[category]
    --     if not c then
    --         c = { }
    --         categories[category] = c
    --     end
    --     local n = c[tag]
    --     if not n then
    --         n = #c + 1
    --         c[n] = data
    --         n = n * 1000 + category
    --         c[tag] = n
    --     end
    --     return n
    -- end
    --
    -- function mathematics.getcategory(n)
    --     local category = n % 1000
    --     return category, categories[category][floor(n/1000)]
    -- end
    --
    -- mathematics.registercategory = registercategory
    --
    -- function commands.taggedmathfunction(tag,label)
    --     if label then
    --         texsetattribute(a_mathcategory,registercategory(1,tag,tag))
    --         context.mathlabeltext(tag)
    --     else
    --         texsetattribute(a_mathcategory,1)
    --         context(tag)
    --     end
    -- end

    local categories       = { }
    mathematics.categories = categories

    local a_mathcategory   = attributes.private("mathcategory")

    local functions        = storage.allocate()
    categories.functions   = functions
    local noffunctions     = 1000 -- offset

    implement {
        name      = "tagmfunctiontxt",
        arguments = { "string", "conditional" },
        actions   = function(tag,apply)
            local delta = apply and 1000 or 0
            texsetattribute(a_mathcategory,1000 + delta)
        end
    }

    implement {
        name      = "tagmfunctionlab",
        arguments = { "string", "conditional" },
        actions   = function(tag,apply)
            local delta = apply and 1000 or 0
            local n = functions[tag]
            if not n then
                noffunctions = noffunctions + 1
                functions[noffunctions] = tag
                functions[tag] = noffunctions
                texsetattribute(a_mathcategory,noffunctions + delta)
            else
                texsetattribute(a_mathcategory,n + delta)
            end
        end
    }

end

do

    local list

    function mathematics.resetattributes()
        if not list then
            list = { }
            for k, v in next, attributes.numbers do
                if find(k,"^math") then
                    list[#list+1] = v
                end
            end
        end
        for i=1,#list do
            texsetattribute(list[i],unsetvalue)
        end
    end

end

implement {
    name      = "resetmathattributes",
    public    = true,
    protected = true,
    actions   = mathematics.resetattributes
}

-- weird to do this here but it's a side affect of math anyway

implement {
    name     = "enableasciimode",
    onlyonce = true,
    actions  = resolvers.macros.enablecomment,
}