% \iffalse meta-comment
%
% Copyright (C) 2015 by Benjamin Weiss <weissbenjamin@me.com>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Benjamin Weiss.
%
% This work consists of the files iffont.dtx and iffont.ins and the derived
% file iffont.sty.
%
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{iffont.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{iffont}
%<*package>
    [2015/11/11 1.0.0 Iffont package]
%</package>
%<*driver>
\documentclass{ltxdoc}
%\EnableCrossrefs
%\CodelineIndex
%\DisableCrossrefs
%\RecordChanges
%\OnlyDescription
\usepackage[parfill]{parskip}
\usepackage{setspace}
\onehalfspacing

\usepackage[no-math]{fontspec}
\setmainfont[BoldFont={Fira Sans}]{Fira Sans Light}
\setmonofont{Fira Mono}
\defaultfontfeatures{Ligatures=TeX}

\usepackage{metalogo}

\usepackage{enumitem}
\setlist[itemize]{noitemsep}
\setlist[enumerate]{noitemsep}

\usepackage[showerrors,table,hyperref]{xcolor}
\usepackage{iffont}

\definecolor{mDarkBrown}{HTML}{604c38}
\definecolor{mDarkTeal}{HTML}{23373b}
\definecolor{mLightBrown}{HTML}{EB811B}
\definecolor{mLightGreen}{HTML}{14B03D}
\definecolor{mBackground}{HTML}{FFFFFF}

\usepackage{listings}
\lstset{%
  gobble=2,
  language=[LaTeX]{TeX},
  basicstyle=\ttfamily\small,
  keywordstyle=\color{mLightBrown}\bfseries,
  commentstyle=\color{mLightGreen},
  stringstyle=\color{mLightGreen},
  backgroundcolor=\color{mBackground},
  numbers=none,
  numberstyle=\tiny\ttfamily,
  stepnumber=2,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  frame=none,
  framerule=1pt,
  tabsize=2,
  rulesep=5em,
  captionpos=b,
  breaklines=true,
  breakatwhitespace=false,
  framexleftmargin=0em,
  framexrightmargin=0em,
  xleftmargin=0em,
  xrightmargin=0em,
  aboveskip=1em,
  belowskip=1em,
  morekeywords={settofirstfound, setmainfont, boldfont, mainfont,
                FrutigerLight, newfontfamily, lightfont},
}
\lstMakeShortInline|

\usepackage[colorlinks=true,
            linkcolor=mLightBrown,
            menucolor=mLightBrown,
            pagecolor=mLightBrown,
            urlcolor=mLightBrown]{hyperref}

\usepackage{tabu}
\usepackage{longtable}
\usepackage{xstring}

\begin{document}
\DocInput{iffont.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \GetFileInfo{iffont.sty}
% \title{The \textsc{iffont} package}
% \author{Benjamin Weiss\\ \url{weissbenjamin@me.com}}
% \date{Version \fileversion~from \filedate}
%
% \maketitle
%
% \section{Introduction}
%
% Using OpenType fonts within \XeLaTeX\ or \LuaTeX\ comes with a lot of
% benefits, but also the problem, that desired fonts may not be installed on
% the system, the user choses for the compilation. In case they are, there can
% still be different revisions of a font with variant names, such as
% \textit{Frutiger LT Std}, \textit{Frutiger Neue LT Pro} and \textit{Frutiger
% Next Pro}.
%
% Inspired by a question on \href{http://tex.stackexchange.com/q/53443/}
% {Stack Exchange} and a discussion on the
% \href{http://tug.org/pipermail/xetex/2010-August/017833.html}{\XeTeX\
% Mailinglist}, this package addresses the problem with a set of easy to use
% macros: a macro to select the first font \XeLaTeX\ or \LuaTeX\ can find in a
% comma separated list and, additionally, a number of macro tests.
%
% \section{Select a Font from a List of Fonts}
%
% \DescribeMacro{\settofirstfound} This command defines a new macro with the
% first font found in the comma separated list. The list is provided as second
% argument. The macro can then be used to set the main font, set the sans
% font, define a new font family and so on. The following code shows how to
% set the main font as the Frutiger Font with the black font weight as bold
% font.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\mainfont}{Frutiger Next Pro,
%                             Frutiger Neue LT Pro,
%                             Frutiger LT Std}
% \settofirstfound{\boldfont}{Frutiger Next Pro Black,
%                             Frutiger Neue LT Pro Black,
%                             Frutiger LT Std 75 Black}
% \setmainfont[BoldFont=\boldfont]{\mainfont}
% \begin{document}
% \begin{itemize}
%   \item \mainfont
%   \item \textbf{\boldfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% Defining a new font family works just the same.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\lightfont}{Frutiger Next Pro Light,
%                              Frutiger Neue LT Pro Light,
%                              Frutiger LT Std 45 Light}
% \newfontfamily\FrutigerLight{\lightfont}
% \begin{document}
% \begin{itemize}
%   \item {\FrutigerLight \lightfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% \StopEventually{}
%
% \section{Macro Tests}
% In most cases the \lstinline|\settofirstfound| command is sufficient, but
% for the more demanding user there is also a set of macro tests. They are
% useful, if one wants to set specific font features only if the first choice
% font cannot be found. The original \textit{Frutiger LT Std} font for example
% does not support small caps. So in the following example the
% \lstinline|\textsc|command is redefined as uppercase with increased letter
% spacing, but only if the \textit{Frutiger Next Pro} font cannot be found.
% \begin{lstlisting}
% \documentclass{article}
% \usepackage{fontspec}
% \usepackage{iffont}
% \settofirstfound{\mainfont}{Frutiger Next Pro,
%                             Frutiger LT Std}
% \setmainfont{\mainfont}
% \ifxfontexists{Frutiger Next Pro}{%
%   \def\textsc#1{\addfontfeature{LetterSpace=5.0}%
%                 \MakeUppercase{#1}}
% }{}
% \begin{document}
% \begin{itemize}
%   \item \mainfont
%   \item \textsc{\mainfont}
% \end{itemize}
% \end{document}
% \end{lstlisting}
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
%
%
% \subsection{Required Packages}
% Load required packages.
%    \begin{macrocode}
\RequirePackage{fontspec}
\RequirePackage{etoolbox}
%    \end{macrocode}
%
% Init required variables.
%    \begin{macrocode}
\newtoggle{@iffont@fontfound}
\newcommand{\@iffont@firstfont}{Fira Sans}
\newcounter{@iffont@fontsnotfound}
%    \end{macrocode}
%
% \begin{macro}{\iffontsexist}
% If all fonts are found the commands in the second argument will be executed,
% otherwise the commands in the third argument.
%    \begin{macrocode}
\newcommand{\iffontsexist}[3]{
  \setcounter{@iffont@fontsnotfound}{0}
  \expandafter\forcsvlist\expandafter\@iffont@checkfont\expandafter{#1}
  \ifnumequal{\value{@iffont@fontsnotfound}}{0}{%
    #2
  }{%
    #3
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifxfontsexist}
% Same as \lstinline|\iffontsexist|, but negated.
%    \begin{macrocode}
\newcommand{\ifxfontsexist}[3]{
  \iffontsexist{#1}{#3}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\iffontexists}
% A simpler and therefore faster if clause, that only checks for a single font.
%    \begin{macrocode}
\newcommand{\iffontexists}[3]{
  \suppressfontnotfounderror=1
  \font\x = "#1" at 10pt
  \ifx\x\nullfont
    #3
  \else
    #2
  \fi
  \suppressfontnotfounderror=0
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifxfontexists}
% Same as \lstinline|\iffontexists|, but negated.
%    \begin{macrocode}
\newcommand{\ifxfontexists}[3]{
  \iffontexists{#1}{#3}{#2}
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\settofirstfound}
% Sets the macro in the first argument to the first font in the comma separated
% list of fonts in the second argument that is found.
%    \begin{macrocode}
\newcommand{\settofirstfound}[2]{
  \togglefalse{@iffont@fontfound}
  \expandafter\forcsvlist\expandafter\@iffont@checkfont\expandafter{#2}
  \let#1\@iffont@firstfont
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@iffont@checkfont}
% Checks if a font is found and increases the \lstinline|@iffont@fontsnotfound|
% counter if not. The first font found will be saved in
% \lstinline|\@iffont@firstfont|.
%    \begin{macrocode}
\newcommand{\@iffont@checkfont}[1]{
  \suppressfontnotfounderror=1
  \font\x = "#1" at 10pt
  \ifx\x\nullfont
    \stepcounter{@iffont@fontsnotfound}
  \else
    \nottoggle{@iffont@fontfound}{%
      \renewcommand{\@iffont@firstfont}{#1}
      \toggletrue{@iffont@fontfound}
    }{}
  \fi
  \suppressfontnotfounderror=0
}
%    \end{macrocode}
% \end{macro}
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput