math-ext.lua /size: 4530 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['math-ext'] = {
2    version   = 1.001,
3    comment   = "companion to math-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local basename = file.basename
10local sortedhash  = table.sortedhash
11
12local mathematics     = mathematics
13local extras          = mathematics.extras or { }
14mathematics.extras    = extras
15
16local characters      = characters
17local chardata        = characters.data
18local mathpairs       = characters.mathpairs
19
20local trace_virtual   = false
21local report_math     = logs.reporter("mathematics")
22
23trackers.register("math.virtual", function(v) trace_virtual = v end)
24
25local mathplus        = { }
26
27-- todo: store them and skip storage if already stored
28-- todo: make a char-ctx.lua (or is this already side effect of save in format)
29
30function extras.add(unicode)
31    local min = mathematics.extrabase
32    local max = min + 0xFFF
33    if unicode >= min and unicode <= max then
34        if chardata[unicode] then
35            mathplus[unicode] = true
36        else
37            report_math("extra %U is not a registered code point",unicode)
38        end
39    else
40        report_math("extra %U should be in range %U - %U",unicode,min,max)
41    end
42end
43
44function extras.copy(target,original)
45    local characters = target.characters
46    local properties = target.properties
47    local parameters = target.parameters
48    for unicode in sortedhash(mathplus) do
49        local extradesc  = chardata[unicode]
50        local nextinsize = extradesc.nextinsize
51        if nextinsize then
52            local extrachar = characters[unicode]
53            local first     = 1
54            local charused  = unicode
55            if not extrachar then
56                for i=1,#nextinsize do
57                    local slot = nextinsize[i]
58                    extrachar = characters[slot]
59                    if extrachar then
60                        characters[unicode] = extrachar
61                        first = i + 1
62                        charused = slot
63                        break
64                    end
65                end
66            end
67            if not extrachar then
68                if trace_virtual then
69                    report_math("extra %U in %a at %p with class %a and name %a is not mapped",
70                        unicode,basename(properties.fullname),parameters.size,
71                        extradesc.mathclass,extradesc.mathname)
72                end
73            elseif not extrachar.next then
74                local nextused = false
75                for i=first,#nextinsize do
76                    local nextslot = nextinsize[i]
77                    local nextbase = characters[nextslot]
78                    if nextbase then
79                        local nextnext = nextbase and nextbase.next
80                        if nextnext then
81                            local nextchar = characters[nextnext]
82                            if nextchar then
83                                extrachar.next = nextchar
84                                nextused = nextslot
85                                break
86                            end
87                        end
88                    end
89                end
90                if trace_virtual then
91                    if nextused then
92                        report_math("extra %U in %a at %p with class %a and name %a maps onto %U with next %U",
93                            unicode,basename(properties.fullname),parameters.size,charused,
94                            extradesc.mathclass,extradesc.mathname,nextused)
95                    else
96                        report_math("extra %U in %a at %p with class %a and name %a maps onto %U with no next",
97                            unicode,basename(properties.fullname),parameters.size,charused,
98                            extradesc.mathclass,extradesc.mathname)
99                    end
100                end
101            else
102                if trace_virtual then
103                    report_math("extra %U in %a at %p with class %a and name %a maps onto %U with no next", -- own next
104                        unicode,basename(properties.fullname),parameters.size,charused,
105                        extradesc.mathclass,extradesc.mathname)
106                end
107            end
108        end
109    end
110end
111
112utilities.sequencers.appendaction(mathactions,"system","mathematics.extras.copy")
113
114extras.add(0xFE321)
115extras.add(0xFE322)
116extras.add(0xFE323)
117extras.add(0xFE324)
118