node-acc.lua /size: 5529 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
node-acc
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to node-ini.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 9
local
nodes
,
node
=
nodes
,
node
10 11
local
tasks
=
nodes
.
tasks
12 13
local
nuts
=
nodes
.
nuts
14
local
tonut
=
nodes
.
tonut
15
local
tonode
=
nodes
.
tonode
16 17
local
getid
=
nuts
.
getid
18
local
getsubtype
=
nuts
.
getsubtype
19
local
getattr
=
nuts
.
getattr
20
local
getlist
=
nuts
.
getlist
21
local
getchar
=
nuts
.
getchar
22
local
getnext
=
nuts
.
getnext
23 24
local
setattr
=
nuts
.
setattr
25
local
setlink
=
nuts
.
setlink
26
local
setchar
=
nuts
.
setchar
27
local
setsubtype
=
nuts
.
setsubtype
28
local
getwidth
=
nuts
.
getwidth
29
local
setwidth
=
nuts
.
setwidth
30 31
local
nextglyph
=
nuts
.
traversers
.
glyph
32
local
nextnode
=
nuts
.
traversers
.
node
33 34
----- copy_node = nuts.copy
35
local
insert_after
=
nuts
.
insert_after
36
local
copy_no_components
=
nuts
.
copy_no_components
37 38
local
nodecodes
=
nodes
.
nodecodes
39
local
gluecodes
=
nodes
.
gluecodes
40 41
local
glue_code
=
nodecodes
.
glue
42
----- kern_code = nodecodes.kern
43
local
glyph_code
=
nodecodes
.
glyph
44
local
hlist_code
=
nodecodes
.
hlist
45
local
vlist_code
=
nodecodes
.
vlist
46 47
local
userskip_code
=
gluecodes
.
user
48
local
spaceskip_code
=
gluecodes
.
spaceskip
49
local
xspaceskip_code
=
gluecodes
.
xspaceskip
50 51
local
a_characters
=
attributes
.
private
(
"
characters
"
)
52 53
local
nofreplaced
=
0
54 55
-- todo: nbsp etc
56
-- todo: collapse kerns (not needed, backend does this)
57
-- todo: maybe cache as we now create many nodes
58
-- todo: check for subtype related to spacing (13/14 but most seems to be user anyway)
59 60
local
trace
=
false
trackers
.
register
(
"
backend.spaces
"
,
function
(
v
)
trace
=
v
end
)
61
local
slot
=
nil
62 63
local
function
injectspaces
(
head
)
64
local
p
,
p_id
65
local
n
=
head
66
while
n
do
67
local
id
=
getid
(
n
)
68
if
id
=
=
glue_code
then
69
if
p
and
getid
(
p
)
=
=
glyph_code
then
70
local
s
=
getsubtype
(
n
)
71
if
s
=
=
spaceskip_code
or
s
=
=
xspaceskip_code
then
72
-- unless we don't care about the little bit of overhead
73
-- we can just: local g = copy_node(g)
74
local
g
=
copy_no_components
(
p
)
75
local
a
=
getattr
(
n
,
a_characters
)
76
setchar
(
g
,
slot
)
77
setlink
(
p
,
g
,
n
)
78
setwidth
(
n
,
getwidth
(
n
)
-
getwidth
(
g
)
)
79
-- setsubtype(n,userskip_code)
80
if
a
then
81
setattr
(
g
,
a_characters
,
a
)
82
end
83
setattr
(
n
,
a_characters
,
0
)
84
nofreplaced
=
nofreplaced
+
1
85
end
86
end
87
elseif
id
=
=
hlist_code
or
id
=
=
vlist_code
then
88
injectspaces
(
getlist
(
n
)
,
slot
)
89
end
90
p_id
=
id
91
p
=
n
92
n
=
getnext
(
n
)
93
end
94
return
head
95
end
96 97
nodes
.
handlers
.
accessibility
=
function
(
head
)
98
if
trace
then
99
if
not
slot
then
100
slot
=
fonts
.
helpers
.
privateslot
(
"
visualspace
"
)
101
end
102
else
103
slot
=
32
104
end
105
return
injectspaces
(
head
,
slot
)
106
end
107 108
statistics
.
register
(
"
inserted spaces in output
"
,
function
(
)
109
if
nofreplaced
>
0
then
110
return
nofreplaced
111
end
112
end
)
113 114
-- todo:
115 116
-- local a_hyphenated = attributes.private('hyphenated')
117
--
118
-- local hyphenated, codes = { }, { }
119
--
120
-- local function compact(n)
121
-- local t = { }
122
-- for n in nextglyph, n do
123
-- t[#t+1] = utfchar(getchar(n)) -- check for unicode
124
-- end
125
-- return concat(t,"")
126
-- end
127
--
128
-- local function injectspans(head)
129
-- local done = false
130
-- for n, id in nextnode, tonuts(head) do
131
-- if id == disc then
132
-- local r = getfield(n,"replace")
133
-- local p = getfield(n,"pre")
134
-- if r and p then
135
-- local str = compact(r)
136
-- local hsh = hyphenated[str]
137
-- if not hsh then
138
-- hsh = #codes + 1
139
-- hyphenated[str] = hsh
140
-- codes[hsh] = str
141
-- end
142
-- setattr(n,a_hyphenated,hsh)
143
-- done = true
144
-- end
145
-- elseif id == hlist_code or id == vlist_code then
146
-- injectspans(getlist(n))
147
-- end
148
-- end
149
-- return tonodes(head), done
150
-- end
151
--
152
-- nodes.injectspans = injectspans
153
--
154
-- tasks.appendaction("processors", "words", "nodes.injectspans")
155
--
156
-- local pageliteral = nuts.pool.pageliteral
157
--
158
-- local function injectspans(head)
159
-- local done = false
160
-- for n, id in nextnode, tonut(head) do
161
-- if id == disc then
162
-- local a = getattr(n,a_hyphenated)
163
-- if a then
164
-- local str = codes[a]
165
-- local b = pageliteral(format("/Span << /ActualText %s >> BDC", lpdf.tosixteen(str)))
166
-- local e = pageliteral("EMC")
167
-- insert_before(head,n,b)
168
-- insert_after(head,n,e)
169
-- done = true
170
-- end
171
-- elseif id == hlist_code or id == vlist_code then
172
-- injectspans(getlist(n))
173
-- end
174
-- end
175
-- return tonodes(head), done
176
-- end
177