0

Is it possible to automatically define fallbackfonts for specific Unicode ranges?

I know there are packages to do this such as xeCJK for CJK fonts as in this answer

But that's limited to those fonts. I'm particularly interested in Bengali, but maybe there's a more general way.

I know from this question that I can define specific unicode fallback characters. I've used this for IPA characters, for example. I could, of course, use the same mechanism and enter each of the (fortunately only 128) Bengali characters as fallback characters. But it would be neat to have a simpler way of doing this, like specifying for the range of code points A to B to use font X for the range of code points C to D to use font Y and so on.

So for example, the Unicode range for Bengali script is from U+0980 to U+09FF (see here) so if there was a way to specify:

  • for unicode range U+0980 to U+09FF use a Bengali font,
  • for U+0250 to U+02AF an IPA compatible font

and so on.

% !TEX TS-program = xelatexmk
\documentclass{article}

% The standard way of typing Bengali text within a document
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{bengali}
\newfontfamily\bengalifont[Script=Bengali, Scale=MatchLowercase]{Bangla MN}

% Defining individual fallback characters
\usepackage{newunicodechar}
\newfontfamily{\fallbackfont}{Junicode}[Scale=MatchLowercase]
\DeclareTextFontCommand{\textfallback}{\fallbackfont}
\newcommand{\fallbackchar}[2][\textfallback]{%
    \newunicodechar{#2}{#1{#2}}}
 \fallbackchar{ʤ}
 \fallbackchar{ʧ}

% Package for CJK fonts
\usepackage[fallback]{xeCJK}
\setCJKmainfont{SimHei}
\setCJKfallbackfamilyfont{rm}{SimHei} 

\begin{document} 

Latin text and Bengali text \textbengali{নাম হাতটা কাছে দারাতে হবে} and Latin again. 

Latin text and Bengali text নাম হাতটা কাছে দারাতে হবে and Latin again. 

Some phonetic characters ʤ in yet another font ʧ. 

CJK example 漢字源𣴑

\end{document}
  • 1
    Have a look at ucharclasses. I think it does what you want. – Javier Bezos Apr 28 at 7:25
  • @JavierBezos This is indeed what I'm looking for! However, I'm running into trouble. It only works when there are spaces after unicode block transitions. E.g., if there's a punctuation mark and then a space, after that space it still remains in the Bengali font. Any suggestions? – jan Apr 28 at 15:50
  • I'm using With \usepackage[Bengali]{ucharclasses} \newfontfamily{\bengalifont}[Scale=MatchUppercase]{Bangla MN} \setTransitionsFor{Bengali}{\bengalifont}{\rmfamily} – jan Apr 28 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.