% ff-mac.tex -- support for font files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Petr Olsak  2016

% This file is part of csplain package.
% See the file cs-heros.tex for more details.

\def\ffdecl [#1]#2#3#4#5#6{\ffdecltest#6 {}
   \iftrue
      {\toks2={#2}\toks3={#3}\toks4={#4}%
      \immediate\write16{FONT: #1 - \the\toks2
         \ifx\relax#3\relax\else ; mod:\the\toks3 \fi 
         \ifx\relax#4\relax\else ; \the\toks4 \fi 
         \ifx\loadmathfonts\relax \else ; +#5\fi}}%
   \else 
      \message{WARNING: #1 family is not available in 
               \fotenc\space encoding. Ignored.}%
      \endinput
   \fi
}
\def\ffdecltest#1 {\ifx\relax#1\relax \expandafter \ffdecltestfin
   \else
      \ismacro\fotenc{#1}\iftrue \ffdecltestfound \fi
      \expandafter\ffdecltest
   \fi
}
\def\ffdecltestfin\iftrue{\iffalse}
\def\ffdecltestfound#1\iftrue{\fi\fi \iftrue}

\ifx\sizespec\undefined \def\sizespec{}\fi

\ifx\fotenc\undefined
   \ifx\chyph\undefined \def\fotenc{8t}\else \def\fotenc{8z}\fi
   \ifx\font\corkencoded \def\fotenc{8t}\fi
   \ifx\font\unicoded \def\fotenc{U}\fi
   \def\tmp#1#2\relax{\def\tmp{#2}}\tmp ^^^^abcd\relax
   \ifx\tmp\empty \def\fotenc{U}\fi  % Unicode engine
\fi

\ifx\protected\undefined
   \let\tryprotected=\relax
   \ifx\addprotect\undefined \else 
      \def\tryprotected#1#2{\addprotect#2#1#2}
   \fi
\else \let\tryprotected=\protected \fi

\ifx\rfontskipat\undefined \input csfontsm \fi

\tryprotected\def\ffvars#1#2#3#4{%
   \def\ffvarY##1{\ifcase##1 #1\or#2\or#3\or#4\fi}%
}
\tryprotected\def\ffsetV#1#2{\expandafter\def\csname #1V\endcsname{#2}}

\long\def\ffsetX#1{\ifx#1\fam \ffsetY=\else \ifx#1\one \ffsetY+\else
   \ifx#1\rm \ffsetY0\else \ifx#1\bf \ffsetY1\else
   \ifx#1\it \ffsetY2\else \ifx#1\bi \ffsetY3\else
   \ffsetZ#1\fi\fi\fi\fi\fi\fi
}
\def\ffsetZ#1\fi\fi\fi\fi\fi\fi{\fi\fi\fi\fi\fi\fi#1}
\ifx\one\undefined \def\one{1}\fi % Something more specific than \undefined.
                                  % User can re-define it without problems.

\tryprotected\def\ffsetY#1{%
   \ifx=#1\ffsetS \ffsetW0\tenrm \ffsetW1\tenbf \ffsetW2\tenit \ffsetW3\tenbi 
      \ffsetT{\tenrm}{\tenbf}{\tenit}{\tenbi}%
   \else \ifx+#1\ffsetS \ffsetT{\ffsetX\rm}{\ffsetX\bf}{\ffsetX\it}{\ffsetX\bi}%
      \else \edef\ffvarV{\ffvarY{#1}}%
         \if!\ffvarV\else \def\ffvarN{#1}\ffsetF\ffmodfont\ffmodfont \fi 
   \fi\fi
}
\def\ffsetF#1#2{%
   \ffnametest\iftrue
      \ifx\dgsize\undefined 
         \expandafter\readsizespec\sizespec at\relax
         \font#1=\whichtfm{\ffnamegen} \sizespec\relax
         \let\dgsize=\undefined
      \else \font#1=\whichtfm{\ffnamegen} \sizespec\relax
   \fi #2\fi
}
\let\ffnametest=\relax

\def\ffsetS{\expandafter\ifx\the\font\tenrm \def\ffvarN{0}%
   \else \expandafter\ifx\the\font\tenbf \def\ffvarN{1}%
   \else \expandafter\ifx\the\font\tenit \def\ffvarN{2}%
   \else \expandafter\ifx\the\font\tenbi \def\ffvarN{3}\fi\fi\fi\fi
}
\def\ffsetT#1#2#3#4{\ifx\ffvarN\undefined \else
   \ifcase\ffvarN\space #1\or#2\or#3\or#4\fi\fi
}
\def\readsizespec#1at#2\relax{\ifx!#2\def\dgsize{#1}\else
   \ifx\relax#2\relax \def\dgsize{10pt}\else \readsizespec#2!\relax\fi\fi
}
\def\ffsetW#1#2{\edef\ffvarV{\ffvarY{#1}}%
   \if!\ffvarV\else \ffsetF#2\relax\fi
}
\def\ismacro#1#2#3{\def\tmp{#2}\ifx#1\tmp}

