typo-tal.mkiv /size: 10 Kb    last modification: 2020-07-01 14:35
1%D \module
2%D   [       file=typo-tal, % spac-cha (2012.06.08) supp-ali (2000.04.17)
3%D        version=2013.10.04,
4%D          title=\CONTEXT\ Typesetting Macros,
5%D       subtitle=Character Alignment,
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 / Character Alignment}
15
16%D This module replaces the \MKII\ character alignment code which hooked into
17%D table mechanisms but used parsing. In fact, this might be one of these cases
18%D where a \TEX\ based solution is faster, but a \LUA\ one a bit more robust.
19%D Anyway, as I had to fix something (to fit the newer table mechanisms) I
20%D decided to go the mixed route, a rather easy going effort in the aftermath of
21%D the 2013 \CONTEXT\ meeting.
22
23\unprotect
24
25\registerctxluafile{typo-tal}{}
26
27\definesystemattribute[characteralign][public]
28
29%D This mechanism is mostly meant for tables:
30%D
31%D \startbuffer
32%D \starttabulate[|l|g{,}|r|]
33%D     \NC test \NC 1.234.456,99 \NC \NC test \NR
34%D     \NC test \NC   234.456,9  \NC \NC test \NR
35%D     \NC test \NC   234.456    \NC \NC test \NR
36%D     \NC test \NC       456    \NC \NC test \NR
37%D     \NC test \NC \bf whatever \NC \NC test \NR
38%D \stoptabulate
39%D \stopbuffer
40%D
41%D \typebuffer \blank \getbuffer \blank
42
43% D \startbuffer
44% D \bTABLE
45% D     \bTR \bTD[aligncharacter=yes] €                      1,1     \eTD \eTR
46% D     \bTR \bTD[aligncharacter=yes] €                     11,11    \eTD \eTR
47% D     \bTR \bTD[aligncharacter=yes] € 12\punctuationspace111,11    \eTD \eTR
48% D     \bTR \bTD[aligncharacter=yes] €                 12 111,11    \eTD \eTR
49% D     \bTR \bTD[aligncharacter=yes] €              1.234.451,22222 \eTD \eTR
50% D     \bTR \bTD[aligncharacter=yes] €                234.451,2     \eTD \eTR
51% D     \bTR \bTD[aligncharacter=yes] €                234.451       \eTD \eTR
52% D     \bTR \bTD[aligncharacter=yes] €                    451       \eTD \eTR
53% D     \bTR \bTD                     \bf some text                  \eTD \eTR
54% D \eTABLE
55% D \stopbuffer
56% D
57% D \typebuffer \blank \getbuffer \blank
58
59\unexpanded\def\signalcharacteralign       #1#2{\c_attr_characteralign\numexpr#1*\maxcardminusone+#2\relax} % 0xFFFF
60\unexpanded\def\setcharacteralign          #1#2{\clf_setcharacteralign#1{#2}}
61\unexpanded\def\resetcharacteralign            {\clf_resetcharacteralign}
62\unexpanded\def\nocharacteralign               {\c_attr_characteralign\attributeunsetvalue}
63\unexpanded\def\setcharacteraligndetail#1#2#3#4{\clf_setcharacteraligndetail#1{#2}#3#4\relax}
64
65%D Mostly downward compatible:
66%D
67%D \startbuffer
68%D \startcharacteralign
69%D     \checkcharacteralign{123.456,78}
70%D     \checkcharacteralign{456}
71%D     \checkcharacteralign{23.456}
72%D     \checkcharacteralign{78,9}
73%D \stopcharacteralign
74%D \stopbuffer
75%D
76%D \typebuffer \blank \getbuffer \blank
77%D
78%D \startbuffer
79%D \startcharacteralign[leftsample=123.456,rightsample=00,character={,}]
80%D     \checkcharacteralign{123.456,78}\par
81%D     \checkcharacteralign    {456}\par
82%D     \checkcharacteralign {23.456}\par
83%D     \checkcharacteralign     {78,9}\par
84%D     \checkcharacteralign     {78}\par
85%D \stopcharacteralign
86%D \stopbuffer
87%D
88%D \typebuffer \blank \getbuffer \blank
89%D
90%D \startbuffer
91%D \startcharacteralign[leftwidth=123.456,rightwidth=00,character={,}]
92%D     \checkcharacteralign{123.456,78}\par
93%D     \checkcharacteralign    {456}\par
94%D     \checkcharacteralign {23.456}\par
95%D     \checkcharacteralign     {78,9}\par
96%D     \checkcharacteralign     {78}\par
97%D \stopcharacteralign
98%D \stopbuffer
99%D
100%D \typebuffer \blank \getbuffer \blank
101%D
102%D We have (currently) two modes: \type {text} and \type {number}. The handler tries
103%D to determine the mode automatically. When using periods and commas as separators
104%D the \type {number} mode is chosen. If you use for instance a \type {-} as
105%D separator, \type {text} is chosen, but you can enforce \type {number} with \type
106%D {number->-} (as with other mechanisms, the arrow indicates a method to apply).
107%D
108%D One can use \type {\nocharacteralign} to disable this mechanism, for instance in
109%D a table cell.
110
111\def\alignmentcharacter{,}
112
113\installcorenamespace{characteralign}
114
115\installparameterhandler\??characteralign {characteralign}
116\installsetuphandler    \??characteralign {characteralign}
117
118\setupcharacteralign
119  [\c!leftwidth  =\zeropoint,
120   \c!rightwidth =\zeropoint,
121   \c!leftsample =,
122   \c!rightsample=,
123   \c!character  =\alignmentcharacter]
124
125\unexpanded\def\typo_charalign_pass_one
126  {\advance\scratchcounter\plusone
127   \setbox\scratchbox\typo_charalign_pass}
128
129\unexpanded\def\typo_charalign_pass_two
130  {\advance\scratchcounter\plusone
131   \typo_charalign_pass}
132
133\def\typo_charalign_pass
134  {\hbox\bgroup\signalcharacteralign\plusone\scratchcounter\let\next}
135
136\unexpanded\def\startcharacteralign
137  {\dosingleempty\typo_charalign_start}
138
139\def\typo_charalign_start[#1]%
140  {\doifelseassignment{#1}\typo_charalign_start_one\typo_charalign_start_two{#1}}
141
142\def\typo_charalign_start_one#1#2\stopcharacteralign
143  {\bgroup
144   % for now no instances
145   \setupcurrentcharacteralign[#1]%
146   \edef\p_left {\characteralignparameter\c!leftsample}%
147   \edef\p_right{\characteralignparameter\c!rightsample}%
148   \ifx\p_left\empty
149     \scratchdimenone\dimexpr\characteralignparameter\c!leftwidth\relax
150   \else
151     \setbox\scratchbox\hbox{\p_left}%
152     \scratchdimenone\wd\scratchbox
153   \fi
154   \ifx\p_right\empty
155     \scratchdimentwo\dimexpr\characteralignparameter\c!rightwidth\relax
156   \else
157     \setbox\scratchbox\hbox{\p_right}%
158     \scratchdimentwo\wd\scratchbox
159   \fi
160   \ifzeropt\scratchdimenone
161      \ifzeropt\scratchdimentwo
162         \donefalse
163      \else
164         \donetrue
165      \fi
166   \else
167     \donetrue
168   \fi
169   \edef\alignmentcharacter{\characteralignparameter\c!character}%
170   \ifdone
171     \clf_setcharacteraligndetail
172       \plusone
173       \alignmentcharacter
174       \scratchdimenone
175       \scratchdimentwo
176   \else
177     \clf_setcharacteralign
178       \plusone
179       \alignmentcharacter
180     \begingroup
181       \scratchcounter\zerocount
182       \let\checkcharacteralign\typo_charalign_pass_one
183       \settrialtypesetting
184       #2\relax
185     \endgroup
186   \fi
187   \begingroup
188     \scratchcounter\zerocount
189     \let\checkcharacteralign\typo_charalign_pass_two
190     #2\relax
191   \endgroup
192   \resetcharacteralign
193   \egroup}
194
195\def\typo_charalign_start_two#1#2\stopcharacteralign
196  {\bgroup
197   \edef\m_temp{#1}%
198   \ifx\m_temp\empty \else
199     \let\alignmentcharacter\m_temp
200   \fi
201   \clf_setcharacteralign
202     \plusone
203     \alignmentcharacter
204   \begingroup
205     \scratchcounter\zerocount
206     \let\checkcharacteralign\typo_charalign_pass_one
207     \settrialtypesetting
208     #2\relax
209   \endgroup
210   \begingroup
211     \scratchcounter\zerocount
212     \let\checkcharacteralign\typo_charalign_pass_two
213     #2\relax
214   \endgroup
215   \resetcharacteralign
216   \egroup}
217
218\let\stopcharacteralign \relax
219\let\checkcharacteralign\gobbleoneargument
220
221\def\setfirstpasscharacteralign {\let\checkcharacteralign\gobbleoneargument}
222\def\setsecondpasscharacteralign{\let\checkcharacteralign\firstofoneargument}
223
224%D We need fonts to provide tabular digits that is, the digits need to have the same
225%D width.
226%D
227%D \startbuffer
228%D \startbuffer[demo]
229%D     \switchtobodyfont[pagella]
230%D     \setupTABLE[column][1][alignmentcharacter=.,aligncharacter=yes]
231%D     \bTABLE
232%D         \bTR \bTD  11.111 \eTD \bTD  11.111 \eTD \eTR
233%D         \bTR \bTD   2.2   \eTD \bTD   2.2   \eTD \eTR
234%D         \bTR \bTD 444.444 \eTD \bTD 444.444 \eTD \eTR
235%D     \eTABLE
236%D \stopbuffer
237%D
238%D \start inlinenumbers:  \crlf \addfeature[inlinenumbers]   \getbuffer \stop
239%D \start tabularnumbers: \crlf \addfeature[tabularnumbers]  \getbuffer \stop
240%D \start oldstylenumbers:\crlf \addfeature[oldstylenumbers] \getbuffer \stop
241%D \stopbuffer
242%D
243%D \enabledirectives[typesetters.characteralign.autofont]
244%D \typebuffer \blank \getbuffer \blank
245%D \disabledirectives[typesetters.characteralign.autofont]
246%D \typebuffer \blank \getbuffer \blank
247%D \enabledirectives[typesetters.characteralign.autofont]
248
249\definefontfeature
250  [system:tabnum]
251  [tnum=yes,
252   lnum=no]
253
254\newconditional\c_tabl_ntb_char_align_auto_font \settrue\c_tabl_ntb_char_align_auto_font
255
256\installtexdirective % yes or no ?
257  {typesetters.characteralign.autofont}
258  {\settrue \c_tabl_ntb_char_align_auto_font}
259  {\setfalse\c_tabl_ntb_char_align_auto_font}
260
261\def\m_font_feature_auto_tabnum{system:tabnum}
262
263\unexpanded\def\typo_charalign_adapt_font_indeed
264  {\let\m_font_feature_asked\m_font_feature_auto_tabnum
265   \font_feature_reset_add_indeed}
266
267\def\typo_charalign_adapt_font % slow but seldom used (expanded in preamble)
268  {\ifconditional\c_tabl_ntb_char_align_auto_font
269     \typo_charalign_adapt_font_indeed
270   \fi}
271
272%D Another example:
273%D
274%D \starttyping
275%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={flushleft}]
276%D \bTABLE
277%D \bTR \bTD 1 \eTD \bTD   125     cm                       \eTD \eTR
278%D \bTR \bTD 2 \eTD \bTD 1 125,80  cm                       \eTD \eTR
279%D \bTR \bTD 6 \eTD \bTD 1 125,80  $\pi^2$                  \eTD \eTR
280%D \bTR \bTD 7 \eTD \bTD   129,3   \unit{square centimeter} \eTD \eTR
281%D \eTABLE
282%D
283%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={flushleft}]
284%D \bTABLE
285%D \bTR \bTD 1 \eTD \bTD   125     cm                       \eTD \eTR
286%D \bTR \bTD 2 \eTD \bTD 1 125,80  cm                       \eTD \eTR
287%D \bTR \bTD 6 \eTD \bTD 1 125,80  $\pi^2$                  \eTD \eTR
288%D \bTR \bTD 7 \eTD \bTD   129,3   \unit{square centimeter} \eTD \eTR
289%D \eTABLE
290%D
291%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={middle}]
292%D \bTABLE
293%D \bTR \bTD 1 \eTD \bTD   125    \eTD \eTR
294%D \bTR \bTD 6 \eTD \bTD 1 125,80 \eTD \eTR
295%D \bTR \bTD 7 \eTD \bTD   129,3  \eTD \eTR
296%D \eTABLE
297%D
298%D \setupTABLE[c][2][alignmentcharacter={text->,},aligncharacter=yes,align={middle}]
299%D \bTABLE
300%D \bTR \bTD 1 \eTD \bTD   125    \eTD \eTR
301%D \bTR \bTD 6 \eTD \bTD 1 125,80 \eTD \eTR
302%D \bTR \bTD 7 \eTD \bTD   129,3  \eTD \eTR
303%D \eTABLE
304%D \stoptyping
305
306\protect \endinput
307