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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
%D \module
%D [ file=font-mat,
%D version=2011.01.13, % (copied fron font-ini)
%D title=\CONTEXT\ Font Macros,
%D subtitle=Math,
%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 / Math}
\unprotect
%D Be nice:
\ifdefined\??fontinstanceready \else \installcorenamespace{fontinstanceready} \fi
\ifdefined\??fontinstancebasic \else \installcorenamespace{fontinstancebasic} \fi
\ifdefined\??fontinstanceclass \else \installcorenamespace{fontinstanceclass} \fi
%D \macros
%D {textonly}
%D
%D Traditionally math has a big impact on font definitions, mainly
%D because we need to define alphabet variants using families and
%D fonts. This means that one can easily get 10 fonts loaded per
%D math size. In \MKIV\ we use a different approach: one family
%D which has either a virtual font made of traditional fonts, or
%D an \OPENTYPE\ font that has it all.
%D
%D We currently use only one math family but in the future we
%D might consider using a second one for bold math. For the
%D moment we keep the \MKII\ method of using a token register
%D for definitions but we already dropped the text and symbols
%D ones since they now live in the same family.
\newtoks \t_font_math_strategies
\newconditional\c_font_synchronize_math_fonts \settrue\c_font_synchronize_math_fonts
\unexpanded\def\font_helpers_synchronize_math % math stuff in mmode
{\ifconditional\c_font_synchronize_math_fonts\the\t_font_math_strategies\fi}
\unexpanded\def\textonly{\setfalse\c_font_synchronize_math_fonts} % document this
%D The main math font definer. We have removed some optimized
%D code simply because we now always have a fontclass. We could
%D check for fontclass being default or empty and save a few
%D tests but it does not help us when no math is defined.
%D Because we want to keep mr=.. and mb=... settings (read: not
%D break downward compatibility by enforcing mrlr etc) we need a
%D bit more code that optimal.
% todo: \c_font_fam_mr
\let\c_font_fam_mr \zerocount % math regular
\let\c_font_fam_mr_lr\plusone % math regular l2r
\let\c_font_fam_mr_rl\plustwo % math regular r2l
\let\c_font_fam_mb \plusthree % math bold
\let\c_font_fam_mb_lr\plusfour % math bold l2r
\let\c_font_fam_mb_rl\plusfive % math bold r2l
\definesystemattribute[mathfamily][public]
\newconditional\c_font_bidirectional_math_strategy % can be default, not that much overhead: \settrue\c_font_bidirectional_math_strategy
\newconditional\c_font_complete_bold_math_strategy \settrue\c_font_complete_bold_math_strategy
\def\mathtextsuffix {-text}
\def\mathscriptsuffix {-script}
\def\mathscriptscriptsuffix{-scriptscript}
\def\mathsizesuffix{\ifcase\fontface\or\mathtextsuffix\or\mathscriptsuffix\or\mathscriptscriptsuffix\fi}
% Beware: truefontname also does a fallback on defaultfontclass so there
% can be some interference here, which is why we use a different method
% for bold.
\def\font_helpers_set_math_family_a
{\ifcsname\??fontinstanceready\fontclass -\fontbody-\s!mm-\fontfamily-\fontsize\endcsname \setfalse\c_font_auto_size
\csname\??fontinstanceready\fontclass -\fontbody-\s!mm-\fontfamily-\fontsize\endcsname \else
\ifcsname\??fontinstanceready\fontclass -\fontbody-\s!mm-\fontfamily \endcsname \settrue \c_font_auto_size
\csname\??fontinstanceready\fontclass -\fontbody-\s!mm-\fontfamily \endcsname \else
\font_helpers_set_math_family_b
\fi\fi}
\def\font_helpers_set_math_family_b
{\ifcsname\??fontinstanceready\defaultfontclass-\fontbody-\s!mm-\fontfamily-\fontsize\endcsname \setfalse\c_font_auto_size
\csname\??fontinstanceready\defaultfontclass-\fontbody-\s!mm-\fontfamily-\fontsize\endcsname \else
\ifcsname\??fontinstanceready\defaultfontclass-\fontbody-\s!mm-\fontfamily \endcsname \settrue \c_font_auto_size
\csname\??fontinstanceready\defaultfontclass-\fontbody-\s!mm-\fontfamily \endcsname \else
\font_helpers_set_math_family_c
\fi\fi}
\def\font_helpers_set_math_family_c
{\ifcsname\??fontinstanceready \fontbody-\s!mm-\fontfamily-\fontsize\endcsname \setfalse\c_font_auto_size
\csname\??fontinstanceready \fontbody-\s!mm-\fontfamily-\fontsize\endcsname \else
\ifcsname\??fontinstanceready \fontbody-\s!mm-\fontfamily \endcsname \settrue \c_font_auto_size
\csname\??fontinstanceready \fontbody-\s!mm-\fontfamily \endcsname \else
\nullfont \settrue \c_font_auto_size
\fi\fi}
\def\font_helpers_set_math_family_indeed#mrtag#family% \fontface etc are also used later on
{\let\savedfontbody\fontbody
\let\fontfamily#family%
\let\fontface\!!plusthree\font_helpers_set_math_family_a\scriptscriptfont#mrtag\font
\let\fontface\!!plustwo \font_helpers_set_math_family_a\scriptfont #mrtag\font
\let\fontface\!!plusone \font_helpers_set_math_family_a\textfont #mrtag\font
\let\fontface\!!zerocount
\let\fontbody\savedfontbody
\setfalse\c_font_auto_size}
\def\font_helpers_set_math_family_bold_a#font#mbfam#mrfam%
{\ifcsname\??fontinstanceready\fontclass-\fontbody-\s!mm-\fontfamily-\fontsize\endcsname \setfalse\c_font_auto_size
\csname\??fontinstanceready\fontclass-\fontbody-\s!mm-\fontfamily-\fontsize\endcsname #font#mbfam\font \else
\ifcsname\??fontinstanceready\fontclass-\fontbody-\s!mm-\fontfamily \endcsname \settrue \c_font_auto_size
\csname\??fontinstanceready\fontclass-\fontbody-\s!mm-\fontfamily \endcsname #font#mbfam\font \else
#font#mbfam#font#mrfam%
\fi\fi}
\def\font_helpers_set_math_family_bold_indeed#mbfam#familytag#mrfam% \c_font_fam_mb \s!mb \c_font_fam_mr
{\let\savedfontclass\defaultfontclass
\let\defaultfontclass\fontclass % else truefontname falls back on the wrong one
\let\savedfontbody\fontbody
\let\fontfamily#familytag%
\let\fontface\!!plusthree\font_helpers_set_math_family_bold_a\scriptscriptfont#mbfam#mrfam%
\let\fontface\!!plustwo \font_helpers_set_math_family_bold_a\scriptfont #mbfam#mrfam%
\let\fontface\!!plusone \font_helpers_set_math_family_bold_a\textfont #mbfam#mrfam%
\let\fontface\!!zerocount
\let\fontbody\savedfontbody
\let\defaultfontclass\savedfontclass
\setfalse\c_font_auto_size}
% optimized: math fonts are never changed (10K \bfa $x$: 3.2 => 2.5 (baseline 1.0))
%
% sort of tricky: we cannot reset in \everybeforedefinetypeface as we don't know
% all sizes so we postpone the optimization to the first starttext
%
% pitfall: we should reset 'm when a fontclass name is reused
\newconditional\optimizemathfontdefinitions \settrue\optimizemathfontdefinitions
\def\font_helpers_set_math_family#mrfam#familytag%
{\ifconditional\optimizemathfontdefinitions
\ifcsname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-1\endcsname
% \writestatus{fonts}{math: reusing \fontclass\fontbody\s!mm#familytag\fontsize1}%
\font_helpers_preset_math_family_indeed#mrfam#familytag%
\else
% \writestatus{fonts}{math: defining \fontclass\fontbody\s!mm#familytag\fontsize1}%
\font_helpers_set_math_family_indeed#mrfam#familytag%
\fi
\else
\font_helpers_set_math_family_indeed#mrfam#familytag%
\fi}
\def\font_helpers_set_math_family_bold#mbfam#familytag#mrfam%
{\ifconditional\optimizemathfontdefinitions
\ifcsname\??fontinstanceclass\fontclass-\textface-\s!mm-#familytag-\fontsize-1\endcsname
\font_helpers_preset_math_family_indeed#mbfam#familytag%
\else
\font_helpers_set_math_family_bold_indeed#mbfam#familytag#mrfam%
\fi
\else
\font_helpers_set_math_family_bold_indeed#mbfam#familytag#mrfam%
\fi}
%D It can happen that we use a bodyfont with no math in which case
%D we have a problem with setting the global bodyfont size in the
%D page builder. For instance in:
%D
%D \starttext
%D \definetypeface[test][rm][serif][pagella][default]
%D \setupbodyfont[test]
%D test
%D \stoptext
%D
%D This is why we need the check:
% \def\font_helpers_preset_math_family_indeed#fam#familytag%
% {\expandafter\ifx\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-1\endcsname\relax
% \writestatus{fonts}{math: unset for global bodyfont \fontclass\space at \fontbody}%
% \else
% \scriptscriptfont#fam\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-3\endcsname
% \scriptfont #fam\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-2\endcsname
% \textfont #fam\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-1\endcsname
% \fi}
%
% Uglier but more efficient (esp in tracing):
\def\font_helpers_preset_math_family_indeed#fam#familytag%
{\expandafter\let\expandafter\v_font_math_one\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-1\endcsname
\ifx\v_font_math_one\relax
\writestatus{fonts}{math: unset for global bodyfont \fontclass\space at \fontbody}%
\else
\scriptscriptfont#fam\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-3\endcsname
\scriptfont #fam\csname\??fontinstanceclass\fontclass-\fontbody-\s!mm-#familytag-\fontsize-2\endcsname
\textfont #fam\v_font_math_one
\fi}
\let\font_helpers_reset_fontclass_math_families\gobbleoneargument
% It would be nice if characters could be defined in a neutral way (say fam 255) and
% be mapped to a real family during noad list construction. However, this changes
% tex in critical places so for the moment we simulate this using manipulation.
% For tracing purposes we use three families but in l2r mode 1 and 2 are copies of 0
% while in rl mode 0 is a copy of 1. There is no real overhead involved in this. This
% also permits different font definitions for normal and mixed.
\let\m_font_class_direction\empty
\let\m_font_class_features \empty
\let\m_font_class_fallbacks\empty
\let\m_font_class_goodies \empty
\let\m_font_direction\empty
\let\m_font_features \empty
\let\m_font_fallbacks\empty
\let\m_font_goodies \empty
\appendtoks % can be analyzed once
% why here ..
\edef\m_font_class_direction{\ifcsname\??fontclass\fontclass\s!mm\s!direction\endcsname\csname\??fontclass\fontclass\s!mm\s!direction\endcsname\fi}%
% ...
\ifx\m_font_class_direction\v!both
\settrue\c_font_bidirectional_math_strategy
\else
\setfalse\c_font_bidirectional_math_strategy
\fi
\to \t_font_math_strategies
\def\font_helpers_bidirectional_mathstrategy_yes
{\font_helpers_set_math_family\c_font_fam_mr_lr\s!mrlr
\font_helpers_set_math_family\c_font_fam_mr_rl\s!mrrl
\textfont \c_font_fam_mr\textfont \c_font_fam_mr_lr
\scriptfont \c_font_fam_mr\scriptfont \c_font_fam_mr_lr
\scriptscriptfont\c_font_fam_mr\scriptscriptfont\c_font_fam_mr_lr}
\def\font_helpers_bidirectional_mathstrategy_nop
{\font_helpers_set_math_family\c_font_fam_mr\s!mr
\textfont \c_font_fam_mr_rl\textfont \c_font_fam_mr
\scriptfont \c_font_fam_mr_rl\scriptfont \c_font_fam_mr
\scriptscriptfont\c_font_fam_mr_rl\scriptscriptfont\c_font_fam_mr
\textfont \c_font_fam_mr_lr\textfont \c_font_fam_mr
\scriptfont \c_font_fam_mr_lr\scriptfont \c_font_fam_mr
\scriptscriptfont\c_font_fam_mr_lr\scriptscriptfont\c_font_fam_mr}
\appendtoks
\ifconditional\c_font_bidirectional_math_strategy
\font_helpers_bidirectional_mathstrategy_yes
\else
\font_helpers_bidirectional_mathstrategy_nop
\fi
\to \t_font_math_strategies
\def\font_helpers_complete_bold_math_strategy_yes_bidi
{\font_helpers_set_math_family_bold\c_font_fam_mb_lr\s!mblr\c_font_fam_mr_lr
\font_helpers_set_math_family_bold\c_font_fam_mb_rl\s!mbrl\c_font_fam_mr_rl
\textfont \c_font_fam_mb\textfont \c_font_fam_mb_lr
\scriptfont \c_font_fam_mb\scriptfont \c_font_fam_mb_lr
\scriptscriptfont\c_font_fam_mb\scriptscriptfont\c_font_fam_mb_lr}
\def\font_helpers_complete_bold_math_strategy_yes
{\font_helpers_set_math_family_bold\c_font_fam_mb\s!mb\c_font_fam_mr
\textfont \c_font_fam_mb_rl\textfont \c_font_fam_mb
\scriptfont \c_font_fam_mb_rl\scriptfont \c_font_fam_mb
\scriptscriptfont\c_font_fam_mb_rl\scriptscriptfont\c_font_fam_mb
\textfont \c_font_fam_mb_lr\textfont \c_font_fam_mb
\scriptfont \c_font_fam_mb_lr\scriptfont \c_font_fam_mb
\scriptscriptfont\c_font_fam_mb_lr\scriptscriptfont\c_font_fam_mb}
\def\font_helpers_complete_bold_math_strategy_nop
{\textfont \c_font_fam_mb \textfont \c_font_fam_mr
\scriptfont \c_font_fam_mb \scriptfont \c_font_fam_mr
\scriptscriptfont\c_font_fam_mb \scriptscriptfont\c_font_fam_mr
\textfont \c_font_fam_mb_rl\textfont \c_font_fam_mr_rl
\scriptfont \c_font_fam_mb_rl\scriptfont \c_font_fam_mr_rl
\scriptscriptfont\c_font_fam_mb_rl\scriptscriptfont\c_font_fam_mr_rl
\textfont \c_font_fam_mb_lr\textfont \c_font_fam_mr_lr
\scriptfont \c_font_fam_mb_lr\scriptfont \c_font_fam_mr_lr
\scriptscriptfont\c_font_fam_mb_lr\scriptscriptfont\c_font_fam_mr_lr}
\def\font_helpers_apply_complete_bold_math_strategy
{\ifconditional\c_font_complete_bold_math_strategy
\ifconditional\c_font_bidirectional_math_strategy
\font_helpers_complete_bold_math_strategy_yes_bidi
\else
\font_helpers_complete_bold_math_strategy_yes
\fi
\else
\font_helpers_complete_bold_math_strategynop
\fi}
\appendtoks
\font_helpers_apply_complete_bold_math_strategy
\to \t_font_math_strategies
\ifdefined\defaultmathfamily \else
\setnewconstant\defaultmathfamily\zerocount
\fi
\appendtoks
\fam\defaultmathfamily % all characters and symbols are in this family
\to \everymathematics
\unexpanded\def\font_helpers_synchronize_math_family_mr
{\attribute\mathfamilyattribute\ifconditional\c_font_bidirectional_math_strategy
\ifconditional\c_math_right_to_left
\plustwo
\else
\plusone
\fi
\else
\zerocount
\fi}
\unexpanded\def\font_helpers_synchronize_math_family_mb
{\attribute\mathfamilyattribute\ifconditional\c_font_bidirectional_math_strategy
\ifconditional\c_math_right_to_left
\ifconditional\c_font_pseudo_bold_math_state\pluseight\else\plusfive\fi
\else
\ifconditional\c_font_pseudo_bold_math_state\plusseven\else\plusfour\fi
\fi
\else
\ifconditional\c_font_pseudo_bold_math_state\plussix\else\plusthree\fi
\fi}
\installcorenamespace{fontmathsynchronizer}
\installcorenamespace{fontmathstoredstrategy}
\letvalue{\??fontmathsynchronizer\s!tf }\font_helpers_synchronize_math_family_mr
\letvalue{\??fontmathsynchronizer\s!sl }\font_helpers_synchronize_math_family_mr
\letvalue{\??fontmathsynchronizer\s!it }\font_helpers_synchronize_math_family_mr
\letvalue{\??fontmathsynchronizer\s!bf }\font_helpers_synchronize_math_family_mb
\letvalue{\??fontmathsynchronizer\s!bs }\font_helpers_synchronize_math_family_mb
\letvalue{\??fontmathsynchronizer\s!bi }\font_helpers_synchronize_math_family_mb
\letvalue{\??fontmathsynchronizer\empty}\font_helpers_synchronize_math_family_mr
\def\font_helpers_synchronize_math_family
{\csname\??fontmathsynchronizer\ifcsname\??fontmathsynchronizer\fontalternative\endcsname\fontalternative\fi\endcsname}
\ifdefined \fontid % we need to keep this test for a while
\appendtoks
\ifnum\fontid\textfont\zerocount=\fontid\textfont\plusthree
\letvalue{\??fontmathstoredstrategy\fontclass}\font_helpers_set_math_partial_bold_strategy
\else
\letvalue{\??fontmathstoredstrategy\fontclass}\font_helpers_set_math_full_bold_strategy
\fi
\to \t_font_math_strategies
\else
\appendtoks
\edef\currentmathfontmr{\fontname\textfont\zerocount}%
\edef\currentmathfontmb{\fontname\textfont\plusthree}%
\ifx\currentmathfontmr\currentmathfontmb
\letvalue{\??fontmathstoredstrategy\fontclass}\font_helpers_set_math_partial_bold_strategy
\else
\letvalue{\??fontmathstoredstrategy\fontclass}\font_helpers_set_math_full_bold_strategy
\fi
\to \t_font_math_strategies
\fi
\def\font_helpers_synchronize_math_bold_strategy{\csname\??fontmathstoredstrategy\fontclass\endcsname}
\newconditional\c_font_pseudo_bold_math_state
\def\font_helpers_set_math_partial_bold_strategy{\settrue \c_font_pseudo_bold_math_state}
\def\font_helpers_set_math_full_bold_strategy {\setfalse\c_font_pseudo_bold_math_state}
\appendtoks
\font_helpers_synchronize_math_bold_strategy
\to \everymathematics
% Bold is somewhat special as we might want both full-bold-math mixed
% regular-math, as well as automatic adaption to outer bold (in titles
% and inline text bold) so we will need explicit switches as well as
% an automatic one. (We will use lucida as an example.)
\unexpanded\def\mr
{\ifmmode
\font_helpers_synchronize_math_family_mr
\else
\font_helpers_set_current_font_alternative\s!mr
\fi}
\unexpanded\def\mb
{\ifmmode
\font_helpers_synchronize_math_family_mb
\else
\font_helpers_set_current_font_alternative\s!mb
\fi}
\appendtoks
\font_helpers_synchronize_math_family % auto bold
\to \everymathematics
%D \macros
%D {bigmath,nobigmath}
%D
%D We can inhibit this slow||downer with:
% these can best be combined
% 0=never 1=everymath 2=always
\setnewconstant\synchronizebigmathflag\plusone
\appendtoks
\ifcase\synchronizebigmathflag
% never
\or
\synchronizebigmath
\or
% always
\fi
\to \everymathematics
\unexpanded\def\nobigmath {\synchronizebigmathflag\zerocount}
\unexpanded\def\autobigmath{\synchronizebigmathflag\plusone\synchronizebigmath}
\unexpanded\def\bigmath {\synchronizebigmathflag\plustwo\synchronizebigmath}
\let\bigmathfontsize\empty
\unexpanded\def\synchronizebigmath
{\ifx\bigmathfontsize\fontsize
% already in sync
\else
\let\bigmathfontsize\fontsize
\font_helpers_synchronize_math
\fi}
\unexpanded\def\font_helpers_check_big_math_synchronization
{\ifcase\synchronizebigmathflag
% never
\or
\ifmmode \synchronizebigmath \fi
\or
\synchronizebigmath
\fi}
\protect \endinput
|