summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/typo-scr.mkxl
blob: 74abe5a45bab34eac1bb2b7f9ce45c58e1734f73 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
%D \module
%D   [       file=typo-scr,
%D        version=2012.01.23, % very old ... 1995.10.10 .. moved from core-mis
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Scripts,
%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 / Scripts}

\unprotect

%D I addition to the commands implemented here, we have alternative commands
%D \type {shiftup} and \type {shiftdown} that can work across paragraphs.

%D \macros
%D   {low, high, definelow, definehigh, setuplow, setuphigh}
%D
%D Although \TEX\ is pretty well aware of super- and subscripts, its mechanism
%D is mainly tuned for math mode. The next few commands take care of script
%D texts both modes.
%D
%D \startbuffer
%D The higher\high{one goes} the lower\low{one drops}, or\lohi{yes}{no}?
%D \stopbuffer
%D
%D \typebuffer
%D \getbuffer
%D
%D Note the different placement of \type {\lohi}, where we need a bit more
%D space. The implementation looks a bit fuzzy, since some \type {\fontdimen}'s
%D are involved to determine the optimal placement.

\installcorenamespace {low}
\installcorenamespace {high}

\installcommandhandler \??low  {low}  \??low
\installcommandhandler \??high {high} \??high

\setuplow [\c!style=\tx,\c!distance=\zeropoint,\c!down=.48\exheight] % historical
\setuphigh[\c!style=\tx,\c!distance=\zeropoint,\c!up  =.86\exheight] % values

\appendtoks \frozen\protected\edefcsname\currentlow \endcsname{\typo_scripts_low [\currentlow ]}\to \everydefinelow
\appendtoks \frozen\protected\edefcsname\currenthigh\endcsname{\typo_scripts_high[\currenthigh]}\to \everydefinehigh

