summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/font-emp.mklx
blob: 8ceee272318c5bb3da449c3f25e416ffc819415b (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
%D \module
%D   [       file=font-emp,
%D        version=20120106, % (moved from font-ini)
%D          title=\CONTEXT\ Font Macros,
%D       subtitle=Emphasis,
%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 Font Macros / Emphasis}

\unprotect

%D \macros
%D  {em,emphasistypeface,emphasisboldface}
%D
%D The next macro started as a copy of Donald Arseneau's \type {\em} (\TUGNEWS\
%D Vol.~3, no.~1, 1994). His implementation was a bit more sophisticated version of
%D the standard \LATEX\ one. We further enhanced the macro, so now it also adapts
%D itself to boldface mode. Because we favor {\sl slanted} type over {\it italic},
%D we made the emphasis adaptable, for instance:
%D
%D \starttyping
%D \def\emphasistypeface {\it}
%D \def\emphasisboldface {\bi}
%D \stoptyping
%D
%D But we prefer:
%D
%D \starttyping
%D \unexpanded\def\emphasistypeface{\sl}
%D \unexpanded\def\emphasisboldface{\bs}
%D \stoptyping
%D
%D or even better:

\let\m_font_emphasized_typeface\empty

\def\font_emphasis_checked_typeface#slanted#italic%
  {\edef\m_font_emphasized_typeface{\font_bodyfontvariable\s!em}%
   \ifx\m_font_emphasized_typeface\v!slanted
     #slanted%
   \orelse\ifx\m_font_emphasized_typeface\v!italic
     #italic%
   \orelse\ifempty\m_font_emphasized_typeface
     #slanted%
   \else
     \csname\m_font_emphasized_typeface\endcsname
   \fi}

\unexpanded\def\emphasistypeface{\font_emphasis_checked_typeface\sl\it}
\unexpanded\def\emphasisboldface{\font_emphasis_checked_typeface\bs\bi}

\unexpanded\def\normalboldface % public
  {\relax
   \ifx\fontalternative\s!it\bi\orelse
   \ifx\fontalternative\s!sl\bs\else
                            \bf\fi}

\unexpanded\def\normalitalicface % public
  {\relax
   \ifx\fontalternative\s!tf\it\orelse
   \ifx\fontalternative\s!bf\bi\else
                            \tf\fi}

\unexpanded\def\normalslantedface % public
  {\relax \ifx\fontalternative\s!tf\sl
   \orelse\ifx\fontalternative\s!bf\bs
   \else                           \tf\fi}

\unexpanded\def\normaltypeface % public
  {\relax
   \ifx\fontalternative\s!bi \it \orelse
   \ifx\fontalternative\s!bs \sl \else
                             \tf \fi}

\unexpanded\def\swaptypeface % public
  {\relax
   \ifx\fontalternative\s!it \tf               \orelse
   \ifx\fontalternative\s!sl \tf               \orelse
   \ifx\fontalternative\s!bf \emphasisboldface \orelse
   \ifx\fontalternative\s!bs \bf               \orelse
   \ifx\fontalternative\s!bi \bf               \else
                             \emphasistypeface \fi}

\let\typeface   \normaltypeface    % public
\let\boldface   \normalboldface    % public
\let\slantedface\normalslantedface % public
\let\italicface \normalitalicface  % public
\let\swapface   \swaptypeface      % public

%D To be set with the default body font environment: \type
%D {em} being \type {slanted} or \type {italic}.

% maybe a \csname...\fontalternative\endcsname

\newconditional\c_font_emphasis_needed

\unexpanded\def\em
  {\relax
   \ifdim\slantperpoint>\zeropoint
     \settrue\c_font_emphasis_needed
   \else
     \setfalse\c_font_emphasis_needed
   \fi
   \font_emphasis_set_emphasis_boldface % new
   \ifx\fontalternative\s!it
     \unexpanded\def\emphasistypeface{\it}\tf
   \orelse\ifx\fontalternative\s!sl
     \unexpanded\def\emphasistypeface{\sl}\tf
   \orelse\ifx\fontalternative\s!bf
     \emphasisboldface
   \orelse\ifx\fontalternative\s!bs
     \unexpanded\def\emphasisboldface{\bs}\bf
   \orelse\ifx\fontalternative\s!bi
     \unexpanded\def\emphasisboldface{\bi}\bf
   \else
     \emphasistypeface
   \fi
   \ifconditional\c_font_emphasis_needed\else
     \expandafter\aftergroup
   \fi
   \emphasiscorrection}

%D The next feature was not present in previous versions. It
%D takes care of \type {\em \bf ...} situations.

\let\font_emphasis_saved_emphasis_boldface\relax

\def\font_emphasis_set_emphasis_boldface
  {\let\font_emphasis_saved_emphasis_boldface\bf
   \let\font_emphasis_set_emphasis_boldface\relax
   \let\bf\font_emphasis_bold_bold}

\unexpanded\def\font_emphasis_bold_bold
  {%\relax
   \let\bf\relax % new
   \ifx\fontalternative\s!it
     \bi
   \orelse\ifx\fontalternative\s!sl
     \bs
   \else
     \font_emphasis_saved_emphasis_boldface
   \fi
   \let\bf\font_emphasis_saved_emphasis_boldface}

%D Donald's (adapted) macros take the next character into account when placing
%D italic correction. As a bonus we also look for something that looks like a dash,
%D in which case we don't correct.

\let\italiccorrection=\/ % tex primitive

\def\emphasiscorrection % public, not in raw alignment groups, else omit problem
  {\ifhmode\ifnum\currentgrouptype=\aligngroupcode\else
     \expandafter\expandafter\expandafter\font_emphasis_look
   \fi\fi}

\def\font_emphasis_look
  {\begingroup
   \futurelet\nexttoken\font_emphasis_test}

\def\font_emphasis_test
  {\ifcat\noexpand\nexttoken,% we can do better in luatex ... some day ...
     \expandafter\font_emphasis_a
   \else
     \expandafter\font_emphasis_b
   \fi}

\def\font_emphasis_a
  {\futurelet\nexttoken\font_emphasis_correction}

\def\font_emphasis_correction
   {\setbox\scratchbox\hbox{\nexttoken}% tricky as next can be a macro
    \ifzeropt\ht\scratchbox % probably a space
      \expandafter\font_emphasis_b
    \orelse\ifdim\ht\scratchbox<.3\exheight
      \expandafter\expandafter\expandafter\endgroup
    \else
      \expandafter\expandafter\expandafter\font_emphasis_b
    \fi}

\def\font_emphasis_b
  {\scratchskip\lastskip
   \ifzeropt\scratchskip
     \italiccorrection\relax
   \else
     \unskip\italiccorrection\hskip\scratchskip
   \fi
   \endgroup}

%D We end with some examples which show the behavior when some punctuation is met.
%D We also show how the mechanism adapts itself to bold, italic and slanted typing.
%D
%D \startbuffer
%D test {test}test       \par
%D test {\sl test}test   \par
%D test {\em test}test   \par
%D test {\em test}--test \par
%D
%D test {test}, test     \par
%D test {\em test}, test \par
%D
%D test {\em test {\em test {\em test} test} test} test \par
%D test {\bf test {\em test {\em test} test} test} test \par
%D test {\sl test {\em test {\em test} test} test} test \par
%D test {\it test {\em test {\em test} test} test} test \par
%D \stopbuffer
%D
%D \typebuffer
%D
%D We get:
%D
%D \startpacked
%D \getbuffer
%D \stoppacked

%D \macros
%D   {emphbf,emphit,emphsl,emphtf}
%D
%D The next emphasis alternatives are for \THANH. They adapt their style as good as
%D possible. These macros are obsolete in \MKIV.

% \unexpanded\def\emphbf{\groupedcommand{\bf\def\emphit{\bi}\def\emphsl{\bs}}{}}
% \unexpanded\def\emphit{\groupedcommand{\it\def\emphbf{\bi}\def\emphsl{\sl}}{}}
% \unexpanded\def\emphsl{\groupedcommand{\sl\def\emphbf{\bs}\def\emphit{\it}}{}}
% \unexpanded\def\emphtf{\groupedcommand{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}{}}
% \unexpanded\def\emph  {\groupedcommand{\em}{}}

\unexpanded\def\font_emphasis_bf{\bf\def\emphit{\bi}\def\emphsl{\bs}}
\unexpanded\def\font_emphasis_it{\it\def\emphbf{\bi}\def\emphsl{\sl}}
\unexpanded\def\font_emphasis_sl{\sl\def\emphbf{\bs}\def\emphit{\it}}
\unexpanded\def\font_emphasis_tf{\tf\def\emphbf{\bf}\def\emphit{\it}\def\emphsl{\sl}}

\unexpanded\def\emphbf{\triggergroupedcommandcs\font_emphasis_bf}
\unexpanded\def\emphit{\triggergroupedcommandcs\font_emphasis_it}
\unexpanded\def\emphsl{\triggergroupedcommandcs\font_emphasis_sl}
\unexpanded\def\emphtf{\triggergroupedcommandcs\font_emphasis_tf}
\unexpanded\def\emph  {\triggergroupedcommandcs\em}

\unexpanded\def\emphasized{\bgroup\em\let\nexttoken}

%D \startbuffer
%D TEXT {\emphbf text \emphit text  \emphtf text  \emphsl text}  TEXT
%D TEXT  \emphbf{text \emphit{text} \emphtf{text} \emphsl{text}} TEXT
%D \stopbuffer
%D
%D \typebuffer
%D \startlines
%D \getbuffer
%D \stoplines

\protect \endinput