%%
%% This is file `kashida-xepersian-hm.def',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% xepersian-hm.dtx  (with options: `kashida-xepersian-hm')
%% 
%% 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
%% 
%% File: xepersian-hm.dtx

% Copyright notice: the following code is partly adapted from the code in
% 'kashida-xepersian.def' from xepersian package (v22.8).

\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
%% 
%%
%% End of file `kashida-xepersian-hm.def'.
