LuaLaTeX:适用于超级系列的更智能的字体选择宏

LuaLaTeX:适用于超级系列的更智能的字体选择宏

注意:这个问题还没有答案。用户 Davislor 对此问题的唯一“答案”并没有回答这个问题,尽管它有赞成票,看起来好像是。如果您有真正的解决方案,请随时回答。

Fontspec 和 LuaLaTeX 适用于小型字体系列,但用于超系列时似乎很麻烦。现代超系列可以在字重/字宽/斜率 (WWS) 矩阵中包含数十种字体。例如,Avenir Next 一个系列中有 32 种字体。

Opentype 字体表包含可用于准确选择超级系列中的字体的字段:1) WWS 系列名称,2) WWS 子系列名称。因此,理想情况下,应该能够通过仅指定系列名称来设置系列,并且可以通过仅指定子系列名称来选择子系列。有没有 LuaLaTeX 方法来做到这一点?

注 1):此子系列名称并不总是字体“全名”的子字符串,因此您不能使用字符串连接技巧并使用 fontspec:)

注 2) 如果字体中缺少 WWS 字段,则解析字体的适当字段为:1) 字体系列名称,以及 2) 字体子系列名称。因此,某处将需要一个 if-else 语句。

\fontfamily{Avenir Next LT Pro}
.
.
.
Hello world {\fstyle{Ultra Light Italic} Hello world} Hello world


以下是一些相关截图:

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案1

您可以使用大量选项列表来执行此操作FontFace=,最好在.fontspec.sty文件中。我没有特定的字体可以测试,所以我下载了此版本的 Noto Serif到名为 的子目录中fonts。以下是示例:

\documentclass{article}
\usepackage{fontspec}
\usepackage{parskip}
\usepackage[paperwidth=10cm]{geometry}

\pagestyle{empty}

