summaryrefslogtreecommitdiff
path: root/macros/xetex/latex/xepersian-hm/xepersian-hm.dtx
blob: 6a7d84f07ee92c2f46321b9f47ec3740642b0080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
% \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.

Please use the Bitbucket issue tracker:
https://bitbucket.org/dma8hm1334/xepersian-hm/issues
to report a bug, request a feature or if you have a comment.

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
%
%<*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{longtable}
\usepackage{hypdoc}
\addtolength{\hoffset}{-10mm}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{xepersian-hm} package\\Fixing kashida in xepersian^^A
% }
% 
% \author{^^A
%  Hossein Movahhedian\thanks
%    {^^A
%      E-mail:
%        \href{mailto:dma8hm1334@gmail.com}
%          {\textsf{dma8hm1334@gmail.com}}
%    }^^A
% }
% 
% \date{Released 2020-04-20 \qquad v0.5a}
% 
% \begin{documentation}
% 
% \maketitle
% 
% 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.
% 
% Please use the Bitbucket issue tracker:
% \href{https://bitbucket.org/dma8hm1334/xepersian-hm/issues}{\textsf{https://bitbucket.org/dma8hm1334/xepersian-hm/issues}}
% to report a bug, request a feature or if you have a comment.
% 
% 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.
% 
% \section{Package loading and options}
% 
% 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}: which implements kashida feature with two possible values:
%     \begin{itemize}
%       \item \texttt{glyph}: which uses the \texttt{Kashida} character to stretch the text.
%         Please note that this feature is still experimental and is not regarded as stable.
%         If you are going to use this option be ready for unpredictable results.
%       \item \texttt{hrule}: which uses a horizontal rule (\verb|\hrule|) to stretch the text.
%     \end{itemize}
%     For example \texttt{Kashida=glyph} inserts a stretched \texttt{Kashida} glyph where ever it is needed.
%   \item \texttt{linebreakpenalty}: which specifies the amount of penalty for preventing bad line-breaking.
%     You may strictly specify the value of this option. For example \texttt{'linebreakpenalty=8'} or you
%     may use the default values. The available default values of penalties which are put into the
%     horizontal list output are the negative of the followings:
% 
%     \begin{longtable}{ll}
%       \texttt{min}:    & \texttt{0} \\
%       \texttt{low}:    & \texttt{8} \\
%       \texttt{medium}: & \texttt{15} \\
%       \texttt{high}:   & \texttt{25} \\
%       \texttt{max}:    & \texttt{10000} \\
%     \end{longtable}
% 
%   \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{longtable}{lll}
%       \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{longtable}
% 
%     For example: \mbox{\texttt{'kashidastretch=titr'}} is equivalent to\linebreak
%     \mbox{\texttt{'kashidastretch=0.12  em plus 0.5 em'}}
% \end{itemize}
% 
% \section{Commands}
% 
% \begin{description}
%   \item \verb|\discouragebadlinebreaks{|{\it text}\verb|}|  {\it text} may not contain paragraph breaks.
%     This command puts a penalty after each Persian word.
%     The value of the penalty is set by the option \texttt{linebreakpenalty}.
%     The default value is 8.
% \end{description}
%
% \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{graphicx}[2019-11-30]
\RequirePackage{zref-savepos}[2020-03-03]
\RequirePackage{xepersian}
\ExplSyntaxOn
\ProvidesExplPackage {xepersian-hm} {2020-04-20} {0.5a} { Fixes~issues~in~xepersian~package }

\bool_set_false:N \l_xephm_kashida_hm_fix_bool

\tl_new:N \l_xephm_gap_filler_tl

\bool_set_false:N \l_xephm_linebreakpenalty_bool

\int_new:N \l_xephm_line_break_penalty_int
\int_new:N \l_xephm_min_penalty_int
\int_new:N \l_xephm_low_penalty_int
\int_new:N \l_xephm_med_penalty_int
\int_new:N \l_xephm_high_penalty_int
\int_new:N \l_xephm_max_penalty_int

\int_set:Nn \l_xephm_min_penalty_int { 0 }
\int_set:Nn \l_xephm_low_penalty_int { 8 }
\int_set:Nn \l_xephm_med_penalty_int { 15 }
\int_set:Nn \l_xephm_high_penalty_int { 25 }
\int_set:Nn \l_xephm_max_penalty_int { 10000 }

\tl_set:Nn \l_xephm_stretch_glyph_tl { glyph }
\tl_set:Nn \l_xephm_stretch_hrule_tl { hrule }

