\def\filedate{23 February 2021}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Set up official Otago fonts based on what’s available. Fall back to close
% facsimiles where possible.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Version History
% v1.0   Initial version, rebuilt from deprecated lecturecommon package.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesPackage{otagofonts}[2021/02/23 Otago standard fonts v1.0]


\RequirePackage{etoolbox}
\RequirePackage{ifxetex}
\RequirePackage{amsmath} % needs to be loaded before mathspec


\ifxetex


\RequirePackage[no-math]{fontspec}
\RequirePackage{mathspec}
\RequirePackage{xunicode}
\RequirePackage{xltxtra}


% Try a few different fonts that are either the official Otago font, or a reasonable
% facsimile thereof. Effectively done via a bunch of nested ifs. I did try using
% etoolbox's list looping macros, but the tricky part was making it generic for all
% three categories of font (primary, sans, mono), and I could only successfully make
% work for the first one encountered. The set of fonts is limited, so this is OK.
%
% Note: Explicitly specify Mapping=tex-text only for the primary and sans fonts,
% otherwise curly quotes will automatically appear in the mono font. Since
% this is pretty much exclusively used for code listings, this is a bad thing.

% Primary font (body text, maths).
\newbool{foundprimaryfont}
\PackageInfo{otagofonts}{Looking for suitable primary font}
\IfFontExistsTF{Minion Pro}{%
    % The following line is equivalent to:
    %   \setmainfont{Minion Pro}
    %   \setmathsfont(Latin,Digits){Minion Pro}
    %   \setmathrm{Minion Pro}
    \setprimaryfont[Mapping=tex-text]{Minion Pro}%
    \PackageInfo{otagofonts}{Using Minion Pro for primary font}%
    \booltrue{foundprimaryfont}%
}{%
    \IfFontExistsTF{Crimson Text}{%
        \setprimaryfont[Mapping=tex-text]{Crimson Text}%
        \PackageInfo{otagofonts}{Using Crimson Text for primary font}%
        \booltrue{foundprimaryfont}%
    }{%
        \IfFontExistsTF{Times New Roman}{%
            \setprimaryfont[Mapping=tex-text]{Times New Roman}%
            \PackageInfo{otagofonts}{Using Times New Roman for primary font}%
            \booltrue{foundprimaryfont}%
        }{%
            \PackageWarning{otagofonts}{%
                Could not find any suitable primary font.\MessageBreak%
                Falling back to LaTeX default.\MessageBreak%
                Recommended fonts:\MessageBreak%
                \space\space Minion Pro (official, commercial)\MessageBreak%
                \space\space Crimson Text (https://fonts.google.com/specimen/Crimson+Text)%
            }%
            \boolfalse{foundprimaryfont}%
        }%
    }%
}

\ifbool{foundprimaryfont}{%
    % Hack to prevent digits in hyperlinks from being set in the main font instead of the mono font.
    % Note: doesn't matter if this is executed multiple times. Only works if you use \setprimaryfont
    % or \setmathsfont first. From https://tex.stackexchange.com/a/99774.
    \makeatletter%
        \DeclareMathSymbol{0}{\mathalpha}{\eu@DigitsArabic@symfont}{`0}%
        \DeclareMathSymbol{1}{\mathalpha}{\eu@DigitsArabic@symfont}{`1}%
        \DeclareMathSymbol{2}{\mathalpha}{\eu@DigitsArabic@symfont}{`2}%
        \DeclareMathSymbol{3}{\mathalpha}{\eu@DigitsArabic@symfont}{`3}%
        \DeclareMathSymbol{4}{\mathalpha}{\eu@DigitsArabic@symfont}{`4}%
        \DeclareMathSymbol{5}{\mathalpha}{\eu@DigitsArabic@symfont}{`5}%
        \DeclareMathSymbol{6}{\mathalpha}{\eu@DigitsArabic@symfont}{`6}%
        \DeclareMathSymbol{7}{\mathalpha}{\eu@DigitsArabic@symfont}{`7}%
        \DeclareMathSymbol{8}{\mathalpha}{\eu@DigitsArabic@symfont}{`8}%
        \DeclareMathSymbol{9}{\mathalpha}{\eu@DigitsArabic@symfont}{`9}%
    \makeatother%
}

% Sans-serif font.
\newbool{foundsansfont}
\PackageInfo{otagofonts}{Looking for suitable sans-serif font}
\IfFontExistsTF{Open Sans Light}{%
    % Note scaling hack to fix the weird upscaling of Open Sans in Texlive 2020.
    % It doesn't affect the serif or monospace fonts (?!?!).
    \setsansfont[Mapping=tex-text, Scale=0.912, BoldFont={Open Sans}]{Open Sans Light}%
    % \setsansfont[Mapping=tex-text, Scale=MatchUppercase, BoldFont={Open Sans}]{Open Sans Light}%
    \PackageInfo{otagofonts}{Using Open Sans Light for sans-serif font}%
    \booltrue{foundsansfont}%
}{%
    \IfFontExistsTF{Roboto Light}{%
        \setsansfont[Mapping=tex-text, Scale=MatchUppercase, BoldFont={Roboto}]{Roboto Light}%
        \PackageInfo{otagofonts}{Using Roboto Light for sans-serif font}%
        \booltrue{foundsansfont}%
    }{%
        \IfFontExistsTF{Arial}{%
            \setsansfont[Mapping=tex-text, Scale=MatchUppercase]{Arial}%
            \PackageInfo{otagofonts}{Using Arial for sans-serif font}%
            \booltrue{foundsansfont}%
        }{%
            \PackageWarning{otagofonts}{Could not find any suitable sans-serif font.\MessageBreak%
                Falling back to LaTeX default.\MessageBreak%
                Recommended fonts:\MessageBreak%
                \space\space Open Sans (https://fonts.google.com/specimen/Open+Sans)\MessageBreak%
                \space\space Roboto (https://fonts.google.com/specimen/Roboto)%
            }%
            \boolfalse{foundsansfont}%
        }%
    }%
}

% Monospaced font, mainly for code listings.
\newbool{foundmonofont}
\PackageInfo{otagofonts}{Looking for suitable monospaced font}
\IfFontExistsTF{Iosevka Light}{%
    \setmonofont[Scale=MatchUppercase, Mapping=]{Iosevka Light}%
    \PackageInfo{otagofonts}{Using Iosevka Light for monospaced font}%
    \booltrue{foundmonofont}%
}{%
    \IfFontExistsTF{Letter Gothic 12 Pitch}{%
        \setmonofont[Scale=MatchUppercase, Mapping=]{Letter Gothic 12 Pitch}%
        \PackageInfo{otagofonts}{Using Letter Gothic 12 Pitch for monospaced font}%
        \booltrue{foundmonofont}%
    }{%
        \IfFontExistsTF{Inconsolata}{%
            \setmonofont[Scale=MatchUppercase, Mapping=]{Inconsolata}%
            \PackageInfo{otagofonts}{Using Inconsolata for monospaced font}%
            \booltrue{foundmonofont}%
        }{%
            \PackageWarning{otagofonts}{Could not find any suitable monospaced font.\MessageBreak%
                Falling back to LaTeX default.\MessageBreak%
                Recommended fonts:\MessageBreak%
                \space\space Iosevka (https://github.com/be5invis/Iosevka/releases, but complex!)\MessageBreak%
                \space\space Inconsolata (https://fonts.google.com/specimen/Inconsolata)%
            }%
            \boolfalse{foundmonofont}%
        }%
    }%
}

\else % assume PDFLaTeX otherwise


\RequirePackage[T1]{fontenc}
\RequirePackage{textcomp}
\RequirePackage{lmodern}
\RequirePackage{mathpazo}

\PackageWarning{otagofonts}{%
    Otago-compatible fonts are not yet supported for LaTeX\MessageBreak%
    or PDFLaTeX. XeLaTeX is preferred%
}

% Gill Sans to come...
% \renewcommand{\sfdefault}{}
% \renewcommand{\ttdefault}{blg}


\fi


\endinput
