# Defining different scripts with fontspec

My actual problem: I want to typeset Tigrinya text, which is using Ethiopic (Ge'ez) script. But the text also contains numbers (which are afaik. in the latin script).

So I've defined a new font family using \newfontfamily{\ethiopicfont}[Script=Ethiopic]{Droid Sans Ethiopic} and introduced a new environment for Tigrinya using: \newenvironment{Tigrinya}{\ethiopicfont}{}. But the Droid Sans Ethiopic font only contains the Ethiopic script.

How do I define a fontfamily using different scripts from different fonts/files. (I guess this procedure is called patching a font?)

My MWE is:

\documentclass[a4paper]{scrartcl}

\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{tigrinya}

\newfontfamily{\ethiopicfont}[Script=Latin, Scale=1]{Droid Sans}
\newfontfamily{\ethiopicfont}[Script=Ethiopic, Scale=1]{Droid Sans Ethiopic}

\newenvironment{Tigrinya}{\ethiopicfont}{}

\begin{document}

\section*{Section}

\begin{Tigrinya}
13 እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።
\end{Tigrinya}

\end{document}

• The ucharclasses package might help (XeTeX). Apr 5 '15 at 19:55
• Thanks for this hint. It looks quite promising but I still didn't manage to use \setTransitionFrom or \setTransitions nor I could find out which block to use with \setTransitionTo for numbers \setTransitionTo{CuneiformNumbersandPunctuation}{\mainfont} doesn't seam to work. Apr 5 '15 at 20:55

Loading a non existent language is useless. Just add a font family for Ethiopic characters (I used Kefa, use whatever you like).

\documentclass[a4paper]{scrartcl}

\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage[Latin,Ethiopic]{ucharclasses}

\setmainlanguage{english}

\newfontfamily{\tigrinyafont}[Script=Ethiopic, Scale=1]{Kefa}

\newenvironment{tigrinya}
{\tigrinyafont}
{}

\setTransitionTo{Ethiopic}{\begin{tigrinya}}
\setTransitionFrom{Ethiopic}{\end{tigrinya}}

\begin{document}

\section*{Section}

\begin{tigrinya}
13 እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።
\end{tigrinya}

Text in English with ኻባታቶም ካብ

Whatever follows

\end{document}


## Version 2

After reading a recommendation/comment by Khaled Hosny I've changed the core of the snippet, so it's switching font only when needed. There is no font change on purpose if a character is a space. In the previous version, it wrapped every single character (not recommended in general as it breaks up ligatures, kerning pairs and probably other character relations in some fonts/languages).

If you can, please, use egreg's answer to this question (based on the XeTeX engine) with the help of ucharclasses package.

In case, you would like to use serious and similar approach in LuaTeX, I recommend Taco Hoekwater's answer mentioned here.

Still, I wanted to present an alternative to the mentioned mainstream solution by manipulation of a TeX parameter at the character level in Lua (unicode.utf8.*).

I've found and downloaded mentioned Droid family fonts from this location to my working directory (Droid Sans and Droid Sans Ethiopic, regular versions).

The Lua snippet checks out the previous and current character and decides if there is a need for font change. I set up Ethiopic range from 1200(hex) to 137F(hex) according to this Unicode chart.

Next to the TeX document, the snippet writes information also to the terminal for easy checking, in this example that is:

\droidsans{}0 123 abc 456 789 \droidsansethiopic{}እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።


The changes of fonts are done within a group, so we return to the Latin Modern family after processing that command. We run lualatex mal-droid.tex. I enclose the code (I've added abc next to digits to see if it works with letters as should be) and a preview of the result.

The mal-droid.tex file:

% lualatex mal-droid.tex
\documentclass[a4paper]{scrartcl}
\pagestyle{empty} % without a page number
\usepackage{polyglossia} % handling languages
\setmainlanguage{english} % setting up a main language
\usepackage{xcolor} % support for colors

% http://unicode.org/charts/PDF/U1200.pdf
\newfontfamily{\droidsansethiopic}[Script=Ethiopic, Color=red, ExternalLocation=./]{DroidSansEthiopic-Regular.ttf} % font for Ethiopic
\newfontfamily{\droidsans}[Script=Latin, Color=green, ExternalLocation=./]{DroidSans.ttf} % font for Latin inside Ethiopic

\begin{luacode*}
function changeme()
text=tex.toks[0]
previous=nil
for i=1,unicode.utf8.len(text) do -- glyph by glyph operation
char=unicode.utf8.sub(text,i,i) -- this glyph is processed right now
typeit="" -- text to be typeset/displayed, initializing to be sure that string is empty
if char>=unicode.utf8.char( tonumber("1200",16) ) and char<=unicode.utf8.char( tonumber("137F",16) ) then droid=1 else droid=0 end -- Is there a character in Ethiopic, or not?
if char==" " then droid=previous end -- don't change a font if character is a space
if (not previous or previous==1) and droid==0 then -- unused font or a switch, Latin
typeit="\\droidsans{}" -- core
end -- if, for Latin
if (not previous or previous==0) and droid==1 then -- unused font or a switch, Ethiopic
typeit="\\droidsansethiopic{}" -- core
end -- if, for Ethiopic
typeit=typeit..char -- a change in font + processed character
io.write(typeit) -- print the code to the terminal
tex.sprint(typeit) -- write a character to a TeX file
previous=droid -- store an actual font family to avoid repetion
end
end
\end{luacode*}
\def\maltig#1{\toks0{#1}% Save parameter to be load by Lua.
\begingroup% Begin of font manipulation.
\directlua{changeme()}% Processing string at a Lua level.
\endgroup% End of font manipulation.
}% End of \maltig command.

\begin{document}
Text before it. \maltig{0 123 abc 456 789 እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።} Text after it.
\end{document}


## Version 1 (old; wrapping every single character)

This is an example with LuaTeX as a TeX engine and string manipulation of the wrapped block of text. In general, this chunk of code could be simplified via a callback (there would be no more need for tagging foreign parts) and switching between fonts when there is a need for it instead of grouping every single character (that would assure creation of ligatures and kerning pairs).

I enclose the code (we run lualatex mal-droid.tex) and a preview of the PDF file.

% lualatex mal-droid.tex
\documentclass[a4paper]{scrartcl}
\pagestyle{empty}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{luatextra}

\newfontfamily{\ethiopicfont}[Scale=1,Color=red,ExternalLocation=./]{Code2000.TTF}
\begin{luacode*}
function changeme()
text=tex.toks[0]
for i=1,unicode.utf8.len(text) do
char=unicode.utf8.sub(text,i,i)
if char<="9" then tex.sprint("{\\setmainfont[Color=green]{Latin Modern Roman}{}"..char.."}") else tex.sprint(char) end
end
end
\end{luacode*}
\def\maltig#1{\toks0{#1}{\ethiopicfont\directlua{changeme()}}}

\begin{document}

• Issuing \setmainfont before each character is likely to be very inefficient, and is also wrong way to do it since every character will be set in a different font and kerning or any other interaction between the characters at the font level will be lost. Apr 9 '15 at 6:01