\tl_set:Nn \l_xephm_kayhan_tl       { kayhan }
\tl_set:Nn \l_xephm_khorramshahr_tl { khorramshahr }
\tl_set:Nn \l_xephm_kayhannavaar_tl { kayhannavaar }
\tl_set:Nn \l_xephm_kayhanpook_tl   { kayhanpook }
\tl_set:Nn \l_xephm_kayhansayeh_tl  { kayhansayeh }
\tl_set:Nn \l_xephm_khoramshahr_tl  { khoramshahr }
\tl_set:Nn \l_xephm_khorramshahr_tl { khorramshahr }
\tl_set:Nn \l_xephm_niloofar_tl     { niloofar }
\tl_set:Nn \l_xephm_paatch_tl       { paatch }
\tl_set:Nn \l_xephm_riyaz_tl        { riyaz }
\tl_set:Nn \l_xephm_roya_tl         { roya }
\tl_set:Nn \l_xephm_shafigh_tl      { shafigh }
\tl_set:Nn \l_xephm_shafighKurd_tl  { shafighKurd }
\tl_set:Nn \l_xephm_shafighUzbek_tl { shafighUzbek }
\tl_set:Nn \l_xephm_shiraz_tl       { shiraz }
\tl_set:Nn \l_xephm_sols_tl         { sols }
\tl_set:Nn \l_xephm_tabriz_tl       { tabriz }
\tl_set:Nn \l_xephm_titr_tl         { titr }
\tl_set:Nn \l_xephm_titre_tl        { titre }
\tl_set:Nn \l_xephm_traffic_tl      { traffic }
\tl_set:Nn \l_xephm_vahid_tl        { vahid }
\tl_set:Nn \l_xephm_vosta_tl        { vosta }
\tl_set:Nn \l_xephm_yaghut_tl       { yaghut }
\tl_set:Nn \l_xephm_yagut_tl        { yagut }
\tl_set:Nn \l_xephm_yas_tl          { yas }
\tl_set:Nn \l_xephm_yekan_tl        { yekan }
\tl_set:Nn \l_xephm_yermook_tl      { yermook }
\tl_set:Nn \l_xephm_zar_tl          { zar }
\tl_set:Nn \l_xephm_ziba_tl         { ziba }
\tl_set:Nn \l_xephm_default_tl      { default }
\tl_set:Nn \l_xephm_noskip_tl       { noskip }

