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
\usepackage{luatextra} % loading luacode, fontspec etc.
% http://unicode.org/charts/PDF/U1200.pdf
% I downloaded fonts from https://github.com/jenskutilek/free-fonts/tree/master/Droid.
\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}
\section*{Section about Tigrinya}
Text before it. \maltig{0 123 abc 456 789 እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።} Text after it.
\end{document}

Version 1 (old; wrapping every single character)
Droid font family is licensed under Apache license [download]. Before I realised that, I had downloaded and unzipped the Code2000 font into my working directory.
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}
% http://www.fonts2u.com/download/code2000.font
\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}
\section*{Section about Tigrinya}
Text before it. \maltig{0 123 456 789 እንሆ ኸኣ በታ መዓልቲ እቲኣ ኽልተ ኻባታቶም ካብ የሩሳሌም ስሳ እስታድዮስ ዝምርሓቓ ኤማሁስ ናብ እትብሃል ዓዲ ይኸዱ ነበሩ።} Text after it.
\end{document}

\setTransitionFrom
or\setTransitions
nor I could find out which block to use with\setTransitionTo
for numbers\setTransitionTo{CuneiformNumbersandPunctuation}{\mainfont}
doesn't seam to work.