typo-tal.mkxl /size: 10 Kb    last modification: 2023-12-21 09:44
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}{autosuffix}
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\permanent\protected\def\signalcharacteralign       #1#2{\c_attr_characteralign\numexpr#1*\maxcardminusone+#2\relax} % 0xFFFF
60\permanent\protected\def\setcharacteralign          #1#2{\clf_setcharacteralign#1{#2}}
61\permanent\protected\def\resetcharacteralign            {\clf_resetcharacteralign}
62\permanent\protected\def\nocharacteralign               {\c_attr_characteralign\attributeunsetvalue}
63\permanent\protected\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\mutable\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\permanent\protected\def\typo_charalign_pass_one
126  {\advanceby\scratchcounter\plusone
127   \setbox\scratchbox\typo_charalign_pass}
128
129\permanent\protected\def\typo_charalign_pass_two
130  {\advanceby\scratchcounter\plusone
131   \typo_charalign_pass}
132
133\def\typo_charalign_pass
134  {\hbox\bgroup\signalcharacteralign\plusone\scratchcounter\let\next}
135
136\permanent\tolerant\protected\def\startcharacteralign[#S#1]%
137  {\doifelseassignment{#1}\typo_charalign_start_one\typo_charalign_start_two{#1}}
138
139\def\typo_charalign_start_one#1#2\stopcharacteralign
140  {\bgroup
141   % for now no instances
142   \setupcurrentcharacteralign[#1]%
143   \edef\p_left {\characteralignparameter\c!leftsample}%
144   \edef\p_right{\characteralignparameter\c!rightsample}%
145   \ifempty\p_left
146     \scratchdimenone\dimexpr\characteralignparameter\c!leftwidth\relax
147   \else
148     \setbox\scratchbox\hbox{\p_left}%
149     \scratchdimenone\wd\scratchbox
150   \fi
151   \ifempty\p_right
152     \scratchdimentwo\dimexpr\characteralignparameter\c!rightwidth\relax
153   \else
154     \setbox\scratchbox\hbox{\p_right}%
155     \scratchdimentwo\wd\scratchbox
156   \fi
157   \ifzeropt\scratchdimenone
158      \ifzeropt\scratchdimentwo
159         \donefalse
160      \else
161         \donetrue
162      \fi
163   \else
164     \donetrue
165   \fi
166   \edef\alignmentcharacter{\characteralignparameter\c!character}%
167   \ifdone
168     \clf_setcharacteraligndetail
169       \plusone
170       \alignmentcharacter
171       \scratchdimenone
172       \scratchdimentwo
173   \else
174     \clf_setcharacteralign
175       \plusone
176       \alignmentcharacter
177     \begingroup
178       \scratchcounter\zerocount
179       \enforced\let\checkcharacteralign\typo_charalign_pass_one
180       \settrialtypesetting
181       #2\relax
182     \endgroup
183   \fi
184   \begingroup
185     \scratchcounter\zerocount
186     \enforced\let\checkcharacteralign\typo_charalign_pass_two
187     #2\relax
188   \endgroup
189   \resetcharacteralign
190   \egroup}
191
192\def\typo_charalign_start_two#1#2\stopcharacteralign
193  {\bgroup
194   \edef\m_temp{#1}%
195   \ifempty\m_temp \else
196     \let\alignmentcharacter\m_temp
197   \fi
198   \clf_setcharacteralign
199     \plusone
200     \alignmentcharacter
201   \begingroup
202     \scratchcounter\zerocount
203     \enforced\let\checkcharacteralign\typo_charalign_pass_one
204     \settrialtypesetting
205     #2\relax
206   \endgroup
207   \begingroup
208     \scratchcounter\zerocount
209     \enforced\let\checkcharacteralign\typo_charalign_pass_two
210     #2\relax
211   \endgroup
212   \resetcharacteralign
213   \egroup}
214
215\permanent\protected\lettonothing\stopcharacteralign
216
217\aliased\let\checkcharacteralign\gobbleoneargument
218
219\permanent\protected\def\setfirstpasscharacteralign {\enforced\let\checkcharacteralign\gobbleoneargument}
220\permanent\protected\def\setsecondpasscharacteralign{\enforced\let\checkcharacteralign\firstofoneargument}
221
222%D We need fonts to provide tabular digits that is, the digits need to have the same
223%D width.
224%D
225%D \startbuffer
226%D \startbuffer[demo]
227%D     \switchtobodyfont[pagella]
228%D     \setupTABLE[column][1][alignmentcharacter=.,aligncharacter=yes]
229%D     \bTABLE
230%D         \bTR \bTD  11.111 \eTD \bTD  11.111 \eTD \eTR
231%D         \bTR \bTD   2.2   \eTD \bTD   2.2   \eTD \eTR
232%D         \bTR \bTD 444.444 \eTD \bTD 444.444 \eTD \eTR
233%D     \eTABLE
234%D \stopbuffer
235%D
236%D \start inlinenumbers:  \crlf \addfeature[inlinenumbers]   \getbuffer \stop
237%D \start tabularnumbers: \crlf \addfeature[tabularnumbers]  \getbuffer \stop
238%D \start oldstylenumbers:\crlf \addfeature[oldstylenumbers] \getbuffer \stop
239%D \stopbuffer
240%D
241%D \enabledirectives[typesetters.characteralign.autofont]
242%D \typebuffer \blank \getbuffer \blank
243%D \disabledirectives[typesetters.characteralign.autofont]
244%D \typebuffer \blank \getbuffer \blank
245%D \enabledirectives[typesetters.characteralign.autofont]
246
247\definefontfeature
248  [system:tabnum]
249  [tnum=yes,
250   lnum=no]
251
252\newconditional\c_tabl_ntb_char_align_auto_font \c_tabl_ntb_char_align_auto_font\conditionaltrue
253
254\installtexdirective % yes or no ?
255  {typesetters.characteralign.autofont}
256  {\c_tabl_ntb_char_align_auto_font\conditionaltrue}
257  {\c_tabl_ntb_char_align_auto_font\conditionalfalse}
258
259\def\m_font_feature_auto_tabnum{system:tabnum}
260
261\protected\def\typo_charalign_adapt_font_indeed
262  {\let\m_font_feature_asked\m_font_feature_auto_tabnum
263   \font_feature_reset_add_indeed}
264
265\def\typo_charalign_adapt_font % slow but seldom used (expanded in preamble)
266  {\ifconditional\c_tabl_ntb_char_align_auto_font
267     \typo_charalign_adapt_font_indeed
268   \fi}
269
270%D Another example:
271%D
272%D \starttyping
273%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={flushleft}]
274%D \bTABLE
275%D \bTR \bTD 1 \eTD \bTD   125     cm                       \eTD \eTR
276%D \bTR \bTD 2 \eTD \bTD 1 125,80  cm                       \eTD \eTR
277%D \bTR \bTD 6 \eTD \bTD 1 125,80  $\pi^2$                  \eTD \eTR
278%D \bTR \bTD 7 \eTD \bTD   129,3   \unit{square centimeter} \eTD \eTR
279%D \eTABLE
280%D
281%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={flushleft}]
282%D \bTABLE
283%D \bTR \bTD 1 \eTD \bTD   125     cm                       \eTD \eTR
284%D \bTR \bTD 2 \eTD \bTD 1 125,80  cm                       \eTD \eTR
285%D \bTR \bTD 6 \eTD \bTD 1 125,80  $\pi^2$                  \eTD \eTR
286%D \bTR \bTD 7 \eTD \bTD   129,3   \unit{square centimeter} \eTD \eTR
287%D \eTABLE
288%D
289%D \setupTABLE[c][2][alignmentcharacter={number->,},aligncharacter=yes,align={middle}]
290%D \bTABLE
291%D \bTR \bTD 1 \eTD \bTD   125    \eTD \eTR
292%D \bTR \bTD 6 \eTD \bTD 1 125,80 \eTD \eTR
293%D \bTR \bTD 7 \eTD \bTD   129,3  \eTD \eTR
294%D \eTABLE
295%D
296%D \setupTABLE[c][2][alignmentcharacter={text->,},aligncharacter=yes,align={middle}]
297%D \bTABLE
298%D \bTR \bTD 1 \eTD \bTD   125    \eTD \eTR
299%D \bTR \bTD 6 \eTD \bTD 1 125,80 \eTD \eTR
300%D \bTR \bTD 7 \eTD \bTD   129,3  \eTD \eTR
301%D \eTABLE
302%D \stoptyping
303
304\protect \endinput
305