加载多种字体并设置备用字体

加载多种字体并设置备用字体

基于这个答案,我尝试加载主字体和无衬线字体,并在字体不存在时为两者提供后备:

\newif\ifgoodfont
\makeatletter
\ExplSyntaxOn
\cs_set:Nn \__fontspec_load_font:
{
  \__fontspec_font_set:Nnn    \l_fontspec_font
    { \__fontspec_fullname:n {\l_fontspec_fontname_up_tl} } {\f@size pt}
  \__fontspec_font_if_null:NT \l_fontspec_font { 
\global\goodfontfalse
\__fontspec_warning:nx {font-not-found} {\l_fontspec_fontname_up_tl} }
  \__fontspec_set_font_type:
  \__fontspec_font_gset:Nnn   \l_fontspec_font
    { \__fontspec_fullname:n {\l_fontspec_fontname_up_tl} } {\f@size pt}
  \l_fontspec_font % this is necessary for LuaLaTeX to check the scripts properly
}

\cs_set:Nn \__fontspec_load_fontname:n
{
    \__fontspec_load_external_fontoptions:Nn \l_fontspec_fontname_tl {#1}
    \prop_get:NVNF \g__fontspec_fontopts_prop \l_fontspec_fontname_tl \l__fontspec_fontopts_clist
    { \clist_clear:N \l__fontspec_fontopts_clist }
    \__fontspec_font_set:Nnn \l_fontspec_font {\__fontspec_fullname:n {\l_fontspec_fontname_tl}} {\f@size pt}
    \__fontspec_font_if_null:NT \l_fontspec_font { 
\global\goodfontfalse
\__fontspec_warning:nx {font-not-found} {#1} }
}
\ExplSyntaxOff
\makeatother

{\scrollmode\global\goodfonttrue
\setmainfont{Equity Text A}[SmallCapsFont={Equity Caps A}]
}
\ifgoodfont
  \typeout{Using Equity as main font}
\else
  \global\goodfonttrue
  \typeout{Equity not found, trying Libertine...}
  \setmainfont{Linux Libertine O}
  \ifgoodfont
    \typeout{OK}
  \else
    \typeout{Libertine also not found. Using default.}
  \fi
\fi

{\scrollmode\global\goodfonttrue
\setsansfont{Concourse T3}[SmallCapsFont={Concourse C3}]
}
\ifgoodfont
  \typeout{Using Concourse as sans serif font}
\else
  \global\goodfonttrue
  \typeout{Concourse not found, trying TeX Gyre Heros...}
  \setsansfont{TeX Gyre Heros}[Scale=MatchLowercase]
  \ifgoodfont
    \typeout{OK}
  \else
    \typeout{TeX Gyre Heros also not found. Using default.}
  \fi
\fi

但是,实际上只有第二种字体被加载。如果我删除第二种字体的代码,第一种字体就会被加载。我怎样才能让两种字体都起作用?

相关内容