Debian Codegraph @

texmf-dist/source/latex/fontspec/fontspec-code-user.dtx

raw
1 %%^^A%% fontspec-code-user.dtx -- part of FONTSPEC <latex3.github.io/fontspec>
2
3 % \section{User command internals}
4 % \label{sec:codeinternal}
5 %
6 % \iffalse
7 % \begin{macrocode}
8 %<*fontspec>
9 % \end{macrocode}
10 % \fi
11 %
12 %
13 % \subsection{Font selection}
14 %
15 % \begin{macro}{\@@_main_fontspec:nn}
16 % This is the main command of the package that
17 % selects fonts with various features. It takes two arguments: the
18 % font name and the optional requested features of that
19 % font.
20 % \begin{macrocode}
21 \cs_new:Nn \@@_main_fontspec:nn
22 {
23 \fontspec_set_family:Nnn \f@family {#1} {#2}
24 \fontencoding { \g_@@_nfss_enc_tl }
25 \selectfont
26 }
27 % \end{macrocode}
28 % \end{macro}
29 %
30 % \begin{macro}{\rmfamily}
31 % \begin{macro}{\sffamily}
32 % \begin{macro}{\ttfamily}
33 % Add an encoding switch to the three family commands.
34 % \begin{macrocode}
35 \tl_set:Nn \l_@@_rmfamily_encoding_tl { \encodingdefault }
36 \tl_set:Nn \l_@@_sffamily_encoding_tl { \encodingdefault }
37 \tl_set:Nn \l_@@_ttfamily_encoding_tl { \encodingdefault }
38 % \end{macrocode}
39 %
40 % \begin{macrocode}
41 \AddToHook{rmfamily}{ \fontencoding \l_@@_rmfamily_encoding_tl }
42 \AddToHook{sffamily}{ \fontencoding \l_@@_sffamily_encoding_tl }
43 \AddToHook{ttfamily}{ \fontencoding \l_@@_ttfamily_encoding_tl }
44 % \end{macrocode}
45 % \end{macro}
46 % \end{macro}
47 % \end{macro}
48 %
49 % \begin{macro}{\setmainfont}
50 % The following three macros perform equivalent operations setting
51 % the default font for a particular family: `roman', sans serif, or typewriter
52 % (monospaced).
53 %
54 % They end with |\normalfont| so that if they're
55 % used in the document, the change registers immediately.
56 % \begin{macrocode}
57 \cs_new:Nn \@@_main_setmainfont:nn
58 {
59 %<debug>\typeout{::~_main_setmainfont:nn}
60 \DeclareFontSeriesDefault[rm]{bf}{\bfdefault}
61 \fontspec_set_family:Nnn \l_@@_rmfamily_family_tl {#1} {#2}
62 \tl_set_eq:NN \rmdefault \l_@@_rmfamily_family_tl
63 \tl_set_eq:NN \l_@@_rmfamily_encoding_tl \g_@@_nfss_enc_tl
64 \str_if_eq:eeT {\familydefault} {\rmdefault}
65 { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
66 \@@_setmainfont_hook:nn {#1} {#2} % for unicode-math only
67 \normalfont
68 }
69 % \end{macrocode}
70 % \end{macro}
71 %
72 % \begin{macro}{\setsansfont}
73 % Same as above.
74 % \begin{macrocode}
75 \cs_new:Nn \@@_main_setsansfont:nn
76 {
77 \DeclareFontSeriesDefault[sf]{bf}{\bfdefault}
78 \fontspec_set_family:Nnn \l_@@_sffamily_family_tl {#1} {#2}
79 \tl_set_eq:NN \sfdefault \l_@@_sffamily_family_tl
80 \tl_set_eq:NN \l_@@_sffamily_encoding_tl \g_@@_nfss_enc_tl
81 \str_if_eq:eeT {\familydefault} {\sfdefault}
82 { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
83 \@@_setsansfont_hook:nn {#1} {#2} % for unicode-math only
84 \normalfont
85 }
86 % \end{macrocode}
87 % \end{macro}
88 %
89 % \begin{macro}{\setmonofont}
90 % Same as above.
91 % \begin{macrocode}
92 \cs_new:Nn \@@_main_setmonofont:nn
93 {
94 \DeclareFontSeriesDefault[tt]{bf}{\bfdefault}
95 \fontspec_set_family:Nnn \l_@@_ttfamily_family_tl {#1} {#2}
96 \tl_set_eq:NN \ttdefault \l_@@_ttfamily_family_tl
97 \tl_set_eq:NN \l_@@_ttfamily_encoding_tl \g_@@_nfss_enc_tl
98 \str_if_eq:eeT {\familydefault} {\ttdefault}
99 { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
100 \@@_setmonofont_hook:nn {#1} {#2} % for unicode-math only
101 \normalfont
102 }
103 % \end{macrocode}
104 % \end{macro}
105 %
106 %
107 %
108 %
109 % \begin{macro}{\setmathrm}
110 % These commands are analogous to \cmd\setmainfont\ and others,
111 % but for selecting the font used for \cmd\mathrm, \etc. They
112 % can only be used in the preamble of the
113 % document. \cmd\setboldmathrm\ is used for specifying which
114 % fonts should be used in \cmd\boldmath.
115 % \begin{macrocode}
116 \cs_new:Nn \@@_main_setmathrm:nn
117 {
118 %<XE> \fontspec_gset_family:Nnn \g_@@_mathrm_tl {#1} {#2}
119 %<LU> \fontspec_gset_family:Nnn \g_@@_mathrm_tl {Renderer=Basic,#1} {#2}
120 \@@_setmathrm_hook:nn {#1} {#2} % for unicode-math only
121 }
122 % \end{macrocode}
123 % \end{macro}
124 %
125 % \begin{macro}{\setboldmathrm}
126 % \begin{macrocode}
127 \cs_new:Nn \@@_main_setboldmathrm:nn
128 {
129 %<XE> \fontspec_gset_family:Nnn \g_@@_bfmathrm_tl {#1} {#2}
130 %<LU> \fontspec_gset_family:Nnn \g_@@_bfmathrm_tl {Renderer=Basic,#1} {#2}
131 \@@_setboldmathrm_hook:nn {#1} {#2} % for unicode-math only
132 }
133 % \end{macrocode}
134 % \end{macro}
135 %
136 % \begin{macro}{\setmathsf}
137 % \begin{macrocode}
138 \cs_new:Nn \@@_main_setmathsf:nn
139 {
140 %<XE> \fontspec_gset_family:Nnn \g_@@_mathsf_tl {#1} {#2}
141 %<LU> \fontspec_gset_family:Nnn \g_@@_mathsf_tl {Renderer=Basic,#1} {#2}
142 \@@_setmathsf_hook:nn {#1} {#2} % for unicode-math only
143 }
144 % \end{macrocode}
145 % \end{macro}
146 %
147 % \begin{macro}{\setmathtt}
148 % \begin{macrocode}
149 \cs_new:Nn \@@_main_setmathtt:nn
150 {
151 %<XE> \fontspec_gset_family:Nnn \g_@@_mathtt_tl {#1} {#2}
152 %<LU> \fontspec_gset_family:Nnn \g_@@_mathtt_tl {Renderer=Basic,#1} {#2}
153 \@@_setmathtt_hook:nn {#1} {#2} % for unicode-math only
154 }
155 % \end{macrocode}
156 % \end{macro}
157 %
158 % Hooks:
159 % \begin{macrocode}
160 \cs_set_eq:NN \@@_setmainfont_hook:nn \use_none:nn
161 \cs_set_eq:NN \@@_setsansfont_hook:nn \use_none:nn
162 \cs_set_eq:NN \@@_setmonofont_hook:nn \use_none:nn
163 \cs_set_eq:NN \@@_setmathrm_hook:nn \use_none:nn
164 \cs_set_eq:NN \@@_setmathsf_hook:nn \use_none:nn
165 \cs_set_eq:NN \@@_setmathtt_hook:nn \use_none:nn
166 \cs_set_eq:NN \@@_setboldmathrm_hook:nn \use_none:nn
167 % \end{macrocode}
168 %
169 % Hmm, this isn't necessary with \pkg{unicode-math}; oh well:
170 % \begin{macrocode}
171 \@onlypreamble\setmathrm
172 \@onlypreamble\setboldmathrm
173 \@onlypreamble\setmathsf
174 \@onlypreamble\setmathtt
175 % \end{macrocode}
176 %
177 % If the commands above are not executed, then \cmd\rmdefault\ (\etc)
178 % will be used.
179 % \begin{macrocode}
180 \tl_gset:Nn \g_@@_mathrm_tl {\rmdefault}
181 \tl_gset:Nn \g_@@_mathsf_tl {\sfdefault}
182 \tl_gset:Nn \g_@@_mathtt_tl {\ttdefault}
183 % \end{macrocode}
184 %
185 % \begin{macro}{\@@_main_newfontfamily:NnnN}
186 % The inner fontspec workings define a font family, which is then used in a typical
187 % NFSS \cmd\fontfamily\ declaration, saved in the macro name specified.
188 % The fourth argument determines which xparse function to set the macro with (new/renew/etc).
189 % \begin{macrocode}
190 \cs_new:Nn \@@_main_newfontfamily:NnnN
191 {
192 \fontspec_set_family:cnn { l_@@_ \cs_to_str:N #1 _family_tl } {#2} {#3}
193 \use:x
194 {
195 \exp_not:N #4 \exp_not:N #1 {}
196 {
197 \exp_not:N \fontfamily { \use:c { l_@@_ \cs_to_str:N #1 _family_tl } }
198 \exp_not:N \fontencoding { \g_@@_nfss_enc_tl }
199 \exp_not:N \selectfont
200 }
201 }
202 }
203 % \end{macrocode}
204 % \end{macro}
205 %
206 % \begin{macro}{\@@_main_newfontface:NnnN}
207 % \cmd\newfontface\ uses the fact that if the argument to \feat{BoldFont}, etc., is empty (\ie, |BoldFont={}|), then no bold font is searched for.
208 % \begin{macrocode}
209 \cs_new:Nn \@@_main_newfontface:NnnN
210 {
211 \@@_main_newfontfamily:NnnN #1 { BoldFont={},ItalicFont={},SmallCapsFont={},#2 } {#3} #4
212 }
213 % \end{macrocode}
214 % \end{macro}
215 %
216 % \subsection{Font feature selection}
217 %
218 % \begin{macro}{\@@_set_default_features:nn}
219 % \begin{macrocode}
220 \cs_new:Nn \@@_set_default_features:nn
221 {
222 \IfBooleanTF {#1} \clist_gput_right:Nn \clist_gset:Nn
223 \g_@@_default_fontopts_clist {#2}
224 }
225 % \end{macrocode}
226 % \end{macro}
227 %
228 % \begin{macro}{\@@_set_font_default_features:nnn}
229 % The optional argument |#2| specifies font identifier(s).
230 % Branch for either (a)~single token input such as \verb|\rmdefault|, or (b)~otherwise assume its a fontname.
231 % In that case, strip spaces and file extensions and lower-case to ensure consistency.
232 % \begin{macrocode}
233 \cs_new:Nn \@@_set_font_default_features:nnn
234 {
235 %<debug> \typeout{\unexpanded{_set_font_default_features:nnn:{#1}{#2}{#3}}}
236 \clist_map_inline:nn {#2}
237 {
238 \tl_if_single:nTF {##1}
239 { \tl_set:No \l_@@_tmp_tl { \cs:w l_@@_ \cs_to_str:N ##1 _family_tl\cs_end: } }
240 { \@@_sanitise_fontname:Nn \l_@@_tmp_tl {##1} }
241
242 \IfBooleanTF {#1}
243 {
244 \prop_get:NVNF \g_@@_fontopts_prop \l_@@_tmp_tl \l_@@_tmpb_tl
245 { \tl_clear:N \l_@@_tmpb_tl }
246 \tl_put_right:Nn \l_@@_tmpb_tl {#3,}
247 \prop_gput:NVV \g_@@_fontopts_prop \l_@@_tmp_tl \l_@@_tmpb_tl
248 }
249 {
250 \tl_if_empty:nTF {#3}
251 { \prop_gremove:NV \g_@@_fontopts_prop \l_@@_tmp_tl }
252 { \prop_gput:NVn \g_@@_fontopts_prop \l_@@_tmp_tl {#3,} }
253 }
254 }
255 }
256 % \end{macrocode}
257 % \end{macro}
258 %
259 %
260 %
261 % \begin{macro}{\addfontfeatures}
262 % In order to be able to extend the feature selection of
263 % a given font, two things need to be known: the currently selected
264 % features, and the currently selected font. Every time a font
265 % family is created, this information is saved inside a control
266 % sequence with the name of the font family itself.
267 %
268 % This macro extracts this information, then appends the requested
269 % font features to add to the already existing ones, and calls the
270 % font again with the top level \cs{fontspec} command.
271 %
272 % The default options are \emph{not} applied (which is why
273 % \cs{g_fontspec_default_fontopts_tl} is emptied inside the group; this is allowed
274 % as \cmd\l_fontspec_family_tl\ is globally defined in \cmd\@@_select_font_family:nn), so this
275 % means that the only added features to the font are strictly those
276 % specified by this command.
277 %
278 % \cs{addfontfeature} is defined as an alias, as I found that I
279 % often typed this instead when adding only a single font feature.
280 % \begin{macrocode}
281 \cs_new:Nn \@@_main_addfontfeatures:n
282 {
283 %<debug> \typeout{^^J::::::::::::::::::::::::::::::::::^^J: addfontfeatures}
284 \fontspec_if_fontspec_font:TF
285 {
286 \group_begin:
287 \keys_set_known:nnN {fontspec-addfeatures} {#1} \l_@@_tmp_tl
288 \prop_get:cnN {g_@@_fontinfo_ \f@family _prop} {options} \l_@@_options_tl
289 \prop_get:cnN {g_@@_fontinfo_ \f@family _prop} {fontname} \l_@@_fontname_tl
290 \bool_set_true:N \l_@@_disable_defaults_bool
291 %<debug> \typeout{ \@@_select_font_family:nn { \l_@@_options_tl , #1 } {\l_@@_fontname_tl} }
292 \use:x
293 {
294 \@@_select_font_family:nn
295 { \l_@@_options_tl , #1 } {\l_@@_fontname_tl}
296 }
297 \group_end:
298 \fontfamily \g_@@_nfss_family_tl \selectfont
299 }
300 {
301 \@@_warning:nx {addfontfeatures-ignored} {#1}
302 }
303 \ignorespaces
304 }
305 % \end{macrocode}
306 % \end{macro}
307 %
308 % \subsection{Defining new font features}
309 %
310 % \begin{macro}{\newfontfeature}
311 % \cs{newfontfeature} takes two arguments: the name of the feature
312 % tag by which to reference it, and the string that is used to
313 % select the font feature.
314 % \begin{macrocode}
315 \cs_new:Nn \@@_main_newfontfeature:nn
316 {
317 \keys_define:nn { fontspec }
318 {
319 #1 .code:n = { \@@_update_featstr:n {#2} }
320 }
321 }
322 % \end{macrocode}
323 % \end{macro}
324 %
325 %
326 % \begin{macro}{\newAATfeature}
327 % This command assigns a new AAT feature by its code (|#2|,|#3|) to a new name (|#1|).
328 % Better than \cmd\newfontfeature\ because it checks if the feature exists in the
329 % font it's being used for.
330 % \begin{macrocode}
331 \cs_new:Nn \@@_main_newAATfeature:nnnn
332 {
333 \keys_if_exist:nnF { fontspec } {#1}
334 { \@@_define_aat_feature_group:n {#1} }
335
336 \keys_if_choice_exist:nnnT {fontspec} {#1} {#2}
337 { \@@_warning:nxx {feature-option-overwrite} {#1} {#2} }
338
339 \@@_define_aat_feature:nnnn {#1}{#2}{#3}{#4}
340 }
341 % \end{macrocode}
342 % \end{macro}
343 %
344 % \begin{macro}{\newopentypefeature}
345 % This command assigns a new OpenType feature by its abbreviation (|#2|) to a new name (|#1|).
346 % Better than \cmd\newfontfeature\ because it checks if the feature exists in the
347 % font it's being used for.
348 % \begin{macrocode}
349 \cs_new:Nn \@@_main_newopentypefeature:nnn
350 {
351 \keys_if_exist:nnF { fontspec / options } {#1}
352 { \@@_define_opentype_feature_group:n {#1} }
353
354 \keys_if_choice_exist:nnnT {fontspec} {#1} {#2}
355 { \@@_warning:nxx {feature-option-overwrite} {#1} {#2} }
356
357 \exp_args:Nnnx \@@_define_opentype_feature:nnnnn
358 {#1} {#2} { \@@_strip_plus_minus:n {#3} } {#3} {}
359 }
360 % \end{macrocode}
361 %
362 % \begin{macrocode}
363 \cs_new:Nn \@@_strip_plus_minus:n { \@@_strip_plus_minus_aux:Nq #1 \q_nil }
364 \cs_new:Npn \@@_strip_plus_minus_aux:Nq #1#2 \q_nil
365 {
366 \str_case:nnF {#1} { {+} {#2} {-} {#2} } {#1#2}
367 }
368 % \end{macrocode}
369 % \end{macro}
370 %
371 %
372 %
373 % \begin{macro}{\aliasfontfeature}
374 % User commands for renaming font features and font feature options.
375 % \begin{macrocode}
376 \cs_new:Nn \@@_main_aliasfontfeature:nn
377 {
378 %<debug> \typeout{::::::::::::::::::::^^J:: aliasfontfeature{#1}{#2}}
379 \bool_set_false:N \l_@@_alias_bool
380
381 \clist_map_inline:Nn \g_@@_all_keyval_modules_clist
382 {
383 \keys_if_exist:nnT {##1} {#1}
384 {
385 %<debug> \typeout{:::: Key~exists~##1~/~#1}
386 \bool_set_true:N \l_@@_alias_bool
387 \keys_define:nn {##1}
388 { #2 .code:n = { \keys_set:nn {##1} { #1 = {####1} } } }
389 }
390 }
391
392 \bool_if:NF \l_@@_alias_bool
393 { \@@_warning:nx {rename-feature-not-exist} {#1} }
394 }
395 % \end{macrocode}
396 % \end{macro}
397 %
398 % \begin{macro}{\aliasfontfeatureoption}
399 % \begin{macrocode}
400 \cs_new:Nn \@@_main_aliasfontfeatureoption:nnn
401 {
402 \bool_set_false:N \l_@@_alias_bool
403
404 \clist_map_inline:Nn \g_@@_all_keyval_modules_clist
405 {
406 \keys_if_exist:nnT { ##1 / #1 } {#2}
407 {
408 %<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2}
409 \bool_set_true:N \l_@@_alias_bool
410 \keys_define:nn { ##1 / #1 }
411 { #3 .code:n = { \keys_set:nn {##1} { #1 = {#2} } } }
412 }
413
414 \keys_if_exist:nnT { ##1 / #1 } {#2Reset}
415 {
416 %<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2Reset}
417 \keys_define:nn { ##1 / #1 }
418 { #3Reset .code:n = { \keys_set:nn {##1} { #1 = {#2Reset} } } }
419 }
420
421 \keys_if_exist:nnT { ##1 / #1 } {#2Off}
422 {
423 %<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2Off}
424 \keys_define:nn { ##1 / #1 }
425 { #3Off .code:n = { \keys_set:nn {##1} { #1 = {#2Off} } } }
426 }
427 }
428
429 \bool_if:NF \l_@@_alias_bool
430 { \@@_warning:nx {rename-feature-not-exist} {#1/#2} }
431 }
432 % \end{macrocode}
433 % \end{macro}
434 %
435 %
436 % \begin{macro}{\@@_main_DeclareFontExtensions:n}
437 % \begin{macrocode}
438 \cs_new:Nn \@@_main_DeclareFontExtensions:n
439 {
440 \clist_set:Nn \l_@@_extensions_clist { #1 }
441 }
442 % \end{macrocode}
443 % Defaults:
444 % \begin{macrocode}
445 \@@_main_DeclareFontExtensions:n {.otf,.ttf,.OTF,.TTF,.ttc,.TTC,.dfont}
446 % \end{macrocode}
447 % \end{macro}
448 %
449 %
450 % \subsection{High level conditionals}
451 %
452 %
453 % \begin{macro}{\IfFontFeatureActiveTF}
454 % \begin{macrocode}
455 \cs_new:Nn \@@_main_IfFontFeatureActiveTF:nnn
456 {
457 %<debug> \typeout{^^J:::::::::::::::::::::::::::::::::::::::::::::::}
458 %<debug> \typeout{:IfFontFeatureActiveTF \exp_not:n{{#1}{#2}{#3}}}
459 \@@_if_font_feature:nTF {#1} {#2} {#3}
460 }
461 % \end{macrocode}
462 %
463 % \begin{macrocode}
464 \prg_new_conditional:Nnn \@@_if_font_feature:n {TF}
465 {
466 \tl_gclear:N \g_@@_single_feat_tl
467 \group_begin:
468 \@@_font_suppress_not_found_error:
469 \@@_init:
470 \bool_set_true:N \l_@@_ot_bool
471 \bool_set_true:N \l_@@_never_check_bool
472 \bool_set_false:N \l_@@_firsttime_bool
473 \clist_clear:N \l_@@_fontfeat_clist
474 \@@_get_features:n {#1}
475 \group_end:
476
477 %<debug> \typeout{:::> \exp_not:N\g_@@_rawfeatures_sclist->~{\g_@@_rawfeatures_sclist}}
478 %<debug> \typeout{:::> \exp_not:N\g_@@_single_feat_tl->~{\g_@@_single_feat_tl}}
479
480 \tl_if_empty:NTF \g_@@_single_feat_tl { \prg_return_false: }
481 {
482 \exp_args:NV \fontspec_if_current_feature:nTF \g_@@_single_feat_tl
483 { \prg_return_true: } { \prg_return_false: }
484 }
485 }
486 % \end{macrocode}
487 % \end{macro}
488 %
489 %
490 % \subsection{\cs{oldstylenums} and \cs{liningnums}}
491 %
492 % \begin{macro}{\oldstylenums}
493 % \begin{macro}{\liningnums}
494 % This command needs a redefinition.
495 % And we may as well provide the reverse command.
496 % \begin{macrocode}
497 \cs_new_protected:Nn \@@_main_oldstylenums:n
498 {
499 \group_begin:
500 \addfontfeature{Numbers=OldStyle}
501 #1
502 \group_end:
503 }
504 \cs_new_protected:Nn \@@_main_liningnums:n
505 {
506 \group_begin:
507 \addfontfeature{Numbers=Lining}
508 #1
509 \group_end:
510 }
511 % \end{macrocode}
512 % \end{macro}
513 % \end{macro}
514 %
515 %
516 %
517 % \iffalse
518 % \begin{macrocode}
519 %</fontspec>
520 % \end{macrocode}
521 % \fi
522
523
524 \endinput
525
526 % /©
527 % ------------------------------------------------
528 % The FONTSPEC package <latex3.github.io/fontspec>
529 % ------------------------------------------------
530 % Copyright 2022-2025 The LaTeX project, LPPL "maintainer"
531 % Copyright 2004-2022 Will Robertson
532 % Copyright 2009-2015 Khaled Hosny
533 % Copyright 2013 Philipp Gesang
534 % Copyright 2013-2016 Joseph Wright
535 % ------------------------------------------------
536 % This package is free software and may be redistributed and/or modified under
537 % the conditions of the LaTeX Project Public License, version 1.3c or higher
538 % (your choice): <http://www.latex-project.org/lppl/>.
539 % ------------------------------------------------
540 % ©/