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

Heiko Oberdiek heiko.oberdiek at googlemail.com
Tue Nov 29 17:15:02 CET 2011


On Tue, Nov 29, 2011 at 05:24:28PM +0200, Tobias Schoel wrote:

> Thanks a lot for the macro. A small summary (as I understand the
> code): \IfXeTeXTextCharExists takes three arguments:
> 1. the character (as direct input or via ^^^^-notation)
> 2. then-clause (executed, if the char exists in the _current_ font)
> 3. else-clause

Yes.

> As I'm no texnician, only XeLaTeX-User, can I copy the code from
> %%%Begin%%% to %%%End%%% into my own package and it'll work as
> described above,

Yes.

Variation, if testing a number instead of a glyph is preferred:

%%% Begin %%%
\def\IfXeTeXTextCharNumExists#1{%
  \begingroup
    \long\def\next##1##2{##2}%
    % or in LaTeX: \let\next\@secondoftwo
    \ifnum\XeTeXfonttype\font>0 %
      \ifnum\XeTeXcharglyph#1>0 %
        \long\def\next##1##2{##1}%
        % or in LaTeX: \let\next\@firstoftwo
      \fi
    \else
      \setbox0=\hbox{%
        \tracinglostchars=0 %
        \kern1sp\char#1\relax
        \expandafter
      }%
      \ifnum\lastkern=1 %
      \else
        \long\def\next##1##2{##1}%
        % or in LaTeX: \let\next\@firstoftwo
      \fi
    \fi
  \expandafter\endgroup
  \next
}
%%% End %%%

\IfXeTeXTextCharNumExists{"018E}{yes}{no}

Yours sincerely
  Heiko Oberdiek


More information about the XeTeX mailing list