[Tex/LaTex] Unicode in XeTeX with automatic font substitution

font-encodingsfontsunicodexetex

I've got a word processor (WordGrinder) which supports Unicode. It can export LaTeX files, which then get run through XeTeX.

Unfortunately, it seems that while XeTeX understands the Unicode correctly, it's not doing any kind of font substitution if the default font doesn't have a particular glyph in it. And since the default font is Computer Modern Roman, which has lousy font coverage, the default behaviour if I don't specify a font is that all my Unicode characters just vanish. This is not optimal.

For test purposes I've installed Linux Libertine O and hard-coded a reference to the font. More things now work, because Linux Libertine has much better Unicode coverage. But I still have missing glyphs. And I'd very much prefer not to have to hardcode a font reference in my output files because that makes it much less likely that the end user will be able to use the output files. (Linux Libertine O is not a default part of my distribution, for example.)

Is there any way I can tell XeTeX to automatically find an appropriate font which supports the Unicode glyphs? I'm quite happy with rendering in Computer Modern Roman and falling back to something else for unsupported glyphs; I don't care if the appearance is mismatched as long as the glyphs are actually there. (If the user wants a specific font they can edit the file.)

The distribution I'm currently using is the Debian version of xetex-live, but I'd like a distribution-agnostic fix if there is one. I'm happy with requiring XeTeX.

Best Answer

This answers tries to complete the previous answer and the comment for a "need to mix-and-match Unicode fonts". The package ucharclasses is meant for this "patchwork".

\usepackage{ucharclasses}
\usepackage{fontspec}

\setDefaultTransitions{\fontspec{Code2000}}{}
\setTransitionsForLatin{\fontspec{Palatino Linotype}}{}
\setTransitionsForGreek{\fontspec{Galatia SIL}}{}

Then xetex will switch the font when the text enters a new Unicode block.

I tried to combine this with Ulrike Fischer's code that checks if a font could be loaded. It seems to work:

\usepackage[Latin,Japanese]{ucharclasses}
\usepackage{fontspec}

\setmainfont{FreeSerif}
\ifnum\the\XeTeXcharglyph"D5=0
  \setTransitionsForLatin{\fontspec{FreeSerif}}{}
\else
  \setTransitionsForLatin{\fontspec{URW Palladio L}}{}
\fi

\setTransitionsForJapanese{\fontspec{VL Gothic}}{}

\begin{document}
Here is ここ.