\keys_define:nn { xepersian-hm }
  {
    Kashida .code:n = 
      {
        \tl_set:Nn \l_tmpa_tl { #1 }
        \tl_case:NnTF \l_tmpa_tl
          {
            \l_xephm_stretch_glyph_tl { \tl_set:Nn \l_xephm_gap_filler_tl { \l_xephm_stretch_glyph_tl } }
            \l_xephm_stretch_hrule_tl { \tl_set:Nn \l_xephm_gap_filler_tl { \l_xephm_stretch_hrule_tl } }
          } { } { \tl_set:Nn \l_xephm_filler_default_tl { #1 } }
        \bool_set_true:N \l_xephm_kashida_hm_fix_bool
        \tex_input:D { kashida-xepersian-hm.def }
      } ,

    linebreakpenalty .code:n = 
      {
        \int_set:Nn \l_tmpa_int { #1 }
        \int_case:nnTF \l_tmpa_int
          {
            \l_xephm_min_penalty_int { \int_set:Nn \l_xephm_line_break_penalty_int { \l_xephm_min_penalty_int } }
            \l_xephm_low_penalty_int { \int_set:Nn \l_xephm_line_break_penalty_int { \l_xephm_low_penalty_int } }
            \l_xephm_med_penalty_int { \int_set:Nn \l_xephm_line_break_penalty_int { \l_xephm_med_penalty_int } }
            \l_xephm_high_penalty_int { \int_set:Nn \l_xephm_line_break_penalty_int { \l_xephm_high_penalty_int } }
            \l_xephm_max_penalty_int { \int_set:Nn \l_xephm_line_break_penalty_int { \l_xephm_max_penalty_int } }
          } { } { \int_set:Nn \l_xephm_line_break_penalty_int { #1 } }
        \bool_set_true:N \l_xephm_linebreakpenalty_bool
      } ,

    kashidastretch .code:n =
      {
        \tl_set:Nn \l_tmpb_tl { #1 }
        \tl_case:NnTF \l_tmpb_tl
          {
             \l_xephm_kayhan_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0.14  em plus 0.5 em } }
             \l_xephm_khorramshahr_tl { \tl_set:Nn \l_xephm_hskip_default_tl { 0.131 em plus 0.5 em } }
             \l_xephm_kayhannavaar_tl { \tl_set:Nn \l_xephm_hskip_default_tl { 0.129 em plus 0.5 em } }
             \l_xephm_kayhanpook_tl   { \tl_set:Nn \l_xephm_hskip_default_tl { 0.133 em plus 0.5 em } }
             \l_xephm_kayhansayeh_tl  { \tl_set:Nn \l_xephm_hskip_default_tl { 0.135 em plus 0.5 em } }
             \l_xephm_khoramshahr_tl  { \tl_set:Nn \l_xephm_hskip_default_tl { 0.128 em plus 0.5 em } }
             \l_xephm_khorramshahr_tl { \tl_set:Nn \l_xephm_hskip_default_tl { 0.13  em plus 0.5 em } }
             \l_xephm_niloofar_tl     { \tl_set:Nn \l_xephm_hskip_default_tl { 0.132 em plus 0.5 em } }
             \l_xephm_paatch_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0.127 em plus 0.5 em } }
             \l_xephm_riyaz_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.125 em plus 0.5 em } }
             \l_xephm_roya_tl         { \tl_set:Nn \l_xephm_hskip_default_tl { 0.142 em plus 0.5 em } }
             \l_xephm_shafigh_tl      { \tl_set:Nn \l_xephm_hskip_default_tl { 0.143 em plus 0.5 em } }
             \l_xephm_shafighKurd_tl  { \tl_set:Nn \l_xephm_hskip_default_tl { 0.126 em plus 0.5 em } }
             \l_xephm_shafighUzbek_tl { \tl_set:Nn \l_xephm_hskip_default_tl { 0.123 em plus 0.5 em } }
             \l_xephm_shiraz_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0.122 em plus 0.5 em } }
             \l_xephm_sols_tl         { \tl_set:Nn \l_xephm_hskip_default_tl { 0.124 em plus 0.5 em } }
             \l_xephm_tabriz_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0.119 em plus 0.5 em } }
             \l_xephm_titr_tl         { \tl_set:Nn \l_xephm_hskip_default_tl { 0.12  em plus 0.5 em } }
             \l_xephm_titre_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.121 em plus 0.5 em } }
             \l_xephm_traffic_tl      { \tl_set:Nn \l_xephm_hskip_default_tl { 0.124 em plus 0.5 em } }
             \l_xephm_vahid_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.134 em plus 0.5 em } }
             \l_xephm_vosta_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.136 em plus 0.5 em } }
             \l_xephm_yaghut_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0.138 em plus 0.5 em } }
             \l_xephm_yagut_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.137 em plus 0.5 em } }
             \l_xephm_yas_tl          { \tl_set:Nn \l_xephm_hskip_default_tl { 0.126 em plus 0.5 em } }
             \l_xephm_yekan_tl        { \tl_set:Nn \l_xephm_hskip_default_tl { 0.141 em plus 0.5 em } }
             \l_xephm_yermook_tl      { \tl_set:Nn \l_xephm_hskip_default_tl { 0.139 em plus 0.5 em } }
             \l_xephm_zar_tl          { \tl_set:Nn \l_xephm_hskip_default_tl { 0.116 em plus 0.5 em } }
             \l_xephm_ziba_tl         { \tl_set:Nn \l_xephm_hskip_default_tl { 0.119 em plus 0.5 em } }
             \l_xephm_default_tl      { \tl_set:Nn \l_xephm_hskip_default_tl { 0.14  em plus 0.5 em } }
             \l_xephm_noskip_tl       { \tl_set:Nn \l_xephm_hskip_default_tl { 0     em plus 0.5 em } }
          } { } { \tl_set:Nn \l_xephm_hskip_default_tl { #1 } }
      } ,
    kashidastretch .default:n = \tl_set:Nn \l_xephm_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-04-20} {0.5a} { Fixes~implementation~of~Kashida~in~xepersian~package }

