% unipersian-fonts.def
%
% UniPersian: Persian language support for LaTeX.
%
% Copyright (C) 2026 Amer Amikhteh
%
% This file is part of the UniPersian package.
%
% This work is free software: you may redistribute it and/or modify
% it under the terms of the LaTeX Project Public License as published
% by the LaTeX Project, either version 1.3c of the License, or
% (at your option) any later version.
%
% This work is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%
% The full license text is available at:
% https://www.latex-project.org/lppl/
%
% This work is "maintained" by Amer Amikhteh.
%
% The current maintainer of this work is Amer Amikhteh.


\ProvidesFile{unipersian-fonts.def}
[2026/10/18 v0.3.0 Common definitions for fonts in XeTeX and LuaTeX]



\makeatletter


\ifLuaTeX
    \fontfallback{english}{persian}
    \fontfallback{persian}{english}
\fi

% --------------------------------------
% Main fonts
% --------------------------------------

\NewCommandCopy\uper@settextfont\settextfont

\RenewDocumentCommand{\settextfont}{O{} m}{%
    \ifXeTeX
        \ifx\bbl@main@language\uper@per
            \uper@settextfont
            [Mapping=farsidigits,#1]{#2}%
        \else
            \uper@settextfont
            [#1]{#2}%
        \fi
    \else
        \uper@settextfont
        [#1]{#2}%
    \fi
}

\NewDocumentCommand{\setpersiantextfont}{O{} m}{%
    \ifXeTeX
        \babelfont[persian]{rm}%
        [Mapping=farsidigits,#1]{#2}%
        \babelfont[persian]{sf}%
        [Mapping=farsidigits,#1]{#2}
        \babelfont[persian]{tt}%
        [Mapping=farsidigits,#1]{#2}
    \else
        \babelfont[persian]{rm}[#1]{#2}%
        \babelfont[persian]{sf}[#1]{#2}%
        \babelfont[persian]{tt}[#1]{#2}%
    \fi
}


\DeclareDocumentCommand \defpersianfont { m o m }
{
    \newfontfamily#1{#3}[Script=Arabic,Renderer=Harfbuzz,#2]
}

% ------------------------------------------
% Default fonts
% ------------------------------------------
\ifnum\bbl@bidimode>\@ne
    % هر bidi غیر از default
    \IfFontExistsTF{HM XNiloofar}{\setpersiantextfont{HM XNiloofar}}
    {\IfFontExistsTF{Vazirmatn}{\setpersiantextfont[ItalicFont={Vazirmatn}]{Vazirmatn}}
        {\IfFontExistsTF{Amiri}{\setpersiantextfont{Amiri}}
            {\IfFontExistsTF{FreeSerif}{\setpersiantextfont{FreeSerif}}
                {\IfFontExistsTF{Tahoma}{\setpersiantextfont{Tahoma}}
                    {\typeout{Warning: No suitable Persian font found!}
                    }}}}}
\fi

% --------------------------------------
% Persian sans and monospaced fonts
% --------------------------------------

\NewDocumentCommand{\setpersiansansfont}{O{} m}{%
    \ifXeTeX
        \babelfont[persian]{sf}%
        [Mapping=farsidigits,#1]{#2}%
    \else
        \babelfont[persian]{sf}[#1]{#2}%
    \fi
}

\providecommand\persianttdefault{}

\NewDocumentCommand{\setpersianmonofont}{O{} m}{%
    \babelfont[persian]{tt}[#1]{#2}%
    \begingroup
    \selectlanguage{persian}%
    \ttfamily
    \xdef\persianttdefault{\f@family}%
    \endgroup
}


\DeclareRobustCommand\persianttfamily{%
    \not@math@alphabet\persianttfamily\mathpersiantt
    \fontfamily\persianttdefault
    \selectfont
}



\makeatother

\endinput
