-
Notifications
You must be signed in to change notification settings - Fork 34
/
fontspec-code-internal.dtx
1303 lines (1253 loc) · 42.7 KB
/
fontspec-code-internal.dtx
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%^^A%% fontspec-code-internal.dtx -- part of FONTSPEC <latex3.github.io/fontspec>
% \section{Internals}
%
% \iffalse
% \begin{macrocode}
%<*fontspec>
% \end{macrocode}
% \fi
%
% \subsection{The main function for setting fonts}
%
% \begin{macro}{\@@_select_font_family:nn}
% This is the command that defines font families for use, the underlying
% procedure of all \cmd\fontspec-like commands. Given a
% list of font features (|#1|) for a requested font (|#2|),
% it will define an NFSS
% family for that font and put the family name (globally) into \cs{l_fontspec_family_tl}.
% The \TeX\ `\cs{font}' command is (globally) stored in \cs{l_fontspec_font}.
%
% This macro does its processing inside a group to attempt to restrict the scope of its internal processing.
% This works to some degree to insulate the internal commands from having to be manually cleared.
%
% Some often-used variables to know about:
% \begin{itemize}
% \item \cmd{\l_fontspec_fontname_tl} is used as the generic name of the font being defined.
% \item \cmd{\l_@@_fontid_tl} is the unique identifier of the font with all its features.
% \item \cmd{\l_@@_fontname_up_tl} is the font specifically to be used as the upright font.
% \item \cmd{\l_@@_basename_tl} is the (immutable) original argument used for |*|-replacing.
% \item \cmd{\l_fontspec_font} is the plain \TeX{} font of the upright font requested.
% \end{itemize}
% \begin{macrocode}
\cs_new_protected:Nn \@@_select_font_family:nn
{
%<debug>\typeout{^^J^^J::::::::::::::::::::::::::::::^^J:: fontspec_select:nn~ {#1}~ {#2} }
\group_begin:
\@@_font_suppress_not_found_error:
\@@_init:
\@@_sanitise_fontname:Nn \l_fontspec_fontname_tl {#2}
\tl_set_eq:NN \l_@@_fontname_up_tl \l_fontspec_fontname_tl
\tl_set_eq:NN \l_@@_basename_tl \l_fontspec_fontname_tl
%<debug>\typeout{^^J::::::::::::::: l_fontspec_fontname_tl~ =~ \l_fontspec_fontname_tl }
\@@_if_detect_external:nT {#2}
{ \keys_set:nn {fontspec-preparse-external} {Path} }
\keys_set_known:nn {fontspec-preparse-cfg} {#1}
\@@_init_ttc:n {#2}
\@@_load_external_fontoptions:N \l_fontspec_fontname_tl
\@@_extract_all_features:n {#1}
\tl_set:Nx \l_@@_fontid_tl { \tl_to_str:N \l_fontspec_fontname_tl-:-\tl_to_str:N \l_@@_all_features_clist }
%<debug>\typeout{fontid: \l_@@_fontid_tl}
\@@_preparse_features:
%<debug>\typeout{^^J::::::::::::::: l_fontspec_fontname_tl~ =~ \l_fontspec_fontname_tl }
%<debug>\typeout{::::::::::::::: _fontname_up_tl~ =~ \l_@@_fontname_up_tl }
%<debug>\typeout{::::::::::::::: l_@@_extension_tl~ =~ \l_@@_extension_tl }
\@@_load_font:
\@@_set_scriptlang:
\@@_get_features:n {}
\bool_set_false:N \l_@@_firsttime_bool
\@@_save_family_needed:nTF {#2}
{
\@@_save_family:nn {#1} {#2}
%<debug>\@@_warning:nxx {defining-font} {#1} {#2}
}
{
%<debug>\typeout{Font~ family~ already~ defined.}
}
\group_end:
\tl_set_eq:NN \l_fontspec_family_tl \g_@@_nfss_family_tl
%<debug>\typeout{::::::::::::::::::::::::::::::}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_select:nn}
% This old name has been used by 3rd party packages so for compatibility:
% \begin{macrocode}
\cs_set_eq:NN \fontspec_select:nn \@@_select_font_family:nn %% deprecated, for compatibility only
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_sanitise_fontname:Nn}
% Assigns font name |#2| to token list variable |#1| and strips extension(s) from it in the case of an external font.
% \begin{macrocode}
\cs_new:Nn \@@_sanitise_fontname:Nn
{
\tl_set:Nx #1 {#2}
\tl_trim_spaces:N #1
\@@_process_ext:N #1
}
\cs_new:Nn \@@_process_ext:N
{
\clist_map_inline:Nn \l_@@_extensions_clist
{
\tl_if_in:NnT #1 {##1}
{
%<debug> \typeout{::@@_process_ext:N~ --~ Removing~ EXT:~ ##1}
\tl_remove_once:Nn #1 {##1}
\tl_set:Nn \l_@@_extension_tl {##1}
\@@_font_is_file:
\clist_map_break:
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_if_detect_external:nT}
% Check if either the fontname ends with a known font extension.
% \begin{macrocode}
\prg_new_conditional:Nnn \@@_if_detect_external:n {T}
{
%<debug> \typeout{:: @@_if_detect_external:n { \exp_not:n {#1} } }
\clist_map_inline:Nn \l_@@_extensions_clist
{
\bool_set_false:N \l_@@_tmpa_bool
\exp_args:Nx % <- this should be handled earlier
\tl_if_in:nnT {#1 <= end_of_string} {##1 <= end_of_string}
{ \bool_set_true:N \l_@@_tmpa_bool \clist_map_break: }
}
\bool_if:NTF \l_@@_tmpa_bool \prg_return_true: \prg_return_false:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_init_ttc:n}
% For TTC fonts we assume they will be loading the italic/bold fonts from the same file,
% so prepopulate the fontnames to avoid needing to do it manually.
% \begin{macrocode}
\cs_new:Nn \@@_init_ttc:n
{
\str_if_eq:eeT { \str_lowercase:f {\l_@@_extension_tl} } {.ttc}
{
\tl_set_eq:NN \l_@@_fontname_it_tl \l_fontspec_fontname_tl
\tl_set_eq:NN \l_@@_fontname_bf_tl \l_fontspec_fontname_tl
\tl_set_eq:NN \l_@@_fontname_bfit_tl \l_fontspec_fontname_tl
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_external_fontoptions:N}
% Load a possible \texttt{.fontspec} font configuration file.
% This file could set font-specific options for the font about to be loaded.
% The parameter should be a token list containing a sanitised fontname.
% In the past this used a space-stripped version of the name, so we check for the file
% both with and without spaces to load it.
% \begin{macrocode}
\cs_new:Nn \@@_load_external_fontoptions:N
{
\bool_if:NT \l_@@_fontcfg_bool
{
%<debug> \typeout{:: @@_load_external_fontoptions:N \exp_not:N #1 }
\tl_set:Nx \l_@@_ext_filename_tl {#1.fontspec}
\tl_remove_all:Nn \l_@@_ext_filename_tl {~}
\prop_if_in:NVF \g_@@_fontopts_prop #1
{
\exp_args:No \file_if_exist:nTF { \l_@@_ext_filename_tl }
{
\file_input:n { \l_@@_ext_filename_tl }
}
{
\tl_remove_all:Nn \l_@@_ext_filename_tl {~}
\exp_args:No \file_if_exist:nT { \l_@@_ext_filename_tl }
{ \file_input:n { \l_@@_ext_filename_tl } }
}
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_extract_all_features:}
% \begin{macrocode}
\cs_new:Nn \@@_extract_all_features:n
{
%<debug> \typeout{:: @@_extract_all_features:n { \unexpanded {#1} } }
\bool_if:NTF \l_@@_disable_defaults_bool
{
\clist_set:Nx \l_@@_all_features_clist {#1}
}
{
\prop_get:NVNF \g_@@_fontopts_prop \l_fontspec_fontname_tl \l_@@_fontopts_clist
{ \clist_clear:N \l_@@_fontopts_clist }
\prop_get:NVNF \g_@@_fontopts_prop \l_@@_family_label_tl \l_@@_family_fontopts_clist
{ \clist_clear:N \l_@@_family_fontopts_clist }
\tl_clear:N \l_@@_family_label_tl
\clist_set:Nx \l_@@_all_features_clist
{
\g_@@_default_fontopts_clist,
\l_@@_family_fontopts_clist,
\l_@@_fontopts_clist,
#1
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_preparse_features:}
% \darg{feature options}
% \darg{font name}
% Perform the (multi-step) feature parsing process.
%
% Convert the requested features to font definition
% strings. First the features are parsed for information about font
% loading (whether it's a named font or external font, etc.), and then
% information is extracted for the names of the other shape fonts.
% \begin{macrocode}
\cs_new:Nn \@@_preparse_features:
{
%<debug> \typeout{:: @@_preparse_features:}
% \end{macrocode}
% Detect if external fonts are to be used, possibly automatically, and
% parse fontspec features for bold/italic fonts and their features.
% \begin{macrocode}
\@@_keys_set_known:nxN {fontspec-preparse-external}
{ \l_@@_all_features_clist }
\l_@@_keys_leftover_clist
% \end{macrocode}
% When \cmd{\l_fontspec_fontname_tl} is augmented with a prefix or whatever to create
% the name of the upright font (\cmd{\l_@@_fontname_up_tl}), this latter is the new `general
% font name' to use.
% \begin{macrocode}
\tl_set_eq:NN \l_fontspec_fontname_tl \l_@@_fontname_up_tl
\@@_keys_set_known:nxN {fontspec-renderer} {\l_@@_keys_leftover_clist}
\l_@@_keys_leftover_clist
\@@_keys_set_known:nxN {fontspec-preparse} {\l_@@_keys_leftover_clist}
\l_@@_fontfeat_clist
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_load_font:}
% \begin{macrocode}
\cs_new:Nn \@@_load_font:
{
%<debug>\typeout{:: @@_load_font}
\@@_sanitise_fontname:Nn \l_@@_fontname_up_tl { \l_@@_fontname_up_tl }
%<debug>\typeout{Set~ base~ font~ for~ preliminary~ analysis:~ "\l_@@_fontname_up_tl"~ with~ features~ "\l_@@_pre_feat_sclist" }
\@@_primitive_font_set:NnnF \l_@@_test_font
{ \@@_construct_font_call:nn { \l_@@_fontname_up_tl } { \l_@@_pre_feat_sclist } }
{ \f@size pt - 2sp }
{ \@@_error:nx {font-not-found} {\l_@@_fontname_up_tl} }
%<debug>\typeout{Set~ base~ font~ properly: \@@_construct_font_call:nn { \l_@@_fontname_up_tl } {} }
\@@_set_font_type:N \l_@@_test_font
\@@_primitive_font_gset:Onn \l_@@_fontface_cs_tl
{ \@@_construct_font_call:nn { \l_@@_fontname_up_tl } { \l_@@_pre_feat_sclist } }
{ \f@size pt + 2sp }
\l_@@_fontface_cs_tl % this is necessary for LuaLaTeX to check the scripts properly
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_construct_font_call:nn}
% Constructs the complete font invocation.
% \darg{Base name}
% \darg{Extension}
% \darg{TTC Index}
% \darg{Renderer}
% \darg{Optical size}
% \darg{Font features}
% We check if \meta{Font features} are empty and if so don't add in the separator colon.
% \begin{macrocode}
\cs_new:Nn \@@_construct_font_call:nnnnnn
{
%<XE> " \@@_fontname_wrap:n { #1 #2 #3 }
%<LU> " \@@_fontname_wrap:n { #1 #2 } #3
#4 #5
\str_if_eq:eeF {#6}{} {:#6} "
}
% \end{macrocode}
% In practice, we don't use the six-argument version, since most arguments are constructed on-the-fly:
% \begin{macrocode}
\cs_new:Nn \@@_construct_font_call:nn
{
\@@_construct_font_call:nnnnnn
{#1}
\l_@@_extension_tl
\l_@@_ttc_index_tl
\l_@@_renderer_tl
\l_@@_optical_size_tl
{#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_font_is_file:,\@@_font_is_name:}
% The \cs{@@_fontname_wrap:n} command takes the font name and either passes it through unchanged or wraps it in the syntax for loading a font `by filename'.
% For Lua\TeX\ there are two kinds kinds of filename based loading supported: Regular filename lookups which include system fonts and lookups restricted to kpse.
% \begin{macrocode}
\cs_new:Nn \@@_font_is_name:
{
%<XE> \cs_set_eq:NN \@@_fontname_wrap:n \use:n
%<LU> \cs_set:Npn \@@_fontname_wrap:n ##1 { name: ##1 }
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new:Nn \@@_font_is_file:
{
%<debug> \typeout{:: _font_is_file:}
\bool_set_true:N \l_@@_external_bool
\bool_lazy_and:nnTF { \l_@@_external_kpse_bool } { \tl_if_empty_p:N \l_@@_font_path_tl }
{
\cs_set:Npn \@@_fontname_wrap:n ##1 { kpse: ##1 }
}
{
\cs_set:Npn \@@_fontname_wrap:n ##1 { [ \l_@@_font_path_tl ##1 ] }
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_scriptlang:}
% Only necessary for OpenType fonts.
% First check if the font supports scripts, then apply defaults if
% none are explicitly requested. Similarly with the language settings.
% \begin{macrocode}
\cs_new:Nn \@@_set_scriptlang:
{
%<debug> \typeout{:: _set_scriptlang:}
\bool_if:NT \l_@@_firsttime_bool
{
\tl_if_empty:NF \l_@@_script_name_tl
{
%<debug> \typeout{:::: Script=\l_@@_script_name_tl, Language=\l_@@_lang_name_tl}
\keys_set:ne {fontspec-opentype} {Script=\l_@@_script_name_tl}
\keys_set:ne {fontspec-opentype} {Language=\l_@@_lang_name_tl}
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_get_features:Nn}
% This macro is a wrapper for |\keys_set:nn| which expands and adds a
% default specification to the original passed options. It begins by
% initialising the commands used to hold font-feature specific
% strings.
% Its argument is any additional features to prepend to the default.
%
% Do not set the colour if not explicitly spec'd else \verb|\color| (using
% specials) will not work.
% \begin{macrocode}
\cs_new:Nn \@@_get_features:n
{
%<debug> \typeout{:: @@_get_features:Nn { \exp_not:n {#1} } }
\@@_init_fontface:
\@@_keys_set_known:nxN {fontspec-renderer} {\l_@@_fontfeat_clist,#1}
\l_@@_keys_leftover_clist
\@@_keys_set_known:nxN {fontspec} {\l_@@_keys_leftover_clist} \l_@@_keys_leftover_clist
%<*XE>
\bool_if:NTF \l_@@_ot_bool
{
%<debug> \typeout{::: Setting~ keys~ for~ OpenType~ font~ features:~"\l_@@_keys_leftover_clist"}
\keys_set_known:nV {fontspec-opentype} \l_@@_keys_leftover_clist
}
{
%<debug> \typeout{::: Setting~ keys~ for~ AAT/Graphite~ font~ features:~"\l_@@_keys_leftover_clist"}
\bool_if:nT { \l_@@_atsui_bool || \l_@@_graphite_bool }
{ \keys_set_known:nV {fontspec-aat} \l_@@_keys_leftover_clist }
}
%</XE>
%<*LU>
%<debug> \typeout{::: Setting~ keys~ for~ OpenType~ font~ features:~"\l_@@_keys_leftover_clist"}
\keys_set_known:nV {fontspec-opentype} \l_@@_keys_leftover_clist
%</LU>
\tl_if_empty:NF \l_@@_mapping_tl
{ \@@_update_featstr:n { mapping = \l_@@_mapping_tl } }
\str_if_eq:eeF { \l_@@_hexcol_tl \l_@@_opacity_tl }
{ \c_@@_hexcol_tl \c_@@_opacity_tl }
%<XE> { \@@_update_featstr:n { color = \l_@@_hexcol_tl\l_@@_opacity_tl } }
%<LU> { \@@_update_featstr:n { color = {\l_@@_hexcol_tl\l_@@_opacity_tl} } }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_save_family_needed:nTF}
% Check if the family is unique and, if so, save its information.
% (\cs{addfontfeature} and other macros use this data.)
% Then the font family and its shapes are defined in the NFSS.
%
% Now we have a unique (in fact, too unique!) string that contains
% the family name and every option in abbreviated form. This is used
% with a counter to create a simple NFSS family name for the font we're
% selecting.
%
% \begin{macrocode}
\prg_new_conditional:Nnn \@@_save_family_needed:n { TF }
{
%<debug> \typeout{save~ family:~ #1}
%<debug> \typeout{== fontid_tl: "\l_@@_fontid_tl".}
\tl_if_empty:NTF \l_@@_nfss_fam_tl
{
\prop_get:NVNTF \g_@@_fontid_family_prop \l_@@_fontid_tl \l_@@_tmp_tl
{
\tl_gset_eq:NN \g_@@_nfss_family_tl \l_@@_tmp_tl
\prg_return_false:
}
{
\tl_set:Nx \l_@@_tmp_tl {#1}
\tl_remove_all:Nn \l_@@_tmp_tl { ~ }
\@@_save_fontid_family:VV \l_@@_fontid_tl \l_@@_tmp_tl
\prg_return_true:
}
}
{
\tl_gset_eq:NN \g_@@_nfss_family_tl \l_@@_nfss_fam_tl
\cs_undefine:c { g_@@_fontinfo_ \g_@@_nfss_family_tl _prop }
\prg_return_true:
}
}
% \end{macrocode}
% \begin{macrocode}
\cs_new:Nn \@@_save_fontid_family:nn
{
\prop_get:NnNTF \g_@@_family_int_prop {#2} \l_@@_tmp_tl
{
\tl_set:Nx \l_@@_tmp_tl
{ \int_eval:n { \l_@@_tmp_tl + 1 } }
}
{ \tl_set:Nn \l_@@_tmp_tl { 0 } }
\prop_gput:NnV \g_@@_family_int_prop {#2} \l_@@_tmp_tl
\tl_gset:Nx \g_@@_nfss_family_tl { #2 ( \l_@@_tmp_tl ) }
\prop_gput:NnV \g_@@_fontid_family_prop {#1} \g_@@_nfss_family_tl
}
\cs_generate_variant:Nn \@@_save_fontid_family:nn { VV }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_save_family:nn}
% Saves the relevant font information for future processing.
% \begin{macrocode}
\cs_new:Nn \@@_save_family:nn
{
\@@_save_fontinfo:n {#2}
\@@_find_autofonts:
\DeclareFontFamily{\g_@@_nfss_enc_tl}{\g_@@_nfss_family_tl}{}
\@@_set_faces:
\@@_info:nxx {defining-font} {#1} {#2}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_save_fontinfo:n}
% Saves the relevant font information for future processing.
% \begin{macrocode}
\cs_new:Nn \@@_save_fontinfo:n
{
\prop_new:c {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop}
\prop_gput:cnx {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {fontname} { #1 }
\prop_gput:cnx {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {options} { \l_@@_all_features_clist }
\prop_gput:cnx {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {fontdef}
{
\@@_construct_font_call:nn {\l_fontspec_fontname_tl}
{ \l_@@_pre_feat_sclist \g_@@_rawfeatures_sclist \@@_get_variations: }
}
\prop_gput:cnV {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {script-num} \l_@@_script_int
\prop_gput:cnV {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {lang-num} \l_@@_language_int
\prop_gput:cnV {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {script-tag} \l_@@_script_tl
\prop_gput:cnV {g_@@_fontinfo_ \g_@@_nfss_family_tl _prop} {lang-tag} \l_@@_lang_tl
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Setting font shapes in a family}
%
% All NFSS specifications take their default values, so if any of them
% are redefined, the shapes will be selected to fit in with the
% current state. For example, if \cmd\bfdefault\ is redefined to |b|, all
% bold shapes defined by this package will also be assigned to |b|.
%
% The combination shapes are searched first because they use information that may be redefined in the single cases.
% E.g., if no bold font is specified then |set_autofont| will attempt to set it.
% This has subtle/small ramifications on the logic of choosing the bold italic font.
%
% \begin{macro}{\@@_find_autofonts:}
% \begin{macrocode}
\cs_new:Nn \@@_find_autofonts:
{
\bool_if:nF {\l_@@_noit_bool || \l_@@_nobf_bool}
{
\@@_set_autofont:Nnn \l_@@_fontname_bfit_tl {\l_@@_fontname_it_tl} {/B}
\@@_set_autofont:Nnn \l_@@_fontname_bfit_tl {\l_@@_fontname_bf_tl} {/I}
\@@_set_autofont:Nnn \l_@@_fontname_bfit_tl {\l_fontspec_fontname_tl} {/BI}
}
\bool_if:NF \l_@@_nobf_bool
{
\@@_set_autofont:Nnn \l_@@_fontname_bf_tl {\l_fontspec_fontname_tl} {/B}
}
\bool_if:NF \l_@@_noit_bool
{
\@@_set_autofont:Nnn \l_@@_fontname_it_tl {\l_fontspec_fontname_tl} {/I}
}
\@@_set_autofont:Nnn \l_@@_fontname_bfsl_tl {\l_@@_fontname_sl_tl} {/B}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_faces:}
% \begin{macrocode}
\cs_new:Nn \@@_set_faces:
{
\@@_add_nfssfont:nnnn \mddefault \shapedefault \l_fontspec_fontname_tl \l_@@_fontfeat_up_clist
\@@_add_nfssfont:nnnn \bfdefault \shapedefault \l_@@_fontname_bf_tl \l_@@_fontfeat_bf_clist
\@@_add_nfssfont:nnnn \mddefault \itdefault \l_@@_fontname_it_tl \l_@@_fontfeat_it_clist
\@@_add_nfssfont:nnnn \mddefault \sldefault \l_@@_fontname_sl_tl \l_@@_fontfeat_sl_clist
\@@_add_nfssfont:nnnn \mddefault \swdefault \l_@@_fontname_sw_tl \l_@@_fontfeat_sw_clist
\@@_add_nfssfont:nnnn \bfdefault \itdefault \l_@@_fontname_bfit_tl \l_@@_fontfeat_bfit_clist
\@@_add_nfssfont:nnnn \bfdefault \sldefault \l_@@_fontname_bfsl_tl \l_@@_fontfeat_bfsl_clist
\@@_add_nfssfont:nnnn \bfdefault \swdefault \l_@@_fontname_bfsw_tl \l_@@_fontfeat_bfsw_clist
\prop_map_inline:Nn \l_@@_nfssfont_prop { \@@_set_faces_aux:nnnnn ##2 }
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new:Nn \@@_set_faces_aux:nnnnn
{
%<debug> \typeout{:: @@_set_faces_aux:nnnnn \exp_not:n { {#1} {#2} {#3} {#4} {#5} } }
\fontspec_complete_fontname:Nn \l_@@_curr_fontname_tl {#3}
\@@_make_font_shapes:Nnnnn \l_@@_curr_fontname_tl {#1} {#2} {#4} {#5}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fontspec_complete_fontname:Nn}
% This macro defines |#1| as the input with any |*| tokens of its input
% replaced by the font name. This lets us define supplementary fonts in full
% (``\texttt{Baskerville Semibold}'') or in abbreviation (``\texttt{* Semibold}'').
% \begin{macrocode}
\cs_new:Nn \fontspec_complete_fontname:Nn
{
\tl_set:Nx #1 {#2}
\tl_if_in:NnF \l_fontspec_fontname_tl {*}
{
\tl_replace_all:Nne #1 {*} {\l_@@_basename_tl}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_add_nfssfont:nnnn}
% \darg{series}
% \darg{shape}
% \darg{fontname}
% \darg{fontspec features}
% \begin{macrocode}
\cs_new:Nn \@@_add_nfssfont:nnnn
{
\tl_set:Nx \l_@@_this_font_tl {#3}
\tl_if_empty:eTF {#4}
{ \clist_set:Nn \l_@@_sizefeat_clist {Size={-}} }
{ \@@_keys_set_known:nxN {fontspec-preparse-nested} {#4} \l_@@_tmp_tl }
\tl_if_empty:NF \l_@@_this_font_tl
{
\prop_put:Nee \l_@@_nfssfont_prop {#1/#2}
{ {#1}{#2}{\l_@@_this_font_tl}{#4}{\l_@@_sizefeat_clist} }
}
}
% \end{macrocode}
% \end{macro}
%
%
% \subsubsection{Fonts}
%
% \begin{macro}{\@@_set_font_type:N}
% Now check if the font is to be rendered with \ATSUI\ or Harfbuzz. This will either
% be automatic (based on the font type), or specified by the user via a font feature.
%
% This macro sets booleans
% accordingly depending if the font in \cmd\l_fontspec_test_font\ is an \AAT\
% font or an OpenType font or a font with feature axes (either \AAT\ or
% Multiple Master), respectively.
% \begin{macrocode}
\cs_new:Nn \@@_set_font_type:N
{
%<debug> \typeout{:: @@_set_font_type:}
%<*XE>
\bool_set_false:N \l_@@_tfm_bool
\bool_set_false:N \l_@@_atsui_bool
\bool_set_false:N \l_@@_ot_bool
\bool_set_false:N \l_@@_mm_bool
\bool_set_false:N \l_@@_graphite_bool
\ifcase\XeTeXfonttype #1
%<debug> \typeout{:::: TFM}
\bool_set_true:N \l_@@_tfm_bool
\or
%<debug> \typeout{:::: AAT}
\bool_set_true:N \l_@@_atsui_bool
\tl_if_empty:NT \l_@@_renderer_tl { \tl_set:Nn \l_@@_renderer_tl {/AAT} }
\ifnum\XeTeXcountvariations #1 > 0\relax
%<debug> \typeout{:::: MM}
\bool_set_true:N \l_@@_mm_bool
\fi
\or
%<debug> \typeout{:::: OpenType}
\bool_set_true:N \l_@@_ot_bool
\tl_if_empty:NT \l_@@_renderer_tl { \tl_set:Nn \l_@@_renderer_tl {/OT} }
\or
%<debug> \typeout{:::: Graphite}
\bool_set_true:N \l_@@_graphite_bool
\tl_if_empty:NT \l_@@_renderer_tl { \tl_set:Nn \l_@@_renderer_tl {/GR} }
\fi
%</XE>
% \end{macrocode}
% If automatic, the \cmd{\l_@@_renderer_tl} token list will still be
% empty (other suffices that could be added will be later in the feature
% processing), and if it is indeed still empty, assign it a value so that the
% other weights of the font are specifically loaded with the same renderer.
%
% LuaTeX only supports one:
% \begin{macrocode}
%<*LU>
\bool_set_true:N \l_@@_ot_bool
%</LU>
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_set_autofont:Nnn}
% \darg{Font name tl}
% \darg{Base font name}
% \darg{Font name modifier}
%
% This function looks for font with \meta{name} and \meta{modifier} |#2#3|, and if found (i.e., different to font with name |#2|) stores it in tl |#1|. A modifier is something like |/B| to look for a bold font, for example.
%
% We can't match external fonts in this way (in \XeTeX\ anyway; todo: test with LuaTeX).
% If \meta{font name tl} is not empty, then it's already been specified by the user so abort.
% If \meta{Base font name} is not given, we also abort for obvious reasons.
%
% If \meta{font name tl} is empty, then proceed.
% If not found, \meta{font name tl} remains empty.
% Otherwise, we have a match.
% \begin{macrocode}
\cs_new:Nn \@@_set_autofont:Nnn
{
\bool_if:NF \l_@@_external_bool
{
\tl_if_empty:eF {#2}
{
\tl_if_empty:NT #1
{
\@@_if_autofont:nnTF {#2} {#3}
{ \tl_set:Nx #1 {#2#3} }
{ \@@_info:nx {no-font-shape} {#2#3} }
}
}
}
}
% \end{macrocode}
%
% \begin{macrocode}
\prg_new_conditional:Nnn \@@_if_autofont:nn {T,TF}
{
\group_begin:
\@@_primitive_font_set:Nnn \l_@@_tmpa_font { \@@_construct_font_call:nn {#1} { \l_@@_pre_feat_sclist } } { \f@size pt + 1sp }
\@@_primitive_font_set:Nnn \l_@@_tmpb_font { \@@_construct_font_call:nn {#1#2} { \l_@@_pre_feat_sclist } } { \f@size pt + 1sp }
\cs_if_eq:NNTF \l_@@_tmpa_font \l_@@_tmpb_font
{ \group_end: \prg_return_false: }
{ \group_end: \prg_return_true: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_make_font_shapes:Nnnnn}
% \darg{Font name}
% \darg{Font series}
% \darg{Font shape}
% \darg{Font features}
% \darg{Size features}
% This macro eventually uses \cs{DeclareFontShape} to define the font shape in
% question.
% \begin{macrocode}
\cs_new:Nn \@@_make_font_shapes:Nnnnn
{
\group_begin:
\@@_keys_set_known:nxN {fontspec-preparse-external} { #4 } \l_@@_leftover_clist
\@@_load_fontname:Nn \l_fontspec_fontname_tl {#1}
\@@_declare_shape:nnxx {#2} {#3} { \l_@@_fontopts_clist, \l_@@_leftover_clist } {#5}
\group_end:
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new:Nn \@@_load_fontname:Nn
{
%<debug> \typeout{:: @@_load_fontname:Nn \exp_not:N #1 (#1) {#2} }
\@@_sanitise_fontname:Nn #1 {#2}
\@@_load_external_fontoptions:N #1
\prop_get:NVNF \g_@@_fontopts_prop #1 \l_@@_fontopts_clist
{ \clist_clear:N \l_@@_fontopts_clist }
\keys_set_groups:nnV {fontspec/fontname} {getfontname} \l_@@_fontopts_clist
\@@_primitive_font_set:OnnF \l_@@_fontface_cs_tl
{ \@@_construct_font_call:nn {#1} { \l_@@_pre_feat_sclist } } { \f@size pt + 2sp }
{ \@@_error:nx {font-not-found} {#2} }
}
% \end{macrocode}
%
% \begin{macrocode}
\keys_define:nn {fontspec/fontname}
{
Font .tl_set:N = \l_fontspec_fontname_tl ,
Font .groups:n = {getfontname} ,
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_declare_shape:nnnn}
% \darg{Font series}
% \darg{Font shape}
% \darg{Font features}
% \darg{Size features}
% Wrapper for \cmd\DeclareFontShape.
% And finally the actual font shape declaration using \cmd\l_@@_nfss_tl\ defined above.
% \cmd\l_@@_postadjust_tl\ is defined in various places to deal with things like the hyphenation
% character and interword spacing.
%
% The main part is to loop through \feat{SizeFeatures} arguments, which are of the form
% {\par\centering |SizeFeatures={{<one>},{<two>},{<three>}}|.\par}
% \begin{macrocode}
\cs_new:Nn \@@_declare_shape:nnnn
{
%<debug>\typeout{=~ declare_shape:~{\l_fontspec_fontname_tl}~{#1}~{#2}}
\tl_build_begin:N \l_@@_nfss_tl
\tl_build_begin:N \l_@@_nfss_sc_tl
\tl_set_eq:NN \l_@@_saved_fontname_tl \l_fontspec_fontname_tl
\exp_args:Nx \clist_map_inline:nn {#4} { \@@_setup_single_size:nn {#3} {##1} }
\tl_build_end:N \l_@@_nfss_tl
\tl_build_end:N \l_@@_nfss_sc_tl
\@@_declare_shapes_normal:nn {#1} {#2}
\@@_declare_shapes_smcaps:nn {#1} {#2}
\@@_declare_shape_slanted:nn {#1} {#2}
\@@_declare_shapes_bx:nn {#1} {#2}
\@@_declare_shape_loginfo:nn {#1} {#2}
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_generate_variant:Nn \@@_declare_shape:nnnn {nnxx}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_setup_single_size:nn}
% \begin{macrocode}
\cs_new:Nn \@@_setup_single_size:nn
{
\tl_clear:N \l_@@_size_tl
\tl_set_eq:NN \l_@@_sizedfont_tl \l_@@_saved_fontname_tl % in case not spec'ed
\keys_set_known:neN {fontspec-sizing} { \exp_after:wN \use:n #2 }
\l_@@_sizing_leftover_clist
\tl_if_empty:NT \l_@@_size_tl { \@@_error:n {no-size-info} }
%<debug>\typeout{==~ size:~\l_@@_size_tl}
% "normal"
\@@_load_fontname:Nn \l_fontspec_fontname_tl {\l_@@_sizedfont_tl}
\@@_setup_nfss:Nn \l_@@_nfss_tl { #1 , \l_@@_sizing_leftover_clist }
%<debug> \typeout{===~ sized~ font:~ \l_@@_sizedfont_tl}
% small caps
\clist_set_eq:NN \l_@@_fontfeat_curr_clist \l_@@_fontfeat_sc_clist
\bool_if:NF \l_@@_nosc_bool
{
\tl_if_empty:NTF \l_@@_fontname_sc_tl
{
\@@_make_smallcaps:TF
{
%<debug>\typeout{====~Small~ caps~ found.}
\clist_put_left:Nn \l_@@_fontfeat_curr_clist {Letters=SmallCaps}
}
{
%<debug>\typeout{====~Small~ caps~ not~ found.}
\bool_set_true:N \l_@@_nosc_bool
}
}
{ \@@_load_fontname:Nn \l_fontspec_fontname_tl {\l_@@_fontname_sc_tl} }% local for each size
}
\bool_if:NF \l_@@_nosc_bool
{
\@@_setup_nfss:Nn \l_@@_nfss_sc_tl
{#1 , \l_@@_sizing_leftover_clist , \l_@@_fontfeat_curr_clist}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_setup_nfss:Nn}
% \begin{macrocode}
\cs_new:Nn \@@_setup_nfss:Nn
{
%<debug>\typeout{====~Setup~NFSS~shape:~<\l_@@_size_tl>~\l_fontspec_fontname_tl}
%<debug>\typeout{====~Requested~features:~#2}
\@@_get_features:n { #2 }
%<debug>\typeout{====~Gathered~features:~\g_@@_rawfeatures_sclist \@@_get_variations:}
\tl_if_empty:NF \l_@@_scale_tl
{
\tl_set:Nx \l_@@_scale_tl { s*[\l_@@_scale_tl] }
}
\tl_build_put_right:Nx #1
{
<\l_@@_size_tl> \l_@@_scale_tl
\@@_construct_font_call:nn { \l_fontspec_fontname_tl }
{ \l_@@_pre_feat_sclist \g_@@_rawfeatures_sclist \@@_get_variations: }
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_declare_shapes_normal:nn}
% \begin{macrocode}
\cs_new:Nn \@@_declare_shapes_normal:nn
{
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl} {\g_@@_nfss_family_tl}
{#1} {#2} {\l_@@_nfss_tl}{\l_@@_postadjust_tl}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_declare_shapes_smcaps:nn}
% \begin{macrocode}
\cs_new:Nn \@@_declare_shapes_smcaps:nn
{
\tl_if_empty:NF \l_@@_nfss_sc_tl
{
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl} {\g_@@_nfss_family_tl} {#1}
{ \@@_combo_sc_shape:n {#2} } {\l_@@_nfss_sc_tl} {\l_@@_postadjust_tl}
}
}
% \end{macrocode}
%
% \begin{macrocode}
\cs_new:Nn \@@_combo_sc_shape:n
{
\tl_if_exist:cTF { \@@_shape_merge:nn {#1} {\scdefault} }
{ \tl_use:c { \@@_shape_merge:nn {#1} {\scdefault} } }
{ \scdefault#1 }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_DeclareFontShape:nnnnnn}
% \begin{macrocode}
\cs_new:Nn \@@_DeclareFontShape:nnnnnn
{
%<debug>\typeout{DeclareFontShape:~{#1}{#2}{#3}{#4}...}
\group_begin:
\normalsize
\cs_undefine:c {#1/#2/#3/#4/\f@size}
\group_end:
\DeclareFontShape{#1}{#2}{#3}{#4}{#5}{#6}
}
\cs_generate_variant:Nn \@@_DeclareFontShape:nnnnnn {xxxxxx}
% \end{macrocode}
%
% \begin{macro}{\@@_declare_shape_slanted:nn}
% This extra stuff for the slanted shape substitution is a little bit awkward.
% We define the slanted shape to be a synonym for it when (a)~we're defining an italic font, but also (b)~when the default slanted shape isn't `it'.
% (Presumably this turned up once in a test and I realised it caused problems. I doubt this would happen much.)
%
% We should test when a slanted font has been specified and not run this code if so, but the \verb|\@@_set_slanted:| code will overwrite this anyway if necessary.
% \begin{macrocode}
\cs_new:Nn \@@_declare_shape_slanted:nn
{
\bool_if:nT
{
\str_if_eq_p:ee {#2} {\itdefault} &&
!(\str_if_eq_p:ee {\itdefault} {\sldefault})
}
{
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl}{\g_@@_nfss_family_tl}{#1}{\sldefault}
{<->ssub*\g_@@_nfss_family_tl/#1/\itdefault}{\l_@@_postadjust_tl}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_declare_shapes_bx:nn}
% Similar processing for setting up b/bx substitutions.
% \begin{macrocode}
\cs_new:Nn \@@_declare_shapes_bx:nn
{
\bool_if:nT
{
\str_if_eq_p:ee {#1} {\bfdefault} &&
!(\str_if_eq_p:ee {\bfdefault} {bx})
}
{
% bx/?
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl} {\g_@@_nfss_family_tl}
{bx} {#2}
{ <->ssub*\g_@@_nfss_family_tl/\bfdefault/#2 }
{ \l_@@_postadjust_tl }
% bx/sc -> b/sc
\tl_if_empty:NF \l_@@_nfss_sc_tl
{
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl} {\g_@@_nfss_family_tl}
{bx} { \@@_combo_sc_shape:n {#2} }
{ <->ssub*\g_@@_nfss_family_tl/\bfdefault/#2 }
{ \l_@@_postadjust_tl }
}
% bx/sl -> bx/it
\bool_if:nT
{
\str_if_eq_p:ee {#2} {\itdefault} &&
!(\str_if_eq_p:ee {\itdefault} {\sldefault})
}
{
\@@_DeclareFontShape:xxxxxx {\g_@@_nfss_enc_tl} {\g_@@_nfss_family_tl}
{bx} {\sldefault}
{ <->ssub*\g_@@_nfss_family_tl/bx/\itdefault }
{ \l_@@_postadjust_tl }
}
}
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_declare_shape_loginfo:nn}
% Lastly some informative messaging.
% \begin{macrocode}
\cs_new:Nn \@@_declare_shape_loginfo:nn
{
\tl_gput_right:Nx \g_@@_defined_shapes_tl
{
\exp_not:n { \\ }
-~ \exp_not:N \str_case:nn {#1/#2}
{
{\mddefault/\shapedefault} {'normal'~}
{\bfdefault/\shapedefault} {'bold'~}
{\mddefault/\itdefault} {'italic'~}
{\mddefault/\sldefault} {'slanted'~}
{\mddefault/\swdefault} {'swash'~}
{\bfdefault/\itdefault} {'bold~ italic'~}
{\bfdefault/\sldefault} {'bold~ slanted'~}
{\bfdefault/\swdefault} {'bold~ swash'~}
} (#1/#2)~
with~ NFSS~ spec.:~
\l_@@_nfss_tl
\tl_if_empty:NF \l_@@_nfss_sc_tl
{
\exp_not:n { \\ }
-~ \exp_not:N \str_case:nn { #1 / \@@_combo_sc_shape:n {#2} }
{
{\mddefault/\scdefault} {'small~ caps'~}
{\bfdefault/\scdefault} {'bold~ small~ caps'~}
{\mddefault/\scitdefault} {'italic~ small~ caps'~}
{\bfdefault/\scitdefault} {'bold~ italic~ small~ caps'~}
{\mddefault/\scsldefault} {'slanted~ small~ caps'~}
{\bfdefault/\scsldefault} {'bold~ slanted~ small~ caps'~}
}~( #1 / \@@_combo_sc_shape:n {#2} )~
with~ NFSS~ spec.:~
\l_@@_nfss_sc_tl
\tl_if_empty:fF {\l_@@_postadjust_tl}
{
\exp_not:N \\ and~ font~ adjustment~ code:
\exp_not:N \\ \l_@@_postadjust_tl
}
}