% -*- coding: utf-8 -*-
%
% Some font hacks for my Sphinx PDF documents
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{my-doc-fonts}[%
  2026/05/15 v0.2.1 (Franz Glasner) Fonts for Sphinx generated PDFs]
\RequirePackage{xkeyval}
\RequirePackage{fontspec}
\IfFileExists{xcolor.sty}{%
% Should Sphinx load xcolor with its dvipsnames and svgnames options?
    \RequirePackage{xcolor}
}{%
    \RequirePackage{color}
}

\newif\if@mdf@mono@dejavusansmono\@mdf@mono@dejavusansmonofalse
\newif\if@mdf@mono@cmmono\@mdf@mono@cmmonofalse
\newif\if@mdf@mono@robotomono\@mdf@mono@robotomonofalse
\newif\if@mdf@mono@sscode\@mdf@mono@sscodefalse
\newif\if@mdf@mono@inconsolata\@mdf@mono@inconsolatafalse

\newif\if@mdf@WithDejaVuSansMono
\newif\if@mdf@WithCM
\newif\if@mdf@WithRobotoMono
\newif\if@mdf@WithIBMPlex
\newif\if@mdf@WithIBMPlexDejaVuSansMono
\newif\if@mdf@WithAdobeSourcePro
\newif\if@mdf@WithSpectral
\newif\if@mdf@WithCharter
\newif\if@mdf@WithLibertinus
\newif\if@mdf@WithGaramond

% A key compatible with options processing as option "fonts" but choices.
\define@choicekey*{my-doc-fonts.sty}{fonts}[\val\nr]{%
  % do not change anything: just deactivate the default "dejavusansmono"
  default,
  % no augmentation needed: all characters available
  dejavusansmono,  % use DejaVu Sans Mono as monospaced font
  % the fonts below are augmented for missing characters
  cm,              % assume CM fonts and just fix missing characters
  robotomono,      % use Roboto Mono as mono font
  ibmplex, plex,   % use the IBM Plex family (incl. Math)
  plex-dejavusansmono,  % use IBM Plex Serif/Sans and DejaVu Sans Mono
  adobesourcepro, adobesource,  % Use Adobe Source Serif/Sans/Mono
  spectral,        % use Spectral, Source Sans Pro and Source Code Pro
  charter,         % use XCharter (with XCharter Math), Lato and Inconsolata
  libertinus,      % use Libertinus family (incl. Math)
  garamond,        % EB Garamond (with Math), Libre Franklin and Inconsolata
  }{%
  % Not used for now : store the normalized option value (global and expanded)
  \xdef\@mdf@fonts{\val}%
  % Reset all option related font selection flags
  \@mdf@WithDejaVuSansMonofalse
  \@mdf@WithCMfalse
  \@mdf@WithRobotoMonofalse
  \@mdf@WithIBMPlexfalse
  \@mdf@WithIBMPlexDejaVuSansMonofalse
  \@mdf@WithAdobeSourceProfalse
  \@mdf@WithSpectralfalse
  \@mdf@WithCharterfalse
  \@mdf@WithLibertinusfalse
  \@mdf@WithGaramondfalse
  % Now set the needed font selection flag
  \ifcase\nr\relax
    \relax
  \or
    \@mdf@WithDejaVuSansMonotrue
  \or
    \@mdf@WithCMtrue
  \or
    \@mdf@WithRobotoMonotrue
  \or
    \@mdf@WithIBMPlextrue
  \or
    \@mdf@WithIBMPlextrue
    % Normalize the stored string
    \gdef\@mdf@fonts{ibmplex}
  \or
    \@mdf@WithIBMPlexDejaVuSansMonotrue
  \or
    \@mdf@WithAdobeSourceProtrue
  \or
    \@mdf@WithAdobeSourceProtrue
    % Normalize the stored string
    \gdef\@mdf@fonts{adobesourcepro}
  \or
   \@mdf@WithSpectraltrue
  \or
    \@mdf@WithChartertrue
  \or
    \@mdf@WithLibertinustrue
  \or
    \@mdf@WithGaramondtrue
  \fi
}

%
% By default use the safe option dejavusansmono because is contains
% all characters without any hacks.
%
\ExecuteOptionsX{fonts=dejavusansmono}
\ProcessOptionsX\relax

\if@mdf@WithDejaVuSansMono
  % default but with DejaVu Sans Mono as monospaced font
  %\RequirePackage[DefaultFeatures={Scale=0.92},mono=false]{plex-otf}
  %\RequirePackage[mono=true,serif=false,sans=false,math=false]{dejavu-otf}  % all symbols are available
  \@mdf@mono@dejavusansmonotrue
