| 1 |
-- Credit: https://gist.github.com/phi-gamma/2622252 |
| 2 |
|
| 3 |
packagedata = packagedata or { } -- namespace proposal for packages |
| 4 |
|
| 5 |
dofile(kpse.find_file"char-def.lua") -- unicode tables |
| 6 |
dofile(kpse.find_file"lualibs-table.lua") -- old Context table code |
| 7 |
|
| 8 |
local utf = unicode.utf8 |
| 9 |
local node = node |
| 10 |
local type = type |
| 11 |
|
| 12 |
local lower, utfchar, utfvalues = string.lower, utf.char, string.utfvalues |
| 13 |
local tableconcat, iowrite = table.concat, io.write |
| 14 |
local stringformat, texprint = string.format, tex.print |
| 15 |
|
| 16 |
local traverse_nodes = node.traverse |
| 17 |
local chardata = characters.data |
| 18 |
|
| 19 |
local glyph_code = node.id"glyph" |
| 20 |
local disc_code = node.id"disc" |
| 21 |
local kern_code = node.id"kern" |
| 22 |
local kerning_code = 0 -- from font |
| 23 |
|
| 24 |
-- * LaTeX counters interface |
| 25 |
|
| 26 |
local latex = latex or {} |
| 27 |
|
| 28 |
-- This reflects the standard Lua(La)TeX way |
| 29 |
-- Credit: https://tex.stackexchange.com/a/330403/238079 |
| 30 |
latex.count = {} |
| 31 |
setmetatable(latex.count, {__index = function(t, counter) |
| 32 |
return tex.count['c@' .. counter] |
| 33 |
end} ) |
| 34 |
|
| 35 |
function latex.getcount (counter) |
| 36 |
return tex.getcount('c@' .. counter) |
| 37 |
end |
| 38 |
|
| 39 |
function latex.setcount (counter, value) |
| 40 |
return tex.setcount('c@' .. counter, value) |
| 41 |
end |
| 42 |
|
| 43 |
function latex.inccount (counter) |
| 44 |
return latex.setcount(counter, latex.count[counter] + 1) |
| 45 |
end |
| 46 |
|
| 47 |
-- * Global wordcount state |
| 48 |
|
| 49 |
-- This table holds the counters we are counting into. Keys are names |
| 50 |
-- of LaTeX counters, values are true (we are counting into it) or |
| 51 |
-- false (we aren't). |
| 52 |
local counters = {} |
| 53 |
|
| 54 |
-- Least number of of characters required to count as a word. |
| 55 |
local threshold = 1 |
| 56 |
|
| 57 |
function enable_counter (counter) |
| 58 |
counters[counter] = true |
| 59 |
end |
| 60 |
|
| 61 |
function disable_counter (counter) |
| 62 |
counters[counter] = false |
| 63 |
end |
| 64 |
|
| 65 |
function set_threshold (n) |
| 66 |
if n then |
| 67 |
threshold = n |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
-- * Counting |
| 72 |
|
| 73 |
local is_letter = table.tohash { "ll", "lm", "lo", "lt", "lu" } |
| 74 |
|
| 75 |
local charcache = { } --- memo without metatable |
| 76 |
local lcchar = function(code) |
| 77 |
if code then |
| 78 |
if charcache[code] then return charcache[code] end |
| 79 |
local c = chardata[code] |
| 80 |
c = c and c.lccode |
| 81 |
if c then --utfstring |
| 82 |
if type(c) == "table" then |
| 83 |
c = utfchar(unpack(c)) |
| 84 |
else |
| 85 |
c = utfchar(c) |
| 86 |
end |
| 87 |
else |
| 88 |
if type(code) == "number" then |
| 89 |
c = utfchar(code) |
| 90 |
else |
| 91 |
c = code |
| 92 |
end |
| 93 |
end |
| 94 |
charcache[code] = c |
| 95 |
return c |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
local lowerchar = function (str) |
| 100 |
local new, n = { }, 0 |
| 101 |
for val in utfvalues(str) do |
| 102 |
n = n + 1 |
| 103 |
new[n] = lcchar(val) -- could be inlined here as well .. |
| 104 |
end |
| 105 |
return tableconcat(new) |
| 106 |
end |
| 107 |
|
| 108 |
local function mark_words (head, whenfound) |
| 109 |
local current, done = head, nil, 0, false |
| 110 |
local str, s, nds, n = { }, 0, { }, 0 |
| 111 |
local function action() |
| 112 |
if s > 0 then |
| 113 |
local word = tableconcat(str, "", 1, s) |
| 114 |
local mark = whenfound(word) |
| 115 |
if mark then |
| 116 |
done = true |
| 117 |
for i=1,n do |
| 118 |
mark(nds[i]) |
| 119 |
end |
| 120 |
end |
| 121 |
end |
| 122 |
n, s = 0, 0 |
| 123 |
end |
| 124 |
while current do -- iterate |
| 125 |
local id = current.id |
| 126 |
if id == glyph_code then |
| 127 |
local components = current.components |
| 128 |
if components then |
| 129 |
n = n + 1 |
| 130 |
nds[n] = current |
| 131 |
for g in traverse_nodes(components) do |
| 132 |
s = s + 1 |
| 133 |
str[s] = utfchar(g.char) |
| 134 |
end |
| 135 |
else |
| 136 |
local code = current.char |
| 137 |
local data = chardata[code] |
| 138 |
if data and is_letter[data.category] then |
| 139 |
n = n + 1 |
| 140 |
nds[n] = current |
| 141 |
s = s + 1 |
| 142 |
str[s] = utfchar(code) |
| 143 |
elseif s > 0 then |
| 144 |
action() |
| 145 |
end |
| 146 |
end |
| 147 |
elseif id == disc_code then -- take the replace |
| 148 |
if n > 0 then |
| 149 |
n = n + 1 |
| 150 |
nds[n] = current |
| 151 |
end |
| 152 |
elseif id == kern_code and current.subtype == kerning_code and s > 0 then |
| 153 |
-- ok |
| 154 |
elseif s > 0 then |
| 155 |
action() |
| 156 |
end |
| 157 |
current = current.next |
| 158 |
end |
| 159 |
if s > 0 then |
| 160 |
action() |
| 161 |
end |
| 162 |
return head, done |
| 163 |
end |
| 164 |
|
| 165 |
local function insert_word (str) -- -Y´sweep(l,s)¡ |
| 166 |
if #str >= threshold then |
| 167 |
-- For each enabled counter... |
| 168 |
for counter, enabled in pairs(counters) do |
| 169 |
-- ...increment the counter. |
| 170 |
if enabled then latex.inccount(counter) end |
| 171 |
end |
| 172 |
end |
| 173 |
end |
| 174 |
|
| 175 |
local callback = function (head) |
| 176 |
return mark_words(head, insert_word) |
| 177 |
end |
| 178 |
|
| 179 |
-- * Export module |
| 180 |
|
| 181 |
return { |
| 182 |
latex = latex, |
| 183 |
enable_counter = enable_counter, |
| 184 |
disable_counter = disable_counter, |
| 185 |
callback = callback, |
| 186 |
set_threshold = set_threshold |
| 187 |
} |