Debian Codegraph @

texmf-dist/tex/luatex/luavlna/luavlna.lua

raw
1 -- Module luavlna
2 -- code originally created by Patrick Gundlach
3 -- http://tex.stackexchange.com/q/27780/2891
4 -- The code was adapted for plain TeX and added some more features
5 -- 1. It is possible to turn this functionality only for some letters
6 -- 2. Code now works even for single letters after brackets etc.
7 --
8 --
9
10 local M = {}
11 -- local utf_match = unicode.utf8.match
12 local utf_char = utf8.char -- unicode.utf8.char
13 local utf_len = utf8.len -- unicode.utf8.len
14
15 local node = node or {}
16 local glue_id = node.id "glue"
17 local glyph_id = node.id "glyph"
18 local hlist_id = node.id "hlist"
19 local vlist_id = node.id "vlist"
20 local penalty_id = node.id "penalty"
21 local kern_id = node.id "kern"
22 local math_id = node.id "math"
23 local period_char = string.byte(".")
24
25 -- we must process only glues with the spaceskip or xspaceskip subtypes
26 local allowed_space_names = {spaceskip = true, xspaceskip = true, userskip = true}
27 local allowed_space_types = {}
28 local glue_subtypes = node.subtypes("glue")
29 -- convert allowed subtypes names to numbers used in nodes
30 for k,v in pairs(glue_subtypes) do
31 if allowed_space_names[v] then
32 allowed_space_types[k] = true
33 end
34 end
35
36 local allowed_kern_names = {italiccorrection=true}
37 local allowed_kern_types = {}
38 local kern_subtypes = node.subtypes("kern")
39 for k,v in pairs(kern_subtypes) do
40 if allowed_kern_names[v] then
41 allowed_kern_types[k] = true
42 end
43 end
44
45
46 local unicode_alphas = "0041-005A0061-007A00AA00B500BA00C0-00D600D8-00F600F8-02C102C6-02D102E0-02E402EC02EE0370-037403760377037A-037D03860388-038A038C038E-03A103A3-03F503F7-0481048A-05270531-055605590561-058705D0-05EA05F0-05F20620-064A066E066F0671-06D306D506E506E606EE06EF06FA-06FC06FF07100712-072F074D-07A507B107CA-07EA07F407F507FA0800-0815081A082408280840-085808A008A2-08AC0904-0939093D09500958-09610971-09770979-097F0985-098C098F09900993-09A809AA-09B009B209B6-09B909BD09CE09DC09DD09DF-09E109F009F10A05-0A0A0A0F0A100A13-0A280A2A-0A300A320A330A350A360A380A390A59-0A5C0A5E0A72-0A740A85-0A8D0A8F-0A910A93-0AA80AAA-0AB00AB20AB30AB5-0AB90ABD0AD00AE00AE10B05-0B0C0B0F0B100B13-0B280B2A-0B300B320B330B35-0B390B3D0B5C0B5D0B5F-0B610B710B830B85-0B8A0B8E-0B900B92-0B950B990B9A0B9C0B9E0B9F0BA30BA40BA8-0BAA0BAE-0BB90BD00C05-0C0C0C0E-0C100C12-0C280C2A-0C330C35-0C390C3D0C580C590C600C610C85-0C8C0C8E-0C900C92-0CA80CAA-0CB30CB5-0CB90CBD0CDE0CE00CE10CF10CF20D05-0D0C0D0E-0D100D12-0D3A0D3D0D4E0D600D610D7A-0D7F0D85-0D960D9A-0DB10DB3-0DBB0DBD0DC0-0DC60E01-0E300E320E330E40-0E460E810E820E840E870E880E8A0E8D0E94-0E970E99-0E9F0EA1-0EA30EA50EA70EAA0EAB0EAD-0EB00EB20EB30EBD0EC0-0EC40EC60EDC-0EDF0F000F40-0F470F49-0F6C0F88-0F8C1000-102A103F1050-1055105A-105D106110651066106E-10701075-1081108E10A0-10C510C710CD10D0-10FA10FC-1248124A-124D1250-12561258125A-125D1260-1288128A-128D1290-12B012B2-12B512B8-12BE12C012C2-12C512C8-12D612D8-13101312-13151318-135A1380-138F13A0-13F41401-166C166F-167F1681-169A16A0-16EA1700-170C170E-17111720-17311740-17511760-176C176E-17701780-17B317D717DC1820-18771880-18A818AA18B0-18F51900-191C1950-196D1970-19741980-19AB19C1-19C71A00-1A161A20-1A541AA71B05-1B331B45-1B4B1B83-1BA01BAE1BAF1BBA-1BE51C00-1C231C4D-1C4F1C5A-1C7D1CE9-1CEC1CEE-1CF11CF51CF61D00-1DBF1E00-1F151F18-1F1D1F20-1F451F48-1F4D1F50-1F571F591F5B1F5D1F5F-1F7D1F80-1FB41FB6-1FBC1FBE1FC2-1FC41FC6-1FCC1FD0-1FD31FD6-1FDB1FE0-1FEC1FF2-1FF41FF6-1FFC2071207F2090-209C21022107210A-211321152119-211D212421262128212A-212D212F-2139213C-213F2145-2149214E218321842C00-2C2E2C30-2C5E2C60-2CE42CEB-2CEE2CF22CF32D00-2D252D272D2D2D30-2D672D6F2D80-2D962DA0-2DA62DA8-2DAE2DB0-2DB62DB8-2DBE2DC0-2DC62DC8-2DCE2DD0-2DD62DD8-2DDE2E2F300530063031-3035303B303C3041-3096309D-309F30A1-30FA30FC-30FF3105-312D3131-318E31A0-31BA31F0-31FF3400-4DB54E00-9FCCA000-A48CA4D0-A4FDA500-A60CA610-A61FA62AA62BA640-A66EA67F-A697A6A0-A6E5A717-A71FA722-A788A78B-A78EA790-A793A7A0-A7AAA7F8-A801A803-A805A807-A80AA80C-A822A840-A873A882-A8B3A8F2-A8F7A8FBA90A-A925A930-A946A960-A97CA984-A9B2A9CFAA00-AA28AA40-AA42AA44-AA4BAA60-AA76AA7AAA80-AAAFAAB1AAB5AAB6AAB9-AABDAAC0AAC2AADB-AADDAAE0-AAEAAAF2-AAF4AB01-AB06AB09-AB0EAB11-AB16AB20-AB26AB28-AB2EABC0-ABE2AC00-D7A3D7B0-D7C6D7CB-D7FBF900-FA6DFA70-FAD9FB00-FB06FB13-FB17FB1DFB1F-FB28FB2A-FB36FB38-FB3CFB3EFB40FB41FB43FB44FB46-FBB1FBD3-FD3DFD50-FD8FFD92-FDC7FDF0-FDFBFE70-FE74FE76-FEFCFF21-FF3AFF41-FF5AFF66-FFBEFFC2-FFC7FFCA-FFCFFFD2-FFD7FFDA-FFDC"
47
48
49 -- construct table with unicode alphas for later testing
50 local alpha_tbl = {}
51 for start, finish in unicode_alphas:gmatch("(....)-(....)") do
52 for i = tonumber(start, 16), tonumber(finish, 16) do
53 alpha_tbl[utf_char(i)] = true
54 end
55 end
56
57
58
59
60 local alpha = string.char(37).."a" -- alpha class, entering
61 -- percent char directly caused error
62 local alphas = {}
63
64 -- test if x is a character
65 local match_table = function(x, chars)
66 local chars=chars or {}
67 return chars[x]
68 end
69 local singlechars = {} -- {a=true,i=true,z=true, v=true, u=true, o = true}
70
71 -- test if the next node after single char is a space or kern
72 local function is_space_after_single(nextn)
73 if nextn == nil then return false end
74 if nextn.id == glue_id then
75 return true
76 end
77 return false
78 end
79
80 local function is_kern_after_single(nextn)
81 -- handle italic correction kerning before space
82 if nextn == nil then return false end
83 local after_next = nextn.next
84 if after_next == nil then return false end
85 if nextn.id == kern_id and allowed_kern_types[nextn.subtype] and after_next.id == glue_id then
86 -- when font changes the italic correction kerning is inserted, so we need to match is as true too
87 return true
88 end
89 return false
90 end
91
92
93 local initials = {}
94
95 local getchar = function(n)
96 local real_char = n.char
97 -- test if char is valid utf8 value, return dummy value otherwise
98 if (real_char >= 57344 and real_char <= 63743) or (real_char >= 983040 and real_char <= 1048573) or real_char >= 1048576 then
99 return "a"
100 end
101 return utf_char(real_char)
102 end
103
104 local main_language = nil
105
106 -- when main_language is set, we will not use lang info in the nodes, but
107 -- main language instead
108 local get_language = function(lang)
109 return main_language or lang
110 end
111
112 local set_main_language = function(lang)
113 main_language = lang
114 end
115
116 local debug = false
117 local tex4ht = false
118 -- Enable processing only for certain letters
119 -- must be table in the {char = true, char2=true} form
120 local set_singlechars= function(lang,c)
121 if type(lang) == "table" then
122 for _,l in pairs(lang) do
123 singlechars[l] = c
124 end
125 else
126 local lang = tonumber(lang)
127 if lang then
128 singlechars[lang] = c
129 end
130 end
131 end
132
133 local set_initials = function(lang,c)
134 if type(lang) == "table" then
135 for _,l in pairs(lang) do
136 initials[l] = c
137 end
138 else
139 local lang = tonumber(lang)
140 if lang then
141 initials[lang]=c
142 end
143 end
144 end
145
146
147 local debug_tex4ht = function(head,p)
148 --[[ local w = node.new("glyph")
149 w.lang = tex.lang
150 w.font = font.current()
151 w.char = 64
152 ]]
153 --node.remove(head,node.prev(p))
154 local w = node.new("whatsit", "special")
155 w.data = "t4ht=<span style='background-color:red;width:2pt;'> </span>"
156 return w, head
157 end
158
159 local debug_node = function(head,p)
160 local w
161 if tex4ht then
162 w, head = debug_tex4ht(head,p)
163 else
164 w = node.new("whatsit","pdf_literal")
165 w.data = "q 1 0 1 RG 1 0 1 rg 0 0 m 0 5 l 2 5 l 2 0 l b Q"
166 end
167 node.insert_after(head,head,w)
168 node.insert_after(head,w,p)
169 -- return w
170 end
171
172
173 local set_debug= function(x)
174 debug = x
175 end
176
177 local set_tex4ht = function()
178 tex4ht = true
179 end
180
181 local insert_penalty = function(head)
182 local p = node.new("penalty")
183 p.penalty = 10000
184 local debug = debug or false
185 if debug then
186 local w = debug_node(head,p)
187 else
188 node.insert_after(head,head,p)
189 end
190 return head
191 end
192
193 local replace_with_thin_space = function(head)
194 local gluenode = node.new(node.id("glue"))
195 local gluespec = node.new(node.id("glue_spec"))
196 gluenode.width = tex.sp("0.2em")
197 -- gluenode.spec = gluespec
198 gluenode.next = head.next
199 gluenode.prev = head.prev
200 gluenode.next.prev = gluenode
201 gluenode.prev.next = gluenode
202 return gluenode
203 end
204
205 local is_alpha = function(c)
206 local status = alphas[c]
207 if not status then
208 status = alpha_tbl[c]
209 alphas[c] = status
210 end
211 return status
212 end
213
214 -- find whether letter is uppercase
215 local up_table = {}
216 local is_uppercase= function(c)
217 if not is_alpha(c) then return false end
218 local status = up_table[c]
219 if status ~= nil then
220 return status
221 end
222 status = unicode.utf8.upper(c) == c
223 up_table[c] = status
224 return status
225 end
226
227 local is_number = function(word)
228 return tonumber(string.sub(word, -1)) ~= nil
229 end
230
231 local init_buffer = ""
232 local is_initial = function(c, lang)
233 -- space is not initial
234 if c == " " then
235 init_buffer = ""
236 return false
237 end
238 -- look in the initals table
239 if lang then
240 -- insert the current character to the init_buffer
241 -- and try to find the string in the initial table
242 -- for the current language
243 init_buffer = init_buffer .. c
244 local tbl = initials[lang] or {}
245 if tbl[init_buffer] then return true end
246 end
247 return is_uppercase(c)
248 end
249
250 local cut_off_end_chars = function(word, dot)
251 local last = string.sub(word, -1)
252 while word ~= "" and (not dot or last ~= ".") and not is_alpha(last) do
253 word = string.sub(word, 1, -2) -- remove last char
254 last = string.sub(word, -1)
255 end
256 return word
257 end
258
259 local part_until_non_alpha = function(word)
260 for i = 1, #word do
261 local c = word:sub(i,i)
262 if not is_alpha(c) then
263 word = string.sub(word, 1, i-1)
264 break
265 end
266 end
267 return word
268 end
269
270
271 function Set (list)
272 local set = {}
273 for _, l in ipairs(list) do set[l] = true end
274 return set
275 end
276
277
278 local presi = (require "luavlna-presi")
279 local si = Set(require "luavlna-si")
280
281 local is_unit = function(word)
282 if M.no_unit==true then return false end
283 word = part_until_non_alpha(word)
284 if si[word] then
285 return true
286 end
287 for _, prefix in pairs(presi) do
288 local s, e = string.find(word, prefix)
289 if s == 1 then
290 local unit = string.sub(word, e+1)
291 if si[unit] then
292 return true
293 end
294 end
295 end
296 return false
297 end
298
299 local function handle_unit(word, anchor)
300 -- insert nbsp between numbers and units
301 word = cut_off_end_chars(word, false)
302 if is_unit(word) then
303 anchor = replace_with_thin_space(anchor)
304 insert_penalty(anchor.prev)
305 end
306 end
307
308 local predegrees = Set (require "luavlna-predegrees")
309 local sufdegrees = Set (require "luavlna-sufdegrees")
310
311 local function prevent_single_letter (head)
312 local singlechars = singlechars -- or {}
313 -- match_char matches all single letters, but this method is abbandoned
314 -- in favor of using table with enabled letters. With this method, multiple
315 -- languages are supported
316 local test_fn = match_table -- singlechars and match_table or match_char
317 local space = true
318 local init = false
319 local anchor = head
320 local wasnumber = false
321 local word = ""
322 local no_predegrees = M.no_predegrees
323 local no_sufdegrees = M.no_sufdegrees
324 local no_initials = M.no_initials
325 local in_math = false
326 local single_kern = false
327 while head do
328 local id = head.id
329 local nextn = head.next
330 if nextn == nil then break end
331 -- local skip = node.has_attribute(head, luatexbase.attributes.preventsinglestatus)
332 local skip = node.has_attribute(head, M.preventsingleid)
333 if id == math_id then
334 if head.subtype == 0 then
335 word = ""
336 in_math = true
337 else
338 in_math = false
339 if is_number(word) then wasnumber = true end
340 word = ""
341 end
342 end
343 if skip ~= 1 and not in_math then
344 if id == glue_id and allowed_space_types[head.subtype] then
345 if wasnumber then
346 if word ~= "" then
347 wasnumber = false
348 handle_unit(word, anchor)
349 end
350 elseif is_number(word) then
351 wasnumber = true
352 elseif single_kern then
353 insert_penalty(head.prev)
354 else
355 word = cut_off_end_chars(word, true)
356 if no_predegrees ~= true and predegrees[word] then
357 insert_penalty(head.prev)
358 elseif no_sufdegrees ~= true and sufdegrees[word] then
359 insert_penalty(anchor.prev)
360 end
361 end
362 space=true
363 anchor = head
364 word = ""
365 init = is_initial " " -- reset initials
366 single_kern = false
367 elseif space==true and id == glyph_id and is_alpha(getchar(head)) then -- a letter
368 local lang = get_language(head.lang)
369 local char = getchar(head)
370 word = char
371 init = is_initial(char,lang)
372 single_kern = false
373 local s = singlechars[lang] or {} -- load singlechars for node's lang
374 if test_fn(char, s) then
375 if is_space_after_single(nextn) then -- only if we are at a one letter word
376 head = insert_penalty(head)
377 elseif is_kern_after_single(nextn) then
378 -- if there is an italic correction after single letter, we should prevent spaces too
379 single_kern = true
380 end
381 end
382 space = false
383 -- handle initials
384 -- uppercase letter followed by period (code 46)
385 elseif no_initials~=true and init and head.id == glyph_id and head.char == period_char and nextn.id == glue_id then
386 head = insert_penalty(head)
387 elseif head.id == glyph_id then
388 local char = getchar(head)
389 word = word .. char
390 init = is_initial(char, head.lang)
391 single_kern = false
392 -- hlist support
393 elseif head.id == hlist_id then
394 prevent_single_letter(head.head)
395 -- vlist support
396 elseif head.id == vlist_id then
397 prevent_single_letter(head.head)
398 elseif head.id == penalty_id then
399 word = ""
400 end
401 elseif id == glyph_id and in_math then
402 word = word .. getchar(head)
403 end
404 head = head.next
405 end
406 -- insert spaces at the end of node list
407 if wasnumber then
408 handle_unit(word, anchor)
409 end
410 return true
411 end
412
413 local hyphenate_langs = {}
414 local hyphenchar = string.byte("-")
415 -- don't break if the next or previous char is hyphen, in order to support ligaturing of dashes
416 local is_hyphen_ligature = function(n)
417 local next_node = n.next
418 -- split_hyphens code
419 if next_node and next_node.id == glyph_id and next_node.char == hyphenchar then
420 return true
421 end
422 local prev_node = n.prev
423 if prev_node and prev_node.id == glyph_id and prev_node.char == hyphenchar then
424 return true
425 end
426 return false
427 end
428
429 -- detect if the current character is a hyphen
430 local function is_breakable_hyphen(n)
431 return n.id == glyph_id and hyphenate_langs[n.lang] and n.char == hyphenchar and n.next
432 end
433
434 -- don't process hyphens if the next node is penalty
435 -- necessary to support \nobreakdash
436 local function is_next_penalty(n)
437 local next_node = n.next
438 if next_node and next_node.id == penalty_id then
439 return true
440 end
441 end
442
443 -- split hyphens should be active only inside words, there is no need to break
444 -- standalone hyphens
445 local function is_next_glyph(n)
446 local next_node = n.next
447 return next_node and next_node.id == glyph_id
448 end
449
450
451 -- Enable hyphenation of words that contain hyphens
452 -- and repeating of the explicit hyphen on a new line when
453 -- the hyphen is hyphenated
454 -- It should be used in the `hyphenate` callback
455 -- based on ShreewatsaR's code from:
456 -- https://tex.stackexchange.com/a/417883/2891
457 local break_hyphens = function(head, tail)
458 local n = head
459 while n do
460 local skip = node.has_attribute(n, M.preventsingleid)
461 if skip ~= 1 and is_breakable_hyphen(n) and not is_next_penalty(n) and is_next_glyph(n) and not is_hyphen_ligature(n) then
462 -- Insert an infinite penalty before, and a zero-width glue node after, the hyphen.
463 -- Like writing "\nobreak-\hspace{0pt}" or equivalently "\penalty10000-\hskip0pt"
464 local p = node.new(node.id('penalty'))
465 p.penalty = 10000
466 head, p = node.insert_before(head, n, p)
467 local g = node.new(node.id('glue'))
468 head, g = node.insert_after(head, n, g)
469 -- insert the discretionary
470 local disc = node.new("disc")
471 disc.penalty = tex.hyphenpenalty
472 disc.subtype = 2
473 disc.pre = node.copy(n)
474 disc.post = node.copy(n)
475 disc.replace = node.copy(n)
476 node.insert_before(head, g, disc)
477 -- insert another penalty
478 node.insert_before(head, g, node.copy(p))
479 node.remove(head,n)
480 n = g
481 end
482 n = n.next
483 end
484 lang.hyphenate(head, tail)
485 end
486
487 M.preventsingle = prevent_single_letter
488 M.singlechars = set_singlechars
489 M.initials = set_initials
490 M.set_tex4ht = set_tex4ht
491 M.debug = set_debug
492 M.set_main_language = set_main_language
493 M.split_hyphen_langs = hyphenate_langs
494 M.split_hyphens = break_hyphens
495 return M