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
|
%D \module
%D [ file=typo-dir,
%D version=2009.03.27, % code moved from core-spa.mkiv
%D title=\CONTEXT\ Typesetting Macros,
%D subtitle=Directions,
%D author=Hans Hagen,
%D date=\currentdate,
%D copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.
\writestatus{loading}{ConTeXt Typesetting Macros / Directions}
\unprotect
\registerctxluafile{typo-dir}{1.001}
\definesystemattribute[directions][public]
\installcorenamespace{directions}
\installcorenamespace{directionsbidimode}
% plural as we can have a combination but maybe better singular
\installsimplecommandhandler \??directions {directions} \??directions % no \define... yet
\unexpanded\def\setdirection[#1]% todo: symbolic names
{\ctxcommand{setdirection(\number#1)}}
\unexpanded\def\resetdirection
%{\ctxcommand{setdirection(0)}}
{\attribute\directionsattribute\attributeunsetvalue}
\newconstant\directionsbidimode % this one might become pivate
\letvalue{\??directionsbidimode\v!off }\zerocount
\letvalue{\??directionsbidimode\v!global}\plusone
\letvalue{\??directionsbidimode\v!local }\plustwo
\letvalue{\??directionsbidimode\v!on }\plustwo
\appendtoks
\directionsbidimode
\ifcsname\??directionsbidimode\directionsparameter\c!bidi\endcsname
\csname\??directionsbidimode\directionsparameter\c!bidi\endcsname
\else
\zerocount
\fi
\relax
\ifcase\directionsbidimode
\resetdirection
\or % 1
\setdirection[1]% global, chars
\or % 2
\setdirection[2]% local, attributes
\or % else
\setdirection[1]% default
\fi
\to \everysetupdirections
% bidi: local=obey grouping, global=ignore grouping (unicode has no grouping)
\setupdirections % maybe start/stop
[\c!bidi=\v!off]
\unexpanded\def\bidilre{\utfchar{"0x202A}} % maybe \edef's
\unexpanded\def\bidirle{\utfchar{"0x202B}}
\unexpanded\def\bidipop{\utfchar{"0x202C}}
\unexpanded\def\bidilro{\utfchar{"0x202D}}
\unexpanded\def\bidirlo{\utfchar{"0x202E}}
\unexpanded\def\dirlre{\ifcase\directionsbidimode\or\bidilre\or\textdir TLT\fi}
\unexpanded\def\dirrle{\ifcase\directionsbidimode\or\bidirle\or\textdir TRT\fi}
\unexpanded\def\dirlro{\ifcase\directionsbidimode\or\bidilro\or\setdirection[3]\fi}
\unexpanded\def\dirrlo{\ifcase\directionsbidimode\or\bidirlo\or\setdirection[4]\fi}
% for the moment: \setdirection[\plusone]
\protect \endinput
% bidi test
\definefontfeature
[arab]
[mode=node,language=dflt,script=arab,
init=yes,medi=yes,fina=yes,isol=yes,
liga=yes,dlig=yes,rlig=yes,clig=yes,
mark=yes,mkmk=yes,kern=yes,curs=yes]
\font\Arabic=arabtype*arab at 20pt
\def\LATIN{LATIN} {\setdirection[1]} % enable this
\def\ARAB {محمد}
\startluacode
function documentdata.split_tokens(str)
for s in str:bytes() do
context.sprint(tex.ctxcatcodes,string.format("\\hbox{\\char %s}",s))
end
end
\stopluacode
\def\biditest#1#2#3% font text raw
{\dontleavehmode\hbox
{\framed[offset=overlay]{\tttf#2}\quad
\enabletrackers[typesetters.directions]%
\framed[offset=overlay]{#1#3}\quad
\disabletrackers[typesetters.directions]%
\tttf\ctxlua{documentdata.split_tokens([[\detokenize{#3}]])}}}
\startbuffer[bidi-sample]
\biditest\Arabic{LATIN BARA}{\textdir TLT\relax \LATIN\ \ARAB}\par
\biditest\Arabic{BARA LATIN}{\textdir TRT\relax \LATIN\ \ARAB}\par
\biditest\Arabic{LATIN ARAB}{\textdir TLT{\bidilro \LATIN\ \ARAB}}\par % right -> left
\biditest\Arabic{LATIN ARAB}{\textdir TRT{\bidilro \LATIN\ \ARAB}}\par % right -> left
\biditest\Arabic{BARA NITAL}{\textdir TLT{\bidirlo \LATIN\ \ARAB}}\par % left -> right
\biditest\Arabic{BARA NITAL}{\textdir TRT{\bidirlo \LATIN\ \ARAB}}\par % left -> right
\stopbuffer
\startbuffer[bidi-sample]
\biditest\Arabic{LATIN BARA}{\textdir TLT\relax \LATIN\ \ARAB}\par
\biditest\Arabic{BARA LATIN}{\textdir TRT\relax \LATIN\ \ARAB}\par
\biditest\Arabic{LATIN ARAB}{\textdir TLT\bidilro \LATIN\ \ARAB}\par % right -> left
\biditest\Arabic{LATIN ARAB}{\textdir TRT\bidilro \LATIN\ \ARAB}\par % right -> left
\biditest\Arabic{BARA NITAL}{\textdir TLT\bidirlo \LATIN\ \ARAB}\par % left -> right
\biditest\Arabic{BARA NITAL}{\textdir TRT\bidirlo \LATIN\ \ARAB}\par % left -> right
\stopbuffer
\startbuffer[bidi-setup]
\setupdirections[bidi=off]
\stopbuffer
{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
\startbuffer[bidi-setup]
\setupdirections[bidi=global]
\stopbuffer
{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
\startbuffer[bidi-setup]
\setupdirections[bidi=local]
\stopbuffer
{\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
\stoptext
|