math-ext.lua /size: 4557 b    last modification: 2020-07-01 14:35
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
30local function addextra(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
44extras.add = addextra
45
46function extras.copy(target,original)
47    local characters = target.characters
48    local properties = target.properties
49    local parameters = target.parameters
50    for unicode in sortedhash(mathplus) do
51        local extradesc  = chardata[unicode]
52        local nextinsize = extradesc.nextinsize
53        if nextinsize then
54            local extrachar = characters[unicode]
55            local first     = 1
56            local charused  = unicode
57            if not extrachar then
58                for i=1,#nextinsize do
59                    local slot = nextinsize[i]
60                    extrachar = characters[slot]
61                    if extrachar then
62                        characters[unicode] = extrachar
63                        first = i + 1
64                        charused = slot
65                        break
66                    end
67                end
68            end
69            if not extrachar then
70                if trace_virtual then
71                    report_math("extra %U in %a at %p with class %a and name %a is not mapped",
72                        unicode,basename(properties.fullname),parameters.size,
73                        extradesc.mathclass,extradesc.mathname)
74                end
75            elseif not extrachar.next then
76                local nextused = false
77                for i=first,#nextinsize do
78                    local nextslot = nextinsize[i]
79                    local nextbase = characters[nextslot]
80                    if nextbase then
81                        local nextnext = nextbase and nextbase.next
82                        if nextnext then
83                            local nextchar = characters[nextnext]
84                            if nextchar then
85                                extrachar.next = nextchar
86                                nextused = nextslot
87                                break
88                            end
89                        end
90                    end
91                end
92                if trace_virtual then
93                    if nextused then
94                        report_math("extra %U in %a at %p with class %a and name %a maps onto %U with next %U",
95                            unicode,basename(properties.fullname),parameters.size,charused,
96                            extradesc.mathclass,extradesc.mathname,nextused)
97                    else
98                        report_math("extra %U in %a at %p with class %a and name %a maps onto %U with no next",
99                            unicode,basename(properties.fullname),parameters.size,charused,
100                            extradesc.mathclass,extradesc.mathname)
101                    end
102                end
103            else
104                if trace_virtual then
105                    report_math("extra %U in %a at %p with class %a and name %a maps onto %U with no next", -- own next
106                        unicode,basename(properties.fullname),parameters.size,charused,
107                        extradesc.mathclass,extradesc.mathname)
108                end
109            end
110        end
111    end
112end
113
114utilities.sequencers.appendaction(mathactions,"system","mathematics.extras.copy")
115
116extras.add(0xFE321)
117extras.add(0xFE322)
118extras.add(0xFE323)
119extras.add(0xFE324)
120