font-sel.mklx /size: 11 Kb    last modification: 2023-12-21 09:44
1%D \module
2%D   [     file=font-sel,
3%D      version=2016.08.28,
4%D        title=\CONTEXT\ User Module,
5%D     subtitle=Selectfont,
6%D       author=Wolfgang Schuster,
7%D         date=\currentdate,
8%D    copyright=Wolfgang Schuster,
9%D      license=GNU General Public License]
10
11\writestatus{loading}{ConTeXt User Module / Selectfont}
12
13\registerctxluafile{font-sel}{}
14
15\unprotect
16
17\installcorenamespace                      {selectfont}
18\installsimplecommandhandler \??selectfont {selectfont}
19
20\let\selectfont_index\!!zerocount
21
22\protected\def\selectfont_register[#style][#settings]%
23  {\begingroup
24   \edef\currentselectfont{\expandnamespacevalue\??fontshortstyle{#style}\s!rm}%
25   \checkselectfontparent
26   \setupcurrentselectfont[#settings]%
27   \edef\p_selectfont_preset{\selectfontparameter\c!preset}%
28   \ifempty\p_selectfont_preset \else
29     \processcommacommand[\p_selectfont_preset]\selectfont_preset_process
30     \setupcurrentselectfont[#settings]%
31   \fi
32   \font_helpers_process_alternative_list\clf_registerfontalternative
33   \setexpandedselectfontparameter\c!style{\expandnamespaceparameter\??fontshortstyle\selectfontparameter\c!style\s!rm}%
34   \xdef\selectfont_index{\clf_registerfontfamily
35        metadata {%
36            typeface    {\selectfontparameter\c!label}%
37            style       {\selectfontparameter\c!style}%
38            family      {\selectfontparameter\c!name}%
39        }
40        options {%
41            designsize  {\selectfontparameter\s!designsize}%
42            rscale      {\selectfontparameter\s!rscale}%
43            goodies     {\selectfontparameter\c!goodies}%
44            extras      {\selectfontparameter\c!extras}%
45            features    {\selectfontparameter\c!features}%
46            preset      {\selectfontparameter\c!preset}%
47            range       {\selectfontparameter\c!range}%  fallback only
48            offset      {\selectfontparameter\c!offset}% fallback only
49            check       {\selectfontparameter\c!check}%  fallback only
50            force       {\selectfontparameter\c!force}%  fallback only
51        }
52        userdata {%
53            \luaexpanded{#settings}%
54        }}%
55   \endgroup}
56
57%D \macros
58%D   {definefontfamilypreset}
59
60\installcorenamespace {selectfontpreset}
61
62\permanent\tolerant\protected\def\definefontfamilypreset[#name]#spacer[#settings]%
63  {\defcsname\??selectfontpreset#name\endcsname{\setupcurrentselectfont[#settings]}%
64   \clf_definefontfamilypreset{#name}{\luaexpanded{#settings}}}
65
66\def\selectfont_preset_process#name%
67  {\ifcsname\??selectfontpreset#name\endcsname
68     \lastnamedcs
69   \else
70     % unknown preset
71   \fi}
72
73\definefontfamilypreset
74  [range:chinese]
75  [\c!range={cjkcompatibilityforms,
76             cjkcompatibilityideographs,
77             cjkcompatibilityideographssupplement,
78             cjkradicalssupplement,
79             cjkstrokes,
80             cjksymbolsandpunctuation,
81             cjkunifiedideographs,
82             cjkunifiedideographsextensiona,
83             cjkunifiedideographsextensionb,
84             halfwidthandfullwidthforms,
85             verticalforms,
86             bopomofo,
87             bopomofoextended}]
88
89\definefontfamilypreset
90  [range:japanese]
91  [\c!range={cjkcompatibilityforms,
92             cjkcompatibilityideographs,
93             cjkcompatibilityideographssupplement,
94             cjkradicalssupplement,
95             cjkstrokes,
96             cjksymbolsandpunctuation,
97             cjkunifiedideographs,
98             cjkunifiedideographsextensiona,
99             cjkunifiedideographsextensionb,
100             halfwidthandfullwidthforms,
101             verticalforms,
102             hiragana,
103             katakana}]
104
105\definefontfamilypreset
106  [range:korean]
107  [\c!range={cjkcompatibilityforms,
108             cjkcompatibilityideographs,
109             cjkcompatibilityideographssupplement,
110             cjkradicalssupplement,
111             cjkstrokes,
112             cjksymbolsandpunctuation,
113             cjkunifiedideographs,
114             cjkunifiedideographsextensiona,
115             cjkunifiedideographsextensionb,
116             halfwidthandfullwidthforms,
117             verticalforms,
118             hangulcompatibilityjamo,
119             hanguljamo,
120             hanguljamoextendeda,
121             hanguljamoextendedb,
122             hangulsyllables}]
123
124\definefontfamilypreset
125  [range:cyrillic]
126  [\c!range={cyrillic,
127             cyrillicextendeda,
128             cyrillicextendedb,
129             cyrillicsupplement}]
130
131\definefontfamilypreset
132  [range:greek]
133  [\c!range={greekandcoptic,
134             greekextended,
135             ancientgreeknumbers}]
136
137\definefontfamilypreset
138  [range:hebrew]
139  [\c!range={hebrew,
140             alphabeticpresentationforms}]
141
142\definefontfamilypreset [math:digitsnormal]             [\c!range=digitsnormal]
143\definefontfamilypreset [math:digitsbold]               [\c!range=digitsnormal,\c!offset=digitsbold,\s!tf=style:bold]
144
145\definefontfamilypreset [math:uppercasenormal]          [\c!range=uppercasenormal]
146\definefontfamilypreset [math:uppercaseitalic]          [\c!range=uppercasenormal,\c!offset=uppercaseitalic,    \s!tf=style:italic]
147\definefontfamilypreset [math:uppercasebold]            [\c!range=uppercasenormal,\c!offset=uppercasebold,      \s!tf=style:bold]
148\definefontfamilypreset [math:uppercasebolditalic]      [\c!range=uppercasenormal,\c!offset=uppercasebolditalic,\s!tf=style:bolditalic]
149
150\definefontfamilypreset [math:lowercasenormal]          [\c!range=lowercasenormal]
151\definefontfamilypreset [math:lowercaseitalic]          [\c!range=lowercasenormal,\c!offset=lowercaseitalic,    \s!tf=style:italic]
152\definefontfamilypreset [math:lowercasebold]            [\c!range=lowercasenormal,\c!offset=lowercasebold,      \s!tf=style:bold]
153\definefontfamilypreset [math:lowercasebolditalic]      [\c!range=lowercasenormal,\c!offset=lowercasebolditalic,\s!tf=style:bolditalic]
154
155\definefontfamilypreset [math:mathematicaloperators]    [\c!range=mathematicaloperators]
156
157\definefontfamilypreset [math:lowercasegreeknormal]     [\c!range=lowercasegreeknormal]
158\definefontfamilypreset [math:lowercasegreekitalic]     [\c!range=lowercasegreeknormal,\c!offset=lowercasegreekitalic,    \s!tf=style:italic]
159\definefontfamilypreset [math:lowercasegreekbold]       [\c!range=lowercasegreeknormal,\c!offset=lowercasegreekbold,      \s!tf=style:bold]
160\definefontfamilypreset [math:lowercasegreekbolditalic] [\c!range=lowercasegreeknormal,\c!offset=lowercasegreekbolditalic,\s!tf=style:bolditalic]
161
162\definefontfamilypreset [math:uppercasegreeknormal]     [\c!range=uppercasegreeknormal]
163\definefontfamilypreset [math:uppercasegreekitalic]     [\c!range=uppercasegreeknormal,\c!offset=uppercasegreekitalic,    \s!tf=style:italic]
164\definefontfamilypreset [math:uppercasegreekbold]       [\c!range=uppercasegreeknormal,\c!offset=uppercasegreekbold,      \s!tf=style:bold]
165\definefontfamilypreset [math:uppercasegreekbolditalic] [\c!range=uppercasegreeknormal,\c!offset=uppercasegreekbolditalic,\s!tf=style:bolditalic]
166
167%D \macros
168%D   {definefontfamily,definefallbackfamily}
169%D
170%D The \tex{definefontfamily} creates like \tex{definetypeface} a collection of font
171%D with different styles which can be later called with the \tex{setupbodyfont} command.
172%D
173%D The command takes three mandatory commands which are (a) the name of the fontclass,
174%D (b) the styles of the font and (c) the name of the font.
175%D
176%D \starttyping
177%D \definefontfamily [dejavu] [serif] [DejaVu Serif]
178%D \definefontfamily [dejavu] [sans]  [DejaVu Sans]
179%D \definefontfamily [dejavu] [mono]  [DejaVu Sans Mono]
180%D \definefontfamily [dejavu] [math]  [XITS Math] [rscale=1.1]
181%D
182%D \definefontfamily [office] [serif] [Times New Roman]
183%D \definefontfamily [office] [sans]  [Arial] [rscale=0.9]
184%D \definefontfamily [office] [mono]  [Courier]
185%D \definefontfamily [office] [math]  [TeX Gyre Termes Math]
186%D
187%D \definefontfamily [linux] [serif] [Linux Libertine O]
188%D \definefontfamily [linux] [sans]  [Linux Biolinum O]
189%D \definefontfamily [linux] [mono]  [Latin Modern Mono]
190%D \definefontfamily [linux] [math]  [TeX Gyre Pagella Math] [rscale=0.9]
191%D
192%D \setupbodyfont[dejavu]
193%D
194%D \starttext
195%D
196%D \rm Serif \ss Sans \tt Mono \m{1+2=3}
197%D
198%D \switchtobodyfont[office]
199%D
200%D \rm Serif 123 \ss Sans \tt Mono \m{1+2=3}
201%D
202%D \switchtobodyfont[linux]
203%D
204%D \rm Serif 123 \ss Sans \tt Mono \m{1+2=3}
205%D
206%D \stoptext
207%D \stoptyping
208%D
209%D When a document contains different languages and the global font lacks some characters
210%D for one language, one could set a different font where these charcters are taken from.
211%D This fallback font (there can be more than one for a certain style) could be set with
212%D the \tex{definefallbackfamily} command which takes the same argument as
213%D the \tex{definefontfamily} command.
214%D
215%D \starttyping
216%D \definefallbackfamily [mainface] [serif] [DejaVu Serif] [range=cyrillic]
217%D \definefontfamily     [mainface] [serif] [TeX Gyre Pagella]
218%D
219%D \setupbodyfont[mainface]
220%D
221%D \setuplanguage[en][patterns={us,ru}]
222%D
223%D \starttext
224%D
225%D \input knuth
226%D
227%D Традиционная систематика лишайников оказывается во многом условна и
228%D
229%D \stoptext
230%D \stoptyping
231%D
232%D Another feature of the module is the \type{designsize} key which allows one to enable
233%D optical sizes when they are a feature of the requested font.
234%D
235%D \starttyping
236%D \definefontfamily[mainface][serif][Latin Modern Roman][designsize=auto]
237%D
238%D \setupbodyfont[mainface]
239%D
240%D \starttext
241%D \scale[width=\textwidth]{\switchtobodyfont   [6pt]\tf Regular, \it Italic \bf Bold and \bi BoldItalic}
242%D \scale[width=\textwidth]{\switchtobodyfont   [8pt]\tf Regular, \it Italic \bf Bold and \bi BoldItalic}
243%D \scale[width=\textwidth]{\switchtobodyfont  [10pt]\tf Regular, \it Italic \bf Bold and \bi BoldItalic}
244%D \scale[width=\textwidth]{\switchtobodyfont  [12pt]\tf Regular, \it Italic \bf Bold and \bi BoldItalic}
245%D \scale[width=\textwidth]{\switchtobodyfont[17.3pt]\tf Regular, \it Italic \bf Bold and \bi BoldItalic}
246%D \stoptext
247%D \stoptyping
248
249% tf = … | * … | name:… | name:* … | file:… | file:* … | style:medium
250
251\permanent\tolerant\protected\def\definefontfamily[#typeface]#spacer[#style]#spacer[#family]#spacer[#settings]%
252  {\ifhastok={#settings}%
253     \selectfont_register[#style][\c!label={#typeface},\c!style={#style},\c!name={#family},#settings]%
254   \else
255     \selectfont_register[#style][\c!label={#typeface},\c!style={#style},\c!name={#family},\c!preset={#settings}]%
256   \fi
257   \clf_definefontfamily\selectfont_index\relax}
258
259\permanent\tolerant\def\definefallbackfamily[#typeface]#spacer[#style]#spacer[#family]#spacer[#settings]%
260  {\ifhastok={#settings}%
261     \selectfont_register[#style][\c!label={#typeface},\c!style={#style},\c!name={#family},#settings]%
262   \else
263     \selectfont_register[#style][\c!label={#typeface},\c!style={#style},\c!name={#family},\c!preset={#settings}]%
264   \fi
265   \clf_definefallbackfamily\selectfont_index\relax}
266
267\permanent\tolerant\protected\def\setupfontfamily[#style]#spacer[#settings]%
268  {\ifparameter#settings\or
269     \edef\currentselectfont{\expandnamespacevalue\??fontshortstyle{#style}\s!rm}%
270     \setupcurrentselectfont[#settings]%
271   \else
272     \lettonothing\currentselectfont
273     \setupcurrentselectfont[#style]%
274   \fi}
275
276\setupselectfont
277  [  \c!features=\s!default,
278   \s!designsize=\s!default,
279       \s!rscale=\selectfontparameter\c!scale,
280        \c!scale=1]
281
282\protect
283