font-imp-braille.lmt /size: 4407 b    last modification: 2025-02-21 11:03
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
74-- should be (test first)
75--
76-- local function initialize(tfmdata,feature,value)
77--     if feature == "braille" and value then
78
79local function initialize(tfmdata,key,value)
80    if value then
81        if not braille then
82            require("char-brl.lmt")
83            braille = characters.braille
84        end
85        callback.register("math_rule", braille_math_rule)
86        local characters = tfmdata.characters
87        local numberdata = characters[braille.special("number")]
88        if numberdata then
89            local ismath  = value ~= "text" and (value == "math" or tfmdata.mathparameters)
90            local list    = ismath and braille.mathlist()  or braille.textlist()
91            local unicode = ismath and braille.mathunicode or braille.textunicode
92            local width   = numberdata.width
93            local height  = numberdata.height
94            local depth   = numberdata.depth
95            for i=1,#list do
96                local u = list[i]
97                local t = unicode(u)
98                local d = characters[u]
99                local w = 0
100                local c = { }
101                local n = #t
102                for i=1,n do
103                    local ub = t[i]
104                    local db = characters[ub]
105                    if db then
106                        c[i] = cache[w][ub]
107                        w = w + width
108                    end
109                end
110                if d then
111                    d.width    = n * width
112                    d.height   = height
113                    d.depth    = depth
114                    d.commands = c
115                    d.smaller  = nil
116                else
117                    characters[u] = {
118                        width    = n * width,
119                        height   = height,
120                        depth    = depth,
121                        commands = c,
122                        unicode  = u,
123                    }
124                end
125            end
126            characters[0xAD] = characters[45]
127        end
128    end
129end
130
131local specification = {
132    name        = "braille",
133    description = "braille",
134    manipulators = {
135        base = initialize,
136        node = initialize,
137    }
138}
139
140fonts.handlers.otf.features.register(specification)
141