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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
if not modules then modules = { } end modules ['s-languages-hyphenation'] = {
version = 1.001,
comment = "companion to s-languages-hyphenation.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
moduledata.languages = moduledata.languages or { }
moduledata.languages.hyphenation = moduledata.languages.hyphenation or { }
local a_colormodel = attributes.private('colormodel')
local tex = tex
local context = context
local nodecodes = nodes.nodecodes
local nuts = nodes.nuts
local nodepool = nuts.pool
local disc_code = nodecodes.disc
local glyph_code = nodecodes.glyph
local emwidths = fonts.hashes.emwidths
local exheights = fonts.hashes.exheights
local newkern = nodepool.kern
local newrule = nodepool.rule
local newglue = nodepool.glue
local insert_node_after = nuts.insert_after
local nextglyph = nuts.traversers.glyph
local tonut = nodes.tonut
local tonode = nodes.tonode
local getid = nuts.getid
local getnext = nuts.getnext
local getdisc = nuts.getdisc
local getattr = nuts.getattr
local getfont = nuts.getfont
local getfield = nuts.getfield
local getlang = nuts.getlang
local setlang = nuts.setlang
local setlink = nuts.setlink
local setdisc = nuts.setdisc
local setfield = nuts.setfield
local free_node = nuts.free
local tracers = nodes.tracers
local colortracers = tracers and tracers.colors
local setnodecolor = colortracers.set
-- maybe this will become code code
local states = table.setmetatableindex(function(t,k)
return {
lefthyphenmin = tex.lefthyphenmin,
righthyphenmin = tex.righthyphenmin,
hyphenationmin = tex.hyphenationmin,
prehyphenchar = tex.prehyphenchar,
posthyphenchar = tex.posthyphenchar,
}
end)
interfaces.implement {
name = "storelanguagestate",
actions = function()
states[tex.language] = {
lefthyphenmin = tex.lefthyphenmin,
righthyphenmin = tex.righthyphenmin,
hyphenationmin = tex.hyphenationmin,
prehyphenchar = tex.prehyphenchar,
posthyphenchar = tex.posthyphenchar,
}
end
}
function moduledata.languages.getstate(l)
return states[l] -- code
end
-- end
local function identify(head,marked)
local current = tonut(head)
local prev = nil
while current do
local id = getid(current)
local next = getnext(current)
if id == disc_code then
if prev and next then -- asume glyphs
marked[#marked+1] = prev
local pre, post, replace, pre_tail, post_tail, replace_tail = getdisc(current,true)
if replace then
setlink(prev,replace)
setlink(replace_tail,next)
setdisc(pre,post,nil)
prev = tail
else
setlink(prev,next)
end
free_node(current)
end
elseif id == glyph_code then
prev = current
else
prev = nil
end
current = next
end
end
local function mark(head,marked,w,h,d,how)
head = tonut(head)
for i=1,#marked do
local current = marked[i]
local font = getfont(current)
local em = emwidths[font]
local ex = exheights[font]
local width = w*em
local rule = newrule(width,h*ex,d*ex)
head, current = insert_node_after(head,current,newkern(-width/2))
head, current = insert_node_after(head,current,rule)
head, current = insert_node_after(head,current,newkern(-width/2))
head, current = insert_node_after(head,current,newglue(0))
setnodecolor(rule,how) -- ,getattr(current,a_colormodel))
end
end
local function getlanguage(head,l,left,right)
local t = { }
for n in nextglyph, tonut(head) do
t[n] = {
getlang(n),
getfield(n,"left"),
getfield(n,"right"),
}
end
end
local langs = { }
local tags = { }
local noflanguages = 0
local colorbytag = false
function moduledata.languages.hyphenation.showhyphens(head)
if noflanguages > 0 then
local marked = { }
local cached = { }
-- somehow assigning -1 fails
for n in nextglyph, tonut(head) do
cached[n] = {
getlang(n),
getfield(n,"left"),
getfield(n,"right")
}
end
for i=1,noflanguages do
local m = { }
local l = langs[i]
local s = states[l]
marked[i] = m
local lmin = s.lefthyphenmin
local rmin = s.righthyphenmin
for n in next, cached do
setlang(n,l)
setfield(n,"left",lmin)
setfield(n,"right",rmin)
end
languages.hyphenators.methods.original(head)
identify(head,m)
end
for i=noflanguages,1,-1 do
local l = noflanguages - i + 1
mark(head,marked[i],1/16,l/2,l/4,"hyphenation:"..(colorbytag and tags[i] or i))
end
for n, d in next, cached do
setlang(n,d[1])
setfield(n,"left",d[2])
setfield(n,"right",d[3])
end
return head, true
else
return head, false
end
end
function moduledata.languages.hyphenation.startcomparepatterns(list)
if list and list ~= "" then
tags = utilities.parsers.settings_to_array(list)
end
noflanguages = #tags
context.begingroup()
for i=1,noflanguages do
langs[i] = tags[i] and languages.getnumber(tags[i])
context.language{tags[i]}
end
context.endgroup()
nodes.tasks.enableaction("processors","moduledata.languages.hyphenation.showhyphens")
end
function moduledata.languages.hyphenation.stopcomparepatterns()
noflanguages = 0
nodes.tasks.disableaction("processors","moduledata.languages.hyphenation.showhyphens")
end
function moduledata.languages.hyphenation.showcomparelegend(list)
if list and list ~= "" then
tags = utilities.parsers.settings_to_array(list)
end
for i=1,#tags do
if i > 1 then
context.enspace()
end
context.color( { "hyphenation:"..(colorbytag and tags[i] or i) }, tags[i])
end
end
nodes.tasks.appendaction("processors","before","moduledata.languages.hyphenation.showhyphens")
nodes.tasks.disableaction("processors","moduledata.languages.hyphenation.showhyphens")
|