summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/lang-hup.lmt
blob: 2099f9d1b7c55a6fd4ce646efe86074cf526952b (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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
if not modules then modules = { } end modules ['lang-hup'] = {
    version   = 1.001,
    comment   = "companion to lang-hup.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type, next = type, next
local utfchar = utf.char
local concat, sortedhash = table.concat, table.sortedhash
local basename = file.basename

local status        = status
local nodes         = nodes

local is_letter     = characters.is_letter
local is_hyphenator = characters.is_hyphenator

local specialskips  = nodes.specialskipcodes
local nodecodes     = nodes.nodecodes
local disc_code     = nodecodes.disc
local glyph_code    = nodecodes.glyph
local glue_code     = nodecodes.glue
local hlist_code    = nodecodes.hlist
local kern_code     = nodecodes.kern
local par_code      = nodecodes.par
local line_code     = nodes.listcodes.line
local fontkern_code = nodes.kerncodes.fontkern
local nuts          = nodes.nuts
local getlist       = nuts.getlist
local getnext       = nuts.getnext
local getprev       = nuts.getprev
local getid         = nuts.getid
local getsubtype    = nuts.getsubtype
local getreplace    = nuts.getreplace
local getdiscpart   = nuts.getdiscpart
local isnextglyph   = nuts.isnextglyph
local nexthlist     = nuts.traversers.list
local nextglyph     = nuts.traversers.glyph
local traverse      = nuts.traverse

local setcolor      = nodes.tracers.colors.set
local setaction     = nodes.tasks.setaction

local hash          = table.setmetatableindex("number")

local report        = logs.reporter("hyphenated")
local trace_detail  = false

local characters    = fonts.hashes.characters

local word = { }
local w    = 0

----- function collect(word,head)
local function collect(head)
    local last = nil
    while head do
        local nxt, char, id = isnextglyph(head)
        if char then
            local u = characters[id][char].unicode -- we could cache it
            if type(u) == "table" then
                for i=1,#u do
                    local c = u[i]
                    if is_letter[c] or is_hyphenator[c] then
                     -- word[#word+1] = utfchar(c)
                        w = w + 1 ; word[w] = utfchar(c)
                    end
                end
            else
                local c = u or char
                if is_letter[c] or is_hyphenator[c] then
                 -- word[#word+1] = utfchar(c)
                    w = w + 1 ; word[w] = utfchar(c)
                end
            end
            last = head
        elseif id == disc_code then
         -- collect(word,getreplace(head))
            collect(getreplace(head))
        elseif id == kern_code and getsubtype(head,fontkern_code) then
            -- we're ok
        else
            break
        end
        head = nxt
    end
    return last and getdiscpart(last) == 1
end

local function getpostpart(current)
    for n, id, subtype in traverse(current) do
        if id == glue_code then
            if not specialskips[subtype] then
                break
            end
        elseif id == glyph_code or id == disc_code then
            return n
        elseif id ~= par_code then
            break
        end
    end
end

local function findprepart(current)
    for n, id, subtype in traverse(current,true,true) do
        if id == glue_code then
            if not specialskips[subtype] then
                break
            end
        elseif id == glyph_code and getdiscpart(n) == 1 then
            return n
        else
            break
        end
    end
end

local function getprepart(disc)
    local back = disc
    for n, id, subtype in traverse(getprev(disc),true) do
        if id == glyph_code or id == disc_code or (id == kern_code and subtype == fontkern_code) then
            back = n
        else
            return back
        end
    end
    return back
end

function nodes.handlers.showhyphenation(head)
 -- local word = { }
    w = 0
    for current, id, subtype, list in nexthlist, head do
        if list and subtype == line_code then
         -- if #word > 0 then
            if w > 0 then
                local after = getpostpart(list)
             -- local more  = collect(word,after)
                local more  = collect(after)
                if more then
                    goto skip
                else
                 -- local result = concat(word)
                    local result = concat(word,"",1,w)
                    if trace_detail then
                        local r = status.readstate
                        report("around line %s in file %s: %s",r.linenumber or "-",basename(r.filename),result)
                    end
                    hash[result] = hash[result] + 1
                 -- word = { }
                    w = 0
                end
            end
            local last = findprepart(list)
            if last then
                local before = getprepart(last)
             -- collect(word,before)
                collect(before)
            end
          ::skip::
        end
    end
    return head
end

local initialize  initialize = function()
    logs.registerfinalactions(function()
        logs.startfilelogging(report,"hyphenated words")
        if hash and next(hash) then
            local oldname = file.nameonly(tex.jobname) .. "-hyphenated-words-old.txt"
            local newname = file.nameonly(tex.jobname) .. "-hyphenated-words-new.txt"
            local old = string.splitlines(string.strip(io.loaddata(oldname) or "")) or { }
            local hsh = table.tohash(old)
            local new = { }
            for word, count in sortedhash(hash) do
                report("%4i : %s",count,word)
                if not hsh[word] then
                    new[#new+1] = word
                end
            end
            logs.stopfilelogging()
            report("old word list : %a",oldname)
            report("new word list : %a",newname)
            report("to be checked : %a",#new)
            io.savedata(newname,concat(new,"\n"))
        else
            report("nothing hyphenated")
            logs.stopfilelogging()
        end
    end)
    --
    statistics.register("hyphenation",function()
        local n = 0
        local m = 0
        for k, v in sortedhash(hash) do
            n = n + 1
            m = m + v
        end
        return string.format("%i hyphenated words, %i unique words",m,n)
    end)
    initialize = false
end

trackers.register("hyphenation.applied", function(v)
    setaction("finalizers","nodes.handlers.showhyphenation",v)
    if v and initialize then
        initialize()
    end
end)

trackers.register("hyphenation.applied.console", function(v)
    setaction("finalizers","nodes.handlers.showhyphenation",v)
    trace_detail = v
    if v and initialize then
        initialize()
    end
end)

-- local c, f = isglyph(current)
-- local char = chardata[f][c]
-- if char and type(char.unicode) == "table" then -- hackery test

local ligature_code = 0x8000 + nodes.glyphcodes.ligature
local ligature_mode = false

local color_n = { "red",     "green",     "blue"     }
local color_l = { "darkred", "darkgreen", "darkblue" }

function nodes.handlers.visualizehyphenation(head)
    for current, id, subtype, list in nexthlist, head do
        if list and subtype == line_code then
            if ligature_mode then
                for n in nextglyph, list do
                    local d = getdiscpart(n)
                    local s = getsubtype(n)
                    if d > 0 and d < 4 then
                        if s == ligature_code then
                            setcolor(n,color_l[d])
                        else
                            setcolor(n,color_n[d])
                        end
                    elseif s == ligature_code then
                        setcolor(n,"darkgray")
                    end
                end
            else
                for n in nextglyph, list do
                    local d = getdiscpart(n)
                    if d > 0 and d < 4 then
                        setcolor(n,color_n[d])
                    end
                end
            end
        end
    end
    return head
end

trackers.register("hyphenation.applied.visualize", { true, false, "ligatures" }, function(v)
    setaction("finalizers","nodes.handlers.visualizehyphenation",v)
    ligature_mode = v == "ligatures"
end)