summaryrefslogtreecommitdiff
path: root/tex/context/base/node-acc.lua
blob: fc0c5fb0fa4c7e9150e5be16e54fff8347fd3076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
if not modules then modules = { } end modules ['node-acc'] = {
    version   = 1.001,
    comment   = "companion to node-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local traverse_nodes, traverse_id, has_attribute, copy_node = node.traverse, node.traverse_id, node.has_attribute, node.copy

nodes.accessibility = nodes.accessibility or { }

local nodecodes = nodes.nodecodes

local glue  = nodecodes.glue
local glyph = nodecodes.glyph
local hlist = nodecodes.hlist
local vlist = nodecodes.vlist

local function injectspaces(head)
    local p
    for n in traverse_nodes(head) do
        local id = n.id
        if id == glue then -- todo: check for subtype related to spacing (13/14 but most seems to be 0)
         -- local at = has_attribute(n,attribute)
         -- if at then
                if p and p.id == glyph then
                    local g = copy_node(p)
                    local s = copy_node(n.spec)
                    g.char, n.spec = 32, s
                    p.next, g.prev = g, p
                    g.next, n.prev = n, g
                    s.width = s.width - g.width
                end
         -- end
        elseif id == hlist or id == vlist then
            injectspaces(n.list,attribute)
        end
        p = n
    end
    return head, true
end

nodes.accessibility.handler = injectspaces

-- nodes.accessibility.handler(tex.box[255].list)

-- todo:

--~ local a_hyphenated = attributes.private('hyphenated')
--~
--~ local hyphenated, codes = { }, { }
--~
--~ local function compact(n)
--~     local t = { }
--~     for n in traverse_id(glyph,n) do
--~         t[#t+1] = utfchar(n.char) -- check for unicode
--~     end
--~     return concat(t,"")
--~ end
--~
--~ local function injectspans(head)
--~     for n in traverse_nodes(head) do
--~         local id = n.id
--~         if id == disc then
--~             local r, p = n.replace, n.pre
--~             if r and p then
--~                 local str = compact(r)
--~                 local hsh = hyphenated[str]
--~                 if not hsh then
--~                     hsh = #codes + 1
--~                     hyphenated[str] = hsh
--~                     codes[hsh] = str
--~                 end
--~                 set_attribute(n,a_hyphenated,hsh)
--~             end
--~         elseif id == hlist or id == vlist then
--~             injectspans(n.list)
--~         end
--~     end
--~     return head, true
--~ end
--~
--~ nodes.injectspans = injectspans
--~
--~ tasks.appendaction("processors", "words", "nodes.injectspans")
--~
--~ local function injectspans(head)
--~     for n in traverse_nodes(head) do
--~         local id = n.id
--~         if id == disc then
--~             local a = has_attribute(n,a_hyphenated)
--~             if a then
--~                 local str = codes[a]
--~                 local b = new_pdfliteral(format("/Span << /ActualText %s >> BDC", lpdf.tosixteen(str)))
--~                 local e = new_pdfliteral("EMC")
--~                 node.insert_before(head,n,b)
--~                 node.insert_after(head,n,e)
--~             end
--~         elseif id == hlist or id == vlist then
--~             injectspans(n.list)
--~         end
--~     end
--~ end