Debian Codegraph @

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

raw
1 % Modify pre_linebreak_filter callback so the spaces can be inserted
2 \directlua{%
3 luavlna = require "luavlna"
4 langno = require "luavlna-langno"
5
6 require "ltluatex"
7 luatexbase.add_to_callback("pre_linebreak_filter", luavlna.preventsingle,"LuaVlna")
8 luatexbase.add_to_callback("hyphenate", luavlna.split_hyphens, "allow hyphen breaks")
9 % -- define the attribute number
10 luavlna.preventsingleid = math.random(2^16)
11
12 local languages = langno.load_languages()
13 local get_lang_id = function(lang)
14 local langid = tonumber(lang)
15 if not tonumber(lang) then
16 langid = languages:get_number(lang)
17 end
18 return langid
19 end
20
21 % Process string and make table of enabled single letters
22 % By default, spaces for all single letters are inserted
23 % This can be modified with \singlechars macro
24 set_singlechars = function(lang,chars)
25 % local langnumbers = languages.names
26 local langid = get_lang_id(lang)
27 if not langid then return nil, "Cannot find language number for: "..lang end
28 local chars = chars or ""
29 local singlechars = {}
30 local percent = string.char(37)
31 local alpha = percent .. "a"
32 for pos, char in utf8.codes(chars) do
33 singlechars[utf8.char(char)] = true
34 end
35 luavlna.singlechars(langid,singlechars)
36 end
37
38 % set compound characters for initials handling
39 % useful for Czech "Ch" etc.
40 % comma separated list of compounds
41 set_compounds = function(lang, compounds)
42 local langid = get_lang_id(lang)
43 if not langid then return nil, "Cannot find language number for: "..lang end
44 local c = compounds:explode(",+") % match multiple colons as one
45 local compoundstable = {}
46 for _, compound in pairs(c) do
47 compoundstable[compound] = true
48 end
49 luavlna.initials(langid, compoundstable)
50 end
51
52 set_main_language = function(lang)
53 local langid = get_lang_id(lang)
54 if not langid then return nil, "Cannot find language number for: "..lang end
55 luavlna.set_main_language(langid)
56 end
57
58 enable_split_hyphens = function(lang)
59 local langid = get_lang_id(lang)
60 if not langid then return nil, "Cannot find language number for: "..lang end
61 luavlna.split_hyphen_langs[langid] = true
62 end
63
64 disable_split_hyphens = function(lang)
65 local langid = get_lang_id(lang)
66 if not langid then return nil, "Cannot find language number for: "..lang end
67 luavlna.split_hyphen_langs[langid] = nil
68 end
69 }
70
71 % the langno library supports both numeric and literal names
72 % we can try to find the language ID from macros first, if it fails
73 % we pass the language name to Lua. it will try to find ID in luanguage.dat and other sources
74 \def\singlecharsgetlang#1{%
75 \ifcsname l@#1\endcsname%
76 \the\csname l@#1\endcsname%
77 \else%
78 \ifcsname lang@#1\endcsname%
79 \the\csname lang@#1\endcsname%
80 \else%
81 % unknown language macro, let langno library to find it
82 #1%
83 \fi%
84 \fi%
85 }
86
87 % Set letters which are prevented from breaking
88 \def\singlechars#1#2{%
89 \directlua{set_singlechars("\singlecharsgetlang{#1}","#2")}%
90 }
91
92 % Define compound initials
93 \def\compoundinitials#1#2{%
94 \directlua{set_compounds("\singlecharsgetlang{#1}","#2")}%
95 }
96 % Enable inserting of visual marks for debugging
97 \def\preventsingledebugon{%
98 \directlua{luavlna.debug(true)}%
99 }
100
101 \def\preventsinglelang#1{%
102 \directlua{set_main_language("\singlecharsgetlang{#1}")}%
103 }
104
105 %\newluatexattribute\preventsinglestatus
106 \attributedef\preventsinglestatus=\directlua{tex.print(luavlna.preventsingleid)} % just a random number
107
108 \def\preventsingleon{%
109 \preventsinglestatus=2
110 }
111
112 \def\preventsingleoff{%
113 \preventsinglestatus=1
114 }
115 % Disable inserting of visual marks for dewbugging
116 \def\preventsingledebugoff{%
117 \directlua{luavlna.debug(false)}%
118 }
119
120 % enable/disable split hyphens for a language
121
122 \def\enablesplithyphens#1{\directlua{enable_split_hyphens("\singlecharsgetlang{#1}")}}
123 \def\disablesplithyphens#1{\directlua{disable_split_hyphens("\singlecharsgetlang{#1}")}}
124
125 % disable processing of units or degrees
126
127 \def\nopredegrees{\directlua{luavlna.no_predegrees = true}}
128 \def\nosufdegrees{\directlua{luavlna.no_sufdegrees = true}}
129 \def\nounits{\directlua{luavlna.no_unit = true}}
130 \def\noinitials{\directlua{luavlna.no_initials = true}}
131
132 \ifdefined\nosingledefaults\else
133 \singlechars{czech}{AaIiVvOoUuSsZzKk}
134 \singlechars{slovak}{AaIiVvOoUuSsZzKk}
135 \compoundinitials{czech}{Ch,CH}
136 \enablesplithyphens{czech}
137 \enablesplithyphens{slovak}
138 \fi
139