font-set.mklx /size: 5783 b    last modification: 2023-12-21 09:44
1%D \module
2%D   [       file=font-set,
3%D        version=2011.01.13, % (copied fron font-ini)
4%D          title=\CONTEXT\ Font Macros,
5%D       subtitle=Initial Loading,
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 / Initial Loading}
15
16\unprotect
17
18%D Defining fonts using \type {\definebodyfont} takes time, so we prefer to
19%D predefine at least the Computer Modern Roman fonts. However, loading all those
20%D fonts at definition time would take both time and space. But even worse, once
21%D fonts are loaded into memory, their encoding vector is fixed, which is a handicap
22%D when we want to distribute the compact \type {fmt} files. So what we want to do
23%D is defining fonts in a way that postpones the loading. We accomplish this by only
24%D loading the fonts when we switch to another bodyfont size. Among the other
25%D alternatives, such as loading the font at the moment of activation and redefining
26%D the activation macro afterwards, this proved to be the most efficient
27%D alternative.
28%D
29%D The next few macros take care of the one exeption on this scheme. When at format
30%D generation time we load the default font file, the one that defines the Computer
31%D Modern Fonts, we don't want the fonts metrics to end up in the format file, so we
32%D temporary prohibit loading. This means that at runtime we have to load the
33%D default bodyfont size just before we start typesetting.
34
35% \enablemode[lmmath]
36
37\lettonothing\m_font_fallback_name
38
39\def\font_preload_check_mode
40  {\def\m_font_fallback_name{modern}%
41   \glet\font_preload_check_mode\relax}
42
43\def\font_preload_default_fonts
44  {\font_preloads_reset
45   \font_preload_check_mode
46   \setupbodyfont[\m_font_fallback_name,\fontstyle,\fontbody]%
47   \showmessage\m!fonts6{fallback \m_font_fallback_name\space \fontstyle\normalspace\normalizedbodyfontsize}}
48
49\def\font_preload_default_fonts_mm
50  {\font_preload_check_mode
51   \writestatus\m!fonts{preloading \m_font_fallback_name\space (math)}%
52   \definetypeface[\fontclass][\s!mm][\s!math][\m_font_fallback_name][\s!default]%
53   \showmessage\m!fonts6{fallback \m_font_fallback_name\space mm \normalizedbodyfontsize}}
54
55\def\font_preload_default_fonts_tt
56  {\font_preload_check_mode
57   \writestatus\m!fonts{preloading \m_font_fallback_name\space (mono)}%
58   \definetypeface[\fontclass][\s!tt][\s!mono][\m_font_fallback_name][\s!default]%
59   \showmessage\m!fonts6{fallback \m_font_fallback_name\space tt \normalizedbodyfontsize}}
60
61\def\font_preloads_reset
62  {\glet\font_preload_default_fonts   \relax
63  %\glet\font_preload_default_fonts_tt\relax
64  %\glet\font_preload_default_fonts_mm\relax
65   \glet\font_preloads_zero_stage     \relax
66   \glet\font_preloads_first_stage    \relax
67   \glet\font_preloads_second_stage   \relax
68  %\glet\font_preloads_third_stage    \relax
69   \global\everyhbox\emptytoks
70   \global\everyvbox\emptytoks
71   % old: \font_preloads_reset_nullfont, new:
72   \clf_resetnullfont}
73
74\appendtoks
75    \font_preloads_reset
76\to \everystartnotext
77
78\def\font_preloads_reset_checked
79  {\glet\font_preload_default_fonts_tt\relax
80   \glet\font_preload_default_fonts_mm\relax
81   \glet\font_preloads_third_stage    \relax
82   \glet\font_preloads_fourth_stage   \relax}
83
84\def\font_preloads_zero_stage_indeed
85  {\definedfont[\s!file:lmmono10-regular sa 1]}
86
87\protected\def\font_preloads_zero_stage
88  {\writestatus\m!fonts{beware: no fonts are loaded yet, using 'lm mono' in box}%
89   \glet\font_preloads_zero_stage\font_preloads_zero_stage_indeed
90   \font_preloads_zero_stage}
91
92\protected\def\font_preloads_first_stage % % *nofonts -> *preloadfonts
93  {\ifmode{*preloadfonts}%
94     \font_preload_default_fonts
95     \writestatus\m!fonts{preloading latin modern fonts (first stage)}%
96   \else
97     \writestatus\m!fonts{latin modern fonts are not preloaded}%
98   \fi}
99
100\protected\def\font_preloads_second_stage
101  {\writestatus\m!fonts{preloading latin modern fonts (second stage)}%
102   \font_preload_default_fonts}
103
104\protected\def\font_preloads_third_stage % always
105  {\ifempty\fontclass
106     \writestatus\m!fonts{preloading latin modern fonts (third stage)}%
107     \font_preload_default_fonts
108     \font_preloads_reset
109   \else
110     \font_preloads_reset
111     \push_macro_fontstyle
112     \ifcsname\??fontclass\fontclass\s!mm\s!features\endcsname \else
113       \font_preload_default_fonts_mm
114     \fi
115     \ifcsname\??fontclass\fontclass\s!tt\s!features\endcsname \else
116       \font_preload_default_fonts_tt
117     \fi
118     \pop_macro_fontstyle
119     \font_preloads_reset_checked % reset third, mm and tt
120     \setupbodyfont[\fontstyle]%
121   \fi}
122
123\protected\def\font_preloads_fourth_stage
124  {\begingroup
125   %ifzeropt\scaledfontcharwd\font\number`!\relax
126   \setbox\scratchbox\hpack{\tf c o n t e x t}%
127   \ifzeropt\wd\scratchbox
128     \writeline
129     \writestatus\m!fonts{!! No bodyfont has been defined and no defaults have been}%
130     \writestatus\m!fonts{!! loaded (maybe due to a missing \string\starttext).}%
131     \writeline
132   \fi
133   \font_preloads_reset
134   \endgroup}
135
136\appendtoks
137    \everyhbox{\font_preloads_zero_stage}%
138    \everyvbox{\font_preloads_zero_stage}%
139\to \everydump
140
141\protected\def\font_preloads_at_every_job {\font_preloads_first_stage }
142\protected\def\font_preloads_at_start_text{\font_preloads_second_stage}
143\protected\def\font_preloads_at_stop_text {\font_preloads_fourth_stage}
144\protected\def\font_preloads_at_definition{\font_preloads_third_stage
145         \glet\font_preloads_at_definition\relax}
146
147% \protected\def\checkpreloadedbodyfont % no \let here
148%   {\font_preloads_at_start_text}
149
150\protect \endinput
151