typo-dir.mkiv /size: 7185 b    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=typo-dir,
3%D        version=2009.03.27, % code moved from core-spa.mkiv
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Directions,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Typesetting Macros / Directions}
15
16%D At some point we might default to method 'two' but first I need to make it more
17%D efficient (and provide some options). I also want to have some basic tracing.
18
19\unprotect
20
21\registerctxluafile{typo-dir}{optimize}
22\registerctxluafile{typo-dha}{}
23%registerctxluafile{typo-dua}{}
24%registerctxluafile{typo-dub}{}
25\registerctxluafile{typo-duc}{}
26
27\definesystemattribute[directions][public,pickup]
28
29\installcorenamespace{directions}
30\installcorenamespace{directionsbidimode}
31
32% plural as we can have a combination but maybe better singular
33
34\installsimplecommandhandler \??directions {directions} \??directions % no \define... yet
35
36\edef\lefttorightmark{\normalUchar"200E} \let\lrm\lefttorightmark
37\edef\righttoleftmark{\normalUchar"200F} \let\rlm\righttoleftmark
38
39\unexpanded\def\setdirection[#1]% todo: symbolic names
40  {\clf_setdirection#1\relax}
41
42% \unexpanded\def\resetdirection
43%   {\clf_setdirection\zerocount}
44%
45% is in fact:
46
47\unexpanded\def\resetdirection
48  {\c_attr_directions\attributeunsetvalue}
49
50\newconstant\directionsbidimode % this one might become pivate
51
52% \setupdirections[bidi=global,method=default]
53% \setupdirections[bidi=global,method=one]
54% \setupdirections[bidi=global,method=two]
55% \setupdirections[bidi=global,method=two,fences=no]
56
57% maybe use chardefs
58
59\def\typo_dir_get_mode
60  {\def\currentbidimode{\clf_getbidimode
61        scope  {\directionsparameter\c!bidi}%
62        method {\directionsparameter\c!method}%
63        fences {\directionsparameter\c!fences}%
64   }%
65   \expandafter\glet\csname\??directionsbidimode\currentbidistamp\endcsname\currentbidimode}
66
67\appendtoks
68    \edef\p_bidi{\directionsparameter\c!bidi}%
69    \edef\currentbidistamp
70      {\p_bidi
71      :\directionsparameter\c!method
72      :\directionsparameter\c!fences}%
73    \expandafter\let\expandafter\currentbidimode\csname\??directionsbidimode\currentbidistamp\endcsname
74    \ifx\currentbidimode\relax
75        \typo_dir_get_mode
76    \fi
77    \directionsbidimode\currentbidimode\relax
78    \ifcase\directionsbidimode
79        \resetdirection
80    \else
81        \setdirection[\number\directionsbidimode]%
82    \fi
83    \ifx\p_bidi\v!global
84        \pickupdirectionsattribute
85    \else
86        \forgetdirectionsattribute
87    \fi
88\to \everysetupdirections
89
90\appendtoks
91    \edef\p_option{\directionsparameter\c!break}% name can change
92    \breakafterdirmode\ifx\p_option\v!both\plusone\else\zerocount\fi
93\to \everysetupdirections
94
95% bidi: local=obey grouping, global=ignore grouping (unicode has no grouping)
96
97\setupdirections % maybe start/stop
98  [\c!bidi=\v!off,
99   \c!method=\v!default,
100   \c!break=\v!both, % experimental value, maybe \v!no will be default (bad name too)
101   \c!fences=\v!yes]
102
103\unexpanded\edef\bidilre{\normalUchar"202A}
104\unexpanded\edef\bidirle{\normalUchar"202B}
105\unexpanded\edef\bidipop{\normalUchar"202C}
106\unexpanded\edef\bidilro{\normalUchar"202D}
107\unexpanded\edef\bidirlo{\normalUchar"202E}
108
109\unexpanded\def\dirlre{\ifcase\directionsbidimode\or\bidilre\or\textdirection\directionlefttoright\fi}
110\unexpanded\def\dirrle{\ifcase\directionsbidimode\or\bidirle\or\textdirection\directionrighttoleft\fi}
111\unexpanded\def\dirlro{\ifcase\directionsbidimode\or\bidilro\or\setdirection[3]\fi}
112\unexpanded\def\dirrlo{\ifcase\directionsbidimode\or\bidirlo\or\setdirection[4]\fi}
113
114% for the moment: \setdirection[\plusone]
115
116\definecolor[bidi:left:original] [r=.6]
117\definecolor[bidi:left:reversed] [g=.6]
118\definecolor[bidi:right:original][b=.6]
119\definecolor[bidi:right:reversed][r=.6,g=.6]
120\definecolor[bidi:mirrored]      [r=.6,b=.6]
121
122\protect \endinput
123
124% bidi test
125
126% \definefontfeature
127%   [arab]
128%   [mode=node,language=dflt,script=arab,
129%    init=yes,medi=yes,fina=yes,isol=yes,
130%    liga=yes,dlig=yes,rlig=yes,clig=yes,
131%    mark=yes,mkmk=yes,kern=yes,curs=yes]
132%
133% \font\Arabic=arabtype*arab at 20pt
134%
135% \def\LATIN{LATIN} {\setdirection[1]} % enable this
136% \def\ARAB {عربي}
137%
138% \startluacode
139%     function documentdata.split_tokens(str)
140%         for s in str:bytes() do
141%             context.sprint(tex.ctxcatcodes,string.format("\\hbox{\\char %s}",s))
142%         end
143%     end
144% \stopluacode
145%
146% \unexpanded\def\biditest#1#2#3% font text raw
147%   {\dontleavehmode\hbox
148%     {\framed[offset=overlay]{\tttf#2}\quad
149%      \enabletrackers[typesetters.directions]%
150%      \framed[offset=overlay]{#1#3}\quad
151%      \disabletrackers[typesetters.directions]%
152%      \tttf\ctxlua{documentdata.split_tokens([[\detokenize{#3}]])}}}
153%
154% \startbuffer[bidi-sample]
155% \biditest\Arabic{LATIN BARA}{\lefttoright\relax   \LATIN\ \ARAB}\par
156% \biditest\Arabic{BARA LATIN}{\righttoleft\relax   \LATIN\ \ARAB}\par
157% \biditest\Arabic{LATIN ARAB}{\lefttoright{\bidilro \LATIN\ \ARAB}}\par % right -> left
158% \biditest\Arabic{LATIN ARAB}{\righttoleft{\bidilro \LATIN\ \ARAB}}\par % right -> left
159% \biditest\Arabic{BARA NITAL}{\lefttoright{\bidirlo \LATIN\ \ARAB}}\par % left -> right
160% \biditest\Arabic{BARA NITAL}{\righttoleft{\bidirlo \LATIN\ \ARAB}}\par % left -> right
161% \stopbuffer
162%
163% \startbuffer[bidi-sample]
164% \biditest\Arabic{LATIN BARA}{\lefttoright\relax   \LATIN\ \ARAB}\par
165% \biditest\Arabic{BARA LATIN}{\righttoleft\relax   \LATIN\ \ARAB}\par
166% \biditest\Arabic{LATIN ARAB}{\lefttoright\bidilro \LATIN\ \ARAB}\par % right -> left
167% \biditest\Arabic{LATIN ARAB}{\righttoleft\bidilro \LATIN\ \ARAB}\par % right -> left
168% \biditest\Arabic{BARA NITAL}{\lefttoright\bidirlo \LATIN\ \ARAB}\par % left -> right
169% \biditest\Arabic{BARA NITAL}{\righttoleft\bidirlo \LATIN\ \ARAB}\par % left -> right
170% \stopbuffer
171%
172% \startbuffer[bidi-setup]
173% \setupdirections[bidi=off]
174% \stopbuffer
175%
176% {\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
177%
178% \startbuffer[bidi-setup]
179% \setupdirections[bidi=global]
180% \stopbuffer
181%
182% {\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
183%
184% \startbuffer[bidi-setup]
185% \setupdirections[bidi=local]
186% \stopbuffer
187%
188% {\typebuffer[bidi-setup] \getbuffer[bidi-setup] \getbuffer[bidi-sample]}
189%
190% \startbuffer[bidi-sample]
191% \setupdirections[bidi=global]
192%
193%                   \hbox{\righttoleft\arabicfont (0001)}\par
194%    \dontleavehmode\hbox{\righttoleft\arabicfont (0002)}\par
195%                        {\righttoleft\arabicfont (0003)\par}
196%                        {\righttoleft\arabicfont (0004)}\par
197%         \dontleavehmode{\righttoleft\arabicfont (0005)\par}
198%         \dontleavehmode{\righttoleft\arabicfont (0006)}\par
199%                \rtlhbox{\arabicfont (0007)}\par
200%                \ltrhbox{\arabicfont (0008)}\par
201% \dontleavehmode\rtlhbox{\arabicfont (0009)}\par
202% \dontleavehmode\ltrhbox{\arabicfont (0010)}\par
203% \stopsetups
204%
205% {\typebuffer[bidi-sample]  \getbuffer[bidi-sample]}
206%
207% \stoptext
208