font-sty.mkvi /size: 15 Kb    last modification: 2020-07-01 14:35
1
%D \module
2
%D [ file=font-sty,
3
%D version=2011.01.13, % (copied fron font-ini)
4
%D title=\CONTEXT\ Font Macros,
5
%D subtitle=Styles,
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
/
Styles
}
15 16
\unprotect
17 18
%D \macros
19
%D {definealternativestyle}
20
%D
21
%D In the main modules we are going to implement lots of parameterized commands and
22
%D one of these parameters will concern the font to use. To suit consistent use of
23
%D fonts we here implement a mechanism for defining the keywords that present a
24
%D particular style or alternative.
25
%D
26
%D \starttyping
27
%D \definealternativestyle [keywords] [\style] [\nostyle]
28
%D \stoptyping
29
%D
30
%D The first command is used in the normal textflow, while the second command takes
31
%D care of headings and alike. Consider the next two definitions:
32
%D
33
%D \starttyping
34
%D \definealternativestyle [bold] [\bf] []
35
%D \definealternativestyle [cap] [\cap] [\cap]
36
%D \stoptyping
37
%D
38
%D A change \type {\bf} in a heading which is to be set in \type {\tfd} does not look
39
%D that well, so therefore we leave the second argument of \type
40
%D {\definealternativestyle} empty. When we capatalize characters using the pseudo
41
%D small cap command \type {\cap}, we want this to take effect in both text and
42
%D headings, which is accomplished by assigning both arguments.
43 44
\installcorenamespace
{
alternativestyles
}
% settings
45
\installcorenamespace
{
alternativestyle
}
% instances
46 47
\installsetuponlycommandhandler
\??alternativestyles
{
alternativestyles
}
48 49
\setnewconstant
\c_font_current_alternative_style_index
\plusone
50 51
\unexpanded
\def
\definealternativestyle
52
{
\dotripleempty
\font_basics_define_alternative_style
}
53 54
\def
\font_basics_define_alternative_style
[#
commands
][#
variantone
][#
varianttwo
]
%
55
{
\processcommalist
[#
commands
]
{
\font_basics_define_alternative_style_indeed
{
#
variantone
}{
#
varianttwo
}}}
56 57
\let
\definestyle\definealternativestyle
% later redefined
58 59
\newconstant
\c_fonts_basics_alternative_style_method
60 61
\def
\font_basics_define_alternative_style_indeed
#
variantone
#
varianttwo
#
command
%
62
{
\setuvalue
{
\??alternativestyle
#
command
}{
\font_helpers_apply_alternative_style
{
#
variantone
}{
#
varianttwo
}}
%
63
\ifcsname
#
command
\endcsname
64
% no redefinition
65
\else\ifnum
\c_fonts_basics_alternative_style_method
=
\plusone
66
\ifthirdargument
67
\setuevalue
{
#
command
}{
\triggergroupedcommandcs
\begincsname
\??alternativestyle
#
command
\endcsname
}
%
68
\else
69
\setuvalue
{
#
command
}{
\triggergroupedcommand
{
#
variantone
}}
%
70
\fi
71
\else
72
\setuvalue
{
#
command
}{
\triggergroupedcommand
{
#
variantone
}}
%
73
\fi\fi
}
74 75
\def
\font_helpers_apply_alternative_style
76
{
\ifcase
\c_font_current_alternative_style_index
77
\expandafter
\gobbletwoarguments
78
\or
79
\expandafter
\firstoftwoarguments
80
\or
81
\expandafter
\secondoftwoarguments
82
\else
83
\expandafter
\firstoftwoarguments
84
\fi
}
85 86
\def
\applyalternativestyle
#
name
% public
87
{
\begincsname
\??alternativestyle
#
name
\endcsname
}
88 89
\appendtoks
90
\doifelse
{
\alternativestylesparameter
\c!method
}
\v!auto
91
{
\c_fonts_basics_alternative_style_method
\plusone
}
%
92
{
\c_fonts_basics_alternative_style_method
\zerocount
}
%
93
\to
\everysetupalternativestyles
94 95
%D Maybe too generic, but probably ok is the following. (Maybe one day we will use a
96
%D dedicated grouped command for styles.)
97 98
% \appendtoks
99
% \let\groupedcommand\thirdofthreearguments
100
% \to \everysimplifycommands
101 102
%D This command also defines the keyword as command. This means that the example
103
%D definition of \type {bold} we gave before, results in a command \type {\bold}
104
%D which can be used as:
105
%D
106
%D \startbuffer
107
%D He's a \bold{bold} man with a {\bold head}.
108
%D \stopbuffer
109
%D
110
%D \typebuffer
111
%D
112
%D or
113
%D
114
%D \startexample
115
%D \definealternativestyle[bold][\bf][]\getbuffer
116
%D \stopexample
117
%D
118
%D Such definitions are of course unwanted for \type {\cap} because this would
119
%D result in an endless recursive call. Therefore we check on the existance of both
120
%D the command and the substitution. The latter is needed because for instance \type
121
%D {\type} is an entirely diferent command. That command handles verbatim, while the
122
%D style command would just switch to teletype font. This is just an example of a
123
%D tricky naming coincidence.
124
%D
125
%D \macros
126
%D {doconvertfont,noconvertfont,
127
%D dontconvertfont,redoconvertfont}
128
%D
129
%D After having defined such keywords, we can call for them by using
130
%D
131
%D \starttyping
132
%D \doconvertfont{keyword}{text}
133
%D \stoptyping
134
%D
135
%D We deliberately pass an argument. This enables us to assign converters that
136
%D handle one argument, like \type {\cap}.
137
%D
138
%D By default the first specification is used to set the style, exept when we say
139
%D \type {\dontconvertfont}, after which the second specification is used. We can
140
%D also directly call for \type {\noconvertfont}. In nested calls, we can restore
141
%D the conversion by saying \type {\redoconvertfont}.
142
%D
143
%D These commands are not grouped! Grouping is most probably done by the calling
144
%D macro's and would lead to unnecessary overhead.
145 146
\let
\m_current_convert_font
\empty
147
\let
\m_current_convert_font_dt
\empty
148 149
\unexpanded
\def
\doconvertfont
#
specification
% takes second argument / this command is obsolete
150
{
\edef
\m_current_convert_font
{
#
specification
}
%
151
\ifx
\m_current_convert_font
\empty
152
%\expandafter\firstofoneargument
153
\else
154
\expandafter
\font_helpers_do_convert_font
155
\fi
}
156 157
\def
\font_helpers_do_convert_font
158
{
\edef
\m_current_convert_font_dt
{
\detokenize\expandafter
{
\m_current_convert_font
}}
%
159
\ifcsname
\??alternativestyle
\m_current_convert_font_dt
\endcsname
160
\expandafter\lastnamedcs
161
\else\ifcsname
\m_current_convert_font_dt
\endcsname
162
\doubleexpandafter
\lastnamedcs
163
\else
164
\doubleexpandafter
\m_current_convert_font
165
\fi\fi
}
166 167
%D Low level switches (downward compatible, but we keep them as one can use them in
168
%D styles):
169
%D
170
%D \starttyping
171
%D \usemodule[abr-02]
172
%D \setuphead[chapter][style=\bfb]
173
%D \setupfooter[style=\dontconvertfont\bf]
174
%D \chapter{This is \TEX}
175
%D \stoptyping
176 177
\unexpanded
\def
\dontconvertfont
{
\c_font_current_alternative_style_index
\plustwo
}
% needs checking in usage
178
\unexpanded
\def
\redoconvertfont
{
\c_font_current_alternative_style_index
\plusone
}
% needs checking in usage
179 180
%D The new one:
181 182
\setfalse
\fontattributeisset
183 184
\unexpanded
\def
\dousestyleparameter
#
value
%
185
{
\edef
\currentstyleparameter
{
#
value
}
%
186
\ifx
\currentstyleparameter
\empty
\else
187
\expandafter
\dousecurrentstyleparameter
188
\fi
}
189 190
\unexpanded
\def
\dousestylehashparameter
#
hash
#
parameter
%
191
{
\ifcsname
#
hash
#
parameter
\endcsname
192
\expandafter
\dousestyleparameter
\lastnamedcs
193
\fi
}
194 195
\unexpanded
\def
\dousecurrentstyleparameter
% empty check outside here
196
{
\edef
\detokenizedstyleparameter
{
\detokenize\expandafter
{
\currentstyleparameter
}}
%
197
\settrue
\fontattributeisset
% reset is done elsewhere
198
\ifcsname
\??alternativestyle
\detokenizedstyleparameter
\endcsname
199
\lastnamedcs
200
\else\ifcsname
\detokenizedstyleparameter
\endcsname
201
\lastnamedcs
202
\else
203
\currentstyleparameter
204
\fi\fi
}
205 206
\let
\dosetfontattribute
\dousestylehashparameter
% for a while
207 208
%D New commands (not yet interfaced):
209
%D
210
%D \startbuffer
211
%D \definestyle[one][style=bold,color=darkblue]
212
%D
213
%D test \one{test} test
214
%D test \style[one]{test} test
215
%D test \style[color=red]{test} test
216
%D test \style[Serif at 20pt]{test} test
217
%D \stopbuffer
218
%D
219
%D \typebuffer \startlines \getbuffer \stoplines
220 221
% definitions .. no tagging here
222 223
\installcorenamespace
{
style
}
224
\installcorenamespace
{
stylecheck
}
225
\installcorenamespace
{
stylehack
}
226 227
\installcommandhandler
\??style
{
style
}
\??style
228 229
\setupstyle
230
[
%\c!style=,
231
%\c!color=,
232
\c!method
=
\v!command
]
233 234
\appendtoks
235
\letvalue
{
\??stylecheck
\currentstyle
}
\relax
236
\edef
\p_method
{
\styleparameter
\c!method
}
%
237
\ifx
\p_method
\v!command
238
\setuevalue
{
\e!start
\currentstyle
}{
\font_styles_apply_start
{
\currentstyle
}}
%
239
\setuevalue
{
\e!stop
\currentstyle
}{
\font_styles_apply_stop
}
%
240
\setuevalue
{
\currentstyle
}{
\font_styles_apply_grouped
{
\currentstyle
}}
% no longer groupedcommand here
241
\fi
242
\to
\everydefinestyle
243 244
\unexpanded
\def
\font_styles_apply_start
#
name
%
245
{
\begingroup
246
\font_styles_use_defined
{
#
name
}}
247 248
\unexpanded
\def
\font_styles_apply_stop
249
{
\endgroup
}
250 251
\unexpanded
\def
\font_styles_apply_grouped
#
name
% assumes that the next is { or \bgroup
252
{
\bgroup
253
\def
\g_style
{
\font_styles_use_defined
{
#
name
}}
%
254
\afterassignment
\g_style
255
\let
\nexttoken
}
256 257
\unexpanded
\def
\font_styles_use_defined
#
name
%
258
{
\edef
\currentstyle
{
#
name
}
%
259
\usestylestyleandcolor
\c!style\c!color
}
260 261
\unexpanded
\def
\font_styles_use_generic
#
specification
%
262
{
\let
\currentstyle
\s!unknown
% reasonable generic tag
263
\letstyleparameter
\c!style
\empty
264
\letstyleparameter
\c!color
\empty
265
\setupcurrentstyle
[#
specification
]
%
266
\usestylestyleandcolor
\c!style\c!color
}
267 268
% commands
269 270
\installcorenamespace
{
styleargument
}
271 272
\unexpanded
\def
\style
[#
name
]
% as this is can be a switch we use groupedcommand
273
{
\csname
\??styleargument
274
\ifcsname
\??stylecheck
#
name
\endcsname
275
2
% defined as style
276
\else\ifcsname
\??stylehack
#
name
\endcsname
277
4
% defined as command
278
\else\ifcsname
#
name
\endcsname
279
1
% defined as command
280
\else
281
3
% specification
282
\fi\fi\fi
283
\endcsname
{
#
name
}}
284 285
% \setvalue{\??styleargument1}#name%
286
% {\groupedcommand{\begincsname#name\endcsname}{}}
287 288
\setvalue
{
\??styleargument
1
}
#
name
%
289
{
\expandafter
\triggergroupedcommandcs
\begincsname
#
name
\endcsname
}
290 291
% \setvalue{\??styleargument2}#name%
292
% {\groupedcommand{\font_styles_use_defined{#name}}{}} % or {\font_styles_apply_grouped{#name}}
293 294
\setvalue
{
\??styleargument
2
}
#
name
%
295
{
\triggergroupedcommand
{
\font_styles_use_defined
{
#
name
}}}
% or {\font_styles_apply_grouped{#name}}
296 297
\setvalue
{
\??styleargument
3
}
#
specification
%
298
{
\doifelseassignment
{
#
specification
}
\font_styles_assignment
\font_styles_direct
{
#
specification
}}
299 300
\setvalue
{
\??styleargument
4
}
#
name
%
301
{
\expandafter
\triggergroupedcommandcs
\begincsname
\??stylehack
#
name
\endcsname
}
302 303
\setvalue
{
\??stylehack\s!math
}
% dirty trick
304
{
\groupedcommand\normalstartimath\normalstopimath
}
305 306
% \def\font_styles_assignment#specification{\groupedcommand{\font_styles_use_generic{#specification}}{}}
307
% \def\font_styles_direct #specification{\groupedcommand{\definedfont[#specification]}{}}
308 309
\def
\font_styles_assignment
#
specification
{
\triggergroupedcommand
{
\font_styles_use_generic
{
#
specification
}}}
310
\def
\font_styles_direct
#
specification
{
\triggergroupedcommand
{
\definedfont
[#
specification
]
}}
311 312
% environments
313 314
\installcorenamespace
{
styleenvironment
}
315 316
\unexpanded
\def
\startstyle
[#
name
]
%
317
{
\begingroup
318
\csname
\??styleenvironment
319
\ifcsname
\??stylecheck
#
name
\endcsname
320
2
% defined as style
321
\else\ifcsname
#
name
\endcsname
322
1
% defined as command
323
\else
324
3
% specification
325
\fi\fi
326
\endcsname
{
#
name
}}
327 328
\unexpanded
\def
\stopstyle
329
{
\endgroup
330
\autoinsertnextspace
}
% will be configurable, maybe also in \definestartstop
331 332
\setvalue
{
\??styleenvironment
1
}
#
name
%
333
{
\csname
#
name
\endcsname
}
334 335
\setvalue
{
\??styleenvironment
2
}
#
name
%
336
{
\font_styles_use_defined
{
#
name
}}
337 338
\setvalue
{
\??styleenvironment
3
}
#
specification
%
339
{
\doifelseassignment
{
#
specification
}
\font_styles_start_assignment
\font_styles_start_direct
{
#
specification
}}
340 341
\def
\font_styles_start_assignment
#
specification
{
\font_styles_use_generic
{
#
specification
}}
342
\def
\font_styles_start_direct
#
specification
{
\definedfont
[#
specification
]
\relax
}
343 344
%D Still experimental (might even go away).
345 346
% \definestylecollection[mine]
347 348
% \definestyleinstance[mine][default][sorry]
349
% \definestyleinstance[mine][tt][bs][ttbs:\rm\sl]
350
% \definestyleinstance[mine][tt][bf][ttbf:\rm\sl]
351
% \definestyleinstance[mine][bf][\sl]
352
% \definestyleinstance[mine][sl][\tt]
353 354
% {\bf test \mine test \sl test \mine test \bs oeps \mine oeps {\tt test \mine \bf test}}
355 356
\installcorenamespace
{
stylecollection
}
357 358
\unexpanded
\def
\definestylecollection
359
{
\dosingleargument
\font_styles_define_style_collection
}
360 361
\def
\font_styles_define_style_collection
[#
name
]
%
362
{
\iffirstargument
363
\setuvalue
{
#
name
}{
\styleinstance
[#
name
]
}
%
364
\def
\font_styles_define_style_collection_a
#
style
%
365
{
\def
\font_styles_define_style_collection_b
#
alternative
{
\undefinevalue
{
\??stylecollection
#
name
:
#
style
:
#
alternative
}}
%
366
\font_helpers_process_alternative_list
\font_styles_define_style_collection_b
367
\font_styles_define_style_collection_b
\s!default
}
%
368
\font_helpers_process_style_list
\font_styles_define_style_collection_a
369
\font_styles_define_style_collection_a
\s!default
370
\fi
}
371 372
\let
\font_styles_define_style_collection_a
\relax
373
\let
\font_styles_define_style_collection_b
\relax
374 375
\unexpanded
\def
\definestyleinstance
376
{
\doquadrupleargument
\font_styles_define_style_instance
}
377 378
\def
\font_styles_define_style_instance
[#
instance
][#
2
][#
3
][#
4
]
% [name] [rm|ss|tt|..] [sl|bf|...] [whatever]
379
{
\iffirstargument
380
\ifcsname
#
instance
\endcsname\else
\font_styles_define_style_collection
[#
instance
]
\fi
381
\fi
382
\iffourthargument
383
\setvalue
{
\??stylecollection
#
instance
:
#
2
:
#
3
}{
#
4
}
%
384
\else
\ifthirdargument
385
\setvalue
{
\??stylecollection
#
instance
:
:
#
2
}{
#
3
}
%
386
\else
\ifsecondargument
387
\letvalueempty
{
\??stylecollection
#
instance
:
:
#
2
}
%
388
\fi\fi\fi
}
389 390
% \unexpanded\def\styleinstance[#instance]% will be made faster
391
% {%\begingroup\normalexpanded{\noexpand\infofont[#1:\fontstyle:\fontalternative]}\endgroup
392
% \executeifdefined{\??stylecollection#instance:\fontstyle:\fontalternative}%
393
% {\executeifdefined{\??stylecollection#instance:\fontstyle:\s!default}%
394
% {\executeifdefined{\??stylecollection#instance::\fontalternative}
395
% {\getvalue {\??stylecollection#instance::\s!default}}}}}
396 397
\unexpanded
\def
\styleinstance
[#
instance
]
% maybe \lastnamedcs here too
398
{
\csname
\??stylecollection
#
instance
:
%
399
\ifcsname
\??stylecollection
#
instance
:
\fontstyle
:
\fontalternative
\endcsname
400
\fontstyle
:
\fontalternative
401
\else\ifcsname
\??stylecollection
#
instance
:
\fontstyle
:
\s!default
\endcsname
402
\fontstyle
:
\s!default
403
\else\ifcsname
\??stylecollection
#
instance
:
:
\fontalternative
\endcsname
404
:
\fontalternative
405
\else
406
:
\s!default
407
\fi\fi\fi
408
\endcsname
}
409 410
%D Variant selectors
411
%D
412
%D \starttyping
413
%D \mathematics {\vsone{\utfchar{0x2229}}}
414
%D \mathematics {\utfchar{0x2229}\vsone{}}
415
%D \stoptyping
416 417
\unexpanded
\edef
\vsone
#
character
{
#
character
\normalUchar
"
FE
0
0
}
% used
418
\unexpanded
\edef
\vstwo
#
character
{
#
character
\normalUchar
"
FE
0
1
}
% not used but handy for testing
419 420
%D For historic reasons we keep the following around but they are no longer that
421
%D relevant for \MKIV.
422 423
\unexpanded
\def
\doattributes
#
1
#
2
#
3
#
4
%
424
{
\begingroup
% geen \bgroup, anders in mathmode lege \hbox
425
\dousestylehashparameter
{
#
1
}{
#
2
}
%
426
\dousecolorhashparameter
{
#
1
}{
#
3
}
%
427
#
4
%
428
\endgroup
}
429 430
\unexpanded
\def
\dostartattributes
#
1
#
2
#
3
%
431
{
\begingroup
% geen \bgroup, anders in mathmode lege \hbox
432
\dousestylehashparameter
{
#
1
}{
#
2
}
%
433
\dousecolorhashparameter
{
#
1
}{
#
3
}}
434 435
\let
\dostopattributes
\endgroup
436 437
%D New but it needs to be supported explicitly (as in natural tables).
438 439
\newconditional
\c_font_styles_math
440 441
\unexpanded
\def
\font_styles_math_reset
442
{
\setfalse
\c_font_styles_math
}
443 444
\unexpanded
\def
\font_styles_math_start
445
{
\ifconditional
\c_font_styles_math
446
\startimath
447
\fi
448
\relax
}
449 450
\unexpanded
\def
\font_styles_math_stop
451
{
\relax
452
\ifconditional
\c_font_styles_math
453
\stopimath
454
\fi
}
455 456
\definealternativestyle
[
\v!math
][
\settrue
\c_font_styles_math
]
457 458
\protect
\endinput
459