[Tex/LaTex] How to specify a chain of fallback fonts in XeLaTeX

fontsfontspecxetex

I am aware of the following questions and answers about font fallback in XeTeX (or LuaLaTeX):

  1. Unicode in XeTeX with automatic font substitution?
  2. Font substitution with XeLaTeX
  3. Define fallback font for specific Unicode characters in LuaLaTeX
  4. Setting a fallback font for non-Arabic charset not supported by a certain Arabic font

From these I learnt that:

  1. XeTeX can't automatically find a font that includes a given character, but ucharclasses can be used to assign fonts to specific unicode ranges
  2. XeTex can be set up to transition from one font to the next based on character classes (interchar tokens).
  3. \newunicodechar can be used to assign a font to a given character.
  4. Polyglossia can be used to mark sections of the text in various languages.

However, none of these covers the following question, as far as I can tell:

How do I tell XeTeX to use a specific font "A", but fall back to another font "B" when a character isn't available in "A" (and so on with "C", and "D", etc.)?

In other words, I know exactly which fonts I want to use, and in which priority order; I don't need XeTeX to look for a font on my system: I can tell it exactly which ones to use. And ucharclasses won't work, because my font "A" covers parts of many classes, but for the rest of these classes I want to use "B".

From https://tug.org/pipermail/xetex/2011-November/022321.html, I know that it's possible to write a macro that detects whether a character is in a font. Thus, I can wrap each character in my document in this macro, test whether the character is in font "A", and use font "B" if it is not (and "C", and "D", until I find one in the list that contains the character).

Best Answer

In theory one could something like this:

  • Create the list of fonts
  • Check if the fonts exist
  • Wade through all unicode and test in which fonts the glyphs are
  • Assign charclasses according the results of this test
  • Setup transitions similar to the one in ucharclasses

In practice this has imho a number of drawbacks. The main that is probably horribly slow (even if it is probably faster than doing tests on all glyphs in a document). So one would have to add some intelligence, e.g. ignore some unicode block which are not used by the document, or run test on "typical glyphs" from a block (but accents will need special attention), or setup some fonts directly for which you already know the needed font, or run the test only on a set of known problematic glyphs, or store the result in some file so that it can be reused.

I have some doubts that it worth the time to go this route. It sounds like a lot of work for something I never really needed. I always knew which font I want/can to use for a unicode block.

Related Question