\int_const:Nn \c_xephm_zwj_int {"200D} % zero-width joiner
\int_const:Nn \c_xephm_lrm_int {"200E} % left-right-mark
\int_const:Nn \c_xephm_two_int {2} % 2
\int_const:Nn \c_xephm_four_int {4} % 4
\int_const:Nn \c_xephm_ksh_int {"0640} % kashida

\int_new:N \l_xephm_counter_int

\newXeTeXintercharclass \c_xephm_d_charclass % dual-joiner class
\newXeTeXintercharclass \c_xephm_l_charclass % lam
\newXeTeXintercharclass \c_xephm_r_charclass % right-joiner
\newXeTeXintercharclass \c_xephm_a_charclass % alef
\newXeTeXintercharclass \c_xephm_y_charclass % yeh

\tl_new:N \l_xephm_hskip_zero_tl
\tl_new:N \l_xephm_hskip_default_tl

\dim_new:N \l_xephm_diff_pos_dim

%% Partly adapted from LaTeX2e source
\cs_new:Nn \xephm_line_break: {
  \if_mode_vertical:
    \GenericError{
      \space\space\space\space\space\space\space\space\space\space\space\space\space\space\space
      }{
         LaTeX Error: There’s no line here to end
      }{
         See the LaTeX manual or LaTeX Companion for explanation.
      }{
         Your command was ignored.\MessageBreak
         Type \space I <command> <return> \space to replace it~
         with another command,\MessageBreak
         or \space <return> \space to continue without it.}
  \else:
    \l_tmpa_skip \tex_lastskip:D
    \tex_unskip:D
    \tex_penalty:D -\l_xephm_line_break_penalty_int
    \dim_compare:nT { \l_tmpa_skip > \c_zero_skip }
      { \skip_horizontal:N \l_tmpa_skip  \tex_ignorespaces:D }
  \fi:
}

\NewDocumentCommand { \discouragebadlinebreaks } { m }
 {
  \xephm_put_line_breaks:n { #1 }
 }

\tl_new:N \l_xephm_line_break_tl

\cs_new_protected:Nn \xephm_put_line_breaks:n
 {
  \tl_set:Nn \l_xephm_line_break_tl { #1 }
  \regex_replace_all:nnN { ([ابپتثجحخچدذرزژسشعغصضفقطظکگلمنوهیـ])+ } { \ \0 \  \c{xephm_line_break:}\  } \l_xephm_line_break_tl
  \tl_use:N \l_xephm_line_break_tl
 }

%% Partly adapted from the code provided by David Carlisle in:
%% https://tex.stackexchange.com/questions/356709/how-to-know-the-width-and-fill-the-glue-space-between-two-characters-when-using/356721#356721
\cs_new:Npn \xephm_kashida_glyph #1
{
  \bool_if:NT \l_xephm_kashida_hm_fix_bool
  {
    \c_xephm_lrm_int\tex_penalty:D 10000
    \mode_leave_vertical:
    \tex_global:D \tex_advance:D \l_xephm_counter_int \c_one_int

    \tl_set:Nx \l_xephm_pos_tl { pos_\tex_romannumeral:D \l_xephm_counter_int }
    \tl_set:Nx \l_xephm_zref_tl { zref_\tex_romannumeral:D \l_xephm_counter_int }

    \zsaveposx{x_i_\l_xephm_zref_tl}
    \tl_set:Nx \l_tmpa_tl
      {
        \iow_now:cx { @auxout }
        {
          \ExplSyntaxOn
          \tl_gset:Nn \exp_after:wN \token_to_str:N \cs:w get_x_i\l_xephm_pos_tl \cs_end: { \zposx{ x_i_\l_xephm_zref_tl } }
          \ExplSyntaxOff
        }
      }
    \l_tmpa_tl
    \skip_horizontal:n { #1 }
    \zsaveposx{x_f_\l_xephm_zref_tl}
    \tl_set:Nx \l_tmpa_tl
      {
        \iow_now:cx { @auxout }
        {
          \ExplSyntaxOn
          \tl_gset:Nn \exp_after:wN \token_to_str:N \cs:w get_x_f\l_xephm_pos_tl \cs_end: { \zposx{ x_f_\l_xephm_zref_tl } }
          \ExplSyntaxOff
        }
      }
    \l_tmpa_tl
    \exp_after:wN
    \if_meaning:w
      \cs:w get_x_i\l_xephm_pos_tl \cs_end: \tex_relax:D
    \else:
      \dim_set:Nn \l_xephm_diff_pos_dim
        {
          \dim_eval:n { \cs:w get_x_i\l_xephm_pos_tl \cs_end: sp - \cs:w get_x_f\l_xephm_pos_tl \cs_end: sp }
        }
      \dim_compare:nTF { \l_xephm_diff_pos_dim == 0sp }
        { }
        { \llap { \resizebox { \l_xephm_diff_pos_dim \tex_relax:D } { \height } { \c_xephm_ksh_int } } }
    \fi:
  }
}

%% Partly adapted from the code provided by Jonathan Kew in:
%% https://tug.org/pipermail/xetex/2009-February/012307.html.
%% Somebody notified me that the code in 'kashida-xepersian.def' from xepersian
%% package is an exact copy of Jonathan Kew's code. Being unaware of this, in
%% the earlier versions of this package I made a mistake and acknowledged
%% Vafa Khalighi instead of Jonathan Kew. A sincere thank you to Jonathan Kew
%% for his excellent code.
\cs_new:Npn \xephm_kashida_hrule #1
  {
    \bool_if:NT \l_xephm_kashida_hm_fix_bool
    {
      \c_xephm_lrm_int\c_xephm_zwj_int\tex_penalty:D 10000
      \tex_leaders:D \tex_hrule:D height \XeTeXglyphbounds \c_xephm_two_int
      \int_use:N \XeTeXcharglyph \c_xephm_ksh_int depth \XeTeXglyphbounds \c_xephm_four_int
      \int_use:N \XeTeXcharglyph \c_xephm_ksh_int \skip_horizontal:n { #1 }
      \c_xephm_zwj_int
    }
  }

\XeTeXinterchartokenstate = 1

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

\clist_set:Nn \l_xephm_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_xephm_d_clist
  {
    \XeTeXcharclass "#1 \c_xephm_d_charclass
  }

\clist_set:Nn \l_xephm_l_clist { 0644 } % ‏ل‏
\clist_map_inline:Nn \l_xephm_l_clist
  {
    \XeTeXcharclass "#1 \c_xephm_l_charclass
  }

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

\clist_set:Nn \l_xephm_y_clist { 0649,064A,06CC } % ‏ی,ي,ى‏
\clist_map_inline:Nn \l_xephm_y_clist
  {
    \XeTeXcharclass "#1 \c_xephm_y_charclass
  }

\msg_new:nnx { xepersian-hm } { value-not-available-for-kashida-option }
  {
    Sorry,~ value~ `\l_xephm_gap_filler_tl'~ is~ not~ available~ for~ kashida~ option~ yet!
  }

\str_case:VnTF \l_xephm_gap_filler_tl
  {
    \l_xephm_stretch_glyph_tl
      {
        \XeTeXinterchartoks \c_xephm_y_charclass \c_xephm_y_charclass =
          {
            \bool_if:NTF \l_xephm_kashida_hm_fix_bool
            { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_default_tl \c_xephm_zwj_int }
            { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
          }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_y_charclass =
          {
            \bool_if:NTF \l_xephm_kashida_hm_fix_bool
            { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_default_tl \c_xephm_zwj_int }
            { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
          }
        \XeTeXinterchartoks \c_xephm_y_charclass \c_xephm_d_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_d_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_d_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_l_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_l_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_r_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_a_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_r_charclass =
          { \c_xephm_zwj_int \xephm_kashida_glyph \l_xephm_hskip_zero_tl \c_xephm_zwj_int }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_a_charclass =
          { }
      }
    \l_xephm_stretch_hrule_tl
      {
        \XeTeXinterchartoks \c_xephm_y_charclass \c_xephm_y_charclass =
          {
            \bool_if:NTF \l_xephm_kashida_hm_fix_bool
            { \xephm_kashida_hrule \l_xephm_hskip_default_tl }
            { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
          }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_y_charclass =
          {
            \bool_if:NTF \l_xephm_kashida_hm_fix_bool
            { \xephm_kashida_hrule \l_xephm_hskip_default_tl }
            { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
          }
        \XeTeXinterchartoks \c_xephm_y_charclass \c_xephm_d_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_d_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_d_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_l_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_l_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_r_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_d_charclass \c_xephm_a_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_r_charclass =
          { \xephm_kashida_hrule \l_xephm_hskip_zero_tl }
        \XeTeXinterchartoks \c_xephm_l_charclass \c_xephm_a_charclass =
          { }
      }
  } { } { \msg_error:nn { xepersian-hm } { value-not-available-for-kashida-option } }

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

\tex_let:D \KashidaOn \KashidaHMFixOn
\tex_let:D \KashidaOff \KashidaHMFixOff

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

\bool_if:NTF \l_xephm_kashida_hm_fix_bool
  {
    \tl_if_empty:NT \l_xephm_hskip_default_tl { \tl_set:Nn \l_xephm_hskip_default_tl  { 0.14 em plus 0.5 em } }
  }
  {
    \tl_set:NV \l_xephm_hskip_default_tl  \l_xephm_hskip_zero_tl
  }

\ExplSyntaxOff

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

 \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