summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/fontspec/fontspec-code-user.dtx
blob: 7cbafbf8b0589a85f8a44782b00fb305ed029742 (plain)
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
%%^^A%%  fontspec-code-user.dtx -- part of FONTSPEC <wspr.io/fontspec>

% \section{User command internals}
% \label{sec:codeinternal}
%
% \iffalse
%    \begin{macrocode}
%<*fontspec>
%    \end{macrocode}
% \fi
%
%
% \subsection{Font selection}
%
% \begin{macro}{\@@_main_fontspec:nn}
%   This is the main command of the package that
%   selects fonts with various features. It takes two arguments: the
%   font name and the optional requested features of that
%   font.
%    \begin{macrocode}
\cs_new:Nn \@@_main_fontspec:nn
  {
    \fontspec_set_family:Nnn \f@family {#1} {#2}
    \fontencoding { \g_@@_nfss_enc_tl }
    \selectfont
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setmainfont}
%     The following three macros perform equivalent operations setting
%     the default font for a particular family: `roman', sans serif, or typewriter
%     (monospaced).
%
%     They end with |\normalfont| so that if they're
%     used in the document, the change registers immediately.
%    \begin{macrocode}
\cs_new:Nn \@@_main_setmainfont:nn
  {
    \fontspec_set_family:Nnn \l_@@_rmfamily_family_tl {#1} {#2}
    \tl_set_eq:NN \rmdefault \l_@@_rmfamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \rmfamily }
          {
            \exp_not:N \fontencoding { \g_@@_nfss_enc_tl }
            \exp_not:N \fontfamily { \exp_not:N \rmdefault }
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\rmdefault}
      { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
    \@@_setmainfont_hook:nn {#1} {#2}
    \normalfont
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setsansfont}
% Same as above.
%    \begin{macrocode}
\cs_new:Nn \@@_main_setsansfont:nn
  {
    \fontspec_set_family:Nnn \l_@@_sffamily_family_tl {#1} {#2}
    \tl_set_eq:NN \sfdefault \l_@@_sffamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \sffamily }
          {
            \exp_not:N \fontencoding { \g_@@_nfss_enc_tl }
            \exp_not:N \fontfamily { \exp_not:N \sfdefault }
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\sfdefault}
      { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
    \@@_setsansfont_hook:nn {#1} {#2}
    \normalfont
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setmonofont}
% Same as above.
%    \begin{macrocode}
\cs_new:Nn \@@_main_setmonofont:nn
  {
    \fontspec_set_family:Nnn \l_@@_ttfamily_family_tl {#1} {#2}
    \tl_set_eq:NN \ttdefault \l_@@_ttfamily_family_tl
    \use:x
      {
        \exp_not:n { \DeclareRobustCommand \ttfamily }
          {
            \exp_not:N \fontencoding { \g_@@_nfss_enc_tl }
            \exp_not:N \fontfamily { \exp_not:N \ttdefault }
            \exp_not:N \selectfont
          }
      }
    \str_if_eq:eeT {\familydefault} {\ttdefault}
      { \tl_set_eq:NN \encodingdefault \g_@@_nfss_enc_tl }
    \@@_setmonofont_hook:nn {#1} {#2}
    \normalfont
  }
%    \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\setmathrm}
% These commands are analogous to \cmd\setmainfont\ and others,
% but for selecting the font used for \cmd\mathrm, \etc. They
% can only be used in the preamble of the
% document. \cmd\setboldmathrm\ is used for specifying which
% fonts should be used in \cmd\boldmath.
%    \begin{macrocode}
\cs_new:Nn \@@_main_setmathrm:nn
  {
%<XE> \fontspec_gset_family:Nnn \g_@@_mathrm_tl {#1} {#2}
%<LU> \fontspec_gset_family:Nnn \g_@@_mathrm_tl {Renderer=Basic,#1} {#2}
    \@@_setmathrm_hook:nn {#1} {#2}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setboldmathrm}
%    \begin{macrocode}
\cs_new:Nn \@@_main_setboldmathrm:nn
  {
%<XE> \fontspec_gset_family:Nnn \g_@@_bfmathrm_tl {#1} {#2}
%<LU> \fontspec_gset_family:Nnn \g_@@_bfmathrm_tl {Renderer=Basic,#1} {#2}
    \@@_setboldmathrm_hook:nn {#1} {#2}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setmathsf}
%    \begin{macrocode}
\cs_new:Nn \@@_main_setmathsf:nn
  {
%<XE> \fontspec_gset_family:Nnn \g_@@_mathsf_tl {#1} {#2}
%<LU> \fontspec_gset_family:Nnn \g_@@_mathsf_tl {Renderer=Basic,#1} {#2}
    \@@_setmathsf_hook:nn {#1} {#2}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\setmathtt}
%    \begin{macrocode}
\cs_new:Nn \@@_main_setmathtt:nn
  {
%<XE> \fontspec_gset_family:Nnn \g_@@_mathtt_tl {#1} {#2}
%<LU> \fontspec_gset_family:Nnn \g_@@_mathtt_tl {Renderer=Basic,#1} {#2}
    \@@_setmathtt_hook:nn {#1} {#2}
  }
%    \end{macrocode}
% \end{macro}
%
% Hooks:
%    \begin{macrocode}
\cs_set_eq:NN \@@_setmainfont_hook:nn   \use_none:nn
\cs_set_eq:NN \@@_setsansfont_hook:nn   \use_none:nn
\cs_set_eq:NN \@@_setmonofont_hook:nn   \use_none:nn
\cs_set_eq:NN \@@_setmathrm_hook:nn     \use_none:nn
\cs_set_eq:NN \@@_setmathsf_hook:nn     \use_none:nn
\cs_set_eq:NN \@@_setmathtt_hook:nn     \use_none:nn
\cs_set_eq:NN \@@_setboldmathrm_hook:nn \use_none:nn
%    \end{macrocode}
%
% Hmm, this isn't necessary with \pkg{unicode-math}; oh well:
%    \begin{macrocode}
\@onlypreamble\setmathrm
\@onlypreamble\setboldmathrm
\@onlypreamble\setmathsf
\@onlypreamble\setmathtt
%    \end{macrocode}
%
% If the commands above are not executed, then \cmd\rmdefault\ (\etc)
% will be used.
%    \begin{macrocode}
\tl_gset:Nn \g_@@_mathrm_tl {\rmdefault}
\tl_gset:Nn \g_@@_mathsf_tl {\sfdefault}
\tl_gset:Nn \g_@@_mathtt_tl {\ttdefault}
%    \end{macrocode}
%
% \begin{macro}{\@@_main_newfontfamily:NnnN}
%   The inner fontspec workings define a font family, which is then used in a typical
%   NFSS \cmd\fontfamily\ declaration, saved in the macro name specified.
%   The fourth argument determines which xparse function to set the macro with (new/renew/etc).
%    \begin{macrocode}
\cs_new:Nn \@@_main_newfontfamily:NnnN
  {
    \fontspec_set_family:cnn { l_@@_ \cs_to_str:N #1 _family_tl } {#2} {#3}
    \use:x
      {
        \exp_not:N #4 \exp_not:N #1 {}
          {
            \exp_not:N \fontfamily { \use:c { l_@@_ \cs_to_str:N #1 _family_tl } }
            \exp_not:N \fontencoding { \g_@@_nfss_enc_tl }
            \exp_not:N \selectfont
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_main_newfontface:NnnN}
% \cmd\newfontface\ uses the fact that if the argument to \feat{BoldFont}, etc., is empty (\ie, |BoldFont={}|), then no bold font is searched for.
%    \begin{macrocode}
\cs_new:Nn \@@_main_newfontface:NnnN
  {
    \@@_main_newfontfamily:NnnN #1 { BoldFont={},ItalicFont={},SmallCapsFont={},#2 } {#3} #4
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Font feature selection}
%
% \begin{macro}{\@@_set_default_features:nn}
%    \begin{macrocode}
\cs_new:Nn \@@_set_default_features:nn
  {
    \IfBooleanTF {#1} \clist_gput_right:Nn \clist_gset:Nn
      \g_@@_default_fontopts_clist {#2}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_font_default_features:nnn}
% The optional argument |#2| specifies font identifier(s).
% Branch for either (a)~single token input such as \verb|\rmdefault|, or (b)~otherwise assume its a fontname.
% In that case, strip spaces and file extensions and lower-case to ensure consistency.
%    \begin{macrocode}
\cs_new:Nn \@@_set_font_default_features:nnn
  {
%<debug> \typeout{\unexpanded{_set_font_default_features:nnn:{#1}{#2}{#3}}}
    \clist_map_inline:nn {#2}
      {
        \tl_if_single:nTF {##1}
          { \tl_set:No \l_@@_tmp_tl { \cs:w l_@@_ \cs_to_str:N ##1 _family_tl\cs_end: } }
          { \@@_sanitise_fontname:Nn \l_@@_tmp_tl {##1} }

        \IfBooleanTF {#1}
          {
            \prop_get:NVNF \g_@@_fontopts_prop \l_@@_tmp_tl \l_@@_tmpb_tl
              { \tl_clear:N \l_@@_tmpb_tl }
            \tl_put_right:Nn \l_@@_tmpb_tl {#3,}
            \prop_gput:NVV   \g_@@_fontopts_prop \l_@@_tmp_tl \l_@@_tmpb_tl
          }
          {
            \tl_if_empty:nTF {#3}
              { \prop_gremove:NV \g_@@_fontopts_prop \l_@@_tmp_tl }
              { \prop_gput:NVn    \g_@@_fontopts_prop \l_@@_tmp_tl {#3,} }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\addfontfeatures}
%   In order to be able to extend the feature selection of
%   a given font, two things need to be known: the currently selected
%   features, and the currently selected font. Every time a font
%   family is created, this information is saved inside a control
%   sequence with the name of the font family itself.
%
%   This macro extracts this information, then appends the requested
%   font features to add to the already existing ones, and calls the
%   font again with the top level \cs{fontspec} command.
%
%   The default options are \emph{not} applied (which is why
%   \cs{g_fontspec_default_fontopts_tl} is emptied inside the group; this is allowed
%   as \cmd\l_fontspec_family_tl\ is globally defined in \cmd\@@_select_font_family:nn), so this
%   means that the only added features to the font are strictly those
%   specified by this command.
%
%   \cs{addfontfeature} is defined as an alias, as I found that I
%   often typed this instead when adding only a single font feature.
%    \begin{macrocode}
\cs_new:Nn \@@_main_addfontfeatures:n
  {
%<debug> \typeout{^^J::::::::::::::::::::::::::::::::::^^J: addfontfeatures}
    \fontspec_if_fontspec_font:TF
      {
        \group_begin:
          \keys_set_known:nnN {fontspec-addfeatures} {#1} \l_@@_tmp_tl
          \prop_get:cnN {g_@@_fontinfo_ \f@family _prop} {options}  \l_@@_options_tl
          \prop_get:cnN {g_@@_fontinfo_ \f@family _prop} {fontname} \l_@@_fontname_tl
          \bool_set_true:N \l_@@_disable_defaults_bool
%<debug> \typeout{ \@@_select_font_family:nn { \l_@@_options_tl , #1 } {\l_@@_fontname_tl} }
          \use:x
            {
              \@@_select_font_family:nn
                { \l_@@_options_tl , #1 } {\l_@@_fontname_tl}
            }
        \group_end:
        \fontfamily \g_@@_nfss_family_tl \selectfont
      }
      {
        \@@_warning:nx {addfontfeatures-ignored} {#1}
      }
    \ignorespaces
  }
%    \end{macrocode}
% \end{macro}
%
% \subsection{Defining new font features}
%
% \begin{macro}{\newfontfeature}
%   \cs{newfontfeature} takes two arguments: the name of the feature
%   tag by which to reference it, and the string that is used to
%   select the font feature.
%    \begin{macrocode}
\cs_new:Nn \@@_main_newfontfeature:nn
  {
    \keys_define:nn { fontspec }
      {
        #1 .code:n = { \@@_update_featstr:n {#2} }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\newAATfeature}
% This command assigns a new AAT feature by its code (|#2|,|#3|) to a new name (|#1|).
% Better than \cmd\newfontfeature\ because it checks if the feature exists in the
% font it's being used for.
%    \begin{macrocode}
\cs_new:Nn \@@_main_newAATfeature:nnnn
  {
    \keys_if_exist:nnF { fontspec } {#1}
      { \@@_define_aat_feature_group:n {#1} }

    \keys_if_choice_exist:nnnT {fontspec} {#1} {#2}
      { \@@_warning:nxx {feature-option-overwrite} {#1} {#2} }

    \@@_define_aat_feature:nnnn {#1}{#2}{#3}{#4}
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newopentypefeature}
% This command assigns a new OpenType feature by its abbreviation (|#2|) to a new name (|#1|).
% Better than \cmd\newfontfeature\ because it checks if the feature exists in the
% font it's being used for.
%    \begin{macrocode}
\cs_new:Nn \@@_main_newopentypefeature:nnn
  {
    \keys_if_exist:nnF { fontspec / options } {#1}
      { \@@_define_opentype_feature_group:n {#1} }

    \keys_if_choice_exist:nnnT {fontspec} {#1} {#2}
      { \@@_warning:nxx {feature-option-overwrite} {#1} {#2} }

    \exp_args:Nnnx \@@_define_opentype_feature:nnnnn
      {#1} {#2} { \@@_strip_plus_minus:n {#3} } {#3} {}
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\cs_new:Nn \@@_strip_plus_minus:n { \@@_strip_plus_minus_aux:Nq #1 \q_nil }
\cs_new:Npn \@@_strip_plus_minus_aux:Nq #1#2 \q_nil
  {
    \str_case:nnF {#1} { {+} {#2} {-} {#2} } {#1#2}
  }
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\aliasfontfeature}
% User commands for renaming font features and font feature options.
%    \begin{macrocode}
\cs_new:Nn \@@_main_aliasfontfeature:nn
  {
%<debug> \typeout{::::::::::::::::::::^^J:: aliasfontfeature{#1}{#2}}
    \bool_set_false:N \l_@@_alias_bool

    \clist_map_inline:Nn \g_@@_all_keyval_modules_clist
      {
        \keys_if_exist:nnT {##1} {#1}
          {
%<debug> \typeout{:::: Key~exists~##1~/~#1}
            \bool_set_true:N \l_@@_alias_bool
            \keys_define:nn {##1}
              { #2 .code:n = { \keys_set:nn {##1} { #1 = {####1} } } }
          }
      }

    \bool_if:NF \l_@@_alias_bool
      { \@@_warning:nx {rename-feature-not-exist} {#1} }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\aliasfontfeatureoption}
%    \begin{macrocode}
\cs_new:Nn \@@_main_aliasfontfeatureoption:nnn
  {
    \bool_set_false:N \l_@@_alias_bool

    \clist_map_inline:Nn \g_@@_all_keyval_modules_clist
      {
        \keys_if_exist:nnT { ##1 / #1 } {#2}
          {
%<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2}
            \bool_set_true:N \l_@@_alias_bool
            \keys_define:nn { ##1 / #1 }
              { #3 .code:n = { \keys_set:nn {##1} { #1 = {#2} } } }
          }

       \keys_if_exist:nnT { ##1 / #1 } {#2Reset}
         {
%<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2Reset}
            \keys_define:nn { ##1 / #1 }
              { #3Reset .code:n = { \keys_set:nn {##1} { #1 = {#2Reset} } } }
         }

       \keys_if_exist:nnT { ##1 / #1 } {#2Off}
         {
%<debug> \typeout{:::: Keyval~exists~##1~/~#1~=~#2Off}
            \keys_define:nn { ##1 / #1 }
              { #3Off .code:n = { \keys_set:nn {##1} { #1 = {#2Off} } } }
         }
     }

    \bool_if:NF \l_@@_alias_bool
      { \@@_warning:nx {rename-feature-not-exist} {#1/#2} }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@@_main_DeclareFontExtensions:n}
%    \begin{macrocode}
\cs_new:Nn \@@_main_DeclareFontExtensions:n
  {
    \clist_set:Nn \l_@@_extensions_clist { #1 }
  }
%    \end{macrocode}
% Defaults:
%    \begin{macrocode}
\@@_main_DeclareFontExtensions:n {.otf,.ttf,.OTF,.TTF,.ttc,.TTC,.dfont}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{High level conditionals}
%
%
% \begin{macro}{\IfFontFeatureActiveTF}
%    \begin{macrocode}
\cs_new:Nn \@@_main_IfFontFeatureActiveTF:nnn
  {
%<debug> \typeout{^^J:::::::::::::::::::::::::::::::::::::::::::::::}
%<debug> \typeout{:IfFontFeatureActiveTF \exp_not:n{{#1}{#2}{#3}}}
    \@@_if_font_feature:nTF {#1} {#2} {#3}
  }
%    \end{macrocode}
%
%    \begin{macrocode}
\prg_new_conditional:Nnn \@@_if_font_feature:n {TF}
  {
    \tl_gclear:N \g_@@_single_feat_tl
    \group_begin:
      \@@_font_suppress_not_found_error:
      \@@_init:
      \bool_set_true:N \l_@@_ot_bool
      \bool_set_true:N \l_@@_never_check_bool
      \bool_set_false:N \l_@@_firsttime_bool
      \clist_clear:N \l_@@_fontfeat_clist
      \@@_get_features:n {#1}
    \group_end:

%<debug> \typeout{:::> \exp_not:N\g_@@_rawfeatures_sclist->~{\g_@@_rawfeatures_sclist}}
%<debug> \typeout{:::> \exp_not:N\g_@@_single_feat_tl->~{\g_@@_single_feat_tl}}

    \tl_if_empty:NTF \g_@@_single_feat_tl { \prg_return_false: }
      {
        \exp_args:NV \fontspec_if_current_feature:nTF \g_@@_single_feat_tl
          { \prg_return_true: } { \prg_return_false: }
      }
  }
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{\cs{oldstylenums} and \cs{liningnums}}
%
% \begin{macro}{\oldstylenums}
% \begin{macro}{\liningnums}
% This command needs a redefinition.
% And we may as well provide the reverse command.
%    \begin{macrocode}
\cs_new_protected:Nn \@@_main_oldstylenums:n
  {
    \group_begin:
      \addfontfeature{Numbers=OldStyle}
      #1
    \group_end:
  }
\cs_new_protected:Nn \@@_main_liningnums:n
  {
    \group_begin:
      \addfontfeature{Numbers=Lining}
      #1
    \group_end:
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
%
% \iffalse
%    \begin{macrocode}
%</fontspec>
%    \end{macrocode}
% \fi


\endinput

% /©
% ------------------------------------------------
% The FONTSPEC package  <wspr.io/fontspec>
% ------------------------------------------------
% Copyright  2004-2019  Will Robertson, LPPL "maintainer"
% Copyright  2009-2015  Khaled Hosny
% Copyright  2013       Philipp Gesang
% Copyright  2013-2016  Joseph Wright
% ------------------------------------------------
% This package is free software and may be redistributed and/or modified under
% the conditions of the LaTeX Project Public License, version 1.3c or higher
% (your choice): <http://www.latex-project.org/lppl/>.
% ------------------------------------------------
% ©/