| 1 |
% Optional comments |
| 2 |
\_def \_unibidilua_version {1.0, 2026-05-19} |
| 3 |
\_codedecl \unibidilua {Unicode Bidi Algorithm for OpTeX <\_unibidilua_version>} |
| 4 |
|
| 5 |
\_initunifonts |
| 6 |
\_directlua{ |
| 7 |
local process = require('unibidi-lua-interface').reorder |
| 8 |
luatexbase.add_to_callback("pre_shaping_filter",process,"unibidi-lua") |
| 9 |
} |
| 10 |
|
| 11 |
\_endcode |
| 12 |
|
| 13 |
\sec Usage |
| 14 |
|
| 15 |
To use the package, as with other packages, you can do ¦\load[unibidi-lua]¦, |
| 16 |
¦\usepackage{unibidi-lua}¦ or ¦\input unibidi-lua¦ if you are using \OpTeX, |
| 17 |
\LaTeX or Plain respectively. The process function is added to the |
| 18 |
¦pre_shaping_filter¦ when you load the package. |
| 19 |
|
| 20 |
To modify things you can use the ¦\unibidilua¦ macro, |
| 21 |
which accepts key-value pairs separated by a space. |
| 22 |
|
| 23 |
\def\key #1:{{\bf #1:}\hskip1em\ignorespaces} |
| 24 |
|
| 25 |
The macro accepts the following keywords: |
| 26 |
|
| 27 |
\begitems \style O |
| 28 |
* \key enable: This key accepts a boolean value. When true, the unibidi-lua |
| 29 |
process function is active in the ¦pre_shaping_filter¦ callback. When false, |
| 30 |
the callback remains registered but processing is skipped. Default is true |
| 31 |
when the package is loaded. |
| 32 |
|
| 33 |
* \key fences: This key accepts a boolean value. It allows to disable or |
| 34 |
enable to consider matched brackets when resolving directional levels |
| 35 |
(\rulelink{N0}). |
| 36 |
Default is true when the package is loaded. |
| 37 |
|
| 38 |
* \key nsm: This key accepts a boolean value. It allows to disable or |
| 39 |
enable reordering of combining marks after the reordering of the text |
| 40 |
(\rulelink{L3}). |
| 41 |
This key is only relevant for the string functions, as there is |
| 42 |
no reordering in the nodes functions (dir nodes are used there instead). |
| 43 |
Default is true when the package is loaded. |
| 44 |
|
| 45 |
* \key mirror: This key accepts a boolean value. It allows to disable or |
| 46 |
enable mirroring of characters (\rulelink{L4}). |
| 47 |
Default is true when the package is loaded. |
| 48 |
|
| 49 |
* \key remove: This key accepts one of ¦none¦, ¦controls¦, or ¦full¦. This controls |
| 50 |
which characters are removed during processing. With ¦none¦, nothing is removed, |
| 51 |
with ¦controls¦ only ¦LRE¦, ¦RLE¦, ¦LRO¦, ¦RLO¦, ¦PDF¦ characters will be removed, |
| 52 |
and with ¦full¦ all characters specified in |
| 53 |
\rulelink{X9} are removed. |
| 54 |
|
| 55 |
* \key setdir: This key accepts an integer (or a range of integers) |
| 56 |
representing Unicode code points, and a direction value. It sets the |
| 57 |
directional property for the specified character(s). |
| 58 |
Example: ¦setdir `\A l¦ or ¦setdir `\A-`\Z al¦. |
| 59 |
|
| 60 |
* \key setmirror: This key accepts an integer (or a range of integers) |
| 61 |
representing Unicode code points, and mirror data. It sets the mirroring |
| 62 |
property for the specified character(s), indicating what character they |
| 63 |
should mirror to in RTL contexts. |
| 64 |
Example: ¦setmirror `\( `\)¦. |
| 65 |
|
| 66 |
* \key setbracket: This key accepts an integer (or a range of integers) |
| 67 |
representing Unicode code points, and a bracket type value (o (open) or c (close)) . It sets the |
| 68 |
bracket type property for the specified character(s). Example: ¦setbracket `\( o¦. |
| 69 |
|
| 70 |
* \key baselevel: This key accepts a Lua function that determines the base |
| 71 |
directionality level of the text. See ¦bidi.set("baselevel")¦ in \ref[lua]{section~@}. |
| 72 |
|
| 73 |
* \key mirrorchar: This key accepts a Lua function that applies the mirroring on nodes. |
| 74 |
See ¦bidi.set("mirrorchar")¦ in \ref[lua]{section~@}. |
| 75 |
|
| 76 |
* \key startlevel: This key accepts a Lua function that determines from which level |
| 77 |
to insert direction nodes. See ¦bidi.set("startlevel")¦ in \ref[lua]{section~@}. |
| 78 |
\enditems |
| 79 |
|
| 80 |
Note that you can also load the \TeX/ side interface without loading the package file using |
| 81 |
\begtt |
| 82 |
\directlua{require('unibidi-lua-interface')} |
| 83 |
\endtt |
| 84 |
This sets up the \TeX/ interface macros but does not add any function to a callback, |
| 85 |
and does not ensure ¦luaotfload¦ is loaded. The return value is a table with a |
| 86 |
¦reorder¦ function that can be added to a callback manually: |
| 87 |
\begtt |
| 88 |
\directlua{ |
| 89 |
local process = require('unibidi-lua-interface').reorder |
| 90 |
luatexbase.add_to_callback("pre_shaping_filter", process, "unibidi-lua") |
| 91 |
} |
| 92 |
\endtt |
| 93 |
|
| 94 |
\sec[lua] Lua API |
| 95 |
|
| 96 |
If you just want the Lua API, you can load it directly using |
| 97 |
|
| 98 |
\begtt |
| 99 |
local bidi = require('unibidi-lua') |
| 100 |
\endtt |
| 101 |
|
| 102 |
This does not define the \TeX/ interface nor add a function to a callback, |
| 103 |
and works with ¦texlua¦ and plain ¦lua¦ as well as \LuaTeX/. Note that the |
| 104 |
node functions are only available when running under \LuaTeX/. |
| 105 |
|
| 106 |
\secc Options |
| 107 |
|
| 108 |
\begitems |
| 109 |
* ¦bidi.set(key, value)¦: Set an option. The following keys are accepted: |
| 110 |
\begitems \style - |
| 111 |
* ¦"fences"¦: boolean, enable/disable bracket pair resolution (\rulelink{N0}). |
| 112 |
* ¦"nsm"¦: boolean, enable/disable combining mark reordering (\rulelink{L3}). |
| 113 |
Only relevant for string processing functions. |
| 114 |
* ¦"mirror"¦: boolean, enable/disable character mirroring (\rulelink{L4}). |
| 115 |
* ¦"remove"¦: string, one of ¦"none"¦, ¦"controls"¦, or ¦"full"¦. |
| 116 |
Controls which characters are removed during processing. |
| 117 |
* ¦"baselevel"¦: function, a custom function to determine the base |
| 118 |
directionality level of the paragraph. See the source code for more |
| 119 |
details. |
| 120 |
* ¦"mirrorchar"¦: function, called for each glyph that has a mirror |
| 121 |
character. Receives the direct node and the mirror codepoint, and is responsible |
| 122 |
for applying the mirroring. The default implementation sets the character |
| 123 |
to its mirror for all non HarfBuzz fonts, |
| 124 |
as HarfBuzz handles mirroring (including using OpenType features). |
| 125 |
Note that this does not check OpenType mirroring features |
| 126 |
(¦rtlm¦/¦rtla¦); fonts that rely on those features for mirroring may |
| 127 |
not render correctly in node mode\fnote{If you want you can implement it using this hook. |
| 128 |
See for example\nl \url{https://github.com/latex3/luaotfload/blob/bidi-dev/src/luaotfload-mirror.lua}}. |
| 129 |
Only relevant for node processing functions. |
| 130 |
* ¦"startlevel"¦: function, called with the base level to determine |
| 131 |
the minimum embedding level from which direction nodes are inserted. |
| 132 |
The default returns the base level itself. |
| 133 |
|
| 134 |
For example consider the following |
| 135 |
\begtt |
| 136 |
\hbox bdir1 {קו 42} |
| 137 |
\endtt |
| 138 |
The list of nodes we start with is something like |
| 139 |
\nodesinit |
| 140 |
With the default ¦baselevel¦ function, the bidi algorithm is run |
| 141 |
with base level 1 (the direction of the hbox containing the text). The resolved levels are |
| 142 |
{\tt\levels}. |
| 143 |
Direction nodes are then inserted starting from ¦startlevel¦ to mark |
| 144 |
the boundaries of each directional run. With the default ¦startlevel¦ |
| 145 |
function the resulting node list is: |
| 146 |
\nodesdefault |
| 147 |
The default inserts direction nodes starting from the base level, ensuring |
| 148 |
that the logical reading order is preserved if the box is later unboxed |
| 149 |
(since unboxing discards the box direction), or if a custom ¦baselevel¦ |
| 150 |
function assigns a different level than the containing box direction. |
| 151 |
If unboxing and custom base level functions are not a concern, the |
| 152 |
outer direction nodes can be omitted by returning ¦baselevel+1¦: |
| 153 |
\begtt |
| 154 |
bidi.set("startlevel", function(baselevel) return baselevel+1 end) |
| 155 |
\endtt |
| 156 |
which produces: |
| 157 |
\nodescustom |
| 158 |
Only relevant for node processing functions. |
| 159 |
\enditems |
| 160 |
* ¦bidi.get(key)¦: Get the current value of an option. Accepts the same |
| 161 |
keys as ¦bidi.set¦. |
| 162 |
\enditems |
| 163 |
|
| 164 |
\secc Data tables |
| 165 |
|
| 166 |
\begitems |
| 167 |
* ¦bidi.directions¦: Table containing character directional properties. |
| 168 |
Indexed by Unicode code point, returns the bidi class string |
| 169 |
(e.g. ¦"l"¦, ¦"r"¦, ¦"al"¦, ¦"an"¦, etc.). |
| 170 |
* ¦bidi.mirrors¦: Table containing character mirroring mappings. |
| 171 |
Indexed by Unicode code point, returns the code point of the |
| 172 |
mirrored character. |
| 173 |
* ¦bidi.brackets¦: Table containing bracket type classifications. |
| 174 |
Indexed by Unicode code point, returns ¦"o"¦ for opening brackets, |
| 175 |
¦"c"¦ for closing brackets. |
| 176 |
\enditems |
| 177 |
|
| 178 |
\secc String functions |
| 179 |
|
| 180 |
\begitems |
| 181 |
* ¦bidi.string.reorder(str, direction, where)¦: Apply the Unicode Bidirectional |
| 182 |
Algorithm to the string ¦str¦ and return the visually reordered string. |
| 183 |
¦direction¦ is ¦0¦ for LTR, ¦1¦ for RTL, or ¦-1¦ (or ¦nil¦) for auto-detect |
| 184 |
(or it can mean something else if the base level function is replaced). |
| 185 |
¦where¦ is just a value that is passed to the base level function, |
| 186 |
and the default one does not use it. |
| 187 |
* ¦bidi.string.levels(str, direction, where)¦: Apply the Unicode Bidirectional |
| 188 |
Algorithm to the string ¦str¦ and return two values: a levels array and |
| 189 |
a reorder array. The levels array maps each character position to its |
| 190 |
resolved bidi level, or ¦false¦ if the character was removed by \rulelink{X9}. |
| 191 |
The reorder array maps each visual position to the original logical index |
| 192 |
of the character. |
| 193 |
* ¦bidi.codepoints.reorder(codepoints, direction, where)¦: Same as |
| 194 |
¦bidi.string.reorder¦ but accepts and returns an array of codepoints |
| 195 |
instead of a string. |
| 196 |
* ¦bidi.codepoints.levels(codepoints, direction, where)¦: Same as |
| 197 |
¦bidi.string.levels¦ but accepts an array of codepoints instead of a string. |
| 198 |
\enditems |
| 199 |
|
| 200 |
\secc Node functions |
| 201 |
There are two variants of the node functions: ¦bidi.direct¦ which operates |
| 202 |
on direct node references (as used in ¦node.direct¦), and ¦bidi.node¦ which |
| 203 |
operates on regular node references. |
| 204 |
\begitems |
| 205 |
* ¦bidi.direct.reorder(head, direction, where)¦: Apply the Unicode |
| 206 |
Bidirectional Algorithm to the node list starting at ¦head¦ (a direct |
| 207 |
node reference). Direction nodes are inserted to mark directional runs. |
| 208 |
Returns the (possibly new) head of the node list. |
| 209 |
* ¦bidi.direct.levels(head, direction, where)¦: Apply the Unicode Bidirectional |
| 210 |
Algorithm to the node list starting at ¦head¦ (a direct node reference) |
| 211 |
and return a levels array. The levels array maps each node position to |
| 212 |
its resolved bidi level, or ¦false¦ if the node was removed by \rulelink{X9}. |
| 213 |
* ¦bidi.node.reorder(head, direction, where)¦: Same as ¦bidi.direct.reorder¦ |
| 214 |
but accepts and returns regular node references. |
| 215 |
* ¦bidi.node.levels(head, direction, where)¦: Same as ¦bidi.direct.levels¦ but |
| 216 |
accepts regular node references. |
| 217 |
\enditems |
| 218 |
|
| 219 |
\_doc |
| 220 |
% optex -jobname unibidi-lua-doc '\docgen unibidi-lua' |
| 221 |
\load [doc] |
| 222 |
\verbchar¦ |
| 223 |
\directlua{ |
| 224 |
local bidi = require('unibidi-lua') |
| 225 |
local function show_nodes(head,csname) |
| 226 |
local function nodename(n) |
| 227 |
if n.id == node.id("glyph") then |
| 228 |
return string.format('glyph "\%s"', utf8.char(node.getchar(n))) |
| 229 |
elseif n.id == node.id("glue") then |
| 230 |
return string.format('glue " "') |
| 231 |
elseif n.id == node.id("dir") then |
| 232 |
return string.format('dir \%s', n.dir) |
| 233 |
else |
| 234 |
return string.format('\%s \%d', node.type(n.id), sub) |
| 235 |
end |
| 236 |
end |
| 237 |
local lines = {"\nbb begtt"} |
| 238 |
local n = head |
| 239 |
while n do |
| 240 |
lines[\csstring\#lines+1] = nodename(n) |
| 241 |
n = n.next |
| 242 |
end |
| 243 |
lines[\csstring\#lines+1] = "\nbb endtt" |
| 244 |
lines[\csstring\#lines+1] = "" |
| 245 |
define_lua_command(csname,function() tex.print(lines) end) |
| 246 |
end |
| 247 |
callback.add_to_callback("pre_shaping_filter",function(head,_,dir) |
| 248 |
show_nodes(head,"nodesinit") |
| 249 |
local levels = bidi.node.levels(head,dir) |
| 250 |
define_lua_command("levels",function() tex.print(levels) end) |
| 251 |
show_nodes(bidi.node.reorder(head,dir),"nodesdefault") |
| 252 |
bidi.set("startlevel",function(baselevel) return baselevel+1 end) |
| 253 |
show_nodes(bidi.node.reorder(head,dir),"nodescustom") |
| 254 |
callback.remove_from_callback("pre_shaping_filter","foo") |
| 255 |
end,"foo") |
| 256 |
} |
| 257 |
\hbox bdir1 {קו 42} |
| 258 |
\edef\rulelink#1{\noexpand\ulink[https://www.unicode.org/reports/tr9/\csstring\##1]{rule #1}} |
| 259 |
\addto\_fontfeatures{fallback=miriam;} |
| 260 |
\directlua{luaotfload.add_fallback("miriam", {"name:MiriamMonoCLM:mode=harf;"})} |
| 261 |
\overfullrule=0pt |
| 262 |
\tit Unicode Bidi Algorithm Implementation for \OpTeX, \LaTeX, and Plain |
| 263 |
\hfill Version: \_unibidilua_version \par |
| 264 |
\centerline{\it Udi Fogiel, 2025-2026} |
| 265 |
\parindent0pt\parskip5pt\parfillskip=20ptplus1fill |
| 266 |
|
| 267 |
The unibidi-lua LuaTeX package is an implementation of the |
| 268 |
\ulink[https://www.unicode.org/reports/tr9/]{Unicode Annex \#9} for \OpTeX, |
| 269 |
\LaTeX/ and Plain \LuaTeX/ formats. The core Lua module also works |
| 270 |
with ¦texlua¦ and plain ¦lua¦. It allows typesetting bidirectional |
| 271 |
documents without the need of special markup. |
| 272 |
|
| 273 |
\printdoctail unibidi-lua.opm % prints the documentation written after \_endcode |
| 274 |
\bye |
| 275 |
\_cod |
| 276 |
\endinput |