\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{josefin}
    [2022/10/01 (Bob Tennent and autoinst)  Style file for Josefin Sans fonts.]

\RequirePackage{ifxetex,ifluatex,xkeyval}

\newif\ifjosefin@ttf 
\ifxetex
  \josefin@ttftrue
\else\ifluatex
  \josefin@ttftrue
\else  % [pdf]LaTeX
  \josefin@ttffalse
\fi\fi

\newif\ifjosefin@medium  \josefin@mediumfalse
\newif\ifjosefin@semibold  \josefin@semiboldfalse
\newif\ifjosefin@light  \josefin@lightfalse
\newif\ifjosefin@extralight  \josefin@extralightfalse
\newif\ifjosefin@thin  \josefin@thinfalse
\newif\ifjosefin@default \josefin@defaultfalse

\newcommand*{\JosefinSans@scale}{1}
\RequirePackage{xkeyval}
\DeclareOptionX{scaled}{\renewcommand*{\JosefinSans@scale}{#1}}
\DeclareOptionX{scale}{\renewcommand*{\JosefinSans@scale}{#1}}
\DeclareOptionX{type1}{\josefin@ttffalse}

\DeclareOptionX{medium}{\josefin@mediumtrue}
\DeclareOptionX{mb}{\josefin@mediumtrue}
\DeclareOptionX{semibold}{\josefin@semiboldtrue}
\DeclareOptionX{sb}{\josefin@semiboldtrue}
\DeclareOptionX{light}{\josefin@lighttrue}
\DeclareOptionX{l}{\josefin@lighttrue}
\DeclareOptionX{extralight}{\josefin@extralighttrue}
\DeclareOptionX{thin}{\josefin@thintrue}
\DeclareOptionX{default}{\josefin@defaulttrue}
\DeclareOptionX{sfdefault}{\josefin@defaulttrue}


\ProcessOptionsX

\ifjosefin@ttf
  \def\josefin@boldstyle{Bold}
  \ifjosefin@medium\def\josefin@boldstyle{Medium}\fi
  \ifjosefin@semibold\def\josefin@boldstyle{SemiBold}\fi

  \def\josefin@regstyle{Regular}
  \ifjosefin@light\def\josefin@regstyle{Light}\fi
  \ifjosefin@extralight\def\josefin@regstyle{ExtraLight}\fi
  \ifjosefin@thin\def\josefin@regstyle{Thin}\fi

\else % type1

  \def\bfseries@sf{b}
  \ifjosefin@medium\def\bfseries@sf{medium}\fi
  \ifjosefin@semibold\def\bfseries@sf{sb}\fi

  \def\mdseries@sf{m}
  \ifjosefin@light\def\mdseries@sf{l}\fi
  \ifjosefin@extralight\def\mdseries@sf{el}\fi
  \ifjosefin@thin\def\mdseries@sf{thin}\fi
\fi


\ifjosefin@ttf
  \RequirePackage{fontspec}
\else
  \RequirePackage{fontaxes}
\fi

\ifjosefin@ttf
  \def\josefin@regular{Regular}
  \defaultfontfeatures{
        Ligatures = TeX ,
        Scale     = \JosefinSans@scale ,
        Extension = .ttf }
  \setsansfont
      [ UprightFont    = *-\josefin@regstyle ,
        ItalicFont = *-\ifx\josefin@regstyle\josefin@regular\else\josefin@regstyle\fi Italic,
        BoldFont    = *-\josefin@boldstyle ,
        BoldItalicFont     = *-\josefin@boldstyle Italic ,
      ]
      {JosefinSans}
  % grab current family in case of subsequent change:
  \let\josefinfamily\sfdefault  
  \ifjosefin@default\renewcommand*\familydefault{\josefinfamily}\fi

  \providecommand\sufigures{\addfontfeatures{VerticalPosition=Superior}}
  \newfontfamily\josefinthin
      [ 
        UprightFont    = *-Thin ,
        ItalicFont     = *-ThinItalic ,
        BoldFont    = *-\josefin@boldstyle ,
        BoldItalicFont     = *-\josefin@boldstyle Italic ,
      ]
      {JosefinSans}
  \newfontfamily\josefinlight
      [ 
        UprightFont    = *-Light ,
        ItalicFont     = *-LightItalic ,
        BoldFont    = *-\josefin@boldstyle ,
        BoldItalicFont     = *-\josefin@boldstyle Italic ,
      ]
      {JosefinSans}
  \newfontfamily\josefinextralight
      [ 
        UprightFont    = *-ExtraLight ,
        ItalicFont     = *-ExtraLightItalic ,
        BoldFont    = *-\josefin@boldstyle ,
        BoldItalicFont     = *-\josefin@boldstyle Italic ,
      ]
      {JosefinSans}
  \newfontfamily\josefinmedium
      [ 
        UprightFont    = *-Medium ,
        ItalicFont     = *-MediumItalic ,
        BoldFont    = *-\josefin@boldstyle ,
        BoldItalicFont     = *-\josefin@boldstyle Italic ,
      ]
      {JosefinSans}
  \newfontfamily\josefinsemibold
      [ 
        UprightFont    = *-SemiBold ,
        ItalicFont     = *-SemiBoldItalic ,
      ]
      {JosefinSans}
\else % type1


  \def\josefin@family{JosefinSans-LF}
  \renewcommand*\sfdefault{\josefin@family}
  \ifjosefin@default\edef\familydefault{\sfdefault}\edef\seriesdefault{\mdseries@sf}\fi

  \def\josefinthin{\fontfamily{\josefin@family}\fontseries{thin}\selectfont}
  \def\josefinlight{\fontfamily{\josefin@family}\fontseries{l}\selectfont}
  \def\josefinextralight{\fontfamily{\josefin@family}\fontseries{el}\selectfont}
  \def\josefinmedium{\fontfamily{\josefin@family}\fontseries{medium}\selectfont}
  \def\josefinsemibold{\fontfamily{\josefin@family}\fontseries{sb}\selectfont}

\fi

\ifjosefin@ttf
  \defaultfontfeatures{}
\fi

\endinput
