font-emp.mklx /size: 11 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=font-emp,
3%D        version=20120106, % (moved from font-ini)
4%D          title=\CONTEXT\ Font Macros,
5%D       subtitle=Emphasis,
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 Font Macros / Emphasis}
15
16\unprotect
17
18%D \macros
19%D  {em,emphasistypeface,emphasisboldface}
20%D
21%D The next macro started as a copy of Donald Arseneau's \type {\em} (\TUGNEWS\
22%D Vol.~3, no.~1, 1994). His implementation was a bit more sophisticated version of
23%D the standard \LATEX\ one. We further enhanced the macro, so now it also adapts
24%D itself to boldface mode. Because we favor {\sl slanted} type over {\it italic},
25%D we made the emphasis adaptable, for instance:
26%D
27%D \starttyping
28%D \def\emphasistypeface {\it}
29%D \def\emphasisboldface {\bi}
30%D \stoptyping
31%D
32%D But we prefer:
33%D
34%D \starttyping
35%D \protected\def\emphasistypeface{\sl}
36%D \protected\def\emphasisboldface{\bs}
37%D \stoptyping
38%D
39%D or even better:
40
41\lettonothing\m_font_emphasized_typeface
42
43\def\font_emphasis_checked_typeface#slanted#italic%
44  {\edef\m_font_emphasized_typeface{\font_bodyfontvariable\s!em}%
45   \ifx\m_font_emphasized_typeface\v!slanted
46     #slanted%
47   \orelse\ifx\m_font_emphasized_typeface\v!italic
48     #italic%
49   \orelse\ifempty\m_font_emphasized_typeface
50     #slanted%
51   \else
52     \csname\m_font_emphasized_typeface\endcsname
53   \fi}
54
55\permanent\protected\def\emphasistypeface{\font_emphasis_checked_typeface\sl\it}
56\permanent\protected\def\emphasisboldface{\font_emphasis_checked_typeface\bs\bi}
57
58\permanent\protected\def\normalboldface % public, upgraded from mkvi / WS
59  {\relax
60   \ifx\fontalternative\s!tf\bf\orelse
61   \ifx\fontalternative\s!it\bi\orelse
62   \ifx\fontalternative\s!sl\bs\orelse
63   \ifx\fontalternative\s!bf   \orelse
64   \ifx\fontalternative\s!bi   \orelse
65   \ifx\fontalternative\s!bs   \else
66                            \bf\fi}
67
68\permanent\protected\def\normalitalicface % public, upgraded from mkvi
69  {\relax
70   \ifx\fontalternative\s!tf\it\orelse
71   \ifx\fontalternative\s!it   \orelse
72   \ifx\fontalternative\s!sl\it\orelse
73   \ifx\fontalternative\s!bf\bi\orelse
74   \ifx\fontalternative\s!bi\bi\orelse
75   \ifx\fontalternative\s!bs\bi\else
76                            \it\fi}
77
78\permanent\protected\def\normalslantedface % public, upgraded from mkvi
79  {\relax
80   \ifx\fontalternative\s!tf\sl\orelse
81   \ifx\fontalternative\s!it\sl\orelse
82   \ifx\fontalternative\s!sl   \orelse
83   \ifx\fontalternative\s!bf\bs\orelse
84   \ifx\fontalternative\s!bi\bs\orelse
85   \ifx\fontalternative\s!bs\bs\else
86                            \sl\fi}
87
88\permanent\protected\def\normaltypeface % public, upgraded from mkvi
89  {\relax
90   \ifx\fontalternative\s!tf   \orelse
91   \ifx\fontalternative\s!it\tf\orelse
92   \ifx\fontalternative\s!sl\tf\orelse
93   \ifx\fontalternative\s!bf   \orelse
94   \ifx\fontalternative\s!bi\bf\orelse
95   \ifx\fontalternative\s!bs\bf\else
96                            \tf\fi}
97
98\permanent\protected\def\swaptypeface % public
99  {\relax
100   \ifx\fontalternative\s!it \tf               \orelse
101   \ifx\fontalternative\s!sl \tf               \orelse
102   \ifx\fontalternative\s!bf \emphasisboldface \orelse
103   \ifx\fontalternative\s!bs \bf               \orelse
104   \ifx\fontalternative\s!bi \bf               \else
105                             \emphasistypeface \fi}
106
107\aliased\let\typeface   \normaltypeface    % public
108\aliased\let\boldface   \normalboldface    % public
109\aliased\let\slantedface\normalslantedface % public
110\aliased\let\italicface \normalitalicface  % public
111\aliased\let\swapface   \swaptypeface      % public
112
113%D \startbuffer[demo]
114%D \starttabulate[|l|c|c|c|c|c|c|c|]
115%D \NC                   \NC \tex{tf}             \NC \tex{it}             \NC \tex{sl}             \NC \tex{bf}
116%D                       \NC \tex{bi}             \NC \tex{bs}             \NC \tex{sc}             \NC \NR
117%D \NC                   \NC \tf              txt \NC \it              txt \NC \sl              txt \NC \bf              txt
118%D                       \NC \bi              txt \NC \bs              txt \NC \sc              txt \NC \NR
119%D \NC \tex{typeface}    \NC \tf \typeface    txt \NC \it \typeface    txt \NC \sl \typeface    txt \NC \bf \typeface    txt
120%D                       \NC \bi \typeface    txt \NC \bs \typeface    txt \NC \sc \typeface    txt \NC \NR
121%D \NC \tex{boldface}    \NC \tf \boldface    txt \NC \it \boldface    txt \NC \sl \boldface    txt \NC \bf \boldface    txt
122%D                       \NC \bi \boldface    txt \NC \bs \boldface    txt \NC \sc \boldface    txt \NC \NR
123%D \NC \tex{slantedface} \NC \tf \slantedface txt \NC \it \slantedface txt \NC \sl \slantedface txt \NC \bf \slantedface txt
124%D                       \NC \bi \slantedface txt \NC \bs \slantedface txt \NC \sc \slantedface txt \NC \NR
125%D \NC \tex{italicface}  \NC \tf \italicface  txt \NC \it \italicface  txt \NC \sl \italicface  txt \NC \bf \italicface  txt
126%D                       \NC \bi \italicface  txt \NC \bs \italicface  txt \NC \sc \italicface  txt \NC \NR
127%D \NC \tex{swapface}    \NC \tf \swapface    txt \NC \it \swapface    txt \NC \sl \swapface    txt \NC \bf \swapface    txt
128%D                       \NC \bi \swapface    txt \NC \bs \swapface    txt \NC \sc \swapface    txt \NC \NR
129%D \stoptabulate
130%D \stopbuffer
131%D
132%D {\small \typebuffer[demo]}
133%D
134%D This gives:
135%D
136%D \getbuffer[demo]
137%D
138%D But with:
139%D
140%D \startbuffer
141%D \setupbodyfontenvironment [default] [em=blue] % affects \swapface
142%D \stopbuffer
143%D
144%D \typebuffer
145%D
146%D We get:
147%D
148%D {\getbuffer\getbuffer[demo]}
149
150%D To be set with the default body font environment: \type {em} being \type
151%D {slanted} or \type {italic}.
152
153% maybe a \csname...\fontalternative\endcsname
154
155\newconditional\c_font_emphasis_needed
156
157\permanent\protected\def\em
158  {\relax
159   \ifdim\slantperpoint>\zeropoint
160     \c_font_emphasis_needed\conditionaltrue
161   \else
162     \c_font_emphasis_needed\conditionalfalse
163   \fi
164   \font_emphasis_set_emphasis_boldface % new
165   \ifx\fontalternative\s!it
166     \enforced\permanent\protected\def\emphasistypeface{\it}\tf
167   \orelse\ifx\fontalternative\s!sl
168     \enforced\permanent\protected\def\emphasistypeface{\sl}\tf
169   \orelse\ifx\fontalternative\s!bf
170     \emphasisboldface
171   \orelse\ifx\fontalternative\s!bs
172     \enforced\permanent\protected\def\emphasisboldface{\bs}\bf
173   \orelse\ifx\fontalternative\s!bi
174     \enforced\permanent\protected\def\emphasisboldface{\bi}\bf
175   \else
176     \emphasistypeface
177   \fi
178   \ifconditional\c_font_emphasis_needed\else
179     \expandafter\aftergroup
180   \fi
181   \emphasiscorrection}
182
183%D The next feature was not present in previous versions. It takes care of \type
184%D {\em \bf ...} situations.
185
186\let\font_emphasis_saved_emphasis_boldface\relax
187
188\def\font_emphasis_set_emphasis_boldface
189  {\let\font_emphasis_saved_emphasis_boldface\bf
190   \let\font_emphasis_set_emphasis_boldface\relax
191   \enforced\let\bf\font_emphasis_bold_bold}
192
193\protected\def\font_emphasis_bold_bold
194  {%\relax
195   \enforced\let\bf\relax % new
196   \ifx\fontalternative\s!it
197     \bi
198   \orelse\ifx\fontalternative\s!sl
199     \bs
200   \else
201     \font_emphasis_saved_emphasis_boldface
202   \fi
203   \enforced\let\bf\font_emphasis_saved_emphasis_boldface}
204
205%D Donald's (adapted) macros take the next character into account when placing
206%D italic correction. As a bonus we also look for something that looks like a dash,
207%D in which case we don't correct.
208
209\aliased\let\normalitaliccorrection\/ % the tex primitive
210
211\let\italiccorrection\normalitaliccorrection % will be overloaded
212
213\permanent\protected\def\emphasiscorrection % public, not in raw alignment groups, else omit problem
214  {\ifhmode\ifnum\currentgrouptype=\aligngroupcode\else
215     \expandafter\expandafter\expandafter\font_emphasis_look
216   \fi\fi}
217
218\def\font_emphasis_look
219  {\begingroup
220   \futurelet\nexttoken\font_emphasis_test}
221
222\def\font_emphasis_test
223  {\ifcat\noexpand\nexttoken,% we can do better in luatex ... some day ...
224     \expandafter\font_emphasis_a
225   \else
226     \expandafter\font_emphasis_b
227   \fi}
228
229\def\font_emphasis_a
230  {\futurelet\nexttoken\font_emphasis_correction}
231
232\def\font_emphasis_correction
233   {\setbox\scratchbox\hbox{\nexttoken}% tricky as next can be a macro
234    \ifzeropt\ht\scratchbox % probably a space
235      \expandafter\font_emphasis_b
236    \orelse\ifdim\ht\scratchbox<.3\exheight
237      \expandafter\expandafter\expandafter\endgroup
238    \else
239      \expandafter\expandafter\expandafter\font_emphasis_b
240    \fi}
241
242\def\font_emphasis_b
243  {\scratchskip\lastskip
244   \ifzeropt\scratchskip
245     \italiccorrection\relax
246   \else
247     \unskip\italiccorrection\hskip\scratchskip
248   \fi
249   \endgroup}
250
251%D We end with some examples which show the behavior when some punctuation is met.
252%D We also show how the mechanism adapts itself to bold, italic and slanted typing.
253%D
254%D \startbuffer
255%D test {test}test       \par
256%D test {\sl test}test   \par
257%D test {\em test}test   \par
258%D test {\em test}--test \par
259%D
260%D test {test}, test     \par
261%D test {\em test}, test \par
262%D
263%D test {\em test {\em test {\em test} test} test} test \par
264%D test {\bf test {\em test {\em test} test} test} test \par
265%D test {\sl test {\em test {\em test} test} test} test \par
266%D test {\it test {\em test {\em test} test} test} test \par
267%D \stopbuffer
268%D
269%D \typebuffer
270%D
271%D We get:
272%D
273%D \startpacked
274%D \getbuffer
275%D \stoppacked
276
277%D \macros
278%D   {emphbf,emphit,emphsl,emphtf}
279%D
280%D The next emphasis alternatives are for \THANH. They adapt their style as good as
281%D possible. These macros are obsolete in \MKIV.
282
283% \protected\def\emphbf{\groupedcommand{\bf\def\emphit{\bi}\def\emphsl{\bs}}{}}
284% \protected\def\emphit{\groupedcommand{\it\def\emphbf{\bi}\def\emphsl{\sl}}{}}
285% \protected\def\emphsl{\groupedcommand{\sl\def\emphbf{\bs}\def\emphit{\it}}{}}
286% \protected\def\emphtf{\groupedcommand{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}{}}
287% \protected\def\emph  {\groupedcommand{\em}{}}
288
289
290\permanent\protected\def\font_tf{\tf} \permanent\protected\def\font_bf{\bf}
291\permanent\protected\def\font_sl{\sl} \permanent\protected\def\font_bs{\bs}
292\permanent\protected\def\font_it{\it} \permanent\protected\def\font_bi{\bi}
293
294\protected\def\font_emphasis_bf{\bf\enforced\aliased\let\emphit\font_bi
295                                   \enforced\aliased\let\emphsl\font_bs}
296\protected\def\font_emphasis_it{\it\enforced\aliased\let\emphbf\font_bi
297                                   \enforced\aliased\let\emphsl\font_sl}
298\protected\def\font_emphasis_sl{\sl\enforced\aliased\let\emphbf\font_bs
299                                   \enforced\aliased\let\emphit\font_it}
300\protected\def\font_emphasis_tf{\tf\enforced\aliased\let\emphbf\font_bf
301                                   \enforced\aliased\let\emphit\font_it
302                                   \enforced\aliased\let\emphsl\font_sl}
303
304\permanent\protected\def\emphbf{\triggergroupedcommandcs\font_emphasis_bf}
305\permanent\protected\def\emphit{\triggergroupedcommandcs\font_emphasis_it}
306\permanent\protected\def\emphsl{\triggergroupedcommandcs\font_emphasis_sl}
307\permanent\protected\def\emphtf{\triggergroupedcommandcs\font_emphasis_tf}
308\permanent\protected\def\emph  {\triggergroupedcommandcs\em}
309
310\permanent\protected\def\emphasized{\bgroup\em\let\nexttoken}
311
312%D \startbuffer
313%D TEXT {\emphbf text \emphit text  \emphtf text  \emphsl text}  TEXT
314%D TEXT  \emphbf{text \emphit{text} \emphtf{text} \emphsl{text}} TEXT
315%D \stopbuffer
316%D
317%D \typebuffer
318%D \startlines
319%D \getbuffer
320%D \stoplines
321
322\protect \endinput
323