| 1 |
% This is the macro package used by OpTeX, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
% vlna.opm, Petr Olšák <[email protected]>, 2021 |
| 4 |
|
| 5 |
% Vlna prevents breaks in spaces after single-letter prepositions, after (or |
| 6 |
% before) degrees, and between value and SI units. It enables or disables |
| 7 |
% split hyphens too. All folks are done at the postprocessing phase before TeX splits |
| 8 |
% the typesetting material into the paragraph lines. |
| 9 |
|
| 10 |
% vlna.opm is based on Lua code from the package `luavlna` (by Michal Hoftich |
| 11 |
% and Miro Hrončok). See `texdoc luavlna` or end of this file for more information. |
| 12 |
|
| 13 |
\_codedecl \singlechars {Preventing single-chars prepositions and more <2023-06-23>} |
| 14 |
|
| 15 |
\_namespace{vlna} |
| 16 |
|
| 17 |
\_doc ----------------------------- |
| 18 |
We declare new attribute, register callbacks, and define Lua functions that |
| 19 |
point to Lua functions from `luavlna.lua` package. |
| 20 |
We need this package installed. |
| 21 |
|
| 22 |
We need not `require ltluatex` because `luatexbase.callbacks` are defined |
| 23 |
in the `optex.lua` file and it is part of the OpTeX format. |
| 24 |
\_cod ----------------------------- |
| 25 |
|
| 26 |
\_newattribute \.preventsinglestatus |
| 27 |
|
| 28 |
\_directlua{% |
| 29 |
luavlna = require "luavlna" |
| 30 |
|
| 31 |
luatexbase.add_to_callback("pre_linebreak_filter", luavlna.preventsingle,"LuaVlna") |
| 32 |
luatexbase.add_to_callback("hyphenate", luavlna.split_hyphens, "allow hyphen breaks") |
| 33 |
luavlna.preventsingleid = \_the\_attributealloc |
| 34 |
|
| 35 |
set_singlechars = function(lang,chars) |
| 36 |
if (lang == "") then return nil end |
| 37 |
local chars = chars or "" |
| 38 |
local singlechars = {} |
| 39 |
for pos, char in utf8.codes(chars) do |
| 40 |
singlechars[utf8.char(char)] = true |
| 41 |
end |
| 42 |
luavlna.singlechars(lang,singlechars) |
| 43 |
end |
| 44 |
|
| 45 |
set_compounds = function(lang, compounds) |
| 46 |
if (lang == "") then return nil end |
| 47 |
local c = compounds:explode(",+") % match multiple colons as one |
| 48 |
local compoundstable = {} |
| 49 |
for _, compound in pairs(c) do |
| 50 |
compoundstable[compound] = true |
| 51 |
end |
| 52 |
luavlna.initials(lang, compoundstable) |
| 53 |
end |
| 54 |
|
| 55 |
set_main_language = function(lang) |
| 56 |
if (lang == "") then return nil end |
| 57 |
luavlna.set_main_language(tonumber(lang)) |
| 58 |
end |
| 59 |
|
| 60 |
enable_split_hyphens = function(lang) |
| 61 |
if (lang == "") then return nil end |
| 62 |
luavlna.split_hyphen_langs[tonumber(lang)] = true |
| 63 |
end |
| 64 |
|
| 65 |
disable_split_hyphens = function(lang) |
| 66 |
if (lang == "") then return nil end |
| 67 |
luavlna.split_hyphen_langs[tonumber(lang)] = nil |
| 68 |
end |
| 69 |
} |
| 70 |
|
| 71 |
\_doc ----------------------------- |
| 72 |
We need not `require luavlna-langno` because of the conversion from |
| 73 |
language name to the `\language` number is done by the macro |
| 74 |
`\.langtonum``<sequence>={<langname>}` defined here. |
| 75 |
It does `\def<sequence>{<langnumber>}` or `\def\sequence{}` if <langname> |
| 76 |
is unknown. The <langname> is case insensitive and these names are |
| 77 |
declared in the hyphen-lan.opm file, section 2.37.2 in OpTeX documentation. |
| 78 |
\_cod ----------------------------- |
| 79 |
|
| 80 |
\_def\.langtonum#1=#2{\_lowercase{\_edef#1{\_trycs{_ulan:#2}{}}}% |
| 81 |
\_ifx#1\_empty \_opwarning{vlna: unknown language "#2", ignored}% |
| 82 |
\_else \_edef#1{\_the\_cs{_#1Patt}}\_fi} |
| 83 |
|
| 84 |
\_doc ----------------------------- |
| 85 |
`\singlechars``{<langname>}{<prepositions>}` sets <prepositions> for |
| 86 |
given language.\nl |
| 87 |
`\compoundinitials``{<langname>}{<comma-separated-list>}` sets |
| 88 |
compound characters from the list for given language. |
| 89 |
\_cod ----------------------------- |
| 90 |
|
| 91 |
\_def\.singlechars#1#2{\.langtonum\.l={#1}\_directlua{set_singlechars("\.l","#2")}} |
| 92 |
\_def\.compoundinitials#1#2{\.langtonum\.l={#1}\_directlua{set_compounds("\.l","#2")}} |
| 93 |
|
| 94 |
\_doc ----------------------------- |
| 95 |
`\enablesplithyphens` and `\disablesplithyphens` are dependent on the language. |
| 96 |
\_cod ----------------------------- |
| 97 |
|
| 98 |
\_def\.enablesplithyphens#1{\.langtonum\.l={#1}\_directlua{enable_split_hyphens("\.l")}} |
| 99 |
\_def\.disablesplithyphens#1{\.langtonum\.l={#1}\_directlua{disable_split_hyphens("\.l")}} |
| 100 |
|
| 101 |
\_doc ----------------------------- |
| 102 |
By default, the language-dependent features of the `luavlna` depend on |
| 103 |
the current `\language` value, i.e. on the currently selected hyphenation. |
| 104 |
`\preventsinglelang``{<langname>}` activates the language-dependent |
| 105 |
features regardless of the current `\language` value. |
| 106 |
\_cod ----------------------------- |
| 107 |
|
| 108 |
\_def\.preventsinglelang#1{\.langtonum\.l={#1}\_directlua{set_main_language("\.l")}} |
| 109 |
|
| 110 |
\_doc ----------------------------- |
| 111 |
Switching on/off `luavlna` preposition features should be done using attribute |
| 112 |
setting. It will allow you to switch them on/off in the middle of the paragraph. |
| 113 |
\_cod ----------------------------- |
| 114 |
|
| 115 |
\_def\.preventsingleon{\.preventsinglestatus=2 } |
| 116 |
\_def\.preventsingleoff{\.preventsinglestatus=1 } |
| 117 |
|
| 118 |
\_doc ----------------------------- |
| 119 |
Switching on/off the other `luavlna` features is implemented only by |
| 120 |
setting a special `luavlna` variable. |
| 121 |
\_cod ----------------------------- |
| 122 |
|
| 123 |
\_def\.preventsingledebugon{\_directlua{luavlna.debug(true)}} |
| 124 |
\_def\.preventsingledebugoff{\_directlua{luavlna.debug(false)}} |
| 125 |
|
| 126 |
\_def\.nopredegrees{\_directlua{luavlna.no_predegrees = true}} |
| 127 |
\_def\.nosufdegrees{\_directlua{luavlna.no_sufdegrees = true}} |
| 128 |
\_def\.nounits{\_directlua{luavlna.no_unit = true}} |
| 129 |
\_def\.noinitials{\_directlua{luavlna.no_initials = true}} |
| 130 |
|
| 131 |
\_nspublic |
| 132 |
\singlechars % declare single-letter prepositions per language |
| 133 |
\compoundinitials % declare compound characters per language |
| 134 |
\enablesplithyphens % enable split hyphens per language |
| 135 |
\disablesplithyphens % disable split hyphens per language |
| 136 |
\preventsinglelang % declare main language independent of current hyphenation |
| 137 |
\preventsingleon % switch on the prepositions feature (default) |
| 138 |
\preventsingleoff % switch off the prepositions feature |
| 139 |
\nopredegrees % switch off pre-degrees feature (default is on) |
| 140 |
\nosufdegrees % switch off post-degrees feature (default is on) |
| 141 |
\nounits % switch off SI units feature (default is on) |
| 142 |
\noinitials % switch off initials feature (default is on) |
| 143 |
\preventsingledebugon % switch on debug printing (purple squares) |
| 144 |
\preventsingledebugoff % switch off debug printing (default) |
| 145 |
; |
| 146 |
|
| 147 |
\_endnamespace |
| 148 |
|
| 149 |
% Default values: |
| 150 |
\singlechars{Czech}{AIiVvOoUuSsZzKk} |
| 151 |
\singlechars{Slovak}{AIiVvOoUuSsZzKk} |
| 152 |
\compoundinitials{Czech}{Ch,CH} % doesn't work in version v0.1g of luavlna |
| 153 |
\enablesplithyphens{Czech} |
| 154 |
\enablesplithyphens{Slovak} |
| 155 |
% + predegrees, sufdegrees, units, initials (they are independent of language value) |
| 156 |
|
| 157 |
\_endcode |
| 158 |
|
| 159 |
What should be done automatically after `\load[vlna]`: |
| 160 |
|
| 161 |
Prepositions feature: v lese -> v~lese, (A taky zde) -> (A~taky zde). |
| 162 |
|
| 163 |
Pre-degrees feature: Mgr. Bown -> Mgr.~Brown. |
| 164 |
|
| 165 |
Post-degrees feature: John Brown, PhD. -> John Brown,~PhD. |
| 166 |
|
| 167 |
SI units feature: 42 cm -> 42~cm (with thin space) |
| 168 |
|
| 169 |
Initials feature: J. Brown -> J.~Brown |
| 170 |
|
| 171 |
Split hyphens feature: propan-butan -> propan\discretionary{-}{-}{-}butan. |
| 172 |
|
| 173 |
The prepositions and split-hyphens features are language-dependent, the other |
| 174 |
features are language-independent. |
| 175 |
|
| 176 |
A testing file: |
| 177 |
|
| 178 |
\begtt |
| 179 |
\load[vlna] |
| 180 |
\fontfam[LMfonts] |
| 181 |
\cslang |
| 182 |
\preventsingledebugon |
| 183 |
|
| 184 |
Mgr. J. Brown, PhD., zkusí v lese (A taky v závorce) zkontrolovat rozměr 42 m. |
| 185 |
Obejde se bez propan-butanových lahví. |
| 186 |
\bye |
| 187 |
\endtt |
| 188 |
|
| 189 |
Note, that versinon v0.1g 2020-06-02 of the luavlna package is an |
| 190 |
experimental code. It seems that `\compoundinitials` does not work in this |
| 191 |
version and when you use `\hbox{-}` in a paragraph when |
| 192 |
`\enablesplithyphens` then LuaTeX hangs. |