21

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.

3
  • 2
    Debian-TeXlive is from 2009: Get TeXLive 2011. Feb 23, 2012 at 16:22
  • Not feasible, I'm afraid --- as a Debian package, I need to make sure my stuff works with the Debian packages that are available, which means the 2009 version. Feb 23, 2012 at 17:19
  • 1
    @MartinSchröder, so TeXLive 2011 can do that? Mar 22, 2014 at 17:26

2 Answers 2

9

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 ここ.
2
  • 2
    This still requires me to manually tell TeX which font to use for each script type, right? Feb 24, 2012 at 10:53
  • did you find a solution for this ? emacs displays my file correctly so it must know what font to use...
    – nicolas
    Apr 25, 2016 at 8:26
7

As far as I know it is not possible to tell XeTeX "try to find an arbitrary font with greek chars". It is also imho not possible to test (without recurring to external tools which need --shell-escape) if a font exists and so to do something like "if font A exist use it else try font B": If you try to load a font which doesn't exist you always get an error.

But it is possible to test if a specific glyph exists in the current font and so implement warnings/errors:

\documentclass{article}
\usepackage{fontspec}

\begin{document}

\ifnum\the\XeTeXcharglyph"D5=0 
 Ä doesn't exist \else Ä exists \fi

\ifnum\the\XeTeXcharglyph"3B1=0
 ^^^3b1 (alpha) doesn't exist\else
 ^^^3b1 (alpha) exists  \fi


\setmainfont{Arial} 

\ifnum\the\XeTeXcharglyph"D5=0
 Ä doesn't exist \else Ä exists \fi

\ifnum\the\XeTeXcharglyph"3B1=0
 ^^^3b1 (alpha) doesn't exist\else
 ^^^3b1 (alpha) exists  \fi

\end{document}
4
  • This is actually a fairly plausible approach; because my document is machine generated I can just test for every single glyph up front and produce human-legible warnings if they're not available. But is this not a completely standard requirement? Surely any document that's in primarily one script but with sections in other scripts is going to need to mix-and-match Unicode fonts? Feb 23, 2012 at 17:23
  • But you didn't ask how to mix fonts (that can e.g. be done with \XeTeXinterclass) but if the fonts used can be chosen by xetex, if xetex is able to fall back to some standard font (like e.g. browser do it) - and this xetex can't do, at least not without help from the users: One would need some lists/database with the fonts of a system so that xetex can decide which one to use. Feb 23, 2012 at 17:37
  • 1
    Well, font fallback requires mixing fonts --- that's what it's all about! This is what stuff like PANOSE is for; to allow the system to determine what fonts look like the one the user asked for to allow automatic font substitution for unsupported glyphs. I'm used to Freetype/Pango, which does all of this automatically, hence my surprise that TeX doesn't... Feb 24, 2012 at 10:13
  • Mixing fonts is not the problem. For a real fallback system as you have in mind the application must 1. be able to test if a font exists on the system 2. understand the main characteristics of the font 3. inspect the system for good replacements. XeTeX was the first TeX engine which could use system fonts "natively" but it hasn't the capabilities needed for real font fallback. But with luatex it should be possible - as far as I know context has already code for it. Feb 24, 2012 at 10:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .