\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{plex-sans}
    [2023/09/30  (Bob Tennent) Style file for IBMPlexSans fonts]

\RequirePackage{ifxetex,ifluatex,xkeyval,textcomp}

\newif\ifplexsans@otf 
\ifxetex
  \plexsans@otftrue
\else\ifluatex
  \plexsans@otftrue
\else  % [pdf]LaTeX
  \plexsans@otffalse
\fi\fi

\newcommand*{\IBMPlexSans@scale}{1}
\DeclareOptionX{scale}{\renewcommand*{\IBMPlexSans@scale}{#1}}

\newif\ifplexsans@sfdefault \plexsans@sfdefaultfalse
\newif\ifplexsans@condensed \plexsans@condensedfalse
\newif\ifplexsans@thin \plexsans@thinfalse
\newif\ifplexsans@extralight \plexsans@extralightfalse
\newif\ifplexsans@light \plexsans@lightfalse
\newif\ifplexsans@textmd \plexsans@textmdfalse
\newif\ifplexsans@text \plexsans@textfalse
\newif\ifplexsans@mediumbold \plexsans@mediumboldfalse
\newif\ifplexsans@semibold \plexsans@semiboldfalse

\DeclareOptionX{type1}{\plexsans@otffalse}
\DeclareOptionX{sfdefault}{\plexsans@sfdefaulttrue}
\DeclareOptionX{condensed}{\plexsans@condensedtrue}

\DeclareOptionX{thin}{\plexsans@thintrue}
\DeclareOptionX{t}{\plexsans@thintrue}
\DeclareOptionX{extralight}{\plexsans@extralighttrue}
\DeclareOptionX{el}{\plexsans@extralighttrue}
\DeclareOptionX{light}{\plexsans@lighttrue}
\DeclareOptionX{l}{\plexsans@lighttrue}
\DeclareOptionX{textmd}{\plexsans@textmdtrue}
\DeclareOptionX{txmd}{\plexsans@textmdtrue}
\DeclareOptionX{text}{\plexsans@texttrue}
\DeclareOptionX{tx}{\plexsans@texttrue}
\DeclareOptionX{medium}{\plexsans@mediumboldtrue}
\DeclareOptionX{mb}{\plexsans@mediumboldtrue}
\DeclareOptionX{semibold}{\plexsans@semiboldtrue}
\DeclareOptionX{sb}{\plexsans@semiboldtrue}


\ProcessOptionsX\relax

\ifplexsans@otf
  \RequirePackage{fontspec}
\else % type1
  \RequirePackage{fontenc,fontaxes,mweights}
  \fa@naming@exception{figures}{{superior}{proportional}}{Sup}
  \fa@naming@exception{figures}{{superior}{tabular}}{Sup}
  \def\sufigures{\@nomath\sufigures
      \fontfigurestyle{superior}\selectfont}
  \DeclareTextFontCommand{\textsu}{\sufigures}
  \let\textsuperior\textsu
\fi

\ifplexsans@otf

  \def\plexsans@medstyle{Regular}
  \ifplexsans@thin\def\plexsans@medstyle{Thin}
  \else\ifplexsans@extralight\def\plexsans@medstyle{ExtraLight}
  \else\ifplexsans@light\def\plexsans@medstyle{Light}
  \else\ifplexsans@textmd\def\plexsans@medstyle{Text}\fi\fi\fi\fi
  \def\plexsans@boldstyle{Bold}
  \ifplexsans@text\def\plexsans@boldstyle{Text}
  \else\ifplexsans@mediumbold\def\plexsans@boldstyle{Medium}
  \else\ifplexsans@semibold\def\plexsans@boldstyle{SemiBold}\fi\fi\fi

\else % type1
  
  \ifplexsans@condensed
    \def\mdseries@sf{c}
    \ifplexsans@thin\def\mdseries@sf{tc}
    \else\ifplexsans@extralight\def\mdseries@sf{elc}
    \else\ifplexsans@light\def\mdseries@sf{lc}
    \else\ifplexsans@textmd\def\mdseries@sf{textcondensed}\fi\fi\fi\fi
    \def\bfseries@sf{bc}
    \ifplexsans@text\def\bfseries@sf{textcondensed}
    \else\ifplexsans@mediumbold\def\bfseries@sf{mc}
    \else\ifplexsans@semibold\def\bfseries@sf{sbc}\fi\fi\fi
  \else
    \def\mdseries@sf{m}
    \ifplexsans@thin\def\mdseries@sf{t}
    \else\ifplexsans@extralight\def\mdseries@sf{el}
    \else\ifplexsans@light\def\mdseries@sf{l}
    \else\ifplexsans@textmd\def\mdseries@sf{text}\fi\fi\fi\fi
    \def\bfseries@sf{b}
    \ifplexsans@text\def\bfseries@sf{text}
    \else\ifplexsans@mediumbold\def\bfseries@sf{medium}
    \else\ifplexsans@semibold\def\bfseries@sf{sb}\fi\fi\fi
  \fi
\fi

\ifplexsans@otf
  \def\plexsans@regular{Regular}
  \defaultfontfeatures{
      Ligatures = TeX ,
      Scale     = \IBMPlexSans@scale ,
      Extension = .otf }
  \ifplexsans@condensed
     \setsansfont 
        [ UprightFont    = *-\plexsans@medstyle ,
          ItalicFont     = *-\ifx\plexsans@medstyle\plexsans@regular 
                                Italic
                             \else\plexsans@medstyle Italic\fi ,
          BoldFont       = *-\plexsans@boldstyle , 
          BoldItalicFont = *-\plexsans@boldstyle Italic ]
        {IBMPlexSansCondensed}
  \else
     \setsansfont 
        [ UprightFont    = *-\plexsans@medstyle ,
          ItalicFont     = *-\ifx\plexsans@medstyle\plexsans@regular 
                                Italic
                             \else\plexsans@medstyle Italic\fi ,
          BoldFont       = *-\plexsans@boldstyle , 
          BoldItalicFont = *-\plexsans@boldstyle Italic ]
        {IBMPlexSans}
  \fi
  \let\plexsansfamily\sfdefault
  \ifplexsans@sfdefault\renewcommand*\familydefault{\plexsansfamily}\fi
  \newfontfamily\plexsans
      [ UprightFont    = *-\plexsans@medstyle ,
        ItalicFont     = *-\ifx\plexsans@medstyle\plexsans@regular 
                              Italic
                           \else\plexsans@medstyle Italic\fi ,
        BoldFont       = *-\plexsans@boldstyle , 
        BoldItalicFont = *-\plexsans@boldstyle Italic ]
        {IBMPlexSans}
  \newfontfamily\plexsanscondensed
      [ UprightFont    = *-\plexsans@medstyle ,
        ItalicFont     = *-\ifx\plexsans@medstyle\plexsans@regular 
                              Italic
                           \else\plexsans@medstyle Italic\fi ,
        BoldFont       = *-\plexsans@boldstyle , 
        BoldItalicFont = *-\plexsans@boldstyle Italic ]
        {IBMPlexSansCondensed}

\else % type1
  \def\plexsansfamily{IBMPlexSans-TLF}
  \newcommand*\plexsans{%
    \def\mdseries@sf{m}
    \ifplexsans@thin\def\mdseries@sf{ul}
    \else\ifplexsans@extralight\def\mdseries@sf{el}
    \else\ifplexsans@light\def\mdseries@sf{l}
    \else\ifplexsans@textmd\def\mdseries@sf{text}\fi\fi\fi\fi
    \def\bfseries@sf{bold}
    \ifplexsans@text\def\bfseries@sf{text}
    \else\ifplexsans@mediumbold\def\bfseries@sf{medium}
    \else\ifplexsans@semibold\def\bfseries@sf{sb}\fi\fi\fi
    \sffamily
  }
  \newcommand*\plexsanscondensed{%
    \def\mdseries@sf{c}
    \ifplexsans@thin\def\mdseries@sf{ulc}
    \else\ifplexsans@extralight\def\mdseries@sf{elc}
    \else\ifplexsans@light\def\mdseries@sf{lc}
    \else\ifplexsans@textmd\def\mdseries@sf{text}\fi\fi\fi\fi
    \def\bfseries@sf{bc}
    \ifplexsans@text\def\bfseries@sf{textcondensed}
    \else\ifplexsans@mediumbold\def\bfseries@sf{mediumcondensed}
    \else\ifplexsans@semibold\def\bfseries@sf{sbc}\fi\fi\fi
    \sffamily
  }   
  \renewcommand*\sfdefault{\plexsansfamily} 
  \ifplexsans@sfdefault\edef\familydefault{\sfdefault}\edef\seriesdefault{\mdseries@sf}\fi
\fi

\newcommand*\plexsanslgr{\fontencoding{LGR}\fontfamily{IBMPlexSans-TLF}\selectfont} % even for otf

\ifplexsans@otf
  % turn off defaults in case other fonts are selected:
  \defaultfontfeatures{}
\fi

\endinput
