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

Tobias Schoel liesdiedatei at googlemail.com
Mon Nov 28 13:34:01 CET 2011


So this seems to work:

\documentclass{minimal}
\usepackage{fontspec}
\usepackage{unicode-math}
\setmathfont{XITS Math}

\usepackage{siunitx}
\sisetup{%
detect-all,
}

\newcounter{works}
\setcounter{works}{0}

\usepackage{pgffor}

\begin{document}

\newcommand{\ifavailablethenelse}[4]{%#1=font,#2=charcode,#3=then-clause,#4=else-clause
\setcounter{works}{0}
\bgroup
\font\test="#1" \test
\ifnum\XeTeXfonttype\font>0
  \ifnum\XeTeXcharglyph"#2>0
   \setcounter{works}{1}
  \fi
\fi
\egroup
\ifnum\theworks=1
  #3
\else
  #4
\fi
}

\sisetup{math-celsius=foo}

\foreach \phont in {Asana Math,XITS Math, STIXGeneral, Neo Euler}
{
\ifavailablethenelse{\phont}{2103}{\sisetup{math-celsius=℃}}{%
  \ifavailablethenelse{\phont}{00B0}{\sisetup{math-celsius=°C}}{
   \sisetup{math-celsius=nix}
  }
}
\setmathfont{\phont}
\(\SI{123}{\celsius}\)
}


\sisetup{text-celsius=bar}

\foreach \phont in {DejaVu Serif, Linux Libertine O, TeX Gyre Pagella, 
Arial}
{
\ifavailablethenelse{\phont}{2103}{\sisetup{text-celsius=℃}}{%
  \ifavailablethenelse{\phont}{00B0}{\sisetup{text-celsius=°C}}{
   \sisetup{text-celsius=niente}
  }
}
\setmainfont{\phont}
\SI{123}{\celsius}
}
\end{document}

Albeit, it seems to be slow.


More information about the XeTeX mailing list