\def\typo_scripts_distance#1%
  {\edef\p_distance{#1\c!distance}%
   \ifx\p_distance\v!tight
     \tightfitboundary
   \else
     \kern\p_distance\relax
   \fi}

%D \startbuffer
%D \setuplow [distance=\zeropoint]
%D \setuphigh[distance=\zeropoint]
%D
%D {\showglyphs f\high{x}\quad p\high{x}\quad}
%D
%D \setuplow [\c!distance=\v!tight]
%D \setuphigh[\c!distance=\v!tight]
%D
%D {\showglyphs f\high{x}\quad p\high{x}\quad}
%D \stopbuffer
%D
%D \typebuffer {\getbuffer}

% \type{f\kern\tightfitcompensation\high{x}}
% \type{f\tightfitboundary         \high{x}}

\protected\def\typo_scripts_low[#1]#2%
  {\dontleavehmode
   \begingroup
   \cdef\currentlow{#1}%
   \typo_scripts_distance\lowparameter
   \setbox\scratchbox\runninghbox\bgroup
     \lower\lowparameter\c!down\hbox\bgroup
       \ifempty\fontsize
         \ifmmode
           \mr % no color yet
         \else
           \uselowstyleandcolor\c!style\c!color
         \fi
       \else
         \uselowstyleandcolor\c!style\c!color
       \fi
       \dostarttagged\t!sub\currentlow
       #2%
       \dostoptagged
     \egroup
   \egroup
   \ht\scratchbox\strutht
   \dp\scratchbox\strutdp
   \box\scratchbox
   \endgroup}

\protected\def\typo_scripts_high[#1]#2%
  {\dontleavehmode
   \begingroup
   \cdef\currenthigh{#1}%
   \typo_scripts_distance\highparameter
   \setbox\scratchbox\runninghbox\bgroup
     \raise\highparameter\c!up\hbox\bgroup
       \ifempty\fontsize
         \ifmmode
           \mr % no color yet
         \else
           \usehighstyleandcolor\c!style\c!color
         \fi
       \else
         \usehighstyleandcolor\c!style\c!color
       \fi
       \dostarttagged\t!sup\currenthigh
       #2%
       \dostoptagged
     \egroup
   \egroup
   \ht\scratchbox\strutht
   \dp\scratchbox\strutdp
   \box\scratchbox
   \endgroup}

\permanent\protected\def\low {\typo_scripts_low []}
\permanent\protected\def\high{\typo_scripts_high[]}

%D \macros
%D   {lohi, hilo, definelohi, setuplohi}
%D
%D You can provide an optional keyword \type {left}, in which case the super and
%D subscripts will be aligned in a way that permits placement at the left of a word
%D (which means that it will be right aligned).
%D
%D \startbuffer
%D \lohi {aha} {ah} test \lohi {aha} {ah} test
%D \lohi [left] {aha} {ah} test \lohi [left] {aha} {ah} test
%D \lohi {aha} {ah} test \lohi {aha} {ah} test
%D \lohi [left] {aha} {ah}test \lohi [left] {aha} {ah} test
%D \stopbuffer
%D
%D \typebuffer
%D \getbuffer

\installcorenamespace {lowhigh}

\installcommandhandler \??lowhigh {lowhigh} \??lowhigh

\setuplowhigh
  [\c!style=\tx,
   \c!distance=.1\exheight,  % these are
   \c!up=.96\exheight,       % historical
   \c!down=.58\exheight]     % values

\appendtoks
    \frozen\edefcsname\currentlowhigh\endcsname{\typo_scripts_lowhigh[\currentlowhigh]}%
\to \everydefinelowhigh

\tolerant\protected\def\typo_scripts_lowhigh[#1]#*[#2]#:#3#4% todo: align .. [#1] is compatible hack
  {\dontleavehmode
   \runninghbox\bgroup
   \cdef\currentlowhigh{#1}%
   \typo_scripts_distance\lowhighparameter
   \dostarttagged\t!subsup\currentlowhigh
   \setbox\plusfour\hpack{\typo_scripts_lowhigh_low_high\lower\c!down\t!sub{#3}}%
   \setbox\plussix \hpack{\typo_scripts_lowhigh_low_high\raise\c!up  \t!sup{#4}}%
   \edef\p_align{#2}%
   \ifx\p_align\v!left
     \ifdim\wd\plusfour<\wd\plussix
       \setbox\plusfour\hpack to \wd\plussix {\hss\box\plusfour}%
     \else
       \setbox\plussix \hpack to \wd\plusfour{\hss\box\plussix }%
     \fi
   \fi
   \ifdim\wd\plusfour<\wd\plussix
     \wd\plusfour\zeropoint
     \box\plusfour
     \box\plussix
   \else
     \wd\plussix\zeropoint
     \box\plussix
     \box\plusfour
   \fi
   \dostoptagged
   \egroup}

\def\typo_scripts_lowhigh_low_high#1#2#3#4%
  {\setbox\scratchbox\hpack\bgroup
     #1\lowhighparameter#2\hbox\bgroup
       \ifempty\fontsize
         \ifmmode
           \mr % no color yet
         \else
           \uselowhighstyleandcolor\c!style\c!color
         \fi
       \else
         \uselowhighstyleandcolor\c!style\c!color
       \fi
       \dostarttagged#3\empty
       #4%
       \dostoptagged
     \egroup
   \egroup
   \ht\scratchbox\strutht
   \dp\scratchbox\strutdp
   \box\scratchbox}

\permanent\tolerant\protected\def\typo_scripts_highlow[#1]#*[#2]#:#3#4%
  {\typo_scripts_lowhigh[#1][#2]{#4}{#3}}

\permanent\protected\def\lohi{\typo_scripts_lowhigh[]}
\permanent\protected\def\hilo{\typo_scripts_highlow[]}

%D \macros
%D   {lowmidhigh, definelowmidhigh, setuplowmidhigh}
%D
%D The previous command originally didn't have definers. These were introduced when
%D the next showed up:
%D
%D \startbuffer
%D \definelow       [MyLow]   [style=\txx]
%D \definehigh      [MyHigh]  [style=\txx]
%D \definelowhigh   [MyLoHi]  [style=\txx]
%D \definelowmidhigh[MyLoMiHi][style=\txx]
%D
%D We have
%D     \ruledhbox{\low         {L}} and \ruledhbox{\MyLow         {L}} and
%D     \ruledhbox{\high        {H}} and \ruledhbox{\MyHigh        {H}} and
%D     \ruledhbox{\lohi     {L}{H}} and \ruledhbox{\MyLoHi     {L}{H}} and
%D     \ruledhbox{\lomihi{L}{M}{H}} and \ruledhbox{\MyLoMiHi{L}{M}{H}}.
%D \stopbuffer
%D
%D \typebuffer \getbuffer

\installcorenamespace {lowmidhigh}

\installcommandhandler \??lowmidhigh {lowmidhigh} \??lowmidhigh

\setuplowmidhigh
  [\c!style=\tx,
   \c!up=.8\struthtdp,
   \c!down=.8\struthtdp]

\appendtoks
    \frozen\edefcsname\currentlowmidhigh\endcsname{\typo_scripts_lowmidhigh[\currentlowmidhigh]}%
\to \everydefinelowmidhigh

\protected\def\typo_scripts_lowmidhigh[#1]#2#3#4%
  {\dontleavehmode
   \runninghbox\bgroup
     \cdef\currentlowmidhigh{#1}%
     \dostarttagged\t!subsup\currentlowmidhigh
     \uselowmidhighstyleandcolor\c!style\c!color
     \setstrut
     \setbox\plustwo \hbox{\strut\dostarttagged\t!sub\empty#2\dostoptagged}%
     \setbox\plusfour\hbox{\strut\dostarttagged\t!mid\empty#3\dostoptagged}% inefficient
     \setbox\plussix \hbox{\strut\dostarttagged\t!sup\empty#4\dostoptagged}%
     \scratchdimen \wd
        \ifdim\wd\plustwo>\wd\plusfour
          \ifdim\wd\plustwo>\wd\plussix
             \plustwo
          \else
             \plussix
          \fi
        \else
          \ifdim\wd\plusfour>\wd\plussix
             \plusfour
          \else
             \plussix
          \fi
        \fi
     \relax
     \setbox\plustwo \hpack to \scratchdimen{\hss\lower\lowmidhighparameter\c!down\box\plustwo \hss}%
     \setbox\plusfour\hpack to \scratchdimen{\hss                                 \box\plusfour\hss}%
     \setbox\plussix \hpack to \scratchdimen{\hss\raise\lowmidhighparameter\c!up  \box\plussix \hss}%
     \wd\plustwo \zeropoint
     \wd\plusfour\zeropoint
     \box\plusfour
     \box\plustwo
     \box\plussix
     \dostoptagged
   \egroup}

\permanent\protected\def\lomihi      {\typo_scripts_lowmidhigh[]}
\permanent\protected\def\himilo#1#2#3{\typo_scripts_lowmidhigh[]{#3}{#2}{#1}}

\protect \endinput