s-math-coverage.lua /size: 7001 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['s-math-coverage'] = {
2    version   = 1.001,
3    comment   = "companion to s-math-coverage.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 utfchar, utfbyte = utf.char, utf.byte
10local formatters, lower = string.formatters, string.lower
11local concat = table.concat
12local sortedhash = table.sortedhash
13
14moduledata.math             = moduledata.math          or { }
15moduledata.math.coverage    = moduledata.math.coverage or { }
16
17local context               = context
18
19local ctx_NC                = context.NC
20local ctx_NR                = context.NR
21local ctx_HL                = context.HL
22
23local ctx_startmixedcolumns = context.startmixedcolumns
24local ctx_stopmixedcolumns  = context.stopmixedcolumns
25local ctx_setupalign        = context.setupalign
26local ctx_starttabulate     = context.starttabulate
27local ctx_stoptabulate      = context.stoptabulate
28local ctx_rawmathematics    = context.formatted.rawmathematics
29local ctx_mathematics       = context.formatted.mathematics
30local ctx_startimath        = context.startimath
31local ctx_stopimath         = context.stopimath
32local ctx_setmathattribute  = context.setmathattribute
33local ctx_underbar          = context.underbar
34local ctx_getglyph          = context.getglyph
35
36local styles                = mathematics.styles
37local alternatives          = mathematics.alternatives
38local charactersets         = mathematics.charactersets
39
40local getboth               = mathematics.getboth
41local remapalphabets        = mathematics.remapalphabets
42
43local chardata              = characters.data
44local superscripts          = characters.superscripts
45local subscripts            = characters.subscripts
46
47context.writestatus("math coverage","underline: not remapped")
48
49function moduledata.math.coverage.showalphabets()
50    ctx_starttabulate { "|lT|l|Tl|" }
51    for i=1,#styles do
52        local style = styles[i]
53        for i=1,#alternatives do
54            local alternative = alternatives[i]
55            for _, alphabet in sortedhash(charactersets) do
56                ctx_NC()
57                    if i == 1 then
58                        context("%s %s",style,alternative)
59                    end
60                ctx_NC()
61                    ctx_startimath()
62                    ctx_setmathattribute(style,alternative)
63                    for i=1,#alphabet do
64                        local letter = alphabet[i]
65                        local id = getboth(style,alternative)
66                        local unicode = remapalphabets(letter,id)
67                        if not unicode then
68                            ctx_underbar(utfchar(letter))
69                        elseif unicode == letter then
70                            context(utfchar(unicode))
71                        else
72                            context(utfchar(unicode))
73                        end
74                    end
75                    ctx_stopimath()
76                ctx_NC()
77                    local first = alphabet[1]
78                    local last = alphabet[#alphabet]
79                    local id = getboth(style,alternative)
80                    local f_unicode = remapalphabets(first,id) or utfbyte(first)
81                    local l_unicode = remapalphabets(last,id) or utfbyte(last)
82                    context("%05X - %05X",f_unicode,l_unicode)
83                ctx_NC()
84                ctx_NR()
85            end
86        end
87    end
88    ctx_stoptabulate()
89end
90
91function moduledata.math.coverage.showcharacters()
92    ctx_startmixedcolumns { balance = "yes" }
93    ctx_setupalign { "nothyphenated" }
94    ctx_starttabulate { "|T|i2|Tpl|" }
95    for u, d in sortedhash(chardata) do
96        local mathclass = d.mathclass
97        local mathspec = d.mathspec
98        if mathclass or mathspec then
99            ctx_NC()
100                context("%05X",u)
101            ctx_NC()
102                ctx_getglyph("MathRoman",u)
103            ctx_NC()
104                if mathspec then
105                    local t = { }
106                    for i=1,#mathspec do
107                        local class = mathspec[i].class
108                        if class then
109                            t[class] = true
110                        else
111-- inspect(d)
112                        end
113                    end
114                    t = table.sortedkeys(t)
115                    context("% t",t)
116                else
117                    context(mathclass)
118                end
119            ctx_NC()
120            ctx_NR()
121        end
122    end
123    ctx_stoptabulate()
124    ctx_stopmixedcolumns()
125end
126
127-- This is a somewhat tricky table as we need to bypass the math machinery.
128
129function moduledata.math.coverage.showscripts()
130    ctx_starttabulate { "|cT|c|cT|c|c|c|l|" }
131    for k, v in sortedhash(table.merged(superscripts,subscripts)) do
132        local ck = utfchar(k)
133        local cv = utfchar(v)
134        local ss = superscripts[k] and "^" or "_"
135        ctx_NC() context("%05X",k)
136        ctx_NC() context(ck)
137        ctx_NC() context("%05X",v)
138        ctx_NC() context(cv)
139        ctx_NC() ctx_rawmathematics("x%s = x%s%s",ck,ss,cv)
140        ctx_NC() ctx_mathematics("x%s = x%s%s",ck,ss,cv)
141        ctx_NC() context(lower(chardata[k].description))
142        ctx_NC() ctx_NR()
143    end
144    ctx_stoptabulate()
145end
146
147-- Handy too.
148
149function moduledata.math.coverage.showbold()
150    ctx_starttabulate { "|lT|cm|lT|cm|lT|" }
151    for k, v in sortedhash(mathematics.boldmap) do
152        ctx_NC() context("%U",k)
153        ctx_NC() context("%c",k)
154        ctx_NC() context("%U",v)
155        ctx_NC() context("%c",v)
156        ctx_NC() context(chardata[k].description)
157        ctx_NC() ctx_NR()
158    end
159    ctx_stoptabulate()
160end
161
162-- function moduledata.math.coverage.showentities()
163--     ctx_startmixedcolumns { balance = "yes" }
164--     ctx_starttabulate { "|Tl|c|Tl|" }
165--     for k, v in sortedhash(characters.entities) do
166--         local b = utf.byte(v)
167--         local d = chardata[b]
168--         local m = d.mathname
169--         local c = d.contextname
170--         local s = ((m and "\\"..m) or (c and "\\".. c) or v) .. "{}{}{}"
171--         ctx_NC()
172--         context("%U",b)
173--         ctx_NC()
174--         ctx_mathematics(s)
175--         ctx_NC()
176--         context(k)
177--         ctx_NC()
178--         ctx_NR()
179--     end
180--     ctx_stoptabulate()
181--     ctx_stopmixedcolumns()
182-- end
183
184function moduledata.math.coverage.showentities()
185    ctx_startmixedcolumns { balance = "yes" }
186    ctx_starttabulate { "|T||T|T|" }
187    for k, v in sortedhash(characters.entities) do
188        local d = chardata[v]
189        if d then
190            local m = d.mathclass or d.mathspec
191            local u = d.unicodeslot
192            ctx_NC() context(m and "m" or "t")
193            ctx_NC() ctx_getglyph("MathRoman",u)
194            ctx_NC() context("%05X",u)
195            ctx_NC() context(k)
196            ctx_NC() ctx_NR()
197        end
198    end
199    ctx_stoptabulate()
200    ctx_stopmixedcolumns()
201end
202
203