summaryrefslogtreecommitdiff
path: root/tex/context/base/typo-tal.mkiv
blob: fb3955aec4debe3cfa8f4c6075ccd469d4b0218e (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
%D \module
%D   [       file=typo-tal, % spac-cha (2012.06.08) supp-ali (2000.04.17)
%D        version=2013.10.04,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Character Alignment,
%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 / Character Alignment}

%D This module replaces the \MKII\ character alignment code which hooked into
%D table mechanisms but used parsing. In fact, this might be one of these cases
%D where a \TEX\ based solution is faster, but a \LUA\ one a bit more robust.
%D Anyway, as I had to fix something (to fit the newer table mechanisms) I
%D decided to go the mixed route, a rather easy going effort in the aftermath of
%D the 2013 \CONTEXT\ meeting.

\unprotect

\registerctxluafile{typo-tal}{1.001}

\definesystemattribute[characteralign][public]

%D This mechanism is mostly meant for tables:
%D
%D \startbuffer
%D \starttabulate[|l|g{,}|r|]
%D     \NC test \NC 1.234.456,99 \NC \NC test \NR
%D     \NC test \NC   234.456,9  \NC \NC test \NR
%D     \NC test \NC   234.456    \NC \NC test \NR
%D     \NC test \NC       456    \NC \NC test \NR
%D     \NC test \NC \bf whatever \NC \NC test \NR
%D \stoptabulate
%D \stopbuffer
%D
%D \typebuffer \blank \getbuffer \blank

%D \startbuffer
%D \bTABLE
%D     \bTR \bTD[aligncharacter=yes] €                      1,1     \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €                     11,11    \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] € 12\punctuationspace111,11    \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €                 12 111,11    \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €              1.234.451,22222 \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €                234.451,2     \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €                234.451       \eTD \eTR
%D     \bTR \bTD[aligncharacter=yes] €                    451       \eTD \eTR
%D     \bTR \bTD                     \bf some text                  \eTD \eTR
%D \eTABLE
%D \stopbuffer
%D
%D \typebuffer \blank \getbuffer \blank

\unexpanded\def\signalcharacteralign#1#2{\attribute\characteralignattribute=\numexpr#1*\plushundred+#2\relax}
\unexpanded\def\setcharacteralign   #1#2{\ctxcommand{setcharacteralign(\number#1,"#2")}}
\unexpanded\def\resetcharacteralign     {\ctxcommand{resetcharacteralign()}}

%D Mostly downward compatible:
%D
%D \startbuffer
%D \startcharacteralign
%D     \checkcharacteralign{123.456,78}
%D     \checkcharacteralign{456}
%D     \checkcharacteralign{23.456}
%D     \checkcharacteralign{78,9}
%D \stopcharacteralign
%D \stopbuffer
%D
%D \typebuffer \blank \getbuffer \blank

\def\alignmentcharacter{,}

\unexpanded\def\typo_charalign_pass_one
  {\advance\scratchcounter\plusone
   \setbox\scratchbox\typo_charalign_pass}

\unexpanded\def\typo_charalign_pass_two
  {\advance\scratchcounter\plusone
   \typo_charalign_pass}

\def\typo_charalign_pass
  {\hbox\bgroup\signalcharacteralign\plusone\scratchcounter\let\next}

\unexpanded\def\startcharacteralign#1\stopcharacteralign
  {\bgroup
   \setcharacteralign\plusone\alignmentcharacter
   \begingroup
     \scratchcounter\zerocount
     \let\checkcharacteralign\typo_charalign_pass_one
     \settrialtypesetting
     #1\relax
   \endgroup
   \begingroup
     \scratchcounter\zerocount
     \let\checkcharacteralign\typo_charalign_pass_two
     #1\relax
   \endgroup
   \resetcharacteralign
   \egroup}

\let\stopcharacteralign \relax
\let\checkcharacteralign\gobbleoneargument

\def\setfirstpasscharacteralign {\let\checkcharacteralign\gobbleoneargument}
\def\setsecondpasscharacteralign{\let\checkcharacteralign\firstofoneargument}

%D We need fonts to provide tabular digits that is, the digits need to have the same
%D width.

\definefontfeature
  [system:tabnum]
  [tnum=yes,
   lnum=no]

\newconditional\c_tabl_ntb_char_align_auto_font \settrue\c_tabl_ntb_char_align_auto_font

\installtexdirective % yes or no ?
  {typesetters.characteralign.autofont}
  {\settrue \c_tabl_ntb_char_align_auto_font}
  {\setfalse\c_tabl_ntb_char_align_auto_font}

\def\m_font_feature_auto_tabnum{system:tabnum}

\def\typo_charalign_adapt_font % slow but seldom used
  {\ifconditional\c_tabl_ntb_char_align_auto_font
     \let\m_font_feature_asked\m_font_feature_auto_tabnum
     \font_feature_reset_add_indeed
   \fi}

\protect \endinput