s-math-coverage.lua /size: 6844 b    last modification: 2020-07-01 14:35
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                        t[mathspec[i].class] = true
108                    end
109                    t = table.sortedkeys(t)
110                    context("% t",t)
111                else
112                    context(mathclass)
113                end
114            ctx_NC()
115            ctx_NR()
116        end
117    end
118    ctx_stoptabulate()
119    ctx_stopmixedcolumns()
120end
121
122-- This is a somewhat tricky table as we need to bypass the math machinery.
123
124function moduledata.math.coverage.showscripts()
125    ctx_starttabulate { "|cT|c|cT|c|c|c|l|" }
126    for k, v in sortedhash(table.merged(superscripts,subscripts)) do
127        local ck = utfchar(k)
128        local cv = utfchar(v)
129        local ss = superscripts[k] and "^" or "_"
130        ctx_NC() context("%05X",k)
131        ctx_NC() context(ck)
132        ctx_NC() context("%05X",v)
133        ctx_NC() context(cv)
134        ctx_NC() ctx_rawmathematics("x%s = x%s%s",ck,ss,cv)
135        ctx_NC() ctx_mathematics("x%s = x%s%s",ck,ss,cv)
136        ctx_NC() context(lower(chardata[k].description))
137        ctx_NC() ctx_NR()
138    end
139    ctx_stoptabulate()
140end
141
142-- Handy too.
143
144function moduledata.math.coverage.showbold()
145    ctx_starttabulate { "|lT|cm|lT|cm|lT|" }
146    for k, v in sortedhash(mathematics.boldmap) do
147        ctx_NC() context("%U",k)
148        ctx_NC() context("%c",k)
149        ctx_NC() context("%U",v)
150        ctx_NC() context("%c",v)
151        ctx_NC() context(chardata[k].description)
152        ctx_NC() ctx_NR()
153    end
154    ctx_stoptabulate()
155end
156
157-- function moduledata.math.coverage.showentities()
158--     ctx_startmixedcolumns { balance = "yes" }
159--     ctx_starttabulate { "|Tl|c|Tl|" }
160--     for k, v in sortedhash(characters.entities) do
161--         local b = utf.byte(v)
162--         local d = chardata[b]
163--         local m = d.mathname
164--         local c = d.contextname
165--         local s = ((m and "\\"..m) or (c and "\\".. c) or v) .. "{}{}{}"
166--         ctx_NC()
167--         context("%U",b)
168--         ctx_NC()
169--         ctx_mathematics(s)
170--         ctx_NC()
171--         context(k)
172--         ctx_NC()
173--         ctx_NR()
174--     end
175--     ctx_stoptabulate()
176--     ctx_stopmixedcolumns()
177-- end
178
179function moduledata.math.coverage.showentities()
180    ctx_startmixedcolumns { balance = "yes" }
181    ctx_starttabulate { "|T||T|T|" }
182    for k, v in sortedhash(characters.entities) do
183        local d = chardata[v]
184        if d then
185            local m = d.mathclass or d.mathspec
186            local u = d.unicodeslot
187            ctx_NC() context(m and "m" or "t")
188            ctx_NC() ctx_getglyph("MathRoman",u)
189            ctx_NC() context("%05X",u)
190            ctx_NC() context(k)
191            ctx_NC() ctx_NR()
192        end
193    end
194    ctx_stoptabulate()
195    ctx_stopmixedcolumns()
196end
197
198