% Optional comments
\_def \_unibidilua_version {0.4, 2026-04-10}
\_codedecl \unibidilua {Unicode Bidi Algorithm for OpTeX <\_unibidilua_version>}

\_initunifonts
\_directlua{require('unibidi-lua')}

\_endcode

This package is still in early phase, and the interface might still change.

The requirements are the \LuaTeX/ engine, and the ¦luaotfload¦ font loader.

\sec Usage

\def\key #1:{{\bf #1:}\hskip1em\ignorespaces}

Currently there is only one macro, ¦\unibidilua¦, which accepts key-value
pairs separated by a space.

The macro accepts the following keywords:

\begitems \style O
* \key enable: This key accepts a boolean value. When true, the unibidi-lua
   process function is active in the ¦pre_shaping_filter¦ callback. When false,
   the callback remains registered but processing is skipped. Default is true
   when the package is loaded.

* \key fences: This key accepts a boolean value. It allows to disable or
   enable step N0 of the UAX9 algorithm (directional formatting characters
   for paired brackets). Default is false when the package is loaded.

* \key removecontrols: This key accepts a boolean value. When true,
   bidirectional control characters are removed from the processed text.
   When false, they are preserved. Default is true when the package is loaded.

* \key setdir: This key accepts an integer (or a range of integers)
   representing Unicode code points, and a direction value. It sets the
   directional property for the specified character(s).
   Example: ¦setdir `\A l¦ or ¦setdir `\A-`\Z al¦.

* \key setmirror: This key accepts an integer (or a range of integers)
   representing Unicode code points, and mirror data. It sets the mirroring
   property for the specified character(s), indicating what character they
   should mirror to in RTL contexts.
   Example: ¦setmirror `\( `\)¦.

* \key settextclass: This key accepts an integer (or a range of integers)
   representing Unicode code points, and a text class value. It sets the
   text class property for the specified character(s) according to UAX9
   classification.

* \key baselevel: This key accepts a Lua function that determines the base
   directionality level of the text. The function is called during processing
   to establish whether the paragraph should be treated as LTR or RTL.

\enditems

To use the package, as with other packages, you can do ¦\load[unibidi-lua]¦,
¦\usepackage{unibidi-lua}¦ or ¦\input unibidi-lua¦ if you are using \OpTeX,
\LaTeX or Plain respectively. The process function is added to the
¦pre_shaping_filter¦ when you load the package.

\sec Lua API

The package exposes the following interface from Lua when you require it:

\begtt
local unibidi = require('unibidi-lua')
\endtt

\begitems
* ¦unibidi.setbaselevel(func)¦: Set a Lua function that determines the base
   directionality level
* ¦unibidi.directions¦: Table containing character directional properties
* ¦unibidi.mirrors¦: Table containing character mirroring mappings
* ¦unibidi.textclasses¦: Table containing character text classifications
\enditems

\_doc
%  optex -jobname unibidi-lua-doc '\docgen unibidi-lua'
\load [doc]
\verbchar¦
\overfullrule=0pt
\tit Unicode Bidi Algorithm Implementation for \OpTeX, \LaTeX, and Plain
\hfill Version: \_unibidilua_version \par
\centerline{\it Udi Fogiel, 2025-2026}
\parindent0pt\parskip5pt\parfillskip=20ptplus1fill

The unibidi-lua LuaTeX package is an implementation of the
\ulink[https://www.unicode.org/reports/tr9/]{Unicode Annex \#9} for \OpTeX,
\LaTeX/ and Plain \LuaTeX/ formats. It allows to typeset bidirectional
documents without the need of special markup.

\printdoctail unibidi-lua.opm % prints the documentation written after \_endcode
\bye
\_cod
\endinput
