% unipersian-kashida.def: Persian justification for UniPersian.
%
% This file is part of the UniPersian package.
%
% Copyright (C) 2026 Amer Amikhteh.
%
% The XeTeX implementation is adapted from Jonathan Kew's Kashida
% code (2009), as incorporated into XePersian's
% kashida-xetex-xepersian.def:
% https://tug.org/pipermail/xetex/2009-February/012307.html
%
% Distributed and modifiable under the LaTeX Project Public License,
% version 1.3c or later: https://www.latex-project.org/lppl/
% This work is provided without warranty.
%
% This work has the LPPL maintenance status "maintained".
% The current maintainer is Amer Amikhteh.


\ProvidesFile{unipersian-kashida.def}[2026/12/04 v1.0.0 Persian justification]
\newif\ifuper@kashida@active
\ifLuaTeX
    % The transform attribute allows a local switch even mid-paragraph.
    \newattribute\uper@kashida@attribute
    \babelprovide[justification=kashida,
        transforms={(\uper@kashida@attribute) kashida.plain}]{persian}
    \NewDocumentCommand{\KashidaOn}{}{\setattribute\uper@kashida@attribute{1}}
    \NewDocumentCommand{\KashidaOff}{}{\unsetattribute\uper@kashida@attribute}
\else

    \newXeTeXintercharclass\uper@kashida@dual
    \newXeTeXintercharclass\uper@kashida@lam
    \newXeTeXintercharclass\uper@kashida@right
    \newXeTeXintercharclass\uper@kashida@alef
    % XeTeX's combining-mark class is transparent to inter-character tokens.
    \chardef\uper@kashida@mark=4096
    \def\uper@kashida@classes#1#2{%
        \@for\uper@kashida@char:=#2\do{%
            \XeTeXcharclass"\uper@kashida@char=#1\relax}}
    \uper@kashida@classes\uper@kashida@alef{0622,0623,0625,0627}
    \uper@kashida@classes\uper@kashida@right{0624,0629,062F,0630,0631,0632,0648,0698}
    \uper@kashida@classes\uper@kashida@dual{0626,0628,062A,062B,062C,062D,062E,0633,0634,0635,0636,0637,0638,0639,063A,0640,0641,0642,0643,0645,0646,0647,0649,064A,067E,0686,06A9,06AF,06CC}
    \uper@kashida@classes\uper@kashida@lam{0644}
    \uper@kashida@classes\uper@kashida@mark{064B,064C,064D,064E,064F,0650,0651,0652}
    \def\uper@kashida@stretch{%
        \ifuper@kashida@active
            \char"200D\nobreak
            \leaders\hrule
            height \XeTeXglyphbounds\tw@\the\XeTeXcharglyph"0640
            depth \XeTeXglyphbounds 4 \the\XeTeXcharglyph"0640
            \hskip\z@ plus .5em\char"200D\relax
        \fi}
    \XeTeXinterchartoks\uper@kashida@dual\uper@kashida@dual={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@dual\uper@kashida@lam={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@dual\uper@kashida@right={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@dual\uper@kashida@alef={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@lam\uper@kashida@dual={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@lam\uper@kashida@lam={\uper@kashida@stretch}
    \XeTeXinterchartoks\uper@kashida@lam\uper@kashida@right={\uper@kashida@stretch}
    \XeTeXinterchartokenstate=1\relax
    \NewDocumentCommand{\KashidaOn}{}{\uper@kashida@activetrue}
    \NewDocumentCommand{\KashidaOff}{}{\uper@kashida@activefalse}
\fi
\KashidaOn
\endinput
