% Unipersian: Persian language support for LaTeX.
%
% Copyright (C) 2026 Amer Amikhteh
%
% This package 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 package 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.
%
% This package consists of the file unipersian.sty and the files
% distributed with it.



\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{unipersian}
[2026/10/26 v0.3.0 Persian support for pdfTeX, XeTeX, and LuaTeX]

\RequirePackage{xparse}
\RequirePackage{xkeyval}
\RequirePackage{iftex}
\ifLuaTeX\RequirePackage{fontspec}\fi


\makeatletter %(
% ------------------------------------------
% Engine-dependent language names
% ------------------------------------------

\ifPDFTeX
    \def\uper@name@persian{farsi}
\else
    \def\uper@name@persian{persian}
\fi


%% ------------------------------------------
% Package options
% ------------------------------------------

\def\uper@encodings{}

\DeclareOptionX{encodings}{%
    \edef\uper@encodings{%
        \uper@encodings,#1%
    }%
}

\DeclareOptionX*{%
    \PassOptionsToPackage{\CurrentOption}{babelbidi}%
}

\ifXeTeX
    \def\uper@babel@persian{}
    \def\uper@babel@english{}
\fi

\ifLuaTeX
    \def\uper@babel@persian{mapdigits,onchar=ids fonts letters}
    \def\uper@babel@english{onchar=ids fonts letters}
\fi

\DeclareOptionX{persianprovide}{\def\uper@babel@persian{#1}}
\DeclareOptionX{englishprovide}{\def\uper@babel@english{#1}}

\ProcessOptionsX\relax


% ------------------------------------------
% Main Language
% ------------------------------------------

\def\uper@fa{farsi}
\def\uper@per{persian}

\newif\ifuper@main@persian

\def\uper@extra#1{
    \addto\extrasfarsi{#1}
    \addto\extraspersian{#1}
}


% ------------------------------------------
% Engine setup
% ------------------------------------------

\ifPDFTeX
    \RequirePackage[utf8]{inputenc}
    \RequirePackage[LAE,LFE,\uper@encodings,T1]{fontenc}
    \def\uper@langs{english,farsi}
\else
    \def\uper@langs{english,persian}
\fi

\RequirePackage[\uper@langs]{babelbidi}

\ifx\bbl@main@language\uper@name@persian
    \uper@main@persiantrue
\fi

\ifTUTeX
    % Expand option lists before Babel splits keys; omit empty entries.
    \edef\uper@provide@options{import%
        \ifuper@main@persian,main\fi
        \ifx\uper@babel@persian\@empty\else,\uper@babel@persian\fi}%
    \expandafter\babelprovide\expandafter[\uper@provide@options]{persian}
    \edef\uper@provide@options{import%
        \ifx\uper@babel@english\@empty\else,\uper@babel@english\fi}%
    \expandafter\babelprovide\expandafter[\uper@provide@options]{english}
\fi


% ------------------------------------------
% Load package components
% ------------------------------------------
\input{unipersian-numerals.def}
\input{unipersian-captions.def}


\makeatletter
\ifPDFTeX \g@addto@macro\captionsfarsi{\uper@captions}\fi
\ifTUTeX \g@addto@macro\captionspersian{\uper@captions} \fi
\makeatother
\ifTUTeX
    \input{unipersian-fonts.def}
\fi


% ------------------------------------------
% Book/report adaptations for a Persian main language
% ------------------------------------------

\makeatletter
\ifuper@main@persian
    \AddToHook{begindocument}{%
        \@ifundefined{frontmatter}{}{%
            \RenewDocumentCommand{\frontmatter}{}{%
                \cleardoublepage
                \@mainmatterfalse
                \pagenumbering{harfi}%
            }%
        }%
        \@ifundefined{thepart}{}{%
            \RenewExpandableDocumentCommand{\thepart}{}{%
                \tartibi{part}%
            }%
        }%
        \@ifundefined{appendix}{}{%
            \RenewDocumentCommand{\appendix}{}{%
                \par
                \setcounter{chapter}{0}%
                \setcounter{section}{0}%
                \gdef\@chapapp{\appendixname}%
                \gdef\thechapter{\harfi{chapter}}%
            }%
        }%
    }
\fi
\makeatother


% ------------------------------------------
% Persian environments
% ------------------------------------------

\makeatletter
\NewDocumentEnvironment{persian}{}%
{%
    \par
    \begin{otherlanguage}{\uper@name@persian}%
}%
{%
    \end{otherlanguage}%
    \par
}%


% ------------------------------------------------
% DeclareUnicodeCharacters
% ------------------------------------------------
% U+0654 = ARABIC HAMZA ABOVE
\ifPDFTeX\DeclareUnicodeCharacter{0654}{\makebox[0pt][r]{
            \raisebox{1.11ex}{ء}
            \kern 0.12em}}
\fi

\makeatother %)


\endinput
