summaryrefslogtreecommitdiff
path: root/macros/xetex/latex/bidihl/bidihl.sty
blob: ec3f12afa94fa7a122e42102d8b75d57405217b5 (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
%%
%% This is file `bidihl.sty'.
%% 
%%   ___________________________________________
%%   Copyright © 2015  Vafa Khalighi <persian-tex@tug.org>
%% 
%%   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
%% 
%%   This work is “author-maintained” (as per LPPL maintenance status)
%%   by Vafa Khalighi.
%% 
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{bidihl}[2015/07/07 v0.1c Experimental bidi-aware text highlighting]

%% Load color package and define highlighting color

\RequirePackage{color}
\definecolor{bidihlcolor}{rgb}{1,1,0} % yellow

%% Register Declaration

\newdimen\bidihl@line@thickness \bidihl@line@thickness.4pt

\newdimen\bidihl@pixel \bidihl@pixel0.1pt

\newdimen\bidihl@height \newdimen\bidihl@depth

\newcount\bidihl@spacefactor

\newif\ifbidihl@nospace

%% User Interface and Initialization

\def\bidihl{\bidihl@highlight{\dp\strutbox}{\ht\strutbox}}


\def\bidihl@line#1#2#3#4{\begingroup \let\\\bidihlnewline
        \ifdim#1<\z@ \bidihl@height#3\relax
        \else \bidihl@height#2\relax \fi
        \bidihl@depth-\bidihl@height
        \ifdim\bidihl@height<\z@ \advance\bidihl@depth\bidihl@line@thickness
        \else                 \advance\bidihl@height\bidihl@line@thickness \fi
        \bidihl@nospacetrue
        \ifvmode\leavevmode\fi \bidihl@spacefactor\spacefactor
        \def\@tempa{#4 }\edef\@tempb{\noexpand\@nil\space}%
        \expandafter\expandafter\expandafter\bidihl@wordloop
                \expandafter\@tempa\@tempb
        \endgroup \bidihl@aftergroup}
        
        
\def\bidihl@highlight#1#2#3{\begingroup \let\\\bidihlnewline
        \bidihl@depth#1\relax
        \bidihl@height#2\relax
        \bidihl@nospacetrue
        \ifvmode\leavevmode\fi \bidihl@spacefactor\spacefactor
        \def\@tempa{#3 }\edef\@tempb{\noexpand\@nil\space}%
        \expandafter\expandafter\expandafter\bidihl@wordloop
                \expandafter\@tempa\@tempb
        \endgroup \bidihl@aftergroup}
       
       
%% Processing Word Elements 
        
\def\bidihl@wordloop{\bidihl@ifbgroup\bidihl@wordblock\bidihl@iwordloop}
\def\bidihl@ifbgroup#1#2{\let\@tempa#1\let\@tempb#2\futurelet\@tempc\bidihl@ifnc}
\def\bidihl@ifnc{\ifx\@tempc\bgroup \let\next\@tempa \else\let\next\@tempb \fi
        \next}
\def\bidihl@wordblock#1{\bidihl@spaceskip
        \def\bidihl@theword{#1}\bidihl@putword \bidihl@nospacetrue
        \bidihl@wordloop}
\def\bidihl@iwordloop#1 {\def\@tempa{#1}\ifx\@tempa\@nnil \let\next\bidihl@end
        \else\ifx\@tempa\empty \else
                \bidihl@spaceskip
                \def\bidihl@theword{}\bidihl@charloop#1\@nil\fi
        \bidihl@nospacefalse \let\next\bidihl@wordloop \fi
        \next}        
        
\def\bidihl@charloop{\bidihl@ifbgroup\bidihl@charblock\bidihl@icharloop}
\def\bidihl@charblock#1{\bidihl@putword
        \def\bidihl@theword{#1}\bidihl@putword
        \bidihl@charloop}
\def\bidihl@icharloop#1{\def\@tempa{#1}%
        \ifx\@tempa\@nnil \bidihl@putword \let\next\relax
        \else\ifx\bidihlspace#1\relax \bidihl@putword \let\next\bidihl@space
        \else\ifx\bidihlnewline#1\relax \bidihl@putword \let\next\bidihl@newline
        \else\edef\bidihl@theword{\bidihl@theword#1}%
                \let\next\bidihl@charloop \fi\fi\fi \next}
\def\bidihlspace{\PackageError{bidihl}%
        {\string\bidihlspace\space cannot be used here.}%
        {\string\bidihlspace\space can be used only in the argument of
                \string\bidihl.}}
\def\bidihlnewline{\PackageError{bidihl}%
        {\string\bidihlnewline\space cannot be used here.}%
        {\string\bidihlnewline\space can be used only in the argument of
                \string\bidihl.}}        

%% Interword Spacing

\def\bidihl@spaceskip{\ifbidihl@nospace\else
        \ifdim\spaceskip=\z@
            \@tempdima\fontdimen3\font\relax
                \multiply\@tempdima\bidihl@spacefactor \divide\@tempdima\@m
            \@tempdimb\fontdimen4\font\relax \multiply\@tempdimb\@m
                \divide\@tempdimb\bidihl@spacefactor
            \@tempskipa\fontdimen2\font plus\@tempdima minus\@tempdimb\relax
        \else
            \edef\@tempa{\the\spaceskip\space @ @ @ @ }%
            \expandafter\bidihl@setspaceskip\@tempa\@nil
        \fi
        \ifnum\bidihl@spacefactor<2000\else
                \ifdim\xspaceskip=\z@ \advance\@tempskipa\fontdimen7\font
                \else \@tempskipa\xspaceskip
        \fi\fi
        \bidihl@skip\@tempskipa \fi}
\def\bidihl@setspaceskip#1 #2 #3 #4 #5 #6\@nil{\@tempdima\z@ \@tempdimb\z@
        \def\@tempa{#2}\def\@tempb{#3}%
        \ifx\@tempa\bidihl@plus \@tempdima#3\def\@tempa{#4}\def\@tempb{#5}\fi
        \ifx\@tempa\bidihl@minus \@tempdimb\@tempb\relax\fi
        \multiply\@tempdima\bidihl@specefactor \divide\@tempdima\@m
        \multiply\@tempdimb\@m \divide\bidihl@spacefactor
        \@tempskipa#1 plus\@tempdima minus\@tempdimb\relax}
\def\@tempa#1 #2 #3 #4 #5\@nil{\def\bidihl@plus{#2}\def\bidihl@minus{#4}}
\@tempskipa1pt plus 2pt minus 3pt
\expandafter\@tempa\the\@tempskipa\@nil



%% Draw highlight text

\def\bidihl@putword{\setbox\@tempboxa\hbox{\bidihl@theword
                \global\bidihl@spacefactor\spacefactor}%
        \@tempdima\wd\@tempboxa
        \if@RTL%
          \box\@tempboxa
          {\color{bidihlcolor}\llap{\vrule\@height\bidihl@height\@depth\bidihl@depth\@width\@tempdima}}%
        \else
        {\color{bidihlcolor}\rlap{\vrule\@height\bidihl@height\@depth\bidihl@depth\@width\@tempdima}}%
        \box\@tempboxa
        \fi
        \def\bidihl@theword{}}
\def\bidihl@skip#1{%
       \if@RTL%
          \leaders\hb@xt@\bidihl@pixel{\hss{\color{bidihlcolor}\vrule\@height\bidihl@height\@depth\bidihl@depth}\hss}%
          \hskip#1\relax%
        \else%
          {\color{bidihlcolor}\leaders\hrule\@height\bidihl@height\@depth\bidihl@depth\hskip#1\relax}%
        \fi}



%% Explicit Spacing and Line Breaking

\def\bidihl@space{\@ifstar\bidihl@sspace\bidihl@ispace}
\def\bidihl@sspace#1{\vrule width\z@\nobreak\bidihl@skip{#1}\bidihl@charloop}
\def\bidihl@ispace#1{\@tempskipa#1\relax
        \@ifstar{\@tempswafalse\bidihl@iispace}{\@tempswatrue\bidihl@iispace}}
\def\bidihl@iispace{\@ifnextchar[%]
                              {\bidihl@penalty}%
                              {\bidihl@skip\@tempskipa \bidihl@charloop}}
\def\bidihl@penalty[#1]{\@tempcnta#1\relax
        \if@tempswa
                \ifnum\@tempcnta<\z@ \@tempcnta-\@tempcnta \fi
                \ifcase\@tempcnta \or
                        \@tempcnta\@lowpenalty \or
                        \@tempcnta\@medpenalty \or
                        \@tempcnta\@highpenalty \else
                        \@tempcnta\@M \fi
                \ifnum#1<\z@ \@tempcnta-\@tempcnta \fi \fi
        \penalty\@tempcnta \bidihl@skip\@tempskipa \bidihl@charloop}

\def\bidihl@newline{\@ifstar{\bidihl@skip{0pt plus1fil}\break \bidihl@charloop}%
                                          {\hfil \break \bidihl@charloop}}





%% Finalization

\def\bidihl@end{\xdef\bidihl@aftergroup{\spacefactor\number\bidihl@spacefactor\relax}}
\endinput
%%
%% End of file `bidihl.sty'.