3838
3939def _get_preamble ():
4040 """Prepare a LaTeX preamble based on the rcParams configuration."""
41- font_size_pt = FontProperties (
42- size = mpl .rcParams ["font.size" ]
43- ).get_size_in_points ()
41+ def _to_fontspec ():
42+ for command , family in [("setmainfont" , "serif" ),
43+ ("setsansfont" , "sans\\ -serif" ),
44+ ("setmonofont" , "monospace" )]:
45+ font_path = fm .findfont (family )
46+ path = pathlib .Path (font_path )
47+ yield r" \%s{%s}[Path=\detokenize{%s/}%s]" % (
48+ command , path .name , path .parent .as_posix (),
49+ f',FontIndex={ font_path .face_index :d} ' if path .suffix == '.ttc' else '' )
50+
51+ font_size_pt = FontProperties (size = mpl .rcParams ["font.size" ]).get_size_in_points ()
4452 return "\n " .join ([
4553 # Remove Matplotlib's custom command \mathdefault. (Not using
4654 # \mathnormal instead since this looks odd with Computer Modern.)
@@ -63,15 +71,8 @@ def _get_preamble():
6371 * ([
6472 r"\ifdefined\pdftexversion\else % non-pdftex case." ,
6573 r" \usepackage{fontspec}" ,
66- ] + [
67- r" \%s{%s}[Path=\detokenize{%s/}]"
68- % (command , path .name , path .parent .as_posix ())
69- for command , path in zip (
70- ["setmainfont" , "setsansfont" , "setmonofont" ],
71- [pathlib .Path (fm .findfont (family ))
72- for family in ["serif" , "sans\\ -serif" , "monospace" ]]
73- )
74- ] + [r"\fi" ] if mpl .rcParams ["pgf.rcfonts" ] else []),
74+ * _to_fontspec (),
75+ r"\fi" ] if mpl .rcParams ["pgf.rcfonts" ] else []),
7576 # Documented as "must come last".
7677 mpl .texmanager ._usepackage_if_not_loaded ("underscore" , option = "strings" ),
7778 ])
0 commit comments