font-emp.mklx /size: 11 Kb    last modification: 2025-02-21 11:03
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  {\dontleavehmode
159  %\relax
160   \ifdim\slantperpoint>\zeropoint
161     \c_font_emphasis_needed\conditionaltrue
162   \else
163     \c_font_emphasis_needed\conditionalfalse
164   \fi
165   \font_emphasis_set_emphasis_boldface % new
166   \ifx\fontalternative\s!it
167     \enforced\permanent\protected\def\emphasistypeface{\it}\tf
168   \orelse\ifx\fontalternative\s!sl
169     \enforced\permanent\protected\def\emphasistypeface{\sl}\tf
170   \orelse\ifx\fontalternative\s!bf
171     \emphasisboldface
172   \orelse\ifx\fontalternative\s!bs
173     \enforced\permanent\protected\def\emphasisboldface{\bs}\bf
174   \orelse\ifx\fontalternative\s!bi
175     \enforced\permanent\protected\def\emphasisboldface{\bi}\bf
176   \else
177     \emphasistypeface
178   \fi
179   \ifconditional\c_font_emphasis_needed\else
180     \expandafter\aftergroup
181   \fi
182   \emphasiscorrection}
183
184%D The next feature was not present in previous versions. It takes care of \type
185%D {\em \bf ...} situations.
186
187\let\font_emphasis_saved_emphasis_boldface\relax
188
189\def\font_emphasis_set_emphasis_boldface
190  {\let\font_emphasis_saved_emphasis_boldface\bf
191   \let\font_emphasis_set_emphasis_boldface\relax
192   \enforced\let\bf\font_emphasis_bold_bold}
193
194\protected\def\font_emphasis_bold_bold
195  {%\relax
196   \enforced\let\bf\relax % new
197   \ifx\fontalternative\s!it
198     \bi
199   \orelse\ifx\fontalternative\s!sl
200     \bs
201   \else
202     \font_emphasis_saved_emphasis_boldface
203   \fi
204   \enforced\let\bf\font_emphasis_saved_emphasis_boldface}
205
206%D Donald's (adapted) macros take the next character into account when placing
207%D italic correction. As a bonus we also look for something that looks like a dash,
208%D in which case we don't correct.
209
210\aliased\let\normalitaliccorrection\/ % the tex primitive
211
212\let\italiccorrection\normalitaliccorrection % will be overloaded
213
214\permanent\protected\def\emphasiscorrection % public, not in raw alignment groups, else omit problem
215  {\ifhmode\ifnum\currentgrouptype=\aligngroupcode\else
216     \expandafter\expandafter\expandafter\font_emphasis_look
217   \fi\fi}
218
219\def\font_emphasis_look
220  {\begingroup
221   \futurelet\nexttoken\font_emphasis_test}
222
223\def\font_emphasis_test
224  {\ifcat\noexpand\nexttoken,% we can do better in luatex ... some day ...
225     \expandafter\font_emphasis_a
226   \else
227     \expandafter\font_emphasis_b
228   \fi}
229
230\def\font_emphasis_a
231  {\futurelet\nexttoken\font_emphasis_correction}
232
233\def\font_emphasis_correction
234   {\setbox\scratchbox\hbox{\nexttoken}% tricky as next can be a macro
235    \ifzeropt\ht\scratchbox % probably a space
236      \expandafter\font_emphasis_b
237    \orelse\ifdim\ht\scratchbox<.3\exheight
238      \expandafter\expandafter\expandafter\endgroup
239    \else
240      \expandafter\expandafter\expandafter\font_emphasis_b
241    \fi}
242
243\def\font_emphasis_b
244  {\scratchskip\lastskip
245   \ifzeropt\scratchskip
246     \italiccorrection\relax
247   \else
248     \unskip\italiccorrection\hskip\scratchskip
249   \fi
250   \endgroup}
251
252%D We end with some examples which show the behavior when some punctuation is met.
253%D We also show how the mechanism adapts itself to bold, italic and slanted typing.
254%D
255%D \startbuffer
256%D test {test}test       \par
257%D test {\sl test}test   \par
258%D test {\em test}test   \par
259%D test {\em test}--test \par
260%D
261%D test {test}, test     \par
262%D test {\em test}, test \par
263%D
264%D test {\em test {\em test {\em test} test} test} test \par
265%D test {\bf test {\em test {\em test} test} test} test \par
266%D test {\sl test {\em test {\em test} test} test} test \par
267%D test {\it test {\em test {\em test} test} test} test \par
268%D \stopbuffer
269%D
270%D \typebuffer
271%D
272%D We get:
273%D
274%D \startpacked
275%D \getbuffer
276%D \stoppacked
277
278%D \macros
279%D   {emphbf,emphit,emphsl,emphtf}
280%D
281%D The next emphasis alternatives are for \THANH. They adapt their style as good as
282%D possible. These macros are obsolete in \MKIV.
283
284% \protected\def\emphbf{\groupedcommand{\bf\def\emphit{\bi}\def\emphsl{\bs}}{}}
285% \protected\def\emphit{\groupedcommand{\it\def\emphbf{\bi}\def\emphsl{\sl}}{}}
286% \protected\def\emphsl{\groupedcommand{\sl\def\emphbf{\bs}\def\emphit{\it}}{}}
287% \protected\def\emphtf{\groupedcommand{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}{}}
288% \protected\def\emph  {\groupedcommand{\em}{}}
289
290
291\permanent\protected\def\font_tf{\tf} \permanent\protected\def\font_bf{\bf}
292\permanent\protected\def\font_sl{\sl} \permanent\protected\def\font_bs{\bs}
293\permanent\protected\def\font_it{\it} \permanent\protected\def\font_bi{\bi}
294
295\protected\def\font_emphasis_bf{\bf
296                                \enforced\aliased\let\emphit\font_bi
297                                \enforced\aliased\let\emphsl\font_bs}
298\protected\def\font_emphasis_it{\it
299                                \enforced\aliased\let\emphbf\font_bi
300                                \enforced\aliased\let\emphsl\font_sl}
301\protected\def\font_emphasis_sl{\sl
302                                \enforced\aliased\let\emphbf\font_bs
303                                \enforced\aliased\let\emphit\font_it}
304\protected\def\font_emphasis_tf{\tf
305                                \enforced\aliased\let\emphbf\font_bf
306                                \enforced\aliased\let\emphit\font_it
307                                \enforced\aliased\let\emphsl\font_sl}
308
309\permanent\protected\def\emphbf{\dontleavehmode\triggergroupedcommandcs\font_emphasis_bf}
310\permanent\protected\def\emphit{\dontleavehmode\triggergroupedcommandcs\font_emphasis_it}
311\permanent\protected\def\emphsl{\dontleavehmode\triggergroupedcommandcs\font_emphasis_sl}
312\permanent\protected\def\emphtf{\dontleavehmode\triggergroupedcommandcs\font_emphasis_tf}
313\permanent\protected\def\emph  {\dontleavehmode\triggergroupedcommandcs\em}
314
315\permanent\protected\def\emphasized{\dontleavehmode\bgroup\em\let\nexttoken}
316
317% maybe some day:
318%
319% \definehighlight[\v!emphasize][\c!style=\em]
320
321%D \startbuffer
322%D TEXT {\emphbf text \emphit text  \emphtf text  \emphsl text}  TEXT
323%D TEXT  \emphbf{text \emphit{text} \emphtf{text} \emphsl{text}} TEXT
324%D \stopbuffer
325%D
326%D \typebuffer
327%D \startlines
328%D \getbuffer
329%D \stoplines
330
331\protect \endinput
332