\defaultfontfeatures[NotoSerif]{
  Path=fonts/,
  Extension=.ttf,
  FontFace={ul}{n}{Font={*-Thin}},
  FontFace={ul}{it}{Font={*-ThinItalic}},
  FontFace={el}{n}{Font={*-ExtraLight}},
  FontFace={el}{it}{Font={*-ExtraLightItalic}},
  FontFace={l}{n}{Font={*-Light}},
  FontFace={l}{it}{Font={*-LightItalic}},
  UprightFont=*-Regular,
  ItalicFont=*-Italic,
  FontFace={mb}{n}{Font={*-Medium}},
  FontFace={mb}{it}{Font={*-MediumItalic}},
  FontFace={sb}{n}{Font={*-SemiBold}},
  FontFace={sb}{it}{Font={*-SemiBoldItalic}},
  BoldFont=*-Bold,
  BoldItalicFont=*-BoldItalic,
  FontFace={eb}{n}{Font={*-ExtraBold}},
  FontFace={eb}{it}{Font={*-ExtraBoldItalic}},
  FontFace={ub}{n}{Font={*-Black}},
  FontFace={ub}{it}{Font={*-BlackItalic}},
  FontFace={uluc}{n}{Font={*-ExtraCondensedThin}},
  FontFace={uluc}{it}{Font={*-ExtraCondensedThinItalic}},
  FontFace={eluc}{n}{Font={*-ExtraCondensedExtraLight}},
  FontFace={eluc}{it}{Font={*-ExtraCondensedExtraLightItalic}},
  FontFace={luc}{n}{Font={*-ExtraCondensedLight}},
  FontFace={luc}{it}{Font={*-ExtraCondensedLightItalic}},
  FontFace={uc}{n}{Font={*-ExtraCondensed}},
  FontFace={uc}{it}{Font={*-ExtraCondensedItalic}},
  FontFace={mbuc}{n}{Font={*-ExtraCondensedMedium}},
  FontFace={mbuc}{it}{Font={*-ExtraCondensedMediumItalic}},
  FontFace={sbuc}{n}{Font={*-ExtraCondensedSemiBold}},
  FontFace={sbuc}{it}{Font={*-ExtraCondensedSemiBoldItalic}},
  FontFace={buc}{n}{Font={*-ExtraCondensedBold}},
  FontFace={buc}{it}{Font={*-ExtraCondensedBoldItalic}},
  FontFace={ebuc}{n}{Font={*-ExtraCondensedExtraBold}},
  FontFace={ebuc}{it}{Font={*-ExtraCondensedExtraBoldItalic}},
  FontFace={ubuc}{n}{Font={*-ExtraCondensedBlack}},
  FontFace={ubuc}{it}{Font={*-ExtraCondensedBlackItalic}},
  FontFace={ulec}{n}{Font={*-CondensedThin}},
  FontFace={ulec}{it}{Font={*-CondensedThinItalic}},
  FontFace={elec}{n}{Font={*-CondensedExtraLight}},
  FontFace={elec}{it}{Font={*-CondensedExtraLightItalic}},
  FontFace={lec}{n}{Font={*-CondensedLight}},
  FontFace={lec}{it}{Font={*-CondensedLightItalic}},
  FontFace={ec}{n}{Font={*-Condensed}},
  FontFace={ec}{it}{Font={*-CondensedItalic}},
  FontFace={mbec}{n}{Font={*-CondensedMedium}},
  FontFace={mbec}{it}{Font={*-CondensedMediumItalic}},
  FontFace={sbec}{n}{Font={*-CondensedSemiBold}},
  FontFace={sbec}{it}{Font={*-CondensedSemiBoldItalic}},
  FontFace={bec}{n}{Font={*-CondensedBold}},
  FontFace={bec}{it}{Font={*-CondensedBoldItalic}},
  FontFace={ebec}{n}{Font={*-CondensedExtraBold}},
  FontFace={ebec}{it}{Font={*-CondensedExtraBoldItalic}},
  FontFace={ubec}{n}{Font={*-CondensedBlack}},
  FontFace={ubec}{it}{Font={*-CondensedBlackItalic}},
  FontFace={ulc}{n}{Font={*-SemiCondensedThin}},
  FontFace={ulc}{it}{Font={*-SemiCondensedThinItalic}},
  FontFace={elc}{n}{Font={*-SemiCondensedExtraLight}},
  FontFace={elc}{it}{Font={*-SemiCondensedExtraLightItalic}},
  FontFace={lc}{n}{Font={*-SemiCondensedLight}},
  FontFace={lc}{it}{Font={*-SemiCondensedLightItalic}},
  FontFace={c}{n}{Font={*-SemiCondensed}},
  FontFace={c}{it}{Font={*-SemiCondensedItalic}},
  FontFace={mbc}{n}{Font={*-SemiCondensedMedium}},
  FontFace={mbc}{it}{Font={*-SemiCondensedMediumItalic}},
  FontFace={sbc}{n}{Font={*-SemiCondensedSemiBold}},
  FontFace={sbc}{it}{Font={*-SemiCondensedSemiBoldItalic}},
  FontFace={bc}{n}{Font={*-SemiCondensedBold}},
  FontFace={bc}{it}{Font={*-SemiCondensedBoldItalic}},
  FontFace={ebc}{n}{Font={*-SemiCondensedExtraBold}},
  FontFace={ebc}{it}{Font={*-SemiCondensedExtraBoldItalic}},
  FontFace={ubc}{n}{Font={*-SemiCondensedBlack}},
  FontFace={ubc}{it}{Font={*-SemiCondensedBlackItalic}}
}

\setmainfont{NotoSerif}

\makeatletter
\edef\current@weight{m}
\edef\current@extent{}

