\def\filedate{26 June 2024}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Set up official Otago fonts based on what’s available. Fall back to reasonable
% alternatives where possible.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Version History
% v1.0   Initial version, rebuilt from deprecated lecturecommon package.
% v1.1   Refactored font checking mechanism.
% v1.2   Fixed sans-serif scaling, added Noto Sans.
% v1.3   Added new Wickliffe fonts and fallbacks.
% v1.3.1 Switched serif font to Wickliffe Light.
% v1.3.2 Switched bold fonts to Wickliffe (Sans) Medium.
% v1.4   Switched from mathspec to unicode-math, further font tweaks.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesPackage{otagofonts}[2024/06/26 Otago standard fonts v1.4]


\RequirePackage{etoolbox}
\RequirePackage{ifxetex}
\RequirePackage{amsmath}


\ifxetex


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


% Try a few different fonts that are either the official Otago font or a
% reasonable alternative, using etoolbox's list looping macros. The tricky
% part is making it generic for all three font categories (main, sans, mono),
% and I can only successfully make it 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 main 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.


% Main font (body text).
\newbool{foundmainfont}
\PackageInfo{otagofonts}{Looking for suitable main font}

% font-specific defaults
\defaultfontfeatures[Wickliffe Light]{BoldFont={Wickliffe Medium}}

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        \setmainfont{#1}[Mapping=tex-text]%
        \PackageInfo{otagofonts}{Using #1 for main font}%
        \booltrue{foundmainfont}%
        \listbreak%
    }{}%
}

\docsvlist{Wickliffe Light,Crimson Pro,Crimson Text,Georgia,TeX Gyre Pagella,Times New Roman}

\ifbool{foundmainfont}{}{%
    \PackageWarning{otagofonts}{%
        Could not find any suitable main font.\MessageBreak%
        Falling back to LaTeX default.\MessageBreak%
        Recommended fonts:\MessageBreak%
        \space\space Wickliffe (official, proprietary)\MessageBreak%
        \space\space Crimson Pro (https://fonts.google.com/specimen/Crimson+Pro)%
        \space\space Crimson Text (https://fonts.google.com/specimen/Crimson+Text)%
    }%
}


% Maths font.
% Use Latin Modern, the unicode-math default. It looks reasonably OK alongside
% many fonts including Wickliffe Light. It can always be manually overidden
% using \setmathfont if a better alternative is available.
\setmathfont{latinmodern-math.otf}
\setmathrm{latinmodern-math.otf}


% Sans-serif font.
\newbool{foundsansfont}
\PackageInfo{otagofonts}{Looking for suitable sans-serif font}

% font-specific defaults
\defaultfontfeatures[Wickliffe Sans Light]{BoldFont={Wickliffe Sans Medium}}
\defaultfontfeatures[Open Sans Light]{BoldFont={Open Sans}}
\defaultfontfeatures[Noto Sans Light]{BoldFont={Noto Sans}}
\defaultfontfeatures[Roboto Light]{BoldFont={Roboto}}

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        \setsansfont{#1}[Scale=MatchLowercase,Mapping=tex-text]%
        \PackageInfo{otagofonts}{Using #1 for sans-serif font}%
        \booltrue{foundsansfont}%
        \listbreak%
    }{}%
}

\docsvlist{Wickliffe Sans Light,Open Sans Light,Noto Sans Light,Roboto Light,Arial}

\ifbool{foundsansfont}{}{%
    \PackageWarning{otagofonts}{Could not find any suitable sans-serif font.\MessageBreak%
        Falling back to LaTeX default.\MessageBreak%
        Recommended fonts:\MessageBreak%
        \space\space Wickliffe Sans (official, proprietary)\MessageBreak%
        \space\space Open Sans (https://fonts.google.com/specimen/Open+Sans)\MessageBreak%
        \space\space Roboto (https://fonts.google.com/specimen/Roboto)%
    }%
}


% Monospaced font, mainly for code listings.
\newbool{foundmonofont}
\PackageInfo{otagofonts}{Looking for suitable monospaced font}

% font-specific defaults
\defaultfontfeatures[Iosevka Light]{RawFeature={+ss03;-calt}}

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        \setmonofont{#1}[Scale=MatchLowercase,Mapping=]%
        \PackageInfo{otagofonts}{Using #1 for monospaced font}%
        \booltrue{foundmonofont}%
        \listbreak%
    }{}%
}

\docsvlist{Iosevka Light,Letter Gothic 12 Pitch,Inconsolata}

\ifbool{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)%
    }%
}


\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