\fi
\if@mdf@WithCM
  \@mdf@mono@cmmonotrue
\fi
\if@mdf@WithRobotoMono
  \@mdf@mono@robotomonotrue
\fi
%
%
\if@mdf@WithIBMPlex
  \RequirePackage{newunicodechar}
  \RequirePackage[DefaultFeatures={Scale=0.92}]{plex-otf}
  % Dont keep all default font features from plex-otf
  \defaultfontfeatures{}
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
    Scale=MatchLowercase,
    Ligatures=TeX,
  ]
  \newunicodechar{▷}{{\DejaVuSansMono ▷}}% U+25B7
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{◆}{{\DejaVuSansMono ◆}}% U+25C6
  \newunicodechar{▪}{{\DejaVuSansMono ▪}}% U+25AA
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{⇒}{{\DejaVuSansMono ⇒}}% U+21D2
  \newunicodechar{⇔}{{\DejaVuSansMono ⇔}}% U+21D4
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C
  %\newunicodechar{␣}{{\DejaVuSansMono ␣}}% U+2423  Missing!
  %\newunicodechar{^^^^2423}{{\DejaVuSansMono^^^^2423}}% U+2423
  %\newunicodechar{␣}{\textvisiblespace}% U+2423
  %\catcode`^^^^2423\active\protected\def^^^^2423{\textvisiblespace}

  \PassOptionsToPackage{%
    verbatimvisiblespace={\textcolor{red}{{\DejaVuSansMono^^^^2423}}},%
    verbatimcontinued={\textcolor{red}{{\DejaVuSansMono^^^^21aa}}}}%
    {sphinx}
\fi
\if@mdf@WithIBMPlexDejaVuSansMono
  \RequirePackage[DefaultFeatures={Scale=0.92},mono=false]{plex-otf}
  \@mdf@mono@dejavusansmonotrue
\fi
\if@mdf@WithAdobeSourcePro
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular}}
  \setmainfont{Source Serif Pro}[
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \setsansfont{Source Sans Pro}[
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \@mdf@mono@sscodetrue
\fi
\if@mdf@WithSpectral
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular}}
  \setmainfont{Spectral}[
    UprightFont = * Regular,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \setsansfont{Source Sans Pro}[
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \@mdf@mono@sscodetrue
\fi
\if@mdf@WithCharter
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular}}
  \RequirePackage{xcharter-otf}% loads also XCharter Math
  \setsansfont{Lato}[
    UprightFont = * Regular,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
    Scale = MatchLowercase,
  ]
  \@mdf@mono@inconsolatatrue
\fi
\if@mdf@WithLibertinus
  \RequirePackage{newunicodechar}
  \RequirePackage[lining,tabular]{libertinus-otf}
  \defaultfontfeatures{}
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
    Scale=MatchLowercase,
    Ligatures=TeX,
  ]
  \newunicodechar{▷}{{\DejaVuSansMono ▷}}% U+25B7
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{◆}{{\DejaVuSansMono ◆}}% U+25C6
  \newunicodechar{▪}{{\DejaVuSansMono ▪}}% U+25AA
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{⇒}{{\DejaVuSansMono ⇒}}% U+21D2
  \newunicodechar{⇔}{{\DejaVuSansMono ⇔}}% U+21D4
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C
  \newunicodechar{≠}{{\DejaVuSansMono ≠}}% U+2260
  \newunicodechar{≤}{{\DejaVuSansMono ≤}}% U+2264
  \newunicodechar{≥}{{\DejaVuSansMono ≥}}% U+2265
  %\newunicodechar{␣}{{\DejaVuSansMono ␣}}% U+2423  Missing!
  %\newunicodechar{^^^^2423}{{\DejaVuSansMono^^^^2423}}% U+2423
  %\newunicodechar{␣}{\textvisiblespace}% U+2423
  %\catcode`^^^^2423\active\protected\def^^^^2423{\textvisiblespace}

  \PassOptionsToPackage{%
    verbatimvisiblespace={\textcolor{red}{{\DejaVuSansMono^^^^2423}}},%
    verbatimcontinued={\textcolor{red}{{\DejaVuSansMono^^^^21aa}}}}%
    {sphinx}
\fi

