<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luatex.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Javier+Bezos</id>
	<title>LuaTeXWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luatex.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Javier+Bezos"/>
	<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php/Special:Contributions/Javier_Bezos"/>
	<updated>2026-06-05T23:39:41Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=191</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=191"/>
		<updated>2016-06-30T14:44:41Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
You may also want to readjust the vertical positioning (the baselines don&amp;#039;t always match). This part resorts to a pdf literal and it&amp;#039;s commented out (because it&amp;#039;s a hack - the glyphs are moved, but the corresponding boxes are not).&lt;br /&gt;
&lt;br /&gt;
As an alternative approach, you may use combo fonts (provided by luaotfload 2.7).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function arabic_text (head)&lt;br /&gt;
  local prevglyf = nil&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;2 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, prevglyf, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;0 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% is changed. If fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Extra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=190</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=190"/>
		<updated>2016-06-21T06:06:59Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
You may also want to readjust the vertical positioning (the baselines don&amp;#039;t always match). This part resorts to a pdf literal and it&amp;#039;s commented out (because it&amp;#039;s a hack - the glyphs are moved, but the corresponding boxes are not).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function arabic_text (head)&lt;br /&gt;
  local prevglyf = nil&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;2 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, prevglyf, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;0 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% is changed. If fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Extra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=189</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=189"/>
		<updated>2016-06-20T14:04:55Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
You may also want to readjust the vertical positioning (the baselines don&amp;#039;t always match). This part resorts to a pdf literal and it&amp;#039;s commented out.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function arabic_text (head)&lt;br /&gt;
  local prevglyf = nil&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;2 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, prevglyf, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;0 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% is changed. If fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Extra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=188</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=188"/>
		<updated>2016-06-20T13:51:37Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: Added vertical adjusting&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
You may also want to readjust the vertical positioning (the baselines don&amp;#039;t always match). This part resorts to a pdf literal and it&amp;#039;s commented out.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
local prevglyf = nil&lt;br /&gt;
function arabic_text (head)&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;2 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        local d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, prevglyf, d)&lt;br /&gt;
        % local r = node.new(node.id&amp;#039;whatsit&amp;#039;, node.subtype&amp;#039;pdf_literal&amp;#039;)&lt;br /&gt;
        % r.data = &amp;#039;0 Ts&amp;#039;&lt;br /&gt;
        % node.insert_before(head, item, r)   &lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% is changed. If fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Extra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=187</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=187"/>
		<updated>2016-06-19T08:18:53Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function arabic_text (head)&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)       &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_after(head, prevglyf, d)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, arabic_text, &amp;quot;Arabic text&amp;quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% changes. It fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Estra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=186</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=186"/>
		<updated>2016-06-19T08:16:13Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function show_nodes (head)&lt;br /&gt;
  local isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)       &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_after(head, prevglyf, d)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, show_nodes, &amp;#039;desc&amp;#039;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, show_nodes, &amp;#039;desc&amp;#039;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% changes. It fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Estra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=185</id>
		<title>Changing the font for a script</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Changing_the_font_for_a_script&amp;diff=185"/>
		<updated>2016-06-19T08:09:12Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: Created page with &amp;quot;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Typically, fonts contain only a selection of scripts, and changing them automatically (as well as the writing direction) to make sure the document is properly typeset would be a nice feature. Here is a mock up.&lt;br /&gt;
&lt;br /&gt;
It is based on the &amp;lt;code&amp;gt;pre_linebreak_filter&amp;lt;/code&amp;gt; callback, which contains the horizontal list of glyphs, kerns, whatsits, etc., before building the paragraph. This callback is used by the font loader, too. It works with LaTeX 2016 and LuaTeX 0.95, but it should work with previous versions with minimal changes.&lt;br /&gt;
&lt;br /&gt;
It is intended just for small chunks of text (a proper noun, a few words, etc.). For proper bidi writing, see typo-dha.lua, typo-dua.lua, typo-dub.lua and typo-duc.lua in context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\directlua{&lt;br /&gt;
local DIR = node.id(&amp;quot;dir&amp;quot;)&lt;br /&gt;
function show_nodes (head)&lt;br /&gt;
  isArab = false&lt;br /&gt;
  for item in node.traverse_id(node.id&amp;quot;glyph&amp;quot;, head) do&lt;br /&gt;
    if item.char &amp;gt; 1536 and item.char &amp;lt; 1792 then&lt;br /&gt;
      item.font = arabicfont % Defined below&lt;br /&gt;
      if isArab == false then &lt;br /&gt;
        isArab = true&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;+TRT&amp;#039;&lt;br /&gt;
        node.insert_before(head, item, d)       &lt;br /&gt;
      end&lt;br /&gt;
    else&lt;br /&gt;
      if isArab == true then&lt;br /&gt;
        isArab = false&lt;br /&gt;
        d = node.new(DIR)&lt;br /&gt;
        d.dir = &amp;#039;-TRT&amp;#039;&lt;br /&gt;
        node.insert_after(head, prevglyf, d)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
    prevglyf = item % A bit dangerous, but usually OK.&lt;br /&gt;
  end&lt;br /&gt;
  return head&lt;br /&gt;
end&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;pre_linebreak_filter&amp;quot;, show_nodes, &amp;#039;desc&amp;#039;)&lt;br /&gt;
luatexbase.add_to_callback(&amp;quot;hpack_filter&amp;quot;, show_nodes, &amp;#039;desc&amp;#039;)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
% The contextual analysis for arabic must be after the font&lt;br /&gt;
% changes. It fontspec is loaded before, then the callbacks must be&lt;br /&gt;
% reorderer, which is feasible.&lt;br /&gt;
&lt;br /&gt;
\usepackage{fontspec}&lt;br /&gt;
\setmainfont{Iwona}&lt;br /&gt;
\newfontfamily\amiri[Script=Arabic]{Amiri}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
{\amiri (نص قصير)\directlua{ arabicfont = font.current() }} Some text&lt;br /&gt;
&lt;br /&gt;
Some text نص قصير Some text&lt;br /&gt;
&lt;br /&gt;
\begin{tabular}{rr}&lt;br /&gt;
One &amp;amp; Two \\&lt;br /&gt;
Three &amp;amp; أربعة % :-/ Estra space&lt;br /&gt;
\end{tabular}&lt;br /&gt;
&lt;br /&gt;
Some text.&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=184</id>
		<title>Token filter</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=184"/>
		<updated>2016-06-16T06:11:42Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: /* Syntax */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new token. No arguments are passed to the callback, and the return value should be either a Lua table representing a to-be-processed token, a table consisting of a list of such tokens, or something else like &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or an empty table. Since no argument is passed, if you want to get the next token in the document, you should use the &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; function. Your lua function should therefore look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function()&lt;br /&gt;
    return &amp;lt;table&amp;gt; token&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your Lua function does not return a table representing a valid token, it will be immediately called again, until it eventually does return a useful token or tokenlist (or until you reset the callback value to nil). If your function returns a single usable token, then that token will be processed by LuaTeX immediately. If the function returns a token list (a table consisting of a list of consecutive token tables), then that list will be pushed to the input stack at a completely new token list level. If what is passed to TeX is expandable, then the result of expansion is inserted into input and &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; will grab it.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note.&amp;#039;&amp;#039;&amp;#039; This callback will be eventually removed and replaced by a new token library. See [http://tug.org/pipermail/luatex/2016-February/005651.html this message] on the luatex list.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Faking \XeTeXinterchartoks ==&lt;br /&gt;
&lt;br /&gt;
Note this is not strictly equivalent to the real XeTeX thing, because the latter operates on the character lever and as implemented a macro breaks the char “chain”. So, &amp;lt;code&amp;gt;a\AA a&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;\def\AA{A}&amp;lt;/code&amp;gt;, doesn&amp;#039;t work as expected.&lt;br /&gt;
&lt;br /&gt;
Lua code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.lua&lt;br /&gt;
&lt;br /&gt;
charclasses = charclasses or {}&lt;br /&gt;
&lt;br /&gt;
function setcharclass (a,b)&lt;br /&gt;
   charclasses[a] = b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local i = 0&lt;br /&gt;
while i &amp;lt; 65536 do&lt;br /&gt;
  charclasses[i]  = 0&lt;br /&gt;
  i = i + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
interchartoks =  interchartoks or {}&lt;br /&gt;
&lt;br /&gt;
function setinterchartoks (a,b,c)&lt;br /&gt;
   interchartoks[a] = interchartoks[a] or {}&lt;br /&gt;
   interchartoks[a][b] = c&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local nc, oc&lt;br /&gt;
oc = 255&lt;br /&gt;
&lt;br /&gt;
function do_intertoks () &lt;br /&gt;
  local tok = token.get_next() &lt;br /&gt;
  if tex.count[&amp;#039;XeTeXinterchartokenstate&amp;#039;] == 1 then&lt;br /&gt;
      if tok[1] == 11 or  tok[1] == 12 then&lt;br /&gt;
        nc = charclasses[tok[2]] &lt;br /&gt;
        newchar = tok[2]&lt;br /&gt;
      else &lt;br /&gt;
        nc = 255&lt;br /&gt;
        newchar = &amp;#039;&amp;#039;&lt;br /&gt;
      end&lt;br /&gt;
      local insert  = &amp;#039;&amp;#039;&lt;br /&gt;
      if interchartoks[oc] and interchartoks[oc][nc] then&lt;br /&gt;
          insert = interchartoks[oc][nc] &lt;br /&gt;
          local newtok = tok&lt;br /&gt;
          if insert&amp;lt;100 then&lt;br /&gt;
            local dec = math.floor(insert / 10) + 48;&lt;br /&gt;
            local unit = math.floor(insert % 10) + 48;&lt;br /&gt;
            newtok = {&lt;br /&gt;
              -- \XeTeXinterchartokenstate=0 \the\toks&amp;lt;n&amp;gt; \XeTeXinterchartokenstate=1&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;0&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;the&amp;#039;),&lt;br /&gt;
              token.create(&amp;#039;toks&amp;#039;),&lt;br /&gt;
              token.create(dec,12),&lt;br /&gt;
              token.create(unit,12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;1&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              {tok[1], tok[2], tok[3]}}               &lt;br /&gt;
          end&lt;br /&gt;
          tok = newtok&lt;br /&gt;
      end&lt;br /&gt;
      oc = nc&lt;br /&gt;
  end&lt;br /&gt;
  return tok&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register (&amp;#039;token_filter&amp;#039;, do_intertoks)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LaTeX package file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.sty&lt;br /&gt;
\newcount\XeTeXinterchartokenstate&lt;br /&gt;
&lt;br /&gt;
\newcount\charclasses&lt;br /&gt;
\def\newXeTeXintercharclass#1%&lt;br /&gt;
  {\global\advance\charclasses1\relax&lt;br /&gt;
   \newcount#1&lt;br /&gt;
   \global#1=\the\charclasses&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
\newcount\cchone&lt;br /&gt;
\newcount\cchtwo&lt;br /&gt;
&lt;br /&gt;
\def\dodoXeTeXcharclass&lt;br /&gt;
    {\directlua{setcharclass(\the\cchone,\the\cchtwo)}}&lt;br /&gt;
&lt;br /&gt;
\def\doXeTeXcharclass%&lt;br /&gt;
   {\afterassignment\dodoXeTeXcharclass\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXcharclass%&lt;br /&gt;
   {\afterassignment\doXeTeXcharclass\cchone }&lt;br /&gt;
&lt;br /&gt;
\protected\def\XeTeXdointerchartoks%&lt;br /&gt;
   {\directlua{setinterchartoks(\the\cchone,\the\cchtwo,\the\allocationnumber)}}&lt;br /&gt;
&lt;br /&gt;
\protected\def\dodoXeTeXinterchartoks%&lt;br /&gt;
   {\newtoks\mytoks\afterassignment\XeTeXdointerchartoks\global\mytoks }&lt;br /&gt;
&lt;br /&gt;
\protected\def\doXeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\dodoXeTeXinterchartoks\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\doXeTeXinterchartoks\cchone }&lt;br /&gt;
&lt;br /&gt;
\luatexdirectlua{require(&amp;#039;luatexinterchartoks.lua&amp;#039;)}&lt;br /&gt;
&lt;br /&gt;
\endinput&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\usepackage{luatexinterchartoks}&lt;br /&gt;
\usepackage{color}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
\newXeTeXintercharclass \mycharclassa&lt;br /&gt;
\newXeTeXintercharclass \mycharclassA&lt;br /&gt;
\newXeTeXintercharclass \mycharclassB&lt;br /&gt;
\XeTeXcharclass `\a \mycharclassa&lt;br /&gt;
\XeTeXcharclass `\A \mycharclassA&lt;br /&gt;
\XeTeXcharclass `\B \mycharclassB&lt;br /&gt;
% between &amp;quot;a&amp;quot; and &amp;quot;A&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassa \mycharclassA = {[\itshape}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassA \mycharclassa = {\upshape]}&lt;br /&gt;
% between &amp;quot; &amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks 255 \mycharclassB = {\bgroup\color{blue}}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB 255 = {\egroup}&lt;br /&gt;
% between &amp;quot;B&amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB \mycharclassB = {.}&lt;br /&gt;
&lt;br /&gt;
\begingroup&lt;br /&gt;
\XeTeXinterchartokenstate = 1&lt;br /&gt;
aAa A a B aBa BB&lt;br /&gt;
\endgroup&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=183</id>
		<title>Token filter</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=183"/>
		<updated>2016-06-15T11:07:18Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: /* Faking \XeTeXinterchartoks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new token. No arguments are passed to the callback, and the return value should be either a Lua table representing a to-be-processed token, a table consisting of a list of such tokens, or something else like &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or an empty table. Since no argument is passed, if you want to get the next token in the document, you should use the &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; function. Your lua function should therefore look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function()&lt;br /&gt;
    return &amp;lt;table&amp;gt; token&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your Lua function does not return a table representing a valid token, it will be immediately called again, until it eventually does return a useful token or tokenlist (or until you reset the callback value to nil). If your function returns a single usable token, then that token will be processed by LuaTeX immediately. If the function returns a token list (a table consisting of a list of consecutive token tables), then that list will be pushed to the input stack at a completely new token list level. If what is passed to TeX is expandable, then the result of expansion is inserted into input and &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; will grab it.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Faking \XeTeXinterchartoks ==&lt;br /&gt;
&lt;br /&gt;
Note this is not strictly equivalent to the real XeTeX thing, because the latter operates on the character lever and as implemented a macro breaks the char “chain”. So, &amp;lt;code&amp;gt;a\AA a&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;\def\AA{A}&amp;lt;/code&amp;gt;, doesn&amp;#039;t work as expected.&lt;br /&gt;
&lt;br /&gt;
Lua code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.lua&lt;br /&gt;
&lt;br /&gt;
charclasses = charclasses or {}&lt;br /&gt;
&lt;br /&gt;
function setcharclass (a,b)&lt;br /&gt;
   charclasses[a] = b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local i = 0&lt;br /&gt;
while i &amp;lt; 65536 do&lt;br /&gt;
  charclasses[i]  = 0&lt;br /&gt;
  i = i + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
interchartoks =  interchartoks or {}&lt;br /&gt;
&lt;br /&gt;
function setinterchartoks (a,b,c)&lt;br /&gt;
   interchartoks[a] = interchartoks[a] or {}&lt;br /&gt;
   interchartoks[a][b] = c&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local nc, oc&lt;br /&gt;
oc = 255&lt;br /&gt;
&lt;br /&gt;
function do_intertoks () &lt;br /&gt;
  local tok = token.get_next() &lt;br /&gt;
  if tex.count[&amp;#039;XeTeXinterchartokenstate&amp;#039;] == 1 then&lt;br /&gt;
      if tok[1] == 11 or  tok[1] == 12 then&lt;br /&gt;
        nc = charclasses[tok[2]] &lt;br /&gt;
        newchar = tok[2]&lt;br /&gt;
      else &lt;br /&gt;
        nc = 255&lt;br /&gt;
        newchar = &amp;#039;&amp;#039;&lt;br /&gt;
      end&lt;br /&gt;
      local insert  = &amp;#039;&amp;#039;&lt;br /&gt;
      if interchartoks[oc] and interchartoks[oc][nc] then&lt;br /&gt;
          insert = interchartoks[oc][nc] &lt;br /&gt;
          local newtok = tok&lt;br /&gt;
          if insert&amp;lt;100 then&lt;br /&gt;
            local dec = math.floor(insert / 10) + 48;&lt;br /&gt;
            local unit = math.floor(insert % 10) + 48;&lt;br /&gt;
            newtok = {&lt;br /&gt;
              -- \XeTeXinterchartokenstate=0 \the\toks&amp;lt;n&amp;gt; \XeTeXinterchartokenstate=1&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;0&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;the&amp;#039;),&lt;br /&gt;
              token.create(&amp;#039;toks&amp;#039;),&lt;br /&gt;
              token.create(dec,12),&lt;br /&gt;
              token.create(unit,12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;1&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              {tok[1], tok[2], tok[3]}}               &lt;br /&gt;
          end&lt;br /&gt;
          tok = newtok&lt;br /&gt;
      end&lt;br /&gt;
      oc = nc&lt;br /&gt;
  end&lt;br /&gt;
  return tok&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register (&amp;#039;token_filter&amp;#039;, do_intertoks)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LaTeX package file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.sty&lt;br /&gt;
\newcount\XeTeXinterchartokenstate&lt;br /&gt;
&lt;br /&gt;
\newcount\charclasses&lt;br /&gt;
\def\newXeTeXintercharclass#1%&lt;br /&gt;
  {\global\advance\charclasses1\relax&lt;br /&gt;
   \newcount#1&lt;br /&gt;
   \global#1=\the\charclasses&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
\newcount\cchone&lt;br /&gt;
\newcount\cchtwo&lt;br /&gt;
&lt;br /&gt;
\def\dodoXeTeXcharclass&lt;br /&gt;
    {\directlua{setcharclass(\the\cchone,\the\cchtwo)}}&lt;br /&gt;
&lt;br /&gt;
\def\doXeTeXcharclass%&lt;br /&gt;
   {\afterassignment\dodoXeTeXcharclass\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXcharclass%&lt;br /&gt;
   {\afterassignment\doXeTeXcharclass\cchone }&lt;br /&gt;
&lt;br /&gt;
\protected\def\XeTeXdointerchartoks%&lt;br /&gt;
   {\directlua{setinterchartoks(\the\cchone,\the\cchtwo,\the\allocationnumber)}}&lt;br /&gt;
&lt;br /&gt;
\protected\def\dodoXeTeXinterchartoks%&lt;br /&gt;
   {\newtoks\mytoks\afterassignment\XeTeXdointerchartoks\global\mytoks }&lt;br /&gt;
&lt;br /&gt;
\protected\def\doXeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\dodoXeTeXinterchartoks\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\doXeTeXinterchartoks\cchone }&lt;br /&gt;
&lt;br /&gt;
\luatexdirectlua{require(&amp;#039;luatexinterchartoks.lua&amp;#039;)}&lt;br /&gt;
&lt;br /&gt;
\endinput&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\usepackage{luatexinterchartoks}&lt;br /&gt;
\usepackage{color}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
\newXeTeXintercharclass \mycharclassa&lt;br /&gt;
\newXeTeXintercharclass \mycharclassA&lt;br /&gt;
\newXeTeXintercharclass \mycharclassB&lt;br /&gt;
\XeTeXcharclass `\a \mycharclassa&lt;br /&gt;
\XeTeXcharclass `\A \mycharclassA&lt;br /&gt;
\XeTeXcharclass `\B \mycharclassB&lt;br /&gt;
% between &amp;quot;a&amp;quot; and &amp;quot;A&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassa \mycharclassA = {[\itshape}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassA \mycharclassa = {\upshape]}&lt;br /&gt;
% between &amp;quot; &amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks 255 \mycharclassB = {\bgroup\color{blue}}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB 255 = {\egroup}&lt;br /&gt;
% between &amp;quot;B&amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB \mycharclassB = {.}&lt;br /&gt;
&lt;br /&gt;
\begingroup&lt;br /&gt;
\XeTeXinterchartokenstate = 1&lt;br /&gt;
aAa A a B aBa BB&lt;br /&gt;
\endgroup&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=182</id>
		<title>Token filter</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=Token_filter&amp;diff=182"/>
		<updated>2016-06-15T08:03:51Z</updated>

		<summary type="html">&lt;p&gt;Javier Bezos: /* Faking \XeTeXinterchartoks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Syntax =&lt;br /&gt;
&lt;br /&gt;
This [[Callbacks|callback]] is called whenever TeX needs a new token. No arguments are passed to the callback, and the return value should be either a Lua table representing a to-be-processed token, a table consisting of a list of such tokens, or something else like &amp;lt;tt&amp;gt;nil&amp;lt;/tt&amp;gt; or an empty table. Since no argument is passed, if you want to get the next token in the document, you should use the &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; function. Your lua function should therefore look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function()&lt;br /&gt;
    return &amp;lt;table&amp;gt; token&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your Lua function does not return a table representing a valid token, it will be immediately called again, until it eventually does return a useful token or tokenlist (or until you reset the callback value to nil). If your function returns a single usable token, then that token will be processed by LuaTeX immediately. If the function returns a token list (a table consisting of a list of consecutive token tables), then that list will be pushed to the input stack at a completely new token list level. If what is passed to TeX is expandable, then the result of expansion is inserted into input and &amp;lt;tt&amp;gt;token.get_next()&amp;lt;/tt&amp;gt; will grab it.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Faking \XeTeXinterchartoks ==&lt;br /&gt;
&lt;br /&gt;
Note this is not strictly equivalent to the real XeTeX thing, because the latter operates on the character level. So, &amp;lt;code&amp;gt;a\AA a&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;\def\AA{A}&amp;lt;/code&amp;gt;, doesn&amp;#039;t work as expected.&lt;br /&gt;
&lt;br /&gt;
Lua code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.lua&lt;br /&gt;
&lt;br /&gt;
charclasses = charclasses or {}&lt;br /&gt;
&lt;br /&gt;
function setcharclass (a,b)&lt;br /&gt;
   charclasses[a] = b&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local i = 0&lt;br /&gt;
while i &amp;lt; 65536 do&lt;br /&gt;
  charclasses[i]  = 0&lt;br /&gt;
  i = i + 1&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
interchartoks =  interchartoks or {}&lt;br /&gt;
&lt;br /&gt;
function setinterchartoks (a,b,c)&lt;br /&gt;
   interchartoks[a] = interchartoks[a] or {}&lt;br /&gt;
   interchartoks[a][b] = c&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local nc, oc&lt;br /&gt;
oc = 255&lt;br /&gt;
&lt;br /&gt;
function do_intertoks () &lt;br /&gt;
  local tok = token.get_next() &lt;br /&gt;
  if tex.count[&amp;#039;XeTeXinterchartokenstate&amp;#039;] == 1 then&lt;br /&gt;
      if tok[1] == 11 or  tok[1] == 12 then&lt;br /&gt;
        nc = charclasses[tok[2]] &lt;br /&gt;
        newchar = tok[2]&lt;br /&gt;
      else &lt;br /&gt;
        nc = 255&lt;br /&gt;
        newchar = &amp;#039;&amp;#039;&lt;br /&gt;
      end&lt;br /&gt;
      local insert  = &amp;#039;&amp;#039;&lt;br /&gt;
      if interchartoks[oc] and interchartoks[oc][nc] then&lt;br /&gt;
          insert = interchartoks[oc][nc] &lt;br /&gt;
          local newtok = tok&lt;br /&gt;
          if insert&amp;lt;100 then&lt;br /&gt;
            local dec = math.floor(insert / 10) + 48;&lt;br /&gt;
            local unit = math.floor(insert % 10) + 48;&lt;br /&gt;
            newtok = {&lt;br /&gt;
              -- \XeTeXinterchartokenstate=0 \the\toks&amp;lt;n&amp;gt; \XeTeXinterchartokenstate=1&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;0&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;the&amp;#039;),&lt;br /&gt;
              token.create(&amp;#039;toks&amp;#039;),&lt;br /&gt;
              token.create(dec,12),&lt;br /&gt;
              token.create(unit,12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              token.create(&amp;#039;XeTeXinterchartokenstate&amp;#039;),&lt;br /&gt;
              token.create(string.byte(&amp;#039;=&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039;1&amp;#039;),12),&lt;br /&gt;
              token.create(string.byte(&amp;#039; &amp;#039;),10),&lt;br /&gt;
              {tok[1], tok[2], tok[3]}}               &lt;br /&gt;
          end&lt;br /&gt;
          tok = newtok&lt;br /&gt;
      end&lt;br /&gt;
      oc = nc&lt;br /&gt;
  end&lt;br /&gt;
  return tok&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
callback.register (&amp;#039;token_filter&amp;#039;, do_intertoks)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
LaTeX package file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
% luatexinterchartoks.sty&lt;br /&gt;
\newcount\XeTeXinterchartokenstate&lt;br /&gt;
&lt;br /&gt;
\newcount\charclasses&lt;br /&gt;
\def\newXeTeXintercharclass#1%&lt;br /&gt;
  {\global\advance\charclasses1\relax&lt;br /&gt;
   \newcount#1&lt;br /&gt;
   \global#1=\the\charclasses&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
\newcount\cchone&lt;br /&gt;
\newcount\cchtwo&lt;br /&gt;
&lt;br /&gt;
\def\dodoXeTeXcharclass&lt;br /&gt;
    {\directlua{setcharclass(\the\cchone,\the\cchtwo)}}&lt;br /&gt;
&lt;br /&gt;
\def\doXeTeXcharclass%&lt;br /&gt;
   {\afterassignment\dodoXeTeXcharclass\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXcharclass%&lt;br /&gt;
   {\afterassignment\doXeTeXcharclass\cchone }&lt;br /&gt;
&lt;br /&gt;
\protected\def\XeTeXdointerchartoks%&lt;br /&gt;
   {\directlua{setinterchartoks(\the\cchone,\the\cchtwo,\the\allocationnumber)}}&lt;br /&gt;
&lt;br /&gt;
\protected\def\dodoXeTeXinterchartoks%&lt;br /&gt;
   {\newtoks\mytoks\afterassignment\XeTeXdointerchartoks\global\mytoks }&lt;br /&gt;
&lt;br /&gt;
\protected\def\doXeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\dodoXeTeXinterchartoks\cchtwo }&lt;br /&gt;
&lt;br /&gt;
\def\XeTeXinterchartoks%&lt;br /&gt;
   {\afterassignment\doXeTeXinterchartoks\cchone }&lt;br /&gt;
&lt;br /&gt;
\luatexdirectlua{require(&amp;#039;luatexinterchartoks.lua&amp;#039;)}&lt;br /&gt;
&lt;br /&gt;
\endinput&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Test document:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
\documentclass{article}&lt;br /&gt;
&lt;br /&gt;
\usepackage{luatexinterchartoks}&lt;br /&gt;
\usepackage{color}&lt;br /&gt;
&lt;br /&gt;
\begin{document}&lt;br /&gt;
&lt;br /&gt;
\newXeTeXintercharclass \mycharclassa&lt;br /&gt;
\newXeTeXintercharclass \mycharclassA&lt;br /&gt;
\newXeTeXintercharclass \mycharclassB&lt;br /&gt;
\XeTeXcharclass `\a \mycharclassa&lt;br /&gt;
\XeTeXcharclass `\A \mycharclassA&lt;br /&gt;
\XeTeXcharclass `\B \mycharclassB&lt;br /&gt;
% between &amp;quot;a&amp;quot; and &amp;quot;A&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassa \mycharclassA = {[\itshape}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassA \mycharclassa = {\upshape]}&lt;br /&gt;
% between &amp;quot; &amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks 255 \mycharclassB = {\bgroup\color{blue}}&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB 255 = {\egroup}&lt;br /&gt;
% between &amp;quot;B&amp;quot; and &amp;quot;B&amp;quot;:&lt;br /&gt;
\XeTeXinterchartoks \mycharclassB \mycharclassB = {.}&lt;br /&gt;
&lt;br /&gt;
\begingroup&lt;br /&gt;
\XeTeXinterchartokenstate = 1&lt;br /&gt;
aAa A a B aBa BB&lt;br /&gt;
\endgroup&lt;br /&gt;
&lt;br /&gt;
\end{document}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Javier Bezos</name></author>
		
	</entry>
</feed>