\DeclareRobustCommand\fontweight[1]{%
  \edef\current@weight{#1}%
  \update@series%
}

\DeclareRobustCommand\fontextent[1]{%
  \edef\current@extent{#1}%
  \update@series%
}

\DeclareRobustCommand\update@series{%
  \expandafter\ifx\current@extent\relax%
    \fontseries{\current@weight}%
  \else%
    \ifx\current@weight m%
      \fontseries{\current@extent}%
    \else%
      \fontseries{\current@weight\current@extent}%
    \fi%
  \fi%
}
\makeatother

\newcommand\ulweight{\fontweight{ul}\selectfont}
\newcommand\elweight{\fontweight{el}\selectfont}
\newcommand\ltweight{\fontweight{l}\selectfont}
\newcommand\mdweight{\fontweight{m}\selectfont}
\newcommand\mbweight{\fontweight{mb}\selectfont}
\newcommand\sbweight{\fontweight{sb}\selectfont}
\newcommand\bfweight{\fontweight{b}\selectfont}
\newcommand\ebweight{\fontweight{eb}\selectfont}
\newcommand\ubweight{\fontweight{ub}\selectfont}
\newcommand\ucwidth{\fontextent{uc}\selectfont}
\newcommand\ecwidth{\fontextent{ec}\selectfont}
\newcommand\cdwidth{\fontextent{c}\selectfont}

\begin{document}
\mdweight Noto Serif \textit{Italic}
\ulweight Thin \textit{Italic}
\elweight ExtraLight \textit{Italic}
\ltweight Light \textit{Italic}
\mbweight Medium \textit{Italic}
\sbweight Semibold \textit{Italic}
\bfweight Bold \textit{Italic}
\ebweight Extra-Bold \textit{Italic}
\ubweight Heavy \textit{Italic}

\ucwidth
\mdweight Noto Serif Extra-Condensed \textit{Italic}
\ulweight Thin \textit{Italic}
\elweight ExtraLight \textit{Italic}
\ltweight Light \textit{Italic}
\mbweight Medium \textit{Italic}
\sbweight Semibold \textit{Italic}
\bfweight Bold \textit{Italic}
\ebweight Extra-Bold \textit{Italic}
\ubweight Heavy \textit{Italic}

\ecwidth
\mdweight Noto Serif Condensed \textit{Italic}
\ulweight Thin \textit{Italic}
\elweight ExtraLight \textit{Italic}
\ltweight Light \textit{Italic}
\mbweight Medium \textit{Italic}
\sbweight Semibold \textit{Italic}
\bfweight Bold \textit{Italic}
\ebweight Extra-Bold \textit{Italic}
\ubweight Heavy \textit{Italic}

\cdwidth
\mdweight Noto Serif Semi-Condensed \textit{Italic}
\ulweight Thin \textit{Italic}
\elweight ExtraLight \textit{Italic}
\ltweight Light \textit{Italic}
\mbweight Medium \textit{Italic}
\sbweight Semibold \textit{Italic}
\bfweight Bold \textit{Italic}
\ebweight Extra-Bold \textit{Italic}
\ubweight Heavy \textit{Italic}
\end{document}

Noto Serif 示例

我重新发明了轮子:nfssext-cfr包裹@cfr 还定义了分别设置重量和宽度的命令,我使用了类似的名称。如果您还需要对应于\textit\textup等的命令,您可以使用\DeclareTextFontCommand\textul{\ulwidth}等来定义它们。

您可能希望重新定义标准\bfseries\mdseries等等,以便用粗体压缩代替\bfseries\cdseries\bfweight\cdwidth这是斜体小写字母的处理方式fontaxesfontspec\itshape\scshape避免在这里覆盖标准命令,但这意味着它们与新命令不兼容。

与大多数软件包一样,我使用了来自LaTeX 字体安装指南,但请注意,fontspec允许您为系列和形状赋予任意名称。没有理由您无法设置\fontweight{Semibold}\fontextent{Condensed}\selectfont或 来工作\fontseries{SemiboldCondensed}\selectfont。只需声明FontFace{SemiboldCondensed}{n}而不是FontFace{sbc}{n}

要启用诸如支持光学尺寸或可变字体等选项,请将它们添加到 后的括号内Font=,例如FontFace{ebc}{n}{ Font={*-ExtraBoldCondensed}, SizeFeatures={...} }。请参阅手册fontspec满足您所需的功能。

LuaTeX 1.13 及更高版本支持可变字体也一样。

如果您只想在代码中使用人类可读的名称,您可以尝试类似\fontspec{Avenir Next LT Pro Medium Condensed}. 但是,不推荐使用通过显示名称加载字体。

相关内容