Newer
Older
LaTeX / lectures / otagofonts.sty
\def\filedate{20 January 2023}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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.
% v1.1   Refactored font checking mechanism.
% v1.2   Fixed sans-serif scaling, added Noto Sans.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}[2001/06/01]
\ProvidesPackage{otagofonts}[2023/01/20 Otago standard fonts v1.2]


\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}

\renewcommand*{\do}[1]{%
    \PackageInfo{otagofonts}{Trying #1...}
    \IfFontExistsTF{#1}{%
        % The following line is equivalent to:
        %   \setmainfont{fontname}[options]
        %   \setmathsfont(Latin,Digits){fontname}
        %   \setmathrm{fontname}
        \setprimaryfont[Mapping=tex-text]{#1}%
        \PackageInfo{otagofonts}{Using #1 for primary font}%
        \booltrue{foundprimaryfont}%
        \listbreak%
    }{}%
}

\docsvlist{Minion Pro,Crimson Pro,Crimson Text,Times New Roman}

\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%
}{%
    \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 Pro (https://fonts.google.com/specimen/Crimson+Pro)%
        \space\space Crimson Text (https://fonts.google.com/specimen/Crimson+Text)%
    }%
}


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

% font-specific defaults
\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}{%
        % Scale=Match{Upper|Lower}case doesn't work very well for the sans fonts.
        % By experimentation Scale=0.912 seems a good compromise, and eliminates the
        % weird upscaling of sans fonts in Texlive 2020, which didn't seem to affect
        % the serif or monospace fonts (?!?!).
        \setsansfont{#1}[Scale=0.912]%
        \PackageInfo{otagofonts}{Using #1 for sans-serif font}%
        \booltrue{foundsansfont}%
        \listbreak%
    }{}%
}

\docsvlist{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 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=MatchUppercase]%
        \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