[XeTeX] \ifFontExists

Will Robertson wspr81 at gmail.com
Fri Jan 2 02:25:20 CET 2009


Arno Trautmann <Arno.Trautmann at ...> writes:

> I need something that test if an font exists or not, like:
> 
> \ifFontExists{myfont}{\fontspec{myfont}}{\fontspec{otherfont}}
> 
> Is there an easy way to do this? I didn’t find anything in the fontspec
> man or on the net.

There's an example how to do this in the XeTeX sample documents called
"testfontavailability" (find attached at end), which are linked from the
"Downloads" section of the XeTeX website. The reason I haven't provided a
built-in way to do this in fontspec is that it's SLOW.

Hope this helps,
Will

%!TEX TS-program = xetex
%!TEX encoding = UTF-8 Unicode

% Macros to try and find available fonts for XeTeX sample docs
%
% Usage:
%
% \testFontIsAvailable{font-name}
%   sets \ifFontIsAvailable according to whether or not it could be found
%
% \FindAnInstalledFont{font-name/alternative/another/yet-another}{\cs}
%   searches for an available font from among the names given,
%   and \def's the control sequence \cs to the first one found
%   or to <No suitable font found> if none (which will subsequently
%   cause an error when used in a \font command). 

\newif\ifFontIsAvailable
\def\testFontAvailability#1{
  \count255=\interactionmode
  \batchmode
  \let\preload=\nullfont
  \font\preload="#1" at 10pt
  \ifx\preload\nullfont \FontIsAvailablefalse
  \else \FontIsAvailabletrue \fi
  \interactionmode=\count255
}

\def\FindAnInstalledFont#1#2{
  \expandafter\getFirstFontName#1/\end
  \let\next\gobbleTwo
  \ifx\trialFontName\empty
    \def#2{<No suitable font found>}%
  \else
    \testFontAvailability{\trialFontName}
    \ifFontIsAvailable
      \edef#2{\trialFontName}%
    \else
      \let\next\FindAnInstalledFont
    \fi
  \fi
  \expandafter\next\expandafter{\remainingNames}{#2}
}
\def\getFirstFontName#1/#2\end{%
  \def\trialFontName{#1}\def\remainingNames{#2}}
\def\gobbleTwo#1#2{}








More information about the XeTeX mailing list