% Source Serif Pro for LaTeX
%
% Silke Hofstra, tex@slxh.nl
%
% This file selects either the OTF or the TYPE1 fonts based on
% the LaTeX engine used.
%
% Licenced under the LPPL v1.3 or higher.
%
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{sourceserifpro}[2018/11/10 v1.4 Adobe's Source Serif Pro typeface]

\RequirePackage{ifxetex,ifluatex,xkeyval}

% Switches
\newif\ifsourceserifpro@otf
\newif\ifsourceserifpro@default
\newif\ifsourceserifpro@rmdefault
\newif\ifsourceserifpro@oldstyle
\newif\ifsourceserifpro@tabular
\newif\ifsourceserifpro@black
\newif\ifsourceserifpro@semibold
\newif\ifsourceserifpro@light
\newif\ifsourceserifpro@extralight

% Scaling command
\newcommand*{\SourceSerifPro@scale}{1}

% Use OpenType fonts for XeTeX and LuaTeX.
\ifxetex
	\sourceserifpro@otftrue
\else
	\ifluatex
		\sourceserifpro@otftrue
	\fi
\fi

\DeclareOptionX{lining}      {\sourceserifpro@oldstylefalse}
\DeclareOptionX{nf}          {\sourceserifpro@oldstylefalse}
\DeclareOptionX{lf}          {\sourceserifpro@oldstylefalse}
\DeclareOptionX{oldstyle}    {\sourceserifpro@oldstyletrue}
\DeclareOptionX{osf}         {\sourceserifpro@oldstyletrue}
\DeclareOptionX{tabular}     {\sourceserifpro@tabulartrue}
\DeclareOptionX{proportional}{\sourceserifpro@tabularfalse}
\DeclareOptionX{black}       {\sourceserifpro@blacktrue \sourceserifpro@semiboldfalse}
\DeclareOptionX{semibold}    {\sourceserifpro@blackfalse\sourceserifpro@semiboldtrue}
\DeclareOptionX{bold}        {\sourceserifpro@blackfalse\sourceserifpro@semiboldfalse}
\DeclareOptionX{light}       {\sourceserifpro@lighttrue \sourceserifpro@extralightfalse}
\DeclareOptionX{extralight}  {\sourceserifpro@lightfalse\sourceserifpro@extralighttrue}
\DeclareOptionX{regular}     {\sourceserifpro@lightfalse\sourceserifpro@extralightfalse}
\DeclareOptionX{scale}       {\renewcommand*{\SourceSerifPro@scale}{#1}}
\DeclareOptionX{scaled}      {\renewcommand*{\SourceSerifPro@scale}{#1}}
\DeclareOptionX{default}     {\sourceserifpro@defaulttrue}
\DeclareOptionX{rmdefault}   {\sourceserifpro@rmdefaulttrue}
\DeclareOptionX{normdefault} {\sourceserifpro@rmdefaultfalse}
\DeclareOptionX{type1}       {\sourceserifpro@otffalse}
\DeclareOptionX{t1}          {\sourceserifpro@otffalse}
\DeclareOptionX{opentype}    {\sourceserifpro@otftrue}
\DeclareOptionX{otf}         {\sourceserifpro@otftrue}

% Default options
\ExecuteOptionsX{rmdefault,lining,proportional,bold,regular}
\ProcessOptionsX\relax

% Load OpenType files
\ifsourceserifpro@otf
	\RequirePackage{fontspec}

	% Oldstyle/Lining
	\ifsourceserifpro@oldstyle
		\def\sourceserifpro@figurestyle{OldStyle}
	\else
		\def\sourceserifpro@figurestyle{}
	\fi

	% Tabular/Proportional
	\ifsourceserifpro@tabular
		\def\sourceserifpro@figurealign{Monospaced}
	\else
		\def\sourceserifpro@figurealign{Proportional}
	\fi

	% Black/Semibold/Bold
	\ifsourceserifpro@black
		\def\sourceserifpro@boldstyle{Black}
	\else
		\ifsourceserifpro@semibold
			\def\sourceserifpro@boldstyle{Semibold}
		\else
			\def\sourceserifpro@boldstyle{Bold}
		\fi
	\fi

	% Light/ExtraLight/Regular
	\ifsourceserifpro@light
		\def\sourceserifpro@regstyle{Light}
	\else
		\ifsourceserifpro@extralight
			\def\sourceserifpro@regstyle{ExtraLight}
		\else
			\def\sourceserifpro@regstyle{Regular}
		\fi
	\fi

	% Shared features
	\defaultfontfeatures{
		Ligatures = TeX ,
		Numbers   = {\sourceserifpro@figurealign , \sourceserifpro@figurestyle } ,
		Scale     = \SourceSerifPro@scale ,
		Extension = .otf }

	% Serif font
	\ifsourceserifpro@rmdefault
		\setmainfont
			[ UprightFont    = *-\sourceserifpro@regstyle ,
			  ItalicFont     = *-\sourceserifpro@regstyle It ,
			  BoldFont       = *-\sourceserifpro@boldstyle ,
			  BoldItalicFont = *-\sourceserifpro@boldstyle It ]
			{SourceSerifPro}
	\fi

	% Font families
	% Regular/Bold
	\newfontfamily\sourceserifpro
		[ UprightFont    = *-Regular ,
		  ItalicFont     = *-RegularIt ,
		  BoldFont       = *-Bold ,
		  BoldItalicFont = *-BoldIt ]
		{SourceSerifPro}

	% Light/Semibold
	\newfontfamily\sourceserifprolight
		[ UprightFont    = *-Light ,
		  ItalicFont     = *-LightIt ,
		  BoldFont       = *-Semibold ,
		  BoldItalicFont = *-SemiboldIt ]
		{SourceSerifPro}

	% ExtraLight/Black
	\newfontfamily\sourceserifproextreme
		[ UprightFont    = *-ExtraLight ,
		  ItalicFont     = *-ExtraLightIt ,
		  BoldFont       = *-Black ,
		  BoldItalicFont = *-BlackIt ]
		{SourceSerifPro}

	% Lining figures
	\newfontfamily\sourceserifprolf
		[ Numbers        = \sourceserifpro@figurealign ,
		  UprightFont    = *-\sourceserifpro@regstyle ,
		  ItalicFont     = *-\sourceserifpro@regstyle It ,
		  BoldFont       = *-\sourceserifpro@boldstyle ,
		  BoldItalicFont = *-\sourceserifpro@boldstyle It ]
		{SourceSerifPro}

	% Dont keep all default font features
	\defaultfontfeatures{}

% Load type1 font.
\else
	\RequirePackage[OT1,LY1,T1]{fontenc}
	\RequirePackage{mweights}

	% Oldstyle/Lining
	\ifsourceserifpro@oldstyle
		\def\sourceserifpro@figurestyle{OsF}
	\else
		\def\sourceserifpro@figurestyle{LF}
	\fi

	% Tabular/Proportional
	\ifsourceserifpro@tabular
		\def\sourceserifpro@figurealign{T}
	\else
		\def\sourceserifpro@figurealign{}
	\fi

	% Black/Semibold/Bold
	\ifsourceserifpro@black
		\def\bfseries@rm{k}
	\else
		\ifsourceserifpro@semibold
			\def\bfseries@rm{sb}
		\else
			\def\bfseries@rm{b}
		\fi
	\fi

	% Light/ExtraLight/Regular
	\ifsourceserifpro@light
		\def\mdseries@rm{l}
	\else
		\ifsourceserifpro@extralight
			\def\mdseries@rm{el}
		\else
			\def\mdseries@rm{m}
		\fi
	\fi

	% Sans serif font
	\ifsourceserifpro@rmdefault
		\edef\rmdefault{SourceSerifPro-\sourceserifpro@figurealign\sourceserifpro@figurestyle}
	\fi

	% If Source Serif Pro is the default font \oldstylenums needs a new definition.
	\let\nativeoldstylenums\oldstylenums
	\ifsourceserifpro@rmdefault
		\renewcommand*\oldstylenums[1]{%
			\begingroup
				\ifmmode
					\mathgroup\symletters #1%
				\else
					\fontfamily{SourceSerifPro-\sourceserifpro@figurealign OsF}\selectfont #1%
				\fi
			\endgroup
		}
		\edef\seriesdefault{\mdseries@rm}
	\fi
\fi

% Set default font family
\ifsourceserifpro@default
	\edef\familydefault{\rmdefault}
\fi

\endinput
