\def\packagedate{9 September 2025}
\def\packageversion{2.0.2}
\def\packageshortdate{2025/09/09}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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.
% v1.4.1  Replaced deprecated ifxetex with iftex.
% v2.0    Rewritten for LuaTeX exclusively.
% v2.0.1  Ensured LuaTeX detection occurs first and moved join operator
%         substitution to relalg.sty.
% v2.0.2  Workaround for fontconfig stupidity with Iosevka SS03 on Linux.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesPackage{otagofonts}[\packageshortdate\space v\packageversion\space Otago standard fonts]


% This package requires LuaTeX, because XeTeX (a) doesn't really support
% OpenType variable fonts and (b) is no longer recommended by the LaTeX
% core team due to issues with PDF tagging.
% (see <https://www.latex-project.org/news/latex2e-news/ltnews40.pdf>)
% There were never really any good solutions for doing this in anything
% other than XeTeX/LuaTeX anyway, so the fallback for other engines has
% also been removed.
\RequirePackage{iftex}
\RequireLuaTeX


\RequirePackage{amsmath}
\RequirePackage{etoolbox}
\RequirePackage[no-math]{fontspec}
\RequirePackage{xstring}
\RequirePackage{unicode-math} % needs to go last


% 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 Ligatures=TeX only for the main and sans fonts,
% otherwise curly quotes will automatically appear in the mono font. Since
% the mono font is pretty much exclusively used for code listings, TeX
% ligatures are a bad thing.


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

% font-specific defaults
\defaultfontfeatures[Wickliffe Light]{
    BoldFont={Wickliffe Medium},
    ItalicFont={Wickliffe Light Italic},
    BoldItalicFont={Wickliffe Medium Italic}
}
\defaultfontfeatures[Crimson Pro Light]{
    BoldFont={Crimson Pro Medium},
    ItalicFont={Crimson Pro Light Italic},
    BoldItalicFont={Crimson Pro Medium Italic}
}

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        \setmainfont{#1}[Ligatures=TeX]%
        \def\mainfontname{#1}%
        \PackageInfo{otagofonts}{Using \mainfontname\space for main font}%
        \booltrue{foundmainfont}%
        \listbreak%
    }{}%
}

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


% Maths font.
% In principle we can do the same thing for the maths font, but we’re
% only going to use fonts built into TeXlive anyway, and we ideally
% want to choose a maths font that complements the main font. If we
% can't find a suitable maths font, default to Latin Modern Math,
% which looks reasonably OK alongside many fonts (especially for
% light weights). The maths font can always be manually overidden
% using \setmathfont in the document if a better alternative is available.
\setmathfont{latinmodern-math.otf}

\ifbool{foundmainfont}{%
    % The following conditions are naturally mutually exclusive, so no need
    % for complex if/then/else nesting.
    \IfSubStr{\mainfontname}{Light}{%
        % KpMath comes in several weights.
        \setmathfont{KpMath-Light.otf}[Scale=MatchLowercase]%
        \setmathfont{KpMath-Semibold.otf}[version=bold]%
        \setmathrm{KpRoman-Light.otf}%
        \setmathfontface\mathit{KpRoman-LightItalic.otf}%
        \setmathfontface\mathbf{KpRoman-Semibold.otf}%
    }{}%
    \IfEq{\mainfontname}{TeX Gyre Pagella}{%
        \setmathfont{TeX Gyre Pagella Math}%
    }{}%
}{%
    \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)%
    }%
}


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

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

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        \setsansfont{#1}[Scale=MatchLowercase, Ligatures=TeX]%
        \def\sansfontname{#1}%
        \PackageInfo{otagofonts}{Using \sansfontname\space 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}
\def\monofontname{none}
\PackageInfo{otagofonts}{Looking for suitable monospaced font}

% font-specific defaults
\defaultfontfeatures[Iosevka Light]{
    RawFeature={+ss03;-calt},
    BoldFont={Iosevka Medium},
    ItalicFont={Iosevka Light Italic},
    BoldItalicFont={Iosevka Medium Italic}
}
% fontconfig on Linux usually ends up naming Iosevka as "Iosevka SS03" :(
\defaultfontfeatures[Iosevka SS03 Light]{
    RawFeature={+ss03;-calt},
    BoldFont={Iosevka SS03 Medium},
    ItalicFont={Iosevka SS03 Light Italic},
    BoldItalicFont={Iosevka SS03 Medium Italic}
}
\defaultfontfeatures[Inconsolata Light]{
    BoldFont={Inconsolata Medium},
    ItalicFont={Inconsolata Light Italic},
    BoldItalicFont={Inconsolata Medium Italic}
}

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

\docsvlist{Iosevka Light,Iosevka SS03 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)%
    }%
}


\endinput
