# [XeTeX] Detect, whether a font contains a certain character

Mon Nov 28 01:14:42 CET 2011

On Sun, Nov 27, 2011 at 11:35:29PM +0100, Zdenek Wagner wrote:

> 2011/11/27 Tobias Schoel <liesdiedatei at googlemail.com>:
> > Hi,
> >
> > I know, this question has been raised several times on this list. But I'm
> >
> > How can I detect, whether a font contains a certain character (let's say
> > u2103=Degree Celsius symbol) from within XeTeX?
> >
> Put it into an \hbox and measure its width (\wd). If the width is
> zero, the glyph does not exist.

There are chars with zero width, e.g. \not (in math mode).

Partially the test can be done (e.g. for "018E) by:
\ifnum\XeTeXfonttype\font>0
\ifnum\XeTeXcharglyph"018E>0
% ==> char exists
\fi
\fi

For the other cases, the \hbox method can be combined with \lastkern,
for example:

\begingroup
\setbox0=\hbox{\kern1sp^^^^018e\expandafter}%
\expandafter\endgroup
\ifdim\lastkern=1sp
\immediate\write16{glyph does not exist}%
\else
\immediate\write16{glyph does exist}%
\fi

\csname @@end\endcsname\end

Disadvantage: There is a warning in the .log file for missing glyphs:

| Missing character: There is no ... in font cmr10!

Yours sincerely
Heiko Oberdiek