font-imp-braille.lmt /size: 4275 b    last modification: 2024-01-16 10:22
1if not modules then modules = { } end modules ['font-imp-braille'] = {
2    version   = 1.001,
3    comment   = "companion to font-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA ADE",
5    copyright = "ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9if not context then return end
10
11-- require("char-brl.lmt")         -- we delay this one
12
13-- we can omit spaces and tweak the math parameters instead
14
15-- callback.register("math_rule", function(kind,font,width,height,attr)
16--     return nodes.tonode(nodes.nuts.pool.rule(width,5*height,5*height))
17-- end)
18
19local braille = characters.braille -- so this one is nil
20
21local braille_math_rule  do
22
23    local nuts         = nodes.nuts
24    local tonut        = nodes.tonut
25    local tonode       = nodes.tonode
26    local nodepool     = nuts.pool
27    local setattrlist  = nuts.setattrlist
28    local hpack        = nuts.hpack
29 -- local getwhd       = nuts.getwhd
30 -- local setwhd       = nuts.setwhd
31 -- local setoffsets   = nuts.setoffsets
32    local newmathglyph = nuts.newmathglyph
33    local newleader    = nodepool.leader
34
35    local braillerule
36
37 -- function(kind,font,width,height,attr)
38 --     return nodes.tonode(nodes.nuts.pool.rule(width,5*height,5*height))
39 -- end
40
41    braille_math_rule = function(kind,font,width,height,attr)
42        if not braillerule then
43            braillerule = braille.special("rule")
44        end
45        local attlst = tonut(attr)
46        local glyph  = newmathglyph(font,braillerule,attlst)
47        local box    = hpack(glyph)
48        --
49     -- local w, h, d = getwhd(box)
50     -- local t = (h + d) / 2
51     -- setwhd(box,w,t,t)
52     -- setoffsets(glyph,0,-t) -- setting the offset of the box messes up (visual) things
53        --
54        local leader = newleader(width,box)
55        local result = hpack(leader,width,"exactly")
56        setattrlist(leader,attlst)
57        return tonode(result)
58    end
59
60end
61
62local setmetatableindex = table.setmetatableindex
63
64local cache = table.setmetatableindex(function(t,w)
65    local v =  table.setmetatableindex(function(t,u)
66        local v = { "offset", w, 0, u }
67        t[u] = v
68        return v
69    end)
70    t[w] = v
71    return v
72end)
73
74local function initialize(tfmdata,value)
75    if value then
76        if not braille then
77            require("char-brl.lmt")
78            braille = characters.braille
79        end
80        callback.register("math_rule", braille_math_rule)
81        local characters = tfmdata.characters
82        local numberdata = characters[braille.special("number")]
83        if numberdata then
84            local ismath  = value ~= "text" and (value == "math" or tfmdata.mathparameters)
85            local list    = ismath and braille.mathlist()  or braille.textlist()
86            local unicode = ismath and braille.mathunicode or braille.textunicode
87            local width   = numberdata.width
88            local height  = numberdata.height
89            local depth   = numberdata.depth
90            for i=1,#list do
91                local u = list[i]
92                local t = unicode(u)
93                local d = characters[u]
94                local w = 0
95                local c = { }
96                local n = #t
97                for i=1,n do
98                    local ub = t[i]
99                    local db = characters[ub]
100                    if db then
101                        c[i] = cache[w][ub]
102                        w = w + width
103                    end
104                end
105                if d then
106                    d.width    = n * width
107                    d.height   = height
108                    d.depth    = depth
109                    d.commands = c
110                    d.smaller  = nil
111                else
112                    characters[u] = {
113                        width    = n * width,
114                        height   = height,
115                        depth    = depth,
116                        commands = c,
117                        unicode  = u,
118                    }
119                end
120            end
121            characters[0xAD] = characters[45]
122        end
123    end
124end
125
126local specification = {
127    name        = "braille",
128    description = "braille",
129    manipulators = {
130        base = initialize,
131        node = initialize,
132    }
133}
134
135fonts.handlers.otf.features.register(specification)
136