\if@mdf@WithGaramond
  \RequirePackage[lining,tabular]{ebgaramond}
  \RequirePackage{unicode-math}
  \setmathfont{Garamond-Math}[Scale=MatchLowercase]
  \setsansfont{Libre Franklin}[Scale=MatchLowercase,Ligatures=TeX]
  \@mdf@mono@inconsolatatrue
\fi

\defaultfontfeatures{}

\if@mdf@mono@dejavusansmono
  % automatically applied with Scale=MatchLowercase,...
  \RequirePackage[mono=true,serif=false,sans=false,math=false]{dejavu-otf}  % all symbols are available
  \fi

\if@mdf@mono@cmmono
  \RequirePackage{newunicodechar}
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular},Scale=MatchLowercase}
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
  ]
  \newunicodechar{▷}{{\DejaVuSansMono ▷}}% U+25B7
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{◆}{{\DejaVuSansMono ◆}}% U+25C6
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{⇒}{{\DejaVuSansMono ⇒}}% U+21D2
  \newunicodechar{⇔}{{\DejaVuSansMono ⇔}}% U+21D4
  \newunicodechar{▪}{{\DejaVuSansMono ▪}}% U+25AA
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C
  \newunicodechar{≠}{{\DejaVuSansMono ≠}}% U+2260
  \newunicodechar{≤}{{\DejaVuSansMono ≤}}% U+2264
  \newunicodechar{≥}{{\DejaVuSansMono ≥}}% U+2265
  \PassOptionsToPackage{%
    verbatimvisiblespace={\textcolor{red}{{\DejaVuSansMono^^^^2423}}},%
    verbatimcontinued={\textcolor{red}{{\DejaVuSansMono^^^^21aa}}}}%
    {sphinx}
\fi

\if@mdf@mono@sscode
  \RequirePackage{newunicodechar}
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular},Scale=MatchLowercase}
  \setmonofont{Source Code Pro}[
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
  ]
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{⇒}{{\DejaVuSansMono ⇒}}% U+21D2
  \newunicodechar{⇔}{{\DejaVuSansMono ⇔}}% U+21D4
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C
  \PassOptionsToPackage{%
    verbatimvisiblespace={\textcolor{red}{{\DejaVuSansMono^^^^2423}}},%
    verbatimcontinued={\textcolor{red}{{\DejaVuSansMono^^^^21aa}}}}%
    {sphinx}
\fi

\if@mdf@mono@inconsolata
  \RequirePackage{newunicodechar}
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular},Scale=MatchLowercase}
  \setmonofont{Inconsolata LGC Nerd Font Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
  ]
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C
\fi

\if@mdf@mono@robotomono
  \RequirePackage{newunicodechar}
  \defaultfontfeatures{Ligatures=TeX,Numbers={Lining,Tabular},Scale=MatchLowercase}
  \setmonofont{Roboto Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Italic,
    BoldItalicFont = * Bold Italic,
  ]
  \newfontfamily\DejaVuSansMono{DejaVu Sans Mono}[%
    UprightFont = *,
    BoldFont = * Bold,
    ItalicFont = * Oblique,
    BoldItalicFont = * Bold Oblique,
  ]
  \newunicodechar{▷}{{\DejaVuSansMono ▷}}% U+25B7
  \newunicodechar{∶}{{\DejaVuSansMono ∶}}% U+2236
  \newunicodechar{◆}{{\DejaVuSansMono ◆}}% U+25C6
  \newunicodechar{⟵}{{\DejaVuSansMono ⟵}}% U+27F5
  \newunicodechar{⟶}{{\DejaVuSansMono ⟶}}% U+27F6
  \newunicodechar{⟷}{{\DejaVuSansMono ⟷}}% U+27F7
  \newunicodechar{⇒}{{\DejaVuSansMono ⇒}}% U+21D2
  \newunicodechar{⇔}{{\DejaVuSansMono ⇔}}% U+21D4
  \newunicodechar{▪}{{\DejaVuSansMono ▪}}% U+25AA
  \newunicodechar{≟}{{\DejaVuSansMono ≟}}% U+225F
  \newunicodechar{∈}{{\DejaVuSansMono ∈}}% U+2208
  \newunicodechar{∌}{{\DejaVuSansMono ∌}}% U+220C

  \PassOptionsToPackage{%
    verbatimvisiblespace={\textcolor{red}{{\DejaVuSansMono^^^^2423}}},%
    verbatimcontinued={\textcolor{red}{{\DejaVuSansMono^^^^21aa}}}}%
    {sphinx}
\fi

\defaultfontfeatures{}

\def\testmdf{
  Normalized: \@mdf@fonts\newline
}

\endinput
