% \iffalse meta-comment
% 
%<*internal>
\iffalse
%</internal>
%<*README>
The XEPERSIAN-HM package
------------------------

Kashida feature in `xepersian' has problems with some fonts such as `HM Series'
fonts available at `https://dma8hm1334.bitbucket.io' and `X Series 2' fonts
available at `http://wiki.irmug.com/index.php/X_Series_2'. The `xepersian-hm'
package fixes these problems.

The file `texmf-dist/doc/xelatex/xepersian-hm/kashida-example.tex' can be used as
a simple example of the usage of the package.

Any comments or suggestions would be gratefully appreciated. 
Please report any bugs or issues to `dma8hm1334@gmail.com'.

I will do my best to fix all the bugs you report, but, unfortunately,
time is a big hurdle to overcome; so, my apologies in advance for those
which I cannot make time to fix.
%</README>
%<*internal>
\fi
%</internal>
%
%% File: xepersian-hm.dtx
%
% Copyright (C) 2020 Hossein Movahhedian
%
% It may be distributed and/or modified under the LaTeX Project Public License,
% version 1.3c or higher (your choice). The latest version of
% this license is at: http://www.latex-project.org/lppl.txt
%
% \iffalse
%<kashida-xepersian-hm> 
%<kashida-xepersian-hm>% Copyright notice: the following code is partly adapted from the code in
%<kashida-xepersian-hm>% 'kashida-xepersian.def' from xepersian package (v22.8).
%<kashida-xepersian-hm>
% \fi
%
%<*driver|package>
% The version of expl3 required is tested as early as possible, as
% some really old versions do not define \ProvidesExplPackage.
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3}[2020-03-06]
%<package>\@ifpackagelater{expl3}{2020-03-06}
%<package>  {}
%<package>  {%
%<package>    \PackageError{xepersian-hm}{Support package l3kernel too old}
%<package>      {%
%<package>        Please install an up to date version of l3kernel\MessageBreak
%<package>        using your TeX package manager or from CTAN.\MessageBreak
%<package>        \MessageBreak
%<package>        Loading xtemplate will abort!%
%<package>      }%
%<package>    \endinput
%<package>  }
%</driver|package>
%<*driver>
\documentclass[full]{l3doc}
\usepackage{hypdoc}
\addtolength{\hoffset}{-10mm}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{xepersian-hm} package\\ Fixes issues in xepersian package^^A
% }
%
% \author{^^A
%  Hossein Movahhedian\thanks
%    {^^A
%      E-mail:
%        \href{mailto:dma8hm1334@gmail.com}
%          {dma8hm1334@gmail.com}^^A
%    }^^A
% }
%
% \date{Released 2020-03-26}
%
% \maketitle
%
% \begin{documentation}
%
% Kashida feature in \textsf{xepersian} has problems with some fonts such as \texttt{HM Series}
% fonts available at
% \href{https://dma8hm1334.bitbucket.io}{\texttt{https://dma8hm1334.bitbucket.io}}
% and \texttt{X Series 2} fonts available at
% \href{http://wiki.irmug.com/index.php/X_Series_2}{\texttt{http://wiki.irmug.com/index.php/X\_Series\_2}}.
% The \textsf{xepersian-hm} package fixes these problems.
% 
% The file \texttt{texmf-dist/doc/xelatex/xepersian-hm/kashida-example.tex} can be
% used as a simple example of the usage of the package.
% 
% Any comments or suggestions would be gratefully appreciated. 
% Please report any bug or issue to \href{mailto:dma8hm1334@gmail.com}{\textsf{dma8hm1334@gmail.com}}.
% 
% I will do my best to fix all the bugs you report, but, unfortunately,
% time is a big hurdle to overcome; so, my apologies in advance for those
% which I cannot make time to fix.
% 
% Please note that \textsf{xepersian-hm} loads \textsf{xepersian} automatically,
% so you may only pass options to the package using the command
% \verb|\PassOptionsToPackage| before \verb|\documentclass|, for example:
% 
% \begin{verbatim}
% \PassOptionsToPackage{Kashida=off,RTLdocument=on}{xepersian}
% \documentclass{report}
% \usepackage{xcolor}
% \usepackage[Kashida,kashidastretch=0.14 em plus 0.5 em]{xepersian-hm}
% \end{verbatim}
% 
% The options available in \textsf{xepersian-hm} are:
% \begin{itemize}
%   \item \texttt{Kashida}: with two possible values, \texttt{on} which enables
%     \texttt{Kashida} feature and \texttt{off} which disables that feature. For
%     example \texttt{Kashida=on} enables \texttt{Kashida} feature.
%   \item \texttt{kashidastretch}: which specifies the amount of extra stretching
%     for some combinations of characters. You may strictly specify the value of
%     this option. For example \texttt{'kashidastretch=0.14 em plus 0.5 em'} or you
%     may use the default values. The available default values are:
% 
%     \begin{tabular}{ll}
%       \texttt{kayhan}       & \texttt{0.14  em plus 0.5 em} \\
%       \texttt{khorramshahr} & \texttt{0.131 em plus 0.5 em} \\
%       \texttt{kayhannavaar} & \texttt{0.129 em plus 0.5 em} \\
%       \texttt{kayhanpook}   & \texttt{0.133 em plus 0.5 em} \\
%       \texttt{kayhansayeh}  & \texttt{0.135 em plus 0.5 em} \\
%       \texttt{khoramshahr}  & \texttt{0.128 em plus 0.5 em} \\
%       \texttt{khorramshahr} & \texttt{0.13  em plus 0.5 em} \\
%       \texttt{niloofar}     & \texttt{0.132 em plus 0.5 em} \\
%       \texttt{paatch}       & \texttt{0.127 em plus 0.5 em} \\
%       \texttt{riyaz}        & \texttt{0.125 em plus 0.5 em} \\
%       \texttt{roya}         & \texttt{0.142 em plus 0.5 em} \\
%       \texttt{shafigh}      & \texttt{0.143 em plus 0.5 em} \\
%       \texttt{shafighKurd}  & \texttt{0.126 em plus 0.5 em} \\
%       \texttt{shafighUzbek} & \texttt{0.123 em plus 0.5 em} \\
%       \texttt{shiraz}       & \texttt{0.122 em plus 0.5 em} \\
%       \texttt{sols}         & \texttt{0.124 em plus 0.5 em} \\
%       \texttt{tabriz}       & \texttt{0.119 em plus 0.5 em} \\
%       \texttt{titr}         & \texttt{0.12  em plus 0.5 em} \\
%       \texttt{titre}        & \texttt{0.121 em plus 0.5 em} \\
%       \texttt{traffic}      & \texttt{0.124 em plus 0.5 em} \\
%       \texttt{vahid}        & \texttt{0.134 em plus 0.5 em} \\
%       \texttt{vosta}        & \texttt{0.136 em plus 0.5 em} \\
%       \texttt{yaghut}       & \texttt{0.138 em plus 0.5 em} \\
%       \texttt{yagut}        & \texttt{0.137 em plus 0.5 em} \\
%       \texttt{yas}          & \texttt{0.126 em plus 0.5 em} \\
%       \texttt{yekan}        & \texttt{0.141 em plus 0.5 em} \\
%       \texttt{yermook}      & \texttt{0.139 em plus 0.5 em} \\
%       \texttt{zar}          & \texttt{0.116 em plus 0.5 em} \\
%       \texttt{ziba}         & \texttt{0.119 em plus 0.5 em} \\
%       \texttt{default}      & \texttt{0.14  em plus 0.5 em} \\
%       \texttt{noskip}       & \texttt{0     em plus 0.5 em}
%     \end{tabular}
% 
%     For example: \mbox{\texttt{'kashidastretch=titr'}} is equivalent to\linebreak
%     \mbox{\texttt{'kashidastretch=0.12  em plus 0.5 em'}}
% \end{itemize}
%
% \end{documentation}
%
%\begin{implementation}
%
% \section{\pkg{xepersian-hm} Implementation}
%
% \subsection{File: \file{xepersian-hm.sty}}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{l3keys2e}
\RequirePackage{xepersian}
\ExplSyntaxOn
\ProvidesExplPackage {xepersian-hm} {2020-03-26} {0.4} { Fixes~issues~in~xepersian~package }

\tl_set:Nn \l_kayhan_tl       { kayhan }
\tl_set:Nn \l_khorramshahr_tl { khorramshahr }
\tl_set:Nn \l_kayhannavaar_tl { kayhannavaar }  
\tl_set:Nn \l_kayhanpook_tl   { kayhanpook }  
\tl_set:Nn \l_kayhansayeh_tl  { kayhansayeh } 
\tl_set:Nn \l_khoramshahr_tl  { khoramshahr } 
\tl_set:Nn \l_khorramshahr_tl { khorramshahr }
\tl_set:Nn \l_niloofar_tl     { niloofar }    
\tl_set:Nn \l_paatch_tl       { paatch }      
\tl_set:Nn \l_riyaz_tl        { riyaz }       
\tl_set:Nn \l_roya_tl         { roya }        
\tl_set:Nn \l_shafigh_tl      { shafigh }     
\tl_set:Nn \l_shafighKurd_tl  { shafighKurd } 
\tl_set:Nn \l_shafighUzbek_tl { shafighUzbek }
\tl_set:Nn \l_shiraz_tl       { shiraz }      
\tl_set:Nn \l_sols_tl         { sols }        
\tl_set:Nn \l_tabriz_tl       { tabriz }      
\tl_set:Nn \l_titr_tl         { titr }        
\tl_set:Nn \l_titre_tl        { titre }       
\tl_set:Nn \l_traffic_tl      { traffic }     
\tl_set:Nn \l_vahid_tl        { vahid }       
\tl_set:Nn \l_vosta_tl        { vosta }       
\tl_set:Nn \l_yaghut_tl       { yaghut }      
\tl_set:Nn \l_yagut_tl        { yagut }       
\tl_set:Nn \l_yas_tl          { yas }         
\tl_set:Nn \l_yekan_tl        { yekan }       
\tl_set:Nn \l_yermook_tl      { yermook }     
\tl_set:Nn \l_zar_tl          { zar }         
\tl_set:Nn \l_ziba_tl         { ziba }        
\tl_set:Nn \l_default_tl      { default }        
\tl_set:Nn \l_noskip_tl       { noskip }        

\keys_define:nn { xepersian-hm }
  {
    Kashida        .code:n = \tex_input:D { kashida-xepersian-hm.def } ,

    kashidastretch .code:n =
      {
        \tl_set:Nn \l_tmpa_tl { #1 }
        \tl_case:NnTF \l_tmpa_tl
          {
             \l_kayhan_tl       { \tl_set:Nn \l_hskip_default_tl { 0.14  em plus 0.5 em } }
             \l_khorramshahr_tl { \tl_set:Nn \l_hskip_default_tl { 0.131 em plus 0.5 em } }
             \l_kayhannavaar_tl { \tl_set:Nn \l_hskip_default_tl { 0.129 em plus 0.5 em } }
             \l_kayhanpook_tl   { \tl_set:Nn \l_hskip_default_tl { 0.133 em plus 0.5 em } }
             \l_kayhansayeh_tl  { \tl_set:Nn \l_hskip_default_tl { 0.135 em plus 0.5 em } }
             \l_khoramshahr_tl  { \tl_set:Nn \l_hskip_default_tl { 0.128 em plus 0.5 em } }
             \l_khorramshahr_tl { \tl_set:Nn \l_hskip_default_tl { 0.13  em plus 0.5 em } }
             \l_niloofar_tl     { \tl_set:Nn \l_hskip_default_tl { 0.132 em plus 0.5 em } }
             \l_paatch_tl       { \tl_set:Nn \l_hskip_default_tl { 0.127 em plus 0.5 em } }
             \l_riyaz_tl        { \tl_set:Nn \l_hskip_default_tl { 0.125 em plus 0.5 em } }
             \l_roya_tl         { \tl_set:Nn \l_hskip_default_tl { 0.142 em plus 0.5 em } }
             \l_shafigh_tl      { \tl_set:Nn \l_hskip_default_tl { 0.143 em plus 0.5 em } }
             \l_shafighKurd_tl  { \tl_set:Nn \l_hskip_default_tl { 0.126 em plus 0.5 em } }
             \l_shafighUzbek_tl { \tl_set:Nn \l_hskip_default_tl { 0.123 em plus 0.5 em } }
             \l_shiraz_tl       { \tl_set:Nn \l_hskip_default_tl { 0.122 em plus 0.5 em } }
             \l_sols_tl         { \tl_set:Nn \l_hskip_default_tl { 0.124 em plus 0.5 em } }
             \l_tabriz_tl       { \tl_set:Nn \l_hskip_default_tl { 0.119 em plus 0.5 em } }
             \l_titr_tl         { \tl_set:Nn \l_hskip_default_tl { 0.12  em plus 0.5 em } }
             \l_titre_tl        { \tl_set:Nn \l_hskip_default_tl { 0.121 em plus 0.5 em } }
             \l_traffic_tl      { \tl_set:Nn \l_hskip_default_tl { 0.124 em plus 0.5 em } }
             \l_vahid_tl        { \tl_set:Nn \l_hskip_default_tl { 0.134 em plus 0.5 em } }
             \l_vosta_tl        { \tl_set:Nn \l_hskip_default_tl { 0.136 em plus 0.5 em } }
             \l_yaghut_tl       { \tl_set:Nn \l_hskip_default_tl { 0.138 em plus 0.5 em } }
             \l_yagut_tl        { \tl_set:Nn \l_hskip_default_tl { 0.137 em plus 0.5 em } }
             \l_yas_tl          { \tl_set:Nn \l_hskip_default_tl { 0.126 em plus 0.5 em } }
             \l_yekan_tl        { \tl_set:Nn \l_hskip_default_tl { 0.141 em plus 0.5 em } }
             \l_yermook_tl      { \tl_set:Nn \l_hskip_default_tl { 0.139 em plus 0.5 em } }
             \l_zar_tl          { \tl_set:Nn \l_hskip_default_tl { 0.116 em plus 0.5 em } }
             \l_ziba_tl         { \tl_set:Nn \l_hskip_default_tl { 0.119 em plus 0.5 em } }
             \l_default_tl      { \tl_set:Nn \l_hskip_default_tl { 0.14  em plus 0.5 em } }
             \l_noskip_tl       { \tl_set:Nn \l_hskip_default_tl { 0     em plus 0.5 em } }
          } { } { \tl_set:Nn \l_hskip_default_tl { #1 } }
      } ,
    kashidastretch .default:n = \tl_set:Nn \l_hskip_default_tl { 0 em plus 0.5 em } ,
  }

\ProcessKeysOptions { xepersian-hm }

\ExplSyntaxOff
 \endinput
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \subsection{File: \file{kashida-xepersian-hm.def}}
%
%    \begin{macrocode}
%<*kashida-xepersian-hm>
\ExplSyntaxOn
\ProvidesExplFile {kashida-xepersian-hm.def} {2020-03-26} {0.4} { Fixes~implementation~of~Kashida~in~xepersian~package }

\int_const:Nn \c_xepersianhm_zwj_int {"200D} % zero-width joiner
\int_const:Nn \c_xepersianhm_lrm_int {"200E} % left-right-mark
\int_const:Nn \c_xepersianhm_two_int {2} % 2
\int_const:Nn \c_xepersianhm_four_int {4} % 4
\int_const:Nn \c_xepersianhm_ksh_int {"0640} % kashida
\int_const:Nn \c_xepersianhm_d_int {10} % dual-joiner class
\int_const:Nn \c_xepersianhm_l_int {11} % lam
\int_const:Nn \c_xepersianhm_r_int {12} % right-joiner
\int_const:Nn \c_xepersianhm_a_int {13} % alef
\int_const:Nn \c_xepersianhm_h_int {14} % heh
\int_const:Nn \c_xepersianhm_y_int {15} % yeh
\int_const:Nn \c_xepersianhm_v_int {4096} % vowel or other combining mark (to be ignored)

\bool_new:N \l_kashida_on_bool
\bool_new:N \l_kashida_hm_fix_bool
\bool_new:N \l_kashida_xb_fix_bool

\tl_new:N \l_hskip_zero_tl
\tl_new:N \l_hskip_default_tl

\cs_new:Npn \xepersian_kashida #1
  {
    \bool_if:NT \l_kashida_on_bool
    {
      \c_xepersianhm_lrm_int\c_xepersianhm_zwj_int\tex_penalty:D 10000
      \tex_leaders:D \tex_hrule:D height \XeTeXglyphbounds \c_xepersianhm_two_int
      \int_use:N \XeTeXcharglyph \c_xepersianhm_ksh_int depth \XeTeXglyphbounds \c_xepersianhm_four_int
      \int_use:N \XeTeXcharglyph \c_xepersianhm_ksh_int \skip_horizontal:n { #1 }
      \c_xepersianhm_zwj_int
    }
  }

\XeTeXinterchartokenstate = 1

\clist_set:Nn \l_xepersianhm_a_clist { 0622,0623,0625,0627 } % ‏ا، إ، أ، آ‏
\clist_map_inline:Nn \l_xepersianhm_a_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_a_int
  }

\clist_set:Nn \l_xepersianhm_d_clist { 0626,0628,062A,062B,062C,062D,062E,0633,0634,0635,0636,0637,0638,0639,063A,0640,0641,0642,0643,0645,0646,0647,067E,0686,06A9,06AF } % ‏ئ,ب,ت,ث,ج,ح,خ,س,ش,ص,ض,ط,ظ,ع,غ,ـ,ف,ق,ك,م,ن,ه,پ,چ,ک,گ‏
\clist_map_inline:Nn \l_xepersianhm_d_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_d_int
  }

\clist_set:Nn \l_xepersianhm_l_clist { 0644 } % ‏ل‏
\clist_map_inline:Nn \l_xepersianhm_l_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_l_int
  }

\clist_set:Nn \l_xepersianhm_r_clist { 0624,0629,062F,0630,0631,0632,0648,0698 } % ‏ؤ,ة,د,ذ,ر,ز,و,ژ‏
\clist_map_inline:Nn \l_xepersianhm_r_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_r_int
  }

\clist_set:Nn \l_xepersianhm_v_clist { 064B,064C,064D,064E,064F,0650,0651,0652 } % ‏ً,ٌ,ٍ,َ,ُ,ِ,ّ,ْ‏
\clist_map_inline:Nn \l_xepersianhm_v_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_v_int
  }

\clist_set:Nn \l_xepersianhm_y_clist { 0649,064A,06CC }
\clist_map_inline:Nn \l_xepersianhm_y_clist
  {
    \XeTeXcharclass "#1 \c_xepersianhm_y_int
  }

\XeTeXinterchartoks \c_xepersianhm_y_int \c_xepersianhm_y_int = {\bool_if:NTF \l_kashida_hm_fix_bool {\xepersian_kashida {\l_hskip_default_tl}} {\xepersian_kashida \l_hskip_zero_tl}}
\XeTeXinterchartoks \c_xepersianhm_d_int \c_xepersianhm_y_int = {\bool_if:NTF \l_kashida_hm_fix_bool {\xepersian_kashida {\l_hskip_default_tl}} {\xepersian_kashida \l_hskip_zero_tl}}
\XeTeXinterchartoks \c_xepersianhm_y_int \c_xepersianhm_d_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_d_int \c_xepersianhm_d_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_l_int \c_xepersianhm_d_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_d_int \c_xepersianhm_l_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_l_int \c_xepersianhm_l_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_d_int \c_xepersianhm_r_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_d_int \c_xepersianhm_a_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_l_int \c_xepersianhm_r_int = {\xepersian_kashida \l_hskip_zero_tl}
\XeTeXinterchartoks \c_xepersianhm_l_int \c_xepersianhm_a_int = {}

\NewDocumentCommand \KashidaOn {} { \bool_set_true:N \l_kashida_on_bool }
\NewDocumentCommand \KashidaOff {} { \bool_set_false:N \l_kashida_on_bool }

\NewDocumentCommand \KashidaXBFixOn {} { \bool_set_true:N \l_kashida_xb_fix_bool }
\NewDocumentCommand \KashidaXBFixOff {} { \bool_set_false:N \l_kashida_xb_fix_bool }

\NewDocumentCommand \KashidaHMFixOn {} { \bool_set_true:N \l_kashida_hm_fix_bool }
\NewDocumentCommand \KashidaHMFixOff {} { \bool_set_false:N \l_kashida_hm_fix_bool }

\ExplSyntaxOff
\makeatletter
\newif\if@Kashida@on
\newif\if@Kashida@XB@fix
\makeatother
\ExplSyntaxOn

\KashidaHMFixOn

\tl_set:Nn \l_hskip_zero_tl { 0 em plus 0.5 em }

\bool_if:NTF \l_kashida_hm_fix_bool
  {
    \tl_if_empty:NT \l_hskip_default_tl { \tl_set:Nn \l_hskip_default_tl  { 0.14 em plus 0.5 em } }
  }
  {
    \tl_set:NV \l_hskip_default_tl  \l_hskip_zero_tl
  }

\KashidaOn

\ExplSyntaxOff
 \endinput
%</kashida-xepersian-hm>
%    \end{macrocode}
%
%\end{implementation}
%
% \typeout{***************************************************}
% \typeout{*}
% \typeout{* To finish the installation you have to move the}
% \typeout{* following files into a directory searched by TeX:}
% \typeout{*}
% \typeout{* \space\space *.def, *.sty }
% \typeout{*}
% \typeout{* Documentation is in xepersian-hm.pdf}
% \typeout{*}
% \typeout{***************************************************}
%
\endinput
