# How hard is it to use typo-dir.lua in LaTeX?

I tried to use typo-dir.lua from ConTeXt distribution in LaTeX but I could not. I searched all context sources and I added all missing functions but still it did not work. Is it possible to use this Lua file in LaTeX? I am only trying to see how that works in LaTeX.

\documentclass{article}
\usepackage{fontspec}
\directlua{
require("typo-dir")
commands.setdirection(1)
}
\luatexpardir TRT
\luatextextdir TRT
\begin{document}
1984
\end{document}


Edit: Using @phg's files, I get the following error:

./test-typo-dir.tex:4: LuaTeX error ...al/texlive/2013/texmf-dist/tex/context/ba
se/typo-dir.lua:48: attempt to index local 'mathcodes' (a nil value)
stack traceback:
...al/texlive/2013/texmf-dist/tex/context/base/typo-dir.lua:48: in main chunk
[C]: in function 'require'
./import-typo-dir.lua:172: in main chunk
[C]: in function 'require'
...xlive/2013/texmf-dist/tex/luatex/luatexbase/modutils.lua:57: in function 're
quire_module'
[string "\directlua "]:1: in main chunk.
\lltxb@requirelua ...xluaescapestring {#2}" \fi )}

l.4

?

-
welcome back to the site –  David Carlisle Sep 8 '13 at 20:04
Thanks a lot David –  Vafa Khalighi Sep 9 '13 at 1:49

Don’t do this. Use Context instead. The code is integrated too tightly into Context to be of practical use outside of it.

(I know this is not a real answer to your question but it’s nevertheless correct.)

Since you ask how hard it would be: I consider it a medium to hard task if you know what you are doing. Probably medium if you (unlike me) are already familiar with the Unicode Bidirectional Algorithm. This gist: https://gist.github.com/phi-gamma/6488187 contains the necessary Lua code as well as some example wrapper macros. It requires the beta (unstable) version of Context (the version in Texlive 2013 is too old) which is most conveniently acquired by installing the Standalone distribution.

In Context, Unicode bidi is implemented as a node processor, which is a managed version of Luatex callbacks. Normally, with some (in your case: extensive) preparation you can hook any of these processors into the raw callback mechanism. For an overview of what this entails you can refer to the steps taken by Luaotfload in order to load Context’s letterspacing code.

typo-dir.lua makes heavy use of Unicode character properties. For its purposes, the character information provided by Luaotfload does not suffice, so we have to load the entire character table (char-def.lua). Also, char-ini.lua is required because it installs a series of special handlers that facilitate access to these properties.

UPDATE On 2013-09-09 the bidi code was split into typo-dir.lua (node processor handling and interface), typo-dha.lua (main bidi node processor), and math-dir.lua (math bidi). The gist has been updated to reflect these changes. Judging from the source and by the recent activity further changes can be expected in the near future.

Besides those there are several further prerequisites: we need to properly map Context’s interface to Luatex attributes onto Luatexbase and preallocate three attributes that contain the bidi state. Also, the context namespace must be present, so we set up a rudimentary substitute which supports both the __index and the __call metamethods. These are only the more complex requirements. For orientation I annotated the source briefly.

I tested the result only against the demonstration section of typo-dir.mkiv. Automatic switching of directions appears to work. I cannot explain the differences in font rendering, though, but that’s not what you asked B-). This is what it looks like -- Latex on the left, Context on the right side:

-
Thanks for the answer. Could you use typo-dir.lua with LaTeX? If the answer is yes, could you please show me how to use it in LaTeX? –  Vafa Khalighi Sep 9 '13 at 1:49
Have you read the gist I linked? The included .tex shows how. –  phg Sep 9 '13 at 8:14
Great, thanks. sorry I am almost getting old and my eyes sight gets weaker every day. –  Vafa Khalighi Sep 9 '13 at 8:19
Oh, that’s sad to hear. Hope the code will get you started. –  phg Sep 9 '13 at 8:31
Please see the edit in my question. When I run lualatex, I get error. –  Vafa Khalighi Sep 9 '13 at 8:33