\def\ffletfont#1=#2#3{%
   {\def\sizespec{#3}\let\ffsetFa=\ffsetF 
    \def\ffsetF##1##2{\ffsetFa#1\relax}\def\ffsetW##1##2{}%
    \ffsetX#2\global\let\tmp=#1}\let#1=\tmp
}
\ifx\regtfm\undefined %%%%% \regtfm, \whichtfm from ams-math.tex:

\def\regtfm #1 0 #2 *{\expandafter
  \def\csname#1:reg\endcsname{#2 16380 \relax}%
  \def\tmpa{#1}\reversetfm #2 * %
}
\def\reversetfm #1 #2 {\expandafter
   \let\csname#1:reg\expandafter\endcsname
   \csname\tmpa:reg\endcsname
   \if*#2\else \expandafter\reversetfm \fi
}
\def\whichtfm #1{\ifx\dgsize\undefined #1\else
   \expandafter \ifx\csname#1:reg\endcsname\relax
      #1%
   \else
      \expandafter\expandafter\expandafter \dowhichtfm
      \csname #1:reg\expandafter\endcsname
   \fi \fi
}
\def\dowhichtfm #1 #2 {%
   \ifdim\dgsize<#2pt #1\expandafter\ignoretfm\else \expandafter\dowhichtfm
\fi
}
\def\ignoretfm #1\relax{}

\fi % of \ifx\regtfm %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\ffalias#1#2{\expandafter\edef\csname#1:reg\endcsname{#2 16380 \relax}}

\def\ffoptV{}
\def\regsizes#1#2{\bgroup \aftergroup\regsizesC #1\relax 
   \let\regtfm=\relax \gdef\tmp{}%
   \edef\ffvarV{\ffvarY{0}}\if!\ffvarV\else \regsizesA{#2}\fi
   \edef\ffvarV{\ffvarY{1}}\if!\ffvarV\else \regsizesA{#2}\fi
   \edef\ffvarV{\ffvarY{2}}\if!\ffvarV\else \regsizesA{#2}\fi
   \edef\ffvarV{\ffvarY{3}}\if!\ffvarV\else \regsizesA{#2}\fi
   \egroup
}
\def\regsizesA#1{\def\ffoptV{}\xdef\tmp{\tmp\regtfm \ffnamegen}%
   \regsizesB#1 * =
}
\def\regsizesB#1 =#2 {\if*#1\xdef\tmp{\tmp\space*}%
   \else \def\ffoptV{#2}\xdef\tmp{\tmp\space #1 \ffnamegen}%
      \expandafter\regsizesB\fi
}
\def\regsizesC{\tmp\xdef\tmp{}}

\def\useff#1{} % do nothing with non U encoding

\ismacro\fotenc{U}\iftrue\else \ismacro\fotenc{sU}\iftrue\else \endinput \fi\fi %%%%%%

\ifx\directlua\undefined \else \ifx\luafonts\undefined \input luafonts
\fi\fi  % lua code to re-define \font primitive

\ifx\fontfeatures\undefined 
   \def\fontfeatures{mapping=tex-text;script=latn;+tlig} % default
\fi

\def\useff#1{\ffsetU{#1}\ffsetX}
\tryprotected\def\ffsetU#1{\edef\fontfeatures{\fontfeatures;#1}}

\endinput
